1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
7 #include <netinet/in.h>
8 #ifdef RTE_EXEC_ENV_LINUX
10 #include <linux/if_tun.h>
12 #include <sys/ioctl.h>
16 #include <rte_mempool.h>
18 #include <rte_ethdev.h>
19 #include <rte_swx_table_em.h>
20 #include <rte_swx_table_wm.h>
21 #include <rte_swx_pipeline.h>
22 #include <rte_swx_ctl.h>
29 TAILQ_HEAD(mempool_list, mempool);
34 TAILQ_HEAD(link_list, link);
39 TAILQ_HEAD(ring_list, ring);
44 TAILQ_HEAD(tap_list, tap);
49 TAILQ_HEAD(pipeline_list, pipeline);
55 struct mempool_list mempool_list;
56 struct link_list link_list;
57 struct ring_list ring_list;
58 struct pipeline_list pipeline_list;
59 struct tap_list tap_list;
65 #define BUFFER_SIZE_MIN (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
68 mempool_create(struct obj *obj, const char *name, struct mempool_params *params)
70 struct mempool *mempool;
71 struct rte_mempool *m;
73 /* Check input params */
75 mempool_find(obj, name) ||
77 (params->buffer_size < BUFFER_SIZE_MIN) ||
78 (params->pool_size == 0))
82 m = rte_pktmbuf_pool_create(
87 params->buffer_size - sizeof(struct rte_mbuf),
94 mempool = calloc(1, sizeof(struct mempool));
95 if (mempool == NULL) {
101 strlcpy(mempool->name, name, sizeof(mempool->name));
103 mempool->buffer_size = params->buffer_size;
105 /* Node add to list */
106 TAILQ_INSERT_TAIL(&obj->mempool_list, mempool, node);
112 mempool_find(struct obj *obj, const char *name)
114 struct mempool *mempool;
119 TAILQ_FOREACH(mempool, &obj->mempool_list, node)
120 if (strcmp(mempool->name, name) == 0)
129 static struct rte_eth_conf port_conf_default = {
132 .mq_mode = RTE_ETH_MQ_RX_NONE,
133 .mtu = 9000 - (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN), /* Jumbo frame MTU */
134 .split_hdr_size = 0, /* Header split buffer size */
144 .mq_mode = RTE_ETH_MQ_TX_NONE,
149 #define RETA_CONF_SIZE (RTE_ETH_RSS_RETA_SIZE_512 / RTE_ETH_RETA_GROUP_SIZE)
152 rss_setup(uint16_t port_id,
154 struct link_params_rss *rss)
156 struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
161 memset(reta_conf, 0, sizeof(reta_conf));
163 for (i = 0; i < reta_size; i++)
164 reta_conf[i / RTE_ETH_RETA_GROUP_SIZE].mask = UINT64_MAX;
166 for (i = 0; i < reta_size; i++) {
167 uint32_t reta_id = i / RTE_ETH_RETA_GROUP_SIZE;
168 uint32_t reta_pos = i % RTE_ETH_RETA_GROUP_SIZE;
169 uint32_t rss_qs_pos = i % rss->n_queues;
171 reta_conf[reta_id].reta[reta_pos] =
172 (uint16_t) rss->queue_id[rss_qs_pos];
176 status = rte_eth_dev_rss_reta_update(port_id,
184 link_create(struct obj *obj, const char *name, struct link_params *params)
186 struct rte_eth_dev_info port_info;
187 struct rte_eth_conf port_conf;
189 struct link_params_rss *rss;
190 struct mempool *mempool;
195 /* Check input params */
196 if ((name == NULL) ||
197 link_find(obj, name) ||
199 (params->rx.n_queues == 0) ||
200 (params->rx.queue_size == 0) ||
201 (params->tx.n_queues == 0) ||
202 (params->tx.queue_size == 0))
205 port_id = params->port_id;
206 if (params->dev_name) {
207 status = rte_eth_dev_get_port_by_name(params->dev_name,
213 if (!rte_eth_dev_is_valid_port(port_id))
216 if (rte_eth_dev_info_get(port_id, &port_info) != 0)
219 mempool = mempool_find(obj, params->rx.mempool_name);
223 rss = params->rx.rss;
225 if ((port_info.reta_size == 0) ||
226 (port_info.reta_size > RTE_ETH_RSS_RETA_SIZE_512))
229 if ((rss->n_queues == 0) ||
230 (rss->n_queues >= LINK_RXQ_RSS_MAX))
233 for (i = 0; i < rss->n_queues; i++)
234 if (rss->queue_id[i] >= port_info.max_rx_queues)
242 memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
244 port_conf.rxmode.mq_mode = RTE_ETH_MQ_RX_RSS;
245 port_conf.rx_adv_conf.rss_conf.rss_hf =
246 (RTE_ETH_RSS_IP | RTE_ETH_RSS_TCP | RTE_ETH_RSS_UDP) &
247 port_info.flow_type_rss_offloads;
250 cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
251 if (cpu_id == (uint32_t) SOCKET_ID_ANY)
254 status = rte_eth_dev_configure(
263 if (params->promiscuous) {
264 status = rte_eth_promiscuous_enable(port_id);
270 for (i = 0; i < params->rx.n_queues; i++) {
271 status = rte_eth_rx_queue_setup(
274 params->rx.queue_size,
284 for (i = 0; i < params->tx.n_queues; i++) {
285 status = rte_eth_tx_queue_setup(
288 params->tx.queue_size,
297 status = rte_eth_dev_start(port_id);
302 status = rss_setup(port_id, port_info.reta_size, rss);
305 rte_eth_dev_stop(port_id);
311 status = rte_eth_dev_set_link_up(port_id);
312 if ((status < 0) && (status != -ENOTSUP)) {
313 rte_eth_dev_stop(port_id);
317 /* Node allocation */
318 link = calloc(1, sizeof(struct link));
320 rte_eth_dev_stop(port_id);
325 strlcpy(link->name, name, sizeof(link->name));
326 link->port_id = port_id;
327 rte_eth_dev_get_name_by_port(port_id, link->dev_name);
328 link->n_rxq = params->rx.n_queues;
329 link->n_txq = params->tx.n_queues;
331 /* Node add to list */
332 TAILQ_INSERT_TAIL(&obj->link_list, link, node);
338 link_is_up(struct obj *obj, const char *name)
340 struct rte_eth_link link_params;
343 /* Check input params */
347 link = link_find(obj, name);
352 if (rte_eth_link_get(link->port_id, &link_params) < 0)
355 return (link_params.link_status == RTE_ETH_LINK_DOWN) ? 0 : 1;
359 link_find(struct obj *obj, const char *name)
366 TAILQ_FOREACH(link, &obj->link_list, node)
367 if (strcmp(link->name, name) == 0)
374 link_next(struct obj *obj, struct link *link)
376 return (link == NULL) ?
377 TAILQ_FIRST(&obj->link_list) : TAILQ_NEXT(link, node);
384 ring_create(struct obj *obj, const char *name, struct ring_params *params)
388 unsigned int flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
390 /* Check input params */
391 if (!name || ring_find(obj, name) || !params || !params->size)
405 /* Node allocation */
406 ring = calloc(1, sizeof(struct ring));
413 strlcpy(ring->name, name, sizeof(ring->name));
415 /* Node add to list */
416 TAILQ_INSERT_TAIL(&obj->ring_list, ring, node);
422 ring_find(struct obj *obj, const char *name)
429 TAILQ_FOREACH(ring, &obj->ring_list, node)
430 if (strcmp(ring->name, name) == 0)
439 #define TAP_DEV "/dev/net/tun"
442 tap_find(struct obj *obj, const char *name)
449 TAILQ_FOREACH(tap, &obj->tap_list, node)
450 if (strcmp(tap->name, name) == 0)
457 tap_next(struct obj *obj, struct tap *tap)
459 return (tap == NULL) ?
460 TAILQ_FIRST(&obj->tap_list) : TAILQ_NEXT(tap, node);
463 #ifndef RTE_EXEC_ENV_LINUX
466 tap_create(struct obj *obj __rte_unused, const char *name __rte_unused)
474 tap_create(struct obj *obj, const char *name)
480 /* Check input params */
481 if ((name == NULL) ||
485 /* Resource create */
486 fd = open(TAP_DEV, O_RDWR | O_NONBLOCK);
490 memset(&ifr, 0, sizeof(ifr));
491 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; /* No packet information */
492 strlcpy(ifr.ifr_name, name, IFNAMSIZ);
494 status = ioctl(fd, TUNSETIFF, (void *) &ifr);
500 /* Node allocation */
501 tap = calloc(1, sizeof(struct tap));
507 strlcpy(tap->name, name, sizeof(tap->name));
510 /* Node add to list */
511 TAILQ_INSERT_TAIL(&obj->tap_list, tap, node);
521 #ifndef PIPELINE_MSGQ_SIZE
522 #define PIPELINE_MSGQ_SIZE 64
526 pipeline_create(struct obj *obj, const char *name, int numa_node)
528 struct pipeline *pipeline;
529 struct rte_swx_pipeline *p = NULL;
532 /* Check input params */
533 if ((name == NULL) ||
534 pipeline_find(obj, name))
537 /* Resource create */
538 status = rte_swx_pipeline_config(&p, numa_node);
542 status = rte_swx_pipeline_table_type_register(p,
544 RTE_SWX_TABLE_MATCH_EXACT,
545 &rte_swx_table_exact_match_ops);
549 status = rte_swx_pipeline_table_type_register(p,
551 RTE_SWX_TABLE_MATCH_WILDCARD,
552 &rte_swx_table_wildcard_match_ops);
556 /* Node allocation */
557 pipeline = calloc(1, sizeof(struct pipeline));
558 if (pipeline == NULL)
562 strlcpy(pipeline->name, name, sizeof(pipeline->name));
564 pipeline->timer_period_ms = 10;
566 /* Node add to list */
567 TAILQ_INSERT_TAIL(&obj->pipeline_list, pipeline, node);
572 rte_swx_pipeline_free(p);
577 pipeline_find(struct obj *obj, const char *name)
579 struct pipeline *pipeline;
584 TAILQ_FOREACH(pipeline, &obj->pipeline_list, node)
585 if (strcmp(name, pipeline->name) == 0)
599 obj = calloc(1, sizeof(struct obj));
603 TAILQ_INIT(&obj->mempool_list);
604 TAILQ_INIT(&obj->link_list);
605 TAILQ_INIT(&obj->ring_list);
606 TAILQ_INIT(&obj->pipeline_list);
607 TAILQ_INIT(&obj->tap_list);