1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <rte_ethdev.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_atomic.h>
18 #include <rte_branch_prediction.h>
20 #include <rte_bus_pci.h>
21 #include <rte_ether.h>
22 #include <rte_common.h>
23 #include <rte_errno.h>
24 #include <rte_cpuflags.h>
26 #include <rte_memory.h>
30 #include "virtio_ethdev.h"
31 #include "virtio_pci.h"
32 #include "virtio_logs.h"
33 #include "virtqueue.h"
34 #include "virtio_rxtx.h"
36 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
37 static int virtio_dev_configure(struct rte_eth_dev *dev);
38 static int virtio_dev_start(struct rte_eth_dev *dev);
39 static void virtio_dev_stop(struct rte_eth_dev *dev);
40 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
41 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
42 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
43 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
44 static void virtio_dev_info_get(struct rte_eth_dev *dev,
45 struct rte_eth_dev_info *dev_info);
46 static int virtio_dev_link_update(struct rte_eth_dev *dev,
47 int wait_to_complete);
48 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
50 static void virtio_set_hwaddr(struct virtio_hw *hw);
51 static void virtio_get_hwaddr(struct virtio_hw *hw);
53 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
54 struct rte_eth_stats *stats);
55 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
56 struct rte_eth_xstat *xstats, unsigned n);
57 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
58 struct rte_eth_xstat_name *xstats_names,
60 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
61 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
62 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
63 uint16_t vlan_id, int on);
64 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
65 struct ether_addr *mac_addr,
66 uint32_t index, uint32_t vmdq);
67 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
68 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
69 struct ether_addr *mac_addr);
71 static int virtio_intr_enable(struct rte_eth_dev *dev);
72 static int virtio_intr_disable(struct rte_eth_dev *dev);
74 static int virtio_dev_queue_stats_mapping_set(
75 struct rte_eth_dev *eth_dev,
81 * The set of PCI devices this driver supports
83 static const struct rte_pci_id pci_id_virtio_map[] = {
84 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
85 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
86 { .vendor_id = 0, /* sentinel */ },
89 struct rte_virtio_xstats_name_off {
90 char name[RTE_ETH_XSTATS_NAME_SIZE];
94 /* [rt]x_qX_ is prepended to the name string here */
95 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
96 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
97 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
98 {"errors", offsetof(struct virtnet_rx, stats.errors)},
99 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
100 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
101 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
102 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
103 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
104 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
105 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
106 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
107 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
108 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
111 /* [rt]x_qX_ is prepended to the name string here */
112 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
113 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
114 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
115 {"errors", offsetof(struct virtnet_tx, stats.errors)},
116 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
117 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
118 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
119 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
120 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
121 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
122 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
123 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
124 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
125 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
128 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
129 sizeof(rte_virtio_rxq_stat_strings[0]))
130 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
131 sizeof(rte_virtio_txq_stat_strings[0]))
133 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
136 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
137 int *dlen, int pkt_num)
141 virtio_net_ctrl_ack status = ~0;
142 struct virtio_pmd_ctrl *result;
143 struct virtqueue *vq;
145 ctrl->status = status;
147 if (!cvq || !cvq->vq) {
148 PMD_INIT_LOG(ERR, "Control queue is not supported.");
152 head = vq->vq_desc_head_idx;
154 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
155 "vq->hw->cvq = %p vq = %p",
156 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
158 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
161 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
162 sizeof(struct virtio_pmd_ctrl));
165 * Format is enforced in qemu code:
166 * One TX packet for header;
167 * At least one TX packet per argument;
168 * One RX packet for ACK.
170 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
171 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
172 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
174 i = vq->vq_ring.desc[head].next;
176 for (k = 0; k < pkt_num; k++) {
177 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
178 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
179 + sizeof(struct virtio_net_ctrl_hdr)
180 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
181 vq->vq_ring.desc[i].len = dlen[k];
184 i = vq->vq_ring.desc[i].next;
187 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
188 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
189 + sizeof(struct virtio_net_ctrl_hdr);
190 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
193 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
195 vq_update_avail_ring(vq, head);
196 vq_update_avail_idx(vq);
198 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
200 virtqueue_notify(vq);
203 while (VIRTQUEUE_NUSED(vq) == 0) {
208 while (VIRTQUEUE_NUSED(vq)) {
209 uint32_t idx, desc_idx, used_idx;
210 struct vring_used_elem *uep;
212 used_idx = (uint32_t)(vq->vq_used_cons_idx
213 & (vq->vq_nentries - 1));
214 uep = &vq->vq_ring.used->ring[used_idx];
215 idx = (uint32_t) uep->id;
218 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
219 desc_idx = vq->vq_ring.desc[desc_idx].next;
223 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
224 vq->vq_desc_head_idx = idx;
226 vq->vq_used_cons_idx++;
230 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
231 vq->vq_free_cnt, vq->vq_desc_head_idx);
233 result = cvq->virtio_net_hdr_mz->addr;
235 return result->status;
239 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
241 struct virtio_hw *hw = dev->data->dev_private;
242 struct virtio_pmd_ctrl ctrl;
246 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
247 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
248 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
250 dlen[0] = sizeof(uint16_t);
252 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
254 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
255 "failed, this is too late now...");
263 virtio_dev_queue_release(void *queue __rte_unused)
269 virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
271 if (vtpci_queue_idx == hw->max_queue_pairs * 2)
273 else if (vtpci_queue_idx % 2 == 0)
280 virtio_get_nr_vq(struct virtio_hw *hw)
282 uint16_t nr_vq = hw->max_queue_pairs * 2;
284 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
291 virtio_init_vring(struct virtqueue *vq)
293 int size = vq->vq_nentries;
294 struct vring *vr = &vq->vq_ring;
295 uint8_t *ring_mem = vq->vq_ring_virt_mem;
297 PMD_INIT_FUNC_TRACE();
300 * Reinitialise since virtio port might have been stopped and restarted
302 memset(ring_mem, 0, vq->vq_ring_size);
303 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
304 vq->vq_used_cons_idx = 0;
305 vq->vq_desc_head_idx = 0;
306 vq->vq_avail_idx = 0;
307 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
308 vq->vq_free_cnt = vq->vq_nentries;
309 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
311 vring_desc_init(vr->desc, size);
314 * Disable device(host) interrupting guest
316 virtqueue_disable_intr(vq);
320 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
322 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
323 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
324 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
325 unsigned int vq_size, size;
326 struct virtio_hw *hw = dev->data->dev_private;
327 struct virtnet_rx *rxvq = NULL;
328 struct virtnet_tx *txvq = NULL;
329 struct virtnet_ctl *cvq = NULL;
330 struct virtqueue *vq;
331 size_t sz_hdr_mz = 0;
332 void *sw_ring = NULL;
333 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
336 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
339 * Read the virtqueue size from the Queue Size field
340 * Always power of 2 and if 0 virtqueue does not exist
342 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
343 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
345 PMD_INIT_LOG(ERR, "virtqueue does not exist");
349 if (!rte_is_power_of_2(vq_size)) {
350 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
354 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
355 dev->data->port_id, vtpci_queue_idx);
357 size = RTE_ALIGN_CEIL(sizeof(*vq) +
358 vq_size * sizeof(struct vq_desc_extra),
359 RTE_CACHE_LINE_SIZE);
360 if (queue_type == VTNET_TQ) {
362 * For each xmit packet, allocate a virtio_net_hdr
363 * and indirect ring elements
365 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
366 } else if (queue_type == VTNET_CQ) {
367 /* Allocate a page for control vq command, data and status */
368 sz_hdr_mz = PAGE_SIZE;
371 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
374 PMD_INIT_LOG(ERR, "can not allocate vq");
377 hw->vqs[vtpci_queue_idx] = vq;
380 vq->vq_queue_index = vtpci_queue_idx;
381 vq->vq_nentries = vq_size;
384 * Reserve a memzone for vring elements
386 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
387 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
388 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
389 size, vq->vq_ring_size);
391 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
393 0, VIRTIO_PCI_VRING_ALIGN);
395 if (rte_errno == EEXIST)
396 mz = rte_memzone_lookup(vq_name);
403 memset(mz->addr, 0, mz->len);
405 vq->vq_ring_mem = mz->iova;
406 vq->vq_ring_virt_mem = mz->addr;
407 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
409 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
410 (uint64_t)(uintptr_t)mz->addr);
412 virtio_init_vring(vq);
415 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
416 dev->data->port_id, vtpci_queue_idx);
417 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
419 RTE_CACHE_LINE_SIZE);
420 if (hdr_mz == NULL) {
421 if (rte_errno == EEXIST)
422 hdr_mz = rte_memzone_lookup(vq_hdr_name);
423 if (hdr_mz == NULL) {
430 if (queue_type == VTNET_RQ) {
431 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
432 sizeof(vq->sw_ring[0]);
434 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
435 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
437 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
442 vq->sw_ring = sw_ring;
445 rxvq->port_id = dev->data->port_id;
447 } else if (queue_type == VTNET_TQ) {
450 txvq->port_id = dev->data->port_id;
452 txvq->virtio_net_hdr_mz = hdr_mz;
453 txvq->virtio_net_hdr_mem = hdr_mz->iova;
454 } else if (queue_type == VTNET_CQ) {
458 cvq->virtio_net_hdr_mz = hdr_mz;
459 cvq->virtio_net_hdr_mem = hdr_mz->iova;
460 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
465 /* For virtio_user case (that is when hw->dev is NULL), we use
466 * virtual address. And we need properly set _offset_, please see
467 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
469 if (!hw->virtio_user_dev)
470 vq->offset = offsetof(struct rte_mbuf, buf_iova);
472 vq->vq_ring_mem = (uintptr_t)mz->addr;
473 vq->offset = offsetof(struct rte_mbuf, buf_addr);
474 if (queue_type == VTNET_TQ)
475 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
476 else if (queue_type == VTNET_CQ)
477 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
480 if (queue_type == VTNET_TQ) {
481 struct virtio_tx_region *txr;
485 memset(txr, 0, vq_size * sizeof(*txr));
486 for (i = 0; i < vq_size; i++) {
487 struct vring_desc *start_dp = txr[i].tx_indir;
489 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
491 /* first indirect descriptor is always the tx header */
492 start_dp->addr = txvq->virtio_net_hdr_mem
494 + offsetof(struct virtio_tx_region, tx_hdr);
496 start_dp->len = hw->vtnet_hdr_size;
497 start_dp->flags = VRING_DESC_F_NEXT;
501 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
502 PMD_INIT_LOG(ERR, "setup_queue failed");
510 rte_memzone_free(hdr_mz);
511 rte_memzone_free(mz);
518 virtio_free_queues(struct virtio_hw *hw)
520 uint16_t nr_vq = virtio_get_nr_vq(hw);
521 struct virtqueue *vq;
528 for (i = 0; i < nr_vq; i++) {
533 queue_type = virtio_get_queue_type(hw, i);
534 if (queue_type == VTNET_RQ) {
535 rte_free(vq->sw_ring);
536 rte_memzone_free(vq->rxq.mz);
537 } else if (queue_type == VTNET_TQ) {
538 rte_memzone_free(vq->txq.mz);
539 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
541 rte_memzone_free(vq->cq.mz);
542 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
554 virtio_alloc_queues(struct rte_eth_dev *dev)
556 struct virtio_hw *hw = dev->data->dev_private;
557 uint16_t nr_vq = virtio_get_nr_vq(hw);
561 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
563 PMD_INIT_LOG(ERR, "failed to allocate vqs");
567 for (i = 0; i < nr_vq; i++) {
568 ret = virtio_init_queue(dev, i);
570 virtio_free_queues(hw);
578 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
581 virtio_dev_close(struct rte_eth_dev *dev)
583 struct virtio_hw *hw = dev->data->dev_private;
584 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
586 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
589 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
590 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
592 virtio_queues_unbind_intr(dev);
594 if (intr_conf->lsc || intr_conf->rxq) {
595 virtio_intr_disable(dev);
596 rte_intr_efd_disable(dev->intr_handle);
597 rte_free(dev->intr_handle->intr_vec);
598 dev->intr_handle->intr_vec = NULL;
602 virtio_dev_free_mbufs(dev);
603 virtio_free_queues(hw);
607 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
609 struct virtio_hw *hw = dev->data->dev_private;
610 struct virtio_pmd_ctrl ctrl;
614 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
615 PMD_INIT_LOG(INFO, "host does not support rx control");
619 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
620 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
624 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
626 PMD_INIT_LOG(ERR, "Failed to enable promisc");
630 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
632 struct virtio_hw *hw = dev->data->dev_private;
633 struct virtio_pmd_ctrl ctrl;
637 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
638 PMD_INIT_LOG(INFO, "host does not support rx control");
642 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
643 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
647 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
649 PMD_INIT_LOG(ERR, "Failed to disable promisc");
653 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
655 struct virtio_hw *hw = dev->data->dev_private;
656 struct virtio_pmd_ctrl ctrl;
660 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
661 PMD_INIT_LOG(INFO, "host does not support rx control");
665 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
666 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
670 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
672 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
676 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
678 struct virtio_hw *hw = dev->data->dev_private;
679 struct virtio_pmd_ctrl ctrl;
683 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
684 PMD_INIT_LOG(INFO, "host does not support rx control");
688 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
689 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
693 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
695 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
698 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
700 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
702 struct virtio_hw *hw = dev->data->dev_private;
703 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
705 uint32_t frame_size = mtu + ether_hdr_len;
706 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
708 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
710 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
711 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
712 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
719 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
721 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
722 struct virtqueue *vq = rxvq->vq;
724 virtqueue_enable_intr(vq);
729 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
731 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
732 struct virtqueue *vq = rxvq->vq;
734 virtqueue_disable_intr(vq);
739 * dev_ops for virtio, bare necessities for basic operation
741 static const struct eth_dev_ops virtio_eth_dev_ops = {
742 .dev_configure = virtio_dev_configure,
743 .dev_start = virtio_dev_start,
744 .dev_stop = virtio_dev_stop,
745 .dev_close = virtio_dev_close,
746 .promiscuous_enable = virtio_dev_promiscuous_enable,
747 .promiscuous_disable = virtio_dev_promiscuous_disable,
748 .allmulticast_enable = virtio_dev_allmulticast_enable,
749 .allmulticast_disable = virtio_dev_allmulticast_disable,
750 .mtu_set = virtio_mtu_set,
751 .dev_infos_get = virtio_dev_info_get,
752 .stats_get = virtio_dev_stats_get,
753 .xstats_get = virtio_dev_xstats_get,
754 .xstats_get_names = virtio_dev_xstats_get_names,
755 .stats_reset = virtio_dev_stats_reset,
756 .xstats_reset = virtio_dev_stats_reset,
757 .link_update = virtio_dev_link_update,
758 .vlan_offload_set = virtio_dev_vlan_offload_set,
759 .rx_queue_setup = virtio_dev_rx_queue_setup,
760 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
761 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
762 .rx_queue_release = virtio_dev_queue_release,
763 .rx_descriptor_done = virtio_dev_rx_queue_done,
764 .tx_queue_setup = virtio_dev_tx_queue_setup,
765 .tx_queue_release = virtio_dev_queue_release,
766 /* collect stats per queue */
767 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
768 .vlan_filter_set = virtio_vlan_filter_set,
769 .mac_addr_add = virtio_mac_addr_add,
770 .mac_addr_remove = virtio_mac_addr_remove,
771 .mac_addr_set = virtio_mac_addr_set,
775 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
776 struct rte_eth_link *link)
778 struct rte_eth_link *dst = link;
779 struct rte_eth_link *src = &(dev->data->dev_link);
781 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
782 *(uint64_t *)src) == 0)
789 * Atomically writes the link status information into global
790 * structure rte_eth_dev.
793 * - Pointer to the structure rte_eth_dev to read from.
794 * - Pointer to the buffer to be saved with the link status.
797 * - On success, zero.
798 * - On failure, negative value.
801 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
802 struct rte_eth_link *link)
804 struct rte_eth_link *dst = &(dev->data->dev_link);
805 struct rte_eth_link *src = link;
807 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
808 *(uint64_t *)src) == 0)
815 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
819 for (i = 0; i < dev->data->nb_tx_queues; i++) {
820 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
824 stats->opackets += txvq->stats.packets;
825 stats->obytes += txvq->stats.bytes;
826 stats->oerrors += txvq->stats.errors;
828 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
829 stats->q_opackets[i] = txvq->stats.packets;
830 stats->q_obytes[i] = txvq->stats.bytes;
834 for (i = 0; i < dev->data->nb_rx_queues; i++) {
835 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
839 stats->ipackets += rxvq->stats.packets;
840 stats->ibytes += rxvq->stats.bytes;
841 stats->ierrors += rxvq->stats.errors;
843 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
844 stats->q_ipackets[i] = rxvq->stats.packets;
845 stats->q_ibytes[i] = rxvq->stats.bytes;
849 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
852 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
853 struct rte_eth_xstat_name *xstats_names,
854 __rte_unused unsigned limit)
860 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
861 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
863 if (xstats_names != NULL) {
864 /* Note: limit checked in rte_eth_xstats_names() */
866 for (i = 0; i < dev->data->nb_rx_queues; i++) {
867 struct virtqueue *rxvq = dev->data->rx_queues[i];
870 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
871 snprintf(xstats_names[count].name,
872 sizeof(xstats_names[count].name),
874 rte_virtio_rxq_stat_strings[t].name);
879 for (i = 0; i < dev->data->nb_tx_queues; i++) {
880 struct virtqueue *txvq = dev->data->tx_queues[i];
883 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
884 snprintf(xstats_names[count].name,
885 sizeof(xstats_names[count].name),
887 rte_virtio_txq_stat_strings[t].name);
897 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
903 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
904 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
909 for (i = 0; i < dev->data->nb_rx_queues; i++) {
910 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
917 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
918 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
919 rte_virtio_rxq_stat_strings[t].offset);
920 xstats[count].id = count;
925 for (i = 0; i < dev->data->nb_tx_queues; i++) {
926 struct virtnet_tx *txvq = dev->data->tx_queues[i];
933 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
934 xstats[count].value = *(uint64_t *)(((char *)txvq) +
935 rte_virtio_txq_stat_strings[t].offset);
936 xstats[count].id = count;
945 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
947 virtio_update_stats(dev, stats);
953 virtio_dev_stats_reset(struct rte_eth_dev *dev)
957 for (i = 0; i < dev->data->nb_tx_queues; i++) {
958 struct virtnet_tx *txvq = dev->data->tx_queues[i];
962 txvq->stats.packets = 0;
963 txvq->stats.bytes = 0;
964 txvq->stats.errors = 0;
965 txvq->stats.multicast = 0;
966 txvq->stats.broadcast = 0;
967 memset(txvq->stats.size_bins, 0,
968 sizeof(txvq->stats.size_bins[0]) * 8);
971 for (i = 0; i < dev->data->nb_rx_queues; i++) {
972 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
976 rxvq->stats.packets = 0;
977 rxvq->stats.bytes = 0;
978 rxvq->stats.errors = 0;
979 rxvq->stats.multicast = 0;
980 rxvq->stats.broadcast = 0;
981 memset(rxvq->stats.size_bins, 0,
982 sizeof(rxvq->stats.size_bins[0]) * 8);
987 virtio_set_hwaddr(struct virtio_hw *hw)
989 vtpci_write_dev_config(hw,
990 offsetof(struct virtio_net_config, mac),
991 &hw->mac_addr, ETHER_ADDR_LEN);
995 virtio_get_hwaddr(struct virtio_hw *hw)
997 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
998 vtpci_read_dev_config(hw,
999 offsetof(struct virtio_net_config, mac),
1000 &hw->mac_addr, ETHER_ADDR_LEN);
1002 eth_random_addr(&hw->mac_addr[0]);
1003 virtio_set_hwaddr(hw);
1008 virtio_mac_table_set(struct virtio_hw *hw,
1009 const struct virtio_net_ctrl_mac *uc,
1010 const struct virtio_net_ctrl_mac *mc)
1012 struct virtio_pmd_ctrl ctrl;
1015 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1016 PMD_DRV_LOG(INFO, "host does not support mac table");
1020 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1021 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1023 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1024 memcpy(ctrl.data, uc, len[0]);
1026 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1027 memcpy(ctrl.data + len[0], mc, len[1]);
1029 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1031 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1036 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1037 uint32_t index, uint32_t vmdq __rte_unused)
1039 struct virtio_hw *hw = dev->data->dev_private;
1040 const struct ether_addr *addrs = dev->data->mac_addrs;
1042 struct virtio_net_ctrl_mac *uc, *mc;
1044 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1045 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1049 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1051 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1054 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1055 const struct ether_addr *addr
1056 = (i == index) ? mac_addr : addrs + i;
1057 struct virtio_net_ctrl_mac *tbl
1058 = is_multicast_ether_addr(addr) ? mc : uc;
1060 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1063 return virtio_mac_table_set(hw, uc, mc);
1067 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1069 struct virtio_hw *hw = dev->data->dev_private;
1070 struct ether_addr *addrs = dev->data->mac_addrs;
1071 struct virtio_net_ctrl_mac *uc, *mc;
1074 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1075 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1079 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1081 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1084 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1085 struct virtio_net_ctrl_mac *tbl;
1087 if (i == index || is_zero_ether_addr(addrs + i))
1090 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1091 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1094 virtio_mac_table_set(hw, uc, mc);
1098 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1100 struct virtio_hw *hw = dev->data->dev_private;
1102 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1104 /* Use atomic update if available */
1105 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1106 struct virtio_pmd_ctrl ctrl;
1107 int len = ETHER_ADDR_LEN;
1109 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1110 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1112 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1113 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1114 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1115 virtio_set_hwaddr(hw);
1119 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1121 struct virtio_hw *hw = dev->data->dev_private;
1122 struct virtio_pmd_ctrl ctrl;
1125 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1128 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1129 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1130 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1131 len = sizeof(vlan_id);
1133 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1137 virtio_intr_enable(struct rte_eth_dev *dev)
1139 struct virtio_hw *hw = dev->data->dev_private;
1141 if (rte_intr_enable(dev->intr_handle) < 0)
1144 if (!hw->virtio_user_dev)
1145 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1151 virtio_intr_disable(struct rte_eth_dev *dev)
1153 struct virtio_hw *hw = dev->data->dev_private;
1155 if (rte_intr_disable(dev->intr_handle) < 0)
1158 if (!hw->virtio_user_dev)
1159 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1165 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1167 uint64_t host_features;
1169 /* Prepare guest_features: feature that driver wants to support */
1170 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1173 /* Read device(host) feature bits */
1174 host_features = VTPCI_OPS(hw)->get_features(hw);
1175 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1178 /* If supported, ensure MTU value is valid before acknowledging it. */
1179 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1180 struct virtio_net_config config;
1182 vtpci_read_dev_config(hw,
1183 offsetof(struct virtio_net_config, mtu),
1184 &config.mtu, sizeof(config.mtu));
1186 if (config.mtu < ETHER_MIN_MTU)
1187 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1191 * Negotiate features: Subset of device feature bits are written back
1192 * guest feature bits.
1194 hw->guest_features = req_features;
1195 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1196 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1197 hw->guest_features);
1200 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1202 "VIRTIO_F_VERSION_1 features is not enabled.");
1205 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1206 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1208 "failed to set FEATURES_OK status!");
1213 hw->req_guest_features = req_features;
1219 * Process Virtio Config changed interrupt and call the callback
1220 * if link state changed.
1223 virtio_interrupt_handler(void *param)
1225 struct rte_eth_dev *dev = param;
1226 struct virtio_hw *hw = dev->data->dev_private;
1229 /* Read interrupt status which clears interrupt */
1230 isr = vtpci_isr(hw);
1231 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1233 if (virtio_intr_enable(dev) < 0)
1234 PMD_DRV_LOG(ERR, "interrupt enable failed");
1236 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1237 if (virtio_dev_link_update(dev, 0) == 0)
1238 _rte_eth_dev_callback_process(dev,
1239 RTE_ETH_EVENT_INTR_LSC,
1245 /* set rx and tx handlers according to what is supported */
1247 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1249 struct virtio_hw *hw = eth_dev->data->dev_private;
1251 if (hw->use_simple_rx) {
1252 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1253 eth_dev->data->port_id);
1254 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1255 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1257 "virtio: using mergeable buffer Rx path on port %u",
1258 eth_dev->data->port_id);
1259 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1261 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1262 eth_dev->data->port_id);
1263 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1266 if (hw->use_simple_tx) {
1267 PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
1268 eth_dev->data->port_id);
1269 eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1271 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1272 eth_dev->data->port_id);
1273 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1277 /* Only support 1:1 queue/interrupt mapping so far.
1278 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1279 * interrupt vectors (<N+1).
1282 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1285 struct virtio_hw *hw = dev->data->dev_private;
1287 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1288 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1289 dev->intr_handle->intr_vec[i] = i + 1;
1290 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1291 VIRTIO_MSI_NO_VECTOR) {
1292 PMD_DRV_LOG(ERR, "failed to set queue vector");
1301 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1304 struct virtio_hw *hw = dev->data->dev_private;
1306 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1307 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1308 VTPCI_OPS(hw)->set_queue_irq(hw,
1309 hw->vqs[i * VTNET_CQ],
1310 VIRTIO_MSI_NO_VECTOR);
1314 virtio_configure_intr(struct rte_eth_dev *dev)
1316 struct virtio_hw *hw = dev->data->dev_private;
1318 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1319 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1323 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1324 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1328 if (!dev->intr_handle->intr_vec) {
1329 dev->intr_handle->intr_vec =
1330 rte_zmalloc("intr_vec",
1331 hw->max_queue_pairs * sizeof(int), 0);
1332 if (!dev->intr_handle->intr_vec) {
1333 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1334 hw->max_queue_pairs);
1339 /* Re-register callback to update max_intr */
1340 rte_intr_callback_unregister(dev->intr_handle,
1341 virtio_interrupt_handler,
1343 rte_intr_callback_register(dev->intr_handle,
1344 virtio_interrupt_handler,
1347 /* DO NOT try to remove this! This function will enable msix, or QEMU
1348 * will encounter SIGSEGV when DRIVER_OK is sent.
1349 * And for legacy devices, this should be done before queue/vec binding
1350 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1351 * (22) will be ignored.
1353 if (virtio_intr_enable(dev) < 0) {
1354 PMD_DRV_LOG(ERR, "interrupt enable failed");
1358 if (virtio_queues_bind_intr(dev) < 0) {
1359 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1366 /* reset device and renegotiate features if needed */
1368 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1370 struct virtio_hw *hw = eth_dev->data->dev_private;
1371 struct virtio_net_config *config;
1372 struct virtio_net_config local_config;
1373 struct rte_pci_device *pci_dev = NULL;
1376 /* Reset the device although not necessary at startup */
1379 /* Tell the host we've noticed this device. */
1380 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1382 /* Tell the host we've known how to drive the device. */
1383 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1384 if (virtio_negotiate_features(hw, req_features) < 0)
1387 if (!hw->virtio_user_dev) {
1388 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1389 rte_eth_copy_pci_info(eth_dev, pci_dev);
1392 /* If host does not support both status and MSI-X then disable LSC */
1393 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1394 hw->use_msix != VIRTIO_MSIX_NONE)
1395 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1397 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1399 /* Setting up rx_header size for the device */
1400 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1401 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1402 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1404 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1406 /* Copy the permanent MAC address to: virtio_hw */
1407 virtio_get_hwaddr(hw);
1408 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1409 ð_dev->data->mac_addrs[0]);
1411 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1412 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1413 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1415 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1416 config = &local_config;
1418 vtpci_read_dev_config(hw,
1419 offsetof(struct virtio_net_config, mac),
1420 &config->mac, sizeof(config->mac));
1422 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1423 vtpci_read_dev_config(hw,
1424 offsetof(struct virtio_net_config, status),
1425 &config->status, sizeof(config->status));
1428 "VIRTIO_NET_F_STATUS is not supported");
1432 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1433 vtpci_read_dev_config(hw,
1434 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1435 &config->max_virtqueue_pairs,
1436 sizeof(config->max_virtqueue_pairs));
1439 "VIRTIO_NET_F_MQ is not supported");
1440 config->max_virtqueue_pairs = 1;
1443 hw->max_queue_pairs = config->max_virtqueue_pairs;
1445 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1446 vtpci_read_dev_config(hw,
1447 offsetof(struct virtio_net_config, mtu),
1449 sizeof(config->mtu));
1452 * MTU value has already been checked at negotiation
1453 * time, but check again in case it has changed since
1454 * then, which should not happen.
1456 if (config->mtu < ETHER_MIN_MTU) {
1457 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1462 hw->max_mtu = config->mtu;
1463 /* Set initial MTU to maximum one supported by vhost */
1464 eth_dev->data->mtu = config->mtu;
1467 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1468 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1471 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1472 config->max_virtqueue_pairs);
1473 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1475 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1476 config->mac[0], config->mac[1],
1477 config->mac[2], config->mac[3],
1478 config->mac[4], config->mac[5]);
1480 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1481 hw->max_queue_pairs = 1;
1484 ret = virtio_alloc_queues(eth_dev);
1488 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1489 if (virtio_configure_intr(eth_dev) < 0) {
1490 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1495 vtpci_reinit_complete(hw);
1498 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1499 eth_dev->data->port_id, pci_dev->id.vendor_id,
1500 pci_dev->id.device_id);
1506 * Remap the PCI device again (IO port map for legacy device and
1507 * memory map for modern device), so that the secondary process
1508 * could have the PCI initiated correctly.
1511 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1515 * We don't have to re-parse the PCI config space, since
1516 * rte_pci_map_device() makes sure the mapped address
1517 * in secondary process would equal to the one mapped in
1518 * the primary process: error will be returned if that
1519 * requirement is not met.
1521 * That said, we could simply reuse all cap pointers
1522 * (such as dev_cfg, common_cfg, etc.) parsed from the
1523 * primary process, which is stored in shared memory.
1525 if (rte_pci_map_device(pci_dev)) {
1526 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1530 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1538 virtio_set_vtpci_ops(struct virtio_hw *hw)
1540 #ifdef RTE_VIRTIO_USER
1541 if (hw->virtio_user_dev)
1542 VTPCI_OPS(hw) = &virtio_user_ops;
1546 VTPCI_OPS(hw) = &modern_ops;
1548 VTPCI_OPS(hw) = &legacy_ops;
1552 * This function is based on probe() function in virtio_pci.c
1553 * It returns 0 on success.
1556 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1558 struct virtio_hw *hw = eth_dev->data->dev_private;
1561 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1563 eth_dev->dev_ops = &virtio_eth_dev_ops;
1565 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1566 if (!hw->virtio_user_dev) {
1567 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1572 virtio_set_vtpci_ops(hw);
1573 set_rxtx_funcs(eth_dev);
1578 /* Allocate memory for storing MAC addresses */
1579 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1580 if (eth_dev->data->mac_addrs == NULL) {
1582 "Failed to allocate %d bytes needed to store MAC addresses",
1583 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1587 hw->port_id = eth_dev->data->port_id;
1588 /* For virtio_user case the hw->virtio_user_dev is populated by
1589 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1591 if (!hw->virtio_user_dev) {
1592 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1597 /* reset device and negotiate default features */
1598 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1602 /* Setup interrupt callback */
1603 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1604 rte_intr_callback_register(eth_dev->intr_handle,
1605 virtio_interrupt_handler, eth_dev);
1610 rte_free(eth_dev->data->mac_addrs);
1615 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1617 PMD_INIT_FUNC_TRACE();
1619 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1622 virtio_dev_stop(eth_dev);
1623 virtio_dev_close(eth_dev);
1625 eth_dev->dev_ops = NULL;
1626 eth_dev->tx_pkt_burst = NULL;
1627 eth_dev->rx_pkt_burst = NULL;
1629 rte_free(eth_dev->data->mac_addrs);
1630 eth_dev->data->mac_addrs = NULL;
1632 /* reset interrupt callback */
1633 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1634 rte_intr_callback_unregister(eth_dev->intr_handle,
1635 virtio_interrupt_handler,
1637 if (eth_dev->device)
1638 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1640 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1645 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1646 struct rte_pci_device *pci_dev)
1648 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1649 eth_virtio_dev_init);
1652 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1654 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1657 static struct rte_pci_driver rte_virtio_pmd = {
1659 .name = "net_virtio",
1661 .id_table = pci_id_virtio_map,
1663 .probe = eth_virtio_pci_probe,
1664 .remove = eth_virtio_pci_remove,
1667 RTE_INIT(rte_virtio_pmd_init);
1669 rte_virtio_pmd_init(void)
1671 if (rte_eal_iopl_init() != 0) {
1672 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1676 rte_pci_register(&rte_virtio_pmd);
1680 * Configure virtio device
1681 * It returns 0 on success.
1684 virtio_dev_configure(struct rte_eth_dev *dev)
1686 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1687 struct virtio_hw *hw = dev->data->dev_private;
1688 uint64_t req_features;
1691 PMD_INIT_LOG(DEBUG, "configure");
1692 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1694 if (dev->data->dev_conf.intr_conf.rxq) {
1695 ret = virtio_init_device(dev, hw->req_guest_features);
1700 /* The name hw_ip_checksum is a bit confusing since it can be
1701 * set by the application to request L3 and/or L4 checksums. In
1702 * case of virtio, only L4 checksum is supported.
1704 if (rxmode->hw_ip_checksum)
1705 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1707 if (rxmode->enable_lro)
1709 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1710 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1712 /* if request features changed, reinit the device */
1713 if (req_features != hw->req_guest_features) {
1714 ret = virtio_init_device(dev, req_features);
1719 if (rxmode->hw_ip_checksum &&
1720 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
1722 "rx checksum not available on this host");
1726 if (rxmode->enable_lro &&
1727 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1728 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
1730 "Large Receive Offload not available on this host");
1734 /* start control queue */
1735 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1736 virtio_dev_cq_start(dev);
1738 hw->vlan_strip = rxmode->hw_vlan_strip;
1740 if (rxmode->hw_vlan_filter
1741 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1743 "vlan filtering not available on this host");
1747 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1748 /* Enable vector (0) for Link State Intrerrupt */
1749 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1750 VIRTIO_MSI_NO_VECTOR) {
1751 PMD_DRV_LOG(ERR, "failed to set config vector");
1755 hw->use_simple_rx = 1;
1756 hw->use_simple_tx = 1;
1758 #if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
1759 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
1760 hw->use_simple_rx = 0;
1761 hw->use_simple_tx = 0;
1764 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1765 hw->use_simple_rx = 0;
1766 hw->use_simple_tx = 0;
1769 if (rxmode->hw_ip_checksum)
1770 hw->use_simple_rx = 0;
1777 virtio_dev_start(struct rte_eth_dev *dev)
1779 uint16_t nb_queues, i;
1780 struct virtnet_rx *rxvq;
1781 struct virtnet_tx *txvq __rte_unused;
1782 struct virtio_hw *hw = dev->data->dev_private;
1785 /* Finish the initialization of the queues */
1786 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1787 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1791 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1792 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1797 /* check if lsc interrupt feature is enabled */
1798 if (dev->data->dev_conf.intr_conf.lsc) {
1799 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1800 PMD_DRV_LOG(ERR, "link status not supported by host");
1805 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1806 * in device configure, but it could be unmapped when device is
1809 if (dev->data->dev_conf.intr_conf.lsc ||
1810 dev->data->dev_conf.intr_conf.rxq) {
1811 virtio_intr_disable(dev);
1813 if (virtio_intr_enable(dev) < 0) {
1814 PMD_DRV_LOG(ERR, "interrupt enable failed");
1819 /*Notify the backend
1820 *Otherwise the tap backend might already stop its queue due to fullness.
1821 *vhost backend will have no chance to be waked up
1823 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1824 if (hw->max_queue_pairs > 1) {
1825 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1829 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1831 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1832 rxvq = dev->data->rx_queues[i];
1833 /* Flush the old packets */
1834 virtqueue_flush(rxvq->vq);
1835 virtqueue_notify(rxvq->vq);
1838 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1839 txvq = dev->data->tx_queues[i];
1840 virtqueue_notify(txvq->vq);
1843 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1845 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1846 rxvq = dev->data->rx_queues[i];
1847 VIRTQUEUE_DUMP(rxvq->vq);
1850 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1851 txvq = dev->data->tx_queues[i];
1852 VIRTQUEUE_DUMP(txvq->vq);
1855 set_rxtx_funcs(dev);
1858 /* Initialize Link state */
1859 virtio_dev_link_update(dev, 0);
1864 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1866 struct rte_mbuf *buf;
1867 int i, mbuf_num = 0;
1869 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1870 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1873 "Before freeing rxq[%d] used and unused buf", i);
1874 VIRTQUEUE_DUMP(rxvq->vq);
1876 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1877 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1878 rte_pktmbuf_free(buf);
1882 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1884 "After freeing rxq[%d] used and unused buf", i);
1885 VIRTQUEUE_DUMP(rxvq->vq);
1888 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1889 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1892 "Before freeing txq[%d] used and unused bufs",
1894 VIRTQUEUE_DUMP(txvq->vq);
1897 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1898 rte_pktmbuf_free(buf);
1902 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1904 "After freeing txq[%d] used and unused buf", i);
1905 VIRTQUEUE_DUMP(txvq->vq);
1910 * Stop device: disable interrupt and mark link down
1913 virtio_dev_stop(struct rte_eth_dev *dev)
1915 struct virtio_hw *hw = dev->data->dev_private;
1916 struct rte_eth_link link;
1917 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
1919 PMD_INIT_LOG(DEBUG, "stop");
1921 if (intr_conf->lsc || intr_conf->rxq)
1922 virtio_intr_disable(dev);
1925 memset(&link, 0, sizeof(link));
1926 virtio_dev_atomic_write_link_status(dev, &link);
1930 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1932 struct rte_eth_link link, old;
1934 struct virtio_hw *hw = dev->data->dev_private;
1935 memset(&link, 0, sizeof(link));
1936 virtio_dev_atomic_read_link_status(dev, &link);
1938 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1939 link.link_speed = ETH_SPEED_NUM_10G;
1941 if (hw->started == 0) {
1942 link.link_status = ETH_LINK_DOWN;
1943 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1944 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1945 vtpci_read_dev_config(hw,
1946 offsetof(struct virtio_net_config, status),
1947 &status, sizeof(status));
1948 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1949 link.link_status = ETH_LINK_DOWN;
1950 PMD_INIT_LOG(DEBUG, "Port %d is down",
1951 dev->data->port_id);
1953 link.link_status = ETH_LINK_UP;
1954 PMD_INIT_LOG(DEBUG, "Port %d is up",
1955 dev->data->port_id);
1958 link.link_status = ETH_LINK_UP;
1960 virtio_dev_atomic_write_link_status(dev, &link);
1962 return (old.link_status == link.link_status) ? -1 : 0;
1966 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1968 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1969 struct virtio_hw *hw = dev->data->dev_private;
1971 if (mask & ETH_VLAN_FILTER_MASK) {
1972 if (rxmode->hw_vlan_filter &&
1973 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1976 "vlan filtering not available on this host");
1982 if (mask & ETH_VLAN_STRIP_MASK)
1983 hw->vlan_strip = rxmode->hw_vlan_strip;
1989 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1991 uint64_t tso_mask, host_features;
1992 struct virtio_hw *hw = dev->data->dev_private;
1994 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
1996 dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
1997 dev_info->max_rx_queues =
1998 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
1999 dev_info->max_tx_queues =
2000 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2001 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2002 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2003 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2004 dev_info->default_txconf = (struct rte_eth_txconf) {
2005 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
2008 host_features = VTPCI_OPS(hw)->get_features(hw);
2009 dev_info->rx_offload_capa = 0;
2010 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2011 dev_info->rx_offload_capa |=
2012 DEV_RX_OFFLOAD_TCP_CKSUM |
2013 DEV_RX_OFFLOAD_UDP_CKSUM;
2015 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2016 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2017 if ((host_features & tso_mask) == tso_mask)
2018 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2020 dev_info->tx_offload_capa = 0;
2021 if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2022 dev_info->tx_offload_capa |=
2023 DEV_TX_OFFLOAD_UDP_CKSUM |
2024 DEV_TX_OFFLOAD_TCP_CKSUM;
2026 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2027 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2028 if ((hw->guest_features & tso_mask) == tso_mask)
2029 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2033 * It enables testpmd to collect per queue stats.
2036 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2037 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2038 __rte_unused uint8_t is_rx)
2043 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2044 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2045 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");