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_user/virtio_user_dev.h"
38 static int virtio_dev_configure(struct rte_eth_dev *dev);
39 static int virtio_dev_start(struct rte_eth_dev *dev);
40 static int virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
41 static int virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
42 static int virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
43 static int virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
44 static uint32_t virtio_dev_speed_capa_get(uint32_t speed);
45 static int virtio_dev_devargs_parse(struct rte_devargs *devargs,
48 static int virtio_dev_info_get(struct rte_eth_dev *dev,
49 struct rte_eth_dev_info *dev_info);
50 static int virtio_dev_link_update(struct rte_eth_dev *dev,
51 int wait_to_complete);
52 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
54 static void virtio_set_hwaddr(struct virtio_hw *hw);
55 static void virtio_get_hwaddr(struct virtio_hw *hw);
57 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
58 struct rte_eth_stats *stats);
59 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
60 struct rte_eth_xstat *xstats, unsigned n);
61 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
62 struct rte_eth_xstat_name *xstats_names,
64 static int virtio_dev_stats_reset(struct rte_eth_dev *dev);
65 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
66 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
67 uint16_t vlan_id, int on);
68 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
69 struct rte_ether_addr *mac_addr,
70 uint32_t index, uint32_t vmdq);
71 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
72 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
73 struct rte_ether_addr *mac_addr);
75 static int virtio_intr_disable(struct rte_eth_dev *dev);
77 static int virtio_dev_queue_stats_mapping_set(
78 struct rte_eth_dev *eth_dev,
83 static void virtio_notify_peers(struct rte_eth_dev *dev);
84 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
86 struct rte_virtio_xstats_name_off {
87 char name[RTE_ETH_XSTATS_NAME_SIZE];
91 /* [rt]x_qX_ is prepended to the name string here */
92 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
93 {"good_packets", offsetof(struct virtnet_rx, stats.packets)},
94 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)},
95 {"errors", offsetof(struct virtnet_rx, stats.errors)},
96 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)},
97 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)},
98 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])},
99 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])},
100 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])},
101 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])},
102 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])},
103 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])},
104 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
105 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])},
108 /* [rt]x_qX_ is prepended to the name string here */
109 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
110 {"good_packets", offsetof(struct virtnet_tx, stats.packets)},
111 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)},
112 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)},
113 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)},
114 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])},
115 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])},
116 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])},
117 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])},
118 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])},
119 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])},
120 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
121 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])},
124 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
125 sizeof(rte_virtio_rxq_stat_strings[0]))
126 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
127 sizeof(rte_virtio_txq_stat_strings[0]))
129 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
131 static struct virtio_pmd_ctrl *
132 virtio_send_command_packed(struct virtnet_ctl *cvq,
133 struct virtio_pmd_ctrl *ctrl,
134 int *dlen, int pkt_num)
136 struct virtqueue *vq = virtnet_cq_to_vq(cvq);
138 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
139 struct virtio_pmd_ctrl *result;
146 * Format is enforced in qemu code:
147 * One TX packet for header;
148 * At least one TX packet per argument;
149 * One RX packet for ACK.
151 head = vq->vq_avail_idx;
152 flags = vq->vq_packed.cached_flags;
153 desc[head].addr = cvq->virtio_net_hdr_mem;
154 desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
157 if (++vq->vq_avail_idx >= vq->vq_nentries) {
158 vq->vq_avail_idx -= vq->vq_nentries;
159 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
162 for (k = 0; k < pkt_num; k++) {
163 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
164 + sizeof(struct virtio_net_ctrl_hdr)
165 + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
166 desc[vq->vq_avail_idx].len = dlen[k];
167 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
168 vq->vq_packed.cached_flags;
172 if (++vq->vq_avail_idx >= vq->vq_nentries) {
173 vq->vq_avail_idx -= vq->vq_nentries;
174 vq->vq_packed.cached_flags ^=
175 VRING_PACKED_DESC_F_AVAIL_USED;
179 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
180 + sizeof(struct virtio_net_ctrl_hdr);
181 desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
182 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
183 vq->vq_packed.cached_flags;
186 if (++vq->vq_avail_idx >= vq->vq_nentries) {
187 vq->vq_avail_idx -= vq->vq_nentries;
188 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
191 virtqueue_store_flags_packed(&desc[head], VRING_DESC_F_NEXT | flags,
192 vq->hw->weak_barriers);
194 virtio_wmb(vq->hw->weak_barriers);
195 virtqueue_notify(vq);
197 /* wait for used desc in virtqueue
198 * desc_is_used has a load-acquire or rte_io_rmb inside
200 while (!desc_is_used(&desc[head], vq))
203 /* now get used descriptors */
204 vq->vq_free_cnt += nb_descs;
205 vq->vq_used_cons_idx += nb_descs;
206 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
207 vq->vq_used_cons_idx -= vq->vq_nentries;
208 vq->vq_packed.used_wrap_counter ^= 1;
211 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
212 "vq->vq_avail_idx=%d\n"
213 "vq->vq_used_cons_idx=%d\n"
214 "vq->vq_packed.cached_flags=0x%x\n"
215 "vq->vq_packed.used_wrap_counter=%d\n",
218 vq->vq_used_cons_idx,
219 vq->vq_packed.cached_flags,
220 vq->vq_packed.used_wrap_counter);
222 result = cvq->virtio_net_hdr_mz->addr;
226 static struct virtio_pmd_ctrl *
227 virtio_send_command_split(struct virtnet_ctl *cvq,
228 struct virtio_pmd_ctrl *ctrl,
229 int *dlen, int pkt_num)
231 struct virtio_pmd_ctrl *result;
232 struct virtqueue *vq = virtnet_cq_to_vq(cvq);
236 head = vq->vq_desc_head_idx;
239 * Format is enforced in qemu code:
240 * One TX packet for header;
241 * At least one TX packet per argument;
242 * One RX packet for ACK.
244 vq->vq_split.ring.desc[head].flags = VRING_DESC_F_NEXT;
245 vq->vq_split.ring.desc[head].addr = cvq->virtio_net_hdr_mem;
246 vq->vq_split.ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
248 i = vq->vq_split.ring.desc[head].next;
250 for (k = 0; k < pkt_num; k++) {
251 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_NEXT;
252 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
253 + sizeof(struct virtio_net_ctrl_hdr)
254 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
255 vq->vq_split.ring.desc[i].len = dlen[k];
258 i = vq->vq_split.ring.desc[i].next;
261 vq->vq_split.ring.desc[i].flags = VRING_DESC_F_WRITE;
262 vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
263 + sizeof(struct virtio_net_ctrl_hdr);
264 vq->vq_split.ring.desc[i].len = sizeof(ctrl->status);
267 vq->vq_desc_head_idx = vq->vq_split.ring.desc[i].next;
269 vq_update_avail_ring(vq, head);
270 vq_update_avail_idx(vq);
272 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
274 virtqueue_notify(vq);
276 while (virtqueue_nused(vq) == 0)
279 while (virtqueue_nused(vq)) {
280 uint32_t idx, desc_idx, used_idx;
281 struct vring_used_elem *uep;
283 used_idx = (uint32_t)(vq->vq_used_cons_idx
284 & (vq->vq_nentries - 1));
285 uep = &vq->vq_split.ring.used->ring[used_idx];
286 idx = (uint32_t) uep->id;
289 while (vq->vq_split.ring.desc[desc_idx].flags &
291 desc_idx = vq->vq_split.ring.desc[desc_idx].next;
295 vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
296 vq->vq_desc_head_idx = idx;
298 vq->vq_used_cons_idx++;
302 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
303 vq->vq_free_cnt, vq->vq_desc_head_idx);
305 result = cvq->virtio_net_hdr_mz->addr;
310 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
311 int *dlen, int pkt_num)
313 virtio_net_ctrl_ack status = ~0;
314 struct virtio_pmd_ctrl *result;
315 struct virtqueue *vq;
317 ctrl->status = status;
320 PMD_INIT_LOG(ERR, "Control queue is not supported.");
324 rte_spinlock_lock(&cvq->lock);
325 vq = virtnet_cq_to_vq(cvq);
327 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
328 "vq->hw->cvq = %p vq = %p",
329 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
331 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
332 rte_spinlock_unlock(&cvq->lock);
336 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
337 sizeof(struct virtio_pmd_ctrl));
339 if (virtio_with_packed_queue(vq->hw))
340 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
342 result = virtio_send_command_split(cvq, ctrl, dlen, pkt_num);
344 rte_spinlock_unlock(&cvq->lock);
345 return result->status;
349 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
351 struct virtio_hw *hw = dev->data->dev_private;
352 struct virtio_pmd_ctrl ctrl;
356 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
357 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
358 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
360 dlen[0] = sizeof(uint16_t);
362 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
364 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
365 "failed, this is too late now...");
373 virtio_dev_queue_release(void *queue __rte_unused)
379 virtio_get_nr_vq(struct virtio_hw *hw)
381 uint16_t nr_vq = hw->max_queue_pairs * 2;
383 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
390 virtio_init_vring(struct virtqueue *vq)
392 int size = vq->vq_nentries;
393 uint8_t *ring_mem = vq->vq_ring_virt_mem;
395 PMD_INIT_FUNC_TRACE();
397 memset(ring_mem, 0, vq->vq_ring_size);
399 vq->vq_used_cons_idx = 0;
400 vq->vq_desc_head_idx = 0;
401 vq->vq_avail_idx = 0;
402 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
403 vq->vq_free_cnt = vq->vq_nentries;
404 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
405 if (virtio_with_packed_queue(vq->hw)) {
406 vring_init_packed(&vq->vq_packed.ring, ring_mem,
407 VIRTIO_VRING_ALIGN, size);
408 vring_desc_init_packed(vq, size);
410 struct vring *vr = &vq->vq_split.ring;
412 vring_init_split(vr, ring_mem, VIRTIO_VRING_ALIGN, size);
413 vring_desc_init_split(vr->desc, size);
416 * Disable device(host) interrupting guest
418 virtqueue_disable_intr(vq);
422 virtio_init_queue(struct rte_eth_dev *dev, uint16_t queue_idx)
424 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
425 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
426 const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
427 unsigned int vq_size, size;
428 struct virtio_hw *hw = dev->data->dev_private;
429 struct virtnet_rx *rxvq = NULL;
430 struct virtnet_tx *txvq = NULL;
431 struct virtnet_ctl *cvq = NULL;
432 struct virtqueue *vq;
433 size_t sz_hdr_mz = 0;
434 void *sw_ring = NULL;
435 int queue_type = virtio_get_queue_type(hw, queue_idx);
437 int numa_node = dev->device->numa_node;
438 struct rte_mbuf *fake_mbuf = NULL;
440 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
441 queue_idx, numa_node);
444 * Read the virtqueue size from the Queue Size field
445 * Always power of 2 and if 0 virtqueue does not exist
447 vq_size = VIRTIO_OPS(hw)->get_queue_num(hw, queue_idx);
448 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
450 PMD_INIT_LOG(ERR, "virtqueue does not exist");
454 if (!virtio_with_packed_queue(hw) && !rte_is_power_of_2(vq_size)) {
455 PMD_INIT_LOG(ERR, "split virtqueue size is not power of 2");
459 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
460 dev->data->port_id, queue_idx);
462 size = RTE_ALIGN_CEIL(sizeof(*vq) +
463 vq_size * sizeof(struct vq_desc_extra),
464 RTE_CACHE_LINE_SIZE);
465 if (queue_type == VTNET_TQ) {
467 * For each xmit packet, allocate a virtio_net_hdr
468 * and indirect ring elements
470 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
471 } else if (queue_type == VTNET_CQ) {
472 /* Allocate a page for control vq command, data and status */
473 sz_hdr_mz = rte_mem_page_size();
476 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
479 PMD_INIT_LOG(ERR, "can not allocate vq");
482 hw->vqs[queue_idx] = vq;
485 vq->vq_queue_index = queue_idx;
486 vq->vq_nentries = vq_size;
487 if (virtio_with_packed_queue(hw)) {
488 vq->vq_packed.used_wrap_counter = 1;
489 vq->vq_packed.cached_flags = VRING_PACKED_DESC_F_AVAIL;
490 vq->vq_packed.event_flags_shadow = 0;
491 if (queue_type == VTNET_RQ)
492 vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
496 * Reserve a memzone for vring elements
498 size = vring_size(hw, vq_size, VIRTIO_VRING_ALIGN);
499 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_VRING_ALIGN);
500 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
501 size, vq->vq_ring_size);
503 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
504 numa_node, RTE_MEMZONE_IOVA_CONTIG,
507 if (rte_errno == EEXIST)
508 mz = rte_memzone_lookup(vq_name);
515 memset(mz->addr, 0, mz->len);
517 vq->vq_ring_mem = mz->iova;
518 vq->vq_ring_virt_mem = mz->addr;
519 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
521 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
522 (uint64_t)(uintptr_t)mz->addr);
524 virtio_init_vring(vq);
527 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
528 dev->data->port_id, queue_idx);
529 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
530 numa_node, RTE_MEMZONE_IOVA_CONTIG,
531 RTE_CACHE_LINE_SIZE);
532 if (hdr_mz == NULL) {
533 if (rte_errno == EEXIST)
534 hdr_mz = rte_memzone_lookup(vq_hdr_name);
535 if (hdr_mz == NULL) {
542 if (queue_type == VTNET_RQ) {
543 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
544 sizeof(vq->sw_ring[0]);
546 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
547 RTE_CACHE_LINE_SIZE, numa_node);
549 PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
554 fake_mbuf = rte_zmalloc_socket("sw_ring", sizeof(*fake_mbuf),
555 RTE_CACHE_LINE_SIZE, numa_node);
557 PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
562 vq->sw_ring = sw_ring;
564 rxvq->port_id = dev->data->port_id;
566 rxvq->fake_mbuf = fake_mbuf;
567 } else if (queue_type == VTNET_TQ) {
569 txvq->port_id = dev->data->port_id;
571 txvq->virtio_net_hdr_mz = hdr_mz;
572 txvq->virtio_net_hdr_mem = hdr_mz->iova;
573 } else if (queue_type == VTNET_CQ) {
576 cvq->virtio_net_hdr_mz = hdr_mz;
577 cvq->virtio_net_hdr_mem = hdr_mz->iova;
578 memset(cvq->virtio_net_hdr_mz->addr, 0, rte_mem_page_size());
583 if (queue_type == VTNET_TQ) {
584 struct virtio_tx_region *txr;
588 memset(txr, 0, vq_size * sizeof(*txr));
589 for (i = 0; i < vq_size; i++) {
590 /* first indirect descriptor is always the tx header */
591 if (!virtio_with_packed_queue(hw)) {
592 struct vring_desc *start_dp = txr[i].tx_indir;
593 vring_desc_init_split(start_dp,
594 RTE_DIM(txr[i].tx_indir));
595 start_dp->addr = txvq->virtio_net_hdr_mem
597 + offsetof(struct virtio_tx_region,
599 start_dp->len = hw->vtnet_hdr_size;
600 start_dp->flags = VRING_DESC_F_NEXT;
602 struct vring_packed_desc *start_dp =
603 txr[i].tx_packed_indir;
604 vring_desc_init_indirect_packed(start_dp,
605 RTE_DIM(txr[i].tx_packed_indir));
606 start_dp->addr = txvq->virtio_net_hdr_mem
608 + offsetof(struct virtio_tx_region,
610 start_dp->len = hw->vtnet_hdr_size;
615 if (VIRTIO_OPS(hw)->setup_queue(hw, vq) < 0) {
616 PMD_INIT_LOG(ERR, "setup_queue failed");
629 rte_memzone_free(hdr_mz);
631 rte_memzone_free(mz);
639 virtio_free_queues(struct virtio_hw *hw)
641 uint16_t nr_vq = virtio_get_nr_vq(hw);
642 struct virtqueue *vq;
649 for (i = 0; i < nr_vq; i++) {
654 queue_type = virtio_get_queue_type(hw, i);
655 if (queue_type == VTNET_RQ) {
656 rte_free(vq->rxq.fake_mbuf);
657 rte_free(vq->sw_ring);
658 rte_memzone_free(vq->rxq.mz);
659 } else if (queue_type == VTNET_TQ) {
660 rte_memzone_free(vq->txq.mz);
661 rte_memzone_free(vq->txq.virtio_net_hdr_mz);
663 rte_memzone_free(vq->cq.mz);
664 rte_memzone_free(vq->cq.virtio_net_hdr_mz);
676 virtio_alloc_queues(struct rte_eth_dev *dev)
678 struct virtio_hw *hw = dev->data->dev_private;
679 uint16_t nr_vq = virtio_get_nr_vq(hw);
683 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
685 PMD_INIT_LOG(ERR, "failed to allocate vqs");
689 for (i = 0; i < nr_vq; i++) {
690 ret = virtio_init_queue(dev, i);
692 virtio_free_queues(hw);
700 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
703 virtio_dev_close(struct rte_eth_dev *dev)
705 struct virtio_hw *hw = dev->data->dev_private;
706 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
708 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
709 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
717 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
718 VIRTIO_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
720 virtio_queues_unbind_intr(dev);
722 if (intr_conf->lsc || intr_conf->rxq) {
723 virtio_intr_disable(dev);
724 rte_intr_efd_disable(dev->intr_handle);
725 rte_free(dev->intr_handle->intr_vec);
726 dev->intr_handle->intr_vec = NULL;
730 virtio_dev_free_mbufs(dev);
731 virtio_free_queues(hw);
733 return VIRTIO_OPS(hw)->dev_close(hw);
737 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
739 struct virtio_hw *hw = dev->data->dev_private;
740 struct virtio_pmd_ctrl ctrl;
744 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
745 PMD_INIT_LOG(INFO, "host does not support rx control");
749 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
750 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
754 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
756 PMD_INIT_LOG(ERR, "Failed to enable promisc");
764 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
766 struct virtio_hw *hw = dev->data->dev_private;
767 struct virtio_pmd_ctrl ctrl;
771 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
772 PMD_INIT_LOG(INFO, "host does not support rx control");
776 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
777 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
781 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
783 PMD_INIT_LOG(ERR, "Failed to disable promisc");
791 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
793 struct virtio_hw *hw = dev->data->dev_private;
794 struct virtio_pmd_ctrl ctrl;
798 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
799 PMD_INIT_LOG(INFO, "host does not support rx control");
803 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
804 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
808 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
810 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
818 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
820 struct virtio_hw *hw = dev->data->dev_private;
821 struct virtio_pmd_ctrl ctrl;
825 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
826 PMD_INIT_LOG(INFO, "host does not support rx control");
830 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
831 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
835 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
837 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
844 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */
846 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
848 struct virtio_hw *hw = dev->data->dev_private;
849 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
851 uint32_t frame_size = mtu + ether_hdr_len;
852 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
854 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
856 if (mtu < RTE_ETHER_MIN_MTU || frame_size > max_frame_size) {
857 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
858 RTE_ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
865 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
867 struct virtio_hw *hw = dev->data->dev_private;
868 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
869 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
871 virtqueue_enable_intr(vq);
872 virtio_mb(hw->weak_barriers);
877 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
879 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
880 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
882 virtqueue_disable_intr(vq);
887 * dev_ops for virtio, bare necessities for basic operation
889 static const struct eth_dev_ops virtio_eth_dev_ops = {
890 .dev_configure = virtio_dev_configure,
891 .dev_start = virtio_dev_start,
892 .dev_stop = virtio_dev_stop,
893 .dev_close = virtio_dev_close,
894 .promiscuous_enable = virtio_dev_promiscuous_enable,
895 .promiscuous_disable = virtio_dev_promiscuous_disable,
896 .allmulticast_enable = virtio_dev_allmulticast_enable,
897 .allmulticast_disable = virtio_dev_allmulticast_disable,
898 .mtu_set = virtio_mtu_set,
899 .dev_infos_get = virtio_dev_info_get,
900 .stats_get = virtio_dev_stats_get,
901 .xstats_get = virtio_dev_xstats_get,
902 .xstats_get_names = virtio_dev_xstats_get_names,
903 .stats_reset = virtio_dev_stats_reset,
904 .xstats_reset = virtio_dev_stats_reset,
905 .link_update = virtio_dev_link_update,
906 .vlan_offload_set = virtio_dev_vlan_offload_set,
907 .rx_queue_setup = virtio_dev_rx_queue_setup,
908 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable,
909 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable,
910 .rx_queue_release = virtio_dev_queue_release,
911 .tx_queue_setup = virtio_dev_tx_queue_setup,
912 .tx_queue_release = virtio_dev_queue_release,
913 /* collect stats per queue */
914 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
915 .vlan_filter_set = virtio_vlan_filter_set,
916 .mac_addr_add = virtio_mac_addr_add,
917 .mac_addr_remove = virtio_mac_addr_remove,
918 .mac_addr_set = virtio_mac_addr_set,
922 * dev_ops for virtio-user in secondary processes, as we just have
923 * some limited supports currently.
925 const struct eth_dev_ops virtio_user_secondary_eth_dev_ops = {
926 .dev_infos_get = virtio_dev_info_get,
927 .stats_get = virtio_dev_stats_get,
928 .xstats_get = virtio_dev_xstats_get,
929 .xstats_get_names = virtio_dev_xstats_get_names,
930 .stats_reset = virtio_dev_stats_reset,
931 .xstats_reset = virtio_dev_stats_reset,
932 /* collect stats per queue */
933 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
937 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
941 for (i = 0; i < dev->data->nb_tx_queues; i++) {
942 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
946 stats->opackets += txvq->stats.packets;
947 stats->obytes += txvq->stats.bytes;
949 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
950 stats->q_opackets[i] = txvq->stats.packets;
951 stats->q_obytes[i] = txvq->stats.bytes;
955 for (i = 0; i < dev->data->nb_rx_queues; i++) {
956 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
960 stats->ipackets += rxvq->stats.packets;
961 stats->ibytes += rxvq->stats.bytes;
962 stats->ierrors += rxvq->stats.errors;
964 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
965 stats->q_ipackets[i] = rxvq->stats.packets;
966 stats->q_ibytes[i] = rxvq->stats.bytes;
970 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
973 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
974 struct rte_eth_xstat_name *xstats_names,
975 __rte_unused unsigned limit)
981 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
982 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
984 if (xstats_names != NULL) {
985 /* Note: limit checked in rte_eth_xstats_names() */
987 for (i = 0; i < dev->data->nb_rx_queues; i++) {
988 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
991 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
992 snprintf(xstats_names[count].name,
993 sizeof(xstats_names[count].name),
995 rte_virtio_rxq_stat_strings[t].name);
1000 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1001 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1004 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1005 snprintf(xstats_names[count].name,
1006 sizeof(xstats_names[count].name),
1008 rte_virtio_txq_stat_strings[t].name);
1018 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1024 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1025 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1030 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1031 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1038 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1039 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1040 rte_virtio_rxq_stat_strings[t].offset);
1041 xstats[count].id = count;
1046 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1047 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1054 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1055 xstats[count].value = *(uint64_t *)(((char *)txvq) +
1056 rte_virtio_txq_stat_strings[t].offset);
1057 xstats[count].id = count;
1066 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1068 virtio_update_stats(dev, stats);
1074 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1078 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1079 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1083 txvq->stats.packets = 0;
1084 txvq->stats.bytes = 0;
1085 txvq->stats.multicast = 0;
1086 txvq->stats.broadcast = 0;
1087 memset(txvq->stats.size_bins, 0,
1088 sizeof(txvq->stats.size_bins[0]) * 8);
1091 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1092 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1096 rxvq->stats.packets = 0;
1097 rxvq->stats.bytes = 0;
1098 rxvq->stats.errors = 0;
1099 rxvq->stats.multicast = 0;
1100 rxvq->stats.broadcast = 0;
1101 memset(rxvq->stats.size_bins, 0,
1102 sizeof(rxvq->stats.size_bins[0]) * 8);
1109 virtio_set_hwaddr(struct virtio_hw *hw)
1111 virtio_write_dev_config(hw,
1112 offsetof(struct virtio_net_config, mac),
1113 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1117 virtio_get_hwaddr(struct virtio_hw *hw)
1119 if (virtio_with_feature(hw, VIRTIO_NET_F_MAC)) {
1120 virtio_read_dev_config(hw,
1121 offsetof(struct virtio_net_config, mac),
1122 &hw->mac_addr, RTE_ETHER_ADDR_LEN);
1124 rte_eth_random_addr(&hw->mac_addr[0]);
1125 virtio_set_hwaddr(hw);
1130 virtio_mac_table_set(struct virtio_hw *hw,
1131 const struct virtio_net_ctrl_mac *uc,
1132 const struct virtio_net_ctrl_mac *mc)
1134 struct virtio_pmd_ctrl ctrl;
1137 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1138 PMD_DRV_LOG(INFO, "host does not support mac table");
1142 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1143 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1145 len[0] = uc->entries * RTE_ETHER_ADDR_LEN + sizeof(uc->entries);
1146 memcpy(ctrl.data, uc, len[0]);
1148 len[1] = mc->entries * RTE_ETHER_ADDR_LEN + sizeof(mc->entries);
1149 memcpy(ctrl.data + len[0], mc, len[1]);
1151 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1153 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1158 virtio_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1159 uint32_t index, uint32_t vmdq __rte_unused)
1161 struct virtio_hw *hw = dev->data->dev_private;
1162 const struct rte_ether_addr *addrs = dev->data->mac_addrs;
1164 struct virtio_net_ctrl_mac *uc, *mc;
1166 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1167 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1171 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1172 sizeof(uc->entries));
1174 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1175 sizeof(mc->entries));
1178 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1179 const struct rte_ether_addr *addr
1180 = (i == index) ? mac_addr : addrs + i;
1181 struct virtio_net_ctrl_mac *tbl
1182 = rte_is_multicast_ether_addr(addr) ? mc : uc;
1184 memcpy(&tbl->macs[tbl->entries++], addr, RTE_ETHER_ADDR_LEN);
1187 return virtio_mac_table_set(hw, uc, mc);
1191 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1193 struct virtio_hw *hw = dev->data->dev_private;
1194 struct rte_ether_addr *addrs = dev->data->mac_addrs;
1195 struct virtio_net_ctrl_mac *uc, *mc;
1198 if (index >= VIRTIO_MAX_MAC_ADDRS) {
1199 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1203 uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1204 sizeof(uc->entries));
1206 mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
1207 sizeof(mc->entries));
1210 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1211 struct virtio_net_ctrl_mac *tbl;
1213 if (i == index || rte_is_zero_ether_addr(addrs + i))
1216 tbl = rte_is_multicast_ether_addr(addrs + i) ? mc : uc;
1217 memcpy(&tbl->macs[tbl->entries++], addrs + i,
1218 RTE_ETHER_ADDR_LEN);
1221 virtio_mac_table_set(hw, uc, mc);
1225 virtio_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1227 struct virtio_hw *hw = dev->data->dev_private;
1229 memcpy(hw->mac_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1231 /* Use atomic update if available */
1232 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1233 struct virtio_pmd_ctrl ctrl;
1234 int len = RTE_ETHER_ADDR_LEN;
1236 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1237 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1239 memcpy(ctrl.data, mac_addr, RTE_ETHER_ADDR_LEN);
1240 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1243 if (!virtio_with_feature(hw, VIRTIO_NET_F_MAC))
1246 virtio_set_hwaddr(hw);
1251 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1253 struct virtio_hw *hw = dev->data->dev_private;
1254 struct virtio_pmd_ctrl ctrl;
1257 if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1260 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1261 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1262 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1263 len = sizeof(vlan_id);
1265 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1269 virtio_intr_unmask(struct rte_eth_dev *dev)
1271 struct virtio_hw *hw = dev->data->dev_private;
1273 if (rte_intr_ack(dev->intr_handle) < 0)
1276 if (VIRTIO_OPS(hw)->intr_detect)
1277 VIRTIO_OPS(hw)->intr_detect(hw);
1283 virtio_intr_enable(struct rte_eth_dev *dev)
1285 struct virtio_hw *hw = dev->data->dev_private;
1287 if (rte_intr_enable(dev->intr_handle) < 0)
1290 if (VIRTIO_OPS(hw)->intr_detect)
1291 VIRTIO_OPS(hw)->intr_detect(hw);
1297 virtio_intr_disable(struct rte_eth_dev *dev)
1299 struct virtio_hw *hw = dev->data->dev_private;
1301 if (rte_intr_disable(dev->intr_handle) < 0)
1304 if (VIRTIO_OPS(hw)->intr_detect)
1305 VIRTIO_OPS(hw)->intr_detect(hw);
1311 virtio_ethdev_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1313 uint64_t host_features;
1315 /* Prepare guest_features: feature that driver wants to support */
1316 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1319 /* Read device(host) feature bits */
1320 host_features = VIRTIO_OPS(hw)->get_features(hw);
1321 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1324 /* If supported, ensure MTU value is valid before acknowledging it. */
1325 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1326 struct virtio_net_config config;
1328 virtio_read_dev_config(hw,
1329 offsetof(struct virtio_net_config, mtu),
1330 &config.mtu, sizeof(config.mtu));
1332 if (config.mtu < RTE_ETHER_MIN_MTU)
1333 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1337 * Negotiate features: Subset of device feature bits are written back
1338 * guest feature bits.
1340 hw->guest_features = req_features;
1341 hw->guest_features = virtio_negotiate_features(hw, host_features);
1342 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1343 hw->guest_features);
1345 if (VIRTIO_OPS(hw)->features_ok(hw) < 0)
1348 if (virtio_with_feature(hw, VIRTIO_F_VERSION_1)) {
1349 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1351 if (!(virtio_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1352 PMD_INIT_LOG(ERR, "Failed to set FEATURES_OK status!");
1357 hw->req_guest_features = req_features;
1363 virtio_dev_pause(struct rte_eth_dev *dev)
1365 struct virtio_hw *hw = dev->data->dev_private;
1367 rte_spinlock_lock(&hw->state_lock);
1369 if (hw->started == 0) {
1370 /* Device is just stopped. */
1371 rte_spinlock_unlock(&hw->state_lock);
1376 * Prevent the worker threads from touching queues to avoid contention,
1377 * 1 ms should be enough for the ongoing Tx function to finish.
1384 * Recover hw state to let the worker threads continue.
1387 virtio_dev_resume(struct rte_eth_dev *dev)
1389 struct virtio_hw *hw = dev->data->dev_private;
1392 rte_spinlock_unlock(&hw->state_lock);
1396 * Should be called only after device is paused.
1399 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1402 struct virtio_hw *hw = dev->data->dev_private;
1403 struct virtnet_tx *txvq = dev->data->tx_queues[0];
1406 hw->inject_pkts = tx_pkts;
1407 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1408 hw->inject_pkts = NULL;
1414 virtio_notify_peers(struct rte_eth_dev *dev)
1416 struct virtio_hw *hw = dev->data->dev_private;
1417 struct virtnet_rx *rxvq;
1418 struct rte_mbuf *rarp_mbuf;
1420 if (!dev->data->rx_queues)
1423 rxvq = dev->data->rx_queues[0];
1427 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1428 (struct rte_ether_addr *)hw->mac_addr);
1429 if (rarp_mbuf == NULL) {
1430 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1434 /* If virtio port just stopped, no need to send RARP */
1435 if (virtio_dev_pause(dev) < 0) {
1436 rte_pktmbuf_free(rarp_mbuf);
1440 virtio_inject_pkts(dev, &rarp_mbuf, 1);
1441 virtio_dev_resume(dev);
1445 virtio_ack_link_announce(struct rte_eth_dev *dev)
1447 struct virtio_hw *hw = dev->data->dev_private;
1448 struct virtio_pmd_ctrl ctrl;
1450 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1451 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1453 virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1457 * Process virtio config changed interrupt. Call the callback
1458 * if link state changed, generate gratuitous RARP packet if
1459 * the status indicates an ANNOUNCE.
1462 virtio_interrupt_handler(void *param)
1464 struct rte_eth_dev *dev = param;
1465 struct virtio_hw *hw = dev->data->dev_private;
1469 /* Read interrupt status which clears interrupt */
1470 isr = virtio_get_isr(hw);
1471 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1473 if (virtio_intr_unmask(dev) < 0)
1474 PMD_DRV_LOG(ERR, "interrupt enable failed");
1476 if (isr & VIRTIO_ISR_CONFIG) {
1477 if (virtio_dev_link_update(dev, 0) == 0)
1478 rte_eth_dev_callback_process(dev,
1479 RTE_ETH_EVENT_INTR_LSC,
1482 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1483 virtio_read_dev_config(hw,
1484 offsetof(struct virtio_net_config, status),
1485 &status, sizeof(status));
1486 if (status & VIRTIO_NET_S_ANNOUNCE) {
1487 virtio_notify_peers(dev);
1489 virtio_ack_link_announce(dev);
1495 /* set rx and tx handlers according to what is supported */
1497 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1499 struct virtio_hw *hw = eth_dev->data->dev_private;
1501 eth_dev->tx_pkt_prepare = virtio_xmit_pkts_prepare;
1502 if (virtio_with_packed_queue(hw)) {
1504 "virtio: using packed ring %s Tx path on port %u",
1505 hw->use_vec_tx ? "vectorized" : "standard",
1506 eth_dev->data->port_id);
1508 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed_vec;
1510 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1512 if (hw->use_inorder_tx) {
1513 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1514 eth_dev->data->port_id);
1515 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1517 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1518 eth_dev->data->port_id);
1519 eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1523 if (virtio_with_packed_queue(hw)) {
1524 if (hw->use_vec_rx) {
1526 "virtio: using packed ring vectorized Rx path on port %u",
1527 eth_dev->data->port_id);
1528 eth_dev->rx_pkt_burst =
1529 &virtio_recv_pkts_packed_vec;
1530 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1532 "virtio: using packed ring mergeable buffer Rx path on port %u",
1533 eth_dev->data->port_id);
1534 eth_dev->rx_pkt_burst =
1535 &virtio_recv_mergeable_pkts_packed;
1538 "virtio: using packed ring standard Rx path on port %u",
1539 eth_dev->data->port_id);
1540 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1543 if (hw->use_vec_rx) {
1544 PMD_INIT_LOG(INFO, "virtio: using vectorized Rx path on port %u",
1545 eth_dev->data->port_id);
1546 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1547 } else if (hw->use_inorder_rx) {
1549 "virtio: using inorder Rx path on port %u",
1550 eth_dev->data->port_id);
1551 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1552 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1554 "virtio: using mergeable buffer Rx path on port %u",
1555 eth_dev->data->port_id);
1556 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1558 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1559 eth_dev->data->port_id);
1560 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1566 /* Only support 1:1 queue/interrupt mapping so far.
1567 * TODO: support n:1 queue/interrupt mapping when there are limited number of
1568 * interrupt vectors (<N+1).
1571 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1574 struct virtio_hw *hw = dev->data->dev_private;
1576 PMD_INIT_LOG(INFO, "queue/interrupt binding");
1577 for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1578 dev->intr_handle->intr_vec[i] = i + 1;
1579 if (VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1580 VIRTIO_MSI_NO_VECTOR) {
1581 PMD_DRV_LOG(ERR, "failed to set queue vector");
1590 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1593 struct virtio_hw *hw = dev->data->dev_private;
1595 PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1596 for (i = 0; i < dev->data->nb_rx_queues; ++i)
1597 VIRTIO_OPS(hw)->set_queue_irq(hw,
1598 hw->vqs[i * VTNET_CQ],
1599 VIRTIO_MSI_NO_VECTOR);
1603 virtio_configure_intr(struct rte_eth_dev *dev)
1605 struct virtio_hw *hw = dev->data->dev_private;
1607 if (!rte_intr_cap_multiple(dev->intr_handle)) {
1608 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1612 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1613 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1617 if (!dev->intr_handle->intr_vec) {
1618 dev->intr_handle->intr_vec =
1619 rte_zmalloc("intr_vec",
1620 hw->max_queue_pairs * sizeof(int), 0);
1621 if (!dev->intr_handle->intr_vec) {
1622 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1623 hw->max_queue_pairs);
1628 /* Re-register callback to update max_intr */
1629 rte_intr_callback_unregister(dev->intr_handle,
1630 virtio_interrupt_handler,
1632 rte_intr_callback_register(dev->intr_handle,
1633 virtio_interrupt_handler,
1636 /* DO NOT try to remove this! This function will enable msix, or QEMU
1637 * will encounter SIGSEGV when DRIVER_OK is sent.
1638 * And for legacy devices, this should be done before queue/vec binding
1639 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1640 * (22) will be ignored.
1642 if (virtio_intr_enable(dev) < 0) {
1643 PMD_DRV_LOG(ERR, "interrupt enable failed");
1647 if (virtio_queues_bind_intr(dev) < 0) {
1648 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1654 #define DUPLEX_UNKNOWN 0xff
1655 /* reset device and renegotiate features if needed */
1657 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1659 struct virtio_hw *hw = eth_dev->data->dev_private;
1660 struct virtio_net_config *config;
1661 struct virtio_net_config local_config;
1664 /* Reset the device although not necessary at startup */
1668 virtio_dev_free_mbufs(eth_dev);
1669 virtio_free_queues(hw);
1672 /* Tell the host we've noticed this device. */
1673 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1675 /* Tell the host we've known how to drive the device. */
1676 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1677 if (virtio_ethdev_negotiate_features(hw, req_features) < 0)
1680 hw->weak_barriers = !virtio_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1682 /* If host does not support both status and MSI-X then disable LSC */
1683 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->intr_lsc)
1684 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1686 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1688 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1690 /* Setting up rx_header size for the device */
1691 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1692 virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
1693 virtio_with_packed_queue(hw))
1694 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1696 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1698 /* Copy the permanent MAC address to: virtio_hw */
1699 virtio_get_hwaddr(hw);
1700 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1701 ð_dev->data->mac_addrs[0]);
1703 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1704 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1705 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1707 if (hw->speed == ETH_SPEED_NUM_UNKNOWN) {
1708 if (virtio_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
1709 config = &local_config;
1710 virtio_read_dev_config(hw,
1711 offsetof(struct virtio_net_config, speed),
1712 &config->speed, sizeof(config->speed));
1713 virtio_read_dev_config(hw,
1714 offsetof(struct virtio_net_config, duplex),
1715 &config->duplex, sizeof(config->duplex));
1716 hw->speed = config->speed;
1717 hw->duplex = config->duplex;
1720 if (hw->duplex == DUPLEX_UNKNOWN)
1721 hw->duplex = ETH_LINK_FULL_DUPLEX;
1722 PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
1723 hw->speed, hw->duplex);
1724 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1725 config = &local_config;
1727 virtio_read_dev_config(hw,
1728 offsetof(struct virtio_net_config, mac),
1729 &config->mac, sizeof(config->mac));
1731 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1732 virtio_read_dev_config(hw,
1733 offsetof(struct virtio_net_config, status),
1734 &config->status, sizeof(config->status));
1737 "VIRTIO_NET_F_STATUS is not supported");
1741 if (virtio_with_feature(hw, VIRTIO_NET_F_MQ)) {
1742 virtio_read_dev_config(hw,
1743 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1744 &config->max_virtqueue_pairs,
1745 sizeof(config->max_virtqueue_pairs));
1748 "VIRTIO_NET_F_MQ is not supported");
1749 config->max_virtqueue_pairs = 1;
1752 hw->max_queue_pairs = config->max_virtqueue_pairs;
1754 if (virtio_with_feature(hw, VIRTIO_NET_F_MTU)) {
1755 virtio_read_dev_config(hw,
1756 offsetof(struct virtio_net_config, mtu),
1758 sizeof(config->mtu));
1761 * MTU value has already been checked at negotiation
1762 * time, but check again in case it has changed since
1763 * then, which should not happen.
1765 if (config->mtu < RTE_ETHER_MIN_MTU) {
1766 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1771 hw->max_mtu = config->mtu;
1772 /* Set initial MTU to maximum one supported by vhost */
1773 eth_dev->data->mtu = config->mtu;
1776 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1777 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1780 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1781 config->max_virtqueue_pairs);
1782 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1784 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1785 config->mac[0], config->mac[1],
1786 config->mac[2], config->mac[3],
1787 config->mac[4], config->mac[5]);
1789 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1790 hw->max_queue_pairs = 1;
1791 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1792 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1795 ret = virtio_alloc_queues(eth_dev);
1799 if (eth_dev->data->dev_conf.intr_conf.rxq) {
1800 if (virtio_configure_intr(eth_dev) < 0) {
1801 PMD_INIT_LOG(ERR, "failed to configure interrupt");
1802 virtio_free_queues(hw);
1807 virtio_reinit_complete(hw);
1813 * This function is based on probe() function in virtio_pci.c
1814 * It returns 0 on success.
1817 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1819 struct virtio_hw *hw = eth_dev->data->dev_private;
1820 uint32_t speed = ETH_SPEED_NUM_UNKNOWN;
1824 if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1826 "Not sufficient headroom required = %d, avail = %d",
1827 (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1828 RTE_PKTMBUF_HEADROOM);
1833 eth_dev->dev_ops = &virtio_eth_dev_ops;
1834 eth_dev->rx_descriptor_done = virtio_dev_rx_queue_done;
1836 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1837 set_rxtx_funcs(eth_dev);
1841 ret = virtio_dev_devargs_parse(eth_dev->device->devargs, &speed, &vectorized);
1846 /* Allocate memory for storing MAC addresses */
1847 eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1848 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1849 if (eth_dev->data->mac_addrs == NULL) {
1851 "Failed to allocate %d bytes needed to store MAC addresses",
1852 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1856 rte_spinlock_init(&hw->state_lock);
1858 /* reset device and negotiate default features */
1859 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1861 goto err_virtio_init;
1864 if (!virtio_with_packed_queue(hw)) {
1867 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
1872 "building environment do not support packed ring vectorized");
1882 rte_free(eth_dev->data->mac_addrs);
1883 eth_dev->data->mac_addrs = NULL;
1888 virtio_dev_speed_capa_get(uint32_t speed)
1891 case ETH_SPEED_NUM_10G:
1892 return ETH_LINK_SPEED_10G;
1893 case ETH_SPEED_NUM_20G:
1894 return ETH_LINK_SPEED_20G;
1895 case ETH_SPEED_NUM_25G:
1896 return ETH_LINK_SPEED_25G;
1897 case ETH_SPEED_NUM_40G:
1898 return ETH_LINK_SPEED_40G;
1899 case ETH_SPEED_NUM_50G:
1900 return ETH_LINK_SPEED_50G;
1901 case ETH_SPEED_NUM_56G:
1902 return ETH_LINK_SPEED_56G;
1903 case ETH_SPEED_NUM_100G:
1904 return ETH_LINK_SPEED_100G;
1905 case ETH_SPEED_NUM_200G:
1906 return ETH_LINK_SPEED_200G;
1912 static int vectorized_check_handler(__rte_unused const char *key,
1913 const char *value, void *ret_val)
1915 if (strcmp(value, "1") == 0)
1916 *(int *)ret_val = 1;
1918 *(int *)ret_val = 0;
1923 #define VIRTIO_ARG_SPEED "speed"
1924 #define VIRTIO_ARG_VECTORIZED "vectorized"
1927 link_speed_handler(const char *key __rte_unused,
1928 const char *value, void *ret_val)
1931 if (!value || !ret_val)
1933 val = strtoul(value, NULL, 0);
1934 /* validate input */
1935 if (virtio_dev_speed_capa_get(val) == 0)
1937 *(uint32_t *)ret_val = val;
1944 virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vectorized)
1946 struct rte_kvargs *kvlist;
1949 if (devargs == NULL)
1952 kvlist = rte_kvargs_parse(devargs->args, NULL);
1953 if (kvlist == NULL) {
1954 PMD_INIT_LOG(ERR, "error when parsing param");
1958 if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
1959 ret = rte_kvargs_process(kvlist,
1961 link_speed_handler, speed);
1963 PMD_INIT_LOG(ERR, "Failed to parse %s",
1970 rte_kvargs_count(kvlist, VIRTIO_ARG_VECTORIZED) == 1) {
1971 ret = rte_kvargs_process(kvlist,
1972 VIRTIO_ARG_VECTORIZED,
1973 vectorized_check_handler, vectorized);
1975 PMD_INIT_LOG(ERR, "Failed to parse %s",
1976 VIRTIO_ARG_VECTORIZED);
1982 rte_kvargs_free(kvlist);
1987 rx_offload_enabled(struct virtio_hw *hw)
1989 return virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1990 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1991 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1995 tx_offload_enabled(struct virtio_hw *hw)
1997 return virtio_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1998 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1999 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2003 * Configure virtio device
2004 * It returns 0 on success.
2007 virtio_dev_configure(struct rte_eth_dev *dev)
2009 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2010 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2011 struct virtio_hw *hw = dev->data->dev_private;
2012 uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2014 uint64_t rx_offloads = rxmode->offloads;
2015 uint64_t tx_offloads = txmode->offloads;
2016 uint64_t req_features;
2019 PMD_INIT_LOG(DEBUG, "configure");
2020 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2022 if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2024 "Unsupported Rx multi queue mode %d",
2029 if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2031 "Unsupported Tx multi queue mode %d",
2036 if (dev->data->dev_conf.intr_conf.rxq) {
2037 ret = virtio_init_device(dev, hw->req_guest_features);
2042 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
2043 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2045 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2046 DEV_RX_OFFLOAD_TCP_CKSUM))
2047 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2049 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2051 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2052 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2054 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2055 DEV_TX_OFFLOAD_TCP_CKSUM))
2056 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2058 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2060 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2061 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2063 /* if request features changed, reinit the device */
2064 if (req_features != hw->req_guest_features) {
2065 ret = virtio_init_device(dev, req_features);
2070 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2071 DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2072 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2074 "rx checksum not available on this host");
2078 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2079 (!virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2080 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2082 "Large Receive Offload not available on this host");
2086 /* start control queue */
2087 if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2088 virtio_dev_cq_start(dev);
2090 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2093 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2094 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2096 "vlan filtering not available on this host");
2100 hw->has_tx_offload = tx_offload_enabled(hw);
2101 hw->has_rx_offload = rx_offload_enabled(hw);
2103 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2104 /* Enable vector (0) for Link State Intrerrupt */
2105 if (VIRTIO_OPS(hw)->set_config_irq(hw, 0) ==
2106 VIRTIO_MSI_NO_VECTOR) {
2107 PMD_DRV_LOG(ERR, "failed to set config vector");
2111 if (virtio_with_packed_queue(hw)) {
2112 #if defined(RTE_ARCH_X86_64) && defined(CC_AVX512_SUPPORT)
2113 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2114 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) ||
2115 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2116 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2117 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)) {
2119 "disabled packed ring vectorized path for requirements not met");
2123 #elif defined(RTE_ARCH_ARM)
2124 if ((hw->use_vec_rx || hw->use_vec_tx) &&
2125 (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) ||
2126 !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
2127 !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
2128 rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)) {
2130 "disabled packed ring vectorized path for requirements not met");
2139 if (hw->use_vec_rx) {
2140 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2142 "disabled packed ring vectorized rx for mrg_rxbuf enabled");
2146 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) {
2148 "disabled packed ring vectorized rx for TCP_LRO enabled");
2153 if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2154 hw->use_inorder_tx = 1;
2155 hw->use_inorder_rx = 1;
2159 if (hw->use_vec_rx) {
2160 #if defined RTE_ARCH_ARM
2161 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2163 "disabled split ring vectorized path for requirement not met");
2167 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2169 "disabled split ring vectorized rx for mrg_rxbuf enabled");
2173 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2174 DEV_RX_OFFLOAD_TCP_CKSUM |
2175 DEV_RX_OFFLOAD_TCP_LRO |
2176 DEV_RX_OFFLOAD_VLAN_STRIP)) {
2178 "disabled split ring vectorized rx for offloading enabled");
2182 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128) {
2184 "disabled split ring vectorized rx, max SIMD bitwidth too low");
2195 virtio_dev_start(struct rte_eth_dev *dev)
2197 uint16_t nb_queues, i;
2198 struct virtqueue *vq;
2199 struct virtio_hw *hw = dev->data->dev_private;
2202 /* Finish the initialization of the queues */
2203 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2204 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2208 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2209 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2214 /* check if lsc interrupt feature is enabled */
2215 if (dev->data->dev_conf.intr_conf.lsc) {
2216 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2217 PMD_DRV_LOG(ERR, "link status not supported by host");
2222 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2223 * in device configure, but it could be unmapped when device is
2226 if (dev->data->dev_conf.intr_conf.lsc ||
2227 dev->data->dev_conf.intr_conf.rxq) {
2228 virtio_intr_disable(dev);
2230 /* Setup interrupt callback */
2231 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2232 rte_intr_callback_register(dev->intr_handle,
2233 virtio_interrupt_handler,
2236 if (virtio_intr_enable(dev) < 0) {
2237 PMD_DRV_LOG(ERR, "interrupt enable failed");
2242 /*Notify the backend
2243 *Otherwise the tap backend might already stop its queue due to fullness.
2244 *vhost backend will have no chance to be waked up
2246 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2247 if (hw->max_queue_pairs > 1) {
2248 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2252 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2254 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2255 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2256 /* Flush the old packets */
2257 virtqueue_rxvq_flush(vq);
2258 virtqueue_notify(vq);
2261 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2262 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2263 virtqueue_notify(vq);
2266 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2268 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2269 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
2273 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2274 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
2278 set_rxtx_funcs(dev);
2281 /* Initialize Link state */
2282 virtio_dev_link_update(dev, 0);
2287 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2289 struct virtio_hw *hw = dev->data->dev_private;
2290 uint16_t nr_vq = virtio_get_nr_vq(hw);
2291 const char *type __rte_unused;
2292 unsigned int i, mbuf_num = 0;
2293 struct virtqueue *vq;
2294 struct rte_mbuf *buf;
2297 if (hw->vqs == NULL)
2300 for (i = 0; i < nr_vq; i++) {
2305 queue_type = virtio_get_queue_type(hw, i);
2306 if (queue_type == VTNET_RQ)
2308 else if (queue_type == VTNET_TQ)
2314 "Before freeing %s[%d] used and unused buf",
2318 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2319 rte_pktmbuf_free(buf);
2324 "After freeing %s[%d] used and unused buf",
2329 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2333 * Stop device: disable interrupt and mark link down
2336 virtio_dev_stop(struct rte_eth_dev *dev)
2338 struct virtio_hw *hw = dev->data->dev_private;
2339 struct rte_eth_link link;
2340 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2342 PMD_INIT_LOG(DEBUG, "stop");
2343 dev->data->dev_started = 0;
2345 rte_spinlock_lock(&hw->state_lock);
2350 if (intr_conf->lsc || intr_conf->rxq) {
2351 virtio_intr_disable(dev);
2353 /* Reset interrupt callback */
2354 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2355 rte_intr_callback_unregister(dev->intr_handle,
2356 virtio_interrupt_handler,
2361 memset(&link, 0, sizeof(link));
2362 rte_eth_linkstatus_set(dev, &link);
2364 rte_spinlock_unlock(&hw->state_lock);
2370 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2372 struct rte_eth_link link;
2374 struct virtio_hw *hw = dev->data->dev_private;
2376 memset(&link, 0, sizeof(link));
2377 link.link_duplex = hw->duplex;
2378 link.link_speed = hw->speed;
2379 link.link_autoneg = ETH_LINK_AUTONEG;
2382 link.link_status = ETH_LINK_DOWN;
2383 link.link_speed = ETH_SPEED_NUM_NONE;
2384 } else if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2385 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2386 virtio_read_dev_config(hw,
2387 offsetof(struct virtio_net_config, status),
2388 &status, sizeof(status));
2389 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2390 link.link_status = ETH_LINK_DOWN;
2391 link.link_speed = ETH_SPEED_NUM_NONE;
2392 PMD_INIT_LOG(DEBUG, "Port %d is down",
2393 dev->data->port_id);
2395 link.link_status = ETH_LINK_UP;
2396 PMD_INIT_LOG(DEBUG, "Port %d is up",
2397 dev->data->port_id);
2400 link.link_status = ETH_LINK_UP;
2403 return rte_eth_linkstatus_set(dev, &link);
2407 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2409 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2410 struct virtio_hw *hw = dev->data->dev_private;
2411 uint64_t offloads = rxmode->offloads;
2413 if (mask & ETH_VLAN_FILTER_MASK) {
2414 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2415 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2418 "vlan filtering not available on this host");
2424 if (mask & ETH_VLAN_STRIP_MASK)
2425 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2431 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2433 uint64_t tso_mask, host_features;
2434 struct virtio_hw *hw = dev->data->dev_private;
2435 dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2437 dev_info->max_rx_queues =
2438 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2439 dev_info->max_tx_queues =
2440 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2441 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2442 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2443 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2445 host_features = VIRTIO_OPS(hw)->get_features(hw);
2446 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2447 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2448 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2449 dev_info->rx_offload_capa |=
2450 DEV_RX_OFFLOAD_TCP_CKSUM |
2451 DEV_RX_OFFLOAD_UDP_CKSUM;
2453 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2454 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2455 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2456 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2457 if ((host_features & tso_mask) == tso_mask)
2458 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2460 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2461 DEV_TX_OFFLOAD_VLAN_INSERT;
2462 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2463 dev_info->tx_offload_capa |=
2464 DEV_TX_OFFLOAD_UDP_CKSUM |
2465 DEV_TX_OFFLOAD_TCP_CKSUM;
2467 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2468 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2469 if ((host_features & tso_mask) == tso_mask)
2470 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2476 * It enables testpmd to collect per queue stats.
2479 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2480 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2481 __rte_unused uint8_t is_rx)
2486 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_init, init, NOTICE);
2487 RTE_LOG_REGISTER_SUFFIX(virtio_logtype_driver, driver, NOTICE);