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);
243 pmd_free(struct pmd_internals *p)
249 softnic_tmgr_free(p);
251 softnic_link_free(p);
253 softnic_mempool_free(p);
258 static struct ether_addr eth_addr = {
263 pmd_ethdev_register(struct rte_vdev_device *vdev,
264 struct pmd_params *params,
267 struct rte_eth_dev *dev;
269 /* Ethdev entry allocation */
270 dev = rte_eth_dev_allocate(params->name);
275 dev->rx_pkt_burst = pmd_rx_pkt_burst;
276 dev->tx_pkt_burst = pmd_tx_pkt_burst;
277 dev->tx_pkt_prepare = NULL;
278 dev->dev_ops = &pmd_ops;
279 dev->device = &vdev->device;
282 dev->data->dev_private = dev_private;
283 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
284 dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
285 dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
286 dev->data->dev_link.link_status = ETH_LINK_DOWN;
287 dev->data->mac_addrs = ð_addr;
288 dev->data->promiscuous = 1;
289 dev->data->kdrv = RTE_KDRV_NONE;
290 dev->data->numa_node = params->cpu_id;
292 rte_eth_dev_probing_finish(dev);
298 get_string(const char *key __rte_unused, const char *value, void *extra_args)
300 if (!value || !extra_args)
303 *(char **)extra_args = strdup(value);
305 if (!*(char **)extra_args)
312 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
314 if (!value || !extra_args)
317 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
323 pmd_parse_args(struct pmd_params *p, const char *params)
325 struct rte_kvargs *kvlist;
328 kvlist = rte_kvargs_parse(params, pmd_valid_args);
332 /* Set default values */
333 memset(p, 0, sizeof(*p));
334 p->firmware = SOFTNIC_FIRMWARE;
335 p->cpu_id = SOFTNIC_CPU_ID;
336 p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
337 p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
338 p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
339 p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
340 p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
342 /* Firmware script (optional) */
343 if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
344 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
345 &get_string, &p->firmware);
350 /* CPU ID (optional) */
351 if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
352 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
353 &get_uint32, &p->cpu_id);
358 /* TM number of queues (optional) */
359 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
360 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
361 &get_uint32, &p->tm.n_queues);
366 /* TM queue size 0 .. 3 (optional) */
367 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
368 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
369 &get_uint32, &p->tm.qsize[0]);
374 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
375 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
376 &get_uint32, &p->tm.qsize[1]);
381 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
382 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
383 &get_uint32, &p->tm.qsize[2]);
388 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
389 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
390 &get_uint32, &p->tm.qsize[3]);
396 rte_kvargs_free(kvlist);
401 pmd_probe(struct rte_vdev_device *vdev)
408 const char *name = rte_vdev_device_name(vdev);
410 PMD_LOG(INFO, "Probing device \"%s\"", name);
412 /* Parse input arguments */
413 params = rte_vdev_device_args(vdev);
417 status = pmd_parse_args(&p, params);
423 /* Allocate and initialize soft ethdev private data */
424 dev_private = pmd_init(&p);
425 if (dev_private == NULL)
428 /* Register soft ethdev */
429 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
431 status = pmd_ethdev_register(vdev, &p, dev_private);
433 pmd_free(dev_private);
441 pmd_remove(struct rte_vdev_device *vdev)
443 struct rte_eth_dev *dev = NULL;
444 struct pmd_internals *p;
449 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
451 /* Find the ethdev entry */
452 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
455 p = dev->data->dev_private;
457 /* Free device data structures*/
459 rte_eth_dev_release_port(dev);
465 static struct rte_vdev_driver pmd_softnic_drv = {
467 .remove = pmd_remove,
470 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
471 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
472 PMD_PARAM_FIRMWARE "=<string> "
473 PMD_PARAM_CPU_ID "=<uint32> "
474 PMD_PARAM_TM_N_QUEUES "=<uint32> "
475 PMD_PARAM_TM_QSIZE0 "=<uint32> "
476 PMD_PARAM_TM_QSIZE1 "=<uint32> "
477 PMD_PARAM_TM_QSIZE2 "=<uint32> "
478 PMD_PARAM_TM_QSIZE3 "=<uint32>"
482 RTE_INIT(pmd_softnic_init_log)
484 pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
485 if (pmd_softnic_logtype >= 0)
486 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);