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 #define VHOST_ASYNC_BATCH_THRESHOLD 32
30 static __rte_always_inline bool
31 rxvq_is_mergeable(struct virtio_net *dev)
33 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
36 static __rte_always_inline bool
37 virtio_net_is_inorder(struct virtio_net *dev)
39 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
43 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
45 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
49 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
51 struct batch_copy_elem *elem = vq->batch_copy_elems;
52 uint16_t count = vq->batch_copy_nb_elems;
55 for (i = 0; i < count; i++) {
56 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
57 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
59 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
62 vq->batch_copy_nb_elems = 0;
66 do_data_copy_dequeue(struct vhost_virtqueue *vq)
68 struct batch_copy_elem *elem = vq->batch_copy_elems;
69 uint16_t count = vq->batch_copy_nb_elems;
72 for (i = 0; i < count; i++)
73 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
75 vq->batch_copy_nb_elems = 0;
78 static __rte_always_inline void
79 do_flush_shadow_used_ring_split(struct virtio_net *dev,
80 struct vhost_virtqueue *vq,
81 uint16_t to, uint16_t from, uint16_t size)
83 rte_memcpy(&vq->used->ring[to],
84 &vq->shadow_used_split[from],
85 size * sizeof(struct vring_used_elem));
86 vhost_log_cache_used_vring(dev, vq,
87 offsetof(struct vring_used, ring[to]),
88 size * sizeof(struct vring_used_elem));
91 static __rte_always_inline void
92 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
94 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
96 if (used_idx + vq->shadow_used_idx <= vq->size) {
97 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
102 /* update used ring interval [used_idx, vq->size] */
103 size = vq->size - used_idx;
104 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
106 /* update the left half used ring interval [0, left_size] */
107 do_flush_shadow_used_ring_split(dev, vq, 0, size,
108 vq->shadow_used_idx - size);
110 vq->last_used_idx += vq->shadow_used_idx;
112 vhost_log_cache_sync(dev, vq);
114 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
116 vq->shadow_used_idx = 0;
117 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
118 sizeof(vq->used->idx));
121 static __rte_always_inline void
122 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
123 uint16_t desc_idx, uint32_t len)
125 uint16_t i = vq->shadow_used_idx++;
127 vq->shadow_used_split[i].id = desc_idx;
128 vq->shadow_used_split[i].len = len;
131 static __rte_always_inline void
132 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
133 struct vhost_virtqueue *vq)
136 uint16_t used_idx = vq->last_used_idx;
137 uint16_t head_idx = vq->last_used_idx;
138 uint16_t head_flags = 0;
140 /* Split loop in two to save memory barriers */
141 for (i = 0; i < vq->shadow_used_idx; i++) {
142 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
143 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
145 used_idx += vq->shadow_used_packed[i].count;
146 if (used_idx >= vq->size)
147 used_idx -= vq->size;
150 /* The ordering for storing desc flags needs to be enforced. */
151 rte_atomic_thread_fence(__ATOMIC_RELEASE);
153 for (i = 0; i < vq->shadow_used_idx; i++) {
156 if (vq->shadow_used_packed[i].len)
157 flags = VRING_DESC_F_WRITE;
161 if (vq->used_wrap_counter) {
162 flags |= VRING_DESC_F_USED;
163 flags |= VRING_DESC_F_AVAIL;
165 flags &= ~VRING_DESC_F_USED;
166 flags &= ~VRING_DESC_F_AVAIL;
170 vq->desc_packed[vq->last_used_idx].flags = flags;
172 vhost_log_cache_used_vring(dev, vq,
174 sizeof(struct vring_packed_desc),
175 sizeof(struct vring_packed_desc));
177 head_idx = vq->last_used_idx;
181 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
184 vq->desc_packed[head_idx].flags = head_flags;
186 vhost_log_cache_used_vring(dev, vq,
188 sizeof(struct vring_packed_desc),
189 sizeof(struct vring_packed_desc));
191 vq->shadow_used_idx = 0;
192 vhost_log_cache_sync(dev, vq);
195 static __rte_always_inline void
196 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
197 struct vhost_virtqueue *vq)
199 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
201 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
202 /* desc flags is the synchronization point for virtio packed vring */
203 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
204 used_elem->flags, __ATOMIC_RELEASE);
206 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
207 sizeof(struct vring_packed_desc),
208 sizeof(struct vring_packed_desc));
209 vq->shadow_used_idx = 0;
210 vhost_log_cache_sync(dev, vq);
213 static __rte_always_inline void
214 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
215 struct vhost_virtqueue *vq,
221 uint16_t last_used_idx = vq->last_used_idx;
222 struct vring_packed_desc *desc_base = &vq->desc_packed[last_used_idx];
224 if (vq->shadow_used_idx) {
225 do_data_copy_enqueue(dev, vq);
226 vhost_flush_enqueue_shadow_packed(dev, vq);
229 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
231 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
232 desc_base[i].id = ids[i];
233 desc_base[i].len = lens[i];
236 rte_atomic_thread_fence(__ATOMIC_RELEASE);
238 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
239 desc_base[i].flags = flags;
242 vhost_log_cache_used_vring(dev, vq, last_used_idx *
243 sizeof(struct vring_packed_desc),
244 sizeof(struct vring_packed_desc) *
246 vhost_log_cache_sync(dev, vq);
248 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
251 static __rte_always_inline void
252 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
255 vq->shadow_used_packed[0].id = id;
257 if (!vq->shadow_used_idx) {
258 vq->shadow_last_used_idx = vq->last_used_idx;
259 vq->shadow_used_packed[0].flags =
260 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
261 vq->shadow_used_packed[0].len = 0;
262 vq->shadow_used_packed[0].count = 1;
263 vq->shadow_used_idx++;
266 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
269 static __rte_always_inline void
270 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
271 struct vhost_virtqueue *vq,
278 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
280 if (!vq->shadow_used_idx) {
281 vq->shadow_last_used_idx = vq->last_used_idx;
282 vq->shadow_used_packed[0].id = ids[0];
283 vq->shadow_used_packed[0].len = 0;
284 vq->shadow_used_packed[0].count = 1;
285 vq->shadow_used_packed[0].flags = flags;
286 vq->shadow_used_idx++;
291 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
292 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
293 vq->desc_packed[vq->last_used_idx + i].len = 0;
296 rte_atomic_thread_fence(__ATOMIC_RELEASE);
297 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
298 vq->desc_packed[vq->last_used_idx + i].flags = flags;
300 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
301 sizeof(struct vring_packed_desc),
302 sizeof(struct vring_packed_desc) *
304 vhost_log_cache_sync(dev, vq);
306 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
309 static __rte_always_inline void
310 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
316 flags = vq->desc_packed[vq->last_used_idx].flags;
317 if (vq->used_wrap_counter) {
318 flags |= VRING_DESC_F_USED;
319 flags |= VRING_DESC_F_AVAIL;
321 flags &= ~VRING_DESC_F_USED;
322 flags &= ~VRING_DESC_F_AVAIL;
325 if (!vq->shadow_used_idx) {
326 vq->shadow_last_used_idx = vq->last_used_idx;
328 vq->shadow_used_packed[0].id = buf_id;
329 vq->shadow_used_packed[0].len = 0;
330 vq->shadow_used_packed[0].flags = flags;
331 vq->shadow_used_idx++;
333 vq->desc_packed[vq->last_used_idx].id = buf_id;
334 vq->desc_packed[vq->last_used_idx].len = 0;
335 vq->desc_packed[vq->last_used_idx].flags = flags;
338 vq_inc_last_used_packed(vq, count);
341 static __rte_always_inline void
342 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
348 vq->shadow_used_packed[0].id = buf_id;
350 flags = vq->desc_packed[vq->last_used_idx].flags;
351 if (vq->used_wrap_counter) {
352 flags |= VRING_DESC_F_USED;
353 flags |= VRING_DESC_F_AVAIL;
355 flags &= ~VRING_DESC_F_USED;
356 flags &= ~VRING_DESC_F_AVAIL;
359 if (!vq->shadow_used_idx) {
360 vq->shadow_last_used_idx = vq->last_used_idx;
361 vq->shadow_used_packed[0].len = 0;
362 vq->shadow_used_packed[0].flags = flags;
363 vq->shadow_used_idx++;
366 vq_inc_last_used_packed(vq, count);
369 static __rte_always_inline void
370 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
374 uint16_t num_buffers)
378 for (i = 0; i < num_buffers; i++) {
379 /* enqueue shadow flush action aligned with batch num */
380 if (!vq->shadow_used_idx)
381 vq->shadow_aligned_idx = vq->last_used_idx &
383 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
384 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
385 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
386 vq->shadow_aligned_idx += count[i];
387 vq->shadow_used_idx++;
391 static __rte_always_inline void
392 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
393 struct vhost_virtqueue *vq,
397 uint16_t num_buffers)
399 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
401 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
402 do_data_copy_enqueue(dev, vq);
403 vhost_flush_enqueue_shadow_packed(dev, vq);
407 /* avoid write operation when necessary, to lessen cache issues */
408 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
409 if ((var) != (val)) \
413 static __rte_always_inline void
414 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
416 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
418 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
419 csum_l4 |= PKT_TX_TCP_CKSUM;
422 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
423 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
426 case PKT_TX_TCP_CKSUM:
427 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
430 case PKT_TX_UDP_CKSUM:
431 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
434 case PKT_TX_SCTP_CKSUM:
435 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
440 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
441 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
442 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
445 /* IP cksum verification cannot be bypassed, then calculate here */
446 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
447 struct rte_ipv4_hdr *ipv4_hdr;
449 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
451 ipv4_hdr->hdr_checksum = 0;
452 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
455 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
456 if (m_buf->ol_flags & PKT_TX_IPV4)
457 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
459 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
460 net_hdr->gso_size = m_buf->tso_segsz;
461 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
463 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
464 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
465 net_hdr->gso_size = m_buf->tso_segsz;
466 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
469 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
470 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
471 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
475 static __rte_always_inline int
476 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
477 struct buf_vector *buf_vec, uint16_t *vec_idx,
478 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
480 uint16_t vec_id = *vec_idx;
484 uint64_t desc_chunck_len = desc_len;
486 if (unlikely(vec_id >= BUF_VECTOR_MAX))
489 desc_addr = vhost_iova_to_vva(dev, vq,
493 if (unlikely(!desc_addr))
496 rte_prefetch0((void *)(uintptr_t)desc_addr);
498 buf_vec[vec_id].buf_iova = desc_iova;
499 buf_vec[vec_id].buf_addr = desc_addr;
500 buf_vec[vec_id].buf_len = desc_chunck_len;
502 desc_len -= desc_chunck_len;
503 desc_iova += desc_chunck_len;
511 static __rte_always_inline int
512 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
513 uint32_t avail_idx, uint16_t *vec_idx,
514 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
515 uint32_t *desc_chain_len, uint8_t perm)
517 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
518 uint16_t vec_id = *vec_idx;
521 uint32_t nr_descs = vq->size;
523 struct vring_desc *descs = vq->desc;
524 struct vring_desc *idesc = NULL;
526 if (unlikely(idx >= vq->size))
529 *desc_chain_head = idx;
531 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
532 dlen = vq->desc[idx].len;
533 nr_descs = dlen / sizeof(struct vring_desc);
534 if (unlikely(nr_descs > vq->size))
537 descs = (struct vring_desc *)(uintptr_t)
538 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
541 if (unlikely(!descs))
544 if (unlikely(dlen < vq->desc[idx].len)) {
546 * The indirect desc table is not contiguous
547 * in process VA space, we have to copy it.
549 idesc = vhost_alloc_copy_ind_table(dev, vq,
550 vq->desc[idx].addr, vq->desc[idx].len);
551 if (unlikely(!idesc))
561 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
562 free_ind_table(idesc);
566 dlen = descs[idx].len;
569 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
570 descs[idx].addr, dlen,
572 free_ind_table(idesc);
576 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
579 idx = descs[idx].next;
582 *desc_chain_len = len;
585 if (unlikely(!!idesc))
586 free_ind_table(idesc);
592 * Returns -1 on fail, 0 on success
595 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
596 uint32_t size, struct buf_vector *buf_vec,
597 uint16_t *num_buffers, uint16_t avail_head,
601 uint16_t vec_idx = 0;
602 uint16_t max_tries, tries = 0;
604 uint16_t head_idx = 0;
608 cur_idx = vq->last_avail_idx;
610 if (rxvq_is_mergeable(dev))
611 max_tries = vq->size - 1;
616 if (unlikely(cur_idx == avail_head))
619 * if we tried all available ring items, and still
620 * can't get enough buf, it means something abnormal
623 if (unlikely(++tries > max_tries))
626 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
629 VHOST_ACCESS_RW) < 0))
631 len = RTE_MIN(len, size);
632 update_shadow_used_ring_split(vq, head_idx, len);
644 static __rte_always_inline int
645 fill_vec_buf_packed_indirect(struct virtio_net *dev,
646 struct vhost_virtqueue *vq,
647 struct vring_packed_desc *desc, uint16_t *vec_idx,
648 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
652 uint16_t vec_id = *vec_idx;
654 struct vring_packed_desc *descs, *idescs = NULL;
657 descs = (struct vring_packed_desc *)(uintptr_t)
658 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
659 if (unlikely(!descs))
662 if (unlikely(dlen < desc->len)) {
664 * The indirect desc table is not contiguous
665 * in process VA space, we have to copy it.
667 idescs = vhost_alloc_copy_ind_table(dev,
668 vq, desc->addr, desc->len);
669 if (unlikely(!idescs))
675 nr_descs = desc->len / sizeof(struct vring_packed_desc);
676 if (unlikely(nr_descs >= vq->size)) {
677 free_ind_table(idescs);
681 for (i = 0; i < nr_descs; i++) {
682 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
683 free_ind_table(idescs);
689 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
696 if (unlikely(!!idescs))
697 free_ind_table(idescs);
702 static __rte_always_inline int
703 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
704 uint16_t avail_idx, uint16_t *desc_count,
705 struct buf_vector *buf_vec, uint16_t *vec_idx,
706 uint16_t *buf_id, uint32_t *len, uint8_t perm)
708 bool wrap_counter = vq->avail_wrap_counter;
709 struct vring_packed_desc *descs = vq->desc_packed;
710 uint16_t vec_id = *vec_idx;
713 if (avail_idx < vq->last_avail_idx)
717 * Perform a load-acquire barrier in desc_is_avail to
718 * enforce the ordering between desc flags and desc
721 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
728 if (unlikely(vec_id >= BUF_VECTOR_MAX))
731 if (unlikely(*desc_count >= vq->size))
735 *buf_id = descs[avail_idx].id;
737 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
738 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
744 dlen = descs[avail_idx].len;
747 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
748 descs[avail_idx].addr,
754 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
757 if (++avail_idx >= vq->size) {
758 avail_idx -= vq->size;
768 static __rte_noinline void
769 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
770 struct buf_vector *buf_vec,
771 struct virtio_net_hdr_mrg_rxbuf *hdr)
774 uint64_t remain = dev->vhost_hlen;
775 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
776 uint64_t iova = buf_vec->buf_iova;
779 len = RTE_MIN(remain,
781 dst = buf_vec->buf_addr;
782 rte_memcpy((void *)(uintptr_t)dst,
783 (void *)(uintptr_t)src,
786 PRINT_PACKET(dev, (uintptr_t)dst,
788 vhost_log_cache_write_iova(dev, vq,
798 static __rte_always_inline int
799 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
800 struct rte_mbuf *m, struct buf_vector *buf_vec,
801 uint16_t nr_vec, uint16_t num_buffers)
803 uint32_t vec_idx = 0;
804 uint32_t mbuf_offset, mbuf_avail;
805 uint32_t buf_offset, buf_avail;
806 uint64_t buf_addr, buf_iova, buf_len;
809 struct rte_mbuf *hdr_mbuf;
810 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
811 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
814 if (unlikely(m == NULL)) {
819 buf_addr = buf_vec[vec_idx].buf_addr;
820 buf_iova = buf_vec[vec_idx].buf_iova;
821 buf_len = buf_vec[vec_idx].buf_len;
823 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
830 if (unlikely(buf_len < dev->vhost_hlen)) {
831 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
834 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
836 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
837 dev->vid, num_buffers);
839 if (unlikely(buf_len < dev->vhost_hlen)) {
840 buf_offset = dev->vhost_hlen - buf_len;
842 buf_addr = buf_vec[vec_idx].buf_addr;
843 buf_iova = buf_vec[vec_idx].buf_iova;
844 buf_len = buf_vec[vec_idx].buf_len;
845 buf_avail = buf_len - buf_offset;
847 buf_offset = dev->vhost_hlen;
848 buf_avail = buf_len - dev->vhost_hlen;
851 mbuf_avail = rte_pktmbuf_data_len(m);
853 while (mbuf_avail != 0 || m->next != NULL) {
854 /* done with current buf, get the next one */
855 if (buf_avail == 0) {
857 if (unlikely(vec_idx >= nr_vec)) {
862 buf_addr = buf_vec[vec_idx].buf_addr;
863 buf_iova = buf_vec[vec_idx].buf_iova;
864 buf_len = buf_vec[vec_idx].buf_len;
870 /* done with current mbuf, get the next one */
871 if (mbuf_avail == 0) {
875 mbuf_avail = rte_pktmbuf_data_len(m);
879 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
880 if (rxvq_is_mergeable(dev))
881 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
884 if (unlikely(hdr == &tmp_hdr)) {
885 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
887 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
889 vhost_log_cache_write_iova(dev, vq,
897 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
899 if (likely(cpy_len > MAX_BATCH_LEN ||
900 vq->batch_copy_nb_elems >= vq->size)) {
901 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
902 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
904 vhost_log_cache_write_iova(dev, vq,
905 buf_iova + buf_offset,
907 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
910 batch_copy[vq->batch_copy_nb_elems].dst =
911 (void *)((uintptr_t)(buf_addr + buf_offset));
912 batch_copy[vq->batch_copy_nb_elems].src =
913 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
914 batch_copy[vq->batch_copy_nb_elems].log_addr =
915 buf_iova + buf_offset;
916 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
917 vq->batch_copy_nb_elems++;
920 mbuf_avail -= cpy_len;
921 mbuf_offset += cpy_len;
922 buf_avail -= cpy_len;
923 buf_offset += cpy_len;
931 static __rte_always_inline void
932 async_fill_vec(struct iovec *v, void *base, size_t len)
938 static __rte_always_inline void
939 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
940 struct iovec *vec, unsigned long nr_seg)
947 it->nr_segs = nr_seg;
954 static __rte_always_inline void
955 async_fill_desc(struct rte_vhost_async_desc *desc,
956 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
962 static __rte_always_inline int
963 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
964 struct rte_mbuf *m, struct buf_vector *buf_vec,
965 uint16_t nr_vec, uint16_t num_buffers,
966 struct iovec *src_iovec, struct iovec *dst_iovec,
967 struct rte_vhost_iov_iter *src_it,
968 struct rte_vhost_iov_iter *dst_it)
970 uint32_t vec_idx = 0;
971 uint32_t mbuf_offset, mbuf_avail;
972 uint32_t buf_offset, buf_avail;
973 uint64_t buf_addr, buf_iova, buf_len;
974 uint32_t cpy_len, cpy_threshold;
976 struct rte_mbuf *hdr_mbuf;
977 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
978 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
986 if (unlikely(m == NULL)) {
991 cpy_threshold = vq->async_threshold;
993 buf_addr = buf_vec[vec_idx].buf_addr;
994 buf_iova = buf_vec[vec_idx].buf_iova;
995 buf_len = buf_vec[vec_idx].buf_len;
997 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1003 hdr_addr = buf_addr;
1004 if (unlikely(buf_len < dev->vhost_hlen)) {
1005 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1008 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1010 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1011 dev->vid, num_buffers);
1013 if (unlikely(buf_len < dev->vhost_hlen)) {
1014 buf_offset = dev->vhost_hlen - buf_len;
1016 buf_addr = buf_vec[vec_idx].buf_addr;
1017 buf_iova = buf_vec[vec_idx].buf_iova;
1018 buf_len = buf_vec[vec_idx].buf_len;
1019 buf_avail = buf_len - buf_offset;
1021 buf_offset = dev->vhost_hlen;
1022 buf_avail = buf_len - dev->vhost_hlen;
1025 mbuf_avail = rte_pktmbuf_data_len(m);
1028 while (mbuf_avail != 0 || m->next != NULL) {
1029 /* done with current buf, get the next one */
1030 if (buf_avail == 0) {
1032 if (unlikely(vec_idx >= nr_vec)) {
1037 buf_addr = buf_vec[vec_idx].buf_addr;
1038 buf_iova = buf_vec[vec_idx].buf_iova;
1039 buf_len = buf_vec[vec_idx].buf_len;
1042 buf_avail = buf_len;
1045 /* done with current mbuf, get the next one */
1046 if (mbuf_avail == 0) {
1050 mbuf_avail = rte_pktmbuf_data_len(m);
1054 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1055 if (rxvq_is_mergeable(dev))
1056 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1059 if (unlikely(hdr == &tmp_hdr)) {
1060 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1062 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1063 dev->vhost_hlen, 0);
1064 vhost_log_cache_write_iova(dev, vq,
1065 buf_vec[0].buf_iova,
1072 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1074 while (unlikely(cpy_len && cpy_len >= cpy_threshold)) {
1075 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1076 buf_iova + buf_offset,
1077 cpy_len, &mapped_len);
1079 if (unlikely(!hpa || mapped_len < cpy_threshold))
1082 async_fill_vec(src_iovec + tvec_idx,
1083 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1084 mbuf_offset), (size_t)mapped_len);
1086 async_fill_vec(dst_iovec + tvec_idx,
1087 hpa, (size_t)mapped_len);
1089 tlen += (uint32_t)mapped_len;
1090 cpy_len -= (uint32_t)mapped_len;
1091 mbuf_avail -= (uint32_t)mapped_len;
1092 mbuf_offset += (uint32_t)mapped_len;
1093 buf_avail -= (uint32_t)mapped_len;
1094 buf_offset += (uint32_t)mapped_len;
1098 if (likely(cpy_len)) {
1099 if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1101 (void *)((uintptr_t)(buf_addr + buf_offset)),
1102 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1106 (uintptr_t)(buf_addr + buf_offset),
1109 batch_copy[vq->batch_copy_nb_elems].dst =
1110 (void *)((uintptr_t)(buf_addr + buf_offset));
1111 batch_copy[vq->batch_copy_nb_elems].src =
1112 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1113 batch_copy[vq->batch_copy_nb_elems].log_addr =
1114 buf_iova + buf_offset;
1115 batch_copy[vq->batch_copy_nb_elems].len =
1117 vq->batch_copy_nb_elems++;
1120 mbuf_avail -= cpy_len;
1121 mbuf_offset += cpy_len;
1122 buf_avail -= cpy_len;
1123 buf_offset += cpy_len;
1130 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1131 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1139 static __rte_always_inline int
1140 vhost_enqueue_single_packed(struct virtio_net *dev,
1141 struct vhost_virtqueue *vq,
1142 struct rte_mbuf *pkt,
1143 struct buf_vector *buf_vec,
1146 uint16_t nr_vec = 0;
1147 uint16_t avail_idx = vq->last_avail_idx;
1148 uint16_t max_tries, tries = 0;
1149 uint16_t buf_id = 0;
1151 uint16_t desc_count;
1152 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1153 uint16_t num_buffers = 0;
1154 uint32_t buffer_len[vq->size];
1155 uint16_t buffer_buf_id[vq->size];
1156 uint16_t buffer_desc_count[vq->size];
1158 if (rxvq_is_mergeable(dev))
1159 max_tries = vq->size - 1;
1165 * if we tried all available ring items, and still
1166 * can't get enough buf, it means something abnormal
1169 if (unlikely(++tries > max_tries))
1172 if (unlikely(fill_vec_buf_packed(dev, vq,
1173 avail_idx, &desc_count,
1176 VHOST_ACCESS_RW) < 0))
1179 len = RTE_MIN(len, size);
1182 buffer_len[num_buffers] = len;
1183 buffer_buf_id[num_buffers] = buf_id;
1184 buffer_desc_count[num_buffers] = desc_count;
1187 *nr_descs += desc_count;
1188 avail_idx += desc_count;
1189 if (avail_idx >= vq->size)
1190 avail_idx -= vq->size;
1193 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1196 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1197 buffer_desc_count, num_buffers);
1202 static __rte_noinline uint32_t
1203 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1204 struct rte_mbuf **pkts, uint32_t count)
1206 uint32_t pkt_idx = 0;
1207 uint16_t num_buffers;
1208 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1209 uint16_t avail_head;
1212 * The ordering between avail index and
1213 * desc reads needs to be enforced.
1215 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1217 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1219 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1220 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1221 uint16_t nr_vec = 0;
1223 if (unlikely(reserve_avail_buf_split(dev, vq,
1224 pkt_len, buf_vec, &num_buffers,
1225 avail_head, &nr_vec) < 0)) {
1226 VHOST_LOG_DATA(DEBUG,
1227 "(%d) failed to get enough desc from vring\n",
1229 vq->shadow_used_idx -= num_buffers;
1233 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1234 dev->vid, vq->last_avail_idx,
1235 vq->last_avail_idx + num_buffers);
1237 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1240 vq->shadow_used_idx -= num_buffers;
1244 vq->last_avail_idx += num_buffers;
1247 do_data_copy_enqueue(dev, vq);
1249 if (likely(vq->shadow_used_idx)) {
1250 flush_shadow_used_ring_split(dev, vq);
1251 vhost_vring_call_split(dev, vq);
1257 static __rte_always_inline int
1258 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1259 struct vhost_virtqueue *vq,
1260 struct rte_mbuf **pkts)
1262 bool wrap_counter = vq->avail_wrap_counter;
1263 struct vring_packed_desc *descs = vq->desc_packed;
1264 uint16_t avail_idx = vq->last_avail_idx;
1265 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1266 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1267 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1268 uint64_t lens[PACKED_BATCH_SIZE];
1269 uint16_t ids[PACKED_BATCH_SIZE];
1272 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1275 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1278 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1279 if (unlikely(pkts[i]->next != NULL))
1281 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1286 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1287 lens[i] = descs[avail_idx + i].len;
1289 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1290 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1294 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1295 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1296 descs[avail_idx + i].addr,
1300 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1301 if (unlikely(!desc_addrs[i]))
1303 if (unlikely(lens[i] != descs[avail_idx + i].len))
1307 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1308 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1309 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1310 (uintptr_t)desc_addrs[i];
1311 lens[i] = pkts[i]->pkt_len +
1312 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1315 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1316 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1318 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1320 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1321 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1322 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1326 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1327 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1330 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1331 ids[i] = descs[avail_idx + i].id;
1333 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1338 static __rte_always_inline int16_t
1339 virtio_dev_rx_single_packed(struct virtio_net *dev,
1340 struct vhost_virtqueue *vq,
1341 struct rte_mbuf *pkt)
1343 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1344 uint16_t nr_descs = 0;
1346 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1348 VHOST_LOG_DATA(DEBUG,
1349 "(%d) failed to get enough desc from vring\n",
1354 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1355 dev->vid, vq->last_avail_idx,
1356 vq->last_avail_idx + nr_descs);
1358 vq_inc_last_avail_packed(vq, nr_descs);
1363 static __rte_noinline uint32_t
1364 virtio_dev_rx_packed(struct virtio_net *dev,
1365 struct vhost_virtqueue *__rte_restrict vq,
1366 struct rte_mbuf **__rte_restrict pkts,
1369 uint32_t pkt_idx = 0;
1372 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1374 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1375 if (!virtio_dev_rx_batch_packed(dev, vq,
1377 pkt_idx += PACKED_BATCH_SIZE;
1382 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1386 } while (pkt_idx < count);
1388 if (vq->shadow_used_idx) {
1389 do_data_copy_enqueue(dev, vq);
1390 vhost_flush_enqueue_shadow_packed(dev, vq);
1394 vhost_vring_call_packed(dev, vq);
1399 static __rte_always_inline uint32_t
1400 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1401 struct rte_mbuf **pkts, uint32_t count)
1403 struct vhost_virtqueue *vq;
1406 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1407 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1408 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1409 dev->vid, __func__, queue_id);
1413 vq = dev->virtqueue[queue_id];
1415 rte_spinlock_lock(&vq->access_lock);
1417 if (unlikely(!vq->enabled))
1418 goto out_access_unlock;
1420 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1421 vhost_user_iotlb_rd_lock(vq);
1423 if (unlikely(!vq->access_ok))
1424 if (unlikely(vring_translate(dev, vq) < 0))
1427 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1431 if (vq_is_packed(dev))
1432 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1434 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1437 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1438 vhost_user_iotlb_rd_unlock(vq);
1441 rte_spinlock_unlock(&vq->access_lock);
1447 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1448 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1450 struct virtio_net *dev = get_device(vid);
1455 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1457 "(%d) %s: built-in vhost net backend is disabled.\n",
1458 dev->vid, __func__);
1462 return virtio_dev_rx(dev, queue_id, pkts, count);
1465 static __rte_always_inline uint16_t
1466 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1467 uint16_t vq_size, uint16_t n_inflight)
1469 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1470 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1473 static __rte_always_inline void
1474 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1475 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1477 size_t elem_size = sizeof(struct vring_used_elem);
1479 if (d_idx + count <= ring_size) {
1480 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1482 uint16_t size = ring_size - d_idx;
1484 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1485 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1489 static __rte_always_inline void
1490 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1491 struct vring_used_elem_packed *d_ring,
1492 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1494 size_t elem_size = sizeof(struct vring_used_elem_packed);
1496 if (d_idx + count <= ring_size) {
1497 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1499 uint16_t size = ring_size - d_idx;
1501 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1502 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1506 static __rte_noinline uint32_t
1507 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1508 struct vhost_virtqueue *vq, uint16_t queue_id,
1509 struct rte_mbuf **pkts, uint32_t count,
1510 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
1512 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1513 uint16_t num_buffers;
1514 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1515 uint16_t avail_head;
1517 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1518 struct iovec *vec_pool = vq->vec_pool;
1519 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1520 struct iovec *src_iovec = vec_pool;
1521 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1522 uint16_t slot_idx = 0;
1523 uint16_t segs_await = 0;
1524 uint16_t iovec_idx = 0, it_idx = 0;
1525 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1526 uint32_t n_pkts = 0, pkt_err = 0;
1527 uint32_t num_async_pkts = 0, num_done_pkts = 0;
1530 uint16_t last_avail_idx;
1531 } async_pkts_log[MAX_PKT_BURST];
1534 * The ordering between avail index and desc reads need to be enforced.
1536 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1538 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1540 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1541 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1542 uint16_t nr_vec = 0;
1544 if (unlikely(reserve_avail_buf_split(dev, vq,
1545 pkt_len, buf_vec, &num_buffers,
1546 avail_head, &nr_vec) < 0)) {
1547 VHOST_LOG_DATA(DEBUG,
1548 "(%d) failed to get enough desc from vring\n",
1550 vq->shadow_used_idx -= num_buffers;
1554 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1555 dev->vid, vq->last_avail_idx,
1556 vq->last_avail_idx + num_buffers);
1558 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers,
1559 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1560 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0) {
1561 vq->shadow_used_idx -= num_buffers;
1565 slot_idx = (vq->async_pkts_idx + num_async_pkts) &
1567 if (it_pool[it_idx].count) {
1570 async_fill_desc(&tdes[pkt_burst_idx++],
1571 &it_pool[it_idx], &it_pool[it_idx + 1]);
1572 pkts_info[slot_idx].descs = num_buffers;
1573 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1574 async_pkts_log[num_async_pkts].pkt_idx = pkt_idx;
1575 async_pkts_log[num_async_pkts++].last_avail_idx =
1578 iovec_idx += it_pool[it_idx].nr_segs;
1581 segs_await += it_pool[it_idx].nr_segs;
1584 * recover shadow used ring and keep DMA-occupied
1587 from = vq->shadow_used_idx - num_buffers;
1588 to = vq->async_desc_idx_split & (vq->size - 1);
1590 store_dma_desc_info_split(vq->shadow_used_split,
1591 vq->async_descs_split, vq->size, from, to, num_buffers);
1593 vq->async_desc_idx_split += num_buffers;
1594 vq->shadow_used_idx -= num_buffers;
1596 comp_pkts[num_done_pkts++] = pkts[pkt_idx];
1598 vq->last_avail_idx += num_buffers;
1601 * conditions to trigger async device transfer:
1602 * - buffered packet number reaches transfer threshold
1603 * - unused async iov number is less than max vhost vector
1605 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1606 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await <
1608 n_pkts = vq->async_ops.transfer_data(dev->vid,
1609 queue_id, tdes, 0, pkt_burst_idx);
1614 vq->async_pkts_inflight_n += n_pkts;
1616 if (unlikely(n_pkts < pkt_burst_idx)) {
1618 * log error packets number here and do actual
1619 * error processing when applications poll
1622 pkt_err = pkt_burst_idx - n_pkts;
1631 if (pkt_burst_idx) {
1632 n_pkts = vq->async_ops.transfer_data(dev->vid,
1633 queue_id, tdes, 0, pkt_burst_idx);
1634 vq->async_pkts_inflight_n += n_pkts;
1636 if (unlikely(n_pkts < pkt_burst_idx))
1637 pkt_err = pkt_burst_idx - n_pkts;
1640 do_data_copy_enqueue(dev, vq);
1642 if (unlikely(pkt_err)) {
1643 uint16_t num_descs = 0;
1645 num_async_pkts -= pkt_err;
1646 /* calculate the sum of descriptors of DMA-error packets. */
1647 while (pkt_err-- > 0) {
1648 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1651 vq->async_desc_idx_split -= num_descs;
1652 /* recover shadow used ring and available ring */
1653 vq->shadow_used_idx -= (vq->last_avail_idx -
1654 async_pkts_log[num_async_pkts].last_avail_idx -
1656 vq->last_avail_idx =
1657 async_pkts_log[num_async_pkts].last_avail_idx;
1658 pkt_idx = async_pkts_log[num_async_pkts].pkt_idx;
1659 num_done_pkts = pkt_idx - num_async_pkts;
1662 vq->async_pkts_idx += num_async_pkts;
1663 *comp_count = num_done_pkts;
1665 if (likely(vq->shadow_used_idx)) {
1666 flush_shadow_used_ring_split(dev, vq);
1667 vhost_vring_call_split(dev, vq);
1673 static __rte_always_inline void
1674 vhost_update_used_packed(struct vhost_virtqueue *vq,
1675 struct vring_used_elem_packed *shadow_ring,
1679 uint16_t used_idx = vq->last_used_idx;
1680 uint16_t head_idx = vq->last_used_idx;
1681 uint16_t head_flags = 0;
1686 /* Split loop in two to save memory barriers */
1687 for (i = 0; i < count; i++) {
1688 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1689 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1691 used_idx += shadow_ring[i].count;
1692 if (used_idx >= vq->size)
1693 used_idx -= vq->size;
1696 /* The ordering for storing desc flags needs to be enforced. */
1697 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1699 for (i = 0; i < count; i++) {
1702 if (vq->shadow_used_packed[i].len)
1703 flags = VRING_DESC_F_WRITE;
1707 if (vq->used_wrap_counter) {
1708 flags |= VRING_DESC_F_USED;
1709 flags |= VRING_DESC_F_AVAIL;
1711 flags &= ~VRING_DESC_F_USED;
1712 flags &= ~VRING_DESC_F_AVAIL;
1716 vq->desc_packed[vq->last_used_idx].flags = flags;
1718 head_idx = vq->last_used_idx;
1722 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1725 vq->desc_packed[head_idx].flags = head_flags;
1728 static __rte_always_inline int
1729 virtio_dev_rx_async_batch_packed(struct virtio_net *dev,
1730 struct vhost_virtqueue *vq,
1731 struct rte_mbuf **pkts,
1732 struct rte_mbuf **comp_pkts, uint32_t *pkt_done)
1735 uint32_t cpy_threshold = vq->async_threshold;
1737 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1738 if (unlikely(pkts[i]->pkt_len >= cpy_threshold))
1741 if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
1742 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1743 comp_pkts[(*pkt_done)++] = pkts[i];
1751 static __rte_always_inline int
1752 vhost_enqueue_async_single_packed(struct virtio_net *dev,
1753 struct vhost_virtqueue *vq,
1754 struct rte_mbuf *pkt,
1755 struct buf_vector *buf_vec,
1757 uint16_t *nr_buffers,
1758 struct vring_packed_desc *async_descs,
1759 struct iovec *src_iovec, struct iovec *dst_iovec,
1760 struct rte_vhost_iov_iter *src_it,
1761 struct rte_vhost_iov_iter *dst_it)
1763 uint16_t nr_vec = 0;
1764 uint16_t avail_idx = vq->last_avail_idx;
1765 uint16_t max_tries, tries = 0;
1766 uint16_t buf_id = 0;
1768 uint16_t desc_count = 0;
1769 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1770 uint32_t buffer_len[vq->size];
1771 uint16_t buffer_buf_id[vq->size];
1772 uint16_t buffer_desc_count[vq->size];
1774 if (rxvq_is_mergeable(dev))
1775 max_tries = vq->size - 1;
1781 * if we tried all available ring items, and still
1782 * can't get enough buf, it means something abnormal
1785 if (unlikely(++tries > max_tries))
1788 if (unlikely(fill_vec_buf_packed(dev, vq, avail_idx, &desc_count, buf_vec, &nr_vec,
1789 &buf_id, &len, VHOST_ACCESS_RW) < 0))
1792 len = RTE_MIN(len, size);
1795 buffer_len[*nr_buffers] = len;
1796 buffer_buf_id[*nr_buffers] = buf_id;
1797 buffer_desc_count[*nr_buffers] = desc_count;
1800 *nr_descs += desc_count;
1801 avail_idx += desc_count;
1802 if (avail_idx >= vq->size)
1803 avail_idx -= vq->size;
1806 if (async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, src_iovec, dst_iovec,
1807 src_it, dst_it) < 0)
1809 /* store descriptors for DMA */
1810 if (avail_idx >= *nr_descs) {
1811 rte_memcpy(async_descs, &vq->desc_packed[vq->last_avail_idx],
1812 *nr_descs * sizeof(struct vring_packed_desc));
1814 uint16_t nr_copy = vq->size - vq->last_avail_idx;
1816 rte_memcpy(async_descs, &vq->desc_packed[vq->last_avail_idx],
1817 nr_copy * sizeof(struct vring_packed_desc));
1818 rte_memcpy(async_descs + nr_copy, vq->desc_packed,
1819 (*nr_descs - nr_copy) * sizeof(struct vring_packed_desc));
1822 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1827 static __rte_always_inline int16_t
1828 virtio_dev_rx_async_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1829 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers,
1830 struct vring_packed_desc *async_descs,
1831 struct iovec *src_iovec, struct iovec *dst_iovec,
1832 struct rte_vhost_iov_iter *src_it, struct rte_vhost_iov_iter *dst_it)
1834 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1836 if (unlikely(vhost_enqueue_async_single_packed(dev, vq, pkt, buf_vec, nr_descs, nr_buffers,
1837 async_descs, src_iovec, dst_iovec,
1838 src_it, dst_it) < 0)) {
1839 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1843 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1844 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1849 static __rte_always_inline void
1850 dma_error_handler_packed(struct vhost_virtqueue *vq, struct vring_packed_desc *async_descs,
1851 uint16_t async_descs_idx, uint16_t slot_idx, uint32_t nr_err,
1852 uint32_t *pkt_idx, uint32_t *num_async_pkts, uint32_t *num_done_pkts)
1854 uint16_t descs_err = 0;
1855 uint16_t buffers_err = 0;
1856 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1858 *num_async_pkts -= nr_err;
1860 /* calculate the sum of buffers and descs of DMA-error packets. */
1861 while (nr_err-- > 0) {
1862 descs_err += pkts_info[slot_idx % vq->size].descs;
1863 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1867 vq->async_buffer_idx_packed -= buffers_err;
1869 if (vq->last_avail_idx >= descs_err) {
1870 vq->last_avail_idx -= descs_err;
1872 rte_memcpy(&vq->desc_packed[vq->last_avail_idx],
1873 &async_descs[async_descs_idx - descs_err],
1874 descs_err * sizeof(struct vring_packed_desc));
1878 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1879 nr_copy = vq->size - vq->last_avail_idx;
1880 rte_memcpy(&vq->desc_packed[vq->last_avail_idx],
1881 &async_descs[async_descs_idx - descs_err],
1882 nr_copy * sizeof(struct vring_packed_desc));
1883 descs_err -= nr_copy;
1884 rte_memcpy(&vq->desc_packed[0], &async_descs[async_descs_idx - descs_err],
1885 descs_err * sizeof(struct vring_packed_desc));
1886 vq->avail_wrap_counter ^= 1;
1889 *num_done_pkts = *pkt_idx - *num_async_pkts;
1892 static __rte_noinline uint32_t
1893 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1894 struct vhost_virtqueue *vq, uint16_t queue_id,
1895 struct rte_mbuf **pkts, uint32_t count,
1896 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
1898 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1899 uint32_t remained = count;
1900 uint16_t async_descs_idx = 0;
1901 uint16_t num_buffers;
1904 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1905 struct iovec *vec_pool = vq->vec_pool;
1906 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1907 struct iovec *src_iovec = vec_pool;
1908 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1909 uint16_t slot_idx = 0;
1910 uint16_t segs_await = 0;
1911 uint16_t iovec_idx = 0, it_idx = 0;
1912 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1913 uint32_t n_pkts = 0, pkt_err = 0;
1914 uint32_t num_async_pkts = 0, num_done_pkts = 0;
1915 struct vring_packed_desc async_descs[vq->size];
1918 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1919 if (remained >= PACKED_BATCH_SIZE) {
1920 if (!virtio_dev_rx_async_batch_packed(dev, vq,
1921 &pkts[pkt_idx], comp_pkts, &num_done_pkts)) {
1922 pkt_idx += PACKED_BATCH_SIZE;
1923 remained -= PACKED_BATCH_SIZE;
1930 if (unlikely(virtio_dev_rx_async_single_packed(dev, vq, pkts[pkt_idx],
1931 &num_descs, &num_buffers,
1932 &async_descs[async_descs_idx],
1933 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1934 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0))
1937 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1938 dev->vid, vq->last_avail_idx,
1939 vq->last_avail_idx + num_descs);
1941 slot_idx = (vq->async_pkts_idx + num_async_pkts) % vq->size;
1942 if (it_pool[it_idx].count) {
1945 async_descs_idx += num_descs;
1946 async_fill_desc(&tdes[pkt_burst_idx++],
1947 &it_pool[it_idx], &it_pool[it_idx + 1]);
1948 pkts_info[slot_idx].descs = num_descs;
1949 pkts_info[slot_idx].nr_buffers = num_buffers;
1950 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1952 iovec_idx += it_pool[it_idx].nr_segs;
1955 segs_await += it_pool[it_idx].nr_segs;
1958 * recover shadow used ring and keep DMA-occupied
1961 from = vq->shadow_used_idx - num_buffers;
1962 to = vq->async_buffer_idx_packed % vq->size;
1963 store_dma_desc_info_packed(vq->shadow_used_packed,
1964 vq->async_buffers_packed, vq->size, from, to, num_buffers);
1966 vq->async_buffer_idx_packed += num_buffers;
1967 vq->shadow_used_idx -= num_buffers;
1969 comp_pkts[num_done_pkts++] = pkts[pkt_idx];
1974 vq_inc_last_avail_packed(vq, num_descs);
1977 * conditions to trigger async device transfer:
1978 * - buffered packet number reaches transfer threshold
1979 * - unused async iov number is less than max vhost vector
1981 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1982 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await < BUF_VECTOR_MAX))) {
1983 n_pkts = vq->async_ops.transfer_data(dev->vid, queue_id,
1984 tdes, 0, pkt_burst_idx);
1988 vq->async_pkts_inflight_n += n_pkts;
1990 if (unlikely(n_pkts < pkt_burst_idx)) {
1992 * log error packets number here and do actual
1993 * error processing when applications poll
1996 pkt_err = pkt_burst_idx - n_pkts;
2003 } while (pkt_idx < count);
2005 if (pkt_burst_idx) {
2006 n_pkts = vq->async_ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
2007 vq->async_pkts_inflight_n += n_pkts;
2009 if (unlikely(n_pkts < pkt_burst_idx))
2010 pkt_err = pkt_burst_idx - n_pkts;
2013 do_data_copy_enqueue(dev, vq);
2015 if (unlikely(pkt_err))
2016 dma_error_handler_packed(vq, async_descs, async_descs_idx, slot_idx, pkt_err,
2017 &pkt_idx, &num_async_pkts, &num_done_pkts);
2018 vq->async_pkts_idx += num_async_pkts;
2019 *comp_count = num_done_pkts;
2021 if (likely(vq->shadow_used_idx)) {
2022 vhost_flush_enqueue_shadow_packed(dev, vq);
2023 vhost_vring_call_packed(dev, vq);
2029 static __rte_always_inline void
2030 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
2032 uint16_t nr_left = n_descs;
2037 from = vq->last_async_desc_idx_split & (vq->size - 1);
2038 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
2039 to = vq->last_used_idx & (vq->size - 1);
2041 if (to + nr_copy <= vq->size) {
2042 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
2043 nr_copy * sizeof(struct vring_used_elem));
2045 uint16_t size = vq->size - to;
2047 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
2048 size * sizeof(struct vring_used_elem));
2049 rte_memcpy(&vq->used->ring[0], &vq->async_descs_split[from + size],
2050 (nr_copy - size) * sizeof(struct vring_used_elem));
2053 vq->last_async_desc_idx_split += nr_copy;
2054 vq->last_used_idx += nr_copy;
2056 } while (nr_left > 0);
2059 static __rte_always_inline void
2060 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
2063 uint16_t nr_left = n_buffers;
2067 from = vq->last_async_buffer_idx_packed % vq->size;
2068 to = (from + nr_left) % vq->size;
2070 vhost_update_used_packed(vq, vq->async_buffers_packed + from, to - from);
2071 vq->last_async_buffer_idx_packed += nr_left;
2074 vhost_update_used_packed(vq, vq->async_buffers_packed + from,
2076 vq->last_async_buffer_idx_packed += vq->size - from;
2077 nr_left -= vq->size - from;
2079 } while (nr_left > 0);
2082 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2083 struct rte_mbuf **pkts, uint16_t count)
2085 struct virtio_net *dev = get_device(vid);
2086 struct vhost_virtqueue *vq;
2087 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0, n_buffers = 0;
2088 uint16_t start_idx, pkts_idx, vq_size;
2089 struct async_inflight_info *pkts_info;
2095 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2096 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2097 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2098 dev->vid, __func__, queue_id);
2102 vq = dev->virtqueue[queue_id];
2104 if (unlikely(!vq->async_registered)) {
2105 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2106 dev->vid, __func__, queue_id);
2110 rte_spinlock_lock(&vq->access_lock);
2112 pkts_idx = vq->async_pkts_idx % vq->size;
2113 pkts_info = vq->async_pkts_info;
2115 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
2116 vq_size, vq->async_pkts_inflight_n);
2118 if (count > vq->async_last_pkts_n)
2119 n_pkts_cpl = vq->async_ops.check_completed_copies(vid,
2120 queue_id, 0, count - vq->async_last_pkts_n);
2121 n_pkts_cpl += vq->async_last_pkts_n;
2123 n_pkts_put = RTE_MIN(count, n_pkts_cpl);
2124 if (unlikely(n_pkts_put == 0)) {
2125 vq->async_last_pkts_n = n_pkts_cpl;
2129 if (vq_is_packed(dev)) {
2130 for (i = 0; i < n_pkts_put; i++) {
2131 from = (start_idx + i) & (vq_size - 1);
2132 n_buffers += pkts_info[from].nr_buffers;
2133 pkts[i] = pkts_info[from].mbuf;
2136 for (i = 0; i < n_pkts_put; i++) {
2137 from = (start_idx + i) & (vq_size - 1);
2138 n_descs += pkts_info[from].descs;
2139 pkts[i] = pkts_info[from].mbuf;
2143 vq->async_last_pkts_n = n_pkts_cpl - n_pkts_put;
2144 vq->async_pkts_inflight_n -= n_pkts_put;
2146 if (likely(vq->enabled && vq->access_ok)) {
2147 if (vq_is_packed(dev)) {
2148 write_back_completed_descs_packed(vq, n_buffers);
2150 vhost_vring_call_packed(dev, vq);
2152 write_back_completed_descs_split(vq, n_descs);
2154 __atomic_add_fetch(&vq->used->idx, n_descs,
2156 vhost_vring_call_split(dev, vq);
2159 if (vq_is_packed(dev))
2160 vq->last_async_buffer_idx_packed += n_buffers;
2162 vq->last_async_desc_idx_split += n_descs;
2166 rte_spinlock_unlock(&vq->access_lock);
2171 static __rte_always_inline uint32_t
2172 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2173 struct rte_mbuf **pkts, uint32_t count,
2174 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
2176 struct vhost_virtqueue *vq;
2179 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2180 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2181 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2182 dev->vid, __func__, queue_id);
2186 vq = dev->virtqueue[queue_id];
2188 rte_spinlock_lock(&vq->access_lock);
2190 if (unlikely(!vq->enabled || !vq->async_registered))
2191 goto out_access_unlock;
2193 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2194 vhost_user_iotlb_rd_lock(vq);
2196 if (unlikely(!vq->access_ok))
2197 if (unlikely(vring_translate(dev, vq) < 0))
2200 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2204 if (vq_is_packed(dev))
2205 nb_tx = virtio_dev_rx_async_submit_packed(dev,
2206 vq, queue_id, pkts, count, comp_pkts,
2209 nb_tx = virtio_dev_rx_async_submit_split(dev,
2210 vq, queue_id, pkts, count, comp_pkts,
2214 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2215 vhost_user_iotlb_rd_unlock(vq);
2218 rte_spinlock_unlock(&vq->access_lock);
2224 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2225 struct rte_mbuf **pkts, uint16_t count,
2226 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
2228 struct virtio_net *dev = get_device(vid);
2234 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2236 "(%d) %s: built-in vhost net backend is disabled.\n",
2237 dev->vid, __func__);
2241 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, comp_pkts,
2246 virtio_net_with_host_offload(struct virtio_net *dev)
2249 ((1ULL << VIRTIO_NET_F_CSUM) |
2250 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2251 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2252 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2253 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2260 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
2262 struct rte_ipv4_hdr *ipv4_hdr;
2263 struct rte_ipv6_hdr *ipv6_hdr;
2264 void *l3_hdr = NULL;
2265 struct rte_ether_hdr *eth_hdr;
2268 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2270 m->l2_len = sizeof(struct rte_ether_hdr);
2271 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2273 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2274 struct rte_vlan_hdr *vlan_hdr =
2275 (struct rte_vlan_hdr *)(eth_hdr + 1);
2277 m->l2_len += sizeof(struct rte_vlan_hdr);
2278 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2281 l3_hdr = (char *)eth_hdr + m->l2_len;
2283 switch (ethertype) {
2284 case RTE_ETHER_TYPE_IPV4:
2286 *l4_proto = ipv4_hdr->next_proto_id;
2287 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2288 *l4_hdr = (char *)l3_hdr + m->l3_len;
2289 m->ol_flags |= PKT_TX_IPV4;
2291 case RTE_ETHER_TYPE_IPV6:
2293 *l4_proto = ipv6_hdr->proto;
2294 m->l3_len = sizeof(struct rte_ipv6_hdr);
2295 *l4_hdr = (char *)l3_hdr + m->l3_len;
2296 m->ol_flags |= PKT_TX_IPV6;
2306 static __rte_always_inline void
2307 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2309 uint16_t l4_proto = 0;
2310 void *l4_hdr = NULL;
2311 struct rte_tcp_hdr *tcp_hdr = NULL;
2313 parse_ethernet(m, &l4_proto, &l4_hdr);
2314 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2315 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2316 switch (hdr->csum_offset) {
2317 case (offsetof(struct rte_tcp_hdr, cksum)):
2318 if (l4_proto == IPPROTO_TCP)
2319 m->ol_flags |= PKT_TX_TCP_CKSUM;
2321 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2322 if (l4_proto == IPPROTO_UDP)
2323 m->ol_flags |= PKT_TX_UDP_CKSUM;
2325 case (offsetof(struct rte_sctp_hdr, cksum)):
2326 if (l4_proto == IPPROTO_SCTP)
2327 m->ol_flags |= PKT_TX_SCTP_CKSUM;
2335 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2336 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2337 case VIRTIO_NET_HDR_GSO_TCPV4:
2338 case VIRTIO_NET_HDR_GSO_TCPV6:
2340 m->ol_flags |= PKT_TX_TCP_SEG;
2341 m->tso_segsz = hdr->gso_size;
2342 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
2344 case VIRTIO_NET_HDR_GSO_UDP:
2345 m->ol_flags |= PKT_TX_UDP_SEG;
2346 m->tso_segsz = hdr->gso_size;
2347 m->l4_len = sizeof(struct rte_udp_hdr);
2350 VHOST_LOG_DATA(WARNING,
2351 "unsupported gso type %u.\n", hdr->gso_type);
2357 static __rte_always_inline void
2358 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2359 bool legacy_ol_flags)
2361 struct rte_net_hdr_lens hdr_lens;
2362 int l4_supported = 0;
2365 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2368 if (legacy_ol_flags) {
2369 vhost_dequeue_offload_legacy(hdr, m);
2373 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
2375 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2376 m->packet_type = ptype;
2377 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2378 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2379 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2382 /* According to Virtio 1.1 spec, the device only needs to look at
2383 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2384 * This differs from the processing incoming packets path where the
2385 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2388 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2389 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2390 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2392 * 5.1.6.2.2 Device Requirements: Packet Transmission
2393 * The device MUST ignore flag bits that it does not recognize.
2395 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2398 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2399 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2400 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
2402 /* Unknown proto or tunnel, do sw cksum. We can assume
2403 * the cksum field is in the first segment since the
2404 * buffers we provided to the host are large enough.
2405 * In case of SCTP, this will be wrong since it's a CRC
2406 * but there's nothing we can do.
2408 uint16_t csum = 0, off;
2410 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2411 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2413 if (likely(csum != 0xffff))
2415 off = hdr->csum_offset + hdr->csum_start;
2416 if (rte_pktmbuf_data_len(m) >= off + 1)
2417 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2421 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2422 if (hdr->gso_size == 0)
2425 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2426 case VIRTIO_NET_HDR_GSO_TCPV4:
2427 case VIRTIO_NET_HDR_GSO_TCPV6:
2428 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2430 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2431 m->tso_segsz = hdr->gso_size;
2433 case VIRTIO_NET_HDR_GSO_UDP:
2434 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2436 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2437 m->tso_segsz = hdr->gso_size;
2445 static __rte_noinline void
2446 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2447 struct buf_vector *buf_vec)
2450 uint64_t remain = sizeof(struct virtio_net_hdr);
2452 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2455 len = RTE_MIN(remain, buf_vec->buf_len);
2456 src = buf_vec->buf_addr;
2457 rte_memcpy((void *)(uintptr_t)dst,
2458 (void *)(uintptr_t)src, len);
2466 static __rte_always_inline int
2467 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2468 struct buf_vector *buf_vec, uint16_t nr_vec,
2469 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2470 bool legacy_ol_flags)
2472 uint32_t buf_avail, buf_offset;
2473 uint64_t buf_addr, buf_len;
2474 uint32_t mbuf_avail, mbuf_offset;
2476 struct rte_mbuf *cur = m, *prev = m;
2477 struct virtio_net_hdr tmp_hdr;
2478 struct virtio_net_hdr *hdr = NULL;
2479 /* A counter to avoid desc dead loop chain */
2480 uint16_t vec_idx = 0;
2481 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2484 buf_addr = buf_vec[vec_idx].buf_addr;
2485 buf_len = buf_vec[vec_idx].buf_len;
2487 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2492 if (virtio_net_with_host_offload(dev)) {
2493 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2495 * No luck, the virtio-net header doesn't fit
2496 * in a contiguous virtual area.
2498 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2501 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2506 * A virtio driver normally uses at least 2 desc buffers
2507 * for Tx: the first for storing the header, and others
2508 * for storing the data.
2510 if (unlikely(buf_len < dev->vhost_hlen)) {
2511 buf_offset = dev->vhost_hlen - buf_len;
2513 buf_addr = buf_vec[vec_idx].buf_addr;
2514 buf_len = buf_vec[vec_idx].buf_len;
2515 buf_avail = buf_len - buf_offset;
2516 } else if (buf_len == dev->vhost_hlen) {
2517 if (unlikely(++vec_idx >= nr_vec))
2519 buf_addr = buf_vec[vec_idx].buf_addr;
2520 buf_len = buf_vec[vec_idx].buf_len;
2523 buf_avail = buf_len;
2525 buf_offset = dev->vhost_hlen;
2526 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2530 (uintptr_t)(buf_addr + buf_offset),
2531 (uint32_t)buf_avail, 0);
2534 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2536 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2538 if (likely(cpy_len > MAX_BATCH_LEN ||
2539 vq->batch_copy_nb_elems >= vq->size ||
2540 (hdr && cur == m))) {
2541 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2543 (void *)((uintptr_t)(buf_addr +
2544 buf_offset)), cpy_len);
2546 batch_copy[vq->batch_copy_nb_elems].dst =
2547 rte_pktmbuf_mtod_offset(cur, void *,
2549 batch_copy[vq->batch_copy_nb_elems].src =
2550 (void *)((uintptr_t)(buf_addr + buf_offset));
2551 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2552 vq->batch_copy_nb_elems++;
2555 mbuf_avail -= cpy_len;
2556 mbuf_offset += cpy_len;
2557 buf_avail -= cpy_len;
2558 buf_offset += cpy_len;
2560 /* This buf reaches to its end, get the next one */
2561 if (buf_avail == 0) {
2562 if (++vec_idx >= nr_vec)
2565 buf_addr = buf_vec[vec_idx].buf_addr;
2566 buf_len = buf_vec[vec_idx].buf_len;
2569 buf_avail = buf_len;
2571 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2572 (uint32_t)buf_avail, 0);
2576 * This mbuf reaches to its end, get a new one
2577 * to hold more data.
2579 if (mbuf_avail == 0) {
2580 cur = rte_pktmbuf_alloc(mbuf_pool);
2581 if (unlikely(cur == NULL)) {
2582 VHOST_LOG_DATA(ERR, "Failed to "
2583 "allocate memory for mbuf.\n");
2589 prev->data_len = mbuf_offset;
2591 m->pkt_len += mbuf_offset;
2595 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2599 prev->data_len = mbuf_offset;
2600 m->pkt_len += mbuf_offset;
2603 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2611 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2617 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2619 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2620 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2625 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2626 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2628 if (unlikely(total_len > UINT16_MAX))
2631 buf_len = total_len;
2632 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2633 if (unlikely(buf == NULL))
2636 /* Initialize shinfo */
2637 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2638 virtio_dev_extbuf_free, buf);
2639 if (unlikely(shinfo == NULL)) {
2641 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2645 iova = rte_malloc_virt2iova(buf);
2646 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2647 rte_pktmbuf_reset_headroom(pkt);
2652 static __rte_always_inline int
2653 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2656 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2659 /* attach an external buffer if supported */
2660 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2663 /* check if chained buffers are allowed */
2664 if (!dev->linearbuf)
2671 * Allocate a host supported pktmbuf.
2673 static __rte_always_inline struct rte_mbuf *
2674 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
2677 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
2679 if (unlikely(pkt == NULL)) {
2681 "Failed to allocate memory for mbuf.\n");
2685 if (virtio_dev_pktmbuf_prep(dev, pkt, data_len)) {
2686 /* Data doesn't fit into the buffer and the host supports
2687 * only linear buffers
2689 rte_pktmbuf_free(pkt);
2698 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2699 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2700 bool legacy_ol_flags)
2703 uint16_t free_entries;
2704 uint16_t dropped = 0;
2705 static bool allocerr_warned;
2708 * The ordering between avail index and
2709 * desc reads needs to be enforced.
2711 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2713 if (free_entries == 0)
2716 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2718 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2720 count = RTE_MIN(count, MAX_PKT_BURST);
2721 count = RTE_MIN(count, free_entries);
2722 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2725 for (i = 0; i < count; i++) {
2726 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2729 uint16_t nr_vec = 0;
2732 if (unlikely(fill_vec_buf_split(dev, vq,
2733 vq->last_avail_idx + i,
2735 &head_idx, &buf_len,
2736 VHOST_ACCESS_RO) < 0))
2739 update_shadow_used_ring_split(vq, head_idx, 0);
2741 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2742 if (unlikely(pkts[i] == NULL)) {
2744 * mbuf allocation fails for jumbo packets when external
2745 * buffer allocation is not allowed and linear buffer
2746 * is required. Drop this packet.
2748 if (!allocerr_warned) {
2750 "Failed mbuf alloc of size %d from %s on %s.\n",
2751 buf_len, mbuf_pool->name, dev->ifname);
2752 allocerr_warned = true;
2759 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2760 mbuf_pool, legacy_ol_flags);
2761 if (unlikely(err)) {
2762 rte_pktmbuf_free(pkts[i]);
2763 if (!allocerr_warned) {
2765 "Failed to copy desc to mbuf on %s.\n",
2767 allocerr_warned = true;
2775 vq->last_avail_idx += i;
2777 do_data_copy_dequeue(vq);
2778 if (unlikely(i < count))
2779 vq->shadow_used_idx = i;
2780 if (likely(vq->shadow_used_idx)) {
2781 flush_shadow_used_ring_split(dev, vq);
2782 vhost_vring_call_split(dev, vq);
2785 return (i - dropped);
2790 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2791 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2792 struct rte_mbuf **pkts, uint16_t count)
2794 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2799 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2800 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2801 struct rte_mbuf **pkts, uint16_t count)
2803 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2806 static __rte_always_inline int
2807 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2808 struct vhost_virtqueue *vq,
2809 struct rte_mbuf **pkts,
2811 uintptr_t *desc_addrs,
2814 bool wrap = vq->avail_wrap_counter;
2815 struct vring_packed_desc *descs = vq->desc_packed;
2816 uint64_t lens[PACKED_BATCH_SIZE];
2817 uint64_t buf_lens[PACKED_BATCH_SIZE];
2818 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2821 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2823 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2826 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2827 flags = descs[avail_idx + i].flags;
2828 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2829 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2830 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2834 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2836 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2837 lens[i] = descs[avail_idx + i].len;
2839 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2840 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2841 descs[avail_idx + i].addr,
2842 &lens[i], VHOST_ACCESS_RW);
2845 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2846 if (unlikely(!desc_addrs[i]))
2848 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2852 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2853 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2857 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2858 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2860 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2861 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2865 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2866 pkts[i]->pkt_len = lens[i] - buf_offset;
2867 pkts[i]->data_len = pkts[i]->pkt_len;
2868 ids[i] = descs[avail_idx + i].id;
2877 static __rte_always_inline int
2878 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2879 struct vhost_virtqueue *vq,
2880 struct rte_mbuf **pkts,
2881 bool legacy_ol_flags)
2883 uint16_t avail_idx = vq->last_avail_idx;
2884 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2885 struct virtio_net_hdr *hdr;
2886 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2887 uint16_t ids[PACKED_BATCH_SIZE];
2890 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2894 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2895 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2897 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2898 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2899 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2902 if (virtio_net_with_host_offload(dev)) {
2903 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2904 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2905 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2909 if (virtio_net_is_inorder(dev))
2910 vhost_shadow_dequeue_batch_packed_inorder(vq,
2911 ids[PACKED_BATCH_SIZE - 1]);
2913 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2915 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2920 static __rte_always_inline int
2921 vhost_dequeue_single_packed(struct virtio_net *dev,
2922 struct vhost_virtqueue *vq,
2923 struct rte_mempool *mbuf_pool,
2924 struct rte_mbuf *pkts,
2926 uint16_t *desc_count,
2927 bool legacy_ol_flags)
2929 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2931 uint16_t nr_vec = 0;
2933 static bool allocerr_warned;
2935 if (unlikely(fill_vec_buf_packed(dev, vq,
2936 vq->last_avail_idx, desc_count,
2939 VHOST_ACCESS_RO) < 0))
2942 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2943 if (!allocerr_warned) {
2945 "Failed mbuf alloc of size %d from %s on %s.\n",
2946 buf_len, mbuf_pool->name, dev->ifname);
2947 allocerr_warned = true;
2952 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2953 mbuf_pool, legacy_ol_flags);
2954 if (unlikely(err)) {
2955 if (!allocerr_warned) {
2957 "Failed to copy desc to mbuf on %s.\n",
2959 allocerr_warned = true;
2967 static __rte_always_inline int
2968 virtio_dev_tx_single_packed(struct virtio_net *dev,
2969 struct vhost_virtqueue *vq,
2970 struct rte_mempool *mbuf_pool,
2971 struct rte_mbuf *pkts,
2972 bool legacy_ol_flags)
2975 uint16_t buf_id, desc_count = 0;
2978 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2979 &desc_count, legacy_ol_flags);
2981 if (likely(desc_count > 0)) {
2982 if (virtio_net_is_inorder(dev))
2983 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2986 vhost_shadow_dequeue_single_packed(vq, buf_id,
2989 vq_inc_last_avail_packed(vq, desc_count);
2997 virtio_dev_tx_packed(struct virtio_net *dev,
2998 struct vhost_virtqueue *__rte_restrict vq,
2999 struct rte_mempool *mbuf_pool,
3000 struct rte_mbuf **__rte_restrict pkts,
3002 bool legacy_ol_flags)
3004 uint32_t pkt_idx = 0;
3006 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
3010 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3012 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3013 if (!virtio_dev_tx_batch_packed(dev, vq,
3016 pkt_idx += PACKED_BATCH_SIZE;
3021 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3026 } while (pkt_idx < count);
3028 if (pkt_idx != count)
3029 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3031 if (vq->shadow_used_idx) {
3032 do_data_copy_dequeue(vq);
3034 vhost_flush_dequeue_shadow_packed(dev, vq);
3035 vhost_vring_call_packed(dev, vq);
3043 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3044 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3045 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3047 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3052 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3053 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3054 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3056 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3060 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3061 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3063 struct virtio_net *dev;
3064 struct rte_mbuf *rarp_mbuf = NULL;
3065 struct vhost_virtqueue *vq;
3066 int16_t success = 1;
3068 dev = get_device(vid);
3072 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3074 "(%d) %s: built-in vhost net backend is disabled.\n",
3075 dev->vid, __func__);
3079 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3081 "(%d) %s: invalid virtqueue idx %d.\n",
3082 dev->vid, __func__, queue_id);
3086 vq = dev->virtqueue[queue_id];
3088 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3091 if (unlikely(!vq->enabled)) {
3093 goto out_access_unlock;
3096 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3097 vhost_user_iotlb_rd_lock(vq);
3099 if (unlikely(!vq->access_ok))
3100 if (unlikely(vring_translate(dev, vq) < 0)) {
3106 * Construct a RARP broadcast packet, and inject it to the "pkts"
3107 * array, to looks like that guest actually send such packet.
3109 * Check user_send_rarp() for more information.
3111 * broadcast_rarp shares a cacheline in the virtio_net structure
3112 * with some fields that are accessed during enqueue and
3113 * __atomic_compare_exchange_n causes a write if performed compare
3114 * and exchange. This could result in false sharing between enqueue
3117 * Prevent unnecessary false sharing by reading broadcast_rarp first
3118 * and only performing compare and exchange if the read indicates it
3119 * is likely to be set.
3121 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3122 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3123 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3125 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3126 if (rarp_mbuf == NULL) {
3127 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3134 if (vq_is_packed(dev)) {
3135 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3136 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3138 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3140 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3141 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3143 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3147 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3148 vhost_user_iotlb_rd_unlock(vq);
3151 rte_spinlock_unlock(&vq->access_lock);
3153 if (unlikely(rarp_mbuf != NULL)) {
3155 * Inject it to the head of "pkts" array, so that switch's mac
3156 * learning table will get updated first.
3158 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
3159 pkts[0] = rarp_mbuf;