1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
12 #include <rte_ether.h>
14 #include <rte_dmadev.h>
15 #include <rte_vhost.h>
20 #include <rte_spinlock.h>
21 #include <rte_malloc.h>
22 #include <rte_vhost_async.h>
27 #define MAX_BATCH_LEN 256
29 /* DMA device copy operation tracking array. */
30 struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
32 static __rte_always_inline bool
33 rxvq_is_mergeable(struct virtio_net *dev)
35 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
38 static __rte_always_inline bool
39 virtio_net_is_inorder(struct virtio_net *dev)
41 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
45 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
47 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
51 * This function must be called with virtqueue's access_lock taken.
54 vhost_queue_stats_update(struct virtio_net *dev, struct vhost_virtqueue *vq,
55 struct rte_mbuf **pkts, uint16_t count)
57 struct virtqueue_stats *stats = &vq->stats;
60 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
63 for (i = 0; i < count; i++) {
64 struct rte_ether_addr *ea;
65 struct rte_mbuf *pkt = pkts[i];
66 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt);
69 stats->bytes += pkt_len;
72 stats->size_bins[1]++;
73 } else if (pkt_len > 64 && pkt_len < 1024) {
76 /* count zeros, and offset into correct bin */
77 bin = (sizeof(pkt_len) * 8) - __builtin_clz(pkt_len) - 5;
78 stats->size_bins[bin]++;
81 stats->size_bins[0]++;
82 else if (pkt_len < 1519)
83 stats->size_bins[6]++;
85 stats->size_bins[7]++;
88 ea = rte_pktmbuf_mtod(pkt, struct rte_ether_addr *);
89 if (rte_is_multicast_ether_addr(ea)) {
90 if (rte_is_broadcast_ether_addr(ea))
98 static __rte_always_inline int64_t
99 vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,
100 int16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,
101 struct vhost_iov_iter *pkt)
103 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
104 uint16_t ring_mask = dma_info->ring_mask;
105 static bool vhost_async_dma_copy_log;
108 struct vhost_iovec *iov = pkt->iov;
110 uint32_t nr_segs = pkt->nr_segs;
113 if (rte_dma_burst_capacity(dma_id, vchan_id) < nr_segs)
116 for (i = 0; i < nr_segs; i++) {
117 copy_idx = rte_dma_copy(dma_id, vchan_id, (rte_iova_t)iov[i].src_addr,
118 (rte_iova_t)iov[i].dst_addr, iov[i].len, RTE_DMA_OP_FLAG_LLC);
120 * Since all memory is pinned and DMA vChannel
121 * ring has enough space, failure should be a
122 * rare case. If failure happens, it means DMA
123 * device encounters serious errors; in this
124 * case, please stop async data-path and check
125 * what has happened to DMA device.
127 if (unlikely(copy_idx < 0)) {
128 if (!vhost_async_dma_copy_log) {
129 VHOST_LOG_DATA(ERR, "(%s) DMA copy failed for channel %d:%u\n",
130 dev->ifname, dma_id, vchan_id);
131 vhost_async_dma_copy_log = true;
138 * Only store packet completion flag address in the last copy's
139 * slot, and other slots are set to NULL.
141 dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = &vq->async->pkts_cmpl_flag[flag_idx];
146 static __rte_always_inline uint16_t
147 vhost_async_dma_transfer(struct virtio_net *dev, struct vhost_virtqueue *vq,
148 int16_t dma_id, uint16_t vchan_id, uint16_t head_idx,
149 struct vhost_iov_iter *pkts, uint16_t nr_pkts)
151 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
152 int64_t ret, nr_copies = 0;
155 rte_spinlock_lock(&dma_info->dma_lock);
157 for (pkt_idx = 0; pkt_idx < nr_pkts; pkt_idx++) {
158 ret = vhost_async_dma_transfer_one(dev, vq, dma_id, vchan_id, head_idx,
160 if (unlikely(ret < 0))
165 if (head_idx >= vq->size)
166 head_idx -= vq->size;
169 if (likely(nr_copies > 0))
170 rte_dma_submit(dma_id, vchan_id);
172 rte_spinlock_unlock(&dma_info->dma_lock);
177 static __rte_always_inline uint16_t
178 vhost_async_dma_check_completed(struct virtio_net *dev, int16_t dma_id, uint16_t vchan_id,
181 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
182 uint16_t ring_mask = dma_info->ring_mask;
183 uint16_t last_idx = 0;
187 bool has_error = false;
188 static bool vhost_async_dma_complete_log;
190 rte_spinlock_lock(&dma_info->dma_lock);
193 * Print error log for debugging, if DMA reports error during
194 * DMA transfer. We do not handle error in vhost level.
196 nr_copies = rte_dma_completed(dma_id, vchan_id, max_pkts, &last_idx, &has_error);
197 if (unlikely(!vhost_async_dma_complete_log && has_error)) {
198 VHOST_LOG_DATA(ERR, "(%s) DMA completion failure on channel %d:%u\n", dev->ifname,
200 vhost_async_dma_complete_log = true;
201 } else if (nr_copies == 0) {
205 copy_idx = last_idx - nr_copies + 1;
206 for (i = 0; i < nr_copies; i++) {
209 flag = dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask];
212 * Mark the packet flag as received. The flag
213 * could belong to another virtqueue but write
217 dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = NULL;
223 rte_spinlock_unlock(&dma_info->dma_lock);
228 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
230 struct batch_copy_elem *elem = vq->batch_copy_elems;
231 uint16_t count = vq->batch_copy_nb_elems;
234 for (i = 0; i < count; i++) {
235 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
236 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
238 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
241 vq->batch_copy_nb_elems = 0;
245 do_data_copy_dequeue(struct vhost_virtqueue *vq)
247 struct batch_copy_elem *elem = vq->batch_copy_elems;
248 uint16_t count = vq->batch_copy_nb_elems;
251 for (i = 0; i < count; i++)
252 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
254 vq->batch_copy_nb_elems = 0;
257 static __rte_always_inline void
258 do_flush_shadow_used_ring_split(struct virtio_net *dev,
259 struct vhost_virtqueue *vq,
260 uint16_t to, uint16_t from, uint16_t size)
262 rte_memcpy(&vq->used->ring[to],
263 &vq->shadow_used_split[from],
264 size * sizeof(struct vring_used_elem));
265 vhost_log_cache_used_vring(dev, vq,
266 offsetof(struct vring_used, ring[to]),
267 size * sizeof(struct vring_used_elem));
270 static __rte_always_inline void
271 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
273 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
275 if (used_idx + vq->shadow_used_idx <= vq->size) {
276 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
277 vq->shadow_used_idx);
281 /* update used ring interval [used_idx, vq->size] */
282 size = vq->size - used_idx;
283 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
285 /* update the left half used ring interval [0, left_size] */
286 do_flush_shadow_used_ring_split(dev, vq, 0, size,
287 vq->shadow_used_idx - size);
289 vq->last_used_idx += vq->shadow_used_idx;
291 vhost_log_cache_sync(dev, vq);
293 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
295 vq->shadow_used_idx = 0;
296 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
297 sizeof(vq->used->idx));
300 static __rte_always_inline void
301 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
302 uint16_t desc_idx, uint32_t len)
304 uint16_t i = vq->shadow_used_idx++;
306 vq->shadow_used_split[i].id = desc_idx;
307 vq->shadow_used_split[i].len = len;
310 static __rte_always_inline void
311 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
312 struct vhost_virtqueue *vq)
315 uint16_t used_idx = vq->last_used_idx;
316 uint16_t head_idx = vq->last_used_idx;
317 uint16_t head_flags = 0;
319 /* Split loop in two to save memory barriers */
320 for (i = 0; i < vq->shadow_used_idx; i++) {
321 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
322 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
324 used_idx += vq->shadow_used_packed[i].count;
325 if (used_idx >= vq->size)
326 used_idx -= vq->size;
329 /* The ordering for storing desc flags needs to be enforced. */
330 rte_atomic_thread_fence(__ATOMIC_RELEASE);
332 for (i = 0; i < vq->shadow_used_idx; i++) {
335 if (vq->shadow_used_packed[i].len)
336 flags = VRING_DESC_F_WRITE;
340 if (vq->used_wrap_counter) {
341 flags |= VRING_DESC_F_USED;
342 flags |= VRING_DESC_F_AVAIL;
344 flags &= ~VRING_DESC_F_USED;
345 flags &= ~VRING_DESC_F_AVAIL;
349 vq->desc_packed[vq->last_used_idx].flags = flags;
351 vhost_log_cache_used_vring(dev, vq,
353 sizeof(struct vring_packed_desc),
354 sizeof(struct vring_packed_desc));
356 head_idx = vq->last_used_idx;
360 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
363 vq->desc_packed[head_idx].flags = head_flags;
365 vhost_log_cache_used_vring(dev, vq,
367 sizeof(struct vring_packed_desc),
368 sizeof(struct vring_packed_desc));
370 vq->shadow_used_idx = 0;
371 vhost_log_cache_sync(dev, vq);
374 static __rte_always_inline void
375 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
376 struct vhost_virtqueue *vq)
378 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
380 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
381 /* desc flags is the synchronization point for virtio packed vring */
382 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
383 used_elem->flags, __ATOMIC_RELEASE);
385 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
386 sizeof(struct vring_packed_desc),
387 sizeof(struct vring_packed_desc));
388 vq->shadow_used_idx = 0;
389 vhost_log_cache_sync(dev, vq);
392 static __rte_always_inline void
393 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
394 struct vhost_virtqueue *vq,
400 uint16_t last_used_idx;
401 struct vring_packed_desc *desc_base;
403 last_used_idx = vq->last_used_idx;
404 desc_base = &vq->desc_packed[last_used_idx];
406 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
408 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
409 desc_base[i].id = ids[i];
410 desc_base[i].len = lens[i];
413 rte_atomic_thread_fence(__ATOMIC_RELEASE);
415 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
416 desc_base[i].flags = flags;
419 vhost_log_cache_used_vring(dev, vq, last_used_idx *
420 sizeof(struct vring_packed_desc),
421 sizeof(struct vring_packed_desc) *
423 vhost_log_cache_sync(dev, vq);
425 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
428 static __rte_always_inline void
429 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
432 vq->shadow_used_packed[0].id = id;
434 if (!vq->shadow_used_idx) {
435 vq->shadow_last_used_idx = vq->last_used_idx;
436 vq->shadow_used_packed[0].flags =
437 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
438 vq->shadow_used_packed[0].len = 0;
439 vq->shadow_used_packed[0].count = 1;
440 vq->shadow_used_idx++;
443 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
446 static __rte_always_inline void
447 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
448 struct vhost_virtqueue *vq,
455 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
457 if (!vq->shadow_used_idx) {
458 vq->shadow_last_used_idx = vq->last_used_idx;
459 vq->shadow_used_packed[0].id = ids[0];
460 vq->shadow_used_packed[0].len = 0;
461 vq->shadow_used_packed[0].count = 1;
462 vq->shadow_used_packed[0].flags = flags;
463 vq->shadow_used_idx++;
468 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
469 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
470 vq->desc_packed[vq->last_used_idx + i].len = 0;
473 rte_atomic_thread_fence(__ATOMIC_RELEASE);
474 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
475 vq->desc_packed[vq->last_used_idx + i].flags = flags;
477 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
478 sizeof(struct vring_packed_desc),
479 sizeof(struct vring_packed_desc) *
481 vhost_log_cache_sync(dev, vq);
483 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
486 static __rte_always_inline void
487 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
493 flags = vq->desc_packed[vq->last_used_idx].flags;
494 if (vq->used_wrap_counter) {
495 flags |= VRING_DESC_F_USED;
496 flags |= VRING_DESC_F_AVAIL;
498 flags &= ~VRING_DESC_F_USED;
499 flags &= ~VRING_DESC_F_AVAIL;
502 if (!vq->shadow_used_idx) {
503 vq->shadow_last_used_idx = vq->last_used_idx;
505 vq->shadow_used_packed[0].id = buf_id;
506 vq->shadow_used_packed[0].len = 0;
507 vq->shadow_used_packed[0].flags = flags;
508 vq->shadow_used_idx++;
510 vq->desc_packed[vq->last_used_idx].id = buf_id;
511 vq->desc_packed[vq->last_used_idx].len = 0;
512 vq->desc_packed[vq->last_used_idx].flags = flags;
515 vq_inc_last_used_packed(vq, count);
518 static __rte_always_inline void
519 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
525 vq->shadow_used_packed[0].id = buf_id;
527 flags = vq->desc_packed[vq->last_used_idx].flags;
528 if (vq->used_wrap_counter) {
529 flags |= VRING_DESC_F_USED;
530 flags |= VRING_DESC_F_AVAIL;
532 flags &= ~VRING_DESC_F_USED;
533 flags &= ~VRING_DESC_F_AVAIL;
536 if (!vq->shadow_used_idx) {
537 vq->shadow_last_used_idx = vq->last_used_idx;
538 vq->shadow_used_packed[0].len = 0;
539 vq->shadow_used_packed[0].flags = flags;
540 vq->shadow_used_idx++;
543 vq_inc_last_used_packed(vq, count);
546 static __rte_always_inline void
547 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
551 uint16_t num_buffers)
555 for (i = 0; i < num_buffers; i++) {
556 /* enqueue shadow flush action aligned with batch num */
557 if (!vq->shadow_used_idx)
558 vq->shadow_aligned_idx = vq->last_used_idx &
560 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
561 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
562 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
563 vq->shadow_aligned_idx += count[i];
564 vq->shadow_used_idx++;
568 static __rte_always_inline void
569 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
570 struct vhost_virtqueue *vq,
574 uint16_t num_buffers)
576 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
578 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
579 do_data_copy_enqueue(dev, vq);
580 vhost_flush_enqueue_shadow_packed(dev, vq);
584 /* avoid write operation when necessary, to lessen cache issues */
585 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
586 if ((var) != (val)) \
590 static __rte_always_inline void
591 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
593 uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
595 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
596 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
599 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
600 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
603 case RTE_MBUF_F_TX_TCP_CKSUM:
604 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
607 case RTE_MBUF_F_TX_UDP_CKSUM:
608 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
611 case RTE_MBUF_F_TX_SCTP_CKSUM:
612 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
617 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
618 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
619 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
622 /* IP cksum verification cannot be bypassed, then calculate here */
623 if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
624 struct rte_ipv4_hdr *ipv4_hdr;
626 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
628 ipv4_hdr->hdr_checksum = 0;
629 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
632 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
633 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
634 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
636 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
637 net_hdr->gso_size = m_buf->tso_segsz;
638 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
640 } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
641 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
642 net_hdr->gso_size = m_buf->tso_segsz;
643 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
646 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
647 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
648 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
652 static __rte_always_inline int
653 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
654 struct buf_vector *buf_vec, uint16_t *vec_idx,
655 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
657 uint16_t vec_id = *vec_idx;
661 uint64_t desc_chunck_len = desc_len;
663 if (unlikely(vec_id >= BUF_VECTOR_MAX))
666 desc_addr = vhost_iova_to_vva(dev, vq,
670 if (unlikely(!desc_addr))
673 rte_prefetch0((void *)(uintptr_t)desc_addr);
675 buf_vec[vec_id].buf_iova = desc_iova;
676 buf_vec[vec_id].buf_addr = desc_addr;
677 buf_vec[vec_id].buf_len = desc_chunck_len;
679 desc_len -= desc_chunck_len;
680 desc_iova += desc_chunck_len;
688 static __rte_always_inline int
689 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
690 uint32_t avail_idx, uint16_t *vec_idx,
691 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
692 uint32_t *desc_chain_len, uint8_t perm)
694 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
695 uint16_t vec_id = *vec_idx;
698 uint32_t nr_descs = vq->size;
700 struct vring_desc *descs = vq->desc;
701 struct vring_desc *idesc = NULL;
703 if (unlikely(idx >= vq->size))
706 *desc_chain_head = idx;
708 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
709 dlen = vq->desc[idx].len;
710 nr_descs = dlen / sizeof(struct vring_desc);
711 if (unlikely(nr_descs > vq->size))
714 descs = (struct vring_desc *)(uintptr_t)
715 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
718 if (unlikely(!descs))
721 if (unlikely(dlen < vq->desc[idx].len)) {
723 * The indirect desc table is not contiguous
724 * in process VA space, we have to copy it.
726 idesc = vhost_alloc_copy_ind_table(dev, vq,
727 vq->desc[idx].addr, vq->desc[idx].len);
728 if (unlikely(!idesc))
738 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
739 free_ind_table(idesc);
743 dlen = descs[idx].len;
746 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
747 descs[idx].addr, dlen,
749 free_ind_table(idesc);
753 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
756 idx = descs[idx].next;
759 *desc_chain_len = len;
762 if (unlikely(!!idesc))
763 free_ind_table(idesc);
769 * Returns -1 on fail, 0 on success
772 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
773 uint32_t size, struct buf_vector *buf_vec,
774 uint16_t *num_buffers, uint16_t avail_head,
778 uint16_t vec_idx = 0;
779 uint16_t max_tries, tries = 0;
781 uint16_t head_idx = 0;
785 cur_idx = vq->last_avail_idx;
787 if (rxvq_is_mergeable(dev))
788 max_tries = vq->size - 1;
793 if (unlikely(cur_idx == avail_head))
796 * if we tried all available ring items, and still
797 * can't get enough buf, it means something abnormal
800 if (unlikely(++tries > max_tries))
803 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
806 VHOST_ACCESS_RW) < 0))
808 len = RTE_MIN(len, size);
809 update_shadow_used_ring_split(vq, head_idx, len);
821 static __rte_always_inline int
822 fill_vec_buf_packed_indirect(struct virtio_net *dev,
823 struct vhost_virtqueue *vq,
824 struct vring_packed_desc *desc, uint16_t *vec_idx,
825 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
829 uint16_t vec_id = *vec_idx;
831 struct vring_packed_desc *descs, *idescs = NULL;
834 descs = (struct vring_packed_desc *)(uintptr_t)
835 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
836 if (unlikely(!descs))
839 if (unlikely(dlen < desc->len)) {
841 * The indirect desc table is not contiguous
842 * in process VA space, we have to copy it.
844 idescs = vhost_alloc_copy_ind_table(dev,
845 vq, desc->addr, desc->len);
846 if (unlikely(!idescs))
852 nr_descs = desc->len / sizeof(struct vring_packed_desc);
853 if (unlikely(nr_descs >= vq->size)) {
854 free_ind_table(idescs);
858 for (i = 0; i < nr_descs; i++) {
859 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
860 free_ind_table(idescs);
866 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
873 if (unlikely(!!idescs))
874 free_ind_table(idescs);
879 static __rte_always_inline int
880 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
881 uint16_t avail_idx, uint16_t *desc_count,
882 struct buf_vector *buf_vec, uint16_t *vec_idx,
883 uint16_t *buf_id, uint32_t *len, uint8_t perm)
885 bool wrap_counter = vq->avail_wrap_counter;
886 struct vring_packed_desc *descs = vq->desc_packed;
887 uint16_t vec_id = *vec_idx;
890 if (avail_idx < vq->last_avail_idx)
894 * Perform a load-acquire barrier in desc_is_avail to
895 * enforce the ordering between desc flags and desc
898 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
905 if (unlikely(vec_id >= BUF_VECTOR_MAX))
908 if (unlikely(*desc_count >= vq->size))
912 *buf_id = descs[avail_idx].id;
914 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
915 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
921 dlen = descs[avail_idx].len;
924 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
925 descs[avail_idx].addr,
931 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
934 if (++avail_idx >= vq->size) {
935 avail_idx -= vq->size;
945 static __rte_noinline void
946 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
947 struct buf_vector *buf_vec,
948 struct virtio_net_hdr_mrg_rxbuf *hdr)
951 uint64_t remain = dev->vhost_hlen;
952 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
953 uint64_t iova = buf_vec->buf_iova;
956 len = RTE_MIN(remain,
958 dst = buf_vec->buf_addr;
959 rte_memcpy((void *)(uintptr_t)dst,
960 (void *)(uintptr_t)src,
963 PRINT_PACKET(dev, (uintptr_t)dst,
965 vhost_log_cache_write_iova(dev, vq,
975 static __rte_always_inline int
976 async_iter_initialize(struct virtio_net *dev, struct vhost_async *async)
978 struct vhost_iov_iter *iter;
980 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
981 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
985 iter = async->iov_iter + async->iter_idx;
986 iter->iov = async->iovec + async->iovec_idx;
992 static __rte_always_inline int
993 async_iter_add_iovec(struct virtio_net *dev, struct vhost_async *async,
994 void *src, void *dst, size_t len)
996 struct vhost_iov_iter *iter;
997 struct vhost_iovec *iovec;
999 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
1000 static bool vhost_max_async_vec_log;
1002 if (!vhost_max_async_vec_log) {
1003 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
1004 vhost_max_async_vec_log = true;
1010 iter = async->iov_iter + async->iter_idx;
1011 iovec = async->iovec + async->iovec_idx;
1013 iovec->src_addr = src;
1014 iovec->dst_addr = dst;
1023 static __rte_always_inline void
1024 async_iter_finalize(struct vhost_async *async)
1029 static __rte_always_inline void
1030 async_iter_cancel(struct vhost_async *async)
1032 struct vhost_iov_iter *iter;
1034 iter = async->iov_iter + async->iter_idx;
1035 async->iovec_idx -= iter->nr_segs;
1040 static __rte_always_inline void
1041 async_iter_reset(struct vhost_async *async)
1043 async->iter_idx = 0;
1044 async->iovec_idx = 0;
1047 static __rte_always_inline int
1048 async_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1049 struct rte_mbuf *m, uint32_t mbuf_offset,
1050 uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
1052 struct vhost_async *async = vq->async;
1053 uint64_t mapped_len;
1054 uint32_t buf_offset = 0;
1059 host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1060 buf_iova + buf_offset, cpy_len, &mapped_len);
1061 if (unlikely(!host_iova)) {
1062 VHOST_LOG_DATA(ERR, "(%s) %s: failed to get host iova.\n",
1063 dev->ifname, __func__);
1068 src = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
1072 dst = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
1075 if (unlikely(async_iter_add_iovec(dev, async, src, dst, (size_t)mapped_len)))
1078 cpy_len -= (uint32_t)mapped_len;
1079 mbuf_offset += (uint32_t)mapped_len;
1080 buf_offset += (uint32_t)mapped_len;
1086 static __rte_always_inline void
1087 sync_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1088 struct rte_mbuf *m, uint32_t mbuf_offset,
1089 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
1091 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1093 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
1095 rte_memcpy((void *)((uintptr_t)(buf_addr)),
1096 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1099 rte_memcpy(rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1100 (void *)((uintptr_t)(buf_addr)),
1103 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1104 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1107 batch_copy[vq->batch_copy_nb_elems].dst =
1108 (void *)((uintptr_t)(buf_addr));
1109 batch_copy[vq->batch_copy_nb_elems].src =
1110 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1112 batch_copy[vq->batch_copy_nb_elems].dst =
1113 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1114 batch_copy[vq->batch_copy_nb_elems].src =
1115 (void *)((uintptr_t)(buf_addr));
1117 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1118 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1119 vq->batch_copy_nb_elems++;
1123 static __rte_always_inline int
1124 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1125 struct rte_mbuf *m, struct buf_vector *buf_vec,
1126 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1128 uint32_t vec_idx = 0;
1129 uint32_t mbuf_offset, mbuf_avail;
1130 uint32_t buf_offset, buf_avail;
1131 uint64_t buf_addr, buf_iova, buf_len;
1134 struct rte_mbuf *hdr_mbuf;
1135 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1136 struct vhost_async *async = vq->async;
1138 if (unlikely(m == NULL))
1141 buf_addr = buf_vec[vec_idx].buf_addr;
1142 buf_iova = buf_vec[vec_idx].buf_iova;
1143 buf_len = buf_vec[vec_idx].buf_len;
1145 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1149 hdr_addr = buf_addr;
1150 if (unlikely(buf_len < dev->vhost_hlen)) {
1151 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1154 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1156 VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
1157 dev->ifname, num_buffers);
1159 if (unlikely(buf_len < dev->vhost_hlen)) {
1160 buf_offset = dev->vhost_hlen - buf_len;
1162 buf_addr = buf_vec[vec_idx].buf_addr;
1163 buf_iova = buf_vec[vec_idx].buf_iova;
1164 buf_len = buf_vec[vec_idx].buf_len;
1165 buf_avail = buf_len - buf_offset;
1167 buf_offset = dev->vhost_hlen;
1168 buf_avail = buf_len - dev->vhost_hlen;
1171 mbuf_avail = rte_pktmbuf_data_len(m);
1175 if (async_iter_initialize(dev, async))
1179 while (mbuf_avail != 0 || m->next != NULL) {
1180 /* done with current buf, get the next one */
1181 if (buf_avail == 0) {
1183 if (unlikely(vec_idx >= nr_vec))
1186 buf_addr = buf_vec[vec_idx].buf_addr;
1187 buf_iova = buf_vec[vec_idx].buf_iova;
1188 buf_len = buf_vec[vec_idx].buf_len;
1191 buf_avail = buf_len;
1194 /* done with current mbuf, get the next one */
1195 if (mbuf_avail == 0) {
1199 mbuf_avail = rte_pktmbuf_data_len(m);
1203 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1204 if (rxvq_is_mergeable(dev))
1205 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1208 if (unlikely(hdr == &tmp_hdr)) {
1209 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1211 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1212 dev->vhost_hlen, 0);
1213 vhost_log_cache_write_iova(dev, vq,
1214 buf_vec[0].buf_iova,
1221 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1224 if (async_fill_seg(dev, vq, m, mbuf_offset,
1225 buf_iova + buf_offset, cpy_len, true) < 0)
1228 sync_fill_seg(dev, vq, m, mbuf_offset,
1229 buf_addr + buf_offset,
1230 buf_iova + buf_offset, cpy_len, true);
1233 mbuf_avail -= cpy_len;
1234 mbuf_offset += cpy_len;
1235 buf_avail -= cpy_len;
1236 buf_offset += cpy_len;
1240 async_iter_finalize(async);
1245 async_iter_cancel(async);
1250 static __rte_always_inline int
1251 vhost_enqueue_single_packed(struct virtio_net *dev,
1252 struct vhost_virtqueue *vq,
1253 struct rte_mbuf *pkt,
1254 struct buf_vector *buf_vec,
1257 uint16_t nr_vec = 0;
1258 uint16_t avail_idx = vq->last_avail_idx;
1259 uint16_t max_tries, tries = 0;
1260 uint16_t buf_id = 0;
1262 uint16_t desc_count;
1263 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1264 uint16_t num_buffers = 0;
1265 uint32_t buffer_len[vq->size];
1266 uint16_t buffer_buf_id[vq->size];
1267 uint16_t buffer_desc_count[vq->size];
1269 if (rxvq_is_mergeable(dev))
1270 max_tries = vq->size - 1;
1276 * if we tried all available ring items, and still
1277 * can't get enough buf, it means something abnormal
1280 if (unlikely(++tries > max_tries))
1283 if (unlikely(fill_vec_buf_packed(dev, vq,
1284 avail_idx, &desc_count,
1287 VHOST_ACCESS_RW) < 0))
1290 len = RTE_MIN(len, size);
1293 buffer_len[num_buffers] = len;
1294 buffer_buf_id[num_buffers] = buf_id;
1295 buffer_desc_count[num_buffers] = desc_count;
1298 *nr_descs += desc_count;
1299 avail_idx += desc_count;
1300 if (avail_idx >= vq->size)
1301 avail_idx -= vq->size;
1304 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1307 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1308 buffer_desc_count, num_buffers);
1313 static __rte_noinline uint32_t
1314 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1315 struct rte_mbuf **pkts, uint32_t count)
1317 uint32_t pkt_idx = 0;
1318 uint16_t num_buffers;
1319 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1320 uint16_t avail_head;
1323 * The ordering between avail index and
1324 * desc reads needs to be enforced.
1326 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1328 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1330 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1331 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1332 uint16_t nr_vec = 0;
1334 if (unlikely(reserve_avail_buf_split(dev, vq,
1335 pkt_len, buf_vec, &num_buffers,
1336 avail_head, &nr_vec) < 0)) {
1337 VHOST_LOG_DATA(DEBUG,
1338 "(%s) failed to get enough desc from vring\n",
1340 vq->shadow_used_idx -= num_buffers;
1344 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1345 dev->ifname, vq->last_avail_idx,
1346 vq->last_avail_idx + num_buffers);
1348 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1349 num_buffers, false) < 0) {
1350 vq->shadow_used_idx -= num_buffers;
1354 vq->last_avail_idx += num_buffers;
1357 do_data_copy_enqueue(dev, vq);
1359 if (likely(vq->shadow_used_idx)) {
1360 flush_shadow_used_ring_split(dev, vq);
1361 vhost_vring_call_split(dev, vq);
1367 static __rte_always_inline int
1368 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1369 struct vhost_virtqueue *vq,
1370 struct rte_mbuf **pkts,
1371 uint64_t *desc_addrs,
1374 bool wrap_counter = vq->avail_wrap_counter;
1375 struct vring_packed_desc *descs = vq->desc_packed;
1376 uint16_t avail_idx = vq->last_avail_idx;
1377 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1380 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1383 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1386 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1387 if (unlikely(pkts[i]->next != NULL))
1389 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1394 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1395 lens[i] = descs[avail_idx + i].len;
1397 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1398 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1402 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1403 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1404 descs[avail_idx + i].addr,
1408 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1409 if (unlikely(!desc_addrs[i]))
1411 if (unlikely(lens[i] != descs[avail_idx + i].len))
1418 static __rte_always_inline void
1419 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1420 struct vhost_virtqueue *vq,
1421 struct rte_mbuf **pkts,
1422 uint64_t *desc_addrs,
1425 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1426 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1427 struct vring_packed_desc *descs = vq->desc_packed;
1428 uint16_t avail_idx = vq->last_avail_idx;
1429 uint16_t ids[PACKED_BATCH_SIZE];
1432 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1433 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1434 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1435 (uintptr_t)desc_addrs[i];
1436 lens[i] = pkts[i]->pkt_len +
1437 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1440 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1441 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1443 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1445 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1446 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1447 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1451 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1452 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1455 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1456 ids[i] = descs[avail_idx + i].id;
1458 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1461 static __rte_always_inline int
1462 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1463 struct vhost_virtqueue *vq,
1464 struct rte_mbuf **pkts)
1466 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1467 uint64_t lens[PACKED_BATCH_SIZE];
1469 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1472 if (vq->shadow_used_idx) {
1473 do_data_copy_enqueue(dev, vq);
1474 vhost_flush_enqueue_shadow_packed(dev, vq);
1477 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1482 static __rte_always_inline int16_t
1483 virtio_dev_rx_single_packed(struct virtio_net *dev,
1484 struct vhost_virtqueue *vq,
1485 struct rte_mbuf *pkt)
1487 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1488 uint16_t nr_descs = 0;
1490 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1492 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1497 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1498 dev->ifname, vq->last_avail_idx,
1499 vq->last_avail_idx + nr_descs);
1501 vq_inc_last_avail_packed(vq, nr_descs);
1506 static __rte_noinline uint32_t
1507 virtio_dev_rx_packed(struct virtio_net *dev,
1508 struct vhost_virtqueue *__rte_restrict vq,
1509 struct rte_mbuf **__rte_restrict pkts,
1512 uint32_t pkt_idx = 0;
1515 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1517 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1518 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1520 pkt_idx += PACKED_BATCH_SIZE;
1525 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1529 } while (pkt_idx < count);
1531 if (vq->shadow_used_idx) {
1532 do_data_copy_enqueue(dev, vq);
1533 vhost_flush_enqueue_shadow_packed(dev, vq);
1537 vhost_vring_call_packed(dev, vq);
1542 static __rte_always_inline uint32_t
1543 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1544 struct rte_mbuf **pkts, uint32_t count)
1546 struct vhost_virtqueue *vq;
1549 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1550 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1551 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1552 dev->ifname, __func__, queue_id);
1556 vq = dev->virtqueue[queue_id];
1558 rte_spinlock_lock(&vq->access_lock);
1560 if (unlikely(!vq->enabled))
1561 goto out_access_unlock;
1563 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1564 vhost_user_iotlb_rd_lock(vq);
1566 if (unlikely(!vq->access_ok))
1567 if (unlikely(vring_translate(dev, vq) < 0))
1570 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1574 if (vq_is_packed(dev))
1575 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1577 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1579 vhost_queue_stats_update(dev, vq, pkts, nb_tx);
1582 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1583 vhost_user_iotlb_rd_unlock(vq);
1586 rte_spinlock_unlock(&vq->access_lock);
1592 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1593 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1595 struct virtio_net *dev = get_device(vid);
1600 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1601 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1602 dev->ifname, __func__);
1606 return virtio_dev_rx(dev, queue_id, pkts, count);
1609 static __rte_always_inline uint16_t
1610 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1612 struct vhost_async *async = vq->async;
1614 if (async->pkts_idx >= async->pkts_inflight_n)
1615 return async->pkts_idx - async->pkts_inflight_n;
1617 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1620 static __rte_always_inline void
1621 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1622 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1624 size_t elem_size = sizeof(struct vring_used_elem);
1626 if (d_idx + count <= ring_size) {
1627 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1629 uint16_t size = ring_size - d_idx;
1631 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1632 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1636 static __rte_always_inline void
1637 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1638 struct vring_used_elem_packed *d_ring,
1639 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1641 size_t elem_size = sizeof(struct vring_used_elem_packed);
1643 if (d_idx + count <= ring_size) {
1644 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1646 uint16_t size = ring_size - d_idx;
1648 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1649 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1653 static __rte_noinline uint32_t
1654 virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1655 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1656 int16_t dma_id, uint16_t vchan_id)
1658 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1659 uint32_t pkt_idx = 0;
1660 uint16_t num_buffers;
1661 uint16_t avail_head;
1663 struct vhost_async *async = vq->async;
1664 struct async_inflight_info *pkts_info = async->pkts_info;
1665 uint32_t pkt_err = 0;
1667 uint16_t slot_idx = 0;
1670 * The ordering between avail index and desc reads need to be enforced.
1672 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1674 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1676 async_iter_reset(async);
1678 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1679 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1680 uint16_t nr_vec = 0;
1682 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1683 &num_buffers, avail_head, &nr_vec) < 0)) {
1684 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1686 vq->shadow_used_idx -= num_buffers;
1690 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1691 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1693 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1694 vq->shadow_used_idx -= num_buffers;
1698 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1699 pkts_info[slot_idx].descs = num_buffers;
1700 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1702 vq->last_avail_idx += num_buffers;
1705 if (unlikely(pkt_idx == 0))
1708 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1709 async->iov_iter, pkt_idx);
1711 pkt_err = pkt_idx - n_xfer;
1712 if (unlikely(pkt_err)) {
1713 uint16_t num_descs = 0;
1715 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1716 dev->ifname, __func__, pkt_err, queue_id);
1718 /* update number of completed packets */
1721 /* calculate the sum of descriptors to revert */
1722 while (pkt_err-- > 0) {
1723 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1727 /* recover shadow used ring and available ring */
1728 vq->shadow_used_idx -= num_descs;
1729 vq->last_avail_idx -= num_descs;
1732 /* keep used descriptors */
1733 if (likely(vq->shadow_used_idx)) {
1734 uint16_t to = async->desc_idx_split & (vq->size - 1);
1736 store_dma_desc_info_split(vq->shadow_used_split,
1737 async->descs_split, vq->size, 0, to,
1738 vq->shadow_used_idx);
1740 async->desc_idx_split += vq->shadow_used_idx;
1742 async->pkts_idx += pkt_idx;
1743 if (async->pkts_idx >= vq->size)
1744 async->pkts_idx -= vq->size;
1746 async->pkts_inflight_n += pkt_idx;
1747 vq->shadow_used_idx = 0;
1754 static __rte_always_inline int
1755 vhost_enqueue_async_packed(struct virtio_net *dev,
1756 struct vhost_virtqueue *vq,
1757 struct rte_mbuf *pkt,
1758 struct buf_vector *buf_vec,
1760 uint16_t *nr_buffers)
1762 uint16_t nr_vec = 0;
1763 uint16_t avail_idx = vq->last_avail_idx;
1764 uint16_t max_tries, tries = 0;
1765 uint16_t buf_id = 0;
1767 uint16_t desc_count = 0;
1768 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1769 uint32_t buffer_len[vq->size];
1770 uint16_t buffer_buf_id[vq->size];
1771 uint16_t buffer_desc_count[vq->size];
1773 if (rxvq_is_mergeable(dev))
1774 max_tries = vq->size - 1;
1780 * if we tried all available ring items, and still
1781 * can't get enough buf, it means something abnormal
1784 if (unlikely(++tries > max_tries))
1787 if (unlikely(fill_vec_buf_packed(dev, vq,
1788 avail_idx, &desc_count,
1791 VHOST_ACCESS_RW) < 0))
1794 len = RTE_MIN(len, size);
1797 buffer_len[*nr_buffers] = len;
1798 buffer_buf_id[*nr_buffers] = buf_id;
1799 buffer_desc_count[*nr_buffers] = desc_count;
1801 *nr_descs += desc_count;
1802 avail_idx += desc_count;
1803 if (avail_idx >= vq->size)
1804 avail_idx -= vq->size;
1807 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1810 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1815 static __rte_always_inline int16_t
1816 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1817 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1819 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1821 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1822 nr_descs, nr_buffers) < 0)) {
1823 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1827 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1828 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1833 static __rte_always_inline void
1834 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1835 uint32_t nr_err, uint32_t *pkt_idx)
1837 uint16_t descs_err = 0;
1838 uint16_t buffers_err = 0;
1839 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1842 /* calculate the sum of buffers and descs of DMA-error packets. */
1843 while (nr_err-- > 0) {
1844 descs_err += pkts_info[slot_idx % vq->size].descs;
1845 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1849 if (vq->last_avail_idx >= descs_err) {
1850 vq->last_avail_idx -= descs_err;
1852 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1853 vq->avail_wrap_counter ^= 1;
1856 vq->shadow_used_idx -= buffers_err;
1859 static __rte_noinline uint32_t
1860 virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1861 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1862 int16_t dma_id, uint16_t vchan_id)
1864 uint32_t pkt_idx = 0;
1865 uint32_t remained = count;
1867 uint16_t num_buffers;
1870 struct vhost_async *async = vq->async;
1871 struct async_inflight_info *pkts_info = async->pkts_info;
1872 uint32_t pkt_err = 0;
1873 uint16_t slot_idx = 0;
1876 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1880 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1881 &num_descs, &num_buffers) < 0))
1884 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1886 pkts_info[slot_idx].descs = num_descs;
1887 pkts_info[slot_idx].nr_buffers = num_buffers;
1888 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1892 vq_inc_last_avail_packed(vq, num_descs);
1893 } while (pkt_idx < count);
1895 if (unlikely(pkt_idx == 0))
1898 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1899 async->iov_iter, pkt_idx);
1901 async_iter_reset(async);
1903 pkt_err = pkt_idx - n_xfer;
1904 if (unlikely(pkt_err)) {
1905 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1906 dev->ifname, __func__, pkt_err, queue_id);
1907 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1910 if (likely(vq->shadow_used_idx)) {
1911 /* keep used descriptors. */
1912 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1913 vq->size, 0, async->buffer_idx_packed,
1914 vq->shadow_used_idx);
1916 async->buffer_idx_packed += vq->shadow_used_idx;
1917 if (async->buffer_idx_packed >= vq->size)
1918 async->buffer_idx_packed -= vq->size;
1920 async->pkts_idx += pkt_idx;
1921 if (async->pkts_idx >= vq->size)
1922 async->pkts_idx -= vq->size;
1924 vq->shadow_used_idx = 0;
1925 async->pkts_inflight_n += pkt_idx;
1931 static __rte_always_inline void
1932 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1934 struct vhost_async *async = vq->async;
1935 uint16_t nr_left = n_descs;
1940 from = async->last_desc_idx_split & (vq->size - 1);
1941 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1942 to = vq->last_used_idx & (vq->size - 1);
1944 if (to + nr_copy <= vq->size) {
1945 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1946 nr_copy * sizeof(struct vring_used_elem));
1948 uint16_t size = vq->size - to;
1950 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1951 size * sizeof(struct vring_used_elem));
1952 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1953 (nr_copy - size) * sizeof(struct vring_used_elem));
1956 async->last_desc_idx_split += nr_copy;
1957 vq->last_used_idx += nr_copy;
1959 } while (nr_left > 0);
1962 static __rte_always_inline void
1963 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1966 struct vhost_async *async = vq->async;
1967 uint16_t from = async->last_buffer_idx_packed;
1968 uint16_t used_idx = vq->last_used_idx;
1969 uint16_t head_idx = vq->last_used_idx;
1970 uint16_t head_flags = 0;
1973 /* Split loop in two to save memory barriers */
1974 for (i = 0; i < n_buffers; i++) {
1975 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1976 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1978 used_idx += async->buffers_packed[from].count;
1979 if (used_idx >= vq->size)
1980 used_idx -= vq->size;
1983 if (from >= vq->size)
1987 /* The ordering for storing desc flags needs to be enforced. */
1988 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1990 from = async->last_buffer_idx_packed;
1992 for (i = 0; i < n_buffers; i++) {
1995 if (async->buffers_packed[from].len)
1996 flags = VRING_DESC_F_WRITE;
2000 if (vq->used_wrap_counter) {
2001 flags |= VRING_DESC_F_USED;
2002 flags |= VRING_DESC_F_AVAIL;
2004 flags &= ~VRING_DESC_F_USED;
2005 flags &= ~VRING_DESC_F_AVAIL;
2009 vq->desc_packed[vq->last_used_idx].flags = flags;
2011 head_idx = vq->last_used_idx;
2015 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
2018 if (from == vq->size)
2022 vq->desc_packed[head_idx].flags = head_flags;
2023 async->last_buffer_idx_packed = from;
2026 static __rte_always_inline uint16_t
2027 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
2028 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2031 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
2032 struct vhost_async *async = vq->async;
2033 struct async_inflight_info *pkts_info = async->pkts_info;
2034 uint16_t nr_cpl_pkts = 0;
2035 uint16_t n_descs = 0, n_buffers = 0;
2036 uint16_t start_idx, from, i;
2038 /* Check completed copies for the given DMA vChannel */
2039 vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
2041 start_idx = async_get_first_inflight_pkt_idx(vq);
2043 * Calculate the number of copy completed packets.
2044 * Note that there may be completed packets even if
2045 * no copies are reported done by the given DMA vChannel,
2046 * as it's possible that a virtqueue uses multiple DMA
2050 while (vq->async->pkts_cmpl_flag[from] && count--) {
2051 vq->async->pkts_cmpl_flag[from] = false;
2053 if (from >= vq->size)
2058 if (nr_cpl_pkts == 0)
2061 for (i = 0; i < nr_cpl_pkts; i++) {
2062 from = (start_idx + i) % vq->size;
2063 /* Only used with packed ring */
2064 n_buffers += pkts_info[from].nr_buffers;
2065 /* Only used with split ring */
2066 n_descs += pkts_info[from].descs;
2067 pkts[i] = pkts_info[from].mbuf;
2070 async->pkts_inflight_n -= nr_cpl_pkts;
2072 if (likely(vq->enabled && vq->access_ok)) {
2073 if (vq_is_packed(dev)) {
2074 write_back_completed_descs_packed(vq, n_buffers);
2075 vhost_vring_call_packed(dev, vq);
2077 write_back_completed_descs_split(vq, n_descs);
2078 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
2079 vhost_vring_call_split(dev, vq);
2082 if (vq_is_packed(dev)) {
2083 async->last_buffer_idx_packed += n_buffers;
2084 if (async->last_buffer_idx_packed >= vq->size)
2085 async->last_buffer_idx_packed -= vq->size;
2087 async->last_desc_idx_split += n_descs;
2095 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2096 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2099 struct virtio_net *dev = get_device(vid);
2100 struct vhost_virtqueue *vq;
2101 uint16_t n_pkts_cpl = 0;
2106 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2107 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2108 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2109 dev->ifname, __func__, queue_id);
2113 if (unlikely(!dma_copy_track[dma_id].vchans ||
2114 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2115 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2120 vq = dev->virtqueue[queue_id];
2122 if (!rte_spinlock_trylock(&vq->access_lock)) {
2123 VHOST_LOG_DATA(DEBUG, "(%s) %s: virtqueue %u is busy.\n", dev->ifname, __func__,
2128 if (unlikely(!vq->async)) {
2129 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for virtqueue %d.\n",
2130 dev->ifname, __func__, queue_id);
2134 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2136 vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2137 vq->stats.inflight_completed += n_pkts_cpl;
2140 rte_spinlock_unlock(&vq->access_lock);
2146 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2147 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2150 struct virtio_net *dev = get_device(vid);
2151 struct vhost_virtqueue *vq;
2152 uint16_t n_pkts_cpl = 0;
2157 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2158 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2159 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2160 dev->ifname, __func__, queue_id);
2164 vq = dev->virtqueue[queue_id];
2166 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
2167 VHOST_LOG_DATA(ERR, "(%s) %s() called without access lock taken.\n",
2168 dev->ifname, __func__);
2172 if (unlikely(!vq->async)) {
2173 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
2174 dev->ifname, __func__, queue_id);
2178 if (unlikely(!dma_copy_track[dma_id].vchans ||
2179 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2180 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2185 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2187 vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2188 vq->stats.inflight_completed += n_pkts_cpl;
2193 static __rte_always_inline uint32_t
2194 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2195 struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2197 struct vhost_virtqueue *vq;
2200 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2201 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2202 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2203 dev->ifname, __func__, queue_id);
2207 if (unlikely(!dma_copy_track[dma_id].vchans ||
2208 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2209 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2214 vq = dev->virtqueue[queue_id];
2216 rte_spinlock_lock(&vq->access_lock);
2218 if (unlikely(!vq->enabled || !vq->async))
2219 goto out_access_unlock;
2221 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2222 vhost_user_iotlb_rd_lock(vq);
2224 if (unlikely(!vq->access_ok))
2225 if (unlikely(vring_translate(dev, vq) < 0))
2228 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2232 if (vq_is_packed(dev))
2233 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2234 pkts, count, dma_id, vchan_id);
2236 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2237 pkts, count, dma_id, vchan_id);
2239 vq->stats.inflight_submitted += nb_tx;
2242 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2243 vhost_user_iotlb_rd_unlock(vq);
2246 rte_spinlock_unlock(&vq->access_lock);
2252 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2253 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2256 struct virtio_net *dev = get_device(vid);
2261 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2262 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2263 dev->ifname, __func__);
2267 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, dma_id, vchan_id);
2271 virtio_net_with_host_offload(struct virtio_net *dev)
2274 ((1ULL << VIRTIO_NET_F_CSUM) |
2275 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2276 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2277 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2278 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2285 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2287 struct rte_ipv4_hdr *ipv4_hdr;
2288 struct rte_ipv6_hdr *ipv6_hdr;
2289 struct rte_ether_hdr *eth_hdr;
2291 uint16_t data_len = rte_pktmbuf_data_len(m);
2293 if (data_len < sizeof(struct rte_ether_hdr))
2296 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2298 m->l2_len = sizeof(struct rte_ether_hdr);
2299 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2301 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2302 if (data_len < sizeof(struct rte_ether_hdr) +
2303 sizeof(struct rte_vlan_hdr))
2306 struct rte_vlan_hdr *vlan_hdr =
2307 (struct rte_vlan_hdr *)(eth_hdr + 1);
2309 m->l2_len += sizeof(struct rte_vlan_hdr);
2310 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2313 switch (ethertype) {
2314 case RTE_ETHER_TYPE_IPV4:
2315 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2317 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2319 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2320 if (data_len < m->l2_len + m->l3_len)
2322 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2323 *l4_proto = ipv4_hdr->next_proto_id;
2325 case RTE_ETHER_TYPE_IPV6:
2326 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2328 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2330 m->l3_len = sizeof(struct rte_ipv6_hdr);
2331 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2332 *l4_proto = ipv6_hdr->proto;
2335 /* a valid L3 header is needed for further L4 parsing */
2339 /* both CSUM and GSO need a valid L4 header */
2340 switch (*l4_proto) {
2342 if (data_len < m->l2_len + m->l3_len +
2343 sizeof(struct rte_tcp_hdr))
2347 if (data_len < m->l2_len + m->l3_len +
2348 sizeof(struct rte_udp_hdr))
2352 if (data_len < m->l2_len + m->l3_len +
2353 sizeof(struct rte_sctp_hdr))
2369 static __rte_always_inline void
2370 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2373 uint8_t l4_proto = 0;
2374 struct rte_tcp_hdr *tcp_hdr = NULL;
2376 uint16_t data_len = rte_pktmbuf_data_len(m);
2378 if (parse_headers(m, &l4_proto) < 0)
2381 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2382 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2383 switch (hdr->csum_offset) {
2384 case (offsetof(struct rte_tcp_hdr, cksum)):
2385 if (l4_proto != IPPROTO_TCP)
2387 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2389 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2390 if (l4_proto != IPPROTO_UDP)
2392 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2394 case (offsetof(struct rte_sctp_hdr, cksum)):
2395 if (l4_proto != IPPROTO_SCTP)
2397 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2407 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2408 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2409 case VIRTIO_NET_HDR_GSO_TCPV4:
2410 case VIRTIO_NET_HDR_GSO_TCPV6:
2411 if (l4_proto != IPPROTO_TCP)
2413 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2414 struct rte_tcp_hdr *,
2415 m->l2_len + m->l3_len);
2416 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2417 if (data_len < m->l2_len + m->l3_len + tcp_len)
2419 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2420 m->tso_segsz = hdr->gso_size;
2421 m->l4_len = tcp_len;
2423 case VIRTIO_NET_HDR_GSO_UDP:
2424 if (l4_proto != IPPROTO_UDP)
2426 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2427 m->tso_segsz = hdr->gso_size;
2428 m->l4_len = sizeof(struct rte_udp_hdr);
2431 VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2432 dev->ifname, hdr->gso_type);
2444 static __rte_always_inline void
2445 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2446 struct rte_mbuf *m, bool legacy_ol_flags)
2448 struct rte_net_hdr_lens hdr_lens;
2449 int l4_supported = 0;
2452 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2455 if (legacy_ol_flags) {
2456 vhost_dequeue_offload_legacy(dev, hdr, m);
2460 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2462 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2463 m->packet_type = ptype;
2464 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2465 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2466 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2469 /* According to Virtio 1.1 spec, the device only needs to look at
2470 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2471 * This differs from the processing incoming packets path where the
2472 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2475 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2476 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2477 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2479 * 5.1.6.2.2 Device Requirements: Packet Transmission
2480 * The device MUST ignore flag bits that it does not recognize.
2482 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2485 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2486 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2487 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2489 /* Unknown proto or tunnel, do sw cksum. We can assume
2490 * the cksum field is in the first segment since the
2491 * buffers we provided to the host are large enough.
2492 * In case of SCTP, this will be wrong since it's a CRC
2493 * but there's nothing we can do.
2495 uint16_t csum = 0, off;
2497 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2498 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2500 if (likely(csum != 0xffff))
2502 off = hdr->csum_offset + hdr->csum_start;
2503 if (rte_pktmbuf_data_len(m) >= off + 1)
2504 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2508 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2509 if (hdr->gso_size == 0)
2512 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2513 case VIRTIO_NET_HDR_GSO_TCPV4:
2514 case VIRTIO_NET_HDR_GSO_TCPV6:
2515 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2517 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2518 m->tso_segsz = hdr->gso_size;
2520 case VIRTIO_NET_HDR_GSO_UDP:
2521 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2523 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2524 m->tso_segsz = hdr->gso_size;
2532 static __rte_noinline void
2533 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2534 struct buf_vector *buf_vec)
2537 uint64_t remain = sizeof(struct virtio_net_hdr);
2539 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2542 len = RTE_MIN(remain, buf_vec->buf_len);
2543 src = buf_vec->buf_addr;
2544 rte_memcpy((void *)(uintptr_t)dst,
2545 (void *)(uintptr_t)src, len);
2553 static __rte_always_inline int
2554 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2555 struct buf_vector *buf_vec, uint16_t nr_vec,
2556 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2557 bool legacy_ol_flags)
2559 uint32_t buf_avail, buf_offset, buf_len;
2560 uint64_t buf_addr, buf_iova;
2561 uint32_t mbuf_avail, mbuf_offset;
2563 struct rte_mbuf *cur = m, *prev = m;
2564 struct virtio_net_hdr tmp_hdr;
2565 struct virtio_net_hdr *hdr = NULL;
2566 /* A counter to avoid desc dead loop chain */
2567 uint16_t vec_idx = 0;
2569 buf_addr = buf_vec[vec_idx].buf_addr;
2570 buf_iova = buf_vec[vec_idx].buf_iova;
2571 buf_len = buf_vec[vec_idx].buf_len;
2573 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
2576 if (virtio_net_with_host_offload(dev)) {
2577 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2579 * No luck, the virtio-net header doesn't fit
2580 * in a contiguous virtual area.
2582 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2585 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2590 * A virtio driver normally uses at least 2 desc buffers
2591 * for Tx: the first for storing the header, and others
2592 * for storing the data.
2594 if (unlikely(buf_len < dev->vhost_hlen)) {
2595 buf_offset = dev->vhost_hlen - buf_len;
2597 buf_addr = buf_vec[vec_idx].buf_addr;
2598 buf_iova = buf_vec[vec_idx].buf_iova;
2599 buf_len = buf_vec[vec_idx].buf_len;
2600 buf_avail = buf_len - buf_offset;
2601 } else if (buf_len == dev->vhost_hlen) {
2602 if (unlikely(++vec_idx >= nr_vec))
2604 buf_addr = buf_vec[vec_idx].buf_addr;
2605 buf_len = buf_vec[vec_idx].buf_len;
2608 buf_avail = buf_len;
2610 buf_offset = dev->vhost_hlen;
2611 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2615 (uintptr_t)(buf_addr + buf_offset),
2616 (uint32_t)buf_avail, 0);
2619 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2621 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2623 sync_fill_seg(dev, vq, cur, mbuf_offset,
2624 buf_addr + buf_offset,
2625 buf_iova + buf_offset, cpy_len, false);
2627 mbuf_avail -= cpy_len;
2628 mbuf_offset += cpy_len;
2629 buf_avail -= cpy_len;
2630 buf_offset += cpy_len;
2632 /* This buf reaches to its end, get the next one */
2633 if (buf_avail == 0) {
2634 if (++vec_idx >= nr_vec)
2637 buf_addr = buf_vec[vec_idx].buf_addr;
2638 buf_iova = buf_vec[vec_idx].buf_iova;
2639 buf_len = buf_vec[vec_idx].buf_len;
2642 buf_avail = buf_len;
2644 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2645 (uint32_t)buf_avail, 0);
2649 * This mbuf reaches to its end, get a new one
2650 * to hold more data.
2652 if (mbuf_avail == 0) {
2653 cur = rte_pktmbuf_alloc(mbuf_pool);
2654 if (unlikely(cur == NULL)) {
2655 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2661 prev->data_len = mbuf_offset;
2663 m->pkt_len += mbuf_offset;
2667 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2671 prev->data_len = mbuf_offset;
2672 m->pkt_len += mbuf_offset;
2675 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2683 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2689 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2691 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2692 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2697 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2698 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2700 if (unlikely(total_len > UINT16_MAX))
2703 buf_len = total_len;
2704 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2705 if (unlikely(buf == NULL))
2708 /* Initialize shinfo */
2709 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2710 virtio_dev_extbuf_free, buf);
2711 if (unlikely(shinfo == NULL)) {
2713 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2717 iova = rte_malloc_virt2iova(buf);
2718 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2719 rte_pktmbuf_reset_headroom(pkt);
2725 * Prepare a host supported pktmbuf.
2727 static __rte_always_inline int
2728 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2731 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2734 /* attach an external buffer if supported */
2735 if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2738 /* check if chained buffers are allowed */
2739 if (!dev->linearbuf)
2747 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2748 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2749 bool legacy_ol_flags)
2752 uint16_t free_entries;
2753 uint16_t dropped = 0;
2754 static bool allocerr_warned;
2757 * The ordering between avail index and
2758 * desc reads needs to be enforced.
2760 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2762 if (free_entries == 0)
2765 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2767 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2769 count = RTE_MIN(count, MAX_PKT_BURST);
2770 count = RTE_MIN(count, free_entries);
2771 VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2772 dev->ifname, count);
2774 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2777 for (i = 0; i < count; i++) {
2778 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2781 uint16_t nr_vec = 0;
2784 if (unlikely(fill_vec_buf_split(dev, vq,
2785 vq->last_avail_idx + i,
2787 &head_idx, &buf_len,
2788 VHOST_ACCESS_RO) < 0))
2791 update_shadow_used_ring_split(vq, head_idx, 0);
2793 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2794 if (unlikely(err)) {
2796 * mbuf allocation fails for jumbo packets when external
2797 * buffer allocation is not allowed and linear buffer
2798 * is required. Drop this packet.
2800 if (!allocerr_warned) {
2801 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2802 dev->ifname, buf_len, mbuf_pool->name);
2803 allocerr_warned = true;
2810 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2811 mbuf_pool, legacy_ol_flags);
2812 if (unlikely(err)) {
2813 if (!allocerr_warned) {
2814 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2816 allocerr_warned = true;
2825 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2827 vq->last_avail_idx += i;
2829 do_data_copy_dequeue(vq);
2830 if (unlikely(i < count))
2831 vq->shadow_used_idx = i;
2832 if (likely(vq->shadow_used_idx)) {
2833 flush_shadow_used_ring_split(dev, vq);
2834 vhost_vring_call_split(dev, vq);
2837 return (i - dropped);
2842 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2843 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2844 struct rte_mbuf **pkts, uint16_t count)
2846 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2851 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2852 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2853 struct rte_mbuf **pkts, uint16_t count)
2855 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2858 static __rte_always_inline int
2859 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2860 struct vhost_virtqueue *vq,
2861 struct rte_mbuf **pkts,
2863 uintptr_t *desc_addrs,
2866 bool wrap = vq->avail_wrap_counter;
2867 struct vring_packed_desc *descs = vq->desc_packed;
2868 uint64_t lens[PACKED_BATCH_SIZE];
2869 uint64_t buf_lens[PACKED_BATCH_SIZE];
2870 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2873 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2875 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2878 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2879 flags = descs[avail_idx + i].flags;
2880 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2881 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2882 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2886 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2888 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2889 lens[i] = descs[avail_idx + i].len;
2891 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2892 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2893 descs[avail_idx + i].addr,
2894 &lens[i], VHOST_ACCESS_RW);
2897 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2898 if (unlikely(!desc_addrs[i]))
2900 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2904 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2905 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2909 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2910 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2912 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2913 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2917 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2918 pkts[i]->pkt_len = lens[i] - buf_offset;
2919 pkts[i]->data_len = pkts[i]->pkt_len;
2920 ids[i] = descs[avail_idx + i].id;
2929 static __rte_always_inline int
2930 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2931 struct vhost_virtqueue *vq,
2932 struct rte_mbuf **pkts,
2933 bool legacy_ol_flags)
2935 uint16_t avail_idx = vq->last_avail_idx;
2936 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2937 struct virtio_net_hdr *hdr;
2938 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2939 uint16_t ids[PACKED_BATCH_SIZE];
2942 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2946 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2947 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2949 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2950 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2951 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2954 if (virtio_net_with_host_offload(dev)) {
2955 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2956 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2957 vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2961 if (virtio_net_is_inorder(dev))
2962 vhost_shadow_dequeue_batch_packed_inorder(vq,
2963 ids[PACKED_BATCH_SIZE - 1]);
2965 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2967 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2972 static __rte_always_inline int
2973 vhost_dequeue_single_packed(struct virtio_net *dev,
2974 struct vhost_virtqueue *vq,
2975 struct rte_mempool *mbuf_pool,
2976 struct rte_mbuf *pkts,
2978 uint16_t *desc_count,
2979 bool legacy_ol_flags)
2981 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2983 uint16_t nr_vec = 0;
2985 static bool allocerr_warned;
2987 if (unlikely(fill_vec_buf_packed(dev, vq,
2988 vq->last_avail_idx, desc_count,
2991 VHOST_ACCESS_RO) < 0))
2994 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2995 if (!allocerr_warned) {
2996 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2997 dev->ifname, buf_len, mbuf_pool->name);
2998 allocerr_warned = true;
3003 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
3004 mbuf_pool, legacy_ol_flags);
3005 if (unlikely(err)) {
3006 if (!allocerr_warned) {
3007 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
3009 allocerr_warned = true;
3017 static __rte_always_inline int
3018 virtio_dev_tx_single_packed(struct virtio_net *dev,
3019 struct vhost_virtqueue *vq,
3020 struct rte_mempool *mbuf_pool,
3021 struct rte_mbuf *pkts,
3022 bool legacy_ol_flags)
3025 uint16_t buf_id, desc_count = 0;
3028 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
3029 &desc_count, legacy_ol_flags);
3031 if (likely(desc_count > 0)) {
3032 if (virtio_net_is_inorder(dev))
3033 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
3036 vhost_shadow_dequeue_single_packed(vq, buf_id,
3039 vq_inc_last_avail_packed(vq, desc_count);
3047 virtio_dev_tx_packed(struct virtio_net *dev,
3048 struct vhost_virtqueue *__rte_restrict vq,
3049 struct rte_mempool *mbuf_pool,
3050 struct rte_mbuf **__rte_restrict pkts,
3052 bool legacy_ol_flags)
3054 uint32_t pkt_idx = 0;
3056 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
3060 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3062 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3063 if (!virtio_dev_tx_batch_packed(dev, vq,
3066 pkt_idx += PACKED_BATCH_SIZE;
3071 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3076 } while (pkt_idx < count);
3078 if (pkt_idx != count)
3079 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3081 if (vq->shadow_used_idx) {
3082 do_data_copy_dequeue(vq);
3084 vhost_flush_dequeue_shadow_packed(dev, vq);
3085 vhost_vring_call_packed(dev, vq);
3093 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3094 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3095 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3097 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3102 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3103 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3104 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3106 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3110 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3111 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3113 struct virtio_net *dev;
3114 struct rte_mbuf *rarp_mbuf = NULL;
3115 struct vhost_virtqueue *vq;
3116 int16_t success = 1;
3118 dev = get_device(vid);
3122 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3123 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
3124 dev->ifname, __func__);
3128 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3129 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
3130 dev->ifname, __func__, queue_id);
3134 vq = dev->virtqueue[queue_id];
3136 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3139 if (unlikely(!vq->enabled)) {
3141 goto out_access_unlock;
3144 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3145 vhost_user_iotlb_rd_lock(vq);
3147 if (unlikely(!vq->access_ok))
3148 if (unlikely(vring_translate(dev, vq) < 0)) {
3154 * Construct a RARP broadcast packet, and inject it to the "pkts"
3155 * array, to looks like that guest actually send such packet.
3157 * Check user_send_rarp() for more information.
3159 * broadcast_rarp shares a cacheline in the virtio_net structure
3160 * with some fields that are accessed during enqueue and
3161 * __atomic_compare_exchange_n causes a write if performed compare
3162 * and exchange. This could result in false sharing between enqueue
3165 * Prevent unnecessary false sharing by reading broadcast_rarp first
3166 * and only performing compare and exchange if the read indicates it
3167 * is likely to be set.
3169 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3170 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3171 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3173 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3174 if (rarp_mbuf == NULL) {
3175 VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
3180 * Inject it to the head of "pkts" array, so that switch's mac
3181 * learning table will get updated first.
3183 pkts[0] = rarp_mbuf;
3184 vhost_queue_stats_update(dev, vq, pkts, 1);
3189 if (vq_is_packed(dev)) {
3190 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3191 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3193 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3195 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3196 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3198 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3201 vhost_queue_stats_update(dev, vq, pkts, count);
3204 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3205 vhost_user_iotlb_rd_unlock(vq);
3208 rte_spinlock_unlock(&vq->access_lock);
3210 if (unlikely(rarp_mbuf != NULL))