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[] = {
106 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
107 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
108 { .vendor_id = 0, /* sentinel */ },
111 struct rte_virtio_xstats_name_off {
112 char name[RTE_ETH_XSTATS_NAME_SIZE];
116 /* [rt]x_qX_ is prepended to the name string here */
117 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
118 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
119 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
120 {"errors", offsetof(struct virtnet_rx, stats.errors)},
121 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
122 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
123 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
124 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
125 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
126 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
127 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
128 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
129 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
130 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
133 /* [rt]x_qX_ is prepended to the name string here */
134 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
135 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
136 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
137 {"errors", offsetof(struct virtnet_tx, stats.errors)},
138 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
139 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
140 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
141 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
142 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
143 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
144 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
145 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
146 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
147 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
150 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
151 sizeof(rte_virtio_rxq_stat_strings[0]))
152 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
153 sizeof(rte_virtio_txq_stat_strings[0]))
155 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
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 (VIRTQUEUE_NUSED(vq) == 0) {
230 while (VIRTQUEUE_NUSED(vq)) {
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(void *queue __rte_unused)
292 virtio_get_nr_vq(struct virtio_hw *hw)
294 uint16_t nr_vq = hw->max_queue_pairs * 2;
296 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
303 virtio_init_vring(struct virtqueue *vq)
305 int size = vq->vq_nentries;
306 struct vring *vr = &vq->vq_ring;
307 uint8_t *ring_mem = vq->vq_ring_virt_mem;
309 PMD_INIT_FUNC_TRACE();
312 * Reinitialise since virtio port might have been stopped and restarted
314 memset(ring_mem, 0, vq->vq_ring_size);
315 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
316 vq->vq_used_cons_idx = 0;
317 vq->vq_desc_head_idx = 0;
318 vq->vq_avail_idx = 0;
319 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
320 vq->vq_free_cnt = vq->vq_nentries;
321 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
323 vring_desc_init(vr->desc, size);
326 * Disable device(host) interrupting guest
328 virtqueue_disable_intr(vq);
332 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
334 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
335 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
336 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
337 unsigned int vq_size, size;
338 struct virtio_hw *hw = dev->data->dev_private;
339 struct virtnet_rx *rxvq = NULL;
340 struct virtnet_tx *txvq = NULL;
341 struct virtnet_ctl *cvq = NULL;
342 struct virtqueue *vq;
343 size_t sz_hdr_mz = 0;
344 void *sw_ring = NULL;
345 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
348 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
351 * Read the virtqueue size from the Queue Size field
352 * Always power of 2 and if 0 virtqueue does not exist
354 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
355 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
357 PMD_INIT_LOG(ERR, "virtqueue does not exist");
361 if (!rte_is_power_of_2(vq_size)) {
362 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
366 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
367 dev->data->port_id, vtpci_queue_idx);
369 size = RTE_ALIGN_CEIL(sizeof(*vq) +
370 vq_size * sizeof(struct vq_desc_extra),
371 RTE_CACHE_LINE_SIZE);
372 if (queue_type == VTNET_TQ) {
374 * For each xmit packet, allocate a virtio_net_hdr
375 * and indirect ring elements
377 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
378 } else if (queue_type == VTNET_CQ) {
379 /* Allocate a page for control vq command, data and status */
380 sz_hdr_mz = PAGE_SIZE;
383 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
386 PMD_INIT_LOG(ERR, "can not allocate vq");
389 hw->vqs[vtpci_queue_idx] = vq;
392 vq->vq_queue_index = vtpci_queue_idx;
393 vq->vq_nentries = vq_size;
396 * Reserve a memzone for vring elements
398 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
399 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
400 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
401 size, vq->vq_ring_size);
403 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
405 0, VIRTIO_PCI_VRING_ALIGN);
407 if (rte_errno == EEXIST)
408 mz = rte_memzone_lookup(vq_name);
415 memset(mz->addr, 0, mz->len);
417 vq->vq_ring_mem = mz->phys_addr;
418 vq->vq_ring_virt_mem = mz->addr;
419 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
420 (uint64_t)mz->phys_addr);
421 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
422 (uint64_t)(uintptr_t)mz->addr);
424 virtio_init_vring(vq);
427 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
428 dev->data->port_id, vtpci_queue_idx);
429 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
431 RTE_CACHE_LINE_SIZE);
432 if (hdr_mz == NULL) {
433 if (rte_errno == EEXIST)
434 hdr_mz = rte_memzone_lookup(vq_hdr_name);
435 if (hdr_mz == NULL) {
442 if (queue_type == VTNET_RQ) {
443 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
444 sizeof(vq->sw_ring[0]);
446 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
447 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
449 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
454 vq->sw_ring = sw_ring;
457 rxvq->port_id = dev->data->port_id;
459 } else if (queue_type == VTNET_TQ) {
462 txvq->port_id = dev->data->port_id;
464 txvq->virtio_net_hdr_mz = hdr_mz;
465 txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
466 } else if (queue_type == VTNET_CQ) {
470 cvq->virtio_net_hdr_mz = hdr_mz;
471 cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
472 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
477 /* For virtio_user case (that is when dev->pci_dev is NULL), we use
478 * virtual address. And we need properly set _offset_, please see
479 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
482 vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
484 vq->vq_ring_mem = (uintptr_t)mz->addr;
485 vq->offset = offsetof(struct rte_mbuf, buf_addr);
486 if (queue_type == VTNET_TQ)
487 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
488 else if (queue_type == VTNET_CQ)
489 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
492 if (queue_type == VTNET_TQ) {
493 struct virtio_tx_region *txr;
497 memset(txr, 0, vq_size * sizeof(*txr));
498 for (i = 0; i < vq_size; i++) {
499 struct vring_desc *start_dp = txr[i].tx_indir;
501 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
503 /* first indirect descriptor is always the tx header */
504 start_dp->addr = txvq->virtio_net_hdr_mem
506 + offsetof(struct virtio_tx_region, tx_hdr);
508 start_dp->len = hw->vtnet_hdr_size;
509 start_dp->flags = VRING_DESC_F_NEXT;
513 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
514 PMD_INIT_LOG(ERR, "setup_queue failed");
522 rte_memzone_free(hdr_mz);
523 rte_memzone_free(mz);
530 virtio_free_queues(struct virtio_hw *hw)
532 uint16_t nr_vq = virtio_get_nr_vq(hw);
533 struct virtqueue *vq;
540 for (i = 0; i < nr_vq; i++) {
545 queue_type = virtio_get_queue_type(hw, i);
546 if (queue_type == VTNET_RQ) {
547 rte_free(vq->sw_ring);
548 rte_memzone_free(vq->rxq.mz);
549 } else if (queue_type == VTNET_TQ) {
550 rte_memzone_free(vq->txq.mz);
551 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
553 rte_memzone_free(vq->cq.mz);
554 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
566 virtio_alloc_queues(struct rte_eth_dev *dev)
568 struct virtio_hw *hw = dev->data->dev_private;
569 uint16_t nr_vq = virtio_get_nr_vq(hw);
573 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
575 PMD_INIT_LOG(ERR, "failed to allocate vqs");
579 for (i = 0; i < nr_vq; i++) {
580 ret = virtio_init_queue(dev, i);
582 virtio_free_queues(hw);
591 virtio_dev_close(struct rte_eth_dev *dev)
593 struct virtio_hw *hw = dev->data->dev_private;
595 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
598 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
599 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
601 virtio_dev_free_mbufs(dev);
602 virtio_free_queues(hw);
606 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
608 struct virtio_hw *hw = dev->data->dev_private;
609 struct virtio_pmd_ctrl ctrl;
613 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
614 PMD_INIT_LOG(INFO, "host does not support rx control\n");
618 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
619 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
623 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
625 PMD_INIT_LOG(ERR, "Failed to enable promisc");
629 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
631 struct virtio_hw *hw = dev->data->dev_private;
632 struct virtio_pmd_ctrl ctrl;
636 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
637 PMD_INIT_LOG(INFO, "host does not support rx control\n");
641 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
642 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
646 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
648 PMD_INIT_LOG(ERR, "Failed to disable promisc");
652 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
654 struct virtio_hw *hw = dev->data->dev_private;
655 struct virtio_pmd_ctrl ctrl;
659 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
660 PMD_INIT_LOG(INFO, "host does not support rx control\n");
664 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
665 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
669 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
671 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
675 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
677 struct virtio_hw *hw = dev->data->dev_private;
678 struct virtio_pmd_ctrl ctrl;
682 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
683 PMD_INIT_LOG(INFO, "host does not support rx control\n");
687 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
688 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
692 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
694 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
697 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
699 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
701 struct virtio_hw *hw = dev->data->dev_private;
702 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
704 uint32_t frame_size = mtu + ether_hdr_len;
706 if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
707 PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
708 ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
715 * dev_ops for virtio, bare necessities for basic operation
717 static const struct eth_dev_ops virtio_eth_dev_ops = {
718 .dev_configure = virtio_dev_configure,
719 .dev_start = virtio_dev_start,
720 .dev_stop = virtio_dev_stop,
721 .dev_close = virtio_dev_close,
722 .promiscuous_enable = virtio_dev_promiscuous_enable,
723 .promiscuous_disable = virtio_dev_promiscuous_disable,
724 .allmulticast_enable = virtio_dev_allmulticast_enable,
725 .allmulticast_disable = virtio_dev_allmulticast_disable,
726 .mtu_set = virtio_mtu_set,
727 .dev_infos_get = virtio_dev_info_get,
728 .stats_get = virtio_dev_stats_get,
729 .xstats_get = virtio_dev_xstats_get,
730 .xstats_get_names = virtio_dev_xstats_get_names,
731 .stats_reset = virtio_dev_stats_reset,
732 .xstats_reset = virtio_dev_stats_reset,
733 .link_update = virtio_dev_link_update,
734 .rx_queue_setup = virtio_dev_rx_queue_setup,
735 .rx_queue_release = virtio_dev_queue_release,
736 .tx_queue_setup = virtio_dev_tx_queue_setup,
737 .tx_queue_release = virtio_dev_queue_release,
738 /* collect stats per queue */
739 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
740 .vlan_filter_set = virtio_vlan_filter_set,
741 .mac_addr_add = virtio_mac_addr_add,
742 .mac_addr_remove = virtio_mac_addr_remove,
743 .mac_addr_set = virtio_mac_addr_set,
747 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
748 struct rte_eth_link *link)
750 struct rte_eth_link *dst = link;
751 struct rte_eth_link *src = &(dev->data->dev_link);
753 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
754 *(uint64_t *)src) == 0)
761 * Atomically writes the link status information into global
762 * structure rte_eth_dev.
765 * - Pointer to the structure rte_eth_dev to read from.
766 * - Pointer to the buffer to be saved with the link status.
769 * - On success, zero.
770 * - On failure, negative value.
773 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
774 struct rte_eth_link *link)
776 struct rte_eth_link *dst = &(dev->data->dev_link);
777 struct rte_eth_link *src = link;
779 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
780 *(uint64_t *)src) == 0)
787 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
791 for (i = 0; i < dev->data->nb_tx_queues; i++) {
792 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
796 stats->opackets += txvq->stats.packets;
797 stats->obytes += txvq->stats.bytes;
798 stats->oerrors += txvq->stats.errors;
800 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
801 stats->q_opackets[i] = txvq->stats.packets;
802 stats->q_obytes[i] = txvq->stats.bytes;
806 for (i = 0; i < dev->data->nb_rx_queues; i++) {
807 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
811 stats->ipackets += rxvq->stats.packets;
812 stats->ibytes += rxvq->stats.bytes;
813 stats->ierrors += rxvq->stats.errors;
815 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
816 stats->q_ipackets[i] = rxvq->stats.packets;
817 stats->q_ibytes[i] = rxvq->stats.bytes;
821 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
824 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
825 struct rte_eth_xstat_name *xstats_names,
826 __rte_unused unsigned limit)
832 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
833 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
835 if (xstats_names != NULL) {
836 /* Note: limit checked in rte_eth_xstats_names() */
838 for (i = 0; i < dev->data->nb_rx_queues; i++) {
839 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
842 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
843 snprintf(xstats_names[count].name,
844 sizeof(xstats_names[count].name),
846 rte_virtio_rxq_stat_strings[t].name);
851 for (i = 0; i < dev->data->nb_tx_queues; i++) {
852 struct virtnet_tx *txvq = dev->data->tx_queues[i];
855 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
856 snprintf(xstats_names[count].name,
857 sizeof(xstats_names[count].name),
859 rte_virtio_txq_stat_strings[t].name);
869 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
875 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
876 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
881 for (i = 0; i < dev->data->nb_rx_queues; i++) {
882 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
889 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
890 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
891 rte_virtio_rxq_stat_strings[t].offset);
892 xstats[count].id = count;
897 for (i = 0; i < dev->data->nb_tx_queues; i++) {
898 struct virtnet_tx *txvq = dev->data->tx_queues[i];
905 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
906 xstats[count].value = *(uint64_t *)(((char *)txvq) +
907 rte_virtio_txq_stat_strings[t].offset);
908 xstats[count].id = count;
917 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
919 virtio_update_stats(dev, stats);
923 virtio_dev_stats_reset(struct rte_eth_dev *dev)
927 for (i = 0; i < dev->data->nb_tx_queues; i++) {
928 struct virtnet_tx *txvq = dev->data->tx_queues[i];
932 txvq->stats.packets = 0;
933 txvq->stats.bytes = 0;
934 txvq->stats.errors = 0;
935 txvq->stats.multicast = 0;
936 txvq->stats.broadcast = 0;
937 memset(txvq->stats.size_bins, 0,
938 sizeof(txvq->stats.size_bins[0]) * 8);
941 for (i = 0; i < dev->data->nb_rx_queues; i++) {
942 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
946 rxvq->stats.packets = 0;
947 rxvq->stats.bytes = 0;
948 rxvq->stats.errors = 0;
949 rxvq->stats.multicast = 0;
950 rxvq->stats.broadcast = 0;
951 memset(rxvq->stats.size_bins, 0,
952 sizeof(rxvq->stats.size_bins[0]) * 8);
957 virtio_set_hwaddr(struct virtio_hw *hw)
959 vtpci_write_dev_config(hw,
960 offsetof(struct virtio_net_config, mac),
961 &hw->mac_addr, ETHER_ADDR_LEN);
965 virtio_get_hwaddr(struct virtio_hw *hw)
967 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
968 vtpci_read_dev_config(hw,
969 offsetof(struct virtio_net_config, mac),
970 &hw->mac_addr, ETHER_ADDR_LEN);
972 eth_random_addr(&hw->mac_addr[0]);
973 virtio_set_hwaddr(hw);
978 virtio_mac_table_set(struct virtio_hw *hw,
979 const struct virtio_net_ctrl_mac *uc,
980 const struct virtio_net_ctrl_mac *mc)
982 struct virtio_pmd_ctrl ctrl;
985 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
986 PMD_DRV_LOG(INFO, "host does not support mac table");
990 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
991 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
993 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
994 memcpy(ctrl.data, uc, len[0]);
996 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
997 memcpy(ctrl.data + len[0], mc, len[1]);
999 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1001 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1005 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1006 uint32_t index, uint32_t vmdq __rte_unused)
1008 struct virtio_hw *hw = dev->data->dev_private;
1009 const struct ether_addr *addrs = dev->data->mac_addrs;
1011 struct virtio_net_ctrl_mac *uc, *mc;
1013 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1014 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1018 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1020 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1023 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1024 const struct ether_addr *addr
1025 = (i == index) ? mac_addr : addrs + i;
1026 struct virtio_net_ctrl_mac *tbl
1027 = is_multicast_ether_addr(addr) ? mc : uc;
1029 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1032 virtio_mac_table_set(hw, uc, mc);
1036 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1038 struct virtio_hw *hw = dev->data->dev_private;
1039 struct ether_addr *addrs = dev->data->mac_addrs;
1040 struct virtio_net_ctrl_mac *uc, *mc;
1043 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1044 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1048 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1050 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1053 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1054 struct virtio_net_ctrl_mac *tbl;
1056 if (i == index || is_zero_ether_addr(addrs + i))
1059 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1060 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1063 virtio_mac_table_set(hw, uc, mc);
1067 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1069 struct virtio_hw *hw = dev->data->dev_private;
1071 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1073 /* Use atomic update if available */
1074 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1075 struct virtio_pmd_ctrl ctrl;
1076 int len = ETHER_ADDR_LEN;
1078 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1079 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1081 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1082 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1083 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1084 virtio_set_hwaddr(hw);
1088 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1090 struct virtio_hw *hw = dev->data->dev_private;
1091 struct virtio_pmd_ctrl ctrl;
1094 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1097 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1098 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1099 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1100 len = sizeof(vlan_id);
1102 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1106 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1108 uint64_t host_features;
1110 /* Prepare guest_features: feature that driver wants to support */
1111 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1114 /* Read device(host) feature bits */
1115 host_features = VTPCI_OPS(hw)->get_features(hw);
1116 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1120 * Negotiate features: Subset of device feature bits are written back
1121 * guest feature bits.
1123 hw->guest_features = req_features;
1124 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1125 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1126 hw->guest_features);
1129 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1131 "VIRTIO_F_VERSION_1 features is not enabled.");
1134 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1135 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1137 "failed to set FEATURES_OK status!");
1142 hw->req_guest_features = req_features;
1148 * Process Virtio Config changed interrupt and call the callback
1149 * if link state changed.
1152 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1155 struct rte_eth_dev *dev = param;
1156 struct virtio_hw *hw = dev->data->dev_private;
1159 /* Read interrupt status which clears interrupt */
1160 isr = vtpci_isr(hw);
1161 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1163 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1164 PMD_DRV_LOG(ERR, "interrupt enable failed");
1166 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1167 if (virtio_dev_link_update(dev, 0) == 0)
1168 _rte_eth_dev_callback_process(dev,
1169 RTE_ETH_EVENT_INTR_LSC, NULL);
1175 rx_func_get(struct rte_eth_dev *eth_dev)
1177 struct virtio_hw *hw = eth_dev->data->dev_private;
1178 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1179 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1181 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1184 /* reset device and renegotiate features if needed */
1186 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1188 struct virtio_hw *hw = eth_dev->data->dev_private;
1189 struct virtio_net_config *config;
1190 struct virtio_net_config local_config;
1191 struct rte_pci_device *pci_dev = eth_dev->pci_dev;
1194 /* Reset the device although not necessary at startup */
1198 virtio_dev_free_mbufs(eth_dev);
1199 virtio_free_queues(hw);
1202 /* Tell the host we've noticed this device. */
1203 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1205 /* Tell the host we've known how to drive the device. */
1206 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1207 if (virtio_negotiate_features(hw, req_features) < 0)
1210 rte_eth_copy_pci_info(eth_dev, pci_dev);
1212 /* If host does not support both status and MSI-X then disable LSC */
1213 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->use_msix)
1214 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1216 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1218 rx_func_get(eth_dev);
1220 /* Setting up rx_header size for the device */
1221 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1222 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1223 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1225 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1227 /* Copy the permanent MAC address to: virtio_hw */
1228 virtio_get_hwaddr(hw);
1229 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1230 ð_dev->data->mac_addrs[0]);
1232 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1233 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1234 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1236 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1237 config = &local_config;
1239 vtpci_read_dev_config(hw,
1240 offsetof(struct virtio_net_config, mac),
1241 &config->mac, sizeof(config->mac));
1243 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1244 vtpci_read_dev_config(hw,
1245 offsetof(struct virtio_net_config, status),
1246 &config->status, sizeof(config->status));
1249 "VIRTIO_NET_F_STATUS is not supported");
1253 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1254 vtpci_read_dev_config(hw,
1255 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1256 &config->max_virtqueue_pairs,
1257 sizeof(config->max_virtqueue_pairs));
1260 "VIRTIO_NET_F_MQ is not supported");
1261 config->max_virtqueue_pairs = 1;
1264 hw->max_queue_pairs = config->max_virtqueue_pairs;
1266 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1267 config->max_virtqueue_pairs);
1268 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1270 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1271 config->mac[0], config->mac[1],
1272 config->mac[2], config->mac[3],
1273 config->mac[4], config->mac[5]);
1275 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1276 hw->max_queue_pairs = 1;
1279 ret = virtio_alloc_queues(eth_dev);
1282 vtpci_reinit_complete(hw);
1285 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1286 eth_dev->data->port_id, pci_dev->id.vendor_id,
1287 pci_dev->id.device_id);
1293 * Remap the PCI device again (IO port map for legacy device and
1294 * memory map for modern device), so that the secondary process
1295 * could have the PCI initiated correctly.
1298 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1302 * We don't have to re-parse the PCI config space, since
1303 * rte_eal_pci_map_device() makes sure the mapped address
1304 * in secondary process would equal to the one mapped in
1305 * the primary process: error will be returned if that
1306 * requirement is not met.
1308 * That said, we could simply reuse all cap pointers
1309 * (such as dev_cfg, common_cfg, etc.) parsed from the
1310 * primary process, which is stored in shared memory.
1312 if (rte_eal_pci_map_device(pci_dev)) {
1313 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1317 if (rte_eal_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1325 virtio_set_vtpci_ops(struct virtio_hw *hw)
1327 #ifdef RTE_VIRTIO_USER
1328 if (hw->virtio_user_dev)
1329 VTPCI_OPS(hw) = &virtio_user_ops;
1333 VTPCI_OPS(hw) = &modern_ops;
1335 VTPCI_OPS(hw) = &legacy_ops;
1339 * This function is based on probe() function in virtio_pci.c
1340 * It returns 0 on success.
1343 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1345 struct virtio_hw *hw = eth_dev->data->dev_private;
1346 struct rte_pci_device *pci_dev;
1347 uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1350 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1352 eth_dev->dev_ops = &virtio_eth_dev_ops;
1353 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1355 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1356 if (!hw->virtio_user_dev) {
1357 ret = virtio_remap_pci(eth_dev->pci_dev, hw);
1362 virtio_set_vtpci_ops(hw);
1363 if (hw->use_simple_rxtx) {
1364 eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1365 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1367 rx_func_get(eth_dev);
1372 /* Allocate memory for storing MAC addresses */
1373 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1374 if (eth_dev->data->mac_addrs == NULL) {
1376 "Failed to allocate %d bytes needed to store MAC addresses",
1377 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1381 pci_dev = eth_dev->pci_dev;
1382 hw->port_id = eth_dev->data->port_id;
1385 ret = vtpci_init(pci_dev, hw, &dev_flags);
1390 eth_dev->data->dev_flags = dev_flags;
1392 /* reset device and negotiate default features */
1393 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1401 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1403 struct rte_pci_device *pci_dev;
1405 PMD_INIT_FUNC_TRACE();
1407 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1410 virtio_dev_stop(eth_dev);
1411 virtio_dev_close(eth_dev);
1412 pci_dev = eth_dev->pci_dev;
1414 eth_dev->dev_ops = NULL;
1415 eth_dev->tx_pkt_burst = NULL;
1416 eth_dev->rx_pkt_burst = NULL;
1418 rte_free(eth_dev->data->mac_addrs);
1419 eth_dev->data->mac_addrs = NULL;
1421 rte_eal_pci_unmap_device(pci_dev);
1423 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1428 static struct eth_driver rte_virtio_pmd = {
1431 .name = "net_virtio",
1433 .id_table = pci_id_virtio_map,
1434 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1435 .probe = rte_eth_dev_pci_probe,
1436 .remove = rte_eth_dev_pci_remove,
1438 .eth_dev_init = eth_virtio_dev_init,
1439 .eth_dev_uninit = eth_virtio_dev_uninit,
1440 .dev_private_size = sizeof(struct virtio_hw),
1443 RTE_INIT(rte_virtio_pmd_init);
1445 rte_virtio_pmd_init(void)
1447 if (rte_eal_iopl_init() != 0) {
1448 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1452 rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
1456 * Configure virtio device
1457 * It returns 0 on success.
1460 virtio_dev_configure(struct rte_eth_dev *dev)
1462 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1463 struct virtio_hw *hw = dev->data->dev_private;
1465 PMD_INIT_LOG(DEBUG, "configure");
1467 /* Virtio does L4 checksum but not L3! */
1468 if (rxmode->hw_ip_checksum) {
1470 "virtio does not support IP checksum");
1474 if (rxmode->enable_lro) {
1476 "virtio does not support Large Receive Offload");
1480 /* start control queue */
1481 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1482 virtio_dev_cq_start(dev);
1484 hw->vlan_strip = rxmode->hw_vlan_strip;
1486 if (rxmode->hw_vlan_filter
1487 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1489 "vlan filtering not available on this host");
1493 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1494 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1495 PMD_DRV_LOG(ERR, "failed to set config vector");
1504 virtio_dev_start(struct rte_eth_dev *dev)
1506 uint16_t nb_queues, i;
1507 struct virtnet_rx *rxvq;
1508 struct virtnet_tx *txvq __rte_unused;
1509 struct virtio_hw *hw = dev->data->dev_private;
1512 /* Finish the initialization of the queues */
1513 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1514 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1518 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1519 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1524 /* check if lsc interrupt feature is enabled */
1525 if (dev->data->dev_conf.intr_conf.lsc) {
1526 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1527 PMD_DRV_LOG(ERR, "link status not supported by host");
1531 /* Setup interrupt callback */
1532 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1533 rte_intr_callback_register(&dev->pci_dev->intr_handle,
1534 virtio_interrupt_handler,
1537 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1538 PMD_DRV_LOG(ERR, "interrupt enable failed");
1543 /*Notify the backend
1544 *Otherwise the tap backend might already stop its queue due to fullness.
1545 *vhost backend will have no chance to be waked up
1547 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1548 if (hw->max_queue_pairs > 1) {
1549 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1553 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1555 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1556 rxvq = dev->data->rx_queues[i];
1557 /* Flush the old packets */
1558 virtqueue_rxvq_flush(rxvq->vq);
1559 virtqueue_notify(rxvq->vq);
1562 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1564 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1565 rxvq = dev->data->rx_queues[i];
1566 VIRTQUEUE_DUMP(rxvq->vq);
1569 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1570 txvq = dev->data->tx_queues[i];
1571 VIRTQUEUE_DUMP(txvq->vq);
1576 /* Initialize Link state */
1577 virtio_dev_link_update(dev, 0);
1582 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1584 struct rte_mbuf *buf;
1585 int i, mbuf_num = 0;
1587 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1588 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1590 if (rxvq == NULL || rxvq->vq == NULL)
1594 "Before freeing rxq[%d] used and unused buf", i);
1595 VIRTQUEUE_DUMP(rxvq->vq);
1597 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1598 while ((buf = virtqueue_detach_unused(rxvq->vq)) != NULL) {
1599 rte_pktmbuf_free(buf);
1603 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1605 "After freeing rxq[%d] used and unused buf", i);
1606 VIRTQUEUE_DUMP(rxvq->vq);
1609 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1610 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1612 if (txvq == NULL || txvq->vq == NULL)
1616 "Before freeing txq[%d] used and unused bufs",
1618 VIRTQUEUE_DUMP(txvq->vq);
1621 while ((buf = virtqueue_detach_unused(txvq->vq)) != NULL) {
1622 rte_pktmbuf_free(buf);
1626 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1628 "After freeing txq[%d] used and unused buf", i);
1629 VIRTQUEUE_DUMP(txvq->vq);
1634 * Stop device: disable interrupt and mark link down
1637 virtio_dev_stop(struct rte_eth_dev *dev)
1639 struct virtio_hw *hw = dev->data->dev_private;
1640 struct rte_eth_link link;
1642 PMD_INIT_LOG(DEBUG, "stop");
1644 if (dev->data->dev_conf.intr_conf.lsc) {
1645 rte_intr_disable(&dev->pci_dev->intr_handle);
1647 /* Reset interrupt callback */
1648 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1649 rte_intr_callback_unregister(&dev->pci_dev->intr_handle,
1650 virtio_interrupt_handler,
1656 memset(&link, 0, sizeof(link));
1657 virtio_dev_atomic_write_link_status(dev, &link);
1661 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1663 struct rte_eth_link link, old;
1665 struct virtio_hw *hw = dev->data->dev_private;
1666 memset(&link, 0, sizeof(link));
1667 virtio_dev_atomic_read_link_status(dev, &link);
1669 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1670 link.link_speed = SPEED_10G;
1672 if (hw->started == 0) {
1673 link.link_status = ETH_LINK_DOWN;
1674 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1675 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1676 vtpci_read_dev_config(hw,
1677 offsetof(struct virtio_net_config, status),
1678 &status, sizeof(status));
1679 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1680 link.link_status = ETH_LINK_DOWN;
1681 PMD_INIT_LOG(DEBUG, "Port %d is down",
1682 dev->data->port_id);
1684 link.link_status = ETH_LINK_UP;
1685 PMD_INIT_LOG(DEBUG, "Port %d is up",
1686 dev->data->port_id);
1689 link.link_status = ETH_LINK_UP;
1691 virtio_dev_atomic_write_link_status(dev, &link);
1693 return (old.link_status == link.link_status) ? -1 : 0;
1697 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1700 struct virtio_hw *hw = dev->data->dev_private;
1703 dev_info->driver_name = dev->driver->pci_drv.driver.name;
1705 dev_info->driver_name = "virtio_user PMD";
1706 dev_info->max_rx_queues =
1707 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
1708 dev_info->max_tx_queues =
1709 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
1710 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1711 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1712 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1713 dev_info->default_txconf = (struct rte_eth_txconf) {
1714 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1716 dev_info->rx_offload_capa =
1717 DEV_RX_OFFLOAD_TCP_CKSUM |
1718 DEV_RX_OFFLOAD_UDP_CKSUM;
1720 dev_info->tx_offload_capa = 0;
1722 if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
1723 dev_info->tx_offload_capa |=
1724 DEV_TX_OFFLOAD_UDP_CKSUM |
1725 DEV_TX_OFFLOAD_TCP_CKSUM;
1728 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1729 (1ULL << VIRTIO_NET_F_HOST_TSO6);
1730 if ((hw->guest_features & tso_mask) == tso_mask)
1731 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
1735 * It enables testpmd to collect per queue stats.
1738 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1739 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1740 __rte_unused uint8_t is_rx)
1745 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
1746 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);