4 * Copyright(c) 2010-2014 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.
39 #ifdef RTE_EXEC_ENV_LINUXAPP
44 #include <rte_ethdev.h>
45 #include <rte_memcpy.h>
46 #include <rte_string_fns.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
52 #include <rte_ether.h>
53 #include <rte_common.h>
55 #include <rte_memory.h>
59 #include "virtio_ethdev.h"
60 #include "virtio_pci.h"
61 #include "virtio_logs.h"
62 #include "virtqueue.h"
65 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
66 static int virtio_dev_configure(struct rte_eth_dev *dev);
67 static int virtio_dev_start(struct rte_eth_dev *dev);
68 static void virtio_dev_stop(struct rte_eth_dev *dev);
69 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
70 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
71 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
72 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
73 static void virtio_dev_info_get(struct rte_eth_dev *dev,
74 struct rte_eth_dev_info *dev_info);
75 static int virtio_dev_link_update(struct rte_eth_dev *dev,
76 __rte_unused int wait_to_complete);
78 static void virtio_set_hwaddr(struct virtio_hw *hw);
79 static void virtio_get_hwaddr(struct virtio_hw *hw);
81 static void virtio_dev_rx_queue_release(__rte_unused void *rxq);
82 static void virtio_dev_tx_queue_release(__rte_unused void *txq);
84 static void virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
88 uint16_t vlan_id, int on);
89 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
90 struct ether_addr *mac_addr,
91 uint32_t index, uint32_t vmdq __rte_unused);
92 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
93 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94 struct ether_addr *mac_addr);
96 static int virtio_dev_queue_stats_mapping_set(
97 __rte_unused struct rte_eth_dev *eth_dev,
98 __rte_unused uint16_t queue_id,
99 __rte_unused uint8_t stat_idx,
100 __rte_unused uint8_t is_rx);
103 * The set of PCI devices this driver supports
105 static const struct rte_pci_id pci_id_virtio_map[] = {
107 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
108 #include "rte_pci_dev_ids.h"
110 { .vendor_id = 0, /* sentinel */ },
114 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
115 int *dlen, int pkt_num)
117 uint16_t head = vq->vq_desc_head_idx, i;
119 virtio_net_ctrl_ack status = ~0;
120 struct virtio_pmd_ctrl result;
122 ctrl->status = status;
126 "%s(): Control queue is not supported.",
131 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
132 "vq->hw->cvq = %p vq = %p",
133 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
135 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
138 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
139 sizeof(struct virtio_pmd_ctrl));
142 * Format is enforced in qemu code:
143 * One TX packet for header;
144 * At least one TX packet per argument;
145 * One RX packet for ACK.
147 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
148 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
149 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
151 i = vq->vq_ring.desc[head].next;
153 for (k = 0; k < pkt_num; k++) {
154 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
155 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
156 + sizeof(struct virtio_net_ctrl_hdr)
157 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
158 vq->vq_ring.desc[i].len = dlen[k];
161 i = vq->vq_ring.desc[i].next;
164 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
165 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
166 + sizeof(struct virtio_net_ctrl_hdr);
167 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
170 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
172 vq_update_avail_ring(vq, head);
173 vq_update_avail_idx(vq);
175 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
177 virtqueue_notify(vq);
180 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
185 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
186 uint32_t idx, desc_idx, used_idx;
187 struct vring_used_elem *uep;
189 used_idx = (uint32_t)(vq->vq_used_cons_idx
190 & (vq->vq_nentries - 1));
191 uep = &vq->vq_ring.used->ring[used_idx];
192 idx = (uint32_t) uep->id;
195 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
196 desc_idx = vq->vq_ring.desc[desc_idx].next;
200 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
201 vq->vq_desc_head_idx = idx;
203 vq->vq_used_cons_idx++;
207 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
208 vq->vq_free_cnt, vq->vq_desc_head_idx);
210 memcpy(&result, vq->virtio_net_hdr_mz->addr,
211 sizeof(struct virtio_pmd_ctrl));
213 return result.status;
217 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
219 struct virtio_hw *hw = dev->data->dev_private;
220 struct virtio_pmd_ctrl ctrl;
224 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
225 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
226 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
228 dlen[0] = sizeof(uint16_t);
230 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
232 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
233 "failed, this is too late now...");
240 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
243 uint16_t vtpci_queue_idx,
245 unsigned int socket_id,
246 struct virtqueue **pvq)
248 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
249 const struct rte_memzone *mz;
252 struct virtio_hw *hw = dev->data->dev_private;
253 struct virtqueue *vq = NULL;
255 /* Write the virtqueue index to the Queue Select Field */
256 VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
257 PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
260 * Read the virtqueue size from the Queue Size field
261 * Always power of 2 and if 0 virtqueue does not exist
263 vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
264 PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
268 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
270 } else if (!rte_is_power_of_2(vq_size)) {
271 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
273 } else if (nb_desc != vq_size) {
274 PMD_INIT_LOG(ERR, "Warning: nb_desc(%d) is not equal to vq size (%d), fall to vq size",
279 if (queue_type == VTNET_RQ) {
280 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
281 dev->data->port_id, queue_idx);
282 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
283 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
284 } else if (queue_type == VTNET_TQ) {
285 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
286 dev->data->port_id, queue_idx);
287 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
288 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
289 } else if (queue_type == VTNET_CQ) {
290 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
292 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
293 vq_size * sizeof(struct vq_desc_extra),
294 RTE_CACHE_LINE_SIZE);
297 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
302 vq->port_id = dev->data->port_id;
303 vq->queue_id = queue_idx;
304 vq->vq_queue_index = vtpci_queue_idx;
305 vq->vq_nentries = vq_size;
306 vq->vq_free_cnt = vq_size;
309 * Reserve a memzone for vring elements
311 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
312 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
313 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
315 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
316 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
323 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
324 * and only accepts 32 bit page frame number.
325 * Check if the allocated physical memory exceeds 16TB.
327 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
328 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
333 memset(mz->addr, 0, sizeof(mz->len));
335 vq->vq_ring_mem = mz->phys_addr;
336 vq->vq_ring_virt_mem = mz->addr;
337 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr);
338 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
339 vq->virtio_net_hdr_mz = NULL;
340 vq->virtio_net_hdr_mem = 0;
342 if (queue_type == VTNET_TQ) {
344 * For each xmit packet, allocate a virtio_net_hdr
346 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
347 dev->data->port_id, queue_idx);
348 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
349 vq_size * hw->vtnet_hdr_size,
350 socket_id, 0, RTE_CACHE_LINE_SIZE);
351 if (vq->virtio_net_hdr_mz == NULL) {
355 vq->virtio_net_hdr_mem =
356 vq->virtio_net_hdr_mz->phys_addr;
357 memset(vq->virtio_net_hdr_mz->addr, 0,
358 vq_size * hw->vtnet_hdr_size);
359 } else if (queue_type == VTNET_CQ) {
360 /* Allocate a page for control vq command, data and status */
361 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
363 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
364 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
365 if (vq->virtio_net_hdr_mz == NULL) {
369 vq->virtio_net_hdr_mem =
370 vq->virtio_net_hdr_mz->phys_addr;
371 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
375 * Set guest physical address of the virtqueue
376 * in VIRTIO_PCI_QUEUE_PFN config register of device
378 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
379 mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
385 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
388 struct virtqueue *vq;
389 uint16_t nb_desc = 0;
391 struct virtio_hw *hw = dev->data->dev_private;
393 PMD_INIT_FUNC_TRACE();
394 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
395 vtpci_queue_idx, nb_desc, socket_id, &vq);
397 PMD_INIT_LOG(ERR, "control vq initialization failed");
406 virtio_dev_close(struct rte_eth_dev *dev)
408 struct virtio_hw *hw = dev->data->dev_private;
409 struct rte_pci_device *pci_dev = dev->pci_dev;
411 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
414 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
415 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
418 virtio_dev_free_mbufs(dev);
422 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
424 struct virtio_hw *hw = dev->data->dev_private;
425 struct virtio_pmd_ctrl ctrl;
429 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
430 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
434 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
436 PMD_INIT_LOG(ERR, "Failed to enable promisc");
440 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
442 struct virtio_hw *hw = dev->data->dev_private;
443 struct virtio_pmd_ctrl ctrl;
447 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
448 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
452 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
454 PMD_INIT_LOG(ERR, "Failed to disable promisc");
458 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
460 struct virtio_hw *hw = dev->data->dev_private;
461 struct virtio_pmd_ctrl ctrl;
465 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
466 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
470 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
472 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
476 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
478 struct virtio_hw *hw = dev->data->dev_private;
479 struct virtio_pmd_ctrl ctrl;
483 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
484 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
488 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
490 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
494 * dev_ops for virtio, bare necessities for basic operation
496 static const struct eth_dev_ops virtio_eth_dev_ops = {
497 .dev_configure = virtio_dev_configure,
498 .dev_start = virtio_dev_start,
499 .dev_stop = virtio_dev_stop,
500 .dev_close = virtio_dev_close,
501 .promiscuous_enable = virtio_dev_promiscuous_enable,
502 .promiscuous_disable = virtio_dev_promiscuous_disable,
503 .allmulticast_enable = virtio_dev_allmulticast_enable,
504 .allmulticast_disable = virtio_dev_allmulticast_disable,
506 .dev_infos_get = virtio_dev_info_get,
507 .stats_get = virtio_dev_stats_get,
508 .stats_reset = virtio_dev_stats_reset,
509 .link_update = virtio_dev_link_update,
510 .rx_queue_setup = virtio_dev_rx_queue_setup,
511 /* meaningfull only to multiple queue */
512 .rx_queue_release = virtio_dev_rx_queue_release,
513 .tx_queue_setup = virtio_dev_tx_queue_setup,
514 /* meaningfull only to multiple queue */
515 .tx_queue_release = virtio_dev_tx_queue_release,
516 /* collect stats per queue */
517 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
518 .vlan_filter_set = virtio_vlan_filter_set,
519 .mac_addr_add = virtio_mac_addr_add,
520 .mac_addr_remove = virtio_mac_addr_remove,
521 .mac_addr_set = virtio_mac_addr_set,
525 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
526 struct rte_eth_link *link)
528 struct rte_eth_link *dst = link;
529 struct rte_eth_link *src = &(dev->data->dev_link);
531 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
532 *(uint64_t *)src) == 0)
539 * Atomically writes the link status information into global
540 * structure rte_eth_dev.
543 * - Pointer to the structure rte_eth_dev to read from.
544 * - Pointer to the buffer to be saved with the link status.
547 * - On success, zero.
548 * - On failure, negative value.
551 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
552 struct rte_eth_link *link)
554 struct rte_eth_link *dst = &(dev->data->dev_link);
555 struct rte_eth_link *src = link;
557 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
558 *(uint64_t *)src) == 0)
565 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
569 for (i = 0; i < dev->data->nb_tx_queues; i++) {
570 const struct virtqueue *txvq = dev->data->tx_queues[i];
574 stats->opackets += txvq->packets;
575 stats->obytes += txvq->bytes;
576 stats->oerrors += txvq->errors;
578 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
579 stats->q_opackets[i] = txvq->packets;
580 stats->q_obytes[i] = txvq->bytes;
584 for (i = 0; i < dev->data->nb_rx_queues; i++) {
585 const struct virtqueue *rxvq = dev->data->rx_queues[i];
589 stats->ipackets += rxvq->packets;
590 stats->ibytes += rxvq->bytes;
591 stats->ierrors += rxvq->errors;
593 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
594 stats->q_ipackets[i] = rxvq->packets;
595 stats->q_ibytes[i] = rxvq->bytes;
599 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
603 virtio_dev_stats_reset(struct rte_eth_dev *dev)
607 for (i = 0; i < dev->data->nb_tx_queues; i++) {
608 struct virtqueue *txvq = dev->data->tx_queues[i];
617 for (i = 0; i < dev->data->nb_rx_queues; i++) {
618 struct virtqueue *rxvq = dev->data->rx_queues[i];
627 dev->data->rx_mbuf_alloc_failed = 0;
631 virtio_set_hwaddr(struct virtio_hw *hw)
633 vtpci_write_dev_config(hw,
634 offsetof(struct virtio_net_config, mac),
635 &hw->mac_addr, ETHER_ADDR_LEN);
639 virtio_get_hwaddr(struct virtio_hw *hw)
641 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
642 vtpci_read_dev_config(hw,
643 offsetof(struct virtio_net_config, mac),
644 &hw->mac_addr, ETHER_ADDR_LEN);
646 eth_random_addr(&hw->mac_addr[0]);
647 virtio_set_hwaddr(hw);
652 virtio_mac_table_set(struct virtio_hw *hw,
653 const struct virtio_net_ctrl_mac *uc,
654 const struct virtio_net_ctrl_mac *mc)
656 struct virtio_pmd_ctrl ctrl;
659 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
660 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
662 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
663 memcpy(ctrl.data, uc, len[0]);
665 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
666 memcpy(ctrl.data + len[0], mc, len[1]);
668 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
670 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
676 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
677 uint32_t index, uint32_t vmdq __rte_unused)
679 struct virtio_hw *hw = dev->data->dev_private;
680 const struct ether_addr *addrs = dev->data->mac_addrs;
682 struct virtio_net_ctrl_mac *uc, *mc;
684 if (index >= VIRTIO_MAX_MAC_ADDRS) {
685 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
689 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
691 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
694 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
695 const struct ether_addr *addr
696 = (i == index) ? mac_addr : addrs + i;
697 struct virtio_net_ctrl_mac *tbl
698 = is_multicast_ether_addr(addr) ? mc : uc;
700 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
703 virtio_mac_table_set(hw, uc, mc);
707 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
709 struct virtio_hw *hw = dev->data->dev_private;
710 struct ether_addr *addrs = dev->data->mac_addrs;
711 struct virtio_net_ctrl_mac *uc, *mc;
714 if (index >= VIRTIO_MAX_MAC_ADDRS) {
715 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
719 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
721 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
724 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
725 struct virtio_net_ctrl_mac *tbl;
727 if (i == index || is_zero_ether_addr(addrs + i))
730 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
731 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
734 virtio_mac_table_set(hw, uc, mc);
738 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
740 struct virtio_hw *hw = dev->data->dev_private;
742 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
744 /* Use atomic update if available */
745 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
746 struct virtio_pmd_ctrl ctrl;
747 int len = ETHER_ADDR_LEN;
749 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
750 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
752 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
753 virtio_send_command(hw->cvq, &ctrl, &len, 1);
754 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
755 virtio_set_hwaddr(hw);
759 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
761 struct virtio_hw *hw = dev->data->dev_private;
762 struct virtio_pmd_ctrl ctrl;
765 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
768 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
769 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
770 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
771 len = sizeof(vlan_id);
773 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
777 virtio_negotiate_features(struct virtio_hw *hw)
779 uint32_t host_features, mask;
781 /* checksum offload not implemented */
782 mask = VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
784 /* TSO and LRO are only available when their corresponding
785 * checksum offload feature is also negotiated.
787 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN;
788 mask |= VIRTIO_NET_F_GUEST_TSO4 | VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN;
789 mask |= VTNET_LRO_FEATURES;
791 /* not negotiating INDIRECT descriptor table support */
792 mask |= VIRTIO_RING_F_INDIRECT_DESC;
794 /* Prepare guest_features: feature that driver wants to support */
795 hw->guest_features = VTNET_FEATURES & ~mask;
796 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
799 /* Read device(host) feature bits */
800 host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
801 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
805 * Negotiate features: Subset of device feature bits are written back
806 * guest feature bits.
808 hw->guest_features = vtpci_negotiate_features(hw, host_features);
809 PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
813 #ifdef RTE_EXEC_ENV_LINUXAPP
815 parse_sysfs_value(const char *filename, unsigned long *val)
821 f = fopen(filename, "r");
823 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
828 if (fgets(buf, sizeof(buf), f) == NULL) {
829 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
834 *val = strtoul(buf, &end, 0);
835 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
836 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
845 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen,
846 unsigned int *uio_num)
850 char dirname[PATH_MAX];
852 /* depending on kernel version, uio can be located in uio/uioX
854 snprintf(dirname, sizeof(dirname),
855 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
856 loc->domain, loc->bus, loc->devid, loc->function);
857 dir = opendir(dirname);
859 /* retry with the parent directory */
860 snprintf(dirname, sizeof(dirname),
861 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
862 loc->domain, loc->bus, loc->devid, loc->function);
863 dir = opendir(dirname);
866 PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
871 /* take the first file starting with "uio" */
872 while ((e = readdir(dir)) != NULL) {
873 /* format could be uio%d ...*/
874 int shortprefix_len = sizeof("uio") - 1;
875 /* ... or uio:uio%d */
876 int longprefix_len = sizeof("uio:uio") - 1;
879 if (strncmp(e->d_name, "uio", 3) != 0)
882 /* first try uio%d */
884 *uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
885 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
886 snprintf(buf, buflen, "%s/uio%u", dirname, *uio_num);
890 /* then try uio:uio%d */
892 *uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
893 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
894 snprintf(buf, buflen, "%s/uio:uio%u", dirname,
901 /* No uio resource found */
903 PMD_INIT_LOG(ERR, "Could not find uio resource");
911 virtio_has_msix(const struct rte_pci_addr *loc)
914 char dirname[PATH_MAX];
916 snprintf(dirname, sizeof(dirname),
917 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
918 loc->domain, loc->bus, loc->devid, loc->function);
920 d = opendir(dirname);
927 /* Extract I/O port numbers from sysfs */
928 static int virtio_resource_init_by_uio(struct rte_pci_device *pci_dev)
930 char dirname[PATH_MAX];
931 char filename[PATH_MAX];
932 unsigned long start, size;
933 unsigned int uio_num;
935 if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname), &uio_num) < 0)
938 /* get portio size */
939 snprintf(filename, sizeof(filename),
940 "%s/portio/port0/size", dirname);
941 if (parse_sysfs_value(filename, &size) < 0) {
942 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
947 /* get portio start */
948 snprintf(filename, sizeof(filename),
949 "%s/portio/port0/start", dirname);
950 if (parse_sysfs_value(filename, &start) < 0) {
951 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
955 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
956 pci_dev->mem_resource[0].len = (uint64_t)size;
958 "PCI Port IO found start=0x%lx with size=0x%lx",
962 memset(dirname, 0, sizeof(dirname));
963 snprintf(dirname, sizeof(dirname), "/dev/uio%u", uio_num);
964 pci_dev->intr_handle.fd = open(dirname, O_RDWR);
965 if (pci_dev->intr_handle.fd < 0) {
966 PMD_INIT_LOG(ERR, "Cannot open %s: %s\n",
967 dirname, strerror(errno));
971 pci_dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
972 pci_dev->driver->drv_flags |= RTE_PCI_DRV_INTR_LSC;
977 /* Extract port I/O numbers from proc/ioports */
978 static int virtio_resource_init_by_ioports(struct rte_pci_device *pci_dev)
988 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
989 pci_dev->addr.domain,
992 pci_dev->addr.function);
994 fp = fopen("/proc/ioports", "r");
996 PMD_INIT_LOG(ERR, "%s(): can't open ioports", __func__);
1000 while (getdelim(&line, &linesz, '\n', fp) > 0) {
1005 n = strcspn(ptr, ":");
1009 while (*left && isspace(*left))
1012 if (!strncmp(left, pci_id, strlen(pci_id))) {
1015 while (*ptr && isspace(*ptr))
1018 sscanf(ptr, "%04hx-%04hx", &start, &end);
1019 size = end - start + 1;
1031 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)(uint32_t)start;
1032 pci_dev->mem_resource[0].len = (uint64_t)size;
1034 "PCI Port IO found start=0x%x with size=0x%x",
1037 /* can't support lsc interrupt without uio */
1038 pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1043 /* Extract I/O port numbers from sysfs */
1044 static int virtio_resource_init(struct rte_pci_device *pci_dev)
1046 if (virtio_resource_init_by_uio(pci_dev) == 0)
1049 return virtio_resource_init_by_ioports(pci_dev);
1054 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
1056 /* nic_uio does not enable interrupts, return 0 (false). */
1060 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
1062 /* no setup required */
1068 * Process Virtio Config changed interrupt and call the callback
1069 * if link state changed.
1072 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1075 struct rte_eth_dev *dev = param;
1076 struct virtio_hw *hw = dev->data->dev_private;
1079 /* Read interrupt status which clears interrupt */
1080 isr = vtpci_isr(hw);
1081 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1083 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1084 PMD_DRV_LOG(ERR, "interrupt enable failed");
1086 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1087 if (virtio_dev_link_update(dev, 0) == 0)
1088 _rte_eth_dev_callback_process(dev,
1089 RTE_ETH_EVENT_INTR_LSC);
1095 rx_func_get(struct rte_eth_dev *eth_dev)
1097 struct virtio_hw *hw = eth_dev->data->dev_private;
1098 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1099 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1101 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1105 * This function is based on probe() function in virtio_pci.c
1106 * It returns 0 on success.
1109 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1111 struct virtio_hw *hw = eth_dev->data->dev_private;
1112 struct virtio_net_config *config;
1113 struct virtio_net_config local_config;
1114 uint32_t offset_conf = sizeof(config->mac);
1115 struct rte_pci_device *pci_dev;
1117 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
1119 eth_dev->dev_ops = &virtio_eth_dev_ops;
1120 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1122 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1123 rx_func_get(eth_dev);
1127 /* Allocate memory for storing MAC addresses */
1128 eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
1129 if (eth_dev->data->mac_addrs == NULL) {
1131 "Failed to allocate %d bytes needed to store MAC addresses",
1136 pci_dev = eth_dev->pci_dev;
1137 if (virtio_resource_init(pci_dev) < 0)
1140 hw->use_msix = virtio_has_msix(&pci_dev->addr);
1141 hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
1143 /* Reset the device although not necessary at startup */
1146 /* Tell the host we've noticed this device. */
1147 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1149 /* Tell the host we've known how to drive the device. */
1150 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1151 virtio_negotiate_features(hw);
1153 rx_func_get(eth_dev);
1155 /* Setting up rx_header size for the device */
1156 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1157 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1159 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1161 /* Copy the permanent MAC address to: virtio_hw */
1162 virtio_get_hwaddr(hw);
1163 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1164 ð_dev->data->mac_addrs[0]);
1166 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1167 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1168 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1170 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1171 config = &local_config;
1173 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1174 offset_conf += sizeof(config->status);
1177 "VIRTIO_NET_F_STATUS is not supported");
1181 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1182 offset_conf += sizeof(config->max_virtqueue_pairs);
1185 "VIRTIO_NET_F_MQ is not supported");
1186 config->max_virtqueue_pairs = 1;
1189 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
1192 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1193 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1195 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1196 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1198 virtio_dev_cq_queue_setup(eth_dev,
1199 config->max_virtqueue_pairs * 2,
1202 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1203 config->max_virtqueue_pairs);
1204 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1206 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1207 config->mac[0], config->mac[1],
1208 config->mac[2], config->mac[3],
1209 config->mac[4], config->mac[5]);
1211 hw->max_rx_queues = 1;
1212 hw->max_tx_queues = 1;
1215 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1216 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1218 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1219 hw->max_rx_queues, hw->max_tx_queues);
1220 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1221 eth_dev->data->port_id, pci_dev->id.vendor_id,
1222 pci_dev->id.device_id);
1224 /* Setup interrupt callback */
1225 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1226 rte_intr_callback_register(&pci_dev->intr_handle,
1227 virtio_interrupt_handler, eth_dev);
1229 virtio_dev_cq_start(eth_dev);
1234 static struct eth_driver rte_virtio_pmd = {
1236 .name = "rte_virtio_pmd",
1237 .id_table = pci_id_virtio_map,
1239 .eth_dev_init = eth_virtio_dev_init,
1240 .dev_private_size = sizeof(struct virtio_hw),
1244 * Driver initialization routine.
1245 * Invoked once at EAL init time.
1246 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1247 * Returns 0 on success.
1250 rte_virtio_pmd_init(const char *name __rte_unused,
1251 const char *param __rte_unused)
1253 if (rte_eal_iopl_init() != 0) {
1254 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1258 rte_eth_driver_register(&rte_virtio_pmd);
1263 * Only 1 queue is supported, no queue release related operation
1266 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1271 virtio_dev_tx_queue_release(__rte_unused void *txq)
1276 * Configure virtio device
1277 * It returns 0 on success.
1280 virtio_dev_configure(struct rte_eth_dev *dev)
1282 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1283 struct virtio_hw *hw = dev->data->dev_private;
1284 struct rte_pci_device *pci_dev = dev->pci_dev;
1286 PMD_INIT_LOG(DEBUG, "configure");
1288 if (rxmode->hw_ip_checksum) {
1289 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1293 hw->vlan_strip = rxmode->hw_vlan_strip;
1295 if (rxmode->hw_vlan_filter
1296 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1298 "vlan filtering not available on this host");
1302 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1303 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1304 PMD_DRV_LOG(ERR, "failed to set config vector");
1313 virtio_dev_start(struct rte_eth_dev *dev)
1315 uint16_t nb_queues, i;
1316 struct virtio_hw *hw = dev->data->dev_private;
1317 struct rte_pci_device *pci_dev = dev->pci_dev;
1319 /* check if lsc interrupt feature is enabled */
1320 if ((dev->data->dev_conf.intr_conf.lsc) &&
1321 (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1322 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1323 PMD_DRV_LOG(ERR, "link status not supported by host");
1327 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1328 PMD_DRV_LOG(ERR, "interrupt enable failed");
1333 /* Initialize Link state */
1334 virtio_dev_link_update(dev, 0);
1336 /* On restart after stop do not touch queues */
1340 /* Do final configuration before rx/tx engine starts */
1341 virtio_dev_rxtx_start(dev);
1342 vtpci_reinit_complete(hw);
1346 /*Notify the backend
1347 *Otherwise the tap backend might already stop its queue due to fullness.
1348 *vhost backend will have no chance to be waked up
1350 nb_queues = dev->data->nb_rx_queues;
1351 if (nb_queues > 1) {
1352 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1356 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1358 for (i = 0; i < nb_queues; i++)
1359 virtqueue_notify(dev->data->rx_queues[i]);
1361 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1363 for (i = 0; i < dev->data->nb_rx_queues; i++)
1364 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1366 for (i = 0; i < dev->data->nb_tx_queues; i++)
1367 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1372 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1374 struct rte_mbuf *buf;
1375 int i, mbuf_num = 0;
1377 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1379 "Before freeing rxq[%d] used and unused buf", i);
1380 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1382 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1383 dev->data->rx_queues[i])) != NULL) {
1384 rte_pktmbuf_free(buf);
1388 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1390 "After freeing rxq[%d] used and unused buf", i);
1391 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1394 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1396 "Before freeing txq[%d] used and unused bufs",
1398 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1401 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1402 dev->data->tx_queues[i])) != NULL) {
1403 rte_pktmbuf_free(buf);
1408 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1410 "After freeing txq[%d] used and unused buf", i);
1411 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1416 * Stop device: disable interrupt and mark link down
1419 virtio_dev_stop(struct rte_eth_dev *dev)
1421 struct rte_eth_link link;
1423 PMD_INIT_LOG(DEBUG, "stop");
1425 if (dev->data->dev_conf.intr_conf.lsc)
1426 rte_intr_disable(&dev->pci_dev->intr_handle);
1428 memset(&link, 0, sizeof(link));
1429 virtio_dev_atomic_write_link_status(dev, &link);
1433 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1435 struct rte_eth_link link, old;
1437 struct virtio_hw *hw = dev->data->dev_private;
1438 memset(&link, 0, sizeof(link));
1439 virtio_dev_atomic_read_link_status(dev, &link);
1441 link.link_duplex = FULL_DUPLEX;
1442 link.link_speed = SPEED_10G;
1444 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1445 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1446 vtpci_read_dev_config(hw,
1447 offsetof(struct virtio_net_config, status),
1448 &status, sizeof(status));
1449 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1450 link.link_status = 0;
1451 PMD_INIT_LOG(DEBUG, "Port %d is down",
1452 dev->data->port_id);
1454 link.link_status = 1;
1455 PMD_INIT_LOG(DEBUG, "Port %d is up",
1456 dev->data->port_id);
1459 link.link_status = 1; /* Link up */
1461 virtio_dev_atomic_write_link_status(dev, &link);
1463 return (old.link_status == link.link_status) ? -1 : 0;
1467 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1469 struct virtio_hw *hw = dev->data->dev_private;
1471 dev_info->driver_name = dev->driver->pci_drv.name;
1472 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1473 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1474 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1475 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1476 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1477 dev_info->default_txconf = (struct rte_eth_txconf) {
1478 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1483 * It enables testpmd to collect per queue stats.
1486 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1487 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1488 __rte_unused uint8_t is_rx)
1493 static struct rte_driver rte_virtio_driver = {
1495 .init = rte_virtio_pmd_init,
1498 PMD_REGISTER_DRIVER(rte_virtio_driver);