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 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, bool is_async)
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;
934 struct vhost_async *async = vq->async;
936 if (unlikely(m == NULL))
939 buf_addr = buf_vec[vec_idx].buf_addr;
940 buf_iova = buf_vec[vec_idx].buf_iova;
941 buf_len = buf_vec[vec_idx].buf_len;
943 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
948 if (unlikely(buf_len < dev->vhost_hlen)) {
949 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
952 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
954 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
955 dev->vid, num_buffers);
957 if (unlikely(buf_len < dev->vhost_hlen)) {
958 buf_offset = dev->vhost_hlen - buf_len;
960 buf_addr = buf_vec[vec_idx].buf_addr;
961 buf_iova = buf_vec[vec_idx].buf_iova;
962 buf_len = buf_vec[vec_idx].buf_len;
963 buf_avail = buf_len - buf_offset;
965 buf_offset = dev->vhost_hlen;
966 buf_avail = buf_len - dev->vhost_hlen;
969 mbuf_avail = rte_pktmbuf_data_len(m);
973 if (async_iter_initialize(async))
977 while (mbuf_avail != 0 || m->next != NULL) {
978 /* done with current buf, get the next one */
979 if (buf_avail == 0) {
981 if (unlikely(vec_idx >= nr_vec))
984 buf_addr = buf_vec[vec_idx].buf_addr;
985 buf_iova = buf_vec[vec_idx].buf_iova;
986 buf_len = buf_vec[vec_idx].buf_len;
992 /* done with current mbuf, get the next one */
993 if (mbuf_avail == 0) {
997 mbuf_avail = rte_pktmbuf_data_len(m);
1001 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1002 if (rxvq_is_mergeable(dev))
1003 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1006 if (unlikely(hdr == &tmp_hdr)) {
1007 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1009 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1010 dev->vhost_hlen, 0);
1011 vhost_log_cache_write_iova(dev, vq,
1012 buf_vec[0].buf_iova,
1019 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1022 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1023 buf_iova + buf_offset, cpy_len) < 0)
1026 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1027 buf_addr + buf_offset,
1028 buf_iova + buf_offset, cpy_len);
1031 mbuf_avail -= cpy_len;
1032 mbuf_offset += cpy_len;
1033 buf_avail -= cpy_len;
1034 buf_offset += cpy_len;
1038 async_iter_finalize(async);
1043 async_iter_cancel(async);
1048 static __rte_always_inline int
1049 vhost_enqueue_single_packed(struct virtio_net *dev,
1050 struct vhost_virtqueue *vq,
1051 struct rte_mbuf *pkt,
1052 struct buf_vector *buf_vec,
1055 uint16_t nr_vec = 0;
1056 uint16_t avail_idx = vq->last_avail_idx;
1057 uint16_t max_tries, tries = 0;
1058 uint16_t buf_id = 0;
1060 uint16_t desc_count;
1061 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1062 uint16_t num_buffers = 0;
1063 uint32_t buffer_len[vq->size];
1064 uint16_t buffer_buf_id[vq->size];
1065 uint16_t buffer_desc_count[vq->size];
1067 if (rxvq_is_mergeable(dev))
1068 max_tries = vq->size - 1;
1074 * if we tried all available ring items, and still
1075 * can't get enough buf, it means something abnormal
1078 if (unlikely(++tries > max_tries))
1081 if (unlikely(fill_vec_buf_packed(dev, vq,
1082 avail_idx, &desc_count,
1085 VHOST_ACCESS_RW) < 0))
1088 len = RTE_MIN(len, size);
1091 buffer_len[num_buffers] = len;
1092 buffer_buf_id[num_buffers] = buf_id;
1093 buffer_desc_count[num_buffers] = desc_count;
1096 *nr_descs += desc_count;
1097 avail_idx += desc_count;
1098 if (avail_idx >= vq->size)
1099 avail_idx -= vq->size;
1102 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1105 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1106 buffer_desc_count, num_buffers);
1111 static __rte_noinline uint32_t
1112 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1113 struct rte_mbuf **pkts, uint32_t count)
1115 uint32_t pkt_idx = 0;
1116 uint16_t num_buffers;
1117 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1118 uint16_t avail_head;
1121 * The ordering between avail index and
1122 * desc reads needs to be enforced.
1124 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1126 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1128 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1129 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1130 uint16_t nr_vec = 0;
1132 if (unlikely(reserve_avail_buf_split(dev, vq,
1133 pkt_len, buf_vec, &num_buffers,
1134 avail_head, &nr_vec) < 0)) {
1135 VHOST_LOG_DATA(DEBUG,
1136 "(%d) failed to get enough desc from vring\n",
1138 vq->shadow_used_idx -= num_buffers;
1142 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1143 dev->vid, vq->last_avail_idx,
1144 vq->last_avail_idx + num_buffers);
1146 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1147 num_buffers, false) < 0) {
1148 vq->shadow_used_idx -= num_buffers;
1152 vq->last_avail_idx += num_buffers;
1155 do_data_copy_enqueue(dev, vq);
1157 if (likely(vq->shadow_used_idx)) {
1158 flush_shadow_used_ring_split(dev, vq);
1159 vhost_vring_call_split(dev, vq);
1165 static __rte_always_inline int
1166 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1167 struct vhost_virtqueue *vq,
1168 struct rte_mbuf **pkts,
1169 uint64_t *desc_addrs,
1172 bool wrap_counter = vq->avail_wrap_counter;
1173 struct vring_packed_desc *descs = vq->desc_packed;
1174 uint16_t avail_idx = vq->last_avail_idx;
1175 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1178 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1181 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1184 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1185 if (unlikely(pkts[i]->next != NULL))
1187 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1192 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1193 lens[i] = descs[avail_idx + i].len;
1195 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1196 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1200 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1201 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1202 descs[avail_idx + i].addr,
1206 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1207 if (unlikely(!desc_addrs[i]))
1209 if (unlikely(lens[i] != descs[avail_idx + i].len))
1216 static __rte_always_inline void
1217 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1218 struct vhost_virtqueue *vq,
1219 struct rte_mbuf **pkts,
1220 uint64_t *desc_addrs,
1223 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1224 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1225 struct vring_packed_desc *descs = vq->desc_packed;
1226 uint16_t avail_idx = vq->last_avail_idx;
1227 uint16_t ids[PACKED_BATCH_SIZE];
1230 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1231 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1232 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1233 (uintptr_t)desc_addrs[i];
1234 lens[i] = pkts[i]->pkt_len +
1235 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1238 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1239 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1241 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1243 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1244 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1245 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1249 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1250 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1253 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1254 ids[i] = descs[avail_idx + i].id;
1256 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1259 static __rte_always_inline int
1260 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1261 struct vhost_virtqueue *vq,
1262 struct rte_mbuf **pkts)
1264 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1265 uint64_t lens[PACKED_BATCH_SIZE];
1267 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1270 if (vq->shadow_used_idx) {
1271 do_data_copy_enqueue(dev, vq);
1272 vhost_flush_enqueue_shadow_packed(dev, vq);
1275 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1280 static __rte_always_inline int16_t
1281 virtio_dev_rx_single_packed(struct virtio_net *dev,
1282 struct vhost_virtqueue *vq,
1283 struct rte_mbuf *pkt)
1285 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1286 uint16_t nr_descs = 0;
1288 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1290 VHOST_LOG_DATA(DEBUG,
1291 "(%d) failed to get enough desc from vring\n",
1296 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1297 dev->vid, 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, "(%d) %s\n", dev->vid, __func__);
1349 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1350 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1351 dev->vid, __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))) {
1399 "(%d) %s: built-in vhost net backend is disabled.\n",
1400 dev->vid, __func__);
1404 return virtio_dev_rx(dev, queue_id, pkts, count);
1407 static __rte_always_inline uint16_t
1408 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1410 struct vhost_async *async = vq->async;
1412 if (async->pkts_idx >= async->pkts_inflight_n)
1413 return async->pkts_idx - async->pkts_inflight_n;
1415 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1418 static __rte_always_inline void
1419 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1420 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1422 size_t elem_size = sizeof(struct vring_used_elem);
1424 if (d_idx + count <= ring_size) {
1425 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1427 uint16_t size = ring_size - d_idx;
1429 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1430 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1434 static __rte_always_inline void
1435 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1436 struct vring_used_elem_packed *d_ring,
1437 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1439 size_t elem_size = sizeof(struct vring_used_elem_packed);
1441 if (d_idx + count <= ring_size) {
1442 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1444 uint16_t size = ring_size - d_idx;
1446 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1447 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1451 static __rte_noinline uint32_t
1452 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1453 struct vhost_virtqueue *vq, uint16_t queue_id,
1454 struct rte_mbuf **pkts, uint32_t count)
1456 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1457 uint32_t pkt_idx = 0;
1458 uint16_t num_buffers;
1459 uint16_t avail_head;
1461 struct vhost_async *async = vq->async;
1462 struct async_inflight_info *pkts_info = async->pkts_info;
1463 uint32_t pkt_err = 0;
1465 uint16_t slot_idx = 0;
1468 * The ordering between avail index and desc reads need to be enforced.
1470 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1472 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1474 async_iter_reset(async);
1476 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1477 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1478 uint16_t nr_vec = 0;
1480 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1481 &num_buffers, avail_head, &nr_vec) < 0)) {
1482 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n",
1484 vq->shadow_used_idx -= num_buffers;
1488 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1489 dev->vid, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1491 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1492 vq->shadow_used_idx -= num_buffers;
1496 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1497 pkts_info[slot_idx].descs = num_buffers;
1498 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1500 vq->last_avail_idx += num_buffers;
1503 if (unlikely(pkt_idx == 0))
1506 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1507 if (unlikely(n_xfer < 0)) {
1508 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1509 dev->vid, __func__, queue_id);
1513 pkt_err = pkt_idx - n_xfer;
1514 if (unlikely(pkt_err)) {
1515 uint16_t num_descs = 0;
1517 /* update number of completed packets */
1520 /* calculate the sum of descriptors to revert */
1521 while (pkt_err-- > 0) {
1522 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1526 /* recover shadow used ring and available ring */
1527 vq->shadow_used_idx -= num_descs;
1528 vq->last_avail_idx -= num_descs;
1531 /* keep used descriptors */
1532 if (likely(vq->shadow_used_idx)) {
1533 uint16_t to = async->desc_idx_split & (vq->size - 1);
1535 store_dma_desc_info_split(vq->shadow_used_split,
1536 async->descs_split, vq->size, 0, to,
1537 vq->shadow_used_idx);
1539 async->desc_idx_split += vq->shadow_used_idx;
1541 async->pkts_idx += pkt_idx;
1542 if (async->pkts_idx >= vq->size)
1543 async->pkts_idx -= vq->size;
1545 async->pkts_inflight_n += pkt_idx;
1546 vq->shadow_used_idx = 0;
1553 static __rte_always_inline int
1554 vhost_enqueue_async_packed(struct virtio_net *dev,
1555 struct vhost_virtqueue *vq,
1556 struct rte_mbuf *pkt,
1557 struct buf_vector *buf_vec,
1559 uint16_t *nr_buffers)
1561 uint16_t nr_vec = 0;
1562 uint16_t avail_idx = vq->last_avail_idx;
1563 uint16_t max_tries, tries = 0;
1564 uint16_t buf_id = 0;
1566 uint16_t desc_count = 0;
1567 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1568 uint32_t buffer_len[vq->size];
1569 uint16_t buffer_buf_id[vq->size];
1570 uint16_t buffer_desc_count[vq->size];
1572 if (rxvq_is_mergeable(dev))
1573 max_tries = vq->size - 1;
1579 * if we tried all available ring items, and still
1580 * can't get enough buf, it means something abnormal
1583 if (unlikely(++tries > max_tries))
1586 if (unlikely(fill_vec_buf_packed(dev, vq,
1587 avail_idx, &desc_count,
1590 VHOST_ACCESS_RW) < 0))
1593 len = RTE_MIN(len, size);
1596 buffer_len[*nr_buffers] = len;
1597 buffer_buf_id[*nr_buffers] = buf_id;
1598 buffer_desc_count[*nr_buffers] = desc_count;
1600 *nr_descs += desc_count;
1601 avail_idx += desc_count;
1602 if (avail_idx >= vq->size)
1603 avail_idx -= vq->size;
1606 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1609 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1614 static __rte_always_inline int16_t
1615 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1616 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1618 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1620 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1621 nr_descs, nr_buffers) < 0)) {
1622 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1626 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1627 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1632 static __rte_always_inline void
1633 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1634 uint32_t nr_err, uint32_t *pkt_idx)
1636 uint16_t descs_err = 0;
1637 uint16_t buffers_err = 0;
1638 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1641 /* calculate the sum of buffers and descs of DMA-error packets. */
1642 while (nr_err-- > 0) {
1643 descs_err += pkts_info[slot_idx % vq->size].descs;
1644 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1648 if (vq->last_avail_idx >= descs_err) {
1649 vq->last_avail_idx -= descs_err;
1651 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1652 vq->avail_wrap_counter ^= 1;
1655 vq->shadow_used_idx -= buffers_err;
1658 static __rte_noinline uint32_t
1659 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1660 struct vhost_virtqueue *vq, uint16_t queue_id,
1661 struct rte_mbuf **pkts, uint32_t count)
1663 uint32_t pkt_idx = 0;
1664 uint32_t remained = count;
1666 uint16_t num_buffers;
1669 struct vhost_async *async = vq->async;
1670 struct async_inflight_info *pkts_info = async->pkts_info;
1671 uint32_t pkt_err = 0;
1672 uint16_t slot_idx = 0;
1675 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1679 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1680 &num_descs, &num_buffers) < 0))
1683 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1685 pkts_info[slot_idx].descs = num_descs;
1686 pkts_info[slot_idx].nr_buffers = num_buffers;
1687 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1691 vq_inc_last_avail_packed(vq, num_descs);
1692 } while (pkt_idx < count);
1694 if (unlikely(pkt_idx == 0))
1697 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1698 if (unlikely(n_xfer < 0)) {
1699 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1700 dev->vid, __func__, queue_id);
1704 pkt_err = pkt_idx - n_xfer;
1706 async_iter_reset(async);
1708 if (unlikely(pkt_err))
1709 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1711 if (likely(vq->shadow_used_idx)) {
1712 /* keep used descriptors. */
1713 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1714 vq->size, 0, async->buffer_idx_packed,
1715 vq->shadow_used_idx);
1717 async->buffer_idx_packed += vq->shadow_used_idx;
1718 if (async->buffer_idx_packed >= vq->size)
1719 async->buffer_idx_packed -= vq->size;
1721 async->pkts_idx += pkt_idx;
1722 if (async->pkts_idx >= vq->size)
1723 async->pkts_idx -= vq->size;
1725 vq->shadow_used_idx = 0;
1726 async->pkts_inflight_n += pkt_idx;
1732 static __rte_always_inline void
1733 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1735 struct vhost_async *async = vq->async;
1736 uint16_t nr_left = n_descs;
1741 from = async->last_desc_idx_split & (vq->size - 1);
1742 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1743 to = vq->last_used_idx & (vq->size - 1);
1745 if (to + nr_copy <= vq->size) {
1746 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1747 nr_copy * sizeof(struct vring_used_elem));
1749 uint16_t size = vq->size - to;
1751 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1752 size * sizeof(struct vring_used_elem));
1753 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1754 (nr_copy - size) * sizeof(struct vring_used_elem));
1757 async->last_desc_idx_split += nr_copy;
1758 vq->last_used_idx += nr_copy;
1760 } while (nr_left > 0);
1763 static __rte_always_inline void
1764 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1767 struct vhost_async *async = vq->async;
1768 uint16_t from = async->last_buffer_idx_packed;
1769 uint16_t used_idx = vq->last_used_idx;
1770 uint16_t head_idx = vq->last_used_idx;
1771 uint16_t head_flags = 0;
1774 /* Split loop in two to save memory barriers */
1775 for (i = 0; i < n_buffers; i++) {
1776 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1777 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1779 used_idx += async->buffers_packed[from].count;
1780 if (used_idx >= vq->size)
1781 used_idx -= vq->size;
1784 if (from >= vq->size)
1788 /* The ordering for storing desc flags needs to be enforced. */
1789 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1791 from = async->last_buffer_idx_packed;
1793 for (i = 0; i < n_buffers; i++) {
1796 if (async->buffers_packed[from].len)
1797 flags = VRING_DESC_F_WRITE;
1801 if (vq->used_wrap_counter) {
1802 flags |= VRING_DESC_F_USED;
1803 flags |= VRING_DESC_F_AVAIL;
1805 flags &= ~VRING_DESC_F_USED;
1806 flags &= ~VRING_DESC_F_AVAIL;
1810 vq->desc_packed[vq->last_used_idx].flags = flags;
1812 head_idx = vq->last_used_idx;
1816 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
1819 if (from == vq->size)
1823 vq->desc_packed[head_idx].flags = head_flags;
1824 async->last_buffer_idx_packed = from;
1827 static __rte_always_inline uint16_t
1828 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1829 struct rte_mbuf **pkts, uint16_t count)
1831 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1832 struct vhost_async *async = vq->async;
1833 struct async_inflight_info *pkts_info = async->pkts_info;
1835 uint16_t n_descs = 0, n_buffers = 0;
1836 uint16_t start_idx, from, i;
1838 n_cpl = async->ops.check_completed_copies(dev->vid, queue_id, 0, count);
1839 if (unlikely(n_cpl < 0)) {
1840 VHOST_LOG_DATA(ERR, "(%d) %s: failed to check completed copies for queue id %d.\n",
1841 dev->vid, __func__, queue_id);
1848 start_idx = async_get_first_inflight_pkt_idx(vq);
1850 for (i = 0; i < n_cpl; i++) {
1851 from = (start_idx + i) % vq->size;
1852 /* Only used with packed ring */
1853 n_buffers += pkts_info[from].nr_buffers;
1854 /* Only used with split ring */
1855 n_descs += pkts_info[from].descs;
1856 pkts[i] = pkts_info[from].mbuf;
1859 async->pkts_inflight_n -= n_cpl;
1861 if (likely(vq->enabled && vq->access_ok)) {
1862 if (vq_is_packed(dev)) {
1863 write_back_completed_descs_packed(vq, n_buffers);
1864 vhost_vring_call_packed(dev, vq);
1866 write_back_completed_descs_split(vq, n_descs);
1867 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1868 vhost_vring_call_split(dev, vq);
1871 if (vq_is_packed(dev)) {
1872 async->last_buffer_idx_packed += n_buffers;
1873 if (async->last_buffer_idx_packed >= vq->size)
1874 async->last_buffer_idx_packed -= vq->size;
1876 async->last_desc_idx_split += n_descs;
1884 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1885 struct rte_mbuf **pkts, uint16_t count)
1887 struct virtio_net *dev = get_device(vid);
1888 struct vhost_virtqueue *vq;
1889 uint16_t n_pkts_cpl = 0;
1894 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1895 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1896 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1897 dev->vid, __func__, queue_id);
1901 vq = dev->virtqueue[queue_id];
1903 if (unlikely(!vq->async)) {
1904 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
1905 dev->vid, __func__, queue_id);
1909 rte_spinlock_lock(&vq->access_lock);
1911 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
1913 rte_spinlock_unlock(&vq->access_lock);
1919 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
1920 struct rte_mbuf **pkts, uint16_t count)
1922 struct virtio_net *dev = get_device(vid);
1923 struct vhost_virtqueue *vq;
1924 uint16_t n_pkts_cpl = 0;
1929 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1930 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1931 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1932 dev->vid, __func__, queue_id);
1936 vq = dev->virtqueue[queue_id];
1938 if (unlikely(!vq->async)) {
1939 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
1940 dev->vid, __func__, queue_id);
1944 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
1949 static __rte_always_inline uint32_t
1950 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1951 struct rte_mbuf **pkts, uint32_t count)
1953 struct vhost_virtqueue *vq;
1956 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1957 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1958 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1959 dev->vid, __func__, queue_id);
1963 vq = dev->virtqueue[queue_id];
1965 rte_spinlock_lock(&vq->access_lock);
1967 if (unlikely(!vq->enabled || !vq->async))
1968 goto out_access_unlock;
1970 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1971 vhost_user_iotlb_rd_lock(vq);
1973 if (unlikely(!vq->access_ok))
1974 if (unlikely(vring_translate(dev, vq) < 0))
1977 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1981 if (vq_is_packed(dev))
1982 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
1985 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
1989 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1990 vhost_user_iotlb_rd_unlock(vq);
1993 rte_spinlock_unlock(&vq->access_lock);
1999 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2000 struct rte_mbuf **pkts, uint16_t count)
2002 struct virtio_net *dev = get_device(vid);
2007 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2009 "(%d) %s: built-in vhost net backend is disabled.\n",
2010 dev->vid, __func__);
2014 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2018 virtio_net_with_host_offload(struct virtio_net *dev)
2021 ((1ULL << VIRTIO_NET_F_CSUM) |
2022 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2023 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2024 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2025 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2032 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2034 struct rte_ipv4_hdr *ipv4_hdr;
2035 struct rte_ipv6_hdr *ipv6_hdr;
2036 struct rte_ether_hdr *eth_hdr;
2038 uint16_t data_len = rte_pktmbuf_data_len(m);
2040 if (data_len < sizeof(struct rte_ether_hdr))
2043 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2045 m->l2_len = sizeof(struct rte_ether_hdr);
2046 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2048 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2049 if (data_len < sizeof(struct rte_ether_hdr) +
2050 sizeof(struct rte_vlan_hdr))
2053 struct rte_vlan_hdr *vlan_hdr =
2054 (struct rte_vlan_hdr *)(eth_hdr + 1);
2056 m->l2_len += sizeof(struct rte_vlan_hdr);
2057 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2060 switch (ethertype) {
2061 case RTE_ETHER_TYPE_IPV4:
2062 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2064 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2066 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2067 if (data_len < m->l2_len + m->l3_len)
2069 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2070 *l4_proto = ipv4_hdr->next_proto_id;
2072 case RTE_ETHER_TYPE_IPV6:
2073 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2075 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2077 m->l3_len = sizeof(struct rte_ipv6_hdr);
2078 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2079 *l4_proto = ipv6_hdr->proto;
2082 /* a valid L3 header is needed for further L4 parsing */
2086 /* both CSUM and GSO need a valid L4 header */
2087 switch (*l4_proto) {
2089 if (data_len < m->l2_len + m->l3_len +
2090 sizeof(struct rte_tcp_hdr))
2094 if (data_len < m->l2_len + m->l3_len +
2095 sizeof(struct rte_udp_hdr))
2099 if (data_len < m->l2_len + m->l3_len +
2100 sizeof(struct rte_sctp_hdr))
2116 static __rte_always_inline void
2117 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2119 uint8_t l4_proto = 0;
2120 struct rte_tcp_hdr *tcp_hdr = NULL;
2122 uint16_t data_len = rte_pktmbuf_data_len(m);
2124 if (parse_headers(m, &l4_proto) < 0)
2127 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2128 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2129 switch (hdr->csum_offset) {
2130 case (offsetof(struct rte_tcp_hdr, cksum)):
2131 if (l4_proto != IPPROTO_TCP)
2133 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2135 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2136 if (l4_proto != IPPROTO_UDP)
2138 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2140 case (offsetof(struct rte_sctp_hdr, cksum)):
2141 if (l4_proto != IPPROTO_SCTP)
2143 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2153 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2154 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2155 case VIRTIO_NET_HDR_GSO_TCPV4:
2156 case VIRTIO_NET_HDR_GSO_TCPV6:
2157 if (l4_proto != IPPROTO_TCP)
2159 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2160 struct rte_tcp_hdr *,
2161 m->l2_len + m->l3_len);
2162 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2163 if (data_len < m->l2_len + m->l3_len + tcp_len)
2165 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2166 m->tso_segsz = hdr->gso_size;
2167 m->l4_len = tcp_len;
2169 case VIRTIO_NET_HDR_GSO_UDP:
2170 if (l4_proto != IPPROTO_UDP)
2172 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2173 m->tso_segsz = hdr->gso_size;
2174 m->l4_len = sizeof(struct rte_udp_hdr);
2177 VHOST_LOG_DATA(WARNING,
2178 "unsupported gso type %u.\n", hdr->gso_type);
2190 static __rte_always_inline void
2191 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2192 bool legacy_ol_flags)
2194 struct rte_net_hdr_lens hdr_lens;
2195 int l4_supported = 0;
2198 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2201 if (legacy_ol_flags) {
2202 vhost_dequeue_offload_legacy(hdr, m);
2206 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2208 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2209 m->packet_type = ptype;
2210 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2211 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2212 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2215 /* According to Virtio 1.1 spec, the device only needs to look at
2216 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2217 * This differs from the processing incoming packets path where the
2218 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2221 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2222 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2223 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2225 * 5.1.6.2.2 Device Requirements: Packet Transmission
2226 * The device MUST ignore flag bits that it does not recognize.
2228 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2231 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2232 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2233 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2235 /* Unknown proto or tunnel, do sw cksum. We can assume
2236 * the cksum field is in the first segment since the
2237 * buffers we provided to the host are large enough.
2238 * In case of SCTP, this will be wrong since it's a CRC
2239 * but there's nothing we can do.
2241 uint16_t csum = 0, off;
2243 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2244 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2246 if (likely(csum != 0xffff))
2248 off = hdr->csum_offset + hdr->csum_start;
2249 if (rte_pktmbuf_data_len(m) >= off + 1)
2250 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2254 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2255 if (hdr->gso_size == 0)
2258 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2259 case VIRTIO_NET_HDR_GSO_TCPV4:
2260 case VIRTIO_NET_HDR_GSO_TCPV6:
2261 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2263 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2264 m->tso_segsz = hdr->gso_size;
2266 case VIRTIO_NET_HDR_GSO_UDP:
2267 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2269 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2270 m->tso_segsz = hdr->gso_size;
2278 static __rte_noinline void
2279 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2280 struct buf_vector *buf_vec)
2283 uint64_t remain = sizeof(struct virtio_net_hdr);
2285 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2288 len = RTE_MIN(remain, buf_vec->buf_len);
2289 src = buf_vec->buf_addr;
2290 rte_memcpy((void *)(uintptr_t)dst,
2291 (void *)(uintptr_t)src, len);
2299 static __rte_always_inline int
2300 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2301 struct buf_vector *buf_vec, uint16_t nr_vec,
2302 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2303 bool legacy_ol_flags)
2305 uint32_t buf_avail, buf_offset;
2306 uint64_t buf_addr, buf_len;
2307 uint32_t mbuf_avail, mbuf_offset;
2309 struct rte_mbuf *cur = m, *prev = m;
2310 struct virtio_net_hdr tmp_hdr;
2311 struct virtio_net_hdr *hdr = NULL;
2312 /* A counter to avoid desc dead loop chain */
2313 uint16_t vec_idx = 0;
2314 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2317 buf_addr = buf_vec[vec_idx].buf_addr;
2318 buf_len = buf_vec[vec_idx].buf_len;
2320 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2325 if (virtio_net_with_host_offload(dev)) {
2326 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2328 * No luck, the virtio-net header doesn't fit
2329 * in a contiguous virtual area.
2331 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2334 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2339 * A virtio driver normally uses at least 2 desc buffers
2340 * for Tx: the first for storing the header, and others
2341 * for storing the data.
2343 if (unlikely(buf_len < dev->vhost_hlen)) {
2344 buf_offset = dev->vhost_hlen - buf_len;
2346 buf_addr = buf_vec[vec_idx].buf_addr;
2347 buf_len = buf_vec[vec_idx].buf_len;
2348 buf_avail = buf_len - buf_offset;
2349 } else if (buf_len == dev->vhost_hlen) {
2350 if (unlikely(++vec_idx >= nr_vec))
2352 buf_addr = buf_vec[vec_idx].buf_addr;
2353 buf_len = buf_vec[vec_idx].buf_len;
2356 buf_avail = buf_len;
2358 buf_offset = dev->vhost_hlen;
2359 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2363 (uintptr_t)(buf_addr + buf_offset),
2364 (uint32_t)buf_avail, 0);
2367 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2369 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2371 if (likely(cpy_len > MAX_BATCH_LEN ||
2372 vq->batch_copy_nb_elems >= vq->size ||
2373 (hdr && cur == m))) {
2374 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2376 (void *)((uintptr_t)(buf_addr +
2377 buf_offset)), cpy_len);
2379 batch_copy[vq->batch_copy_nb_elems].dst =
2380 rte_pktmbuf_mtod_offset(cur, void *,
2382 batch_copy[vq->batch_copy_nb_elems].src =
2383 (void *)((uintptr_t)(buf_addr + buf_offset));
2384 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2385 vq->batch_copy_nb_elems++;
2388 mbuf_avail -= cpy_len;
2389 mbuf_offset += cpy_len;
2390 buf_avail -= cpy_len;
2391 buf_offset += cpy_len;
2393 /* This buf reaches to its end, get the next one */
2394 if (buf_avail == 0) {
2395 if (++vec_idx >= nr_vec)
2398 buf_addr = buf_vec[vec_idx].buf_addr;
2399 buf_len = buf_vec[vec_idx].buf_len;
2402 buf_avail = buf_len;
2404 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2405 (uint32_t)buf_avail, 0);
2409 * This mbuf reaches to its end, get a new one
2410 * to hold more data.
2412 if (mbuf_avail == 0) {
2413 cur = rte_pktmbuf_alloc(mbuf_pool);
2414 if (unlikely(cur == NULL)) {
2415 VHOST_LOG_DATA(ERR, "Failed to "
2416 "allocate memory for mbuf.\n");
2422 prev->data_len = mbuf_offset;
2424 m->pkt_len += mbuf_offset;
2428 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2432 prev->data_len = mbuf_offset;
2433 m->pkt_len += mbuf_offset;
2436 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2444 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2450 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2452 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2453 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2458 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2459 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2461 if (unlikely(total_len > UINT16_MAX))
2464 buf_len = total_len;
2465 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2466 if (unlikely(buf == NULL))
2469 /* Initialize shinfo */
2470 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2471 virtio_dev_extbuf_free, buf);
2472 if (unlikely(shinfo == NULL)) {
2474 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2478 iova = rte_malloc_virt2iova(buf);
2479 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2480 rte_pktmbuf_reset_headroom(pkt);
2486 * Prepare a host supported pktmbuf.
2488 static __rte_always_inline int
2489 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2492 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2495 /* attach an external buffer if supported */
2496 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2499 /* check if chained buffers are allowed */
2500 if (!dev->linearbuf)
2508 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2509 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2510 bool legacy_ol_flags)
2513 uint16_t free_entries;
2514 uint16_t dropped = 0;
2515 static bool allocerr_warned;
2518 * The ordering between avail index and
2519 * desc reads needs to be enforced.
2521 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2523 if (free_entries == 0)
2526 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2528 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2530 count = RTE_MIN(count, MAX_PKT_BURST);
2531 count = RTE_MIN(count, free_entries);
2532 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2535 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2538 for (i = 0; i < count; i++) {
2539 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2542 uint16_t nr_vec = 0;
2545 if (unlikely(fill_vec_buf_split(dev, vq,
2546 vq->last_avail_idx + i,
2548 &head_idx, &buf_len,
2549 VHOST_ACCESS_RO) < 0))
2552 update_shadow_used_ring_split(vq, head_idx, 0);
2554 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2555 if (unlikely(err)) {
2557 * mbuf allocation fails for jumbo packets when external
2558 * buffer allocation is not allowed and linear buffer
2559 * is required. Drop this packet.
2561 if (!allocerr_warned) {
2563 "Failed mbuf alloc of size %d from %s on %s.\n",
2564 buf_len, mbuf_pool->name, dev->ifname);
2565 allocerr_warned = true;
2572 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2573 mbuf_pool, legacy_ol_flags);
2574 if (unlikely(err)) {
2575 if (!allocerr_warned) {
2577 "Failed to copy desc to mbuf on %s.\n",
2579 allocerr_warned = true;
2588 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2590 vq->last_avail_idx += i;
2592 do_data_copy_dequeue(vq);
2593 if (unlikely(i < count))
2594 vq->shadow_used_idx = i;
2595 if (likely(vq->shadow_used_idx)) {
2596 flush_shadow_used_ring_split(dev, vq);
2597 vhost_vring_call_split(dev, vq);
2600 return (i - dropped);
2605 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2606 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2607 struct rte_mbuf **pkts, uint16_t count)
2609 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2614 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2615 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2616 struct rte_mbuf **pkts, uint16_t count)
2618 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2621 static __rte_always_inline int
2622 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2623 struct vhost_virtqueue *vq,
2624 struct rte_mbuf **pkts,
2626 uintptr_t *desc_addrs,
2629 bool wrap = vq->avail_wrap_counter;
2630 struct vring_packed_desc *descs = vq->desc_packed;
2631 uint64_t lens[PACKED_BATCH_SIZE];
2632 uint64_t buf_lens[PACKED_BATCH_SIZE];
2633 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2636 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2638 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2641 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2642 flags = descs[avail_idx + i].flags;
2643 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2644 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2645 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2649 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2651 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2652 lens[i] = descs[avail_idx + i].len;
2654 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2655 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2656 descs[avail_idx + i].addr,
2657 &lens[i], VHOST_ACCESS_RW);
2660 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2661 if (unlikely(!desc_addrs[i]))
2663 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2667 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2668 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2672 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2673 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2675 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2676 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2680 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2681 pkts[i]->pkt_len = lens[i] - buf_offset;
2682 pkts[i]->data_len = pkts[i]->pkt_len;
2683 ids[i] = descs[avail_idx + i].id;
2692 static __rte_always_inline int
2693 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2694 struct vhost_virtqueue *vq,
2695 struct rte_mbuf **pkts,
2696 bool legacy_ol_flags)
2698 uint16_t avail_idx = vq->last_avail_idx;
2699 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2700 struct virtio_net_hdr *hdr;
2701 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2702 uint16_t ids[PACKED_BATCH_SIZE];
2705 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2709 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2710 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2712 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2713 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2714 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2717 if (virtio_net_with_host_offload(dev)) {
2718 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2719 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2720 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2724 if (virtio_net_is_inorder(dev))
2725 vhost_shadow_dequeue_batch_packed_inorder(vq,
2726 ids[PACKED_BATCH_SIZE - 1]);
2728 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2730 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2735 static __rte_always_inline int
2736 vhost_dequeue_single_packed(struct virtio_net *dev,
2737 struct vhost_virtqueue *vq,
2738 struct rte_mempool *mbuf_pool,
2739 struct rte_mbuf *pkts,
2741 uint16_t *desc_count,
2742 bool legacy_ol_flags)
2744 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2746 uint16_t nr_vec = 0;
2748 static bool allocerr_warned;
2750 if (unlikely(fill_vec_buf_packed(dev, vq,
2751 vq->last_avail_idx, desc_count,
2754 VHOST_ACCESS_RO) < 0))
2757 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2758 if (!allocerr_warned) {
2760 "Failed mbuf alloc of size %d from %s on %s.\n",
2761 buf_len, mbuf_pool->name, dev->ifname);
2762 allocerr_warned = true;
2767 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2768 mbuf_pool, legacy_ol_flags);
2769 if (unlikely(err)) {
2770 if (!allocerr_warned) {
2772 "Failed to copy desc to mbuf on %s.\n",
2774 allocerr_warned = true;
2782 static __rte_always_inline int
2783 virtio_dev_tx_single_packed(struct virtio_net *dev,
2784 struct vhost_virtqueue *vq,
2785 struct rte_mempool *mbuf_pool,
2786 struct rte_mbuf *pkts,
2787 bool legacy_ol_flags)
2790 uint16_t buf_id, desc_count = 0;
2793 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2794 &desc_count, legacy_ol_flags);
2796 if (likely(desc_count > 0)) {
2797 if (virtio_net_is_inorder(dev))
2798 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2801 vhost_shadow_dequeue_single_packed(vq, buf_id,
2804 vq_inc_last_avail_packed(vq, desc_count);
2812 virtio_dev_tx_packed(struct virtio_net *dev,
2813 struct vhost_virtqueue *__rte_restrict vq,
2814 struct rte_mempool *mbuf_pool,
2815 struct rte_mbuf **__rte_restrict pkts,
2817 bool legacy_ol_flags)
2819 uint32_t pkt_idx = 0;
2821 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2825 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2827 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2828 if (!virtio_dev_tx_batch_packed(dev, vq,
2831 pkt_idx += PACKED_BATCH_SIZE;
2836 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2841 } while (pkt_idx < count);
2843 if (pkt_idx != count)
2844 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
2846 if (vq->shadow_used_idx) {
2847 do_data_copy_dequeue(vq);
2849 vhost_flush_dequeue_shadow_packed(dev, vq);
2850 vhost_vring_call_packed(dev, vq);
2858 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
2859 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2860 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2862 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
2867 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
2868 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2869 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2871 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
2875 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2876 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2878 struct virtio_net *dev;
2879 struct rte_mbuf *rarp_mbuf = NULL;
2880 struct vhost_virtqueue *vq;
2881 int16_t success = 1;
2883 dev = get_device(vid);
2887 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2889 "(%d) %s: built-in vhost net backend is disabled.\n",
2890 dev->vid, __func__);
2894 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2896 "(%d) %s: invalid virtqueue idx %d.\n",
2897 dev->vid, __func__, queue_id);
2901 vq = dev->virtqueue[queue_id];
2903 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2906 if (unlikely(!vq->enabled)) {
2908 goto out_access_unlock;
2911 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2912 vhost_user_iotlb_rd_lock(vq);
2914 if (unlikely(!vq->access_ok))
2915 if (unlikely(vring_translate(dev, vq) < 0)) {
2921 * Construct a RARP broadcast packet, and inject it to the "pkts"
2922 * array, to looks like that guest actually send such packet.
2924 * Check user_send_rarp() for more information.
2926 * broadcast_rarp shares a cacheline in the virtio_net structure
2927 * with some fields that are accessed during enqueue and
2928 * __atomic_compare_exchange_n causes a write if performed compare
2929 * and exchange. This could result in false sharing between enqueue
2932 * Prevent unnecessary false sharing by reading broadcast_rarp first
2933 * and only performing compare and exchange if the read indicates it
2934 * is likely to be set.
2936 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2937 __atomic_compare_exchange_n(&dev->broadcast_rarp,
2938 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2940 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2941 if (rarp_mbuf == NULL) {
2942 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2947 * Inject it to the head of "pkts" array, so that switch's mac
2948 * learning table will get updated first.
2950 pkts[0] = rarp_mbuf;
2955 if (vq_is_packed(dev)) {
2956 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
2957 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
2959 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
2961 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
2962 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
2964 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
2968 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2969 vhost_user_iotlb_rd_unlock(vq);
2972 rte_spinlock_unlock(&vq->access_lock);
2974 if (unlikely(rarp_mbuf != NULL))