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;
152 bool avail_wrap_counter;
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 avail_wrap_counter = vq->avail_wrap_counter;
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;
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 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
180 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
181 VRING_DESC_F_USED(!vq->avail_wrap_counter);
185 if (++vq->vq_avail_idx >= vq->vq_nentries) {
186 vq->vq_avail_idx -= vq->vq_nentries;
187 vq->avail_wrap_counter ^= 1;
191 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
192 + sizeof(struct virtio_net_ctrl_hdr);
193 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
194 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
195 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
196 VRING_DESC_F_USED(!vq->avail_wrap_counter);
199 if (++vq->vq_avail_idx >= vq->vq_nentries) {
200 vq->vq_avail_idx -= vq->vq_nentries;
201 vq->avail_wrap_counter ^= 1;
204 virtio_wmb(vq->hw->weak_barriers);
205 desc[head].flags = VRING_DESC_F_NEXT |
206 VRING_DESC_F_AVAIL(avail_wrap_counter) |
207 VRING_DESC_F_USED(!avail_wrap_counter);
209 virtio_wmb(vq->hw->weak_barriers);
210 virtqueue_notify(vq);
212 /* wait for used descriptors in virtqueue */
213 while (!desc_is_used(&desc[head], vq))
216 virtio_rmb(vq->hw->weak_barriers);
218 /* now get used descriptors */
219 vq->vq_free_cnt += nb_descs;
220 vq->vq_used_cons_idx += nb_descs;
221 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
222 vq->vq_used_cons_idx -= vq->vq_nentries;
223 vq->used_wrap_counter ^= 1;
226 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
227 "vq->vq_avail_idx=%d\n"
228 "vq->vq_used_cons_idx=%d\n"
229 "vq->avail_wrap_counter=%d\n"
230 "vq->used_wrap_counter=%d\n",
233 vq->vq_used_cons_idx,
234 vq->avail_wrap_counter,
235 vq->used_wrap_counter);
237 result = cvq->virtio_net_hdr_mz->addr;
242 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
243 int *dlen, int pkt_num)
247 virtio_net_ctrl_ack status = ~0;
248 struct virtio_pmd_ctrl *result;
249 struct virtqueue *vq;
251 ctrl->status = status;
253 if (!cvq || !cvq->vq) {
254 PMD_INIT_LOG(ERR, "Control queue is not supported.");
258 rte_spinlock_lock(&cvq->lock);
260 head = vq->vq_desc_head_idx;
262 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
263 "vq->hw->cvq = %p vq = %p",
264 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
266 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
267 rte_spinlock_unlock(&cvq->lock);
271 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
272 sizeof(struct virtio_pmd_ctrl));
274 if (vtpci_packed_queue(vq->hw)) {
275 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
280 * Format is enforced in qemu code:
281 * One TX packet for header;
282 * At least one TX packet per argument;
283 * One RX packet for ACK.
285 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
286 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
287 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
289 i = vq->vq_ring.desc[head].next;
291 for (k = 0; k < pkt_num; k++) {
292 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
293 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
294 + sizeof(struct virtio_net_ctrl_hdr)
295 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
296 vq->vq_ring.desc[i].len = dlen[k];
299 i = vq->vq_ring.desc[i].next;
302 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
303 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
304 + sizeof(struct virtio_net_ctrl_hdr);
305 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
308 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
310 vq_update_avail_ring(vq, head);
311 vq_update_avail_idx(vq);
313 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
315 virtqueue_notify(vq);
318 while (VIRTQUEUE_NUSED(vq) == 0) {
323 while (VIRTQUEUE_NUSED(vq)) {
324 uint32_t idx, desc_idx, used_idx;
325 struct vring_used_elem *uep;
327 used_idx = (uint32_t)(vq->vq_used_cons_idx
328 & (vq->vq_nentries - 1));
329 uep = &vq->vq_ring.used->ring[used_idx];
330 idx = (uint32_t) uep->id;
333 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
334 desc_idx = vq->vq_ring.desc[desc_idx].next;
338 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
339 vq->vq_desc_head_idx = idx;
341 vq->vq_used_cons_idx++;
345 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
346 vq->vq_free_cnt, vq->vq_desc_head_idx);
348 result = cvq->virtio_net_hdr_mz->addr;
351 rte_spinlock_unlock(&cvq->lock);
352 return result->status;
356 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
358 struct virtio_hw *hw = dev->data->dev_private;
359 struct virtio_pmd_ctrl ctrl;
363 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
364 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
365 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
367 dlen[0] = sizeof(uint16_t);
369 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
371 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
372 "failed, this is too late now...");
380 virtio_dev_queue_release(void *queue __rte_unused)
386 virtio_get_nr_vq(struct virtio_hw *hw)
388 uint16_t nr_vq = hw->max_queue_pairs * 2;
390 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
397 virtio_init_vring(struct virtqueue *vq)
399 int size = vq->vq_nentries;
400 struct vring *vr = &vq->vq_ring;
401 uint8_t *ring_mem = vq->vq_ring_virt_mem;
403 PMD_INIT_FUNC_TRACE();
405 memset(ring_mem, 0, vq->vq_ring_size);
407 vq->vq_used_cons_idx = 0;
408 vq->vq_desc_head_idx = 0;
409 vq->vq_avail_idx = 0;
410 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
411 vq->vq_free_cnt = vq->vq_nentries;
412 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
413 if (vtpci_packed_queue(vq->hw)) {
414 vring_init_packed(&vq->ring_packed, ring_mem,
415 VIRTIO_PCI_VRING_ALIGN, size);
416 vring_desc_init_packed(vq, size);
418 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
419 vring_desc_init_split(vr->desc, size);
422 * Disable device(host) interrupting guest
424 virtqueue_disable_intr(vq);
428 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
430 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
431 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
432 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
433 unsigned int vq_size, size;
434 struct virtio_hw *hw = dev->data->dev_private;
435 struct virtnet_rx *rxvq = NULL;
436 struct virtnet_tx *txvq = NULL;
437 struct virtnet_ctl *cvq = NULL;
438 struct virtqueue *vq;
439 size_t sz_hdr_mz = 0;
440 void *sw_ring = NULL;
441 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
443 int numa_node = dev->device->numa_node;
445 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
446 vtpci_queue_idx, numa_node);
449 * Read the virtqueue size from the Queue Size field
450 * Always power of 2 and if 0 virtqueue does not exist
452 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
453 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
455 PMD_INIT_LOG(ERR, "virtqueue does not exist");
459 if (!rte_is_power_of_2(vq_size)) {
460 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
464 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
465 dev->data->port_id, vtpci_queue_idx);
467 size = RTE_ALIGN_CEIL(sizeof(*vq) +
468 vq_size * sizeof(struct vq_desc_extra),
469 RTE_CACHE_LINE_SIZE);
470 if (queue_type == VTNET_TQ) {
472 * For each xmit packet, allocate a virtio_net_hdr
473 * and indirect ring elements
475 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
476 } else if (queue_type == VTNET_CQ) {
477 /* Allocate a page for control vq command, data and status */
478 sz_hdr_mz = PAGE_SIZE;
481 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
484 PMD_INIT_LOG(ERR, "can not allocate vq");
487 hw->vqs[vtpci_queue_idx] = vq;
490 vq->vq_queue_index = vtpci_queue_idx;
491 vq->vq_nentries = vq_size;
492 vq->event_flags_shadow = 0;
493 if (vtpci_packed_queue(hw)) {
494 vq->avail_wrap_counter = 1;
495 vq->used_wrap_counter = 1;
496 vq->avail_used_flags =
497 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
498 VRING_DESC_F_USED(!vq->avail_wrap_counter);
502 * Reserve a memzone for vring elements
504 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
505 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
506 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
507 size, vq->vq_ring_size);
509 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
510 numa_node, RTE_MEMZONE_IOVA_CONTIG,
511 VIRTIO_PCI_VRING_ALIGN);
513 if (rte_errno == EEXIST)
514 mz = rte_memzone_lookup(vq_name);
521 memset(mz->addr, 0, mz->len);
523 vq->vq_ring_mem = mz->iova;
524 vq->vq_ring_virt_mem = mz->addr;
525 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
527 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
528 (uint64_t)(uintptr_t)mz->addr);
530 virtio_init_vring(vq);
533 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
534 dev->data->port_id, vtpci_queue_idx);
535 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
536 numa_node, RTE_MEMZONE_IOVA_CONTIG,
537 RTE_CACHE_LINE_SIZE);
538 if (hdr_mz == NULL) {
539 if (rte_errno == EEXIST)
540 hdr_mz = rte_memzone_lookup(vq_hdr_name);
541 if (hdr_mz == NULL) {
548 if (queue_type == VTNET_RQ) {
549 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
550 sizeof(vq->sw_ring[0]);
552 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
553 RTE_CACHE_LINE_SIZE, numa_node);
555 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
560 vq->sw_ring = sw_ring;
563 rxvq->port_id = dev->data->port_id;
565 } else if (queue_type == VTNET_TQ) {
568 txvq->port_id = dev->data->port_id;
570 txvq->virtio_net_hdr_mz = hdr_mz;
571 txvq->virtio_net_hdr_mem = hdr_mz->iova;
572 } else if (queue_type == VTNET_CQ) {
576 cvq->virtio_net_hdr_mz = hdr_mz;
577 cvq->virtio_net_hdr_mem = hdr_mz->iova;
578 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
583 /* For virtio_user case (that is when hw->dev is NULL), we use
584 * virtual address. And we need properly set _offset_, please see
585 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
587 if (!hw->virtio_user_dev)
588 vq->offset = offsetof(struct rte_mbuf, buf_iova);
590 vq->vq_ring_mem = (uintptr_t)mz->addr;
591 vq->offset = offsetof(struct rte_mbuf, buf_addr);
592 if (queue_type == VTNET_TQ)
593 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
594 else if (queue_type == VTNET_CQ)
595 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
598 if (queue_type == VTNET_TQ) {
599 struct virtio_tx_region *txr;
603 memset(txr, 0, vq_size * sizeof(*txr));
604 for (i = 0; i < vq_size; i++) {
605 struct vring_desc *start_dp = txr[i].tx_indir;
606 struct vring_packed_desc *start_dp_packed =
609 /* first indirect descriptor is always the tx header */
610 if (vtpci_packed_queue(hw)) {
611 start_dp_packed->addr = txvq->virtio_net_hdr_mem
613 + offsetof(struct virtio_tx_region,
615 start_dp_packed->len = hw->vtnet_hdr_size;
617 vring_desc_init_split(start_dp,
618 RTE_DIM(txr[i].tx_indir));
619 start_dp->addr = txvq->virtio_net_hdr_mem
621 + offsetof(struct virtio_tx_region,
623 start_dp->len = hw->vtnet_hdr_size;
624 start_dp->flags = VRING_DESC_F_NEXT;
629 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
630 PMD_INIT_LOG(ERR, "setup_queue failed");
638 rte_memzone_free(hdr_mz);
639 rte_memzone_free(mz);
646 virtio_free_queues(struct virtio_hw *hw)
648 uint16_t nr_vq = virtio_get_nr_vq(hw);
649 struct virtqueue *vq;
656 for (i = 0; i < nr_vq; i++) {
661 queue_type = virtio_get_queue_type(hw, i);
662 if (queue_type == VTNET_RQ) {
663 rte_free(vq->sw_ring);
664 rte_memzone_free(vq->rxq.mz);
665 } else if (queue_type == VTNET_TQ) {
666 rte_memzone_free(vq->txq.mz);
667 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
669 rte_memzone_free(vq->cq.mz);
670 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
682 virtio_alloc_queues(struct rte_eth_dev *dev)
684 struct virtio_hw *hw = dev->data->dev_private;
685 uint16_t nr_vq = virtio_get_nr_vq(hw);
689 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
691 PMD_INIT_LOG(ERR, "failed to allocate vqs");
695 for (i = 0; i < nr_vq; i++) {
696 ret = virtio_init_queue(dev, i);
698 virtio_free_queues(hw);
706 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
709 virtio_dev_close(struct rte_eth_dev *dev)
711 struct virtio_hw *hw = dev->data->dev_private;
712 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
714 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
721 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
722 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
724 virtio_queues_unbind_intr(dev);
726 if (intr_conf->lsc || intr_conf->rxq) {
727 virtio_intr_disable(dev);
728 rte_intr_efd_disable(dev->intr_handle);
729 rte_free(dev->intr_handle->intr_vec);
730 dev->intr_handle->intr_vec = NULL;
734 virtio_dev_free_mbufs(dev);
735 virtio_free_queues(hw);
739 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
741 struct virtio_hw *hw = dev->data->dev_private;
742 struct virtio_pmd_ctrl ctrl;
746 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
747 PMD_INIT_LOG(INFO, "host does not support rx control");
751 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
752 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
756 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
758 PMD_INIT_LOG(ERR, "Failed to enable promisc");
762 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
764 struct virtio_hw *hw = dev->data->dev_private;
765 struct virtio_pmd_ctrl ctrl;
769 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
770 PMD_INIT_LOG(INFO, "host does not support rx control");
774 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
775 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
779 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
781 PMD_INIT_LOG(ERR, "Failed to disable promisc");
785 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
787 struct virtio_hw *hw = dev->data->dev_private;
788 struct virtio_pmd_ctrl ctrl;
792 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
793 PMD_INIT_LOG(INFO, "host does not support rx control");
797 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
798 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
802 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
804 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
808 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
810 struct virtio_hw *hw = dev->data->dev_private;
811 struct virtio_pmd_ctrl ctrl;
815 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
816 PMD_INIT_LOG(INFO, "host does not support rx control");
820 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
821 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
825 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
827 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
830 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
832 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
834 struct virtio_hw *hw = dev->data->dev_private;
835 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
837 uint32_t frame_size = mtu + ether_hdr_len;
838 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
840 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
842 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
843 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
844 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
851 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
853 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
854 struct virtqueue *vq = rxvq->vq;
856 virtqueue_enable_intr(vq);
861 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
863 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
864 struct virtqueue *vq = rxvq->vq;
866 virtqueue_disable_intr(vq);
871 * dev_ops for virtio, bare necessities for basic operation
873 static const struct eth_dev_ops virtio_eth_dev_ops = {
874 .dev_configure = virtio_dev_configure,
875 .dev_start = virtio_dev_start,
876 .dev_stop = virtio_dev_stop,
877 .dev_close = virtio_dev_close,
878 .promiscuous_enable = virtio_dev_promiscuous_enable,
879 .promiscuous_disable = virtio_dev_promiscuous_disable,
880 .allmulticast_enable = virtio_dev_allmulticast_enable,
881 .allmulticast_disable = virtio_dev_allmulticast_disable,
882 .mtu_set = virtio_mtu_set,
883 .dev_infos_get = virtio_dev_info_get,
884 .stats_get = virtio_dev_stats_get,
885 .xstats_get = virtio_dev_xstats_get,
886 .xstats_get_names = virtio_dev_xstats_get_names,
887 .stats_reset = virtio_dev_stats_reset,
888 .xstats_reset = virtio_dev_stats_reset,
889 .link_update = virtio_dev_link_update,
890 .vlan_offload_set = virtio_dev_vlan_offload_set,
891 .rx_queue_setup = virtio_dev_rx_queue_setup,
892 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
893 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
894 .rx_queue_release = virtio_dev_queue_release,
895 .rx_descriptor_done = virtio_dev_rx_queue_done,
896 .tx_queue_setup = virtio_dev_tx_queue_setup,
897 .tx_queue_release = virtio_dev_queue_release,
898 /* collect stats per queue */
899 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
900 .vlan_filter_set = virtio_vlan_filter_set,
901 .mac_addr_add = virtio_mac_addr_add,
902 .mac_addr_remove = virtio_mac_addr_remove,
903 .mac_addr_set = virtio_mac_addr_set,
907 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
911 for (i = 0; i < dev->data->nb_tx_queues; i++) {
912 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
916 stats->opackets += txvq->stats.packets;
917 stats->obytes += txvq->stats.bytes;
918 stats->oerrors += txvq->stats.errors;
920 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
921 stats->q_opackets[i] = txvq->stats.packets;
922 stats->q_obytes[i] = txvq->stats.bytes;
926 for (i = 0; i < dev->data->nb_rx_queues; i++) {
927 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
931 stats->ipackets += rxvq->stats.packets;
932 stats->ibytes += rxvq->stats.bytes;
933 stats->ierrors += rxvq->stats.errors;
935 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
936 stats->q_ipackets[i] = rxvq->stats.packets;
937 stats->q_ibytes[i] = rxvq->stats.bytes;
941 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
944 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
945 struct rte_eth_xstat_name *xstats_names,
946 __rte_unused unsigned limit)
952 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
953 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
955 if (xstats_names != NULL) {
956 /* Note: limit checked in rte_eth_xstats_names() */
958 for (i = 0; i < dev->data->nb_rx_queues; i++) {
959 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
962 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
963 snprintf(xstats_names[count].name,
964 sizeof(xstats_names[count].name),
966 rte_virtio_rxq_stat_strings[t].name);
971 for (i = 0; i < dev->data->nb_tx_queues; i++) {
972 struct virtnet_tx *txvq = dev->data->tx_queues[i];
975 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
976 snprintf(xstats_names[count].name,
977 sizeof(xstats_names[count].name),
979 rte_virtio_txq_stat_strings[t].name);
989 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
995 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
996 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1001 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1002 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1009 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1010 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1011 rte_virtio_rxq_stat_strings[t].offset);
1012 xstats[count].id = count;
1017 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1018 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1025 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1026 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1027 rte_virtio_txq_stat_strings[t].offset);
1028 xstats[count].id = count;
1037 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1039 virtio_update_stats(dev, stats);
1045 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1049 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1050 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1054 txvq->stats.packets = 0;
1055 txvq->stats.bytes = 0;
1056 txvq->stats.errors = 0;
1057 txvq->stats.multicast = 0;
1058 txvq->stats.broadcast = 0;
1059 memset(txvq->stats.size_bins, 0,
1060 sizeof(txvq->stats.size_bins[0]) * 8);
1063 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1064 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1068 rxvq->stats.packets = 0;
1069 rxvq->stats.bytes = 0;
1070 rxvq->stats.errors = 0;
1071 rxvq->stats.multicast = 0;
1072 rxvq->stats.broadcast = 0;
1073 memset(rxvq->stats.size_bins, 0,
1074 sizeof(rxvq->stats.size_bins[0]) * 8);
1079 virtio_set_hwaddr(struct virtio_hw *hw)
1081 vtpci_write_dev_config(hw,
1082 offsetof(struct virtio_net_config, mac),
1083 &hw->mac_addr, ETHER_ADDR_LEN);
1087 virtio_get_hwaddr(struct virtio_hw *hw)
1089 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1090 vtpci_read_dev_config(hw,
1091 offsetof(struct virtio_net_config, mac),
1092 &hw->mac_addr, ETHER_ADDR_LEN);
1094 eth_random_addr(&hw->mac_addr[0]);
1095 virtio_set_hwaddr(hw);
1100 virtio_mac_table_set(struct virtio_hw *hw,
1101 const struct virtio_net_ctrl_mac *uc,
1102 const struct virtio_net_ctrl_mac *mc)
1104 struct virtio_pmd_ctrl ctrl;
1107 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1108 PMD_DRV_LOG(INFO, "host does not support mac table");
1112 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1113 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1115 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1116 memcpy(ctrl.data, uc, len[0]);
1118 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1119 memcpy(ctrl.data + len[0], mc, len[1]);
1121 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1123 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1128 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1129 uint32_t index, uint32_t vmdq __rte_unused)
1131 struct virtio_hw *hw = dev->data->dev_private;
1132 const struct ether_addr *addrs = dev->data->mac_addrs;
1134 struct virtio_net_ctrl_mac *uc, *mc;
1136 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1137 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1141 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1143 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1146 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1147 const struct ether_addr *addr
1148 = (i == index) ? mac_addr : addrs + i;
1149 struct virtio_net_ctrl_mac *tbl
1150 = is_multicast_ether_addr(addr) ? mc : uc;
1152 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1155 return virtio_mac_table_set(hw, uc, mc);
1159 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1161 struct virtio_hw *hw = dev->data->dev_private;
1162 struct ether_addr *addrs = dev->data->mac_addrs;
1163 struct virtio_net_ctrl_mac *uc, *mc;
1166 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1167 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1171 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1173 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1176 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1177 struct virtio_net_ctrl_mac *tbl;
1179 if (i == index || is_zero_ether_addr(addrs + i))
1182 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1183 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1186 virtio_mac_table_set(hw, uc, mc);
1190 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1192 struct virtio_hw *hw = dev->data->dev_private;
1194 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1196 /* Use atomic update if available */
1197 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1198 struct virtio_pmd_ctrl ctrl;
1199 int len = ETHER_ADDR_LEN;
1201 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1202 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1204 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1205 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1208 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1211 virtio_set_hwaddr(hw);
1216 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1218 struct virtio_hw *hw = dev->data->dev_private;
1219 struct virtio_pmd_ctrl ctrl;
1222 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1225 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1226 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1227 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1228 len = sizeof(vlan_id);
1230 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1234 virtio_intr_enable(struct rte_eth_dev *dev)
1236 struct virtio_hw *hw = dev->data->dev_private;
1238 if (rte_intr_enable(dev->intr_handle) < 0)
1241 if (!hw->virtio_user_dev)
1242 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1248 virtio_intr_disable(struct rte_eth_dev *dev)
1250 struct virtio_hw *hw = dev->data->dev_private;
1252 if (rte_intr_disable(dev->intr_handle) < 0)
1255 if (!hw->virtio_user_dev)
1256 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1262 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1264 uint64_t host_features;
1266 /* Prepare guest_features: feature that driver wants to support */
1267 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1270 /* Read device(host) feature bits */
1271 host_features = VTPCI_OPS(hw)->get_features(hw);
1272 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1275 /* If supported, ensure MTU value is valid before acknowledging it. */
1276 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1277 struct virtio_net_config config;
1279 vtpci_read_dev_config(hw,
1280 offsetof(struct virtio_net_config, mtu),
1281 &config.mtu, sizeof(config.mtu));
1283 if (config.mtu < ETHER_MIN_MTU)
1284 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1288 * Negotiate features: Subset of device feature bits are written back
1289 * guest feature bits.
1291 hw->guest_features = req_features;
1292 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1293 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1294 hw->guest_features);
1297 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1299 "VIRTIO_F_VERSION_1 features is not enabled.");
1302 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1303 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1305 "failed to set FEATURES_OK status!");
1310 hw->req_guest_features = req_features;
1316 virtio_dev_pause(struct rte_eth_dev *dev)
1318 struct virtio_hw *hw = dev->data->dev_private;
1320 rte_spinlock_lock(&hw->state_lock);
1322 if (hw->started == 0) {
1323 /* Device is just stopped. */
1324 rte_spinlock_unlock(&hw->state_lock);
1329 * Prevent the worker threads from touching queues to avoid contention,
1330 * 1 ms should be enough for the ongoing Tx function to finish.
1337 * Recover hw state to let the worker threads continue.
1340 virtio_dev_resume(struct rte_eth_dev *dev)
1342 struct virtio_hw *hw = dev->data->dev_private;
1345 rte_spinlock_unlock(&hw->state_lock);
1349 * Should be called only after device is paused.
1352 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1355 struct virtio_hw *hw = dev->data->dev_private;
1356 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1359 hw->inject_pkts = tx_pkts;
1360 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1361 hw->inject_pkts = NULL;
1367 virtio_notify_peers(struct rte_eth_dev *dev)
1369 struct virtio_hw *hw = dev->data->dev_private;
1370 struct virtnet_rx *rxvq;
1371 struct rte_mbuf *rarp_mbuf;
1373 if (!dev->data->rx_queues)
1376 rxvq = dev->data->rx_queues[0];
1380 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1381 (struct ether_addr *)hw->mac_addr);
1382 if (rarp_mbuf == NULL) {
1383 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1387 /* If virtio port just stopped, no need to send RARP */
1388 if (virtio_dev_pause(dev) < 0) {
1389 rte_pktmbuf_free(rarp_mbuf);
1393 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1394 virtio_dev_resume(dev);
1398 virtio_ack_link_announce(struct rte_eth_dev *dev)
1400 struct virtio_hw *hw = dev->data->dev_private;
1401 struct virtio_pmd_ctrl ctrl;
1403 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1404 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1406 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1410 * Process virtio config changed interrupt. Call the callback
1411 * if link state changed, generate gratuitous RARP packet if
1412 * the status indicates an ANNOUNCE.
1415 virtio_interrupt_handler(void *param)
1417 struct rte_eth_dev *dev = param;
1418 struct virtio_hw *hw = dev->data->dev_private;
1422 /* Read interrupt status which clears interrupt */
1423 isr = vtpci_isr(hw);
1424 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1426 if (virtio_intr_enable(dev) < 0)
1427 PMD_DRV_LOG(ERR, "interrupt enable failed");
1429 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1430 if (virtio_dev_link_update(dev, 0) == 0)
1431 _rte_eth_dev_callback_process(dev,
1432 RTE_ETH_EVENT_INTR_LSC,
1435 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1436 vtpci_read_dev_config(hw,
1437 offsetof(struct virtio_net_config, status),
1438 &status, sizeof(status));
1439 if (status & VIRTIO_NET_S_ANNOUNCE) {
1440 virtio_notify_peers(dev);
1442 virtio_ack_link_announce(dev);
1448 /* set rx and tx handlers according to what is supported */
1450 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1452 struct virtio_hw *hw = eth_dev->data->dev_private;
1454 if (vtpci_packed_queue(hw)) {
1456 "virtio: using packed ring standard Tx path on port %u",
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;
2072 hw->use_inorder_tx = 0;
2075 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2076 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2077 hw->use_simple_rx = 0;
2080 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2081 hw->use_simple_rx = 0;
2084 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2085 DEV_RX_OFFLOAD_TCP_CKSUM |
2086 DEV_RX_OFFLOAD_TCP_LRO |
2087 DEV_RX_OFFLOAD_VLAN_STRIP))
2088 hw->use_simple_rx = 0;
2097 virtio_dev_start(struct rte_eth_dev *dev)
2099 uint16_t nb_queues, i;
2100 struct virtnet_rx *rxvq;
2101 struct virtnet_tx *txvq __rte_unused;
2102 struct virtio_hw *hw = dev->data->dev_private;
2105 /* Finish the initialization of the queues */
2106 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2107 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2111 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2112 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2117 /* check if lsc interrupt feature is enabled */
2118 if (dev->data->dev_conf.intr_conf.lsc) {
2119 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2120 PMD_DRV_LOG(ERR, "link status not supported by host");
2125 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2126 * in device configure, but it could be unmapped when device is
2129 if (dev->data->dev_conf.intr_conf.lsc ||
2130 dev->data->dev_conf.intr_conf.rxq) {
2131 virtio_intr_disable(dev);
2133 /* Setup interrupt callback */
2134 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2135 rte_intr_callback_register(dev->intr_handle,
2136 virtio_interrupt_handler,
2139 if (virtio_intr_enable(dev) < 0) {
2140 PMD_DRV_LOG(ERR, "interrupt enable failed");
2145 /*Notify the backend
2146 *Otherwise the tap backend might already stop its queue due to fullness.
2147 *vhost backend will have no chance to be waked up
2149 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2150 if (hw->max_queue_pairs > 1) {
2151 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2155 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2157 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2158 rxvq = dev->data->rx_queues[i];
2159 /* Flush the old packets */
2160 virtqueue_rxvq_flush(rxvq->vq);
2161 virtqueue_notify(rxvq->vq);
2164 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2165 txvq = dev->data->tx_queues[i];
2166 virtqueue_notify(txvq->vq);
2169 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2171 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2172 rxvq = dev->data->rx_queues[i];
2173 VIRTQUEUE_DUMP(rxvq->vq);
2176 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2177 txvq = dev->data->tx_queues[i];
2178 VIRTQUEUE_DUMP(txvq->vq);
2181 set_rxtx_funcs(dev);
2184 /* Initialize Link state */
2185 virtio_dev_link_update(dev, 0);
2190 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2192 struct virtio_hw *hw = dev->data->dev_private;
2193 uint16_t nr_vq = virtio_get_nr_vq(hw);
2194 const char *type __rte_unused;
2195 unsigned int i, mbuf_num = 0;
2196 struct virtqueue *vq;
2197 struct rte_mbuf *buf;
2200 if (hw->vqs == NULL)
2203 for (i = 0; i < nr_vq; i++) {
2208 queue_type = virtio_get_queue_type(hw, i);
2209 if (queue_type == VTNET_RQ)
2211 else if (queue_type == VTNET_TQ)
2217 "Before freeing %s[%d] used and unused buf",
2221 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2222 rte_pktmbuf_free(buf);
2227 "After freeing %s[%d] used and unused buf",
2232 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2236 * Stop device: disable interrupt and mark link down
2239 virtio_dev_stop(struct rte_eth_dev *dev)
2241 struct virtio_hw *hw = dev->data->dev_private;
2242 struct rte_eth_link link;
2243 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2245 PMD_INIT_LOG(DEBUG, "stop");
2247 rte_spinlock_lock(&hw->state_lock);
2250 hw->started = false;
2252 if (intr_conf->lsc || intr_conf->rxq) {
2253 virtio_intr_disable(dev);
2255 /* Reset interrupt callback */
2256 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2257 rte_intr_callback_unregister(dev->intr_handle,
2258 virtio_interrupt_handler,
2263 memset(&link, 0, sizeof(link));
2264 rte_eth_linkstatus_set(dev, &link);
2266 rte_spinlock_unlock(&hw->state_lock);
2270 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2272 struct rte_eth_link link;
2274 struct virtio_hw *hw = dev->data->dev_private;
2276 memset(&link, 0, sizeof(link));
2277 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2278 link.link_speed = ETH_SPEED_NUM_10G;
2279 link.link_autoneg = ETH_LINK_FIXED;
2282 link.link_status = ETH_LINK_DOWN;
2283 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2284 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2285 vtpci_read_dev_config(hw,
2286 offsetof(struct virtio_net_config, status),
2287 &status, sizeof(status));
2288 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2289 link.link_status = ETH_LINK_DOWN;
2290 PMD_INIT_LOG(DEBUG, "Port %d is down",
2291 dev->data->port_id);
2293 link.link_status = ETH_LINK_UP;
2294 PMD_INIT_LOG(DEBUG, "Port %d is up",
2295 dev->data->port_id);
2298 link.link_status = ETH_LINK_UP;
2301 return rte_eth_linkstatus_set(dev, &link);
2305 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2307 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2308 struct virtio_hw *hw = dev->data->dev_private;
2309 uint64_t offloads = rxmode->offloads;
2311 if (mask & ETH_VLAN_FILTER_MASK) {
2312 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2313 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2316 "vlan filtering not available on this host");
2322 if (mask & ETH_VLAN_STRIP_MASK)
2323 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2329 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2331 uint64_t tso_mask, host_features;
2332 struct virtio_hw *hw = dev->data->dev_private;
2334 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2336 dev_info->max_rx_queues =
2337 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2338 dev_info->max_tx_queues =
2339 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2340 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2341 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2342 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2344 host_features = VTPCI_OPS(hw)->get_features(hw);
2345 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2346 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2347 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2348 dev_info->rx_offload_capa |=
2349 DEV_RX_OFFLOAD_TCP_CKSUM |
2350 DEV_RX_OFFLOAD_UDP_CKSUM;
2352 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2353 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2354 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2355 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2356 if ((host_features & tso_mask) == tso_mask)
2357 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2359 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2360 DEV_TX_OFFLOAD_VLAN_INSERT;
2361 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2362 dev_info->tx_offload_capa |=
2363 DEV_TX_OFFLOAD_UDP_CKSUM |
2364 DEV_TX_OFFLOAD_TCP_CKSUM;
2366 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2367 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2368 if ((host_features & tso_mask) == tso_mask)
2369 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2373 * It enables testpmd to collect per queue stats.
2376 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2377 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2378 __rte_unused uint8_t is_rx)
2383 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2384 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2385 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2387 RTE_INIT(virtio_init_log)
2389 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2390 if (virtio_logtype_init >= 0)
2391 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2392 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2393 if (virtio_logtype_driver >= 0)
2394 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);