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 Rx path on port %u",
1485 eth_dev->data->port_id);
1486 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1487 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1489 "virtio: using mergeable buffer Rx path on port %u",
1490 eth_dev->data->port_id);
1491 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1493 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1494 eth_dev->data->port_id);
1495 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1501 /* Only support 1:1 queue/interrupt mapping so far.
1502 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1503 * interrupt vectors (<N+1).
1506 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1509 struct virtio_hw *hw = dev->data->dev_private;
1511 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1512 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1513 dev->intr_handle->intr_vec[i] = i + 1;
1514 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1515 VIRTIO_MSI_NO_VECTOR) {
1516 PMD_DRV_LOG(ERR, "failed to set queue vector");
1525 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1528 struct virtio_hw *hw = dev->data->dev_private;
1530 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1531 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1532 VTPCI_OPS(hw)->set_queue_irq(hw,
1533 hw->vqs[i * VTNET_CQ],
1534 VIRTIO_MSI_NO_VECTOR);
1538 virtio_configure_intr(struct rte_eth_dev *dev)
1540 struct virtio_hw *hw = dev->data->dev_private;
1542 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1543 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1547 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1548 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1552 if (!dev->intr_handle->intr_vec) {
1553 dev->intr_handle->intr_vec =
1554 rte_zmalloc("intr_vec",
1555 hw->max_queue_pairs * sizeof(int), 0);
1556 if (!dev->intr_handle->intr_vec) {
1557 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1558 hw->max_queue_pairs);
1563 /* Re-register callback to update max_intr */
1564 rte_intr_callback_unregister(dev->intr_handle,
1565 virtio_interrupt_handler,
1567 rte_intr_callback_register(dev->intr_handle,
1568 virtio_interrupt_handler,
1571 /* DO NOT try to remove this! This function will enable msix, or QEMU
1572 * will encounter SIGSEGV when DRIVER_OK is sent.
1573 * And for legacy devices, this should be done before queue/vec binding
1574 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1575 * (22) will be ignored.
1577 if (virtio_intr_enable(dev) < 0) {
1578 PMD_DRV_LOG(ERR, "interrupt enable failed");
1582 if (virtio_queues_bind_intr(dev) < 0) {
1583 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1590 /* reset device and renegotiate features if needed */
1592 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1594 struct virtio_hw *hw = eth_dev->data->dev_private;
1595 struct virtio_net_config *config;
1596 struct virtio_net_config local_config;
1597 struct rte_pci_device *pci_dev = NULL;
1600 /* Reset the device although not necessary at startup */
1604 virtio_dev_free_mbufs(eth_dev);
1605 virtio_free_queues(hw);
1608 /* Tell the host we've noticed this device. */
1609 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1611 /* Tell the host we've known how to drive the device. */
1612 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1613 if (virtio_negotiate_features(hw, req_features) < 0)
1616 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1618 if (!hw->virtio_user_dev) {
1619 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1620 rte_eth_copy_pci_info(eth_dev, pci_dev);
1623 /* If host does not support both status and MSI-X then disable LSC */
1624 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1625 hw->use_msix != VIRTIO_MSIX_NONE)
1626 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1628 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1630 /* Setting up rx_header size for the device */
1631 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1632 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1633 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1634 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1636 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1638 /* Copy the permanent MAC address to: virtio_hw */
1639 virtio_get_hwaddr(hw);
1640 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1641 ð_dev->data->mac_addrs[0]);
1643 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1644 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1645 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1647 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1648 config = &local_config;
1650 vtpci_read_dev_config(hw,
1651 offsetof(struct virtio_net_config, mac),
1652 &config->mac, sizeof(config->mac));
1654 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1655 vtpci_read_dev_config(hw,
1656 offsetof(struct virtio_net_config, status),
1657 &config->status, sizeof(config->status));
1660 "VIRTIO_NET_F_STATUS is not supported");
1664 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1665 vtpci_read_dev_config(hw,
1666 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1667 &config->max_virtqueue_pairs,
1668 sizeof(config->max_virtqueue_pairs));
1671 "VIRTIO_NET_F_MQ is not supported");
1672 config->max_virtqueue_pairs = 1;
1675 hw->max_queue_pairs = config->max_virtqueue_pairs;
1677 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1678 vtpci_read_dev_config(hw,
1679 offsetof(struct virtio_net_config, mtu),
1681 sizeof(config->mtu));
1684 * MTU value has already been checked at negotiation
1685 * time, but check again in case it has changed since
1686 * then, which should not happen.
1688 if (config->mtu < ETHER_MIN_MTU) {
1689 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1694 hw->max_mtu = config->mtu;
1695 /* Set initial MTU to maximum one supported by vhost */
1696 eth_dev->data->mtu = config->mtu;
1699 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1700 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1703 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1704 config->max_virtqueue_pairs);
1705 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1707 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1708 config->mac[0], config->mac[1],
1709 config->mac[2], config->mac[3],
1710 config->mac[4], config->mac[5]);
1712 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1713 hw->max_queue_pairs = 1;
1714 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1715 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1718 ret = virtio_alloc_queues(eth_dev);
1722 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1723 if (virtio_configure_intr(eth_dev) < 0) {
1724 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1729 vtpci_reinit_complete(hw);
1732 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1733 eth_dev->data->port_id, pci_dev->id.vendor_id,
1734 pci_dev->id.device_id);
1740 * Remap the PCI device again (IO port map for legacy device and
1741 * memory map for modern device), so that the secondary process
1742 * could have the PCI initiated correctly.
1745 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1749 * We don't have to re-parse the PCI config space, since
1750 * rte_pci_map_device() makes sure the mapped address
1751 * in secondary process would equal to the one mapped in
1752 * the primary process: error will be returned if that
1753 * requirement is not met.
1755 * That said, we could simply reuse all cap pointers
1756 * (such as dev_cfg, common_cfg, etc.) parsed from the
1757 * primary process, which is stored in shared memory.
1759 if (rte_pci_map_device(pci_dev)) {
1760 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1764 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1772 virtio_set_vtpci_ops(struct virtio_hw *hw)
1774 #ifdef RTE_VIRTIO_USER
1775 if (hw->virtio_user_dev)
1776 VTPCI_OPS(hw) = &virtio_user_ops;
1780 VTPCI_OPS(hw) = &modern_ops;
1782 VTPCI_OPS(hw) = &legacy_ops;
1786 * This function is based on probe() function in virtio_pci.c
1787 * It returns 0 on success.
1790 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1792 struct virtio_hw *hw = eth_dev->data->dev_private;
1795 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1797 eth_dev->dev_ops = &virtio_eth_dev_ops;
1799 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1800 if (!hw->virtio_user_dev) {
1801 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1806 virtio_set_vtpci_ops(hw);
1807 set_rxtx_funcs(eth_dev);
1812 /* Allocate memory for storing MAC addresses */
1813 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1814 if (eth_dev->data->mac_addrs == NULL) {
1816 "Failed to allocate %d bytes needed to store MAC addresses",
1817 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1821 hw->port_id = eth_dev->data->port_id;
1822 /* For virtio_user case the hw->virtio_user_dev is populated by
1823 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1825 if (!hw->virtio_user_dev) {
1826 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1831 /* reset device and negotiate default features */
1832 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1839 rte_free(eth_dev->data->mac_addrs);
1844 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1846 PMD_INIT_FUNC_TRACE();
1848 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1851 virtio_dev_stop(eth_dev);
1852 virtio_dev_close(eth_dev);
1854 eth_dev->dev_ops = NULL;
1855 eth_dev->tx_pkt_burst = NULL;
1856 eth_dev->rx_pkt_burst = NULL;
1858 if (eth_dev->device)
1859 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1861 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1866 static int vdpa_check_handler(__rte_unused const char *key,
1867 const char *value, __rte_unused void *opaque)
1869 if (strcmp(value, "1"))
1876 vdpa_mode_selected(struct rte_devargs *devargs)
1878 struct rte_kvargs *kvlist;
1879 const char *key = "vdpa";
1882 if (devargs == NULL)
1885 kvlist = rte_kvargs_parse(devargs->args, NULL);
1889 if (!rte_kvargs_count(kvlist, key))
1892 /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1893 if (rte_kvargs_process(kvlist, key,
1894 vdpa_check_handler, NULL) < 0) {
1900 rte_kvargs_free(kvlist);
1904 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1905 struct rte_pci_device *pci_dev)
1907 if (rte_eal_iopl_init() != 0) {
1908 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1912 /* virtio pmd skips probe if device needs to work in vdpa mode */
1913 if (vdpa_mode_selected(pci_dev->device.devargs))
1916 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1917 eth_virtio_dev_init);
1920 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1922 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1925 static struct rte_pci_driver rte_virtio_pmd = {
1927 .name = "net_virtio",
1929 .id_table = pci_id_virtio_map,
1931 .probe = eth_virtio_pci_probe,
1932 .remove = eth_virtio_pci_remove,
1935 RTE_INIT(rte_virtio_pmd_init)
1937 rte_eal_iopl_init();
1938 rte_pci_register(&rte_virtio_pmd);
1942 rx_offload_enabled(struct virtio_hw *hw)
1944 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1945 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1946 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1950 tx_offload_enabled(struct virtio_hw *hw)
1952 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1953 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1954 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1958 * Configure virtio device
1959 * It returns 0 on success.
1962 virtio_dev_configure(struct rte_eth_dev *dev)
1964 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1965 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1966 struct virtio_hw *hw = dev->data->dev_private;
1967 uint64_t rx_offloads = rxmode->offloads;
1968 uint64_t tx_offloads = txmode->offloads;
1969 uint64_t req_features;
1972 PMD_INIT_LOG(DEBUG, "configure");
1973 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1975 if (dev->data->dev_conf.intr_conf.rxq) {
1976 ret = virtio_init_device(dev, hw->req_guest_features);
1981 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1982 DEV_RX_OFFLOAD_TCP_CKSUM))
1983 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1985 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
1987 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1988 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1990 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
1991 DEV_TX_OFFLOAD_TCP_CKSUM))
1992 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
1994 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
1996 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1997 (1ULL << VIRTIO_NET_F_HOST_TSO6);
1999 /* if request features changed, reinit the device */
2000 if (req_features != hw->req_guest_features) {
2001 ret = virtio_init_device(dev, req_features);
2006 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2007 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2008 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2010 "rx checksum not available on this host");
2014 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2015 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2016 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2018 "Large Receive Offload not available on this host");
2022 /* start control queue */
2023 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2024 virtio_dev_cq_start(dev);
2026 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2029 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2030 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2032 "vlan filtering not available on this host");
2036 hw->has_tx_offload = tx_offload_enabled(hw);
2037 hw->has_rx_offload = rx_offload_enabled(hw);
2039 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2040 /* Enable vector (0) for Link State Intrerrupt */
2041 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2042 VIRTIO_MSI_NO_VECTOR) {
2043 PMD_DRV_LOG(ERR, "failed to set config vector");
2047 rte_spinlock_init(&hw->state_lock);
2049 hw->use_simple_rx = 1;
2051 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2052 hw->use_inorder_tx = 1;
2053 hw->use_inorder_rx = 1;
2054 hw->use_simple_rx = 0;
2057 if (vtpci_packed_queue(hw)) {
2058 hw->use_simple_rx = 0;
2059 hw->use_inorder_rx = 0;
2060 hw->use_inorder_tx = 0;
2063 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2064 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2065 hw->use_simple_rx = 0;
2068 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2069 hw->use_simple_rx = 0;
2072 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2073 DEV_RX_OFFLOAD_TCP_CKSUM |
2074 DEV_RX_OFFLOAD_TCP_LRO |
2075 DEV_RX_OFFLOAD_VLAN_STRIP))
2076 hw->use_simple_rx = 0;
2085 virtio_dev_start(struct rte_eth_dev *dev)
2087 uint16_t nb_queues, i;
2088 struct virtnet_rx *rxvq;
2089 struct virtnet_tx *txvq __rte_unused;
2090 struct virtio_hw *hw = dev->data->dev_private;
2093 /* Finish the initialization of the queues */
2094 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2095 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2099 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2100 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2105 /* check if lsc interrupt feature is enabled */
2106 if (dev->data->dev_conf.intr_conf.lsc) {
2107 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2108 PMD_DRV_LOG(ERR, "link status not supported by host");
2113 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2114 * in device configure, but it could be unmapped when device is
2117 if (dev->data->dev_conf.intr_conf.lsc ||
2118 dev->data->dev_conf.intr_conf.rxq) {
2119 virtio_intr_disable(dev);
2121 /* Setup interrupt callback */
2122 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2123 rte_intr_callback_register(dev->intr_handle,
2124 virtio_interrupt_handler,
2127 if (virtio_intr_enable(dev) < 0) {
2128 PMD_DRV_LOG(ERR, "interrupt enable failed");
2133 /*Notify the backend
2134 *Otherwise the tap backend might already stop its queue due to fullness.
2135 *vhost backend will have no chance to be waked up
2137 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2138 if (hw->max_queue_pairs > 1) {
2139 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2143 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2145 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2146 rxvq = dev->data->rx_queues[i];
2147 /* Flush the old packets */
2148 virtqueue_rxvq_flush(rxvq->vq);
2149 virtqueue_notify(rxvq->vq);
2152 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2153 txvq = dev->data->tx_queues[i];
2154 virtqueue_notify(txvq->vq);
2157 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2159 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2160 rxvq = dev->data->rx_queues[i];
2161 VIRTQUEUE_DUMP(rxvq->vq);
2164 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2165 txvq = dev->data->tx_queues[i];
2166 VIRTQUEUE_DUMP(txvq->vq);
2169 set_rxtx_funcs(dev);
2172 /* Initialize Link state */
2173 virtio_dev_link_update(dev, 0);
2178 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2180 struct virtio_hw *hw = dev->data->dev_private;
2181 uint16_t nr_vq = virtio_get_nr_vq(hw);
2182 const char *type __rte_unused;
2183 unsigned int i, mbuf_num = 0;
2184 struct virtqueue *vq;
2185 struct rte_mbuf *buf;
2188 if (hw->vqs == NULL)
2191 for (i = 0; i < nr_vq; i++) {
2196 queue_type = virtio_get_queue_type(hw, i);
2197 if (queue_type == VTNET_RQ)
2199 else if (queue_type == VTNET_TQ)
2205 "Before freeing %s[%d] used and unused buf",
2209 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2210 rte_pktmbuf_free(buf);
2215 "After freeing %s[%d] used and unused buf",
2220 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2224 * Stop device: disable interrupt and mark link down
2227 virtio_dev_stop(struct rte_eth_dev *dev)
2229 struct virtio_hw *hw = dev->data->dev_private;
2230 struct rte_eth_link link;
2231 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2233 PMD_INIT_LOG(DEBUG, "stop");
2235 rte_spinlock_lock(&hw->state_lock);
2238 hw->started = false;
2240 if (intr_conf->lsc || intr_conf->rxq) {
2241 virtio_intr_disable(dev);
2243 /* Reset interrupt callback */
2244 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2245 rte_intr_callback_unregister(dev->intr_handle,
2246 virtio_interrupt_handler,
2251 memset(&link, 0, sizeof(link));
2252 rte_eth_linkstatus_set(dev, &link);
2254 rte_spinlock_unlock(&hw->state_lock);
2258 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2260 struct rte_eth_link link;
2262 struct virtio_hw *hw = dev->data->dev_private;
2264 memset(&link, 0, sizeof(link));
2265 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2266 link.link_speed = ETH_SPEED_NUM_10G;
2267 link.link_autoneg = ETH_LINK_FIXED;
2270 link.link_status = ETH_LINK_DOWN;
2271 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2272 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2273 vtpci_read_dev_config(hw,
2274 offsetof(struct virtio_net_config, status),
2275 &status, sizeof(status));
2276 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2277 link.link_status = ETH_LINK_DOWN;
2278 PMD_INIT_LOG(DEBUG, "Port %d is down",
2279 dev->data->port_id);
2281 link.link_status = ETH_LINK_UP;
2282 PMD_INIT_LOG(DEBUG, "Port %d is up",
2283 dev->data->port_id);
2286 link.link_status = ETH_LINK_UP;
2289 return rte_eth_linkstatus_set(dev, &link);
2293 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2295 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2296 struct virtio_hw *hw = dev->data->dev_private;
2297 uint64_t offloads = rxmode->offloads;
2299 if (mask & ETH_VLAN_FILTER_MASK) {
2300 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2301 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2304 "vlan filtering not available on this host");
2310 if (mask & ETH_VLAN_STRIP_MASK)
2311 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2317 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2319 uint64_t tso_mask, host_features;
2320 struct virtio_hw *hw = dev->data->dev_private;
2322 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2324 dev_info->max_rx_queues =
2325 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2326 dev_info->max_tx_queues =
2327 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2328 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2329 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2330 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2332 host_features = VTPCI_OPS(hw)->get_features(hw);
2333 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2334 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2335 dev_info->rx_offload_capa |=
2336 DEV_RX_OFFLOAD_TCP_CKSUM |
2337 DEV_RX_OFFLOAD_UDP_CKSUM;
2339 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2340 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2341 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2342 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2343 if ((host_features & tso_mask) == tso_mask)
2344 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2346 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2347 DEV_TX_OFFLOAD_VLAN_INSERT;
2348 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2349 dev_info->tx_offload_capa |=
2350 DEV_TX_OFFLOAD_UDP_CKSUM |
2351 DEV_TX_OFFLOAD_TCP_CKSUM;
2353 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2354 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2355 if ((host_features & tso_mask) == tso_mask)
2356 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2360 * It enables testpmd to collect per queue stats.
2363 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2364 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2365 __rte_unused uint8_t is_rx)
2370 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2371 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2372 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2374 RTE_INIT(virtio_init_log)
2376 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2377 if (virtio_logtype_init >= 0)
2378 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2379 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2380 if (virtio_logtype_driver >= 0)
2381 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);