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>
32 #include "virtio_ethdev.h"
33 #include "virtio_pci.h"
34 #include "virtio_logs.h"
35 #include "virtqueue.h"
36 #include "virtio_rxtx.h"
38 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
39 static int virtio_dev_configure(struct rte_eth_dev *dev);
40 static int virtio_dev_start(struct rte_eth_dev *dev);
41 static void virtio_dev_stop(struct rte_eth_dev *dev);
42 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
43 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
44 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
45 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
46 static void virtio_dev_info_get(struct rte_eth_dev *dev,
47 struct rte_eth_dev_info *dev_info);
48 static int virtio_dev_link_update(struct rte_eth_dev *dev,
49 int wait_to_complete);
50 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
52 static void virtio_set_hwaddr(struct virtio_hw *hw);
53 static void virtio_get_hwaddr(struct virtio_hw *hw);
55 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
56 struct rte_eth_stats *stats);
57 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
58 struct rte_eth_xstat *xstats, unsigned n);
59 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
60 struct rte_eth_xstat_name *xstats_names,
62 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
63 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
64 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
65 uint16_t vlan_id, int on);
66 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
67 struct ether_addr *mac_addr,
68 uint32_t index, uint32_t vmdq);
69 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
70 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
71 struct ether_addr *mac_addr);
73 static int virtio_intr_enable(struct rte_eth_dev *dev);
74 static int virtio_intr_disable(struct rte_eth_dev *dev);
76 static int virtio_dev_queue_stats_mapping_set(
77 struct rte_eth_dev *eth_dev,
82 int virtio_logtype_init;
83 int virtio_logtype_driver;
85 static void virtio_notify_peers(struct rte_eth_dev *dev);
86 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
89 * The set of PCI devices this driver supports
91 static const struct rte_pci_id pci_id_virtio_map[] = {
92 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
93 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
94 { .vendor_id = 0, /* sentinel */ },
97 struct rte_virtio_xstats_name_off {
98 char name[RTE_ETH_XSTATS_NAME_SIZE];
102 /* [rt]x_qX_ is prepended to the name string here */
103 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
104 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
105 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
106 {"errors", offsetof(struct virtnet_rx, stats.errors)},
107 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
108 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
109 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
110 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
111 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
112 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
113 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
114 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
115 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
116 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
119 /* [rt]x_qX_ is prepended to the name string here */
120 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
121 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
122 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
123 {"errors", offsetof(struct virtnet_tx, stats.errors)},
124 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
125 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
126 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
127 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
128 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
129 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
130 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
131 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
132 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
133 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
136 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
137 sizeof(rte_virtio_rxq_stat_strings[0]))
138 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
139 sizeof(rte_virtio_txq_stat_strings[0]))
141 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
144 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
145 int *dlen, int pkt_num)
149 virtio_net_ctrl_ack status = ~0;
150 struct virtio_pmd_ctrl *result;
151 struct virtqueue *vq;
153 ctrl->status = status;
155 if (!cvq || !cvq->vq) {
156 PMD_INIT_LOG(ERR, "Control queue is not supported.");
160 rte_spinlock_lock(&cvq->lock);
162 head = vq->vq_desc_head_idx;
164 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
165 "vq->hw->cvq = %p vq = %p",
166 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
168 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
169 rte_spinlock_unlock(&cvq->lock);
173 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
174 sizeof(struct virtio_pmd_ctrl));
177 * Format is enforced in qemu code:
178 * One TX packet for header;
179 * At least one TX packet per argument;
180 * One RX packet for ACK.
182 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
183 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
184 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
186 i = vq->vq_ring.desc[head].next;
188 for (k = 0; k < pkt_num; k++) {
189 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
190 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
191 + sizeof(struct virtio_net_ctrl_hdr)
192 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
193 vq->vq_ring.desc[i].len = dlen[k];
196 i = vq->vq_ring.desc[i].next;
199 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
200 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
201 + sizeof(struct virtio_net_ctrl_hdr);
202 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
205 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
207 vq_update_avail_ring(vq, head);
208 vq_update_avail_idx(vq);
210 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
212 virtqueue_notify(vq);
215 while (VIRTQUEUE_NUSED(vq) == 0) {
220 while (VIRTQUEUE_NUSED(vq)) {
221 uint32_t idx, desc_idx, used_idx;
222 struct vring_used_elem *uep;
224 used_idx = (uint32_t)(vq->vq_used_cons_idx
225 & (vq->vq_nentries - 1));
226 uep = &vq->vq_ring.used->ring[used_idx];
227 idx = (uint32_t) uep->id;
230 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
231 desc_idx = vq->vq_ring.desc[desc_idx].next;
235 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
236 vq->vq_desc_head_idx = idx;
238 vq->vq_used_cons_idx++;
242 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
243 vq->vq_free_cnt, vq->vq_desc_head_idx);
245 result = cvq->virtio_net_hdr_mz->addr;
247 rte_spinlock_unlock(&cvq->lock);
248 return result->status;
252 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
254 struct virtio_hw *hw = dev->data->dev_private;
255 struct virtio_pmd_ctrl ctrl;
259 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
260 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
261 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
263 dlen[0] = sizeof(uint16_t);
265 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
267 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
268 "failed, this is too late now...");
276 virtio_dev_queue_release(void *queue __rte_unused)
282 virtio_get_nr_vq(struct virtio_hw *hw)
284 uint16_t nr_vq = hw->max_queue_pairs * 2;
286 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
293 virtio_init_vring(struct virtqueue *vq)
295 int size = vq->vq_nentries;
296 struct vring *vr = &vq->vq_ring;
297 uint8_t *ring_mem = vq->vq_ring_virt_mem;
299 PMD_INIT_FUNC_TRACE();
302 * Reinitialise since virtio port might have been stopped and restarted
304 memset(ring_mem, 0, vq->vq_ring_size);
305 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
306 vq->vq_used_cons_idx = 0;
307 vq->vq_desc_head_idx = 0;
308 vq->vq_avail_idx = 0;
309 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
310 vq->vq_free_cnt = vq->vq_nentries;
311 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
313 vring_desc_init(vr->desc, size);
316 * Disable device(host) interrupting guest
318 virtqueue_disable_intr(vq);
322 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
324 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
325 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
326 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
327 unsigned int vq_size, size;
328 struct virtio_hw *hw = dev->data->dev_private;
329 struct virtnet_rx *rxvq = NULL;
330 struct virtnet_tx *txvq = NULL;
331 struct virtnet_ctl *cvq = NULL;
332 struct virtqueue *vq;
333 size_t sz_hdr_mz = 0;
334 void *sw_ring = NULL;
335 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
338 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
341 * Read the virtqueue size from the Queue Size field
342 * Always power of 2 and if 0 virtqueue does not exist
344 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
345 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
347 PMD_INIT_LOG(ERR, "virtqueue does not exist");
351 if (!rte_is_power_of_2(vq_size)) {
352 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
356 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
357 dev->data->port_id, vtpci_queue_idx);
359 size = RTE_ALIGN_CEIL(sizeof(*vq) +
360 vq_size * sizeof(struct vq_desc_extra),
361 RTE_CACHE_LINE_SIZE);
362 if (queue_type == VTNET_TQ) {
364 * For each xmit packet, allocate a virtio_net_hdr
365 * and indirect ring elements
367 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
368 } else if (queue_type == VTNET_CQ) {
369 /* Allocate a page for control vq command, data and status */
370 sz_hdr_mz = PAGE_SIZE;
373 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
376 PMD_INIT_LOG(ERR, "can not allocate vq");
379 hw->vqs[vtpci_queue_idx] = vq;
382 vq->vq_queue_index = vtpci_queue_idx;
383 vq->vq_nentries = vq_size;
386 * Reserve a memzone for vring elements
388 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
389 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
390 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
391 size, vq->vq_ring_size);
393 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
395 0, VIRTIO_PCI_VRING_ALIGN);
397 if (rte_errno == EEXIST)
398 mz = rte_memzone_lookup(vq_name);
405 memset(mz->addr, 0, mz->len);
407 vq->vq_ring_mem = mz->iova;
408 vq->vq_ring_virt_mem = mz->addr;
409 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
411 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
412 (uint64_t)(uintptr_t)mz->addr);
414 virtio_init_vring(vq);
417 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
418 dev->data->port_id, vtpci_queue_idx);
419 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
421 RTE_CACHE_LINE_SIZE);
422 if (hdr_mz == NULL) {
423 if (rte_errno == EEXIST)
424 hdr_mz = rte_memzone_lookup(vq_hdr_name);
425 if (hdr_mz == NULL) {
432 if (queue_type == VTNET_RQ) {
433 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
434 sizeof(vq->sw_ring[0]);
436 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
437 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
439 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
444 vq->sw_ring = sw_ring;
447 rxvq->port_id = dev->data->port_id;
449 } else if (queue_type == VTNET_TQ) {
452 txvq->port_id = dev->data->port_id;
454 txvq->virtio_net_hdr_mz = hdr_mz;
455 txvq->virtio_net_hdr_mem = hdr_mz->iova;
456 } else if (queue_type == VTNET_CQ) {
460 cvq->virtio_net_hdr_mz = hdr_mz;
461 cvq->virtio_net_hdr_mem = hdr_mz->iova;
462 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
467 /* For virtio_user case (that is when hw->dev is NULL), we use
468 * virtual address. And we need properly set _offset_, please see
469 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
471 if (!hw->virtio_user_dev)
472 vq->offset = offsetof(struct rte_mbuf, buf_iova);
474 vq->vq_ring_mem = (uintptr_t)mz->addr;
475 vq->offset = offsetof(struct rte_mbuf, buf_addr);
476 if (queue_type == VTNET_TQ)
477 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
478 else if (queue_type == VTNET_CQ)
479 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
482 if (queue_type == VTNET_TQ) {
483 struct virtio_tx_region *txr;
487 memset(txr, 0, vq_size * sizeof(*txr));
488 for (i = 0; i < vq_size; i++) {
489 struct vring_desc *start_dp = txr[i].tx_indir;
491 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
493 /* first indirect descriptor is always the tx header */
494 start_dp->addr = txvq->virtio_net_hdr_mem
496 + offsetof(struct virtio_tx_region, tx_hdr);
498 start_dp->len = hw->vtnet_hdr_size;
499 start_dp->flags = VRING_DESC_F_NEXT;
503 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
504 PMD_INIT_LOG(ERR, "setup_queue failed");
512 rte_memzone_free(hdr_mz);
513 rte_memzone_free(mz);
520 virtio_free_queues(struct virtio_hw *hw)
522 uint16_t nr_vq = virtio_get_nr_vq(hw);
523 struct virtqueue *vq;
530 for (i = 0; i < nr_vq; i++) {
535 queue_type = virtio_get_queue_type(hw, i);
536 if (queue_type == VTNET_RQ) {
537 rte_free(vq->sw_ring);
538 rte_memzone_free(vq->rxq.mz);
539 } else if (queue_type == VTNET_TQ) {
540 rte_memzone_free(vq->txq.mz);
541 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
543 rte_memzone_free(vq->cq.mz);
544 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
556 virtio_alloc_queues(struct rte_eth_dev *dev)
558 struct virtio_hw *hw = dev->data->dev_private;
559 uint16_t nr_vq = virtio_get_nr_vq(hw);
563 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
565 PMD_INIT_LOG(ERR, "failed to allocate vqs");
569 for (i = 0; i < nr_vq; i++) {
570 ret = virtio_init_queue(dev, i);
572 virtio_free_queues(hw);
580 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
583 virtio_dev_close(struct rte_eth_dev *dev)
585 struct virtio_hw *hw = dev->data->dev_private;
586 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
588 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
591 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
592 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
594 virtio_queues_unbind_intr(dev);
596 if (intr_conf->lsc || intr_conf->rxq) {
597 virtio_intr_disable(dev);
598 rte_intr_efd_disable(dev->intr_handle);
599 rte_free(dev->intr_handle->intr_vec);
600 dev->intr_handle->intr_vec = NULL;
604 virtio_dev_free_mbufs(dev);
605 virtio_free_queues(hw);
609 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
611 struct virtio_hw *hw = dev->data->dev_private;
612 struct virtio_pmd_ctrl ctrl;
616 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
617 PMD_INIT_LOG(INFO, "host does not support rx control");
621 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
622 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
626 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
628 PMD_INIT_LOG(ERR, "Failed to enable promisc");
632 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
634 struct virtio_hw *hw = dev->data->dev_private;
635 struct virtio_pmd_ctrl ctrl;
639 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
640 PMD_INIT_LOG(INFO, "host does not support rx control");
644 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
645 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
649 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
651 PMD_INIT_LOG(ERR, "Failed to disable promisc");
655 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
657 struct virtio_hw *hw = dev->data->dev_private;
658 struct virtio_pmd_ctrl ctrl;
662 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
663 PMD_INIT_LOG(INFO, "host does not support rx control");
667 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
668 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
672 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
674 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
678 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
680 struct virtio_hw *hw = dev->data->dev_private;
681 struct virtio_pmd_ctrl ctrl;
685 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
686 PMD_INIT_LOG(INFO, "host does not support rx control");
690 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
691 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
695 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
697 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
700 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
702 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
704 struct virtio_hw *hw = dev->data->dev_private;
705 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
707 uint32_t frame_size = mtu + ether_hdr_len;
708 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
710 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
712 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
713 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
714 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
721 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
723 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
724 struct virtqueue *vq = rxvq->vq;
726 virtqueue_enable_intr(vq);
731 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
733 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
734 struct virtqueue *vq = rxvq->vq;
736 virtqueue_disable_intr(vq);
741 * dev_ops for virtio, bare necessities for basic operation
743 static const struct eth_dev_ops virtio_eth_dev_ops = {
744 .dev_configure = virtio_dev_configure,
745 .dev_start = virtio_dev_start,
746 .dev_stop = virtio_dev_stop,
747 .dev_close = virtio_dev_close,
748 .promiscuous_enable = virtio_dev_promiscuous_enable,
749 .promiscuous_disable = virtio_dev_promiscuous_disable,
750 .allmulticast_enable = virtio_dev_allmulticast_enable,
751 .allmulticast_disable = virtio_dev_allmulticast_disable,
752 .mtu_set = virtio_mtu_set,
753 .dev_infos_get = virtio_dev_info_get,
754 .stats_get = virtio_dev_stats_get,
755 .xstats_get = virtio_dev_xstats_get,
756 .xstats_get_names = virtio_dev_xstats_get_names,
757 .stats_reset = virtio_dev_stats_reset,
758 .xstats_reset = virtio_dev_stats_reset,
759 .link_update = virtio_dev_link_update,
760 .vlan_offload_set = virtio_dev_vlan_offload_set,
761 .rx_queue_setup = virtio_dev_rx_queue_setup,
762 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
763 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
764 .rx_queue_release = virtio_dev_queue_release,
765 .rx_descriptor_done = virtio_dev_rx_queue_done,
766 .tx_queue_setup = virtio_dev_tx_queue_setup,
767 .tx_queue_release = virtio_dev_queue_release,
768 /* collect stats per queue */
769 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
770 .vlan_filter_set = virtio_vlan_filter_set,
771 .mac_addr_add = virtio_mac_addr_add,
772 .mac_addr_remove = virtio_mac_addr_remove,
773 .mac_addr_set = virtio_mac_addr_set,
777 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
781 for (i = 0; i < dev->data->nb_tx_queues; i++) {
782 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
786 stats->opackets += txvq->stats.packets;
787 stats->obytes += txvq->stats.bytes;
788 stats->oerrors += txvq->stats.errors;
790 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
791 stats->q_opackets[i] = txvq->stats.packets;
792 stats->q_obytes[i] = txvq->stats.bytes;
796 for (i = 0; i < dev->data->nb_rx_queues; i++) {
797 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
801 stats->ipackets += rxvq->stats.packets;
802 stats->ibytes += rxvq->stats.bytes;
803 stats->ierrors += rxvq->stats.errors;
805 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
806 stats->q_ipackets[i] = rxvq->stats.packets;
807 stats->q_ibytes[i] = rxvq->stats.bytes;
811 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
814 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
815 struct rte_eth_xstat_name *xstats_names,
816 __rte_unused unsigned limit)
822 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
823 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
825 if (xstats_names != NULL) {
826 /* Note: limit checked in rte_eth_xstats_names() */
828 for (i = 0; i < dev->data->nb_rx_queues; i++) {
829 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
832 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
833 snprintf(xstats_names[count].name,
834 sizeof(xstats_names[count].name),
836 rte_virtio_rxq_stat_strings[t].name);
841 for (i = 0; i < dev->data->nb_tx_queues; i++) {
842 struct virtnet_tx *txvq = dev->data->tx_queues[i];
845 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
846 snprintf(xstats_names[count].name,
847 sizeof(xstats_names[count].name),
849 rte_virtio_txq_stat_strings[t].name);
859 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
865 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
866 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
871 for (i = 0; i < dev->data->nb_rx_queues; i++) {
872 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
879 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
880 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
881 rte_virtio_rxq_stat_strings[t].offset);
882 xstats[count].id = count;
887 for (i = 0; i < dev->data->nb_tx_queues; i++) {
888 struct virtnet_tx *txvq = dev->data->tx_queues[i];
895 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
896 xstats[count].value = *(uint64_t *)(((char *)txvq) +
897 rte_virtio_txq_stat_strings[t].offset);
898 xstats[count].id = count;
907 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
909 virtio_update_stats(dev, stats);
915 virtio_dev_stats_reset(struct rte_eth_dev *dev)
919 for (i = 0; i < dev->data->nb_tx_queues; i++) {
920 struct virtnet_tx *txvq = dev->data->tx_queues[i];
924 txvq->stats.packets = 0;
925 txvq->stats.bytes = 0;
926 txvq->stats.errors = 0;
927 txvq->stats.multicast = 0;
928 txvq->stats.broadcast = 0;
929 memset(txvq->stats.size_bins, 0,
930 sizeof(txvq->stats.size_bins[0]) * 8);
933 for (i = 0; i < dev->data->nb_rx_queues; i++) {
934 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
938 rxvq->stats.packets = 0;
939 rxvq->stats.bytes = 0;
940 rxvq->stats.errors = 0;
941 rxvq->stats.multicast = 0;
942 rxvq->stats.broadcast = 0;
943 memset(rxvq->stats.size_bins, 0,
944 sizeof(rxvq->stats.size_bins[0]) * 8);
949 virtio_set_hwaddr(struct virtio_hw *hw)
951 vtpci_write_dev_config(hw,
952 offsetof(struct virtio_net_config, mac),
953 &hw->mac_addr, ETHER_ADDR_LEN);
957 virtio_get_hwaddr(struct virtio_hw *hw)
959 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
960 vtpci_read_dev_config(hw,
961 offsetof(struct virtio_net_config, mac),
962 &hw->mac_addr, ETHER_ADDR_LEN);
964 eth_random_addr(&hw->mac_addr[0]);
965 virtio_set_hwaddr(hw);
970 virtio_mac_table_set(struct virtio_hw *hw,
971 const struct virtio_net_ctrl_mac *uc,
972 const struct virtio_net_ctrl_mac *mc)
974 struct virtio_pmd_ctrl ctrl;
977 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
978 PMD_DRV_LOG(INFO, "host does not support mac table");
982 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
983 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
985 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
986 memcpy(ctrl.data, uc, len[0]);
988 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
989 memcpy(ctrl.data + len[0], mc, len[1]);
991 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
993 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
998 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
999 uint32_t index, uint32_t vmdq __rte_unused)
1001 struct virtio_hw *hw = dev->data->dev_private;
1002 const struct ether_addr *addrs = dev->data->mac_addrs;
1004 struct virtio_net_ctrl_mac *uc, *mc;
1006 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1007 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1011 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1013 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1016 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1017 const struct ether_addr *addr
1018 = (i == index) ? mac_addr : addrs + i;
1019 struct virtio_net_ctrl_mac *tbl
1020 = is_multicast_ether_addr(addr) ? mc : uc;
1022 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1025 return virtio_mac_table_set(hw, uc, mc);
1029 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1031 struct virtio_hw *hw = dev->data->dev_private;
1032 struct ether_addr *addrs = dev->data->mac_addrs;
1033 struct virtio_net_ctrl_mac *uc, *mc;
1036 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1037 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1041 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1043 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1046 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1047 struct virtio_net_ctrl_mac *tbl;
1049 if (i == index || is_zero_ether_addr(addrs + i))
1052 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1053 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1056 virtio_mac_table_set(hw, uc, mc);
1060 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1062 struct virtio_hw *hw = dev->data->dev_private;
1064 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1066 /* Use atomic update if available */
1067 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1068 struct virtio_pmd_ctrl ctrl;
1069 int len = ETHER_ADDR_LEN;
1071 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1072 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1074 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1075 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1076 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1077 virtio_set_hwaddr(hw);
1081 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1083 struct virtio_hw *hw = dev->data->dev_private;
1084 struct virtio_pmd_ctrl ctrl;
1087 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1090 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1091 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1092 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1093 len = sizeof(vlan_id);
1095 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1099 virtio_intr_enable(struct rte_eth_dev *dev)
1101 struct virtio_hw *hw = dev->data->dev_private;
1103 if (rte_intr_enable(dev->intr_handle) < 0)
1106 if (!hw->virtio_user_dev)
1107 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1113 virtio_intr_disable(struct rte_eth_dev *dev)
1115 struct virtio_hw *hw = dev->data->dev_private;
1117 if (rte_intr_disable(dev->intr_handle) < 0)
1120 if (!hw->virtio_user_dev)
1121 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1127 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1129 uint64_t host_features;
1131 /* Prepare guest_features: feature that driver wants to support */
1132 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1135 /* Read device(host) feature bits */
1136 host_features = VTPCI_OPS(hw)->get_features(hw);
1137 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1140 /* If supported, ensure MTU value is valid before acknowledging it. */
1141 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1142 struct virtio_net_config config;
1144 vtpci_read_dev_config(hw,
1145 offsetof(struct virtio_net_config, mtu),
1146 &config.mtu, sizeof(config.mtu));
1148 if (config.mtu < ETHER_MIN_MTU)
1149 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1153 * Negotiate features: Subset of device feature bits are written back
1154 * guest feature bits.
1156 hw->guest_features = req_features;
1157 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1158 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1159 hw->guest_features);
1162 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1164 "VIRTIO_F_VERSION_1 features is not enabled.");
1167 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1168 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1170 "failed to set FEATURES_OK status!");
1175 hw->req_guest_features = req_features;
1181 virtio_dev_pause(struct rte_eth_dev *dev)
1183 struct virtio_hw *hw = dev->data->dev_private;
1185 rte_spinlock_lock(&hw->state_lock);
1187 if (hw->started == 0) {
1188 /* Device is just stopped. */
1189 rte_spinlock_unlock(&hw->state_lock);
1194 * Prevent the worker threads from touching queues to avoid contention,
1195 * 1 ms should be enough for the ongoing Tx function to finish.
1202 * Recover hw state to let the worker threads continue.
1205 virtio_dev_resume(struct rte_eth_dev *dev)
1207 struct virtio_hw *hw = dev->data->dev_private;
1210 rte_spinlock_unlock(&hw->state_lock);
1214 * Should be called only after device is paused.
1217 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1220 struct virtio_hw *hw = dev->data->dev_private;
1221 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1224 hw->inject_pkts = tx_pkts;
1225 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1226 hw->inject_pkts = NULL;
1232 virtio_notify_peers(struct rte_eth_dev *dev)
1234 struct virtio_hw *hw = dev->data->dev_private;
1235 struct virtnet_rx *rxvq = dev->data->rx_queues[0];
1236 struct rte_mbuf *rarp_mbuf;
1238 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1239 (struct ether_addr *)hw->mac_addr);
1240 if (rarp_mbuf == NULL) {
1241 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1245 /* If virtio port just stopped, no need to send RARP */
1246 if (virtio_dev_pause(dev) < 0) {
1247 rte_pktmbuf_free(rarp_mbuf);
1251 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1252 virtio_dev_resume(dev);
1256 virtio_ack_link_announce(struct rte_eth_dev *dev)
1258 struct virtio_hw *hw = dev->data->dev_private;
1259 struct virtio_pmd_ctrl ctrl;
1261 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1262 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1264 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1268 * Process virtio config changed interrupt. Call the callback
1269 * if link state changed, generate gratuitous RARP packet if
1270 * the status indicates an ANNOUNCE.
1273 virtio_interrupt_handler(void *param)
1275 struct rte_eth_dev *dev = param;
1276 struct virtio_hw *hw = dev->data->dev_private;
1279 /* Read interrupt status which clears interrupt */
1280 isr = vtpci_isr(hw);
1281 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1283 if (virtio_intr_enable(dev) < 0)
1284 PMD_DRV_LOG(ERR, "interrupt enable failed");
1286 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1287 if (virtio_dev_link_update(dev, 0) == 0)
1288 _rte_eth_dev_callback_process(dev,
1289 RTE_ETH_EVENT_INTR_LSC,
1293 if (isr & VIRTIO_NET_S_ANNOUNCE) {
1294 virtio_notify_peers(dev);
1295 virtio_ack_link_announce(dev);
1299 /* set rx and tx handlers according to what is supported */
1301 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1303 struct virtio_hw *hw = eth_dev->data->dev_private;
1305 if (hw->use_simple_rx) {
1306 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1307 eth_dev->data->port_id);
1308 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1309 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1311 "virtio: using mergeable buffer Rx path on port %u",
1312 eth_dev->data->port_id);
1313 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1315 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1316 eth_dev->data->port_id);
1317 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1320 if (hw->use_simple_tx) {
1321 PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
1322 eth_dev->data->port_id);
1323 eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1325 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1326 eth_dev->data->port_id);
1327 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1331 /* Only support 1:1 queue/interrupt mapping so far.
1332 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1333 * interrupt vectors (<N+1).
1336 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1339 struct virtio_hw *hw = dev->data->dev_private;
1341 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1342 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1343 dev->intr_handle->intr_vec[i] = i + 1;
1344 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1345 VIRTIO_MSI_NO_VECTOR) {
1346 PMD_DRV_LOG(ERR, "failed to set queue vector");
1355 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1358 struct virtio_hw *hw = dev->data->dev_private;
1360 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1361 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1362 VTPCI_OPS(hw)->set_queue_irq(hw,
1363 hw->vqs[i * VTNET_CQ],
1364 VIRTIO_MSI_NO_VECTOR);
1368 virtio_configure_intr(struct rte_eth_dev *dev)
1370 struct virtio_hw *hw = dev->data->dev_private;
1372 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1373 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1377 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1378 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1382 if (!dev->intr_handle->intr_vec) {
1383 dev->intr_handle->intr_vec =
1384 rte_zmalloc("intr_vec",
1385 hw->max_queue_pairs * sizeof(int), 0);
1386 if (!dev->intr_handle->intr_vec) {
1387 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1388 hw->max_queue_pairs);
1393 /* Re-register callback to update max_intr */
1394 rte_intr_callback_unregister(dev->intr_handle,
1395 virtio_interrupt_handler,
1397 rte_intr_callback_register(dev->intr_handle,
1398 virtio_interrupt_handler,
1401 /* DO NOT try to remove this! This function will enable msix, or QEMU
1402 * will encounter SIGSEGV when DRIVER_OK is sent.
1403 * And for legacy devices, this should be done before queue/vec binding
1404 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1405 * (22) will be ignored.
1407 if (virtio_intr_enable(dev) < 0) {
1408 PMD_DRV_LOG(ERR, "interrupt enable failed");
1412 if (virtio_queues_bind_intr(dev) < 0) {
1413 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1420 /* reset device and renegotiate features if needed */
1422 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1424 struct virtio_hw *hw = eth_dev->data->dev_private;
1425 struct virtio_net_config *config;
1426 struct virtio_net_config local_config;
1427 struct rte_pci_device *pci_dev = NULL;
1430 /* Reset the device although not necessary at startup */
1434 virtio_dev_free_mbufs(eth_dev);
1435 virtio_free_queues(hw);
1438 /* Tell the host we've noticed this device. */
1439 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1441 /* Tell the host we've known how to drive the device. */
1442 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1443 if (virtio_negotiate_features(hw, req_features) < 0)
1446 if (!hw->virtio_user_dev) {
1447 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1448 rte_eth_copy_pci_info(eth_dev, pci_dev);
1451 /* If host does not support both status and MSI-X then disable LSC */
1452 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1453 hw->use_msix != VIRTIO_MSIX_NONE)
1454 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1456 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1458 /* Setting up rx_header size for the device */
1459 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1460 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1461 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1463 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1465 /* Copy the permanent MAC address to: virtio_hw */
1466 virtio_get_hwaddr(hw);
1467 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1468 ð_dev->data->mac_addrs[0]);
1470 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1471 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1472 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1474 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1475 config = &local_config;
1477 vtpci_read_dev_config(hw,
1478 offsetof(struct virtio_net_config, mac),
1479 &config->mac, sizeof(config->mac));
1481 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1482 vtpci_read_dev_config(hw,
1483 offsetof(struct virtio_net_config, status),
1484 &config->status, sizeof(config->status));
1487 "VIRTIO_NET_F_STATUS is not supported");
1491 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1492 vtpci_read_dev_config(hw,
1493 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1494 &config->max_virtqueue_pairs,
1495 sizeof(config->max_virtqueue_pairs));
1498 "VIRTIO_NET_F_MQ is not supported");
1499 config->max_virtqueue_pairs = 1;
1502 hw->max_queue_pairs = config->max_virtqueue_pairs;
1504 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1505 vtpci_read_dev_config(hw,
1506 offsetof(struct virtio_net_config, mtu),
1508 sizeof(config->mtu));
1511 * MTU value has already been checked at negotiation
1512 * time, but check again in case it has changed since
1513 * then, which should not happen.
1515 if (config->mtu < ETHER_MIN_MTU) {
1516 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1521 hw->max_mtu = config->mtu;
1522 /* Set initial MTU to maximum one supported by vhost */
1523 eth_dev->data->mtu = config->mtu;
1526 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1527 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1530 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1531 config->max_virtqueue_pairs);
1532 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1534 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1535 config->mac[0], config->mac[1],
1536 config->mac[2], config->mac[3],
1537 config->mac[4], config->mac[5]);
1539 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1540 hw->max_queue_pairs = 1;
1541 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1542 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1545 ret = virtio_alloc_queues(eth_dev);
1549 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1550 if (virtio_configure_intr(eth_dev) < 0) {
1551 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1556 vtpci_reinit_complete(hw);
1559 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1560 eth_dev->data->port_id, pci_dev->id.vendor_id,
1561 pci_dev->id.device_id);
1567 * Remap the PCI device again (IO port map for legacy device and
1568 * memory map for modern device), so that the secondary process
1569 * could have the PCI initiated correctly.
1572 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1576 * We don't have to re-parse the PCI config space, since
1577 * rte_pci_map_device() makes sure the mapped address
1578 * in secondary process would equal to the one mapped in
1579 * the primary process: error will be returned if that
1580 * requirement is not met.
1582 * That said, we could simply reuse all cap pointers
1583 * (such as dev_cfg, common_cfg, etc.) parsed from the
1584 * primary process, which is stored in shared memory.
1586 if (rte_pci_map_device(pci_dev)) {
1587 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1591 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1599 virtio_set_vtpci_ops(struct virtio_hw *hw)
1601 #ifdef RTE_VIRTIO_USER
1602 if (hw->virtio_user_dev)
1603 VTPCI_OPS(hw) = &virtio_user_ops;
1607 VTPCI_OPS(hw) = &modern_ops;
1609 VTPCI_OPS(hw) = &legacy_ops;
1613 * This function is based on probe() function in virtio_pci.c
1614 * It returns 0 on success.
1617 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1619 struct virtio_hw *hw = eth_dev->data->dev_private;
1622 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1624 eth_dev->dev_ops = &virtio_eth_dev_ops;
1626 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1627 if (!hw->virtio_user_dev) {
1628 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1633 virtio_set_vtpci_ops(hw);
1634 set_rxtx_funcs(eth_dev);
1639 /* Allocate memory for storing MAC addresses */
1640 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1641 if (eth_dev->data->mac_addrs == NULL) {
1643 "Failed to allocate %d bytes needed to store MAC addresses",
1644 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1648 hw->port_id = eth_dev->data->port_id;
1649 /* For virtio_user case the hw->virtio_user_dev is populated by
1650 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1652 if (!hw->virtio_user_dev) {
1653 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1658 /* reset device and negotiate default features */
1659 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1663 /* Setup interrupt callback */
1664 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1665 rte_intr_callback_register(eth_dev->intr_handle,
1666 virtio_interrupt_handler, eth_dev);
1671 rte_free(eth_dev->data->mac_addrs);
1676 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1678 PMD_INIT_FUNC_TRACE();
1680 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1683 virtio_dev_stop(eth_dev);
1684 virtio_dev_close(eth_dev);
1686 eth_dev->dev_ops = NULL;
1687 eth_dev->tx_pkt_burst = NULL;
1688 eth_dev->rx_pkt_burst = NULL;
1690 rte_free(eth_dev->data->mac_addrs);
1691 eth_dev->data->mac_addrs = NULL;
1693 /* reset interrupt callback */
1694 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1695 rte_intr_callback_unregister(eth_dev->intr_handle,
1696 virtio_interrupt_handler,
1698 if (eth_dev->device)
1699 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1701 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1706 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1707 struct rte_pci_device *pci_dev)
1709 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1710 eth_virtio_dev_init);
1713 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1715 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1718 static struct rte_pci_driver rte_virtio_pmd = {
1720 .name = "net_virtio",
1722 .id_table = pci_id_virtio_map,
1724 .probe = eth_virtio_pci_probe,
1725 .remove = eth_virtio_pci_remove,
1728 RTE_INIT(rte_virtio_pmd_init);
1730 rte_virtio_pmd_init(void)
1732 if (rte_eal_iopl_init() != 0) {
1733 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1737 rte_pci_register(&rte_virtio_pmd);
1741 * Configure virtio device
1742 * It returns 0 on success.
1745 virtio_dev_configure(struct rte_eth_dev *dev)
1747 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1748 struct virtio_hw *hw = dev->data->dev_private;
1749 uint64_t req_features;
1752 PMD_INIT_LOG(DEBUG, "configure");
1753 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1755 if (dev->data->dev_conf.intr_conf.rxq) {
1756 ret = virtio_init_device(dev, hw->req_guest_features);
1761 /* The name hw_ip_checksum is a bit confusing since it can be
1762 * set by the application to request L3 and/or L4 checksums. In
1763 * case of virtio, only L4 checksum is supported.
1765 if (rxmode->hw_ip_checksum)
1766 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1768 if (rxmode->enable_lro)
1770 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1771 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1773 /* if request features changed, reinit the device */
1774 if (req_features != hw->req_guest_features) {
1775 ret = virtio_init_device(dev, req_features);
1780 if (rxmode->hw_ip_checksum &&
1781 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
1783 "rx checksum not available on this host");
1787 if (rxmode->enable_lro &&
1788 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1789 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
1791 "Large Receive Offload not available on this host");
1795 /* start control queue */
1796 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1797 virtio_dev_cq_start(dev);
1799 hw->vlan_strip = rxmode->hw_vlan_strip;
1801 if (rxmode->hw_vlan_filter
1802 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1804 "vlan filtering not available on this host");
1808 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1809 /* Enable vector (0) for Link State Intrerrupt */
1810 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1811 VIRTIO_MSI_NO_VECTOR) {
1812 PMD_DRV_LOG(ERR, "failed to set config vector");
1816 rte_spinlock_init(&hw->state_lock);
1818 hw->use_simple_rx = 1;
1819 hw->use_simple_tx = 1;
1821 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
1822 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
1823 hw->use_simple_rx = 0;
1824 hw->use_simple_tx = 0;
1827 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1828 hw->use_simple_rx = 0;
1829 hw->use_simple_tx = 0;
1832 if (rxmode->hw_ip_checksum)
1833 hw->use_simple_rx = 0;
1840 virtio_dev_start(struct rte_eth_dev *dev)
1842 uint16_t nb_queues, i;
1843 struct virtnet_rx *rxvq;
1844 struct virtnet_tx *txvq __rte_unused;
1845 struct virtio_hw *hw = dev->data->dev_private;
1848 /* Finish the initialization of the queues */
1849 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1850 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1854 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1855 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1860 /* check if lsc interrupt feature is enabled */
1861 if (dev->data->dev_conf.intr_conf.lsc) {
1862 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1863 PMD_DRV_LOG(ERR, "link status not supported by host");
1868 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1869 * in device configure, but it could be unmapped when device is
1872 if (dev->data->dev_conf.intr_conf.lsc ||
1873 dev->data->dev_conf.intr_conf.rxq) {
1874 virtio_intr_disable(dev);
1876 if (virtio_intr_enable(dev) < 0) {
1877 PMD_DRV_LOG(ERR, "interrupt enable failed");
1882 /*Notify the backend
1883 *Otherwise the tap backend might already stop its queue due to fullness.
1884 *vhost backend will have no chance to be waked up
1886 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1887 if (hw->max_queue_pairs > 1) {
1888 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1892 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1894 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1895 rxvq = dev->data->rx_queues[i];
1896 /* Flush the old packets */
1897 virtqueue_rxvq_flush(rxvq->vq);
1898 virtqueue_notify(rxvq->vq);
1901 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1902 txvq = dev->data->tx_queues[i];
1903 virtqueue_notify(txvq->vq);
1906 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1908 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1909 rxvq = dev->data->rx_queues[i];
1910 VIRTQUEUE_DUMP(rxvq->vq);
1913 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1914 txvq = dev->data->tx_queues[i];
1915 VIRTQUEUE_DUMP(txvq->vq);
1918 set_rxtx_funcs(dev);
1921 /* Initialize Link state */
1922 virtio_dev_link_update(dev, 0);
1927 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1929 struct virtio_hw *hw = dev->data->dev_private;
1930 uint16_t nr_vq = virtio_get_nr_vq(hw);
1931 const char *type __rte_unused;
1932 unsigned int i, mbuf_num = 0;
1933 struct virtqueue *vq;
1934 struct rte_mbuf *buf;
1937 if (hw->vqs == NULL)
1940 for (i = 0; i < nr_vq; i++) {
1945 queue_type = virtio_get_queue_type(hw, i);
1946 if (queue_type == VTNET_RQ)
1948 else if (queue_type == VTNET_TQ)
1954 "Before freeing %s[%d] used and unused buf",
1958 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
1959 rte_pktmbuf_free(buf);
1964 "After freeing %s[%d] used and unused buf",
1969 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
1973 * Stop device: disable interrupt and mark link down
1976 virtio_dev_stop(struct rte_eth_dev *dev)
1978 struct virtio_hw *hw = dev->data->dev_private;
1979 struct rte_eth_link link;
1980 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
1982 PMD_INIT_LOG(DEBUG, "stop");
1984 rte_spinlock_lock(&hw->state_lock);
1985 if (intr_conf->lsc || intr_conf->rxq)
1986 virtio_intr_disable(dev);
1989 memset(&link, 0, sizeof(link));
1990 rte_eth_linkstatus_set(dev, &link);
1991 rte_spinlock_unlock(&hw->state_lock);
1995 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1997 struct rte_eth_link link;
1999 struct virtio_hw *hw = dev->data->dev_private;
2001 memset(&link, 0, sizeof(link));
2002 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2003 link.link_speed = ETH_SPEED_NUM_10G;
2004 link.link_autoneg = ETH_LINK_FIXED;
2006 if (hw->started == 0) {
2007 link.link_status = ETH_LINK_DOWN;
2008 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2009 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2010 vtpci_read_dev_config(hw,
2011 offsetof(struct virtio_net_config, status),
2012 &status, sizeof(status));
2013 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2014 link.link_status = ETH_LINK_DOWN;
2015 PMD_INIT_LOG(DEBUG, "Port %d is down",
2016 dev->data->port_id);
2018 link.link_status = ETH_LINK_UP;
2019 PMD_INIT_LOG(DEBUG, "Port %d is up",
2020 dev->data->port_id);
2023 link.link_status = ETH_LINK_UP;
2026 return rte_eth_linkstatus_set(dev, &link);
2030 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2032 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2033 struct virtio_hw *hw = dev->data->dev_private;
2035 if (mask & ETH_VLAN_FILTER_MASK) {
2036 if (rxmode->hw_vlan_filter &&
2037 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2040 "vlan filtering not available on this host");
2046 if (mask & ETH_VLAN_STRIP_MASK)
2047 hw->vlan_strip = rxmode->hw_vlan_strip;
2053 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2055 uint64_t tso_mask, host_features;
2056 struct virtio_hw *hw = dev->data->dev_private;
2058 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2060 dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
2061 dev_info->max_rx_queues =
2062 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2063 dev_info->max_tx_queues =
2064 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2065 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2066 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2067 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2068 dev_info->default_txconf = (struct rte_eth_txconf) {
2069 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
2072 host_features = VTPCI_OPS(hw)->get_features(hw);
2073 dev_info->rx_offload_capa = 0;
2074 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2075 dev_info->rx_offload_capa |=
2076 DEV_RX_OFFLOAD_TCP_CKSUM |
2077 DEV_RX_OFFLOAD_UDP_CKSUM;
2079 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2080 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2081 if ((host_features & tso_mask) == tso_mask)
2082 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2084 dev_info->tx_offload_capa = 0;
2085 if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2086 dev_info->tx_offload_capa |=
2087 DEV_TX_OFFLOAD_UDP_CKSUM |
2088 DEV_TX_OFFLOAD_TCP_CKSUM;
2090 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2091 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2092 if ((hw->guest_features & tso_mask) == tso_mask)
2093 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2097 * It enables testpmd to collect per queue stats.
2100 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2101 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2102 __rte_unused uint8_t is_rx)
2107 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2108 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2109 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2111 RTE_INIT(virtio_init_log);
2113 virtio_init_log(void)
2115 virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2116 if (virtio_logtype_init >= 0)
2117 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2118 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2119 if (virtio_logtype_driver >= 0)
2120 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);