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_vhost.h>
19 #include <rte_spinlock.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost_async.h>
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
34 static __rte_always_inline bool
35 virtio_net_is_inorder(struct virtio_net *dev)
37 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
41 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
43 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
47 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
49 struct batch_copy_elem *elem = vq->batch_copy_elems;
50 uint16_t count = vq->batch_copy_nb_elems;
53 for (i = 0; i < count; i++) {
54 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
55 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
57 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
60 vq->batch_copy_nb_elems = 0;
64 do_data_copy_dequeue(struct vhost_virtqueue *vq)
66 struct batch_copy_elem *elem = vq->batch_copy_elems;
67 uint16_t count = vq->batch_copy_nb_elems;
70 for (i = 0; i < count; i++)
71 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
73 vq->batch_copy_nb_elems = 0;
76 static __rte_always_inline void
77 do_flush_shadow_used_ring_split(struct virtio_net *dev,
78 struct vhost_virtqueue *vq,
79 uint16_t to, uint16_t from, uint16_t size)
81 rte_memcpy(&vq->used->ring[to],
82 &vq->shadow_used_split[from],
83 size * sizeof(struct vring_used_elem));
84 vhost_log_cache_used_vring(dev, vq,
85 offsetof(struct vring_used, ring[to]),
86 size * sizeof(struct vring_used_elem));
89 static __rte_always_inline void
90 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
92 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
94 if (used_idx + vq->shadow_used_idx <= vq->size) {
95 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
100 /* update used ring interval [used_idx, vq->size] */
101 size = vq->size - used_idx;
102 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
104 /* update the left half used ring interval [0, left_size] */
105 do_flush_shadow_used_ring_split(dev, vq, 0, size,
106 vq->shadow_used_idx - size);
108 vq->last_used_idx += vq->shadow_used_idx;
110 vhost_log_cache_sync(dev, vq);
112 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
114 vq->shadow_used_idx = 0;
115 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
116 sizeof(vq->used->idx));
119 static __rte_always_inline void
120 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
121 uint16_t desc_idx, uint32_t len)
123 uint16_t i = vq->shadow_used_idx++;
125 vq->shadow_used_split[i].id = desc_idx;
126 vq->shadow_used_split[i].len = len;
129 static __rte_always_inline void
130 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
131 struct vhost_virtqueue *vq)
134 uint16_t used_idx = vq->last_used_idx;
135 uint16_t head_idx = vq->last_used_idx;
136 uint16_t head_flags = 0;
138 /* Split loop in two to save memory barriers */
139 for (i = 0; i < vq->shadow_used_idx; i++) {
140 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
141 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
143 used_idx += vq->shadow_used_packed[i].count;
144 if (used_idx >= vq->size)
145 used_idx -= vq->size;
148 /* The ordering for storing desc flags needs to be enforced. */
149 rte_atomic_thread_fence(__ATOMIC_RELEASE);
151 for (i = 0; i < vq->shadow_used_idx; i++) {
154 if (vq->shadow_used_packed[i].len)
155 flags = VRING_DESC_F_WRITE;
159 if (vq->used_wrap_counter) {
160 flags |= VRING_DESC_F_USED;
161 flags |= VRING_DESC_F_AVAIL;
163 flags &= ~VRING_DESC_F_USED;
164 flags &= ~VRING_DESC_F_AVAIL;
168 vq->desc_packed[vq->last_used_idx].flags = flags;
170 vhost_log_cache_used_vring(dev, vq,
172 sizeof(struct vring_packed_desc),
173 sizeof(struct vring_packed_desc));
175 head_idx = vq->last_used_idx;
179 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
182 vq->desc_packed[head_idx].flags = head_flags;
184 vhost_log_cache_used_vring(dev, vq,
186 sizeof(struct vring_packed_desc),
187 sizeof(struct vring_packed_desc));
189 vq->shadow_used_idx = 0;
190 vhost_log_cache_sync(dev, vq);
193 static __rte_always_inline void
194 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
195 struct vhost_virtqueue *vq)
197 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
199 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
200 /* desc flags is the synchronization point for virtio packed vring */
201 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
202 used_elem->flags, __ATOMIC_RELEASE);
204 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
205 sizeof(struct vring_packed_desc),
206 sizeof(struct vring_packed_desc));
207 vq->shadow_used_idx = 0;
208 vhost_log_cache_sync(dev, vq);
211 static __rte_always_inline void
212 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
213 struct vhost_virtqueue *vq,
219 uint16_t last_used_idx;
220 struct vring_packed_desc *desc_base;
222 last_used_idx = vq->last_used_idx;
223 desc_base = &vq->desc_packed[last_used_idx];
225 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
227 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
228 desc_base[i].id = ids[i];
229 desc_base[i].len = lens[i];
232 rte_atomic_thread_fence(__ATOMIC_RELEASE);
234 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
235 desc_base[i].flags = flags;
238 vhost_log_cache_used_vring(dev, vq, last_used_idx *
239 sizeof(struct vring_packed_desc),
240 sizeof(struct vring_packed_desc) *
242 vhost_log_cache_sync(dev, vq);
244 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
247 static __rte_always_inline void
248 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
251 vq->shadow_used_packed[0].id = id;
253 if (!vq->shadow_used_idx) {
254 vq->shadow_last_used_idx = vq->last_used_idx;
255 vq->shadow_used_packed[0].flags =
256 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
257 vq->shadow_used_packed[0].len = 0;
258 vq->shadow_used_packed[0].count = 1;
259 vq->shadow_used_idx++;
262 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
265 static __rte_always_inline void
266 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
267 struct vhost_virtqueue *vq,
274 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
276 if (!vq->shadow_used_idx) {
277 vq->shadow_last_used_idx = vq->last_used_idx;
278 vq->shadow_used_packed[0].id = ids[0];
279 vq->shadow_used_packed[0].len = 0;
280 vq->shadow_used_packed[0].count = 1;
281 vq->shadow_used_packed[0].flags = flags;
282 vq->shadow_used_idx++;
287 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
288 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
289 vq->desc_packed[vq->last_used_idx + i].len = 0;
292 rte_atomic_thread_fence(__ATOMIC_RELEASE);
293 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
294 vq->desc_packed[vq->last_used_idx + i].flags = flags;
296 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
297 sizeof(struct vring_packed_desc),
298 sizeof(struct vring_packed_desc) *
300 vhost_log_cache_sync(dev, vq);
302 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
305 static __rte_always_inline void
306 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
312 flags = vq->desc_packed[vq->last_used_idx].flags;
313 if (vq->used_wrap_counter) {
314 flags |= VRING_DESC_F_USED;
315 flags |= VRING_DESC_F_AVAIL;
317 flags &= ~VRING_DESC_F_USED;
318 flags &= ~VRING_DESC_F_AVAIL;
321 if (!vq->shadow_used_idx) {
322 vq->shadow_last_used_idx = vq->last_used_idx;
324 vq->shadow_used_packed[0].id = buf_id;
325 vq->shadow_used_packed[0].len = 0;
326 vq->shadow_used_packed[0].flags = flags;
327 vq->shadow_used_idx++;
329 vq->desc_packed[vq->last_used_idx].id = buf_id;
330 vq->desc_packed[vq->last_used_idx].len = 0;
331 vq->desc_packed[vq->last_used_idx].flags = flags;
334 vq_inc_last_used_packed(vq, count);
337 static __rte_always_inline void
338 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
344 vq->shadow_used_packed[0].id = buf_id;
346 flags = vq->desc_packed[vq->last_used_idx].flags;
347 if (vq->used_wrap_counter) {
348 flags |= VRING_DESC_F_USED;
349 flags |= VRING_DESC_F_AVAIL;
351 flags &= ~VRING_DESC_F_USED;
352 flags &= ~VRING_DESC_F_AVAIL;
355 if (!vq->shadow_used_idx) {
356 vq->shadow_last_used_idx = vq->last_used_idx;
357 vq->shadow_used_packed[0].len = 0;
358 vq->shadow_used_packed[0].flags = flags;
359 vq->shadow_used_idx++;
362 vq_inc_last_used_packed(vq, count);
365 static __rte_always_inline void
366 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
370 uint16_t num_buffers)
374 for (i = 0; i < num_buffers; i++) {
375 /* enqueue shadow flush action aligned with batch num */
376 if (!vq->shadow_used_idx)
377 vq->shadow_aligned_idx = vq->last_used_idx &
379 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
380 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
381 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
382 vq->shadow_aligned_idx += count[i];
383 vq->shadow_used_idx++;
387 static __rte_always_inline void
388 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
389 struct vhost_virtqueue *vq,
393 uint16_t num_buffers)
395 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
397 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
398 do_data_copy_enqueue(dev, vq);
399 vhost_flush_enqueue_shadow_packed(dev, vq);
403 /* avoid write operation when necessary, to lessen cache issues */
404 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
405 if ((var) != (val)) \
409 static __rte_always_inline void
410 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
412 uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
414 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
415 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
418 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
419 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
422 case RTE_MBUF_F_TX_TCP_CKSUM:
423 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
426 case RTE_MBUF_F_TX_UDP_CKSUM:
427 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
430 case RTE_MBUF_F_TX_SCTP_CKSUM:
431 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
436 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
437 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
438 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
441 /* IP cksum verification cannot be bypassed, then calculate here */
442 if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
443 struct rte_ipv4_hdr *ipv4_hdr;
445 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
447 ipv4_hdr->hdr_checksum = 0;
448 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
451 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
452 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
453 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
455 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
456 net_hdr->gso_size = m_buf->tso_segsz;
457 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
459 } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
460 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
461 net_hdr->gso_size = m_buf->tso_segsz;
462 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
465 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
466 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
467 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
471 static __rte_always_inline int
472 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
473 struct buf_vector *buf_vec, uint16_t *vec_idx,
474 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
476 uint16_t vec_id = *vec_idx;
480 uint64_t desc_chunck_len = desc_len;
482 if (unlikely(vec_id >= BUF_VECTOR_MAX))
485 desc_addr = vhost_iova_to_vva(dev, vq,
489 if (unlikely(!desc_addr))
492 rte_prefetch0((void *)(uintptr_t)desc_addr);
494 buf_vec[vec_id].buf_iova = desc_iova;
495 buf_vec[vec_id].buf_addr = desc_addr;
496 buf_vec[vec_id].buf_len = desc_chunck_len;
498 desc_len -= desc_chunck_len;
499 desc_iova += desc_chunck_len;
507 static __rte_always_inline int
508 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
509 uint32_t avail_idx, uint16_t *vec_idx,
510 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
511 uint32_t *desc_chain_len, uint8_t perm)
513 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
514 uint16_t vec_id = *vec_idx;
517 uint32_t nr_descs = vq->size;
519 struct vring_desc *descs = vq->desc;
520 struct vring_desc *idesc = NULL;
522 if (unlikely(idx >= vq->size))
525 *desc_chain_head = idx;
527 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
528 dlen = vq->desc[idx].len;
529 nr_descs = dlen / sizeof(struct vring_desc);
530 if (unlikely(nr_descs > vq->size))
533 descs = (struct vring_desc *)(uintptr_t)
534 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
537 if (unlikely(!descs))
540 if (unlikely(dlen < vq->desc[idx].len)) {
542 * The indirect desc table is not contiguous
543 * in process VA space, we have to copy it.
545 idesc = vhost_alloc_copy_ind_table(dev, vq,
546 vq->desc[idx].addr, vq->desc[idx].len);
547 if (unlikely(!idesc))
557 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
558 free_ind_table(idesc);
562 dlen = descs[idx].len;
565 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
566 descs[idx].addr, dlen,
568 free_ind_table(idesc);
572 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
575 idx = descs[idx].next;
578 *desc_chain_len = len;
581 if (unlikely(!!idesc))
582 free_ind_table(idesc);
588 * Returns -1 on fail, 0 on success
591 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
592 uint32_t size, struct buf_vector *buf_vec,
593 uint16_t *num_buffers, uint16_t avail_head,
597 uint16_t vec_idx = 0;
598 uint16_t max_tries, tries = 0;
600 uint16_t head_idx = 0;
604 cur_idx = vq->last_avail_idx;
606 if (rxvq_is_mergeable(dev))
607 max_tries = vq->size - 1;
612 if (unlikely(cur_idx == avail_head))
615 * if we tried all available ring items, and still
616 * can't get enough buf, it means something abnormal
619 if (unlikely(++tries > max_tries))
622 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
625 VHOST_ACCESS_RW) < 0))
627 len = RTE_MIN(len, size);
628 update_shadow_used_ring_split(vq, head_idx, len);
640 static __rte_always_inline int
641 fill_vec_buf_packed_indirect(struct virtio_net *dev,
642 struct vhost_virtqueue *vq,
643 struct vring_packed_desc *desc, uint16_t *vec_idx,
644 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
648 uint16_t vec_id = *vec_idx;
650 struct vring_packed_desc *descs, *idescs = NULL;
653 descs = (struct vring_packed_desc *)(uintptr_t)
654 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
655 if (unlikely(!descs))
658 if (unlikely(dlen < desc->len)) {
660 * The indirect desc table is not contiguous
661 * in process VA space, we have to copy it.
663 idescs = vhost_alloc_copy_ind_table(dev,
664 vq, desc->addr, desc->len);
665 if (unlikely(!idescs))
671 nr_descs = desc->len / sizeof(struct vring_packed_desc);
672 if (unlikely(nr_descs >= vq->size)) {
673 free_ind_table(idescs);
677 for (i = 0; i < nr_descs; i++) {
678 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
679 free_ind_table(idescs);
685 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
692 if (unlikely(!!idescs))
693 free_ind_table(idescs);
698 static __rte_always_inline int
699 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
700 uint16_t avail_idx, uint16_t *desc_count,
701 struct buf_vector *buf_vec, uint16_t *vec_idx,
702 uint16_t *buf_id, uint32_t *len, uint8_t perm)
704 bool wrap_counter = vq->avail_wrap_counter;
705 struct vring_packed_desc *descs = vq->desc_packed;
706 uint16_t vec_id = *vec_idx;
709 if (avail_idx < vq->last_avail_idx)
713 * Perform a load-acquire barrier in desc_is_avail to
714 * enforce the ordering between desc flags and desc
717 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
724 if (unlikely(vec_id >= BUF_VECTOR_MAX))
727 if (unlikely(*desc_count >= vq->size))
731 *buf_id = descs[avail_idx].id;
733 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
734 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
740 dlen = descs[avail_idx].len;
743 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
744 descs[avail_idx].addr,
750 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
753 if (++avail_idx >= vq->size) {
754 avail_idx -= vq->size;
764 static __rte_noinline void
765 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
766 struct buf_vector *buf_vec,
767 struct virtio_net_hdr_mrg_rxbuf *hdr)
770 uint64_t remain = dev->vhost_hlen;
771 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
772 uint64_t iova = buf_vec->buf_iova;
775 len = RTE_MIN(remain,
777 dst = buf_vec->buf_addr;
778 rte_memcpy((void *)(uintptr_t)dst,
779 (void *)(uintptr_t)src,
782 PRINT_PACKET(dev, (uintptr_t)dst,
784 vhost_log_cache_write_iova(dev, vq,
794 static __rte_always_inline int
795 async_iter_initialize(struct vhost_async *async)
797 struct rte_vhost_iov_iter *iter;
799 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
800 VHOST_LOG_DATA(ERR, "no more async iovec available\n");
804 iter = async->iov_iter + async->iter_idx;
805 iter->iov = async->iovec + async->iovec_idx;
811 static __rte_always_inline int
812 async_iter_add_iovec(struct vhost_async *async, void *src, void *dst, size_t len)
814 struct rte_vhost_iov_iter *iter;
815 struct rte_vhost_iovec *iovec;
817 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
818 static bool vhost_max_async_vec_log;
820 if (!vhost_max_async_vec_log) {
821 VHOST_LOG_DATA(ERR, "no more async iovec available\n");
822 vhost_max_async_vec_log = true;
828 iter = async->iov_iter + async->iter_idx;
829 iovec = async->iovec + async->iovec_idx;
831 iovec->src_addr = src;
832 iovec->dst_addr = dst;
841 static __rte_always_inline void
842 async_iter_finalize(struct vhost_async *async)
847 static __rte_always_inline void
848 async_iter_cancel(struct vhost_async *async)
850 struct rte_vhost_iov_iter *iter;
852 iter = async->iov_iter + async->iter_idx;
853 async->iovec_idx -= iter->nr_segs;
858 static __rte_always_inline void
859 async_iter_reset(struct vhost_async *async)
862 async->iovec_idx = 0;
865 static __rte_always_inline int
866 async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
867 struct rte_mbuf *m, uint32_t mbuf_offset,
868 uint64_t buf_iova, uint32_t cpy_len)
870 struct vhost_async *async = vq->async;
872 uint32_t buf_offset = 0;
876 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
877 buf_iova + buf_offset, cpy_len, &mapped_len);
878 if (unlikely(!hpa)) {
879 VHOST_LOG_DATA(ERR, "(%d) %s: failed to get hpa.\n", dev->vid, __func__);
883 if (unlikely(async_iter_add_iovec(async,
884 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
886 hpa, (size_t)mapped_len)))
889 cpy_len -= (uint32_t)mapped_len;
890 mbuf_offset += (uint32_t)mapped_len;
891 buf_offset += (uint32_t)mapped_len;
897 static __rte_always_inline void
898 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
899 struct rte_mbuf *m, uint32_t mbuf_offset,
900 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
902 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
904 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
905 rte_memcpy((void *)((uintptr_t)(buf_addr)),
906 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
908 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
909 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
911 batch_copy[vq->batch_copy_nb_elems].dst =
912 (void *)((uintptr_t)(buf_addr));
913 batch_copy[vq->batch_copy_nb_elems].src =
914 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
915 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
916 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
917 vq->batch_copy_nb_elems++;
921 static __rte_always_inline int
922 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
923 struct rte_mbuf *m, struct buf_vector *buf_vec,
924 uint16_t nr_vec, uint16_t num_buffers)
926 uint32_t vec_idx = 0;
927 uint32_t mbuf_offset, mbuf_avail;
928 uint32_t buf_offset, buf_avail;
929 uint64_t buf_addr, buf_iova, buf_len;
932 struct rte_mbuf *hdr_mbuf;
933 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
935 if (unlikely(m == NULL))
938 buf_addr = buf_vec[vec_idx].buf_addr;
939 buf_iova = buf_vec[vec_idx].buf_iova;
940 buf_len = buf_vec[vec_idx].buf_len;
942 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
947 if (unlikely(buf_len < dev->vhost_hlen)) {
948 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
951 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
953 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
954 dev->vid, num_buffers);
956 if (unlikely(buf_len < dev->vhost_hlen)) {
957 buf_offset = dev->vhost_hlen - buf_len;
959 buf_addr = buf_vec[vec_idx].buf_addr;
960 buf_iova = buf_vec[vec_idx].buf_iova;
961 buf_len = buf_vec[vec_idx].buf_len;
962 buf_avail = buf_len - buf_offset;
964 buf_offset = dev->vhost_hlen;
965 buf_avail = buf_len - dev->vhost_hlen;
968 mbuf_avail = rte_pktmbuf_data_len(m);
970 while (mbuf_avail != 0 || m->next != NULL) {
971 /* done with current buf, get the next one */
972 if (buf_avail == 0) {
974 if (unlikely(vec_idx >= nr_vec))
977 buf_addr = buf_vec[vec_idx].buf_addr;
978 buf_iova = buf_vec[vec_idx].buf_iova;
979 buf_len = buf_vec[vec_idx].buf_len;
985 /* done with current mbuf, get the next one */
986 if (mbuf_avail == 0) {
990 mbuf_avail = rte_pktmbuf_data_len(m);
994 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
995 if (rxvq_is_mergeable(dev))
996 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
999 if (unlikely(hdr == &tmp_hdr)) {
1000 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1002 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1003 dev->vhost_hlen, 0);
1004 vhost_log_cache_write_iova(dev, vq,
1005 buf_vec[0].buf_iova,
1012 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1014 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1015 buf_addr + buf_offset,
1016 buf_iova + buf_offset, cpy_len);
1018 mbuf_avail -= cpy_len;
1019 mbuf_offset += cpy_len;
1020 buf_avail -= cpy_len;
1021 buf_offset += cpy_len;
1029 static __rte_always_inline int
1030 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1031 struct rte_mbuf *m, struct buf_vector *buf_vec,
1032 uint16_t nr_vec, uint16_t num_buffers)
1034 struct vhost_async *async = vq->async;
1035 struct rte_mbuf *hdr_mbuf;
1036 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1037 uint64_t buf_addr, buf_iova;
1039 uint32_t vec_idx = 0;
1040 uint32_t mbuf_offset, mbuf_avail;
1041 uint32_t buf_offset, buf_avail;
1042 uint32_t cpy_len, buf_len;
1044 if (unlikely(m == NULL))
1047 buf_addr = buf_vec[vec_idx].buf_addr;
1048 buf_iova = buf_vec[vec_idx].buf_iova;
1049 buf_len = buf_vec[vec_idx].buf_len;
1051 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1055 hdr_addr = buf_addr;
1056 if (unlikely(buf_len < dev->vhost_hlen)) {
1057 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1060 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1062 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1063 dev->vid, num_buffers);
1065 if (unlikely(buf_len < dev->vhost_hlen)) {
1066 buf_offset = dev->vhost_hlen - buf_len;
1068 buf_addr = buf_vec[vec_idx].buf_addr;
1069 buf_iova = buf_vec[vec_idx].buf_iova;
1070 buf_len = buf_vec[vec_idx].buf_len;
1071 buf_avail = buf_len - buf_offset;
1073 buf_offset = dev->vhost_hlen;
1074 buf_avail = buf_len - dev->vhost_hlen;
1077 mbuf_avail = rte_pktmbuf_data_len(m);
1080 if (async_iter_initialize(async))
1083 while (mbuf_avail != 0 || m->next != NULL) {
1084 /* done with current buf, get the next one */
1085 if (buf_avail == 0) {
1087 if (unlikely(vec_idx >= nr_vec))
1090 buf_addr = buf_vec[vec_idx].buf_addr;
1091 buf_iova = buf_vec[vec_idx].buf_iova;
1092 buf_len = buf_vec[vec_idx].buf_len;
1095 buf_avail = buf_len;
1098 /* done with current mbuf, get the next one */
1099 if (mbuf_avail == 0) {
1103 mbuf_avail = rte_pktmbuf_data_len(m);
1107 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1108 if (rxvq_is_mergeable(dev))
1109 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1112 if (unlikely(hdr == &tmp_hdr)) {
1113 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1115 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1116 dev->vhost_hlen, 0);
1117 vhost_log_cache_write_iova(dev, vq,
1118 buf_vec[0].buf_iova,
1125 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1127 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1128 buf_iova + buf_offset, cpy_len) < 0) {
1132 mbuf_avail -= cpy_len;
1133 mbuf_offset += cpy_len;
1134 buf_avail -= cpy_len;
1135 buf_offset += cpy_len;
1138 async_iter_finalize(async);
1142 async_iter_cancel(async);
1147 static __rte_always_inline int
1148 vhost_enqueue_single_packed(struct virtio_net *dev,
1149 struct vhost_virtqueue *vq,
1150 struct rte_mbuf *pkt,
1151 struct buf_vector *buf_vec,
1154 uint16_t nr_vec = 0;
1155 uint16_t avail_idx = vq->last_avail_idx;
1156 uint16_t max_tries, tries = 0;
1157 uint16_t buf_id = 0;
1159 uint16_t desc_count;
1160 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1161 uint16_t num_buffers = 0;
1162 uint32_t buffer_len[vq->size];
1163 uint16_t buffer_buf_id[vq->size];
1164 uint16_t buffer_desc_count[vq->size];
1166 if (rxvq_is_mergeable(dev))
1167 max_tries = vq->size - 1;
1173 * if we tried all available ring items, and still
1174 * can't get enough buf, it means something abnormal
1177 if (unlikely(++tries > max_tries))
1180 if (unlikely(fill_vec_buf_packed(dev, vq,
1181 avail_idx, &desc_count,
1184 VHOST_ACCESS_RW) < 0))
1187 len = RTE_MIN(len, size);
1190 buffer_len[num_buffers] = len;
1191 buffer_buf_id[num_buffers] = buf_id;
1192 buffer_desc_count[num_buffers] = desc_count;
1195 *nr_descs += desc_count;
1196 avail_idx += desc_count;
1197 if (avail_idx >= vq->size)
1198 avail_idx -= vq->size;
1201 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1204 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1205 buffer_desc_count, num_buffers);
1210 static __rte_noinline uint32_t
1211 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1212 struct rte_mbuf **pkts, uint32_t count)
1214 uint32_t pkt_idx = 0;
1215 uint16_t num_buffers;
1216 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1217 uint16_t avail_head;
1220 * The ordering between avail index and
1221 * desc reads needs to be enforced.
1223 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1225 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1227 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1228 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1229 uint16_t nr_vec = 0;
1231 if (unlikely(reserve_avail_buf_split(dev, vq,
1232 pkt_len, buf_vec, &num_buffers,
1233 avail_head, &nr_vec) < 0)) {
1234 VHOST_LOG_DATA(DEBUG,
1235 "(%d) failed to get enough desc from vring\n",
1237 vq->shadow_used_idx -= num_buffers;
1241 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1242 dev->vid, vq->last_avail_idx,
1243 vq->last_avail_idx + num_buffers);
1245 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1248 vq->shadow_used_idx -= num_buffers;
1252 vq->last_avail_idx += num_buffers;
1255 do_data_copy_enqueue(dev, vq);
1257 if (likely(vq->shadow_used_idx)) {
1258 flush_shadow_used_ring_split(dev, vq);
1259 vhost_vring_call_split(dev, vq);
1265 static __rte_always_inline int
1266 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1267 struct vhost_virtqueue *vq,
1268 struct rte_mbuf **pkts,
1269 uint64_t *desc_addrs,
1272 bool wrap_counter = vq->avail_wrap_counter;
1273 struct vring_packed_desc *descs = vq->desc_packed;
1274 uint16_t avail_idx = vq->last_avail_idx;
1275 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1278 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1281 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1284 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1285 if (unlikely(pkts[i]->next != NULL))
1287 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1292 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1293 lens[i] = descs[avail_idx + i].len;
1295 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1296 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1300 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1301 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1302 descs[avail_idx + i].addr,
1306 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1307 if (unlikely(!desc_addrs[i]))
1309 if (unlikely(lens[i] != descs[avail_idx + i].len))
1316 static __rte_always_inline void
1317 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1318 struct vhost_virtqueue *vq,
1319 struct rte_mbuf **pkts,
1320 uint64_t *desc_addrs,
1323 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1324 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1325 struct vring_packed_desc *descs = vq->desc_packed;
1326 uint16_t avail_idx = vq->last_avail_idx;
1327 uint16_t ids[PACKED_BATCH_SIZE];
1330 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1331 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1332 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1333 (uintptr_t)desc_addrs[i];
1334 lens[i] = pkts[i]->pkt_len +
1335 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1338 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1339 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1341 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1343 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1344 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1345 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1349 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1350 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1353 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1354 ids[i] = descs[avail_idx + i].id;
1356 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1359 static __rte_always_inline int
1360 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1361 struct vhost_virtqueue *vq,
1362 struct rte_mbuf **pkts)
1364 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1365 uint64_t lens[PACKED_BATCH_SIZE];
1367 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1370 if (vq->shadow_used_idx) {
1371 do_data_copy_enqueue(dev, vq);
1372 vhost_flush_enqueue_shadow_packed(dev, vq);
1375 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1380 static __rte_always_inline int16_t
1381 virtio_dev_rx_single_packed(struct virtio_net *dev,
1382 struct vhost_virtqueue *vq,
1383 struct rte_mbuf *pkt)
1385 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1386 uint16_t nr_descs = 0;
1388 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1390 VHOST_LOG_DATA(DEBUG,
1391 "(%d) failed to get enough desc from vring\n",
1396 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1397 dev->vid, vq->last_avail_idx,
1398 vq->last_avail_idx + nr_descs);
1400 vq_inc_last_avail_packed(vq, nr_descs);
1405 static __rte_noinline uint32_t
1406 virtio_dev_rx_packed(struct virtio_net *dev,
1407 struct vhost_virtqueue *__rte_restrict vq,
1408 struct rte_mbuf **__rte_restrict pkts,
1411 uint32_t pkt_idx = 0;
1414 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1416 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1417 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1419 pkt_idx += PACKED_BATCH_SIZE;
1424 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1428 } while (pkt_idx < count);
1430 if (vq->shadow_used_idx) {
1431 do_data_copy_enqueue(dev, vq);
1432 vhost_flush_enqueue_shadow_packed(dev, vq);
1436 vhost_vring_call_packed(dev, vq);
1441 static __rte_always_inline uint32_t
1442 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1443 struct rte_mbuf **pkts, uint32_t count)
1445 struct vhost_virtqueue *vq;
1448 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1449 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1450 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1451 dev->vid, __func__, queue_id);
1455 vq = dev->virtqueue[queue_id];
1457 rte_spinlock_lock(&vq->access_lock);
1459 if (unlikely(!vq->enabled))
1460 goto out_access_unlock;
1462 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1463 vhost_user_iotlb_rd_lock(vq);
1465 if (unlikely(!vq->access_ok))
1466 if (unlikely(vring_translate(dev, vq) < 0))
1469 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1473 if (vq_is_packed(dev))
1474 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1476 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1479 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1480 vhost_user_iotlb_rd_unlock(vq);
1483 rte_spinlock_unlock(&vq->access_lock);
1489 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1490 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1492 struct virtio_net *dev = get_device(vid);
1497 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1499 "(%d) %s: built-in vhost net backend is disabled.\n",
1500 dev->vid, __func__);
1504 return virtio_dev_rx(dev, queue_id, pkts, count);
1507 static __rte_always_inline uint16_t
1508 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1510 struct vhost_async *async = vq->async;
1512 if (async->pkts_idx >= async->pkts_inflight_n)
1513 return async->pkts_idx - async->pkts_inflight_n;
1515 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1518 static __rte_always_inline void
1519 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1520 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1522 size_t elem_size = sizeof(struct vring_used_elem);
1524 if (d_idx + count <= ring_size) {
1525 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1527 uint16_t size = ring_size - d_idx;
1529 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1530 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1534 static __rte_always_inline void
1535 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1536 struct vring_used_elem_packed *d_ring,
1537 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1539 size_t elem_size = sizeof(struct vring_used_elem_packed);
1541 if (d_idx + count <= ring_size) {
1542 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1544 uint16_t size = ring_size - d_idx;
1546 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1547 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1551 static __rte_noinline uint32_t
1552 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1553 struct vhost_virtqueue *vq, uint16_t queue_id,
1554 struct rte_mbuf **pkts, uint32_t count)
1556 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1557 uint32_t pkt_idx = 0;
1558 uint16_t num_buffers;
1559 uint16_t avail_head;
1561 struct vhost_async *async = vq->async;
1562 struct async_inflight_info *pkts_info = async->pkts_info;
1563 uint32_t pkt_err = 0;
1565 uint16_t slot_idx = 0;
1568 * The ordering between avail index and desc reads need to be enforced.
1570 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1572 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1574 async_iter_reset(async);
1576 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1577 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1578 uint16_t nr_vec = 0;
1580 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1581 &num_buffers, avail_head, &nr_vec) < 0)) {
1582 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n",
1584 vq->shadow_used_idx -= num_buffers;
1588 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1589 dev->vid, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1591 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers) < 0) {
1592 vq->shadow_used_idx -= num_buffers;
1596 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1597 pkts_info[slot_idx].descs = num_buffers;
1598 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1600 vq->last_avail_idx += num_buffers;
1603 if (unlikely(pkt_idx == 0))
1606 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1607 if (unlikely(n_xfer < 0)) {
1608 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1609 dev->vid, __func__, queue_id);
1613 pkt_err = pkt_idx - n_xfer;
1614 if (unlikely(pkt_err)) {
1615 uint16_t num_descs = 0;
1617 /* update number of completed packets */
1620 /* calculate the sum of descriptors to revert */
1621 while (pkt_err-- > 0) {
1622 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1626 /* recover shadow used ring and available ring */
1627 vq->shadow_used_idx -= num_descs;
1628 vq->last_avail_idx -= num_descs;
1631 /* keep used descriptors */
1632 if (likely(vq->shadow_used_idx)) {
1633 uint16_t to = async->desc_idx_split & (vq->size - 1);
1635 store_dma_desc_info_split(vq->shadow_used_split,
1636 async->descs_split, vq->size, 0, to,
1637 vq->shadow_used_idx);
1639 async->desc_idx_split += vq->shadow_used_idx;
1641 async->pkts_idx += pkt_idx;
1642 if (async->pkts_idx >= vq->size)
1643 async->pkts_idx -= vq->size;
1645 async->pkts_inflight_n += pkt_idx;
1646 vq->shadow_used_idx = 0;
1652 static __rte_always_inline void
1653 vhost_update_used_packed(struct vhost_virtqueue *vq,
1654 struct vring_used_elem_packed *shadow_ring,
1658 uint16_t used_idx = vq->last_used_idx;
1659 uint16_t head_idx = vq->last_used_idx;
1660 uint16_t head_flags = 0;
1665 /* Split loop in two to save memory barriers */
1666 for (i = 0; i < count; i++) {
1667 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1668 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1670 used_idx += shadow_ring[i].count;
1671 if (used_idx >= vq->size)
1672 used_idx -= vq->size;
1675 /* The ordering for storing desc flags needs to be enforced. */
1676 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1678 for (i = 0; i < count; i++) {
1681 if (vq->shadow_used_packed[i].len)
1682 flags = VRING_DESC_F_WRITE;
1686 if (vq->used_wrap_counter) {
1687 flags |= VRING_DESC_F_USED;
1688 flags |= VRING_DESC_F_AVAIL;
1690 flags &= ~VRING_DESC_F_USED;
1691 flags &= ~VRING_DESC_F_AVAIL;
1695 vq->desc_packed[vq->last_used_idx].flags = flags;
1697 head_idx = vq->last_used_idx;
1701 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1704 vq->desc_packed[head_idx].flags = head_flags;
1707 static __rte_always_inline int
1708 vhost_enqueue_async_packed(struct virtio_net *dev,
1709 struct vhost_virtqueue *vq,
1710 struct rte_mbuf *pkt,
1711 struct buf_vector *buf_vec,
1713 uint16_t *nr_buffers)
1715 uint16_t nr_vec = 0;
1716 uint16_t avail_idx = vq->last_avail_idx;
1717 uint16_t max_tries, tries = 0;
1718 uint16_t buf_id = 0;
1720 uint16_t desc_count = 0;
1721 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1722 uint32_t buffer_len[vq->size];
1723 uint16_t buffer_buf_id[vq->size];
1724 uint16_t buffer_desc_count[vq->size];
1726 if (rxvq_is_mergeable(dev))
1727 max_tries = vq->size - 1;
1733 * if we tried all available ring items, and still
1734 * can't get enough buf, it means something abnormal
1737 if (unlikely(++tries > max_tries))
1740 if (unlikely(fill_vec_buf_packed(dev, vq,
1741 avail_idx, &desc_count,
1744 VHOST_ACCESS_RW) < 0))
1747 len = RTE_MIN(len, size);
1750 buffer_len[*nr_buffers] = len;
1751 buffer_buf_id[*nr_buffers] = buf_id;
1752 buffer_desc_count[*nr_buffers] = desc_count;
1754 *nr_descs += desc_count;
1755 avail_idx += desc_count;
1756 if (avail_idx >= vq->size)
1757 avail_idx -= vq->size;
1760 if (unlikely(async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec,
1764 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1769 static __rte_always_inline int16_t
1770 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1771 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1773 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1775 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1776 nr_descs, nr_buffers) < 0)) {
1777 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1781 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1782 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1787 static __rte_always_inline void
1788 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1789 uint32_t nr_err, uint32_t *pkt_idx)
1791 uint16_t descs_err = 0;
1792 uint16_t buffers_err = 0;
1793 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1796 /* calculate the sum of buffers and descs of DMA-error packets. */
1797 while (nr_err-- > 0) {
1798 descs_err += pkts_info[slot_idx % vq->size].descs;
1799 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1803 if (vq->last_avail_idx >= descs_err) {
1804 vq->last_avail_idx -= descs_err;
1806 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1807 vq->avail_wrap_counter ^= 1;
1810 vq->shadow_used_idx -= buffers_err;
1813 static __rte_noinline uint32_t
1814 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1815 struct vhost_virtqueue *vq, uint16_t queue_id,
1816 struct rte_mbuf **pkts, uint32_t count)
1818 uint32_t pkt_idx = 0;
1819 uint32_t remained = count;
1821 uint16_t num_buffers;
1824 struct vhost_async *async = vq->async;
1825 struct async_inflight_info *pkts_info = async->pkts_info;
1826 uint32_t pkt_err = 0;
1827 uint16_t slot_idx = 0;
1830 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1834 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1835 &num_descs, &num_buffers) < 0))
1838 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1840 pkts_info[slot_idx].descs = num_descs;
1841 pkts_info[slot_idx].nr_buffers = num_buffers;
1842 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1846 vq_inc_last_avail_packed(vq, num_descs);
1847 } while (pkt_idx < count);
1849 if (unlikely(pkt_idx == 0))
1852 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1853 if (unlikely(n_xfer < 0)) {
1854 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1855 dev->vid, __func__, queue_id);
1859 pkt_err = pkt_idx - n_xfer;
1861 async_iter_reset(async);
1863 if (unlikely(pkt_err))
1864 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1866 if (likely(vq->shadow_used_idx)) {
1867 /* keep used descriptors. */
1868 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1869 vq->size, 0, async->buffer_idx_packed,
1870 vq->shadow_used_idx);
1872 async->buffer_idx_packed += vq->shadow_used_idx;
1873 if (async->buffer_idx_packed >= vq->size)
1874 async->buffer_idx_packed -= vq->size;
1876 async->pkts_idx += pkt_idx;
1877 if (async->pkts_idx >= vq->size)
1878 async->pkts_idx -= vq->size;
1880 vq->shadow_used_idx = 0;
1881 async->pkts_inflight_n += pkt_idx;
1887 static __rte_always_inline void
1888 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1890 struct vhost_async *async = vq->async;
1891 uint16_t nr_left = n_descs;
1896 from = async->last_desc_idx_split & (vq->size - 1);
1897 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1898 to = vq->last_used_idx & (vq->size - 1);
1900 if (to + nr_copy <= vq->size) {
1901 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1902 nr_copy * sizeof(struct vring_used_elem));
1904 uint16_t size = vq->size - to;
1906 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1907 size * sizeof(struct vring_used_elem));
1908 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1909 (nr_copy - size) * sizeof(struct vring_used_elem));
1912 async->last_desc_idx_split += nr_copy;
1913 vq->last_used_idx += nr_copy;
1915 } while (nr_left > 0);
1918 static __rte_always_inline void
1919 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1922 struct vhost_async *async = vq->async;
1923 uint16_t nr_left = n_buffers;
1927 from = async->last_buffer_idx_packed;
1928 to = (from + nr_left) % vq->size;
1930 vhost_update_used_packed(vq, async->buffers_packed + from, to - from);
1931 async->last_buffer_idx_packed += nr_left;
1934 vhost_update_used_packed(vq, async->buffers_packed + from,
1936 async->last_buffer_idx_packed = 0;
1937 nr_left -= vq->size - from;
1939 } while (nr_left > 0);
1942 static __rte_always_inline uint16_t
1943 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1944 struct rte_mbuf **pkts, uint16_t count)
1946 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1947 struct vhost_async *async = vq->async;
1948 struct async_inflight_info *pkts_info = async->pkts_info;
1950 uint16_t n_descs = 0, n_buffers = 0;
1951 uint16_t start_idx, from, i;
1953 n_cpl = async->ops.check_completed_copies(dev->vid, queue_id, 0, count);
1954 if (unlikely(n_cpl < 0)) {
1955 VHOST_LOG_DATA(ERR, "(%d) %s: failed to check completed copies for queue id %d.\n",
1956 dev->vid, __func__, queue_id);
1963 start_idx = async_get_first_inflight_pkt_idx(vq);
1965 for (i = 0; i < n_cpl; i++) {
1966 from = (start_idx + i) % vq->size;
1967 /* Only used with packed ring */
1968 n_buffers += pkts_info[from].nr_buffers;
1969 /* Only used with split ring */
1970 n_descs += pkts_info[from].descs;
1971 pkts[i] = pkts_info[from].mbuf;
1974 async->pkts_inflight_n -= n_cpl;
1976 if (likely(vq->enabled && vq->access_ok)) {
1977 if (vq_is_packed(dev)) {
1978 write_back_completed_descs_packed(vq, n_buffers);
1979 vhost_vring_call_packed(dev, vq);
1981 write_back_completed_descs_split(vq, n_descs);
1982 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1983 vhost_vring_call_split(dev, vq);
1986 if (vq_is_packed(dev)) {
1987 async->last_buffer_idx_packed += n_buffers;
1988 if (async->last_buffer_idx_packed >= vq->size)
1989 async->last_buffer_idx_packed -= vq->size;
1991 async->last_desc_idx_split += n_descs;
1999 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2000 struct rte_mbuf **pkts, uint16_t count)
2002 struct virtio_net *dev = get_device(vid);
2003 struct vhost_virtqueue *vq;
2004 uint16_t n_pkts_cpl = 0;
2009 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2010 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2011 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2012 dev->vid, __func__, queue_id);
2016 vq = dev->virtqueue[queue_id];
2018 if (unlikely(!vq->async)) {
2019 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2020 dev->vid, __func__, queue_id);
2024 rte_spinlock_lock(&vq->access_lock);
2026 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2028 rte_spinlock_unlock(&vq->access_lock);
2034 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2035 struct rte_mbuf **pkts, uint16_t count)
2037 struct virtio_net *dev = get_device(vid);
2038 struct vhost_virtqueue *vq;
2039 uint16_t n_pkts_cpl = 0;
2044 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2045 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2046 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2047 dev->vid, __func__, queue_id);
2051 vq = dev->virtqueue[queue_id];
2053 if (unlikely(!vq->async)) {
2054 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2055 dev->vid, __func__, queue_id);
2059 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2064 static __rte_always_inline uint32_t
2065 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2066 struct rte_mbuf **pkts, uint32_t count)
2068 struct vhost_virtqueue *vq;
2071 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2072 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2073 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2074 dev->vid, __func__, queue_id);
2078 vq = dev->virtqueue[queue_id];
2080 rte_spinlock_lock(&vq->access_lock);
2082 if (unlikely(!vq->enabled || !vq->async))
2083 goto out_access_unlock;
2085 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2086 vhost_user_iotlb_rd_lock(vq);
2088 if (unlikely(!vq->access_ok))
2089 if (unlikely(vring_translate(dev, vq) < 0))
2092 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2096 if (vq_is_packed(dev))
2097 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2100 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2104 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2105 vhost_user_iotlb_rd_unlock(vq);
2108 rte_spinlock_unlock(&vq->access_lock);
2114 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2115 struct rte_mbuf **pkts, uint16_t count)
2117 struct virtio_net *dev = get_device(vid);
2122 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2124 "(%d) %s: built-in vhost net backend is disabled.\n",
2125 dev->vid, __func__);
2129 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2133 virtio_net_with_host_offload(struct virtio_net *dev)
2136 ((1ULL << VIRTIO_NET_F_CSUM) |
2137 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2138 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2139 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2140 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2147 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2149 struct rte_ipv4_hdr *ipv4_hdr;
2150 struct rte_ipv6_hdr *ipv6_hdr;
2151 struct rte_ether_hdr *eth_hdr;
2153 uint16_t data_len = rte_pktmbuf_data_len(m);
2155 if (data_len < sizeof(struct rte_ether_hdr))
2158 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2160 m->l2_len = sizeof(struct rte_ether_hdr);
2161 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2163 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2164 if (data_len < sizeof(struct rte_ether_hdr) +
2165 sizeof(struct rte_vlan_hdr))
2168 struct rte_vlan_hdr *vlan_hdr =
2169 (struct rte_vlan_hdr *)(eth_hdr + 1);
2171 m->l2_len += sizeof(struct rte_vlan_hdr);
2172 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2175 switch (ethertype) {
2176 case RTE_ETHER_TYPE_IPV4:
2177 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2179 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2181 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2182 if (data_len < m->l2_len + m->l3_len)
2184 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2185 *l4_proto = ipv4_hdr->next_proto_id;
2187 case RTE_ETHER_TYPE_IPV6:
2188 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2190 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2192 m->l3_len = sizeof(struct rte_ipv6_hdr);
2193 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2194 *l4_proto = ipv6_hdr->proto;
2197 /* a valid L3 header is needed for further L4 parsing */
2201 /* both CSUM and GSO need a valid L4 header */
2202 switch (*l4_proto) {
2204 if (data_len < m->l2_len + m->l3_len +
2205 sizeof(struct rte_tcp_hdr))
2209 if (data_len < m->l2_len + m->l3_len +
2210 sizeof(struct rte_udp_hdr))
2214 if (data_len < m->l2_len + m->l3_len +
2215 sizeof(struct rte_sctp_hdr))
2231 static __rte_always_inline void
2232 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2234 uint8_t l4_proto = 0;
2235 struct rte_tcp_hdr *tcp_hdr = NULL;
2237 uint16_t data_len = rte_pktmbuf_data_len(m);
2239 if (parse_headers(m, &l4_proto) < 0)
2242 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2243 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2244 switch (hdr->csum_offset) {
2245 case (offsetof(struct rte_tcp_hdr, cksum)):
2246 if (l4_proto != IPPROTO_TCP)
2248 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2250 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2251 if (l4_proto != IPPROTO_UDP)
2253 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2255 case (offsetof(struct rte_sctp_hdr, cksum)):
2256 if (l4_proto != IPPROTO_SCTP)
2258 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2268 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2269 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2270 case VIRTIO_NET_HDR_GSO_TCPV4:
2271 case VIRTIO_NET_HDR_GSO_TCPV6:
2272 if (l4_proto != IPPROTO_TCP)
2274 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2275 struct rte_tcp_hdr *,
2276 m->l2_len + m->l3_len);
2277 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2278 if (data_len < m->l2_len + m->l3_len + tcp_len)
2280 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2281 m->tso_segsz = hdr->gso_size;
2282 m->l4_len = tcp_len;
2284 case VIRTIO_NET_HDR_GSO_UDP:
2285 if (l4_proto != IPPROTO_UDP)
2287 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2288 m->tso_segsz = hdr->gso_size;
2289 m->l4_len = sizeof(struct rte_udp_hdr);
2292 VHOST_LOG_DATA(WARNING,
2293 "unsupported gso type %u.\n", hdr->gso_type);
2305 static __rte_always_inline void
2306 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2307 bool legacy_ol_flags)
2309 struct rte_net_hdr_lens hdr_lens;
2310 int l4_supported = 0;
2313 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2316 if (legacy_ol_flags) {
2317 vhost_dequeue_offload_legacy(hdr, m);
2321 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2323 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2324 m->packet_type = ptype;
2325 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2326 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2327 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2330 /* According to Virtio 1.1 spec, the device only needs to look at
2331 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2332 * This differs from the processing incoming packets path where the
2333 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2336 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2337 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2338 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2340 * 5.1.6.2.2 Device Requirements: Packet Transmission
2341 * The device MUST ignore flag bits that it does not recognize.
2343 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2346 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2347 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2348 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2350 /* Unknown proto or tunnel, do sw cksum. We can assume
2351 * the cksum field is in the first segment since the
2352 * buffers we provided to the host are large enough.
2353 * In case of SCTP, this will be wrong since it's a CRC
2354 * but there's nothing we can do.
2356 uint16_t csum = 0, off;
2358 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2359 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2361 if (likely(csum != 0xffff))
2363 off = hdr->csum_offset + hdr->csum_start;
2364 if (rte_pktmbuf_data_len(m) >= off + 1)
2365 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2369 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2370 if (hdr->gso_size == 0)
2373 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2374 case VIRTIO_NET_HDR_GSO_TCPV4:
2375 case VIRTIO_NET_HDR_GSO_TCPV6:
2376 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2378 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2379 m->tso_segsz = hdr->gso_size;
2381 case VIRTIO_NET_HDR_GSO_UDP:
2382 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2384 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2385 m->tso_segsz = hdr->gso_size;
2393 static __rte_noinline void
2394 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2395 struct buf_vector *buf_vec)
2398 uint64_t remain = sizeof(struct virtio_net_hdr);
2400 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2403 len = RTE_MIN(remain, buf_vec->buf_len);
2404 src = buf_vec->buf_addr;
2405 rte_memcpy((void *)(uintptr_t)dst,
2406 (void *)(uintptr_t)src, len);
2414 static __rte_always_inline int
2415 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2416 struct buf_vector *buf_vec, uint16_t nr_vec,
2417 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2418 bool legacy_ol_flags)
2420 uint32_t buf_avail, buf_offset;
2421 uint64_t buf_addr, buf_len;
2422 uint32_t mbuf_avail, mbuf_offset;
2424 struct rte_mbuf *cur = m, *prev = m;
2425 struct virtio_net_hdr tmp_hdr;
2426 struct virtio_net_hdr *hdr = NULL;
2427 /* A counter to avoid desc dead loop chain */
2428 uint16_t vec_idx = 0;
2429 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2432 buf_addr = buf_vec[vec_idx].buf_addr;
2433 buf_len = buf_vec[vec_idx].buf_len;
2435 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2440 if (virtio_net_with_host_offload(dev)) {
2441 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2443 * No luck, the virtio-net header doesn't fit
2444 * in a contiguous virtual area.
2446 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2449 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2454 * A virtio driver normally uses at least 2 desc buffers
2455 * for Tx: the first for storing the header, and others
2456 * for storing the data.
2458 if (unlikely(buf_len < dev->vhost_hlen)) {
2459 buf_offset = dev->vhost_hlen - buf_len;
2461 buf_addr = buf_vec[vec_idx].buf_addr;
2462 buf_len = buf_vec[vec_idx].buf_len;
2463 buf_avail = buf_len - buf_offset;
2464 } else if (buf_len == dev->vhost_hlen) {
2465 if (unlikely(++vec_idx >= nr_vec))
2467 buf_addr = buf_vec[vec_idx].buf_addr;
2468 buf_len = buf_vec[vec_idx].buf_len;
2471 buf_avail = buf_len;
2473 buf_offset = dev->vhost_hlen;
2474 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2478 (uintptr_t)(buf_addr + buf_offset),
2479 (uint32_t)buf_avail, 0);
2482 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2484 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2486 if (likely(cpy_len > MAX_BATCH_LEN ||
2487 vq->batch_copy_nb_elems >= vq->size ||
2488 (hdr && cur == m))) {
2489 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2491 (void *)((uintptr_t)(buf_addr +
2492 buf_offset)), cpy_len);
2494 batch_copy[vq->batch_copy_nb_elems].dst =
2495 rte_pktmbuf_mtod_offset(cur, void *,
2497 batch_copy[vq->batch_copy_nb_elems].src =
2498 (void *)((uintptr_t)(buf_addr + buf_offset));
2499 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2500 vq->batch_copy_nb_elems++;
2503 mbuf_avail -= cpy_len;
2504 mbuf_offset += cpy_len;
2505 buf_avail -= cpy_len;
2506 buf_offset += cpy_len;
2508 /* This buf reaches to its end, get the next one */
2509 if (buf_avail == 0) {
2510 if (++vec_idx >= nr_vec)
2513 buf_addr = buf_vec[vec_idx].buf_addr;
2514 buf_len = buf_vec[vec_idx].buf_len;
2517 buf_avail = buf_len;
2519 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2520 (uint32_t)buf_avail, 0);
2524 * This mbuf reaches to its end, get a new one
2525 * to hold more data.
2527 if (mbuf_avail == 0) {
2528 cur = rte_pktmbuf_alloc(mbuf_pool);
2529 if (unlikely(cur == NULL)) {
2530 VHOST_LOG_DATA(ERR, "Failed to "
2531 "allocate memory for mbuf.\n");
2537 prev->data_len = mbuf_offset;
2539 m->pkt_len += mbuf_offset;
2543 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2547 prev->data_len = mbuf_offset;
2548 m->pkt_len += mbuf_offset;
2551 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2559 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2565 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2567 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2568 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2573 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2574 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2576 if (unlikely(total_len > UINT16_MAX))
2579 buf_len = total_len;
2580 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2581 if (unlikely(buf == NULL))
2584 /* Initialize shinfo */
2585 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2586 virtio_dev_extbuf_free, buf);
2587 if (unlikely(shinfo == NULL)) {
2589 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2593 iova = rte_malloc_virt2iova(buf);
2594 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2595 rte_pktmbuf_reset_headroom(pkt);
2601 * Prepare a host supported pktmbuf.
2603 static __rte_always_inline int
2604 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2607 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2610 /* attach an external buffer if supported */
2611 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2614 /* check if chained buffers are allowed */
2615 if (!dev->linearbuf)
2623 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2624 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2625 bool legacy_ol_flags)
2628 uint16_t free_entries;
2629 uint16_t dropped = 0;
2630 static bool allocerr_warned;
2633 * The ordering between avail index and
2634 * desc reads needs to be enforced.
2636 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2638 if (free_entries == 0)
2641 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2643 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2645 count = RTE_MIN(count, MAX_PKT_BURST);
2646 count = RTE_MIN(count, free_entries);
2647 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2650 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2653 for (i = 0; i < count; i++) {
2654 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2657 uint16_t nr_vec = 0;
2660 if (unlikely(fill_vec_buf_split(dev, vq,
2661 vq->last_avail_idx + i,
2663 &head_idx, &buf_len,
2664 VHOST_ACCESS_RO) < 0))
2667 update_shadow_used_ring_split(vq, head_idx, 0);
2669 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2670 if (unlikely(err)) {
2672 * mbuf allocation fails for jumbo packets when external
2673 * buffer allocation is not allowed and linear buffer
2674 * is required. Drop this packet.
2676 if (!allocerr_warned) {
2678 "Failed mbuf alloc of size %d from %s on %s.\n",
2679 buf_len, mbuf_pool->name, dev->ifname);
2680 allocerr_warned = true;
2687 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2688 mbuf_pool, legacy_ol_flags);
2689 if (unlikely(err)) {
2690 if (!allocerr_warned) {
2692 "Failed to copy desc to mbuf on %s.\n",
2694 allocerr_warned = true;
2703 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2705 vq->last_avail_idx += i;
2707 do_data_copy_dequeue(vq);
2708 if (unlikely(i < count))
2709 vq->shadow_used_idx = i;
2710 if (likely(vq->shadow_used_idx)) {
2711 flush_shadow_used_ring_split(dev, vq);
2712 vhost_vring_call_split(dev, vq);
2715 return (i - dropped);
2720 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2721 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2722 struct rte_mbuf **pkts, uint16_t count)
2724 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2729 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2730 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2731 struct rte_mbuf **pkts, uint16_t count)
2733 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2736 static __rte_always_inline int
2737 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2738 struct vhost_virtqueue *vq,
2739 struct rte_mbuf **pkts,
2741 uintptr_t *desc_addrs,
2744 bool wrap = vq->avail_wrap_counter;
2745 struct vring_packed_desc *descs = vq->desc_packed;
2746 uint64_t lens[PACKED_BATCH_SIZE];
2747 uint64_t buf_lens[PACKED_BATCH_SIZE];
2748 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2751 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2753 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2756 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2757 flags = descs[avail_idx + i].flags;
2758 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2759 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2760 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2764 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2766 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2767 lens[i] = descs[avail_idx + i].len;
2769 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2770 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2771 descs[avail_idx + i].addr,
2772 &lens[i], VHOST_ACCESS_RW);
2775 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2776 if (unlikely(!desc_addrs[i]))
2778 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2782 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2783 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2787 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2788 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2790 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2791 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2795 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2796 pkts[i]->pkt_len = lens[i] - buf_offset;
2797 pkts[i]->data_len = pkts[i]->pkt_len;
2798 ids[i] = descs[avail_idx + i].id;
2807 static __rte_always_inline int
2808 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2809 struct vhost_virtqueue *vq,
2810 struct rte_mbuf **pkts,
2811 bool legacy_ol_flags)
2813 uint16_t avail_idx = vq->last_avail_idx;
2814 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2815 struct virtio_net_hdr *hdr;
2816 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2817 uint16_t ids[PACKED_BATCH_SIZE];
2820 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2824 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2825 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2827 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2828 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2829 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2832 if (virtio_net_with_host_offload(dev)) {
2833 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2834 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2835 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2839 if (virtio_net_is_inorder(dev))
2840 vhost_shadow_dequeue_batch_packed_inorder(vq,
2841 ids[PACKED_BATCH_SIZE - 1]);
2843 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2845 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2850 static __rte_always_inline int
2851 vhost_dequeue_single_packed(struct virtio_net *dev,
2852 struct vhost_virtqueue *vq,
2853 struct rte_mempool *mbuf_pool,
2854 struct rte_mbuf *pkts,
2856 uint16_t *desc_count,
2857 bool legacy_ol_flags)
2859 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2861 uint16_t nr_vec = 0;
2863 static bool allocerr_warned;
2865 if (unlikely(fill_vec_buf_packed(dev, vq,
2866 vq->last_avail_idx, desc_count,
2869 VHOST_ACCESS_RO) < 0))
2872 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2873 if (!allocerr_warned) {
2875 "Failed mbuf alloc of size %d from %s on %s.\n",
2876 buf_len, mbuf_pool->name, dev->ifname);
2877 allocerr_warned = true;
2882 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2883 mbuf_pool, legacy_ol_flags);
2884 if (unlikely(err)) {
2885 if (!allocerr_warned) {
2887 "Failed to copy desc to mbuf on %s.\n",
2889 allocerr_warned = true;
2897 static __rte_always_inline int
2898 virtio_dev_tx_single_packed(struct virtio_net *dev,
2899 struct vhost_virtqueue *vq,
2900 struct rte_mempool *mbuf_pool,
2901 struct rte_mbuf *pkts,
2902 bool legacy_ol_flags)
2905 uint16_t buf_id, desc_count = 0;
2908 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2909 &desc_count, legacy_ol_flags);
2911 if (likely(desc_count > 0)) {
2912 if (virtio_net_is_inorder(dev))
2913 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2916 vhost_shadow_dequeue_single_packed(vq, buf_id,
2919 vq_inc_last_avail_packed(vq, desc_count);
2927 virtio_dev_tx_packed(struct virtio_net *dev,
2928 struct vhost_virtqueue *__rte_restrict vq,
2929 struct rte_mempool *mbuf_pool,
2930 struct rte_mbuf **__rte_restrict pkts,
2932 bool legacy_ol_flags)
2934 uint32_t pkt_idx = 0;
2936 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2940 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2942 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2943 if (!virtio_dev_tx_batch_packed(dev, vq,
2946 pkt_idx += PACKED_BATCH_SIZE;
2951 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2956 } while (pkt_idx < count);
2958 if (pkt_idx != count)
2959 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
2961 if (vq->shadow_used_idx) {
2962 do_data_copy_dequeue(vq);
2964 vhost_flush_dequeue_shadow_packed(dev, vq);
2965 vhost_vring_call_packed(dev, vq);
2973 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
2974 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2975 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2977 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
2982 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
2983 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2984 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2986 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
2990 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2991 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2993 struct virtio_net *dev;
2994 struct rte_mbuf *rarp_mbuf = NULL;
2995 struct vhost_virtqueue *vq;
2996 int16_t success = 1;
2998 dev = get_device(vid);
3002 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3004 "(%d) %s: built-in vhost net backend is disabled.\n",
3005 dev->vid, __func__);
3009 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3011 "(%d) %s: invalid virtqueue idx %d.\n",
3012 dev->vid, __func__, queue_id);
3016 vq = dev->virtqueue[queue_id];
3018 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3021 if (unlikely(!vq->enabled)) {
3023 goto out_access_unlock;
3026 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3027 vhost_user_iotlb_rd_lock(vq);
3029 if (unlikely(!vq->access_ok))
3030 if (unlikely(vring_translate(dev, vq) < 0)) {
3036 * Construct a RARP broadcast packet, and inject it to the "pkts"
3037 * array, to looks like that guest actually send such packet.
3039 * Check user_send_rarp() for more information.
3041 * broadcast_rarp shares a cacheline in the virtio_net structure
3042 * with some fields that are accessed during enqueue and
3043 * __atomic_compare_exchange_n causes a write if performed compare
3044 * and exchange. This could result in false sharing between enqueue
3047 * Prevent unnecessary false sharing by reading broadcast_rarp first
3048 * and only performing compare and exchange if the read indicates it
3049 * is likely to be set.
3051 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3052 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3053 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3055 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3056 if (rarp_mbuf == NULL) {
3057 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3062 * Inject it to the head of "pkts" array, so that switch's mac
3063 * learning table will get updated first.
3065 pkts[0] = rarp_mbuf;
3070 if (vq_is_packed(dev)) {
3071 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3072 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3074 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3076 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3077 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3079 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3083 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3084 vhost_user_iotlb_rd_unlock(vq);
3087 rte_spinlock_unlock(&vq->access_lock);
3089 if (unlikely(rarp_mbuf != NULL))