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_mbuf_to_desc_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)
1052 struct vhost_async *async = vq->async;
1053 uint64_t mapped_len;
1054 uint32_t buf_offset = 0;
1058 host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1059 buf_iova + buf_offset, cpy_len, &mapped_len);
1060 if (unlikely(!host_iova)) {
1061 VHOST_LOG_DATA(ERR, "(%s) %s: failed to get host iova.\n",
1062 dev->ifname, __func__);
1066 if (unlikely(async_iter_add_iovec(dev, async,
1067 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1069 host_iova, (size_t)mapped_len)))
1072 cpy_len -= (uint32_t)mapped_len;
1073 mbuf_offset += (uint32_t)mapped_len;
1074 buf_offset += (uint32_t)mapped_len;
1080 static __rte_always_inline void
1081 sync_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1082 struct rte_mbuf *m, uint32_t mbuf_offset,
1083 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
1085 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1087 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
1089 rte_memcpy((void *)((uintptr_t)(buf_addr)),
1090 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1093 rte_memcpy(rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1094 (void *)((uintptr_t)(buf_addr)),
1097 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1098 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1101 batch_copy[vq->batch_copy_nb_elems].dst =
1102 (void *)((uintptr_t)(buf_addr));
1103 batch_copy[vq->batch_copy_nb_elems].src =
1104 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1106 batch_copy[vq->batch_copy_nb_elems].dst =
1107 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1108 batch_copy[vq->batch_copy_nb_elems].src =
1109 (void *)((uintptr_t)(buf_addr));
1111 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1112 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1113 vq->batch_copy_nb_elems++;
1117 static __rte_always_inline int
1118 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1119 struct rte_mbuf *m, struct buf_vector *buf_vec,
1120 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1122 uint32_t vec_idx = 0;
1123 uint32_t mbuf_offset, mbuf_avail;
1124 uint32_t buf_offset, buf_avail;
1125 uint64_t buf_addr, buf_iova, buf_len;
1128 struct rte_mbuf *hdr_mbuf;
1129 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1130 struct vhost_async *async = vq->async;
1132 if (unlikely(m == NULL))
1135 buf_addr = buf_vec[vec_idx].buf_addr;
1136 buf_iova = buf_vec[vec_idx].buf_iova;
1137 buf_len = buf_vec[vec_idx].buf_len;
1139 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1143 hdr_addr = buf_addr;
1144 if (unlikely(buf_len < dev->vhost_hlen)) {
1145 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1148 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1150 VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
1151 dev->ifname, num_buffers);
1153 if (unlikely(buf_len < dev->vhost_hlen)) {
1154 buf_offset = dev->vhost_hlen - buf_len;
1156 buf_addr = buf_vec[vec_idx].buf_addr;
1157 buf_iova = buf_vec[vec_idx].buf_iova;
1158 buf_len = buf_vec[vec_idx].buf_len;
1159 buf_avail = buf_len - buf_offset;
1161 buf_offset = dev->vhost_hlen;
1162 buf_avail = buf_len - dev->vhost_hlen;
1165 mbuf_avail = rte_pktmbuf_data_len(m);
1169 if (async_iter_initialize(dev, async))
1173 while (mbuf_avail != 0 || m->next != NULL) {
1174 /* done with current buf, get the next one */
1175 if (buf_avail == 0) {
1177 if (unlikely(vec_idx >= nr_vec))
1180 buf_addr = buf_vec[vec_idx].buf_addr;
1181 buf_iova = buf_vec[vec_idx].buf_iova;
1182 buf_len = buf_vec[vec_idx].buf_len;
1185 buf_avail = buf_len;
1188 /* done with current mbuf, get the next one */
1189 if (mbuf_avail == 0) {
1193 mbuf_avail = rte_pktmbuf_data_len(m);
1197 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1198 if (rxvq_is_mergeable(dev))
1199 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1202 if (unlikely(hdr == &tmp_hdr)) {
1203 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1205 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1206 dev->vhost_hlen, 0);
1207 vhost_log_cache_write_iova(dev, vq,
1208 buf_vec[0].buf_iova,
1215 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1218 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1219 buf_iova + buf_offset, cpy_len) < 0)
1222 sync_fill_seg(dev, vq, m, mbuf_offset,
1223 buf_addr + buf_offset,
1224 buf_iova + buf_offset, cpy_len, true);
1227 mbuf_avail -= cpy_len;
1228 mbuf_offset += cpy_len;
1229 buf_avail -= cpy_len;
1230 buf_offset += cpy_len;
1234 async_iter_finalize(async);
1239 async_iter_cancel(async);
1244 static __rte_always_inline int
1245 vhost_enqueue_single_packed(struct virtio_net *dev,
1246 struct vhost_virtqueue *vq,
1247 struct rte_mbuf *pkt,
1248 struct buf_vector *buf_vec,
1251 uint16_t nr_vec = 0;
1252 uint16_t avail_idx = vq->last_avail_idx;
1253 uint16_t max_tries, tries = 0;
1254 uint16_t buf_id = 0;
1256 uint16_t desc_count;
1257 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1258 uint16_t num_buffers = 0;
1259 uint32_t buffer_len[vq->size];
1260 uint16_t buffer_buf_id[vq->size];
1261 uint16_t buffer_desc_count[vq->size];
1263 if (rxvq_is_mergeable(dev))
1264 max_tries = vq->size - 1;
1270 * if we tried all available ring items, and still
1271 * can't get enough buf, it means something abnormal
1274 if (unlikely(++tries > max_tries))
1277 if (unlikely(fill_vec_buf_packed(dev, vq,
1278 avail_idx, &desc_count,
1281 VHOST_ACCESS_RW) < 0))
1284 len = RTE_MIN(len, size);
1287 buffer_len[num_buffers] = len;
1288 buffer_buf_id[num_buffers] = buf_id;
1289 buffer_desc_count[num_buffers] = desc_count;
1292 *nr_descs += desc_count;
1293 avail_idx += desc_count;
1294 if (avail_idx >= vq->size)
1295 avail_idx -= vq->size;
1298 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1301 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1302 buffer_desc_count, num_buffers);
1307 static __rte_noinline uint32_t
1308 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1309 struct rte_mbuf **pkts, uint32_t count)
1311 uint32_t pkt_idx = 0;
1312 uint16_t num_buffers;
1313 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1314 uint16_t avail_head;
1317 * The ordering between avail index and
1318 * desc reads needs to be enforced.
1320 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1322 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1324 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1325 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1326 uint16_t nr_vec = 0;
1328 if (unlikely(reserve_avail_buf_split(dev, vq,
1329 pkt_len, buf_vec, &num_buffers,
1330 avail_head, &nr_vec) < 0)) {
1331 VHOST_LOG_DATA(DEBUG,
1332 "(%s) failed to get enough desc from vring\n",
1334 vq->shadow_used_idx -= num_buffers;
1338 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1339 dev->ifname, vq->last_avail_idx,
1340 vq->last_avail_idx + num_buffers);
1342 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1343 num_buffers, false) < 0) {
1344 vq->shadow_used_idx -= num_buffers;
1348 vq->last_avail_idx += num_buffers;
1351 do_data_copy_enqueue(dev, vq);
1353 if (likely(vq->shadow_used_idx)) {
1354 flush_shadow_used_ring_split(dev, vq);
1355 vhost_vring_call_split(dev, vq);
1361 static __rte_always_inline int
1362 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1363 struct vhost_virtqueue *vq,
1364 struct rte_mbuf **pkts,
1365 uint64_t *desc_addrs,
1368 bool wrap_counter = vq->avail_wrap_counter;
1369 struct vring_packed_desc *descs = vq->desc_packed;
1370 uint16_t avail_idx = vq->last_avail_idx;
1371 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1374 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1377 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1380 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1381 if (unlikely(pkts[i]->next != NULL))
1383 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1388 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1389 lens[i] = descs[avail_idx + i].len;
1391 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1392 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1396 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1397 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1398 descs[avail_idx + i].addr,
1402 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1403 if (unlikely(!desc_addrs[i]))
1405 if (unlikely(lens[i] != descs[avail_idx + i].len))
1412 static __rte_always_inline void
1413 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1414 struct vhost_virtqueue *vq,
1415 struct rte_mbuf **pkts,
1416 uint64_t *desc_addrs,
1419 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1420 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1421 struct vring_packed_desc *descs = vq->desc_packed;
1422 uint16_t avail_idx = vq->last_avail_idx;
1423 uint16_t ids[PACKED_BATCH_SIZE];
1426 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1427 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1428 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1429 (uintptr_t)desc_addrs[i];
1430 lens[i] = pkts[i]->pkt_len +
1431 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1434 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1435 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1437 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1439 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1440 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1441 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1445 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1446 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1449 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1450 ids[i] = descs[avail_idx + i].id;
1452 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1455 static __rte_always_inline int
1456 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1457 struct vhost_virtqueue *vq,
1458 struct rte_mbuf **pkts)
1460 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1461 uint64_t lens[PACKED_BATCH_SIZE];
1463 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1466 if (vq->shadow_used_idx) {
1467 do_data_copy_enqueue(dev, vq);
1468 vhost_flush_enqueue_shadow_packed(dev, vq);
1471 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1476 static __rte_always_inline int16_t
1477 virtio_dev_rx_single_packed(struct virtio_net *dev,
1478 struct vhost_virtqueue *vq,
1479 struct rte_mbuf *pkt)
1481 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1482 uint16_t nr_descs = 0;
1484 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1486 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1491 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1492 dev->ifname, vq->last_avail_idx,
1493 vq->last_avail_idx + nr_descs);
1495 vq_inc_last_avail_packed(vq, nr_descs);
1500 static __rte_noinline uint32_t
1501 virtio_dev_rx_packed(struct virtio_net *dev,
1502 struct vhost_virtqueue *__rte_restrict vq,
1503 struct rte_mbuf **__rte_restrict pkts,
1506 uint32_t pkt_idx = 0;
1509 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1511 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1512 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1514 pkt_idx += PACKED_BATCH_SIZE;
1519 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1523 } while (pkt_idx < count);
1525 if (vq->shadow_used_idx) {
1526 do_data_copy_enqueue(dev, vq);
1527 vhost_flush_enqueue_shadow_packed(dev, vq);
1531 vhost_vring_call_packed(dev, vq);
1536 static __rte_always_inline uint32_t
1537 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1538 struct rte_mbuf **pkts, uint32_t count)
1540 struct vhost_virtqueue *vq;
1543 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1544 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1545 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1546 dev->ifname, __func__, queue_id);
1550 vq = dev->virtqueue[queue_id];
1552 rte_spinlock_lock(&vq->access_lock);
1554 if (unlikely(!vq->enabled))
1555 goto out_access_unlock;
1557 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1558 vhost_user_iotlb_rd_lock(vq);
1560 if (unlikely(!vq->access_ok))
1561 if (unlikely(vring_translate(dev, vq) < 0))
1564 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1568 if (vq_is_packed(dev))
1569 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1571 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1573 vhost_queue_stats_update(dev, vq, pkts, nb_tx);
1576 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1577 vhost_user_iotlb_rd_unlock(vq);
1580 rte_spinlock_unlock(&vq->access_lock);
1586 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1587 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1589 struct virtio_net *dev = get_device(vid);
1594 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1595 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1596 dev->ifname, __func__);
1600 return virtio_dev_rx(dev, queue_id, pkts, count);
1603 static __rte_always_inline uint16_t
1604 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1606 struct vhost_async *async = vq->async;
1608 if (async->pkts_idx >= async->pkts_inflight_n)
1609 return async->pkts_idx - async->pkts_inflight_n;
1611 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1614 static __rte_always_inline void
1615 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1616 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1618 size_t elem_size = sizeof(struct vring_used_elem);
1620 if (d_idx + count <= ring_size) {
1621 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1623 uint16_t size = ring_size - d_idx;
1625 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1626 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1630 static __rte_always_inline void
1631 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1632 struct vring_used_elem_packed *d_ring,
1633 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1635 size_t elem_size = sizeof(struct vring_used_elem_packed);
1637 if (d_idx + count <= ring_size) {
1638 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1640 uint16_t size = ring_size - d_idx;
1642 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1643 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1647 static __rte_noinline uint32_t
1648 virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1649 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1650 int16_t dma_id, uint16_t vchan_id)
1652 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1653 uint32_t pkt_idx = 0;
1654 uint16_t num_buffers;
1655 uint16_t avail_head;
1657 struct vhost_async *async = vq->async;
1658 struct async_inflight_info *pkts_info = async->pkts_info;
1659 uint32_t pkt_err = 0;
1661 uint16_t slot_idx = 0;
1664 * The ordering between avail index and desc reads need to be enforced.
1666 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1668 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1670 async_iter_reset(async);
1672 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1673 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1674 uint16_t nr_vec = 0;
1676 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1677 &num_buffers, avail_head, &nr_vec) < 0)) {
1678 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1680 vq->shadow_used_idx -= num_buffers;
1684 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1685 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1687 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1688 vq->shadow_used_idx -= num_buffers;
1692 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1693 pkts_info[slot_idx].descs = num_buffers;
1694 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1696 vq->last_avail_idx += num_buffers;
1699 if (unlikely(pkt_idx == 0))
1702 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1703 async->iov_iter, pkt_idx);
1705 pkt_err = pkt_idx - n_xfer;
1706 if (unlikely(pkt_err)) {
1707 uint16_t num_descs = 0;
1709 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1710 dev->ifname, __func__, pkt_err, queue_id);
1712 /* update number of completed packets */
1715 /* calculate the sum of descriptors to revert */
1716 while (pkt_err-- > 0) {
1717 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1721 /* recover shadow used ring and available ring */
1722 vq->shadow_used_idx -= num_descs;
1723 vq->last_avail_idx -= num_descs;
1726 /* keep used descriptors */
1727 if (likely(vq->shadow_used_idx)) {
1728 uint16_t to = async->desc_idx_split & (vq->size - 1);
1730 store_dma_desc_info_split(vq->shadow_used_split,
1731 async->descs_split, vq->size, 0, to,
1732 vq->shadow_used_idx);
1734 async->desc_idx_split += vq->shadow_used_idx;
1736 async->pkts_idx += pkt_idx;
1737 if (async->pkts_idx >= vq->size)
1738 async->pkts_idx -= vq->size;
1740 async->pkts_inflight_n += pkt_idx;
1741 vq->shadow_used_idx = 0;
1748 static __rte_always_inline int
1749 vhost_enqueue_async_packed(struct virtio_net *dev,
1750 struct vhost_virtqueue *vq,
1751 struct rte_mbuf *pkt,
1752 struct buf_vector *buf_vec,
1754 uint16_t *nr_buffers)
1756 uint16_t nr_vec = 0;
1757 uint16_t avail_idx = vq->last_avail_idx;
1758 uint16_t max_tries, tries = 0;
1759 uint16_t buf_id = 0;
1761 uint16_t desc_count = 0;
1762 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1763 uint32_t buffer_len[vq->size];
1764 uint16_t buffer_buf_id[vq->size];
1765 uint16_t buffer_desc_count[vq->size];
1767 if (rxvq_is_mergeable(dev))
1768 max_tries = vq->size - 1;
1774 * if we tried all available ring items, and still
1775 * can't get enough buf, it means something abnormal
1778 if (unlikely(++tries > max_tries))
1781 if (unlikely(fill_vec_buf_packed(dev, vq,
1782 avail_idx, &desc_count,
1785 VHOST_ACCESS_RW) < 0))
1788 len = RTE_MIN(len, size);
1791 buffer_len[*nr_buffers] = len;
1792 buffer_buf_id[*nr_buffers] = buf_id;
1793 buffer_desc_count[*nr_buffers] = desc_count;
1795 *nr_descs += desc_count;
1796 avail_idx += desc_count;
1797 if (avail_idx >= vq->size)
1798 avail_idx -= vq->size;
1801 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1804 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1809 static __rte_always_inline int16_t
1810 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1811 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1813 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1815 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1816 nr_descs, nr_buffers) < 0)) {
1817 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1821 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1822 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1827 static __rte_always_inline void
1828 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1829 uint32_t nr_err, uint32_t *pkt_idx)
1831 uint16_t descs_err = 0;
1832 uint16_t buffers_err = 0;
1833 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1836 /* calculate the sum of buffers and descs of DMA-error packets. */
1837 while (nr_err-- > 0) {
1838 descs_err += pkts_info[slot_idx % vq->size].descs;
1839 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1843 if (vq->last_avail_idx >= descs_err) {
1844 vq->last_avail_idx -= descs_err;
1846 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1847 vq->avail_wrap_counter ^= 1;
1850 vq->shadow_used_idx -= buffers_err;
1853 static __rte_noinline uint32_t
1854 virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1855 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1856 int16_t dma_id, uint16_t vchan_id)
1858 uint32_t pkt_idx = 0;
1859 uint32_t remained = count;
1861 uint16_t num_buffers;
1864 struct vhost_async *async = vq->async;
1865 struct async_inflight_info *pkts_info = async->pkts_info;
1866 uint32_t pkt_err = 0;
1867 uint16_t slot_idx = 0;
1870 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1874 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1875 &num_descs, &num_buffers) < 0))
1878 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1880 pkts_info[slot_idx].descs = num_descs;
1881 pkts_info[slot_idx].nr_buffers = num_buffers;
1882 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1886 vq_inc_last_avail_packed(vq, num_descs);
1887 } while (pkt_idx < count);
1889 if (unlikely(pkt_idx == 0))
1892 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1893 async->iov_iter, pkt_idx);
1895 async_iter_reset(async);
1897 pkt_err = pkt_idx - n_xfer;
1898 if (unlikely(pkt_err)) {
1899 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1900 dev->ifname, __func__, pkt_err, queue_id);
1901 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1904 if (likely(vq->shadow_used_idx)) {
1905 /* keep used descriptors. */
1906 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1907 vq->size, 0, async->buffer_idx_packed,
1908 vq->shadow_used_idx);
1910 async->buffer_idx_packed += vq->shadow_used_idx;
1911 if (async->buffer_idx_packed >= vq->size)
1912 async->buffer_idx_packed -= vq->size;
1914 async->pkts_idx += pkt_idx;
1915 if (async->pkts_idx >= vq->size)
1916 async->pkts_idx -= vq->size;
1918 vq->shadow_used_idx = 0;
1919 async->pkts_inflight_n += pkt_idx;
1925 static __rte_always_inline void
1926 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1928 struct vhost_async *async = vq->async;
1929 uint16_t nr_left = n_descs;
1934 from = async->last_desc_idx_split & (vq->size - 1);
1935 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1936 to = vq->last_used_idx & (vq->size - 1);
1938 if (to + nr_copy <= vq->size) {
1939 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1940 nr_copy * sizeof(struct vring_used_elem));
1942 uint16_t size = vq->size - to;
1944 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1945 size * sizeof(struct vring_used_elem));
1946 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1947 (nr_copy - size) * sizeof(struct vring_used_elem));
1950 async->last_desc_idx_split += nr_copy;
1951 vq->last_used_idx += nr_copy;
1953 } while (nr_left > 0);
1956 static __rte_always_inline void
1957 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1960 struct vhost_async *async = vq->async;
1961 uint16_t from = async->last_buffer_idx_packed;
1962 uint16_t used_idx = vq->last_used_idx;
1963 uint16_t head_idx = vq->last_used_idx;
1964 uint16_t head_flags = 0;
1967 /* Split loop in two to save memory barriers */
1968 for (i = 0; i < n_buffers; i++) {
1969 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1970 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1972 used_idx += async->buffers_packed[from].count;
1973 if (used_idx >= vq->size)
1974 used_idx -= vq->size;
1977 if (from >= vq->size)
1981 /* The ordering for storing desc flags needs to be enforced. */
1982 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1984 from = async->last_buffer_idx_packed;
1986 for (i = 0; i < n_buffers; i++) {
1989 if (async->buffers_packed[from].len)
1990 flags = VRING_DESC_F_WRITE;
1994 if (vq->used_wrap_counter) {
1995 flags |= VRING_DESC_F_USED;
1996 flags |= VRING_DESC_F_AVAIL;
1998 flags &= ~VRING_DESC_F_USED;
1999 flags &= ~VRING_DESC_F_AVAIL;
2003 vq->desc_packed[vq->last_used_idx].flags = flags;
2005 head_idx = vq->last_used_idx;
2009 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
2012 if (from == vq->size)
2016 vq->desc_packed[head_idx].flags = head_flags;
2017 async->last_buffer_idx_packed = from;
2020 static __rte_always_inline uint16_t
2021 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
2022 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2025 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
2026 struct vhost_async *async = vq->async;
2027 struct async_inflight_info *pkts_info = async->pkts_info;
2028 uint16_t nr_cpl_pkts = 0;
2029 uint16_t n_descs = 0, n_buffers = 0;
2030 uint16_t start_idx, from, i;
2032 /* Check completed copies for the given DMA vChannel */
2033 vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
2035 start_idx = async_get_first_inflight_pkt_idx(vq);
2037 * Calculate the number of copy completed packets.
2038 * Note that there may be completed packets even if
2039 * no copies are reported done by the given DMA vChannel,
2040 * as it's possible that a virtqueue uses multiple DMA
2044 while (vq->async->pkts_cmpl_flag[from] && count--) {
2045 vq->async->pkts_cmpl_flag[from] = false;
2047 if (from >= vq->size)
2052 if (nr_cpl_pkts == 0)
2055 for (i = 0; i < nr_cpl_pkts; i++) {
2056 from = (start_idx + i) % vq->size;
2057 /* Only used with packed ring */
2058 n_buffers += pkts_info[from].nr_buffers;
2059 /* Only used with split ring */
2060 n_descs += pkts_info[from].descs;
2061 pkts[i] = pkts_info[from].mbuf;
2064 async->pkts_inflight_n -= nr_cpl_pkts;
2066 if (likely(vq->enabled && vq->access_ok)) {
2067 if (vq_is_packed(dev)) {
2068 write_back_completed_descs_packed(vq, n_buffers);
2069 vhost_vring_call_packed(dev, vq);
2071 write_back_completed_descs_split(vq, n_descs);
2072 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
2073 vhost_vring_call_split(dev, vq);
2076 if (vq_is_packed(dev)) {
2077 async->last_buffer_idx_packed += n_buffers;
2078 if (async->last_buffer_idx_packed >= vq->size)
2079 async->last_buffer_idx_packed -= vq->size;
2081 async->last_desc_idx_split += n_descs;
2089 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2090 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2093 struct virtio_net *dev = get_device(vid);
2094 struct vhost_virtqueue *vq;
2095 uint16_t n_pkts_cpl = 0;
2100 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2101 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2102 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2103 dev->ifname, __func__, queue_id);
2107 if (unlikely(!dma_copy_track[dma_id].vchans ||
2108 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2109 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2114 vq = dev->virtqueue[queue_id];
2116 if (!rte_spinlock_trylock(&vq->access_lock)) {
2117 VHOST_LOG_DATA(DEBUG, "(%s) %s: virtqueue %u is busy.\n", dev->ifname, __func__,
2122 if (unlikely(!vq->async)) {
2123 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for virtqueue %d.\n",
2124 dev->ifname, __func__, queue_id);
2128 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2130 vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2131 vq->stats.inflight_completed += n_pkts_cpl;
2134 rte_spinlock_unlock(&vq->access_lock);
2140 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2141 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2144 struct virtio_net *dev = get_device(vid);
2145 struct vhost_virtqueue *vq;
2146 uint16_t n_pkts_cpl = 0;
2151 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2152 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2153 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2154 dev->ifname, __func__, queue_id);
2158 vq = dev->virtqueue[queue_id];
2160 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
2161 VHOST_LOG_DATA(ERR, "(%s) %s() called without access lock taken.\n",
2162 dev->ifname, __func__);
2166 if (unlikely(!vq->async)) {
2167 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
2168 dev->ifname, __func__, queue_id);
2172 if (unlikely(!dma_copy_track[dma_id].vchans ||
2173 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2174 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2179 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2181 vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2182 vq->stats.inflight_completed += n_pkts_cpl;
2187 static __rte_always_inline uint32_t
2188 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2189 struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2191 struct vhost_virtqueue *vq;
2194 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2195 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2196 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2197 dev->ifname, __func__, queue_id);
2201 if (unlikely(!dma_copy_track[dma_id].vchans ||
2202 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2203 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2208 vq = dev->virtqueue[queue_id];
2210 rte_spinlock_lock(&vq->access_lock);
2212 if (unlikely(!vq->enabled || !vq->async))
2213 goto out_access_unlock;
2215 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2216 vhost_user_iotlb_rd_lock(vq);
2218 if (unlikely(!vq->access_ok))
2219 if (unlikely(vring_translate(dev, vq) < 0))
2222 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2226 if (vq_is_packed(dev))
2227 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2228 pkts, count, dma_id, vchan_id);
2230 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2231 pkts, count, dma_id, vchan_id);
2233 vq->stats.inflight_submitted += nb_tx;
2236 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2237 vhost_user_iotlb_rd_unlock(vq);
2240 rte_spinlock_unlock(&vq->access_lock);
2246 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2247 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2250 struct virtio_net *dev = get_device(vid);
2255 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2256 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2257 dev->ifname, __func__);
2261 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, dma_id, vchan_id);
2265 virtio_net_with_host_offload(struct virtio_net *dev)
2268 ((1ULL << VIRTIO_NET_F_CSUM) |
2269 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2270 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2271 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2272 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2279 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2281 struct rte_ipv4_hdr *ipv4_hdr;
2282 struct rte_ipv6_hdr *ipv6_hdr;
2283 struct rte_ether_hdr *eth_hdr;
2285 uint16_t data_len = rte_pktmbuf_data_len(m);
2287 if (data_len < sizeof(struct rte_ether_hdr))
2290 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2292 m->l2_len = sizeof(struct rte_ether_hdr);
2293 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2295 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2296 if (data_len < sizeof(struct rte_ether_hdr) +
2297 sizeof(struct rte_vlan_hdr))
2300 struct rte_vlan_hdr *vlan_hdr =
2301 (struct rte_vlan_hdr *)(eth_hdr + 1);
2303 m->l2_len += sizeof(struct rte_vlan_hdr);
2304 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2307 switch (ethertype) {
2308 case RTE_ETHER_TYPE_IPV4:
2309 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2311 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2313 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2314 if (data_len < m->l2_len + m->l3_len)
2316 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2317 *l4_proto = ipv4_hdr->next_proto_id;
2319 case RTE_ETHER_TYPE_IPV6:
2320 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2322 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2324 m->l3_len = sizeof(struct rte_ipv6_hdr);
2325 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2326 *l4_proto = ipv6_hdr->proto;
2329 /* a valid L3 header is needed for further L4 parsing */
2333 /* both CSUM and GSO need a valid L4 header */
2334 switch (*l4_proto) {
2336 if (data_len < m->l2_len + m->l3_len +
2337 sizeof(struct rte_tcp_hdr))
2341 if (data_len < m->l2_len + m->l3_len +
2342 sizeof(struct rte_udp_hdr))
2346 if (data_len < m->l2_len + m->l3_len +
2347 sizeof(struct rte_sctp_hdr))
2363 static __rte_always_inline void
2364 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2367 uint8_t l4_proto = 0;
2368 struct rte_tcp_hdr *tcp_hdr = NULL;
2370 uint16_t data_len = rte_pktmbuf_data_len(m);
2372 if (parse_headers(m, &l4_proto) < 0)
2375 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2376 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2377 switch (hdr->csum_offset) {
2378 case (offsetof(struct rte_tcp_hdr, cksum)):
2379 if (l4_proto != IPPROTO_TCP)
2381 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2383 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2384 if (l4_proto != IPPROTO_UDP)
2386 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2388 case (offsetof(struct rte_sctp_hdr, cksum)):
2389 if (l4_proto != IPPROTO_SCTP)
2391 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2401 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2402 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2403 case VIRTIO_NET_HDR_GSO_TCPV4:
2404 case VIRTIO_NET_HDR_GSO_TCPV6:
2405 if (l4_proto != IPPROTO_TCP)
2407 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2408 struct rte_tcp_hdr *,
2409 m->l2_len + m->l3_len);
2410 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2411 if (data_len < m->l2_len + m->l3_len + tcp_len)
2413 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2414 m->tso_segsz = hdr->gso_size;
2415 m->l4_len = tcp_len;
2417 case VIRTIO_NET_HDR_GSO_UDP:
2418 if (l4_proto != IPPROTO_UDP)
2420 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2421 m->tso_segsz = hdr->gso_size;
2422 m->l4_len = sizeof(struct rte_udp_hdr);
2425 VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2426 dev->ifname, hdr->gso_type);
2438 static __rte_always_inline void
2439 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2440 struct rte_mbuf *m, bool legacy_ol_flags)
2442 struct rte_net_hdr_lens hdr_lens;
2443 int l4_supported = 0;
2446 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2449 if (legacy_ol_flags) {
2450 vhost_dequeue_offload_legacy(dev, hdr, m);
2454 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2456 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2457 m->packet_type = ptype;
2458 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2459 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2460 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2463 /* According to Virtio 1.1 spec, the device only needs to look at
2464 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2465 * This differs from the processing incoming packets path where the
2466 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2469 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2470 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2471 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2473 * 5.1.6.2.2 Device Requirements: Packet Transmission
2474 * The device MUST ignore flag bits that it does not recognize.
2476 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2479 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2480 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2481 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2483 /* Unknown proto or tunnel, do sw cksum. We can assume
2484 * the cksum field is in the first segment since the
2485 * buffers we provided to the host are large enough.
2486 * In case of SCTP, this will be wrong since it's a CRC
2487 * but there's nothing we can do.
2489 uint16_t csum = 0, off;
2491 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2492 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2494 if (likely(csum != 0xffff))
2496 off = hdr->csum_offset + hdr->csum_start;
2497 if (rte_pktmbuf_data_len(m) >= off + 1)
2498 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2502 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2503 if (hdr->gso_size == 0)
2506 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2507 case VIRTIO_NET_HDR_GSO_TCPV4:
2508 case VIRTIO_NET_HDR_GSO_TCPV6:
2509 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2511 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2512 m->tso_segsz = hdr->gso_size;
2514 case VIRTIO_NET_HDR_GSO_UDP:
2515 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2517 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2518 m->tso_segsz = hdr->gso_size;
2526 static __rte_noinline void
2527 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2528 struct buf_vector *buf_vec)
2531 uint64_t remain = sizeof(struct virtio_net_hdr);
2533 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2536 len = RTE_MIN(remain, buf_vec->buf_len);
2537 src = buf_vec->buf_addr;
2538 rte_memcpy((void *)(uintptr_t)dst,
2539 (void *)(uintptr_t)src, len);
2547 static __rte_always_inline int
2548 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2549 struct buf_vector *buf_vec, uint16_t nr_vec,
2550 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2551 bool legacy_ol_flags)
2553 uint32_t buf_avail, buf_offset, buf_len;
2554 uint64_t buf_addr, buf_iova;
2555 uint32_t mbuf_avail, mbuf_offset;
2557 struct rte_mbuf *cur = m, *prev = m;
2558 struct virtio_net_hdr tmp_hdr;
2559 struct virtio_net_hdr *hdr = NULL;
2560 /* A counter to avoid desc dead loop chain */
2561 uint16_t vec_idx = 0;
2563 buf_addr = buf_vec[vec_idx].buf_addr;
2564 buf_iova = buf_vec[vec_idx].buf_iova;
2565 buf_len = buf_vec[vec_idx].buf_len;
2567 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
2570 if (virtio_net_with_host_offload(dev)) {
2571 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2573 * No luck, the virtio-net header doesn't fit
2574 * in a contiguous virtual area.
2576 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2579 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2584 * A virtio driver normally uses at least 2 desc buffers
2585 * for Tx: the first for storing the header, and others
2586 * for storing the data.
2588 if (unlikely(buf_len < dev->vhost_hlen)) {
2589 buf_offset = dev->vhost_hlen - buf_len;
2591 buf_addr = buf_vec[vec_idx].buf_addr;
2592 buf_iova = buf_vec[vec_idx].buf_iova;
2593 buf_len = buf_vec[vec_idx].buf_len;
2594 buf_avail = buf_len - buf_offset;
2595 } else if (buf_len == dev->vhost_hlen) {
2596 if (unlikely(++vec_idx >= nr_vec))
2598 buf_addr = buf_vec[vec_idx].buf_addr;
2599 buf_len = buf_vec[vec_idx].buf_len;
2602 buf_avail = buf_len;
2604 buf_offset = dev->vhost_hlen;
2605 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2609 (uintptr_t)(buf_addr + buf_offset),
2610 (uint32_t)buf_avail, 0);
2613 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2615 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2617 sync_fill_seg(dev, vq, cur, mbuf_offset,
2618 buf_addr + buf_offset,
2619 buf_iova + buf_offset, cpy_len, false);
2621 mbuf_avail -= cpy_len;
2622 mbuf_offset += cpy_len;
2623 buf_avail -= cpy_len;
2624 buf_offset += cpy_len;
2626 /* This buf reaches to its end, get the next one */
2627 if (buf_avail == 0) {
2628 if (++vec_idx >= nr_vec)
2631 buf_addr = buf_vec[vec_idx].buf_addr;
2632 buf_iova = buf_vec[vec_idx].buf_iova;
2633 buf_len = buf_vec[vec_idx].buf_len;
2636 buf_avail = buf_len;
2638 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2639 (uint32_t)buf_avail, 0);
2643 * This mbuf reaches to its end, get a new one
2644 * to hold more data.
2646 if (mbuf_avail == 0) {
2647 cur = rte_pktmbuf_alloc(mbuf_pool);
2648 if (unlikely(cur == NULL)) {
2649 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2655 prev->data_len = mbuf_offset;
2657 m->pkt_len += mbuf_offset;
2661 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2665 prev->data_len = mbuf_offset;
2666 m->pkt_len += mbuf_offset;
2669 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2677 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2683 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2685 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2686 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2691 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2692 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2694 if (unlikely(total_len > UINT16_MAX))
2697 buf_len = total_len;
2698 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2699 if (unlikely(buf == NULL))
2702 /* Initialize shinfo */
2703 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2704 virtio_dev_extbuf_free, buf);
2705 if (unlikely(shinfo == NULL)) {
2707 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2711 iova = rte_malloc_virt2iova(buf);
2712 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2713 rte_pktmbuf_reset_headroom(pkt);
2719 * Prepare a host supported pktmbuf.
2721 static __rte_always_inline int
2722 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2725 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2728 /* attach an external buffer if supported */
2729 if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2732 /* check if chained buffers are allowed */
2733 if (!dev->linearbuf)
2741 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2742 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2743 bool legacy_ol_flags)
2746 uint16_t free_entries;
2747 uint16_t dropped = 0;
2748 static bool allocerr_warned;
2751 * The ordering between avail index and
2752 * desc reads needs to be enforced.
2754 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2756 if (free_entries == 0)
2759 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2761 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2763 count = RTE_MIN(count, MAX_PKT_BURST);
2764 count = RTE_MIN(count, free_entries);
2765 VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2766 dev->ifname, count);
2768 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2771 for (i = 0; i < count; i++) {
2772 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2775 uint16_t nr_vec = 0;
2778 if (unlikely(fill_vec_buf_split(dev, vq,
2779 vq->last_avail_idx + i,
2781 &head_idx, &buf_len,
2782 VHOST_ACCESS_RO) < 0))
2785 update_shadow_used_ring_split(vq, head_idx, 0);
2787 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2788 if (unlikely(err)) {
2790 * mbuf allocation fails for jumbo packets when external
2791 * buffer allocation is not allowed and linear buffer
2792 * is required. Drop this packet.
2794 if (!allocerr_warned) {
2795 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2796 dev->ifname, buf_len, mbuf_pool->name);
2797 allocerr_warned = true;
2804 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2805 mbuf_pool, legacy_ol_flags);
2806 if (unlikely(err)) {
2807 if (!allocerr_warned) {
2808 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2810 allocerr_warned = true;
2819 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2821 vq->last_avail_idx += i;
2823 do_data_copy_dequeue(vq);
2824 if (unlikely(i < count))
2825 vq->shadow_used_idx = i;
2826 if (likely(vq->shadow_used_idx)) {
2827 flush_shadow_used_ring_split(dev, vq);
2828 vhost_vring_call_split(dev, vq);
2831 return (i - dropped);
2836 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2837 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2838 struct rte_mbuf **pkts, uint16_t count)
2840 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2845 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2846 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2847 struct rte_mbuf **pkts, uint16_t count)
2849 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2852 static __rte_always_inline int
2853 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2854 struct vhost_virtqueue *vq,
2855 struct rte_mbuf **pkts,
2857 uintptr_t *desc_addrs,
2860 bool wrap = vq->avail_wrap_counter;
2861 struct vring_packed_desc *descs = vq->desc_packed;
2862 uint64_t lens[PACKED_BATCH_SIZE];
2863 uint64_t buf_lens[PACKED_BATCH_SIZE];
2864 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2867 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2869 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2872 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2873 flags = descs[avail_idx + i].flags;
2874 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2875 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2876 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2880 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2882 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2883 lens[i] = descs[avail_idx + i].len;
2885 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2886 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2887 descs[avail_idx + i].addr,
2888 &lens[i], VHOST_ACCESS_RW);
2891 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2892 if (unlikely(!desc_addrs[i]))
2894 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2898 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2899 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2903 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2904 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2906 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2907 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2911 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2912 pkts[i]->pkt_len = lens[i] - buf_offset;
2913 pkts[i]->data_len = pkts[i]->pkt_len;
2914 ids[i] = descs[avail_idx + i].id;
2923 static __rte_always_inline int
2924 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2925 struct vhost_virtqueue *vq,
2926 struct rte_mbuf **pkts,
2927 bool legacy_ol_flags)
2929 uint16_t avail_idx = vq->last_avail_idx;
2930 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2931 struct virtio_net_hdr *hdr;
2932 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2933 uint16_t ids[PACKED_BATCH_SIZE];
2936 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2940 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2941 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2943 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2944 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2945 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2948 if (virtio_net_with_host_offload(dev)) {
2949 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2950 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2951 vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2955 if (virtio_net_is_inorder(dev))
2956 vhost_shadow_dequeue_batch_packed_inorder(vq,
2957 ids[PACKED_BATCH_SIZE - 1]);
2959 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2961 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2966 static __rte_always_inline int
2967 vhost_dequeue_single_packed(struct virtio_net *dev,
2968 struct vhost_virtqueue *vq,
2969 struct rte_mempool *mbuf_pool,
2970 struct rte_mbuf *pkts,
2972 uint16_t *desc_count,
2973 bool legacy_ol_flags)
2975 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2977 uint16_t nr_vec = 0;
2979 static bool allocerr_warned;
2981 if (unlikely(fill_vec_buf_packed(dev, vq,
2982 vq->last_avail_idx, desc_count,
2985 VHOST_ACCESS_RO) < 0))
2988 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2989 if (!allocerr_warned) {
2990 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2991 dev->ifname, buf_len, mbuf_pool->name);
2992 allocerr_warned = true;
2997 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2998 mbuf_pool, legacy_ol_flags);
2999 if (unlikely(err)) {
3000 if (!allocerr_warned) {
3001 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
3003 allocerr_warned = true;
3011 static __rte_always_inline int
3012 virtio_dev_tx_single_packed(struct virtio_net *dev,
3013 struct vhost_virtqueue *vq,
3014 struct rte_mempool *mbuf_pool,
3015 struct rte_mbuf *pkts,
3016 bool legacy_ol_flags)
3019 uint16_t buf_id, desc_count = 0;
3022 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
3023 &desc_count, legacy_ol_flags);
3025 if (likely(desc_count > 0)) {
3026 if (virtio_net_is_inorder(dev))
3027 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
3030 vhost_shadow_dequeue_single_packed(vq, buf_id,
3033 vq_inc_last_avail_packed(vq, desc_count);
3041 virtio_dev_tx_packed(struct virtio_net *dev,
3042 struct vhost_virtqueue *__rte_restrict vq,
3043 struct rte_mempool *mbuf_pool,
3044 struct rte_mbuf **__rte_restrict pkts,
3046 bool legacy_ol_flags)
3048 uint32_t pkt_idx = 0;
3050 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
3054 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3056 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3057 if (!virtio_dev_tx_batch_packed(dev, vq,
3060 pkt_idx += PACKED_BATCH_SIZE;
3065 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3070 } while (pkt_idx < count);
3072 if (pkt_idx != count)
3073 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3075 if (vq->shadow_used_idx) {
3076 do_data_copy_dequeue(vq);
3078 vhost_flush_dequeue_shadow_packed(dev, vq);
3079 vhost_vring_call_packed(dev, vq);
3087 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3088 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3089 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3091 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3096 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3097 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3098 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3100 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3104 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3105 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3107 struct virtio_net *dev;
3108 struct rte_mbuf *rarp_mbuf = NULL;
3109 struct vhost_virtqueue *vq;
3110 int16_t success = 1;
3112 dev = get_device(vid);
3116 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3117 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
3118 dev->ifname, __func__);
3122 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3123 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
3124 dev->ifname, __func__, queue_id);
3128 vq = dev->virtqueue[queue_id];
3130 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3133 if (unlikely(!vq->enabled)) {
3135 goto out_access_unlock;
3138 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3139 vhost_user_iotlb_rd_lock(vq);
3141 if (unlikely(!vq->access_ok))
3142 if (unlikely(vring_translate(dev, vq) < 0)) {
3148 * Construct a RARP broadcast packet, and inject it to the "pkts"
3149 * array, to looks like that guest actually send such packet.
3151 * Check user_send_rarp() for more information.
3153 * broadcast_rarp shares a cacheline in the virtio_net structure
3154 * with some fields that are accessed during enqueue and
3155 * __atomic_compare_exchange_n causes a write if performed compare
3156 * and exchange. This could result in false sharing between enqueue
3159 * Prevent unnecessary false sharing by reading broadcast_rarp first
3160 * and only performing compare and exchange if the read indicates it
3161 * is likely to be set.
3163 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3164 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3165 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3167 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3168 if (rarp_mbuf == NULL) {
3169 VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
3174 * Inject it to the head of "pkts" array, so that switch's mac
3175 * learning table will get updated first.
3177 pkts[0] = rarp_mbuf;
3178 vhost_queue_stats_update(dev, vq, pkts, 1);
3183 if (vq_is_packed(dev)) {
3184 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3185 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3187 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3189 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3190 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3192 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3195 vhost_queue_stats_update(dev, vq, pkts, count);
3198 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3199 vhost_user_iotlb_rd_unlock(vq);
3202 rte_spinlock_unlock(&vq->access_lock);
3204 if (unlikely(rarp_mbuf != NULL))