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"
24 static const char *pmd_valid_args[] = {
30 static const struct rte_eth_dev_info pmd_dev_info = {
32 .max_rx_pktlen = UINT32_MAX,
33 .max_rx_queues = UINT16_MAX,
34 .max_tx_queues = UINT16_MAX,
45 .rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP,
48 static int pmd_softnic_logtype;
50 #define PMD_LOG(level, fmt, args...) \
51 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
52 "%s(): " fmt "\n", __func__, ##args)
55 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
56 struct rte_eth_dev_info *dev_info)
58 memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
62 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
68 pmd_rx_queue_setup(struct rte_eth_dev *dev,
71 unsigned int socket_id,
72 const struct rte_eth_rxconf *rx_conf __rte_unused,
73 struct rte_mempool *mb_pool __rte_unused)
75 uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_rxq") + 4;
79 snprintf(name, sizeof(name), "%s_rxq%04x",
83 r = rte_ring_create(name,
86 RING_F_SP_ENQ | RING_F_SC_DEQ);
90 dev->data->rx_queues[rx_queue_id] = r;
95 pmd_tx_queue_setup(struct rte_eth_dev *dev,
98 unsigned int socket_id,
99 const struct rte_eth_txconf *tx_conf __rte_unused)
101 uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
105 snprintf(name, sizeof(name), "%s_txq%04x",
109 r = rte_ring_create(name,
112 RING_F_SP_ENQ | RING_F_SC_DEQ);
116 dev->data->tx_queues[tx_queue_id] = r;
121 pmd_dev_start(struct rte_eth_dev *dev)
123 dev->data->dev_link.link_status = ETH_LINK_UP;
129 pmd_dev_stop(struct rte_eth_dev *dev)
131 dev->data->dev_link.link_status = ETH_LINK_DOWN;
135 pmd_dev_close(struct rte_eth_dev *dev)
140 for (i = 0; i < dev->data->nb_rx_queues; i++)
141 rte_ring_free((struct rte_ring *)dev->data->rx_queues[i]);
144 for (i = 0; i < dev->data->nb_tx_queues; i++)
145 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
149 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
150 int wait_to_complete __rte_unused)
156 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
158 *(const struct rte_tm_ops **)arg = NULL;
163 static const struct eth_dev_ops pmd_ops = {
164 .dev_configure = pmd_dev_configure,
165 .dev_start = pmd_dev_start,
166 .dev_stop = pmd_dev_stop,
167 .dev_close = pmd_dev_close,
168 .link_update = pmd_link_update,
169 .dev_infos_get = pmd_dev_infos_get,
170 .rx_queue_setup = pmd_rx_queue_setup,
171 .tx_queue_setup = pmd_tx_queue_setup,
172 .tm_ops_get = pmd_tm_ops_get,
176 pmd_rx_pkt_burst(void *rxq,
177 struct rte_mbuf **rx_pkts,
180 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
187 pmd_tx_pkt_burst(void *txq,
188 struct rte_mbuf **tx_pkts,
191 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
198 rte_pmd_softnic_run(uint16_t port_id __rte_unused)
204 pmd_init(struct pmd_params *params)
206 struct pmd_internals *p;
208 p = rte_zmalloc_socket(params->name,
209 sizeof(struct pmd_internals),
216 memcpy(&p->params, params, sizeof(p->params));
224 pmd_free(struct pmd_internals *p)
233 static struct ether_addr eth_addr = {
238 pmd_ethdev_register(struct rte_vdev_device *vdev,
239 struct pmd_params *params,
242 struct rte_eth_dev *dev;
244 /* Ethdev entry allocation */
245 dev = rte_eth_dev_allocate(params->name);
250 dev->rx_pkt_burst = pmd_rx_pkt_burst;
251 dev->tx_pkt_burst = pmd_tx_pkt_burst;
252 dev->tx_pkt_prepare = NULL;
253 dev->dev_ops = &pmd_ops;
254 dev->device = &vdev->device;
257 dev->data->dev_private = dev_private;
258 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
259 dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
260 dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
261 dev->data->dev_link.link_status = ETH_LINK_DOWN;
262 dev->data->mac_addrs = ð_addr;
263 dev->data->promiscuous = 1;
264 dev->data->kdrv = RTE_KDRV_NONE;
265 dev->data->numa_node = params->cpu_id;
267 rte_eth_dev_probing_finish(dev);
273 get_string(const char *key __rte_unused, const char *value, void *extra_args)
275 if (!value || !extra_args)
278 *(char **)extra_args = strdup(value);
280 if (!*(char **)extra_args)
287 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
289 if (!value || !extra_args)
292 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
298 pmd_parse_args(struct pmd_params *p, const char *params)
300 struct rte_kvargs *kvlist;
303 kvlist = rte_kvargs_parse(params, pmd_valid_args);
307 /* Set default values */
308 memset(p, 0, sizeof(*p));
309 p->firmware = SOFTNIC_FIRMWARE;
310 p->cpu_id = SOFTNIC_CPU_ID;
312 /* Firmware script (optional) */
313 if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
314 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
315 &get_string, &p->firmware);
320 /* CPU ID (optional) */
321 if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
322 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
323 &get_uint32, &p->cpu_id);
329 rte_kvargs_free(kvlist);
334 pmd_probe(struct rte_vdev_device *vdev)
341 const char *name = rte_vdev_device_name(vdev);
343 PMD_LOG(INFO, "Probing device \"%s\"", name);
345 /* Parse input arguments */
346 params = rte_vdev_device_args(vdev);
350 status = pmd_parse_args(&p, params);
356 /* Allocate and initialize soft ethdev private data */
357 dev_private = pmd_init(&p);
358 if (dev_private == NULL)
361 /* Register soft ethdev */
362 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
364 status = pmd_ethdev_register(vdev, &p, dev_private);
366 pmd_free(dev_private);
374 pmd_remove(struct rte_vdev_device *vdev)
376 struct rte_eth_dev *dev = NULL;
377 struct pmd_internals *p;
382 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
384 /* Find the ethdev entry */
385 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
388 p = dev->data->dev_private;
390 /* Free device data structures*/
392 rte_eth_dev_release_port(dev);
398 static struct rte_vdev_driver pmd_softnic_drv = {
400 .remove = pmd_remove,
403 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
404 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
405 PMD_PARAM_FIRMWARE "=<string> "
406 PMD_PARAM_CPU_ID "=<uint32>");
408 RTE_INIT(pmd_softnic_init_log)
410 pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
411 if (pmd_softnic_logtype >= 0)
412 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);