4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_ethdev.h>
41 #include <rte_memcpy.h>
42 #include <rte_string_fns.h>
43 #include <rte_memzone.h>
44 #include <rte_malloc.h>
45 #include <rte_atomic.h>
46 #include <rte_branch_prediction.h>
48 #include <rte_ether.h>
49 #include <rte_common.h>
50 #include <rte_errno.h>
52 #include <rte_memory.h>
56 #include "virtio_ethdev.h"
57 #include "virtio_pci.h"
58 #include "virtio_logs.h"
59 #include "virtqueue.h"
60 #include "virtio_rxtx.h"
63 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
64 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
65 static int virtio_dev_configure(struct rte_eth_dev *dev);
66 static int virtio_dev_start(struct rte_eth_dev *dev);
67 static void virtio_dev_stop(struct rte_eth_dev *dev);
68 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
69 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
70 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
71 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
72 static void virtio_dev_info_get(struct rte_eth_dev *dev,
73 struct rte_eth_dev_info *dev_info);
74 static int virtio_dev_link_update(struct rte_eth_dev *dev,
75 __rte_unused int wait_to_complete);
77 static void virtio_set_hwaddr(struct virtio_hw *hw);
78 static void virtio_get_hwaddr(struct virtio_hw *hw);
80 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
81 struct rte_eth_stats *stats);
82 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
83 struct rte_eth_xstats *xstats, unsigned n);
84 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
85 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
86 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
87 uint16_t vlan_id, int on);
88 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
89 struct ether_addr *mac_addr,
90 uint32_t index, uint32_t vmdq __rte_unused);
91 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
92 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
93 struct ether_addr *mac_addr);
95 static int virtio_dev_queue_stats_mapping_set(
96 __rte_unused struct rte_eth_dev *eth_dev,
97 __rte_unused uint16_t queue_id,
98 __rte_unused uint8_t stat_idx,
99 __rte_unused uint8_t is_rx);
102 * The set of PCI devices this driver supports
104 static const struct rte_pci_id pci_id_virtio_map[] = {
106 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
107 #include "rte_pci_dev_ids.h"
109 { .vendor_id = 0, /* sentinel */ },
112 struct rte_virtio_xstats_name_off {
113 char name[RTE_ETH_XSTATS_NAME_SIZE];
117 /* [rt]x_qX_ is prepended to the name string here */
118 static const struct rte_virtio_xstats_name_off rte_virtio_q_stat_strings[] = {
119 {"good_packets", offsetof(struct virtqueue, packets)},
120 {"good_bytes", offsetof(struct virtqueue, bytes)},
121 {"errors", offsetof(struct virtqueue, errors)},
122 {"multicast_packets", offsetof(struct virtqueue, multicast)},
123 {"broadcast_packets", offsetof(struct virtqueue, broadcast)},
124 {"undersize_packets", offsetof(struct virtqueue, size_bins[0])},
125 {"size_64_packets", offsetof(struct virtqueue, size_bins[1])},
126 {"size_65_127_packets", offsetof(struct virtqueue, size_bins[2])},
127 {"size_128_255_packets", offsetof(struct virtqueue, size_bins[3])},
128 {"size_256_511_packets", offsetof(struct virtqueue, size_bins[4])},
129 {"size_512_1023_packets", offsetof(struct virtqueue, size_bins[5])},
130 {"size_1024_1517_packets", offsetof(struct virtqueue, size_bins[6])},
131 {"size_1518_max_packets", offsetof(struct virtqueue, size_bins[7])},
134 #define VIRTIO_NB_Q_XSTATS (sizeof(rte_virtio_q_stat_strings) / \
135 sizeof(rte_virtio_q_stat_strings[0]))
138 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
139 int *dlen, int pkt_num)
143 virtio_net_ctrl_ack status = ~0;
144 struct virtio_pmd_ctrl result;
146 ctrl->status = status;
148 if (!(vq && vq->hw->cvq)) {
150 "%s(): Control queue is not supported.",
154 head = vq->vq_desc_head_idx;
156 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
157 "vq->hw->cvq = %p vq = %p",
158 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
160 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
163 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
164 sizeof(struct virtio_pmd_ctrl));
167 * Format is enforced in qemu code:
168 * One TX packet for header;
169 * At least one TX packet per argument;
170 * One RX packet for ACK.
172 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
173 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
174 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
176 i = vq->vq_ring.desc[head].next;
178 for (k = 0; k < pkt_num; k++) {
179 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
180 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
181 + sizeof(struct virtio_net_ctrl_hdr)
182 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
183 vq->vq_ring.desc[i].len = dlen[k];
186 i = vq->vq_ring.desc[i].next;
189 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
190 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
191 + sizeof(struct virtio_net_ctrl_hdr);
192 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
195 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
197 vq_update_avail_ring(vq, head);
198 vq_update_avail_idx(vq);
200 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
202 virtqueue_notify(vq);
205 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
210 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
211 uint32_t idx, desc_idx, used_idx;
212 struct vring_used_elem *uep;
214 used_idx = (uint32_t)(vq->vq_used_cons_idx
215 & (vq->vq_nentries - 1));
216 uep = &vq->vq_ring.used->ring[used_idx];
217 idx = (uint32_t) uep->id;
220 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
221 desc_idx = vq->vq_ring.desc[desc_idx].next;
225 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
226 vq->vq_desc_head_idx = idx;
228 vq->vq_used_cons_idx++;
232 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
233 vq->vq_free_cnt, vq->vq_desc_head_idx);
235 memcpy(&result, vq->virtio_net_hdr_mz->addr,
236 sizeof(struct virtio_pmd_ctrl));
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(struct virtqueue *vq) {
267 struct virtio_hw *hw;
271 hw->vtpci_ops->del_queue(hw, vq);
273 rte_free(vq->sw_ring);
278 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
281 uint16_t vtpci_queue_idx,
283 unsigned int socket_id,
284 struct virtqueue **pvq)
286 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
287 const struct rte_memzone *mz;
288 unsigned int vq_size, size;
289 struct virtio_hw *hw = dev->data->dev_private;
290 struct virtqueue *vq = NULL;
292 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
295 * Read the virtqueue size from the Queue Size field
296 * Always power of 2 and if 0 virtqueue does not exist
298 vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
299 PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
301 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
305 if (!rte_is_power_of_2(vq_size)) {
306 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
310 if (queue_type == VTNET_RQ) {
311 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
312 dev->data->port_id, queue_idx);
313 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
314 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
315 vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
316 (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
317 sizeof(vq->sw_ring[0]), RTE_CACHE_LINE_SIZE, socket_id);
318 } else if (queue_type == VTNET_TQ) {
319 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
320 dev->data->port_id, queue_idx);
321 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
322 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
323 } else if (queue_type == VTNET_CQ) {
324 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
326 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
327 vq_size * sizeof(struct vq_desc_extra),
328 RTE_CACHE_LINE_SIZE);
331 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
334 if (queue_type == VTNET_RQ && vq->sw_ring == NULL) {
335 PMD_INIT_LOG(ERR, "%s: Can not allocate RX soft ring",
342 vq->port_id = dev->data->port_id;
343 vq->queue_id = queue_idx;
344 vq->vq_queue_index = vtpci_queue_idx;
345 vq->vq_nentries = vq_size;
347 if (nb_desc == 0 || nb_desc > vq_size)
349 vq->vq_free_cnt = nb_desc;
352 * Reserve a memzone for vring elements
354 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
355 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
356 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
358 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
359 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
361 if (rte_errno == EEXIST)
362 mz = rte_memzone_lookup(vq_name);
370 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
371 * and only accepts 32 bit page frame number.
372 * Check if the allocated physical memory exceeds 16TB.
374 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
375 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
380 memset(mz->addr, 0, sizeof(mz->len));
382 vq->vq_ring_mem = mz->phys_addr;
383 vq->vq_ring_virt_mem = mz->addr;
384 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr);
385 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
386 vq->virtio_net_hdr_mz = NULL;
387 vq->virtio_net_hdr_mem = 0;
389 if (queue_type == VTNET_TQ) {
391 * For each xmit packet, allocate a virtio_net_hdr
393 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
394 dev->data->port_id, queue_idx);
395 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
396 vq_size * hw->vtnet_hdr_size,
397 socket_id, 0, RTE_CACHE_LINE_SIZE);
398 if (vq->virtio_net_hdr_mz == NULL) {
399 if (rte_errno == EEXIST)
400 vq->virtio_net_hdr_mz =
401 rte_memzone_lookup(vq_name);
402 if (vq->virtio_net_hdr_mz == NULL) {
407 vq->virtio_net_hdr_mem =
408 vq->virtio_net_hdr_mz->phys_addr;
409 memset(vq->virtio_net_hdr_mz->addr, 0,
410 vq_size * hw->vtnet_hdr_size);
411 } else if (queue_type == VTNET_CQ) {
412 /* Allocate a page for control vq command, data and status */
413 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
415 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
416 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
417 if (vq->virtio_net_hdr_mz == NULL) {
418 if (rte_errno == EEXIST)
419 vq->virtio_net_hdr_mz =
420 rte_memzone_lookup(vq_name);
421 if (vq->virtio_net_hdr_mz == NULL) {
426 vq->virtio_net_hdr_mem =
427 vq->virtio_net_hdr_mz->phys_addr;
428 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
431 hw->vtpci_ops->setup_queue(hw, vq);
438 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
441 struct virtqueue *vq;
443 struct virtio_hw *hw = dev->data->dev_private;
445 PMD_INIT_FUNC_TRACE();
446 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
447 vtpci_queue_idx, 0, socket_id, &vq);
449 PMD_INIT_LOG(ERR, "control vq initialization failed");
458 virtio_free_queues(struct rte_eth_dev *dev)
462 for (i = 0; i < dev->data->nb_rx_queues; i++)
463 virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
465 dev->data->nb_rx_queues = 0;
467 for (i = 0; i < dev->data->nb_tx_queues; i++)
468 virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
470 dev->data->nb_tx_queues = 0;
474 virtio_dev_close(struct rte_eth_dev *dev)
476 struct virtio_hw *hw = dev->data->dev_private;
477 struct rte_pci_device *pci_dev = dev->pci_dev;
479 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
481 if (hw->started == 1)
482 virtio_dev_stop(dev);
485 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
486 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
488 virtio_dev_free_mbufs(dev);
489 virtio_free_queues(dev);
493 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
495 struct virtio_hw *hw = dev->data->dev_private;
496 struct virtio_pmd_ctrl ctrl;
500 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
501 PMD_INIT_LOG(INFO, "host does not support rx control\n");
505 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
506 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
510 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
512 PMD_INIT_LOG(ERR, "Failed to enable promisc");
516 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
518 struct virtio_hw *hw = dev->data->dev_private;
519 struct virtio_pmd_ctrl ctrl;
523 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
524 PMD_INIT_LOG(INFO, "host does not support rx control\n");
528 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
529 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
533 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
535 PMD_INIT_LOG(ERR, "Failed to disable promisc");
539 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
541 struct virtio_hw *hw = dev->data->dev_private;
542 struct virtio_pmd_ctrl ctrl;
546 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
547 PMD_INIT_LOG(INFO, "host does not support rx control\n");
551 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
552 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
556 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
558 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
562 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
564 struct virtio_hw *hw = dev->data->dev_private;
565 struct virtio_pmd_ctrl ctrl;
569 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
570 PMD_INIT_LOG(INFO, "host does not support rx control\n");
574 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
575 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
579 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
581 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
585 * dev_ops for virtio, bare necessities for basic operation
587 static const struct eth_dev_ops virtio_eth_dev_ops = {
588 .dev_configure = virtio_dev_configure,
589 .dev_start = virtio_dev_start,
590 .dev_stop = virtio_dev_stop,
591 .dev_close = virtio_dev_close,
592 .promiscuous_enable = virtio_dev_promiscuous_enable,
593 .promiscuous_disable = virtio_dev_promiscuous_disable,
594 .allmulticast_enable = virtio_dev_allmulticast_enable,
595 .allmulticast_disable = virtio_dev_allmulticast_disable,
597 .dev_infos_get = virtio_dev_info_get,
598 .stats_get = virtio_dev_stats_get,
599 .xstats_get = virtio_dev_xstats_get,
600 .stats_reset = virtio_dev_stats_reset,
601 .xstats_reset = virtio_dev_stats_reset,
602 .link_update = virtio_dev_link_update,
603 .rx_queue_setup = virtio_dev_rx_queue_setup,
604 .rx_queue_release = virtio_dev_rx_queue_release,
605 .tx_queue_setup = virtio_dev_tx_queue_setup,
606 .tx_queue_release = virtio_dev_tx_queue_release,
607 /* collect stats per queue */
608 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
609 .vlan_filter_set = virtio_vlan_filter_set,
610 .mac_addr_add = virtio_mac_addr_add,
611 .mac_addr_remove = virtio_mac_addr_remove,
612 .mac_addr_set = virtio_mac_addr_set,
616 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
617 struct rte_eth_link *link)
619 struct rte_eth_link *dst = link;
620 struct rte_eth_link *src = &(dev->data->dev_link);
622 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
623 *(uint64_t *)src) == 0)
630 * Atomically writes the link status information into global
631 * structure rte_eth_dev.
634 * - Pointer to the structure rte_eth_dev to read from.
635 * - Pointer to the buffer to be saved with the link status.
638 * - On success, zero.
639 * - On failure, negative value.
642 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
643 struct rte_eth_link *link)
645 struct rte_eth_link *dst = &(dev->data->dev_link);
646 struct rte_eth_link *src = link;
648 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
649 *(uint64_t *)src) == 0)
656 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
660 for (i = 0; i < dev->data->nb_tx_queues; i++) {
661 const struct virtqueue *txvq = dev->data->tx_queues[i];
665 stats->opackets += txvq->packets;
666 stats->obytes += txvq->bytes;
667 stats->oerrors += txvq->errors;
669 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
670 stats->q_opackets[i] = txvq->packets;
671 stats->q_obytes[i] = txvq->bytes;
675 for (i = 0; i < dev->data->nb_rx_queues; i++) {
676 const struct virtqueue *rxvq = dev->data->rx_queues[i];
680 stats->ipackets += rxvq->packets;
681 stats->ibytes += rxvq->bytes;
682 stats->ierrors += rxvq->errors;
684 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
685 stats->q_ipackets[i] = rxvq->packets;
686 stats->q_ibytes[i] = rxvq->bytes;
690 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
694 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
700 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_Q_XSTATS +
701 dev->data->nb_rx_queues * VIRTIO_NB_Q_XSTATS;
706 for (i = 0; i < dev->data->nb_rx_queues; i++) {
707 struct virtqueue *rxvq = dev->data->rx_queues[i];
714 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
715 snprintf(xstats[count].name, sizeof(xstats[count].name),
717 rte_virtio_q_stat_strings[t].name);
718 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
719 rte_virtio_q_stat_strings[t].offset);
724 for (i = 0; i < dev->data->nb_tx_queues; i++) {
725 struct virtqueue *txvq = dev->data->tx_queues[i];
732 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
733 snprintf(xstats[count].name, sizeof(xstats[count].name),
735 rte_virtio_q_stat_strings[t].name);
736 xstats[count].value = *(uint64_t *)(((char *)txvq) +
737 rte_virtio_q_stat_strings[t].offset);
746 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
748 virtio_update_stats(dev, stats);
752 virtio_dev_stats_reset(struct rte_eth_dev *dev)
756 for (i = 0; i < dev->data->nb_tx_queues; i++) {
757 struct virtqueue *txvq = dev->data->tx_queues[i];
766 memset(txvq->size_bins, 0, sizeof(txvq->size_bins[0]) * 8);
769 for (i = 0; i < dev->data->nb_rx_queues; i++) {
770 struct virtqueue *rxvq = dev->data->rx_queues[i];
779 memset(rxvq->size_bins, 0, sizeof(rxvq->size_bins[0]) * 8);
784 virtio_set_hwaddr(struct virtio_hw *hw)
786 vtpci_write_dev_config(hw,
787 offsetof(struct virtio_net_config, mac),
788 &hw->mac_addr, ETHER_ADDR_LEN);
792 virtio_get_hwaddr(struct virtio_hw *hw)
794 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
795 vtpci_read_dev_config(hw,
796 offsetof(struct virtio_net_config, mac),
797 &hw->mac_addr, ETHER_ADDR_LEN);
799 eth_random_addr(&hw->mac_addr[0]);
800 virtio_set_hwaddr(hw);
805 virtio_mac_table_set(struct virtio_hw *hw,
806 const struct virtio_net_ctrl_mac *uc,
807 const struct virtio_net_ctrl_mac *mc)
809 struct virtio_pmd_ctrl ctrl;
812 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
813 PMD_DRV_LOG(INFO, "host does not support mac table\n");
817 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
818 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
820 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
821 memcpy(ctrl.data, uc, len[0]);
823 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
824 memcpy(ctrl.data + len[0], mc, len[1]);
826 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
828 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
832 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
833 uint32_t index, uint32_t vmdq __rte_unused)
835 struct virtio_hw *hw = dev->data->dev_private;
836 const struct ether_addr *addrs = dev->data->mac_addrs;
838 struct virtio_net_ctrl_mac *uc, *mc;
840 if (index >= VIRTIO_MAX_MAC_ADDRS) {
841 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
845 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
847 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
850 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
851 const struct ether_addr *addr
852 = (i == index) ? mac_addr : addrs + i;
853 struct virtio_net_ctrl_mac *tbl
854 = is_multicast_ether_addr(addr) ? mc : uc;
856 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
859 virtio_mac_table_set(hw, uc, mc);
863 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
865 struct virtio_hw *hw = dev->data->dev_private;
866 struct ether_addr *addrs = dev->data->mac_addrs;
867 struct virtio_net_ctrl_mac *uc, *mc;
870 if (index >= VIRTIO_MAX_MAC_ADDRS) {
871 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
875 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
877 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
880 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
881 struct virtio_net_ctrl_mac *tbl;
883 if (i == index || is_zero_ether_addr(addrs + i))
886 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
887 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
890 virtio_mac_table_set(hw, uc, mc);
894 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
896 struct virtio_hw *hw = dev->data->dev_private;
898 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
900 /* Use atomic update if available */
901 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
902 struct virtio_pmd_ctrl ctrl;
903 int len = ETHER_ADDR_LEN;
905 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
906 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
908 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
909 virtio_send_command(hw->cvq, &ctrl, &len, 1);
910 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
911 virtio_set_hwaddr(hw);
915 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
917 struct virtio_hw *hw = dev->data->dev_private;
918 struct virtio_pmd_ctrl ctrl;
921 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
924 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
925 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
926 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
927 len = sizeof(vlan_id);
929 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
933 virtio_negotiate_features(struct virtio_hw *hw)
935 uint64_t host_features;
937 /* Prepare guest_features: feature that driver wants to support */
938 hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
939 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
942 /* Read device(host) feature bits */
943 host_features = hw->vtpci_ops->get_features(hw);
944 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
948 * Negotiate features: Subset of device feature bits are written back
949 * guest feature bits.
951 hw->guest_features = vtpci_negotiate_features(hw, host_features);
952 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
956 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
958 "VIRTIO_F_VERSION_1 features is not enabled.");
961 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
962 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
964 "failed to set FEATURES_OK status!");
973 * Process Virtio Config changed interrupt and call the callback
974 * if link state changed.
977 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
980 struct rte_eth_dev *dev = param;
981 struct virtio_hw *hw = dev->data->dev_private;
984 /* Read interrupt status which clears interrupt */
986 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
988 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
989 PMD_DRV_LOG(ERR, "interrupt enable failed");
991 if (isr & VIRTIO_PCI_ISR_CONFIG) {
992 if (virtio_dev_link_update(dev, 0) == 0)
993 _rte_eth_dev_callback_process(dev,
994 RTE_ETH_EVENT_INTR_LSC);
1000 rx_func_get(struct rte_eth_dev *eth_dev)
1002 struct virtio_hw *hw = eth_dev->data->dev_private;
1003 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1004 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1006 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1010 * This function is based on probe() function in virtio_pci.c
1011 * It returns 0 on success.
1014 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1016 struct virtio_hw *hw = eth_dev->data->dev_private;
1017 struct virtio_net_config *config;
1018 struct virtio_net_config local_config;
1019 struct rte_pci_device *pci_dev;
1022 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
1024 eth_dev->dev_ops = &virtio_eth_dev_ops;
1025 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1027 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1028 rx_func_get(eth_dev);
1032 /* Allocate memory for storing MAC addresses */
1033 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1034 if (eth_dev->data->mac_addrs == NULL) {
1036 "Failed to allocate %d bytes needed to store MAC addresses",
1037 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1041 pci_dev = eth_dev->pci_dev;
1043 ret = vtpci_init(pci_dev, hw);
1047 /* Reset the device although not necessary at startup */
1050 /* Tell the host we've noticed this device. */
1051 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1053 /* Tell the host we've known how to drive the device. */
1054 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1055 if (virtio_negotiate_features(hw) < 0)
1058 /* If host does not support status then disable LSC */
1059 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1060 pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1062 rte_eth_copy_pci_info(eth_dev, pci_dev);
1064 rx_func_get(eth_dev);
1066 /* Setting up rx_header size for the device */
1067 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1068 vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1069 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1071 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1073 /* Copy the permanent MAC address to: virtio_hw */
1074 virtio_get_hwaddr(hw);
1075 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1076 ð_dev->data->mac_addrs[0]);
1078 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1079 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1080 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1082 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1083 config = &local_config;
1085 vtpci_read_dev_config(hw,
1086 offsetof(struct virtio_net_config, mac),
1087 &config->mac, sizeof(config->mac));
1089 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1090 vtpci_read_dev_config(hw,
1091 offsetof(struct virtio_net_config, status),
1092 &config->status, sizeof(config->status));
1095 "VIRTIO_NET_F_STATUS is not supported");
1099 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1100 vtpci_read_dev_config(hw,
1101 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1102 &config->max_virtqueue_pairs,
1103 sizeof(config->max_virtqueue_pairs));
1106 "VIRTIO_NET_F_MQ is not supported");
1107 config->max_virtqueue_pairs = 1;
1111 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1112 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1114 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1115 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1117 virtio_dev_cq_queue_setup(eth_dev,
1118 config->max_virtqueue_pairs * 2,
1121 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1122 config->max_virtqueue_pairs);
1123 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1125 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1126 config->mac[0], config->mac[1],
1127 config->mac[2], config->mac[3],
1128 config->mac[4], config->mac[5]);
1130 hw->max_rx_queues = 1;
1131 hw->max_tx_queues = 1;
1134 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1135 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1137 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1138 hw->max_rx_queues, hw->max_tx_queues);
1139 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1140 eth_dev->data->port_id, pci_dev->id.vendor_id,
1141 pci_dev->id.device_id);
1143 /* Setup interrupt callback */
1144 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1145 rte_intr_callback_register(&pci_dev->intr_handle,
1146 virtio_interrupt_handler, eth_dev);
1148 virtio_dev_cq_start(eth_dev);
1154 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1156 struct rte_pci_device *pci_dev;
1157 struct virtio_hw *hw = eth_dev->data->dev_private;
1159 PMD_INIT_FUNC_TRACE();
1161 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1164 /* Close it anyway since there's no way to know if closed */
1165 virtio_dev_close(eth_dev);
1167 pci_dev = eth_dev->pci_dev;
1169 eth_dev->dev_ops = NULL;
1170 eth_dev->tx_pkt_burst = NULL;
1171 eth_dev->rx_pkt_burst = NULL;
1173 virtio_dev_queue_release(hw->cvq);
1175 rte_free(eth_dev->data->mac_addrs);
1176 eth_dev->data->mac_addrs = NULL;
1178 /* reset interrupt callback */
1179 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1180 rte_intr_callback_unregister(&pci_dev->intr_handle,
1181 virtio_interrupt_handler,
1183 rte_eal_pci_unmap_device(pci_dev);
1185 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1190 static struct eth_driver rte_virtio_pmd = {
1192 .name = "rte_virtio_pmd",
1193 .id_table = pci_id_virtio_map,
1194 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1196 .eth_dev_init = eth_virtio_dev_init,
1197 .eth_dev_uninit = eth_virtio_dev_uninit,
1198 .dev_private_size = sizeof(struct virtio_hw),
1202 * Driver initialization routine.
1203 * Invoked once at EAL init time.
1204 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1205 * Returns 0 on success.
1208 rte_virtio_pmd_init(const char *name __rte_unused,
1209 const char *param __rte_unused)
1211 if (rte_eal_iopl_init() != 0) {
1212 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1216 rte_eth_driver_register(&rte_virtio_pmd);
1221 * Configure virtio device
1222 * It returns 0 on success.
1225 virtio_dev_configure(struct rte_eth_dev *dev)
1227 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1228 struct virtio_hw *hw = dev->data->dev_private;
1229 struct rte_pci_device *pci_dev = dev->pci_dev;
1231 PMD_INIT_LOG(DEBUG, "configure");
1233 if (rxmode->hw_ip_checksum) {
1234 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1238 hw->vlan_strip = rxmode->hw_vlan_strip;
1240 if (rxmode->hw_vlan_filter
1241 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1243 "vlan filtering not available on this host");
1247 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1248 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1249 PMD_DRV_LOG(ERR, "failed to set config vector");
1258 virtio_dev_start(struct rte_eth_dev *dev)
1260 uint16_t nb_queues, i;
1261 struct virtio_hw *hw = dev->data->dev_private;
1262 struct rte_pci_device *pci_dev = dev->pci_dev;
1264 /* check if lsc interrupt feature is enabled */
1265 if (dev->data->dev_conf.intr_conf.lsc) {
1266 if (!(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1267 PMD_DRV_LOG(ERR, "link status not supported by host");
1271 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1272 PMD_DRV_LOG(ERR, "interrupt enable failed");
1277 /* Initialize Link state */
1278 virtio_dev_link_update(dev, 0);
1280 /* On restart after stop do not touch queues */
1284 /* Do final configuration before rx/tx engine starts */
1285 virtio_dev_rxtx_start(dev);
1286 vtpci_reinit_complete(hw);
1290 /*Notify the backend
1291 *Otherwise the tap backend might already stop its queue due to fullness.
1292 *vhost backend will have no chance to be waked up
1294 nb_queues = dev->data->nb_rx_queues;
1295 if (nb_queues > 1) {
1296 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1300 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1302 for (i = 0; i < nb_queues; i++)
1303 virtqueue_notify(dev->data->rx_queues[i]);
1305 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1307 for (i = 0; i < dev->data->nb_rx_queues; i++)
1308 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1310 for (i = 0; i < dev->data->nb_tx_queues; i++)
1311 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1316 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1318 struct rte_mbuf *buf;
1319 int i, mbuf_num = 0;
1321 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1323 "Before freeing rxq[%d] used and unused buf", i);
1324 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1326 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p",
1327 i, dev->data->rx_queues[i]);
1328 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1329 dev->data->rx_queues[i])) != NULL) {
1330 rte_pktmbuf_free(buf);
1334 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1336 "After freeing rxq[%d] used and unused buf", i);
1337 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1340 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1342 "Before freeing txq[%d] used and unused bufs",
1344 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1347 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1348 dev->data->tx_queues[i])) != NULL) {
1349 rte_pktmbuf_free(buf);
1354 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1356 "After freeing txq[%d] used and unused buf", i);
1357 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1362 * Stop device: disable interrupt and mark link down
1365 virtio_dev_stop(struct rte_eth_dev *dev)
1367 struct rte_eth_link link;
1368 struct virtio_hw *hw = dev->data->dev_private;
1370 PMD_INIT_LOG(DEBUG, "stop");
1374 if (dev->data->dev_conf.intr_conf.lsc)
1375 rte_intr_disable(&dev->pci_dev->intr_handle);
1377 memset(&link, 0, sizeof(link));
1378 virtio_dev_atomic_write_link_status(dev, &link);
1382 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1384 struct rte_eth_link link, old;
1386 struct virtio_hw *hw = dev->data->dev_private;
1387 memset(&link, 0, sizeof(link));
1388 virtio_dev_atomic_read_link_status(dev, &link);
1390 link.link_duplex = FULL_DUPLEX;
1391 link.link_speed = SPEED_10G;
1393 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1394 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1395 vtpci_read_dev_config(hw,
1396 offsetof(struct virtio_net_config, status),
1397 &status, sizeof(status));
1398 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1399 link.link_status = 0;
1400 PMD_INIT_LOG(DEBUG, "Port %d is down",
1401 dev->data->port_id);
1403 link.link_status = 1;
1404 PMD_INIT_LOG(DEBUG, "Port %d is up",
1405 dev->data->port_id);
1408 link.link_status = 1; /* Link up */
1410 virtio_dev_atomic_write_link_status(dev, &link);
1412 return (old.link_status == link.link_status) ? -1 : 0;
1416 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1418 struct virtio_hw *hw = dev->data->dev_private;
1420 dev_info->driver_name = dev->driver->pci_drv.name;
1421 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1422 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1423 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1424 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1425 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1426 dev_info->default_txconf = (struct rte_eth_txconf) {
1427 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1432 * It enables testpmd to collect per queue stats.
1435 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1436 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1437 __rte_unused uint8_t is_rx)
1442 static struct rte_driver rte_virtio_driver = {
1444 .init = rte_virtio_pmd_init,
1447 PMD_REGISTER_DRIVER(rte_virtio_driver);