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,
80 int virtio_logtype_init;
81 int virtio_logtype_driver;
84 * The set of PCI devices this driver supports
86 static const struct rte_pci_id pci_id_virtio_map[] = {
87 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
88 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
89 { .vendor_id = 0, /* sentinel */ },
92 struct rte_virtio_xstats_name_off {
93 char name[RTE_ETH_XSTATS_NAME_SIZE];
97 /* [rt]x_qX_ is prepended to the name string here */
98 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
99 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
100 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
101 {"errors", offsetof(struct virtnet_rx, stats.errors)},
102 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
103 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
104 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
105 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
106 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
107 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
108 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
109 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
110 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
111 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
114 /* [rt]x_qX_ is prepended to the name string here */
115 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
116 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
117 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
118 {"errors", offsetof(struct virtnet_tx, stats.errors)},
119 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
120 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
121 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
122 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
123 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
124 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
125 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
126 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
127 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
128 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
131 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
132 sizeof(rte_virtio_rxq_stat_strings[0]))
133 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
134 sizeof(rte_virtio_txq_stat_strings[0]))
136 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
139 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
140 int *dlen, int pkt_num)
144 virtio_net_ctrl_ack status = ~0;
145 struct virtio_pmd_ctrl *result;
146 struct virtqueue *vq;
148 ctrl->status = status;
150 if (!cvq || !cvq->vq) {
151 PMD_INIT_LOG(ERR, "Control queue is not supported.");
155 rte_spinlock_lock(&cvq->lock);
157 head = vq->vq_desc_head_idx;
159 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
160 "vq->hw->cvq = %p vq = %p",
161 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
163 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
164 rte_spinlock_unlock(&cvq->lock);
168 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
169 sizeof(struct virtio_pmd_ctrl));
172 * Format is enforced in qemu code:
173 * One TX packet for header;
174 * At least one TX packet per argument;
175 * One RX packet for ACK.
177 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
178 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
179 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
181 i = vq->vq_ring.desc[head].next;
183 for (k = 0; k < pkt_num; k++) {
184 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
185 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
186 + sizeof(struct virtio_net_ctrl_hdr)
187 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
188 vq->vq_ring.desc[i].len = dlen[k];
191 i = vq->vq_ring.desc[i].next;
194 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
195 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
196 + sizeof(struct virtio_net_ctrl_hdr);
197 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
200 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
202 vq_update_avail_ring(vq, head);
203 vq_update_avail_idx(vq);
205 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
207 virtqueue_notify(vq);
210 while (VIRTQUEUE_NUSED(vq) == 0) {
215 while (VIRTQUEUE_NUSED(vq)) {
216 uint32_t idx, desc_idx, used_idx;
217 struct vring_used_elem *uep;
219 used_idx = (uint32_t)(vq->vq_used_cons_idx
220 & (vq->vq_nentries - 1));
221 uep = &vq->vq_ring.used->ring[used_idx];
222 idx = (uint32_t) uep->id;
225 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
226 desc_idx = vq->vq_ring.desc[desc_idx].next;
230 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
231 vq->vq_desc_head_idx = idx;
233 vq->vq_used_cons_idx++;
237 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
238 vq->vq_free_cnt, vq->vq_desc_head_idx);
240 result = cvq->virtio_net_hdr_mz->addr;
242 rte_spinlock_unlock(&cvq->lock);
243 return result->status;
247 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
249 struct virtio_hw *hw = dev->data->dev_private;
250 struct virtio_pmd_ctrl ctrl;
254 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
255 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
256 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
258 dlen[0] = sizeof(uint16_t);
260 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
262 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
263 "failed, this is too late now...");
271 virtio_dev_queue_release(void *queue __rte_unused)
277 virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
279 if (vtpci_queue_idx == hw->max_queue_pairs * 2)
281 else if (vtpci_queue_idx % 2 == 0)
288 virtio_get_nr_vq(struct virtio_hw *hw)
290 uint16_t nr_vq = hw->max_queue_pairs * 2;
292 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
299 virtio_init_vring(struct virtqueue *vq)
301 int size = vq->vq_nentries;
302 struct vring *vr = &vq->vq_ring;
303 uint8_t *ring_mem = vq->vq_ring_virt_mem;
305 PMD_INIT_FUNC_TRACE();
308 * Reinitialise since virtio port might have been stopped and restarted
310 memset(ring_mem, 0, vq->vq_ring_size);
311 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
312 vq->vq_used_cons_idx = 0;
313 vq->vq_desc_head_idx = 0;
314 vq->vq_avail_idx = 0;
315 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
316 vq->vq_free_cnt = vq->vq_nentries;
317 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
319 vring_desc_init(vr->desc, size);
322 * Disable device(host) interrupting guest
324 virtqueue_disable_intr(vq);
328 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
330 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
331 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
332 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
333 unsigned int vq_size, size;
334 struct virtio_hw *hw = dev->data->dev_private;
335 struct virtnet_rx *rxvq = NULL;
336 struct virtnet_tx *txvq = NULL;
337 struct virtnet_ctl *cvq = NULL;
338 struct virtqueue *vq;
339 size_t sz_hdr_mz = 0;
340 void *sw_ring = NULL;
341 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
344 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
347 * Read the virtqueue size from the Queue Size field
348 * Always power of 2 and if 0 virtqueue does not exist
350 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
351 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
353 PMD_INIT_LOG(ERR, "virtqueue does not exist");
357 if (!rte_is_power_of_2(vq_size)) {
358 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
362 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
363 dev->data->port_id, vtpci_queue_idx);
365 size = RTE_ALIGN_CEIL(sizeof(*vq) +
366 vq_size * sizeof(struct vq_desc_extra),
367 RTE_CACHE_LINE_SIZE);
368 if (queue_type == VTNET_TQ) {
370 * For each xmit packet, allocate a virtio_net_hdr
371 * and indirect ring elements
373 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
374 } else if (queue_type == VTNET_CQ) {
375 /* Allocate a page for control vq command, data and status */
376 sz_hdr_mz = PAGE_SIZE;
379 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
382 PMD_INIT_LOG(ERR, "can not allocate vq");
385 hw->vqs[vtpci_queue_idx] = vq;
388 vq->vq_queue_index = vtpci_queue_idx;
389 vq->vq_nentries = vq_size;
392 * Reserve a memzone for vring elements
394 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
395 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
396 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
397 size, vq->vq_ring_size);
399 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
401 0, VIRTIO_PCI_VRING_ALIGN);
403 if (rte_errno == EEXIST)
404 mz = rte_memzone_lookup(vq_name);
411 memset(mz->addr, 0, mz->len);
413 vq->vq_ring_mem = mz->iova;
414 vq->vq_ring_virt_mem = mz->addr;
415 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
417 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
418 (uint64_t)(uintptr_t)mz->addr);
420 virtio_init_vring(vq);
423 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
424 dev->data->port_id, vtpci_queue_idx);
425 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
427 RTE_CACHE_LINE_SIZE);
428 if (hdr_mz == NULL) {
429 if (rte_errno == EEXIST)
430 hdr_mz = rte_memzone_lookup(vq_hdr_name);
431 if (hdr_mz == NULL) {
438 if (queue_type == VTNET_RQ) {
439 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
440 sizeof(vq->sw_ring[0]);
442 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
443 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
445 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
450 vq->sw_ring = sw_ring;
453 rxvq->port_id = dev->data->port_id;
455 } else if (queue_type == VTNET_TQ) {
458 txvq->port_id = dev->data->port_id;
460 txvq->virtio_net_hdr_mz = hdr_mz;
461 txvq->virtio_net_hdr_mem = hdr_mz->iova;
462 } else if (queue_type == VTNET_CQ) {
466 cvq->virtio_net_hdr_mz = hdr_mz;
467 cvq->virtio_net_hdr_mem = hdr_mz->iova;
468 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
473 /* For virtio_user case (that is when hw->dev is NULL), we use
474 * virtual address. And we need properly set _offset_, please see
475 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
477 if (!hw->virtio_user_dev)
478 vq->offset = offsetof(struct rte_mbuf, buf_iova);
480 vq->vq_ring_mem = (uintptr_t)mz->addr;
481 vq->offset = offsetof(struct rte_mbuf, buf_addr);
482 if (queue_type == VTNET_TQ)
483 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
484 else if (queue_type == VTNET_CQ)
485 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
488 if (queue_type == VTNET_TQ) {
489 struct virtio_tx_region *txr;
493 memset(txr, 0, vq_size * sizeof(*txr));
494 for (i = 0; i < vq_size; i++) {
495 struct vring_desc *start_dp = txr[i].tx_indir;
497 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
499 /* first indirect descriptor is always the tx header */
500 start_dp->addr = txvq->virtio_net_hdr_mem
502 + offsetof(struct virtio_tx_region, tx_hdr);
504 start_dp->len = hw->vtnet_hdr_size;
505 start_dp->flags = VRING_DESC_F_NEXT;
509 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
510 PMD_INIT_LOG(ERR, "setup_queue failed");
518 rte_memzone_free(hdr_mz);
519 rte_memzone_free(mz);
526 virtio_free_queues(struct virtio_hw *hw)
528 uint16_t nr_vq = virtio_get_nr_vq(hw);
529 struct virtqueue *vq;
536 for (i = 0; i < nr_vq; i++) {
541 queue_type = virtio_get_queue_type(hw, i);
542 if (queue_type == VTNET_RQ) {
543 rte_free(vq->sw_ring);
544 rte_memzone_free(vq->rxq.mz);
545 } else if (queue_type == VTNET_TQ) {
546 rte_memzone_free(vq->txq.mz);
547 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
549 rte_memzone_free(vq->cq.mz);
550 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
562 virtio_alloc_queues(struct rte_eth_dev *dev)
564 struct virtio_hw *hw = dev->data->dev_private;
565 uint16_t nr_vq = virtio_get_nr_vq(hw);
569 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
571 PMD_INIT_LOG(ERR, "failed to allocate vqs");
575 for (i = 0; i < nr_vq; i++) {
576 ret = virtio_init_queue(dev, i);
578 virtio_free_queues(hw);
586 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
589 virtio_dev_close(struct rte_eth_dev *dev)
591 struct virtio_hw *hw = dev->data->dev_private;
592 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
594 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
597 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
598 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
600 virtio_queues_unbind_intr(dev);
602 if (intr_conf->lsc || intr_conf->rxq) {
603 virtio_intr_disable(dev);
604 rte_intr_efd_disable(dev->intr_handle);
605 rte_free(dev->intr_handle->intr_vec);
606 dev->intr_handle->intr_vec = NULL;
610 virtio_dev_free_mbufs(dev);
611 virtio_free_queues(hw);
615 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
617 struct virtio_hw *hw = dev->data->dev_private;
618 struct virtio_pmd_ctrl ctrl;
622 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
623 PMD_INIT_LOG(INFO, "host does not support rx control");
627 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
628 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
632 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
634 PMD_INIT_LOG(ERR, "Failed to enable promisc");
638 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
640 struct virtio_hw *hw = dev->data->dev_private;
641 struct virtio_pmd_ctrl ctrl;
645 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
646 PMD_INIT_LOG(INFO, "host does not support rx control");
650 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
651 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
655 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
657 PMD_INIT_LOG(ERR, "Failed to disable promisc");
661 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
663 struct virtio_hw *hw = dev->data->dev_private;
664 struct virtio_pmd_ctrl ctrl;
668 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
669 PMD_INIT_LOG(INFO, "host does not support rx control");
673 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
674 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
678 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
680 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
684 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
686 struct virtio_hw *hw = dev->data->dev_private;
687 struct virtio_pmd_ctrl ctrl;
691 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
692 PMD_INIT_LOG(INFO, "host does not support rx control");
696 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
697 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
701 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
703 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
706 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
708 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
710 struct virtio_hw *hw = dev->data->dev_private;
711 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
713 uint32_t frame_size = mtu + ether_hdr_len;
714 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
716 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
718 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
719 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
720 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
727 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
729 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
730 struct virtqueue *vq = rxvq->vq;
732 virtqueue_enable_intr(vq);
737 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
739 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
740 struct virtqueue *vq = rxvq->vq;
742 virtqueue_disable_intr(vq);
747 * dev_ops for virtio, bare necessities for basic operation
749 static const struct eth_dev_ops virtio_eth_dev_ops = {
750 .dev_configure = virtio_dev_configure,
751 .dev_start = virtio_dev_start,
752 .dev_stop = virtio_dev_stop,
753 .dev_close = virtio_dev_close,
754 .promiscuous_enable = virtio_dev_promiscuous_enable,
755 .promiscuous_disable = virtio_dev_promiscuous_disable,
756 .allmulticast_enable = virtio_dev_allmulticast_enable,
757 .allmulticast_disable = virtio_dev_allmulticast_disable,
758 .mtu_set = virtio_mtu_set,
759 .dev_infos_get = virtio_dev_info_get,
760 .stats_get = virtio_dev_stats_get,
761 .xstats_get = virtio_dev_xstats_get,
762 .xstats_get_names = virtio_dev_xstats_get_names,
763 .stats_reset = virtio_dev_stats_reset,
764 .xstats_reset = virtio_dev_stats_reset,
765 .link_update = virtio_dev_link_update,
766 .vlan_offload_set = virtio_dev_vlan_offload_set,
767 .rx_queue_setup = virtio_dev_rx_queue_setup,
768 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
769 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
770 .rx_queue_release = virtio_dev_queue_release,
771 .rx_descriptor_done = virtio_dev_rx_queue_done,
772 .tx_queue_setup = virtio_dev_tx_queue_setup,
773 .tx_queue_release = virtio_dev_queue_release,
774 /* collect stats per queue */
775 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
776 .vlan_filter_set = virtio_vlan_filter_set,
777 .mac_addr_add = virtio_mac_addr_add,
778 .mac_addr_remove = virtio_mac_addr_remove,
779 .mac_addr_set = virtio_mac_addr_set,
783 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
784 struct rte_eth_link *link)
786 struct rte_eth_link *dst = link;
787 struct rte_eth_link *src = &(dev->data->dev_link);
789 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
790 *(uint64_t *)src) == 0)
797 * Atomically writes the link status information into global
798 * structure rte_eth_dev.
801 * - Pointer to the structure rte_eth_dev to read from.
802 * - Pointer to the buffer to be saved with the link status.
805 * - On success, zero.
806 * - On failure, negative value.
809 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
810 struct rte_eth_link *link)
812 struct rte_eth_link *dst = &(dev->data->dev_link);
813 struct rte_eth_link *src = link;
815 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
816 *(uint64_t *)src) == 0)
823 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
827 for (i = 0; i < dev->data->nb_tx_queues; i++) {
828 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
832 stats->opackets += txvq->stats.packets;
833 stats->obytes += txvq->stats.bytes;
834 stats->oerrors += txvq->stats.errors;
836 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
837 stats->q_opackets[i] = txvq->stats.packets;
838 stats->q_obytes[i] = txvq->stats.bytes;
842 for (i = 0; i < dev->data->nb_rx_queues; i++) {
843 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
847 stats->ipackets += rxvq->stats.packets;
848 stats->ibytes += rxvq->stats.bytes;
849 stats->ierrors += rxvq->stats.errors;
851 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
852 stats->q_ipackets[i] = rxvq->stats.packets;
853 stats->q_ibytes[i] = rxvq->stats.bytes;
857 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
860 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
861 struct rte_eth_xstat_name *xstats_names,
862 __rte_unused unsigned limit)
868 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
869 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
871 if (xstats_names != NULL) {
872 /* Note: limit checked in rte_eth_xstats_names() */
874 for (i = 0; i < dev->data->nb_rx_queues; i++) {
875 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
878 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
879 snprintf(xstats_names[count].name,
880 sizeof(xstats_names[count].name),
882 rte_virtio_rxq_stat_strings[t].name);
887 for (i = 0; i < dev->data->nb_tx_queues; i++) {
888 struct virtnet_tx *txvq = dev->data->tx_queues[i];
891 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
892 snprintf(xstats_names[count].name,
893 sizeof(xstats_names[count].name),
895 rte_virtio_txq_stat_strings[t].name);
905 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
911 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
912 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
917 for (i = 0; i < dev->data->nb_rx_queues; i++) {
918 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
925 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
926 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
927 rte_virtio_rxq_stat_strings[t].offset);
928 xstats[count].id = count;
933 for (i = 0; i < dev->data->nb_tx_queues; i++) {
934 struct virtnet_tx *txvq = dev->data->tx_queues[i];
941 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
942 xstats[count].value = *(uint64_t *)(((char *)txvq) +
943 rte_virtio_txq_stat_strings[t].offset);
944 xstats[count].id = count;
953 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
955 virtio_update_stats(dev, stats);
961 virtio_dev_stats_reset(struct rte_eth_dev *dev)
965 for (i = 0; i < dev->data->nb_tx_queues; i++) {
966 struct virtnet_tx *txvq = dev->data->tx_queues[i];
970 txvq->stats.packets = 0;
971 txvq->stats.bytes = 0;
972 txvq->stats.errors = 0;
973 txvq->stats.multicast = 0;
974 txvq->stats.broadcast = 0;
975 memset(txvq->stats.size_bins, 0,
976 sizeof(txvq->stats.size_bins[0]) * 8);
979 for (i = 0; i < dev->data->nb_rx_queues; i++) {
980 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
984 rxvq->stats.packets = 0;
985 rxvq->stats.bytes = 0;
986 rxvq->stats.errors = 0;
987 rxvq->stats.multicast = 0;
988 rxvq->stats.broadcast = 0;
989 memset(rxvq->stats.size_bins, 0,
990 sizeof(rxvq->stats.size_bins[0]) * 8);
995 virtio_set_hwaddr(struct virtio_hw *hw)
997 vtpci_write_dev_config(hw,
998 offsetof(struct virtio_net_config, mac),
999 &hw->mac_addr, ETHER_ADDR_LEN);
1003 virtio_get_hwaddr(struct virtio_hw *hw)
1005 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1006 vtpci_read_dev_config(hw,
1007 offsetof(struct virtio_net_config, mac),
1008 &hw->mac_addr, ETHER_ADDR_LEN);
1010 eth_random_addr(&hw->mac_addr[0]);
1011 virtio_set_hwaddr(hw);
1016 virtio_mac_table_set(struct virtio_hw *hw,
1017 const struct virtio_net_ctrl_mac *uc,
1018 const struct virtio_net_ctrl_mac *mc)
1020 struct virtio_pmd_ctrl ctrl;
1023 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1024 PMD_DRV_LOG(INFO, "host does not support mac table");
1028 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1029 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1031 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1032 memcpy(ctrl.data, uc, len[0]);
1034 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1035 memcpy(ctrl.data + len[0], mc, len[1]);
1037 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1039 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1044 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1045 uint32_t index, uint32_t vmdq __rte_unused)
1047 struct virtio_hw *hw = dev->data->dev_private;
1048 const struct ether_addr *addrs = dev->data->mac_addrs;
1050 struct virtio_net_ctrl_mac *uc, *mc;
1052 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1053 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1057 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1059 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1062 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1063 const struct ether_addr *addr
1064 = (i == index) ? mac_addr : addrs + i;
1065 struct virtio_net_ctrl_mac *tbl
1066 = is_multicast_ether_addr(addr) ? mc : uc;
1068 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1071 return virtio_mac_table_set(hw, uc, mc);
1075 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1077 struct virtio_hw *hw = dev->data->dev_private;
1078 struct ether_addr *addrs = dev->data->mac_addrs;
1079 struct virtio_net_ctrl_mac *uc, *mc;
1082 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1083 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1087 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1089 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1092 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1093 struct virtio_net_ctrl_mac *tbl;
1095 if (i == index || is_zero_ether_addr(addrs + i))
1098 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1099 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1102 virtio_mac_table_set(hw, uc, mc);
1106 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1108 struct virtio_hw *hw = dev->data->dev_private;
1110 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1112 /* Use atomic update if available */
1113 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1114 struct virtio_pmd_ctrl ctrl;
1115 int len = ETHER_ADDR_LEN;
1117 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1118 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1120 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1121 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1122 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1123 virtio_set_hwaddr(hw);
1127 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1129 struct virtio_hw *hw = dev->data->dev_private;
1130 struct virtio_pmd_ctrl ctrl;
1133 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1136 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1137 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1138 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1139 len = sizeof(vlan_id);
1141 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1145 virtio_intr_enable(struct rte_eth_dev *dev)
1147 struct virtio_hw *hw = dev->data->dev_private;
1149 if (rte_intr_enable(dev->intr_handle) < 0)
1152 if (!hw->virtio_user_dev)
1153 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1159 virtio_intr_disable(struct rte_eth_dev *dev)
1161 struct virtio_hw *hw = dev->data->dev_private;
1163 if (rte_intr_disable(dev->intr_handle) < 0)
1166 if (!hw->virtio_user_dev)
1167 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1173 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1175 uint64_t host_features;
1177 /* Prepare guest_features: feature that driver wants to support */
1178 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1181 /* Read device(host) feature bits */
1182 host_features = VTPCI_OPS(hw)->get_features(hw);
1183 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1186 /* If supported, ensure MTU value is valid before acknowledging it. */
1187 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1188 struct virtio_net_config config;
1190 vtpci_read_dev_config(hw,
1191 offsetof(struct virtio_net_config, mtu),
1192 &config.mtu, sizeof(config.mtu));
1194 if (config.mtu < ETHER_MIN_MTU)
1195 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1199 * Negotiate features: Subset of device feature bits are written back
1200 * guest feature bits.
1202 hw->guest_features = req_features;
1203 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1204 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1205 hw->guest_features);
1208 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1210 "VIRTIO_F_VERSION_1 features is not enabled.");
1213 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1214 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1216 "failed to set FEATURES_OK status!");
1221 hw->req_guest_features = req_features;
1227 * Process Virtio Config changed interrupt and call the callback
1228 * if link state changed.
1231 virtio_interrupt_handler(void *param)
1233 struct rte_eth_dev *dev = param;
1234 struct virtio_hw *hw = dev->data->dev_private;
1237 /* Read interrupt status which clears interrupt */
1238 isr = vtpci_isr(hw);
1239 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1241 if (virtio_intr_enable(dev) < 0)
1242 PMD_DRV_LOG(ERR, "interrupt enable failed");
1244 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1245 if (virtio_dev_link_update(dev, 0) == 0)
1246 _rte_eth_dev_callback_process(dev,
1247 RTE_ETH_EVENT_INTR_LSC,
1253 /* set rx and tx handlers according to what is supported */
1255 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1257 struct virtio_hw *hw = eth_dev->data->dev_private;
1259 if (hw->use_simple_rx) {
1260 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1261 eth_dev->data->port_id);
1262 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1263 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1265 "virtio: using mergeable buffer Rx path on port %u",
1266 eth_dev->data->port_id);
1267 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1269 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1270 eth_dev->data->port_id);
1271 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1274 if (hw->use_simple_tx) {
1275 PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
1276 eth_dev->data->port_id);
1277 eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1279 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1280 eth_dev->data->port_id);
1281 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1285 /* Only support 1:1 queue/interrupt mapping so far.
1286 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1287 * interrupt vectors (<N+1).
1290 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1293 struct virtio_hw *hw = dev->data->dev_private;
1295 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1296 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1297 dev->intr_handle->intr_vec[i] = i + 1;
1298 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1299 VIRTIO_MSI_NO_VECTOR) {
1300 PMD_DRV_LOG(ERR, "failed to set queue vector");
1309 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1312 struct virtio_hw *hw = dev->data->dev_private;
1314 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1315 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1316 VTPCI_OPS(hw)->set_queue_irq(hw,
1317 hw->vqs[i * VTNET_CQ],
1318 VIRTIO_MSI_NO_VECTOR);
1322 virtio_configure_intr(struct rte_eth_dev *dev)
1324 struct virtio_hw *hw = dev->data->dev_private;
1326 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1327 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1331 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1332 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1336 if (!dev->intr_handle->intr_vec) {
1337 dev->intr_handle->intr_vec =
1338 rte_zmalloc("intr_vec",
1339 hw->max_queue_pairs * sizeof(int), 0);
1340 if (!dev->intr_handle->intr_vec) {
1341 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1342 hw->max_queue_pairs);
1347 /* Re-register callback to update max_intr */
1348 rte_intr_callback_unregister(dev->intr_handle,
1349 virtio_interrupt_handler,
1351 rte_intr_callback_register(dev->intr_handle,
1352 virtio_interrupt_handler,
1355 /* DO NOT try to remove this! This function will enable msix, or QEMU
1356 * will encounter SIGSEGV when DRIVER_OK is sent.
1357 * And for legacy devices, this should be done before queue/vec binding
1358 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1359 * (22) will be ignored.
1361 if (virtio_intr_enable(dev) < 0) {
1362 PMD_DRV_LOG(ERR, "interrupt enable failed");
1366 if (virtio_queues_bind_intr(dev) < 0) {
1367 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1374 /* reset device and renegotiate features if needed */
1376 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1378 struct virtio_hw *hw = eth_dev->data->dev_private;
1379 struct virtio_net_config *config;
1380 struct virtio_net_config local_config;
1381 struct rte_pci_device *pci_dev = NULL;
1384 /* Reset the device although not necessary at startup */
1387 /* Tell the host we've noticed this device. */
1388 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1390 /* Tell the host we've known how to drive the device. */
1391 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1392 if (virtio_negotiate_features(hw, req_features) < 0)
1395 if (!hw->virtio_user_dev) {
1396 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1397 rte_eth_copy_pci_info(eth_dev, pci_dev);
1400 /* If host does not support both status and MSI-X then disable LSC */
1401 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1402 hw->use_msix != VIRTIO_MSIX_NONE)
1403 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1405 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1407 /* Setting up rx_header size for the device */
1408 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1409 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1410 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1412 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1414 /* Copy the permanent MAC address to: virtio_hw */
1415 virtio_get_hwaddr(hw);
1416 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1417 ð_dev->data->mac_addrs[0]);
1419 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1420 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1421 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1423 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1424 config = &local_config;
1426 vtpci_read_dev_config(hw,
1427 offsetof(struct virtio_net_config, mac),
1428 &config->mac, sizeof(config->mac));
1430 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1431 vtpci_read_dev_config(hw,
1432 offsetof(struct virtio_net_config, status),
1433 &config->status, sizeof(config->status));
1436 "VIRTIO_NET_F_STATUS is not supported");
1440 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1441 vtpci_read_dev_config(hw,
1442 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1443 &config->max_virtqueue_pairs,
1444 sizeof(config->max_virtqueue_pairs));
1447 "VIRTIO_NET_F_MQ is not supported");
1448 config->max_virtqueue_pairs = 1;
1451 hw->max_queue_pairs = config->max_virtqueue_pairs;
1453 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1454 vtpci_read_dev_config(hw,
1455 offsetof(struct virtio_net_config, mtu),
1457 sizeof(config->mtu));
1460 * MTU value has already been checked at negotiation
1461 * time, but check again in case it has changed since
1462 * then, which should not happen.
1464 if (config->mtu < ETHER_MIN_MTU) {
1465 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1470 hw->max_mtu = config->mtu;
1471 /* Set initial MTU to maximum one supported by vhost */
1472 eth_dev->data->mtu = config->mtu;
1475 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1476 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1479 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1480 config->max_virtqueue_pairs);
1481 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1483 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1484 config->mac[0], config->mac[1],
1485 config->mac[2], config->mac[3],
1486 config->mac[4], config->mac[5]);
1488 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1489 hw->max_queue_pairs = 1;
1490 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1491 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1494 ret = virtio_alloc_queues(eth_dev);
1498 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1499 if (virtio_configure_intr(eth_dev) < 0) {
1500 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1505 vtpci_reinit_complete(hw);
1508 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1509 eth_dev->data->port_id, pci_dev->id.vendor_id,
1510 pci_dev->id.device_id);
1516 * Remap the PCI device again (IO port map for legacy device and
1517 * memory map for modern device), so that the secondary process
1518 * could have the PCI initiated correctly.
1521 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1525 * We don't have to re-parse the PCI config space, since
1526 * rte_pci_map_device() makes sure the mapped address
1527 * in secondary process would equal to the one mapped in
1528 * the primary process: error will be returned if that
1529 * requirement is not met.
1531 * That said, we could simply reuse all cap pointers
1532 * (such as dev_cfg, common_cfg, etc.) parsed from the
1533 * primary process, which is stored in shared memory.
1535 if (rte_pci_map_device(pci_dev)) {
1536 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1540 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1548 virtio_set_vtpci_ops(struct virtio_hw *hw)
1550 #ifdef RTE_VIRTIO_USER
1551 if (hw->virtio_user_dev)
1552 VTPCI_OPS(hw) = &virtio_user_ops;
1556 VTPCI_OPS(hw) = &modern_ops;
1558 VTPCI_OPS(hw) = &legacy_ops;
1562 * This function is based on probe() function in virtio_pci.c
1563 * It returns 0 on success.
1566 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1568 struct virtio_hw *hw = eth_dev->data->dev_private;
1571 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1573 eth_dev->dev_ops = &virtio_eth_dev_ops;
1575 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1576 if (!hw->virtio_user_dev) {
1577 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1582 virtio_set_vtpci_ops(hw);
1583 set_rxtx_funcs(eth_dev);
1588 /* Allocate memory for storing MAC addresses */
1589 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1590 if (eth_dev->data->mac_addrs == NULL) {
1592 "Failed to allocate %d bytes needed to store MAC addresses",
1593 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1597 hw->port_id = eth_dev->data->port_id;
1598 /* For virtio_user case the hw->virtio_user_dev is populated by
1599 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1601 if (!hw->virtio_user_dev) {
1602 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1607 /* reset device and negotiate default features */
1608 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1612 /* Setup interrupt callback */
1613 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1614 rte_intr_callback_register(eth_dev->intr_handle,
1615 virtio_interrupt_handler, eth_dev);
1620 rte_free(eth_dev->data->mac_addrs);
1625 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1627 PMD_INIT_FUNC_TRACE();
1629 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1632 virtio_dev_stop(eth_dev);
1633 virtio_dev_close(eth_dev);
1635 eth_dev->dev_ops = NULL;
1636 eth_dev->tx_pkt_burst = NULL;
1637 eth_dev->rx_pkt_burst = NULL;
1639 rte_free(eth_dev->data->mac_addrs);
1640 eth_dev->data->mac_addrs = NULL;
1642 /* reset interrupt callback */
1643 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1644 rte_intr_callback_unregister(eth_dev->intr_handle,
1645 virtio_interrupt_handler,
1647 if (eth_dev->device)
1648 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1650 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1655 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1656 struct rte_pci_device *pci_dev)
1658 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1659 eth_virtio_dev_init);
1662 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1664 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1667 static struct rte_pci_driver rte_virtio_pmd = {
1669 .name = "net_virtio",
1671 .id_table = pci_id_virtio_map,
1673 .probe = eth_virtio_pci_probe,
1674 .remove = eth_virtio_pci_remove,
1677 RTE_INIT(rte_virtio_pmd_init);
1679 rte_virtio_pmd_init(void)
1681 if (rte_eal_iopl_init() != 0) {
1682 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1686 rte_pci_register(&rte_virtio_pmd);
1690 * Configure virtio device
1691 * It returns 0 on success.
1694 virtio_dev_configure(struct rte_eth_dev *dev)
1696 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1697 struct virtio_hw *hw = dev->data->dev_private;
1698 uint64_t req_features;
1701 PMD_INIT_LOG(DEBUG, "configure");
1702 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1704 if (dev->data->dev_conf.intr_conf.rxq) {
1705 ret = virtio_init_device(dev, hw->req_guest_features);
1710 /* The name hw_ip_checksum is a bit confusing since it can be
1711 * set by the application to request L3 and/or L4 checksums. In
1712 * case of virtio, only L4 checksum is supported.
1714 if (rxmode->hw_ip_checksum)
1715 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1717 if (rxmode->enable_lro)
1719 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1720 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1722 /* if request features changed, reinit the device */
1723 if (req_features != hw->req_guest_features) {
1724 ret = virtio_init_device(dev, req_features);
1729 if (rxmode->hw_ip_checksum &&
1730 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
1732 "rx checksum not available on this host");
1736 if (rxmode->enable_lro &&
1737 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1738 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
1740 "Large Receive Offload not available on this host");
1744 /* start control queue */
1745 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1746 virtio_dev_cq_start(dev);
1748 hw->vlan_strip = rxmode->hw_vlan_strip;
1750 if (rxmode->hw_vlan_filter
1751 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1753 "vlan filtering not available on this host");
1757 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1758 /* Enable vector (0) for Link State Intrerrupt */
1759 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1760 VIRTIO_MSI_NO_VECTOR) {
1761 PMD_DRV_LOG(ERR, "failed to set config vector");
1765 hw->use_simple_rx = 1;
1766 hw->use_simple_tx = 1;
1768 #if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
1769 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
1770 hw->use_simple_rx = 0;
1771 hw->use_simple_tx = 0;
1774 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1775 hw->use_simple_rx = 0;
1776 hw->use_simple_tx = 0;
1779 if (rxmode->hw_ip_checksum)
1780 hw->use_simple_rx = 0;
1787 virtio_dev_start(struct rte_eth_dev *dev)
1789 uint16_t nb_queues, i;
1790 struct virtnet_rx *rxvq;
1791 struct virtnet_tx *txvq __rte_unused;
1792 struct virtio_hw *hw = dev->data->dev_private;
1795 /* Finish the initialization of the queues */
1796 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1797 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1801 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1802 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1807 /* check if lsc interrupt feature is enabled */
1808 if (dev->data->dev_conf.intr_conf.lsc) {
1809 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1810 PMD_DRV_LOG(ERR, "link status not supported by host");
1815 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1816 * in device configure, but it could be unmapped when device is
1819 if (dev->data->dev_conf.intr_conf.lsc ||
1820 dev->data->dev_conf.intr_conf.rxq) {
1821 virtio_intr_disable(dev);
1823 if (virtio_intr_enable(dev) < 0) {
1824 PMD_DRV_LOG(ERR, "interrupt enable failed");
1829 /*Notify the backend
1830 *Otherwise the tap backend might already stop its queue due to fullness.
1831 *vhost backend will have no chance to be waked up
1833 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1834 if (hw->max_queue_pairs > 1) {
1835 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1839 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1841 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1842 rxvq = dev->data->rx_queues[i];
1843 /* Flush the old packets */
1844 virtqueue_rxvq_flush(rxvq->vq);
1845 virtqueue_notify(rxvq->vq);
1848 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1849 txvq = dev->data->tx_queues[i];
1850 virtqueue_notify(txvq->vq);
1853 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1855 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1856 rxvq = dev->data->rx_queues[i];
1857 VIRTQUEUE_DUMP(rxvq->vq);
1860 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1861 txvq = dev->data->tx_queues[i];
1862 VIRTQUEUE_DUMP(txvq->vq);
1865 set_rxtx_funcs(dev);
1868 /* Initialize Link state */
1869 virtio_dev_link_update(dev, 0);
1874 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1876 struct rte_mbuf *buf;
1877 int i, mbuf_num = 0;
1879 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1880 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1883 "Before freeing rxq[%d] used and unused buf", i);
1884 VIRTQUEUE_DUMP(rxvq->vq);
1886 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1887 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1888 rte_pktmbuf_free(buf);
1892 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1894 "After freeing rxq[%d] used and unused buf", i);
1895 VIRTQUEUE_DUMP(rxvq->vq);
1898 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1899 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1902 "Before freeing txq[%d] used and unused bufs",
1904 VIRTQUEUE_DUMP(txvq->vq);
1907 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1908 rte_pktmbuf_free(buf);
1912 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1914 "After freeing txq[%d] used and unused buf", i);
1915 VIRTQUEUE_DUMP(txvq->vq);
1920 * Stop device: disable interrupt and mark link down
1923 virtio_dev_stop(struct rte_eth_dev *dev)
1925 struct virtio_hw *hw = dev->data->dev_private;
1926 struct rte_eth_link link;
1927 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
1929 PMD_INIT_LOG(DEBUG, "stop");
1931 if (intr_conf->lsc || intr_conf->rxq)
1932 virtio_intr_disable(dev);
1935 memset(&link, 0, sizeof(link));
1936 virtio_dev_atomic_write_link_status(dev, &link);
1940 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1942 struct rte_eth_link link, old;
1944 struct virtio_hw *hw = dev->data->dev_private;
1945 memset(&link, 0, sizeof(link));
1946 virtio_dev_atomic_read_link_status(dev, &link);
1948 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1949 link.link_speed = ETH_SPEED_NUM_10G;
1951 if (hw->started == 0) {
1952 link.link_status = ETH_LINK_DOWN;
1953 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1954 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1955 vtpci_read_dev_config(hw,
1956 offsetof(struct virtio_net_config, status),
1957 &status, sizeof(status));
1958 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1959 link.link_status = ETH_LINK_DOWN;
1960 PMD_INIT_LOG(DEBUG, "Port %d is down",
1961 dev->data->port_id);
1963 link.link_status = ETH_LINK_UP;
1964 PMD_INIT_LOG(DEBUG, "Port %d is up",
1965 dev->data->port_id);
1968 link.link_status = ETH_LINK_UP;
1970 virtio_dev_atomic_write_link_status(dev, &link);
1972 return (old.link_status == link.link_status) ? -1 : 0;
1976 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1978 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1979 struct virtio_hw *hw = dev->data->dev_private;
1981 if (mask & ETH_VLAN_FILTER_MASK) {
1982 if (rxmode->hw_vlan_filter &&
1983 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1986 "vlan filtering not available on this host");
1992 if (mask & ETH_VLAN_STRIP_MASK)
1993 hw->vlan_strip = rxmode->hw_vlan_strip;
1999 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2001 uint64_t tso_mask, host_features;
2002 struct virtio_hw *hw = dev->data->dev_private;
2004 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2006 dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
2007 dev_info->max_rx_queues =
2008 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2009 dev_info->max_tx_queues =
2010 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2011 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2012 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2013 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2014 dev_info->default_txconf = (struct rte_eth_txconf) {
2015 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
2018 host_features = VTPCI_OPS(hw)->get_features(hw);
2019 dev_info->rx_offload_capa = 0;
2020 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2021 dev_info->rx_offload_capa |=
2022 DEV_RX_OFFLOAD_TCP_CKSUM |
2023 DEV_RX_OFFLOAD_UDP_CKSUM;
2025 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2026 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2027 if ((host_features & tso_mask) == tso_mask)
2028 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2030 dev_info->tx_offload_capa = 0;
2031 if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2032 dev_info->tx_offload_capa |=
2033 DEV_TX_OFFLOAD_UDP_CKSUM |
2034 DEV_TX_OFFLOAD_TCP_CKSUM;
2036 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2037 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2038 if ((hw->guest_features & tso_mask) == tso_mask)
2039 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2043 * It enables testpmd to collect per queue stats.
2046 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2047 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2048 __rte_unused uint8_t is_rx)
2053 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2054 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2055 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2057 RTE_INIT(virtio_init_log);
2059 virtio_init_log(void)
2061 virtio_logtype_init = rte_log_register("pmd.virtio.init");
2062 if (virtio_logtype_init >= 0)
2063 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2064 virtio_logtype_driver = rte_log_register("pmd.virtio.driver");
2065 if (virtio_logtype_driver >= 0)
2066 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);