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 head = vq->vq_desc_head_idx;
157 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
158 "vq->hw->cvq = %p vq = %p",
159 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
161 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
164 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
165 sizeof(struct virtio_pmd_ctrl));
168 * Format is enforced in qemu code:
169 * One TX packet for header;
170 * At least one TX packet per argument;
171 * One RX packet for ACK.
173 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
174 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
175 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
177 i = vq->vq_ring.desc[head].next;
179 for (k = 0; k < pkt_num; k++) {
180 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
181 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
182 + sizeof(struct virtio_net_ctrl_hdr)
183 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
184 vq->vq_ring.desc[i].len = dlen[k];
187 i = vq->vq_ring.desc[i].next;
190 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
191 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
192 + sizeof(struct virtio_net_ctrl_hdr);
193 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
196 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
198 vq_update_avail_ring(vq, head);
199 vq_update_avail_idx(vq);
201 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
203 virtqueue_notify(vq);
206 while (VIRTQUEUE_NUSED(vq) == 0) {
211 while (VIRTQUEUE_NUSED(vq)) {
212 uint32_t idx, desc_idx, used_idx;
213 struct vring_used_elem *uep;
215 used_idx = (uint32_t)(vq->vq_used_cons_idx
216 & (vq->vq_nentries - 1));
217 uep = &vq->vq_ring.used->ring[used_idx];
218 idx = (uint32_t) uep->id;
221 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
222 desc_idx = vq->vq_ring.desc[desc_idx].next;
226 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
227 vq->vq_desc_head_idx = idx;
229 vq->vq_used_cons_idx++;
233 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
234 vq->vq_free_cnt, vq->vq_desc_head_idx);
236 result = cvq->virtio_net_hdr_mz->addr;
238 return result->status;
242 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
244 struct virtio_hw *hw = dev->data->dev_private;
245 struct virtio_pmd_ctrl ctrl;
249 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
250 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
251 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
253 dlen[0] = sizeof(uint16_t);
255 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
257 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
258 "failed, this is too late now...");
266 virtio_dev_queue_release(void *queue __rte_unused)
272 virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
274 if (vtpci_queue_idx == hw->max_queue_pairs * 2)
276 else if (vtpci_queue_idx % 2 == 0)
283 virtio_get_nr_vq(struct virtio_hw *hw)
285 uint16_t nr_vq = hw->max_queue_pairs * 2;
287 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
294 virtio_init_vring(struct virtqueue *vq)
296 int size = vq->vq_nentries;
297 struct vring *vr = &vq->vq_ring;
298 uint8_t *ring_mem = vq->vq_ring_virt_mem;
300 PMD_INIT_FUNC_TRACE();
303 * Reinitialise since virtio port might have been stopped and restarted
305 memset(ring_mem, 0, vq->vq_ring_size);
306 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
307 vq->vq_used_cons_idx = 0;
308 vq->vq_desc_head_idx = 0;
309 vq->vq_avail_idx = 0;
310 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
311 vq->vq_free_cnt = vq->vq_nentries;
312 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
314 vring_desc_init(vr->desc, size);
317 * Disable device(host) interrupting guest
319 virtqueue_disable_intr(vq);
323 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
325 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
326 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
327 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
328 unsigned int vq_size, size;
329 struct virtio_hw *hw = dev->data->dev_private;
330 struct virtnet_rx *rxvq = NULL;
331 struct virtnet_tx *txvq = NULL;
332 struct virtnet_ctl *cvq = NULL;
333 struct virtqueue *vq;
334 size_t sz_hdr_mz = 0;
335 void *sw_ring = NULL;
336 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
339 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
342 * Read the virtqueue size from the Queue Size field
343 * Always power of 2 and if 0 virtqueue does not exist
345 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
346 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
348 PMD_INIT_LOG(ERR, "virtqueue does not exist");
352 if (!rte_is_power_of_2(vq_size)) {
353 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
357 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
358 dev->data->port_id, vtpci_queue_idx);
360 size = RTE_ALIGN_CEIL(sizeof(*vq) +
361 vq_size * sizeof(struct vq_desc_extra),
362 RTE_CACHE_LINE_SIZE);
363 if (queue_type == VTNET_TQ) {
365 * For each xmit packet, allocate a virtio_net_hdr
366 * and indirect ring elements
368 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
369 } else if (queue_type == VTNET_CQ) {
370 /* Allocate a page for control vq command, data and status */
371 sz_hdr_mz = PAGE_SIZE;
374 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
377 PMD_INIT_LOG(ERR, "can not allocate vq");
380 hw->vqs[vtpci_queue_idx] = vq;
383 vq->vq_queue_index = vtpci_queue_idx;
384 vq->vq_nentries = vq_size;
387 * Reserve a memzone for vring elements
389 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
390 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
391 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
392 size, vq->vq_ring_size);
394 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
396 0, VIRTIO_PCI_VRING_ALIGN);
398 if (rte_errno == EEXIST)
399 mz = rte_memzone_lookup(vq_name);
406 memset(mz->addr, 0, mz->len);
408 vq->vq_ring_mem = mz->iova;
409 vq->vq_ring_virt_mem = mz->addr;
410 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
412 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
413 (uint64_t)(uintptr_t)mz->addr);
415 virtio_init_vring(vq);
418 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
419 dev->data->port_id, vtpci_queue_idx);
420 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
422 RTE_CACHE_LINE_SIZE);
423 if (hdr_mz == NULL) {
424 if (rte_errno == EEXIST)
425 hdr_mz = rte_memzone_lookup(vq_hdr_name);
426 if (hdr_mz == NULL) {
433 if (queue_type == VTNET_RQ) {
434 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
435 sizeof(vq->sw_ring[0]);
437 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
438 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
440 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
445 vq->sw_ring = sw_ring;
448 rxvq->port_id = dev->data->port_id;
450 } else if (queue_type == VTNET_TQ) {
453 txvq->port_id = dev->data->port_id;
455 txvq->virtio_net_hdr_mz = hdr_mz;
456 txvq->virtio_net_hdr_mem = hdr_mz->iova;
457 } else if (queue_type == VTNET_CQ) {
461 cvq->virtio_net_hdr_mz = hdr_mz;
462 cvq->virtio_net_hdr_mem = hdr_mz->iova;
463 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
468 /* For virtio_user case (that is when hw->dev is NULL), we use
469 * virtual address. And we need properly set _offset_, please see
470 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
472 if (!hw->virtio_user_dev)
473 vq->offset = offsetof(struct rte_mbuf, buf_iova);
475 vq->vq_ring_mem = (uintptr_t)mz->addr;
476 vq->offset = offsetof(struct rte_mbuf, buf_addr);
477 if (queue_type == VTNET_TQ)
478 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
479 else if (queue_type == VTNET_CQ)
480 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
483 if (queue_type == VTNET_TQ) {
484 struct virtio_tx_region *txr;
488 memset(txr, 0, vq_size * sizeof(*txr));
489 for (i = 0; i < vq_size; i++) {
490 struct vring_desc *start_dp = txr[i].tx_indir;
492 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
494 /* first indirect descriptor is always the tx header */
495 start_dp->addr = txvq->virtio_net_hdr_mem
497 + offsetof(struct virtio_tx_region, tx_hdr);
499 start_dp->len = hw->vtnet_hdr_size;
500 start_dp->flags = VRING_DESC_F_NEXT;
504 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
505 PMD_INIT_LOG(ERR, "setup_queue failed");
513 rte_memzone_free(hdr_mz);
514 rte_memzone_free(mz);
521 virtio_free_queues(struct virtio_hw *hw)
523 uint16_t nr_vq = virtio_get_nr_vq(hw);
524 struct virtqueue *vq;
531 for (i = 0; i < nr_vq; i++) {
536 queue_type = virtio_get_queue_type(hw, i);
537 if (queue_type == VTNET_RQ) {
538 rte_free(vq->sw_ring);
539 rte_memzone_free(vq->rxq.mz);
540 } else if (queue_type == VTNET_TQ) {
541 rte_memzone_free(vq->txq.mz);
542 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
544 rte_memzone_free(vq->cq.mz);
545 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
557 virtio_alloc_queues(struct rte_eth_dev *dev)
559 struct virtio_hw *hw = dev->data->dev_private;
560 uint16_t nr_vq = virtio_get_nr_vq(hw);
564 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
566 PMD_INIT_LOG(ERR, "failed to allocate vqs");
570 for (i = 0; i < nr_vq; i++) {
571 ret = virtio_init_queue(dev, i);
573 virtio_free_queues(hw);
581 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
584 virtio_dev_close(struct rte_eth_dev *dev)
586 struct virtio_hw *hw = dev->data->dev_private;
587 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
589 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
592 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
593 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
595 virtio_queues_unbind_intr(dev);
597 if (intr_conf->lsc || intr_conf->rxq) {
598 virtio_intr_disable(dev);
599 rte_intr_efd_disable(dev->intr_handle);
600 rte_free(dev->intr_handle->intr_vec);
601 dev->intr_handle->intr_vec = NULL;
605 virtio_dev_free_mbufs(dev);
606 virtio_free_queues(hw);
610 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
612 struct virtio_hw *hw = dev->data->dev_private;
613 struct virtio_pmd_ctrl ctrl;
617 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
618 PMD_INIT_LOG(INFO, "host does not support rx control");
622 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
623 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
627 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
629 PMD_INIT_LOG(ERR, "Failed to enable promisc");
633 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
635 struct virtio_hw *hw = dev->data->dev_private;
636 struct virtio_pmd_ctrl ctrl;
640 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
641 PMD_INIT_LOG(INFO, "host does not support rx control");
645 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
646 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
650 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
652 PMD_INIT_LOG(ERR, "Failed to disable promisc");
656 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
658 struct virtio_hw *hw = dev->data->dev_private;
659 struct virtio_pmd_ctrl ctrl;
663 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
664 PMD_INIT_LOG(INFO, "host does not support rx control");
668 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
669 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
673 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
675 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
679 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
681 struct virtio_hw *hw = dev->data->dev_private;
682 struct virtio_pmd_ctrl ctrl;
686 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
687 PMD_INIT_LOG(INFO, "host does not support rx control");
691 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
692 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
696 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
698 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
701 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
703 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
705 struct virtio_hw *hw = dev->data->dev_private;
706 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
708 uint32_t frame_size = mtu + ether_hdr_len;
709 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
711 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
713 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
714 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
715 ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
722 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
724 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
725 struct virtqueue *vq = rxvq->vq;
727 virtqueue_enable_intr(vq);
732 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
734 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
735 struct virtqueue *vq = rxvq->vq;
737 virtqueue_disable_intr(vq);
742 * dev_ops for virtio, bare necessities for basic operation
744 static const struct eth_dev_ops virtio_eth_dev_ops = {
745 .dev_configure = virtio_dev_configure,
746 .dev_start = virtio_dev_start,
747 .dev_stop = virtio_dev_stop,
748 .dev_close = virtio_dev_close,
749 .promiscuous_enable = virtio_dev_promiscuous_enable,
750 .promiscuous_disable = virtio_dev_promiscuous_disable,
751 .allmulticast_enable = virtio_dev_allmulticast_enable,
752 .allmulticast_disable = virtio_dev_allmulticast_disable,
753 .mtu_set = virtio_mtu_set,
754 .dev_infos_get = virtio_dev_info_get,
755 .stats_get = virtio_dev_stats_get,
756 .xstats_get = virtio_dev_xstats_get,
757 .xstats_get_names = virtio_dev_xstats_get_names,
758 .stats_reset = virtio_dev_stats_reset,
759 .xstats_reset = virtio_dev_stats_reset,
760 .link_update = virtio_dev_link_update,
761 .vlan_offload_set = virtio_dev_vlan_offload_set,
762 .rx_queue_setup = virtio_dev_rx_queue_setup,
763 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
764 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
765 .rx_queue_release = virtio_dev_queue_release,
766 .rx_descriptor_done = virtio_dev_rx_queue_done,
767 .tx_queue_setup = virtio_dev_tx_queue_setup,
768 .tx_queue_release = virtio_dev_queue_release,
769 /* collect stats per queue */
770 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
771 .vlan_filter_set = virtio_vlan_filter_set,
772 .mac_addr_add = virtio_mac_addr_add,
773 .mac_addr_remove = virtio_mac_addr_remove,
774 .mac_addr_set = virtio_mac_addr_set,
778 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
779 struct rte_eth_link *link)
781 struct rte_eth_link *dst = link;
782 struct rte_eth_link *src = &(dev->data->dev_link);
784 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
785 *(uint64_t *)src) == 0)
792 * Atomically writes the link status information into global
793 * structure rte_eth_dev.
796 * - Pointer to the structure rte_eth_dev to read from.
797 * - Pointer to the buffer to be saved with the link status.
800 * - On success, zero.
801 * - On failure, negative value.
804 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
805 struct rte_eth_link *link)
807 struct rte_eth_link *dst = &(dev->data->dev_link);
808 struct rte_eth_link *src = link;
810 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
811 *(uint64_t *)src) == 0)
818 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
822 for (i = 0; i < dev->data->nb_tx_queues; i++) {
823 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
827 stats->opackets += txvq->stats.packets;
828 stats->obytes += txvq->stats.bytes;
829 stats->oerrors += txvq->stats.errors;
831 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
832 stats->q_opackets[i] = txvq->stats.packets;
833 stats->q_obytes[i] = txvq->stats.bytes;
837 for (i = 0; i < dev->data->nb_rx_queues; i++) {
838 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
842 stats->ipackets += rxvq->stats.packets;
843 stats->ibytes += rxvq->stats.bytes;
844 stats->ierrors += rxvq->stats.errors;
846 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
847 stats->q_ipackets[i] = rxvq->stats.packets;
848 stats->q_ibytes[i] = rxvq->stats.bytes;
852 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
855 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
856 struct rte_eth_xstat_name *xstats_names,
857 __rte_unused unsigned limit)
863 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
864 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
866 if (xstats_names != NULL) {
867 /* Note: limit checked in rte_eth_xstats_names() */
869 for (i = 0; i < dev->data->nb_rx_queues; i++) {
870 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
873 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
874 snprintf(xstats_names[count].name,
875 sizeof(xstats_names[count].name),
877 rte_virtio_rxq_stat_strings[t].name);
882 for (i = 0; i < dev->data->nb_tx_queues; i++) {
883 struct virtnet_tx *txvq = dev->data->tx_queues[i];
886 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
887 snprintf(xstats_names[count].name,
888 sizeof(xstats_names[count].name),
890 rte_virtio_txq_stat_strings[t].name);
900 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
906 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
907 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
912 for (i = 0; i < dev->data->nb_rx_queues; i++) {
913 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
920 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
921 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
922 rte_virtio_rxq_stat_strings[t].offset);
923 xstats[count].id = count;
928 for (i = 0; i < dev->data->nb_tx_queues; i++) {
929 struct virtnet_tx *txvq = dev->data->tx_queues[i];
936 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
937 xstats[count].value = *(uint64_t *)(((char *)txvq) +
938 rte_virtio_txq_stat_strings[t].offset);
939 xstats[count].id = count;
948 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
950 virtio_update_stats(dev, stats);
956 virtio_dev_stats_reset(struct rte_eth_dev *dev)
960 for (i = 0; i < dev->data->nb_tx_queues; i++) {
961 struct virtnet_tx *txvq = dev->data->tx_queues[i];
965 txvq->stats.packets = 0;
966 txvq->stats.bytes = 0;
967 txvq->stats.errors = 0;
968 txvq->stats.multicast = 0;
969 txvq->stats.broadcast = 0;
970 memset(txvq->stats.size_bins, 0,
971 sizeof(txvq->stats.size_bins[0]) * 8);
974 for (i = 0; i < dev->data->nb_rx_queues; i++) {
975 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
979 rxvq->stats.packets = 0;
980 rxvq->stats.bytes = 0;
981 rxvq->stats.errors = 0;
982 rxvq->stats.multicast = 0;
983 rxvq->stats.broadcast = 0;
984 memset(rxvq->stats.size_bins, 0,
985 sizeof(rxvq->stats.size_bins[0]) * 8);
990 virtio_set_hwaddr(struct virtio_hw *hw)
992 vtpci_write_dev_config(hw,
993 offsetof(struct virtio_net_config, mac),
994 &hw->mac_addr, ETHER_ADDR_LEN);
998 virtio_get_hwaddr(struct virtio_hw *hw)
1000 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1001 vtpci_read_dev_config(hw,
1002 offsetof(struct virtio_net_config, mac),
1003 &hw->mac_addr, ETHER_ADDR_LEN);
1005 eth_random_addr(&hw->mac_addr[0]);
1006 virtio_set_hwaddr(hw);
1011 virtio_mac_table_set(struct virtio_hw *hw,
1012 const struct virtio_net_ctrl_mac *uc,
1013 const struct virtio_net_ctrl_mac *mc)
1015 struct virtio_pmd_ctrl ctrl;
1018 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1019 PMD_DRV_LOG(INFO, "host does not support mac table");
1023 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1024 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1026 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1027 memcpy(ctrl.data, uc, len[0]);
1029 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1030 memcpy(ctrl.data + len[0], mc, len[1]);
1032 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1034 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1039 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1040 uint32_t index, uint32_t vmdq __rte_unused)
1042 struct virtio_hw *hw = dev->data->dev_private;
1043 const struct ether_addr *addrs = dev->data->mac_addrs;
1045 struct virtio_net_ctrl_mac *uc, *mc;
1047 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1048 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1052 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1054 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1057 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1058 const struct ether_addr *addr
1059 = (i == index) ? mac_addr : addrs + i;
1060 struct virtio_net_ctrl_mac *tbl
1061 = is_multicast_ether_addr(addr) ? mc : uc;
1063 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1066 return virtio_mac_table_set(hw, uc, mc);
1070 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1072 struct virtio_hw *hw = dev->data->dev_private;
1073 struct ether_addr *addrs = dev->data->mac_addrs;
1074 struct virtio_net_ctrl_mac *uc, *mc;
1077 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1078 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1082 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1084 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1087 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1088 struct virtio_net_ctrl_mac *tbl;
1090 if (i == index || is_zero_ether_addr(addrs + i))
1093 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1094 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1097 virtio_mac_table_set(hw, uc, mc);
1101 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1103 struct virtio_hw *hw = dev->data->dev_private;
1105 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1107 /* Use atomic update if available */
1108 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1109 struct virtio_pmd_ctrl ctrl;
1110 int len = ETHER_ADDR_LEN;
1112 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1113 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1115 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1116 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1117 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1118 virtio_set_hwaddr(hw);
1122 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1124 struct virtio_hw *hw = dev->data->dev_private;
1125 struct virtio_pmd_ctrl ctrl;
1128 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1131 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1132 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1133 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1134 len = sizeof(vlan_id);
1136 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1140 virtio_intr_enable(struct rte_eth_dev *dev)
1142 struct virtio_hw *hw = dev->data->dev_private;
1144 if (rte_intr_enable(dev->intr_handle) < 0)
1147 if (!hw->virtio_user_dev)
1148 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1154 virtio_intr_disable(struct rte_eth_dev *dev)
1156 struct virtio_hw *hw = dev->data->dev_private;
1158 if (rte_intr_disable(dev->intr_handle) < 0)
1161 if (!hw->virtio_user_dev)
1162 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1168 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1170 uint64_t host_features;
1172 /* Prepare guest_features: feature that driver wants to support */
1173 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1176 /* Read device(host) feature bits */
1177 host_features = VTPCI_OPS(hw)->get_features(hw);
1178 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1181 /* If supported, ensure MTU value is valid before acknowledging it. */
1182 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1183 struct virtio_net_config config;
1185 vtpci_read_dev_config(hw,
1186 offsetof(struct virtio_net_config, mtu),
1187 &config.mtu, sizeof(config.mtu));
1189 if (config.mtu < ETHER_MIN_MTU)
1190 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1194 * Negotiate features: Subset of device feature bits are written back
1195 * guest feature bits.
1197 hw->guest_features = req_features;
1198 hw->guest_features = vtpci_negotiate_features(hw, host_features);
1199 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1200 hw->guest_features);
1203 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1205 "VIRTIO_F_VERSION_1 features is not enabled.");
1208 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1209 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1211 "failed to set FEATURES_OK status!");
1216 hw->req_guest_features = req_features;
1222 * Process Virtio Config changed interrupt and call the callback
1223 * if link state changed.
1226 virtio_interrupt_handler(void *param)
1228 struct rte_eth_dev *dev = param;
1229 struct virtio_hw *hw = dev->data->dev_private;
1232 /* Read interrupt status which clears interrupt */
1233 isr = vtpci_isr(hw);
1234 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1236 if (virtio_intr_enable(dev) < 0)
1237 PMD_DRV_LOG(ERR, "interrupt enable failed");
1239 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1240 if (virtio_dev_link_update(dev, 0) == 0)
1241 _rte_eth_dev_callback_process(dev,
1242 RTE_ETH_EVENT_INTR_LSC,
1248 /* set rx and tx handlers according to what is supported */
1250 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1252 struct virtio_hw *hw = eth_dev->data->dev_private;
1254 if (hw->use_simple_rx) {
1255 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1256 eth_dev->data->port_id);
1257 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1258 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1260 "virtio: using mergeable buffer Rx path on port %u",
1261 eth_dev->data->port_id);
1262 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1264 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1265 eth_dev->data->port_id);
1266 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1269 if (hw->use_simple_tx) {
1270 PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
1271 eth_dev->data->port_id);
1272 eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1274 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1275 eth_dev->data->port_id);
1276 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1280 /* Only support 1:1 queue/interrupt mapping so far.
1281 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1282 * interrupt vectors (<N+1).
1285 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1288 struct virtio_hw *hw = dev->data->dev_private;
1290 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1291 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1292 dev->intr_handle->intr_vec[i] = i + 1;
1293 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1294 VIRTIO_MSI_NO_VECTOR) {
1295 PMD_DRV_LOG(ERR, "failed to set queue vector");
1304 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1307 struct virtio_hw *hw = dev->data->dev_private;
1309 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1310 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1311 VTPCI_OPS(hw)->set_queue_irq(hw,
1312 hw->vqs[i * VTNET_CQ],
1313 VIRTIO_MSI_NO_VECTOR);
1317 virtio_configure_intr(struct rte_eth_dev *dev)
1319 struct virtio_hw *hw = dev->data->dev_private;
1321 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1322 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1326 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1327 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1331 if (!dev->intr_handle->intr_vec) {
1332 dev->intr_handle->intr_vec =
1333 rte_zmalloc("intr_vec",
1334 hw->max_queue_pairs * sizeof(int), 0);
1335 if (!dev->intr_handle->intr_vec) {
1336 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1337 hw->max_queue_pairs);
1342 /* Re-register callback to update max_intr */
1343 rte_intr_callback_unregister(dev->intr_handle,
1344 virtio_interrupt_handler,
1346 rte_intr_callback_register(dev->intr_handle,
1347 virtio_interrupt_handler,
1350 /* DO NOT try to remove this! This function will enable msix, or QEMU
1351 * will encounter SIGSEGV when DRIVER_OK is sent.
1352 * And for legacy devices, this should be done before queue/vec binding
1353 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1354 * (22) will be ignored.
1356 if (virtio_intr_enable(dev) < 0) {
1357 PMD_DRV_LOG(ERR, "interrupt enable failed");
1361 if (virtio_queues_bind_intr(dev) < 0) {
1362 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1369 /* reset device and renegotiate features if needed */
1371 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1373 struct virtio_hw *hw = eth_dev->data->dev_private;
1374 struct virtio_net_config *config;
1375 struct virtio_net_config local_config;
1376 struct rte_pci_device *pci_dev = NULL;
1379 /* Reset the device although not necessary at startup */
1382 /* Tell the host we've noticed this device. */
1383 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1385 /* Tell the host we've known how to drive the device. */
1386 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1387 if (virtio_negotiate_features(hw, req_features) < 0)
1390 if (!hw->virtio_user_dev) {
1391 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1392 rte_eth_copy_pci_info(eth_dev, pci_dev);
1395 /* If host does not support both status and MSI-X then disable LSC */
1396 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1397 hw->use_msix != VIRTIO_MSIX_NONE)
1398 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1400 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1402 /* Setting up rx_header size for the device */
1403 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1404 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1405 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1407 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1409 /* Copy the permanent MAC address to: virtio_hw */
1410 virtio_get_hwaddr(hw);
1411 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1412 ð_dev->data->mac_addrs[0]);
1414 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1415 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1416 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1418 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1419 config = &local_config;
1421 vtpci_read_dev_config(hw,
1422 offsetof(struct virtio_net_config, mac),
1423 &config->mac, sizeof(config->mac));
1425 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1426 vtpci_read_dev_config(hw,
1427 offsetof(struct virtio_net_config, status),
1428 &config->status, sizeof(config->status));
1431 "VIRTIO_NET_F_STATUS is not supported");
1435 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1436 vtpci_read_dev_config(hw,
1437 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1438 &config->max_virtqueue_pairs,
1439 sizeof(config->max_virtqueue_pairs));
1442 "VIRTIO_NET_F_MQ is not supported");
1443 config->max_virtqueue_pairs = 1;
1446 hw->max_queue_pairs = config->max_virtqueue_pairs;
1448 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1449 vtpci_read_dev_config(hw,
1450 offsetof(struct virtio_net_config, mtu),
1452 sizeof(config->mtu));
1455 * MTU value has already been checked at negotiation
1456 * time, but check again in case it has changed since
1457 * then, which should not happen.
1459 if (config->mtu < ETHER_MIN_MTU) {
1460 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1465 hw->max_mtu = config->mtu;
1466 /* Set initial MTU to maximum one supported by vhost */
1467 eth_dev->data->mtu = config->mtu;
1470 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1471 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1474 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1475 config->max_virtqueue_pairs);
1476 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1478 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1479 config->mac[0], config->mac[1],
1480 config->mac[2], config->mac[3],
1481 config->mac[4], config->mac[5]);
1483 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1484 hw->max_queue_pairs = 1;
1485 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1486 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1489 ret = virtio_alloc_queues(eth_dev);
1493 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1494 if (virtio_configure_intr(eth_dev) < 0) {
1495 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1500 vtpci_reinit_complete(hw);
1503 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1504 eth_dev->data->port_id, pci_dev->id.vendor_id,
1505 pci_dev->id.device_id);
1511 * Remap the PCI device again (IO port map for legacy device and
1512 * memory map for modern device), so that the secondary process
1513 * could have the PCI initiated correctly.
1516 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1520 * We don't have to re-parse the PCI config space, since
1521 * rte_pci_map_device() makes sure the mapped address
1522 * in secondary process would equal to the one mapped in
1523 * the primary process: error will be returned if that
1524 * requirement is not met.
1526 * That said, we could simply reuse all cap pointers
1527 * (such as dev_cfg, common_cfg, etc.) parsed from the
1528 * primary process, which is stored in shared memory.
1530 if (rte_pci_map_device(pci_dev)) {
1531 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1535 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1543 virtio_set_vtpci_ops(struct virtio_hw *hw)
1545 #ifdef RTE_VIRTIO_USER
1546 if (hw->virtio_user_dev)
1547 VTPCI_OPS(hw) = &virtio_user_ops;
1551 VTPCI_OPS(hw) = &modern_ops;
1553 VTPCI_OPS(hw) = &legacy_ops;
1557 * This function is based on probe() function in virtio_pci.c
1558 * It returns 0 on success.
1561 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1563 struct virtio_hw *hw = eth_dev->data->dev_private;
1566 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1568 eth_dev->dev_ops = &virtio_eth_dev_ops;
1570 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1571 if (!hw->virtio_user_dev) {
1572 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1577 virtio_set_vtpci_ops(hw);
1578 set_rxtx_funcs(eth_dev);
1583 /* Allocate memory for storing MAC addresses */
1584 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1585 if (eth_dev->data->mac_addrs == NULL) {
1587 "Failed to allocate %d bytes needed to store MAC addresses",
1588 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1592 hw->port_id = eth_dev->data->port_id;
1593 /* For virtio_user case the hw->virtio_user_dev is populated by
1594 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1596 if (!hw->virtio_user_dev) {
1597 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1602 /* reset device and negotiate default features */
1603 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1607 /* Setup interrupt callback */
1608 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1609 rte_intr_callback_register(eth_dev->intr_handle,
1610 virtio_interrupt_handler, eth_dev);
1615 rte_free(eth_dev->data->mac_addrs);
1620 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1622 PMD_INIT_FUNC_TRACE();
1624 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1627 virtio_dev_stop(eth_dev);
1628 virtio_dev_close(eth_dev);
1630 eth_dev->dev_ops = NULL;
1631 eth_dev->tx_pkt_burst = NULL;
1632 eth_dev->rx_pkt_burst = NULL;
1634 rte_free(eth_dev->data->mac_addrs);
1635 eth_dev->data->mac_addrs = NULL;
1637 /* reset interrupt callback */
1638 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1639 rte_intr_callback_unregister(eth_dev->intr_handle,
1640 virtio_interrupt_handler,
1642 if (eth_dev->device)
1643 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1645 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1650 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1651 struct rte_pci_device *pci_dev)
1653 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1654 eth_virtio_dev_init);
1657 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1659 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1662 static struct rte_pci_driver rte_virtio_pmd = {
1664 .name = "net_virtio",
1666 .id_table = pci_id_virtio_map,
1668 .probe = eth_virtio_pci_probe,
1669 .remove = eth_virtio_pci_remove,
1672 RTE_INIT(rte_virtio_pmd_init);
1674 rte_virtio_pmd_init(void)
1676 if (rte_eal_iopl_init() != 0) {
1677 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1681 rte_pci_register(&rte_virtio_pmd);
1685 * Configure virtio device
1686 * It returns 0 on success.
1689 virtio_dev_configure(struct rte_eth_dev *dev)
1691 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1692 struct virtio_hw *hw = dev->data->dev_private;
1693 uint64_t req_features;
1696 PMD_INIT_LOG(DEBUG, "configure");
1697 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1699 if (dev->data->dev_conf.intr_conf.rxq) {
1700 ret = virtio_init_device(dev, hw->req_guest_features);
1705 /* The name hw_ip_checksum is a bit confusing since it can be
1706 * set by the application to request L3 and/or L4 checksums. In
1707 * case of virtio, only L4 checksum is supported.
1709 if (rxmode->hw_ip_checksum)
1710 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1712 if (rxmode->enable_lro)
1714 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1715 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1717 /* if request features changed, reinit the device */
1718 if (req_features != hw->req_guest_features) {
1719 ret = virtio_init_device(dev, req_features);
1724 if (rxmode->hw_ip_checksum &&
1725 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
1727 "rx checksum not available on this host");
1731 if (rxmode->enable_lro &&
1732 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1733 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
1735 "Large Receive Offload not available on this host");
1739 /* start control queue */
1740 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1741 virtio_dev_cq_start(dev);
1743 hw->vlan_strip = rxmode->hw_vlan_strip;
1745 if (rxmode->hw_vlan_filter
1746 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1748 "vlan filtering not available on this host");
1752 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1753 /* Enable vector (0) for Link State Intrerrupt */
1754 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1755 VIRTIO_MSI_NO_VECTOR) {
1756 PMD_DRV_LOG(ERR, "failed to set config vector");
1760 hw->use_simple_rx = 1;
1761 hw->use_simple_tx = 1;
1763 #if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
1764 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
1765 hw->use_simple_rx = 0;
1766 hw->use_simple_tx = 0;
1769 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1770 hw->use_simple_rx = 0;
1771 hw->use_simple_tx = 0;
1774 if (rxmode->hw_ip_checksum)
1775 hw->use_simple_rx = 0;
1782 virtio_dev_start(struct rte_eth_dev *dev)
1784 uint16_t nb_queues, i;
1785 struct virtnet_rx *rxvq;
1786 struct virtnet_tx *txvq __rte_unused;
1787 struct virtio_hw *hw = dev->data->dev_private;
1790 /* Finish the initialization of the queues */
1791 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1792 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1796 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1797 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1802 /* check if lsc interrupt feature is enabled */
1803 if (dev->data->dev_conf.intr_conf.lsc) {
1804 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1805 PMD_DRV_LOG(ERR, "link status not supported by host");
1810 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1811 * in device configure, but it could be unmapped when device is
1814 if (dev->data->dev_conf.intr_conf.lsc ||
1815 dev->data->dev_conf.intr_conf.rxq) {
1816 virtio_intr_disable(dev);
1818 if (virtio_intr_enable(dev) < 0) {
1819 PMD_DRV_LOG(ERR, "interrupt enable failed");
1824 /*Notify the backend
1825 *Otherwise the tap backend might already stop its queue due to fullness.
1826 *vhost backend will have no chance to be waked up
1828 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1829 if (hw->max_queue_pairs > 1) {
1830 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1834 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1836 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1837 rxvq = dev->data->rx_queues[i];
1838 /* Flush the old packets */
1839 virtqueue_rxvq_flush(rxvq->vq);
1840 virtqueue_notify(rxvq->vq);
1843 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1844 txvq = dev->data->tx_queues[i];
1845 virtqueue_notify(txvq->vq);
1848 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1850 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1851 rxvq = dev->data->rx_queues[i];
1852 VIRTQUEUE_DUMP(rxvq->vq);
1855 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1856 txvq = dev->data->tx_queues[i];
1857 VIRTQUEUE_DUMP(txvq->vq);
1860 set_rxtx_funcs(dev);
1863 /* Initialize Link state */
1864 virtio_dev_link_update(dev, 0);
1869 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1871 struct rte_mbuf *buf;
1872 int i, mbuf_num = 0;
1874 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1875 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1878 "Before freeing rxq[%d] used and unused buf", i);
1879 VIRTQUEUE_DUMP(rxvq->vq);
1881 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1882 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1883 rte_pktmbuf_free(buf);
1887 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1889 "After freeing rxq[%d] used and unused buf", i);
1890 VIRTQUEUE_DUMP(rxvq->vq);
1893 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1894 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1897 "Before freeing txq[%d] used and unused bufs",
1899 VIRTQUEUE_DUMP(txvq->vq);
1902 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1903 rte_pktmbuf_free(buf);
1907 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1909 "After freeing txq[%d] used and unused buf", i);
1910 VIRTQUEUE_DUMP(txvq->vq);
1915 * Stop device: disable interrupt and mark link down
1918 virtio_dev_stop(struct rte_eth_dev *dev)
1920 struct virtio_hw *hw = dev->data->dev_private;
1921 struct rte_eth_link link;
1922 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
1924 PMD_INIT_LOG(DEBUG, "stop");
1926 if (intr_conf->lsc || intr_conf->rxq)
1927 virtio_intr_disable(dev);
1930 memset(&link, 0, sizeof(link));
1931 virtio_dev_atomic_write_link_status(dev, &link);
1935 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1937 struct rte_eth_link link, old;
1939 struct virtio_hw *hw = dev->data->dev_private;
1940 memset(&link, 0, sizeof(link));
1941 virtio_dev_atomic_read_link_status(dev, &link);
1943 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1944 link.link_speed = ETH_SPEED_NUM_10G;
1946 if (hw->started == 0) {
1947 link.link_status = ETH_LINK_DOWN;
1948 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1949 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1950 vtpci_read_dev_config(hw,
1951 offsetof(struct virtio_net_config, status),
1952 &status, sizeof(status));
1953 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1954 link.link_status = ETH_LINK_DOWN;
1955 PMD_INIT_LOG(DEBUG, "Port %d is down",
1956 dev->data->port_id);
1958 link.link_status = ETH_LINK_UP;
1959 PMD_INIT_LOG(DEBUG, "Port %d is up",
1960 dev->data->port_id);
1963 link.link_status = ETH_LINK_UP;
1965 virtio_dev_atomic_write_link_status(dev, &link);
1967 return (old.link_status == link.link_status) ? -1 : 0;
1971 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1973 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1974 struct virtio_hw *hw = dev->data->dev_private;
1976 if (mask & ETH_VLAN_FILTER_MASK) {
1977 if (rxmode->hw_vlan_filter &&
1978 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1981 "vlan filtering not available on this host");
1987 if (mask & ETH_VLAN_STRIP_MASK)
1988 hw->vlan_strip = rxmode->hw_vlan_strip;
1994 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1996 uint64_t tso_mask, host_features;
1997 struct virtio_hw *hw = dev->data->dev_private;
1999 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2001 dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
2002 dev_info->max_rx_queues =
2003 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2004 dev_info->max_tx_queues =
2005 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2006 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2007 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2008 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2009 dev_info->default_txconf = (struct rte_eth_txconf) {
2010 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
2013 host_features = VTPCI_OPS(hw)->get_features(hw);
2014 dev_info->rx_offload_capa = 0;
2015 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2016 dev_info->rx_offload_capa |=
2017 DEV_RX_OFFLOAD_TCP_CKSUM |
2018 DEV_RX_OFFLOAD_UDP_CKSUM;
2020 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2021 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2022 if ((host_features & tso_mask) == tso_mask)
2023 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2025 dev_info->tx_offload_capa = 0;
2026 if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2027 dev_info->tx_offload_capa |=
2028 DEV_TX_OFFLOAD_UDP_CKSUM |
2029 DEV_TX_OFFLOAD_TCP_CKSUM;
2031 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2032 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2033 if ((hw->guest_features & tso_mask) == tso_mask)
2034 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2038 * It enables testpmd to collect per queue stats.
2041 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2042 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2043 __rte_unused uint8_t is_rx)
2048 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2049 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2050 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2052 RTE_INIT(virtio_init_log);
2054 virtio_init_log(void)
2056 virtio_logtype_init = rte_log_register("pmd.virtio.init");
2057 if (virtio_logtype_init >= 0)
2058 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2059 virtio_logtype_driver = rte_log_register("pmd.virtio.driver");
2060 if (virtio_logtype_driver >= 0)
2061 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);