4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
38 #include <rte_memory.h>
41 #include "virtual_pmd.h"
43 #define MAX_PKT_BURST 512
45 static const char *virtual_ethdev_driver_name = "Virtual PMD";
47 struct virtual_ethdev_private {
48 struct eth_dev_ops dev_ops;
49 struct rte_eth_stats eth_stats;
51 struct rte_ring *rx_queue;
52 struct rte_ring *tx_queue;
54 int tx_burst_fail_count;
57 struct virtual_ethdev_queue {
63 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
65 eth_dev->data->dev_started = 1;
71 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
73 eth_dev->data->dev_started = 0;
77 static void virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
80 struct virtual_ethdev_private *prv = eth_dev->data->dev_private;
82 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
83 eth_dev->data->dev_started = 0;
84 while (rte_ring_dequeue(prv->rx_queue, &pkt) != -ENOENT)
85 rte_pktmbuf_free(pkt);
87 while (rte_ring_dequeue(prv->tx_queue, &pkt) != -ENOENT)
88 rte_pktmbuf_free(pkt);
92 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
96 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
102 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
108 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
109 struct rte_eth_dev_info *dev_info)
111 dev_info->driver_name = virtual_ethdev_driver_name;
112 dev_info->max_mac_addrs = 1;
114 dev_info->max_rx_pktlen = (uint32_t)2048;
116 dev_info->max_rx_queues = (uint16_t)128;
117 dev_info->max_tx_queues = (uint16_t)512;
119 dev_info->min_rx_bufsize = 0;
123 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
124 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
125 unsigned int socket_id,
126 const struct rte_eth_rxconf *rx_conf __rte_unused,
127 struct rte_mempool *mb_pool __rte_unused)
129 struct virtual_ethdev_queue *rx_q;
131 rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
132 sizeof(struct virtual_ethdev_queue), 0, socket_id);
137 rx_q->port_id = dev->data->port_id;
138 rx_q->queue_id = rx_queue_id;
140 dev->data->rx_queues[rx_queue_id] = rx_q;
146 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
147 uint16_t rx_queue_id __rte_unused, uint16_t nb_rx_desc __rte_unused,
148 unsigned int socket_id __rte_unused,
149 const struct rte_eth_rxconf *rx_conf __rte_unused,
150 struct rte_mempool *mb_pool __rte_unused)
156 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
157 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
158 unsigned int socket_id,
159 const struct rte_eth_txconf *tx_conf __rte_unused)
161 struct virtual_ethdev_queue *tx_q;
163 tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
164 sizeof(struct virtual_ethdev_queue), 0, socket_id);
169 tx_q->port_id = dev->data->port_id;
170 tx_q->queue_id = tx_queue_id;
172 dev->data->tx_queues[tx_queue_id] = tx_q;
178 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
179 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
180 unsigned int socket_id __rte_unused,
181 const struct rte_eth_txconf *tx_conf __rte_unused)
187 virtual_ethdev_rx_queue_release(void *q __rte_unused)
192 virtual_ethdev_tx_queue_release(void *q __rte_unused)
197 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
198 int wait_to_complete __rte_unused)
200 if (!bonded_eth_dev->data->dev_started)
201 bonded_eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
207 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
208 int wait_to_complete __rte_unused)
214 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
216 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
219 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
223 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
225 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
228 while (rte_ring_dequeue(dev_private->tx_queue, &pkt) == -ENOBUFS)
229 rte_pktmbuf_free(pkt);
231 /* Reset internal statistics */
232 memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
236 virtual_ethdev_promiscuous_mode_enable(struct rte_eth_dev *dev __rte_unused)
240 virtual_ethdev_promiscuous_mode_disable(struct rte_eth_dev *dev __rte_unused)
244 static const struct eth_dev_ops virtual_ethdev_default_dev_ops = {
245 .dev_configure = virtual_ethdev_configure_success,
246 .dev_start = virtual_ethdev_start_success,
247 .dev_stop = virtual_ethdev_stop,
248 .dev_close = virtual_ethdev_close,
249 .dev_infos_get = virtual_ethdev_info_get,
250 .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
251 .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
252 .rx_queue_release = virtual_ethdev_rx_queue_release,
253 .tx_queue_release = virtual_ethdev_tx_queue_release,
254 .link_update = virtual_ethdev_link_update_success,
255 .stats_get = virtual_ethdev_stats_get,
256 .stats_reset = virtual_ethdev_stats_reset,
257 .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
258 .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
263 virtual_ethdev_start_fn_set_success(uint8_t port_id, uint8_t success)
265 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
266 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
267 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
270 dev_ops->dev_start = virtual_ethdev_start_success;
272 dev_ops->dev_start = virtual_ethdev_start_fail;
277 virtual_ethdev_configure_fn_set_success(uint8_t port_id, uint8_t success)
279 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
280 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
281 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
284 dev_ops->dev_configure = virtual_ethdev_configure_success;
286 dev_ops->dev_configure = virtual_ethdev_configure_fail;
290 virtual_ethdev_rx_queue_setup_fn_set_success(uint8_t port_id, uint8_t success)
292 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
293 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
294 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
297 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_success;
299 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_fail;
303 virtual_ethdev_tx_queue_setup_fn_set_success(uint8_t port_id, uint8_t success)
305 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
306 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
307 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
310 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_success;
312 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_fail;
316 virtual_ethdev_link_update_fn_set_success(uint8_t port_id, uint8_t success)
318 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
319 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
320 struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
323 dev_ops->link_update = virtual_ethdev_link_update_success;
325 dev_ops->link_update = virtual_ethdev_link_update_fail;
330 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
331 struct rte_mbuf **bufs,
334 struct rte_eth_dev *vrtl_eth_dev;
335 struct virtual_ethdev_queue *pq_map;
336 struct virtual_ethdev_private *dev_private;
340 pq_map = (struct virtual_ethdev_queue *)queue;
341 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
342 dev_private = vrtl_eth_dev->data->dev_private;
344 rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
347 /* increments ipackets count */
348 dev_private->eth_stats.ipackets += rx_count;
350 /* increments ibytes count */
351 for (i = 0; i < rx_count; i++)
352 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
358 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
359 struct rte_mbuf **bufs __rte_unused,
360 uint16_t nb_pkts __rte_unused)
366 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
369 struct virtual_ethdev_queue *tx_q = (struct virtual_ethdev_queue *)queue;
371 struct rte_eth_dev *vrtl_eth_dev;
372 struct virtual_ethdev_private *dev_private;
376 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
377 dev_private = vrtl_eth_dev->data->dev_private;
379 if (!vrtl_eth_dev->data->dev_link.link_status)
382 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
385 /* increment opacket count */
386 dev_private->eth_stats.opackets += nb_pkts;
388 /* increment obytes count */
389 for (i = 0; i < nb_pkts; i++)
390 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
396 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
399 struct rte_eth_dev *vrtl_eth_dev = NULL;
400 struct virtual_ethdev_queue *tx_q = NULL;
401 struct virtual_ethdev_private *dev_private = NULL;
405 tx_q = (struct virtual_ethdev_queue *)queue;
406 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
407 dev_private = vrtl_eth_dev->data->dev_private;
409 if (dev_private->tx_burst_fail_count < nb_pkts) {
410 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
412 /* increment opacket count */
413 dev_private->eth_stats.opackets += successfully_txd;
415 /* free packets in burst */
416 for (i = 0; i < successfully_txd; i++) {
417 /* free packets in burst */
419 rte_pktmbuf_free(bufs[i]);
424 return successfully_txd;
432 virtual_ethdev_rx_burst_fn_set_success(uint8_t port_id, uint8_t success)
434 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
437 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
439 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
444 virtual_ethdev_tx_burst_fn_set_success(uint8_t port_id, uint8_t success)
446 struct virtual_ethdev_private *dev_private = NULL;
447 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
449 dev_private = vrtl_eth_dev->data->dev_private;
452 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
454 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
456 dev_private->tx_burst_fail_count = 0;
460 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint8_t port_id,
461 uint8_t packet_fail_count)
463 struct virtual_ethdev_private *dev_private = NULL;
464 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
467 dev_private = vrtl_eth_dev->data->dev_private;
468 dev_private->tx_burst_fail_count = packet_fail_count;
472 virtual_ethdev_set_link_status(uint8_t port_id, uint8_t link_status)
474 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
476 vrtl_eth_dev->data->dev_link.link_status = link_status;
480 virtual_ethdev_simulate_link_status_interrupt(uint8_t port_id,
483 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
485 vrtl_eth_dev->data->dev_link.link_status = link_status;
487 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
491 virtual_ethdev_add_mbufs_to_rx_queue(uint8_t port_id,
492 struct rte_mbuf **pkt_burst, int burst_length)
494 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
495 struct virtual_ethdev_private *dev_private =
496 vrtl_eth_dev->data->dev_private;
498 return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
503 virtual_ethdev_get_mbufs_from_tx_queue(uint8_t port_id,
504 struct rte_mbuf **pkt_burst, int burst_length)
506 struct virtual_ethdev_private *dev_private;
507 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
509 dev_private = vrtl_eth_dev->data->dev_private;
510 return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
515 get_number_of_sockets(void)
519 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
521 for (i = 0; i < RTE_MAX_MEMSEG && ms[i].addr != NULL; i++) {
522 if (sockets < ms[i].socket_id)
523 sockets = ms[i].socket_id;
525 /* Number of sockets = maximum socket_id + 1 */
530 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
531 uint8_t socket_id, uint8_t isr_support)
533 struct rte_pci_device *pci_dev = NULL;
534 struct rte_eth_dev *eth_dev = NULL;
535 struct eth_driver *eth_drv = NULL;
536 struct rte_pci_driver *pci_drv = NULL;
537 struct rte_pci_id *id_table = NULL;
538 struct virtual_ethdev_private *dev_private = NULL;
539 char name_buf[RTE_RING_NAMESIZE];
542 /* now do all data allocation - for eth_dev structure, dummy pci driver
543 * and internal (dev_private) data
546 if (socket_id >= get_number_of_sockets())
549 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
553 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
557 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
561 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
562 if (id_table == NULL)
564 id_table->device_id = 0xBEEF;
566 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
567 if (dev_private == NULL)
570 snprintf(name_buf, sizeof(name_buf), "%s_rxQ", name);
571 dev_private->rx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
573 if (dev_private->rx_queue == NULL)
576 snprintf(name_buf, sizeof(name_buf), "%s_txQ", name);
577 dev_private->tx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
579 if (dev_private->tx_queue == NULL)
582 /* reserve an ethdev entry */
583 eth_dev = rte_eth_dev_allocate(name);
587 pci_dev->device.numa_node = socket_id;
588 pci_drv->driver.name = virtual_ethdev_driver_name;
589 pci_drv->id_table = id_table;
592 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
594 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
597 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
598 eth_dev->driver = eth_drv;
600 eth_dev->data->nb_rx_queues = (uint16_t)1;
601 eth_dev->data->nb_tx_queues = (uint16_t)1;
603 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
604 eth_dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
605 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
607 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
608 if (eth_dev->data->mac_addrs == NULL)
611 memcpy(eth_dev->data->mac_addrs, mac_addr,
612 sizeof(*eth_dev->data->mac_addrs));
614 eth_dev->data->dev_started = 0;
615 eth_dev->data->promiscuous = 0;
616 eth_dev->data->scattered_rx = 0;
617 eth_dev->data->all_multicast = 0;
619 eth_dev->data->dev_private = dev_private;
621 /* Copy default device operation functions */
622 dev_private->dev_ops = virtual_ethdev_default_dev_ops;
623 eth_dev->dev_ops = &dev_private->dev_ops;
625 pci_dev->device.driver = ð_drv->pci_drv.driver;
626 eth_dev->device = &pci_dev->device;
628 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
629 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
631 return eth_dev->data->port_id;
638 rte_free(dev_private);