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>
40 #include "virtual_pmd.h"
42 #define MAX_PKT_BURST 512
44 static const char *virtual_ethdev_driver_name = "Virtual PMD";
46 struct virtual_ethdev_private {
47 struct rte_eth_stats eth_stats;
49 struct rte_mbuf *rx_pkt_burst[MAX_PKT_BURST];
52 int tx_burst_fail_count;
55 struct virtual_ethdev_queue {
61 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
63 eth_dev->data->dev_started = 1;
69 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
71 eth_dev->data->dev_started = 0;
75 static void virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
77 eth_dev->data->dev_link.link_status = 0;
78 eth_dev->data->dev_started = 0;
82 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
86 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
92 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
98 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
99 struct rte_eth_dev_info *dev_info)
101 dev_info->driver_name = virtual_ethdev_driver_name;
102 dev_info->max_mac_addrs = 1;
104 dev_info->max_rx_pktlen = (uint32_t)2048;
106 dev_info->max_rx_queues = (uint16_t)128;
107 dev_info->max_tx_queues = (uint16_t)512;
109 dev_info->min_rx_bufsize = 0;
110 dev_info->pci_dev = NULL;
114 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
115 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
116 unsigned int socket_id,
117 const struct rte_eth_rxconf *rx_conf __rte_unused,
118 struct rte_mempool *mb_pool __rte_unused)
120 struct virtual_ethdev_queue *rx_q;
122 rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
123 sizeof(struct virtual_ethdev_queue), 0, socket_id);
128 rx_q->port_id = dev->data->port_id;
129 rx_q->queue_id = rx_queue_id;
131 dev->data->rx_queues[rx_queue_id] = rx_q;
137 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
138 uint16_t rx_queue_id __rte_unused, uint16_t nb_rx_desc __rte_unused,
139 unsigned int socket_id __rte_unused,
140 const struct rte_eth_rxconf *rx_conf __rte_unused,
141 struct rte_mempool *mb_pool __rte_unused)
147 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
148 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
149 unsigned int socket_id,
150 const struct rte_eth_txconf *tx_conf __rte_unused)
152 struct virtual_ethdev_queue *tx_q;
154 tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
155 sizeof(struct virtual_ethdev_queue), 0, socket_id);
160 tx_q->port_id = dev->data->port_id;
161 tx_q->queue_id = tx_queue_id;
163 dev->data->tx_queues[tx_queue_id] = tx_q;
169 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
170 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
171 unsigned int socket_id __rte_unused,
172 const struct rte_eth_txconf *tx_conf __rte_unused)
178 virtual_ethdev_rx_queue_release(void *q __rte_unused)
183 virtual_ethdev_tx_queue_release(void *q __rte_unused)
188 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
189 int wait_to_complete __rte_unused)
191 if (!bonded_eth_dev->data->dev_started)
192 bonded_eth_dev->data->dev_link.link_status = 0;
198 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
199 int wait_to_complete __rte_unused)
205 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
207 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
210 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
214 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
216 struct virtual_ethdev_private *dev_private = dev->data->dev_private;
218 dev_private->rx_pkt_burst_len = 0;
220 /* Reset internal statistics */
221 memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
225 virtual_ethdev_promiscuous_mode_enable(struct rte_eth_dev *dev __rte_unused)
229 virtual_ethdev_promiscuous_mode_disable(struct rte_eth_dev *dev __rte_unused)
233 static struct eth_dev_ops virtual_ethdev_default_dev_ops = {
234 .dev_configure = virtual_ethdev_configure_success,
235 .dev_start = virtual_ethdev_start_success,
236 .dev_stop = virtual_ethdev_stop,
237 .dev_close = virtual_ethdev_close,
238 .dev_infos_get = virtual_ethdev_info_get,
239 .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
240 .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
241 .rx_queue_release = virtual_ethdev_rx_queue_release,
242 .tx_queue_release = virtual_ethdev_tx_queue_release,
243 .link_update = virtual_ethdev_link_update_success,
244 .stats_get = virtual_ethdev_stats_get,
245 .stats_reset = virtual_ethdev_stats_reset,
246 .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
247 .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
252 virtual_ethdev_start_fn_set_success(uint8_t port_id, uint8_t success)
254 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
257 vrtl_eth_dev->dev_ops->dev_start = virtual_ethdev_start_success;
259 vrtl_eth_dev->dev_ops->dev_start = virtual_ethdev_start_fail;
264 virtual_ethdev_configure_fn_set_success(uint8_t port_id, uint8_t success)
266 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
269 vrtl_eth_dev->dev_ops->dev_configure = virtual_ethdev_configure_success;
271 vrtl_eth_dev->dev_ops->dev_configure = virtual_ethdev_configure_fail;
275 virtual_ethdev_rx_queue_setup_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->rx_queue_setup =
281 virtual_ethdev_rx_queue_setup_success;
283 vrtl_eth_dev->dev_ops->rx_queue_setup =
284 virtual_ethdev_rx_queue_setup_fail;
288 virtual_ethdev_tx_queue_setup_fn_set_success(uint8_t port_id, uint8_t success)
290 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
293 vrtl_eth_dev->dev_ops->tx_queue_setup =
294 virtual_ethdev_tx_queue_setup_success;
296 vrtl_eth_dev->dev_ops->tx_queue_setup =
297 virtual_ethdev_tx_queue_setup_fail;
301 virtual_ethdev_link_update_fn_set_success(uint8_t port_id, uint8_t success)
303 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
306 vrtl_eth_dev->dev_ops->link_update = virtual_ethdev_link_update_success;
308 vrtl_eth_dev->dev_ops->link_update = virtual_ethdev_link_update_fail;
313 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
314 struct rte_mbuf **bufs,
317 struct rte_eth_dev *vrtl_eth_dev;
318 struct virtual_ethdev_queue *pq_map;
319 struct virtual_ethdev_private *dev_private;
323 pq_map = (struct virtual_ethdev_queue *)queue;
325 vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
327 dev_private = vrtl_eth_dev->data->dev_private;
329 if (dev_private->rx_pkt_burst_len > 0) {
330 if (dev_private->rx_pkt_burst_len < nb_pkts) {
332 for (i = 0; i < dev_private->rx_pkt_burst_len; i++) {
333 bufs[i] = dev_private->rx_pkt_burst[i];
334 dev_private->rx_pkt_burst[i] = NULL;
337 dev_private->eth_stats.ipackets = dev_private->rx_pkt_burst_len;
339 /* reset private burst values */
340 dev_private->rx_pkt_burst_len = 0;
343 return dev_private->eth_stats.ipackets;
347 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
348 struct rte_mbuf **bufs __rte_unused,
349 uint16_t nb_pkts __rte_unused)
355 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
358 struct virtual_ethdev_queue *tx_q = (struct virtual_ethdev_queue *)queue;
360 struct rte_eth_dev *vrtl_eth_dev;
361 struct virtual_ethdev_private *dev_private;
365 for (i = 0; i < nb_pkts; i++)
366 obytes += rte_pktmbuf_pkt_len(bufs[i]);
367 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
368 dev_private = vrtl_eth_dev->data->dev_private;
370 if (vrtl_eth_dev->data->dev_link.link_status) {
371 /* increment opacket count */
372 dev_private->eth_stats.opackets += nb_pkts;
373 dev_private->eth_stats.obytes += obytes;
374 /* free packets in burst */
375 for (i = 0; i < nb_pkts; i++)
376 rte_pktmbuf_free(bufs[i]);
385 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
388 struct rte_eth_dev *vrtl_eth_dev = NULL;
389 struct virtual_ethdev_queue *tx_q = NULL;
390 struct virtual_ethdev_private *dev_private = NULL;
394 tx_q = (struct virtual_ethdev_queue *)queue;
395 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
396 dev_private = vrtl_eth_dev->data->dev_private;
398 if (dev_private->tx_burst_fail_count < nb_pkts) {
399 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
401 /* increment opacket count */
402 dev_private->eth_stats.opackets += successfully_txd;
404 /* free packets in burst */
405 for (i = 0; i < successfully_txd; i++) {
406 /* free packets in burst */
408 rte_pktmbuf_free(bufs[i]);
413 return successfully_txd;
421 virtual_ethdev_rx_burst_fn_set_success(uint8_t port_id, uint8_t success)
423 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
426 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
428 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
433 virtual_ethdev_tx_burst_fn_set_success(uint8_t port_id, uint8_t success)
435 struct virtual_ethdev_private *dev_private = NULL;
436 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
438 dev_private = vrtl_eth_dev->data->dev_private;
441 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
443 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
445 dev_private->tx_burst_fail_count = 0;
449 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint8_t port_id,
450 uint8_t packet_fail_count)
452 struct virtual_ethdev_private *dev_private = NULL;
453 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
456 dev_private = vrtl_eth_dev->data->dev_private;
457 dev_private->tx_burst_fail_count = packet_fail_count;
461 virtual_ethdev_set_link_status(uint8_t port_id, uint8_t link_status)
463 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
465 vrtl_eth_dev->data->dev_link.link_status = link_status;
469 virtual_ethdev_simulate_link_status_interrupt(uint8_t port_id,
472 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
474 vrtl_eth_dev->data->dev_link.link_status = link_status;
476 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC);
482 virtual_ethdev_add_mbufs_to_rx_queue(uint8_t port_id,
483 struct rte_mbuf **pkt_burst, int burst_length)
485 struct virtual_ethdev_private *dev_private = NULL;
486 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
490 dev_private = vrtl_eth_dev->data->dev_private;
492 for (i = 0; i < burst_length; i++)
493 dev_private->rx_pkt_burst[i] = pkt_burst[i];
495 dev_private->rx_pkt_burst_len = burst_length;
499 get_number_of_sockets(void)
503 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
505 for (i = 0; i < RTE_MAX_MEMSEG && ms[i].addr != NULL; i++) {
506 if (sockets < ms[i].socket_id)
507 sockets = ms[i].socket_id;
509 /* Number of sockets = maximum socket_id + 1 */
515 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
516 uint8_t socket_id, uint8_t isr_support)
518 struct rte_pci_device *pci_dev = NULL;
519 struct rte_eth_dev *eth_dev = NULL;
520 struct eth_driver *eth_drv = NULL;
521 struct rte_pci_driver *pci_drv = NULL;
522 struct eth_dev_ops *dev_ops = NULL;
523 struct rte_pci_id *id_table = NULL;
524 struct virtual_ethdev_private *dev_private = NULL;
527 /* now do all data allocation - for eth_dev structure, dummy pci driver
528 * and internal (dev_private) data
531 if (socket_id >= get_number_of_sockets())
534 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
538 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
542 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
546 dev_ops = rte_zmalloc_socket(name, sizeof(*dev_ops), 0, socket_id);
550 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
551 if (id_table == NULL)
554 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
555 if (dev_private == NULL)
558 /* reserve an ethdev entry */
559 eth_dev = rte_eth_dev_allocate(name);
563 pci_dev->numa_node = socket_id;
564 pci_drv->name = virtual_ethdev_driver_name;
565 pci_drv->id_table = id_table;
568 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
570 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
573 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
574 eth_dev->driver = eth_drv;
576 eth_dev->data->nb_rx_queues = (uint16_t)1;
577 eth_dev->data->nb_tx_queues = (uint16_t)1;
579 TAILQ_INIT(&(eth_dev->callbacks));
581 eth_dev->data->dev_link.link_status = 0;
582 eth_dev->data->dev_link.link_speed = ETH_LINK_SPEED_10000;
583 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
585 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
586 if (eth_dev->data->mac_addrs == NULL)
589 memcpy(eth_dev->data->mac_addrs, mac_addr,
590 sizeof(*eth_dev->data->mac_addrs));
592 eth_dev->data->dev_started = 0;
593 eth_dev->data->promiscuous = 0;
594 eth_dev->data->scattered_rx = 0;
595 eth_dev->data->all_multicast = 0;
597 memset(dev_private, 0, sizeof(*dev_private));
598 eth_dev->data->dev_private = dev_private;
600 eth_dev->dev_ops = dev_ops;
602 /* Copy default device operation functions */
603 memcpy(eth_dev->dev_ops, &virtual_ethdev_default_dev_ops,
604 sizeof(*eth_dev->dev_ops));
606 eth_dev->pci_dev = pci_dev;
607 eth_dev->pci_dev->driver = ð_drv->pci_drv;
609 eth_dev->pci_dev->driver->id_table->device_id = 0xBEEF;
611 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
612 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
614 return eth_dev->data->port_id;
628 rte_free(dev_private);