1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
8 #include <rte_mempool.h>
10 #include <rte_ethdev.h>
11 #include <rte_swx_port_ethdev.h>
12 #include <rte_swx_port_source_sink.h>
13 #include <rte_swx_table_em.h>
14 #include <rte_swx_pipeline.h>
15 #include <rte_swx_ctl.h>
22 TAILQ_HEAD(mempool_list, mempool);
27 TAILQ_HEAD(link_list, link);
32 TAILQ_HEAD(pipeline_list, pipeline);
38 struct mempool_list mempool_list;
39 struct link_list link_list;
40 struct pipeline_list pipeline_list;
46 #define BUFFER_SIZE_MIN (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
49 mempool_create(struct obj *obj, const char *name, struct mempool_params *params)
51 struct mempool *mempool;
52 struct rte_mempool *m;
54 /* Check input params */
56 mempool_find(obj, name) ||
58 (params->buffer_size < BUFFER_SIZE_MIN) ||
59 (params->pool_size == 0))
63 m = rte_pktmbuf_pool_create(
68 params->buffer_size - sizeof(struct rte_mbuf),
75 mempool = calloc(1, sizeof(struct mempool));
76 if (mempool == NULL) {
82 strlcpy(mempool->name, name, sizeof(mempool->name));
84 mempool->buffer_size = params->buffer_size;
86 /* Node add to list */
87 TAILQ_INSERT_TAIL(&obj->mempool_list, mempool, node);
93 mempool_find(struct obj *obj, const char *name)
95 struct mempool *mempool;
100 TAILQ_FOREACH(mempool, &obj->mempool_list, node)
101 if (strcmp(mempool->name, name) == 0)
110 static struct rte_eth_conf port_conf_default = {
113 .mq_mode = ETH_MQ_RX_NONE,
114 .max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
115 .split_hdr_size = 0, /* Header split buffer size */
125 .mq_mode = ETH_MQ_TX_NONE,
130 #define RETA_CONF_SIZE (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
133 rss_setup(uint16_t port_id,
135 struct link_params_rss *rss)
137 struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
142 memset(reta_conf, 0, sizeof(reta_conf));
144 for (i = 0; i < reta_size; i++)
145 reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
147 for (i = 0; i < reta_size; i++) {
148 uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
149 uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
150 uint32_t rss_qs_pos = i % rss->n_queues;
152 reta_conf[reta_id].reta[reta_pos] =
153 (uint16_t) rss->queue_id[rss_qs_pos];
157 status = rte_eth_dev_rss_reta_update(port_id,
165 link_create(struct obj *obj, const char *name, struct link_params *params)
167 struct rte_eth_dev_info port_info;
168 struct rte_eth_conf port_conf;
170 struct link_params_rss *rss;
171 struct mempool *mempool;
176 /* Check input params */
177 if ((name == NULL) ||
178 link_find(obj, name) ||
180 (params->rx.n_queues == 0) ||
181 (params->rx.queue_size == 0) ||
182 (params->tx.n_queues == 0) ||
183 (params->tx.queue_size == 0))
186 port_id = params->port_id;
187 if (params->dev_name) {
188 status = rte_eth_dev_get_port_by_name(params->dev_name,
194 if (!rte_eth_dev_is_valid_port(port_id))
197 if (rte_eth_dev_info_get(port_id, &port_info) != 0)
200 mempool = mempool_find(obj, params->rx.mempool_name);
204 rss = params->rx.rss;
206 if ((port_info.reta_size == 0) ||
207 (port_info.reta_size > ETH_RSS_RETA_SIZE_512))
210 if ((rss->n_queues == 0) ||
211 (rss->n_queues >= LINK_RXQ_RSS_MAX))
214 for (i = 0; i < rss->n_queues; i++)
215 if (rss->queue_id[i] >= port_info.max_rx_queues)
223 memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
225 port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
226 port_conf.rx_adv_conf.rss_conf.rss_hf =
227 (ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) &
228 port_info.flow_type_rss_offloads;
231 cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
232 if (cpu_id == (uint32_t) SOCKET_ID_ANY)
235 status = rte_eth_dev_configure(
244 if (params->promiscuous) {
245 status = rte_eth_promiscuous_enable(port_id);
251 for (i = 0; i < params->rx.n_queues; i++) {
252 status = rte_eth_rx_queue_setup(
255 params->rx.queue_size,
265 for (i = 0; i < params->tx.n_queues; i++) {
266 status = rte_eth_tx_queue_setup(
269 params->tx.queue_size,
278 status = rte_eth_dev_start(port_id);
283 status = rss_setup(port_id, port_info.reta_size, rss);
286 rte_eth_dev_stop(port_id);
292 status = rte_eth_dev_set_link_up(port_id);
293 if ((status < 0) && (status != -ENOTSUP)) {
294 rte_eth_dev_stop(port_id);
298 /* Node allocation */
299 link = calloc(1, sizeof(struct link));
301 rte_eth_dev_stop(port_id);
306 strlcpy(link->name, name, sizeof(link->name));
307 link->port_id = port_id;
308 rte_eth_dev_get_name_by_port(port_id, link->dev_name);
309 link->n_rxq = params->rx.n_queues;
310 link->n_txq = params->tx.n_queues;
312 /* Node add to list */
313 TAILQ_INSERT_TAIL(&obj->link_list, link, node);
319 link_is_up(struct obj *obj, const char *name)
321 struct rte_eth_link link_params;
324 /* Check input params */
328 link = link_find(obj, name);
333 if (rte_eth_link_get(link->port_id, &link_params) < 0)
336 return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
340 link_find(struct obj *obj, const char *name)
347 TAILQ_FOREACH(link, &obj->link_list, node)
348 if (strcmp(link->name, name) == 0)
355 link_next(struct obj *obj, struct link *link)
357 return (link == NULL) ?
358 TAILQ_FIRST(&obj->link_list) : TAILQ_NEXT(link, node);
364 #ifndef PIPELINE_MSGQ_SIZE
365 #define PIPELINE_MSGQ_SIZE 64
369 pipeline_create(struct obj *obj, const char *name, int numa_node)
371 struct pipeline *pipeline;
372 struct rte_swx_pipeline *p = NULL;
375 /* Check input params */
376 if ((name == NULL) ||
377 pipeline_find(obj, name))
380 /* Resource create */
381 status = rte_swx_pipeline_config(&p, numa_node);
385 status = rte_swx_pipeline_port_in_type_register(p,
387 &rte_swx_port_ethdev_reader_ops);
391 status = rte_swx_pipeline_port_out_type_register(p,
393 &rte_swx_port_ethdev_writer_ops);
398 status = rte_swx_pipeline_port_in_type_register(p,
400 &rte_swx_port_source_ops);
405 status = rte_swx_pipeline_port_out_type_register(p,
407 &rte_swx_port_sink_ops);
411 status = rte_swx_pipeline_table_type_register(p,
413 RTE_SWX_TABLE_MATCH_EXACT,
414 &rte_swx_table_exact_match_ops);
418 /* Node allocation */
419 pipeline = calloc(1, sizeof(struct pipeline));
420 if (pipeline == NULL)
424 strlcpy(pipeline->name, name, sizeof(pipeline->name));
426 pipeline->timer_period_ms = 10;
428 /* Node add to list */
429 TAILQ_INSERT_TAIL(&obj->pipeline_list, pipeline, node);
434 rte_swx_pipeline_free(p);
439 pipeline_find(struct obj *obj, const char *name)
441 struct pipeline *pipeline;
446 TAILQ_FOREACH(pipeline, &obj->pipeline_list, node)
447 if (strcmp(name, pipeline->name) == 0)
461 obj = calloc(1, sizeof(struct obj));
465 TAILQ_INIT(&obj->mempool_list);
466 TAILQ_INIT(&obj->link_list);
467 TAILQ_INIT(&obj->pipeline_list);