1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <ethdev_driver.h>
12 #include <rte_memcpy.h>
13 #include <rte_string_fns.h>
14 #include <rte_memzone.h>
15 #include <rte_malloc.h>
16 #include <rte_branch_prediction.h>
17 #include <rte_ether.h>
20 #include <rte_common.h>
21 #include <rte_errno.h>
22 #include <rte_cpuflags.h>
24 #include <rte_memory.h>
25 #include <rte_eal_paging.h>
28 #include <rte_cycles.h>
29 #include <rte_kvargs.h>
31 #include "virtio_ethdev.h"
33 #include "virtio_logs.h"
34 #include "virtqueue.h"
35 #include "virtio_rxtx.h"
36 #include "virtio_rxtx_simple.h"
37 #include "virtio_user/virtio_user_dev.h"
39 static int virtio_dev_configure(struct rte_eth_dev *dev);
40 static int virtio_dev_start(struct rte_eth_dev *dev);
41 static int virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
42 static int virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
43 static int virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
44 static int virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
45 static uint32_t virtio_dev_speed_capa_get(uint32_t speed);
46 static int virtio_dev_devargs_parse(struct rte_devargs *devargs,
49 static int virtio_dev_info_get(struct rte_eth_dev *dev,
50 struct rte_eth_dev_info *dev_info);
51 static int virtio_dev_link_update(struct rte_eth_dev *dev,
52 int wait_to_complete);
53 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
55 static void virtio_set_hwaddr(struct virtio_hw *hw);
56 static void virtio_get_hwaddr(struct virtio_hw *hw);
58 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
59 struct rte_eth_stats *stats);
60 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
61 struct rte_eth_xstat *xstats, unsigned n);
62 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
63 struct rte_eth_xstat_name *xstats_names,
65 static int virtio_dev_stats_reset(struct rte_eth_dev *dev);
66 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
67 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
68 uint16_t vlan_id, int on);
69 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
70 struct rte_ether_addr *mac_addr,
71 uint32_t index, uint32_t vmdq);
72 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
73 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
74 struct rte_ether_addr *mac_addr);
76 static int virtio_intr_disable(struct rte_eth_dev *dev);
78 static int virtio_dev_queue_stats_mapping_set(
79 struct rte_eth_dev *eth_dev,
84 static void virtio_notify_peers(struct rte_eth_dev *dev);
85 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
87 struct rte_virtio_xstats_name_off {
88 char name[RTE_ETH_XSTATS_NAME_SIZE];
92 /* [rt]x_qX_ is prepended to the name string here */
93 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
94 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
95 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
96 {"errors", offsetof(struct virtnet_rx, stats.errors)},
97 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
98 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
99 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
100 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
101 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
102 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
103 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
104 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
105 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
106 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
109 /* [rt]x_qX_ is prepended to the name string here */
110 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
111 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
112 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
113 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
114 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
115 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
116 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
117 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
118 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
119 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
120 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
121 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
122 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
125 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
126 sizeof(rte_virtio_rxq_stat_strings[0]))
127 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
128 sizeof(rte_virtio_txq_stat_strings[0]))
130 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
132 static struct virtio_pmd_ctrl *
133 virtio_send_command_packed(struct virtnet_ctl *cvq,
134 struct virtio_pmd_ctrl *ctrl,
135 int *dlen, int pkt_num)
137 struct virtqueue *vq = virtnet_cq_to_vq(cvq);
139 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
140 struct virtio_pmd_ctrl *result;
147 * Format is enforced in qemu code:
148 * One TX packet for header;
149 * At least one TX packet per argument;
150 * One RX packet for ACK.
152 head = vq->vq_avail_idx;
153 flags = vq->vq_packed.cached_flags;
154 desc[head].addr = cvq->virtio_net_hdr_mem;
155 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
158 if (++vq->vq_avail_idx >= vq->vq_nentries) {
159 vq->vq_avail_idx -= vq->vq_nentries;
160 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
163 for (k = 0; k < pkt_num; k++) {
164 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
165 + sizeof(struct virtio_net_ctrl_hdr)
166 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
167 desc[vq->vq_avail_idx].len = dlen[k];
168 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
169 vq->vq_packed.cached_flags;
173 if (++vq->vq_avail_idx >= vq->vq_nentries) {
174 vq->vq_avail_idx -= vq->vq_nentries;
175 vq->vq_packed.cached_flags ^=
176 VRING_PACKED_DESC_F_AVAIL_USED;
180 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
181 + sizeof(struct virtio_net_ctrl_hdr);
182 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
183 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
184 vq->vq_packed.cached_flags;
187 if (++vq->vq_avail_idx >= vq->vq_nentries) {
188 vq->vq_avail_idx -= vq->vq_nentries;
189 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
192 virtqueue_store_flags_packed(&desc[head], VRING_DESC_F_NEXT | flags,
193 vq->hw->weak_barriers);
195 virtio_wmb(vq->hw->weak_barriers);
196 virtqueue_notify(vq);
198 /* wait for used desc in virtqueue
199 * desc_is_used has a load-acquire or rte_io_rmb inside
201 while (!desc_is_used(&desc[head], vq))
204 /* now get used descriptors */
205 vq->vq_free_cnt += nb_descs;
206 vq->vq_used_cons_idx += nb_descs;
207 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
208 vq->vq_used_cons_idx -= vq->vq_nentries;
209 vq->vq_packed.used_wrap_counter ^= 1;
212 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
213 "vq->vq_avail_idx=%d\n"
214 "vq->vq_used_cons_idx=%d\n"
215 "vq->vq_packed.cached_flags=0x%x\n"
216 "vq->vq_packed.used_wrap_counter=%d\n",
219 vq->vq_used_cons_idx,
220 vq->vq_packed.cached_flags,
221 vq->vq_packed.used_wrap_counter);
223 result = cvq->virtio_net_hdr_mz->addr;
227 static struct virtio_pmd_ctrl *
228 virtio_send_command_split(struct virtnet_ctl *cvq,
229 struct virtio_pmd_ctrl *ctrl,
230 int *dlen, int pkt_num)
232 struct virtio_pmd_ctrl *result;
233 struct virtqueue *vq = virtnet_cq_to_vq(cvq);
237 head = vq->vq_desc_head_idx;
240 * Format is enforced in qemu code:
241 * One TX packet for header;
242 * At least one TX packet per argument;
243 * One RX packet for ACK.
245 vq->vq_split.ring.desc[head].flags = VRING_DESC_F_NEXT;
246 vq->vq_split.ring.desc[head].addr = cvq->virtio_net_hdr_mem;
247 vq->vq_split.ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
249 i = vq->vq_split.ring.desc[head].next;
251 for (k = 0; k < pkt_num; k++) {
252 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_NEXT;
253 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
254 + sizeof(struct virtio_net_ctrl_hdr)
255 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
256 vq->vq_split.ring.desc[i].len = dlen[k];
259 i = vq->vq_split.ring.desc[i].next;
262 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_WRITE;
263 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
264 + sizeof(struct virtio_net_ctrl_hdr);
265 vq->vq_split.ring.desc[i].len = sizeof(ctrl->status);
268 vq->vq_desc_head_idx = vq->vq_split.ring.desc[i].next;
270 vq_update_avail_ring(vq, head);
271 vq_update_avail_idx(vq);
273 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
275 virtqueue_notify(vq);
277 while (virtqueue_nused(vq) == 0)
280 while (virtqueue_nused(vq)) {
281 uint32_t idx, desc_idx, used_idx;
282 struct vring_used_elem *uep;
284 used_idx = (uint32_t)(vq->vq_used_cons_idx
285 & (vq->vq_nentries - 1));
286 uep = &vq->vq_split.ring.used->ring[used_idx];
287 idx = (uint32_t) uep->id;
290 while (vq->vq_split.ring.desc[desc_idx].flags &
292 desc_idx = vq->vq_split.ring.desc[desc_idx].next;
296 vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
297 vq->vq_desc_head_idx = idx;
299 vq->vq_used_cons_idx++;
303 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
304 vq->vq_free_cnt, vq->vq_desc_head_idx);
306 result = cvq->virtio_net_hdr_mz->addr;
311 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
312 int *dlen, int pkt_num)
314 virtio_net_ctrl_ack status = ~0;
315 struct virtio_pmd_ctrl *result;
316 struct virtqueue *vq;
318 ctrl->status = status;
321 PMD_INIT_LOG(ERR, "Control queue is not supported.");
325 rte_spinlock_lock(&cvq->lock);
326 vq = virtnet_cq_to_vq(cvq);
328 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
329 "vq->hw->cvq = %p vq = %p",
330 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
332 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
333 rte_spinlock_unlock(&cvq->lock);
337 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
338 sizeof(struct virtio_pmd_ctrl));
340 if (virtio_with_packed_queue(vq->hw))
341 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
343 result = virtio_send_command_split(cvq, ctrl, dlen, pkt_num);
345 rte_spinlock_unlock(&cvq->lock);
346 return result->status;
350 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
352 struct virtio_hw *hw = dev->data->dev_private;
353 struct virtio_pmd_ctrl ctrl;
357 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
358 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
359 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
361 dlen[0] = sizeof(uint16_t);
363 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
365 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
366 "failed, this is too late now...");
374 virtio_dev_queue_release(void *queue __rte_unused)
380 virtio_get_nr_vq(struct virtio_hw *hw)
382 uint16_t nr_vq = hw->max_queue_pairs * 2;
384 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
391 virtio_init_vring(struct virtqueue *vq)
393 int size = vq->vq_nentries;
394 uint8_t *ring_mem = vq->vq_ring_virt_mem;
396 PMD_INIT_FUNC_TRACE();
398 memset(ring_mem, 0, vq->vq_ring_size);
400 vq->vq_used_cons_idx = 0;
401 vq->vq_desc_head_idx = 0;
402 vq->vq_avail_idx = 0;
403 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
404 vq->vq_free_cnt = vq->vq_nentries;
405 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
406 if (virtio_with_packed_queue(vq->hw)) {
407 vring_init_packed(&vq->vq_packed.ring, ring_mem,
408 VIRTIO_VRING_ALIGN, size);
409 vring_desc_init_packed(vq, size);
411 struct vring *vr = &vq->vq_split.ring;
413 vring_init_split(vr, ring_mem, VIRTIO_VRING_ALIGN, size);
414 vring_desc_init_split(vr->desc, size);
417 * Disable device(host) interrupting guest
419 virtqueue_disable_intr(vq);
423 virtio_init_queue(struct rte_eth_dev *dev, uint16_t queue_idx)
425 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
426 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
427 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
428 unsigned int vq_size, size;
429 struct virtio_hw *hw = dev->data->dev_private;
430 struct virtnet_rx *rxvq = NULL;
431 struct virtnet_tx *txvq = NULL;
432 struct virtnet_ctl *cvq = NULL;
433 struct virtqueue *vq;
434 size_t sz_hdr_mz = 0;
435 void *sw_ring = NULL;
436 int queue_type = virtio_get_queue_type(hw, queue_idx);
438 int numa_node = dev->device->numa_node;
439 struct rte_mbuf *fake_mbuf = NULL;
441 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
442 queue_idx, numa_node);
445 * Read the virtqueue size from the Queue Size field
446 * Always power of 2 and if 0 virtqueue does not exist
448 vq_size = VIRTIO_OPS(hw)->get_queue_num(hw, queue_idx);
449 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
451 PMD_INIT_LOG(ERR, "virtqueue does not exist");
455 if (!virtio_with_packed_queue(hw) && !rte_is_power_of_2(vq_size)) {
456 PMD_INIT_LOG(ERR, "split virtqueue size is not power of 2");
460 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
461 dev->data->port_id, queue_idx);
463 size = RTE_ALIGN_CEIL(sizeof(*vq) +
464 vq_size * sizeof(struct vq_desc_extra),
465 RTE_CACHE_LINE_SIZE);
466 if (queue_type == VTNET_TQ) {
468 * For each xmit packet, allocate a virtio_net_hdr
469 * and indirect ring elements
471 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
472 } else if (queue_type == VTNET_CQ) {
473 /* Allocate a page for control vq command, data and status */
474 sz_hdr_mz = rte_mem_page_size();
477 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
480 PMD_INIT_LOG(ERR, "can not allocate vq");
483 hw->vqs[queue_idx] = vq;
486 vq->vq_queue_index = queue_idx;
487 vq->vq_nentries = vq_size;
488 if (virtio_with_packed_queue(hw)) {
489 vq->vq_packed.used_wrap_counter = 1;
490 vq->vq_packed.cached_flags = VRING_PACKED_DESC_F_AVAIL;
491 vq->vq_packed.event_flags_shadow = 0;
492 if (queue_type == VTNET_RQ)
493 vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
497 * Reserve a memzone for vring elements
499 size = vring_size(hw, vq_size, VIRTIO_VRING_ALIGN);
500 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_VRING_ALIGN);
501 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
502 size, vq->vq_ring_size);
504 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
505 numa_node, RTE_MEMZONE_IOVA_CONTIG,
508 if (rte_errno == EEXIST)
509 mz = rte_memzone_lookup(vq_name);
516 memset(mz->addr, 0, mz->len);
518 vq->vq_ring_mem = mz->iova;
519 vq->vq_ring_virt_mem = mz->addr;
520 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
522 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
523 (uint64_t)(uintptr_t)mz->addr);
525 virtio_init_vring(vq);
528 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
529 dev->data->port_id, queue_idx);
530 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
531 numa_node, RTE_MEMZONE_IOVA_CONTIG,
532 RTE_CACHE_LINE_SIZE);
533 if (hdr_mz == NULL) {
534 if (rte_errno == EEXIST)
535 hdr_mz = rte_memzone_lookup(vq_hdr_name);
536 if (hdr_mz == NULL) {
543 if (queue_type == VTNET_RQ) {
544 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
545 sizeof(vq->sw_ring[0]);
547 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
548 RTE_CACHE_LINE_SIZE, numa_node);
550 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
555 fake_mbuf = rte_zmalloc_socket("sw_ring", sizeof(*fake_mbuf),
556 RTE_CACHE_LINE_SIZE, numa_node);
558 PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
563 vq->sw_ring = sw_ring;
565 rxvq->port_id = dev->data->port_id;
567 rxvq->fake_mbuf = fake_mbuf;
568 } else if (queue_type == VTNET_TQ) {
570 txvq->port_id = dev->data->port_id;
572 txvq->virtio_net_hdr_mz = hdr_mz;
573 txvq->virtio_net_hdr_mem = hdr_mz->iova;
574 } else if (queue_type == VTNET_CQ) {
577 cvq->virtio_net_hdr_mz = hdr_mz;
578 cvq->virtio_net_hdr_mem = hdr_mz->iova;
579 memset(cvq->virtio_net_hdr_mz->addr, 0, rte_mem_page_size());
584 if (queue_type == VTNET_TQ) {
585 struct virtio_tx_region *txr;
589 memset(txr, 0, vq_size * sizeof(*txr));
590 for (i = 0; i < vq_size; i++) {
591 /* first indirect descriptor is always the tx header */
592 if (!virtio_with_packed_queue(hw)) {
593 struct vring_desc *start_dp = txr[i].tx_indir;
594 vring_desc_init_split(start_dp,
595 RTE_DIM(txr[i].tx_indir));
596 start_dp->addr = txvq->virtio_net_hdr_mem
598 + offsetof(struct virtio_tx_region,
600 start_dp->len = hw->vtnet_hdr_size;
601 start_dp->flags = VRING_DESC_F_NEXT;
603 struct vring_packed_desc *start_dp =
604 txr[i].tx_packed_indir;
605 vring_desc_init_indirect_packed(start_dp,
606 RTE_DIM(txr[i].tx_packed_indir));
607 start_dp->addr = txvq->virtio_net_hdr_mem
609 + offsetof(struct virtio_tx_region,
611 start_dp->len = hw->vtnet_hdr_size;
616 if (VIRTIO_OPS(hw)->setup_queue(hw, vq) < 0) {
617 PMD_INIT_LOG(ERR, "setup_queue failed");
630 rte_memzone_free(hdr_mz);
632 rte_memzone_free(mz);
635 hw->vqs[queue_idx] = NULL;
641 virtio_free_queues(struct virtio_hw *hw)
643 uint16_t nr_vq = virtio_get_nr_vq(hw);
644 struct virtqueue *vq;
651 for (i = 0; i < nr_vq; i++) {
656 queue_type = virtio_get_queue_type(hw, i);
657 if (queue_type == VTNET_RQ) {
658 rte_free(vq->rxq.fake_mbuf);
659 rte_free(vq->sw_ring);
660 rte_memzone_free(vq->rxq.mz);
661 } else if (queue_type == VTNET_TQ) {
662 rte_memzone_free(vq->txq.mz);
663 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
665 rte_memzone_free(vq->cq.mz);
666 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
678 virtio_alloc_queues(struct rte_eth_dev *dev)
680 struct virtio_hw *hw = dev->data->dev_private;
681 uint16_t nr_vq = virtio_get_nr_vq(hw);
685 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
687 PMD_INIT_LOG(ERR, "failed to allocate vqs");
691 for (i = 0; i < nr_vq; i++) {
692 ret = virtio_init_queue(dev, i);
694 virtio_free_queues(hw);
702 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
705 virtio_dev_close(struct rte_eth_dev *dev)
707 struct virtio_hw *hw = dev->data->dev_private;
708 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
710 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
711 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
719 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
720 VIRTIO_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
722 virtio_queues_unbind_intr(dev);
724 if (intr_conf->lsc || intr_conf->rxq) {
725 virtio_intr_disable(dev);
726 rte_intr_efd_disable(dev->intr_handle);
727 rte_free(dev->intr_handle->intr_vec);
728 dev->intr_handle->intr_vec = NULL;
732 virtio_dev_free_mbufs(dev);
733 virtio_free_queues(hw);
735 return VIRTIO_OPS(hw)->dev_close(hw);
739 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
741 struct virtio_hw *hw = dev->data->dev_private;
742 struct virtio_pmd_ctrl ctrl;
746 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
747 PMD_INIT_LOG(INFO, "host does not support rx control");
751 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
752 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
756 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
758 PMD_INIT_LOG(ERR, "Failed to enable promisc");
766 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
768 struct virtio_hw *hw = dev->data->dev_private;
769 struct virtio_pmd_ctrl ctrl;
773 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
774 PMD_INIT_LOG(INFO, "host does not support rx control");
778 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
779 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
783 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
785 PMD_INIT_LOG(ERR, "Failed to disable promisc");
793 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
795 struct virtio_hw *hw = dev->data->dev_private;
796 struct virtio_pmd_ctrl ctrl;
800 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
801 PMD_INIT_LOG(INFO, "host does not support rx control");
805 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
806 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
810 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
812 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
820 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
822 struct virtio_hw *hw = dev->data->dev_private;
823 struct virtio_pmd_ctrl ctrl;
827 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
828 PMD_INIT_LOG(INFO, "host does not support rx control");
832 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
833 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
837 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
839 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
847 virtio_rx_mem_pool_buf_size(struct rte_mempool *mp)
849 return rte_pktmbuf_data_room_size(mp) - RTE_PKTMBUF_HEADROOM;
853 virtio_rx_check_scatter(uint16_t max_rx_pkt_len, uint16_t rx_buf_size,
854 bool rx_scatter_enabled, const char **error)
856 if (!rx_scatter_enabled && max_rx_pkt_len > rx_buf_size) {
857 *error = "Rx scatter is disabled and RxQ mbuf pool object size is too small";
865 virtio_check_scatter_on_all_rx_queues(struct rte_eth_dev *dev,
868 struct virtio_hw *hw = dev->data->dev_private;
869 struct virtnet_rx *rxvq;
870 struct virtqueue *vq;
878 for (qidx = 0; (vq = hw->vqs[2 * qidx + VTNET_SQ_RQ_QUEUE_IDX]) != NULL;
881 if (rxvq->mpool == NULL)
883 buf_size = virtio_rx_mem_pool_buf_size(rxvq->mpool);
885 if (!virtio_rx_check_scatter(frame_size, buf_size,
886 hw->rx_ol_scatter, &error)) {
887 PMD_INIT_LOG(ERR, "MTU check for RxQ %u failed: %s",
896 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
898 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
900 struct virtio_hw *hw = dev->data->dev_private;
901 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
903 uint32_t frame_size = mtu + ether_hdr_len;
904 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
906 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
908 if (mtu < RTE_ETHER_MIN_MTU || frame_size > max_frame_size) {
909 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
910 RTE_ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
914 if (!virtio_check_scatter_on_all_rx_queues(dev, frame_size)) {
915 PMD_INIT_LOG(ERR, "MTU vs Rx scatter and Rx buffers check failed");
919 hw->max_rx_pkt_len = frame_size;
920 dev->data->dev_conf.rxmode.max_rx_pkt_len = hw->max_rx_pkt_len;
926 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
928 struct virtio_hw *hw = dev->data->dev_private;
929 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
930 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
932 virtqueue_enable_intr(vq);
933 virtio_mb(hw->weak_barriers);
938 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
940 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
941 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
943 virtqueue_disable_intr(vq);
948 * dev_ops for virtio, bare necessities for basic operation
950 static const struct eth_dev_ops virtio_eth_dev_ops = {
951 .dev_configure = virtio_dev_configure,
952 .dev_start = virtio_dev_start,
953 .dev_stop = virtio_dev_stop,
954 .dev_close = virtio_dev_close,
955 .promiscuous_enable = virtio_dev_promiscuous_enable,
956 .promiscuous_disable = virtio_dev_promiscuous_disable,
957 .allmulticast_enable = virtio_dev_allmulticast_enable,
958 .allmulticast_disable = virtio_dev_allmulticast_disable,
959 .mtu_set = virtio_mtu_set,
960 .dev_infos_get = virtio_dev_info_get,
961 .stats_get = virtio_dev_stats_get,
962 .xstats_get = virtio_dev_xstats_get,
963 .xstats_get_names = virtio_dev_xstats_get_names,
964 .stats_reset = virtio_dev_stats_reset,
965 .xstats_reset = virtio_dev_stats_reset,
966 .link_update = virtio_dev_link_update,
967 .vlan_offload_set = virtio_dev_vlan_offload_set,
968 .rx_queue_setup = virtio_dev_rx_queue_setup,
969 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
970 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
971 .rx_queue_release = virtio_dev_queue_release,
972 .tx_queue_setup = virtio_dev_tx_queue_setup,
973 .tx_queue_release = virtio_dev_queue_release,
974 /* collect stats per queue */
975 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
976 .vlan_filter_set = virtio_vlan_filter_set,
977 .mac_addr_add = virtio_mac_addr_add,
978 .mac_addr_remove = virtio_mac_addr_remove,
979 .mac_addr_set = virtio_mac_addr_set,
983 * dev_ops for virtio-user in secondary processes, as we just have
984 * some limited supports currently.
986 const struct eth_dev_ops virtio_user_secondary_eth_dev_ops = {
987 .dev_infos_get = virtio_dev_info_get,
988 .stats_get = virtio_dev_stats_get,
989 .xstats_get = virtio_dev_xstats_get,
990 .xstats_get_names = virtio_dev_xstats_get_names,
991 .stats_reset = virtio_dev_stats_reset,
992 .xstats_reset = virtio_dev_stats_reset,
993 /* collect stats per queue */
994 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
998 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1002 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1003 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
1007 stats->opackets += txvq->stats.packets;
1008 stats->obytes += txvq->stats.bytes;
1010 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1011 stats->q_opackets[i] = txvq->stats.packets;
1012 stats->q_obytes[i] = txvq->stats.bytes;
1016 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1017 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1021 stats->ipackets += rxvq->stats.packets;
1022 stats->ibytes += rxvq->stats.bytes;
1023 stats->ierrors += rxvq->stats.errors;
1025 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1026 stats->q_ipackets[i] = rxvq->stats.packets;
1027 stats->q_ibytes[i] = rxvq->stats.bytes;
1031 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1034 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
1035 struct rte_eth_xstat_name *xstats_names,
1036 __rte_unused unsigned limit)
1042 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1043 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1045 if (xstats_names != NULL) {
1046 /* Note: limit checked in rte_eth_xstats_names() */
1048 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1049 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1052 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1053 snprintf(xstats_names[count].name,
1054 sizeof(xstats_names[count].name),
1056 rte_virtio_rxq_stat_strings[t].name);
1061 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1062 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1065 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1066 snprintf(xstats_names[count].name,
1067 sizeof(xstats_names[count].name),
1069 rte_virtio_txq_stat_strings[t].name);
1079 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1085 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1086 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1091 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1092 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1099 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1100 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1101 rte_virtio_rxq_stat_strings[t].offset);
1102 xstats[count].id = count;
1107 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1108 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1115 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1116 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1117 rte_virtio_txq_stat_strings[t].offset);
1118 xstats[count].id = count;
1127 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1129 virtio_update_stats(dev, stats);
1135 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1139 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1140 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1144 txvq->stats.packets = 0;
1145 txvq->stats.bytes = 0;
1146 txvq->stats.multicast = 0;
1147 txvq->stats.broadcast = 0;
1148 memset(txvq->stats.size_bins, 0,
1149 sizeof(txvq->stats.size_bins[0]) * 8);
1152 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1153 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1157 rxvq->stats.packets = 0;
1158 rxvq->stats.bytes = 0;
1159 rxvq->stats.errors = 0;
1160 rxvq->stats.multicast = 0;
1161 rxvq->stats.broadcast = 0;
1162 memset(rxvq->stats.size_bins, 0,
1163 sizeof(rxvq->stats.size_bins[0]) * 8);
1170 virtio_set_hwaddr(struct virtio_hw *hw)
1172 virtio_write_dev_config(hw,
1173 offsetof(struct virtio_net_config, mac),
1174 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1178 virtio_get_hwaddr(struct virtio_hw *hw)
1180 if (virtio_with_feature(hw, VIRTIO_NET_F_MAC)) {
1181 virtio_read_dev_config(hw,
1182 offsetof(struct virtio_net_config, mac),
1183 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1185 rte_eth_random_addr(&hw->mac_addr[0]);
1186 virtio_set_hwaddr(hw);
1191 virtio_mac_table_set(struct virtio_hw *hw,
1192 const struct virtio_net_ctrl_mac *uc,
1193 const struct virtio_net_ctrl_mac *mc)
1195 struct virtio_pmd_ctrl ctrl;
1198 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1199 PMD_DRV_LOG(INFO, "host does not support mac table");
1203 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1204 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1206 len[0] = uc->entries * RTE_ETHER_ADDR_LEN + sizeof(uc->entries);
1207 memcpy(ctrl.data, uc, len[0]);
1209 len[1] = mc->entries * RTE_ETHER_ADDR_LEN + sizeof(mc->entries);
1210 memcpy(ctrl.data + len[0], mc, len[1]);
1212 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1214 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1219 virtio_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1220 uint32_t index, uint32_t vmdq __rte_unused)
1222 struct virtio_hw *hw = dev->data->dev_private;
1223 const struct rte_ether_addr *addrs = dev->data->mac_addrs;
1225 struct virtio_net_ctrl_mac *uc, *mc;
1227 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1228 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1232 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1233 sizeof(uc->entries));
1235 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1236 sizeof(mc->entries));
1239 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1240 const struct rte_ether_addr *addr
1241 = (i == index) ? mac_addr : addrs + i;
1242 struct virtio_net_ctrl_mac *tbl
1243 = rte_is_multicast_ether_addr(addr) ? mc : uc;
1245 memcpy(&tbl->macs[tbl->entries++], addr, RTE_ETHER_ADDR_LEN);
1248 return virtio_mac_table_set(hw, uc, mc);
1252 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1254 struct virtio_hw *hw = dev->data->dev_private;
1255 struct rte_ether_addr *addrs = dev->data->mac_addrs;
1256 struct virtio_net_ctrl_mac *uc, *mc;
1259 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1260 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1264 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1265 sizeof(uc->entries));
1267 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1268 sizeof(mc->entries));
1271 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1272 struct virtio_net_ctrl_mac *tbl;
1274 if (i == index || rte_is_zero_ether_addr(addrs + i))
1277 tbl = rte_is_multicast_ether_addr(addrs + i) ? mc : uc;
1278 memcpy(&tbl->macs[tbl->entries++], addrs + i,
1279 RTE_ETHER_ADDR_LEN);
1282 virtio_mac_table_set(hw, uc, mc);
1286 virtio_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1288 struct virtio_hw *hw = dev->data->dev_private;
1290 memcpy(hw->mac_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1292 /* Use atomic update if available */
1293 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1294 struct virtio_pmd_ctrl ctrl;
1295 int len = RTE_ETHER_ADDR_LEN;
1297 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1298 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1300 memcpy(ctrl.data, mac_addr, RTE_ETHER_ADDR_LEN);
1301 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1304 if (!virtio_with_feature(hw, VIRTIO_NET_F_MAC))
1307 virtio_set_hwaddr(hw);
1312 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1314 struct virtio_hw *hw = dev->data->dev_private;
1315 struct virtio_pmd_ctrl ctrl;
1318 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1321 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1322 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1323 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1324 len = sizeof(vlan_id);
1326 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1330 virtio_intr_unmask(struct rte_eth_dev *dev)
1332 struct virtio_hw *hw = dev->data->dev_private;
1334 if (rte_intr_ack(dev->intr_handle) < 0)
1337 if (VIRTIO_OPS(hw)->intr_detect)
1338 VIRTIO_OPS(hw)->intr_detect(hw);
1344 virtio_intr_enable(struct rte_eth_dev *dev)
1346 struct virtio_hw *hw = dev->data->dev_private;
1348 if (rte_intr_enable(dev->intr_handle) < 0)
1351 if (VIRTIO_OPS(hw)->intr_detect)
1352 VIRTIO_OPS(hw)->intr_detect(hw);
1358 virtio_intr_disable(struct rte_eth_dev *dev)
1360 struct virtio_hw *hw = dev->data->dev_private;
1362 if (rte_intr_disable(dev->intr_handle) < 0)
1365 if (VIRTIO_OPS(hw)->intr_detect)
1366 VIRTIO_OPS(hw)->intr_detect(hw);
1372 virtio_ethdev_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1374 uint64_t host_features;
1376 /* Prepare guest_features: feature that driver wants to support */
1377 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1380 /* Read device(host) feature bits */
1381 host_features = VIRTIO_OPS(hw)->get_features(hw);
1382 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1385 /* If supported, ensure MTU value is valid before acknowledging it. */
1386 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1387 struct virtio_net_config config;
1389 virtio_read_dev_config(hw,
1390 offsetof(struct virtio_net_config, mtu),
1391 &config.mtu, sizeof(config.mtu));
1393 if (config.mtu < RTE_ETHER_MIN_MTU)
1394 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1398 * Negotiate features: Subset of device feature bits are written back
1399 * guest feature bits.
1401 hw->guest_features = req_features;
1402 hw->guest_features = virtio_negotiate_features(hw, host_features);
1403 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1404 hw->guest_features);
1406 if (VIRTIO_OPS(hw)->features_ok(hw) < 0)
1409 if (virtio_with_feature(hw, VIRTIO_F_VERSION_1)) {
1410 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1412 if (!(virtio_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1413 PMD_INIT_LOG(ERR, "Failed to set FEATURES_OK status!");
1418 hw->req_guest_features = req_features;
1424 virtio_dev_pause(struct rte_eth_dev *dev)
1426 struct virtio_hw *hw = dev->data->dev_private;
1428 rte_spinlock_lock(&hw->state_lock);
1430 if (hw->started == 0) {
1431 /* Device is just stopped. */
1432 rte_spinlock_unlock(&hw->state_lock);
1437 * Prevent the worker threads from touching queues to avoid contention,
1438 * 1 ms should be enough for the ongoing Tx function to finish.
1445 * Recover hw state to let the worker threads continue.
1448 virtio_dev_resume(struct rte_eth_dev *dev)
1450 struct virtio_hw *hw = dev->data->dev_private;
1453 rte_spinlock_unlock(&hw->state_lock);
1457 * Should be called only after device is paused.
1460 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1463 struct virtio_hw *hw = dev->data->dev_private;
1464 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1467 hw->inject_pkts = tx_pkts;
1468 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1469 hw->inject_pkts = NULL;
1475 virtio_notify_peers(struct rte_eth_dev *dev)
1477 struct virtio_hw *hw = dev->data->dev_private;
1478 struct virtnet_rx *rxvq;
1479 struct rte_mbuf *rarp_mbuf;
1481 if (!dev->data->rx_queues)
1484 rxvq = dev->data->rx_queues[0];
1488 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1489 (struct rte_ether_addr *)hw->mac_addr);
1490 if (rarp_mbuf == NULL) {
1491 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1495 /* If virtio port just stopped, no need to send RARP */
1496 if (virtio_dev_pause(dev) < 0) {
1497 rte_pktmbuf_free(rarp_mbuf);
1501 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1502 virtio_dev_resume(dev);
1506 virtio_ack_link_announce(struct rte_eth_dev *dev)
1508 struct virtio_hw *hw = dev->data->dev_private;
1509 struct virtio_pmd_ctrl ctrl;
1511 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1512 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1514 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1518 * Process virtio config changed interrupt. Call the callback
1519 * if link state changed, generate gratuitous RARP packet if
1520 * the status indicates an ANNOUNCE.
1523 virtio_interrupt_handler(void *param)
1525 struct rte_eth_dev *dev = param;
1526 struct virtio_hw *hw = dev->data->dev_private;
1530 /* Read interrupt status which clears interrupt */
1531 isr = virtio_get_isr(hw);
1532 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1534 if (virtio_intr_unmask(dev) < 0)
1535 PMD_DRV_LOG(ERR, "interrupt enable failed");
1537 if (isr & VIRTIO_ISR_CONFIG) {
1538 if (virtio_dev_link_update(dev, 0) == 0)
1539 rte_eth_dev_callback_process(dev,
1540 RTE_ETH_EVENT_INTR_LSC,
1543 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1544 virtio_read_dev_config(hw,
1545 offsetof(struct virtio_net_config, status),
1546 &status, sizeof(status));
1547 if (status & VIRTIO_NET_S_ANNOUNCE) {
1548 virtio_notify_peers(dev);
1550 virtio_ack_link_announce(dev);
1556 /* set rx and tx handlers according to what is supported */
1558 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1560 struct virtio_hw *hw = eth_dev->data->dev_private;
1562 eth_dev->tx_pkt_prepare = virtio_xmit_pkts_prepare;
1563 if (virtio_with_packed_queue(hw)) {
1565 "virtio: using packed ring %s Tx path on port %u",
1566 hw->use_vec_tx ? "vectorized" : "standard",
1567 eth_dev->data->port_id);
1569 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed_vec;
1571 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1573 if (hw->use_inorder_tx) {
1574 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1575 eth_dev->data->port_id);
1576 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1578 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1579 eth_dev->data->port_id);
1580 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1584 if (virtio_with_packed_queue(hw)) {
1585 if (hw->use_vec_rx) {
1587 "virtio: using packed ring vectorized Rx path on port %u",
1588 eth_dev->data->port_id);
1589 eth_dev->rx_pkt_burst =
1590 &virtio_recv_pkts_packed_vec;
1591 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1593 "virtio: using packed ring mergeable buffer Rx path on port %u",
1594 eth_dev->data->port_id);
1595 eth_dev->rx_pkt_burst =
1596 &virtio_recv_mergeable_pkts_packed;
1599 "virtio: using packed ring standard Rx path on port %u",
1600 eth_dev->data->port_id);
1601 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1604 if (hw->use_vec_rx) {
1605 PMD_INIT_LOG(INFO, "virtio: using vectorized Rx path on port %u",
1606 eth_dev->data->port_id);
1607 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1608 } else if (hw->use_inorder_rx) {
1610 "virtio: using inorder Rx path on port %u",
1611 eth_dev->data->port_id);
1612 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1613 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1615 "virtio: using mergeable buffer Rx path on port %u",
1616 eth_dev->data->port_id);
1617 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1619 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1620 eth_dev->data->port_id);
1621 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1627 /* Only support 1:1 queue/interrupt mapping so far.
1628 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1629 * interrupt vectors (<N+1).
1632 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1635 struct virtio_hw *hw = dev->data->dev_private;
1637 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1638 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1639 dev->intr_handle->intr_vec[i] = i + 1;
1640 if (VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1641 VIRTIO_MSI_NO_VECTOR) {
1642 PMD_DRV_LOG(ERR, "failed to set queue vector");
1651 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1654 struct virtio_hw *hw = dev->data->dev_private;
1656 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1657 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1658 VIRTIO_OPS(hw)->set_queue_irq(hw,
1659 hw->vqs[i * VTNET_CQ],
1660 VIRTIO_MSI_NO_VECTOR);
1664 virtio_configure_intr(struct rte_eth_dev *dev)
1666 struct virtio_hw *hw = dev->data->dev_private;
1668 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1669 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1673 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1674 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1678 if (!dev->intr_handle->intr_vec) {
1679 dev->intr_handle->intr_vec =
1680 rte_zmalloc("intr_vec",
1681 hw->max_queue_pairs * sizeof(int), 0);
1682 if (!dev->intr_handle->intr_vec) {
1683 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1684 hw->max_queue_pairs);
1689 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1690 /* Re-register callback to update max_intr */
1691 rte_intr_callback_unregister(dev->intr_handle,
1692 virtio_interrupt_handler,
1694 rte_intr_callback_register(dev->intr_handle,
1695 virtio_interrupt_handler,
1699 /* DO NOT try to remove this! This function will enable msix, or QEMU
1700 * will encounter SIGSEGV when DRIVER_OK is sent.
1701 * And for legacy devices, this should be done before queue/vec binding
1702 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1703 * (22) will be ignored.
1705 if (virtio_intr_enable(dev) < 0) {
1706 PMD_DRV_LOG(ERR, "interrupt enable failed");
1710 if (virtio_queues_bind_intr(dev) < 0) {
1711 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1717 #define DUPLEX_UNKNOWN 0xff
1718 /* reset device and renegotiate features if needed */
1720 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1722 struct virtio_hw *hw = eth_dev->data->dev_private;
1723 struct virtio_net_config *config;
1724 struct virtio_net_config local_config;
1727 /* Reset the device although not necessary at startup */
1731 virtio_dev_free_mbufs(eth_dev);
1732 virtio_free_queues(hw);
1735 /* Tell the host we've noticed this device. */
1736 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1738 /* Tell the host we've known how to drive the device. */
1739 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1740 if (virtio_ethdev_negotiate_features(hw, req_features) < 0)
1743 hw->weak_barriers = !virtio_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1745 /* If host does not support both status and MSI-X then disable LSC */
1746 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->intr_lsc)
1747 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1749 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1751 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1753 /* Setting up rx_header size for the device */
1754 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1755 virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
1756 virtio_with_packed_queue(hw))
1757 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1759 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1761 /* Copy the permanent MAC address to: virtio_hw */
1762 virtio_get_hwaddr(hw);
1763 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1764 ð_dev->data->mac_addrs[0]);
1766 "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
1767 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1768 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1770 if (hw->speed == ETH_SPEED_NUM_UNKNOWN) {
1771 if (virtio_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
1772 config = &local_config;
1773 virtio_read_dev_config(hw,
1774 offsetof(struct virtio_net_config, speed),
1775 &config->speed, sizeof(config->speed));
1776 virtio_read_dev_config(hw,
1777 offsetof(struct virtio_net_config, duplex),
1778 &config->duplex, sizeof(config->duplex));
1779 hw->speed = config->speed;
1780 hw->duplex = config->duplex;
1783 if (hw->duplex == DUPLEX_UNKNOWN)
1784 hw->duplex = ETH_LINK_FULL_DUPLEX;
1785 PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
1786 hw->speed, hw->duplex);
1787 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1788 config = &local_config;
1790 virtio_read_dev_config(hw,
1791 offsetof(struct virtio_net_config, mac),
1792 &config->mac, sizeof(config->mac));
1794 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1795 virtio_read_dev_config(hw,
1796 offsetof(struct virtio_net_config, status),
1797 &config->status, sizeof(config->status));
1800 "VIRTIO_NET_F_STATUS is not supported");
1804 if (virtio_with_feature(hw, VIRTIO_NET_F_MQ)) {
1805 virtio_read_dev_config(hw,
1806 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1807 &config->max_virtqueue_pairs,
1808 sizeof(config->max_virtqueue_pairs));
1811 "VIRTIO_NET_F_MQ is not supported");
1812 config->max_virtqueue_pairs = 1;
1815 hw->max_queue_pairs = config->max_virtqueue_pairs;
1817 if (virtio_with_feature(hw, VIRTIO_NET_F_MTU)) {
1818 virtio_read_dev_config(hw,
1819 offsetof(struct virtio_net_config, mtu),
1821 sizeof(config->mtu));
1824 * MTU value has already been checked at negotiation
1825 * time, but check again in case it has changed since
1826 * then, which should not happen.
1828 if (config->mtu < RTE_ETHER_MIN_MTU) {
1829 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1834 hw->max_mtu = config->mtu;
1835 /* Set initial MTU to maximum one supported by vhost */
1836 eth_dev->data->mtu = config->mtu;
1839 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1840 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1843 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1844 config->max_virtqueue_pairs);
1845 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1847 "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
1848 config->mac[0], config->mac[1],
1849 config->mac[2], config->mac[3],
1850 config->mac[4], config->mac[5]);
1852 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1853 hw->max_queue_pairs = 1;
1854 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1855 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1858 ret = virtio_alloc_queues(eth_dev);
1862 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1863 if (virtio_configure_intr(eth_dev) < 0) {
1864 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1865 virtio_free_queues(hw);
1870 virtio_reinit_complete(hw);
1876 * This function is based on probe() function in virtio_pci.c
1877 * It returns 0 on success.
1880 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1882 struct virtio_hw *hw = eth_dev->data->dev_private;
1883 uint32_t speed = ETH_SPEED_NUM_UNKNOWN;
1887 if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1889 "Not sufficient headroom required = %d, avail = %d",
1890 (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1891 RTE_PKTMBUF_HEADROOM);
1896 eth_dev->dev_ops = &virtio_eth_dev_ops;
1897 eth_dev->rx_descriptor_done = virtio_dev_rx_queue_done;
1899 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1900 set_rxtx_funcs(eth_dev);
1904 ret = virtio_dev_devargs_parse(eth_dev->device->devargs, &speed, &vectorized);
1908 hw->duplex = DUPLEX_UNKNOWN;
1910 /* Allocate memory for storing MAC addresses */
1911 eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1912 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1913 if (eth_dev->data->mac_addrs == NULL) {
1915 "Failed to allocate %d bytes needed to store MAC addresses",
1916 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1920 rte_spinlock_init(&hw->state_lock);
1922 /* reset device and negotiate default features */
1923 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1925 goto err_virtio_init;
1928 if (!virtio_with_packed_queue(hw)) {
1931 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
1936 "building environment do not support packed ring vectorized");
1946 rte_free(eth_dev->data->mac_addrs);
1947 eth_dev->data->mac_addrs = NULL;
1952 virtio_dev_speed_capa_get(uint32_t speed)
1955 case ETH_SPEED_NUM_10G:
1956 return ETH_LINK_SPEED_10G;
1957 case ETH_SPEED_NUM_20G:
1958 return ETH_LINK_SPEED_20G;
1959 case ETH_SPEED_NUM_25G:
1960 return ETH_LINK_SPEED_25G;
1961 case ETH_SPEED_NUM_40G:
1962 return ETH_LINK_SPEED_40G;
1963 case ETH_SPEED_NUM_50G:
1964 return ETH_LINK_SPEED_50G;
1965 case ETH_SPEED_NUM_56G:
1966 return ETH_LINK_SPEED_56G;
1967 case ETH_SPEED_NUM_100G:
1968 return ETH_LINK_SPEED_100G;
1969 case ETH_SPEED_NUM_200G:
1970 return ETH_LINK_SPEED_200G;
1976 static int vectorized_check_handler(__rte_unused const char *key,
1977 const char *value, void *ret_val)
1979 if (strcmp(value, "1") == 0)
1980 *(int *)ret_val = 1;
1982 *(int *)ret_val = 0;
1987 #define VIRTIO_ARG_SPEED "speed"
1988 #define VIRTIO_ARG_VECTORIZED "vectorized"
1991 link_speed_handler(const char *key __rte_unused,
1992 const char *value, void *ret_val)
1995 if (!value || !ret_val)
1997 val = strtoul(value, NULL, 0);
1998 /* validate input */
1999 if (virtio_dev_speed_capa_get(val) == 0)
2001 *(uint32_t *)ret_val = val;
2008 virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vectorized)
2010 struct rte_kvargs *kvlist;
2013 if (devargs == NULL)
2016 kvlist = rte_kvargs_parse(devargs->args, NULL);
2017 if (kvlist == NULL) {
2018 PMD_INIT_LOG(ERR, "error when parsing param");
2022 if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
2023 ret = rte_kvargs_process(kvlist,
2025 link_speed_handler, speed);
2027 PMD_INIT_LOG(ERR, "Failed to parse %s",
2034 rte_kvargs_count(kvlist, VIRTIO_ARG_VECTORIZED) == 1) {
2035 ret = rte_kvargs_process(kvlist,
2036 VIRTIO_ARG_VECTORIZED,
2037 vectorized_check_handler, vectorized);
2039 PMD_INIT_LOG(ERR, "Failed to parse %s",
2040 VIRTIO_ARG_VECTORIZED);
2046 rte_kvargs_free(kvlist);
2051 rx_offload_enabled(struct virtio_hw *hw)
2053 return virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
2054 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2055 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
2059 tx_offload_enabled(struct virtio_hw *hw)
2061 return virtio_with_feature(hw, VIRTIO_NET_F_CSUM) ||
2062 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
2063 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2067 * Configure virtio device
2068 * It returns 0 on success.
2071 virtio_dev_configure(struct rte_eth_dev *dev)
2073 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2074 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2075 struct virtio_hw *hw = dev->data->dev_private;
2076 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2078 uint64_t rx_offloads = rxmode->offloads;
2079 uint64_t tx_offloads = txmode->offloads;
2080 uint64_t req_features;
2083 PMD_INIT_LOG(DEBUG, "configure");
2084 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2086 if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2088 "Unsupported Rx multi queue mode %d",
2093 if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2095 "Unsupported Tx multi queue mode %d",
2100 if (dev->data->dev_conf.intr_conf.rxq) {
2101 ret = virtio_init_device(dev, hw->req_guest_features);
2106 if ((rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) &&
2107 (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len))
2108 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2110 if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
2111 hw->max_rx_pkt_len = rxmode->max_rx_pkt_len;
2113 hw->max_rx_pkt_len = ether_hdr_len + dev->data->mtu;
2115 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2116 DEV_RX_OFFLOAD_TCP_CKSUM))
2117 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2119 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2121 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2122 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2124 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2125 DEV_TX_OFFLOAD_TCP_CKSUM))
2126 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2128 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2130 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2131 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2133 /* if request features changed, reinit the device */
2134 if (req_features != hw->req_guest_features) {
2135 ret = virtio_init_device(dev, req_features);
2140 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2141 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2142 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2144 "rx checksum not available on this host");
2148 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2149 (!virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2150 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2152 "Large Receive Offload not available on this host");
2156 /* start control queue */
2157 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2158 virtio_dev_cq_start(dev);
2160 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2163 hw->rx_ol_scatter = (rx_offloads & DEV_RX_OFFLOAD_SCATTER);
2165 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2166 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2168 "vlan filtering not available on this host");
2172 hw->has_tx_offload = tx_offload_enabled(hw);
2173 hw->has_rx_offload = rx_offload_enabled(hw);
2175 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2176 /* Enable vector (0) for Link State Intrerrupt */
2177 if (VIRTIO_OPS(hw)->set_config_irq(hw, 0) ==
2178 VIRTIO_MSI_NO_VECTOR) {
2179 PMD_DRV_LOG(ERR, "failed to set config vector");
2183 if (virtio_with_packed_queue(hw)) {
2184 #if defined(RTE_ARCH_X86_64) && defined(CC_AVX512_SUPPORT)
2185 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2186 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) ||
2187 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2188 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2189 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)) {
2191 "disabled packed ring vectorized path for requirements not met");
2195 #elif defined(RTE_ARCH_ARM)
2196 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2197 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) ||
2198 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2199 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2200 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)) {
2202 "disabled packed ring vectorized path for requirements not met");
2211 if (hw->use_vec_rx) {
2212 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2214 "disabled packed ring vectorized rx for mrg_rxbuf enabled");
2218 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) {
2220 "disabled packed ring vectorized rx for TCP_LRO enabled");
2225 if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2226 hw->use_inorder_tx = 1;
2227 hw->use_inorder_rx = 1;
2231 if (hw->use_vec_rx) {
2232 #if defined RTE_ARCH_ARM
2233 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2235 "disabled split ring vectorized path for requirement not met");
2239 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2241 "disabled split ring vectorized rx for mrg_rxbuf enabled");
2245 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2246 DEV_RX_OFFLOAD_TCP_CKSUM |
2247 DEV_RX_OFFLOAD_TCP_LRO |
2248 DEV_RX_OFFLOAD_VLAN_STRIP)) {
2250 "disabled split ring vectorized rx for offloading enabled");
2254 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128) {
2256 "disabled split ring vectorized rx, max SIMD bitwidth too low");
2267 virtio_dev_start(struct rte_eth_dev *dev)
2269 uint16_t nb_queues, i;
2270 struct virtqueue *vq;
2271 struct virtio_hw *hw = dev->data->dev_private;
2274 /* Finish the initialization of the queues */
2275 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2276 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2280 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2281 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2286 /* check if lsc interrupt feature is enabled */
2287 if (dev->data->dev_conf.intr_conf.lsc) {
2288 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2289 PMD_DRV_LOG(ERR, "link status not supported by host");
2294 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2295 * in device configure, but it could be unmapped when device is
2298 if (dev->data->dev_conf.intr_conf.lsc ||
2299 dev->data->dev_conf.intr_conf.rxq) {
2300 virtio_intr_disable(dev);
2302 /* Setup interrupt callback */
2303 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2304 rte_intr_callback_register(dev->intr_handle,
2305 virtio_interrupt_handler,
2308 if (virtio_intr_enable(dev) < 0) {
2309 PMD_DRV_LOG(ERR, "interrupt enable failed");
2314 /*Notify the backend
2315 *Otherwise the tap backend might already stop its queue due to fullness.
2316 *vhost backend will have no chance to be waked up
2318 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2319 if (hw->max_queue_pairs > 1) {
2320 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2324 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2326 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2327 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2328 /* Flush the old packets */
2329 virtqueue_rxvq_flush(vq);
2330 virtqueue_notify(vq);
2333 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2334 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2335 virtqueue_notify(vq);
2338 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2340 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2341 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2345 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2346 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2350 set_rxtx_funcs(dev);
2353 /* Initialize Link state */
2354 virtio_dev_link_update(dev, 0);
2359 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2361 struct virtio_hw *hw = dev->data->dev_private;
2362 uint16_t nr_vq = virtio_get_nr_vq(hw);
2363 const char *type __rte_unused;
2364 unsigned int i, mbuf_num = 0;
2365 struct virtqueue *vq;
2366 struct rte_mbuf *buf;
2369 if (hw->vqs == NULL)
2372 for (i = 0; i < nr_vq; i++) {
2377 queue_type = virtio_get_queue_type(hw, i);
2378 if (queue_type == VTNET_RQ)
2380 else if (queue_type == VTNET_TQ)
2386 "Before freeing %s[%d] used and unused buf",
2390 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2391 rte_pktmbuf_free(buf);
2396 "After freeing %s[%d] used and unused buf",
2401 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2405 * Stop device: disable interrupt and mark link down
2408 virtio_dev_stop(struct rte_eth_dev *dev)
2410 struct virtio_hw *hw = dev->data->dev_private;
2411 struct rte_eth_link link;
2412 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2414 PMD_INIT_LOG(DEBUG, "stop");
2415 dev->data->dev_started = 0;
2417 rte_spinlock_lock(&hw->state_lock);
2422 if (intr_conf->lsc || intr_conf->rxq) {
2423 virtio_intr_disable(dev);
2425 /* Reset interrupt callback */
2426 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2427 rte_intr_callback_unregister(dev->intr_handle,
2428 virtio_interrupt_handler,
2433 memset(&link, 0, sizeof(link));
2434 rte_eth_linkstatus_set(dev, &link);
2436 rte_spinlock_unlock(&hw->state_lock);
2442 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2444 struct rte_eth_link link;
2446 struct virtio_hw *hw = dev->data->dev_private;
2448 memset(&link, 0, sizeof(link));
2449 link.link_duplex = hw->duplex;
2450 link.link_speed = hw->speed;
2451 link.link_autoneg = ETH_LINK_AUTONEG;
2454 link.link_status = ETH_LINK_DOWN;
2455 link.link_speed = ETH_SPEED_NUM_NONE;
2456 } else if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2457 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2458 virtio_read_dev_config(hw,
2459 offsetof(struct virtio_net_config, status),
2460 &status, sizeof(status));
2461 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2462 link.link_status = ETH_LINK_DOWN;
2463 link.link_speed = ETH_SPEED_NUM_NONE;
2464 PMD_INIT_LOG(DEBUG, "Port %d is down",
2465 dev->data->port_id);
2467 link.link_status = ETH_LINK_UP;
2468 PMD_INIT_LOG(DEBUG, "Port %d is up",
2469 dev->data->port_id);
2472 link.link_status = ETH_LINK_UP;
2475 return rte_eth_linkstatus_set(dev, &link);
2479 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2481 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2482 struct virtio_hw *hw = dev->data->dev_private;
2483 uint64_t offloads = rxmode->offloads;
2485 if (mask & ETH_VLAN_FILTER_MASK) {
2486 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2487 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2490 "vlan filtering not available on this host");
2496 if (mask & ETH_VLAN_STRIP_MASK)
2497 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2503 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2505 uint64_t tso_mask, host_features;
2506 struct virtio_hw *hw = dev->data->dev_private;
2507 dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2509 dev_info->max_rx_queues =
2510 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2511 dev_info->max_tx_queues =
2512 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2513 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2514 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2515 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2516 dev_info->max_mtu = hw->max_mtu;
2518 host_features = VIRTIO_OPS(hw)->get_features(hw);
2519 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2520 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2521 if (host_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))
2522 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_SCATTER;
2523 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2524 dev_info->rx_offload_capa |=
2525 DEV_RX_OFFLOAD_TCP_CKSUM |
2526 DEV_RX_OFFLOAD_UDP_CKSUM;
2528 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2529 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2530 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2531 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2532 if ((host_features & tso_mask) == tso_mask)
2533 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2535 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2536 DEV_TX_OFFLOAD_VLAN_INSERT;
2537 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2538 dev_info->tx_offload_capa |=
2539 DEV_TX_OFFLOAD_UDP_CKSUM |
2540 DEV_TX_OFFLOAD_TCP_CKSUM;
2542 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2543 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2544 if ((host_features & tso_mask) == tso_mask)
2545 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2547 if (host_features & (1ULL << VIRTIO_F_RING_PACKED)) {
2549 * According to 2.7 Packed Virtqueues,
2550 * 2.7.10.1 Structure Size and Alignment:
2551 * The Queue Size value does not have to be a power of 2.
2553 dev_info->rx_desc_lim.nb_max = UINT16_MAX;
2556 * According to 2.6 Split Virtqueues:
2557 * Queue Size value is always a power of 2. The maximum Queue
2558 * Size value is 32768.
2560 dev_info->rx_desc_lim.nb_max = 32768;
2563 * Actual minimum is not the same for virtqueues of different kinds,
2564 * but to avoid tangling the code with separate branches, rely on
2565 * default thresholds since desc number must be at least of their size.
2567 dev_info->rx_desc_lim.nb_min = RTE_MAX(DEFAULT_RX_FREE_THRESH,
2568 RTE_VIRTIO_VPMD_RX_REARM_THRESH);
2569 dev_info->rx_desc_lim.nb_align = 1;
2575 * It enables testpmd to collect per queue stats.
2578 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2579 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2580 __rte_unused uint8_t is_rx)
2585 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_init, init, NOTICE);
2586 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_driver, driver, NOTICE);