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 virtio_net *dev, 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, "(%s) no more async iovec available\n", dev->ifname);
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 virtio_net *dev, struct vhost_async *async,
813 void *src, void *dst, size_t len)
815 struct rte_vhost_iov_iter *iter;
816 struct rte_vhost_iovec *iovec;
818 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
819 static bool vhost_max_async_vec_log;
821 if (!vhost_max_async_vec_log) {
822 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
823 vhost_max_async_vec_log = true;
829 iter = async->iov_iter + async->iter_idx;
830 iovec = async->iovec + async->iovec_idx;
832 iovec->src_addr = src;
833 iovec->dst_addr = dst;
842 static __rte_always_inline void
843 async_iter_finalize(struct vhost_async *async)
848 static __rte_always_inline void
849 async_iter_cancel(struct vhost_async *async)
851 struct rte_vhost_iov_iter *iter;
853 iter = async->iov_iter + async->iter_idx;
854 async->iovec_idx -= iter->nr_segs;
859 static __rte_always_inline void
860 async_iter_reset(struct vhost_async *async)
863 async->iovec_idx = 0;
866 static __rte_always_inline int
867 async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
868 struct rte_mbuf *m, uint32_t mbuf_offset,
869 uint64_t buf_iova, uint32_t cpy_len)
871 struct vhost_async *async = vq->async;
873 uint32_t buf_offset = 0;
877 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
878 buf_iova + buf_offset, cpy_len, &mapped_len);
879 if (unlikely(!hpa)) {
880 VHOST_LOG_DATA(ERR, "(%s) %s: failed to get hpa.\n", dev->ifname, __func__);
884 if (unlikely(async_iter_add_iovec(dev, async,
885 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
887 hpa, (size_t)mapped_len)))
890 cpy_len -= (uint32_t)mapped_len;
891 mbuf_offset += (uint32_t)mapped_len;
892 buf_offset += (uint32_t)mapped_len;
898 static __rte_always_inline void
899 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
900 struct rte_mbuf *m, uint32_t mbuf_offset,
901 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
903 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
905 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
906 rte_memcpy((void *)((uintptr_t)(buf_addr)),
907 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
909 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
910 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
912 batch_copy[vq->batch_copy_nb_elems].dst =
913 (void *)((uintptr_t)(buf_addr));
914 batch_copy[vq->batch_copy_nb_elems].src =
915 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
916 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
917 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
918 vq->batch_copy_nb_elems++;
922 static __rte_always_inline int
923 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
924 struct rte_mbuf *m, struct buf_vector *buf_vec,
925 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
927 uint32_t vec_idx = 0;
928 uint32_t mbuf_offset, mbuf_avail;
929 uint32_t buf_offset, buf_avail;
930 uint64_t buf_addr, buf_iova, buf_len;
933 struct rte_mbuf *hdr_mbuf;
934 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
935 struct vhost_async *async = vq->async;
937 if (unlikely(m == NULL))
940 buf_addr = buf_vec[vec_idx].buf_addr;
941 buf_iova = buf_vec[vec_idx].buf_iova;
942 buf_len = buf_vec[vec_idx].buf_len;
944 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
949 if (unlikely(buf_len < dev->vhost_hlen)) {
950 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
953 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
955 VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
956 dev->ifname, num_buffers);
958 if (unlikely(buf_len < dev->vhost_hlen)) {
959 buf_offset = dev->vhost_hlen - buf_len;
961 buf_addr = buf_vec[vec_idx].buf_addr;
962 buf_iova = buf_vec[vec_idx].buf_iova;
963 buf_len = buf_vec[vec_idx].buf_len;
964 buf_avail = buf_len - buf_offset;
966 buf_offset = dev->vhost_hlen;
967 buf_avail = buf_len - dev->vhost_hlen;
970 mbuf_avail = rte_pktmbuf_data_len(m);
974 if (async_iter_initialize(dev, async))
978 while (mbuf_avail != 0 || m->next != NULL) {
979 /* done with current buf, get the next one */
980 if (buf_avail == 0) {
982 if (unlikely(vec_idx >= nr_vec))
985 buf_addr = buf_vec[vec_idx].buf_addr;
986 buf_iova = buf_vec[vec_idx].buf_iova;
987 buf_len = buf_vec[vec_idx].buf_len;
993 /* done with current mbuf, get the next one */
994 if (mbuf_avail == 0) {
998 mbuf_avail = rte_pktmbuf_data_len(m);
1002 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1003 if (rxvq_is_mergeable(dev))
1004 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1007 if (unlikely(hdr == &tmp_hdr)) {
1008 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1010 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1011 dev->vhost_hlen, 0);
1012 vhost_log_cache_write_iova(dev, vq,
1013 buf_vec[0].buf_iova,
1020 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1023 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1024 buf_iova + buf_offset, cpy_len) < 0)
1027 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1028 buf_addr + buf_offset,
1029 buf_iova + buf_offset, cpy_len);
1032 mbuf_avail -= cpy_len;
1033 mbuf_offset += cpy_len;
1034 buf_avail -= cpy_len;
1035 buf_offset += cpy_len;
1039 async_iter_finalize(async);
1044 async_iter_cancel(async);
1049 static __rte_always_inline int
1050 vhost_enqueue_single_packed(struct virtio_net *dev,
1051 struct vhost_virtqueue *vq,
1052 struct rte_mbuf *pkt,
1053 struct buf_vector *buf_vec,
1056 uint16_t nr_vec = 0;
1057 uint16_t avail_idx = vq->last_avail_idx;
1058 uint16_t max_tries, tries = 0;
1059 uint16_t buf_id = 0;
1061 uint16_t desc_count;
1062 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1063 uint16_t num_buffers = 0;
1064 uint32_t buffer_len[vq->size];
1065 uint16_t buffer_buf_id[vq->size];
1066 uint16_t buffer_desc_count[vq->size];
1068 if (rxvq_is_mergeable(dev))
1069 max_tries = vq->size - 1;
1075 * if we tried all available ring items, and still
1076 * can't get enough buf, it means something abnormal
1079 if (unlikely(++tries > max_tries))
1082 if (unlikely(fill_vec_buf_packed(dev, vq,
1083 avail_idx, &desc_count,
1086 VHOST_ACCESS_RW) < 0))
1089 len = RTE_MIN(len, size);
1092 buffer_len[num_buffers] = len;
1093 buffer_buf_id[num_buffers] = buf_id;
1094 buffer_desc_count[num_buffers] = desc_count;
1097 *nr_descs += desc_count;
1098 avail_idx += desc_count;
1099 if (avail_idx >= vq->size)
1100 avail_idx -= vq->size;
1103 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1106 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1107 buffer_desc_count, num_buffers);
1112 static __rte_noinline uint32_t
1113 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1114 struct rte_mbuf **pkts, uint32_t count)
1116 uint32_t pkt_idx = 0;
1117 uint16_t num_buffers;
1118 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1119 uint16_t avail_head;
1122 * The ordering between avail index and
1123 * desc reads needs to be enforced.
1125 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1127 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1129 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1130 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1131 uint16_t nr_vec = 0;
1133 if (unlikely(reserve_avail_buf_split(dev, vq,
1134 pkt_len, buf_vec, &num_buffers,
1135 avail_head, &nr_vec) < 0)) {
1136 VHOST_LOG_DATA(DEBUG,
1137 "(%s) failed to get enough desc from vring\n",
1139 vq->shadow_used_idx -= num_buffers;
1143 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1144 dev->ifname, vq->last_avail_idx,
1145 vq->last_avail_idx + num_buffers);
1147 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1148 num_buffers, false) < 0) {
1149 vq->shadow_used_idx -= num_buffers;
1153 vq->last_avail_idx += num_buffers;
1156 do_data_copy_enqueue(dev, vq);
1158 if (likely(vq->shadow_used_idx)) {
1159 flush_shadow_used_ring_split(dev, vq);
1160 vhost_vring_call_split(dev, vq);
1166 static __rte_always_inline int
1167 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1168 struct vhost_virtqueue *vq,
1169 struct rte_mbuf **pkts,
1170 uint64_t *desc_addrs,
1173 bool wrap_counter = vq->avail_wrap_counter;
1174 struct vring_packed_desc *descs = vq->desc_packed;
1175 uint16_t avail_idx = vq->last_avail_idx;
1176 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1179 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1182 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1185 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1186 if (unlikely(pkts[i]->next != NULL))
1188 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1193 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1194 lens[i] = descs[avail_idx + i].len;
1196 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1197 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1201 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1202 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1203 descs[avail_idx + i].addr,
1207 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1208 if (unlikely(!desc_addrs[i]))
1210 if (unlikely(lens[i] != descs[avail_idx + i].len))
1217 static __rte_always_inline void
1218 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1219 struct vhost_virtqueue *vq,
1220 struct rte_mbuf **pkts,
1221 uint64_t *desc_addrs,
1224 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1225 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1226 struct vring_packed_desc *descs = vq->desc_packed;
1227 uint16_t avail_idx = vq->last_avail_idx;
1228 uint16_t ids[PACKED_BATCH_SIZE];
1231 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1232 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1233 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1234 (uintptr_t)desc_addrs[i];
1235 lens[i] = pkts[i]->pkt_len +
1236 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1239 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1240 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1242 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1244 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1245 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1246 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1250 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1251 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1254 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1255 ids[i] = descs[avail_idx + i].id;
1257 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1260 static __rte_always_inline int
1261 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1262 struct vhost_virtqueue *vq,
1263 struct rte_mbuf **pkts)
1265 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1266 uint64_t lens[PACKED_BATCH_SIZE];
1268 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1271 if (vq->shadow_used_idx) {
1272 do_data_copy_enqueue(dev, vq);
1273 vhost_flush_enqueue_shadow_packed(dev, vq);
1276 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1281 static __rte_always_inline int16_t
1282 virtio_dev_rx_single_packed(struct virtio_net *dev,
1283 struct vhost_virtqueue *vq,
1284 struct rte_mbuf *pkt)
1286 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1287 uint16_t nr_descs = 0;
1289 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1291 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1296 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1297 dev->ifname, vq->last_avail_idx,
1298 vq->last_avail_idx + nr_descs);
1300 vq_inc_last_avail_packed(vq, nr_descs);
1305 static __rte_noinline uint32_t
1306 virtio_dev_rx_packed(struct virtio_net *dev,
1307 struct vhost_virtqueue *__rte_restrict vq,
1308 struct rte_mbuf **__rte_restrict pkts,
1311 uint32_t pkt_idx = 0;
1314 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1316 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1317 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1319 pkt_idx += PACKED_BATCH_SIZE;
1324 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1328 } while (pkt_idx < count);
1330 if (vq->shadow_used_idx) {
1331 do_data_copy_enqueue(dev, vq);
1332 vhost_flush_enqueue_shadow_packed(dev, vq);
1336 vhost_vring_call_packed(dev, vq);
1341 static __rte_always_inline uint32_t
1342 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1343 struct rte_mbuf **pkts, uint32_t count)
1345 struct vhost_virtqueue *vq;
1348 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1349 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1350 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1351 dev->ifname, __func__, queue_id);
1355 vq = dev->virtqueue[queue_id];
1357 rte_spinlock_lock(&vq->access_lock);
1359 if (unlikely(!vq->enabled))
1360 goto out_access_unlock;
1362 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1363 vhost_user_iotlb_rd_lock(vq);
1365 if (unlikely(!vq->access_ok))
1366 if (unlikely(vring_translate(dev, vq) < 0))
1369 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1373 if (vq_is_packed(dev))
1374 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1376 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1379 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1380 vhost_user_iotlb_rd_unlock(vq);
1383 rte_spinlock_unlock(&vq->access_lock);
1389 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1390 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1392 struct virtio_net *dev = get_device(vid);
1397 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1398 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1399 dev->ifname, __func__);
1403 return virtio_dev_rx(dev, queue_id, pkts, count);
1406 static __rte_always_inline uint16_t
1407 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1409 struct vhost_async *async = vq->async;
1411 if (async->pkts_idx >= async->pkts_inflight_n)
1412 return async->pkts_idx - async->pkts_inflight_n;
1414 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1417 static __rte_always_inline void
1418 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1419 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1421 size_t elem_size = sizeof(struct vring_used_elem);
1423 if (d_idx + count <= ring_size) {
1424 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1426 uint16_t size = ring_size - d_idx;
1428 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1429 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1433 static __rte_always_inline void
1434 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1435 struct vring_used_elem_packed *d_ring,
1436 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1438 size_t elem_size = sizeof(struct vring_used_elem_packed);
1440 if (d_idx + count <= ring_size) {
1441 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1443 uint16_t size = ring_size - d_idx;
1445 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1446 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1450 static __rte_noinline uint32_t
1451 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1452 struct vhost_virtqueue *vq, uint16_t queue_id,
1453 struct rte_mbuf **pkts, uint32_t count)
1455 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1456 uint32_t pkt_idx = 0;
1457 uint16_t num_buffers;
1458 uint16_t avail_head;
1460 struct vhost_async *async = vq->async;
1461 struct async_inflight_info *pkts_info = async->pkts_info;
1462 uint32_t pkt_err = 0;
1464 uint16_t slot_idx = 0;
1467 * The ordering between avail index and desc reads need to be enforced.
1469 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1471 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1473 async_iter_reset(async);
1475 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1476 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1477 uint16_t nr_vec = 0;
1479 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1480 &num_buffers, avail_head, &nr_vec) < 0)) {
1481 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1483 vq->shadow_used_idx -= num_buffers;
1487 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1488 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1490 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1491 vq->shadow_used_idx -= num_buffers;
1495 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1496 pkts_info[slot_idx].descs = num_buffers;
1497 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1499 vq->last_avail_idx += num_buffers;
1502 if (unlikely(pkt_idx == 0))
1505 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1506 if (unlikely(n_xfer < 0)) {
1507 VHOST_LOG_DATA(ERR, "(%s) %s: failed to transfer data for queue id %d.\n",
1508 dev->ifname, __func__, queue_id);
1512 pkt_err = pkt_idx - n_xfer;
1513 if (unlikely(pkt_err)) {
1514 uint16_t num_descs = 0;
1516 /* update number of completed packets */
1519 /* calculate the sum of descriptors to revert */
1520 while (pkt_err-- > 0) {
1521 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1525 /* recover shadow used ring and available ring */
1526 vq->shadow_used_idx -= num_descs;
1527 vq->last_avail_idx -= num_descs;
1530 /* keep used descriptors */
1531 if (likely(vq->shadow_used_idx)) {
1532 uint16_t to = async->desc_idx_split & (vq->size - 1);
1534 store_dma_desc_info_split(vq->shadow_used_split,
1535 async->descs_split, vq->size, 0, to,
1536 vq->shadow_used_idx);
1538 async->desc_idx_split += vq->shadow_used_idx;
1540 async->pkts_idx += pkt_idx;
1541 if (async->pkts_idx >= vq->size)
1542 async->pkts_idx -= vq->size;
1544 async->pkts_inflight_n += pkt_idx;
1545 vq->shadow_used_idx = 0;
1552 static __rte_always_inline int
1553 vhost_enqueue_async_packed(struct virtio_net *dev,
1554 struct vhost_virtqueue *vq,
1555 struct rte_mbuf *pkt,
1556 struct buf_vector *buf_vec,
1558 uint16_t *nr_buffers)
1560 uint16_t nr_vec = 0;
1561 uint16_t avail_idx = vq->last_avail_idx;
1562 uint16_t max_tries, tries = 0;
1563 uint16_t buf_id = 0;
1565 uint16_t desc_count = 0;
1566 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1567 uint32_t buffer_len[vq->size];
1568 uint16_t buffer_buf_id[vq->size];
1569 uint16_t buffer_desc_count[vq->size];
1571 if (rxvq_is_mergeable(dev))
1572 max_tries = vq->size - 1;
1578 * if we tried all available ring items, and still
1579 * can't get enough buf, it means something abnormal
1582 if (unlikely(++tries > max_tries))
1585 if (unlikely(fill_vec_buf_packed(dev, vq,
1586 avail_idx, &desc_count,
1589 VHOST_ACCESS_RW) < 0))
1592 len = RTE_MIN(len, size);
1595 buffer_len[*nr_buffers] = len;
1596 buffer_buf_id[*nr_buffers] = buf_id;
1597 buffer_desc_count[*nr_buffers] = desc_count;
1599 *nr_descs += desc_count;
1600 avail_idx += desc_count;
1601 if (avail_idx >= vq->size)
1602 avail_idx -= vq->size;
1605 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1608 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1613 static __rte_always_inline int16_t
1614 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1615 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1617 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1619 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1620 nr_descs, nr_buffers) < 0)) {
1621 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1625 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1626 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1631 static __rte_always_inline void
1632 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1633 uint32_t nr_err, uint32_t *pkt_idx)
1635 uint16_t descs_err = 0;
1636 uint16_t buffers_err = 0;
1637 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1640 /* calculate the sum of buffers and descs of DMA-error packets. */
1641 while (nr_err-- > 0) {
1642 descs_err += pkts_info[slot_idx % vq->size].descs;
1643 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1647 if (vq->last_avail_idx >= descs_err) {
1648 vq->last_avail_idx -= descs_err;
1650 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1651 vq->avail_wrap_counter ^= 1;
1654 vq->shadow_used_idx -= buffers_err;
1657 static __rte_noinline uint32_t
1658 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1659 struct vhost_virtqueue *vq, uint16_t queue_id,
1660 struct rte_mbuf **pkts, uint32_t count)
1662 uint32_t pkt_idx = 0;
1663 uint32_t remained = count;
1665 uint16_t num_buffers;
1668 struct vhost_async *async = vq->async;
1669 struct async_inflight_info *pkts_info = async->pkts_info;
1670 uint32_t pkt_err = 0;
1671 uint16_t slot_idx = 0;
1674 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1678 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1679 &num_descs, &num_buffers) < 0))
1682 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1684 pkts_info[slot_idx].descs = num_descs;
1685 pkts_info[slot_idx].nr_buffers = num_buffers;
1686 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1690 vq_inc_last_avail_packed(vq, num_descs);
1691 } while (pkt_idx < count);
1693 if (unlikely(pkt_idx == 0))
1696 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1697 if (unlikely(n_xfer < 0)) {
1698 VHOST_LOG_DATA(ERR, "(%s) %s: failed to transfer data for queue id %d.\n",
1699 dev->ifname, __func__, queue_id);
1703 pkt_err = pkt_idx - n_xfer;
1705 async_iter_reset(async);
1707 if (unlikely(pkt_err))
1708 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1710 if (likely(vq->shadow_used_idx)) {
1711 /* keep used descriptors. */
1712 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1713 vq->size, 0, async->buffer_idx_packed,
1714 vq->shadow_used_idx);
1716 async->buffer_idx_packed += vq->shadow_used_idx;
1717 if (async->buffer_idx_packed >= vq->size)
1718 async->buffer_idx_packed -= vq->size;
1720 async->pkts_idx += pkt_idx;
1721 if (async->pkts_idx >= vq->size)
1722 async->pkts_idx -= vq->size;
1724 vq->shadow_used_idx = 0;
1725 async->pkts_inflight_n += pkt_idx;
1731 static __rte_always_inline void
1732 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1734 struct vhost_async *async = vq->async;
1735 uint16_t nr_left = n_descs;
1740 from = async->last_desc_idx_split & (vq->size - 1);
1741 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1742 to = vq->last_used_idx & (vq->size - 1);
1744 if (to + nr_copy <= vq->size) {
1745 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1746 nr_copy * sizeof(struct vring_used_elem));
1748 uint16_t size = vq->size - to;
1750 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1751 size * sizeof(struct vring_used_elem));
1752 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1753 (nr_copy - size) * sizeof(struct vring_used_elem));
1756 async->last_desc_idx_split += nr_copy;
1757 vq->last_used_idx += nr_copy;
1759 } while (nr_left > 0);
1762 static __rte_always_inline void
1763 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1766 struct vhost_async *async = vq->async;
1767 uint16_t from = async->last_buffer_idx_packed;
1768 uint16_t used_idx = vq->last_used_idx;
1769 uint16_t head_idx = vq->last_used_idx;
1770 uint16_t head_flags = 0;
1773 /* Split loop in two to save memory barriers */
1774 for (i = 0; i < n_buffers; i++) {
1775 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1776 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1778 used_idx += async->buffers_packed[from].count;
1779 if (used_idx >= vq->size)
1780 used_idx -= vq->size;
1783 if (from >= vq->size)
1787 /* The ordering for storing desc flags needs to be enforced. */
1788 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1790 from = async->last_buffer_idx_packed;
1792 for (i = 0; i < n_buffers; i++) {
1795 if (async->buffers_packed[from].len)
1796 flags = VRING_DESC_F_WRITE;
1800 if (vq->used_wrap_counter) {
1801 flags |= VRING_DESC_F_USED;
1802 flags |= VRING_DESC_F_AVAIL;
1804 flags &= ~VRING_DESC_F_USED;
1805 flags &= ~VRING_DESC_F_AVAIL;
1809 vq->desc_packed[vq->last_used_idx].flags = flags;
1811 head_idx = vq->last_used_idx;
1815 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
1818 if (from == vq->size)
1822 vq->desc_packed[head_idx].flags = head_flags;
1823 async->last_buffer_idx_packed = from;
1826 static __rte_always_inline uint16_t
1827 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1828 struct rte_mbuf **pkts, uint16_t count)
1830 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1831 struct vhost_async *async = vq->async;
1832 struct async_inflight_info *pkts_info = async->pkts_info;
1834 uint16_t n_descs = 0, n_buffers = 0;
1835 uint16_t start_idx, from, i;
1837 n_cpl = async->ops.check_completed_copies(dev->vid, queue_id, 0, count);
1838 if (unlikely(n_cpl < 0)) {
1839 VHOST_LOG_DATA(ERR, "(%s) %s: failed to check completed copies for queue id %d.\n",
1840 dev->ifname, __func__, queue_id);
1847 start_idx = async_get_first_inflight_pkt_idx(vq);
1849 for (i = 0; i < n_cpl; i++) {
1850 from = (start_idx + i) % vq->size;
1851 /* Only used with packed ring */
1852 n_buffers += pkts_info[from].nr_buffers;
1853 /* Only used with split ring */
1854 n_descs += pkts_info[from].descs;
1855 pkts[i] = pkts_info[from].mbuf;
1858 async->pkts_inflight_n -= n_cpl;
1860 if (likely(vq->enabled && vq->access_ok)) {
1861 if (vq_is_packed(dev)) {
1862 write_back_completed_descs_packed(vq, n_buffers);
1863 vhost_vring_call_packed(dev, vq);
1865 write_back_completed_descs_split(vq, n_descs);
1866 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1867 vhost_vring_call_split(dev, vq);
1870 if (vq_is_packed(dev)) {
1871 async->last_buffer_idx_packed += n_buffers;
1872 if (async->last_buffer_idx_packed >= vq->size)
1873 async->last_buffer_idx_packed -= vq->size;
1875 async->last_desc_idx_split += n_descs;
1883 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1884 struct rte_mbuf **pkts, uint16_t count)
1886 struct virtio_net *dev = get_device(vid);
1887 struct vhost_virtqueue *vq;
1888 uint16_t n_pkts_cpl = 0;
1893 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1894 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1895 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1896 dev->ifname, __func__, queue_id);
1900 vq = dev->virtqueue[queue_id];
1902 if (unlikely(!vq->async)) {
1903 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
1904 dev->ifname, __func__, queue_id);
1908 rte_spinlock_lock(&vq->access_lock);
1910 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
1912 rte_spinlock_unlock(&vq->access_lock);
1918 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
1919 struct rte_mbuf **pkts, uint16_t count)
1921 struct virtio_net *dev = get_device(vid);
1922 struct vhost_virtqueue *vq;
1923 uint16_t n_pkts_cpl = 0;
1928 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1929 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1930 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1931 dev->ifname, __func__, queue_id);
1935 vq = dev->virtqueue[queue_id];
1937 if (unlikely(!vq->async)) {
1938 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
1939 dev->ifname, __func__, queue_id);
1943 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
1948 static __rte_always_inline uint32_t
1949 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1950 struct rte_mbuf **pkts, uint32_t count)
1952 struct vhost_virtqueue *vq;
1955 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1956 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1957 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1958 dev->ifname, __func__, queue_id);
1962 vq = dev->virtqueue[queue_id];
1964 rte_spinlock_lock(&vq->access_lock);
1966 if (unlikely(!vq->enabled || !vq->async))
1967 goto out_access_unlock;
1969 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1970 vhost_user_iotlb_rd_lock(vq);
1972 if (unlikely(!vq->access_ok))
1973 if (unlikely(vring_translate(dev, vq) < 0))
1976 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1980 if (vq_is_packed(dev))
1981 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
1984 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
1988 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1989 vhost_user_iotlb_rd_unlock(vq);
1992 rte_spinlock_unlock(&vq->access_lock);
1998 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
1999 struct rte_mbuf **pkts, uint16_t count)
2001 struct virtio_net *dev = get_device(vid);
2006 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2007 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2008 dev->ifname, __func__);
2012 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2016 virtio_net_with_host_offload(struct virtio_net *dev)
2019 ((1ULL << VIRTIO_NET_F_CSUM) |
2020 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2021 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2022 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2023 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2030 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2032 struct rte_ipv4_hdr *ipv4_hdr;
2033 struct rte_ipv6_hdr *ipv6_hdr;
2034 struct rte_ether_hdr *eth_hdr;
2036 uint16_t data_len = rte_pktmbuf_data_len(m);
2038 if (data_len < sizeof(struct rte_ether_hdr))
2041 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2043 m->l2_len = sizeof(struct rte_ether_hdr);
2044 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2046 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2047 if (data_len < sizeof(struct rte_ether_hdr) +
2048 sizeof(struct rte_vlan_hdr))
2051 struct rte_vlan_hdr *vlan_hdr =
2052 (struct rte_vlan_hdr *)(eth_hdr + 1);
2054 m->l2_len += sizeof(struct rte_vlan_hdr);
2055 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2058 switch (ethertype) {
2059 case RTE_ETHER_TYPE_IPV4:
2060 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2062 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2064 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2065 if (data_len < m->l2_len + m->l3_len)
2067 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2068 *l4_proto = ipv4_hdr->next_proto_id;
2070 case RTE_ETHER_TYPE_IPV6:
2071 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2073 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2075 m->l3_len = sizeof(struct rte_ipv6_hdr);
2076 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2077 *l4_proto = ipv6_hdr->proto;
2080 /* a valid L3 header is needed for further L4 parsing */
2084 /* both CSUM and GSO need a valid L4 header */
2085 switch (*l4_proto) {
2087 if (data_len < m->l2_len + m->l3_len +
2088 sizeof(struct rte_tcp_hdr))
2092 if (data_len < m->l2_len + m->l3_len +
2093 sizeof(struct rte_udp_hdr))
2097 if (data_len < m->l2_len + m->l3_len +
2098 sizeof(struct rte_sctp_hdr))
2114 static __rte_always_inline void
2115 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2118 uint8_t l4_proto = 0;
2119 struct rte_tcp_hdr *tcp_hdr = NULL;
2121 uint16_t data_len = rte_pktmbuf_data_len(m);
2123 if (parse_headers(m, &l4_proto) < 0)
2126 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2127 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2128 switch (hdr->csum_offset) {
2129 case (offsetof(struct rte_tcp_hdr, cksum)):
2130 if (l4_proto != IPPROTO_TCP)
2132 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2134 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2135 if (l4_proto != IPPROTO_UDP)
2137 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2139 case (offsetof(struct rte_sctp_hdr, cksum)):
2140 if (l4_proto != IPPROTO_SCTP)
2142 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2152 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2153 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2154 case VIRTIO_NET_HDR_GSO_TCPV4:
2155 case VIRTIO_NET_HDR_GSO_TCPV6:
2156 if (l4_proto != IPPROTO_TCP)
2158 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2159 struct rte_tcp_hdr *,
2160 m->l2_len + m->l3_len);
2161 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2162 if (data_len < m->l2_len + m->l3_len + tcp_len)
2164 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2165 m->tso_segsz = hdr->gso_size;
2166 m->l4_len = tcp_len;
2168 case VIRTIO_NET_HDR_GSO_UDP:
2169 if (l4_proto != IPPROTO_UDP)
2171 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2172 m->tso_segsz = hdr->gso_size;
2173 m->l4_len = sizeof(struct rte_udp_hdr);
2176 VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2177 dev->ifname, hdr->gso_type);
2189 static __rte_always_inline void
2190 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2191 struct rte_mbuf *m, bool legacy_ol_flags)
2193 struct rte_net_hdr_lens hdr_lens;
2194 int l4_supported = 0;
2197 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2200 if (legacy_ol_flags) {
2201 vhost_dequeue_offload_legacy(dev, hdr, m);
2205 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2207 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2208 m->packet_type = ptype;
2209 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2210 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2211 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2214 /* According to Virtio 1.1 spec, the device only needs to look at
2215 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2216 * This differs from the processing incoming packets path where the
2217 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2220 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2221 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2222 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2224 * 5.1.6.2.2 Device Requirements: Packet Transmission
2225 * The device MUST ignore flag bits that it does not recognize.
2227 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2230 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2231 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2232 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2234 /* Unknown proto or tunnel, do sw cksum. We can assume
2235 * the cksum field is in the first segment since the
2236 * buffers we provided to the host are large enough.
2237 * In case of SCTP, this will be wrong since it's a CRC
2238 * but there's nothing we can do.
2240 uint16_t csum = 0, off;
2242 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2243 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2245 if (likely(csum != 0xffff))
2247 off = hdr->csum_offset + hdr->csum_start;
2248 if (rte_pktmbuf_data_len(m) >= off + 1)
2249 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2253 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2254 if (hdr->gso_size == 0)
2257 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2258 case VIRTIO_NET_HDR_GSO_TCPV4:
2259 case VIRTIO_NET_HDR_GSO_TCPV6:
2260 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2262 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2263 m->tso_segsz = hdr->gso_size;
2265 case VIRTIO_NET_HDR_GSO_UDP:
2266 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2268 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2269 m->tso_segsz = hdr->gso_size;
2277 static __rte_noinline void
2278 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2279 struct buf_vector *buf_vec)
2282 uint64_t remain = sizeof(struct virtio_net_hdr);
2284 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2287 len = RTE_MIN(remain, buf_vec->buf_len);
2288 src = buf_vec->buf_addr;
2289 rte_memcpy((void *)(uintptr_t)dst,
2290 (void *)(uintptr_t)src, len);
2298 static __rte_always_inline int
2299 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2300 struct buf_vector *buf_vec, uint16_t nr_vec,
2301 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2302 bool legacy_ol_flags)
2304 uint32_t buf_avail, buf_offset;
2305 uint64_t buf_addr, buf_len;
2306 uint32_t mbuf_avail, mbuf_offset;
2308 struct rte_mbuf *cur = m, *prev = m;
2309 struct virtio_net_hdr tmp_hdr;
2310 struct virtio_net_hdr *hdr = NULL;
2311 /* A counter to avoid desc dead loop chain */
2312 uint16_t vec_idx = 0;
2313 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2316 buf_addr = buf_vec[vec_idx].buf_addr;
2317 buf_len = buf_vec[vec_idx].buf_len;
2319 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2324 if (virtio_net_with_host_offload(dev)) {
2325 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2327 * No luck, the virtio-net header doesn't fit
2328 * in a contiguous virtual area.
2330 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2333 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2338 * A virtio driver normally uses at least 2 desc buffers
2339 * for Tx: the first for storing the header, and others
2340 * for storing the data.
2342 if (unlikely(buf_len < dev->vhost_hlen)) {
2343 buf_offset = dev->vhost_hlen - buf_len;
2345 buf_addr = buf_vec[vec_idx].buf_addr;
2346 buf_len = buf_vec[vec_idx].buf_len;
2347 buf_avail = buf_len - buf_offset;
2348 } else if (buf_len == dev->vhost_hlen) {
2349 if (unlikely(++vec_idx >= nr_vec))
2351 buf_addr = buf_vec[vec_idx].buf_addr;
2352 buf_len = buf_vec[vec_idx].buf_len;
2355 buf_avail = buf_len;
2357 buf_offset = dev->vhost_hlen;
2358 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2362 (uintptr_t)(buf_addr + buf_offset),
2363 (uint32_t)buf_avail, 0);
2366 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2368 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2370 if (likely(cpy_len > MAX_BATCH_LEN ||
2371 vq->batch_copy_nb_elems >= vq->size ||
2372 (hdr && cur == m))) {
2373 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2375 (void *)((uintptr_t)(buf_addr +
2376 buf_offset)), cpy_len);
2378 batch_copy[vq->batch_copy_nb_elems].dst =
2379 rte_pktmbuf_mtod_offset(cur, void *,
2381 batch_copy[vq->batch_copy_nb_elems].src =
2382 (void *)((uintptr_t)(buf_addr + buf_offset));
2383 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2384 vq->batch_copy_nb_elems++;
2387 mbuf_avail -= cpy_len;
2388 mbuf_offset += cpy_len;
2389 buf_avail -= cpy_len;
2390 buf_offset += cpy_len;
2392 /* This buf reaches to its end, get the next one */
2393 if (buf_avail == 0) {
2394 if (++vec_idx >= nr_vec)
2397 buf_addr = buf_vec[vec_idx].buf_addr;
2398 buf_len = buf_vec[vec_idx].buf_len;
2401 buf_avail = buf_len;
2403 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2404 (uint32_t)buf_avail, 0);
2408 * This mbuf reaches to its end, get a new one
2409 * to hold more data.
2411 if (mbuf_avail == 0) {
2412 cur = rte_pktmbuf_alloc(mbuf_pool);
2413 if (unlikely(cur == NULL)) {
2414 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2421 prev->data_len = mbuf_offset;
2423 m->pkt_len += mbuf_offset;
2427 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2431 prev->data_len = mbuf_offset;
2432 m->pkt_len += mbuf_offset;
2435 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2443 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2449 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2451 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2452 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2457 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2458 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2460 if (unlikely(total_len > UINT16_MAX))
2463 buf_len = total_len;
2464 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2465 if (unlikely(buf == NULL))
2468 /* Initialize shinfo */
2469 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2470 virtio_dev_extbuf_free, buf);
2471 if (unlikely(shinfo == NULL)) {
2473 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2477 iova = rte_malloc_virt2iova(buf);
2478 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2479 rte_pktmbuf_reset_headroom(pkt);
2485 * Prepare a host supported pktmbuf.
2487 static __rte_always_inline int
2488 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2491 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2494 /* attach an external buffer if supported */
2495 if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2498 /* check if chained buffers are allowed */
2499 if (!dev->linearbuf)
2507 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2508 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2509 bool legacy_ol_flags)
2512 uint16_t free_entries;
2513 uint16_t dropped = 0;
2514 static bool allocerr_warned;
2517 * The ordering between avail index and
2518 * desc reads needs to be enforced.
2520 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2522 if (free_entries == 0)
2525 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2527 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2529 count = RTE_MIN(count, MAX_PKT_BURST);
2530 count = RTE_MIN(count, free_entries);
2531 VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2532 dev->ifname, count);
2534 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2537 for (i = 0; i < count; i++) {
2538 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2541 uint16_t nr_vec = 0;
2544 if (unlikely(fill_vec_buf_split(dev, vq,
2545 vq->last_avail_idx + i,
2547 &head_idx, &buf_len,
2548 VHOST_ACCESS_RO) < 0))
2551 update_shadow_used_ring_split(vq, head_idx, 0);
2553 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2554 if (unlikely(err)) {
2556 * mbuf allocation fails for jumbo packets when external
2557 * buffer allocation is not allowed and linear buffer
2558 * is required. Drop this packet.
2560 if (!allocerr_warned) {
2561 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2562 dev->ifname, buf_len, mbuf_pool->name);
2563 allocerr_warned = true;
2570 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2571 mbuf_pool, legacy_ol_flags);
2572 if (unlikely(err)) {
2573 if (!allocerr_warned) {
2574 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2576 allocerr_warned = true;
2585 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2587 vq->last_avail_idx += i;
2589 do_data_copy_dequeue(vq);
2590 if (unlikely(i < count))
2591 vq->shadow_used_idx = i;
2592 if (likely(vq->shadow_used_idx)) {
2593 flush_shadow_used_ring_split(dev, vq);
2594 vhost_vring_call_split(dev, vq);
2597 return (i - dropped);
2602 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2603 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2604 struct rte_mbuf **pkts, uint16_t count)
2606 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2611 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2612 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2613 struct rte_mbuf **pkts, uint16_t count)
2615 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2618 static __rte_always_inline int
2619 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2620 struct vhost_virtqueue *vq,
2621 struct rte_mbuf **pkts,
2623 uintptr_t *desc_addrs,
2626 bool wrap = vq->avail_wrap_counter;
2627 struct vring_packed_desc *descs = vq->desc_packed;
2628 uint64_t lens[PACKED_BATCH_SIZE];
2629 uint64_t buf_lens[PACKED_BATCH_SIZE];
2630 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2633 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2635 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2638 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2639 flags = descs[avail_idx + i].flags;
2640 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2641 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2642 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2646 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2648 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2649 lens[i] = descs[avail_idx + i].len;
2651 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2652 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2653 descs[avail_idx + i].addr,
2654 &lens[i], VHOST_ACCESS_RW);
2657 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2658 if (unlikely(!desc_addrs[i]))
2660 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2664 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2665 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2669 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2670 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2672 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2673 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2677 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2678 pkts[i]->pkt_len = lens[i] - buf_offset;
2679 pkts[i]->data_len = pkts[i]->pkt_len;
2680 ids[i] = descs[avail_idx + i].id;
2689 static __rte_always_inline int
2690 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2691 struct vhost_virtqueue *vq,
2692 struct rte_mbuf **pkts,
2693 bool legacy_ol_flags)
2695 uint16_t avail_idx = vq->last_avail_idx;
2696 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2697 struct virtio_net_hdr *hdr;
2698 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2699 uint16_t ids[PACKED_BATCH_SIZE];
2702 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2706 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2707 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2709 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2710 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2711 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2714 if (virtio_net_with_host_offload(dev)) {
2715 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2716 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2717 vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2721 if (virtio_net_is_inorder(dev))
2722 vhost_shadow_dequeue_batch_packed_inorder(vq,
2723 ids[PACKED_BATCH_SIZE - 1]);
2725 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2727 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2732 static __rte_always_inline int
2733 vhost_dequeue_single_packed(struct virtio_net *dev,
2734 struct vhost_virtqueue *vq,
2735 struct rte_mempool *mbuf_pool,
2736 struct rte_mbuf *pkts,
2738 uint16_t *desc_count,
2739 bool legacy_ol_flags)
2741 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2743 uint16_t nr_vec = 0;
2745 static bool allocerr_warned;
2747 if (unlikely(fill_vec_buf_packed(dev, vq,
2748 vq->last_avail_idx, desc_count,
2751 VHOST_ACCESS_RO) < 0))
2754 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2755 if (!allocerr_warned) {
2756 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2757 dev->ifname, buf_len, mbuf_pool->name);
2758 allocerr_warned = true;
2763 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2764 mbuf_pool, legacy_ol_flags);
2765 if (unlikely(err)) {
2766 if (!allocerr_warned) {
2767 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2769 allocerr_warned = true;
2777 static __rte_always_inline int
2778 virtio_dev_tx_single_packed(struct virtio_net *dev,
2779 struct vhost_virtqueue *vq,
2780 struct rte_mempool *mbuf_pool,
2781 struct rte_mbuf *pkts,
2782 bool legacy_ol_flags)
2785 uint16_t buf_id, desc_count = 0;
2788 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2789 &desc_count, legacy_ol_flags);
2791 if (likely(desc_count > 0)) {
2792 if (virtio_net_is_inorder(dev))
2793 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2796 vhost_shadow_dequeue_single_packed(vq, buf_id,
2799 vq_inc_last_avail_packed(vq, desc_count);
2807 virtio_dev_tx_packed(struct virtio_net *dev,
2808 struct vhost_virtqueue *__rte_restrict vq,
2809 struct rte_mempool *mbuf_pool,
2810 struct rte_mbuf **__rte_restrict pkts,
2812 bool legacy_ol_flags)
2814 uint32_t pkt_idx = 0;
2816 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2820 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2822 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2823 if (!virtio_dev_tx_batch_packed(dev, vq,
2826 pkt_idx += PACKED_BATCH_SIZE;
2831 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2836 } while (pkt_idx < count);
2838 if (pkt_idx != count)
2839 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
2841 if (vq->shadow_used_idx) {
2842 do_data_copy_dequeue(vq);
2844 vhost_flush_dequeue_shadow_packed(dev, vq);
2845 vhost_vring_call_packed(dev, vq);
2853 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
2854 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2855 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2857 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
2862 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
2863 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2864 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2866 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
2870 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2871 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2873 struct virtio_net *dev;
2874 struct rte_mbuf *rarp_mbuf = NULL;
2875 struct vhost_virtqueue *vq;
2876 int16_t success = 1;
2878 dev = get_device(vid);
2882 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2883 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2884 dev->ifname, __func__);
2888 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2889 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2890 dev->ifname, __func__, queue_id);
2894 vq = dev->virtqueue[queue_id];
2896 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2899 if (unlikely(!vq->enabled)) {
2901 goto out_access_unlock;
2904 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2905 vhost_user_iotlb_rd_lock(vq);
2907 if (unlikely(!vq->access_ok))
2908 if (unlikely(vring_translate(dev, vq) < 0)) {
2914 * Construct a RARP broadcast packet, and inject it to the "pkts"
2915 * array, to looks like that guest actually send such packet.
2917 * Check user_send_rarp() for more information.
2919 * broadcast_rarp shares a cacheline in the virtio_net structure
2920 * with some fields that are accessed during enqueue and
2921 * __atomic_compare_exchange_n causes a write if performed compare
2922 * and exchange. This could result in false sharing between enqueue
2925 * Prevent unnecessary false sharing by reading broadcast_rarp first
2926 * and only performing compare and exchange if the read indicates it
2927 * is likely to be set.
2929 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2930 __atomic_compare_exchange_n(&dev->broadcast_rarp,
2931 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2933 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2934 if (rarp_mbuf == NULL) {
2935 VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
2940 * Inject it to the head of "pkts" array, so that switch's mac
2941 * learning table will get updated first.
2943 pkts[0] = rarp_mbuf;
2948 if (vq_is_packed(dev)) {
2949 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
2950 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
2952 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
2954 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
2955 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
2957 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
2961 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2962 vhost_user_iotlb_rd_unlock(vq);
2965 rte_spinlock_unlock(&vq->access_lock);
2967 if (unlikely(rarp_mbuf != NULL))