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 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
366 dev_private = vrtl_eth_dev->data->dev_private;
368 if (vrtl_eth_dev->data->dev_link.link_status) {
369 /* increment opacket count */
370 dev_private->eth_stats.opackets += nb_pkts;
372 /* free packets in burst */
373 for (i = 0; i < nb_pkts; i++)
374 rte_pktmbuf_free(bufs[i]);
383 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
386 struct rte_eth_dev *vrtl_eth_dev = NULL;
387 struct virtual_ethdev_queue *tx_q = NULL;
388 struct virtual_ethdev_private *dev_private = NULL;
392 tx_q = (struct virtual_ethdev_queue *)queue;
393 vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
394 dev_private = vrtl_eth_dev->data->dev_private;
396 if (dev_private->tx_burst_fail_count < nb_pkts) {
397 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
399 /* increment opacket count */
400 dev_private->eth_stats.opackets += successfully_txd;
402 /* free packets in burst */
403 for (i = 0; i < successfully_txd; i++) {
404 /* free packets in burst */
406 rte_pktmbuf_free(bufs[i]);
411 return successfully_txd;
419 virtual_ethdev_rx_burst_fn_set_success(uint8_t port_id, uint8_t success)
421 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
424 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
426 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
431 virtual_ethdev_tx_burst_fn_set_success(uint8_t port_id, uint8_t success)
433 struct virtual_ethdev_private *dev_private = NULL;
434 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
436 dev_private = vrtl_eth_dev->data->dev_private;
439 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
441 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
443 dev_private->tx_burst_fail_count = 0;
447 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint8_t port_id,
448 uint8_t packet_fail_count)
450 struct virtual_ethdev_private *dev_private = NULL;
451 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
454 dev_private = vrtl_eth_dev->data->dev_private;
455 dev_private->tx_burst_fail_count = packet_fail_count;
459 virtual_ethdev_set_link_status(uint8_t port_id, uint8_t link_status)
461 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
463 vrtl_eth_dev->data->dev_link.link_status = link_status;
467 virtual_ethdev_simulate_link_status_interrupt(uint8_t port_id,
470 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
472 vrtl_eth_dev->data->dev_link.link_status = link_status;
474 _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC);
480 virtual_ethdev_add_mbufs_to_rx_queue(uint8_t port_id,
481 struct rte_mbuf **pkt_burst, int burst_length)
483 struct virtual_ethdev_private *dev_private = NULL;
484 struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
488 dev_private = vrtl_eth_dev->data->dev_private;
490 for (i = 0; i < burst_length; i++)
491 dev_private->rx_pkt_burst[i] = pkt_burst[i];
493 dev_private->rx_pkt_burst_len = burst_length;
497 get_number_of_sockets(void)
501 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
503 for (i = 0; i < RTE_MAX_MEMSEG && ms[i].addr != NULL; i++) {
504 if (sockets < ms[i].socket_id)
505 sockets = ms[i].socket_id;
507 /* Number of sockets = maximum socket_id + 1 */
513 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
514 uint8_t socket_id, uint8_t isr_support)
516 struct rte_pci_device *pci_dev = NULL;
517 struct rte_eth_dev *eth_dev = NULL;
518 struct eth_driver *eth_drv = NULL;
519 struct rte_pci_driver *pci_drv = NULL;
520 struct eth_dev_ops *dev_ops = NULL;
521 struct rte_pci_id *id_table = NULL;
522 struct virtual_ethdev_private *dev_private = NULL;
525 /* now do all data allocation - for eth_dev structure, dummy pci driver
526 * and internal (dev_private) data
529 if (socket_id >= get_number_of_sockets())
532 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
536 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
540 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
544 dev_ops = rte_zmalloc_socket(name, sizeof(*dev_ops), 0, socket_id);
548 id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
549 if (id_table == NULL)
552 dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
553 if (dev_private == NULL)
556 /* reserve an ethdev entry */
557 eth_dev = rte_eth_dev_allocate(name);
561 pci_dev->numa_node = socket_id;
562 pci_drv->name = virtual_ethdev_driver_name;
563 pci_drv->id_table = id_table;
566 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
568 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
571 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
572 eth_dev->driver = eth_drv;
574 eth_dev->data->nb_rx_queues = (uint16_t)1;
575 eth_dev->data->nb_tx_queues = (uint16_t)1;
577 TAILQ_INIT(&(eth_dev->callbacks));
579 eth_dev->data->dev_link.link_status = 0;
580 eth_dev->data->dev_link.link_speed = ETH_LINK_SPEED_10000;
581 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
583 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
584 if (eth_dev->data->mac_addrs == NULL)
587 memcpy(eth_dev->data->mac_addrs, mac_addr,
588 sizeof(*eth_dev->data->mac_addrs));
589 eth_dev->data->mac_addrs->addr_bytes[5] = eth_dev->data->port_id;
591 eth_dev->data->dev_started = 0;
592 eth_dev->data->promiscuous = 0;
593 eth_dev->data->scattered_rx = 0;
594 eth_dev->data->all_multicast = 0;
596 memset(dev_private, 0, sizeof(*dev_private));
597 eth_dev->data->dev_private = dev_private;
599 eth_dev->dev_ops = dev_ops;
601 /* Copy default device operation functions */
602 memcpy(eth_dev->dev_ops, &virtual_ethdev_default_dev_ops,
603 sizeof(*eth_dev->dev_ops));
605 eth_dev->pci_dev = pci_dev;
606 eth_dev->pci_dev->driver = ð_drv->pci_drv;
608 eth_dev->pci_dev->driver->id_table->device_id = 0xBEEF;
610 eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
611 eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
613 return eth_dev->data->port_id;
627 rte_free(dev_private);