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"
38 #include "virtio_user/virtio_user_dev.h"
40 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
41 static int virtio_dev_configure(struct rte_eth_dev *dev);
42 static int virtio_dev_start(struct rte_eth_dev *dev);
43 static void virtio_dev_stop(struct rte_eth_dev *dev);
44 static int virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
45 static int virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
46 static int virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
47 static int virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
48 static uint32_t virtio_dev_speed_capa_get(uint32_t speed);
49 static int virtio_dev_devargs_parse(struct rte_devargs *devargs,
52 static int virtio_dev_info_get(struct rte_eth_dev *dev,
53 struct rte_eth_dev_info *dev_info);
54 static int virtio_dev_link_update(struct rte_eth_dev *dev,
55 int wait_to_complete);
56 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
58 static void virtio_set_hwaddr(struct virtio_hw *hw);
59 static void virtio_get_hwaddr(struct virtio_hw *hw);
61 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
62 struct rte_eth_stats *stats);
63 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
64 struct rte_eth_xstat *xstats, unsigned n);
65 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
66 struct rte_eth_xstat_name *xstats_names,
68 static int virtio_dev_stats_reset(struct rte_eth_dev *dev);
69 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
70 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
71 uint16_t vlan_id, int on);
72 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
73 struct rte_ether_addr *mac_addr,
74 uint32_t index, uint32_t vmdq);
75 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
76 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
77 struct rte_ether_addr *mac_addr);
79 static int virtio_intr_disable(struct rte_eth_dev *dev);
81 static int virtio_dev_queue_stats_mapping_set(
82 struct rte_eth_dev *eth_dev,
87 int virtio_logtype_init;
88 int virtio_logtype_driver;
90 static void virtio_notify_peers(struct rte_eth_dev *dev);
91 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
94 * The set of PCI devices this driver supports
96 static const struct rte_pci_id pci_id_virtio_map[] = {
97 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
98 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
99 { .vendor_id = 0, /* sentinel */ },
102 struct rte_virtio_xstats_name_off {
103 char name[RTE_ETH_XSTATS_NAME_SIZE];
107 /* [rt]x_qX_ is prepended to the name string here */
108 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
109 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
110 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
111 {"errors", offsetof(struct virtnet_rx, stats.errors)},
112 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
113 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
114 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
115 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
116 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
117 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
118 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
119 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
120 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
121 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
124 /* [rt]x_qX_ is prepended to the name string here */
125 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
126 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
127 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
128 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
129 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
130 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
131 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
132 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
133 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
134 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
135 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
136 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
137 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
140 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
141 sizeof(rte_virtio_rxq_stat_strings[0]))
142 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
143 sizeof(rte_virtio_txq_stat_strings[0]))
145 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
147 static struct virtio_pmd_ctrl *
148 virtio_send_command_packed(struct virtnet_ctl *cvq,
149 struct virtio_pmd_ctrl *ctrl,
150 int *dlen, int pkt_num)
152 struct virtqueue *vq = cvq->vq;
154 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
155 struct virtio_pmd_ctrl *result;
162 * Format is enforced in qemu code:
163 * One TX packet for header;
164 * At least one TX packet per argument;
165 * One RX packet for ACK.
167 head = vq->vq_avail_idx;
168 flags = vq->vq_packed.cached_flags;
169 desc[head].addr = cvq->virtio_net_hdr_mem;
170 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
173 if (++vq->vq_avail_idx >= vq->vq_nentries) {
174 vq->vq_avail_idx -= vq->vq_nentries;
175 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
178 for (k = 0; k < pkt_num; k++) {
179 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
180 + sizeof(struct virtio_net_ctrl_hdr)
181 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
182 desc[vq->vq_avail_idx].len = dlen[k];
183 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
184 vq->vq_packed.cached_flags;
188 if (++vq->vq_avail_idx >= vq->vq_nentries) {
189 vq->vq_avail_idx -= vq->vq_nentries;
190 vq->vq_packed.cached_flags ^=
191 VRING_PACKED_DESC_F_AVAIL_USED;
195 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
196 + sizeof(struct virtio_net_ctrl_hdr);
197 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
198 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
199 vq->vq_packed.cached_flags;
202 if (++vq->vq_avail_idx >= vq->vq_nentries) {
203 vq->vq_avail_idx -= vq->vq_nentries;
204 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
207 virtio_wmb(vq->hw->weak_barriers);
208 desc[head].flags = VRING_DESC_F_NEXT | flags;
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->vq_packed.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->vq_packed.cached_flags=0x%x\n"
231 "vq->vq_packed.used_wrap_counter=%d\n",
234 vq->vq_used_cons_idx,
235 vq->vq_packed.cached_flags,
236 vq->vq_packed.used_wrap_counter);
238 result = cvq->virtio_net_hdr_mz->addr;
242 static struct virtio_pmd_ctrl *
243 virtio_send_command_split(struct virtnet_ctl *cvq,
244 struct virtio_pmd_ctrl *ctrl,
245 int *dlen, int pkt_num)
247 struct virtio_pmd_ctrl *result;
248 struct virtqueue *vq = cvq->vq;
252 head = vq->vq_desc_head_idx;
255 * Format is enforced in qemu code:
256 * One TX packet for header;
257 * At least one TX packet per argument;
258 * One RX packet for ACK.
260 vq->vq_split.ring.desc[head].flags = VRING_DESC_F_NEXT;
261 vq->vq_split.ring.desc[head].addr = cvq->virtio_net_hdr_mem;
262 vq->vq_split.ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
264 i = vq->vq_split.ring.desc[head].next;
266 for (k = 0; k < pkt_num; k++) {
267 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_NEXT;
268 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
269 + sizeof(struct virtio_net_ctrl_hdr)
270 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
271 vq->vq_split.ring.desc[i].len = dlen[k];
274 i = vq->vq_split.ring.desc[i].next;
277 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_WRITE;
278 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
279 + sizeof(struct virtio_net_ctrl_hdr);
280 vq->vq_split.ring.desc[i].len = sizeof(ctrl->status);
283 vq->vq_desc_head_idx = vq->vq_split.ring.desc[i].next;
285 vq_update_avail_ring(vq, head);
286 vq_update_avail_idx(vq);
288 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
290 virtqueue_notify(vq);
293 while (VIRTQUEUE_NUSED(vq) == 0) {
298 while (VIRTQUEUE_NUSED(vq)) {
299 uint32_t idx, desc_idx, used_idx;
300 struct vring_used_elem *uep;
302 used_idx = (uint32_t)(vq->vq_used_cons_idx
303 & (vq->vq_nentries - 1));
304 uep = &vq->vq_split.ring.used->ring[used_idx];
305 idx = (uint32_t) uep->id;
308 while (vq->vq_split.ring.desc[desc_idx].flags &
310 desc_idx = vq->vq_split.ring.desc[desc_idx].next;
314 vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
315 vq->vq_desc_head_idx = idx;
317 vq->vq_used_cons_idx++;
321 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
322 vq->vq_free_cnt, vq->vq_desc_head_idx);
324 result = cvq->virtio_net_hdr_mz->addr;
329 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
330 int *dlen, int pkt_num)
332 virtio_net_ctrl_ack status = ~0;
333 struct virtio_pmd_ctrl *result;
334 struct virtqueue *vq;
336 ctrl->status = status;
338 if (!cvq || !cvq->vq) {
339 PMD_INIT_LOG(ERR, "Control queue is not supported.");
343 rte_spinlock_lock(&cvq->lock);
346 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
347 "vq->hw->cvq = %p vq = %p",
348 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
350 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
351 rte_spinlock_unlock(&cvq->lock);
355 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
356 sizeof(struct virtio_pmd_ctrl));
358 if (vtpci_packed_queue(vq->hw))
359 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
361 result = virtio_send_command_split(cvq, ctrl, dlen, pkt_num);
363 rte_spinlock_unlock(&cvq->lock);
364 return result->status;
368 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
370 struct virtio_hw *hw = dev->data->dev_private;
371 struct virtio_pmd_ctrl ctrl;
375 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
376 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
377 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
379 dlen[0] = sizeof(uint16_t);
381 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
383 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
384 "failed, this is too late now...");
392 virtio_dev_queue_release(void *queue __rte_unused)
398 virtio_get_nr_vq(struct virtio_hw *hw)
400 uint16_t nr_vq = hw->max_queue_pairs * 2;
402 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
409 virtio_init_vring(struct virtqueue *vq)
411 int size = vq->vq_nentries;
412 uint8_t *ring_mem = vq->vq_ring_virt_mem;
414 PMD_INIT_FUNC_TRACE();
416 memset(ring_mem, 0, vq->vq_ring_size);
418 vq->vq_used_cons_idx = 0;
419 vq->vq_desc_head_idx = 0;
420 vq->vq_avail_idx = 0;
421 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
422 vq->vq_free_cnt = vq->vq_nentries;
423 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
424 if (vtpci_packed_queue(vq->hw)) {
425 vring_init_packed(&vq->vq_packed.ring, ring_mem,
426 VIRTIO_PCI_VRING_ALIGN, size);
427 vring_desc_init_packed(vq, size);
429 struct vring *vr = &vq->vq_split.ring;
431 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
432 vring_desc_init_split(vr->desc, size);
435 * Disable device(host) interrupting guest
437 virtqueue_disable_intr(vq);
441 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
443 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
444 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
445 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
446 unsigned int vq_size, size;
447 struct virtio_hw *hw = dev->data->dev_private;
448 struct virtnet_rx *rxvq = NULL;
449 struct virtnet_tx *txvq = NULL;
450 struct virtnet_ctl *cvq = NULL;
451 struct virtqueue *vq;
452 size_t sz_hdr_mz = 0;
453 void *sw_ring = NULL;
454 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
456 int numa_node = dev->device->numa_node;
458 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
459 vtpci_queue_idx, numa_node);
462 * Read the virtqueue size from the Queue Size field
463 * Always power of 2 and if 0 virtqueue does not exist
465 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
466 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
468 PMD_INIT_LOG(ERR, "virtqueue does not exist");
472 if (!vtpci_packed_queue(hw) && !rte_is_power_of_2(vq_size)) {
473 PMD_INIT_LOG(ERR, "split virtqueue size is not power of 2");
477 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
478 dev->data->port_id, vtpci_queue_idx);
480 size = RTE_ALIGN_CEIL(sizeof(*vq) +
481 vq_size * sizeof(struct vq_desc_extra),
482 RTE_CACHE_LINE_SIZE);
483 if (queue_type == VTNET_TQ) {
485 * For each xmit packet, allocate a virtio_net_hdr
486 * and indirect ring elements
488 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
489 } else if (queue_type == VTNET_CQ) {
490 /* Allocate a page for control vq command, data and status */
491 sz_hdr_mz = PAGE_SIZE;
494 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
497 PMD_INIT_LOG(ERR, "can not allocate vq");
500 hw->vqs[vtpci_queue_idx] = vq;
503 vq->vq_queue_index = vtpci_queue_idx;
504 vq->vq_nentries = vq_size;
505 if (vtpci_packed_queue(hw)) {
506 vq->vq_packed.used_wrap_counter = 1;
507 vq->vq_packed.cached_flags = VRING_PACKED_DESC_F_AVAIL;
508 vq->vq_packed.event_flags_shadow = 0;
509 if (queue_type == VTNET_RQ)
510 vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
514 * Reserve a memzone for vring elements
516 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
517 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
518 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
519 size, vq->vq_ring_size);
521 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
522 numa_node, RTE_MEMZONE_IOVA_CONTIG,
523 VIRTIO_PCI_VRING_ALIGN);
525 if (rte_errno == EEXIST)
526 mz = rte_memzone_lookup(vq_name);
533 memset(mz->addr, 0, mz->len);
535 vq->vq_ring_mem = mz->iova;
536 vq->vq_ring_virt_mem = mz->addr;
537 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
539 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
540 (uint64_t)(uintptr_t)mz->addr);
542 virtio_init_vring(vq);
545 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
546 dev->data->port_id, vtpci_queue_idx);
547 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
548 numa_node, RTE_MEMZONE_IOVA_CONTIG,
549 RTE_CACHE_LINE_SIZE);
550 if (hdr_mz == NULL) {
551 if (rte_errno == EEXIST)
552 hdr_mz = rte_memzone_lookup(vq_hdr_name);
553 if (hdr_mz == NULL) {
560 if (queue_type == VTNET_RQ) {
561 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
562 sizeof(vq->sw_ring[0]);
564 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
565 RTE_CACHE_LINE_SIZE, numa_node);
567 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
572 vq->sw_ring = sw_ring;
575 rxvq->port_id = dev->data->port_id;
577 } else if (queue_type == VTNET_TQ) {
580 txvq->port_id = dev->data->port_id;
582 txvq->virtio_net_hdr_mz = hdr_mz;
583 txvq->virtio_net_hdr_mem = hdr_mz->iova;
584 } else if (queue_type == VTNET_CQ) {
588 cvq->virtio_net_hdr_mz = hdr_mz;
589 cvq->virtio_net_hdr_mem = hdr_mz->iova;
590 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
595 /* For virtio_user case (that is when hw->virtio_user_dev is not NULL),
596 * we use virtual address. And we need properly set _offset_, please see
597 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
599 if (!hw->virtio_user_dev)
600 vq->offset = offsetof(struct rte_mbuf, buf_iova);
602 vq->vq_ring_mem = (uintptr_t)mz->addr;
603 vq->offset = offsetof(struct rte_mbuf, buf_addr);
604 if (queue_type == VTNET_TQ)
605 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
606 else if (queue_type == VTNET_CQ)
607 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
610 if (queue_type == VTNET_TQ) {
611 struct virtio_tx_region *txr;
615 memset(txr, 0, vq_size * sizeof(*txr));
616 for (i = 0; i < vq_size; i++) {
617 struct vring_desc *start_dp = txr[i].tx_indir;
619 /* first indirect descriptor is always the tx header */
620 if (!vtpci_packed_queue(hw)) {
621 vring_desc_init_split(start_dp,
622 RTE_DIM(txr[i].tx_indir));
623 start_dp->addr = txvq->virtio_net_hdr_mem
625 + offsetof(struct virtio_tx_region,
627 start_dp->len = hw->vtnet_hdr_size;
628 start_dp->flags = VRING_DESC_F_NEXT;
633 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
634 PMD_INIT_LOG(ERR, "setup_queue failed");
642 rte_memzone_free(hdr_mz);
643 rte_memzone_free(mz);
650 virtio_free_queues(struct virtio_hw *hw)
652 uint16_t nr_vq = virtio_get_nr_vq(hw);
653 struct virtqueue *vq;
660 for (i = 0; i < nr_vq; i++) {
665 queue_type = virtio_get_queue_type(hw, i);
666 if (queue_type == VTNET_RQ) {
667 rte_free(vq->sw_ring);
668 rte_memzone_free(vq->rxq.mz);
669 } else if (queue_type == VTNET_TQ) {
670 rte_memzone_free(vq->txq.mz);
671 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
673 rte_memzone_free(vq->cq.mz);
674 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
686 virtio_alloc_queues(struct rte_eth_dev *dev)
688 struct virtio_hw *hw = dev->data->dev_private;
689 uint16_t nr_vq = virtio_get_nr_vq(hw);
693 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
695 PMD_INIT_LOG(ERR, "failed to allocate vqs");
699 for (i = 0; i < nr_vq; i++) {
700 ret = virtio_init_queue(dev, i);
702 virtio_free_queues(hw);
710 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
713 virtio_dev_close(struct rte_eth_dev *dev)
715 struct virtio_hw *hw = dev->data->dev_private;
716 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
718 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
725 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
726 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
728 virtio_queues_unbind_intr(dev);
730 if (intr_conf->lsc || intr_conf->rxq) {
731 virtio_intr_disable(dev);
732 rte_intr_efd_disable(dev->intr_handle);
733 rte_free(dev->intr_handle->intr_vec);
734 dev->intr_handle->intr_vec = NULL;
738 virtio_dev_free_mbufs(dev);
739 virtio_free_queues(hw);
741 #ifdef RTE_VIRTIO_USER
742 if (hw->virtio_user_dev)
743 virtio_user_dev_uninit(hw->virtio_user_dev);
747 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(dev));
749 rte_pci_ioport_unmap(VTPCI_IO(hw));
754 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
756 struct virtio_hw *hw = dev->data->dev_private;
757 struct virtio_pmd_ctrl ctrl;
761 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
762 PMD_INIT_LOG(INFO, "host does not support rx control");
766 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
767 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
771 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
773 PMD_INIT_LOG(ERR, "Failed to enable promisc");
781 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
783 struct virtio_hw *hw = dev->data->dev_private;
784 struct virtio_pmd_ctrl ctrl;
788 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
789 PMD_INIT_LOG(INFO, "host does not support rx control");
793 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
794 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
798 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
800 PMD_INIT_LOG(ERR, "Failed to disable promisc");
808 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
810 struct virtio_hw *hw = dev->data->dev_private;
811 struct virtio_pmd_ctrl ctrl;
815 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
816 PMD_INIT_LOG(INFO, "host does not support rx control");
820 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
821 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
825 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
827 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
835 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
837 struct virtio_hw *hw = dev->data->dev_private;
838 struct virtio_pmd_ctrl ctrl;
842 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
843 PMD_INIT_LOG(INFO, "host does not support rx control");
847 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
848 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
852 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
854 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
861 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
863 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
865 struct virtio_hw *hw = dev->data->dev_private;
866 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
868 uint32_t frame_size = mtu + ether_hdr_len;
869 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
871 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
873 if (mtu < RTE_ETHER_MIN_MTU || frame_size > max_frame_size) {
874 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
875 RTE_ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
882 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
884 struct virtio_hw *hw = dev->data->dev_private;
885 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
886 struct virtqueue *vq = rxvq->vq;
888 virtqueue_enable_intr(vq);
889 virtio_mb(hw->weak_barriers);
894 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
896 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
897 struct virtqueue *vq = rxvq->vq;
899 virtqueue_disable_intr(vq);
904 * dev_ops for virtio, bare necessities for basic operation
906 static const struct eth_dev_ops virtio_eth_dev_ops = {
907 .dev_configure = virtio_dev_configure,
908 .dev_start = virtio_dev_start,
909 .dev_stop = virtio_dev_stop,
910 .dev_close = virtio_dev_close,
911 .promiscuous_enable = virtio_dev_promiscuous_enable,
912 .promiscuous_disable = virtio_dev_promiscuous_disable,
913 .allmulticast_enable = virtio_dev_allmulticast_enable,
914 .allmulticast_disable = virtio_dev_allmulticast_disable,
915 .mtu_set = virtio_mtu_set,
916 .dev_infos_get = virtio_dev_info_get,
917 .stats_get = virtio_dev_stats_get,
918 .xstats_get = virtio_dev_xstats_get,
919 .xstats_get_names = virtio_dev_xstats_get_names,
920 .stats_reset = virtio_dev_stats_reset,
921 .xstats_reset = virtio_dev_stats_reset,
922 .link_update = virtio_dev_link_update,
923 .vlan_offload_set = virtio_dev_vlan_offload_set,
924 .rx_queue_setup = virtio_dev_rx_queue_setup,
925 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
926 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
927 .rx_queue_release = virtio_dev_queue_release,
928 .rx_descriptor_done = virtio_dev_rx_queue_done,
929 .tx_queue_setup = virtio_dev_tx_queue_setup,
930 .tx_queue_release = virtio_dev_queue_release,
931 /* collect stats per queue */
932 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
933 .vlan_filter_set = virtio_vlan_filter_set,
934 .mac_addr_add = virtio_mac_addr_add,
935 .mac_addr_remove = virtio_mac_addr_remove,
936 .mac_addr_set = virtio_mac_addr_set,
940 * dev_ops for virtio-user in secondary processes, as we just have
941 * some limited supports currently.
943 const struct eth_dev_ops virtio_user_secondary_eth_dev_ops = {
944 .dev_infos_get = virtio_dev_info_get,
945 .stats_get = virtio_dev_stats_get,
946 .xstats_get = virtio_dev_xstats_get,
947 .xstats_get_names = virtio_dev_xstats_get_names,
948 .stats_reset = virtio_dev_stats_reset,
949 .xstats_reset = virtio_dev_stats_reset,
950 /* collect stats per queue */
951 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
955 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
959 for (i = 0; i < dev->data->nb_tx_queues; i++) {
960 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
964 stats->opackets += txvq->stats.packets;
965 stats->obytes += txvq->stats.bytes;
967 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
968 stats->q_opackets[i] = txvq->stats.packets;
969 stats->q_obytes[i] = txvq->stats.bytes;
973 for (i = 0; i < dev->data->nb_rx_queues; i++) {
974 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
978 stats->ipackets += rxvq->stats.packets;
979 stats->ibytes += rxvq->stats.bytes;
980 stats->ierrors += rxvq->stats.errors;
982 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
983 stats->q_ipackets[i] = rxvq->stats.packets;
984 stats->q_ibytes[i] = rxvq->stats.bytes;
988 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
991 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
992 struct rte_eth_xstat_name *xstats_names,
993 __rte_unused unsigned limit)
999 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1000 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1002 if (xstats_names != NULL) {
1003 /* Note: limit checked in rte_eth_xstats_names() */
1005 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1006 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1009 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1010 snprintf(xstats_names[count].name,
1011 sizeof(xstats_names[count].name),
1013 rte_virtio_rxq_stat_strings[t].name);
1018 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1019 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1022 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1023 snprintf(xstats_names[count].name,
1024 sizeof(xstats_names[count].name),
1026 rte_virtio_txq_stat_strings[t].name);
1036 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1042 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1043 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1048 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1049 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1056 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1057 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1058 rte_virtio_rxq_stat_strings[t].offset);
1059 xstats[count].id = count;
1064 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1065 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1072 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1073 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1074 rte_virtio_txq_stat_strings[t].offset);
1075 xstats[count].id = count;
1084 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1086 virtio_update_stats(dev, stats);
1092 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1096 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1097 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1101 txvq->stats.packets = 0;
1102 txvq->stats.bytes = 0;
1103 txvq->stats.multicast = 0;
1104 txvq->stats.broadcast = 0;
1105 memset(txvq->stats.size_bins, 0,
1106 sizeof(txvq->stats.size_bins[0]) * 8);
1109 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1110 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1114 rxvq->stats.packets = 0;
1115 rxvq->stats.bytes = 0;
1116 rxvq->stats.errors = 0;
1117 rxvq->stats.multicast = 0;
1118 rxvq->stats.broadcast = 0;
1119 memset(rxvq->stats.size_bins, 0,
1120 sizeof(rxvq->stats.size_bins[0]) * 8);
1127 virtio_set_hwaddr(struct virtio_hw *hw)
1129 vtpci_write_dev_config(hw,
1130 offsetof(struct virtio_net_config, mac),
1131 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1135 virtio_get_hwaddr(struct virtio_hw *hw)
1137 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1138 vtpci_read_dev_config(hw,
1139 offsetof(struct virtio_net_config, mac),
1140 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1142 rte_eth_random_addr(&hw->mac_addr[0]);
1143 virtio_set_hwaddr(hw);
1148 virtio_mac_table_set(struct virtio_hw *hw,
1149 const struct virtio_net_ctrl_mac *uc,
1150 const struct virtio_net_ctrl_mac *mc)
1152 struct virtio_pmd_ctrl ctrl;
1155 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1156 PMD_DRV_LOG(INFO, "host does not support mac table");
1160 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1161 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1163 len[0] = uc->entries * RTE_ETHER_ADDR_LEN + sizeof(uc->entries);
1164 memcpy(ctrl.data, uc, len[0]);
1166 len[1] = mc->entries * RTE_ETHER_ADDR_LEN + sizeof(mc->entries);
1167 memcpy(ctrl.data + len[0], mc, len[1]);
1169 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1171 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1176 virtio_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1177 uint32_t index, uint32_t vmdq __rte_unused)
1179 struct virtio_hw *hw = dev->data->dev_private;
1180 const struct rte_ether_addr *addrs = dev->data->mac_addrs;
1182 struct virtio_net_ctrl_mac *uc, *mc;
1184 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1185 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1189 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1190 sizeof(uc->entries));
1192 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1193 sizeof(mc->entries));
1196 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1197 const struct rte_ether_addr *addr
1198 = (i == index) ? mac_addr : addrs + i;
1199 struct virtio_net_ctrl_mac *tbl
1200 = rte_is_multicast_ether_addr(addr) ? mc : uc;
1202 memcpy(&tbl->macs[tbl->entries++], addr, RTE_ETHER_ADDR_LEN);
1205 return virtio_mac_table_set(hw, uc, mc);
1209 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1211 struct virtio_hw *hw = dev->data->dev_private;
1212 struct rte_ether_addr *addrs = dev->data->mac_addrs;
1213 struct virtio_net_ctrl_mac *uc, *mc;
1216 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1217 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1221 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1222 sizeof(uc->entries));
1224 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1225 sizeof(mc->entries));
1228 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1229 struct virtio_net_ctrl_mac *tbl;
1231 if (i == index || rte_is_zero_ether_addr(addrs + i))
1234 tbl = rte_is_multicast_ether_addr(addrs + i) ? mc : uc;
1235 memcpy(&tbl->macs[tbl->entries++], addrs + i,
1236 RTE_ETHER_ADDR_LEN);
1239 virtio_mac_table_set(hw, uc, mc);
1243 virtio_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1245 struct virtio_hw *hw = dev->data->dev_private;
1247 memcpy(hw->mac_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1249 /* Use atomic update if available */
1250 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1251 struct virtio_pmd_ctrl ctrl;
1252 int len = RTE_ETHER_ADDR_LEN;
1254 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1255 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1257 memcpy(ctrl.data, mac_addr, RTE_ETHER_ADDR_LEN);
1258 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1261 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1264 virtio_set_hwaddr(hw);
1269 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1271 struct virtio_hw *hw = dev->data->dev_private;
1272 struct virtio_pmd_ctrl ctrl;
1275 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1278 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1279 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1280 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1281 len = sizeof(vlan_id);
1283 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1287 virtio_intr_unmask(struct rte_eth_dev *dev)
1289 struct virtio_hw *hw = dev->data->dev_private;
1291 if (rte_intr_ack(dev->intr_handle) < 0)
1294 if (!hw->virtio_user_dev)
1295 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1301 virtio_intr_enable(struct rte_eth_dev *dev)
1303 struct virtio_hw *hw = dev->data->dev_private;
1305 if (rte_intr_enable(dev->intr_handle) < 0)
1308 if (!hw->virtio_user_dev)
1309 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1315 virtio_intr_disable(struct rte_eth_dev *dev)
1317 struct virtio_hw *hw = dev->data->dev_private;
1319 if (rte_intr_disable(dev->intr_handle) < 0)
1322 if (!hw->virtio_user_dev)
1323 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1329 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1331 uint64_t host_features;
1333 /* Prepare guest_features: feature that driver wants to support */
1334 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1337 /* Read device(host) feature bits */
1338 host_features = VTPCI_OPS(hw)->get_features(hw);
1339 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1342 /* If supported, ensure MTU value is valid before acknowledging it. */
1343 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1344 struct virtio_net_config config;
1346 vtpci_read_dev_config(hw,
1347 offsetof(struct virtio_net_config, mtu),
1348 &config.mtu, sizeof(config.mtu));
1350 if (config.mtu < RTE_ETHER_MIN_MTU)
1351 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1355 * Negotiate features: Subset of device feature bits are written back
1356 * guest feature bits.
1358 hw->guest_features = req_features;
1359 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1360 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1361 hw->guest_features);
1364 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1366 "VIRTIO_F_VERSION_1 features is not enabled.");
1369 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1370 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1372 "failed to set FEATURES_OK status!");
1377 hw->req_guest_features = req_features;
1383 virtio_dev_pause(struct rte_eth_dev *dev)
1385 struct virtio_hw *hw = dev->data->dev_private;
1387 rte_spinlock_lock(&hw->state_lock);
1389 if (hw->started == 0) {
1390 /* Device is just stopped. */
1391 rte_spinlock_unlock(&hw->state_lock);
1396 * Prevent the worker threads from touching queues to avoid contention,
1397 * 1 ms should be enough for the ongoing Tx function to finish.
1404 * Recover hw state to let the worker threads continue.
1407 virtio_dev_resume(struct rte_eth_dev *dev)
1409 struct virtio_hw *hw = dev->data->dev_private;
1412 rte_spinlock_unlock(&hw->state_lock);
1416 * Should be called only after device is paused.
1419 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1422 struct virtio_hw *hw = dev->data->dev_private;
1423 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1426 hw->inject_pkts = tx_pkts;
1427 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1428 hw->inject_pkts = NULL;
1434 virtio_notify_peers(struct rte_eth_dev *dev)
1436 struct virtio_hw *hw = dev->data->dev_private;
1437 struct virtnet_rx *rxvq;
1438 struct rte_mbuf *rarp_mbuf;
1440 if (!dev->data->rx_queues)
1443 rxvq = dev->data->rx_queues[0];
1447 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1448 (struct rte_ether_addr *)hw->mac_addr);
1449 if (rarp_mbuf == NULL) {
1450 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1454 /* If virtio port just stopped, no need to send RARP */
1455 if (virtio_dev_pause(dev) < 0) {
1456 rte_pktmbuf_free(rarp_mbuf);
1460 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1461 virtio_dev_resume(dev);
1465 virtio_ack_link_announce(struct rte_eth_dev *dev)
1467 struct virtio_hw *hw = dev->data->dev_private;
1468 struct virtio_pmd_ctrl ctrl;
1470 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1471 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1473 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1477 * Process virtio config changed interrupt. Call the callback
1478 * if link state changed, generate gratuitous RARP packet if
1479 * the status indicates an ANNOUNCE.
1482 virtio_interrupt_handler(void *param)
1484 struct rte_eth_dev *dev = param;
1485 struct virtio_hw *hw = dev->data->dev_private;
1489 /* Read interrupt status which clears interrupt */
1490 isr = vtpci_isr(hw);
1491 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1493 if (virtio_intr_unmask(dev) < 0)
1494 PMD_DRV_LOG(ERR, "interrupt enable failed");
1496 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1497 if (virtio_dev_link_update(dev, 0) == 0)
1498 _rte_eth_dev_callback_process(dev,
1499 RTE_ETH_EVENT_INTR_LSC,
1502 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1503 vtpci_read_dev_config(hw,
1504 offsetof(struct virtio_net_config, status),
1505 &status, sizeof(status));
1506 if (status & VIRTIO_NET_S_ANNOUNCE) {
1507 virtio_notify_peers(dev);
1509 virtio_ack_link_announce(dev);
1515 /* set rx and tx handlers according to what is supported */
1517 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1519 struct virtio_hw *hw = eth_dev->data->dev_private;
1521 eth_dev->tx_pkt_prepare = virtio_xmit_pkts_prepare;
1522 if (vtpci_packed_queue(hw)) {
1524 "virtio: using packed ring %s Tx path on port %u",
1525 hw->use_inorder_tx ? "inorder" : "standard",
1526 eth_dev->data->port_id);
1527 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1529 if (hw->use_inorder_tx) {
1530 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1531 eth_dev->data->port_id);
1532 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1534 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1535 eth_dev->data->port_id);
1536 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1540 if (vtpci_packed_queue(hw)) {
1541 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1543 "virtio: using packed ring mergeable buffer Rx path on port %u",
1544 eth_dev->data->port_id);
1545 eth_dev->rx_pkt_burst =
1546 &virtio_recv_mergeable_pkts_packed;
1549 "virtio: using packed ring standard Rx path on port %u",
1550 eth_dev->data->port_id);
1551 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1554 if (hw->use_simple_rx) {
1555 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1556 eth_dev->data->port_id);
1557 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1558 } else if (hw->use_inorder_rx) {
1560 "virtio: using inorder Rx path on port %u",
1561 eth_dev->data->port_id);
1562 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1563 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1565 "virtio: using mergeable buffer Rx path on port %u",
1566 eth_dev->data->port_id);
1567 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1569 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1570 eth_dev->data->port_id);
1571 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1577 /* Only support 1:1 queue/interrupt mapping so far.
1578 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1579 * interrupt vectors (<N+1).
1582 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1585 struct virtio_hw *hw = dev->data->dev_private;
1587 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1588 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1589 dev->intr_handle->intr_vec[i] = i + 1;
1590 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1591 VIRTIO_MSI_NO_VECTOR) {
1592 PMD_DRV_LOG(ERR, "failed to set queue vector");
1601 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1604 struct virtio_hw *hw = dev->data->dev_private;
1606 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1607 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1608 VTPCI_OPS(hw)->set_queue_irq(hw,
1609 hw->vqs[i * VTNET_CQ],
1610 VIRTIO_MSI_NO_VECTOR);
1614 virtio_configure_intr(struct rte_eth_dev *dev)
1616 struct virtio_hw *hw = dev->data->dev_private;
1618 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1619 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1623 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1624 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1628 if (!dev->intr_handle->intr_vec) {
1629 dev->intr_handle->intr_vec =
1630 rte_zmalloc("intr_vec",
1631 hw->max_queue_pairs * sizeof(int), 0);
1632 if (!dev->intr_handle->intr_vec) {
1633 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1634 hw->max_queue_pairs);
1639 /* Re-register callback to update max_intr */
1640 rte_intr_callback_unregister(dev->intr_handle,
1641 virtio_interrupt_handler,
1643 rte_intr_callback_register(dev->intr_handle,
1644 virtio_interrupt_handler,
1647 /* DO NOT try to remove this! This function will enable msix, or QEMU
1648 * will encounter SIGSEGV when DRIVER_OK is sent.
1649 * And for legacy devices, this should be done before queue/vec binding
1650 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1651 * (22) will be ignored.
1653 if (virtio_intr_enable(dev) < 0) {
1654 PMD_DRV_LOG(ERR, "interrupt enable failed");
1658 if (virtio_queues_bind_intr(dev) < 0) {
1659 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1665 #define SPEED_UNKNOWN 0xffffffff
1666 #define DUPLEX_UNKNOWN 0xff
1667 /* reset device and renegotiate features if needed */
1669 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1671 struct virtio_hw *hw = eth_dev->data->dev_private;
1672 struct virtio_net_config *config;
1673 struct virtio_net_config local_config;
1674 struct rte_pci_device *pci_dev = NULL;
1677 /* Reset the device although not necessary at startup */
1681 virtio_dev_free_mbufs(eth_dev);
1682 virtio_free_queues(hw);
1685 /* Tell the host we've noticed this device. */
1686 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1688 /* Tell the host we've known how to drive the device. */
1689 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1690 if (virtio_negotiate_features(hw, req_features) < 0)
1693 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1695 if (!hw->virtio_user_dev)
1696 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1698 /* If host does not support both status and MSI-X then disable LSC */
1699 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1700 hw->use_msix != VIRTIO_MSIX_NONE)
1701 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1703 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1705 /* Setting up rx_header size for the device */
1706 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1707 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1708 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1709 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1711 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1713 /* Copy the permanent MAC address to: virtio_hw */
1714 virtio_get_hwaddr(hw);
1715 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1716 ð_dev->data->mac_addrs[0]);
1718 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1719 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1720 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1722 if (hw->speed == SPEED_UNKNOWN) {
1723 if (vtpci_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
1724 config = &local_config;
1725 vtpci_read_dev_config(hw,
1726 offsetof(struct virtio_net_config, speed),
1727 &config->speed, sizeof(config->speed));
1728 vtpci_read_dev_config(hw,
1729 offsetof(struct virtio_net_config, duplex),
1730 &config->duplex, sizeof(config->duplex));
1731 hw->speed = config->speed;
1732 hw->duplex = config->duplex;
1735 if (hw->speed == SPEED_UNKNOWN)
1736 hw->speed = ETH_SPEED_NUM_10G;
1737 if (hw->duplex == DUPLEX_UNKNOWN)
1738 hw->duplex = ETH_LINK_FULL_DUPLEX;
1739 PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
1740 hw->speed, hw->duplex);
1741 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1742 config = &local_config;
1744 vtpci_read_dev_config(hw,
1745 offsetof(struct virtio_net_config, mac),
1746 &config->mac, sizeof(config->mac));
1748 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1749 vtpci_read_dev_config(hw,
1750 offsetof(struct virtio_net_config, status),
1751 &config->status, sizeof(config->status));
1754 "VIRTIO_NET_F_STATUS is not supported");
1758 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1759 vtpci_read_dev_config(hw,
1760 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1761 &config->max_virtqueue_pairs,
1762 sizeof(config->max_virtqueue_pairs));
1765 "VIRTIO_NET_F_MQ is not supported");
1766 config->max_virtqueue_pairs = 1;
1769 hw->max_queue_pairs = config->max_virtqueue_pairs;
1771 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1772 vtpci_read_dev_config(hw,
1773 offsetof(struct virtio_net_config, mtu),
1775 sizeof(config->mtu));
1778 * MTU value has already been checked at negotiation
1779 * time, but check again in case it has changed since
1780 * then, which should not happen.
1782 if (config->mtu < RTE_ETHER_MIN_MTU) {
1783 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1788 hw->max_mtu = config->mtu;
1789 /* Set initial MTU to maximum one supported by vhost */
1790 eth_dev->data->mtu = config->mtu;
1793 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1794 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1797 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1798 config->max_virtqueue_pairs);
1799 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1801 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1802 config->mac[0], config->mac[1],
1803 config->mac[2], config->mac[3],
1804 config->mac[4], config->mac[5]);
1806 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1807 hw->max_queue_pairs = 1;
1808 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1809 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1812 ret = virtio_alloc_queues(eth_dev);
1816 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1817 if (virtio_configure_intr(eth_dev) < 0) {
1818 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1819 virtio_free_queues(hw);
1824 vtpci_reinit_complete(hw);
1827 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1828 eth_dev->data->port_id, pci_dev->id.vendor_id,
1829 pci_dev->id.device_id);
1835 * Remap the PCI device again (IO port map for legacy device and
1836 * memory map for modern device), so that the secondary process
1837 * could have the PCI initiated correctly.
1840 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1844 * We don't have to re-parse the PCI config space, since
1845 * rte_pci_map_device() makes sure the mapped address
1846 * in secondary process would equal to the one mapped in
1847 * the primary process: error will be returned if that
1848 * requirement is not met.
1850 * That said, we could simply reuse all cap pointers
1851 * (such as dev_cfg, common_cfg, etc.) parsed from the
1852 * primary process, which is stored in shared memory.
1854 if (rte_pci_map_device(pci_dev)) {
1855 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1859 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1867 virtio_set_vtpci_ops(struct virtio_hw *hw)
1869 #ifdef RTE_VIRTIO_USER
1870 if (hw->virtio_user_dev)
1871 VTPCI_OPS(hw) = &virtio_user_ops;
1875 VTPCI_OPS(hw) = &modern_ops;
1877 VTPCI_OPS(hw) = &legacy_ops;
1881 * This function is based on probe() function in virtio_pci.c
1882 * It returns 0 on success.
1885 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1887 struct virtio_hw *hw = eth_dev->data->dev_private;
1888 uint32_t speed = SPEED_UNKNOWN;
1891 if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1893 "Not sufficient headroom required = %d, avail = %d",
1894 (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1895 RTE_PKTMBUF_HEADROOM);
1900 eth_dev->dev_ops = &virtio_eth_dev_ops;
1902 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1903 if (!hw->virtio_user_dev) {
1904 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1909 virtio_set_vtpci_ops(hw);
1910 set_rxtx_funcs(eth_dev);
1914 ret = virtio_dev_devargs_parse(eth_dev->device->devargs,
1920 * Pass the information to the rte_eth_dev_close() that it should also
1921 * release the private port resources.
1923 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1925 /* Allocate memory for storing MAC addresses */
1926 eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1927 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1928 if (eth_dev->data->mac_addrs == NULL) {
1930 "Failed to allocate %d bytes needed to store MAC addresses",
1931 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1935 hw->port_id = eth_dev->data->port_id;
1936 /* For virtio_user case the hw->virtio_user_dev is populated by
1937 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1939 if (!hw->virtio_user_dev) {
1940 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1942 goto err_vtpci_init;
1945 rte_spinlock_init(&hw->state_lock);
1947 /* reset device and negotiate default features */
1948 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1950 goto err_virtio_init;
1957 if (!hw->virtio_user_dev) {
1958 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1960 rte_pci_ioport_unmap(VTPCI_IO(hw));
1963 rte_free(eth_dev->data->mac_addrs);
1964 eth_dev->data->mac_addrs = NULL;
1969 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1971 PMD_INIT_FUNC_TRACE();
1973 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1976 virtio_dev_stop(eth_dev);
1977 virtio_dev_close(eth_dev);
1979 eth_dev->dev_ops = NULL;
1980 eth_dev->tx_pkt_burst = NULL;
1981 eth_dev->rx_pkt_burst = NULL;
1983 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1989 static int vdpa_check_handler(__rte_unused const char *key,
1990 const char *value, void *ret_val)
1992 if (strcmp(value, "1") == 0)
1993 *(int *)ret_val = 1;
1995 *(int *)ret_val = 0;
2002 virtio_dev_speed_capa_get(uint32_t speed)
2005 case ETH_SPEED_NUM_10G:
2006 return ETH_LINK_SPEED_10G;
2007 case ETH_SPEED_NUM_20G:
2008 return ETH_LINK_SPEED_20G;
2009 case ETH_SPEED_NUM_25G:
2010 return ETH_LINK_SPEED_25G;
2011 case ETH_SPEED_NUM_40G:
2012 return ETH_LINK_SPEED_40G;
2013 case ETH_SPEED_NUM_50G:
2014 return ETH_LINK_SPEED_50G;
2015 case ETH_SPEED_NUM_56G:
2016 return ETH_LINK_SPEED_56G;
2017 case ETH_SPEED_NUM_100G:
2018 return ETH_LINK_SPEED_100G;
2025 #define VIRTIO_ARG_SPEED "speed"
2026 #define VIRTIO_ARG_VDPA "vdpa"
2030 link_speed_handler(const char *key __rte_unused,
2031 const char *value, void *ret_val)
2034 if (!value || !ret_val)
2036 val = strtoul(value, NULL, 0);
2037 /* validate input */
2038 if (virtio_dev_speed_capa_get(val) == 0)
2040 *(uint32_t *)ret_val = val;
2047 virtio_dev_devargs_parse(struct rte_devargs *devargs, int *vdpa,
2050 struct rte_kvargs *kvlist;
2053 if (devargs == NULL)
2056 kvlist = rte_kvargs_parse(devargs->args, NULL);
2057 if (kvlist == NULL) {
2058 PMD_INIT_LOG(ERR, "error when parsing param");
2061 if (vdpa && rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) {
2062 /* vdpa mode selected when there's a key-value pair:
2065 ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA,
2066 vdpa_check_handler, vdpa);
2068 PMD_INIT_LOG(ERR, "Failed to parse %s",
2073 if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
2074 ret = rte_kvargs_process(kvlist,
2076 link_speed_handler, speed);
2078 PMD_INIT_LOG(ERR, "Failed to parse %s",
2085 rte_kvargs_free(kvlist);
2089 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2090 struct rte_pci_device *pci_dev)
2095 ret = virtio_dev_devargs_parse(pci_dev->device.devargs, &vdpa, NULL);
2097 PMD_INIT_LOG(ERR, "devargs parsing is failed");
2100 /* virtio pmd skips probe if device needs to work in vdpa mode */
2104 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
2105 eth_virtio_dev_init);
2108 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
2112 ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
2113 /* Port has already been released by close. */
2119 static struct rte_pci_driver rte_virtio_pmd = {
2121 .name = "net_virtio",
2123 .id_table = pci_id_virtio_map,
2125 .probe = eth_virtio_pci_probe,
2126 .remove = eth_virtio_pci_remove,
2129 RTE_INIT(rte_virtio_pmd_init)
2131 rte_eal_iopl_init();
2132 rte_pci_register(&rte_virtio_pmd);
2136 rx_offload_enabled(struct virtio_hw *hw)
2138 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
2139 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2140 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
2144 tx_offload_enabled(struct virtio_hw *hw)
2146 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
2147 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
2148 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2152 * Configure virtio device
2153 * It returns 0 on success.
2156 virtio_dev_configure(struct rte_eth_dev *dev)
2158 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2159 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2160 struct virtio_hw *hw = dev->data->dev_private;
2161 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2163 uint64_t rx_offloads = rxmode->offloads;
2164 uint64_t tx_offloads = txmode->offloads;
2165 uint64_t req_features;
2168 PMD_INIT_LOG(DEBUG, "configure");
2169 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2171 if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2173 "Unsupported Rx multi queue mode %d",
2178 if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2180 "Unsupported Tx multi queue mode %d",
2185 if (dev->data->dev_conf.intr_conf.rxq) {
2186 ret = virtio_init_device(dev, hw->req_guest_features);
2191 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
2192 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2194 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2195 DEV_RX_OFFLOAD_TCP_CKSUM))
2196 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2198 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2200 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2201 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2203 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2204 DEV_TX_OFFLOAD_TCP_CKSUM))
2205 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2207 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2209 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2210 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2212 /* if request features changed, reinit the device */
2213 if (req_features != hw->req_guest_features) {
2214 ret = virtio_init_device(dev, req_features);
2219 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2220 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2221 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2223 "rx checksum not available on this host");
2227 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2228 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2229 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2231 "Large Receive Offload not available on this host");
2235 /* start control queue */
2236 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2237 virtio_dev_cq_start(dev);
2239 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2242 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2243 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2245 "vlan filtering not available on this host");
2249 hw->has_tx_offload = tx_offload_enabled(hw);
2250 hw->has_rx_offload = rx_offload_enabled(hw);
2252 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2253 /* Enable vector (0) for Link State Intrerrupt */
2254 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2255 VIRTIO_MSI_NO_VECTOR) {
2256 PMD_DRV_LOG(ERR, "failed to set config vector");
2260 hw->use_simple_rx = 1;
2262 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2263 hw->use_inorder_tx = 1;
2264 hw->use_inorder_rx = 1;
2265 hw->use_simple_rx = 0;
2268 if (vtpci_packed_queue(hw)) {
2269 hw->use_simple_rx = 0;
2270 hw->use_inorder_rx = 0;
2273 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2274 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2275 hw->use_simple_rx = 0;
2278 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2279 hw->use_simple_rx = 0;
2282 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2283 DEV_RX_OFFLOAD_TCP_CKSUM |
2284 DEV_RX_OFFLOAD_TCP_LRO |
2285 DEV_RX_OFFLOAD_VLAN_STRIP))
2286 hw->use_simple_rx = 0;
2293 virtio_dev_start(struct rte_eth_dev *dev)
2295 uint16_t nb_queues, i;
2296 struct virtnet_rx *rxvq;
2297 struct virtnet_tx *txvq __rte_unused;
2298 struct virtio_hw *hw = dev->data->dev_private;
2301 /* Finish the initialization of the queues */
2302 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2303 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2307 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2308 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2313 /* check if lsc interrupt feature is enabled */
2314 if (dev->data->dev_conf.intr_conf.lsc) {
2315 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2316 PMD_DRV_LOG(ERR, "link status not supported by host");
2321 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2322 * in device configure, but it could be unmapped when device is
2325 if (dev->data->dev_conf.intr_conf.lsc ||
2326 dev->data->dev_conf.intr_conf.rxq) {
2327 virtio_intr_disable(dev);
2329 /* Setup interrupt callback */
2330 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2331 rte_intr_callback_register(dev->intr_handle,
2332 virtio_interrupt_handler,
2335 if (virtio_intr_enable(dev) < 0) {
2336 PMD_DRV_LOG(ERR, "interrupt enable failed");
2341 /*Notify the backend
2342 *Otherwise the tap backend might already stop its queue due to fullness.
2343 *vhost backend will have no chance to be waked up
2345 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2346 if (hw->max_queue_pairs > 1) {
2347 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2351 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2353 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2354 rxvq = dev->data->rx_queues[i];
2355 /* Flush the old packets */
2356 virtqueue_rxvq_flush(rxvq->vq);
2357 virtqueue_notify(rxvq->vq);
2360 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2361 txvq = dev->data->tx_queues[i];
2362 virtqueue_notify(txvq->vq);
2365 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2367 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2368 rxvq = dev->data->rx_queues[i];
2369 VIRTQUEUE_DUMP(rxvq->vq);
2372 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2373 txvq = dev->data->tx_queues[i];
2374 VIRTQUEUE_DUMP(txvq->vq);
2377 set_rxtx_funcs(dev);
2380 /* Initialize Link state */
2381 virtio_dev_link_update(dev, 0);
2386 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2388 struct virtio_hw *hw = dev->data->dev_private;
2389 uint16_t nr_vq = virtio_get_nr_vq(hw);
2390 const char *type __rte_unused;
2391 unsigned int i, mbuf_num = 0;
2392 struct virtqueue *vq;
2393 struct rte_mbuf *buf;
2396 if (hw->vqs == NULL)
2399 for (i = 0; i < nr_vq; i++) {
2404 queue_type = virtio_get_queue_type(hw, i);
2405 if (queue_type == VTNET_RQ)
2407 else if (queue_type == VTNET_TQ)
2413 "Before freeing %s[%d] used and unused buf",
2417 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2418 rte_pktmbuf_free(buf);
2423 "After freeing %s[%d] used and unused buf",
2428 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2432 * Stop device: disable interrupt and mark link down
2435 virtio_dev_stop(struct rte_eth_dev *dev)
2437 struct virtio_hw *hw = dev->data->dev_private;
2438 struct rte_eth_link link;
2439 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2441 PMD_INIT_LOG(DEBUG, "stop");
2443 rte_spinlock_lock(&hw->state_lock);
2446 hw->started = false;
2448 if (intr_conf->lsc || intr_conf->rxq) {
2449 virtio_intr_disable(dev);
2451 /* Reset interrupt callback */
2452 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2453 rte_intr_callback_unregister(dev->intr_handle,
2454 virtio_interrupt_handler,
2459 memset(&link, 0, sizeof(link));
2460 rte_eth_linkstatus_set(dev, &link);
2462 rte_spinlock_unlock(&hw->state_lock);
2466 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2468 struct rte_eth_link link;
2470 struct virtio_hw *hw = dev->data->dev_private;
2472 memset(&link, 0, sizeof(link));
2473 link.link_duplex = hw->duplex;
2474 link.link_speed = hw->speed;
2475 link.link_autoneg = ETH_LINK_AUTONEG;
2478 link.link_status = ETH_LINK_DOWN;
2479 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2480 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2481 vtpci_read_dev_config(hw,
2482 offsetof(struct virtio_net_config, status),
2483 &status, sizeof(status));
2484 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2485 link.link_status = ETH_LINK_DOWN;
2486 PMD_INIT_LOG(DEBUG, "Port %d is down",
2487 dev->data->port_id);
2489 link.link_status = ETH_LINK_UP;
2490 PMD_INIT_LOG(DEBUG, "Port %d is up",
2491 dev->data->port_id);
2494 link.link_status = ETH_LINK_UP;
2497 return rte_eth_linkstatus_set(dev, &link);
2501 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2503 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2504 struct virtio_hw *hw = dev->data->dev_private;
2505 uint64_t offloads = rxmode->offloads;
2507 if (mask & ETH_VLAN_FILTER_MASK) {
2508 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2509 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2512 "vlan filtering not available on this host");
2518 if (mask & ETH_VLAN_STRIP_MASK)
2519 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2525 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2527 uint64_t tso_mask, host_features;
2528 struct virtio_hw *hw = dev->data->dev_private;
2529 dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2531 dev_info->max_rx_queues =
2532 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2533 dev_info->max_tx_queues =
2534 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2535 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2536 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2537 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2539 host_features = VTPCI_OPS(hw)->get_features(hw);
2540 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2541 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2542 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2543 dev_info->rx_offload_capa |=
2544 DEV_RX_OFFLOAD_TCP_CKSUM |
2545 DEV_RX_OFFLOAD_UDP_CKSUM;
2547 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2548 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2549 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2550 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2551 if ((host_features & tso_mask) == tso_mask)
2552 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2554 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2555 DEV_TX_OFFLOAD_VLAN_INSERT;
2556 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2557 dev_info->tx_offload_capa |=
2558 DEV_TX_OFFLOAD_UDP_CKSUM |
2559 DEV_TX_OFFLOAD_TCP_CKSUM;
2561 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2562 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2563 if ((host_features & tso_mask) == tso_mask)
2564 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2570 * It enables testpmd to collect per queue stats.
2573 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2574 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2575 __rte_unused uint8_t is_rx)
2580 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2581 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2582 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2584 RTE_INIT(virtio_init_log)
2586 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2587 if (virtio_logtype_init >= 0)
2588 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2589 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2590 if (virtio_logtype_driver >= 0)
2591 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);