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->vq_packed.ring.desc;
151 struct virtio_pmd_ctrl *result;
158 * Format is enforced in qemu code:
159 * One TX packet for header;
160 * At least one TX packet per argument;
161 * One RX packet for ACK.
163 head = vq->vq_avail_idx;
164 flags = vq->vq_packed.cached_flags;
165 desc[head].addr = cvq->virtio_net_hdr_mem;
166 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
169 if (++vq->vq_avail_idx >= vq->vq_nentries) {
170 vq->vq_avail_idx -= vq->vq_nentries;
171 vq->vq_packed.cached_flags ^=
172 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
175 for (k = 0; k < pkt_num; k++) {
176 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
177 + sizeof(struct virtio_net_ctrl_hdr)
178 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
179 desc[vq->vq_avail_idx].len = dlen[k];
180 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
181 vq->vq_packed.cached_flags;
185 if (++vq->vq_avail_idx >= vq->vq_nentries) {
186 vq->vq_avail_idx -= vq->vq_nentries;
187 vq->vq_packed.cached_flags ^=
188 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
192 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
193 + sizeof(struct virtio_net_ctrl_hdr);
194 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
195 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
196 vq->vq_packed.cached_flags;
199 if (++vq->vq_avail_idx >= vq->vq_nentries) {
200 vq->vq_avail_idx -= vq->vq_nentries;
201 vq->vq_packed.cached_flags ^=
202 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
205 virtio_wmb(vq->hw->weak_barriers);
206 desc[head].flags = VRING_DESC_F_NEXT | flags;
208 virtio_wmb(vq->hw->weak_barriers);
209 virtqueue_notify(vq);
211 /* wait for used descriptors in virtqueue */
212 while (!desc_is_used(&desc[head], vq))
215 virtio_rmb(vq->hw->weak_barriers);
217 /* now get used descriptors */
218 vq->vq_free_cnt += nb_descs;
219 vq->vq_used_cons_idx += nb_descs;
220 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
221 vq->vq_used_cons_idx -= vq->vq_nentries;
222 vq->vq_packed.used_wrap_counter ^= 1;
225 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
226 "vq->vq_avail_idx=%d\n"
227 "vq->vq_used_cons_idx=%d\n"
228 "vq->vq_packed.cached_flags=0x%x\n"
229 "vq->vq_packed.used_wrap_counter=%d\n",
232 vq->vq_used_cons_idx,
233 vq->vq_packed.cached_flags,
234 vq->vq_packed.used_wrap_counter);
236 result = cvq->virtio_net_hdr_mz->addr;
240 static struct virtio_pmd_ctrl *
241 virtio_send_command_split(struct virtnet_ctl *cvq,
242 struct virtio_pmd_ctrl *ctrl,
243 int *dlen, int pkt_num)
245 struct virtio_pmd_ctrl *result;
246 struct virtqueue *vq = cvq->vq;
250 head = vq->vq_desc_head_idx;
253 * Format is enforced in qemu code:
254 * One TX packet for header;
255 * At least one TX packet per argument;
256 * One RX packet for ACK.
258 vq->vq_split.ring.desc[head].flags = VRING_DESC_F_NEXT;
259 vq->vq_split.ring.desc[head].addr = cvq->virtio_net_hdr_mem;
260 vq->vq_split.ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
262 i = vq->vq_split.ring.desc[head].next;
264 for (k = 0; k < pkt_num; k++) {
265 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_NEXT;
266 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
267 + sizeof(struct virtio_net_ctrl_hdr)
268 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
269 vq->vq_split.ring.desc[i].len = dlen[k];
272 i = vq->vq_split.ring.desc[i].next;
275 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_WRITE;
276 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
277 + sizeof(struct virtio_net_ctrl_hdr);
278 vq->vq_split.ring.desc[i].len = sizeof(ctrl->status);
281 vq->vq_desc_head_idx = vq->vq_split.ring.desc[i].next;
283 vq_update_avail_ring(vq, head);
284 vq_update_avail_idx(vq);
286 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
288 virtqueue_notify(vq);
291 while (VIRTQUEUE_NUSED(vq) == 0) {
296 while (VIRTQUEUE_NUSED(vq)) {
297 uint32_t idx, desc_idx, used_idx;
298 struct vring_used_elem *uep;
300 used_idx = (uint32_t)(vq->vq_used_cons_idx
301 & (vq->vq_nentries - 1));
302 uep = &vq->vq_split.ring.used->ring[used_idx];
303 idx = (uint32_t) uep->id;
306 while (vq->vq_split.ring.desc[desc_idx].flags &
308 desc_idx = vq->vq_split.ring.desc[desc_idx].next;
312 vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
313 vq->vq_desc_head_idx = idx;
315 vq->vq_used_cons_idx++;
319 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
320 vq->vq_free_cnt, vq->vq_desc_head_idx);
322 result = cvq->virtio_net_hdr_mz->addr;
327 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
328 int *dlen, int pkt_num)
330 virtio_net_ctrl_ack status = ~0;
331 struct virtio_pmd_ctrl *result;
332 struct virtqueue *vq;
334 ctrl->status = status;
336 if (!cvq || !cvq->vq) {
337 PMD_INIT_LOG(ERR, "Control queue is not supported.");
341 rte_spinlock_lock(&cvq->lock);
344 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
345 "vq->hw->cvq = %p vq = %p",
346 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
348 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
349 rte_spinlock_unlock(&cvq->lock);
353 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
354 sizeof(struct virtio_pmd_ctrl));
356 if (vtpci_packed_queue(vq->hw))
357 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
359 result = virtio_send_command_split(cvq, ctrl, dlen, pkt_num);
361 rte_spinlock_unlock(&cvq->lock);
362 return result->status;
366 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
368 struct virtio_hw *hw = dev->data->dev_private;
369 struct virtio_pmd_ctrl ctrl;
373 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
374 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
375 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
377 dlen[0] = sizeof(uint16_t);
379 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
381 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
382 "failed, this is too late now...");
390 virtio_dev_queue_release(void *queue __rte_unused)
396 virtio_get_nr_vq(struct virtio_hw *hw)
398 uint16_t nr_vq = hw->max_queue_pairs * 2;
400 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
407 virtio_init_vring(struct virtqueue *vq)
409 int size = vq->vq_nentries;
410 uint8_t *ring_mem = vq->vq_ring_virt_mem;
412 PMD_INIT_FUNC_TRACE();
414 memset(ring_mem, 0, vq->vq_ring_size);
416 vq->vq_used_cons_idx = 0;
417 vq->vq_desc_head_idx = 0;
418 vq->vq_avail_idx = 0;
419 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
420 vq->vq_free_cnt = vq->vq_nentries;
421 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
422 if (vtpci_packed_queue(vq->hw)) {
423 vring_init_packed(&vq->vq_packed.ring, ring_mem,
424 VIRTIO_PCI_VRING_ALIGN, size);
425 vring_desc_init_packed(vq, size);
427 struct vring *vr = &vq->vq_split.ring;
429 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
430 vring_desc_init_split(vr->desc, size);
433 * Disable device(host) interrupting guest
435 virtqueue_disable_intr(vq);
439 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
441 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
442 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
443 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
444 unsigned int vq_size, size;
445 struct virtio_hw *hw = dev->data->dev_private;
446 struct virtnet_rx *rxvq = NULL;
447 struct virtnet_tx *txvq = NULL;
448 struct virtnet_ctl *cvq = NULL;
449 struct virtqueue *vq;
450 size_t sz_hdr_mz = 0;
451 void *sw_ring = NULL;
452 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
454 int numa_node = dev->device->numa_node;
456 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
457 vtpci_queue_idx, numa_node);
460 * Read the virtqueue size from the Queue Size field
461 * Always power of 2 and if 0 virtqueue does not exist
463 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
464 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
466 PMD_INIT_LOG(ERR, "virtqueue does not exist");
470 if (!rte_is_power_of_2(vq_size)) {
471 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
475 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
476 dev->data->port_id, vtpci_queue_idx);
478 size = RTE_ALIGN_CEIL(sizeof(*vq) +
479 vq_size * sizeof(struct vq_desc_extra),
480 RTE_CACHE_LINE_SIZE);
481 if (queue_type == VTNET_TQ) {
483 * For each xmit packet, allocate a virtio_net_hdr
484 * and indirect ring elements
486 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
487 } else if (queue_type == VTNET_CQ) {
488 /* Allocate a page for control vq command, data and status */
489 sz_hdr_mz = PAGE_SIZE;
492 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
495 PMD_INIT_LOG(ERR, "can not allocate vq");
498 hw->vqs[vtpci_queue_idx] = vq;
501 vq->vq_queue_index = vtpci_queue_idx;
502 vq->vq_nentries = vq_size;
503 if (vtpci_packed_queue(hw)) {
504 vq->vq_packed.used_wrap_counter = 1;
505 vq->vq_packed.cached_flags = VRING_DESC_F_AVAIL(1);
506 vq->vq_packed.event_flags_shadow = 0;
507 if (queue_type == VTNET_RQ)
508 vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
512 * Reserve a memzone for vring elements
514 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
515 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
516 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
517 size, vq->vq_ring_size);
519 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
520 numa_node, RTE_MEMZONE_IOVA_CONTIG,
521 VIRTIO_PCI_VRING_ALIGN);
523 if (rte_errno == EEXIST)
524 mz = rte_memzone_lookup(vq_name);
531 memset(mz->addr, 0, mz->len);
533 vq->vq_ring_mem = mz->iova;
534 vq->vq_ring_virt_mem = mz->addr;
535 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
537 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
538 (uint64_t)(uintptr_t)mz->addr);
540 virtio_init_vring(vq);
543 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
544 dev->data->port_id, vtpci_queue_idx);
545 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
546 numa_node, RTE_MEMZONE_IOVA_CONTIG,
547 RTE_CACHE_LINE_SIZE);
548 if (hdr_mz == NULL) {
549 if (rte_errno == EEXIST)
550 hdr_mz = rte_memzone_lookup(vq_hdr_name);
551 if (hdr_mz == NULL) {
558 if (queue_type == VTNET_RQ) {
559 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
560 sizeof(vq->sw_ring[0]);
562 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
563 RTE_CACHE_LINE_SIZE, numa_node);
565 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
570 vq->sw_ring = sw_ring;
573 rxvq->port_id = dev->data->port_id;
575 } else if (queue_type == VTNET_TQ) {
578 txvq->port_id = dev->data->port_id;
580 txvq->virtio_net_hdr_mz = hdr_mz;
581 txvq->virtio_net_hdr_mem = hdr_mz->iova;
582 } else if (queue_type == VTNET_CQ) {
586 cvq->virtio_net_hdr_mz = hdr_mz;
587 cvq->virtio_net_hdr_mem = hdr_mz->iova;
588 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
593 /* For virtio_user case (that is when hw->dev is NULL), we use
594 * virtual address. And we need properly set _offset_, please see
595 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
597 if (!hw->virtio_user_dev)
598 vq->offset = offsetof(struct rte_mbuf, buf_iova);
600 vq->vq_ring_mem = (uintptr_t)mz->addr;
601 vq->offset = offsetof(struct rte_mbuf, buf_addr);
602 if (queue_type == VTNET_TQ)
603 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
604 else if (queue_type == VTNET_CQ)
605 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
608 if (queue_type == VTNET_TQ) {
609 struct virtio_tx_region *txr;
613 memset(txr, 0, vq_size * sizeof(*txr));
614 for (i = 0; i < vq_size; i++) {
615 struct vring_desc *start_dp = txr[i].tx_indir;
617 /* first indirect descriptor is always the tx header */
618 if (!vtpci_packed_queue(hw)) {
619 vring_desc_init_split(start_dp,
620 RTE_DIM(txr[i].tx_indir));
621 start_dp->addr = txvq->virtio_net_hdr_mem
623 + offsetof(struct virtio_tx_region,
625 start_dp->len = hw->vtnet_hdr_size;
626 start_dp->flags = VRING_DESC_F_NEXT;
631 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
632 PMD_INIT_LOG(ERR, "setup_queue failed");
640 rte_memzone_free(hdr_mz);
641 rte_memzone_free(mz);
648 virtio_free_queues(struct virtio_hw *hw)
650 uint16_t nr_vq = virtio_get_nr_vq(hw);
651 struct virtqueue *vq;
658 for (i = 0; i < nr_vq; i++) {
663 queue_type = virtio_get_queue_type(hw, i);
664 if (queue_type == VTNET_RQ) {
665 rte_free(vq->sw_ring);
666 rte_memzone_free(vq->rxq.mz);
667 } else if (queue_type == VTNET_TQ) {
668 rte_memzone_free(vq->txq.mz);
669 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
671 rte_memzone_free(vq->cq.mz);
672 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
684 virtio_alloc_queues(struct rte_eth_dev *dev)
686 struct virtio_hw *hw = dev->data->dev_private;
687 uint16_t nr_vq = virtio_get_nr_vq(hw);
691 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
693 PMD_INIT_LOG(ERR, "failed to allocate vqs");
697 for (i = 0; i < nr_vq; i++) {
698 ret = virtio_init_queue(dev, i);
700 virtio_free_queues(hw);
708 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
711 virtio_dev_close(struct rte_eth_dev *dev)
713 struct virtio_hw *hw = dev->data->dev_private;
714 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
716 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
723 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
724 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
726 virtio_queues_unbind_intr(dev);
728 if (intr_conf->lsc || intr_conf->rxq) {
729 virtio_intr_disable(dev);
730 rte_intr_efd_disable(dev->intr_handle);
731 rte_free(dev->intr_handle->intr_vec);
732 dev->intr_handle->intr_vec = NULL;
736 virtio_dev_free_mbufs(dev);
737 virtio_free_queues(hw);
741 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
743 struct virtio_hw *hw = dev->data->dev_private;
744 struct virtio_pmd_ctrl ctrl;
748 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
749 PMD_INIT_LOG(INFO, "host does not support rx control");
753 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
754 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
758 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
760 PMD_INIT_LOG(ERR, "Failed to enable promisc");
764 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
766 struct virtio_hw *hw = dev->data->dev_private;
767 struct virtio_pmd_ctrl ctrl;
771 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
772 PMD_INIT_LOG(INFO, "host does not support rx control");
776 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
777 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
781 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
783 PMD_INIT_LOG(ERR, "Failed to disable promisc");
787 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
789 struct virtio_hw *hw = dev->data->dev_private;
790 struct virtio_pmd_ctrl ctrl;
794 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
795 PMD_INIT_LOG(INFO, "host does not support rx control");
799 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
800 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
804 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
806 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
810 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
812 struct virtio_hw *hw = dev->data->dev_private;
813 struct virtio_pmd_ctrl ctrl;
817 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
818 PMD_INIT_LOG(INFO, "host does not support rx control");
822 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
823 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
827 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
829 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
832 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
834 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
836 struct virtio_hw *hw = dev->data->dev_private;
837 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
839 uint32_t frame_size = mtu + ether_hdr_len;
840 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
842 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
844 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
845 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
846 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
853 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
855 struct virtio_hw *hw = dev->data->dev_private;
856 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
857 struct virtqueue *vq = rxvq->vq;
859 virtqueue_enable_intr(vq);
860 virtio_mb(hw->weak_barriers);
865 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
867 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
868 struct virtqueue *vq = rxvq->vq;
870 virtqueue_disable_intr(vq);
875 * dev_ops for virtio, bare necessities for basic operation
877 static const struct eth_dev_ops virtio_eth_dev_ops = {
878 .dev_configure = virtio_dev_configure,
879 .dev_start = virtio_dev_start,
880 .dev_stop = virtio_dev_stop,
881 .dev_close = virtio_dev_close,
882 .promiscuous_enable = virtio_dev_promiscuous_enable,
883 .promiscuous_disable = virtio_dev_promiscuous_disable,
884 .allmulticast_enable = virtio_dev_allmulticast_enable,
885 .allmulticast_disable = virtio_dev_allmulticast_disable,
886 .mtu_set = virtio_mtu_set,
887 .dev_infos_get = virtio_dev_info_get,
888 .stats_get = virtio_dev_stats_get,
889 .xstats_get = virtio_dev_xstats_get,
890 .xstats_get_names = virtio_dev_xstats_get_names,
891 .stats_reset = virtio_dev_stats_reset,
892 .xstats_reset = virtio_dev_stats_reset,
893 .link_update = virtio_dev_link_update,
894 .vlan_offload_set = virtio_dev_vlan_offload_set,
895 .rx_queue_setup = virtio_dev_rx_queue_setup,
896 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
897 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
898 .rx_queue_release = virtio_dev_queue_release,
899 .rx_descriptor_done = virtio_dev_rx_queue_done,
900 .tx_queue_setup = virtio_dev_tx_queue_setup,
901 .tx_queue_release = virtio_dev_queue_release,
902 /* collect stats per queue */
903 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
904 .vlan_filter_set = virtio_vlan_filter_set,
905 .mac_addr_add = virtio_mac_addr_add,
906 .mac_addr_remove = virtio_mac_addr_remove,
907 .mac_addr_set = virtio_mac_addr_set,
911 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
915 for (i = 0; i < dev->data->nb_tx_queues; i++) {
916 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
920 stats->opackets += txvq->stats.packets;
921 stats->obytes += txvq->stats.bytes;
922 stats->oerrors += txvq->stats.errors;
924 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
925 stats->q_opackets[i] = txvq->stats.packets;
926 stats->q_obytes[i] = txvq->stats.bytes;
930 for (i = 0; i < dev->data->nb_rx_queues; i++) {
931 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
935 stats->ipackets += rxvq->stats.packets;
936 stats->ibytes += rxvq->stats.bytes;
937 stats->ierrors += rxvq->stats.errors;
939 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
940 stats->q_ipackets[i] = rxvq->stats.packets;
941 stats->q_ibytes[i] = rxvq->stats.bytes;
945 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
948 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
949 struct rte_eth_xstat_name *xstats_names,
950 __rte_unused unsigned limit)
956 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
957 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
959 if (xstats_names != NULL) {
960 /* Note: limit checked in rte_eth_xstats_names() */
962 for (i = 0; i < dev->data->nb_rx_queues; i++) {
963 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
966 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
967 snprintf(xstats_names[count].name,
968 sizeof(xstats_names[count].name),
970 rte_virtio_rxq_stat_strings[t].name);
975 for (i = 0; i < dev->data->nb_tx_queues; i++) {
976 struct virtnet_tx *txvq = dev->data->tx_queues[i];
979 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
980 snprintf(xstats_names[count].name,
981 sizeof(xstats_names[count].name),
983 rte_virtio_txq_stat_strings[t].name);
993 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
999 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1000 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1005 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1006 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1013 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1014 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1015 rte_virtio_rxq_stat_strings[t].offset);
1016 xstats[count].id = count;
1021 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1022 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1029 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1030 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1031 rte_virtio_txq_stat_strings[t].offset);
1032 xstats[count].id = count;
1041 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1043 virtio_update_stats(dev, stats);
1049 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1053 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1054 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1058 txvq->stats.packets = 0;
1059 txvq->stats.bytes = 0;
1060 txvq->stats.errors = 0;
1061 txvq->stats.multicast = 0;
1062 txvq->stats.broadcast = 0;
1063 memset(txvq->stats.size_bins, 0,
1064 sizeof(txvq->stats.size_bins[0]) * 8);
1067 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1068 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1072 rxvq->stats.packets = 0;
1073 rxvq->stats.bytes = 0;
1074 rxvq->stats.errors = 0;
1075 rxvq->stats.multicast = 0;
1076 rxvq->stats.broadcast = 0;
1077 memset(rxvq->stats.size_bins, 0,
1078 sizeof(rxvq->stats.size_bins[0]) * 8);
1083 virtio_set_hwaddr(struct virtio_hw *hw)
1085 vtpci_write_dev_config(hw,
1086 offsetof(struct virtio_net_config, mac),
1087 &hw->mac_addr, ETHER_ADDR_LEN);
1091 virtio_get_hwaddr(struct virtio_hw *hw)
1093 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1094 vtpci_read_dev_config(hw,
1095 offsetof(struct virtio_net_config, mac),
1096 &hw->mac_addr, ETHER_ADDR_LEN);
1098 eth_random_addr(&hw->mac_addr[0]);
1099 virtio_set_hwaddr(hw);
1104 virtio_mac_table_set(struct virtio_hw *hw,
1105 const struct virtio_net_ctrl_mac *uc,
1106 const struct virtio_net_ctrl_mac *mc)
1108 struct virtio_pmd_ctrl ctrl;
1111 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1112 PMD_DRV_LOG(INFO, "host does not support mac table");
1116 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1117 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1119 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1120 memcpy(ctrl.data, uc, len[0]);
1122 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1123 memcpy(ctrl.data + len[0], mc, len[1]);
1125 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1127 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1132 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1133 uint32_t index, uint32_t vmdq __rte_unused)
1135 struct virtio_hw *hw = dev->data->dev_private;
1136 const struct ether_addr *addrs = dev->data->mac_addrs;
1138 struct virtio_net_ctrl_mac *uc, *mc;
1140 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1141 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1145 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1147 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1150 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1151 const struct ether_addr *addr
1152 = (i == index) ? mac_addr : addrs + i;
1153 struct virtio_net_ctrl_mac *tbl
1154 = is_multicast_ether_addr(addr) ? mc : uc;
1156 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1159 return virtio_mac_table_set(hw, uc, mc);
1163 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1165 struct virtio_hw *hw = dev->data->dev_private;
1166 struct ether_addr *addrs = dev->data->mac_addrs;
1167 struct virtio_net_ctrl_mac *uc, *mc;
1170 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1171 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1175 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1177 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1180 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1181 struct virtio_net_ctrl_mac *tbl;
1183 if (i == index || is_zero_ether_addr(addrs + i))
1186 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1187 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1190 virtio_mac_table_set(hw, uc, mc);
1194 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1196 struct virtio_hw *hw = dev->data->dev_private;
1198 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1200 /* Use atomic update if available */
1201 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1202 struct virtio_pmd_ctrl ctrl;
1203 int len = ETHER_ADDR_LEN;
1205 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1206 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1208 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1209 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1212 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1215 virtio_set_hwaddr(hw);
1220 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1222 struct virtio_hw *hw = dev->data->dev_private;
1223 struct virtio_pmd_ctrl ctrl;
1226 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1229 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1230 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1231 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1232 len = sizeof(vlan_id);
1234 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1238 virtio_intr_enable(struct rte_eth_dev *dev)
1240 struct virtio_hw *hw = dev->data->dev_private;
1242 if (rte_intr_enable(dev->intr_handle) < 0)
1245 if (!hw->virtio_user_dev)
1246 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1252 virtio_intr_disable(struct rte_eth_dev *dev)
1254 struct virtio_hw *hw = dev->data->dev_private;
1256 if (rte_intr_disable(dev->intr_handle) < 0)
1259 if (!hw->virtio_user_dev)
1260 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1266 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1268 uint64_t host_features;
1270 /* Prepare guest_features: feature that driver wants to support */
1271 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1274 /* Read device(host) feature bits */
1275 host_features = VTPCI_OPS(hw)->get_features(hw);
1276 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1279 /* If supported, ensure MTU value is valid before acknowledging it. */
1280 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1281 struct virtio_net_config config;
1283 vtpci_read_dev_config(hw,
1284 offsetof(struct virtio_net_config, mtu),
1285 &config.mtu, sizeof(config.mtu));
1287 if (config.mtu < ETHER_MIN_MTU)
1288 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1292 * Negotiate features: Subset of device feature bits are written back
1293 * guest feature bits.
1295 hw->guest_features = req_features;
1296 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1297 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1298 hw->guest_features);
1301 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1303 "VIRTIO_F_VERSION_1 features is not enabled.");
1306 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1307 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1309 "failed to set FEATURES_OK status!");
1314 hw->req_guest_features = req_features;
1320 virtio_dev_pause(struct rte_eth_dev *dev)
1322 struct virtio_hw *hw = dev->data->dev_private;
1324 rte_spinlock_lock(&hw->state_lock);
1326 if (hw->started == 0) {
1327 /* Device is just stopped. */
1328 rte_spinlock_unlock(&hw->state_lock);
1333 * Prevent the worker threads from touching queues to avoid contention,
1334 * 1 ms should be enough for the ongoing Tx function to finish.
1341 * Recover hw state to let the worker threads continue.
1344 virtio_dev_resume(struct rte_eth_dev *dev)
1346 struct virtio_hw *hw = dev->data->dev_private;
1349 rte_spinlock_unlock(&hw->state_lock);
1353 * Should be called only after device is paused.
1356 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1359 struct virtio_hw *hw = dev->data->dev_private;
1360 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1363 hw->inject_pkts = tx_pkts;
1364 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1365 hw->inject_pkts = NULL;
1371 virtio_notify_peers(struct rte_eth_dev *dev)
1373 struct virtio_hw *hw = dev->data->dev_private;
1374 struct virtnet_rx *rxvq;
1375 struct rte_mbuf *rarp_mbuf;
1377 if (!dev->data->rx_queues)
1380 rxvq = dev->data->rx_queues[0];
1384 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1385 (struct ether_addr *)hw->mac_addr);
1386 if (rarp_mbuf == NULL) {
1387 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1391 /* If virtio port just stopped, no need to send RARP */
1392 if (virtio_dev_pause(dev) < 0) {
1393 rte_pktmbuf_free(rarp_mbuf);
1397 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1398 virtio_dev_resume(dev);
1402 virtio_ack_link_announce(struct rte_eth_dev *dev)
1404 struct virtio_hw *hw = dev->data->dev_private;
1405 struct virtio_pmd_ctrl ctrl;
1407 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1408 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1410 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1414 * Process virtio config changed interrupt. Call the callback
1415 * if link state changed, generate gratuitous RARP packet if
1416 * the status indicates an ANNOUNCE.
1419 virtio_interrupt_handler(void *param)
1421 struct rte_eth_dev *dev = param;
1422 struct virtio_hw *hw = dev->data->dev_private;
1426 /* Read interrupt status which clears interrupt */
1427 isr = vtpci_isr(hw);
1428 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1430 if (virtio_intr_enable(dev) < 0)
1431 PMD_DRV_LOG(ERR, "interrupt enable failed");
1433 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1434 if (virtio_dev_link_update(dev, 0) == 0)
1435 _rte_eth_dev_callback_process(dev,
1436 RTE_ETH_EVENT_INTR_LSC,
1439 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1440 vtpci_read_dev_config(hw,
1441 offsetof(struct virtio_net_config, status),
1442 &status, sizeof(status));
1443 if (status & VIRTIO_NET_S_ANNOUNCE) {
1444 virtio_notify_peers(dev);
1446 virtio_ack_link_announce(dev);
1452 /* set rx and tx handlers according to what is supported */
1454 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1456 struct virtio_hw *hw = eth_dev->data->dev_private;
1458 if (vtpci_packed_queue(hw)) {
1460 "virtio: using packed ring %s Tx path on port %u",
1461 hw->use_inorder_tx ? "inorder" : "standard",
1462 eth_dev->data->port_id);
1463 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1465 if (hw->use_inorder_tx) {
1466 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1467 eth_dev->data->port_id);
1468 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1470 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1471 eth_dev->data->port_id);
1472 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1476 if (vtpci_packed_queue(hw)) {
1477 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1479 "virtio: using packed ring mergeable buffer Rx path on port %u",
1480 eth_dev->data->port_id);
1481 eth_dev->rx_pkt_burst =
1482 &virtio_recv_mergeable_pkts_packed;
1485 "virtio: using packed ring standard Rx path on port %u",
1486 eth_dev->data->port_id);
1487 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1490 if (hw->use_simple_rx) {
1491 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1492 eth_dev->data->port_id);
1493 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1494 } else if (hw->use_inorder_rx) {
1496 "virtio: using inorder Rx path on port %u",
1497 eth_dev->data->port_id);
1498 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1499 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1501 "virtio: using mergeable buffer Rx path on port %u",
1502 eth_dev->data->port_id);
1503 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1505 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1506 eth_dev->data->port_id);
1507 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1513 /* Only support 1:1 queue/interrupt mapping so far.
1514 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1515 * interrupt vectors (<N+1).
1518 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1521 struct virtio_hw *hw = dev->data->dev_private;
1523 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1524 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1525 dev->intr_handle->intr_vec[i] = i + 1;
1526 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1527 VIRTIO_MSI_NO_VECTOR) {
1528 PMD_DRV_LOG(ERR, "failed to set queue vector");
1537 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1540 struct virtio_hw *hw = dev->data->dev_private;
1542 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1543 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1544 VTPCI_OPS(hw)->set_queue_irq(hw,
1545 hw->vqs[i * VTNET_CQ],
1546 VIRTIO_MSI_NO_VECTOR);
1550 virtio_configure_intr(struct rte_eth_dev *dev)
1552 struct virtio_hw *hw = dev->data->dev_private;
1554 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1555 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1559 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1560 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1564 if (!dev->intr_handle->intr_vec) {
1565 dev->intr_handle->intr_vec =
1566 rte_zmalloc("intr_vec",
1567 hw->max_queue_pairs * sizeof(int), 0);
1568 if (!dev->intr_handle->intr_vec) {
1569 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1570 hw->max_queue_pairs);
1575 /* Re-register callback to update max_intr */
1576 rte_intr_callback_unregister(dev->intr_handle,
1577 virtio_interrupt_handler,
1579 rte_intr_callback_register(dev->intr_handle,
1580 virtio_interrupt_handler,
1583 /* DO NOT try to remove this! This function will enable msix, or QEMU
1584 * will encounter SIGSEGV when DRIVER_OK is sent.
1585 * And for legacy devices, this should be done before queue/vec binding
1586 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1587 * (22) will be ignored.
1589 if (virtio_intr_enable(dev) < 0) {
1590 PMD_DRV_LOG(ERR, "interrupt enable failed");
1594 if (virtio_queues_bind_intr(dev) < 0) {
1595 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1602 /* reset device and renegotiate features if needed */
1604 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1606 struct virtio_hw *hw = eth_dev->data->dev_private;
1607 struct virtio_net_config *config;
1608 struct virtio_net_config local_config;
1609 struct rte_pci_device *pci_dev = NULL;
1612 /* Reset the device although not necessary at startup */
1616 virtio_dev_free_mbufs(eth_dev);
1617 virtio_free_queues(hw);
1620 /* Tell the host we've noticed this device. */
1621 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1623 /* Tell the host we've known how to drive the device. */
1624 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1625 if (virtio_negotiate_features(hw, req_features) < 0)
1628 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1630 if (!hw->virtio_user_dev) {
1631 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1632 rte_eth_copy_pci_info(eth_dev, pci_dev);
1635 /* If host does not support both status and MSI-X then disable LSC */
1636 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1637 hw->use_msix != VIRTIO_MSIX_NONE)
1638 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1640 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1642 /* Setting up rx_header size for the device */
1643 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1644 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1645 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1646 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1648 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1650 /* Copy the permanent MAC address to: virtio_hw */
1651 virtio_get_hwaddr(hw);
1652 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1653 ð_dev->data->mac_addrs[0]);
1655 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1656 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1657 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1659 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1660 config = &local_config;
1662 vtpci_read_dev_config(hw,
1663 offsetof(struct virtio_net_config, mac),
1664 &config->mac, sizeof(config->mac));
1666 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1667 vtpci_read_dev_config(hw,
1668 offsetof(struct virtio_net_config, status),
1669 &config->status, sizeof(config->status));
1672 "VIRTIO_NET_F_STATUS is not supported");
1676 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1677 vtpci_read_dev_config(hw,
1678 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1679 &config->max_virtqueue_pairs,
1680 sizeof(config->max_virtqueue_pairs));
1683 "VIRTIO_NET_F_MQ is not supported");
1684 config->max_virtqueue_pairs = 1;
1687 hw->max_queue_pairs = config->max_virtqueue_pairs;
1689 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1690 vtpci_read_dev_config(hw,
1691 offsetof(struct virtio_net_config, mtu),
1693 sizeof(config->mtu));
1696 * MTU value has already been checked at negotiation
1697 * time, but check again in case it has changed since
1698 * then, which should not happen.
1700 if (config->mtu < ETHER_MIN_MTU) {
1701 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1706 hw->max_mtu = config->mtu;
1707 /* Set initial MTU to maximum one supported by vhost */
1708 eth_dev->data->mtu = config->mtu;
1711 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1712 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1715 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1716 config->max_virtqueue_pairs);
1717 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1719 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1720 config->mac[0], config->mac[1],
1721 config->mac[2], config->mac[3],
1722 config->mac[4], config->mac[5]);
1724 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1725 hw->max_queue_pairs = 1;
1726 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1727 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1730 ret = virtio_alloc_queues(eth_dev);
1734 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1735 if (virtio_configure_intr(eth_dev) < 0) {
1736 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1741 vtpci_reinit_complete(hw);
1744 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1745 eth_dev->data->port_id, pci_dev->id.vendor_id,
1746 pci_dev->id.device_id);
1752 * Remap the PCI device again (IO port map for legacy device and
1753 * memory map for modern device), so that the secondary process
1754 * could have the PCI initiated correctly.
1757 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1761 * We don't have to re-parse the PCI config space, since
1762 * rte_pci_map_device() makes sure the mapped address
1763 * in secondary process would equal to the one mapped in
1764 * the primary process: error will be returned if that
1765 * requirement is not met.
1767 * That said, we could simply reuse all cap pointers
1768 * (such as dev_cfg, common_cfg, etc.) parsed from the
1769 * primary process, which is stored in shared memory.
1771 if (rte_pci_map_device(pci_dev)) {
1772 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1776 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1784 virtio_set_vtpci_ops(struct virtio_hw *hw)
1786 #ifdef RTE_VIRTIO_USER
1787 if (hw->virtio_user_dev)
1788 VTPCI_OPS(hw) = &virtio_user_ops;
1792 VTPCI_OPS(hw) = &modern_ops;
1794 VTPCI_OPS(hw) = &legacy_ops;
1798 * This function is based on probe() function in virtio_pci.c
1799 * It returns 0 on success.
1802 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1804 struct virtio_hw *hw = eth_dev->data->dev_private;
1807 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1809 eth_dev->dev_ops = &virtio_eth_dev_ops;
1811 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1812 if (!hw->virtio_user_dev) {
1813 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1818 virtio_set_vtpci_ops(hw);
1819 set_rxtx_funcs(eth_dev);
1824 /* Allocate memory for storing MAC addresses */
1825 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1826 if (eth_dev->data->mac_addrs == NULL) {
1828 "Failed to allocate %d bytes needed to store MAC addresses",
1829 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1833 hw->port_id = eth_dev->data->port_id;
1834 /* For virtio_user case the hw->virtio_user_dev is populated by
1835 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1837 if (!hw->virtio_user_dev) {
1838 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1843 /* reset device and negotiate default features */
1844 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1851 rte_free(eth_dev->data->mac_addrs);
1856 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1858 PMD_INIT_FUNC_TRACE();
1860 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1863 virtio_dev_stop(eth_dev);
1864 virtio_dev_close(eth_dev);
1866 eth_dev->dev_ops = NULL;
1867 eth_dev->tx_pkt_burst = NULL;
1868 eth_dev->rx_pkt_burst = NULL;
1870 if (eth_dev->device)
1871 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1873 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1878 static int vdpa_check_handler(__rte_unused const char *key,
1879 const char *value, __rte_unused void *opaque)
1881 if (strcmp(value, "1"))
1888 vdpa_mode_selected(struct rte_devargs *devargs)
1890 struct rte_kvargs *kvlist;
1891 const char *key = "vdpa";
1894 if (devargs == NULL)
1897 kvlist = rte_kvargs_parse(devargs->args, NULL);
1901 if (!rte_kvargs_count(kvlist, key))
1904 /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1905 if (rte_kvargs_process(kvlist, key,
1906 vdpa_check_handler, NULL) < 0) {
1912 rte_kvargs_free(kvlist);
1916 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1917 struct rte_pci_device *pci_dev)
1919 if (rte_eal_iopl_init() != 0) {
1920 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1924 /* virtio pmd skips probe if device needs to work in vdpa mode */
1925 if (vdpa_mode_selected(pci_dev->device.devargs))
1928 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1929 eth_virtio_dev_init);
1932 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1934 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1937 static struct rte_pci_driver rte_virtio_pmd = {
1939 .name = "net_virtio",
1941 .id_table = pci_id_virtio_map,
1943 .probe = eth_virtio_pci_probe,
1944 .remove = eth_virtio_pci_remove,
1947 RTE_INIT(rte_virtio_pmd_init)
1949 rte_eal_iopl_init();
1950 rte_pci_register(&rte_virtio_pmd);
1954 rx_offload_enabled(struct virtio_hw *hw)
1956 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1957 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1958 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1962 tx_offload_enabled(struct virtio_hw *hw)
1964 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1965 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1966 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1970 * Configure virtio device
1971 * It returns 0 on success.
1974 virtio_dev_configure(struct rte_eth_dev *dev)
1976 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1977 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1978 struct virtio_hw *hw = dev->data->dev_private;
1979 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
1981 uint64_t rx_offloads = rxmode->offloads;
1982 uint64_t tx_offloads = txmode->offloads;
1983 uint64_t req_features;
1986 PMD_INIT_LOG(DEBUG, "configure");
1987 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1989 if (dev->data->dev_conf.intr_conf.rxq) {
1990 ret = virtio_init_device(dev, hw->req_guest_features);
1995 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
1996 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1998 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1999 DEV_RX_OFFLOAD_TCP_CKSUM))
2000 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2002 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2004 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2005 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2007 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2008 DEV_TX_OFFLOAD_TCP_CKSUM))
2009 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2011 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2013 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2014 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2016 /* if request features changed, reinit the device */
2017 if (req_features != hw->req_guest_features) {
2018 ret = virtio_init_device(dev, req_features);
2023 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2024 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2025 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2027 "rx checksum not available on this host");
2031 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2032 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2033 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2035 "Large Receive Offload not available on this host");
2039 /* start control queue */
2040 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2041 virtio_dev_cq_start(dev);
2043 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2046 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2047 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2049 "vlan filtering not available on this host");
2053 hw->has_tx_offload = tx_offload_enabled(hw);
2054 hw->has_rx_offload = rx_offload_enabled(hw);
2056 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2057 /* Enable vector (0) for Link State Intrerrupt */
2058 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2059 VIRTIO_MSI_NO_VECTOR) {
2060 PMD_DRV_LOG(ERR, "failed to set config vector");
2064 rte_spinlock_init(&hw->state_lock);
2066 hw->use_simple_rx = 1;
2068 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2069 hw->use_inorder_tx = 1;
2070 hw->use_inorder_rx = 1;
2071 hw->use_simple_rx = 0;
2074 if (vtpci_packed_queue(hw)) {
2075 hw->use_simple_rx = 0;
2076 hw->use_inorder_rx = 0;
2079 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2080 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2081 hw->use_simple_rx = 0;
2084 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2085 hw->use_simple_rx = 0;
2088 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2089 DEV_RX_OFFLOAD_TCP_CKSUM |
2090 DEV_RX_OFFLOAD_TCP_LRO |
2091 DEV_RX_OFFLOAD_VLAN_STRIP))
2092 hw->use_simple_rx = 0;
2101 virtio_dev_start(struct rte_eth_dev *dev)
2103 uint16_t nb_queues, i;
2104 struct virtnet_rx *rxvq;
2105 struct virtnet_tx *txvq __rte_unused;
2106 struct virtio_hw *hw = dev->data->dev_private;
2109 /* Finish the initialization of the queues */
2110 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2111 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2115 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2116 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2121 /* check if lsc interrupt feature is enabled */
2122 if (dev->data->dev_conf.intr_conf.lsc) {
2123 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2124 PMD_DRV_LOG(ERR, "link status not supported by host");
2129 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2130 * in device configure, but it could be unmapped when device is
2133 if (dev->data->dev_conf.intr_conf.lsc ||
2134 dev->data->dev_conf.intr_conf.rxq) {
2135 virtio_intr_disable(dev);
2137 /* Setup interrupt callback */
2138 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2139 rte_intr_callback_register(dev->intr_handle,
2140 virtio_interrupt_handler,
2143 if (virtio_intr_enable(dev) < 0) {
2144 PMD_DRV_LOG(ERR, "interrupt enable failed");
2149 /*Notify the backend
2150 *Otherwise the tap backend might already stop its queue due to fullness.
2151 *vhost backend will have no chance to be waked up
2153 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2154 if (hw->max_queue_pairs > 1) {
2155 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2159 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2161 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2162 rxvq = dev->data->rx_queues[i];
2163 /* Flush the old packets */
2164 virtqueue_rxvq_flush(rxvq->vq);
2165 virtqueue_notify(rxvq->vq);
2168 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2169 txvq = dev->data->tx_queues[i];
2170 virtqueue_notify(txvq->vq);
2173 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2175 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2176 rxvq = dev->data->rx_queues[i];
2177 VIRTQUEUE_DUMP(rxvq->vq);
2180 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2181 txvq = dev->data->tx_queues[i];
2182 VIRTQUEUE_DUMP(txvq->vq);
2185 set_rxtx_funcs(dev);
2188 /* Initialize Link state */
2189 virtio_dev_link_update(dev, 0);
2194 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2196 struct virtio_hw *hw = dev->data->dev_private;
2197 uint16_t nr_vq = virtio_get_nr_vq(hw);
2198 const char *type __rte_unused;
2199 unsigned int i, mbuf_num = 0;
2200 struct virtqueue *vq;
2201 struct rte_mbuf *buf;
2204 if (hw->vqs == NULL)
2207 for (i = 0; i < nr_vq; i++) {
2212 queue_type = virtio_get_queue_type(hw, i);
2213 if (queue_type == VTNET_RQ)
2215 else if (queue_type == VTNET_TQ)
2221 "Before freeing %s[%d] used and unused buf",
2225 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2226 rte_pktmbuf_free(buf);
2231 "After freeing %s[%d] used and unused buf",
2236 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2240 * Stop device: disable interrupt and mark link down
2243 virtio_dev_stop(struct rte_eth_dev *dev)
2245 struct virtio_hw *hw = dev->data->dev_private;
2246 struct rte_eth_link link;
2247 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2249 PMD_INIT_LOG(DEBUG, "stop");
2251 rte_spinlock_lock(&hw->state_lock);
2254 hw->started = false;
2256 if (intr_conf->lsc || intr_conf->rxq) {
2257 virtio_intr_disable(dev);
2259 /* Reset interrupt callback */
2260 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2261 rte_intr_callback_unregister(dev->intr_handle,
2262 virtio_interrupt_handler,
2267 memset(&link, 0, sizeof(link));
2268 rte_eth_linkstatus_set(dev, &link);
2270 rte_spinlock_unlock(&hw->state_lock);
2274 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2276 struct rte_eth_link link;
2278 struct virtio_hw *hw = dev->data->dev_private;
2280 memset(&link, 0, sizeof(link));
2281 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2282 link.link_speed = ETH_SPEED_NUM_10G;
2283 link.link_autoneg = ETH_LINK_FIXED;
2286 link.link_status = ETH_LINK_DOWN;
2287 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2288 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2289 vtpci_read_dev_config(hw,
2290 offsetof(struct virtio_net_config, status),
2291 &status, sizeof(status));
2292 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2293 link.link_status = ETH_LINK_DOWN;
2294 PMD_INIT_LOG(DEBUG, "Port %d is down",
2295 dev->data->port_id);
2297 link.link_status = ETH_LINK_UP;
2298 PMD_INIT_LOG(DEBUG, "Port %d is up",
2299 dev->data->port_id);
2302 link.link_status = ETH_LINK_UP;
2305 return rte_eth_linkstatus_set(dev, &link);
2309 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2311 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2312 struct virtio_hw *hw = dev->data->dev_private;
2313 uint64_t offloads = rxmode->offloads;
2315 if (mask & ETH_VLAN_FILTER_MASK) {
2316 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2317 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2320 "vlan filtering not available on this host");
2326 if (mask & ETH_VLAN_STRIP_MASK)
2327 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2333 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2335 uint64_t tso_mask, host_features;
2336 struct virtio_hw *hw = dev->data->dev_private;
2338 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2340 dev_info->max_rx_queues =
2341 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2342 dev_info->max_tx_queues =
2343 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2344 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2345 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2346 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2348 host_features = VTPCI_OPS(hw)->get_features(hw);
2349 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2350 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2351 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2352 dev_info->rx_offload_capa |=
2353 DEV_RX_OFFLOAD_TCP_CKSUM |
2354 DEV_RX_OFFLOAD_UDP_CKSUM;
2356 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2357 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2358 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2359 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2360 if ((host_features & tso_mask) == tso_mask)
2361 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2363 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2364 DEV_TX_OFFLOAD_VLAN_INSERT;
2365 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2366 dev_info->tx_offload_capa |=
2367 DEV_TX_OFFLOAD_UDP_CKSUM |
2368 DEV_TX_OFFLOAD_TCP_CKSUM;
2370 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2371 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2372 if ((host_features & tso_mask) == tso_mask)
2373 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2377 * It enables testpmd to collect per queue stats.
2380 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2381 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2382 __rte_unused uint8_t is_rx)
2387 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2388 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2389 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2391 RTE_INIT(virtio_init_log)
2393 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2394 if (virtio_logtype_init >= 0)
2395 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2396 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2397 if (virtio_logtype_driver >= 0)
2398 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);