1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 #include <rte_ethdev.h>
7 #include <rte_ethdev_driver.h>
9 #include <rte_bus_pci.h>
10 #include <rte_malloc.h>
11 #include <rte_memcpy.h>
12 #include <rte_memory.h>
15 #include "virtual_pmd.h"
17 #define MAX_PKT_BURST 512
19 static const char *virtual_ethdev_driver_name = "Virtual PMD";
21 struct virtual_ethdev_private {
22 struct eth_dev_ops dev_ops;
23 struct rte_eth_stats eth_stats;
25 struct rte_ring *rx_queue;
26 struct rte_ring *tx_queue;
28 int tx_burst_fail_count;
31 struct virtual_ethdev_queue {
37 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
39 eth_dev->data->dev_started = 1;
45 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
47 eth_dev->data->dev_started = 0;
51 static void virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
54 struct virtual_ethdev_private *prv = eth_dev->data->dev_private;
56 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
57 eth_dev->data->dev_started = 0;
58 while (rte_ring_dequeue(prv->rx_queue, &pkt) != -ENOENT)
59 rte_pktmbuf_free(pkt);
61 while (rte_ring_dequeue(prv->tx_queue, &pkt) != -ENOENT)
62 rte_pktmbuf_free(pkt);
66 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
70 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
76 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
82 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
83 struct rte_eth_dev_info *dev_info)
85 dev_info->driver_name = virtual_ethdev_driver_name;
86 dev_info->max_mac_addrs = 1;
88 dev_info->max_rx_pktlen = (uint32_t)2048;
90 dev_info->max_rx_queues = (uint16_t)128;
91 dev_info->max_tx_queues = (uint16_t)512;
93 dev_info->min_rx_bufsize = 0;
97 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
98 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
99 unsigned int socket_id,
100 const struct rte_eth_rxconf *rx_conf __rte_unused,
101 struct rte_mempool *mb_pool __rte_unused)
103 struct virtual_ethdev_queue *rx_q;
105 rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
106 sizeof(struct virtual_ethdev_queue), 0, socket_id);
111 rx_q->port_id = dev->data->port_id;
112 rx_q->queue_id = rx_queue_id;
114 dev->data->rx_queues[rx_queue_id] = rx_q;
120 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
121 uint16_t rx_queue_id __rte_unused, uint16_t nb_rx_desc __rte_unused,
122 unsigned int socket_id __rte_unused,
123 const struct rte_eth_rxconf *rx_conf __rte_unused,
124 struct rte_mempool *mb_pool __rte_unused)
130 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
131 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
132 unsigned int socket_id,
133 const struct rte_eth_txconf *tx_conf __rte_unused)
135 struct virtual_ethdev_queue *tx_q;
137 tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
138 sizeof(struct virtual_ethdev_queue), 0, socket_id);
143 tx_q->port_id = dev->data->port_id;
144 tx_q->queue_id = tx_queue_id;
146 dev->data->tx_queues[tx_queue_id] = tx_q;
152 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
153 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
154 unsigned int socket_id __rte_unused,
155 const struct rte_eth_txconf *tx_conf __rte_unused)
161 virtual_ethdev_rx_queue_release(void *q __rte_unused)
166 virtual_ethdev_tx_queue_release(void *q __rte_unused)
171 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
172 int wait_to_complete __rte_unused)
174 if (!bonded_eth_dev->data->dev_started)
175 bonded_eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
181 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
182 int wait_to_complete __rte_unused)
188 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
190 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
193 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
199 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
201 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
204 while (rte_ring_dequeue(dev_private->tx_queue, &pkt) == -ENOBUFS)
205 rte_pktmbuf_free(pkt);
207 /* Reset internal statistics */
208 memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
212 virtual_ethdev_promiscuous_mode_enable(struct rte_eth_dev *dev __rte_unused)
216 virtual_ethdev_promiscuous_mode_disable(struct rte_eth_dev *dev __rte_unused)
220 static const struct eth_dev_ops virtual_ethdev_default_dev_ops = {
221 .dev_configure = virtual_ethdev_configure_success,
222 .dev_start = virtual_ethdev_start_success,
223 .dev_stop = virtual_ethdev_stop,
224 .dev_close = virtual_ethdev_close,
225 .dev_infos_get = virtual_ethdev_info_get,
226 .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
227 .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
228 .rx_queue_release = virtual_ethdev_rx_queue_release,
229 .tx_queue_release = virtual_ethdev_tx_queue_release,
230 .link_update = virtual_ethdev_link_update_success,
231 .stats_get = virtual_ethdev_stats_get,
232 .stats_reset = virtual_ethdev_stats_reset,
233 .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
234 .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
239 virtual_ethdev_start_fn_set_success(uint16_t port_id, uint8_t success)
241 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
242 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
243 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
246 dev_ops->dev_start = virtual_ethdev_start_success;
248 dev_ops->dev_start = virtual_ethdev_start_fail;
253 virtual_ethdev_configure_fn_set_success(uint16_t port_id, uint8_t success)
255 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
256 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
257 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
260 dev_ops->dev_configure = virtual_ethdev_configure_success;
262 dev_ops->dev_configure = virtual_ethdev_configure_fail;
266 virtual_ethdev_rx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
268 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
269 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
270 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
273 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_success;
275 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_fail;
279 virtual_ethdev_tx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
281 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
282 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
283 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
286 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_success;
288 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_fail;
292 virtual_ethdev_link_update_fn_set_success(uint16_t port_id, uint8_t success)
294 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
295 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
296 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
299 dev_ops->link_update = virtual_ethdev_link_update_success;
301 dev_ops->link_update = virtual_ethdev_link_update_fail;
306 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
307 struct rte_mbuf **bufs,
310 struct rte_eth_dev *vrtl_eth_dev;
311 struct virtual_ethdev_queue *pq_map;
312 struct virtual_ethdev_private *dev_private;
316 pq_map = (struct virtual_ethdev_queue *)queue;
317 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
318 dev_private = vrtl_eth_dev->data->dev_private;
320 rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
323 /* increments ipackets count */
324 dev_private->eth_stats.ipackets += rx_count;
326 /* increments ibytes count */
327 for (i = 0; i < rx_count; i++)
328 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
334 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
335 struct rte_mbuf **bufs __rte_unused,
336 uint16_t nb_pkts __rte_unused)
342 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
345 struct virtual_ethdev_queue *tx_q = queue;
347 struct rte_eth_dev *vrtl_eth_dev;
348 struct virtual_ethdev_private *dev_private;
352 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
353 dev_private = vrtl_eth_dev->data->dev_private;
355 if (!vrtl_eth_dev->data->dev_link.link_status)
358 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
361 /* increment opacket count */
362 dev_private->eth_stats.opackets += nb_pkts;
364 /* increment obytes count */
365 for (i = 0; i < nb_pkts; i++)
366 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
372 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
375 struct rte_eth_dev *vrtl_eth_dev = NULL;
376 struct virtual_ethdev_queue *tx_q = NULL;
377 struct virtual_ethdev_private *dev_private = NULL;
382 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
383 dev_private = vrtl_eth_dev->data->dev_private;
385 if (dev_private->tx_burst_fail_count < nb_pkts) {
386 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
388 /* increment opacket count */
389 dev_private->eth_stats.opackets += successfully_txd;
391 /* free packets in burst */
392 for (i = 0; i < successfully_txd; i++) {
393 /* free packets in burst */
395 rte_pktmbuf_free(bufs[i]);
400 return successfully_txd;
408 virtual_ethdev_rx_burst_fn_set_success(uint16_t port_id, uint8_t success)
410 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
413 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
415 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
420 virtual_ethdev_tx_burst_fn_set_success(uint16_t port_id, uint8_t success)
422 struct virtual_ethdev_private *dev_private = NULL;
423 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
425 dev_private = vrtl_eth_dev->data->dev_private;
428 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
430 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
432 dev_private->tx_burst_fail_count = 0;
436 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint16_t port_id,
437 uint8_t packet_fail_count)
439 struct virtual_ethdev_private *dev_private = NULL;
440 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
443 dev_private = vrtl_eth_dev->data->dev_private;
444 dev_private->tx_burst_fail_count = packet_fail_count;
448 virtual_ethdev_set_link_status(uint16_t port_id, uint8_t link_status)
450 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
452 vrtl_eth_dev->data->dev_link.link_status = link_status;
456 virtual_ethdev_simulate_link_status_interrupt(uint16_t port_id,
459 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
461 vrtl_eth_dev->data->dev_link.link_status = link_status;
463 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC,
468 virtual_ethdev_add_mbufs_to_rx_queue(uint16_t port_id,
469 struct rte_mbuf **pkt_burst, int burst_length)
471 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
472 struct virtual_ethdev_private *dev_private =
473 vrtl_eth_dev->data->dev_private;
475 return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
480 virtual_ethdev_get_mbufs_from_tx_queue(uint16_t port_id,
481 struct rte_mbuf **pkt_burst, int burst_length)
483 struct virtual_ethdev_private *dev_private;
484 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
486 dev_private = vrtl_eth_dev->data->dev_private;
487 return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
493 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
494 uint8_t socket_id, uint8_t isr_support)
496 struct rte_pci_device *pci_dev = NULL;
497 struct rte_eth_dev *eth_dev = NULL;
498 struct rte_pci_driver *pci_drv = NULL;
499 struct rte_pci_id *id_table = NULL;
500 struct virtual_ethdev_private *dev_private = NULL;
501 char name_buf[RTE_RING_NAMESIZE];
504 /* now do all data allocation - for eth_dev structure, dummy pci driver
505 * and internal (dev_private) data
508 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
512 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
516 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
517 if (id_table == NULL)
519 id_table->device_id = 0xBEEF;
521 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
522 if (dev_private == NULL)
525 snprintf(name_buf, sizeof(name_buf), "%s_rxQ", name);
526 dev_private->rx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
528 if (dev_private->rx_queue == NULL)
531 snprintf(name_buf, sizeof(name_buf), "%s_txQ", name);
532 dev_private->tx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
534 if (dev_private->tx_queue == NULL)
537 /* reserve an ethdev entry */
538 eth_dev = rte_eth_dev_allocate(name);
542 pci_dev->device.numa_node = socket_id;
543 pci_dev->device.name = eth_dev->data->name;
544 pci_drv->driver.name = virtual_ethdev_driver_name;
545 pci_drv->id_table = id_table;
548 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
550 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
553 eth_dev->device = &pci_dev->device;
554 eth_dev->device->driver = &pci_drv->driver;
556 eth_dev->data->nb_rx_queues = (uint16_t)1;
557 eth_dev->data->nb_tx_queues = (uint16_t)1;
559 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
560 eth_dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
561 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
563 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
564 if (eth_dev->data->mac_addrs == NULL)
567 memcpy(eth_dev->data->mac_addrs, mac_addr,
568 sizeof(*eth_dev->data->mac_addrs));
570 eth_dev->data->dev_started = 0;
571 eth_dev->data->promiscuous = 0;
572 eth_dev->data->scattered_rx = 0;
573 eth_dev->data->all_multicast = 0;
575 eth_dev->data->dev_private = dev_private;
577 /* Copy default device operation functions */
578 dev_private->dev_ops = virtual_ethdev_default_dev_ops;
579 eth_dev->dev_ops = &dev_private->dev_ops;
581 pci_dev->device.driver = &pci_drv->driver;
582 eth_dev->device = &pci_dev->device;
584 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
585 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
587 return eth_dev->data->port_id;
593 rte_free(dev_private);