xref: /dpdk/examples/pipeline/obj.c (revision 66440b7b22f226e5277368722c6f0ef35d51fa5a)
1b77f6600SCristian Dumitrescu /* SPDX-License-Identifier: BSD-3-Clause
2b77f6600SCristian Dumitrescu  * Copyright(c) 2020 Intel Corporation
3b77f6600SCristian Dumitrescu  */
4b77f6600SCristian Dumitrescu 
5b77f6600SCristian Dumitrescu #include <stdlib.h>
6b77f6600SCristian Dumitrescu #include <string.h>
7b77f6600SCristian Dumitrescu 
8b77f6600SCristian Dumitrescu #include <rte_mempool.h>
9b77f6600SCristian Dumitrescu #include <rte_mbuf.h>
10b77f6600SCristian Dumitrescu #include <rte_ethdev.h>
11b77f6600SCristian Dumitrescu #include <rte_swx_port_ethdev.h>
1277a41301SCristian Dumitrescu #include <rte_swx_port_ring.h>
13b77f6600SCristian Dumitrescu #include <rte_swx_port_source_sink.h>
14b77f6600SCristian Dumitrescu #include <rte_swx_table_em.h>
15*66440b7bSCristian Dumitrescu #include <rte_swx_table_wm.h>
16b77f6600SCristian Dumitrescu #include <rte_swx_pipeline.h>
17b77f6600SCristian Dumitrescu #include <rte_swx_ctl.h>
18b77f6600SCristian Dumitrescu 
19b77f6600SCristian Dumitrescu #include "obj.h"
20b77f6600SCristian Dumitrescu 
21b77f6600SCristian Dumitrescu /*
22b77f6600SCristian Dumitrescu  * mempool
23b77f6600SCristian Dumitrescu  */
24b77f6600SCristian Dumitrescu TAILQ_HEAD(mempool_list, mempool);
25b77f6600SCristian Dumitrescu 
26b77f6600SCristian Dumitrescu /*
27b77f6600SCristian Dumitrescu  * link
28b77f6600SCristian Dumitrescu  */
29b77f6600SCristian Dumitrescu TAILQ_HEAD(link_list, link);
30b77f6600SCristian Dumitrescu 
31b77f6600SCristian Dumitrescu /*
3277a41301SCristian Dumitrescu  * ring
3377a41301SCristian Dumitrescu  */
3477a41301SCristian Dumitrescu TAILQ_HEAD(ring_list, ring);
3577a41301SCristian Dumitrescu 
3677a41301SCristian Dumitrescu /*
37b77f6600SCristian Dumitrescu  * pipeline
38b77f6600SCristian Dumitrescu  */
39b77f6600SCristian Dumitrescu TAILQ_HEAD(pipeline_list, pipeline);
40b77f6600SCristian Dumitrescu 
41b77f6600SCristian Dumitrescu /*
42b77f6600SCristian Dumitrescu  * obj
43b77f6600SCristian Dumitrescu  */
44b77f6600SCristian Dumitrescu struct obj {
45b77f6600SCristian Dumitrescu 	struct mempool_list mempool_list;
46b77f6600SCristian Dumitrescu 	struct link_list link_list;
4777a41301SCristian Dumitrescu 	struct ring_list ring_list;
48b77f6600SCristian Dumitrescu 	struct pipeline_list pipeline_list;
49b77f6600SCristian Dumitrescu };
50b77f6600SCristian Dumitrescu 
51b77f6600SCristian Dumitrescu /*
52b77f6600SCristian Dumitrescu  * mempool
53b77f6600SCristian Dumitrescu  */
54b77f6600SCristian Dumitrescu #define BUFFER_SIZE_MIN (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
55b77f6600SCristian Dumitrescu 
56b77f6600SCristian Dumitrescu struct mempool *
57b77f6600SCristian Dumitrescu mempool_create(struct obj *obj, const char *name, struct mempool_params *params)
58b77f6600SCristian Dumitrescu {
59b77f6600SCristian Dumitrescu 	struct mempool *mempool;
60b77f6600SCristian Dumitrescu 	struct rte_mempool *m;
61b77f6600SCristian Dumitrescu 
62b77f6600SCristian Dumitrescu 	/* Check input params */
63b77f6600SCristian Dumitrescu 	if ((name == NULL) ||
64b77f6600SCristian Dumitrescu 		mempool_find(obj, name) ||
65b77f6600SCristian Dumitrescu 		(params == NULL) ||
66b77f6600SCristian Dumitrescu 		(params->buffer_size < BUFFER_SIZE_MIN) ||
67b77f6600SCristian Dumitrescu 		(params->pool_size == 0))
68b77f6600SCristian Dumitrescu 		return NULL;
69b77f6600SCristian Dumitrescu 
70b77f6600SCristian Dumitrescu 	/* Resource create */
71b77f6600SCristian Dumitrescu 	m = rte_pktmbuf_pool_create(
72b77f6600SCristian Dumitrescu 		name,
73b77f6600SCristian Dumitrescu 		params->pool_size,
74b77f6600SCristian Dumitrescu 		params->cache_size,
75b77f6600SCristian Dumitrescu 		0,
76b77f6600SCristian Dumitrescu 		params->buffer_size - sizeof(struct rte_mbuf),
77b77f6600SCristian Dumitrescu 		params->cpu_id);
78b77f6600SCristian Dumitrescu 
79b77f6600SCristian Dumitrescu 	if (m == NULL)
80b77f6600SCristian Dumitrescu 		return NULL;
81b77f6600SCristian Dumitrescu 
82b77f6600SCristian Dumitrescu 	/* Node allocation */
83b77f6600SCristian Dumitrescu 	mempool = calloc(1, sizeof(struct mempool));
84b77f6600SCristian Dumitrescu 	if (mempool == NULL) {
85b77f6600SCristian Dumitrescu 		rte_mempool_free(m);
86b77f6600SCristian Dumitrescu 		return NULL;
87b77f6600SCristian Dumitrescu 	}
88b77f6600SCristian Dumitrescu 
89b77f6600SCristian Dumitrescu 	/* Node fill in */
90b77f6600SCristian Dumitrescu 	strlcpy(mempool->name, name, sizeof(mempool->name));
91b77f6600SCristian Dumitrescu 	mempool->m = m;
92b77f6600SCristian Dumitrescu 	mempool->buffer_size = params->buffer_size;
93b77f6600SCristian Dumitrescu 
94b77f6600SCristian Dumitrescu 	/* Node add to list */
95b77f6600SCristian Dumitrescu 	TAILQ_INSERT_TAIL(&obj->mempool_list, mempool, node);
96b77f6600SCristian Dumitrescu 
97b77f6600SCristian Dumitrescu 	return mempool;
98b77f6600SCristian Dumitrescu }
99b77f6600SCristian Dumitrescu 
100b77f6600SCristian Dumitrescu struct mempool *
101b77f6600SCristian Dumitrescu mempool_find(struct obj *obj, const char *name)
102b77f6600SCristian Dumitrescu {
103b77f6600SCristian Dumitrescu 	struct mempool *mempool;
104b77f6600SCristian Dumitrescu 
105b77f6600SCristian Dumitrescu 	if (!obj || !name)
106b77f6600SCristian Dumitrescu 		return NULL;
107b77f6600SCristian Dumitrescu 
108b77f6600SCristian Dumitrescu 	TAILQ_FOREACH(mempool, &obj->mempool_list, node)
109b77f6600SCristian Dumitrescu 		if (strcmp(mempool->name, name) == 0)
110b77f6600SCristian Dumitrescu 			return mempool;
111b77f6600SCristian Dumitrescu 
112b77f6600SCristian Dumitrescu 	return NULL;
113b77f6600SCristian Dumitrescu }
114b77f6600SCristian Dumitrescu 
115b77f6600SCristian Dumitrescu /*
116b77f6600SCristian Dumitrescu  * link
117b77f6600SCristian Dumitrescu  */
118b77f6600SCristian Dumitrescu static struct rte_eth_conf port_conf_default = {
119b77f6600SCristian Dumitrescu 	.link_speeds = 0,
120b77f6600SCristian Dumitrescu 	.rxmode = {
121b77f6600SCristian Dumitrescu 		.mq_mode = ETH_MQ_RX_NONE,
122b77f6600SCristian Dumitrescu 		.max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
123b77f6600SCristian Dumitrescu 		.split_hdr_size = 0, /* Header split buffer size */
124b77f6600SCristian Dumitrescu 	},
125b77f6600SCristian Dumitrescu 	.rx_adv_conf = {
126b77f6600SCristian Dumitrescu 		.rss_conf = {
127b77f6600SCristian Dumitrescu 			.rss_key = NULL,
128b77f6600SCristian Dumitrescu 			.rss_key_len = 40,
129b77f6600SCristian Dumitrescu 			.rss_hf = 0,
130b77f6600SCristian Dumitrescu 		},
131b77f6600SCristian Dumitrescu 	},
132b77f6600SCristian Dumitrescu 	.txmode = {
133b77f6600SCristian Dumitrescu 		.mq_mode = ETH_MQ_TX_NONE,
134b77f6600SCristian Dumitrescu 	},
135b77f6600SCristian Dumitrescu 	.lpbk_mode = 0,
136b77f6600SCristian Dumitrescu };
137b77f6600SCristian Dumitrescu 
138b77f6600SCristian Dumitrescu #define RETA_CONF_SIZE     (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
139b77f6600SCristian Dumitrescu 
140b77f6600SCristian Dumitrescu static int
141b77f6600SCristian Dumitrescu rss_setup(uint16_t port_id,
142b77f6600SCristian Dumitrescu 	uint16_t reta_size,
143b77f6600SCristian Dumitrescu 	struct link_params_rss *rss)
144b77f6600SCristian Dumitrescu {
145b77f6600SCristian Dumitrescu 	struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
146b77f6600SCristian Dumitrescu 	uint32_t i;
147b77f6600SCristian Dumitrescu 	int status;
148b77f6600SCristian Dumitrescu 
149b77f6600SCristian Dumitrescu 	/* RETA setting */
150b77f6600SCristian Dumitrescu 	memset(reta_conf, 0, sizeof(reta_conf));
151b77f6600SCristian Dumitrescu 
152b77f6600SCristian Dumitrescu 	for (i = 0; i < reta_size; i++)
153b77f6600SCristian Dumitrescu 		reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
154b77f6600SCristian Dumitrescu 
155b77f6600SCristian Dumitrescu 	for (i = 0; i < reta_size; i++) {
156b77f6600SCristian Dumitrescu 		uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
157b77f6600SCristian Dumitrescu 		uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
158b77f6600SCristian Dumitrescu 		uint32_t rss_qs_pos = i % rss->n_queues;
159b77f6600SCristian Dumitrescu 
160b77f6600SCristian Dumitrescu 		reta_conf[reta_id].reta[reta_pos] =
161b77f6600SCristian Dumitrescu 			(uint16_t) rss->queue_id[rss_qs_pos];
162b77f6600SCristian Dumitrescu 	}
163b77f6600SCristian Dumitrescu 
164b77f6600SCristian Dumitrescu 	/* RETA update */
165b77f6600SCristian Dumitrescu 	status = rte_eth_dev_rss_reta_update(port_id,
166b77f6600SCristian Dumitrescu 		reta_conf,
167b77f6600SCristian Dumitrescu 		reta_size);
168b77f6600SCristian Dumitrescu 
169b77f6600SCristian Dumitrescu 	return status;
170b77f6600SCristian Dumitrescu }
171b77f6600SCristian Dumitrescu 
172b77f6600SCristian Dumitrescu struct link *
173b77f6600SCristian Dumitrescu link_create(struct obj *obj, const char *name, struct link_params *params)
174b77f6600SCristian Dumitrescu {
175b77f6600SCristian Dumitrescu 	struct rte_eth_dev_info port_info;
176b77f6600SCristian Dumitrescu 	struct rte_eth_conf port_conf;
177b77f6600SCristian Dumitrescu 	struct link *link;
178b77f6600SCristian Dumitrescu 	struct link_params_rss *rss;
179b77f6600SCristian Dumitrescu 	struct mempool *mempool;
180b77f6600SCristian Dumitrescu 	uint32_t cpu_id, i;
181b77f6600SCristian Dumitrescu 	int status;
182b77f6600SCristian Dumitrescu 	uint16_t port_id;
183b77f6600SCristian Dumitrescu 
184b77f6600SCristian Dumitrescu 	/* Check input params */
185b77f6600SCristian Dumitrescu 	if ((name == NULL) ||
186b77f6600SCristian Dumitrescu 		link_find(obj, name) ||
187b77f6600SCristian Dumitrescu 		(params == NULL) ||
188b77f6600SCristian Dumitrescu 		(params->rx.n_queues == 0) ||
189b77f6600SCristian Dumitrescu 		(params->rx.queue_size == 0) ||
190b77f6600SCristian Dumitrescu 		(params->tx.n_queues == 0) ||
191b77f6600SCristian Dumitrescu 		(params->tx.queue_size == 0))
192b77f6600SCristian Dumitrescu 		return NULL;
193b77f6600SCristian Dumitrescu 
194b77f6600SCristian Dumitrescu 	port_id = params->port_id;
195b77f6600SCristian Dumitrescu 	if (params->dev_name) {
196b77f6600SCristian Dumitrescu 		status = rte_eth_dev_get_port_by_name(params->dev_name,
197b77f6600SCristian Dumitrescu 			&port_id);
198b77f6600SCristian Dumitrescu 
199b77f6600SCristian Dumitrescu 		if (status)
200b77f6600SCristian Dumitrescu 			return NULL;
201b77f6600SCristian Dumitrescu 	} else
202b77f6600SCristian Dumitrescu 		if (!rte_eth_dev_is_valid_port(port_id))
203b77f6600SCristian Dumitrescu 			return NULL;
204b77f6600SCristian Dumitrescu 
205b77f6600SCristian Dumitrescu 	if (rte_eth_dev_info_get(port_id, &port_info) != 0)
206b77f6600SCristian Dumitrescu 		return NULL;
207b77f6600SCristian Dumitrescu 
208b77f6600SCristian Dumitrescu 	mempool = mempool_find(obj, params->rx.mempool_name);
209b77f6600SCristian Dumitrescu 	if (mempool == NULL)
210b77f6600SCristian Dumitrescu 		return NULL;
211b77f6600SCristian Dumitrescu 
212b77f6600SCristian Dumitrescu 	rss = params->rx.rss;
213b77f6600SCristian Dumitrescu 	if (rss) {
214b77f6600SCristian Dumitrescu 		if ((port_info.reta_size == 0) ||
215b77f6600SCristian Dumitrescu 			(port_info.reta_size > ETH_RSS_RETA_SIZE_512))
216b77f6600SCristian Dumitrescu 			return NULL;
217b77f6600SCristian Dumitrescu 
218b77f6600SCristian Dumitrescu 		if ((rss->n_queues == 0) ||
219b77f6600SCristian Dumitrescu 			(rss->n_queues >= LINK_RXQ_RSS_MAX))
220b77f6600SCristian Dumitrescu 			return NULL;
221b77f6600SCristian Dumitrescu 
222b77f6600SCristian Dumitrescu 		for (i = 0; i < rss->n_queues; i++)
223b77f6600SCristian Dumitrescu 			if (rss->queue_id[i] >= port_info.max_rx_queues)
224b77f6600SCristian Dumitrescu 				return NULL;
225b77f6600SCristian Dumitrescu 	}
226b77f6600SCristian Dumitrescu 
227b77f6600SCristian Dumitrescu 	/**
228b77f6600SCristian Dumitrescu 	 * Resource create
229b77f6600SCristian Dumitrescu 	 */
230b77f6600SCristian Dumitrescu 	/* Port */
231b77f6600SCristian Dumitrescu 	memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
232b77f6600SCristian Dumitrescu 	if (rss) {
233b77f6600SCristian Dumitrescu 		port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
234b77f6600SCristian Dumitrescu 		port_conf.rx_adv_conf.rss_conf.rss_hf =
235b77f6600SCristian Dumitrescu 			(ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) &
236b77f6600SCristian Dumitrescu 			port_info.flow_type_rss_offloads;
237b77f6600SCristian Dumitrescu 	}
238b77f6600SCristian Dumitrescu 
239b77f6600SCristian Dumitrescu 	cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
240b77f6600SCristian Dumitrescu 	if (cpu_id == (uint32_t) SOCKET_ID_ANY)
241b77f6600SCristian Dumitrescu 		cpu_id = 0;
242b77f6600SCristian Dumitrescu 
243b77f6600SCristian Dumitrescu 	status = rte_eth_dev_configure(
244b77f6600SCristian Dumitrescu 		port_id,
245b77f6600SCristian Dumitrescu 		params->rx.n_queues,
246b77f6600SCristian Dumitrescu 		params->tx.n_queues,
247b77f6600SCristian Dumitrescu 		&port_conf);
248b77f6600SCristian Dumitrescu 
249b77f6600SCristian Dumitrescu 	if (status < 0)
250b77f6600SCristian Dumitrescu 		return NULL;
251b77f6600SCristian Dumitrescu 
252b77f6600SCristian Dumitrescu 	if (params->promiscuous) {
253b77f6600SCristian Dumitrescu 		status = rte_eth_promiscuous_enable(port_id);
254b77f6600SCristian Dumitrescu 		if (status != 0)
255b77f6600SCristian Dumitrescu 			return NULL;
256b77f6600SCristian Dumitrescu 	}
257b77f6600SCristian Dumitrescu 
258b77f6600SCristian Dumitrescu 	/* Port RX */
259b77f6600SCristian Dumitrescu 	for (i = 0; i < params->rx.n_queues; i++) {
260b77f6600SCristian Dumitrescu 		status = rte_eth_rx_queue_setup(
261b77f6600SCristian Dumitrescu 			port_id,
262b77f6600SCristian Dumitrescu 			i,
263b77f6600SCristian Dumitrescu 			params->rx.queue_size,
264b77f6600SCristian Dumitrescu 			cpu_id,
265b77f6600SCristian Dumitrescu 			NULL,
266b77f6600SCristian Dumitrescu 			mempool->m);
267b77f6600SCristian Dumitrescu 
268b77f6600SCristian Dumitrescu 		if (status < 0)
269b77f6600SCristian Dumitrescu 			return NULL;
270b77f6600SCristian Dumitrescu 	}
271b77f6600SCristian Dumitrescu 
272b77f6600SCristian Dumitrescu 	/* Port TX */
273b77f6600SCristian Dumitrescu 	for (i = 0; i < params->tx.n_queues; i++) {
274b77f6600SCristian Dumitrescu 		status = rte_eth_tx_queue_setup(
275b77f6600SCristian Dumitrescu 			port_id,
276b77f6600SCristian Dumitrescu 			i,
277b77f6600SCristian Dumitrescu 			params->tx.queue_size,
278b77f6600SCristian Dumitrescu 			cpu_id,
279b77f6600SCristian Dumitrescu 			NULL);
280b77f6600SCristian Dumitrescu 
281b77f6600SCristian Dumitrescu 		if (status < 0)
282b77f6600SCristian Dumitrescu 			return NULL;
283b77f6600SCristian Dumitrescu 	}
284b77f6600SCristian Dumitrescu 
285b77f6600SCristian Dumitrescu 	/* Port start */
286b77f6600SCristian Dumitrescu 	status = rte_eth_dev_start(port_id);
287b77f6600SCristian Dumitrescu 	if (status < 0)
288b77f6600SCristian Dumitrescu 		return NULL;
289b77f6600SCristian Dumitrescu 
290b77f6600SCristian Dumitrescu 	if (rss) {
291b77f6600SCristian Dumitrescu 		status = rss_setup(port_id, port_info.reta_size, rss);
292b77f6600SCristian Dumitrescu 
293b77f6600SCristian Dumitrescu 		if (status) {
294b77f6600SCristian Dumitrescu 			rte_eth_dev_stop(port_id);
295b77f6600SCristian Dumitrescu 			return NULL;
296b77f6600SCristian Dumitrescu 		}
297b77f6600SCristian Dumitrescu 	}
298b77f6600SCristian Dumitrescu 
299b77f6600SCristian Dumitrescu 	/* Port link up */
300b77f6600SCristian Dumitrescu 	status = rte_eth_dev_set_link_up(port_id);
301b77f6600SCristian Dumitrescu 	if ((status < 0) && (status != -ENOTSUP)) {
302b77f6600SCristian Dumitrescu 		rte_eth_dev_stop(port_id);
303b77f6600SCristian Dumitrescu 		return NULL;
304b77f6600SCristian Dumitrescu 	}
305b77f6600SCristian Dumitrescu 
306b77f6600SCristian Dumitrescu 	/* Node allocation */
307b77f6600SCristian Dumitrescu 	link = calloc(1, sizeof(struct link));
308b77f6600SCristian Dumitrescu 	if (link == NULL) {
309b77f6600SCristian Dumitrescu 		rte_eth_dev_stop(port_id);
310b77f6600SCristian Dumitrescu 		return NULL;
311b77f6600SCristian Dumitrescu 	}
312b77f6600SCristian Dumitrescu 
313b77f6600SCristian Dumitrescu 	/* Node fill in */
314b77f6600SCristian Dumitrescu 	strlcpy(link->name, name, sizeof(link->name));
315b77f6600SCristian Dumitrescu 	link->port_id = port_id;
316b77f6600SCristian Dumitrescu 	rte_eth_dev_get_name_by_port(port_id, link->dev_name);
317b77f6600SCristian Dumitrescu 	link->n_rxq = params->rx.n_queues;
318b77f6600SCristian Dumitrescu 	link->n_txq = params->tx.n_queues;
319b77f6600SCristian Dumitrescu 
320b77f6600SCristian Dumitrescu 	/* Node add to list */
321b77f6600SCristian Dumitrescu 	TAILQ_INSERT_TAIL(&obj->link_list, link, node);
322b77f6600SCristian Dumitrescu 
323b77f6600SCristian Dumitrescu 	return link;
324b77f6600SCristian Dumitrescu }
325b77f6600SCristian Dumitrescu 
326b77f6600SCristian Dumitrescu int
327b77f6600SCristian Dumitrescu link_is_up(struct obj *obj, const char *name)
328b77f6600SCristian Dumitrescu {
329b77f6600SCristian Dumitrescu 	struct rte_eth_link link_params;
330b77f6600SCristian Dumitrescu 	struct link *link;
331b77f6600SCristian Dumitrescu 
332b77f6600SCristian Dumitrescu 	/* Check input params */
333b77f6600SCristian Dumitrescu 	if (!obj || !name)
334b77f6600SCristian Dumitrescu 		return 0;
335b77f6600SCristian Dumitrescu 
336b77f6600SCristian Dumitrescu 	link = link_find(obj, name);
337b77f6600SCristian Dumitrescu 	if (link == NULL)
338b77f6600SCristian Dumitrescu 		return 0;
339b77f6600SCristian Dumitrescu 
340b77f6600SCristian Dumitrescu 	/* Resource */
341b77f6600SCristian Dumitrescu 	if (rte_eth_link_get(link->port_id, &link_params) < 0)
342b77f6600SCristian Dumitrescu 		return 0;
343b77f6600SCristian Dumitrescu 
344b77f6600SCristian Dumitrescu 	return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
345b77f6600SCristian Dumitrescu }
346b77f6600SCristian Dumitrescu 
347b77f6600SCristian Dumitrescu struct link *
348b77f6600SCristian Dumitrescu link_find(struct obj *obj, const char *name)
349b77f6600SCristian Dumitrescu {
350b77f6600SCristian Dumitrescu 	struct link *link;
351b77f6600SCristian Dumitrescu 
352b77f6600SCristian Dumitrescu 	if (!obj || !name)
353b77f6600SCristian Dumitrescu 		return NULL;
354b77f6600SCristian Dumitrescu 
355b77f6600SCristian Dumitrescu 	TAILQ_FOREACH(link, &obj->link_list, node)
356b77f6600SCristian Dumitrescu 		if (strcmp(link->name, name) == 0)
357b77f6600SCristian Dumitrescu 			return link;
358b77f6600SCristian Dumitrescu 
359b77f6600SCristian Dumitrescu 	return NULL;
360b77f6600SCristian Dumitrescu }
361b77f6600SCristian Dumitrescu 
362b77f6600SCristian Dumitrescu struct link *
363b77f6600SCristian Dumitrescu link_next(struct obj *obj, struct link *link)
364b77f6600SCristian Dumitrescu {
365b77f6600SCristian Dumitrescu 	return (link == NULL) ?
366b77f6600SCristian Dumitrescu 		TAILQ_FIRST(&obj->link_list) : TAILQ_NEXT(link, node);
367b77f6600SCristian Dumitrescu }
368b77f6600SCristian Dumitrescu 
369b77f6600SCristian Dumitrescu /*
37077a41301SCristian Dumitrescu  * ring
37177a41301SCristian Dumitrescu  */
37277a41301SCristian Dumitrescu struct ring *
37377a41301SCristian Dumitrescu ring_create(struct obj *obj, const char *name, struct ring_params *params)
37477a41301SCristian Dumitrescu {
37577a41301SCristian Dumitrescu 	struct ring *ring;
37677a41301SCristian Dumitrescu 	struct rte_ring *r;
37777a41301SCristian Dumitrescu 	unsigned int flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
37877a41301SCristian Dumitrescu 
37977a41301SCristian Dumitrescu 	/* Check input params */
38077a41301SCristian Dumitrescu 	if (!name || ring_find(obj, name) || !params || !params->size)
38177a41301SCristian Dumitrescu 		return NULL;
38277a41301SCristian Dumitrescu 
38377a41301SCristian Dumitrescu 	/**
38477a41301SCristian Dumitrescu 	 * Resource create
38577a41301SCristian Dumitrescu 	 */
38677a41301SCristian Dumitrescu 	r = rte_ring_create(
38777a41301SCristian Dumitrescu 		name,
38877a41301SCristian Dumitrescu 		params->size,
38977a41301SCristian Dumitrescu 		params->numa_node,
39077a41301SCristian Dumitrescu 		flags);
39177a41301SCristian Dumitrescu 	if (!r)
39277a41301SCristian Dumitrescu 		return NULL;
39377a41301SCristian Dumitrescu 
39477a41301SCristian Dumitrescu 	/* Node allocation */
39577a41301SCristian Dumitrescu 	ring = calloc(1, sizeof(struct ring));
39677a41301SCristian Dumitrescu 	if (!ring) {
39777a41301SCristian Dumitrescu 		rte_ring_free(r);
39877a41301SCristian Dumitrescu 		return NULL;
39977a41301SCristian Dumitrescu 	}
40077a41301SCristian Dumitrescu 
40177a41301SCristian Dumitrescu 	/* Node fill in */
40277a41301SCristian Dumitrescu 	strlcpy(ring->name, name, sizeof(ring->name));
40377a41301SCristian Dumitrescu 
40477a41301SCristian Dumitrescu 	/* Node add to list */
40577a41301SCristian Dumitrescu 	TAILQ_INSERT_TAIL(&obj->ring_list, ring, node);
40677a41301SCristian Dumitrescu 
40777a41301SCristian Dumitrescu 	return ring;
40877a41301SCristian Dumitrescu }
40977a41301SCristian Dumitrescu 
41077a41301SCristian Dumitrescu struct ring *
41177a41301SCristian Dumitrescu ring_find(struct obj *obj, const char *name)
41277a41301SCristian Dumitrescu {
41377a41301SCristian Dumitrescu 	struct ring *ring;
41477a41301SCristian Dumitrescu 
41577a41301SCristian Dumitrescu 	if (!obj || !name)
41677a41301SCristian Dumitrescu 		return NULL;
41777a41301SCristian Dumitrescu 
41877a41301SCristian Dumitrescu 	TAILQ_FOREACH(ring, &obj->ring_list, node)
41977a41301SCristian Dumitrescu 		if (strcmp(ring->name, name) == 0)
42077a41301SCristian Dumitrescu 			return ring;
42177a41301SCristian Dumitrescu 
42277a41301SCristian Dumitrescu 	return NULL;
42377a41301SCristian Dumitrescu }
42477a41301SCristian Dumitrescu 
42577a41301SCristian Dumitrescu /*
426b77f6600SCristian Dumitrescu  * pipeline
427b77f6600SCristian Dumitrescu  */
428b77f6600SCristian Dumitrescu #ifndef PIPELINE_MSGQ_SIZE
429b77f6600SCristian Dumitrescu #define PIPELINE_MSGQ_SIZE                                 64
430b77f6600SCristian Dumitrescu #endif
431b77f6600SCristian Dumitrescu 
432b77f6600SCristian Dumitrescu struct pipeline *
433b77f6600SCristian Dumitrescu pipeline_create(struct obj *obj, const char *name, int numa_node)
434b77f6600SCristian Dumitrescu {
435b77f6600SCristian Dumitrescu 	struct pipeline *pipeline;
436b77f6600SCristian Dumitrescu 	struct rte_swx_pipeline *p = NULL;
437b77f6600SCristian Dumitrescu 	int status;
438b77f6600SCristian Dumitrescu 
439b77f6600SCristian Dumitrescu 	/* Check input params */
440b77f6600SCristian Dumitrescu 	if ((name == NULL) ||
441b77f6600SCristian Dumitrescu 		pipeline_find(obj, name))
442b77f6600SCristian Dumitrescu 		return NULL;
443b77f6600SCristian Dumitrescu 
444b77f6600SCristian Dumitrescu 	/* Resource create */
445b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_config(&p, numa_node);
446b77f6600SCristian Dumitrescu 	if (status)
447b77f6600SCristian Dumitrescu 		goto error;
448b77f6600SCristian Dumitrescu 
449b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_port_in_type_register(p,
450b77f6600SCristian Dumitrescu 		"ethdev",
451b77f6600SCristian Dumitrescu 		&rte_swx_port_ethdev_reader_ops);
452b77f6600SCristian Dumitrescu 	if (status)
453b77f6600SCristian Dumitrescu 		goto error;
454b77f6600SCristian Dumitrescu 
455b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_port_out_type_register(p,
456b77f6600SCristian Dumitrescu 		"ethdev",
457b77f6600SCristian Dumitrescu 		&rte_swx_port_ethdev_writer_ops);
458b77f6600SCristian Dumitrescu 	if (status)
459b77f6600SCristian Dumitrescu 		goto error;
460b77f6600SCristian Dumitrescu 
46177a41301SCristian Dumitrescu 	status = rte_swx_pipeline_port_in_type_register(p,
46277a41301SCristian Dumitrescu 		"ring",
46377a41301SCristian Dumitrescu 		&rte_swx_port_ring_reader_ops);
46477a41301SCristian Dumitrescu 	if (status)
46577a41301SCristian Dumitrescu 		goto error;
46677a41301SCristian Dumitrescu 
46777a41301SCristian Dumitrescu 	status = rte_swx_pipeline_port_out_type_register(p,
46877a41301SCristian Dumitrescu 		"ring",
46977a41301SCristian Dumitrescu 		&rte_swx_port_ring_writer_ops);
47077a41301SCristian Dumitrescu 	if (status)
47177a41301SCristian Dumitrescu 		goto error;
47277a41301SCristian Dumitrescu 
473b77f6600SCristian Dumitrescu #ifdef RTE_PORT_PCAP
474b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_port_in_type_register(p,
475b77f6600SCristian Dumitrescu 		"source",
476b77f6600SCristian Dumitrescu 		&rte_swx_port_source_ops);
477b77f6600SCristian Dumitrescu 	if (status)
478b77f6600SCristian Dumitrescu 		goto error;
479b77f6600SCristian Dumitrescu #endif
480b77f6600SCristian Dumitrescu 
481b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_port_out_type_register(p,
482b77f6600SCristian Dumitrescu 		"sink",
483b77f6600SCristian Dumitrescu 		&rte_swx_port_sink_ops);
484b77f6600SCristian Dumitrescu 	if (status)
485b77f6600SCristian Dumitrescu 		goto error;
486b77f6600SCristian Dumitrescu 
487b77f6600SCristian Dumitrescu 	status = rte_swx_pipeline_table_type_register(p,
488b77f6600SCristian Dumitrescu 		"exact",
489b77f6600SCristian Dumitrescu 		RTE_SWX_TABLE_MATCH_EXACT,
490b77f6600SCristian Dumitrescu 		&rte_swx_table_exact_match_ops);
491b77f6600SCristian Dumitrescu 	if (status)
492b77f6600SCristian Dumitrescu 		goto error;
493b77f6600SCristian Dumitrescu 
494*66440b7bSCristian Dumitrescu 	status = rte_swx_pipeline_table_type_register(p,
495*66440b7bSCristian Dumitrescu 		"wildcard",
496*66440b7bSCristian Dumitrescu 		RTE_SWX_TABLE_MATCH_WILDCARD,
497*66440b7bSCristian Dumitrescu 		&rte_swx_table_wildcard_match_ops);
498*66440b7bSCristian Dumitrescu 	if (status)
499*66440b7bSCristian Dumitrescu 		goto error;
500*66440b7bSCristian Dumitrescu 
501b77f6600SCristian Dumitrescu 	/* Node allocation */
502b77f6600SCristian Dumitrescu 	pipeline = calloc(1, sizeof(struct pipeline));
503b77f6600SCristian Dumitrescu 	if (pipeline == NULL)
504b77f6600SCristian Dumitrescu 		goto error;
505b77f6600SCristian Dumitrescu 
506b77f6600SCristian Dumitrescu 	/* Node fill in */
507b77f6600SCristian Dumitrescu 	strlcpy(pipeline->name, name, sizeof(pipeline->name));
508b77f6600SCristian Dumitrescu 	pipeline->p = p;
509b77f6600SCristian Dumitrescu 	pipeline->timer_period_ms = 10;
510b77f6600SCristian Dumitrescu 
511b77f6600SCristian Dumitrescu 	/* Node add to list */
512b77f6600SCristian Dumitrescu 	TAILQ_INSERT_TAIL(&obj->pipeline_list, pipeline, node);
513b77f6600SCristian Dumitrescu 
514b77f6600SCristian Dumitrescu 	return pipeline;
515b77f6600SCristian Dumitrescu 
516b77f6600SCristian Dumitrescu error:
517b77f6600SCristian Dumitrescu 	rte_swx_pipeline_free(p);
518b77f6600SCristian Dumitrescu 	return NULL;
519b77f6600SCristian Dumitrescu }
520b77f6600SCristian Dumitrescu 
521b77f6600SCristian Dumitrescu struct pipeline *
522b77f6600SCristian Dumitrescu pipeline_find(struct obj *obj, const char *name)
523b77f6600SCristian Dumitrescu {
524b77f6600SCristian Dumitrescu 	struct pipeline *pipeline;
525b77f6600SCristian Dumitrescu 
526b77f6600SCristian Dumitrescu 	if (!obj || !name)
527b77f6600SCristian Dumitrescu 		return NULL;
528b77f6600SCristian Dumitrescu 
529b77f6600SCristian Dumitrescu 	TAILQ_FOREACH(pipeline, &obj->pipeline_list, node)
530b77f6600SCristian Dumitrescu 		if (strcmp(name, pipeline->name) == 0)
531b77f6600SCristian Dumitrescu 			return pipeline;
532b77f6600SCristian Dumitrescu 
533b77f6600SCristian Dumitrescu 	return NULL;
534b77f6600SCristian Dumitrescu }
535b77f6600SCristian Dumitrescu 
536b77f6600SCristian Dumitrescu /*
537b77f6600SCristian Dumitrescu  * obj
538b77f6600SCristian Dumitrescu  */
539b77f6600SCristian Dumitrescu struct obj *
540b77f6600SCristian Dumitrescu obj_init(void)
541b77f6600SCristian Dumitrescu {
542b77f6600SCristian Dumitrescu 	struct obj *obj;
543b77f6600SCristian Dumitrescu 
544b77f6600SCristian Dumitrescu 	obj = calloc(1, sizeof(struct obj));
545b77f6600SCristian Dumitrescu 	if (!obj)
546b77f6600SCristian Dumitrescu 		return NULL;
547b77f6600SCristian Dumitrescu 
548b77f6600SCristian Dumitrescu 	TAILQ_INIT(&obj->mempool_list);
549b77f6600SCristian Dumitrescu 	TAILQ_INIT(&obj->link_list);
55077a41301SCristian Dumitrescu 	TAILQ_INIT(&obj->ring_list);
551b77f6600SCristian Dumitrescu 	TAILQ_INIT(&obj->pipeline_list);
552b77f6600SCristian Dumitrescu 
553b77f6600SCristian Dumitrescu 	return obj;
554b77f6600SCristian Dumitrescu }
555