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_ring.h>
13 #include <rte_swx_port_source_sink.h>
14 #include <rte_swx_table_em.h>
15 #include <rte_swx_pipeline.h>
16 #include <rte_swx_ctl.h>
23 TAILQ_HEAD(mempool_list, mempool);
28 TAILQ_HEAD(link_list, link);
33 TAILQ_HEAD(ring_list, ring);
38 TAILQ_HEAD(pipeline_list, pipeline);
44 struct mempool_list mempool_list;
45 struct link_list link_list;
46 struct ring_list ring_list;
47 struct pipeline_list pipeline_list;
53 #define BUFFER_SIZE_MIN (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
56 mempool_create(struct obj *obj, const char *name, struct mempool_params *params)
58 struct mempool *mempool;
59 struct rte_mempool *m;
61 /* Check input params */
63 mempool_find(obj, name) ||
65 (params->buffer_size < BUFFER_SIZE_MIN) ||
66 (params->pool_size == 0))
70 m = rte_pktmbuf_pool_create(
75 params->buffer_size - sizeof(struct rte_mbuf),
82 mempool = calloc(1, sizeof(struct mempool));
83 if (mempool == NULL) {
89 strlcpy(mempool->name, name, sizeof(mempool->name));
91 mempool->buffer_size = params->buffer_size;
93 /* Node add to list */
94 TAILQ_INSERT_TAIL(&obj->mempool_list, mempool, node);
100 mempool_find(struct obj *obj, const char *name)
102 struct mempool *mempool;
107 TAILQ_FOREACH(mempool, &obj->mempool_list, node)
108 if (strcmp(mempool->name, name) == 0)
117 static struct rte_eth_conf port_conf_default = {
120 .mq_mode = ETH_MQ_RX_NONE,
121 .max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
122 .split_hdr_size = 0, /* Header split buffer size */
132 .mq_mode = ETH_MQ_TX_NONE,
137 #define RETA_CONF_SIZE (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
140 rss_setup(uint16_t port_id,
142 struct link_params_rss *rss)
144 struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
149 memset(reta_conf, 0, sizeof(reta_conf));
151 for (i = 0; i < reta_size; i++)
152 reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
154 for (i = 0; i < reta_size; i++) {
155 uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
156 uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
157 uint32_t rss_qs_pos = i % rss->n_queues;
159 reta_conf[reta_id].reta[reta_pos] =
160 (uint16_t) rss->queue_id[rss_qs_pos];
164 status = rte_eth_dev_rss_reta_update(port_id,
172 link_create(struct obj *obj, const char *name, struct link_params *params)
174 struct rte_eth_dev_info port_info;
175 struct rte_eth_conf port_conf;
177 struct link_params_rss *rss;
178 struct mempool *mempool;
183 /* Check input params */
184 if ((name == NULL) ||
185 link_find(obj, name) ||
187 (params->rx.n_queues == 0) ||
188 (params->rx.queue_size == 0) ||
189 (params->tx.n_queues == 0) ||
190 (params->tx.queue_size == 0))
193 port_id = params->port_id;
194 if (params->dev_name) {
195 status = rte_eth_dev_get_port_by_name(params->dev_name,
201 if (!rte_eth_dev_is_valid_port(port_id))
204 if (rte_eth_dev_info_get(port_id, &port_info) != 0)
207 mempool = mempool_find(obj, params->rx.mempool_name);
211 rss = params->rx.rss;
213 if ((port_info.reta_size == 0) ||
214 (port_info.reta_size > ETH_RSS_RETA_SIZE_512))
217 if ((rss->n_queues == 0) ||
218 (rss->n_queues >= LINK_RXQ_RSS_MAX))
221 for (i = 0; i < rss->n_queues; i++)
222 if (rss->queue_id[i] >= port_info.max_rx_queues)
230 memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
232 port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
233 port_conf.rx_adv_conf.rss_conf.rss_hf =
234 (ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) &
235 port_info.flow_type_rss_offloads;
238 cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
239 if (cpu_id == (uint32_t) SOCKET_ID_ANY)
242 status = rte_eth_dev_configure(
251 if (params->promiscuous) {
252 status = rte_eth_promiscuous_enable(port_id);
258 for (i = 0; i < params->rx.n_queues; i++) {
259 status = rte_eth_rx_queue_setup(
262 params->rx.queue_size,
272 for (i = 0; i < params->tx.n_queues; i++) {
273 status = rte_eth_tx_queue_setup(
276 params->tx.queue_size,
285 status = rte_eth_dev_start(port_id);
290 status = rss_setup(port_id, port_info.reta_size, rss);
293 rte_eth_dev_stop(port_id);
299 status = rte_eth_dev_set_link_up(port_id);
300 if ((status < 0) && (status != -ENOTSUP)) {
301 rte_eth_dev_stop(port_id);
305 /* Node allocation */
306 link = calloc(1, sizeof(struct link));
308 rte_eth_dev_stop(port_id);
313 strlcpy(link->name, name, sizeof(link->name));
314 link->port_id = port_id;
315 rte_eth_dev_get_name_by_port(port_id, link->dev_name);
316 link->n_rxq = params->rx.n_queues;
317 link->n_txq = params->tx.n_queues;
319 /* Node add to list */
320 TAILQ_INSERT_TAIL(&obj->link_list, link, node);
326 link_is_up(struct obj *obj, const char *name)
328 struct rte_eth_link link_params;
331 /* Check input params */
335 link = link_find(obj, name);
340 if (rte_eth_link_get(link->port_id, &link_params) < 0)
343 return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
347 link_find(struct obj *obj, const char *name)
354 TAILQ_FOREACH(link, &obj->link_list, node)
355 if (strcmp(link->name, name) == 0)
362 link_next(struct obj *obj, struct link *link)
364 return (link == NULL) ?
365 TAILQ_FIRST(&obj->link_list) : TAILQ_NEXT(link, node);
372 ring_create(struct obj *obj, const char *name, struct ring_params *params)
376 unsigned int flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
378 /* Check input params */
379 if (!name || ring_find(obj, name) || !params || !params->size)
393 /* Node allocation */
394 ring = calloc(1, sizeof(struct ring));
401 strlcpy(ring->name, name, sizeof(ring->name));
403 /* Node add to list */
404 TAILQ_INSERT_TAIL(&obj->ring_list, ring, node);
410 ring_find(struct obj *obj, const char *name)
417 TAILQ_FOREACH(ring, &obj->ring_list, node)
418 if (strcmp(ring->name, name) == 0)
427 #ifndef PIPELINE_MSGQ_SIZE
428 #define PIPELINE_MSGQ_SIZE 64
432 pipeline_create(struct obj *obj, const char *name, int numa_node)
434 struct pipeline *pipeline;
435 struct rte_swx_pipeline *p = NULL;
438 /* Check input params */
439 if ((name == NULL) ||
440 pipeline_find(obj, name))
443 /* Resource create */
444 status = rte_swx_pipeline_config(&p, numa_node);
448 status = rte_swx_pipeline_port_in_type_register(p,
450 &rte_swx_port_ethdev_reader_ops);
454 status = rte_swx_pipeline_port_out_type_register(p,
456 &rte_swx_port_ethdev_writer_ops);
460 status = rte_swx_pipeline_port_in_type_register(p,
462 &rte_swx_port_ring_reader_ops);
466 status = rte_swx_pipeline_port_out_type_register(p,
468 &rte_swx_port_ring_writer_ops);
473 status = rte_swx_pipeline_port_in_type_register(p,
475 &rte_swx_port_source_ops);
480 status = rte_swx_pipeline_port_out_type_register(p,
482 &rte_swx_port_sink_ops);
486 status = rte_swx_pipeline_table_type_register(p,
488 RTE_SWX_TABLE_MATCH_EXACT,
489 &rte_swx_table_exact_match_ops);
493 /* Node allocation */
494 pipeline = calloc(1, sizeof(struct pipeline));
495 if (pipeline == NULL)
499 strlcpy(pipeline->name, name, sizeof(pipeline->name));
501 pipeline->timer_period_ms = 10;
503 /* Node add to list */
504 TAILQ_INSERT_TAIL(&obj->pipeline_list, pipeline, node);
509 rte_swx_pipeline_free(p);
514 pipeline_find(struct obj *obj, const char *name)
516 struct pipeline *pipeline;
521 TAILQ_FOREACH(pipeline, &obj->pipeline_list, node)
522 if (strcmp(name, pipeline->name) == 0)
536 obj = calloc(1, sizeof(struct obj));
540 TAILQ_INIT(&obj->mempool_list);
541 TAILQ_INIT(&obj->link_list);
542 TAILQ_INIT(&obj->ring_list);
543 TAILQ_INIT(&obj->pipeline_list);