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 rte_eth_stats eth_stats;
50 struct rte_ring *rx_queue;
51 struct rte_ring *tx_queue;
53 int tx_burst_fail_count;
56 struct virtual_ethdev_queue {
62 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
64 eth_dev->data->dev_started = 1;
70 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
72 eth_dev->data->dev_started = 0;
76 static void virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
79 struct virtual_ethdev_private *prv = eth_dev->data->dev_private;
81 eth_dev->data->dev_link.link_status = 0;
82 eth_dev->data->dev_started = 0;
83 while (rte_ring_dequeue(prv->rx_queue, &pkt) != -ENOENT)
84 rte_pktmbuf_free(pkt);
86 while (rte_ring_dequeue(prv->tx_queue, &pkt) != -ENOENT)
87 rte_pktmbuf_free(pkt);
91 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
95 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
101 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
107 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
108 struct rte_eth_dev_info *dev_info)
110 dev_info->driver_name = virtual_ethdev_driver_name;
111 dev_info->max_mac_addrs = 1;
113 dev_info->max_rx_pktlen = (uint32_t)2048;
115 dev_info->max_rx_queues = (uint16_t)128;
116 dev_info->max_tx_queues = (uint16_t)512;
118 dev_info->min_rx_bufsize = 0;
119 dev_info->pci_dev = NULL;
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 = 0;
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 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 *vrtl_eth_dev = &rte_eth_devices[port_id];
268 vrtl_eth_dev->dev_ops->dev_start = virtual_ethdev_start_success;
270 vrtl_eth_dev->dev_ops->dev_start = virtual_ethdev_start_fail;
275 virtual_ethdev_configure_fn_set_success(uint8_t port_id, uint8_t success)
277 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
280 vrtl_eth_dev->dev_ops->dev_configure = virtual_ethdev_configure_success;
282 vrtl_eth_dev->dev_ops->dev_configure = virtual_ethdev_configure_fail;
286 virtual_ethdev_rx_queue_setup_fn_set_success(uint8_t port_id, uint8_t success)
288 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
291 vrtl_eth_dev->dev_ops->rx_queue_setup =
292 virtual_ethdev_rx_queue_setup_success;
294 vrtl_eth_dev->dev_ops->rx_queue_setup =
295 virtual_ethdev_rx_queue_setup_fail;
299 virtual_ethdev_tx_queue_setup_fn_set_success(uint8_t port_id, uint8_t success)
301 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
304 vrtl_eth_dev->dev_ops->tx_queue_setup =
305 virtual_ethdev_tx_queue_setup_success;
307 vrtl_eth_dev->dev_ops->tx_queue_setup =
308 virtual_ethdev_tx_queue_setup_fail;
312 virtual_ethdev_link_update_fn_set_success(uint8_t port_id, uint8_t success)
314 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
317 vrtl_eth_dev->dev_ops->link_update = virtual_ethdev_link_update_success;
319 vrtl_eth_dev->dev_ops->link_update = virtual_ethdev_link_update_fail;
324 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
325 struct rte_mbuf **bufs,
328 struct rte_eth_dev *vrtl_eth_dev;
329 struct virtual_ethdev_queue *pq_map;
330 struct virtual_ethdev_private *dev_private;
334 pq_map = (struct virtual_ethdev_queue *)queue;
335 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
336 dev_private = vrtl_eth_dev->data->dev_private;
338 rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
341 /* increments ipackets count */
342 dev_private->eth_stats.ipackets += rx_count;
344 /* increments ibytes count */
345 for (i = 0; i < rx_count; i++)
346 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
352 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
353 struct rte_mbuf **bufs __rte_unused,
354 uint16_t nb_pkts __rte_unused)
360 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
363 struct virtual_ethdev_queue *tx_q = (struct virtual_ethdev_queue *)queue;
365 struct rte_eth_dev *vrtl_eth_dev;
366 struct virtual_ethdev_private *dev_private;
370 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
371 dev_private = vrtl_eth_dev->data->dev_private;
373 if (!vrtl_eth_dev->data->dev_link.link_status)
376 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
379 /* increment opacket count */
380 dev_private->eth_stats.opackets += nb_pkts;
382 /* increment obytes count */
383 for (i = 0; i < nb_pkts; i++)
384 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
390 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
393 struct rte_eth_dev *vrtl_eth_dev = NULL;
394 struct virtual_ethdev_queue *tx_q = NULL;
395 struct virtual_ethdev_private *dev_private = NULL;
399 tx_q = (struct virtual_ethdev_queue *)queue;
400 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
401 dev_private = vrtl_eth_dev->data->dev_private;
403 if (dev_private->tx_burst_fail_count < nb_pkts) {
404 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
406 /* increment opacket count */
407 dev_private->eth_stats.opackets += successfully_txd;
409 /* free packets in burst */
410 for (i = 0; i < successfully_txd; i++) {
411 /* free packets in burst */
413 rte_pktmbuf_free(bufs[i]);
418 return successfully_txd;
426 virtual_ethdev_rx_burst_fn_set_success(uint8_t port_id, uint8_t success)
428 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
431 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
433 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
438 virtual_ethdev_tx_burst_fn_set_success(uint8_t port_id, uint8_t success)
440 struct virtual_ethdev_private *dev_private = NULL;
441 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
443 dev_private = vrtl_eth_dev->data->dev_private;
446 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
448 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
450 dev_private->tx_burst_fail_count = 0;
454 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint8_t port_id,
455 uint8_t packet_fail_count)
457 struct virtual_ethdev_private *dev_private = NULL;
458 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
461 dev_private = vrtl_eth_dev->data->dev_private;
462 dev_private->tx_burst_fail_count = packet_fail_count;
466 virtual_ethdev_set_link_status(uint8_t port_id, uint8_t link_status)
468 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
470 vrtl_eth_dev->data->dev_link.link_status = link_status;
474 virtual_ethdev_simulate_link_status_interrupt(uint8_t port_id,
477 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
479 vrtl_eth_dev->data->dev_link.link_status = link_status;
481 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC);
485 virtual_ethdev_add_mbufs_to_rx_queue(uint8_t port_id,
486 struct rte_mbuf **pkt_burst, int burst_length)
488 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
489 struct virtual_ethdev_private *dev_private =
490 vrtl_eth_dev->data->dev_private;
492 return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
497 virtual_ethdev_get_mbufs_from_tx_queue(uint8_t port_id,
498 struct rte_mbuf **pkt_burst, int burst_length)
500 struct virtual_ethdev_private *dev_private;
501 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
503 dev_private = vrtl_eth_dev->data->dev_private;
504 return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
509 get_number_of_sockets(void)
513 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
515 for (i = 0; i < RTE_MAX_MEMSEG && ms[i].addr != NULL; i++) {
516 if (sockets < ms[i].socket_id)
517 sockets = ms[i].socket_id;
519 /* Number of sockets = maximum socket_id + 1 */
524 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
525 uint8_t socket_id, uint8_t isr_support)
527 struct rte_pci_device *pci_dev = NULL;
528 struct rte_eth_dev *eth_dev = NULL;
529 struct eth_driver *eth_drv = NULL;
530 struct rte_pci_driver *pci_drv = NULL;
531 struct eth_dev_ops *dev_ops = NULL;
532 struct rte_pci_id *id_table = NULL;
533 struct virtual_ethdev_private *dev_private = NULL;
534 char name_buf[RTE_RING_NAMESIZE];
537 /* now do all data allocation - for eth_dev structure, dummy pci driver
538 * and internal (dev_private) data
541 if (socket_id >= get_number_of_sockets())
544 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
548 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
552 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
556 dev_ops = rte_zmalloc_socket(name, sizeof(*dev_ops), 0, socket_id);
560 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
561 if (id_table == NULL)
564 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
565 if (dev_private == NULL)
568 memset(dev_private, 0, sizeof(*dev_private));
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, RTE_ETH_DEV_PCI);
587 pci_dev->numa_node = socket_id;
588 pci_drv->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 TAILQ_INIT(&(eth_dev->link_intr_cbs));
605 eth_dev->data->dev_link.link_status = 0;
606 eth_dev->data->dev_link.link_speed = ETH_LINK_SPEED_10000;
607 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
609 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
610 if (eth_dev->data->mac_addrs == NULL)
613 memcpy(eth_dev->data->mac_addrs, mac_addr,
614 sizeof(*eth_dev->data->mac_addrs));
616 eth_dev->data->dev_started = 0;
617 eth_dev->data->promiscuous = 0;
618 eth_dev->data->scattered_rx = 0;
619 eth_dev->data->all_multicast = 0;
621 eth_dev->data->dev_private = dev_private;
623 eth_dev->dev_ops = dev_ops;
625 /* Copy default device operation functions */
626 memcpy(eth_dev->dev_ops, &virtual_ethdev_default_dev_ops,
627 sizeof(*eth_dev->dev_ops));
629 eth_dev->pci_dev = pci_dev;
630 eth_dev->pci_dev->driver = ð_drv->pci_drv;
632 eth_dev->pci_dev->driver->id_table->device_id = 0xBEEF;
634 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
635 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
637 return eth_dev->data->port_id;
651 rte_free(dev_private);