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");
1666 /* reset device and renegotiate features if needed */
1668 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1670 struct virtio_hw *hw = eth_dev->data->dev_private;
1671 struct virtio_net_config *config;
1672 struct virtio_net_config local_config;
1673 struct rte_pci_device *pci_dev = NULL;
1676 /* Reset the device although not necessary at startup */
1680 virtio_dev_free_mbufs(eth_dev);
1681 virtio_free_queues(hw);
1684 /* Tell the host we've noticed this device. */
1685 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1687 /* Tell the host we've known how to drive the device. */
1688 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1689 if (virtio_negotiate_features(hw, req_features) < 0)
1692 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1694 if (!hw->virtio_user_dev)
1695 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1697 /* If host does not support both status and MSI-X then disable LSC */
1698 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1699 hw->use_msix != VIRTIO_MSIX_NONE)
1700 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1702 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1704 /* Setting up rx_header size for the device */
1705 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1706 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1707 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1708 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1710 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1712 /* Copy the permanent MAC address to: virtio_hw */
1713 virtio_get_hwaddr(hw);
1714 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1715 ð_dev->data->mac_addrs[0]);
1717 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1718 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1719 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1721 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1722 config = &local_config;
1724 vtpci_read_dev_config(hw,
1725 offsetof(struct virtio_net_config, mac),
1726 &config->mac, sizeof(config->mac));
1728 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1729 vtpci_read_dev_config(hw,
1730 offsetof(struct virtio_net_config, status),
1731 &config->status, sizeof(config->status));
1734 "VIRTIO_NET_F_STATUS is not supported");
1738 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1739 vtpci_read_dev_config(hw,
1740 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1741 &config->max_virtqueue_pairs,
1742 sizeof(config->max_virtqueue_pairs));
1745 "VIRTIO_NET_F_MQ is not supported");
1746 config->max_virtqueue_pairs = 1;
1749 hw->max_queue_pairs = config->max_virtqueue_pairs;
1751 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1752 vtpci_read_dev_config(hw,
1753 offsetof(struct virtio_net_config, mtu),
1755 sizeof(config->mtu));
1758 * MTU value has already been checked at negotiation
1759 * time, but check again in case it has changed since
1760 * then, which should not happen.
1762 if (config->mtu < RTE_ETHER_MIN_MTU) {
1763 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1768 hw->max_mtu = config->mtu;
1769 /* Set initial MTU to maximum one supported by vhost */
1770 eth_dev->data->mtu = config->mtu;
1773 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1774 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1777 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1778 config->max_virtqueue_pairs);
1779 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1781 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1782 config->mac[0], config->mac[1],
1783 config->mac[2], config->mac[3],
1784 config->mac[4], config->mac[5]);
1786 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1787 hw->max_queue_pairs = 1;
1788 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1789 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1792 ret = virtio_alloc_queues(eth_dev);
1796 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1797 if (virtio_configure_intr(eth_dev) < 0) {
1798 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1799 virtio_free_queues(hw);
1804 vtpci_reinit_complete(hw);
1807 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1808 eth_dev->data->port_id, pci_dev->id.vendor_id,
1809 pci_dev->id.device_id);
1815 * Remap the PCI device again (IO port map for legacy device and
1816 * memory map for modern device), so that the secondary process
1817 * could have the PCI initiated correctly.
1820 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1824 * We don't have to re-parse the PCI config space, since
1825 * rte_pci_map_device() makes sure the mapped address
1826 * in secondary process would equal to the one mapped in
1827 * the primary process: error will be returned if that
1828 * requirement is not met.
1830 * That said, we could simply reuse all cap pointers
1831 * (such as dev_cfg, common_cfg, etc.) parsed from the
1832 * primary process, which is stored in shared memory.
1834 if (rte_pci_map_device(pci_dev)) {
1835 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1839 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1847 virtio_set_vtpci_ops(struct virtio_hw *hw)
1849 #ifdef RTE_VIRTIO_USER
1850 if (hw->virtio_user_dev)
1851 VTPCI_OPS(hw) = &virtio_user_ops;
1855 VTPCI_OPS(hw) = &modern_ops;
1857 VTPCI_OPS(hw) = &legacy_ops;
1861 * This function is based on probe() function in virtio_pci.c
1862 * It returns 0 on success.
1865 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1867 struct virtio_hw *hw = eth_dev->data->dev_private;
1868 uint32_t speed = ETH_SPEED_NUM_10G;
1871 if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1873 "Not sufficient headroom required = %d, avail = %d",
1874 (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1875 RTE_PKTMBUF_HEADROOM);
1880 eth_dev->dev_ops = &virtio_eth_dev_ops;
1882 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1883 if (!hw->virtio_user_dev) {
1884 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1889 virtio_set_vtpci_ops(hw);
1890 set_rxtx_funcs(eth_dev);
1894 ret = virtio_dev_devargs_parse(eth_dev->device->devargs,
1900 * Pass the information to the rte_eth_dev_close() that it should also
1901 * release the private port resources.
1903 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1905 /* Allocate memory for storing MAC addresses */
1906 eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1907 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1908 if (eth_dev->data->mac_addrs == NULL) {
1910 "Failed to allocate %d bytes needed to store MAC addresses",
1911 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1915 hw->port_id = eth_dev->data->port_id;
1916 /* For virtio_user case the hw->virtio_user_dev is populated by
1917 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1919 if (!hw->virtio_user_dev) {
1920 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1922 goto err_vtpci_init;
1925 rte_spinlock_init(&hw->state_lock);
1927 /* reset device and negotiate default features */
1928 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1930 goto err_virtio_init;
1937 if (!hw->virtio_user_dev) {
1938 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1940 rte_pci_ioport_unmap(VTPCI_IO(hw));
1943 rte_free(eth_dev->data->mac_addrs);
1944 eth_dev->data->mac_addrs = NULL;
1949 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1951 PMD_INIT_FUNC_TRACE();
1953 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1956 virtio_dev_stop(eth_dev);
1957 virtio_dev_close(eth_dev);
1959 eth_dev->dev_ops = NULL;
1960 eth_dev->tx_pkt_burst = NULL;
1961 eth_dev->rx_pkt_burst = NULL;
1963 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1969 static int vdpa_check_handler(__rte_unused const char *key,
1970 const char *value, void *ret_val)
1972 if (strcmp(value, "1") == 0)
1973 *(int *)ret_val = 1;
1975 *(int *)ret_val = 0;
1982 virtio_dev_speed_capa_get(uint32_t speed)
1985 case ETH_SPEED_NUM_10G:
1986 return ETH_LINK_SPEED_10G;
1987 case ETH_SPEED_NUM_20G:
1988 return ETH_LINK_SPEED_20G;
1989 case ETH_SPEED_NUM_25G:
1990 return ETH_LINK_SPEED_25G;
1991 case ETH_SPEED_NUM_40G:
1992 return ETH_LINK_SPEED_40G;
1993 case ETH_SPEED_NUM_50G:
1994 return ETH_LINK_SPEED_50G;
1995 case ETH_SPEED_NUM_56G:
1996 return ETH_LINK_SPEED_56G;
1997 case ETH_SPEED_NUM_100G:
1998 return ETH_LINK_SPEED_100G;
2005 #define VIRTIO_ARG_SPEED "speed"
2006 #define VIRTIO_ARG_VDPA "vdpa"
2010 link_speed_handler(const char *key __rte_unused,
2011 const char *value, void *ret_val)
2014 if (!value || !ret_val)
2016 val = strtoul(value, NULL, 0);
2017 /* validate input */
2018 if (virtio_dev_speed_capa_get(val) == 0)
2020 *(uint32_t *)ret_val = val;
2027 virtio_dev_devargs_parse(struct rte_devargs *devargs, int *vdpa,
2030 struct rte_kvargs *kvlist;
2033 if (devargs == NULL)
2036 kvlist = rte_kvargs_parse(devargs->args, NULL);
2037 if (kvlist == NULL) {
2038 PMD_INIT_LOG(ERR, "error when parsing param");
2041 if (vdpa && rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) {
2042 /* vdpa mode selected when there's a key-value pair:
2045 ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA,
2046 vdpa_check_handler, vdpa);
2048 PMD_INIT_LOG(ERR, "Failed to parse %s",
2053 if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
2054 ret = rte_kvargs_process(kvlist,
2056 link_speed_handler, speed);
2058 PMD_INIT_LOG(ERR, "Failed to parse %s",
2065 rte_kvargs_free(kvlist);
2069 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2070 struct rte_pci_device *pci_dev)
2075 ret = virtio_dev_devargs_parse(pci_dev->device.devargs, &vdpa, NULL);
2077 PMD_INIT_LOG(ERR, "devargs parsing is failed");
2080 /* virtio pmd skips probe if device needs to work in vdpa mode */
2084 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
2085 eth_virtio_dev_init);
2088 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
2092 ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
2093 /* Port has already been released by close. */
2099 static struct rte_pci_driver rte_virtio_pmd = {
2101 .name = "net_virtio",
2103 .id_table = pci_id_virtio_map,
2105 .probe = eth_virtio_pci_probe,
2106 .remove = eth_virtio_pci_remove,
2109 RTE_INIT(rte_virtio_pmd_init)
2111 rte_eal_iopl_init();
2112 rte_pci_register(&rte_virtio_pmd);
2116 rx_offload_enabled(struct virtio_hw *hw)
2118 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
2119 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2120 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
2124 tx_offload_enabled(struct virtio_hw *hw)
2126 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
2127 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
2128 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2132 * Configure virtio device
2133 * It returns 0 on success.
2136 virtio_dev_configure(struct rte_eth_dev *dev)
2138 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2139 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2140 struct virtio_hw *hw = dev->data->dev_private;
2141 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2143 uint64_t rx_offloads = rxmode->offloads;
2144 uint64_t tx_offloads = txmode->offloads;
2145 uint64_t req_features;
2148 PMD_INIT_LOG(DEBUG, "configure");
2149 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2151 if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2153 "Unsupported Rx multi queue mode %d",
2158 if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2160 "Unsupported Tx multi queue mode %d",
2165 if (dev->data->dev_conf.intr_conf.rxq) {
2166 ret = virtio_init_device(dev, hw->req_guest_features);
2171 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
2172 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2174 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2175 DEV_RX_OFFLOAD_TCP_CKSUM))
2176 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2178 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2180 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2181 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2183 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2184 DEV_TX_OFFLOAD_TCP_CKSUM))
2185 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2187 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2189 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2190 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2192 /* if request features changed, reinit the device */
2193 if (req_features != hw->req_guest_features) {
2194 ret = virtio_init_device(dev, req_features);
2199 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2200 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2201 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2203 "rx checksum not available on this host");
2207 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2208 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2209 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2211 "Large Receive Offload not available on this host");
2215 /* start control queue */
2216 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2217 virtio_dev_cq_start(dev);
2219 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2222 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2223 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2225 "vlan filtering not available on this host");
2229 hw->has_tx_offload = tx_offload_enabled(hw);
2230 hw->has_rx_offload = rx_offload_enabled(hw);
2232 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2233 /* Enable vector (0) for Link State Intrerrupt */
2234 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2235 VIRTIO_MSI_NO_VECTOR) {
2236 PMD_DRV_LOG(ERR, "failed to set config vector");
2240 hw->use_simple_rx = 1;
2242 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2243 hw->use_inorder_tx = 1;
2244 hw->use_inorder_rx = 1;
2245 hw->use_simple_rx = 0;
2248 if (vtpci_packed_queue(hw)) {
2249 hw->use_simple_rx = 0;
2250 hw->use_inorder_rx = 0;
2253 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2254 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2255 hw->use_simple_rx = 0;
2258 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2259 hw->use_simple_rx = 0;
2262 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2263 DEV_RX_OFFLOAD_TCP_CKSUM |
2264 DEV_RX_OFFLOAD_TCP_LRO |
2265 DEV_RX_OFFLOAD_VLAN_STRIP))
2266 hw->use_simple_rx = 0;
2273 virtio_dev_start(struct rte_eth_dev *dev)
2275 uint16_t nb_queues, i;
2276 struct virtnet_rx *rxvq;
2277 struct virtnet_tx *txvq __rte_unused;
2278 struct virtio_hw *hw = dev->data->dev_private;
2281 /* Finish the initialization of the queues */
2282 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2283 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2287 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2288 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2293 /* check if lsc interrupt feature is enabled */
2294 if (dev->data->dev_conf.intr_conf.lsc) {
2295 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2296 PMD_DRV_LOG(ERR, "link status not supported by host");
2301 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2302 * in device configure, but it could be unmapped when device is
2305 if (dev->data->dev_conf.intr_conf.lsc ||
2306 dev->data->dev_conf.intr_conf.rxq) {
2307 virtio_intr_disable(dev);
2309 /* Setup interrupt callback */
2310 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2311 rte_intr_callback_register(dev->intr_handle,
2312 virtio_interrupt_handler,
2315 if (virtio_intr_enable(dev) < 0) {
2316 PMD_DRV_LOG(ERR, "interrupt enable failed");
2321 /*Notify the backend
2322 *Otherwise the tap backend might already stop its queue due to fullness.
2323 *vhost backend will have no chance to be waked up
2325 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2326 if (hw->max_queue_pairs > 1) {
2327 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2331 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2333 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2334 rxvq = dev->data->rx_queues[i];
2335 /* Flush the old packets */
2336 virtqueue_rxvq_flush(rxvq->vq);
2337 virtqueue_notify(rxvq->vq);
2340 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2341 txvq = dev->data->tx_queues[i];
2342 virtqueue_notify(txvq->vq);
2345 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2347 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2348 rxvq = dev->data->rx_queues[i];
2349 VIRTQUEUE_DUMP(rxvq->vq);
2352 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2353 txvq = dev->data->tx_queues[i];
2354 VIRTQUEUE_DUMP(txvq->vq);
2357 set_rxtx_funcs(dev);
2360 /* Initialize Link state */
2361 virtio_dev_link_update(dev, 0);
2366 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2368 struct virtio_hw *hw = dev->data->dev_private;
2369 uint16_t nr_vq = virtio_get_nr_vq(hw);
2370 const char *type __rte_unused;
2371 unsigned int i, mbuf_num = 0;
2372 struct virtqueue *vq;
2373 struct rte_mbuf *buf;
2376 if (hw->vqs == NULL)
2379 for (i = 0; i < nr_vq; i++) {
2384 queue_type = virtio_get_queue_type(hw, i);
2385 if (queue_type == VTNET_RQ)
2387 else if (queue_type == VTNET_TQ)
2393 "Before freeing %s[%d] used and unused buf",
2397 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2398 rte_pktmbuf_free(buf);
2403 "After freeing %s[%d] used and unused buf",
2408 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2412 * Stop device: disable interrupt and mark link down
2415 virtio_dev_stop(struct rte_eth_dev *dev)
2417 struct virtio_hw *hw = dev->data->dev_private;
2418 struct rte_eth_link link;
2419 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2421 PMD_INIT_LOG(DEBUG, "stop");
2423 rte_spinlock_lock(&hw->state_lock);
2426 hw->started = false;
2428 if (intr_conf->lsc || intr_conf->rxq) {
2429 virtio_intr_disable(dev);
2431 /* Reset interrupt callback */
2432 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2433 rte_intr_callback_unregister(dev->intr_handle,
2434 virtio_interrupt_handler,
2439 memset(&link, 0, sizeof(link));
2440 rte_eth_linkstatus_set(dev, &link);
2442 rte_spinlock_unlock(&hw->state_lock);
2446 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2448 struct rte_eth_link link;
2450 struct virtio_hw *hw = dev->data->dev_private;
2452 memset(&link, 0, sizeof(link));
2453 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2454 link.link_speed = hw->speed;
2455 link.link_autoneg = ETH_LINK_AUTONEG;
2458 link.link_status = ETH_LINK_DOWN;
2459 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2460 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2461 vtpci_read_dev_config(hw,
2462 offsetof(struct virtio_net_config, status),
2463 &status, sizeof(status));
2464 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2465 link.link_status = ETH_LINK_DOWN;
2466 PMD_INIT_LOG(DEBUG, "Port %d is down",
2467 dev->data->port_id);
2469 link.link_status = ETH_LINK_UP;
2470 PMD_INIT_LOG(DEBUG, "Port %d is up",
2471 dev->data->port_id);
2474 link.link_status = ETH_LINK_UP;
2477 return rte_eth_linkstatus_set(dev, &link);
2481 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2483 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2484 struct virtio_hw *hw = dev->data->dev_private;
2485 uint64_t offloads = rxmode->offloads;
2487 if (mask & ETH_VLAN_FILTER_MASK) {
2488 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2489 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2492 "vlan filtering not available on this host");
2498 if (mask & ETH_VLAN_STRIP_MASK)
2499 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2505 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2507 uint64_t tso_mask, host_features;
2508 struct virtio_hw *hw = dev->data->dev_private;
2509 dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2511 dev_info->max_rx_queues =
2512 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2513 dev_info->max_tx_queues =
2514 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2515 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2516 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2517 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2519 host_features = VTPCI_OPS(hw)->get_features(hw);
2520 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2521 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2522 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2523 dev_info->rx_offload_capa |=
2524 DEV_RX_OFFLOAD_TCP_CKSUM |
2525 DEV_RX_OFFLOAD_UDP_CKSUM;
2527 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2528 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2529 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2530 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2531 if ((host_features & tso_mask) == tso_mask)
2532 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2534 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2535 DEV_TX_OFFLOAD_VLAN_INSERT;
2536 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2537 dev_info->tx_offload_capa |=
2538 DEV_TX_OFFLOAD_UDP_CKSUM |
2539 DEV_TX_OFFLOAD_TCP_CKSUM;
2541 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2542 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2543 if ((host_features & tso_mask) == tso_mask)
2544 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2550 * It enables testpmd to collect per queue stats.
2553 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2554 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2555 __rte_unused uint8_t is_rx)
2560 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2561 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2562 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2564 RTE_INIT(virtio_init_log)
2566 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2567 if (virtio_logtype_init >= 0)
2568 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2569 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2570 if (virtio_logtype_driver >= 0)
2571 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);