1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
9 #include <rte_ethdev_driver.h>
10 #include <rte_ethdev_vdev.h>
11 #include <rte_malloc.h>
12 #include <rte_bus_vdev.h>
13 #include <rte_kvargs.h>
14 #include <rte_errno.h>
16 #include <rte_tm_driver.h>
18 #include "rte_eth_softnic.h"
19 #include "rte_eth_softnic_internals.h"
21 #define PMD_PARAM_FIRMWARE "firmware"
22 #define PMD_PARAM_CPU_ID "cpu_id"
23 #define PMD_PARAM_SCRIPT "script"
24 #define PMD_PARAM_CONN_PORT "conn_port"
25 #define PMD_PARAM_CPU_ID "cpu_id"
26 #define PMD_PARAM_TM_N_QUEUES "tm_n_queues"
27 #define PMD_PARAM_TM_QSIZE0 "tm_qsize0"
28 #define PMD_PARAM_TM_QSIZE1 "tm_qsize1"
29 #define PMD_PARAM_TM_QSIZE2 "tm_qsize2"
30 #define PMD_PARAM_TM_QSIZE3 "tm_qsize3"
32 static const char *pmd_valid_args[] = {
35 PMD_PARAM_TM_N_QUEUES,
43 static const struct rte_eth_dev_info pmd_dev_info = {
45 .max_rx_pktlen = UINT32_MAX,
46 .max_rx_queues = UINT16_MAX,
47 .max_tx_queues = UINT16_MAX,
58 .rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP,
61 static int pmd_softnic_logtype;
63 #define PMD_LOG(level, fmt, args...) \
64 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
65 "%s(): " fmt "\n", __func__, ##args)
68 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
69 struct rte_eth_dev_info *dev_info)
71 memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
75 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
81 pmd_rx_queue_setup(struct rte_eth_dev *dev,
84 unsigned int socket_id,
85 const struct rte_eth_rxconf *rx_conf __rte_unused,
86 struct rte_mempool *mb_pool __rte_unused)
88 uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_rxq") + 4;
92 snprintf(name, sizeof(name), "%s_rxq%04x",
96 r = rte_ring_create(name,
99 RING_F_SP_ENQ | RING_F_SC_DEQ);
103 dev->data->rx_queues[rx_queue_id] = r;
108 pmd_tx_queue_setup(struct rte_eth_dev *dev,
109 uint16_t tx_queue_id,
111 unsigned int socket_id,
112 const struct rte_eth_txconf *tx_conf __rte_unused)
114 uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
118 snprintf(name, sizeof(name), "%s_txq%04x",
122 r = rte_ring_create(name,
125 RING_F_SP_ENQ | RING_F_SC_DEQ);
129 dev->data->tx_queues[tx_queue_id] = r;
134 pmd_dev_start(struct rte_eth_dev *dev)
136 dev->data->dev_link.link_status = ETH_LINK_UP;
142 pmd_dev_stop(struct rte_eth_dev *dev)
144 dev->data->dev_link.link_status = ETH_LINK_DOWN;
148 pmd_dev_close(struct rte_eth_dev *dev)
153 for (i = 0; i < dev->data->nb_rx_queues; i++)
154 rte_ring_free((struct rte_ring *)dev->data->rx_queues[i]);
157 for (i = 0; i < dev->data->nb_tx_queues; i++)
158 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
162 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
163 int wait_to_complete __rte_unused)
169 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
171 *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
176 static const struct eth_dev_ops pmd_ops = {
177 .dev_configure = pmd_dev_configure,
178 .dev_start = pmd_dev_start,
179 .dev_stop = pmd_dev_stop,
180 .dev_close = pmd_dev_close,
181 .link_update = pmd_link_update,
182 .dev_infos_get = pmd_dev_infos_get,
183 .rx_queue_setup = pmd_rx_queue_setup,
184 .tx_queue_setup = pmd_tx_queue_setup,
185 .tm_ops_get = pmd_tm_ops_get,
189 pmd_rx_pkt_burst(void *rxq,
190 struct rte_mbuf **rx_pkts,
193 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
200 pmd_tx_pkt_burst(void *txq,
201 struct rte_mbuf **tx_pkts,
204 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
211 rte_pmd_softnic_run(uint16_t port_id __rte_unused)
217 pmd_init(struct pmd_params *params)
219 struct pmd_internals *p;
221 p = rte_zmalloc_socket(params->name,
222 sizeof(struct pmd_internals),
229 memcpy(&p->params, params, sizeof(p->params));
232 softnic_mempool_init(p);
234 softnic_link_init(p);
236 softnic_tmgr_init(p);
238 softnic_port_in_action_profile_init(p);
239 softnic_table_action_profile_init(p);
240 softnic_pipeline_init(p);
246 pmd_free(struct pmd_internals *p)
251 softnic_pipeline_free(p);
252 softnic_table_action_profile_free(p);
253 softnic_port_in_action_profile_free(p);
255 softnic_tmgr_free(p);
257 softnic_link_free(p);
259 softnic_mempool_free(p);
264 static struct ether_addr eth_addr = {
269 pmd_ethdev_register(struct rte_vdev_device *vdev,
270 struct pmd_params *params,
273 struct rte_eth_dev *dev;
275 /* Ethdev entry allocation */
276 dev = rte_eth_dev_allocate(params->name);
281 dev->rx_pkt_burst = pmd_rx_pkt_burst;
282 dev->tx_pkt_burst = pmd_tx_pkt_burst;
283 dev->tx_pkt_prepare = NULL;
284 dev->dev_ops = &pmd_ops;
285 dev->device = &vdev->device;
288 dev->data->dev_private = dev_private;
289 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
290 dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
291 dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
292 dev->data->dev_link.link_status = ETH_LINK_DOWN;
293 dev->data->mac_addrs = ð_addr;
294 dev->data->promiscuous = 1;
295 dev->data->kdrv = RTE_KDRV_NONE;
296 dev->data->numa_node = params->cpu_id;
298 rte_eth_dev_probing_finish(dev);
304 get_string(const char *key __rte_unused, const char *value, void *extra_args)
306 if (!value || !extra_args)
309 *(char **)extra_args = strdup(value);
311 if (!*(char **)extra_args)
318 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
320 if (!value || !extra_args)
323 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
329 pmd_parse_args(struct pmd_params *p, const char *params)
331 struct rte_kvargs *kvlist;
334 kvlist = rte_kvargs_parse(params, pmd_valid_args);
338 /* Set default values */
339 memset(p, 0, sizeof(*p));
340 p->firmware = SOFTNIC_FIRMWARE;
341 p->cpu_id = SOFTNIC_CPU_ID;
342 p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
343 p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
344 p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
345 p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
346 p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
348 /* Firmware script (optional) */
349 if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
350 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
351 &get_string, &p->firmware);
356 /* CPU ID (optional) */
357 if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
358 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
359 &get_uint32, &p->cpu_id);
364 /* TM number of queues (optional) */
365 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
366 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
367 &get_uint32, &p->tm.n_queues);
372 /* TM queue size 0 .. 3 (optional) */
373 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
374 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
375 &get_uint32, &p->tm.qsize[0]);
380 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
381 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
382 &get_uint32, &p->tm.qsize[1]);
387 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
388 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
389 &get_uint32, &p->tm.qsize[2]);
394 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
395 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
396 &get_uint32, &p->tm.qsize[3]);
402 rte_kvargs_free(kvlist);
407 pmd_probe(struct rte_vdev_device *vdev)
414 const char *name = rte_vdev_device_name(vdev);
416 PMD_LOG(INFO, "Probing device \"%s\"", name);
418 /* Parse input arguments */
419 params = rte_vdev_device_args(vdev);
423 status = pmd_parse_args(&p, params);
429 /* Allocate and initialize soft ethdev private data */
430 dev_private = pmd_init(&p);
431 if (dev_private == NULL)
434 /* Register soft ethdev */
435 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
437 status = pmd_ethdev_register(vdev, &p, dev_private);
439 pmd_free(dev_private);
447 pmd_remove(struct rte_vdev_device *vdev)
449 struct rte_eth_dev *dev = NULL;
450 struct pmd_internals *p;
455 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
457 /* Find the ethdev entry */
458 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
461 p = dev->data->dev_private;
463 /* Free device data structures*/
465 rte_eth_dev_release_port(dev);
471 static struct rte_vdev_driver pmd_softnic_drv = {
473 .remove = pmd_remove,
476 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
477 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
478 PMD_PARAM_FIRMWARE "=<string> "
479 PMD_PARAM_CPU_ID "=<uint32> "
480 PMD_PARAM_TM_N_QUEUES "=<uint32> "
481 PMD_PARAM_TM_QSIZE0 "=<uint32> "
482 PMD_PARAM_TM_QSIZE1 "=<uint32> "
483 PMD_PARAM_TM_QSIZE2 "=<uint32> "
484 PMD_PARAM_TM_QSIZE3 "=<uint32>"
488 RTE_INIT(pmd_softnic_init_log)
490 pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
491 if (pmd_softnic_logtype >= 0)
492 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);