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
43 #include <rte_ethdev.h>
44 #include <rte_memcpy.h>
45 #include <rte_string_fns.h>
46 #include <rte_memzone.h>
47 #include <rte_malloc.h>
48 #include <rte_atomic.h>
49 #include <rte_branch_prediction.h>
51 #include <rte_ether.h>
52 #include <rte_common.h>
54 #include <rte_memory.h>
58 #include "virtio_ethdev.h"
59 #include "virtio_pci.h"
60 #include "virtio_logs.h"
61 #include "virtqueue.h"
64 static int eth_virtio_dev_init(struct eth_driver *eth_drv,
65 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);
94 static int virtio_dev_queue_stats_mapping_set(
95 __rte_unused struct rte_eth_dev *eth_dev,
96 __rte_unused uint16_t queue_id,
97 __rte_unused uint8_t stat_idx,
98 __rte_unused uint8_t is_rx);
101 * The set of PCI devices this driver supports
103 static struct rte_pci_id pci_id_virtio_map[] = {
105 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
106 #include "rte_pci_dev_ids.h"
108 { .vendor_id = 0, /* sentinel */ },
112 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
113 int *dlen, int pkt_num)
115 uint32_t head = vq->vq_desc_head_idx, i;
117 virtio_net_ctrl_ack status = ~0;
118 struct virtio_pmd_ctrl result;
120 ctrl->status = status;
124 "%s(): Control queue is not supported.",
129 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
130 "vq->hw->cvq = %p vq = %p",
131 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
133 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
136 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
137 sizeof(struct virtio_pmd_ctrl));
140 * Format is enforced in qemu code:
141 * One TX packet for header;
142 * At least one TX packet per argument;
143 * One RX packet for ACK.
145 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
146 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
147 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
149 i = vq->vq_ring.desc[head].next;
151 for (k = 0; k < pkt_num; k++) {
152 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
153 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
154 + sizeof(struct virtio_net_ctrl_hdr)
155 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
156 vq->vq_ring.desc[i].len = dlen[k];
159 i = vq->vq_ring.desc[i].next;
162 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
163 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
164 + sizeof(struct virtio_net_ctrl_hdr);
165 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
168 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
170 vq_update_avail_ring(vq, head);
171 vq_update_avail_idx(vq);
173 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
175 virtqueue_notify(vq);
177 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
180 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
181 uint32_t idx, desc_idx, used_idx;
182 struct vring_used_elem *uep;
186 used_idx = (uint32_t)(vq->vq_used_cons_idx
187 & (vq->vq_nentries - 1));
188 uep = &vq->vq_ring.used->ring[used_idx];
189 idx = (uint32_t) uep->id;
192 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
193 desc_idx = vq->vq_ring.desc[desc_idx].next;
197 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
198 vq->vq_desc_head_idx = idx;
200 vq->vq_used_cons_idx++;
204 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
205 vq->vq_free_cnt, vq->vq_desc_head_idx);
207 memcpy(&result, vq->virtio_net_hdr_mz->addr,
208 sizeof(struct virtio_pmd_ctrl));
210 return result.status;
214 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
216 struct virtio_hw *hw = dev->data->dev_private;
217 struct virtio_pmd_ctrl ctrl;
221 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
222 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
223 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
225 dlen[0] = sizeof(uint16_t);
227 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
230 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
231 "failed, this is too late now...");
238 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
241 uint8_t vtpci_queue_idx,
243 unsigned int socket_id,
244 struct virtqueue **pvq)
246 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
247 const struct rte_memzone *mz;
250 struct virtio_hw *hw = dev->data->dev_private;
251 struct virtqueue *vq = NULL;
253 /* Write the virtqueue index to the Queue Select Field */
254 VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
255 PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
258 * Read the virtqueue size from the Queue Size field
259 * Always power of 2 and if 0 virtqueue does not exist
261 vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
262 PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
266 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
268 } else if (!rte_is_power_of_2(vq_size)) {
269 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
271 } else if (nb_desc != vq_size) {
272 PMD_INIT_LOG(ERR, "Warning: nb_desc(%d) is not equal to vq size (%d), fall to vq size",
277 if (queue_type == VTNET_RQ) {
278 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
279 dev->data->port_id, queue_idx);
280 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
281 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
282 } else if (queue_type == VTNET_TQ) {
283 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
284 dev->data->port_id, queue_idx);
285 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
286 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
287 } else if (queue_type == VTNET_CQ) {
288 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
290 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
291 vq_size * sizeof(struct vq_desc_extra),
292 RTE_CACHE_LINE_SIZE);
295 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
300 vq->port_id = dev->data->port_id;
301 vq->queue_id = queue_idx;
302 vq->vq_queue_index = vtpci_queue_idx;
303 vq->vq_nentries = vq_size;
304 vq->vq_free_cnt = vq_size;
307 * Reserve a memzone for vring elements
309 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
310 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
311 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
313 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
314 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
321 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
322 * and only accepts 32 bit page frame number.
323 * Check if the allocated physical memory exceeds 16TB.
325 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
326 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
331 memset(mz->addr, 0, sizeof(mz->len));
333 vq->vq_ring_mem = mz->phys_addr;
334 vq->vq_ring_virt_mem = mz->addr;
335 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr);
336 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)mz->addr);
337 vq->virtio_net_hdr_mz = NULL;
338 vq->virtio_net_hdr_mem = 0;
340 if (queue_type == VTNET_TQ) {
342 * For each xmit packet, allocate a virtio_net_hdr
344 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
345 dev->data->port_id, queue_idx);
346 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
347 vq_size * hw->vtnet_hdr_size,
348 socket_id, 0, RTE_CACHE_LINE_SIZE);
349 if (vq->virtio_net_hdr_mz == NULL) {
353 vq->virtio_net_hdr_mem =
354 vq->virtio_net_hdr_mz->phys_addr;
355 memset(vq->virtio_net_hdr_mz->addr, 0,
356 vq_size * hw->vtnet_hdr_size);
357 } else if (queue_type == VTNET_CQ) {
358 /* Allocate a page for control vq command, data and status */
359 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
361 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
362 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
363 if (vq->virtio_net_hdr_mz == NULL) {
367 vq->virtio_net_hdr_mem =
368 vq->virtio_net_hdr_mz->phys_addr;
369 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
373 * Set guest physical address of the virtqueue
374 * in VIRTIO_PCI_QUEUE_PFN config register of device
376 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
377 mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
383 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
386 struct virtqueue *vq;
387 uint16_t nb_desc = 0;
389 struct virtio_hw *hw = dev->data->dev_private;
391 PMD_INIT_FUNC_TRACE();
392 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
393 vtpci_queue_idx, nb_desc, socket_id, &vq);
396 PMD_INIT_LOG(ERR, "control vq initialization failed");
405 virtio_dev_close(struct rte_eth_dev *dev)
407 struct virtio_hw *hw = dev->data->dev_private;
409 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
412 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
415 virtio_dev_free_mbufs(dev);
419 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
421 struct virtio_hw *hw = dev->data->dev_private;
422 struct virtio_pmd_ctrl ctrl;
426 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
427 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
431 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
434 PMD_INIT_LOG(ERR, "Failed to enable promisc");
438 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
440 struct virtio_hw *hw = dev->data->dev_private;
441 struct virtio_pmd_ctrl ctrl;
445 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
446 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
450 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
453 PMD_INIT_LOG(ERR, "Failed to disable promisc");
457 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
459 struct virtio_hw *hw = dev->data->dev_private;
460 struct virtio_pmd_ctrl ctrl;
464 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
465 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
469 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);
491 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
495 * dev_ops for virtio, bare necessities for basic operation
497 static struct eth_dev_ops virtio_eth_dev_ops = {
498 .dev_configure = virtio_dev_configure,
499 .dev_start = virtio_dev_start,
500 .dev_stop = virtio_dev_stop,
501 .dev_close = virtio_dev_close,
502 .promiscuous_enable = virtio_dev_promiscuous_enable,
503 .promiscuous_disable = virtio_dev_promiscuous_disable,
504 .allmulticast_enable = virtio_dev_allmulticast_enable,
505 .allmulticast_disable = virtio_dev_allmulticast_disable,
507 .dev_infos_get = virtio_dev_info_get,
508 .stats_get = virtio_dev_stats_get,
509 .stats_reset = virtio_dev_stats_reset,
510 .link_update = virtio_dev_link_update,
511 .rx_queue_setup = virtio_dev_rx_queue_setup,
512 /* meaningfull only to multiple queue */
513 .rx_queue_release = virtio_dev_rx_queue_release,
514 .tx_queue_setup = virtio_dev_tx_queue_setup,
515 /* meaningfull only to multiple queue */
516 .tx_queue_release = virtio_dev_tx_queue_release,
517 /* collect stats per queue */
518 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
519 .vlan_filter_set = virtio_vlan_filter_set,
520 .mac_addr_add = virtio_mac_addr_add,
521 .mac_addr_remove = virtio_mac_addr_remove,
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_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
740 struct virtio_hw *hw = dev->data->dev_private;
741 struct virtio_pmd_ctrl ctrl;
744 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
747 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
748 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
749 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
750 len = sizeof(vlan_id);
752 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
756 virtio_negotiate_features(struct virtio_hw *hw)
758 uint32_t host_features, mask;
760 /* checksum offload not implemented */
761 mask = VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
763 /* TSO and LRO are only available when their corresponding
764 * checksum offload feature is also negotiated.
766 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN;
767 mask |= VIRTIO_NET_F_GUEST_TSO4 | VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN;
768 mask |= VTNET_LRO_FEATURES;
770 /* not negotiating INDIRECT descriptor table support */
771 mask |= VIRTIO_RING_F_INDIRECT_DESC;
773 /* Prepare guest_features: feature that driver wants to support */
774 hw->guest_features = VTNET_FEATURES & ~mask;
775 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
778 /* Read device(host) feature bits */
779 host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
780 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
784 * Negotiate features: Subset of device feature bits are written back
785 * guest feature bits.
787 hw->guest_features = vtpci_negotiate_features(hw, host_features);
788 PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
792 #ifdef RTE_EXEC_ENV_LINUXAPP
794 parse_sysfs_value(const char *filename, unsigned long *val)
800 f = fopen(filename, "r");
802 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
807 if (fgets(buf, sizeof(buf), f) == NULL) {
808 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
813 *val = strtoul(buf, &end, 0);
814 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
815 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
824 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen)
826 unsigned int uio_num;
829 char dirname[PATH_MAX];
831 /* depending on kernel version, uio can be located in uio/uioX
833 snprintf(dirname, sizeof(dirname),
834 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
835 loc->domain, loc->bus, loc->devid, loc->function);
836 dir = opendir(dirname);
838 /* retry with the parent directory */
839 snprintf(dirname, sizeof(dirname),
840 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
841 loc->domain, loc->bus, loc->devid, loc->function);
842 dir = opendir(dirname);
845 PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
850 /* take the first file starting with "uio" */
851 while ((e = readdir(dir)) != NULL) {
852 /* format could be uio%d ...*/
853 int shortprefix_len = sizeof("uio") - 1;
854 /* ... or uio:uio%d */
855 int longprefix_len = sizeof("uio:uio") - 1;
858 if (strncmp(e->d_name, "uio", 3) != 0)
861 /* first try uio%d */
863 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
864 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
865 snprintf(buf, buflen, "%s/uio%u", dirname, uio_num);
869 /* then try uio:uio%d */
871 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
872 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
873 snprintf(buf, buflen, "%s/uio:uio%u", dirname,
880 /* No uio resource found */
882 PMD_INIT_LOG(ERR, "Could not find uio resource");
890 virtio_has_msix(const struct rte_pci_addr *loc)
893 char dirname[PATH_MAX];
895 snprintf(dirname, sizeof(dirname),
896 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
897 loc->domain, loc->bus, loc->devid, loc->function);
899 d = opendir(dirname);
906 /* Extract I/O port numbers from sysfs */
907 static int virtio_resource_init(struct rte_pci_device *pci_dev)
909 char dirname[PATH_MAX];
910 char filename[PATH_MAX];
911 unsigned long start, size;
913 if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname)) < 0)
916 /* get portio size */
917 snprintf(filename, sizeof(filename),
918 "%s/portio/port0/size", dirname);
919 if (parse_sysfs_value(filename, &size) < 0) {
920 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
925 /* get portio start */
926 snprintf(filename, sizeof(filename),
927 "%s/portio/port0/start", dirname);
928 if (parse_sysfs_value(filename, &start) < 0) {
929 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
933 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
934 pci_dev->mem_resource[0].len = (uint64_t)size;
936 "PCI Port IO found start=0x%lx with size=0x%lx",
942 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
944 /* nic_uio does not enable interrupts, return 0 (false). */
948 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
950 /* no setup required */
956 * Process Virtio Config changed interrupt and call the callback
957 * if link state changed.
960 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
963 struct rte_eth_dev *dev = param;
964 struct virtio_hw *hw = dev->data->dev_private;
967 /* Read interrupt status which clears interrupt */
969 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
971 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
972 PMD_DRV_LOG(ERR, "interrupt enable failed");
974 if (isr & VIRTIO_PCI_ISR_CONFIG) {
975 if (virtio_dev_link_update(dev, 0) == 0)
976 _rte_eth_dev_callback_process(dev,
977 RTE_ETH_EVENT_INTR_LSC);
983 * This function is based on probe() function in virtio_pci.c
984 * It returns 0 on success.
987 eth_virtio_dev_init(__rte_unused struct eth_driver *eth_drv,
988 struct rte_eth_dev *eth_dev)
990 struct virtio_hw *hw = eth_dev->data->dev_private;
991 struct virtio_net_config *config;
992 struct virtio_net_config local_config;
993 uint32_t offset_conf = sizeof(config->mac);
994 struct rte_pci_device *pci_dev;
996 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
998 eth_dev->dev_ops = &virtio_eth_dev_ops;
999 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1001 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1004 /* Allocate memory for storing MAC addresses */
1005 eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
1006 if (eth_dev->data->mac_addrs == NULL) {
1008 "Failed to allocate %d bytes needed to store MAC addresses",
1013 pci_dev = eth_dev->pci_dev;
1014 if (virtio_resource_init(pci_dev) < 0)
1017 hw->use_msix = virtio_has_msix(&pci_dev->addr);
1018 hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
1020 /* Reset the device although not necessary at startup */
1023 /* Tell the host we've noticed this device. */
1024 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1026 /* Tell the host we've known how to drive the device. */
1027 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1028 virtio_negotiate_features(hw);
1030 /* Setting up rx_header size for the device */
1031 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1032 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1033 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1035 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1036 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1039 /* Copy the permanent MAC address to: virtio_hw */
1040 virtio_get_hwaddr(hw);
1041 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1042 ð_dev->data->mac_addrs[0]);
1044 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1045 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1046 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1048 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1049 config = &local_config;
1051 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1052 offset_conf += sizeof(config->status);
1055 "VIRTIO_NET_F_STATUS is not supported");
1059 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1060 offset_conf += sizeof(config->max_virtqueue_pairs);
1063 "VIRTIO_NET_F_MQ is not supported");
1064 config->max_virtqueue_pairs = 1;
1067 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
1070 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1071 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1073 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1074 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1076 virtio_dev_cq_queue_setup(eth_dev,
1077 config->max_virtqueue_pairs * 2,
1080 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1081 config->max_virtqueue_pairs);
1082 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1084 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1085 config->mac[0], config->mac[1],
1086 config->mac[2], config->mac[3],
1087 config->mac[4], config->mac[5]);
1089 hw->max_rx_queues = 1;
1090 hw->max_tx_queues = 1;
1093 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1094 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1096 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1097 hw->max_rx_queues, hw->max_tx_queues);
1098 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1099 eth_dev->data->port_id, pci_dev->id.vendor_id,
1100 pci_dev->id.device_id);
1102 /* Setup interrupt callback */
1103 rte_intr_callback_register(&pci_dev->intr_handle,
1104 virtio_interrupt_handler, eth_dev);
1106 virtio_dev_cq_start(eth_dev);
1111 static struct eth_driver rte_virtio_pmd = {
1113 .name = "rte_virtio_pmd",
1114 .id_table = pci_id_virtio_map,
1115 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1117 .eth_dev_init = eth_virtio_dev_init,
1118 .dev_private_size = sizeof(struct virtio_hw),
1122 * Driver initialization routine.
1123 * Invoked once at EAL init time.
1124 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1125 * Returns 0 on success.
1128 rte_virtio_pmd_init(const char *name __rte_unused,
1129 const char *param __rte_unused)
1131 if (rte_eal_iopl_init() != 0) {
1132 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1136 rte_eth_driver_register(&rte_virtio_pmd);
1141 * Only 1 queue is supported, no queue release related operation
1144 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1149 virtio_dev_tx_queue_release(__rte_unused void *txq)
1154 * Configure virtio device
1155 * It returns 0 on success.
1158 virtio_dev_configure(struct rte_eth_dev *dev)
1160 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1161 struct virtio_hw *hw = dev->data->dev_private;
1163 PMD_INIT_LOG(DEBUG, "configure");
1165 if (rxmode->hw_ip_checksum) {
1166 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1170 hw->vlan_strip = rxmode->hw_vlan_strip;
1172 if (rxmode->hw_vlan_filter
1173 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1175 "vlan filtering not available on this host");
1179 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1180 PMD_DRV_LOG(ERR, "failed to set config vector");
1189 virtio_dev_start(struct rte_eth_dev *dev)
1191 uint16_t nb_queues, i;
1192 struct virtio_hw *hw = dev->data->dev_private;
1194 /* check if lsc interrupt feature is enabled */
1195 if (dev->data->dev_conf.intr_conf.lsc) {
1196 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1197 PMD_DRV_LOG(ERR, "link status not supported by host");
1201 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1202 PMD_DRV_LOG(ERR, "interrupt enable failed");
1207 /* Initialize Link state */
1208 virtio_dev_link_update(dev, 0);
1210 /* On restart after stop do not touch queues */
1214 /* Do final configuration before rx/tx engine starts */
1215 virtio_dev_rxtx_start(dev);
1216 vtpci_reinit_complete(hw);
1220 /*Notify the backend
1221 *Otherwise the tap backend might already stop its queue due to fullness.
1222 *vhost backend will have no chance to be waked up
1224 nb_queues = dev->data->nb_rx_queues;
1225 if (nb_queues > 1) {
1226 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1230 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1232 for (i = 0; i < nb_queues; i++)
1233 virtqueue_notify(dev->data->rx_queues[i]);
1235 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1237 for (i = 0; i < dev->data->nb_rx_queues; i++)
1238 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1240 for (i = 0; i < dev->data->nb_tx_queues; i++)
1241 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1246 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1248 struct rte_mbuf *buf;
1249 int i, mbuf_num = 0;
1251 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1253 "Before freeing rxq[%d] used and unused buf", i);
1254 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1256 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1257 dev->data->rx_queues[i])) != NULL) {
1258 rte_pktmbuf_free(buf);
1262 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1264 "After freeing rxq[%d] used and unused buf", i);
1265 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1268 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1270 "Before freeing txq[%d] used and unused bufs",
1272 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1275 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1276 dev->data->tx_queues[i])) != NULL) {
1277 rte_pktmbuf_free(buf);
1282 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1284 "After freeing txq[%d] used and unused buf", i);
1285 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1290 * Stop device: disable interrupt and mark link down
1293 virtio_dev_stop(struct rte_eth_dev *dev)
1295 struct rte_eth_link link;
1297 PMD_INIT_LOG(DEBUG, "stop");
1299 if (dev->data->dev_conf.intr_conf.lsc)
1300 rte_intr_disable(&dev->pci_dev->intr_handle);
1302 memset(&link, 0, sizeof(link));
1303 virtio_dev_atomic_write_link_status(dev, &link);
1307 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1309 struct rte_eth_link link, old;
1311 struct virtio_hw *hw = dev->data->dev_private;
1312 memset(&link, 0, sizeof(link));
1313 virtio_dev_atomic_read_link_status(dev, &link);
1315 link.link_duplex = FULL_DUPLEX;
1316 link.link_speed = SPEED_10G;
1318 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1319 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1320 vtpci_read_dev_config(hw,
1321 offsetof(struct virtio_net_config, status),
1322 &status, sizeof(status));
1323 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1324 link.link_status = 0;
1325 PMD_INIT_LOG(DEBUG, "Port %d is down",
1326 dev->data->port_id);
1328 link.link_status = 1;
1329 PMD_INIT_LOG(DEBUG, "Port %d is up",
1330 dev->data->port_id);
1333 link.link_status = 1; /* Link up */
1335 virtio_dev_atomic_write_link_status(dev, &link);
1337 return (old.link_status == link.link_status) ? -1 : 0;
1341 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1343 struct virtio_hw *hw = dev->data->dev_private;
1345 dev_info->driver_name = dev->driver->pci_drv.name;
1346 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1347 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1348 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1349 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1350 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1354 * It enables testpmd to collect per queue stats.
1357 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1358 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1359 __rte_unused uint8_t is_rx)
1364 static struct rte_driver rte_virtio_driver = {
1366 .init = rte_virtio_pmd_init,
1369 PMD_REGISTER_DRIVER(rte_virtio_driver);