1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 #include <rte_ethdev.h>
7 #include <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 int 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);
68 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
74 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
80 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
86 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
87 struct rte_eth_dev_info *dev_info)
89 dev_info->driver_name = virtual_ethdev_driver_name;
90 dev_info->max_mac_addrs = 1;
92 dev_info->max_rx_pktlen = (uint32_t)2048;
94 dev_info->max_rx_queues = (uint16_t)128;
95 dev_info->max_tx_queues = (uint16_t)512;
97 dev_info->min_rx_bufsize = 0;
103 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
104 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
105 unsigned int socket_id,
106 const struct rte_eth_rxconf *rx_conf __rte_unused,
107 struct rte_mempool *mb_pool __rte_unused)
109 struct virtual_ethdev_queue *rx_q;
111 rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
112 sizeof(struct virtual_ethdev_queue), 0, socket_id);
117 rx_q->port_id = dev->data->port_id;
118 rx_q->queue_id = rx_queue_id;
120 dev->data->rx_queues[rx_queue_id] = rx_q;
126 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
127 uint16_t rx_queue_id __rte_unused, uint16_t nb_rx_desc __rte_unused,
128 unsigned int socket_id __rte_unused,
129 const struct rte_eth_rxconf *rx_conf __rte_unused,
130 struct rte_mempool *mb_pool __rte_unused)
136 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
137 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
138 unsigned int socket_id,
139 const struct rte_eth_txconf *tx_conf __rte_unused)
141 struct virtual_ethdev_queue *tx_q;
143 tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
144 sizeof(struct virtual_ethdev_queue), 0, socket_id);
149 tx_q->port_id = dev->data->port_id;
150 tx_q->queue_id = tx_queue_id;
152 dev->data->tx_queues[tx_queue_id] = tx_q;
158 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
159 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
160 unsigned int socket_id __rte_unused,
161 const struct rte_eth_txconf *tx_conf __rte_unused)
167 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
168 int wait_to_complete __rte_unused)
170 if (!bonded_eth_dev->data->dev_started)
171 bonded_eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
177 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
178 int wait_to_complete __rte_unused)
184 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
186 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
189 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
195 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
197 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
200 while (rte_ring_dequeue(dev_private->tx_queue, &pkt) == -ENOBUFS)
201 rte_pktmbuf_free(pkt);
203 /* Reset internal statistics */
204 memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
210 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)
222 virtual_ethdev_mac_address_set(__rte_unused struct rte_eth_dev *dev,
223 __rte_unused struct rte_ether_addr *addr)
228 static const struct eth_dev_ops virtual_ethdev_default_dev_ops = {
229 .dev_configure = virtual_ethdev_configure_success,
230 .dev_start = virtual_ethdev_start_success,
231 .dev_stop = virtual_ethdev_stop,
232 .dev_close = virtual_ethdev_close,
233 .dev_infos_get = virtual_ethdev_info_get,
234 .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
235 .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
236 .link_update = virtual_ethdev_link_update_success,
237 .mac_addr_set = virtual_ethdev_mac_address_set,
238 .stats_get = virtual_ethdev_stats_get,
239 .stats_reset = virtual_ethdev_stats_reset,
240 .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
241 .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
245 virtual_ethdev_start_fn_set_success(uint16_t port_id, uint8_t success)
247 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
248 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
249 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
252 dev_ops->dev_start = virtual_ethdev_start_success;
254 dev_ops->dev_start = virtual_ethdev_start_fail;
259 virtual_ethdev_configure_fn_set_success(uint16_t port_id, uint8_t success)
261 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
262 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
263 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
266 dev_ops->dev_configure = virtual_ethdev_configure_success;
268 dev_ops->dev_configure = virtual_ethdev_configure_fail;
272 virtual_ethdev_rx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
274 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
275 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
276 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
279 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_success;
281 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_fail;
285 virtual_ethdev_tx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
287 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
288 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
289 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
292 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_success;
294 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_fail;
298 virtual_ethdev_link_update_fn_set_success(uint16_t port_id, uint8_t success)
300 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
301 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
302 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
305 dev_ops->link_update = virtual_ethdev_link_update_success;
307 dev_ops->link_update = virtual_ethdev_link_update_fail;
312 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
313 struct rte_mbuf **bufs,
316 struct rte_eth_dev *vrtl_eth_dev;
317 struct virtual_ethdev_queue *pq_map;
318 struct virtual_ethdev_private *dev_private;
322 pq_map = (struct virtual_ethdev_queue *)queue;
323 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
324 dev_private = vrtl_eth_dev->data->dev_private;
326 rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
329 /* increments ipackets count */
330 dev_private->eth_stats.ipackets += rx_count;
332 /* increments ibytes count */
333 for (i = 0; i < rx_count; i++)
334 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
340 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
341 struct rte_mbuf **bufs __rte_unused,
342 uint16_t nb_pkts __rte_unused)
348 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
351 struct virtual_ethdev_queue *tx_q = queue;
353 struct rte_eth_dev *vrtl_eth_dev;
354 struct virtual_ethdev_private *dev_private;
358 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
359 dev_private = vrtl_eth_dev->data->dev_private;
361 if (!vrtl_eth_dev->data->dev_link.link_status)
364 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
367 /* increment opacket count */
368 dev_private->eth_stats.opackets += nb_pkts;
370 /* increment obytes count */
371 for (i = 0; i < nb_pkts; i++)
372 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
378 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
381 struct rte_eth_dev *vrtl_eth_dev = NULL;
382 struct virtual_ethdev_queue *tx_q = NULL;
383 struct virtual_ethdev_private *dev_private = NULL;
388 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
389 dev_private = vrtl_eth_dev->data->dev_private;
391 if (dev_private->tx_burst_fail_count < nb_pkts) {
392 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
394 /* increment opacket count */
395 dev_private->eth_stats.opackets += successfully_txd;
397 /* free packets in burst */
398 for (i = 0; i < successfully_txd; i++) {
399 /* free packets in burst */
401 rte_pktmbuf_free(bufs[i]);
406 return successfully_txd;
414 virtual_ethdev_rx_burst_fn_set_success(uint16_t port_id, uint8_t success)
416 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
419 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
421 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
426 virtual_ethdev_tx_burst_fn_set_success(uint16_t port_id, uint8_t success)
428 struct virtual_ethdev_private *dev_private = NULL;
429 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
431 dev_private = vrtl_eth_dev->data->dev_private;
434 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
436 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
438 dev_private->tx_burst_fail_count = 0;
442 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint16_t port_id,
443 uint8_t packet_fail_count)
445 struct virtual_ethdev_private *dev_private = NULL;
446 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
449 dev_private = vrtl_eth_dev->data->dev_private;
450 dev_private->tx_burst_fail_count = packet_fail_count;
454 virtual_ethdev_set_link_status(uint16_t port_id, uint8_t link_status)
456 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
458 vrtl_eth_dev->data->dev_link.link_status = link_status;
462 virtual_ethdev_simulate_link_status_interrupt(uint16_t port_id,
465 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
467 vrtl_eth_dev->data->dev_link.link_status = link_status;
469 rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC,
474 virtual_ethdev_add_mbufs_to_rx_queue(uint16_t port_id,
475 struct rte_mbuf **pkt_burst, int burst_length)
477 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
478 struct virtual_ethdev_private *dev_private =
479 vrtl_eth_dev->data->dev_private;
481 return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
486 virtual_ethdev_get_mbufs_from_tx_queue(uint16_t port_id,
487 struct rte_mbuf **pkt_burst, int burst_length)
489 struct virtual_ethdev_private *dev_private;
490 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
492 dev_private = vrtl_eth_dev->data->dev_private;
493 return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
499 virtual_ethdev_create(const char *name, struct rte_ether_addr *mac_addr,
500 uint8_t socket_id, uint8_t isr_support)
502 struct rte_pci_device *pci_dev = NULL;
503 struct rte_eth_dev *eth_dev = NULL;
504 struct rte_pci_driver *pci_drv = NULL;
505 struct rte_pci_id *id_table = NULL;
506 struct virtual_ethdev_private *dev_private = NULL;
507 char name_buf[RTE_RING_NAMESIZE];
510 /* now do all data allocation - for eth_dev structure, dummy pci driver
511 * and internal (dev_private) data
514 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
518 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
522 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
523 if (id_table == NULL)
525 id_table->device_id = 0xBEEF;
527 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
528 if (dev_private == NULL)
531 snprintf(name_buf, sizeof(name_buf), "%s_rxQ", name);
532 dev_private->rx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
534 if (dev_private->rx_queue == NULL)
537 snprintf(name_buf, sizeof(name_buf), "%s_txQ", name);
538 dev_private->tx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
540 if (dev_private->tx_queue == NULL)
543 /* reserve an ethdev entry */
544 eth_dev = rte_eth_dev_allocate(name);
548 pci_dev->device.numa_node = socket_id;
549 pci_dev->device.name = eth_dev->data->name;
550 pci_drv->driver.name = virtual_ethdev_driver_name;
551 pci_drv->id_table = id_table;
554 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
556 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
559 eth_dev->device = &pci_dev->device;
560 eth_dev->device->driver = &pci_drv->driver;
562 eth_dev->data->nb_rx_queues = (uint16_t)1;
563 eth_dev->data->nb_tx_queues = (uint16_t)1;
565 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
566 eth_dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
567 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
569 eth_dev->data->mac_addrs = rte_zmalloc(name, RTE_ETHER_ADDR_LEN, 0);
570 if (eth_dev->data->mac_addrs == NULL)
573 memcpy(eth_dev->data->mac_addrs, mac_addr,
574 sizeof(*eth_dev->data->mac_addrs));
576 eth_dev->data->dev_started = 0;
577 eth_dev->data->promiscuous = 0;
578 eth_dev->data->scattered_rx = 0;
579 eth_dev->data->all_multicast = 0;
581 eth_dev->data->dev_private = dev_private;
583 /* Copy default device operation functions */
584 dev_private->dev_ops = virtual_ethdev_default_dev_ops;
585 eth_dev->dev_ops = &dev_private->dev_ops;
587 pci_dev->device.driver = &pci_drv->driver;
588 eth_dev->device = &pci_dev->device;
590 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
591 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
593 rte_eth_dev_probing_finish(eth_dev);
595 return eth_dev->data->port_id;
601 rte_free(dev_private);