1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <rte_ethdev_driver.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_memcpy.h>
14 #include <rte_string_fns.h>
15 #include <rte_memzone.h>
16 #include <rte_malloc.h>
17 #include <rte_branch_prediction.h>
19 #include <rte_bus_pci.h>
20 #include <rte_ether.h>
23 #include <rte_common.h>
24 #include <rte_errno.h>
25 #include <rte_cpuflags.h>
27 #include <rte_memory.h>
30 #include <rte_cycles.h>
31 #include <rte_kvargs.h>
33 #include "virtio_ethdev.h"
34 #include "virtio_pci.h"
35 #include "virtio_logs.h"
36 #include "virtqueue.h"
37 #include "virtio_rxtx.h"
39 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
40 static int virtio_dev_configure(struct rte_eth_dev *dev);
41 static int virtio_dev_start(struct rte_eth_dev *dev);
42 static void virtio_dev_stop(struct rte_eth_dev *dev);
43 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
44 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
45 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
46 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
47 static void virtio_dev_info_get(struct rte_eth_dev *dev,
48 struct rte_eth_dev_info *dev_info);
49 static int virtio_dev_link_update(struct rte_eth_dev *dev,
50 int wait_to_complete);
51 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
53 static void virtio_set_hwaddr(struct virtio_hw *hw);
54 static void virtio_get_hwaddr(struct virtio_hw *hw);
56 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
57 struct rte_eth_stats *stats);
58 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
59 struct rte_eth_xstat *xstats, unsigned n);
60 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
61 struct rte_eth_xstat_name *xstats_names,
63 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
64 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
65 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
66 uint16_t vlan_id, int on);
67 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
68 struct ether_addr *mac_addr,
69 uint32_t index, uint32_t vmdq);
70 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
71 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
72 struct ether_addr *mac_addr);
74 static int virtio_intr_enable(struct rte_eth_dev *dev);
75 static int virtio_intr_disable(struct rte_eth_dev *dev);
77 static int virtio_dev_queue_stats_mapping_set(
78 struct rte_eth_dev *eth_dev,
83 int virtio_logtype_init;
84 int virtio_logtype_driver;
86 static void virtio_notify_peers(struct rte_eth_dev *dev);
87 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
90 * The set of PCI devices this driver supports
92 static const struct rte_pci_id pci_id_virtio_map[] = {
93 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
94 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
95 { .vendor_id = 0, /* sentinel */ },
98 struct rte_virtio_xstats_name_off {
99 char name[RTE_ETH_XSTATS_NAME_SIZE];
103 /* [rt]x_qX_ is prepended to the name string here */
104 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
105 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
106 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
107 {"errors", offsetof(struct virtnet_rx, stats.errors)},
108 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
109 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
110 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
111 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
112 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
113 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
114 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
115 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
116 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
117 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
120 /* [rt]x_qX_ is prepended to the name string here */
121 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
122 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
123 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
124 {"errors", offsetof(struct virtnet_tx, stats.errors)},
125 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
126 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
127 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
128 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
129 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
130 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
131 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
132 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
133 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
134 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
137 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
138 sizeof(rte_virtio_rxq_stat_strings[0]))
139 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
140 sizeof(rte_virtio_txq_stat_strings[0]))
142 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
144 static struct virtio_pmd_ctrl *
145 virtio_pq_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
146 int *dlen, int pkt_num)
148 struct virtqueue *vq = cvq->vq;
150 struct vring_packed_desc *desc = vq->ring_packed.desc_packed;
151 struct virtio_pmd_ctrl *result;
158 * Format is enforced in qemu code:
159 * One TX packet for header;
160 * At least one TX packet per argument;
161 * One RX packet for ACK.
163 head = vq->vq_avail_idx;
164 wrap_counter = vq->avail_wrap_counter;
165 desc[head].flags = VRING_DESC_F_NEXT;
166 desc[head].addr = cvq->virtio_net_hdr_mem;
167 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
169 if (++vq->vq_avail_idx >= vq->vq_nentries) {
170 vq->vq_avail_idx -= vq->vq_nentries;
171 vq->avail_wrap_counter ^= 1;
174 for (k = 0; k < pkt_num; k++) {
175 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
176 + sizeof(struct virtio_net_ctrl_hdr)
177 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
178 desc[vq->vq_avail_idx].len = dlen[k];
179 flags = VRING_DESC_F_NEXT;
182 flags |= VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
183 VRING_DESC_F_USED(!vq->avail_wrap_counter);
184 desc[vq->vq_avail_idx].flags = flags;
187 if (++vq->vq_avail_idx >= vq->vq_nentries) {
188 vq->vq_avail_idx -= vq->vq_nentries;
189 vq->avail_wrap_counter ^= 1;
194 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
195 + sizeof(struct virtio_net_ctrl_hdr);
196 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
197 flags = VRING_DESC_F_WRITE;
198 flags |= VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
199 VRING_DESC_F_USED(!vq->avail_wrap_counter);
200 desc[vq->vq_avail_idx].flags = flags;
201 flags = VRING_DESC_F_NEXT;
202 flags |= VRING_DESC_F_AVAIL(wrap_counter) |
203 VRING_DESC_F_USED(!wrap_counter);
204 desc[head].flags = flags;
208 if (++vq->vq_avail_idx >= vq->vq_nentries) {
209 vq->vq_avail_idx -= vq->vq_nentries;
210 vq->avail_wrap_counter ^= 1;
213 virtqueue_notify(vq);
215 /* wait for used descriptors in virtqueue */
219 } while (!desc_is_used(&desc[head], vq));
221 /* now get used descriptors */
222 while (desc_is_used(&desc[vq->vq_used_cons_idx], vq)) {
224 if (++vq->vq_used_cons_idx >= vq->vq_nentries) {
225 vq->vq_used_cons_idx -= vq->vq_nentries;
226 vq->used_wrap_counter ^= 1;
230 result = cvq->virtio_net_hdr_mz->addr;
235 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
236 int *dlen, int pkt_num)
240 virtio_net_ctrl_ack status = ~0;
241 struct virtio_pmd_ctrl *result;
242 struct virtqueue *vq;
244 ctrl->status = status;
246 if (!cvq || !cvq->vq) {
247 PMD_INIT_LOG(ERR, "Control queue is not supported.");
251 rte_spinlock_lock(&cvq->lock);
253 head = vq->vq_desc_head_idx;
255 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
256 "vq->hw->cvq = %p vq = %p",
257 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
259 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
260 rte_spinlock_unlock(&cvq->lock);
264 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
265 sizeof(struct virtio_pmd_ctrl));
267 if (vtpci_packed_queue(vq->hw)) {
268 result = virtio_pq_send_command(cvq, ctrl, dlen, pkt_num);
273 * Format is enforced in qemu code:
274 * One TX packet for header;
275 * At least one TX packet per argument;
276 * One RX packet for ACK.
278 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
279 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
280 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
282 i = vq->vq_ring.desc[head].next;
284 for (k = 0; k < pkt_num; k++) {
285 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
286 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
287 + sizeof(struct virtio_net_ctrl_hdr)
288 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
289 vq->vq_ring.desc[i].len = dlen[k];
292 i = vq->vq_ring.desc[i].next;
295 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
296 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
297 + sizeof(struct virtio_net_ctrl_hdr);
298 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
301 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
303 vq_update_avail_ring(vq, head);
304 vq_update_avail_idx(vq);
306 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
308 virtqueue_notify(vq);
311 while (VIRTQUEUE_NUSED(vq) == 0) {
316 while (VIRTQUEUE_NUSED(vq)) {
317 uint32_t idx, desc_idx, used_idx;
318 struct vring_used_elem *uep;
320 used_idx = (uint32_t)(vq->vq_used_cons_idx
321 & (vq->vq_nentries - 1));
322 uep = &vq->vq_ring.used->ring[used_idx];
323 idx = (uint32_t) uep->id;
326 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
327 desc_idx = vq->vq_ring.desc[desc_idx].next;
331 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
332 vq->vq_desc_head_idx = idx;
334 vq->vq_used_cons_idx++;
338 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
339 vq->vq_free_cnt, vq->vq_desc_head_idx);
341 result = cvq->virtio_net_hdr_mz->addr;
344 rte_spinlock_unlock(&cvq->lock);
345 return result->status;
349 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
351 struct virtio_hw *hw = dev->data->dev_private;
352 struct virtio_pmd_ctrl ctrl;
356 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
357 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
358 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
360 dlen[0] = sizeof(uint16_t);
362 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
364 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
365 "failed, this is too late now...");
373 virtio_dev_queue_release(void *queue __rte_unused)
379 virtio_get_nr_vq(struct virtio_hw *hw)
381 uint16_t nr_vq = hw->max_queue_pairs * 2;
383 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
390 virtio_init_vring(struct virtqueue *vq)
392 int size = vq->vq_nentries;
393 struct vring *vr = &vq->vq_ring;
394 uint8_t *ring_mem = vq->vq_ring_virt_mem;
396 PMD_INIT_FUNC_TRACE();
398 memset(ring_mem, 0, vq->vq_ring_size);
400 vq->vq_used_cons_idx = 0;
401 vq->vq_desc_head_idx = 0;
402 vq->vq_avail_idx = 0;
403 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
404 vq->vq_free_cnt = vq->vq_nentries;
405 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
406 if (vtpci_packed_queue(vq->hw)) {
407 vring_init_packed(&vq->ring_packed, ring_mem,
408 VIRTIO_PCI_VRING_ALIGN, size);
409 vring_desc_init_packed(vq, size);
411 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
412 vring_desc_init_split(vr->desc, size);
415 * Disable device(host) interrupting guest
417 virtqueue_disable_intr(vq);
421 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
423 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
424 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
425 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
426 unsigned int vq_size, size;
427 struct virtio_hw *hw = dev->data->dev_private;
428 struct virtnet_rx *rxvq = NULL;
429 struct virtnet_tx *txvq = NULL;
430 struct virtnet_ctl *cvq = NULL;
431 struct virtqueue *vq;
432 size_t sz_hdr_mz = 0;
433 void *sw_ring = NULL;
434 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
436 int numa_node = dev->device->numa_node;
438 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
439 vtpci_queue_idx, numa_node);
442 * Read the virtqueue size from the Queue Size field
443 * Always power of 2 and if 0 virtqueue does not exist
445 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
446 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
448 PMD_INIT_LOG(ERR, "virtqueue does not exist");
452 if (!rte_is_power_of_2(vq_size)) {
453 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
457 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
458 dev->data->port_id, vtpci_queue_idx);
460 size = RTE_ALIGN_CEIL(sizeof(*vq) +
461 vq_size * sizeof(struct vq_desc_extra),
462 RTE_CACHE_LINE_SIZE);
463 if (queue_type == VTNET_TQ) {
465 * For each xmit packet, allocate a virtio_net_hdr
466 * and indirect ring elements
468 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
469 } else if (queue_type == VTNET_CQ) {
470 /* Allocate a page for control vq command, data and status */
471 sz_hdr_mz = PAGE_SIZE;
474 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
477 PMD_INIT_LOG(ERR, "can not allocate vq");
480 hw->vqs[vtpci_queue_idx] = vq;
483 vq->vq_queue_index = vtpci_queue_idx;
484 vq->vq_nentries = vq_size;
485 vq->event_flags_shadow = 0;
486 if (vtpci_packed_queue(hw)) {
487 vq->avail_wrap_counter = 1;
488 vq->used_wrap_counter = 1;
489 vq->avail_used_flags =
490 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
491 VRING_DESC_F_USED(!vq->avail_wrap_counter);
495 * Reserve a memzone for vring elements
497 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
498 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
499 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
500 size, vq->vq_ring_size);
502 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
503 numa_node, RTE_MEMZONE_IOVA_CONTIG,
504 VIRTIO_PCI_VRING_ALIGN);
506 if (rte_errno == EEXIST)
507 mz = rte_memzone_lookup(vq_name);
514 memset(mz->addr, 0, mz->len);
516 vq->vq_ring_mem = mz->iova;
517 vq->vq_ring_virt_mem = mz->addr;
518 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
520 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
521 (uint64_t)(uintptr_t)mz->addr);
523 virtio_init_vring(vq);
526 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
527 dev->data->port_id, vtpci_queue_idx);
528 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
529 numa_node, RTE_MEMZONE_IOVA_CONTIG,
530 RTE_CACHE_LINE_SIZE);
531 if (hdr_mz == NULL) {
532 if (rte_errno == EEXIST)
533 hdr_mz = rte_memzone_lookup(vq_hdr_name);
534 if (hdr_mz == NULL) {
541 if (queue_type == VTNET_RQ) {
542 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
543 sizeof(vq->sw_ring[0]);
545 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
546 RTE_CACHE_LINE_SIZE, numa_node);
548 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
553 vq->sw_ring = sw_ring;
556 rxvq->port_id = dev->data->port_id;
558 } else if (queue_type == VTNET_TQ) {
561 txvq->port_id = dev->data->port_id;
563 txvq->virtio_net_hdr_mz = hdr_mz;
564 txvq->virtio_net_hdr_mem = hdr_mz->iova;
565 } else if (queue_type == VTNET_CQ) {
569 cvq->virtio_net_hdr_mz = hdr_mz;
570 cvq->virtio_net_hdr_mem = hdr_mz->iova;
571 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
576 /* For virtio_user case (that is when hw->dev is NULL), we use
577 * virtual address. And we need properly set _offset_, please see
578 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
580 if (!hw->virtio_user_dev)
581 vq->offset = offsetof(struct rte_mbuf, buf_iova);
583 vq->vq_ring_mem = (uintptr_t)mz->addr;
584 vq->offset = offsetof(struct rte_mbuf, buf_addr);
585 if (queue_type == VTNET_TQ)
586 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
587 else if (queue_type == VTNET_CQ)
588 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
591 if (queue_type == VTNET_TQ) {
592 struct virtio_tx_region *txr;
596 memset(txr, 0, vq_size * sizeof(*txr));
597 for (i = 0; i < vq_size; i++) {
598 struct vring_desc *start_dp = txr[i].tx_indir;
599 struct vring_packed_desc *start_dp_packed =
602 /* first indirect descriptor is always the tx header */
603 if (vtpci_packed_queue(hw)) {
604 start_dp_packed->addr = txvq->virtio_net_hdr_mem
606 + offsetof(struct virtio_tx_region,
608 start_dp_packed->len = hw->vtnet_hdr_size;
610 vring_desc_init_split(start_dp,
611 RTE_DIM(txr[i].tx_indir));
612 start_dp->addr = txvq->virtio_net_hdr_mem
614 + offsetof(struct virtio_tx_region,
616 start_dp->len = hw->vtnet_hdr_size;
617 start_dp->flags = VRING_DESC_F_NEXT;
622 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
623 PMD_INIT_LOG(ERR, "setup_queue failed");
631 rte_memzone_free(hdr_mz);
632 rte_memzone_free(mz);
639 virtio_free_queues(struct virtio_hw *hw)
641 uint16_t nr_vq = virtio_get_nr_vq(hw);
642 struct virtqueue *vq;
649 for (i = 0; i < nr_vq; i++) {
654 queue_type = virtio_get_queue_type(hw, i);
655 if (queue_type == VTNET_RQ) {
656 rte_free(vq->sw_ring);
657 rte_memzone_free(vq->rxq.mz);
658 } else if (queue_type == VTNET_TQ) {
659 rte_memzone_free(vq->txq.mz);
660 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
662 rte_memzone_free(vq->cq.mz);
663 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
675 virtio_alloc_queues(struct rte_eth_dev *dev)
677 struct virtio_hw *hw = dev->data->dev_private;
678 uint16_t nr_vq = virtio_get_nr_vq(hw);
682 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
684 PMD_INIT_LOG(ERR, "failed to allocate vqs");
688 for (i = 0; i < nr_vq; i++) {
689 ret = virtio_init_queue(dev, i);
691 virtio_free_queues(hw);
699 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
702 virtio_dev_close(struct rte_eth_dev *dev)
704 struct virtio_hw *hw = dev->data->dev_private;
705 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
707 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
714 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
715 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
717 virtio_queues_unbind_intr(dev);
719 if (intr_conf->lsc || intr_conf->rxq) {
720 virtio_intr_disable(dev);
721 rte_intr_efd_disable(dev->intr_handle);
722 rte_free(dev->intr_handle->intr_vec);
723 dev->intr_handle->intr_vec = NULL;
727 virtio_dev_free_mbufs(dev);
728 virtio_free_queues(hw);
732 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
734 struct virtio_hw *hw = dev->data->dev_private;
735 struct virtio_pmd_ctrl ctrl;
739 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
740 PMD_INIT_LOG(INFO, "host does not support rx control");
744 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
745 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
749 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
751 PMD_INIT_LOG(ERR, "Failed to enable promisc");
755 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
757 struct virtio_hw *hw = dev->data->dev_private;
758 struct virtio_pmd_ctrl ctrl;
762 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
763 PMD_INIT_LOG(INFO, "host does not support rx control");
767 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
768 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
772 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
774 PMD_INIT_LOG(ERR, "Failed to disable promisc");
778 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
780 struct virtio_hw *hw = dev->data->dev_private;
781 struct virtio_pmd_ctrl ctrl;
785 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
786 PMD_INIT_LOG(INFO, "host does not support rx control");
790 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
791 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
795 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
797 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
801 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
803 struct virtio_hw *hw = dev->data->dev_private;
804 struct virtio_pmd_ctrl ctrl;
808 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
809 PMD_INIT_LOG(INFO, "host does not support rx control");
813 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
814 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
818 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
820 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
823 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
825 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
827 struct virtio_hw *hw = dev->data->dev_private;
828 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
830 uint32_t frame_size = mtu + ether_hdr_len;
831 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
833 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
835 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
836 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
837 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
844 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
846 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
847 struct virtqueue *vq = rxvq->vq;
849 virtqueue_enable_intr(vq);
854 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
856 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
857 struct virtqueue *vq = rxvq->vq;
859 virtqueue_disable_intr(vq);
864 * dev_ops for virtio, bare necessities for basic operation
866 static const struct eth_dev_ops virtio_eth_dev_ops = {
867 .dev_configure = virtio_dev_configure,
868 .dev_start = virtio_dev_start,
869 .dev_stop = virtio_dev_stop,
870 .dev_close = virtio_dev_close,
871 .promiscuous_enable = virtio_dev_promiscuous_enable,
872 .promiscuous_disable = virtio_dev_promiscuous_disable,
873 .allmulticast_enable = virtio_dev_allmulticast_enable,
874 .allmulticast_disable = virtio_dev_allmulticast_disable,
875 .mtu_set = virtio_mtu_set,
876 .dev_infos_get = virtio_dev_info_get,
877 .stats_get = virtio_dev_stats_get,
878 .xstats_get = virtio_dev_xstats_get,
879 .xstats_get_names = virtio_dev_xstats_get_names,
880 .stats_reset = virtio_dev_stats_reset,
881 .xstats_reset = virtio_dev_stats_reset,
882 .link_update = virtio_dev_link_update,
883 .vlan_offload_set = virtio_dev_vlan_offload_set,
884 .rx_queue_setup = virtio_dev_rx_queue_setup,
885 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
886 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
887 .rx_queue_release = virtio_dev_queue_release,
888 .rx_descriptor_done = virtio_dev_rx_queue_done,
889 .tx_queue_setup = virtio_dev_tx_queue_setup,
890 .tx_queue_release = virtio_dev_queue_release,
891 /* collect stats per queue */
892 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
893 .vlan_filter_set = virtio_vlan_filter_set,
894 .mac_addr_add = virtio_mac_addr_add,
895 .mac_addr_remove = virtio_mac_addr_remove,
896 .mac_addr_set = virtio_mac_addr_set,
900 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
904 for (i = 0; i < dev->data->nb_tx_queues; i++) {
905 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
909 stats->opackets += txvq->stats.packets;
910 stats->obytes += txvq->stats.bytes;
911 stats->oerrors += txvq->stats.errors;
913 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
914 stats->q_opackets[i] = txvq->stats.packets;
915 stats->q_obytes[i] = txvq->stats.bytes;
919 for (i = 0; i < dev->data->nb_rx_queues; i++) {
920 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
924 stats->ipackets += rxvq->stats.packets;
925 stats->ibytes += rxvq->stats.bytes;
926 stats->ierrors += rxvq->stats.errors;
928 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
929 stats->q_ipackets[i] = rxvq->stats.packets;
930 stats->q_ibytes[i] = rxvq->stats.bytes;
934 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
937 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
938 struct rte_eth_xstat_name *xstats_names,
939 __rte_unused unsigned limit)
945 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
946 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
948 if (xstats_names != NULL) {
949 /* Note: limit checked in rte_eth_xstats_names() */
951 for (i = 0; i < dev->data->nb_rx_queues; i++) {
952 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
955 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
956 snprintf(xstats_names[count].name,
957 sizeof(xstats_names[count].name),
959 rte_virtio_rxq_stat_strings[t].name);
964 for (i = 0; i < dev->data->nb_tx_queues; i++) {
965 struct virtnet_tx *txvq = dev->data->tx_queues[i];
968 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
969 snprintf(xstats_names[count].name,
970 sizeof(xstats_names[count].name),
972 rte_virtio_txq_stat_strings[t].name);
982 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
988 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
989 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
994 for (i = 0; i < dev->data->nb_rx_queues; i++) {
995 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1002 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1003 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1004 rte_virtio_rxq_stat_strings[t].offset);
1005 xstats[count].id = count;
1010 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1011 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1018 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1019 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1020 rte_virtio_txq_stat_strings[t].offset);
1021 xstats[count].id = count;
1030 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1032 virtio_update_stats(dev, stats);
1038 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1042 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1043 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1047 txvq->stats.packets = 0;
1048 txvq->stats.bytes = 0;
1049 txvq->stats.errors = 0;
1050 txvq->stats.multicast = 0;
1051 txvq->stats.broadcast = 0;
1052 memset(txvq->stats.size_bins, 0,
1053 sizeof(txvq->stats.size_bins[0]) * 8);
1056 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1057 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1061 rxvq->stats.packets = 0;
1062 rxvq->stats.bytes = 0;
1063 rxvq->stats.errors = 0;
1064 rxvq->stats.multicast = 0;
1065 rxvq->stats.broadcast = 0;
1066 memset(rxvq->stats.size_bins, 0,
1067 sizeof(rxvq->stats.size_bins[0]) * 8);
1072 virtio_set_hwaddr(struct virtio_hw *hw)
1074 vtpci_write_dev_config(hw,
1075 offsetof(struct virtio_net_config, mac),
1076 &hw->mac_addr, ETHER_ADDR_LEN);
1080 virtio_get_hwaddr(struct virtio_hw *hw)
1082 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1083 vtpci_read_dev_config(hw,
1084 offsetof(struct virtio_net_config, mac),
1085 &hw->mac_addr, ETHER_ADDR_LEN);
1087 eth_random_addr(&hw->mac_addr[0]);
1088 virtio_set_hwaddr(hw);
1093 virtio_mac_table_set(struct virtio_hw *hw,
1094 const struct virtio_net_ctrl_mac *uc,
1095 const struct virtio_net_ctrl_mac *mc)
1097 struct virtio_pmd_ctrl ctrl;
1100 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1101 PMD_DRV_LOG(INFO, "host does not support mac table");
1105 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1106 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1108 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1109 memcpy(ctrl.data, uc, len[0]);
1111 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1112 memcpy(ctrl.data + len[0], mc, len[1]);
1114 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1116 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1121 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1122 uint32_t index, uint32_t vmdq __rte_unused)
1124 struct virtio_hw *hw = dev->data->dev_private;
1125 const struct ether_addr *addrs = dev->data->mac_addrs;
1127 struct virtio_net_ctrl_mac *uc, *mc;
1129 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1130 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1134 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1136 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1139 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1140 const struct ether_addr *addr
1141 = (i == index) ? mac_addr : addrs + i;
1142 struct virtio_net_ctrl_mac *tbl
1143 = is_multicast_ether_addr(addr) ? mc : uc;
1145 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1148 return virtio_mac_table_set(hw, uc, mc);
1152 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1154 struct virtio_hw *hw = dev->data->dev_private;
1155 struct ether_addr *addrs = dev->data->mac_addrs;
1156 struct virtio_net_ctrl_mac *uc, *mc;
1159 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1160 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1164 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1166 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1169 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1170 struct virtio_net_ctrl_mac *tbl;
1172 if (i == index || is_zero_ether_addr(addrs + i))
1175 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1176 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1179 virtio_mac_table_set(hw, uc, mc);
1183 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1185 struct virtio_hw *hw = dev->data->dev_private;
1187 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1189 /* Use atomic update if available */
1190 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1191 struct virtio_pmd_ctrl ctrl;
1192 int len = ETHER_ADDR_LEN;
1194 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1195 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1197 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1198 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1201 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1204 virtio_set_hwaddr(hw);
1209 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1211 struct virtio_hw *hw = dev->data->dev_private;
1212 struct virtio_pmd_ctrl ctrl;
1215 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1218 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1219 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1220 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1221 len = sizeof(vlan_id);
1223 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1227 virtio_intr_enable(struct rte_eth_dev *dev)
1229 struct virtio_hw *hw = dev->data->dev_private;
1231 if (rte_intr_enable(dev->intr_handle) < 0)
1234 if (!hw->virtio_user_dev)
1235 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1241 virtio_intr_disable(struct rte_eth_dev *dev)
1243 struct virtio_hw *hw = dev->data->dev_private;
1245 if (rte_intr_disable(dev->intr_handle) < 0)
1248 if (!hw->virtio_user_dev)
1249 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1255 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1257 uint64_t host_features;
1259 /* Prepare guest_features: feature that driver wants to support */
1260 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1263 /* Read device(host) feature bits */
1264 host_features = VTPCI_OPS(hw)->get_features(hw);
1265 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1268 /* If supported, ensure MTU value is valid before acknowledging it. */
1269 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1270 struct virtio_net_config config;
1272 vtpci_read_dev_config(hw,
1273 offsetof(struct virtio_net_config, mtu),
1274 &config.mtu, sizeof(config.mtu));
1276 if (config.mtu < ETHER_MIN_MTU)
1277 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1281 * Negotiate features: Subset of device feature bits are written back
1282 * guest feature bits.
1284 hw->guest_features = req_features;
1285 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1286 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1287 hw->guest_features);
1290 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1292 "VIRTIO_F_VERSION_1 features is not enabled.");
1295 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1296 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1298 "failed to set FEATURES_OK status!");
1303 hw->req_guest_features = req_features;
1309 virtio_dev_pause(struct rte_eth_dev *dev)
1311 struct virtio_hw *hw = dev->data->dev_private;
1313 rte_spinlock_lock(&hw->state_lock);
1315 if (hw->started == 0) {
1316 /* Device is just stopped. */
1317 rte_spinlock_unlock(&hw->state_lock);
1322 * Prevent the worker threads from touching queues to avoid contention,
1323 * 1 ms should be enough for the ongoing Tx function to finish.
1330 * Recover hw state to let the worker threads continue.
1333 virtio_dev_resume(struct rte_eth_dev *dev)
1335 struct virtio_hw *hw = dev->data->dev_private;
1338 rte_spinlock_unlock(&hw->state_lock);
1342 * Should be called only after device is paused.
1345 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1348 struct virtio_hw *hw = dev->data->dev_private;
1349 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1352 hw->inject_pkts = tx_pkts;
1353 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1354 hw->inject_pkts = NULL;
1360 virtio_notify_peers(struct rte_eth_dev *dev)
1362 struct virtio_hw *hw = dev->data->dev_private;
1363 struct virtnet_rx *rxvq;
1364 struct rte_mbuf *rarp_mbuf;
1366 if (!dev->data->rx_queues)
1369 rxvq = dev->data->rx_queues[0];
1373 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1374 (struct ether_addr *)hw->mac_addr);
1375 if (rarp_mbuf == NULL) {
1376 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1380 /* If virtio port just stopped, no need to send RARP */
1381 if (virtio_dev_pause(dev) < 0) {
1382 rte_pktmbuf_free(rarp_mbuf);
1386 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1387 virtio_dev_resume(dev);
1391 virtio_ack_link_announce(struct rte_eth_dev *dev)
1393 struct virtio_hw *hw = dev->data->dev_private;
1394 struct virtio_pmd_ctrl ctrl;
1396 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1397 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1399 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1403 * Process virtio config changed interrupt. Call the callback
1404 * if link state changed, generate gratuitous RARP packet if
1405 * the status indicates an ANNOUNCE.
1408 virtio_interrupt_handler(void *param)
1410 struct rte_eth_dev *dev = param;
1411 struct virtio_hw *hw = dev->data->dev_private;
1415 /* Read interrupt status which clears interrupt */
1416 isr = vtpci_isr(hw);
1417 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1419 if (virtio_intr_enable(dev) < 0)
1420 PMD_DRV_LOG(ERR, "interrupt enable failed");
1422 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1423 if (virtio_dev_link_update(dev, 0) == 0)
1424 _rte_eth_dev_callback_process(dev,
1425 RTE_ETH_EVENT_INTR_LSC,
1428 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1429 vtpci_read_dev_config(hw,
1430 offsetof(struct virtio_net_config, status),
1431 &status, sizeof(status));
1432 if (status & VIRTIO_NET_S_ANNOUNCE) {
1433 virtio_notify_peers(dev);
1435 virtio_ack_link_announce(dev);
1441 /* set rx and tx handlers according to what is supported */
1443 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1445 struct virtio_hw *hw = eth_dev->data->dev_private;
1447 if (vtpci_packed_queue(hw)) {
1449 "virtio: using packed ring standard Tx path on port %u",
1450 eth_dev->data->port_id);
1451 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1453 if (hw->use_inorder_tx) {
1454 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1455 eth_dev->data->port_id);
1456 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1458 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1459 eth_dev->data->port_id);
1460 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1464 if (vtpci_packed_queue(hw)) {
1465 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1467 "virtio: using packed ring mergeable buffer Rx path on port %u",
1468 eth_dev->data->port_id);
1469 eth_dev->rx_pkt_burst =
1470 &virtio_recv_mergeable_pkts_packed;
1473 "virtio: using packed ring standard Rx path on port %u",
1474 eth_dev->data->port_id);
1475 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1478 if (hw->use_simple_rx) {
1479 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1480 eth_dev->data->port_id);
1481 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1482 } else if (hw->use_inorder_rx) {
1484 "virtio: using inorder mergeable buffer Rx path on port %u",
1485 eth_dev->data->port_id);
1486 eth_dev->rx_pkt_burst =
1487 &virtio_recv_mergeable_pkts_inorder;
1488 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1490 "virtio: using mergeable buffer Rx path on port %u",
1491 eth_dev->data->port_id);
1492 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1494 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1495 eth_dev->data->port_id);
1496 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1502 /* Only support 1:1 queue/interrupt mapping so far.
1503 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1504 * interrupt vectors (<N+1).
1507 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1510 struct virtio_hw *hw = dev->data->dev_private;
1512 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1513 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1514 dev->intr_handle->intr_vec[i] = i + 1;
1515 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1516 VIRTIO_MSI_NO_VECTOR) {
1517 PMD_DRV_LOG(ERR, "failed to set queue vector");
1526 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1529 struct virtio_hw *hw = dev->data->dev_private;
1531 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1532 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1533 VTPCI_OPS(hw)->set_queue_irq(hw,
1534 hw->vqs[i * VTNET_CQ],
1535 VIRTIO_MSI_NO_VECTOR);
1539 virtio_configure_intr(struct rte_eth_dev *dev)
1541 struct virtio_hw *hw = dev->data->dev_private;
1543 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1544 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1548 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1549 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1553 if (!dev->intr_handle->intr_vec) {
1554 dev->intr_handle->intr_vec =
1555 rte_zmalloc("intr_vec",
1556 hw->max_queue_pairs * sizeof(int), 0);
1557 if (!dev->intr_handle->intr_vec) {
1558 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1559 hw->max_queue_pairs);
1564 /* Re-register callback to update max_intr */
1565 rte_intr_callback_unregister(dev->intr_handle,
1566 virtio_interrupt_handler,
1568 rte_intr_callback_register(dev->intr_handle,
1569 virtio_interrupt_handler,
1572 /* DO NOT try to remove this! This function will enable msix, or QEMU
1573 * will encounter SIGSEGV when DRIVER_OK is sent.
1574 * And for legacy devices, this should be done before queue/vec binding
1575 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1576 * (22) will be ignored.
1578 if (virtio_intr_enable(dev) < 0) {
1579 PMD_DRV_LOG(ERR, "interrupt enable failed");
1583 if (virtio_queues_bind_intr(dev) < 0) {
1584 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1591 /* reset device and renegotiate features if needed */
1593 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1595 struct virtio_hw *hw = eth_dev->data->dev_private;
1596 struct virtio_net_config *config;
1597 struct virtio_net_config local_config;
1598 struct rte_pci_device *pci_dev = NULL;
1601 /* Reset the device although not necessary at startup */
1605 virtio_dev_free_mbufs(eth_dev);
1606 virtio_free_queues(hw);
1609 /* Tell the host we've noticed this device. */
1610 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1612 /* Tell the host we've known how to drive the device. */
1613 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1614 if (virtio_negotiate_features(hw, req_features) < 0)
1617 if (!hw->virtio_user_dev) {
1618 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1619 rte_eth_copy_pci_info(eth_dev, pci_dev);
1622 /* If host does not support both status and MSI-X then disable LSC */
1623 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1624 hw->use_msix != VIRTIO_MSIX_NONE)
1625 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1627 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1629 /* Setting up rx_header size for the device */
1630 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1631 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1632 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1633 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1635 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1637 /* Copy the permanent MAC address to: virtio_hw */
1638 virtio_get_hwaddr(hw);
1639 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1640 ð_dev->data->mac_addrs[0]);
1642 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1643 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1644 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1646 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1647 config = &local_config;
1649 vtpci_read_dev_config(hw,
1650 offsetof(struct virtio_net_config, mac),
1651 &config->mac, sizeof(config->mac));
1653 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1654 vtpci_read_dev_config(hw,
1655 offsetof(struct virtio_net_config, status),
1656 &config->status, sizeof(config->status));
1659 "VIRTIO_NET_F_STATUS is not supported");
1663 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1664 vtpci_read_dev_config(hw,
1665 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1666 &config->max_virtqueue_pairs,
1667 sizeof(config->max_virtqueue_pairs));
1670 "VIRTIO_NET_F_MQ is not supported");
1671 config->max_virtqueue_pairs = 1;
1674 hw->max_queue_pairs = config->max_virtqueue_pairs;
1676 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1677 vtpci_read_dev_config(hw,
1678 offsetof(struct virtio_net_config, mtu),
1680 sizeof(config->mtu));
1683 * MTU value has already been checked at negotiation
1684 * time, but check again in case it has changed since
1685 * then, which should not happen.
1687 if (config->mtu < ETHER_MIN_MTU) {
1688 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1693 hw->max_mtu = config->mtu;
1694 /* Set initial MTU to maximum one supported by vhost */
1695 eth_dev->data->mtu = config->mtu;
1698 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1699 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1702 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1703 config->max_virtqueue_pairs);
1704 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1706 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1707 config->mac[0], config->mac[1],
1708 config->mac[2], config->mac[3],
1709 config->mac[4], config->mac[5]);
1711 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1712 hw->max_queue_pairs = 1;
1713 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1714 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1717 ret = virtio_alloc_queues(eth_dev);
1721 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1722 if (virtio_configure_intr(eth_dev) < 0) {
1723 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1728 vtpci_reinit_complete(hw);
1731 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1732 eth_dev->data->port_id, pci_dev->id.vendor_id,
1733 pci_dev->id.device_id);
1739 * Remap the PCI device again (IO port map for legacy device and
1740 * memory map for modern device), so that the secondary process
1741 * could have the PCI initiated correctly.
1744 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1748 * We don't have to re-parse the PCI config space, since
1749 * rte_pci_map_device() makes sure the mapped address
1750 * in secondary process would equal to the one mapped in
1751 * the primary process: error will be returned if that
1752 * requirement is not met.
1754 * That said, we could simply reuse all cap pointers
1755 * (such as dev_cfg, common_cfg, etc.) parsed from the
1756 * primary process, which is stored in shared memory.
1758 if (rte_pci_map_device(pci_dev)) {
1759 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1763 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1771 virtio_set_vtpci_ops(struct virtio_hw *hw)
1773 #ifdef RTE_VIRTIO_USER
1774 if (hw->virtio_user_dev)
1775 VTPCI_OPS(hw) = &virtio_user_ops;
1779 VTPCI_OPS(hw) = &modern_ops;
1781 VTPCI_OPS(hw) = &legacy_ops;
1785 * This function is based on probe() function in virtio_pci.c
1786 * It returns 0 on success.
1789 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1791 struct virtio_hw *hw = eth_dev->data->dev_private;
1794 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1796 eth_dev->dev_ops = &virtio_eth_dev_ops;
1798 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1799 if (!hw->virtio_user_dev) {
1800 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1805 virtio_set_vtpci_ops(hw);
1806 set_rxtx_funcs(eth_dev);
1811 /* Allocate memory for storing MAC addresses */
1812 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1813 if (eth_dev->data->mac_addrs == NULL) {
1815 "Failed to allocate %d bytes needed to store MAC addresses",
1816 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1820 hw->port_id = eth_dev->data->port_id;
1821 /* For virtio_user case the hw->virtio_user_dev is populated by
1822 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1824 if (!hw->virtio_user_dev) {
1825 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1830 /* reset device and negotiate default features */
1831 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1838 rte_free(eth_dev->data->mac_addrs);
1843 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1845 PMD_INIT_FUNC_TRACE();
1847 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1850 virtio_dev_stop(eth_dev);
1851 virtio_dev_close(eth_dev);
1853 eth_dev->dev_ops = NULL;
1854 eth_dev->tx_pkt_burst = NULL;
1855 eth_dev->rx_pkt_burst = NULL;
1857 if (eth_dev->device)
1858 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1860 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1865 static int vdpa_check_handler(__rte_unused const char *key,
1866 const char *value, __rte_unused void *opaque)
1868 if (strcmp(value, "1"))
1875 vdpa_mode_selected(struct rte_devargs *devargs)
1877 struct rte_kvargs *kvlist;
1878 const char *key = "vdpa";
1881 if (devargs == NULL)
1884 kvlist = rte_kvargs_parse(devargs->args, NULL);
1888 if (!rte_kvargs_count(kvlist, key))
1891 /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1892 if (rte_kvargs_process(kvlist, key,
1893 vdpa_check_handler, NULL) < 0) {
1899 rte_kvargs_free(kvlist);
1903 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1904 struct rte_pci_device *pci_dev)
1906 if (rte_eal_iopl_init() != 0) {
1907 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1911 /* virtio pmd skips probe if device needs to work in vdpa mode */
1912 if (vdpa_mode_selected(pci_dev->device.devargs))
1915 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1916 eth_virtio_dev_init);
1919 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1921 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1924 static struct rte_pci_driver rte_virtio_pmd = {
1926 .name = "net_virtio",
1928 .id_table = pci_id_virtio_map,
1930 .probe = eth_virtio_pci_probe,
1931 .remove = eth_virtio_pci_remove,
1934 RTE_INIT(rte_virtio_pmd_init)
1936 rte_eal_iopl_init();
1937 rte_pci_register(&rte_virtio_pmd);
1941 rx_offload_enabled(struct virtio_hw *hw)
1943 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1944 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1945 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1949 tx_offload_enabled(struct virtio_hw *hw)
1951 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1952 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1953 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1957 * Configure virtio device
1958 * It returns 0 on success.
1961 virtio_dev_configure(struct rte_eth_dev *dev)
1963 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1964 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1965 struct virtio_hw *hw = dev->data->dev_private;
1966 uint64_t rx_offloads = rxmode->offloads;
1967 uint64_t tx_offloads = txmode->offloads;
1968 uint64_t req_features;
1971 PMD_INIT_LOG(DEBUG, "configure");
1972 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1974 if (dev->data->dev_conf.intr_conf.rxq) {
1975 ret = virtio_init_device(dev, hw->req_guest_features);
1980 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1981 DEV_RX_OFFLOAD_TCP_CKSUM))
1982 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1984 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
1986 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1987 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1989 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
1990 DEV_TX_OFFLOAD_TCP_CKSUM))
1991 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
1993 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
1995 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1996 (1ULL << VIRTIO_NET_F_HOST_TSO6);
1998 /* if request features changed, reinit the device */
1999 if (req_features != hw->req_guest_features) {
2000 ret = virtio_init_device(dev, req_features);
2005 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2006 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2007 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2009 "rx checksum not available on this host");
2013 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2014 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2015 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2017 "Large Receive Offload not available on this host");
2021 /* start control queue */
2022 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2023 virtio_dev_cq_start(dev);
2025 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2028 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2029 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2031 "vlan filtering not available on this host");
2035 hw->has_tx_offload = tx_offload_enabled(hw);
2036 hw->has_rx_offload = rx_offload_enabled(hw);
2038 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2039 /* Enable vector (0) for Link State Intrerrupt */
2040 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2041 VIRTIO_MSI_NO_VECTOR) {
2042 PMD_DRV_LOG(ERR, "failed to set config vector");
2046 rte_spinlock_init(&hw->state_lock);
2048 hw->use_simple_rx = 1;
2050 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2051 hw->use_inorder_tx = 1;
2052 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) &&
2053 !vtpci_packed_queue(hw)) {
2054 hw->use_inorder_rx = 1;
2055 hw->use_simple_rx = 0;
2057 hw->use_inorder_rx = 0;
2061 if (vtpci_packed_queue(hw)) {
2062 hw->use_simple_rx = 0;
2063 hw->use_inorder_rx = 0;
2064 hw->use_inorder_tx = 0;
2067 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2068 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2069 hw->use_simple_rx = 0;
2072 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2073 hw->use_simple_rx = 0;
2076 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2077 DEV_RX_OFFLOAD_TCP_CKSUM |
2078 DEV_RX_OFFLOAD_TCP_LRO |
2079 DEV_RX_OFFLOAD_VLAN_STRIP))
2080 hw->use_simple_rx = 0;
2089 virtio_dev_start(struct rte_eth_dev *dev)
2091 uint16_t nb_queues, i;
2092 struct virtnet_rx *rxvq;
2093 struct virtnet_tx *txvq __rte_unused;
2094 struct virtio_hw *hw = dev->data->dev_private;
2097 /* Finish the initialization of the queues */
2098 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2099 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2103 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2104 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2109 /* check if lsc interrupt feature is enabled */
2110 if (dev->data->dev_conf.intr_conf.lsc) {
2111 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2112 PMD_DRV_LOG(ERR, "link status not supported by host");
2117 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2118 * in device configure, but it could be unmapped when device is
2121 if (dev->data->dev_conf.intr_conf.lsc ||
2122 dev->data->dev_conf.intr_conf.rxq) {
2123 virtio_intr_disable(dev);
2125 /* Setup interrupt callback */
2126 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2127 rte_intr_callback_register(dev->intr_handle,
2128 virtio_interrupt_handler,
2131 if (virtio_intr_enable(dev) < 0) {
2132 PMD_DRV_LOG(ERR, "interrupt enable failed");
2137 /*Notify the backend
2138 *Otherwise the tap backend might already stop its queue due to fullness.
2139 *vhost backend will have no chance to be waked up
2141 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2142 if (hw->max_queue_pairs > 1) {
2143 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2147 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2149 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2150 rxvq = dev->data->rx_queues[i];
2151 /* Flush the old packets */
2152 virtqueue_rxvq_flush(rxvq->vq);
2153 virtqueue_notify(rxvq->vq);
2156 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2157 txvq = dev->data->tx_queues[i];
2158 virtqueue_notify(txvq->vq);
2161 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2163 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2164 rxvq = dev->data->rx_queues[i];
2165 VIRTQUEUE_DUMP(rxvq->vq);
2168 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2169 txvq = dev->data->tx_queues[i];
2170 VIRTQUEUE_DUMP(txvq->vq);
2173 set_rxtx_funcs(dev);
2176 /* Initialize Link state */
2177 virtio_dev_link_update(dev, 0);
2182 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2184 struct virtio_hw *hw = dev->data->dev_private;
2185 uint16_t nr_vq = virtio_get_nr_vq(hw);
2186 const char *type __rte_unused;
2187 unsigned int i, mbuf_num = 0;
2188 struct virtqueue *vq;
2189 struct rte_mbuf *buf;
2192 if (hw->vqs == NULL)
2195 for (i = 0; i < nr_vq; i++) {
2200 queue_type = virtio_get_queue_type(hw, i);
2201 if (queue_type == VTNET_RQ)
2203 else if (queue_type == VTNET_TQ)
2209 "Before freeing %s[%d] used and unused buf",
2213 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2214 rte_pktmbuf_free(buf);
2219 "After freeing %s[%d] used and unused buf",
2224 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2228 * Stop device: disable interrupt and mark link down
2231 virtio_dev_stop(struct rte_eth_dev *dev)
2233 struct virtio_hw *hw = dev->data->dev_private;
2234 struct rte_eth_link link;
2235 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2237 PMD_INIT_LOG(DEBUG, "stop");
2239 rte_spinlock_lock(&hw->state_lock);
2242 hw->started = false;
2244 if (intr_conf->lsc || intr_conf->rxq) {
2245 virtio_intr_disable(dev);
2247 /* Reset interrupt callback */
2248 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2249 rte_intr_callback_unregister(dev->intr_handle,
2250 virtio_interrupt_handler,
2255 memset(&link, 0, sizeof(link));
2256 rte_eth_linkstatus_set(dev, &link);
2258 rte_spinlock_unlock(&hw->state_lock);
2262 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2264 struct rte_eth_link link;
2266 struct virtio_hw *hw = dev->data->dev_private;
2268 memset(&link, 0, sizeof(link));
2269 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2270 link.link_speed = ETH_SPEED_NUM_10G;
2271 link.link_autoneg = ETH_LINK_FIXED;
2274 link.link_status = ETH_LINK_DOWN;
2275 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2276 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2277 vtpci_read_dev_config(hw,
2278 offsetof(struct virtio_net_config, status),
2279 &status, sizeof(status));
2280 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2281 link.link_status = ETH_LINK_DOWN;
2282 PMD_INIT_LOG(DEBUG, "Port %d is down",
2283 dev->data->port_id);
2285 link.link_status = ETH_LINK_UP;
2286 PMD_INIT_LOG(DEBUG, "Port %d is up",
2287 dev->data->port_id);
2290 link.link_status = ETH_LINK_UP;
2293 return rte_eth_linkstatus_set(dev, &link);
2297 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2299 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2300 struct virtio_hw *hw = dev->data->dev_private;
2301 uint64_t offloads = rxmode->offloads;
2303 if (mask & ETH_VLAN_FILTER_MASK) {
2304 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2305 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2308 "vlan filtering not available on this host");
2314 if (mask & ETH_VLAN_STRIP_MASK)
2315 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2321 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2323 uint64_t tso_mask, host_features;
2324 struct virtio_hw *hw = dev->data->dev_private;
2326 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2328 dev_info->max_rx_queues =
2329 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2330 dev_info->max_tx_queues =
2331 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2332 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2333 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2334 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2336 host_features = VTPCI_OPS(hw)->get_features(hw);
2337 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2338 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2339 dev_info->rx_offload_capa |=
2340 DEV_RX_OFFLOAD_TCP_CKSUM |
2341 DEV_RX_OFFLOAD_UDP_CKSUM;
2343 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2344 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2345 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2346 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2347 if ((host_features & tso_mask) == tso_mask)
2348 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2350 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2351 DEV_TX_OFFLOAD_VLAN_INSERT;
2352 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2353 dev_info->tx_offload_capa |=
2354 DEV_TX_OFFLOAD_UDP_CKSUM |
2355 DEV_TX_OFFLOAD_TCP_CKSUM;
2357 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2358 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2359 if ((host_features & tso_mask) == tso_mask)
2360 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2364 * It enables testpmd to collect per queue stats.
2367 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2368 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2369 __rte_unused uint8_t is_rx)
2374 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2375 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2376 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2378 RTE_INIT(virtio_init_log)
2380 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2381 if (virtio_logtype_init >= 0)
2382 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2383 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2384 if (virtio_logtype_driver >= 0)
2385 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);