4 * Copyright(c) 2010-2016 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.
40 #include <rte_ethdev.h>
41 #include <rte_memcpy.h>
42 #include <rte_string_fns.h>
43 #include <rte_memzone.h>
44 #include <rte_malloc.h>
45 #include <rte_atomic.h>
46 #include <rte_branch_prediction.h>
48 #include <rte_ether.h>
49 #include <rte_common.h>
50 #include <rte_errno.h>
52 #include <rte_memory.h>
56 #include "virtio_ethdev.h"
57 #include "virtio_pci.h"
58 #include "virtio_logs.h"
59 #include "virtqueue.h"
60 #include "virtio_rxtx.h"
62 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
63 static int virtio_dev_configure(struct rte_eth_dev *dev);
64 static int virtio_dev_start(struct rte_eth_dev *dev);
65 static void virtio_dev_stop(struct rte_eth_dev *dev);
66 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
67 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
68 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
69 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
70 static void virtio_dev_info_get(struct rte_eth_dev *dev,
71 struct rte_eth_dev_info *dev_info);
72 static int virtio_dev_link_update(struct rte_eth_dev *dev,
73 __rte_unused int wait_to_complete);
75 static void virtio_set_hwaddr(struct virtio_hw *hw);
76 static void virtio_get_hwaddr(struct virtio_hw *hw);
78 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
79 struct rte_eth_stats *stats);
80 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
81 struct rte_eth_xstat *xstats, unsigned n);
82 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
83 struct rte_eth_xstat_name *xstats_names,
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
88 uint16_t vlan_id, int on);
89 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
90 struct ether_addr *mac_addr,
91 uint32_t index, uint32_t vmdq __rte_unused);
92 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
93 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94 struct ether_addr *mac_addr);
96 static int virtio_dev_queue_stats_mapping_set(
97 __rte_unused struct rte_eth_dev *eth_dev,
98 __rte_unused uint16_t queue_id,
99 __rte_unused uint8_t stat_idx,
100 __rte_unused uint8_t is_rx);
103 * The set of PCI devices this driver supports
105 static const struct rte_pci_id pci_id_virtio_map[] = {
107 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
108 #include "rte_pci_dev_ids.h"
110 { .vendor_id = 0, /* sentinel */ },
113 struct rte_virtio_xstats_name_off {
114 char name[RTE_ETH_XSTATS_NAME_SIZE];
118 /* [rt]x_qX_ is prepended to the name string here */
119 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
120 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
121 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
122 {"errors", offsetof(struct virtnet_rx, stats.errors)},
123 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
124 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
125 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
126 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
127 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
128 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
129 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
130 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
131 {"size_1024_1517_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
132 {"size_1518_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
135 /* [rt]x_qX_ is prepended to the name string here */
136 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
137 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
138 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
139 {"errors", offsetof(struct virtnet_tx, stats.errors)},
140 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
141 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
142 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
143 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
144 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
145 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
146 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
147 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
148 {"size_1024_1517_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
149 {"size_1518_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
152 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
153 sizeof(rte_virtio_rxq_stat_strings[0]))
154 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
155 sizeof(rte_virtio_txq_stat_strings[0]))
158 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
159 int *dlen, int pkt_num)
163 virtio_net_ctrl_ack status = ~0;
164 struct virtio_pmd_ctrl result;
165 struct virtqueue *vq;
167 ctrl->status = status;
169 if (!cvq && !cvq->vq) {
170 PMD_INIT_LOG(ERR, "Control queue is not supported.");
174 head = vq->vq_desc_head_idx;
176 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
177 "vq->hw->cvq = %p vq = %p",
178 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
180 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
183 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
184 sizeof(struct virtio_pmd_ctrl));
187 * Format is enforced in qemu code:
188 * One TX packet for header;
189 * At least one TX packet per argument;
190 * One RX packet for ACK.
192 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
193 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
194 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
196 i = vq->vq_ring.desc[head].next;
198 for (k = 0; k < pkt_num; k++) {
199 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
200 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
201 + sizeof(struct virtio_net_ctrl_hdr)
202 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
203 vq->vq_ring.desc[i].len = dlen[k];
206 i = vq->vq_ring.desc[i].next;
209 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
210 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
211 + sizeof(struct virtio_net_ctrl_hdr);
212 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
215 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
217 vq_update_avail_ring(vq, head);
218 vq_update_avail_idx(vq);
220 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
222 virtqueue_notify(vq);
225 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
230 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
231 uint32_t idx, desc_idx, used_idx;
232 struct vring_used_elem *uep;
234 used_idx = (uint32_t)(vq->vq_used_cons_idx
235 & (vq->vq_nentries - 1));
236 uep = &vq->vq_ring.used->ring[used_idx];
237 idx = (uint32_t) uep->id;
240 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
241 desc_idx = vq->vq_ring.desc[desc_idx].next;
245 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
246 vq->vq_desc_head_idx = idx;
248 vq->vq_used_cons_idx++;
252 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
253 vq->vq_free_cnt, vq->vq_desc_head_idx);
255 memcpy(&result, cvq->virtio_net_hdr_mz->addr,
256 sizeof(struct virtio_pmd_ctrl));
258 return result.status;
262 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
264 struct virtio_hw *hw = dev->data->dev_private;
265 struct virtio_pmd_ctrl ctrl;
269 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
270 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
271 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
273 dlen[0] = sizeof(uint16_t);
275 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
277 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
278 "failed, this is too late now...");
286 virtio_dev_queue_release(struct virtqueue *vq)
288 struct virtio_hw *hw;
293 hw->vtpci_ops->del_queue(hw, vq);
295 rte_free(vq->sw_ring);
300 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
303 uint16_t vtpci_queue_idx,
305 unsigned int socket_id,
308 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
309 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
310 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
311 unsigned int vq_size, size;
312 struct virtio_hw *hw = dev->data->dev_private;
313 struct virtnet_rx *rxvq = NULL;
314 struct virtnet_tx *txvq = NULL;
315 struct virtnet_ctl *cvq = NULL;
316 struct virtqueue *vq;
317 const char *queue_names[] = {"rvq", "txq", "cvq"};
318 size_t sz_vq, sz_q = 0, sz_hdr_mz = 0;
319 void *sw_ring = NULL;
322 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
325 * Read the virtqueue size from the Queue Size field
326 * Always power of 2 and if 0 virtqueue does not exist
328 vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
329 PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
331 PMD_INIT_LOG(ERR, "virtqueue does not exist");
335 if (!rte_is_power_of_2(vq_size)) {
336 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
340 snprintf(vq_name, sizeof(vq_name), "port%d_%s%d",
341 dev->data->port_id, queue_names[queue_type], queue_idx);
343 sz_vq = RTE_ALIGN_CEIL(sizeof(*vq) +
344 vq_size * sizeof(struct vq_desc_extra),
345 RTE_CACHE_LINE_SIZE);
346 if (queue_type == VTNET_RQ) {
347 sz_q = sz_vq + sizeof(*rxvq);
348 } else if (queue_type == VTNET_TQ) {
349 sz_q = sz_vq + sizeof(*txvq);
351 * For each xmit packet, allocate a virtio_net_hdr
352 * and indirect ring elements
354 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
355 } else if (queue_type == VTNET_CQ) {
356 sz_q = sz_vq + sizeof(*cvq);
357 /* Allocate a page for control vq command, data and status */
358 sz_hdr_mz = PAGE_SIZE;
361 vq = rte_zmalloc_socket(vq_name, sz_q, RTE_CACHE_LINE_SIZE, socket_id);
363 PMD_INIT_LOG(ERR, "can not allocate vq");
367 vq->vq_queue_index = vtpci_queue_idx;
368 vq->vq_nentries = vq_size;
370 if (nb_desc == 0 || nb_desc > vq_size)
372 vq->vq_free_cnt = nb_desc;
375 * Reserve a memzone for vring elements
377 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
378 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
379 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
380 size, vq->vq_ring_size);
382 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, socket_id,
383 0, VIRTIO_PCI_VRING_ALIGN);
385 if (rte_errno == EEXIST)
386 mz = rte_memzone_lookup(vq_name);
393 memset(mz->addr, 0, sizeof(mz->len));
395 vq->vq_ring_mem = mz->phys_addr;
396 vq->vq_ring_virt_mem = mz->addr;
397 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
398 (uint64_t)mz->phys_addr);
399 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
400 (uint64_t)(uintptr_t)mz->addr);
403 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_%s%d_hdr",
404 dev->data->port_id, queue_names[queue_type],
406 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
408 RTE_CACHE_LINE_SIZE);
409 if (hdr_mz == NULL) {
410 if (rte_errno == EEXIST)
411 hdr_mz = rte_memzone_lookup(vq_hdr_name);
412 if (hdr_mz == NULL) {
419 if (queue_type == VTNET_RQ) {
420 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
421 sizeof(vq->sw_ring[0]);
423 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
424 RTE_CACHE_LINE_SIZE, socket_id);
426 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
431 vq->sw_ring = sw_ring;
432 rxvq = (struct virtnet_rx *)RTE_PTR_ADD(vq, sz_vq);
434 rxvq->port_id = dev->data->port_id;
435 rxvq->queue_id = queue_idx;
438 } else if (queue_type == VTNET_TQ) {
439 txvq = (struct virtnet_tx *)RTE_PTR_ADD(vq, sz_vq);
441 txvq->port_id = dev->data->port_id;
442 txvq->queue_id = queue_idx;
444 txvq->virtio_net_hdr_mz = hdr_mz;
445 txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
448 } else if (queue_type == VTNET_CQ) {
449 cvq = (struct virtnet_ctl *)RTE_PTR_ADD(vq, sz_vq);
452 cvq->virtio_net_hdr_mz = hdr_mz;
453 cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
454 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
458 /* For virtio-user case (that is when dev->pci_dev is NULL), we use
459 * virtual address. And we need properly set _offset_, please see
460 * MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
463 vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
465 vq->vq_ring_mem = (uintptr_t)mz->addr;
466 vq->offset = offsetof(struct rte_mbuf, buf_addr);
467 if (queue_type == VTNET_TQ)
468 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
469 else if (queue_type == VTNET_CQ)
470 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
473 if (queue_type == VTNET_TQ) {
474 struct virtio_tx_region *txr;
478 memset(txr, 0, vq_size * sizeof(*txr));
479 for (i = 0; i < vq_size; i++) {
480 struct vring_desc *start_dp = txr[i].tx_indir;
482 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
484 /* first indirect descriptor is always the tx header */
485 start_dp->addr = txvq->virtio_net_hdr_mem
487 + offsetof(struct virtio_tx_region, tx_hdr);
489 start_dp->len = hw->vtnet_hdr_size;
490 start_dp->flags = VRING_DESC_F_NEXT;
494 if (hw->vtpci_ops->setup_queue(hw, vq) < 0) {
495 PMD_INIT_LOG(ERR, "setup_queue failed");
496 virtio_dev_queue_release(vq);
505 rte_memzone_free(hdr_mz);
506 rte_memzone_free(mz);
513 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
516 struct virtnet_ctl *cvq;
518 struct virtio_hw *hw = dev->data->dev_private;
520 PMD_INIT_FUNC_TRACE();
521 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
522 vtpci_queue_idx, 0, socket_id, (void **)&cvq);
524 PMD_INIT_LOG(ERR, "control vq initialization failed");
533 virtio_free_queues(struct rte_eth_dev *dev)
537 for (i = 0; i < dev->data->nb_rx_queues; i++)
538 virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
540 dev->data->nb_rx_queues = 0;
542 for (i = 0; i < dev->data->nb_tx_queues; i++)
543 virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
545 dev->data->nb_tx_queues = 0;
549 virtio_dev_close(struct rte_eth_dev *dev)
551 struct virtio_hw *hw = dev->data->dev_private;
553 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
555 if (hw->started == 1)
556 virtio_dev_stop(dev);
559 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
560 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
562 virtio_dev_free_mbufs(dev);
563 virtio_free_queues(dev);
567 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
569 struct virtio_hw *hw = dev->data->dev_private;
570 struct virtio_pmd_ctrl ctrl;
574 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
575 PMD_INIT_LOG(INFO, "host does not support rx control\n");
579 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
580 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
584 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
586 PMD_INIT_LOG(ERR, "Failed to enable promisc");
590 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
592 struct virtio_hw *hw = dev->data->dev_private;
593 struct virtio_pmd_ctrl ctrl;
597 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
598 PMD_INIT_LOG(INFO, "host does not support rx control\n");
602 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
603 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
607 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
609 PMD_INIT_LOG(ERR, "Failed to disable promisc");
613 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
615 struct virtio_hw *hw = dev->data->dev_private;
616 struct virtio_pmd_ctrl ctrl;
620 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
621 PMD_INIT_LOG(INFO, "host does not support rx control\n");
625 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
626 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
630 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
632 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
636 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
638 struct virtio_hw *hw = dev->data->dev_private;
639 struct virtio_pmd_ctrl ctrl;
643 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
644 PMD_INIT_LOG(INFO, "host does not support rx control\n");
648 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
649 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
653 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
655 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
659 * dev_ops for virtio, bare necessities for basic operation
661 static const struct eth_dev_ops virtio_eth_dev_ops = {
662 .dev_configure = virtio_dev_configure,
663 .dev_start = virtio_dev_start,
664 .dev_stop = virtio_dev_stop,
665 .dev_close = virtio_dev_close,
666 .promiscuous_enable = virtio_dev_promiscuous_enable,
667 .promiscuous_disable = virtio_dev_promiscuous_disable,
668 .allmulticast_enable = virtio_dev_allmulticast_enable,
669 .allmulticast_disable = virtio_dev_allmulticast_disable,
671 .dev_infos_get = virtio_dev_info_get,
672 .stats_get = virtio_dev_stats_get,
673 .xstats_get = virtio_dev_xstats_get,
674 .xstats_get_names = virtio_dev_xstats_get_names,
675 .stats_reset = virtio_dev_stats_reset,
676 .xstats_reset = virtio_dev_stats_reset,
677 .link_update = virtio_dev_link_update,
678 .rx_queue_setup = virtio_dev_rx_queue_setup,
679 .rx_queue_release = virtio_dev_rx_queue_release,
680 .tx_queue_setup = virtio_dev_tx_queue_setup,
681 .tx_queue_release = virtio_dev_tx_queue_release,
682 /* collect stats per queue */
683 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
684 .vlan_filter_set = virtio_vlan_filter_set,
685 .mac_addr_add = virtio_mac_addr_add,
686 .mac_addr_remove = virtio_mac_addr_remove,
687 .mac_addr_set = virtio_mac_addr_set,
691 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
692 struct rte_eth_link *link)
694 struct rte_eth_link *dst = link;
695 struct rte_eth_link *src = &(dev->data->dev_link);
697 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
698 *(uint64_t *)src) == 0)
705 * Atomically writes the link status information into global
706 * structure rte_eth_dev.
709 * - Pointer to the structure rte_eth_dev to read from.
710 * - Pointer to the buffer to be saved with the link status.
713 * - On success, zero.
714 * - On failure, negative value.
717 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
718 struct rte_eth_link *link)
720 struct rte_eth_link *dst = &(dev->data->dev_link);
721 struct rte_eth_link *src = link;
723 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
724 *(uint64_t *)src) == 0)
731 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
735 for (i = 0; i < dev->data->nb_tx_queues; i++) {
736 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
740 stats->opackets += txvq->stats.packets;
741 stats->obytes += txvq->stats.bytes;
742 stats->oerrors += txvq->stats.errors;
744 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
745 stats->q_opackets[i] = txvq->stats.packets;
746 stats->q_obytes[i] = txvq->stats.bytes;
750 for (i = 0; i < dev->data->nb_rx_queues; i++) {
751 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
755 stats->ipackets += rxvq->stats.packets;
756 stats->ibytes += rxvq->stats.bytes;
757 stats->ierrors += rxvq->stats.errors;
759 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
760 stats->q_ipackets[i] = rxvq->stats.packets;
761 stats->q_ibytes[i] = rxvq->stats.bytes;
765 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
768 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
769 struct rte_eth_xstat_name *xstats_names,
770 __rte_unused unsigned limit)
776 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
777 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
779 if (xstats_names == NULL) {
780 /* Note: limit checked in rte_eth_xstats_names() */
782 for (i = 0; i < dev->data->nb_rx_queues; i++) {
783 struct virtqueue *rxvq = dev->data->rx_queues[i];
786 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
787 snprintf(xstats_names[count].name,
788 sizeof(xstats_names[count].name),
790 rte_virtio_rxq_stat_strings[t].name);
791 xstats_names[count].id = count;
796 for (i = 0; i < dev->data->nb_tx_queues; i++) {
797 struct virtqueue *txvq = dev->data->tx_queues[i];
800 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
801 snprintf(xstats_names[count].name,
802 sizeof(xstats_names[count].name),
804 rte_virtio_txq_stat_strings[t].name);
805 xstats_names[count].id = count;
815 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
821 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
822 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
827 for (i = 0; i < dev->data->nb_rx_queues; i++) {
828 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
835 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
836 xstats[count].id = count;
837 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
838 rte_virtio_rxq_stat_strings[t].offset);
843 for (i = 0; i < dev->data->nb_tx_queues; i++) {
844 struct virtnet_tx *txvq = dev->data->tx_queues[i];
851 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
852 xstats[count].id = count;
853 xstats[count].value = *(uint64_t *)(((char *)txvq) +
854 rte_virtio_txq_stat_strings[t].offset);
863 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
865 virtio_update_stats(dev, stats);
869 virtio_dev_stats_reset(struct rte_eth_dev *dev)
873 for (i = 0; i < dev->data->nb_tx_queues; i++) {
874 struct virtnet_tx *txvq = dev->data->tx_queues[i];
878 txvq->stats.packets = 0;
879 txvq->stats.bytes = 0;
880 txvq->stats.errors = 0;
881 txvq->stats.multicast = 0;
882 txvq->stats.broadcast = 0;
883 memset(txvq->stats.size_bins, 0,
884 sizeof(txvq->stats.size_bins[0]) * 8);
887 for (i = 0; i < dev->data->nb_rx_queues; i++) {
888 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
892 rxvq->stats.packets = 0;
893 rxvq->stats.bytes = 0;
894 rxvq->stats.errors = 0;
895 rxvq->stats.multicast = 0;
896 rxvq->stats.broadcast = 0;
897 memset(rxvq->stats.size_bins, 0,
898 sizeof(rxvq->stats.size_bins[0]) * 8);
903 virtio_set_hwaddr(struct virtio_hw *hw)
905 vtpci_write_dev_config(hw,
906 offsetof(struct virtio_net_config, mac),
907 &hw->mac_addr, ETHER_ADDR_LEN);
911 virtio_get_hwaddr(struct virtio_hw *hw)
913 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
914 vtpci_read_dev_config(hw,
915 offsetof(struct virtio_net_config, mac),
916 &hw->mac_addr, ETHER_ADDR_LEN);
918 eth_random_addr(&hw->mac_addr[0]);
919 virtio_set_hwaddr(hw);
924 virtio_mac_table_set(struct virtio_hw *hw,
925 const struct virtio_net_ctrl_mac *uc,
926 const struct virtio_net_ctrl_mac *mc)
928 struct virtio_pmd_ctrl ctrl;
931 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
932 PMD_DRV_LOG(INFO, "host does not support mac table");
936 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
937 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
939 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
940 memcpy(ctrl.data, uc, len[0]);
942 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
943 memcpy(ctrl.data + len[0], mc, len[1]);
945 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
947 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
951 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
952 uint32_t index, uint32_t vmdq __rte_unused)
954 struct virtio_hw *hw = dev->data->dev_private;
955 const struct ether_addr *addrs = dev->data->mac_addrs;
957 struct virtio_net_ctrl_mac *uc, *mc;
959 if (index >= VIRTIO_MAX_MAC_ADDRS) {
960 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
964 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
966 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
969 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
970 const struct ether_addr *addr
971 = (i == index) ? mac_addr : addrs + i;
972 struct virtio_net_ctrl_mac *tbl
973 = is_multicast_ether_addr(addr) ? mc : uc;
975 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
978 virtio_mac_table_set(hw, uc, mc);
982 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
984 struct virtio_hw *hw = dev->data->dev_private;
985 struct ether_addr *addrs = dev->data->mac_addrs;
986 struct virtio_net_ctrl_mac *uc, *mc;
989 if (index >= VIRTIO_MAX_MAC_ADDRS) {
990 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
994 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
996 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
999 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1000 struct virtio_net_ctrl_mac *tbl;
1002 if (i == index || is_zero_ether_addr(addrs + i))
1005 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1006 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1009 virtio_mac_table_set(hw, uc, mc);
1013 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1015 struct virtio_hw *hw = dev->data->dev_private;
1017 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1019 /* Use atomic update if available */
1020 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1021 struct virtio_pmd_ctrl ctrl;
1022 int len = ETHER_ADDR_LEN;
1024 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1025 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1027 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1028 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1029 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1030 virtio_set_hwaddr(hw);
1034 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1036 struct virtio_hw *hw = dev->data->dev_private;
1037 struct virtio_pmd_ctrl ctrl;
1040 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1043 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1044 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1045 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1046 len = sizeof(vlan_id);
1048 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1052 virtio_negotiate_features(struct virtio_hw *hw)
1054 uint64_t host_features;
1056 /* Prepare guest_features: feature that driver wants to support */
1057 hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
1058 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1059 hw->guest_features);
1061 /* Read device(host) feature bits */
1062 host_features = hw->vtpci_ops->get_features(hw);
1063 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1067 * Negotiate features: Subset of device feature bits are written back
1068 * guest feature bits.
1070 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1071 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1072 hw->guest_features);
1075 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1077 "VIRTIO_F_VERSION_1 features is not enabled.");
1080 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1081 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1083 "failed to set FEATURES_OK status!");
1092 * Process Virtio Config changed interrupt and call the callback
1093 * if link state changed.
1096 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1099 struct rte_eth_dev *dev = param;
1100 struct virtio_hw *hw = dev->data->dev_private;
1103 /* Read interrupt status which clears interrupt */
1104 isr = vtpci_isr(hw);
1105 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1107 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1108 PMD_DRV_LOG(ERR, "interrupt enable failed");
1110 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1111 if (virtio_dev_link_update(dev, 0) == 0)
1112 _rte_eth_dev_callback_process(dev,
1113 RTE_ETH_EVENT_INTR_LSC);
1119 rx_func_get(struct rte_eth_dev *eth_dev)
1121 struct virtio_hw *hw = eth_dev->data->dev_private;
1122 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1123 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1125 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1129 * This function is based on probe() function in virtio_pci.c
1130 * It returns 0 on success.
1133 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1135 struct virtio_hw *hw = eth_dev->data->dev_private;
1136 struct virtio_net_config *config;
1137 struct virtio_net_config local_config;
1138 struct rte_pci_device *pci_dev;
1139 uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1142 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1144 eth_dev->dev_ops = &virtio_eth_dev_ops;
1145 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1147 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1148 rx_func_get(eth_dev);
1152 /* Allocate memory for storing MAC addresses */
1153 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1154 if (eth_dev->data->mac_addrs == NULL) {
1156 "Failed to allocate %d bytes needed to store MAC addresses",
1157 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1161 pci_dev = eth_dev->pci_dev;
1164 ret = vtpci_init(pci_dev, hw, &dev_flags);
1169 /* Reset the device although not necessary at startup */
1172 /* Tell the host we've noticed this device. */
1173 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1175 /* Tell the host we've known how to drive the device. */
1176 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1177 if (virtio_negotiate_features(hw) < 0)
1180 /* If host does not support status then disable LSC */
1181 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1182 dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1184 rte_eth_copy_pci_info(eth_dev, pci_dev);
1185 eth_dev->data->dev_flags = dev_flags;
1187 rx_func_get(eth_dev);
1189 /* Setting up rx_header size for the device */
1190 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1191 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1192 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1194 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1196 /* Copy the permanent MAC address to: virtio_hw */
1197 virtio_get_hwaddr(hw);
1198 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1199 ð_dev->data->mac_addrs[0]);
1201 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1202 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1203 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1205 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1206 config = &local_config;
1208 vtpci_read_dev_config(hw,
1209 offsetof(struct virtio_net_config, mac),
1210 &config->mac, sizeof(config->mac));
1212 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1213 vtpci_read_dev_config(hw,
1214 offsetof(struct virtio_net_config, status),
1215 &config->status, sizeof(config->status));
1218 "VIRTIO_NET_F_STATUS is not supported");
1222 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1223 vtpci_read_dev_config(hw,
1224 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1225 &config->max_virtqueue_pairs,
1226 sizeof(config->max_virtqueue_pairs));
1229 "VIRTIO_NET_F_MQ is not supported");
1230 config->max_virtqueue_pairs = 1;
1234 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1235 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1237 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1238 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1240 virtio_dev_cq_queue_setup(eth_dev,
1241 config->max_virtqueue_pairs * 2,
1244 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1245 config->max_virtqueue_pairs);
1246 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1248 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1249 config->mac[0], config->mac[1],
1250 config->mac[2], config->mac[3],
1251 config->mac[4], config->mac[5]);
1253 hw->max_rx_queues = 1;
1254 hw->max_tx_queues = 1;
1257 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1258 hw->max_rx_queues, hw->max_tx_queues);
1260 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1261 eth_dev->data->port_id, pci_dev->id.vendor_id,
1262 pci_dev->id.device_id);
1264 /* Setup interrupt callback */
1265 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1266 rte_intr_callback_register(&pci_dev->intr_handle,
1267 virtio_interrupt_handler, eth_dev);
1269 virtio_dev_cq_start(eth_dev);
1275 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1277 struct rte_pci_device *pci_dev;
1278 struct virtio_hw *hw = eth_dev->data->dev_private;
1280 PMD_INIT_FUNC_TRACE();
1282 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1285 /* Close it anyway since there's no way to know if closed */
1286 virtio_dev_close(eth_dev);
1288 pci_dev = eth_dev->pci_dev;
1290 eth_dev->dev_ops = NULL;
1291 eth_dev->tx_pkt_burst = NULL;
1292 eth_dev->rx_pkt_burst = NULL;
1295 virtio_dev_queue_release(hw->cvq->vq);
1297 rte_free(eth_dev->data->mac_addrs);
1298 eth_dev->data->mac_addrs = NULL;
1300 /* reset interrupt callback */
1301 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1302 rte_intr_callback_unregister(&pci_dev->intr_handle,
1303 virtio_interrupt_handler,
1305 rte_eal_pci_unmap_device(pci_dev);
1307 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1312 static struct eth_driver rte_virtio_pmd = {
1314 .name = "rte_virtio_pmd",
1315 .id_table = pci_id_virtio_map,
1316 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1318 .eth_dev_init = eth_virtio_dev_init,
1319 .eth_dev_uninit = eth_virtio_dev_uninit,
1320 .dev_private_size = sizeof(struct virtio_hw),
1324 * Driver initialization routine.
1325 * Invoked once at EAL init time.
1326 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1327 * Returns 0 on success.
1330 rte_virtio_pmd_init(const char *name __rte_unused,
1331 const char *param __rte_unused)
1333 if (rte_eal_iopl_init() != 0) {
1334 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1338 rte_eth_driver_register(&rte_virtio_pmd);
1343 * Configure virtio device
1344 * It returns 0 on success.
1347 virtio_dev_configure(struct rte_eth_dev *dev)
1349 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1350 struct virtio_hw *hw = dev->data->dev_private;
1352 PMD_INIT_LOG(DEBUG, "configure");
1354 if (rxmode->hw_ip_checksum) {
1355 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1359 hw->vlan_strip = rxmode->hw_vlan_strip;
1361 if (rxmode->hw_vlan_filter
1362 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1364 "vlan filtering not available on this host");
1368 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1369 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1370 PMD_DRV_LOG(ERR, "failed to set config vector");
1379 virtio_dev_start(struct rte_eth_dev *dev)
1381 uint16_t nb_queues, i;
1382 struct virtio_hw *hw = dev->data->dev_private;
1383 struct virtnet_rx *rxvq;
1384 struct virtnet_tx *txvq __rte_unused;
1386 /* check if lsc interrupt feature is enabled */
1387 if (dev->data->dev_conf.intr_conf.lsc) {
1388 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1389 PMD_DRV_LOG(ERR, "link status not supported by host");
1393 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1394 PMD_DRV_LOG(ERR, "interrupt enable failed");
1399 /* Initialize Link state */
1400 virtio_dev_link_update(dev, 0);
1402 /* On restart after stop do not touch queues */
1406 /* Do final configuration before rx/tx engine starts */
1407 virtio_dev_rxtx_start(dev);
1408 vtpci_reinit_complete(hw);
1412 /*Notify the backend
1413 *Otherwise the tap backend might already stop its queue due to fullness.
1414 *vhost backend will have no chance to be waked up
1416 nb_queues = dev->data->nb_rx_queues;
1417 if (nb_queues > 1) {
1418 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1422 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1424 for (i = 0; i < nb_queues; i++) {
1425 rxvq = dev->data->rx_queues[i];
1426 virtqueue_notify(rxvq->vq);
1429 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1431 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1432 rxvq = dev->data->rx_queues[i];
1433 VIRTQUEUE_DUMP(rxvq->vq);
1436 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1437 txvq = dev->data->tx_queues[i];
1438 VIRTQUEUE_DUMP(txvq->vq);
1444 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1446 struct rte_mbuf *buf;
1447 int i, mbuf_num = 0;
1449 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1450 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1453 "Before freeing rxq[%d] used and unused buf", i);
1454 VIRTQUEUE_DUMP(rxvq->vq);
1456 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1457 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1458 rte_pktmbuf_free(buf);
1462 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1464 "After freeing rxq[%d] used and unused buf", i);
1465 VIRTQUEUE_DUMP(rxvq->vq);
1468 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1469 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1472 "Before freeing txq[%d] used and unused bufs",
1474 VIRTQUEUE_DUMP(txvq->vq);
1477 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1478 rte_pktmbuf_free(buf);
1482 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1484 "After freeing txq[%d] used and unused buf", i);
1485 VIRTQUEUE_DUMP(txvq->vq);
1490 * Stop device: disable interrupt and mark link down
1493 virtio_dev_stop(struct rte_eth_dev *dev)
1495 struct rte_eth_link link;
1496 struct virtio_hw *hw = dev->data->dev_private;
1498 PMD_INIT_LOG(DEBUG, "stop");
1502 if (dev->data->dev_conf.intr_conf.lsc)
1503 rte_intr_disable(&dev->pci_dev->intr_handle);
1505 memset(&link, 0, sizeof(link));
1506 virtio_dev_atomic_write_link_status(dev, &link);
1510 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1512 struct rte_eth_link link, old;
1514 struct virtio_hw *hw = dev->data->dev_private;
1515 memset(&link, 0, sizeof(link));
1516 virtio_dev_atomic_read_link_status(dev, &link);
1518 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1519 link.link_speed = SPEED_10G;
1521 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1522 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1523 vtpci_read_dev_config(hw,
1524 offsetof(struct virtio_net_config, status),
1525 &status, sizeof(status));
1526 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1527 link.link_status = ETH_LINK_DOWN;
1528 PMD_INIT_LOG(DEBUG, "Port %d is down",
1529 dev->data->port_id);
1531 link.link_status = ETH_LINK_UP;
1532 PMD_INIT_LOG(DEBUG, "Port %d is up",
1533 dev->data->port_id);
1536 link.link_status = ETH_LINK_UP;
1538 virtio_dev_atomic_write_link_status(dev, &link);
1540 return (old.link_status == link.link_status) ? -1 : 0;
1544 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1546 struct virtio_hw *hw = dev->data->dev_private;
1549 dev_info->driver_name = dev->driver->pci_drv.name;
1551 dev_info->driver_name = "virtio-user PMD";
1552 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1553 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1554 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1555 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1556 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1557 dev_info->default_txconf = (struct rte_eth_txconf) {
1558 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1563 * It enables testpmd to collect per queue stats.
1566 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1567 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1568 __rte_unused uint8_t is_rx)
1573 static struct rte_driver rte_virtio_driver = {
1575 .init = rte_virtio_pmd_init,
1578 PMD_REGISTER_DRIVER(rte_virtio_driver);