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",
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);
519 vq->vq_ring_mem = (uintptr_t)mz->addr;
521 vq->vq_ring_mem = mz->iova;
523 vq->vq_ring_virt_mem = mz->addr;
524 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64, vq->vq_ring_mem);
525 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: %p", vq->vq_ring_virt_mem);
527 virtio_init_vring(vq);
530 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
531 dev->data->port_id, queue_idx);
532 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
533 numa_node, RTE_MEMZONE_IOVA_CONTIG,
534 RTE_CACHE_LINE_SIZE);
535 if (hdr_mz == NULL) {
536 if (rte_errno == EEXIST)
537 hdr_mz = rte_memzone_lookup(vq_hdr_name);
538 if (hdr_mz == NULL) {
545 if (queue_type == VTNET_RQ) {
546 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
547 sizeof(vq->sw_ring[0]);
549 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
550 RTE_CACHE_LINE_SIZE, numa_node);
552 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
557 fake_mbuf = rte_zmalloc_socket("sw_ring", sizeof(*fake_mbuf),
558 RTE_CACHE_LINE_SIZE, numa_node);
560 PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
565 vq->sw_ring = sw_ring;
567 rxvq->port_id = dev->data->port_id;
569 rxvq->fake_mbuf = fake_mbuf;
570 } else if (queue_type == VTNET_TQ) {
572 txvq->port_id = dev->data->port_id;
574 txvq->virtio_net_hdr_mz = hdr_mz;
576 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
578 txvq->virtio_net_hdr_mem = hdr_mz->iova;
579 } else if (queue_type == VTNET_CQ) {
582 cvq->virtio_net_hdr_mz = hdr_mz;
584 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
586 cvq->virtio_net_hdr_mem = hdr_mz->iova;
587 memset(cvq->virtio_net_hdr_mz->addr, 0, rte_mem_page_size());
593 vq->mbuf_addr_offset = offsetof(struct rte_mbuf, buf_addr);
595 vq->mbuf_addr_offset = offsetof(struct rte_mbuf, buf_iova);
597 if (queue_type == VTNET_TQ) {
598 struct virtio_tx_region *txr;
602 memset(txr, 0, vq_size * sizeof(*txr));
603 for (i = 0; i < vq_size; i++) {
604 /* first indirect descriptor is always the tx header */
605 if (!virtio_with_packed_queue(hw)) {
606 struct vring_desc *start_dp = txr[i].tx_indir;
607 vring_desc_init_split(start_dp,
608 RTE_DIM(txr[i].tx_indir));
609 start_dp->addr = txvq->virtio_net_hdr_mem
611 + offsetof(struct virtio_tx_region,
613 start_dp->len = hw->vtnet_hdr_size;
614 start_dp->flags = VRING_DESC_F_NEXT;
616 struct vring_packed_desc *start_dp =
617 txr[i].tx_packed_indir;
618 vring_desc_init_indirect_packed(start_dp,
619 RTE_DIM(txr[i].tx_packed_indir));
620 start_dp->addr = txvq->virtio_net_hdr_mem
622 + offsetof(struct virtio_tx_region,
624 start_dp->len = hw->vtnet_hdr_size;
629 if (VIRTIO_OPS(hw)->setup_queue(hw, vq) < 0) {
630 PMD_INIT_LOG(ERR, "setup_queue failed");
643 rte_memzone_free(hdr_mz);
645 rte_memzone_free(mz);
648 hw->vqs[queue_idx] = NULL;
654 virtio_free_queues(struct virtio_hw *hw)
656 uint16_t nr_vq = virtio_get_nr_vq(hw);
657 struct virtqueue *vq;
664 for (i = 0; i < nr_vq; i++) {
669 queue_type = virtio_get_queue_type(hw, i);
670 if (queue_type == VTNET_RQ) {
671 rte_free(vq->rxq.fake_mbuf);
672 rte_free(vq->sw_ring);
673 rte_memzone_free(vq->rxq.mz);
674 } else if (queue_type == VTNET_TQ) {
675 rte_memzone_free(vq->txq.mz);
676 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
678 rte_memzone_free(vq->cq.mz);
679 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
691 virtio_alloc_queues(struct rte_eth_dev *dev)
693 struct virtio_hw *hw = dev->data->dev_private;
694 uint16_t nr_vq = virtio_get_nr_vq(hw);
698 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
700 PMD_INIT_LOG(ERR, "failed to allocate vqs");
704 for (i = 0; i < nr_vq; i++) {
705 ret = virtio_init_queue(dev, i);
707 virtio_free_queues(hw);
715 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
718 virtio_dev_close(struct rte_eth_dev *dev)
720 struct virtio_hw *hw = dev->data->dev_private;
721 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
723 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
724 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
732 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
733 VIRTIO_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
735 virtio_queues_unbind_intr(dev);
737 if (intr_conf->lsc || intr_conf->rxq) {
738 virtio_intr_disable(dev);
739 rte_intr_efd_disable(dev->intr_handle);
740 rte_free(dev->intr_handle->intr_vec);
741 dev->intr_handle->intr_vec = NULL;
745 virtio_dev_free_mbufs(dev);
746 virtio_free_queues(hw);
748 return VIRTIO_OPS(hw)->dev_close(hw);
752 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
754 struct virtio_hw *hw = dev->data->dev_private;
755 struct virtio_pmd_ctrl ctrl;
759 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
760 PMD_INIT_LOG(INFO, "host does not support rx control");
764 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
765 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
769 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
771 PMD_INIT_LOG(ERR, "Failed to enable promisc");
779 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
781 struct virtio_hw *hw = dev->data->dev_private;
782 struct virtio_pmd_ctrl ctrl;
786 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
787 PMD_INIT_LOG(INFO, "host does not support rx control");
791 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
792 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
796 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
798 PMD_INIT_LOG(ERR, "Failed to disable promisc");
806 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
808 struct virtio_hw *hw = dev->data->dev_private;
809 struct virtio_pmd_ctrl ctrl;
813 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
814 PMD_INIT_LOG(INFO, "host does not support rx control");
818 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
819 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
823 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
825 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
833 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
835 struct virtio_hw *hw = dev->data->dev_private;
836 struct virtio_pmd_ctrl ctrl;
840 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
841 PMD_INIT_LOG(INFO, "host does not support rx control");
845 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
846 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
850 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
852 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
860 virtio_rx_mem_pool_buf_size(struct rte_mempool *mp)
862 return rte_pktmbuf_data_room_size(mp) - RTE_PKTMBUF_HEADROOM;
866 virtio_rx_check_scatter(uint16_t max_rx_pkt_len, uint16_t rx_buf_size,
867 bool rx_scatter_enabled, const char **error)
869 if (!rx_scatter_enabled && max_rx_pkt_len > rx_buf_size) {
870 *error = "Rx scatter is disabled and RxQ mbuf pool object size is too small";
878 virtio_check_scatter_on_all_rx_queues(struct rte_eth_dev *dev,
881 struct virtio_hw *hw = dev->data->dev_private;
882 struct virtnet_rx *rxvq;
883 struct virtqueue *vq;
891 for (qidx = 0; (vq = hw->vqs[2 * qidx + VTNET_SQ_RQ_QUEUE_IDX]) != NULL;
894 if (rxvq->mpool == NULL)
896 buf_size = virtio_rx_mem_pool_buf_size(rxvq->mpool);
898 if (!virtio_rx_check_scatter(frame_size, buf_size,
899 hw->rx_ol_scatter, &error)) {
900 PMD_INIT_LOG(ERR, "MTU check for RxQ %u failed: %s",
909 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
911 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
913 struct virtio_hw *hw = dev->data->dev_private;
914 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
916 uint32_t frame_size = mtu + ether_hdr_len;
917 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
919 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
921 if (mtu < RTE_ETHER_MIN_MTU || frame_size > max_frame_size) {
922 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
923 RTE_ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
927 if (!virtio_check_scatter_on_all_rx_queues(dev, frame_size)) {
928 PMD_INIT_LOG(ERR, "MTU vs Rx scatter and Rx buffers check failed");
932 hw->max_rx_pkt_len = frame_size;
933 dev->data->dev_conf.rxmode.max_rx_pkt_len = hw->max_rx_pkt_len;
939 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
941 struct virtio_hw *hw = dev->data->dev_private;
942 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
943 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
945 virtqueue_enable_intr(vq);
946 virtio_mb(hw->weak_barriers);
951 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
953 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
954 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
956 virtqueue_disable_intr(vq);
961 * dev_ops for virtio, bare necessities for basic operation
963 static const struct eth_dev_ops virtio_eth_dev_ops = {
964 .dev_configure = virtio_dev_configure,
965 .dev_start = virtio_dev_start,
966 .dev_stop = virtio_dev_stop,
967 .dev_close = virtio_dev_close,
968 .promiscuous_enable = virtio_dev_promiscuous_enable,
969 .promiscuous_disable = virtio_dev_promiscuous_disable,
970 .allmulticast_enable = virtio_dev_allmulticast_enable,
971 .allmulticast_disable = virtio_dev_allmulticast_disable,
972 .mtu_set = virtio_mtu_set,
973 .dev_infos_get = virtio_dev_info_get,
974 .stats_get = virtio_dev_stats_get,
975 .xstats_get = virtio_dev_xstats_get,
976 .xstats_get_names = virtio_dev_xstats_get_names,
977 .stats_reset = virtio_dev_stats_reset,
978 .xstats_reset = virtio_dev_stats_reset,
979 .link_update = virtio_dev_link_update,
980 .vlan_offload_set = virtio_dev_vlan_offload_set,
981 .rx_queue_setup = virtio_dev_rx_queue_setup,
982 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
983 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
984 .rx_queue_release = virtio_dev_queue_release,
985 .tx_queue_setup = virtio_dev_tx_queue_setup,
986 .tx_queue_release = virtio_dev_queue_release,
987 /* collect stats per queue */
988 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
989 .vlan_filter_set = virtio_vlan_filter_set,
990 .mac_addr_add = virtio_mac_addr_add,
991 .mac_addr_remove = virtio_mac_addr_remove,
992 .mac_addr_set = virtio_mac_addr_set,
996 * dev_ops for virtio-user in secondary processes, as we just have
997 * some limited supports currently.
999 const struct eth_dev_ops virtio_user_secondary_eth_dev_ops = {
1000 .dev_infos_get = virtio_dev_info_get,
1001 .stats_get = virtio_dev_stats_get,
1002 .xstats_get = virtio_dev_xstats_get,
1003 .xstats_get_names = virtio_dev_xstats_get_names,
1004 .stats_reset = virtio_dev_stats_reset,
1005 .xstats_reset = virtio_dev_stats_reset,
1006 /* collect stats per queue */
1007 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
1011 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1015 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1016 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
1020 stats->opackets += txvq->stats.packets;
1021 stats->obytes += txvq->stats.bytes;
1023 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1024 stats->q_opackets[i] = txvq->stats.packets;
1025 stats->q_obytes[i] = txvq->stats.bytes;
1029 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1030 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1034 stats->ipackets += rxvq->stats.packets;
1035 stats->ibytes += rxvq->stats.bytes;
1036 stats->ierrors += rxvq->stats.errors;
1038 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1039 stats->q_ipackets[i] = rxvq->stats.packets;
1040 stats->q_ibytes[i] = rxvq->stats.bytes;
1044 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1047 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
1048 struct rte_eth_xstat_name *xstats_names,
1049 __rte_unused unsigned limit)
1055 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1056 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1058 if (xstats_names != NULL) {
1059 /* Note: limit checked in rte_eth_xstats_names() */
1061 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1062 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1065 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1066 snprintf(xstats_names[count].name,
1067 sizeof(xstats_names[count].name),
1069 rte_virtio_rxq_stat_strings[t].name);
1074 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1075 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1078 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1079 snprintf(xstats_names[count].name,
1080 sizeof(xstats_names[count].name),
1082 rte_virtio_txq_stat_strings[t].name);
1092 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1098 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1099 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1104 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1105 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1112 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1113 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1114 rte_virtio_rxq_stat_strings[t].offset);
1115 xstats[count].id = count;
1120 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1121 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1128 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1129 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1130 rte_virtio_txq_stat_strings[t].offset);
1131 xstats[count].id = count;
1140 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1142 virtio_update_stats(dev, stats);
1148 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1152 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1153 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1157 txvq->stats.packets = 0;
1158 txvq->stats.bytes = 0;
1159 txvq->stats.multicast = 0;
1160 txvq->stats.broadcast = 0;
1161 memset(txvq->stats.size_bins, 0,
1162 sizeof(txvq->stats.size_bins[0]) * 8);
1165 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1166 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1170 rxvq->stats.packets = 0;
1171 rxvq->stats.bytes = 0;
1172 rxvq->stats.errors = 0;
1173 rxvq->stats.multicast = 0;
1174 rxvq->stats.broadcast = 0;
1175 memset(rxvq->stats.size_bins, 0,
1176 sizeof(rxvq->stats.size_bins[0]) * 8);
1183 virtio_set_hwaddr(struct virtio_hw *hw)
1185 virtio_write_dev_config(hw,
1186 offsetof(struct virtio_net_config, mac),
1187 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1191 virtio_get_hwaddr(struct virtio_hw *hw)
1193 if (virtio_with_feature(hw, VIRTIO_NET_F_MAC)) {
1194 virtio_read_dev_config(hw,
1195 offsetof(struct virtio_net_config, mac),
1196 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1198 rte_eth_random_addr(&hw->mac_addr[0]);
1199 virtio_set_hwaddr(hw);
1204 virtio_mac_table_set(struct virtio_hw *hw,
1205 const struct virtio_net_ctrl_mac *uc,
1206 const struct virtio_net_ctrl_mac *mc)
1208 struct virtio_pmd_ctrl ctrl;
1211 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1212 PMD_DRV_LOG(INFO, "host does not support mac table");
1216 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1217 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1219 len[0] = uc->entries * RTE_ETHER_ADDR_LEN + sizeof(uc->entries);
1220 memcpy(ctrl.data, uc, len[0]);
1222 len[1] = mc->entries * RTE_ETHER_ADDR_LEN + sizeof(mc->entries);
1223 memcpy(ctrl.data + len[0], mc, len[1]);
1225 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1227 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1232 virtio_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1233 uint32_t index, uint32_t vmdq __rte_unused)
1235 struct virtio_hw *hw = dev->data->dev_private;
1236 const struct rte_ether_addr *addrs = dev->data->mac_addrs;
1238 struct virtio_net_ctrl_mac *uc, *mc;
1240 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1241 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1245 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1246 sizeof(uc->entries));
1248 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1249 sizeof(mc->entries));
1252 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1253 const struct rte_ether_addr *addr
1254 = (i == index) ? mac_addr : addrs + i;
1255 struct virtio_net_ctrl_mac *tbl
1256 = rte_is_multicast_ether_addr(addr) ? mc : uc;
1258 memcpy(&tbl->macs[tbl->entries++], addr, RTE_ETHER_ADDR_LEN);
1261 return virtio_mac_table_set(hw, uc, mc);
1265 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1267 struct virtio_hw *hw = dev->data->dev_private;
1268 struct rte_ether_addr *addrs = dev->data->mac_addrs;
1269 struct virtio_net_ctrl_mac *uc, *mc;
1272 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1273 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1277 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1278 sizeof(uc->entries));
1280 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1281 sizeof(mc->entries));
1284 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1285 struct virtio_net_ctrl_mac *tbl;
1287 if (i == index || rte_is_zero_ether_addr(addrs + i))
1290 tbl = rte_is_multicast_ether_addr(addrs + i) ? mc : uc;
1291 memcpy(&tbl->macs[tbl->entries++], addrs + i,
1292 RTE_ETHER_ADDR_LEN);
1295 virtio_mac_table_set(hw, uc, mc);
1299 virtio_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1301 struct virtio_hw *hw = dev->data->dev_private;
1303 memcpy(hw->mac_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1305 /* Use atomic update if available */
1306 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1307 struct virtio_pmd_ctrl ctrl;
1308 int len = RTE_ETHER_ADDR_LEN;
1310 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1311 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1313 memcpy(ctrl.data, mac_addr, RTE_ETHER_ADDR_LEN);
1314 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1317 if (!virtio_with_feature(hw, VIRTIO_NET_F_MAC))
1320 virtio_set_hwaddr(hw);
1325 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1327 struct virtio_hw *hw = dev->data->dev_private;
1328 struct virtio_pmd_ctrl ctrl;
1331 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1334 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1335 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1336 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1337 len = sizeof(vlan_id);
1339 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1343 virtio_intr_unmask(struct rte_eth_dev *dev)
1345 struct virtio_hw *hw = dev->data->dev_private;
1347 if (rte_intr_ack(dev->intr_handle) < 0)
1350 if (VIRTIO_OPS(hw)->intr_detect)
1351 VIRTIO_OPS(hw)->intr_detect(hw);
1357 virtio_intr_enable(struct rte_eth_dev *dev)
1359 struct virtio_hw *hw = dev->data->dev_private;
1361 if (rte_intr_enable(dev->intr_handle) < 0)
1364 if (VIRTIO_OPS(hw)->intr_detect)
1365 VIRTIO_OPS(hw)->intr_detect(hw);
1371 virtio_intr_disable(struct rte_eth_dev *dev)
1373 struct virtio_hw *hw = dev->data->dev_private;
1375 if (rte_intr_disable(dev->intr_handle) < 0)
1378 if (VIRTIO_OPS(hw)->intr_detect)
1379 VIRTIO_OPS(hw)->intr_detect(hw);
1385 virtio_ethdev_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1387 uint64_t host_features;
1389 /* Prepare guest_features: feature that driver wants to support */
1390 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1393 /* Read device(host) feature bits */
1394 host_features = VIRTIO_OPS(hw)->get_features(hw);
1395 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1398 /* If supported, ensure MTU value is valid before acknowledging it. */
1399 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1400 struct virtio_net_config config;
1402 virtio_read_dev_config(hw,
1403 offsetof(struct virtio_net_config, mtu),
1404 &config.mtu, sizeof(config.mtu));
1406 if (config.mtu < RTE_ETHER_MIN_MTU)
1407 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1411 * Negotiate features: Subset of device feature bits are written back
1412 * guest feature bits.
1414 hw->guest_features = req_features;
1415 hw->guest_features = virtio_negotiate_features(hw, host_features);
1416 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1417 hw->guest_features);
1419 if (VIRTIO_OPS(hw)->features_ok(hw) < 0)
1422 if (virtio_with_feature(hw, VIRTIO_F_VERSION_1)) {
1423 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1425 if (!(virtio_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1426 PMD_INIT_LOG(ERR, "Failed to set FEATURES_OK status!");
1431 hw->req_guest_features = req_features;
1437 virtio_dev_pause(struct rte_eth_dev *dev)
1439 struct virtio_hw *hw = dev->data->dev_private;
1441 rte_spinlock_lock(&hw->state_lock);
1443 if (hw->started == 0) {
1444 /* Device is just stopped. */
1445 rte_spinlock_unlock(&hw->state_lock);
1450 * Prevent the worker threads from touching queues to avoid contention,
1451 * 1 ms should be enough for the ongoing Tx function to finish.
1458 * Recover hw state to let the worker threads continue.
1461 virtio_dev_resume(struct rte_eth_dev *dev)
1463 struct virtio_hw *hw = dev->data->dev_private;
1466 rte_spinlock_unlock(&hw->state_lock);
1470 * Should be called only after device is paused.
1473 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1476 struct virtio_hw *hw = dev->data->dev_private;
1477 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1480 hw->inject_pkts = tx_pkts;
1481 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1482 hw->inject_pkts = NULL;
1488 virtio_notify_peers(struct rte_eth_dev *dev)
1490 struct virtio_hw *hw = dev->data->dev_private;
1491 struct virtnet_rx *rxvq;
1492 struct rte_mbuf *rarp_mbuf;
1494 if (!dev->data->rx_queues)
1497 rxvq = dev->data->rx_queues[0];
1501 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1502 (struct rte_ether_addr *)hw->mac_addr);
1503 if (rarp_mbuf == NULL) {
1504 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1508 /* If virtio port just stopped, no need to send RARP */
1509 if (virtio_dev_pause(dev) < 0) {
1510 rte_pktmbuf_free(rarp_mbuf);
1514 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1515 virtio_dev_resume(dev);
1519 virtio_ack_link_announce(struct rte_eth_dev *dev)
1521 struct virtio_hw *hw = dev->data->dev_private;
1522 struct virtio_pmd_ctrl ctrl;
1524 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1525 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1527 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1531 * Process virtio config changed interrupt. Call the callback
1532 * if link state changed, generate gratuitous RARP packet if
1533 * the status indicates an ANNOUNCE.
1536 virtio_interrupt_handler(void *param)
1538 struct rte_eth_dev *dev = param;
1539 struct virtio_hw *hw = dev->data->dev_private;
1543 /* Read interrupt status which clears interrupt */
1544 isr = virtio_get_isr(hw);
1545 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1547 if (virtio_intr_unmask(dev) < 0)
1548 PMD_DRV_LOG(ERR, "interrupt enable failed");
1550 if (isr & VIRTIO_ISR_CONFIG) {
1551 if (virtio_dev_link_update(dev, 0) == 0)
1552 rte_eth_dev_callback_process(dev,
1553 RTE_ETH_EVENT_INTR_LSC,
1556 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1557 virtio_read_dev_config(hw,
1558 offsetof(struct virtio_net_config, status),
1559 &status, sizeof(status));
1560 if (status & VIRTIO_NET_S_ANNOUNCE) {
1561 virtio_notify_peers(dev);
1563 virtio_ack_link_announce(dev);
1569 /* set rx and tx handlers according to what is supported */
1571 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1573 struct virtio_hw *hw = eth_dev->data->dev_private;
1575 eth_dev->tx_pkt_prepare = virtio_xmit_pkts_prepare;
1576 if (virtio_with_packed_queue(hw)) {
1578 "virtio: using packed ring %s Tx path on port %u",
1579 hw->use_vec_tx ? "vectorized" : "standard",
1580 eth_dev->data->port_id);
1582 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed_vec;
1584 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1586 if (hw->use_inorder_tx) {
1587 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1588 eth_dev->data->port_id);
1589 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1591 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1592 eth_dev->data->port_id);
1593 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1597 if (virtio_with_packed_queue(hw)) {
1598 if (hw->use_vec_rx) {
1600 "virtio: using packed ring vectorized Rx path on port %u",
1601 eth_dev->data->port_id);
1602 eth_dev->rx_pkt_burst =
1603 &virtio_recv_pkts_packed_vec;
1604 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1606 "virtio: using packed ring mergeable buffer Rx path on port %u",
1607 eth_dev->data->port_id);
1608 eth_dev->rx_pkt_burst =
1609 &virtio_recv_mergeable_pkts_packed;
1612 "virtio: using packed ring standard Rx path on port %u",
1613 eth_dev->data->port_id);
1614 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1617 if (hw->use_vec_rx) {
1618 PMD_INIT_LOG(INFO, "virtio: using vectorized Rx path on port %u",
1619 eth_dev->data->port_id);
1620 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1621 } else if (hw->use_inorder_rx) {
1623 "virtio: using inorder Rx path on port %u",
1624 eth_dev->data->port_id);
1625 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1626 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1628 "virtio: using mergeable buffer Rx path on port %u",
1629 eth_dev->data->port_id);
1630 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1632 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1633 eth_dev->data->port_id);
1634 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1640 /* Only support 1:1 queue/interrupt mapping so far.
1641 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1642 * interrupt vectors (<N+1).
1645 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1648 struct virtio_hw *hw = dev->data->dev_private;
1650 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1651 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1652 dev->intr_handle->intr_vec[i] = i + 1;
1653 if (VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1654 VIRTIO_MSI_NO_VECTOR) {
1655 PMD_DRV_LOG(ERR, "failed to set queue vector");
1664 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1667 struct virtio_hw *hw = dev->data->dev_private;
1669 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1670 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1671 VIRTIO_OPS(hw)->set_queue_irq(hw,
1672 hw->vqs[i * VTNET_CQ],
1673 VIRTIO_MSI_NO_VECTOR);
1677 virtio_configure_intr(struct rte_eth_dev *dev)
1679 struct virtio_hw *hw = dev->data->dev_private;
1681 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1682 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1686 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1687 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1691 if (!dev->intr_handle->intr_vec) {
1692 dev->intr_handle->intr_vec =
1693 rte_zmalloc("intr_vec",
1694 hw->max_queue_pairs * sizeof(int), 0);
1695 if (!dev->intr_handle->intr_vec) {
1696 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1697 hw->max_queue_pairs);
1702 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1703 /* Re-register callback to update max_intr */
1704 rte_intr_callback_unregister(dev->intr_handle,
1705 virtio_interrupt_handler,
1707 rte_intr_callback_register(dev->intr_handle,
1708 virtio_interrupt_handler,
1712 /* DO NOT try to remove this! This function will enable msix, or QEMU
1713 * will encounter SIGSEGV when DRIVER_OK is sent.
1714 * And for legacy devices, this should be done before queue/vec binding
1715 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1716 * (22) will be ignored.
1718 if (virtio_intr_enable(dev) < 0) {
1719 PMD_DRV_LOG(ERR, "interrupt enable failed");
1723 if (virtio_queues_bind_intr(dev) < 0) {
1724 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1730 #define DUPLEX_UNKNOWN 0xff
1731 /* reset device and renegotiate features if needed */
1733 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1735 struct virtio_hw *hw = eth_dev->data->dev_private;
1736 struct virtio_net_config *config;
1737 struct virtio_net_config local_config;
1740 /* Reset the device although not necessary at startup */
1744 virtio_dev_free_mbufs(eth_dev);
1745 virtio_free_queues(hw);
1748 /* Tell the host we've noticed this device. */
1749 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1751 /* Tell the host we've known how to drive the device. */
1752 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1753 if (virtio_ethdev_negotiate_features(hw, req_features) < 0)
1756 hw->weak_barriers = !virtio_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1758 /* If host does not support both status and MSI-X then disable LSC */
1759 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->intr_lsc)
1760 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1762 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1764 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1766 /* Setting up rx_header size for the device */
1767 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1768 virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
1769 virtio_with_packed_queue(hw))
1770 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1772 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1774 /* Copy the permanent MAC address to: virtio_hw */
1775 virtio_get_hwaddr(hw);
1776 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1777 ð_dev->data->mac_addrs[0]);
1779 "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
1780 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1781 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1783 if (hw->speed == ETH_SPEED_NUM_UNKNOWN) {
1784 if (virtio_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
1785 config = &local_config;
1786 virtio_read_dev_config(hw,
1787 offsetof(struct virtio_net_config, speed),
1788 &config->speed, sizeof(config->speed));
1789 virtio_read_dev_config(hw,
1790 offsetof(struct virtio_net_config, duplex),
1791 &config->duplex, sizeof(config->duplex));
1792 hw->speed = config->speed;
1793 hw->duplex = config->duplex;
1796 if (hw->duplex == DUPLEX_UNKNOWN)
1797 hw->duplex = ETH_LINK_FULL_DUPLEX;
1798 PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
1799 hw->speed, hw->duplex);
1800 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1801 config = &local_config;
1803 virtio_read_dev_config(hw,
1804 offsetof(struct virtio_net_config, mac),
1805 &config->mac, sizeof(config->mac));
1807 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1808 virtio_read_dev_config(hw,
1809 offsetof(struct virtio_net_config, status),
1810 &config->status, sizeof(config->status));
1813 "VIRTIO_NET_F_STATUS is not supported");
1817 if (virtio_with_feature(hw, VIRTIO_NET_F_MQ)) {
1818 virtio_read_dev_config(hw,
1819 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1820 &config->max_virtqueue_pairs,
1821 sizeof(config->max_virtqueue_pairs));
1824 "VIRTIO_NET_F_MQ is not supported");
1825 config->max_virtqueue_pairs = 1;
1828 hw->max_queue_pairs = config->max_virtqueue_pairs;
1830 if (virtio_with_feature(hw, VIRTIO_NET_F_MTU)) {
1831 virtio_read_dev_config(hw,
1832 offsetof(struct virtio_net_config, mtu),
1834 sizeof(config->mtu));
1837 * MTU value has already been checked at negotiation
1838 * time, but check again in case it has changed since
1839 * then, which should not happen.
1841 if (config->mtu < RTE_ETHER_MIN_MTU) {
1842 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1847 hw->max_mtu = config->mtu;
1848 /* Set initial MTU to maximum one supported by vhost */
1849 eth_dev->data->mtu = config->mtu;
1852 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1853 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1856 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1857 config->max_virtqueue_pairs);
1858 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1860 "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
1861 config->mac[0], config->mac[1],
1862 config->mac[2], config->mac[3],
1863 config->mac[4], config->mac[5]);
1865 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1866 hw->max_queue_pairs = 1;
1867 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1868 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1871 ret = virtio_alloc_queues(eth_dev);
1875 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1876 if (virtio_configure_intr(eth_dev) < 0) {
1877 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1878 virtio_free_queues(hw);
1883 virtio_reinit_complete(hw);
1889 * This function is based on probe() function in virtio_pci.c
1890 * It returns 0 on success.
1893 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1895 struct virtio_hw *hw = eth_dev->data->dev_private;
1896 uint32_t speed = ETH_SPEED_NUM_UNKNOWN;
1900 if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1902 "Not sufficient headroom required = %d, avail = %d",
1903 (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1904 RTE_PKTMBUF_HEADROOM);
1909 eth_dev->dev_ops = &virtio_eth_dev_ops;
1910 eth_dev->rx_descriptor_done = virtio_dev_rx_queue_done;
1912 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1913 set_rxtx_funcs(eth_dev);
1917 ret = virtio_dev_devargs_parse(eth_dev->device->devargs, &speed, &vectorized);
1921 hw->duplex = DUPLEX_UNKNOWN;
1923 /* Allocate memory for storing MAC addresses */
1924 eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1925 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1926 if (eth_dev->data->mac_addrs == NULL) {
1928 "Failed to allocate %d bytes needed to store MAC addresses",
1929 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1933 rte_spinlock_init(&hw->state_lock);
1935 /* reset device and negotiate default features */
1936 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1938 goto err_virtio_init;
1941 if (!virtio_with_packed_queue(hw)) {
1944 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
1949 "building environment do not support packed ring vectorized");
1959 rte_free(eth_dev->data->mac_addrs);
1960 eth_dev->data->mac_addrs = NULL;
1965 virtio_dev_speed_capa_get(uint32_t speed)
1968 case ETH_SPEED_NUM_10G:
1969 return ETH_LINK_SPEED_10G;
1970 case ETH_SPEED_NUM_20G:
1971 return ETH_LINK_SPEED_20G;
1972 case ETH_SPEED_NUM_25G:
1973 return ETH_LINK_SPEED_25G;
1974 case ETH_SPEED_NUM_40G:
1975 return ETH_LINK_SPEED_40G;
1976 case ETH_SPEED_NUM_50G:
1977 return ETH_LINK_SPEED_50G;
1978 case ETH_SPEED_NUM_56G:
1979 return ETH_LINK_SPEED_56G;
1980 case ETH_SPEED_NUM_100G:
1981 return ETH_LINK_SPEED_100G;
1982 case ETH_SPEED_NUM_200G:
1983 return ETH_LINK_SPEED_200G;
1989 static int vectorized_check_handler(__rte_unused const char *key,
1990 const char *value, void *ret_val)
1992 if (strcmp(value, "1") == 0)
1993 *(int *)ret_val = 1;
1995 *(int *)ret_val = 0;
2000 #define VIRTIO_ARG_SPEED "speed"
2001 #define VIRTIO_ARG_VECTORIZED "vectorized"
2004 link_speed_handler(const char *key __rte_unused,
2005 const char *value, void *ret_val)
2008 if (!value || !ret_val)
2010 val = strtoul(value, NULL, 0);
2011 /* validate input */
2012 if (virtio_dev_speed_capa_get(val) == 0)
2014 *(uint32_t *)ret_val = val;
2021 virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vectorized)
2023 struct rte_kvargs *kvlist;
2026 if (devargs == NULL)
2029 kvlist = rte_kvargs_parse(devargs->args, NULL);
2030 if (kvlist == NULL) {
2031 PMD_INIT_LOG(ERR, "error when parsing param");
2035 if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
2036 ret = rte_kvargs_process(kvlist,
2038 link_speed_handler, speed);
2040 PMD_INIT_LOG(ERR, "Failed to parse %s",
2047 rte_kvargs_count(kvlist, VIRTIO_ARG_VECTORIZED) == 1) {
2048 ret = rte_kvargs_process(kvlist,
2049 VIRTIO_ARG_VECTORIZED,
2050 vectorized_check_handler, vectorized);
2052 PMD_INIT_LOG(ERR, "Failed to parse %s",
2053 VIRTIO_ARG_VECTORIZED);
2059 rte_kvargs_free(kvlist);
2064 rx_offload_enabled(struct virtio_hw *hw)
2066 return virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
2067 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2068 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
2072 tx_offload_enabled(struct virtio_hw *hw)
2074 return virtio_with_feature(hw, VIRTIO_NET_F_CSUM) ||
2075 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
2076 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2080 * Configure virtio device
2081 * It returns 0 on success.
2084 virtio_dev_configure(struct rte_eth_dev *dev)
2086 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2087 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2088 struct virtio_hw *hw = dev->data->dev_private;
2089 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2091 uint64_t rx_offloads = rxmode->offloads;
2092 uint64_t tx_offloads = txmode->offloads;
2093 uint64_t req_features;
2096 PMD_INIT_LOG(DEBUG, "configure");
2097 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2099 if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2101 "Unsupported Rx multi queue mode %d",
2106 if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2108 "Unsupported Tx multi queue mode %d",
2113 if (dev->data->dev_conf.intr_conf.rxq) {
2114 ret = virtio_init_device(dev, hw->req_guest_features);
2119 if ((rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) &&
2120 (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len))
2121 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2123 if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
2124 hw->max_rx_pkt_len = rxmode->max_rx_pkt_len;
2126 hw->max_rx_pkt_len = ether_hdr_len + dev->data->mtu;
2128 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2129 DEV_RX_OFFLOAD_TCP_CKSUM))
2130 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2132 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2134 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2135 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2137 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2138 DEV_TX_OFFLOAD_TCP_CKSUM))
2139 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2141 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2143 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2144 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2146 /* if request features changed, reinit the device */
2147 if (req_features != hw->req_guest_features) {
2148 ret = virtio_init_device(dev, req_features);
2153 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2154 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2155 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2157 "rx checksum not available on this host");
2161 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2162 (!virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2163 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2165 "Large Receive Offload not available on this host");
2169 /* start control queue */
2170 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2171 virtio_dev_cq_start(dev);
2173 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2176 hw->rx_ol_scatter = (rx_offloads & DEV_RX_OFFLOAD_SCATTER);
2178 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2179 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2181 "vlan filtering not available on this host");
2185 hw->has_tx_offload = tx_offload_enabled(hw);
2186 hw->has_rx_offload = rx_offload_enabled(hw);
2188 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2189 /* Enable vector (0) for Link State Intrerrupt */
2190 if (VIRTIO_OPS(hw)->set_config_irq(hw, 0) ==
2191 VIRTIO_MSI_NO_VECTOR) {
2192 PMD_DRV_LOG(ERR, "failed to set config vector");
2196 if (virtio_with_packed_queue(hw)) {
2197 #if defined(RTE_ARCH_X86_64) && defined(CC_AVX512_SUPPORT)
2198 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2199 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) ||
2200 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2201 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2202 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)) {
2204 "disabled packed ring vectorized path for requirements not met");
2208 #elif defined(RTE_ARCH_ARM)
2209 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2210 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) ||
2211 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2212 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2213 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)) {
2215 "disabled packed ring vectorized path for requirements not met");
2224 if (hw->use_vec_rx) {
2225 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2227 "disabled packed ring vectorized rx for mrg_rxbuf enabled");
2231 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) {
2233 "disabled packed ring vectorized rx for TCP_LRO enabled");
2238 if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2239 hw->use_inorder_tx = 1;
2240 hw->use_inorder_rx = 1;
2244 if (hw->use_vec_rx) {
2245 #if defined RTE_ARCH_ARM
2246 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2248 "disabled split ring vectorized path for requirement not met");
2252 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2254 "disabled split ring vectorized rx for mrg_rxbuf enabled");
2258 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2259 DEV_RX_OFFLOAD_TCP_CKSUM |
2260 DEV_RX_OFFLOAD_TCP_LRO |
2261 DEV_RX_OFFLOAD_VLAN_STRIP)) {
2263 "disabled split ring vectorized rx for offloading enabled");
2267 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128) {
2269 "disabled split ring vectorized rx, max SIMD bitwidth too low");
2280 virtio_dev_start(struct rte_eth_dev *dev)
2282 uint16_t nb_queues, i;
2283 struct virtqueue *vq;
2284 struct virtio_hw *hw = dev->data->dev_private;
2287 /* Finish the initialization of the queues */
2288 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2289 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2293 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2294 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2299 /* check if lsc interrupt feature is enabled */
2300 if (dev->data->dev_conf.intr_conf.lsc) {
2301 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2302 PMD_DRV_LOG(ERR, "link status not supported by host");
2307 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2308 * in device configure, but it could be unmapped when device is
2311 if (dev->data->dev_conf.intr_conf.lsc ||
2312 dev->data->dev_conf.intr_conf.rxq) {
2313 virtio_intr_disable(dev);
2315 /* Setup interrupt callback */
2316 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2317 rte_intr_callback_register(dev->intr_handle,
2318 virtio_interrupt_handler,
2321 if (virtio_intr_enable(dev) < 0) {
2322 PMD_DRV_LOG(ERR, "interrupt enable failed");
2327 /*Notify the backend
2328 *Otherwise the tap backend might already stop its queue due to fullness.
2329 *vhost backend will have no chance to be waked up
2331 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2332 if (hw->max_queue_pairs > 1) {
2333 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2337 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2339 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2340 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2341 /* Flush the old packets */
2342 virtqueue_rxvq_flush(vq);
2343 virtqueue_notify(vq);
2346 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2347 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2348 virtqueue_notify(vq);
2351 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2353 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2354 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2358 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2359 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2363 set_rxtx_funcs(dev);
2366 /* Initialize Link state */
2367 virtio_dev_link_update(dev, 0);
2372 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2374 struct virtio_hw *hw = dev->data->dev_private;
2375 uint16_t nr_vq = virtio_get_nr_vq(hw);
2376 const char *type __rte_unused;
2377 unsigned int i, mbuf_num = 0;
2378 struct virtqueue *vq;
2379 struct rte_mbuf *buf;
2382 if (hw->vqs == NULL)
2385 for (i = 0; i < nr_vq; i++) {
2390 queue_type = virtio_get_queue_type(hw, i);
2391 if (queue_type == VTNET_RQ)
2393 else if (queue_type == VTNET_TQ)
2399 "Before freeing %s[%d] used and unused buf",
2403 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2404 rte_pktmbuf_free(buf);
2409 "After freeing %s[%d] used and unused buf",
2414 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2418 virtio_tx_completed_cleanup(struct rte_eth_dev *dev)
2420 struct virtio_hw *hw = dev->data->dev_private;
2421 struct virtqueue *vq;
2423 void (*xmit_cleanup)(struct virtqueue *vq, uint16_t nb_used);
2425 if (virtio_with_packed_queue(hw)) {
2427 xmit_cleanup = &virtio_xmit_cleanup_inorder_packed;
2428 else if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER))
2429 xmit_cleanup = &virtio_xmit_cleanup_inorder_packed;
2431 xmit_cleanup = &virtio_xmit_cleanup_normal_packed;
2433 if (hw->use_inorder_tx)
2434 xmit_cleanup = &virtio_xmit_cleanup_inorder;
2436 xmit_cleanup = &virtio_xmit_cleanup;
2439 for (qidx = 0; qidx < hw->max_queue_pairs; qidx++) {
2440 vq = hw->vqs[2 * qidx + VTNET_SQ_TQ_QUEUE_IDX];
2442 xmit_cleanup(vq, virtqueue_nused(vq));
2447 * Stop device: disable interrupt and mark link down
2450 virtio_dev_stop(struct rte_eth_dev *dev)
2452 struct virtio_hw *hw = dev->data->dev_private;
2453 struct rte_eth_link link;
2454 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2456 PMD_INIT_LOG(DEBUG, "stop");
2457 dev->data->dev_started = 0;
2459 rte_spinlock_lock(&hw->state_lock);
2464 virtio_tx_completed_cleanup(dev);
2466 if (intr_conf->lsc || intr_conf->rxq) {
2467 virtio_intr_disable(dev);
2469 /* Reset interrupt callback */
2470 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2471 rte_intr_callback_unregister(dev->intr_handle,
2472 virtio_interrupt_handler,
2477 memset(&link, 0, sizeof(link));
2478 rte_eth_linkstatus_set(dev, &link);
2480 rte_spinlock_unlock(&hw->state_lock);
2486 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2488 struct rte_eth_link link;
2490 struct virtio_hw *hw = dev->data->dev_private;
2492 memset(&link, 0, sizeof(link));
2493 link.link_duplex = hw->duplex;
2494 link.link_speed = hw->speed;
2495 link.link_autoneg = ETH_LINK_AUTONEG;
2498 link.link_status = ETH_LINK_DOWN;
2499 link.link_speed = ETH_SPEED_NUM_NONE;
2500 } else if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2501 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2502 virtio_read_dev_config(hw,
2503 offsetof(struct virtio_net_config, status),
2504 &status, sizeof(status));
2505 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2506 link.link_status = ETH_LINK_DOWN;
2507 link.link_speed = ETH_SPEED_NUM_NONE;
2508 PMD_INIT_LOG(DEBUG, "Port %d is down",
2509 dev->data->port_id);
2511 link.link_status = ETH_LINK_UP;
2512 PMD_INIT_LOG(DEBUG, "Port %d is up",
2513 dev->data->port_id);
2516 link.link_status = ETH_LINK_UP;
2519 return rte_eth_linkstatus_set(dev, &link);
2523 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2525 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2526 struct virtio_hw *hw = dev->data->dev_private;
2527 uint64_t offloads = rxmode->offloads;
2529 if (mask & ETH_VLAN_FILTER_MASK) {
2530 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2531 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2534 "vlan filtering not available on this host");
2540 if (mask & ETH_VLAN_STRIP_MASK)
2541 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2547 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2549 uint64_t tso_mask, host_features;
2550 struct virtio_hw *hw = dev->data->dev_private;
2551 dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2553 dev_info->max_rx_queues =
2554 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2555 dev_info->max_tx_queues =
2556 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2557 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2558 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2559 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2560 dev_info->max_mtu = hw->max_mtu;
2562 host_features = VIRTIO_OPS(hw)->get_features(hw);
2563 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2564 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2565 if (host_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))
2566 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_SCATTER;
2567 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2568 dev_info->rx_offload_capa |=
2569 DEV_RX_OFFLOAD_TCP_CKSUM |
2570 DEV_RX_OFFLOAD_UDP_CKSUM;
2572 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2573 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2574 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2575 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2576 if ((host_features & tso_mask) == tso_mask)
2577 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2579 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2580 DEV_TX_OFFLOAD_VLAN_INSERT;
2581 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2582 dev_info->tx_offload_capa |=
2583 DEV_TX_OFFLOAD_UDP_CKSUM |
2584 DEV_TX_OFFLOAD_TCP_CKSUM;
2586 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2587 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2588 if ((host_features & tso_mask) == tso_mask)
2589 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2591 if (host_features & (1ULL << VIRTIO_F_RING_PACKED)) {
2593 * According to 2.7 Packed Virtqueues,
2594 * 2.7.10.1 Structure Size and Alignment:
2595 * The Queue Size value does not have to be a power of 2.
2597 dev_info->rx_desc_lim.nb_max = UINT16_MAX;
2598 dev_info->tx_desc_lim.nb_max = UINT16_MAX;
2601 * According to 2.6 Split Virtqueues:
2602 * Queue Size value is always a power of 2. The maximum Queue
2603 * Size value is 32768.
2605 dev_info->rx_desc_lim.nb_max = 32768;
2606 dev_info->tx_desc_lim.nb_max = 32768;
2609 * Actual minimum is not the same for virtqueues of different kinds,
2610 * but to avoid tangling the code with separate branches, rely on
2611 * default thresholds since desc number must be at least of their size.
2613 dev_info->rx_desc_lim.nb_min = RTE_MAX(DEFAULT_RX_FREE_THRESH,
2614 RTE_VIRTIO_VPMD_RX_REARM_THRESH);
2615 dev_info->tx_desc_lim.nb_min = DEFAULT_TX_FREE_THRESH;
2616 dev_info->rx_desc_lim.nb_align = 1;
2617 dev_info->tx_desc_lim.nb_align = 1;
2623 * It enables testpmd to collect per queue stats.
2626 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2627 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2628 __rte_unused uint8_t is_rx)
2633 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_init, init, NOTICE);
2634 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_driver, driver, NOTICE);