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_disable(struct rte_eth_dev *dev);
76 static int virtio_dev_queue_stats_mapping_set(
77 struct rte_eth_dev *eth_dev,
82 int virtio_logtype_init;
83 int virtio_logtype_driver;
85 static void virtio_notify_peers(struct rte_eth_dev *dev);
86 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
89 * The set of PCI devices this driver supports
91 static const struct rte_pci_id pci_id_virtio_map[] = {
92 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
93 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
94 { .vendor_id = 0, /* sentinel */ },
97 struct rte_virtio_xstats_name_off {
98 char name[RTE_ETH_XSTATS_NAME_SIZE];
102 /* [rt]x_qX_ is prepended to the name string here */
103 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
104 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
105 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
106 {"errors", offsetof(struct virtnet_rx, stats.errors)},
107 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
108 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
109 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
110 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
111 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
112 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
113 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
114 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
115 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
116 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
119 /* [rt]x_qX_ is prepended to the name string here */
120 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
121 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
122 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
123 {"errors", offsetof(struct virtnet_tx, stats.errors)},
124 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
125 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
126 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
127 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
128 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
129 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
130 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
131 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
132 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
133 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
136 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
137 sizeof(rte_virtio_rxq_stat_strings[0]))
138 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
139 sizeof(rte_virtio_txq_stat_strings[0]))
141 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
143 static struct virtio_pmd_ctrl *
144 virtio_send_command_packed(struct virtnet_ctl *cvq,
145 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 flags = vq->cached_flags;
165 desc[head].addr = cvq->virtio_net_hdr_mem;
166 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;
172 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
175 for (k = 0; k < pkt_num; k++) {
176 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
177 + sizeof(struct virtio_net_ctrl_hdr)
178 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
179 desc[vq->vq_avail_idx].len = dlen[k];
180 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
185 if (++vq->vq_avail_idx >= vq->vq_nentries) {
186 vq->vq_avail_idx -= vq->vq_nentries;
188 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
192 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
193 + sizeof(struct virtio_net_ctrl_hdr);
194 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
195 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE | vq->cached_flags;
198 if (++vq->vq_avail_idx >= vq->vq_nentries) {
199 vq->vq_avail_idx -= vq->vq_nentries;
201 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
204 virtio_wmb(vq->hw->weak_barriers);
205 desc[head].flags = VRING_DESC_F_NEXT | flags;
207 virtio_wmb(vq->hw->weak_barriers);
208 virtqueue_notify(vq);
210 /* wait for used descriptors in virtqueue */
211 while (!desc_is_used(&desc[head], vq))
214 virtio_rmb(vq->hw->weak_barriers);
216 /* now get used descriptors */
217 vq->vq_free_cnt += nb_descs;
218 vq->vq_used_cons_idx += nb_descs;
219 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
220 vq->vq_used_cons_idx -= vq->vq_nentries;
221 vq->used_wrap_counter ^= 1;
224 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
225 "vq->vq_avail_idx=%d\n"
226 "vq->vq_used_cons_idx=%d\n"
227 "vq->cached_flags=0x%x\n"
228 "vq->used_wrap_counter=%d\n",
231 vq->vq_used_cons_idx,
233 vq->used_wrap_counter);
235 result = cvq->virtio_net_hdr_mz->addr;
240 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
241 int *dlen, int pkt_num)
245 virtio_net_ctrl_ack status = ~0;
246 struct virtio_pmd_ctrl *result;
247 struct virtqueue *vq;
249 ctrl->status = status;
251 if (!cvq || !cvq->vq) {
252 PMD_INIT_LOG(ERR, "Control queue is not supported.");
256 rte_spinlock_lock(&cvq->lock);
258 head = vq->vq_desc_head_idx;
260 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
261 "vq->hw->cvq = %p vq = %p",
262 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
264 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
265 rte_spinlock_unlock(&cvq->lock);
269 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
270 sizeof(struct virtio_pmd_ctrl));
272 if (vtpci_packed_queue(vq->hw)) {
273 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
278 * Format is enforced in qemu code:
279 * One TX packet for header;
280 * At least one TX packet per argument;
281 * One RX packet for ACK.
283 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
284 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
285 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
287 i = vq->vq_ring.desc[head].next;
289 for (k = 0; k < pkt_num; k++) {
290 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
291 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
292 + sizeof(struct virtio_net_ctrl_hdr)
293 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
294 vq->vq_ring.desc[i].len = dlen[k];
297 i = vq->vq_ring.desc[i].next;
300 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
301 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
302 + sizeof(struct virtio_net_ctrl_hdr);
303 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
306 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
308 vq_update_avail_ring(vq, head);
309 vq_update_avail_idx(vq);
311 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
313 virtqueue_notify(vq);
316 while (VIRTQUEUE_NUSED(vq) == 0) {
321 while (VIRTQUEUE_NUSED(vq)) {
322 uint32_t idx, desc_idx, used_idx;
323 struct vring_used_elem *uep;
325 used_idx = (uint32_t)(vq->vq_used_cons_idx
326 & (vq->vq_nentries - 1));
327 uep = &vq->vq_ring.used->ring[used_idx];
328 idx = (uint32_t) uep->id;
331 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
332 desc_idx = vq->vq_ring.desc[desc_idx].next;
336 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
337 vq->vq_desc_head_idx = idx;
339 vq->vq_used_cons_idx++;
343 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
344 vq->vq_free_cnt, vq->vq_desc_head_idx);
346 result = cvq->virtio_net_hdr_mz->addr;
349 rte_spinlock_unlock(&cvq->lock);
350 return result->status;
354 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
356 struct virtio_hw *hw = dev->data->dev_private;
357 struct virtio_pmd_ctrl ctrl;
361 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
362 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
363 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
365 dlen[0] = sizeof(uint16_t);
367 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
369 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
370 "failed, this is too late now...");
378 virtio_dev_queue_release(void *queue __rte_unused)
384 virtio_get_nr_vq(struct virtio_hw *hw)
386 uint16_t nr_vq = hw->max_queue_pairs * 2;
388 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
395 virtio_init_vring(struct virtqueue *vq)
397 int size = vq->vq_nentries;
398 struct vring *vr = &vq->vq_ring;
399 uint8_t *ring_mem = vq->vq_ring_virt_mem;
401 PMD_INIT_FUNC_TRACE();
403 memset(ring_mem, 0, vq->vq_ring_size);
405 vq->vq_used_cons_idx = 0;
406 vq->vq_desc_head_idx = 0;
407 vq->vq_avail_idx = 0;
408 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
409 vq->vq_free_cnt = vq->vq_nentries;
410 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
411 if (vtpci_packed_queue(vq->hw)) {
412 vring_init_packed(&vq->ring_packed, ring_mem,
413 VIRTIO_PCI_VRING_ALIGN, size);
414 vring_desc_init_packed(vq, size);
416 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
417 vring_desc_init_split(vr->desc, size);
420 * Disable device(host) interrupting guest
422 virtqueue_disable_intr(vq);
426 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
428 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
429 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
430 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
431 unsigned int vq_size, size;
432 struct virtio_hw *hw = dev->data->dev_private;
433 struct virtnet_rx *rxvq = NULL;
434 struct virtnet_tx *txvq = NULL;
435 struct virtnet_ctl *cvq = NULL;
436 struct virtqueue *vq;
437 size_t sz_hdr_mz = 0;
438 void *sw_ring = NULL;
439 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
441 int numa_node = dev->device->numa_node;
443 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
444 vtpci_queue_idx, numa_node);
447 * Read the virtqueue size from the Queue Size field
448 * Always power of 2 and if 0 virtqueue does not exist
450 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
451 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
453 PMD_INIT_LOG(ERR, "virtqueue does not exist");
457 if (!rte_is_power_of_2(vq_size)) {
458 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
462 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
463 dev->data->port_id, vtpci_queue_idx);
465 size = RTE_ALIGN_CEIL(sizeof(*vq) +
466 vq_size * sizeof(struct vq_desc_extra),
467 RTE_CACHE_LINE_SIZE);
468 if (queue_type == VTNET_TQ) {
470 * For each xmit packet, allocate a virtio_net_hdr
471 * and indirect ring elements
473 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
474 } else if (queue_type == VTNET_CQ) {
475 /* Allocate a page for control vq command, data and status */
476 sz_hdr_mz = PAGE_SIZE;
479 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
482 PMD_INIT_LOG(ERR, "can not allocate vq");
485 hw->vqs[vtpci_queue_idx] = vq;
488 vq->vq_queue_index = vtpci_queue_idx;
489 vq->vq_nentries = vq_size;
490 vq->event_flags_shadow = 0;
491 if (vtpci_packed_queue(hw)) {
492 vq->used_wrap_counter = 1;
493 vq->cached_flags = VRING_DESC_F_AVAIL(1);
494 if (queue_type == VTNET_RQ)
495 vq->cached_flags |= VRING_DESC_F_WRITE;
499 * Reserve a memzone for vring elements
501 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
502 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
503 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
504 size, vq->vq_ring_size);
506 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
507 numa_node, RTE_MEMZONE_IOVA_CONTIG,
508 VIRTIO_PCI_VRING_ALIGN);
510 if (rte_errno == EEXIST)
511 mz = rte_memzone_lookup(vq_name);
518 memset(mz->addr, 0, mz->len);
520 vq->vq_ring_mem = mz->iova;
521 vq->vq_ring_virt_mem = mz->addr;
522 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
524 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
525 (uint64_t)(uintptr_t)mz->addr);
527 virtio_init_vring(vq);
530 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
531 dev->data->port_id, vtpci_queue_idx);
532 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
533 numa_node, RTE_MEMZONE_IOVA_CONTIG,
534 RTE_CACHE_LINE_SIZE);
535 if (hdr_mz == NULL) {
536 if (rte_errno == EEXIST)
537 hdr_mz = rte_memzone_lookup(vq_hdr_name);
538 if (hdr_mz == NULL) {
545 if (queue_type == VTNET_RQ) {
546 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
547 sizeof(vq->sw_ring[0]);
549 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
550 RTE_CACHE_LINE_SIZE, numa_node);
552 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
557 vq->sw_ring = sw_ring;
560 rxvq->port_id = dev->data->port_id;
562 } else if (queue_type == VTNET_TQ) {
565 txvq->port_id = dev->data->port_id;
567 txvq->virtio_net_hdr_mz = hdr_mz;
568 txvq->virtio_net_hdr_mem = hdr_mz->iova;
569 } else if (queue_type == VTNET_CQ) {
573 cvq->virtio_net_hdr_mz = hdr_mz;
574 cvq->virtio_net_hdr_mem = hdr_mz->iova;
575 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
580 /* For virtio_user case (that is when hw->dev is NULL), we use
581 * virtual address. And we need properly set _offset_, please see
582 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
584 if (!hw->virtio_user_dev)
585 vq->offset = offsetof(struct rte_mbuf, buf_iova);
587 vq->vq_ring_mem = (uintptr_t)mz->addr;
588 vq->offset = offsetof(struct rte_mbuf, buf_addr);
589 if (queue_type == VTNET_TQ)
590 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
591 else if (queue_type == VTNET_CQ)
592 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
595 if (queue_type == VTNET_TQ) {
596 struct virtio_tx_region *txr;
600 memset(txr, 0, vq_size * sizeof(*txr));
601 for (i = 0; i < vq_size; i++) {
602 struct vring_desc *start_dp = txr[i].tx_indir;
603 struct vring_packed_desc *start_dp_packed =
606 /* first indirect descriptor is always the tx header */
607 if (vtpci_packed_queue(hw)) {
608 start_dp_packed->addr = txvq->virtio_net_hdr_mem
610 + offsetof(struct virtio_tx_region,
612 start_dp_packed->len = hw->vtnet_hdr_size;
614 vring_desc_init_split(start_dp,
615 RTE_DIM(txr[i].tx_indir));
616 start_dp->addr = txvq->virtio_net_hdr_mem
618 + offsetof(struct virtio_tx_region,
620 start_dp->len = hw->vtnet_hdr_size;
621 start_dp->flags = VRING_DESC_F_NEXT;
626 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
627 PMD_INIT_LOG(ERR, "setup_queue failed");
635 rte_memzone_free(hdr_mz);
636 rte_memzone_free(mz);
643 virtio_free_queues(struct virtio_hw *hw)
645 uint16_t nr_vq = virtio_get_nr_vq(hw);
646 struct virtqueue *vq;
653 for (i = 0; i < nr_vq; i++) {
658 queue_type = virtio_get_queue_type(hw, i);
659 if (queue_type == VTNET_RQ) {
660 rte_free(vq->sw_ring);
661 rte_memzone_free(vq->rxq.mz);
662 } else if (queue_type == VTNET_TQ) {
663 rte_memzone_free(vq->txq.mz);
664 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
666 rte_memzone_free(vq->cq.mz);
667 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
679 virtio_alloc_queues(struct rte_eth_dev *dev)
681 struct virtio_hw *hw = dev->data->dev_private;
682 uint16_t nr_vq = virtio_get_nr_vq(hw);
686 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
688 PMD_INIT_LOG(ERR, "failed to allocate vqs");
692 for (i = 0; i < nr_vq; i++) {
693 ret = virtio_init_queue(dev, i);
695 virtio_free_queues(hw);
703 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
706 virtio_dev_close(struct rte_eth_dev *dev)
708 struct virtio_hw *hw = dev->data->dev_private;
709 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
711 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
718 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
719 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
721 virtio_queues_unbind_intr(dev);
723 if (intr_conf->lsc || intr_conf->rxq) {
724 virtio_intr_disable(dev);
725 rte_intr_efd_disable(dev->intr_handle);
726 rte_free(dev->intr_handle->intr_vec);
727 dev->intr_handle->intr_vec = NULL;
731 virtio_dev_free_mbufs(dev);
732 virtio_free_queues(hw);
736 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
738 struct virtio_hw *hw = dev->data->dev_private;
739 struct virtio_pmd_ctrl ctrl;
743 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
744 PMD_INIT_LOG(INFO, "host does not support rx control");
748 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
749 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
753 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
755 PMD_INIT_LOG(ERR, "Failed to enable promisc");
759 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
761 struct virtio_hw *hw = dev->data->dev_private;
762 struct virtio_pmd_ctrl ctrl;
766 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
767 PMD_INIT_LOG(INFO, "host does not support rx control");
771 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
772 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
776 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
778 PMD_INIT_LOG(ERR, "Failed to disable promisc");
782 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
784 struct virtio_hw *hw = dev->data->dev_private;
785 struct virtio_pmd_ctrl ctrl;
789 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
790 PMD_INIT_LOG(INFO, "host does not support rx control");
794 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
795 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
799 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
801 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
805 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
807 struct virtio_hw *hw = dev->data->dev_private;
808 struct virtio_pmd_ctrl ctrl;
812 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
813 PMD_INIT_LOG(INFO, "host does not support rx control");
817 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
818 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
822 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
824 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
827 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
829 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
831 struct virtio_hw *hw = dev->data->dev_private;
832 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
834 uint32_t frame_size = mtu + ether_hdr_len;
835 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
837 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
839 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
840 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
841 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
848 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
850 struct virtio_hw *hw = dev->data->dev_private;
851 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
852 struct virtqueue *vq = rxvq->vq;
854 virtqueue_enable_intr(vq);
855 virtio_mb(hw->weak_barriers);
860 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
862 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
863 struct virtqueue *vq = rxvq->vq;
865 virtqueue_disable_intr(vq);
870 * dev_ops for virtio, bare necessities for basic operation
872 static const struct eth_dev_ops virtio_eth_dev_ops = {
873 .dev_configure = virtio_dev_configure,
874 .dev_start = virtio_dev_start,
875 .dev_stop = virtio_dev_stop,
876 .dev_close = virtio_dev_close,
877 .promiscuous_enable = virtio_dev_promiscuous_enable,
878 .promiscuous_disable = virtio_dev_promiscuous_disable,
879 .allmulticast_enable = virtio_dev_allmulticast_enable,
880 .allmulticast_disable = virtio_dev_allmulticast_disable,
881 .mtu_set = virtio_mtu_set,
882 .dev_infos_get = virtio_dev_info_get,
883 .stats_get = virtio_dev_stats_get,
884 .xstats_get = virtio_dev_xstats_get,
885 .xstats_get_names = virtio_dev_xstats_get_names,
886 .stats_reset = virtio_dev_stats_reset,
887 .xstats_reset = virtio_dev_stats_reset,
888 .link_update = virtio_dev_link_update,
889 .vlan_offload_set = virtio_dev_vlan_offload_set,
890 .rx_queue_setup = virtio_dev_rx_queue_setup,
891 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
892 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
893 .rx_queue_release = virtio_dev_queue_release,
894 .rx_descriptor_done = virtio_dev_rx_queue_done,
895 .tx_queue_setup = virtio_dev_tx_queue_setup,
896 .tx_queue_release = virtio_dev_queue_release,
897 /* collect stats per queue */
898 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
899 .vlan_filter_set = virtio_vlan_filter_set,
900 .mac_addr_add = virtio_mac_addr_add,
901 .mac_addr_remove = virtio_mac_addr_remove,
902 .mac_addr_set = virtio_mac_addr_set,
906 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
910 for (i = 0; i < dev->data->nb_tx_queues; i++) {
911 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
915 stats->opackets += txvq->stats.packets;
916 stats->obytes += txvq->stats.bytes;
917 stats->oerrors += txvq->stats.errors;
919 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
920 stats->q_opackets[i] = txvq->stats.packets;
921 stats->q_obytes[i] = txvq->stats.bytes;
925 for (i = 0; i < dev->data->nb_rx_queues; i++) {
926 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
930 stats->ipackets += rxvq->stats.packets;
931 stats->ibytes += rxvq->stats.bytes;
932 stats->ierrors += rxvq->stats.errors;
934 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
935 stats->q_ipackets[i] = rxvq->stats.packets;
936 stats->q_ibytes[i] = rxvq->stats.bytes;
940 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
943 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
944 struct rte_eth_xstat_name *xstats_names,
945 __rte_unused unsigned limit)
951 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
952 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
954 if (xstats_names != NULL) {
955 /* Note: limit checked in rte_eth_xstats_names() */
957 for (i = 0; i < dev->data->nb_rx_queues; i++) {
958 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
961 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
962 snprintf(xstats_names[count].name,
963 sizeof(xstats_names[count].name),
965 rte_virtio_rxq_stat_strings[t].name);
970 for (i = 0; i < dev->data->nb_tx_queues; i++) {
971 struct virtnet_tx *txvq = dev->data->tx_queues[i];
974 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
975 snprintf(xstats_names[count].name,
976 sizeof(xstats_names[count].name),
978 rte_virtio_txq_stat_strings[t].name);
988 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
994 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
995 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1000 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1001 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1008 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1009 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1010 rte_virtio_rxq_stat_strings[t].offset);
1011 xstats[count].id = count;
1016 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1017 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1024 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1025 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1026 rte_virtio_txq_stat_strings[t].offset);
1027 xstats[count].id = count;
1036 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1038 virtio_update_stats(dev, stats);
1044 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1048 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1049 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1053 txvq->stats.packets = 0;
1054 txvq->stats.bytes = 0;
1055 txvq->stats.errors = 0;
1056 txvq->stats.multicast = 0;
1057 txvq->stats.broadcast = 0;
1058 memset(txvq->stats.size_bins, 0,
1059 sizeof(txvq->stats.size_bins[0]) * 8);
1062 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1063 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1067 rxvq->stats.packets = 0;
1068 rxvq->stats.bytes = 0;
1069 rxvq->stats.errors = 0;
1070 rxvq->stats.multicast = 0;
1071 rxvq->stats.broadcast = 0;
1072 memset(rxvq->stats.size_bins, 0,
1073 sizeof(rxvq->stats.size_bins[0]) * 8);
1078 virtio_set_hwaddr(struct virtio_hw *hw)
1080 vtpci_write_dev_config(hw,
1081 offsetof(struct virtio_net_config, mac),
1082 &hw->mac_addr, ETHER_ADDR_LEN);
1086 virtio_get_hwaddr(struct virtio_hw *hw)
1088 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1089 vtpci_read_dev_config(hw,
1090 offsetof(struct virtio_net_config, mac),
1091 &hw->mac_addr, ETHER_ADDR_LEN);
1093 eth_random_addr(&hw->mac_addr[0]);
1094 virtio_set_hwaddr(hw);
1099 virtio_mac_table_set(struct virtio_hw *hw,
1100 const struct virtio_net_ctrl_mac *uc,
1101 const struct virtio_net_ctrl_mac *mc)
1103 struct virtio_pmd_ctrl ctrl;
1106 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1107 PMD_DRV_LOG(INFO, "host does not support mac table");
1111 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1112 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1114 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1115 memcpy(ctrl.data, uc, len[0]);
1117 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1118 memcpy(ctrl.data + len[0], mc, len[1]);
1120 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1122 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1127 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1128 uint32_t index, uint32_t vmdq __rte_unused)
1130 struct virtio_hw *hw = dev->data->dev_private;
1131 const struct ether_addr *addrs = dev->data->mac_addrs;
1133 struct virtio_net_ctrl_mac *uc, *mc;
1135 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1136 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1140 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1142 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1145 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1146 const struct ether_addr *addr
1147 = (i == index) ? mac_addr : addrs + i;
1148 struct virtio_net_ctrl_mac *tbl
1149 = is_multicast_ether_addr(addr) ? mc : uc;
1151 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1154 return virtio_mac_table_set(hw, uc, mc);
1158 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1160 struct virtio_hw *hw = dev->data->dev_private;
1161 struct ether_addr *addrs = dev->data->mac_addrs;
1162 struct virtio_net_ctrl_mac *uc, *mc;
1165 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1166 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1170 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1172 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1175 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1176 struct virtio_net_ctrl_mac *tbl;
1178 if (i == index || is_zero_ether_addr(addrs + i))
1181 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1182 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1185 virtio_mac_table_set(hw, uc, mc);
1189 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1191 struct virtio_hw *hw = dev->data->dev_private;
1193 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1195 /* Use atomic update if available */
1196 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1197 struct virtio_pmd_ctrl ctrl;
1198 int len = ETHER_ADDR_LEN;
1200 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1201 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1203 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1204 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1207 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1210 virtio_set_hwaddr(hw);
1215 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1217 struct virtio_hw *hw = dev->data->dev_private;
1218 struct virtio_pmd_ctrl ctrl;
1221 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1224 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1225 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1226 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1227 len = sizeof(vlan_id);
1229 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1233 virtio_intr_enable(struct rte_eth_dev *dev)
1235 struct virtio_hw *hw = dev->data->dev_private;
1237 if (rte_intr_enable(dev->intr_handle) < 0)
1240 if (!hw->virtio_user_dev)
1241 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1247 virtio_intr_disable(struct rte_eth_dev *dev)
1249 struct virtio_hw *hw = dev->data->dev_private;
1251 if (rte_intr_disable(dev->intr_handle) < 0)
1254 if (!hw->virtio_user_dev)
1255 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1261 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1263 uint64_t host_features;
1265 /* Prepare guest_features: feature that driver wants to support */
1266 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1269 /* Read device(host) feature bits */
1270 host_features = VTPCI_OPS(hw)->get_features(hw);
1271 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1274 /* If supported, ensure MTU value is valid before acknowledging it. */
1275 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1276 struct virtio_net_config config;
1278 vtpci_read_dev_config(hw,
1279 offsetof(struct virtio_net_config, mtu),
1280 &config.mtu, sizeof(config.mtu));
1282 if (config.mtu < ETHER_MIN_MTU)
1283 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1287 * Negotiate features: Subset of device feature bits are written back
1288 * guest feature bits.
1290 hw->guest_features = req_features;
1291 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1292 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1293 hw->guest_features);
1296 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1298 "VIRTIO_F_VERSION_1 features is not enabled.");
1301 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1302 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1304 "failed to set FEATURES_OK status!");
1309 hw->req_guest_features = req_features;
1315 virtio_dev_pause(struct rte_eth_dev *dev)
1317 struct virtio_hw *hw = dev->data->dev_private;
1319 rte_spinlock_lock(&hw->state_lock);
1321 if (hw->started == 0) {
1322 /* Device is just stopped. */
1323 rte_spinlock_unlock(&hw->state_lock);
1328 * Prevent the worker threads from touching queues to avoid contention,
1329 * 1 ms should be enough for the ongoing Tx function to finish.
1336 * Recover hw state to let the worker threads continue.
1339 virtio_dev_resume(struct rte_eth_dev *dev)
1341 struct virtio_hw *hw = dev->data->dev_private;
1344 rte_spinlock_unlock(&hw->state_lock);
1348 * Should be called only after device is paused.
1351 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1354 struct virtio_hw *hw = dev->data->dev_private;
1355 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1358 hw->inject_pkts = tx_pkts;
1359 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1360 hw->inject_pkts = NULL;
1366 virtio_notify_peers(struct rte_eth_dev *dev)
1368 struct virtio_hw *hw = dev->data->dev_private;
1369 struct virtnet_rx *rxvq;
1370 struct rte_mbuf *rarp_mbuf;
1372 if (!dev->data->rx_queues)
1375 rxvq = dev->data->rx_queues[0];
1379 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1380 (struct ether_addr *)hw->mac_addr);
1381 if (rarp_mbuf == NULL) {
1382 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1386 /* If virtio port just stopped, no need to send RARP */
1387 if (virtio_dev_pause(dev) < 0) {
1388 rte_pktmbuf_free(rarp_mbuf);
1392 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1393 virtio_dev_resume(dev);
1397 virtio_ack_link_announce(struct rte_eth_dev *dev)
1399 struct virtio_hw *hw = dev->data->dev_private;
1400 struct virtio_pmd_ctrl ctrl;
1402 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1403 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1405 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1409 * Process virtio config changed interrupt. Call the callback
1410 * if link state changed, generate gratuitous RARP packet if
1411 * the status indicates an ANNOUNCE.
1414 virtio_interrupt_handler(void *param)
1416 struct rte_eth_dev *dev = param;
1417 struct virtio_hw *hw = dev->data->dev_private;
1421 /* Read interrupt status which clears interrupt */
1422 isr = vtpci_isr(hw);
1423 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1425 if (virtio_intr_enable(dev) < 0)
1426 PMD_DRV_LOG(ERR, "interrupt enable failed");
1428 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1429 if (virtio_dev_link_update(dev, 0) == 0)
1430 _rte_eth_dev_callback_process(dev,
1431 RTE_ETH_EVENT_INTR_LSC,
1434 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1435 vtpci_read_dev_config(hw,
1436 offsetof(struct virtio_net_config, status),
1437 &status, sizeof(status));
1438 if (status & VIRTIO_NET_S_ANNOUNCE) {
1439 virtio_notify_peers(dev);
1441 virtio_ack_link_announce(dev);
1447 /* set rx and tx handlers according to what is supported */
1449 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1451 struct virtio_hw *hw = eth_dev->data->dev_private;
1453 if (vtpci_packed_queue(hw)) {
1455 "virtio: using packed ring %s Tx path on port %u",
1456 hw->use_inorder_tx ? "inorder" : "standard",
1457 eth_dev->data->port_id);
1458 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1460 if (hw->use_inorder_tx) {
1461 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1462 eth_dev->data->port_id);
1463 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1465 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1466 eth_dev->data->port_id);
1467 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1471 if (vtpci_packed_queue(hw)) {
1472 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1474 "virtio: using packed ring mergeable buffer Rx path on port %u",
1475 eth_dev->data->port_id);
1476 eth_dev->rx_pkt_burst =
1477 &virtio_recv_mergeable_pkts_packed;
1480 "virtio: using packed ring standard Rx path on port %u",
1481 eth_dev->data->port_id);
1482 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1485 if (hw->use_simple_rx) {
1486 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1487 eth_dev->data->port_id);
1488 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1489 } else if (hw->use_inorder_rx) {
1491 "virtio: using inorder Rx path on port %u",
1492 eth_dev->data->port_id);
1493 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1494 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1496 "virtio: using mergeable buffer Rx path on port %u",
1497 eth_dev->data->port_id);
1498 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1500 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1501 eth_dev->data->port_id);
1502 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1508 /* Only support 1:1 queue/interrupt mapping so far.
1509 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1510 * interrupt vectors (<N+1).
1513 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1516 struct virtio_hw *hw = dev->data->dev_private;
1518 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1519 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1520 dev->intr_handle->intr_vec[i] = i + 1;
1521 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1522 VIRTIO_MSI_NO_VECTOR) {
1523 PMD_DRV_LOG(ERR, "failed to set queue vector");
1532 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1535 struct virtio_hw *hw = dev->data->dev_private;
1537 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1538 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1539 VTPCI_OPS(hw)->set_queue_irq(hw,
1540 hw->vqs[i * VTNET_CQ],
1541 VIRTIO_MSI_NO_VECTOR);
1545 virtio_configure_intr(struct rte_eth_dev *dev)
1547 struct virtio_hw *hw = dev->data->dev_private;
1549 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1550 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1554 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1555 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1559 if (!dev->intr_handle->intr_vec) {
1560 dev->intr_handle->intr_vec =
1561 rte_zmalloc("intr_vec",
1562 hw->max_queue_pairs * sizeof(int), 0);
1563 if (!dev->intr_handle->intr_vec) {
1564 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1565 hw->max_queue_pairs);
1570 /* Re-register callback to update max_intr */
1571 rte_intr_callback_unregister(dev->intr_handle,
1572 virtio_interrupt_handler,
1574 rte_intr_callback_register(dev->intr_handle,
1575 virtio_interrupt_handler,
1578 /* DO NOT try to remove this! This function will enable msix, or QEMU
1579 * will encounter SIGSEGV when DRIVER_OK is sent.
1580 * And for legacy devices, this should be done before queue/vec binding
1581 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1582 * (22) will be ignored.
1584 if (virtio_intr_enable(dev) < 0) {
1585 PMD_DRV_LOG(ERR, "interrupt enable failed");
1589 if (virtio_queues_bind_intr(dev) < 0) {
1590 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1597 /* reset device and renegotiate features if needed */
1599 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1601 struct virtio_hw *hw = eth_dev->data->dev_private;
1602 struct virtio_net_config *config;
1603 struct virtio_net_config local_config;
1604 struct rte_pci_device *pci_dev = NULL;
1607 /* Reset the device although not necessary at startup */
1611 virtio_dev_free_mbufs(eth_dev);
1612 virtio_free_queues(hw);
1615 /* Tell the host we've noticed this device. */
1616 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1618 /* Tell the host we've known how to drive the device. */
1619 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1620 if (virtio_negotiate_features(hw, req_features) < 0)
1623 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1625 if (!hw->virtio_user_dev) {
1626 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1627 rte_eth_copy_pci_info(eth_dev, pci_dev);
1630 /* If host does not support both status and MSI-X then disable LSC */
1631 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1632 hw->use_msix != VIRTIO_MSIX_NONE)
1633 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1635 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1637 /* Setting up rx_header size for the device */
1638 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1639 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1640 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1641 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1643 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1645 /* Copy the permanent MAC address to: virtio_hw */
1646 virtio_get_hwaddr(hw);
1647 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1648 ð_dev->data->mac_addrs[0]);
1650 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1651 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1652 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1654 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1655 config = &local_config;
1657 vtpci_read_dev_config(hw,
1658 offsetof(struct virtio_net_config, mac),
1659 &config->mac, sizeof(config->mac));
1661 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1662 vtpci_read_dev_config(hw,
1663 offsetof(struct virtio_net_config, status),
1664 &config->status, sizeof(config->status));
1667 "VIRTIO_NET_F_STATUS is not supported");
1671 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1672 vtpci_read_dev_config(hw,
1673 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1674 &config->max_virtqueue_pairs,
1675 sizeof(config->max_virtqueue_pairs));
1678 "VIRTIO_NET_F_MQ is not supported");
1679 config->max_virtqueue_pairs = 1;
1682 hw->max_queue_pairs = config->max_virtqueue_pairs;
1684 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1685 vtpci_read_dev_config(hw,
1686 offsetof(struct virtio_net_config, mtu),
1688 sizeof(config->mtu));
1691 * MTU value has already been checked at negotiation
1692 * time, but check again in case it has changed since
1693 * then, which should not happen.
1695 if (config->mtu < ETHER_MIN_MTU) {
1696 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1701 hw->max_mtu = config->mtu;
1702 /* Set initial MTU to maximum one supported by vhost */
1703 eth_dev->data->mtu = config->mtu;
1706 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1707 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1710 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1711 config->max_virtqueue_pairs);
1712 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1714 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1715 config->mac[0], config->mac[1],
1716 config->mac[2], config->mac[3],
1717 config->mac[4], config->mac[5]);
1719 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1720 hw->max_queue_pairs = 1;
1721 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1722 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1725 ret = virtio_alloc_queues(eth_dev);
1729 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1730 if (virtio_configure_intr(eth_dev) < 0) {
1731 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1736 vtpci_reinit_complete(hw);
1739 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1740 eth_dev->data->port_id, pci_dev->id.vendor_id,
1741 pci_dev->id.device_id);
1747 * Remap the PCI device again (IO port map for legacy device and
1748 * memory map for modern device), so that the secondary process
1749 * could have the PCI initiated correctly.
1752 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1756 * We don't have to re-parse the PCI config space, since
1757 * rte_pci_map_device() makes sure the mapped address
1758 * in secondary process would equal to the one mapped in
1759 * the primary process: error will be returned if that
1760 * requirement is not met.
1762 * That said, we could simply reuse all cap pointers
1763 * (such as dev_cfg, common_cfg, etc.) parsed from the
1764 * primary process, which is stored in shared memory.
1766 if (rte_pci_map_device(pci_dev)) {
1767 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1771 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1779 virtio_set_vtpci_ops(struct virtio_hw *hw)
1781 #ifdef RTE_VIRTIO_USER
1782 if (hw->virtio_user_dev)
1783 VTPCI_OPS(hw) = &virtio_user_ops;
1787 VTPCI_OPS(hw) = &modern_ops;
1789 VTPCI_OPS(hw) = &legacy_ops;
1793 * This function is based on probe() function in virtio_pci.c
1794 * It returns 0 on success.
1797 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1799 struct virtio_hw *hw = eth_dev->data->dev_private;
1802 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1804 eth_dev->dev_ops = &virtio_eth_dev_ops;
1806 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1807 if (!hw->virtio_user_dev) {
1808 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1813 virtio_set_vtpci_ops(hw);
1814 set_rxtx_funcs(eth_dev);
1819 /* Allocate memory for storing MAC addresses */
1820 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1821 if (eth_dev->data->mac_addrs == NULL) {
1823 "Failed to allocate %d bytes needed to store MAC addresses",
1824 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1828 hw->port_id = eth_dev->data->port_id;
1829 /* For virtio_user case the hw->virtio_user_dev is populated by
1830 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1832 if (!hw->virtio_user_dev) {
1833 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1838 /* reset device and negotiate default features */
1839 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1846 rte_free(eth_dev->data->mac_addrs);
1851 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1853 PMD_INIT_FUNC_TRACE();
1855 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1858 virtio_dev_stop(eth_dev);
1859 virtio_dev_close(eth_dev);
1861 eth_dev->dev_ops = NULL;
1862 eth_dev->tx_pkt_burst = NULL;
1863 eth_dev->rx_pkt_burst = NULL;
1865 if (eth_dev->device)
1866 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1868 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1873 static int vdpa_check_handler(__rte_unused const char *key,
1874 const char *value, __rte_unused void *opaque)
1876 if (strcmp(value, "1"))
1883 vdpa_mode_selected(struct rte_devargs *devargs)
1885 struct rte_kvargs *kvlist;
1886 const char *key = "vdpa";
1889 if (devargs == NULL)
1892 kvlist = rte_kvargs_parse(devargs->args, NULL);
1896 if (!rte_kvargs_count(kvlist, key))
1899 /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1900 if (rte_kvargs_process(kvlist, key,
1901 vdpa_check_handler, NULL) < 0) {
1907 rte_kvargs_free(kvlist);
1911 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1912 struct rte_pci_device *pci_dev)
1914 if (rte_eal_iopl_init() != 0) {
1915 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1919 /* virtio pmd skips probe if device needs to work in vdpa mode */
1920 if (vdpa_mode_selected(pci_dev->device.devargs))
1923 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1924 eth_virtio_dev_init);
1927 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1929 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1932 static struct rte_pci_driver rte_virtio_pmd = {
1934 .name = "net_virtio",
1936 .id_table = pci_id_virtio_map,
1938 .probe = eth_virtio_pci_probe,
1939 .remove = eth_virtio_pci_remove,
1942 RTE_INIT(rte_virtio_pmd_init)
1944 rte_eal_iopl_init();
1945 rte_pci_register(&rte_virtio_pmd);
1949 rx_offload_enabled(struct virtio_hw *hw)
1951 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1952 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1953 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1957 tx_offload_enabled(struct virtio_hw *hw)
1959 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1960 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1961 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1965 * Configure virtio device
1966 * It returns 0 on success.
1969 virtio_dev_configure(struct rte_eth_dev *dev)
1971 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1972 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1973 struct virtio_hw *hw = dev->data->dev_private;
1974 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
1976 uint64_t rx_offloads = rxmode->offloads;
1977 uint64_t tx_offloads = txmode->offloads;
1978 uint64_t req_features;
1981 PMD_INIT_LOG(DEBUG, "configure");
1982 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1984 if (dev->data->dev_conf.intr_conf.rxq) {
1985 ret = virtio_init_device(dev, hw->req_guest_features);
1990 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
1991 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1993 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1994 DEV_RX_OFFLOAD_TCP_CKSUM))
1995 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1997 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
1999 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2000 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2002 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2003 DEV_TX_OFFLOAD_TCP_CKSUM))
2004 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2006 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2008 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2009 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2011 /* if request features changed, reinit the device */
2012 if (req_features != hw->req_guest_features) {
2013 ret = virtio_init_device(dev, req_features);
2018 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2019 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2020 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2022 "rx checksum not available on this host");
2026 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2027 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2028 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2030 "Large Receive Offload not available on this host");
2034 /* start control queue */
2035 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2036 virtio_dev_cq_start(dev);
2038 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2041 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2042 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2044 "vlan filtering not available on this host");
2048 hw->has_tx_offload = tx_offload_enabled(hw);
2049 hw->has_rx_offload = rx_offload_enabled(hw);
2051 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2052 /* Enable vector (0) for Link State Intrerrupt */
2053 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2054 VIRTIO_MSI_NO_VECTOR) {
2055 PMD_DRV_LOG(ERR, "failed to set config vector");
2059 rte_spinlock_init(&hw->state_lock);
2061 hw->use_simple_rx = 1;
2063 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2064 hw->use_inorder_tx = 1;
2065 hw->use_inorder_rx = 1;
2066 hw->use_simple_rx = 0;
2069 if (vtpci_packed_queue(hw)) {
2070 hw->use_simple_rx = 0;
2071 hw->use_inorder_rx = 0;
2074 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2075 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2076 hw->use_simple_rx = 0;
2079 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2080 hw->use_simple_rx = 0;
2083 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2084 DEV_RX_OFFLOAD_TCP_CKSUM |
2085 DEV_RX_OFFLOAD_TCP_LRO |
2086 DEV_RX_OFFLOAD_VLAN_STRIP))
2087 hw->use_simple_rx = 0;
2096 virtio_dev_start(struct rte_eth_dev *dev)
2098 uint16_t nb_queues, i;
2099 struct virtnet_rx *rxvq;
2100 struct virtnet_tx *txvq __rte_unused;
2101 struct virtio_hw *hw = dev->data->dev_private;
2104 /* Finish the initialization of the queues */
2105 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2106 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2110 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2111 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2116 /* check if lsc interrupt feature is enabled */
2117 if (dev->data->dev_conf.intr_conf.lsc) {
2118 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2119 PMD_DRV_LOG(ERR, "link status not supported by host");
2124 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2125 * in device configure, but it could be unmapped when device is
2128 if (dev->data->dev_conf.intr_conf.lsc ||
2129 dev->data->dev_conf.intr_conf.rxq) {
2130 virtio_intr_disable(dev);
2132 /* Setup interrupt callback */
2133 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2134 rte_intr_callback_register(dev->intr_handle,
2135 virtio_interrupt_handler,
2138 if (virtio_intr_enable(dev) < 0) {
2139 PMD_DRV_LOG(ERR, "interrupt enable failed");
2144 /*Notify the backend
2145 *Otherwise the tap backend might already stop its queue due to fullness.
2146 *vhost backend will have no chance to be waked up
2148 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2149 if (hw->max_queue_pairs > 1) {
2150 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2154 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2156 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2157 rxvq = dev->data->rx_queues[i];
2158 /* Flush the old packets */
2159 virtqueue_rxvq_flush(rxvq->vq);
2160 virtqueue_notify(rxvq->vq);
2163 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2164 txvq = dev->data->tx_queues[i];
2165 virtqueue_notify(txvq->vq);
2168 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2170 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2171 rxvq = dev->data->rx_queues[i];
2172 VIRTQUEUE_DUMP(rxvq->vq);
2175 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2176 txvq = dev->data->tx_queues[i];
2177 VIRTQUEUE_DUMP(txvq->vq);
2180 set_rxtx_funcs(dev);
2183 /* Initialize Link state */
2184 virtio_dev_link_update(dev, 0);
2189 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2191 struct virtio_hw *hw = dev->data->dev_private;
2192 uint16_t nr_vq = virtio_get_nr_vq(hw);
2193 const char *type __rte_unused;
2194 unsigned int i, mbuf_num = 0;
2195 struct virtqueue *vq;
2196 struct rte_mbuf *buf;
2199 if (hw->vqs == NULL)
2202 for (i = 0; i < nr_vq; i++) {
2207 queue_type = virtio_get_queue_type(hw, i);
2208 if (queue_type == VTNET_RQ)
2210 else if (queue_type == VTNET_TQ)
2216 "Before freeing %s[%d] used and unused buf",
2220 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2221 rte_pktmbuf_free(buf);
2226 "After freeing %s[%d] used and unused buf",
2231 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2235 * Stop device: disable interrupt and mark link down
2238 virtio_dev_stop(struct rte_eth_dev *dev)
2240 struct virtio_hw *hw = dev->data->dev_private;
2241 struct rte_eth_link link;
2242 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2244 PMD_INIT_LOG(DEBUG, "stop");
2246 rte_spinlock_lock(&hw->state_lock);
2249 hw->started = false;
2251 if (intr_conf->lsc || intr_conf->rxq) {
2252 virtio_intr_disable(dev);
2254 /* Reset interrupt callback */
2255 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2256 rte_intr_callback_unregister(dev->intr_handle,
2257 virtio_interrupt_handler,
2262 memset(&link, 0, sizeof(link));
2263 rte_eth_linkstatus_set(dev, &link);
2265 rte_spinlock_unlock(&hw->state_lock);
2269 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2271 struct rte_eth_link link;
2273 struct virtio_hw *hw = dev->data->dev_private;
2275 memset(&link, 0, sizeof(link));
2276 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2277 link.link_speed = ETH_SPEED_NUM_10G;
2278 link.link_autoneg = ETH_LINK_FIXED;
2281 link.link_status = ETH_LINK_DOWN;
2282 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2283 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2284 vtpci_read_dev_config(hw,
2285 offsetof(struct virtio_net_config, status),
2286 &status, sizeof(status));
2287 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2288 link.link_status = ETH_LINK_DOWN;
2289 PMD_INIT_LOG(DEBUG, "Port %d is down",
2290 dev->data->port_id);
2292 link.link_status = ETH_LINK_UP;
2293 PMD_INIT_LOG(DEBUG, "Port %d is up",
2294 dev->data->port_id);
2297 link.link_status = ETH_LINK_UP;
2300 return rte_eth_linkstatus_set(dev, &link);
2304 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2306 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2307 struct virtio_hw *hw = dev->data->dev_private;
2308 uint64_t offloads = rxmode->offloads;
2310 if (mask & ETH_VLAN_FILTER_MASK) {
2311 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2312 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2315 "vlan filtering not available on this host");
2321 if (mask & ETH_VLAN_STRIP_MASK)
2322 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2328 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2330 uint64_t tso_mask, host_features;
2331 struct virtio_hw *hw = dev->data->dev_private;
2333 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2335 dev_info->max_rx_queues =
2336 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2337 dev_info->max_tx_queues =
2338 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2339 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2340 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2341 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2343 host_features = VTPCI_OPS(hw)->get_features(hw);
2344 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2345 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2346 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2347 dev_info->rx_offload_capa |=
2348 DEV_RX_OFFLOAD_TCP_CKSUM |
2349 DEV_RX_OFFLOAD_UDP_CKSUM;
2351 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2352 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2353 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2354 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2355 if ((host_features & tso_mask) == tso_mask)
2356 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2358 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2359 DEV_TX_OFFLOAD_VLAN_INSERT;
2360 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2361 dev_info->tx_offload_capa |=
2362 DEV_TX_OFFLOAD_UDP_CKSUM |
2363 DEV_TX_OFFLOAD_TCP_CKSUM;
2365 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2366 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2367 if ((host_features & tso_mask) == tso_mask)
2368 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2372 * It enables testpmd to collect per queue stats.
2375 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2376 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2377 __rte_unused uint8_t is_rx)
2382 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2383 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2384 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2386 RTE_INIT(virtio_init_log)
2388 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2389 if (virtio_logtype_init >= 0)
2390 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2391 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2392 if (virtio_logtype_driver >= 0)
2393 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);