1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 #include <rte_ethdev.h>
8 #include <rte_bus_pci.h>
9 #include <rte_malloc.h>
10 #include <rte_memcpy.h>
11 #include <rte_memory.h>
14 #include "virtual_pmd.h"
16 #define MAX_PKT_BURST 512
18 static const char *virtual_ethdev_driver_name = "Virtual PMD";
20 struct virtual_ethdev_private {
21 struct eth_dev_ops dev_ops;
22 struct rte_eth_stats eth_stats;
24 struct rte_ring *rx_queue;
25 struct rte_ring *tx_queue;
27 int tx_burst_fail_count;
30 struct virtual_ethdev_queue {
36 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
38 eth_dev->data->dev_started = 1;
44 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
46 eth_dev->data->dev_started = 0;
50 static void virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
53 struct virtual_ethdev_private *prv = eth_dev->data->dev_private;
55 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
56 eth_dev->data->dev_started = 0;
57 while (rte_ring_dequeue(prv->rx_queue, &pkt) != -ENOENT)
58 rte_pktmbuf_free(pkt);
60 while (rte_ring_dequeue(prv->tx_queue, &pkt) != -ENOENT)
61 rte_pktmbuf_free(pkt);
65 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
69 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
75 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
81 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
82 struct rte_eth_dev_info *dev_info)
84 dev_info->driver_name = virtual_ethdev_driver_name;
85 dev_info->max_mac_addrs = 1;
87 dev_info->max_rx_pktlen = (uint32_t)2048;
89 dev_info->max_rx_queues = (uint16_t)128;
90 dev_info->max_tx_queues = (uint16_t)512;
92 dev_info->min_rx_bufsize = 0;
96 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
97 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
98 unsigned int socket_id,
99 const struct rte_eth_rxconf *rx_conf __rte_unused,
100 struct rte_mempool *mb_pool __rte_unused)
102 struct virtual_ethdev_queue *rx_q;
104 rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
105 sizeof(struct virtual_ethdev_queue), 0, socket_id);
110 rx_q->port_id = dev->data->port_id;
111 rx_q->queue_id = rx_queue_id;
113 dev->data->rx_queues[rx_queue_id] = rx_q;
119 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
120 uint16_t rx_queue_id __rte_unused, uint16_t nb_rx_desc __rte_unused,
121 unsigned int socket_id __rte_unused,
122 const struct rte_eth_rxconf *rx_conf __rte_unused,
123 struct rte_mempool *mb_pool __rte_unused)
129 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
130 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
131 unsigned int socket_id,
132 const struct rte_eth_txconf *tx_conf __rte_unused)
134 struct virtual_ethdev_queue *tx_q;
136 tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
137 sizeof(struct virtual_ethdev_queue), 0, socket_id);
142 tx_q->port_id = dev->data->port_id;
143 tx_q->queue_id = tx_queue_id;
145 dev->data->tx_queues[tx_queue_id] = tx_q;
151 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
152 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
153 unsigned int socket_id __rte_unused,
154 const struct rte_eth_txconf *tx_conf __rte_unused)
160 virtual_ethdev_rx_queue_release(void *q __rte_unused)
165 virtual_ethdev_tx_queue_release(void *q __rte_unused)
170 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
171 int wait_to_complete __rte_unused)
173 if (!bonded_eth_dev->data->dev_started)
174 bonded_eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
180 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
181 int wait_to_complete __rte_unused)
187 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
189 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
192 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
198 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
200 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
203 while (rte_ring_dequeue(dev_private->tx_queue, &pkt) == -ENOBUFS)
204 rte_pktmbuf_free(pkt);
206 /* Reset internal statistics */
207 memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
211 virtual_ethdev_promiscuous_mode_enable(struct rte_eth_dev *dev __rte_unused)
215 virtual_ethdev_promiscuous_mode_disable(struct rte_eth_dev *dev __rte_unused)
219 static const struct eth_dev_ops virtual_ethdev_default_dev_ops = {
220 .dev_configure = virtual_ethdev_configure_success,
221 .dev_start = virtual_ethdev_start_success,
222 .dev_stop = virtual_ethdev_stop,
223 .dev_close = virtual_ethdev_close,
224 .dev_infos_get = virtual_ethdev_info_get,
225 .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
226 .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
227 .rx_queue_release = virtual_ethdev_rx_queue_release,
228 .tx_queue_release = virtual_ethdev_tx_queue_release,
229 .link_update = virtual_ethdev_link_update_success,
230 .stats_get = virtual_ethdev_stats_get,
231 .stats_reset = virtual_ethdev_stats_reset,
232 .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
233 .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
238 virtual_ethdev_start_fn_set_success(uint16_t port_id, uint8_t success)
240 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
241 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
242 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
245 dev_ops->dev_start = virtual_ethdev_start_success;
247 dev_ops->dev_start = virtual_ethdev_start_fail;
252 virtual_ethdev_configure_fn_set_success(uint16_t port_id, uint8_t success)
254 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
255 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
256 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
259 dev_ops->dev_configure = virtual_ethdev_configure_success;
261 dev_ops->dev_configure = virtual_ethdev_configure_fail;
265 virtual_ethdev_rx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
267 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
268 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
269 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
272 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_success;
274 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_fail;
278 virtual_ethdev_tx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
280 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
281 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
282 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
285 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_success;
287 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_fail;
291 virtual_ethdev_link_update_fn_set_success(uint16_t port_id, uint8_t success)
293 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
294 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
295 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
298 dev_ops->link_update = virtual_ethdev_link_update_success;
300 dev_ops->link_update = virtual_ethdev_link_update_fail;
305 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
306 struct rte_mbuf **bufs,
309 struct rte_eth_dev *vrtl_eth_dev;
310 struct virtual_ethdev_queue *pq_map;
311 struct virtual_ethdev_private *dev_private;
315 pq_map = (struct virtual_ethdev_queue *)queue;
316 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
317 dev_private = vrtl_eth_dev->data->dev_private;
319 rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
322 /* increments ipackets count */
323 dev_private->eth_stats.ipackets += rx_count;
325 /* increments ibytes count */
326 for (i = 0; i < rx_count; i++)
327 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
333 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
334 struct rte_mbuf **bufs __rte_unused,
335 uint16_t nb_pkts __rte_unused)
341 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
344 struct virtual_ethdev_queue *tx_q = queue;
346 struct rte_eth_dev *vrtl_eth_dev;
347 struct virtual_ethdev_private *dev_private;
351 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
352 dev_private = vrtl_eth_dev->data->dev_private;
354 if (!vrtl_eth_dev->data->dev_link.link_status)
357 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
360 /* increment opacket count */
361 dev_private->eth_stats.opackets += nb_pkts;
363 /* increment obytes count */
364 for (i = 0; i < nb_pkts; i++)
365 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
371 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
374 struct rte_eth_dev *vrtl_eth_dev = NULL;
375 struct virtual_ethdev_queue *tx_q = NULL;
376 struct virtual_ethdev_private *dev_private = NULL;
381 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
382 dev_private = vrtl_eth_dev->data->dev_private;
384 if (dev_private->tx_burst_fail_count < nb_pkts) {
385 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
387 /* increment opacket count */
388 dev_private->eth_stats.opackets += successfully_txd;
390 /* free packets in burst */
391 for (i = 0; i < successfully_txd; i++) {
392 /* free packets in burst */
394 rte_pktmbuf_free(bufs[i]);
399 return successfully_txd;
407 virtual_ethdev_rx_burst_fn_set_success(uint16_t port_id, uint8_t success)
409 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
412 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
414 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
419 virtual_ethdev_tx_burst_fn_set_success(uint16_t port_id, uint8_t success)
421 struct virtual_ethdev_private *dev_private = NULL;
422 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
424 dev_private = vrtl_eth_dev->data->dev_private;
427 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
429 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
431 dev_private->tx_burst_fail_count = 0;
435 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint16_t port_id,
436 uint8_t packet_fail_count)
438 struct virtual_ethdev_private *dev_private = NULL;
439 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
442 dev_private = vrtl_eth_dev->data->dev_private;
443 dev_private->tx_burst_fail_count = packet_fail_count;
447 virtual_ethdev_set_link_status(uint16_t port_id, uint8_t link_status)
449 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
451 vrtl_eth_dev->data->dev_link.link_status = link_status;
455 virtual_ethdev_simulate_link_status_interrupt(uint16_t port_id,
458 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
460 vrtl_eth_dev->data->dev_link.link_status = link_status;
462 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC,
467 virtual_ethdev_add_mbufs_to_rx_queue(uint16_t port_id,
468 struct rte_mbuf **pkt_burst, int burst_length)
470 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
471 struct virtual_ethdev_private *dev_private =
472 vrtl_eth_dev->data->dev_private;
474 return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
479 virtual_ethdev_get_mbufs_from_tx_queue(uint16_t port_id,
480 struct rte_mbuf **pkt_burst, int burst_length)
482 struct virtual_ethdev_private *dev_private;
483 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
485 dev_private = vrtl_eth_dev->data->dev_private;
486 return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
492 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
493 uint8_t socket_id, uint8_t isr_support)
495 struct rte_pci_device *pci_dev = NULL;
496 struct rte_eth_dev *eth_dev = NULL;
497 struct rte_pci_driver *pci_drv = NULL;
498 struct rte_pci_id *id_table = NULL;
499 struct virtual_ethdev_private *dev_private = NULL;
500 char name_buf[RTE_RING_NAMESIZE];
503 /* now do all data allocation - for eth_dev structure, dummy pci driver
504 * and internal (dev_private) data
507 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
511 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
515 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
516 if (id_table == NULL)
518 id_table->device_id = 0xBEEF;
520 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
521 if (dev_private == NULL)
524 snprintf(name_buf, sizeof(name_buf), "%s_rxQ", name);
525 dev_private->rx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
527 if (dev_private->rx_queue == NULL)
530 snprintf(name_buf, sizeof(name_buf), "%s_txQ", name);
531 dev_private->tx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
533 if (dev_private->tx_queue == NULL)
536 /* reserve an ethdev entry */
537 eth_dev = rte_eth_dev_allocate(name);
541 pci_dev->device.numa_node = socket_id;
542 pci_dev->device.name = eth_dev->data->name;
543 pci_drv->driver.name = virtual_ethdev_driver_name;
544 pci_drv->id_table = id_table;
547 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
549 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
552 eth_dev->device = &pci_dev->device;
553 eth_dev->device->driver = &pci_drv->driver;
555 eth_dev->data->nb_rx_queues = (uint16_t)1;
556 eth_dev->data->nb_tx_queues = (uint16_t)1;
558 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
559 eth_dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
560 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
562 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
563 if (eth_dev->data->mac_addrs == NULL)
566 memcpy(eth_dev->data->mac_addrs, mac_addr,
567 sizeof(*eth_dev->data->mac_addrs));
569 eth_dev->data->dev_started = 0;
570 eth_dev->data->promiscuous = 0;
571 eth_dev->data->scattered_rx = 0;
572 eth_dev->data->all_multicast = 0;
574 eth_dev->data->dev_private = dev_private;
576 /* Copy default device operation functions */
577 dev_private->dev_ops = virtual_ethdev_default_dev_ops;
578 eth_dev->dev_ops = &dev_private->dev_ops;
580 pci_dev->device.driver = &pci_drv->driver;
581 eth_dev->device = &pci_dev->device;
583 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
584 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
586 return eth_dev->data->port_id;
592 rte_free(dev_private);