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>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
20 #include <rte_vhost_async.h>
25 #define MAX_BATCH_LEN 256
27 #define VHOST_ASYNC_BATCH_THRESHOLD 32
29 static __rte_always_inline bool
30 rxvq_is_mergeable(struct virtio_net *dev)
32 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 static __rte_always_inline bool
36 virtio_net_is_inorder(struct virtio_net *dev)
38 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
42 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
44 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
48 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
50 struct batch_copy_elem *elem = vq->batch_copy_elems;
51 uint16_t count = vq->batch_copy_nb_elems;
54 for (i = 0; i < count; i++) {
55 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
56 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
58 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
61 vq->batch_copy_nb_elems = 0;
65 do_data_copy_dequeue(struct vhost_virtqueue *vq)
67 struct batch_copy_elem *elem = vq->batch_copy_elems;
68 uint16_t count = vq->batch_copy_nb_elems;
71 for (i = 0; i < count; i++)
72 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
74 vq->batch_copy_nb_elems = 0;
77 static __rte_always_inline void
78 do_flush_shadow_used_ring_split(struct virtio_net *dev,
79 struct vhost_virtqueue *vq,
80 uint16_t to, uint16_t from, uint16_t size)
82 rte_memcpy(&vq->used->ring[to],
83 &vq->shadow_used_split[from],
84 size * sizeof(struct vring_used_elem));
85 vhost_log_cache_used_vring(dev, vq,
86 offsetof(struct vring_used, ring[to]),
87 size * sizeof(struct vring_used_elem));
90 static __rte_always_inline void
91 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
93 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
95 if (used_idx + vq->shadow_used_idx <= vq->size) {
96 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
101 /* update used ring interval [used_idx, vq->size] */
102 size = vq->size - used_idx;
103 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
105 /* update the left half used ring interval [0, left_size] */
106 do_flush_shadow_used_ring_split(dev, vq, 0, size,
107 vq->shadow_used_idx - size);
109 vq->last_used_idx += vq->shadow_used_idx;
111 vhost_log_cache_sync(dev, vq);
113 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
115 vq->shadow_used_idx = 0;
116 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
117 sizeof(vq->used->idx));
120 static __rte_always_inline void
121 async_flush_shadow_used_ring_split(struct virtio_net *dev,
122 struct vhost_virtqueue *vq)
124 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
126 if (used_idx + vq->shadow_used_idx <= vq->size) {
127 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
128 vq->shadow_used_idx);
132 /* update used ring interval [used_idx, vq->size] */
133 size = vq->size - used_idx;
134 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
136 /* update the left half used ring interval [0, left_size] */
137 do_flush_shadow_used_ring_split(dev, vq, 0, size,
138 vq->shadow_used_idx - size);
141 vq->last_used_idx += vq->shadow_used_idx;
142 vq->shadow_used_idx = 0;
145 static __rte_always_inline void
146 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
147 uint16_t desc_idx, uint32_t len)
149 uint16_t i = vq->shadow_used_idx++;
151 vq->shadow_used_split[i].id = desc_idx;
152 vq->shadow_used_split[i].len = len;
155 static __rte_always_inline void
156 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
157 struct vhost_virtqueue *vq)
160 uint16_t used_idx = vq->last_used_idx;
161 uint16_t head_idx = vq->last_used_idx;
162 uint16_t head_flags = 0;
164 /* Split loop in two to save memory barriers */
165 for (i = 0; i < vq->shadow_used_idx; i++) {
166 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
167 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
169 used_idx += vq->shadow_used_packed[i].count;
170 if (used_idx >= vq->size)
171 used_idx -= vq->size;
176 for (i = 0; i < vq->shadow_used_idx; i++) {
179 if (vq->shadow_used_packed[i].len)
180 flags = VRING_DESC_F_WRITE;
184 if (vq->used_wrap_counter) {
185 flags |= VRING_DESC_F_USED;
186 flags |= VRING_DESC_F_AVAIL;
188 flags &= ~VRING_DESC_F_USED;
189 flags &= ~VRING_DESC_F_AVAIL;
193 vq->desc_packed[vq->last_used_idx].flags = flags;
195 vhost_log_cache_used_vring(dev, vq,
197 sizeof(struct vring_packed_desc),
198 sizeof(struct vring_packed_desc));
200 head_idx = vq->last_used_idx;
204 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
207 vq->desc_packed[head_idx].flags = head_flags;
209 vhost_log_cache_used_vring(dev, vq,
211 sizeof(struct vring_packed_desc),
212 sizeof(struct vring_packed_desc));
214 vq->shadow_used_idx = 0;
215 vhost_log_cache_sync(dev, vq);
218 static __rte_always_inline void
219 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
220 struct vhost_virtqueue *vq)
222 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
224 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
226 vq->desc_packed[vq->shadow_last_used_idx].flags = used_elem->flags;
228 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
229 sizeof(struct vring_packed_desc),
230 sizeof(struct vring_packed_desc));
231 vq->shadow_used_idx = 0;
232 vhost_log_cache_sync(dev, vq);
235 static __rte_always_inline void
236 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
237 struct vhost_virtqueue *vq,
244 if (vq->shadow_used_idx) {
245 do_data_copy_enqueue(dev, vq);
246 vhost_flush_enqueue_shadow_packed(dev, vq);
249 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
251 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
252 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
253 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
258 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
259 vq->desc_packed[vq->last_used_idx + i].flags = flags;
261 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
262 sizeof(struct vring_packed_desc),
263 sizeof(struct vring_packed_desc) *
265 vhost_log_cache_sync(dev, vq);
267 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
270 static __rte_always_inline void
271 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
274 vq->shadow_used_packed[0].id = id;
276 if (!vq->shadow_used_idx) {
277 vq->shadow_last_used_idx = vq->last_used_idx;
278 vq->shadow_used_packed[0].flags =
279 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
280 vq->shadow_used_packed[0].len = 0;
281 vq->shadow_used_packed[0].count = 1;
282 vq->shadow_used_idx++;
285 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
288 static __rte_always_inline void
289 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
290 struct vhost_virtqueue *vq,
297 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
299 if (!vq->shadow_used_idx) {
300 vq->shadow_last_used_idx = vq->last_used_idx;
301 vq->shadow_used_packed[0].id = ids[0];
302 vq->shadow_used_packed[0].len = 0;
303 vq->shadow_used_packed[0].count = 1;
304 vq->shadow_used_packed[0].flags = flags;
305 vq->shadow_used_idx++;
310 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
311 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
312 vq->desc_packed[vq->last_used_idx + i].len = 0;
316 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
317 vq->desc_packed[vq->last_used_idx + i].flags = flags;
319 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
320 sizeof(struct vring_packed_desc),
321 sizeof(struct vring_packed_desc) *
323 vhost_log_cache_sync(dev, vq);
325 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
328 static __rte_always_inline void
329 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
335 flags = vq->desc_packed[vq->last_used_idx].flags;
336 if (vq->used_wrap_counter) {
337 flags |= VRING_DESC_F_USED;
338 flags |= VRING_DESC_F_AVAIL;
340 flags &= ~VRING_DESC_F_USED;
341 flags &= ~VRING_DESC_F_AVAIL;
344 if (!vq->shadow_used_idx) {
345 vq->shadow_last_used_idx = vq->last_used_idx;
347 vq->shadow_used_packed[0].id = buf_id;
348 vq->shadow_used_packed[0].len = 0;
349 vq->shadow_used_packed[0].flags = flags;
350 vq->shadow_used_idx++;
352 vq->desc_packed[vq->last_used_idx].id = buf_id;
353 vq->desc_packed[vq->last_used_idx].len = 0;
354 vq->desc_packed[vq->last_used_idx].flags = flags;
357 vq_inc_last_used_packed(vq, count);
360 static __rte_always_inline void
361 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
367 vq->shadow_used_packed[0].id = buf_id;
369 flags = vq->desc_packed[vq->last_used_idx].flags;
370 if (vq->used_wrap_counter) {
371 flags |= VRING_DESC_F_USED;
372 flags |= VRING_DESC_F_AVAIL;
374 flags &= ~VRING_DESC_F_USED;
375 flags &= ~VRING_DESC_F_AVAIL;
378 if (!vq->shadow_used_idx) {
379 vq->shadow_last_used_idx = vq->last_used_idx;
380 vq->shadow_used_packed[0].len = 0;
381 vq->shadow_used_packed[0].flags = flags;
382 vq->shadow_used_idx++;
385 vq_inc_last_used_packed(vq, count);
388 static __rte_always_inline void
389 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
390 struct vhost_virtqueue *vq,
394 uint16_t num_buffers)
397 for (i = 0; i < num_buffers; i++) {
398 /* enqueue shadow flush action aligned with batch num */
399 if (!vq->shadow_used_idx)
400 vq->shadow_aligned_idx = vq->last_used_idx &
402 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
403 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
404 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
405 vq->shadow_aligned_idx += count[i];
406 vq->shadow_used_idx++;
409 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
410 do_data_copy_enqueue(dev, vq);
411 vhost_flush_enqueue_shadow_packed(dev, vq);
415 /* avoid write operation when necessary, to lessen cache issues */
416 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
417 if ((var) != (val)) \
421 static __rte_always_inline void
422 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
424 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
426 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
427 csum_l4 |= PKT_TX_TCP_CKSUM;
430 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
431 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
434 case PKT_TX_TCP_CKSUM:
435 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
438 case PKT_TX_UDP_CKSUM:
439 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
442 case PKT_TX_SCTP_CKSUM:
443 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
448 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
449 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
450 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
453 /* IP cksum verification cannot be bypassed, then calculate here */
454 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
455 struct rte_ipv4_hdr *ipv4_hdr;
457 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
459 ipv4_hdr->hdr_checksum = 0;
460 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
463 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
464 if (m_buf->ol_flags & PKT_TX_IPV4)
465 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
467 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
468 net_hdr->gso_size = m_buf->tso_segsz;
469 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
471 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
472 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
473 net_hdr->gso_size = m_buf->tso_segsz;
474 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
477 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
478 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
479 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
483 static __rte_always_inline int
484 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
485 struct buf_vector *buf_vec, uint16_t *vec_idx,
486 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
488 uint16_t vec_id = *vec_idx;
492 uint64_t desc_chunck_len = desc_len;
494 if (unlikely(vec_id >= BUF_VECTOR_MAX))
497 desc_addr = vhost_iova_to_vva(dev, vq,
501 if (unlikely(!desc_addr))
504 rte_prefetch0((void *)(uintptr_t)desc_addr);
506 buf_vec[vec_id].buf_iova = desc_iova;
507 buf_vec[vec_id].buf_addr = desc_addr;
508 buf_vec[vec_id].buf_len = desc_chunck_len;
510 desc_len -= desc_chunck_len;
511 desc_iova += desc_chunck_len;
519 static __rte_always_inline int
520 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
521 uint32_t avail_idx, uint16_t *vec_idx,
522 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
523 uint32_t *desc_chain_len, uint8_t perm)
525 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
526 uint16_t vec_id = *vec_idx;
529 uint32_t nr_descs = vq->size;
531 struct vring_desc *descs = vq->desc;
532 struct vring_desc *idesc = NULL;
534 if (unlikely(idx >= vq->size))
537 *desc_chain_head = idx;
539 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
540 dlen = vq->desc[idx].len;
541 nr_descs = dlen / sizeof(struct vring_desc);
542 if (unlikely(nr_descs > vq->size))
545 descs = (struct vring_desc *)(uintptr_t)
546 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
549 if (unlikely(!descs))
552 if (unlikely(dlen < vq->desc[idx].len)) {
554 * The indirect desc table is not contiguous
555 * in process VA space, we have to copy it.
557 idesc = vhost_alloc_copy_ind_table(dev, vq,
558 vq->desc[idx].addr, vq->desc[idx].len);
559 if (unlikely(!idesc))
569 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
570 free_ind_table(idesc);
574 len += descs[idx].len;
576 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
577 descs[idx].addr, descs[idx].len,
579 free_ind_table(idesc);
583 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
586 idx = descs[idx].next;
589 *desc_chain_len = len;
592 if (unlikely(!!idesc))
593 free_ind_table(idesc);
599 * Returns -1 on fail, 0 on success
602 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
603 uint32_t size, struct buf_vector *buf_vec,
604 uint16_t *num_buffers, uint16_t avail_head,
608 uint16_t vec_idx = 0;
609 uint16_t max_tries, tries = 0;
611 uint16_t head_idx = 0;
615 cur_idx = vq->last_avail_idx;
617 if (rxvq_is_mergeable(dev))
618 max_tries = vq->size - 1;
623 if (unlikely(cur_idx == avail_head))
626 * if we tried all available ring items, and still
627 * can't get enough buf, it means something abnormal
630 if (unlikely(++tries > max_tries))
633 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
636 VHOST_ACCESS_RW) < 0))
638 len = RTE_MIN(len, size);
639 update_shadow_used_ring_split(vq, head_idx, len);
651 static __rte_always_inline int
652 fill_vec_buf_packed_indirect(struct virtio_net *dev,
653 struct vhost_virtqueue *vq,
654 struct vring_packed_desc *desc, uint16_t *vec_idx,
655 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
659 uint16_t vec_id = *vec_idx;
661 struct vring_packed_desc *descs, *idescs = NULL;
664 descs = (struct vring_packed_desc *)(uintptr_t)
665 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
666 if (unlikely(!descs))
669 if (unlikely(dlen < desc->len)) {
671 * The indirect desc table is not contiguous
672 * in process VA space, we have to copy it.
674 idescs = vhost_alloc_copy_ind_table(dev,
675 vq, desc->addr, desc->len);
676 if (unlikely(!idescs))
682 nr_descs = desc->len / sizeof(struct vring_packed_desc);
683 if (unlikely(nr_descs >= vq->size)) {
684 free_ind_table(idescs);
688 for (i = 0; i < nr_descs; i++) {
689 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
690 free_ind_table(idescs);
694 *len += descs[i].len;
695 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
696 descs[i].addr, descs[i].len,
702 if (unlikely(!!idescs))
703 free_ind_table(idescs);
708 static __rte_always_inline int
709 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
710 uint16_t avail_idx, uint16_t *desc_count,
711 struct buf_vector *buf_vec, uint16_t *vec_idx,
712 uint16_t *buf_id, uint32_t *len, uint8_t perm)
714 bool wrap_counter = vq->avail_wrap_counter;
715 struct vring_packed_desc *descs = vq->desc_packed;
716 uint16_t vec_id = *vec_idx;
718 if (avail_idx < vq->last_avail_idx)
722 * Perform a load-acquire barrier in desc_is_avail to
723 * enforce the ordering between desc flags and desc
726 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
733 if (unlikely(vec_id >= BUF_VECTOR_MAX))
736 if (unlikely(*desc_count >= vq->size))
740 *buf_id = descs[avail_idx].id;
742 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
743 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
749 *len += descs[avail_idx].len;
751 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
752 descs[avail_idx].addr,
753 descs[avail_idx].len,
758 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
761 if (++avail_idx >= vq->size) {
762 avail_idx -= vq->size;
772 static __rte_noinline void
773 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
774 struct buf_vector *buf_vec,
775 struct virtio_net_hdr_mrg_rxbuf *hdr)
778 uint64_t remain = dev->vhost_hlen;
779 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
780 uint64_t iova = buf_vec->buf_iova;
783 len = RTE_MIN(remain,
785 dst = buf_vec->buf_addr;
786 rte_memcpy((void *)(uintptr_t)dst,
787 (void *)(uintptr_t)src,
790 PRINT_PACKET(dev, (uintptr_t)dst,
792 vhost_log_cache_write_iova(dev, vq,
802 static __rte_always_inline int
803 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
804 struct rte_mbuf *m, struct buf_vector *buf_vec,
805 uint16_t nr_vec, uint16_t num_buffers)
807 uint32_t vec_idx = 0;
808 uint32_t mbuf_offset, mbuf_avail;
809 uint32_t buf_offset, buf_avail;
810 uint64_t buf_addr, buf_iova, buf_len;
813 struct rte_mbuf *hdr_mbuf;
814 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
815 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
818 if (unlikely(m == NULL)) {
823 buf_addr = buf_vec[vec_idx].buf_addr;
824 buf_iova = buf_vec[vec_idx].buf_iova;
825 buf_len = buf_vec[vec_idx].buf_len;
827 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
834 if (unlikely(buf_len < dev->vhost_hlen))
837 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
839 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
840 dev->vid, num_buffers);
842 if (unlikely(buf_len < dev->vhost_hlen)) {
843 buf_offset = dev->vhost_hlen - buf_len;
845 buf_addr = buf_vec[vec_idx].buf_addr;
846 buf_iova = buf_vec[vec_idx].buf_iova;
847 buf_len = buf_vec[vec_idx].buf_len;
848 buf_avail = buf_len - buf_offset;
850 buf_offset = dev->vhost_hlen;
851 buf_avail = buf_len - dev->vhost_hlen;
854 mbuf_avail = rte_pktmbuf_data_len(m);
856 while (mbuf_avail != 0 || m->next != NULL) {
857 /* done with current buf, get the next one */
858 if (buf_avail == 0) {
860 if (unlikely(vec_idx >= nr_vec)) {
865 buf_addr = buf_vec[vec_idx].buf_addr;
866 buf_iova = buf_vec[vec_idx].buf_iova;
867 buf_len = buf_vec[vec_idx].buf_len;
873 /* done with current mbuf, get the next one */
874 if (mbuf_avail == 0) {
878 mbuf_avail = rte_pktmbuf_data_len(m);
882 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
883 if (rxvq_is_mergeable(dev))
884 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
887 if (unlikely(hdr == &tmp_hdr)) {
888 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
890 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
892 vhost_log_cache_write_iova(dev, vq,
900 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
902 if (likely(cpy_len > MAX_BATCH_LEN ||
903 vq->batch_copy_nb_elems >= vq->size)) {
904 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
905 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
907 vhost_log_cache_write_iova(dev, vq,
908 buf_iova + buf_offset,
910 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
913 batch_copy[vq->batch_copy_nb_elems].dst =
914 (void *)((uintptr_t)(buf_addr + buf_offset));
915 batch_copy[vq->batch_copy_nb_elems].src =
916 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
917 batch_copy[vq->batch_copy_nb_elems].log_addr =
918 buf_iova + buf_offset;
919 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
920 vq->batch_copy_nb_elems++;
923 mbuf_avail -= cpy_len;
924 mbuf_offset += cpy_len;
925 buf_avail -= cpy_len;
926 buf_offset += cpy_len;
934 static __rte_always_inline void
935 async_fill_vec(struct iovec *v, void *base, size_t len)
941 static __rte_always_inline void
942 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
943 struct iovec *vec, unsigned long nr_seg)
950 it->nr_segs = nr_seg;
957 static __rte_always_inline void
958 async_fill_desc(struct rte_vhost_async_desc *desc,
959 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
965 static __rte_always_inline int
966 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
967 struct rte_mbuf *m, struct buf_vector *buf_vec,
968 uint16_t nr_vec, uint16_t num_buffers,
969 struct iovec *src_iovec, struct iovec *dst_iovec,
970 struct rte_vhost_iov_iter *src_it,
971 struct rte_vhost_iov_iter *dst_it)
973 uint32_t vec_idx = 0;
974 uint32_t mbuf_offset, mbuf_avail;
975 uint32_t buf_offset, buf_avail;
976 uint64_t buf_addr, buf_iova, buf_len;
977 uint32_t cpy_len, cpy_threshold;
979 struct rte_mbuf *hdr_mbuf;
980 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
981 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
988 if (unlikely(m == NULL)) {
993 cpy_threshold = vq->async_threshold;
995 buf_addr = buf_vec[vec_idx].buf_addr;
996 buf_iova = buf_vec[vec_idx].buf_iova;
997 buf_len = buf_vec[vec_idx].buf_len;
999 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1005 hdr_addr = buf_addr;
1006 if (unlikely(buf_len < dev->vhost_hlen))
1009 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1011 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1012 dev->vid, num_buffers);
1014 if (unlikely(buf_len < dev->vhost_hlen)) {
1015 buf_offset = dev->vhost_hlen - buf_len;
1017 buf_addr = buf_vec[vec_idx].buf_addr;
1018 buf_iova = buf_vec[vec_idx].buf_iova;
1019 buf_len = buf_vec[vec_idx].buf_len;
1020 buf_avail = buf_len - buf_offset;
1022 buf_offset = dev->vhost_hlen;
1023 buf_avail = buf_len - dev->vhost_hlen;
1026 mbuf_avail = rte_pktmbuf_data_len(m);
1029 while (mbuf_avail != 0 || m->next != NULL) {
1030 /* done with current buf, get the next one */
1031 if (buf_avail == 0) {
1033 if (unlikely(vec_idx >= nr_vec)) {
1038 buf_addr = buf_vec[vec_idx].buf_addr;
1039 buf_iova = buf_vec[vec_idx].buf_iova;
1040 buf_len = buf_vec[vec_idx].buf_len;
1043 buf_avail = buf_len;
1046 /* done with current mbuf, get the next one */
1047 if (mbuf_avail == 0) {
1051 mbuf_avail = rte_pktmbuf_data_len(m);
1055 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1056 if (rxvq_is_mergeable(dev))
1057 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1060 if (unlikely(hdr == &tmp_hdr)) {
1061 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1063 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1064 dev->vhost_hlen, 0);
1065 vhost_log_cache_write_iova(dev, vq,
1066 buf_vec[0].buf_iova,
1073 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1075 if (unlikely(cpy_len >= cpy_threshold)) {
1076 hpa = (void *)(uintptr_t)gpa_to_hpa(dev,
1077 buf_iova + buf_offset, cpy_len);
1079 if (unlikely(!hpa)) {
1084 async_fill_vec(src_iovec + tvec_idx,
1085 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1086 mbuf_offset), cpy_len);
1088 async_fill_vec(dst_iovec + tvec_idx, hpa, cpy_len);
1093 if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1095 (void *)((uintptr_t)(buf_addr + buf_offset)),
1096 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1100 (uintptr_t)(buf_addr + buf_offset),
1103 batch_copy[vq->batch_copy_nb_elems].dst =
1104 (void *)((uintptr_t)(buf_addr + buf_offset));
1105 batch_copy[vq->batch_copy_nb_elems].src =
1106 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1107 batch_copy[vq->batch_copy_nb_elems].log_addr =
1108 buf_iova + buf_offset;
1109 batch_copy[vq->batch_copy_nb_elems].len =
1111 vq->batch_copy_nb_elems++;
1115 mbuf_avail -= cpy_len;
1116 mbuf_offset += cpy_len;
1117 buf_avail -= cpy_len;
1118 buf_offset += cpy_len;
1122 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1123 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1128 static __rte_always_inline int
1129 vhost_enqueue_single_packed(struct virtio_net *dev,
1130 struct vhost_virtqueue *vq,
1131 struct rte_mbuf *pkt,
1132 struct buf_vector *buf_vec,
1135 uint16_t nr_vec = 0;
1136 uint16_t avail_idx = vq->last_avail_idx;
1137 uint16_t max_tries, tries = 0;
1138 uint16_t buf_id = 0;
1140 uint16_t desc_count;
1141 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
1142 uint16_t num_buffers = 0;
1143 uint32_t buffer_len[vq->size];
1144 uint16_t buffer_buf_id[vq->size];
1145 uint16_t buffer_desc_count[vq->size];
1147 if (rxvq_is_mergeable(dev))
1148 max_tries = vq->size - 1;
1154 * if we tried all available ring items, and still
1155 * can't get enough buf, it means something abnormal
1158 if (unlikely(++tries > max_tries))
1161 if (unlikely(fill_vec_buf_packed(dev, vq,
1162 avail_idx, &desc_count,
1165 VHOST_ACCESS_RW) < 0))
1168 len = RTE_MIN(len, size);
1171 buffer_len[num_buffers] = len;
1172 buffer_buf_id[num_buffers] = buf_id;
1173 buffer_desc_count[num_buffers] = desc_count;
1176 *nr_descs += desc_count;
1177 avail_idx += desc_count;
1178 if (avail_idx >= vq->size)
1179 avail_idx -= vq->size;
1182 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1185 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1186 buffer_desc_count, num_buffers);
1191 static __rte_noinline uint32_t
1192 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1193 struct rte_mbuf **pkts, uint32_t count)
1195 uint32_t pkt_idx = 0;
1196 uint16_t num_buffers;
1197 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1198 uint16_t avail_head;
1201 * The ordering between avail index and
1202 * desc reads needs to be enforced.
1204 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1206 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1208 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1209 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1210 uint16_t nr_vec = 0;
1212 if (unlikely(reserve_avail_buf_split(dev, vq,
1213 pkt_len, buf_vec, &num_buffers,
1214 avail_head, &nr_vec) < 0)) {
1215 VHOST_LOG_DATA(DEBUG,
1216 "(%d) failed to get enough desc from vring\n",
1218 vq->shadow_used_idx -= num_buffers;
1222 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1223 dev->vid, vq->last_avail_idx,
1224 vq->last_avail_idx + num_buffers);
1226 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1229 vq->shadow_used_idx -= num_buffers;
1233 vq->last_avail_idx += num_buffers;
1236 do_data_copy_enqueue(dev, vq);
1238 if (likely(vq->shadow_used_idx)) {
1239 flush_shadow_used_ring_split(dev, vq);
1240 vhost_vring_call_split(dev, vq);
1246 static __rte_always_inline int
1247 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1248 struct vhost_virtqueue *vq,
1249 struct rte_mbuf **pkts)
1251 bool wrap_counter = vq->avail_wrap_counter;
1252 struct vring_packed_desc *descs = vq->desc_packed;
1253 uint16_t avail_idx = vq->last_avail_idx;
1254 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1255 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1256 uint32_t buf_offset = dev->vhost_hlen;
1257 uint64_t lens[PACKED_BATCH_SIZE];
1258 uint16_t ids[PACKED_BATCH_SIZE];
1261 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1264 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1267 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1268 if (unlikely(pkts[i]->next != NULL))
1270 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1277 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1278 lens[i] = descs[avail_idx + i].len;
1280 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1281 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1285 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1286 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1287 descs[avail_idx + i].addr,
1291 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1292 if (unlikely(!desc_addrs[i]))
1294 if (unlikely(lens[i] != descs[avail_idx + i].len))
1298 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1299 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1300 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1301 (uintptr_t)desc_addrs[i];
1302 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1305 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1306 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1308 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1310 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1311 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1312 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1316 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1317 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1320 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1321 ids[i] = descs[avail_idx + i].id;
1323 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1328 static __rte_always_inline int16_t
1329 virtio_dev_rx_single_packed(struct virtio_net *dev,
1330 struct vhost_virtqueue *vq,
1331 struct rte_mbuf *pkt)
1333 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1334 uint16_t nr_descs = 0;
1337 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1339 VHOST_LOG_DATA(DEBUG,
1340 "(%d) failed to get enough desc from vring\n",
1345 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1346 dev->vid, vq->last_avail_idx,
1347 vq->last_avail_idx + nr_descs);
1349 vq_inc_last_avail_packed(vq, nr_descs);
1354 static __rte_noinline uint32_t
1355 virtio_dev_rx_packed(struct virtio_net *dev,
1356 struct vhost_virtqueue *__rte_restrict vq,
1357 struct rte_mbuf **__rte_restrict pkts,
1360 uint32_t pkt_idx = 0;
1361 uint32_t remained = count;
1364 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1366 if (remained >= PACKED_BATCH_SIZE) {
1367 if (!virtio_dev_rx_batch_packed(dev, vq,
1369 pkt_idx += PACKED_BATCH_SIZE;
1370 remained -= PACKED_BATCH_SIZE;
1375 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1380 } while (pkt_idx < count);
1382 if (vq->shadow_used_idx) {
1383 do_data_copy_enqueue(dev, vq);
1384 vhost_flush_enqueue_shadow_packed(dev, vq);
1388 vhost_vring_call_packed(dev, vq);
1393 static __rte_always_inline uint32_t
1394 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1395 struct rte_mbuf **pkts, uint32_t count)
1397 struct vhost_virtqueue *vq;
1400 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1401 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1402 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1403 dev->vid, __func__, queue_id);
1407 vq = dev->virtqueue[queue_id];
1409 rte_spinlock_lock(&vq->access_lock);
1411 if (unlikely(vq->enabled == 0))
1412 goto out_access_unlock;
1414 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1415 vhost_user_iotlb_rd_lock(vq);
1417 if (unlikely(vq->access_ok == 0))
1418 if (unlikely(vring_translate(dev, vq) < 0))
1421 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1425 if (vq_is_packed(dev))
1426 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1428 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1431 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1432 vhost_user_iotlb_rd_unlock(vq);
1435 rte_spinlock_unlock(&vq->access_lock);
1441 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1442 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1444 struct virtio_net *dev = get_device(vid);
1449 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1451 "(%d) %s: built-in vhost net backend is disabled.\n",
1452 dev->vid, __func__);
1456 return virtio_dev_rx(dev, queue_id, pkts, count);
1459 static __rte_always_inline uint16_t
1460 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1461 uint16_t vq_size, uint16_t n_inflight)
1463 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1464 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1467 static __rte_always_inline void
1468 virtio_dev_rx_async_submit_split_err(struct virtio_net *dev,
1469 struct vhost_virtqueue *vq, uint16_t queue_id,
1470 uint16_t last_idx, uint16_t shadow_idx)
1472 uint16_t start_idx, pkts_idx, vq_size;
1473 uint64_t *async_pending_info;
1475 pkts_idx = vq->async_pkts_idx;
1476 async_pending_info = vq->async_pending_info;
1478 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
1479 vq_size, vq->async_pkts_inflight_n);
1481 while (likely((start_idx & (vq_size - 1)) != pkts_idx)) {
1483 async_pending_info[(start_idx) & (vq_size - 1)] >>
1484 ASYNC_PENDING_INFO_N_SFT;
1487 n_seg -= vq->async_ops.check_completed_copies(dev->vid,
1491 vq->async_pkts_inflight_n = 0;
1492 vq->batch_copy_nb_elems = 0;
1494 vq->shadow_used_idx = shadow_idx;
1495 vq->last_avail_idx = last_idx;
1498 static __rte_noinline uint32_t
1499 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1500 struct vhost_virtqueue *vq, uint16_t queue_id,
1501 struct rte_mbuf **pkts, uint32_t count)
1503 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1504 uint16_t num_buffers;
1505 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1506 uint16_t avail_head, last_idx, shadow_idx;
1508 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1509 struct iovec *vec_pool = vq->vec_pool;
1510 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1511 struct iovec *src_iovec = vec_pool;
1512 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1513 struct rte_vhost_iov_iter *src_it = it_pool;
1514 struct rte_vhost_iov_iter *dst_it = it_pool + 1;
1515 uint16_t n_free_slot, slot_idx;
1518 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1519 last_idx = vq->last_avail_idx;
1520 shadow_idx = vq->shadow_used_idx;
1523 * The ordering between avail index and
1524 * desc reads needs to be enforced.
1528 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1530 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1531 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1532 uint16_t nr_vec = 0;
1534 if (unlikely(reserve_avail_buf_split(dev, vq,
1535 pkt_len, buf_vec, &num_buffers,
1536 avail_head, &nr_vec) < 0)) {
1537 VHOST_LOG_DATA(DEBUG,
1538 "(%d) failed to get enough desc from vring\n",
1540 vq->shadow_used_idx -= num_buffers;
1544 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1545 dev->vid, vq->last_avail_idx,
1546 vq->last_avail_idx + num_buffers);
1548 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1549 buf_vec, nr_vec, num_buffers,
1550 src_iovec, dst_iovec, src_it, dst_it) < 0) {
1551 vq->shadow_used_idx -= num_buffers;
1555 slot_idx = (vq->async_pkts_idx + pkt_idx) & (vq->size - 1);
1556 if (src_it->count) {
1557 async_fill_desc(&tdes[pkt_burst_idx], src_it, dst_it);
1559 vq->async_pending_info[slot_idx] =
1560 num_buffers | (src_it->nr_segs << 16);
1561 src_iovec += src_it->nr_segs;
1562 dst_iovec += dst_it->nr_segs;
1566 vq->async_pending_info[slot_idx] = num_buffers;
1567 vq->async_pkts_inflight_n++;
1570 vq->last_avail_idx += num_buffers;
1572 if (pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1573 (pkt_idx == count - 1 && pkt_burst_idx)) {
1574 n_pkts = vq->async_ops.transfer_data(dev->vid,
1575 queue_id, tdes, 0, pkt_burst_idx);
1576 src_iovec = vec_pool;
1577 dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1579 dst_it = it_pool + 1;
1581 if (unlikely(n_pkts < (int)pkt_burst_idx)) {
1582 vq->async_pkts_inflight_n +=
1583 n_pkts > 0 ? n_pkts : 0;
1584 virtio_dev_rx_async_submit_split_err(dev,
1585 vq, queue_id, last_idx, shadow_idx);
1590 vq->async_pkts_inflight_n += n_pkts;
1594 if (pkt_burst_idx) {
1595 n_pkts = vq->async_ops.transfer_data(dev->vid,
1596 queue_id, tdes, 0, pkt_burst_idx);
1597 if (unlikely(n_pkts < (int)pkt_burst_idx)) {
1598 vq->async_pkts_inflight_n += n_pkts > 0 ? n_pkts : 0;
1599 virtio_dev_rx_async_submit_split_err(dev, vq, queue_id,
1600 last_idx, shadow_idx);
1604 vq->async_pkts_inflight_n += n_pkts;
1607 do_data_copy_enqueue(dev, vq);
1609 n_free_slot = vq->size - vq->async_pkts_idx;
1610 if (n_free_slot > pkt_idx) {
1611 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1612 pkts, pkt_idx * sizeof(uintptr_t));
1613 vq->async_pkts_idx += pkt_idx;
1615 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1616 pkts, n_free_slot * sizeof(uintptr_t));
1617 rte_memcpy(&vq->async_pkts_pending[0],
1619 (pkt_idx - n_free_slot) * sizeof(uintptr_t));
1620 vq->async_pkts_idx = pkt_idx - n_free_slot;
1623 if (likely(vq->shadow_used_idx))
1624 async_flush_shadow_used_ring_split(dev, vq);
1629 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1630 struct rte_mbuf **pkts, uint16_t count)
1632 struct virtio_net *dev = get_device(vid);
1633 struct vhost_virtqueue *vq;
1634 uint16_t n_pkts_cpl, n_pkts_put = 0, n_descs = 0;
1635 uint16_t start_idx, pkts_idx, vq_size;
1636 uint64_t *async_pending_info;
1641 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1642 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1643 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1644 dev->vid, __func__, queue_id);
1648 vq = dev->virtqueue[queue_id];
1650 rte_spinlock_lock(&vq->access_lock);
1652 pkts_idx = vq->async_pkts_idx;
1653 async_pending_info = vq->async_pending_info;
1655 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
1656 vq_size, vq->async_pkts_inflight_n);
1659 vq->async_ops.check_completed_copies(vid, queue_id, 0, count);
1663 while (likely(((start_idx + n_pkts_put) & (vq_size - 1)) != pkts_idx)) {
1664 uint64_t info = async_pending_info[
1665 (start_idx + n_pkts_put) & (vq_size - 1)];
1668 n_descs += info & ASYNC_PENDING_INFO_N_MSK;
1669 n_segs = info >> ASYNC_PENDING_INFO_N_SFT;
1672 if (!n_pkts_cpl || n_pkts_cpl < n_segs) {
1674 n_descs -= info & ASYNC_PENDING_INFO_N_MSK;
1677 (start_idx + n_pkts_put) &
1679 ((n_segs - n_pkts_cpl) <<
1680 ASYNC_PENDING_INFO_N_SFT) |
1681 (info & ASYNC_PENDING_INFO_N_MSK);
1686 n_pkts_cpl -= n_segs;
1691 vq->async_pkts_inflight_n -= n_pkts_put;
1692 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1694 vhost_vring_call_split(dev, vq);
1697 if (start_idx + n_pkts_put <= vq_size) {
1698 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1699 n_pkts_put * sizeof(uintptr_t));
1701 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1702 (vq_size - start_idx) * sizeof(uintptr_t));
1703 rte_memcpy(&pkts[vq_size - start_idx], vq->async_pkts_pending,
1704 (n_pkts_put - vq_size + start_idx) * sizeof(uintptr_t));
1707 rte_spinlock_unlock(&vq->access_lock);
1712 static __rte_always_inline uint32_t
1713 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1714 struct rte_mbuf **pkts, uint32_t count)
1716 struct vhost_virtqueue *vq;
1718 bool drawback = false;
1720 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1721 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1722 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1723 dev->vid, __func__, queue_id);
1727 vq = dev->virtqueue[queue_id];
1729 rte_spinlock_lock(&vq->access_lock);
1731 if (unlikely(vq->enabled == 0))
1732 goto out_access_unlock;
1734 if (unlikely(!vq->async_registered)) {
1736 goto out_access_unlock;
1739 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1740 vhost_user_iotlb_rd_lock(vq);
1742 if (unlikely(vq->access_ok == 0))
1743 if (unlikely(vring_translate(dev, vq) < 0))
1746 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1750 /* TODO: packed queue not implemented */
1751 if (vq_is_packed(dev))
1754 nb_tx = virtio_dev_rx_async_submit_split(dev,
1755 vq, queue_id, pkts, count);
1758 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1759 vhost_user_iotlb_rd_unlock(vq);
1762 rte_spinlock_unlock(&vq->access_lock);
1765 return rte_vhost_enqueue_burst(dev->vid, queue_id, pkts, count);
1771 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
1772 struct rte_mbuf **pkts, uint16_t count)
1774 struct virtio_net *dev = get_device(vid);
1779 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1781 "(%d) %s: built-in vhost net backend is disabled.\n",
1782 dev->vid, __func__);
1786 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
1790 virtio_net_with_host_offload(struct virtio_net *dev)
1793 ((1ULL << VIRTIO_NET_F_CSUM) |
1794 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1795 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1796 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1797 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1804 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1806 struct rte_ipv4_hdr *ipv4_hdr;
1807 struct rte_ipv6_hdr *ipv6_hdr;
1808 void *l3_hdr = NULL;
1809 struct rte_ether_hdr *eth_hdr;
1812 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1814 m->l2_len = sizeof(struct rte_ether_hdr);
1815 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1817 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1818 struct rte_vlan_hdr *vlan_hdr =
1819 (struct rte_vlan_hdr *)(eth_hdr + 1);
1821 m->l2_len += sizeof(struct rte_vlan_hdr);
1822 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1825 l3_hdr = (char *)eth_hdr + m->l2_len;
1827 switch (ethertype) {
1828 case RTE_ETHER_TYPE_IPV4:
1830 *l4_proto = ipv4_hdr->next_proto_id;
1831 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1832 *l4_hdr = (char *)l3_hdr + m->l3_len;
1833 m->ol_flags |= PKT_TX_IPV4;
1835 case RTE_ETHER_TYPE_IPV6:
1837 *l4_proto = ipv6_hdr->proto;
1838 m->l3_len = sizeof(struct rte_ipv6_hdr);
1839 *l4_hdr = (char *)l3_hdr + m->l3_len;
1840 m->ol_flags |= PKT_TX_IPV6;
1850 static __rte_always_inline void
1851 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1853 uint16_t l4_proto = 0;
1854 void *l4_hdr = NULL;
1855 struct rte_tcp_hdr *tcp_hdr = NULL;
1857 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1860 parse_ethernet(m, &l4_proto, &l4_hdr);
1861 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1862 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1863 switch (hdr->csum_offset) {
1864 case (offsetof(struct rte_tcp_hdr, cksum)):
1865 if (l4_proto == IPPROTO_TCP)
1866 m->ol_flags |= PKT_TX_TCP_CKSUM;
1868 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1869 if (l4_proto == IPPROTO_UDP)
1870 m->ol_flags |= PKT_TX_UDP_CKSUM;
1872 case (offsetof(struct rte_sctp_hdr, cksum)):
1873 if (l4_proto == IPPROTO_SCTP)
1874 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1882 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1883 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1884 case VIRTIO_NET_HDR_GSO_TCPV4:
1885 case VIRTIO_NET_HDR_GSO_TCPV6:
1887 m->ol_flags |= PKT_TX_TCP_SEG;
1888 m->tso_segsz = hdr->gso_size;
1889 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1891 case VIRTIO_NET_HDR_GSO_UDP:
1892 m->ol_flags |= PKT_TX_UDP_SEG;
1893 m->tso_segsz = hdr->gso_size;
1894 m->l4_len = sizeof(struct rte_udp_hdr);
1897 VHOST_LOG_DATA(WARNING,
1898 "unsupported gso type %u.\n", hdr->gso_type);
1904 static __rte_noinline void
1905 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1906 struct buf_vector *buf_vec)
1909 uint64_t remain = sizeof(struct virtio_net_hdr);
1911 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1914 len = RTE_MIN(remain, buf_vec->buf_len);
1915 src = buf_vec->buf_addr;
1916 rte_memcpy((void *)(uintptr_t)dst,
1917 (void *)(uintptr_t)src, len);
1925 static __rte_always_inline int
1926 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1927 struct buf_vector *buf_vec, uint16_t nr_vec,
1928 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1930 uint32_t buf_avail, buf_offset;
1931 uint64_t buf_addr, buf_iova, buf_len;
1932 uint32_t mbuf_avail, mbuf_offset;
1934 struct rte_mbuf *cur = m, *prev = m;
1935 struct virtio_net_hdr tmp_hdr;
1936 struct virtio_net_hdr *hdr = NULL;
1937 /* A counter to avoid desc dead loop chain */
1938 uint16_t vec_idx = 0;
1939 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1942 buf_addr = buf_vec[vec_idx].buf_addr;
1943 buf_iova = buf_vec[vec_idx].buf_iova;
1944 buf_len = buf_vec[vec_idx].buf_len;
1946 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1951 if (virtio_net_with_host_offload(dev)) {
1952 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1954 * No luck, the virtio-net header doesn't fit
1955 * in a contiguous virtual area.
1957 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1960 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1965 * A virtio driver normally uses at least 2 desc buffers
1966 * for Tx: the first for storing the header, and others
1967 * for storing the data.
1969 if (unlikely(buf_len < dev->vhost_hlen)) {
1970 buf_offset = dev->vhost_hlen - buf_len;
1972 buf_addr = buf_vec[vec_idx].buf_addr;
1973 buf_iova = buf_vec[vec_idx].buf_iova;
1974 buf_len = buf_vec[vec_idx].buf_len;
1975 buf_avail = buf_len - buf_offset;
1976 } else if (buf_len == dev->vhost_hlen) {
1977 if (unlikely(++vec_idx >= nr_vec))
1979 buf_addr = buf_vec[vec_idx].buf_addr;
1980 buf_iova = buf_vec[vec_idx].buf_iova;
1981 buf_len = buf_vec[vec_idx].buf_len;
1984 buf_avail = buf_len;
1986 buf_offset = dev->vhost_hlen;
1987 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1991 (uintptr_t)(buf_addr + buf_offset),
1992 (uint32_t)buf_avail, 0);
1995 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1999 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2002 * A desc buf might across two host physical pages that are
2003 * not continuous. In such case (gpa_to_hpa returns 0), data
2004 * will be copied even though zero copy is enabled.
2006 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
2007 buf_iova + buf_offset, cpy_len)))) {
2008 cur->data_len = cpy_len;
2011 (void *)(uintptr_t)(buf_addr + buf_offset);
2012 cur->buf_iova = hpa;
2015 * In zero copy mode, one mbuf can only reference data
2016 * for one or partial of one desc buff.
2018 mbuf_avail = cpy_len;
2020 if (likely(cpy_len > MAX_BATCH_LEN ||
2021 vq->batch_copy_nb_elems >= vq->size ||
2022 (hdr && cur == m))) {
2023 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2025 (void *)((uintptr_t)(buf_addr +
2029 batch_copy[vq->batch_copy_nb_elems].dst =
2030 rte_pktmbuf_mtod_offset(cur, void *,
2032 batch_copy[vq->batch_copy_nb_elems].src =
2033 (void *)((uintptr_t)(buf_addr +
2035 batch_copy[vq->batch_copy_nb_elems].len =
2037 vq->batch_copy_nb_elems++;
2041 mbuf_avail -= cpy_len;
2042 mbuf_offset += cpy_len;
2043 buf_avail -= cpy_len;
2044 buf_offset += cpy_len;
2046 /* This buf reaches to its end, get the next one */
2047 if (buf_avail == 0) {
2048 if (++vec_idx >= nr_vec)
2051 buf_addr = buf_vec[vec_idx].buf_addr;
2052 buf_iova = buf_vec[vec_idx].buf_iova;
2053 buf_len = buf_vec[vec_idx].buf_len;
2056 buf_avail = buf_len;
2058 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2059 (uint32_t)buf_avail, 0);
2063 * This mbuf reaches to its end, get a new one
2064 * to hold more data.
2066 if (mbuf_avail == 0) {
2067 cur = rte_pktmbuf_alloc(mbuf_pool);
2068 if (unlikely(cur == NULL)) {
2069 VHOST_LOG_DATA(ERR, "Failed to "
2070 "allocate memory for mbuf.\n");
2074 if (unlikely(dev->dequeue_zero_copy))
2075 rte_mbuf_refcnt_update(cur, 1);
2078 prev->data_len = mbuf_offset;
2080 m->pkt_len += mbuf_offset;
2084 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2088 prev->data_len = mbuf_offset;
2089 m->pkt_len += mbuf_offset;
2092 vhost_dequeue_offload(hdr, m);
2099 static __rte_always_inline struct zcopy_mbuf *
2100 get_zmbuf(struct vhost_virtqueue *vq)
2106 /* search [last_zmbuf_idx, zmbuf_size) */
2107 i = vq->last_zmbuf_idx;
2108 last = vq->zmbuf_size;
2111 for (; i < last; i++) {
2112 if (vq->zmbufs[i].in_use == 0) {
2113 vq->last_zmbuf_idx = i + 1;
2114 vq->zmbufs[i].in_use = 1;
2115 return &vq->zmbufs[i];
2121 /* search [0, last_zmbuf_idx) */
2123 last = vq->last_zmbuf_idx;
2131 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2137 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2139 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2140 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2145 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
2146 * required, otherwise store shinfo in the new buffer.
2148 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
2149 shinfo = rte_pktmbuf_mtod(pkt,
2150 struct rte_mbuf_ext_shared_info *);
2152 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2153 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2156 if (unlikely(total_len > UINT16_MAX))
2159 buf_len = total_len;
2160 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2161 if (unlikely(buf == NULL))
2164 /* Initialize shinfo */
2166 shinfo->free_cb = virtio_dev_extbuf_free;
2167 shinfo->fcb_opaque = buf;
2168 rte_mbuf_ext_refcnt_set(shinfo, 1);
2170 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2171 virtio_dev_extbuf_free, buf);
2172 if (unlikely(shinfo == NULL)) {
2174 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2179 iova = rte_malloc_virt2iova(buf);
2180 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2181 rte_pktmbuf_reset_headroom(pkt);
2187 * Allocate a host supported pktmbuf.
2189 static __rte_always_inline struct rte_mbuf *
2190 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
2193 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
2195 if (unlikely(pkt == NULL)) {
2197 "Failed to allocate memory for mbuf.\n");
2201 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2204 /* attach an external buffer if supported */
2205 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2208 /* check if chained buffers are allowed */
2209 if (!dev->linearbuf)
2212 /* Data doesn't fit into the buffer and the host supports
2213 * only linear buffers
2215 rte_pktmbuf_free(pkt);
2220 static __rte_noinline uint16_t
2221 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2222 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2225 uint16_t free_entries;
2226 uint16_t dropped = 0;
2227 static bool allocerr_warned;
2229 if (unlikely(dev->dequeue_zero_copy)) {
2230 struct zcopy_mbuf *zmbuf, *next;
2232 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
2233 zmbuf != NULL; zmbuf = next) {
2234 next = TAILQ_NEXT(zmbuf, next);
2236 if (mbuf_is_consumed(zmbuf->mbuf)) {
2237 update_shadow_used_ring_split(vq,
2238 zmbuf->desc_idx, 0);
2239 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2240 restore_mbuf(zmbuf->mbuf);
2241 rte_pktmbuf_free(zmbuf->mbuf);
2247 if (likely(vq->shadow_used_idx)) {
2248 flush_shadow_used_ring_split(dev, vq);
2249 vhost_vring_call_split(dev, vq);
2254 * The ordering between avail index and
2255 * desc reads needs to be enforced.
2257 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2259 if (free_entries == 0)
2262 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2264 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2266 count = RTE_MIN(count, MAX_PKT_BURST);
2267 count = RTE_MIN(count, free_entries);
2268 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2271 for (i = 0; i < count; i++) {
2272 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2275 uint16_t nr_vec = 0;
2278 if (unlikely(fill_vec_buf_split(dev, vq,
2279 vq->last_avail_idx + i,
2281 &head_idx, &buf_len,
2282 VHOST_ACCESS_RO) < 0))
2285 if (likely(dev->dequeue_zero_copy == 0))
2286 update_shadow_used_ring_split(vq, head_idx, 0);
2288 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2289 if (unlikely(pkts[i] == NULL)) {
2291 * mbuf allocation fails for jumbo packets when external
2292 * buffer allocation is not allowed and linear buffer
2293 * is required. Drop this packet.
2295 if (!allocerr_warned) {
2297 "Failed mbuf alloc of size %d from %s on %s.\n",
2298 buf_len, mbuf_pool->name, dev->ifname);
2299 allocerr_warned = true;
2306 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2308 if (unlikely(err)) {
2309 rte_pktmbuf_free(pkts[i]);
2310 if (!allocerr_warned) {
2312 "Failed to copy desc to mbuf on %s.\n",
2314 allocerr_warned = true;
2321 if (unlikely(dev->dequeue_zero_copy)) {
2322 struct zcopy_mbuf *zmbuf;
2324 zmbuf = get_zmbuf(vq);
2326 rte_pktmbuf_free(pkts[i]);
2331 zmbuf->mbuf = pkts[i];
2332 zmbuf->desc_idx = head_idx;
2335 * Pin lock the mbuf; we will check later to see
2336 * whether the mbuf is freed (when we are the last
2337 * user) or not. If that's the case, we then could
2338 * update the used ring safely.
2340 rte_mbuf_refcnt_update(pkts[i], 1);
2343 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2346 vq->last_avail_idx += i;
2348 if (likely(dev->dequeue_zero_copy == 0)) {
2349 do_data_copy_dequeue(vq);
2350 if (unlikely(i < count))
2351 vq->shadow_used_idx = i;
2352 if (likely(vq->shadow_used_idx)) {
2353 flush_shadow_used_ring_split(dev, vq);
2354 vhost_vring_call_split(dev, vq);
2358 return (i - dropped);
2361 static __rte_always_inline int
2362 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2363 struct vhost_virtqueue *vq,
2364 struct rte_mempool *mbuf_pool,
2365 struct rte_mbuf **pkts,
2367 uintptr_t *desc_addrs,
2370 bool wrap = vq->avail_wrap_counter;
2371 struct vring_packed_desc *descs = vq->desc_packed;
2372 struct virtio_net_hdr *hdr;
2373 uint64_t lens[PACKED_BATCH_SIZE];
2374 uint64_t buf_lens[PACKED_BATCH_SIZE];
2375 uint32_t buf_offset = dev->vhost_hlen;
2378 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2380 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2383 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2384 flags = descs[avail_idx + i].flags;
2385 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2386 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2387 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2393 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2394 lens[i] = descs[avail_idx + i].len;
2396 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2397 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2398 descs[avail_idx + i].addr,
2399 &lens[i], VHOST_ACCESS_RW);
2402 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2403 if (unlikely(!desc_addrs[i]))
2405 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2409 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2410 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
2415 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2416 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2418 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2419 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2423 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2424 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
2425 pkts[i]->data_len = pkts[i]->pkt_len;
2426 ids[i] = descs[avail_idx + i].id;
2429 if (virtio_net_with_host_offload(dev)) {
2430 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2431 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2432 vhost_dequeue_offload(hdr, pkts[i]);
2439 for (i = 0; i < PACKED_BATCH_SIZE; i++)
2440 rte_pktmbuf_free(pkts[i]);
2445 static __rte_always_inline int
2446 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2447 struct vhost_virtqueue *vq,
2448 struct rte_mempool *mbuf_pool,
2449 struct rte_mbuf **pkts)
2451 uint16_t avail_idx = vq->last_avail_idx;
2452 uint32_t buf_offset = dev->vhost_hlen;
2453 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2454 uint16_t ids[PACKED_BATCH_SIZE];
2457 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
2458 avail_idx, desc_addrs, ids))
2461 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2462 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2464 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2465 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2466 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2469 if (virtio_net_is_inorder(dev))
2470 vhost_shadow_dequeue_batch_packed_inorder(vq,
2471 ids[PACKED_BATCH_SIZE - 1]);
2473 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2475 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2480 static __rte_always_inline int
2481 vhost_dequeue_single_packed(struct virtio_net *dev,
2482 struct vhost_virtqueue *vq,
2483 struct rte_mempool *mbuf_pool,
2484 struct rte_mbuf **pkts,
2486 uint16_t *desc_count)
2488 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2490 uint16_t nr_vec = 0;
2492 static bool allocerr_warned;
2494 if (unlikely(fill_vec_buf_packed(dev, vq,
2495 vq->last_avail_idx, desc_count,
2498 VHOST_ACCESS_RO) < 0))
2501 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2502 if (unlikely(*pkts == NULL)) {
2503 if (!allocerr_warned) {
2505 "Failed mbuf alloc of size %d from %s on %s.\n",
2506 buf_len, mbuf_pool->name, dev->ifname);
2507 allocerr_warned = true;
2512 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
2514 if (unlikely(err)) {
2515 if (!allocerr_warned) {
2517 "Failed to copy desc to mbuf on %s.\n",
2519 allocerr_warned = true;
2521 rte_pktmbuf_free(*pkts);
2528 static __rte_always_inline int
2529 virtio_dev_tx_single_packed(struct virtio_net *dev,
2530 struct vhost_virtqueue *vq,
2531 struct rte_mempool *mbuf_pool,
2532 struct rte_mbuf **pkts)
2535 uint16_t buf_id, desc_count = 0;
2538 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2541 if (likely(desc_count > 0)) {
2542 if (virtio_net_is_inorder(dev))
2543 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2546 vhost_shadow_dequeue_single_packed(vq, buf_id,
2549 vq_inc_last_avail_packed(vq, desc_count);
2555 static __rte_always_inline int
2556 virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,
2557 struct vhost_virtqueue *vq,
2558 struct rte_mempool *mbuf_pool,
2559 struct rte_mbuf **pkts)
2561 struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE];
2562 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2563 uint16_t ids[PACKED_BATCH_SIZE];
2566 uint16_t avail_idx = vq->last_avail_idx;
2568 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
2569 avail_idx, desc_addrs, ids))
2572 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2573 zmbufs[i] = get_zmbuf(vq);
2575 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2580 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2581 zmbufs[i]->mbuf = pkts[i];
2582 zmbufs[i]->desc_idx = ids[i];
2583 zmbufs[i]->desc_count = 1;
2586 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2587 rte_mbuf_refcnt_update(pkts[i], 1);
2589 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2590 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbufs[i], next);
2592 vq->nr_zmbuf += PACKED_BATCH_SIZE;
2593 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2598 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2599 rte_pktmbuf_free(pkts[i]);
2604 static __rte_always_inline int
2605 virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev,
2606 struct vhost_virtqueue *vq,
2607 struct rte_mempool *mbuf_pool,
2608 struct rte_mbuf **pkts)
2610 uint16_t buf_id, desc_count;
2611 struct zcopy_mbuf *zmbuf;
2613 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2617 zmbuf = get_zmbuf(vq);
2619 rte_pktmbuf_free(*pkts);
2622 zmbuf->mbuf = *pkts;
2623 zmbuf->desc_idx = buf_id;
2624 zmbuf->desc_count = desc_count;
2626 rte_mbuf_refcnt_update(*pkts, 1);
2629 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2631 vq_inc_last_avail_packed(vq, desc_count);
2635 static __rte_always_inline void
2636 free_zmbuf(struct vhost_virtqueue *vq)
2638 struct zcopy_mbuf *next = NULL;
2639 struct zcopy_mbuf *zmbuf;
2641 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
2642 zmbuf != NULL; zmbuf = next) {
2643 next = TAILQ_NEXT(zmbuf, next);
2645 uint16_t last_used_idx = vq->last_used_idx;
2647 if (mbuf_is_consumed(zmbuf->mbuf)) {
2649 flags = vq->desc_packed[last_used_idx].flags;
2650 if (vq->used_wrap_counter) {
2651 flags |= VRING_DESC_F_USED;
2652 flags |= VRING_DESC_F_AVAIL;
2654 flags &= ~VRING_DESC_F_USED;
2655 flags &= ~VRING_DESC_F_AVAIL;
2658 vq->desc_packed[last_used_idx].id = zmbuf->desc_idx;
2659 vq->desc_packed[last_used_idx].len = 0;
2662 vq->desc_packed[last_used_idx].flags = flags;
2664 vq_inc_last_used_packed(vq, zmbuf->desc_count);
2666 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2667 restore_mbuf(zmbuf->mbuf);
2668 rte_pktmbuf_free(zmbuf->mbuf);
2675 static __rte_noinline uint16_t
2676 virtio_dev_tx_packed_zmbuf(struct virtio_net *dev,
2677 struct vhost_virtqueue *__rte_restrict vq,
2678 struct rte_mempool *mbuf_pool,
2679 struct rte_mbuf **__rte_restrict pkts,
2682 uint32_t pkt_idx = 0;
2683 uint32_t remained = count;
2688 if (remained >= PACKED_BATCH_SIZE) {
2689 if (!virtio_dev_tx_batch_packed_zmbuf(dev, vq,
2690 mbuf_pool, &pkts[pkt_idx])) {
2691 pkt_idx += PACKED_BATCH_SIZE;
2692 remained -= PACKED_BATCH_SIZE;
2697 if (virtio_dev_tx_single_packed_zmbuf(dev, vq, mbuf_pool,
2706 vhost_vring_call_packed(dev, vq);
2711 static __rte_noinline uint16_t
2712 virtio_dev_tx_packed(struct virtio_net *dev,
2713 struct vhost_virtqueue *__rte_restrict vq,
2714 struct rte_mempool *mbuf_pool,
2715 struct rte_mbuf **__rte_restrict pkts,
2718 uint32_t pkt_idx = 0;
2719 uint32_t remained = count;
2722 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2724 if (remained >= PACKED_BATCH_SIZE) {
2725 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2727 pkt_idx += PACKED_BATCH_SIZE;
2728 remained -= PACKED_BATCH_SIZE;
2733 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2741 if (vq->shadow_used_idx) {
2742 do_data_copy_dequeue(vq);
2744 vhost_flush_dequeue_shadow_packed(dev, vq);
2745 vhost_vring_call_packed(dev, vq);
2752 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2753 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2755 struct virtio_net *dev;
2756 struct rte_mbuf *rarp_mbuf = NULL;
2757 struct vhost_virtqueue *vq;
2758 int16_t success = 1;
2760 dev = get_device(vid);
2764 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2766 "(%d) %s: built-in vhost net backend is disabled.\n",
2767 dev->vid, __func__);
2771 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2773 "(%d) %s: invalid virtqueue idx %d.\n",
2774 dev->vid, __func__, queue_id);
2778 vq = dev->virtqueue[queue_id];
2780 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2783 if (unlikely(vq->enabled == 0)) {
2785 goto out_access_unlock;
2788 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2789 vhost_user_iotlb_rd_lock(vq);
2791 if (unlikely(vq->access_ok == 0))
2792 if (unlikely(vring_translate(dev, vq) < 0)) {
2798 * Construct a RARP broadcast packet, and inject it to the "pkts"
2799 * array, to looks like that guest actually send such packet.
2801 * Check user_send_rarp() for more information.
2803 * broadcast_rarp shares a cacheline in the virtio_net structure
2804 * with some fields that are accessed during enqueue and
2805 * __atomic_compare_exchange_n causes a write if performed compare
2806 * and exchange. This could result in false sharing between enqueue
2809 * Prevent unnecessary false sharing by reading broadcast_rarp first
2810 * and only performing compare and exchange if the read indicates it
2811 * is likely to be set.
2813 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2814 __atomic_compare_exchange_n(&dev->broadcast_rarp,
2815 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2817 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2818 if (rarp_mbuf == NULL) {
2819 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2826 if (vq_is_packed(dev)) {
2827 if (unlikely(dev->dequeue_zero_copy))
2828 count = virtio_dev_tx_packed_zmbuf(dev, vq, mbuf_pool,
2831 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts,
2834 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2837 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2838 vhost_user_iotlb_rd_unlock(vq);
2841 rte_spinlock_unlock(&vq->access_lock);
2843 if (unlikely(rarp_mbuf != NULL)) {
2845 * Inject it to the head of "pkts" array, so that switch's mac
2846 * learning table will get updated first.
2848 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2849 pkts[0] = rarp_mbuf;