1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <rte_ethdev_driver.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_memcpy.h>
14 #include <rte_string_fns.h>
15 #include <rte_memzone.h>
16 #include <rte_malloc.h>
17 #include <rte_branch_prediction.h>
19 #include <rte_bus_pci.h>
20 #include <rte_ether.h>
23 #include <rte_common.h>
24 #include <rte_errno.h>
25 #include <rte_cpuflags.h>
27 #include <rte_memory.h>
30 #include <rte_cycles.h>
31 #include <rte_kvargs.h>
33 #include "virtio_ethdev.h"
34 #include "virtio_pci.h"
35 #include "virtio_logs.h"
36 #include "virtqueue.h"
37 #include "virtio_rxtx.h"
39 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
40 static int virtio_dev_configure(struct rte_eth_dev *dev);
41 static int virtio_dev_start(struct rte_eth_dev *dev);
42 static void virtio_dev_stop(struct rte_eth_dev *dev);
43 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
44 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
45 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
46 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
47 static void virtio_dev_info_get(struct rte_eth_dev *dev,
48 struct rte_eth_dev_info *dev_info);
49 static int virtio_dev_link_update(struct rte_eth_dev *dev,
50 int wait_to_complete);
51 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
53 static void virtio_set_hwaddr(struct virtio_hw *hw);
54 static void virtio_get_hwaddr(struct virtio_hw *hw);
56 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
57 struct rte_eth_stats *stats);
58 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
59 struct rte_eth_xstat *xstats, unsigned n);
60 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
61 struct rte_eth_xstat_name *xstats_names,
63 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
64 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
65 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
66 uint16_t vlan_id, int on);
67 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
68 struct ether_addr *mac_addr,
69 uint32_t index, uint32_t vmdq);
70 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
71 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
72 struct ether_addr *mac_addr);
74 static int virtio_intr_enable(struct rte_eth_dev *dev);
75 static int virtio_intr_disable(struct rte_eth_dev *dev);
77 static int virtio_dev_queue_stats_mapping_set(
78 struct rte_eth_dev *eth_dev,
83 int virtio_logtype_init;
84 int virtio_logtype_driver;
86 static void virtio_notify_peers(struct rte_eth_dev *dev);
87 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
90 * The set of PCI devices this driver supports
92 static const struct rte_pci_id pci_id_virtio_map[] = {
93 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
94 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
95 { .vendor_id = 0, /* sentinel */ },
98 struct rte_virtio_xstats_name_off {
99 char name[RTE_ETH_XSTATS_NAME_SIZE];
103 /* [rt]x_qX_ is prepended to the name string here */
104 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
105 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
106 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
107 {"errors", offsetof(struct virtnet_rx, stats.errors)},
108 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
109 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
110 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
111 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
112 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
113 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
114 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
115 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
116 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
117 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
120 /* [rt]x_qX_ is prepended to the name string here */
121 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
122 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
123 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
124 {"errors", offsetof(struct virtnet_tx, stats.errors)},
125 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
126 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
127 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
128 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
129 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
130 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
131 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
132 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
133 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
134 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
137 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
138 sizeof(rte_virtio_rxq_stat_strings[0]))
139 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
140 sizeof(rte_virtio_txq_stat_strings[0]))
142 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
144 static struct virtio_pmd_ctrl *
145 virtio_send_command_packed(struct virtnet_ctl *cvq,
146 struct virtio_pmd_ctrl *ctrl,
147 int *dlen, int pkt_num)
149 struct virtqueue *vq = cvq->vq;
151 struct vring_packed_desc *desc = vq->ring_packed.desc_packed;
152 struct virtio_pmd_ctrl *result;
153 bool avail_wrap_counter;
159 * Format is enforced in qemu code:
160 * One TX packet for header;
161 * At least one TX packet per argument;
162 * One RX packet for ACK.
164 head = vq->vq_avail_idx;
165 avail_wrap_counter = vq->avail_wrap_counter;
166 desc[head].addr = cvq->virtio_net_hdr_mem;
167 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
170 if (++vq->vq_avail_idx >= vq->vq_nentries) {
171 vq->vq_avail_idx -= vq->vq_nentries;
172 vq->avail_wrap_counter ^= 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 |
181 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
182 VRING_DESC_F_USED(!vq->avail_wrap_counter);
186 if (++vq->vq_avail_idx >= vq->vq_nentries) {
187 vq->vq_avail_idx -= vq->vq_nentries;
188 vq->avail_wrap_counter ^= 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 |
196 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
197 VRING_DESC_F_USED(!vq->avail_wrap_counter);
200 if (++vq->vq_avail_idx >= vq->vq_nentries) {
201 vq->vq_avail_idx -= vq->vq_nentries;
202 vq->avail_wrap_counter ^= 1;
205 virtio_wmb(vq->hw->weak_barriers);
206 desc[head].flags = VRING_DESC_F_NEXT |
207 VRING_DESC_F_AVAIL(avail_wrap_counter) |
208 VRING_DESC_F_USED(!avail_wrap_counter);
210 virtio_wmb(vq->hw->weak_barriers);
211 virtqueue_notify(vq);
213 /* wait for used descriptors in virtqueue */
214 while (!desc_is_used(&desc[head], vq))
217 virtio_rmb(vq->hw->weak_barriers);
219 /* now get used descriptors */
220 vq->vq_free_cnt += nb_descs;
221 vq->vq_used_cons_idx += nb_descs;
222 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
223 vq->vq_used_cons_idx -= vq->vq_nentries;
224 vq->used_wrap_counter ^= 1;
227 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
228 "vq->vq_avail_idx=%d\n"
229 "vq->vq_used_cons_idx=%d\n"
230 "vq->avail_wrap_counter=%d\n"
231 "vq->used_wrap_counter=%d\n",
234 vq->vq_used_cons_idx,
235 vq->avail_wrap_counter,
236 vq->used_wrap_counter);
238 result = cvq->virtio_net_hdr_mz->addr;
243 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
244 int *dlen, int pkt_num)
248 virtio_net_ctrl_ack status = ~0;
249 struct virtio_pmd_ctrl *result;
250 struct virtqueue *vq;
252 ctrl->status = status;
254 if (!cvq || !cvq->vq) {
255 PMD_INIT_LOG(ERR, "Control queue is not supported.");
259 rte_spinlock_lock(&cvq->lock);
261 head = vq->vq_desc_head_idx;
263 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
264 "vq->hw->cvq = %p vq = %p",
265 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
267 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
268 rte_spinlock_unlock(&cvq->lock);
272 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
273 sizeof(struct virtio_pmd_ctrl));
275 if (vtpci_packed_queue(vq->hw)) {
276 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
281 * Format is enforced in qemu code:
282 * One TX packet for header;
283 * At least one TX packet per argument;
284 * One RX packet for ACK.
286 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
287 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
288 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
290 i = vq->vq_ring.desc[head].next;
292 for (k = 0; k < pkt_num; k++) {
293 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
294 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
295 + sizeof(struct virtio_net_ctrl_hdr)
296 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
297 vq->vq_ring.desc[i].len = dlen[k];
300 i = vq->vq_ring.desc[i].next;
303 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
304 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
305 + sizeof(struct virtio_net_ctrl_hdr);
306 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
309 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
311 vq_update_avail_ring(vq, head);
312 vq_update_avail_idx(vq);
314 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
316 virtqueue_notify(vq);
319 while (VIRTQUEUE_NUSED(vq) == 0) {
324 while (VIRTQUEUE_NUSED(vq)) {
325 uint32_t idx, desc_idx, used_idx;
326 struct vring_used_elem *uep;
328 used_idx = (uint32_t)(vq->vq_used_cons_idx
329 & (vq->vq_nentries - 1));
330 uep = &vq->vq_ring.used->ring[used_idx];
331 idx = (uint32_t) uep->id;
334 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
335 desc_idx = vq->vq_ring.desc[desc_idx].next;
339 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
340 vq->vq_desc_head_idx = idx;
342 vq->vq_used_cons_idx++;
346 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
347 vq->vq_free_cnt, vq->vq_desc_head_idx);
349 result = cvq->virtio_net_hdr_mz->addr;
352 rte_spinlock_unlock(&cvq->lock);
353 return result->status;
357 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
359 struct virtio_hw *hw = dev->data->dev_private;
360 struct virtio_pmd_ctrl ctrl;
364 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
365 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
366 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
368 dlen[0] = sizeof(uint16_t);
370 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
372 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
373 "failed, this is too late now...");
381 virtio_dev_queue_release(void *queue __rte_unused)
387 virtio_get_nr_vq(struct virtio_hw *hw)
389 uint16_t nr_vq = hw->max_queue_pairs * 2;
391 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
398 virtio_init_vring(struct virtqueue *vq)
400 int size = vq->vq_nentries;
401 struct vring *vr = &vq->vq_ring;
402 uint8_t *ring_mem = vq->vq_ring_virt_mem;
404 PMD_INIT_FUNC_TRACE();
406 memset(ring_mem, 0, vq->vq_ring_size);
408 vq->vq_used_cons_idx = 0;
409 vq->vq_desc_head_idx = 0;
410 vq->vq_avail_idx = 0;
411 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
412 vq->vq_free_cnt = vq->vq_nentries;
413 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
414 if (vtpci_packed_queue(vq->hw)) {
415 vring_init_packed(&vq->ring_packed, ring_mem,
416 VIRTIO_PCI_VRING_ALIGN, size);
417 vring_desc_init_packed(vq, size);
419 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
420 vring_desc_init_split(vr->desc, size);
423 * Disable device(host) interrupting guest
425 virtqueue_disable_intr(vq);
429 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
431 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
432 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
433 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
434 unsigned int vq_size, size;
435 struct virtio_hw *hw = dev->data->dev_private;
436 struct virtnet_rx *rxvq = NULL;
437 struct virtnet_tx *txvq = NULL;
438 struct virtnet_ctl *cvq = NULL;
439 struct virtqueue *vq;
440 size_t sz_hdr_mz = 0;
441 void *sw_ring = NULL;
442 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
444 int numa_node = dev->device->numa_node;
446 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
447 vtpci_queue_idx, numa_node);
450 * Read the virtqueue size from the Queue Size field
451 * Always power of 2 and if 0 virtqueue does not exist
453 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
454 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
456 PMD_INIT_LOG(ERR, "virtqueue does not exist");
460 if (!rte_is_power_of_2(vq_size)) {
461 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
465 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
466 dev->data->port_id, vtpci_queue_idx);
468 size = RTE_ALIGN_CEIL(sizeof(*vq) +
469 vq_size * sizeof(struct vq_desc_extra),
470 RTE_CACHE_LINE_SIZE);
471 if (queue_type == VTNET_TQ) {
473 * For each xmit packet, allocate a virtio_net_hdr
474 * and indirect ring elements
476 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
477 } else if (queue_type == VTNET_CQ) {
478 /* Allocate a page for control vq command, data and status */
479 sz_hdr_mz = PAGE_SIZE;
482 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
485 PMD_INIT_LOG(ERR, "can not allocate vq");
488 hw->vqs[vtpci_queue_idx] = vq;
491 vq->vq_queue_index = vtpci_queue_idx;
492 vq->vq_nentries = vq_size;
493 vq->event_flags_shadow = 0;
494 if (vtpci_packed_queue(hw)) {
495 vq->avail_wrap_counter = 1;
496 vq->used_wrap_counter = 1;
497 vq->avail_used_flags =
498 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
499 VRING_DESC_F_USED(!vq->avail_wrap_counter);
503 * Reserve a memzone for vring elements
505 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
506 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
507 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
508 size, vq->vq_ring_size);
510 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
511 numa_node, RTE_MEMZONE_IOVA_CONTIG,
512 VIRTIO_PCI_VRING_ALIGN);
514 if (rte_errno == EEXIST)
515 mz = rte_memzone_lookup(vq_name);
522 memset(mz->addr, 0, mz->len);
524 vq->vq_ring_mem = mz->iova;
525 vq->vq_ring_virt_mem = mz->addr;
526 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
528 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
529 (uint64_t)(uintptr_t)mz->addr);
531 virtio_init_vring(vq);
534 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
535 dev->data->port_id, vtpci_queue_idx);
536 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
537 numa_node, RTE_MEMZONE_IOVA_CONTIG,
538 RTE_CACHE_LINE_SIZE);
539 if (hdr_mz == NULL) {
540 if (rte_errno == EEXIST)
541 hdr_mz = rte_memzone_lookup(vq_hdr_name);
542 if (hdr_mz == NULL) {
549 if (queue_type == VTNET_RQ) {
550 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
551 sizeof(vq->sw_ring[0]);
553 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
554 RTE_CACHE_LINE_SIZE, numa_node);
556 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
561 vq->sw_ring = sw_ring;
564 rxvq->port_id = dev->data->port_id;
566 } else if (queue_type == VTNET_TQ) {
569 txvq->port_id = dev->data->port_id;
571 txvq->virtio_net_hdr_mz = hdr_mz;
572 txvq->virtio_net_hdr_mem = hdr_mz->iova;
573 } else if (queue_type == VTNET_CQ) {
577 cvq->virtio_net_hdr_mz = hdr_mz;
578 cvq->virtio_net_hdr_mem = hdr_mz->iova;
579 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
584 /* For virtio_user case (that is when hw->dev is NULL), we use
585 * virtual address. And we need properly set _offset_, please see
586 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
588 if (!hw->virtio_user_dev)
589 vq->offset = offsetof(struct rte_mbuf, buf_iova);
591 vq->vq_ring_mem = (uintptr_t)mz->addr;
592 vq->offset = offsetof(struct rte_mbuf, buf_addr);
593 if (queue_type == VTNET_TQ)
594 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
595 else if (queue_type == VTNET_CQ)
596 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
599 if (queue_type == VTNET_TQ) {
600 struct virtio_tx_region *txr;
604 memset(txr, 0, vq_size * sizeof(*txr));
605 for (i = 0; i < vq_size; i++) {
606 struct vring_desc *start_dp = txr[i].tx_indir;
607 struct vring_packed_desc *start_dp_packed =
610 /* first indirect descriptor is always the tx header */
611 if (vtpci_packed_queue(hw)) {
612 start_dp_packed->addr = txvq->virtio_net_hdr_mem
614 + offsetof(struct virtio_tx_region,
616 start_dp_packed->len = hw->vtnet_hdr_size;
618 vring_desc_init_split(start_dp,
619 RTE_DIM(txr[i].tx_indir));
620 start_dp->addr = txvq->virtio_net_hdr_mem
622 + offsetof(struct virtio_tx_region,
624 start_dp->len = hw->vtnet_hdr_size;
625 start_dp->flags = VRING_DESC_F_NEXT;
630 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
631 PMD_INIT_LOG(ERR, "setup_queue failed");
639 rte_memzone_free(hdr_mz);
640 rte_memzone_free(mz);
647 virtio_free_queues(struct virtio_hw *hw)
649 uint16_t nr_vq = virtio_get_nr_vq(hw);
650 struct virtqueue *vq;
657 for (i = 0; i < nr_vq; i++) {
662 queue_type = virtio_get_queue_type(hw, i);
663 if (queue_type == VTNET_RQ) {
664 rte_free(vq->sw_ring);
665 rte_memzone_free(vq->rxq.mz);
666 } else if (queue_type == VTNET_TQ) {
667 rte_memzone_free(vq->txq.mz);
668 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
670 rte_memzone_free(vq->cq.mz);
671 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
683 virtio_alloc_queues(struct rte_eth_dev *dev)
685 struct virtio_hw *hw = dev->data->dev_private;
686 uint16_t nr_vq = virtio_get_nr_vq(hw);
690 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
692 PMD_INIT_LOG(ERR, "failed to allocate vqs");
696 for (i = 0; i < nr_vq; i++) {
697 ret = virtio_init_queue(dev, i);
699 virtio_free_queues(hw);
707 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
710 virtio_dev_close(struct rte_eth_dev *dev)
712 struct virtio_hw *hw = dev->data->dev_private;
713 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
715 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
722 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
723 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
725 virtio_queues_unbind_intr(dev);
727 if (intr_conf->lsc || intr_conf->rxq) {
728 virtio_intr_disable(dev);
729 rte_intr_efd_disable(dev->intr_handle);
730 rte_free(dev->intr_handle->intr_vec);
731 dev->intr_handle->intr_vec = NULL;
735 virtio_dev_free_mbufs(dev);
736 virtio_free_queues(hw);
740 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
742 struct virtio_hw *hw = dev->data->dev_private;
743 struct virtio_pmd_ctrl ctrl;
747 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
748 PMD_INIT_LOG(INFO, "host does not support rx control");
752 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
753 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
757 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
759 PMD_INIT_LOG(ERR, "Failed to enable promisc");
763 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
765 struct virtio_hw *hw = dev->data->dev_private;
766 struct virtio_pmd_ctrl ctrl;
770 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
771 PMD_INIT_LOG(INFO, "host does not support rx control");
775 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
776 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
780 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
782 PMD_INIT_LOG(ERR, "Failed to disable promisc");
786 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
788 struct virtio_hw *hw = dev->data->dev_private;
789 struct virtio_pmd_ctrl ctrl;
793 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
794 PMD_INIT_LOG(INFO, "host does not support rx control");
798 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
799 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
803 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
805 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
809 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
811 struct virtio_hw *hw = dev->data->dev_private;
812 struct virtio_pmd_ctrl ctrl;
816 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
817 PMD_INIT_LOG(INFO, "host does not support rx control");
821 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
822 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
826 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
828 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
831 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
833 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
835 struct virtio_hw *hw = dev->data->dev_private;
836 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
838 uint32_t frame_size = mtu + ether_hdr_len;
839 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
841 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
843 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
844 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
845 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
852 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
854 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
855 struct virtqueue *vq = rxvq->vq;
857 virtqueue_enable_intr(vq);
862 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
864 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
865 struct virtqueue *vq = rxvq->vq;
867 virtqueue_disable_intr(vq);
872 * dev_ops for virtio, bare necessities for basic operation
874 static const struct eth_dev_ops virtio_eth_dev_ops = {
875 .dev_configure = virtio_dev_configure,
876 .dev_start = virtio_dev_start,
877 .dev_stop = virtio_dev_stop,
878 .dev_close = virtio_dev_close,
879 .promiscuous_enable = virtio_dev_promiscuous_enable,
880 .promiscuous_disable = virtio_dev_promiscuous_disable,
881 .allmulticast_enable = virtio_dev_allmulticast_enable,
882 .allmulticast_disable = virtio_dev_allmulticast_disable,
883 .mtu_set = virtio_mtu_set,
884 .dev_infos_get = virtio_dev_info_get,
885 .stats_get = virtio_dev_stats_get,
886 .xstats_get = virtio_dev_xstats_get,
887 .xstats_get_names = virtio_dev_xstats_get_names,
888 .stats_reset = virtio_dev_stats_reset,
889 .xstats_reset = virtio_dev_stats_reset,
890 .link_update = virtio_dev_link_update,
891 .vlan_offload_set = virtio_dev_vlan_offload_set,
892 .rx_queue_setup = virtio_dev_rx_queue_setup,
893 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
894 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
895 .rx_queue_release = virtio_dev_queue_release,
896 .rx_descriptor_done = virtio_dev_rx_queue_done,
897 .tx_queue_setup = virtio_dev_tx_queue_setup,
898 .tx_queue_release = virtio_dev_queue_release,
899 /* collect stats per queue */
900 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
901 .vlan_filter_set = virtio_vlan_filter_set,
902 .mac_addr_add = virtio_mac_addr_add,
903 .mac_addr_remove = virtio_mac_addr_remove,
904 .mac_addr_set = virtio_mac_addr_set,
908 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
912 for (i = 0; i < dev->data->nb_tx_queues; i++) {
913 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
917 stats->opackets += txvq->stats.packets;
918 stats->obytes += txvq->stats.bytes;
919 stats->oerrors += txvq->stats.errors;
921 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
922 stats->q_opackets[i] = txvq->stats.packets;
923 stats->q_obytes[i] = txvq->stats.bytes;
927 for (i = 0; i < dev->data->nb_rx_queues; i++) {
928 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
932 stats->ipackets += rxvq->stats.packets;
933 stats->ibytes += rxvq->stats.bytes;
934 stats->ierrors += rxvq->stats.errors;
936 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
937 stats->q_ipackets[i] = rxvq->stats.packets;
938 stats->q_ibytes[i] = rxvq->stats.bytes;
942 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
945 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
946 struct rte_eth_xstat_name *xstats_names,
947 __rte_unused unsigned limit)
953 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
954 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
956 if (xstats_names != NULL) {
957 /* Note: limit checked in rte_eth_xstats_names() */
959 for (i = 0; i < dev->data->nb_rx_queues; i++) {
960 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
963 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
964 snprintf(xstats_names[count].name,
965 sizeof(xstats_names[count].name),
967 rte_virtio_rxq_stat_strings[t].name);
972 for (i = 0; i < dev->data->nb_tx_queues; i++) {
973 struct virtnet_tx *txvq = dev->data->tx_queues[i];
976 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
977 snprintf(xstats_names[count].name,
978 sizeof(xstats_names[count].name),
980 rte_virtio_txq_stat_strings[t].name);
990 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
996 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
997 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1002 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1003 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1010 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1011 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1012 rte_virtio_rxq_stat_strings[t].offset);
1013 xstats[count].id = count;
1018 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1019 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1026 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1027 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1028 rte_virtio_txq_stat_strings[t].offset);
1029 xstats[count].id = count;
1038 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1040 virtio_update_stats(dev, stats);
1046 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1050 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1051 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1055 txvq->stats.packets = 0;
1056 txvq->stats.bytes = 0;
1057 txvq->stats.errors = 0;
1058 txvq->stats.multicast = 0;
1059 txvq->stats.broadcast = 0;
1060 memset(txvq->stats.size_bins, 0,
1061 sizeof(txvq->stats.size_bins[0]) * 8);
1064 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1065 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1069 rxvq->stats.packets = 0;
1070 rxvq->stats.bytes = 0;
1071 rxvq->stats.errors = 0;
1072 rxvq->stats.multicast = 0;
1073 rxvq->stats.broadcast = 0;
1074 memset(rxvq->stats.size_bins, 0,
1075 sizeof(rxvq->stats.size_bins[0]) * 8);
1080 virtio_set_hwaddr(struct virtio_hw *hw)
1082 vtpci_write_dev_config(hw,
1083 offsetof(struct virtio_net_config, mac),
1084 &hw->mac_addr, ETHER_ADDR_LEN);
1088 virtio_get_hwaddr(struct virtio_hw *hw)
1090 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1091 vtpci_read_dev_config(hw,
1092 offsetof(struct virtio_net_config, mac),
1093 &hw->mac_addr, ETHER_ADDR_LEN);
1095 eth_random_addr(&hw->mac_addr[0]);
1096 virtio_set_hwaddr(hw);
1101 virtio_mac_table_set(struct virtio_hw *hw,
1102 const struct virtio_net_ctrl_mac *uc,
1103 const struct virtio_net_ctrl_mac *mc)
1105 struct virtio_pmd_ctrl ctrl;
1108 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1109 PMD_DRV_LOG(INFO, "host does not support mac table");
1113 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1114 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1116 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1117 memcpy(ctrl.data, uc, len[0]);
1119 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1120 memcpy(ctrl.data + len[0], mc, len[1]);
1122 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1124 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1129 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1130 uint32_t index, uint32_t vmdq __rte_unused)
1132 struct virtio_hw *hw = dev->data->dev_private;
1133 const struct ether_addr *addrs = dev->data->mac_addrs;
1135 struct virtio_net_ctrl_mac *uc, *mc;
1137 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1138 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1142 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1144 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1147 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1148 const struct ether_addr *addr
1149 = (i == index) ? mac_addr : addrs + i;
1150 struct virtio_net_ctrl_mac *tbl
1151 = is_multicast_ether_addr(addr) ? mc : uc;
1153 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1156 return virtio_mac_table_set(hw, uc, mc);
1160 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1162 struct virtio_hw *hw = dev->data->dev_private;
1163 struct ether_addr *addrs = dev->data->mac_addrs;
1164 struct virtio_net_ctrl_mac *uc, *mc;
1167 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1168 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1172 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1174 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1177 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1178 struct virtio_net_ctrl_mac *tbl;
1180 if (i == index || is_zero_ether_addr(addrs + i))
1183 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1184 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1187 virtio_mac_table_set(hw, uc, mc);
1191 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1193 struct virtio_hw *hw = dev->data->dev_private;
1195 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1197 /* Use atomic update if available */
1198 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1199 struct virtio_pmd_ctrl ctrl;
1200 int len = ETHER_ADDR_LEN;
1202 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1203 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1205 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1206 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1209 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1212 virtio_set_hwaddr(hw);
1217 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1219 struct virtio_hw *hw = dev->data->dev_private;
1220 struct virtio_pmd_ctrl ctrl;
1223 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1226 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1227 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1228 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1229 len = sizeof(vlan_id);
1231 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1235 virtio_intr_enable(struct rte_eth_dev *dev)
1237 struct virtio_hw *hw = dev->data->dev_private;
1239 if (rte_intr_enable(dev->intr_handle) < 0)
1242 if (!hw->virtio_user_dev)
1243 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1249 virtio_intr_disable(struct rte_eth_dev *dev)
1251 struct virtio_hw *hw = dev->data->dev_private;
1253 if (rte_intr_disable(dev->intr_handle) < 0)
1256 if (!hw->virtio_user_dev)
1257 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1263 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1265 uint64_t host_features;
1267 /* Prepare guest_features: feature that driver wants to support */
1268 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1271 /* Read device(host) feature bits */
1272 host_features = VTPCI_OPS(hw)->get_features(hw);
1273 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1276 /* If supported, ensure MTU value is valid before acknowledging it. */
1277 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1278 struct virtio_net_config config;
1280 vtpci_read_dev_config(hw,
1281 offsetof(struct virtio_net_config, mtu),
1282 &config.mtu, sizeof(config.mtu));
1284 if (config.mtu < ETHER_MIN_MTU)
1285 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1289 * Negotiate features: Subset of device feature bits are written back
1290 * guest feature bits.
1292 hw->guest_features = req_features;
1293 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1294 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1295 hw->guest_features);
1298 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1300 "VIRTIO_F_VERSION_1 features is not enabled.");
1303 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1304 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1306 "failed to set FEATURES_OK status!");
1311 hw->req_guest_features = req_features;
1317 virtio_dev_pause(struct rte_eth_dev *dev)
1319 struct virtio_hw *hw = dev->data->dev_private;
1321 rte_spinlock_lock(&hw->state_lock);
1323 if (hw->started == 0) {
1324 /* Device is just stopped. */
1325 rte_spinlock_unlock(&hw->state_lock);
1330 * Prevent the worker threads from touching queues to avoid contention,
1331 * 1 ms should be enough for the ongoing Tx function to finish.
1338 * Recover hw state to let the worker threads continue.
1341 virtio_dev_resume(struct rte_eth_dev *dev)
1343 struct virtio_hw *hw = dev->data->dev_private;
1346 rte_spinlock_unlock(&hw->state_lock);
1350 * Should be called only after device is paused.
1353 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1356 struct virtio_hw *hw = dev->data->dev_private;
1357 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1360 hw->inject_pkts = tx_pkts;
1361 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1362 hw->inject_pkts = NULL;
1368 virtio_notify_peers(struct rte_eth_dev *dev)
1370 struct virtio_hw *hw = dev->data->dev_private;
1371 struct virtnet_rx *rxvq;
1372 struct rte_mbuf *rarp_mbuf;
1374 if (!dev->data->rx_queues)
1377 rxvq = dev->data->rx_queues[0];
1381 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1382 (struct ether_addr *)hw->mac_addr);
1383 if (rarp_mbuf == NULL) {
1384 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1388 /* If virtio port just stopped, no need to send RARP */
1389 if (virtio_dev_pause(dev) < 0) {
1390 rte_pktmbuf_free(rarp_mbuf);
1394 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1395 virtio_dev_resume(dev);
1399 virtio_ack_link_announce(struct rte_eth_dev *dev)
1401 struct virtio_hw *hw = dev->data->dev_private;
1402 struct virtio_pmd_ctrl ctrl;
1404 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1405 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1407 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1411 * Process virtio config changed interrupt. Call the callback
1412 * if link state changed, generate gratuitous RARP packet if
1413 * the status indicates an ANNOUNCE.
1416 virtio_interrupt_handler(void *param)
1418 struct rte_eth_dev *dev = param;
1419 struct virtio_hw *hw = dev->data->dev_private;
1423 /* Read interrupt status which clears interrupt */
1424 isr = vtpci_isr(hw);
1425 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1427 if (virtio_intr_enable(dev) < 0)
1428 PMD_DRV_LOG(ERR, "interrupt enable failed");
1430 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1431 if (virtio_dev_link_update(dev, 0) == 0)
1432 _rte_eth_dev_callback_process(dev,
1433 RTE_ETH_EVENT_INTR_LSC,
1436 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1437 vtpci_read_dev_config(hw,
1438 offsetof(struct virtio_net_config, status),
1439 &status, sizeof(status));
1440 if (status & VIRTIO_NET_S_ANNOUNCE) {
1441 virtio_notify_peers(dev);
1443 virtio_ack_link_announce(dev);
1449 /* set rx and tx handlers according to what is supported */
1451 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1453 struct virtio_hw *hw = eth_dev->data->dev_private;
1455 if (vtpci_packed_queue(hw)) {
1457 "virtio: using packed ring standard Tx path on port %u",
1458 eth_dev->data->port_id);
1459 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1461 if (hw->use_inorder_tx) {
1462 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1463 eth_dev->data->port_id);
1464 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1466 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1467 eth_dev->data->port_id);
1468 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1472 if (vtpci_packed_queue(hw)) {
1473 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1475 "virtio: using packed ring mergeable buffer Rx path on port %u",
1476 eth_dev->data->port_id);
1477 eth_dev->rx_pkt_burst =
1478 &virtio_recv_mergeable_pkts_packed;
1481 "virtio: using packed ring standard Rx path on port %u",
1482 eth_dev->data->port_id);
1483 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1486 if (hw->use_simple_rx) {
1487 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1488 eth_dev->data->port_id);
1489 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1490 } else if (hw->use_inorder_rx) {
1492 "virtio: using inorder Rx path on port %u",
1493 eth_dev->data->port_id);
1494 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1495 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1497 "virtio: using mergeable buffer Rx path on port %u",
1498 eth_dev->data->port_id);
1499 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1501 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1502 eth_dev->data->port_id);
1503 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1509 /* Only support 1:1 queue/interrupt mapping so far.
1510 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1511 * interrupt vectors (<N+1).
1514 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1517 struct virtio_hw *hw = dev->data->dev_private;
1519 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1520 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1521 dev->intr_handle->intr_vec[i] = i + 1;
1522 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1523 VIRTIO_MSI_NO_VECTOR) {
1524 PMD_DRV_LOG(ERR, "failed to set queue vector");
1533 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1536 struct virtio_hw *hw = dev->data->dev_private;
1538 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1539 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1540 VTPCI_OPS(hw)->set_queue_irq(hw,
1541 hw->vqs[i * VTNET_CQ],
1542 VIRTIO_MSI_NO_VECTOR);
1546 virtio_configure_intr(struct rte_eth_dev *dev)
1548 struct virtio_hw *hw = dev->data->dev_private;
1550 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1551 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1555 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1556 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1560 if (!dev->intr_handle->intr_vec) {
1561 dev->intr_handle->intr_vec =
1562 rte_zmalloc("intr_vec",
1563 hw->max_queue_pairs * sizeof(int), 0);
1564 if (!dev->intr_handle->intr_vec) {
1565 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1566 hw->max_queue_pairs);
1571 /* Re-register callback to update max_intr */
1572 rte_intr_callback_unregister(dev->intr_handle,
1573 virtio_interrupt_handler,
1575 rte_intr_callback_register(dev->intr_handle,
1576 virtio_interrupt_handler,
1579 /* DO NOT try to remove this! This function will enable msix, or QEMU
1580 * will encounter SIGSEGV when DRIVER_OK is sent.
1581 * And for legacy devices, this should be done before queue/vec binding
1582 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1583 * (22) will be ignored.
1585 if (virtio_intr_enable(dev) < 0) {
1586 PMD_DRV_LOG(ERR, "interrupt enable failed");
1590 if (virtio_queues_bind_intr(dev) < 0) {
1591 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1598 /* reset device and renegotiate features if needed */
1600 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1602 struct virtio_hw *hw = eth_dev->data->dev_private;
1603 struct virtio_net_config *config;
1604 struct virtio_net_config local_config;
1605 struct rte_pci_device *pci_dev = NULL;
1608 /* Reset the device although not necessary at startup */
1612 virtio_dev_free_mbufs(eth_dev);
1613 virtio_free_queues(hw);
1616 /* Tell the host we've noticed this device. */
1617 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1619 /* Tell the host we've known how to drive the device. */
1620 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1621 if (virtio_negotiate_features(hw, req_features) < 0)
1624 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1626 if (!hw->virtio_user_dev) {
1627 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1628 rte_eth_copy_pci_info(eth_dev, pci_dev);
1631 /* If host does not support both status and MSI-X then disable LSC */
1632 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1633 hw->use_msix != VIRTIO_MSIX_NONE)
1634 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1636 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1638 /* Setting up rx_header size for the device */
1639 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1640 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1641 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1642 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1644 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1646 /* Copy the permanent MAC address to: virtio_hw */
1647 virtio_get_hwaddr(hw);
1648 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1649 ð_dev->data->mac_addrs[0]);
1651 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1652 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1653 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1655 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1656 config = &local_config;
1658 vtpci_read_dev_config(hw,
1659 offsetof(struct virtio_net_config, mac),
1660 &config->mac, sizeof(config->mac));
1662 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1663 vtpci_read_dev_config(hw,
1664 offsetof(struct virtio_net_config, status),
1665 &config->status, sizeof(config->status));
1668 "VIRTIO_NET_F_STATUS is not supported");
1672 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1673 vtpci_read_dev_config(hw,
1674 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1675 &config->max_virtqueue_pairs,
1676 sizeof(config->max_virtqueue_pairs));
1679 "VIRTIO_NET_F_MQ is not supported");
1680 config->max_virtqueue_pairs = 1;
1683 hw->max_queue_pairs = config->max_virtqueue_pairs;
1685 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1686 vtpci_read_dev_config(hw,
1687 offsetof(struct virtio_net_config, mtu),
1689 sizeof(config->mtu));
1692 * MTU value has already been checked at negotiation
1693 * time, but check again in case it has changed since
1694 * then, which should not happen.
1696 if (config->mtu < ETHER_MIN_MTU) {
1697 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1702 hw->max_mtu = config->mtu;
1703 /* Set initial MTU to maximum one supported by vhost */
1704 eth_dev->data->mtu = config->mtu;
1707 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1708 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1711 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1712 config->max_virtqueue_pairs);
1713 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1715 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1716 config->mac[0], config->mac[1],
1717 config->mac[2], config->mac[3],
1718 config->mac[4], config->mac[5]);
1720 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1721 hw->max_queue_pairs = 1;
1722 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1723 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1726 ret = virtio_alloc_queues(eth_dev);
1730 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1731 if (virtio_configure_intr(eth_dev) < 0) {
1732 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1737 vtpci_reinit_complete(hw);
1740 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1741 eth_dev->data->port_id, pci_dev->id.vendor_id,
1742 pci_dev->id.device_id);
1748 * Remap the PCI device again (IO port map for legacy device and
1749 * memory map for modern device), so that the secondary process
1750 * could have the PCI initiated correctly.
1753 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1757 * We don't have to re-parse the PCI config space, since
1758 * rte_pci_map_device() makes sure the mapped address
1759 * in secondary process would equal to the one mapped in
1760 * the primary process: error will be returned if that
1761 * requirement is not met.
1763 * That said, we could simply reuse all cap pointers
1764 * (such as dev_cfg, common_cfg, etc.) parsed from the
1765 * primary process, which is stored in shared memory.
1767 if (rte_pci_map_device(pci_dev)) {
1768 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1772 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1780 virtio_set_vtpci_ops(struct virtio_hw *hw)
1782 #ifdef RTE_VIRTIO_USER
1783 if (hw->virtio_user_dev)
1784 VTPCI_OPS(hw) = &virtio_user_ops;
1788 VTPCI_OPS(hw) = &modern_ops;
1790 VTPCI_OPS(hw) = &legacy_ops;
1794 * This function is based on probe() function in virtio_pci.c
1795 * It returns 0 on success.
1798 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1800 struct virtio_hw *hw = eth_dev->data->dev_private;
1803 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1805 eth_dev->dev_ops = &virtio_eth_dev_ops;
1807 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1808 if (!hw->virtio_user_dev) {
1809 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1814 virtio_set_vtpci_ops(hw);
1815 set_rxtx_funcs(eth_dev);
1820 /* Allocate memory for storing MAC addresses */
1821 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1822 if (eth_dev->data->mac_addrs == NULL) {
1824 "Failed to allocate %d bytes needed to store MAC addresses",
1825 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1829 hw->port_id = eth_dev->data->port_id;
1830 /* For virtio_user case the hw->virtio_user_dev is populated by
1831 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1833 if (!hw->virtio_user_dev) {
1834 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1839 /* reset device and negotiate default features */
1840 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1847 rte_free(eth_dev->data->mac_addrs);
1852 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1854 PMD_INIT_FUNC_TRACE();
1856 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1859 virtio_dev_stop(eth_dev);
1860 virtio_dev_close(eth_dev);
1862 eth_dev->dev_ops = NULL;
1863 eth_dev->tx_pkt_burst = NULL;
1864 eth_dev->rx_pkt_burst = NULL;
1866 if (eth_dev->device)
1867 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1869 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1874 static int vdpa_check_handler(__rte_unused const char *key,
1875 const char *value, __rte_unused void *opaque)
1877 if (strcmp(value, "1"))
1884 vdpa_mode_selected(struct rte_devargs *devargs)
1886 struct rte_kvargs *kvlist;
1887 const char *key = "vdpa";
1890 if (devargs == NULL)
1893 kvlist = rte_kvargs_parse(devargs->args, NULL);
1897 if (!rte_kvargs_count(kvlist, key))
1900 /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1901 if (rte_kvargs_process(kvlist, key,
1902 vdpa_check_handler, NULL) < 0) {
1908 rte_kvargs_free(kvlist);
1912 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1913 struct rte_pci_device *pci_dev)
1915 if (rte_eal_iopl_init() != 0) {
1916 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1920 /* virtio pmd skips probe if device needs to work in vdpa mode */
1921 if (vdpa_mode_selected(pci_dev->device.devargs))
1924 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1925 eth_virtio_dev_init);
1928 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1930 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1933 static struct rte_pci_driver rte_virtio_pmd = {
1935 .name = "net_virtio",
1937 .id_table = pci_id_virtio_map,
1939 .probe = eth_virtio_pci_probe,
1940 .remove = eth_virtio_pci_remove,
1943 RTE_INIT(rte_virtio_pmd_init)
1945 rte_eal_iopl_init();
1946 rte_pci_register(&rte_virtio_pmd);
1950 rx_offload_enabled(struct virtio_hw *hw)
1952 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1953 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1954 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1958 tx_offload_enabled(struct virtio_hw *hw)
1960 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1961 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1962 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1966 * Configure virtio device
1967 * It returns 0 on success.
1970 virtio_dev_configure(struct rte_eth_dev *dev)
1972 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1973 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1974 struct virtio_hw *hw = dev->data->dev_private;
1975 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
1977 uint64_t rx_offloads = rxmode->offloads;
1978 uint64_t tx_offloads = txmode->offloads;
1979 uint64_t req_features;
1982 PMD_INIT_LOG(DEBUG, "configure");
1983 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1985 if (dev->data->dev_conf.intr_conf.rxq) {
1986 ret = virtio_init_device(dev, hw->req_guest_features);
1991 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
1992 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1994 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1995 DEV_RX_OFFLOAD_TCP_CKSUM))
1996 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1998 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2000 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2001 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2003 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2004 DEV_TX_OFFLOAD_TCP_CKSUM))
2005 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2007 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2009 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2010 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2012 /* if request features changed, reinit the device */
2013 if (req_features != hw->req_guest_features) {
2014 ret = virtio_init_device(dev, req_features);
2019 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2020 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2021 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2023 "rx checksum not available on this host");
2027 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2028 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2029 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2031 "Large Receive Offload not available on this host");
2035 /* start control queue */
2036 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2037 virtio_dev_cq_start(dev);
2039 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2042 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2043 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2045 "vlan filtering not available on this host");
2049 hw->has_tx_offload = tx_offload_enabled(hw);
2050 hw->has_rx_offload = rx_offload_enabled(hw);
2052 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2053 /* Enable vector (0) for Link State Intrerrupt */
2054 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2055 VIRTIO_MSI_NO_VECTOR) {
2056 PMD_DRV_LOG(ERR, "failed to set config vector");
2060 rte_spinlock_init(&hw->state_lock);
2062 hw->use_simple_rx = 1;
2064 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2065 hw->use_inorder_tx = 1;
2066 hw->use_inorder_rx = 1;
2067 hw->use_simple_rx = 0;
2070 if (vtpci_packed_queue(hw)) {
2071 hw->use_simple_rx = 0;
2072 hw->use_inorder_rx = 0;
2073 hw->use_inorder_tx = 0;
2076 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2077 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2078 hw->use_simple_rx = 0;
2081 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2082 hw->use_simple_rx = 0;
2085 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2086 DEV_RX_OFFLOAD_TCP_CKSUM |
2087 DEV_RX_OFFLOAD_TCP_LRO |
2088 DEV_RX_OFFLOAD_VLAN_STRIP))
2089 hw->use_simple_rx = 0;
2098 virtio_dev_start(struct rte_eth_dev *dev)
2100 uint16_t nb_queues, i;
2101 struct virtnet_rx *rxvq;
2102 struct virtnet_tx *txvq __rte_unused;
2103 struct virtio_hw *hw = dev->data->dev_private;
2106 /* Finish the initialization of the queues */
2107 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2108 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2112 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2113 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2118 /* check if lsc interrupt feature is enabled */
2119 if (dev->data->dev_conf.intr_conf.lsc) {
2120 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2121 PMD_DRV_LOG(ERR, "link status not supported by host");
2126 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2127 * in device configure, but it could be unmapped when device is
2130 if (dev->data->dev_conf.intr_conf.lsc ||
2131 dev->data->dev_conf.intr_conf.rxq) {
2132 virtio_intr_disable(dev);
2134 /* Setup interrupt callback */
2135 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2136 rte_intr_callback_register(dev->intr_handle,
2137 virtio_interrupt_handler,
2140 if (virtio_intr_enable(dev) < 0) {
2141 PMD_DRV_LOG(ERR, "interrupt enable failed");
2146 /*Notify the backend
2147 *Otherwise the tap backend might already stop its queue due to fullness.
2148 *vhost backend will have no chance to be waked up
2150 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2151 if (hw->max_queue_pairs > 1) {
2152 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2156 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2158 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2159 rxvq = dev->data->rx_queues[i];
2160 /* Flush the old packets */
2161 virtqueue_rxvq_flush(rxvq->vq);
2162 virtqueue_notify(rxvq->vq);
2165 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2166 txvq = dev->data->tx_queues[i];
2167 virtqueue_notify(txvq->vq);
2170 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2172 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2173 rxvq = dev->data->rx_queues[i];
2174 VIRTQUEUE_DUMP(rxvq->vq);
2177 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2178 txvq = dev->data->tx_queues[i];
2179 VIRTQUEUE_DUMP(txvq->vq);
2182 set_rxtx_funcs(dev);
2185 /* Initialize Link state */
2186 virtio_dev_link_update(dev, 0);
2191 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2193 struct virtio_hw *hw = dev->data->dev_private;
2194 uint16_t nr_vq = virtio_get_nr_vq(hw);
2195 const char *type __rte_unused;
2196 unsigned int i, mbuf_num = 0;
2197 struct virtqueue *vq;
2198 struct rte_mbuf *buf;
2201 if (hw->vqs == NULL)
2204 for (i = 0; i < nr_vq; i++) {
2209 queue_type = virtio_get_queue_type(hw, i);
2210 if (queue_type == VTNET_RQ)
2212 else if (queue_type == VTNET_TQ)
2218 "Before freeing %s[%d] used and unused buf",
2222 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2223 rte_pktmbuf_free(buf);
2228 "After freeing %s[%d] used and unused buf",
2233 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2237 * Stop device: disable interrupt and mark link down
2240 virtio_dev_stop(struct rte_eth_dev *dev)
2242 struct virtio_hw *hw = dev->data->dev_private;
2243 struct rte_eth_link link;
2244 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2246 PMD_INIT_LOG(DEBUG, "stop");
2248 rte_spinlock_lock(&hw->state_lock);
2251 hw->started = false;
2253 if (intr_conf->lsc || intr_conf->rxq) {
2254 virtio_intr_disable(dev);
2256 /* Reset interrupt callback */
2257 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2258 rte_intr_callback_unregister(dev->intr_handle,
2259 virtio_interrupt_handler,
2264 memset(&link, 0, sizeof(link));
2265 rte_eth_linkstatus_set(dev, &link);
2267 rte_spinlock_unlock(&hw->state_lock);
2271 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2273 struct rte_eth_link link;
2275 struct virtio_hw *hw = dev->data->dev_private;
2277 memset(&link, 0, sizeof(link));
2278 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2279 link.link_speed = ETH_SPEED_NUM_10G;
2280 link.link_autoneg = ETH_LINK_FIXED;
2283 link.link_status = ETH_LINK_DOWN;
2284 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2285 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2286 vtpci_read_dev_config(hw,
2287 offsetof(struct virtio_net_config, status),
2288 &status, sizeof(status));
2289 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2290 link.link_status = ETH_LINK_DOWN;
2291 PMD_INIT_LOG(DEBUG, "Port %d is down",
2292 dev->data->port_id);
2294 link.link_status = ETH_LINK_UP;
2295 PMD_INIT_LOG(DEBUG, "Port %d is up",
2296 dev->data->port_id);
2299 link.link_status = ETH_LINK_UP;
2302 return rte_eth_linkstatus_set(dev, &link);
2306 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2308 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2309 struct virtio_hw *hw = dev->data->dev_private;
2310 uint64_t offloads = rxmode->offloads;
2312 if (mask & ETH_VLAN_FILTER_MASK) {
2313 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2314 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2317 "vlan filtering not available on this host");
2323 if (mask & ETH_VLAN_STRIP_MASK)
2324 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2330 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2332 uint64_t tso_mask, host_features;
2333 struct virtio_hw *hw = dev->data->dev_private;
2335 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2337 dev_info->max_rx_queues =
2338 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2339 dev_info->max_tx_queues =
2340 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2341 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2342 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2343 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2345 host_features = VTPCI_OPS(hw)->get_features(hw);
2346 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2347 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2348 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2349 dev_info->rx_offload_capa |=
2350 DEV_RX_OFFLOAD_TCP_CKSUM |
2351 DEV_RX_OFFLOAD_UDP_CKSUM;
2353 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2354 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2355 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2356 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2357 if ((host_features & tso_mask) == tso_mask)
2358 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2360 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2361 DEV_TX_OFFLOAD_VLAN_INSERT;
2362 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2363 dev_info->tx_offload_capa |=
2364 DEV_TX_OFFLOAD_UDP_CKSUM |
2365 DEV_TX_OFFLOAD_TCP_CKSUM;
2367 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2368 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2369 if ((host_features & tso_mask) == tso_mask)
2370 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2374 * It enables testpmd to collect per queue stats.
2377 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2378 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2379 __rte_unused uint8_t is_rx)
2384 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2385 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2386 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2388 RTE_INIT(virtio_init_log)
2390 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2391 if (virtio_logtype_init >= 0)
2392 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2393 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2394 if (virtio_logtype_driver >= 0)
2395 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);