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;
989 if (unlikely(m == NULL)) {
994 cpy_threshold = vq->async_threshold;
996 buf_addr = buf_vec[vec_idx].buf_addr;
997 buf_iova = buf_vec[vec_idx].buf_iova;
998 buf_len = buf_vec[vec_idx].buf_len;
1000 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1006 hdr_addr = buf_addr;
1007 if (unlikely(buf_len < dev->vhost_hlen))
1010 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1012 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1013 dev->vid, num_buffers);
1015 if (unlikely(buf_len < dev->vhost_hlen)) {
1016 buf_offset = dev->vhost_hlen - buf_len;
1018 buf_addr = buf_vec[vec_idx].buf_addr;
1019 buf_iova = buf_vec[vec_idx].buf_iova;
1020 buf_len = buf_vec[vec_idx].buf_len;
1021 buf_avail = buf_len - buf_offset;
1023 buf_offset = dev->vhost_hlen;
1024 buf_avail = buf_len - dev->vhost_hlen;
1027 mbuf_avail = rte_pktmbuf_data_len(m);
1030 while (mbuf_avail != 0 || m->next != NULL) {
1031 /* done with current buf, get the next one */
1032 if (buf_avail == 0) {
1034 if (unlikely(vec_idx >= nr_vec)) {
1039 buf_addr = buf_vec[vec_idx].buf_addr;
1040 buf_iova = buf_vec[vec_idx].buf_iova;
1041 buf_len = buf_vec[vec_idx].buf_len;
1044 buf_avail = buf_len;
1047 /* done with current mbuf, get the next one */
1048 if (mbuf_avail == 0) {
1052 mbuf_avail = rte_pktmbuf_data_len(m);
1056 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1057 if (rxvq_is_mergeable(dev))
1058 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1061 if (unlikely(hdr == &tmp_hdr)) {
1062 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1064 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1065 dev->vhost_hlen, 0);
1066 vhost_log_cache_write_iova(dev, vq,
1067 buf_vec[0].buf_iova,
1074 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1076 while (unlikely(cpy_len && cpy_len >= cpy_threshold)) {
1077 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1078 buf_iova + buf_offset,
1079 cpy_len, &mapped_len);
1081 if (unlikely(!hpa || mapped_len < cpy_threshold))
1084 async_fill_vec(src_iovec + tvec_idx,
1085 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1086 mbuf_offset), (size_t)mapped_len);
1088 async_fill_vec(dst_iovec + tvec_idx,
1089 hpa, (size_t)mapped_len);
1091 tlen += (uint32_t)mapped_len;
1092 cpy_len -= (uint32_t)mapped_len;
1093 mbuf_avail -= (uint32_t)mapped_len;
1094 mbuf_offset += (uint32_t)mapped_len;
1095 buf_avail -= (uint32_t)mapped_len;
1096 buf_offset += (uint32_t)mapped_len;
1100 if (likely(cpy_len)) {
1101 if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1103 (void *)((uintptr_t)(buf_addr + buf_offset)),
1104 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1108 (uintptr_t)(buf_addr + buf_offset),
1111 batch_copy[vq->batch_copy_nb_elems].dst =
1112 (void *)((uintptr_t)(buf_addr + buf_offset));
1113 batch_copy[vq->batch_copy_nb_elems].src =
1114 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1115 batch_copy[vq->batch_copy_nb_elems].log_addr =
1116 buf_iova + buf_offset;
1117 batch_copy[vq->batch_copy_nb_elems].len =
1119 vq->batch_copy_nb_elems++;
1122 mbuf_avail -= cpy_len;
1123 mbuf_offset += cpy_len;
1124 buf_avail -= cpy_len;
1125 buf_offset += cpy_len;
1131 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1132 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1137 static __rte_always_inline int
1138 vhost_enqueue_single_packed(struct virtio_net *dev,
1139 struct vhost_virtqueue *vq,
1140 struct rte_mbuf *pkt,
1141 struct buf_vector *buf_vec,
1144 uint16_t nr_vec = 0;
1145 uint16_t avail_idx = vq->last_avail_idx;
1146 uint16_t max_tries, tries = 0;
1147 uint16_t buf_id = 0;
1149 uint16_t desc_count;
1150 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1151 uint16_t num_buffers = 0;
1152 uint32_t buffer_len[vq->size];
1153 uint16_t buffer_buf_id[vq->size];
1154 uint16_t buffer_desc_count[vq->size];
1156 if (rxvq_is_mergeable(dev))
1157 max_tries = vq->size - 1;
1163 * if we tried all available ring items, and still
1164 * can't get enough buf, it means something abnormal
1167 if (unlikely(++tries > max_tries))
1170 if (unlikely(fill_vec_buf_packed(dev, vq,
1171 avail_idx, &desc_count,
1174 VHOST_ACCESS_RW) < 0))
1177 len = RTE_MIN(len, size);
1180 buffer_len[num_buffers] = len;
1181 buffer_buf_id[num_buffers] = buf_id;
1182 buffer_desc_count[num_buffers] = desc_count;
1185 *nr_descs += desc_count;
1186 avail_idx += desc_count;
1187 if (avail_idx >= vq->size)
1188 avail_idx -= vq->size;
1191 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1194 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1195 buffer_desc_count, num_buffers);
1200 static __rte_noinline uint32_t
1201 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1202 struct rte_mbuf **pkts, uint32_t count)
1204 uint32_t pkt_idx = 0;
1205 uint16_t num_buffers;
1206 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1207 uint16_t avail_head;
1210 * The ordering between avail index and
1211 * desc reads needs to be enforced.
1213 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1215 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1217 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1218 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1219 uint16_t nr_vec = 0;
1221 if (unlikely(reserve_avail_buf_split(dev, vq,
1222 pkt_len, buf_vec, &num_buffers,
1223 avail_head, &nr_vec) < 0)) {
1224 VHOST_LOG_DATA(DEBUG,
1225 "(%d) failed to get enough desc from vring\n",
1227 vq->shadow_used_idx -= num_buffers;
1231 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1232 dev->vid, vq->last_avail_idx,
1233 vq->last_avail_idx + num_buffers);
1235 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1238 vq->shadow_used_idx -= num_buffers;
1242 vq->last_avail_idx += num_buffers;
1245 do_data_copy_enqueue(dev, vq);
1247 if (likely(vq->shadow_used_idx)) {
1248 flush_shadow_used_ring_split(dev, vq);
1249 vhost_vring_call_split(dev, vq);
1255 static __rte_always_inline int
1256 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1257 struct vhost_virtqueue *vq,
1258 struct rte_mbuf **pkts)
1260 bool wrap_counter = vq->avail_wrap_counter;
1261 struct vring_packed_desc *descs = vq->desc_packed;
1262 uint16_t avail_idx = vq->last_avail_idx;
1263 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1264 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1265 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1266 uint64_t lens[PACKED_BATCH_SIZE];
1267 uint16_t ids[PACKED_BATCH_SIZE];
1270 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1273 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1276 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1277 if (unlikely(pkts[i]->next != NULL))
1279 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;
1347 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1349 VHOST_LOG_DATA(DEBUG,
1350 "(%d) failed to get enough desc from vring\n",
1355 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1356 dev->vid, vq->last_avail_idx,
1357 vq->last_avail_idx + nr_descs);
1359 vq_inc_last_avail_packed(vq, nr_descs);
1364 static __rte_noinline uint32_t
1365 virtio_dev_rx_packed(struct virtio_net *dev,
1366 struct vhost_virtqueue *__rte_restrict vq,
1367 struct rte_mbuf **__rte_restrict pkts,
1370 uint32_t pkt_idx = 0;
1371 uint32_t remained = count;
1374 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1376 if (remained >= PACKED_BATCH_SIZE) {
1377 if (!virtio_dev_rx_batch_packed(dev, vq,
1379 pkt_idx += PACKED_BATCH_SIZE;
1380 remained -= PACKED_BATCH_SIZE;
1385 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1390 } while (pkt_idx < count);
1392 if (vq->shadow_used_idx) {
1393 do_data_copy_enqueue(dev, vq);
1394 vhost_flush_enqueue_shadow_packed(dev, vq);
1398 vhost_vring_call_packed(dev, vq);
1403 static __rte_always_inline uint32_t
1404 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1405 struct rte_mbuf **pkts, uint32_t count)
1407 struct vhost_virtqueue *vq;
1410 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1411 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1412 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1413 dev->vid, __func__, queue_id);
1417 vq = dev->virtqueue[queue_id];
1419 rte_spinlock_lock(&vq->access_lock);
1421 if (unlikely(vq->enabled == 0))
1422 goto out_access_unlock;
1424 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1425 vhost_user_iotlb_rd_lock(vq);
1427 if (unlikely(vq->access_ok == 0))
1428 if (unlikely(vring_translate(dev, vq) < 0))
1431 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1435 if (vq_is_packed(dev))
1436 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1438 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1441 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1442 vhost_user_iotlb_rd_unlock(vq);
1445 rte_spinlock_unlock(&vq->access_lock);
1451 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1452 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1454 struct virtio_net *dev = get_device(vid);
1459 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1461 "(%d) %s: built-in vhost net backend is disabled.\n",
1462 dev->vid, __func__);
1466 return virtio_dev_rx(dev, queue_id, pkts, count);
1469 static __rte_always_inline uint16_t
1470 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1471 uint16_t vq_size, uint16_t n_inflight)
1473 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1474 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1477 static __rte_noinline uint32_t
1478 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1479 struct vhost_virtqueue *vq, uint16_t queue_id,
1480 struct rte_mbuf **pkts, uint32_t count)
1482 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1483 uint16_t num_buffers;
1484 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1485 uint16_t avail_head;
1487 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1488 struct iovec *vec_pool = vq->vec_pool;
1489 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1490 struct iovec *src_iovec = vec_pool;
1491 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1492 struct rte_vhost_iov_iter *src_it = it_pool;
1493 struct rte_vhost_iov_iter *dst_it = it_pool + 1;
1494 uint16_t n_free_slot, slot_idx;
1495 uint16_t pkt_err = 0;
1496 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1499 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1502 * The ordering between avail index and
1503 * desc reads needs to be enforced.
1507 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1509 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1510 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1511 uint16_t nr_vec = 0;
1513 if (unlikely(reserve_avail_buf_split(dev, vq,
1514 pkt_len, buf_vec, &num_buffers,
1515 avail_head, &nr_vec) < 0)) {
1516 VHOST_LOG_DATA(DEBUG,
1517 "(%d) failed to get enough desc from vring\n",
1519 vq->shadow_used_idx -= num_buffers;
1523 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1524 dev->vid, vq->last_avail_idx,
1525 vq->last_avail_idx + num_buffers);
1527 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1528 buf_vec, nr_vec, num_buffers,
1529 src_iovec, dst_iovec, src_it, dst_it) < 0) {
1530 vq->shadow_used_idx -= num_buffers;
1534 slot_idx = (vq->async_pkts_idx + pkt_idx) & (vq->size - 1);
1535 if (src_it->count) {
1536 async_fill_desc(&tdes[pkt_burst_idx], src_it, dst_it);
1538 pkts_info[slot_idx].descs = num_buffers;
1539 pkts_info[slot_idx].segs = src_it->nr_segs;
1540 src_iovec += src_it->nr_segs;
1541 dst_iovec += dst_it->nr_segs;
1545 pkts_info[slot_idx].info = num_buffers;
1546 vq->async_pkts_inflight_n++;
1549 vq->last_avail_idx += num_buffers;
1551 if (pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1552 (pkt_idx == count - 1 && pkt_burst_idx)) {
1553 n_pkts = vq->async_ops.transfer_data(dev->vid,
1554 queue_id, tdes, 0, pkt_burst_idx);
1555 src_iovec = vec_pool;
1556 dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1558 dst_it = it_pool + 1;
1559 vq->async_pkts_inflight_n += n_pkts;
1561 if (unlikely(n_pkts < (int)pkt_burst_idx)) {
1563 * log error packets number here and do actual
1564 * error processing when applications poll
1567 pkt_err = pkt_burst_idx - n_pkts;
1576 if (pkt_burst_idx) {
1577 n_pkts = vq->async_ops.transfer_data(dev->vid,
1578 queue_id, tdes, 0, pkt_burst_idx);
1579 vq->async_pkts_inflight_n += n_pkts;
1581 if (unlikely(n_pkts < (int)pkt_burst_idx))
1582 pkt_err = pkt_burst_idx - n_pkts;
1585 do_data_copy_enqueue(dev, vq);
1587 while (unlikely(pkt_err && pkt_idx)) {
1588 if (pkts_info[slot_idx].segs)
1590 vq->last_avail_idx -= pkts_info[slot_idx].descs;
1591 vq->shadow_used_idx -= pkts_info[slot_idx].descs;
1592 vq->async_pkts_inflight_n--;
1593 slot_idx = (slot_idx - 1) & (vq->size - 1);
1597 n_free_slot = vq->size - vq->async_pkts_idx;
1598 if (n_free_slot > pkt_idx) {
1599 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1600 pkts, pkt_idx * sizeof(uintptr_t));
1601 vq->async_pkts_idx += pkt_idx;
1603 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1604 pkts, n_free_slot * sizeof(uintptr_t));
1605 rte_memcpy(&vq->async_pkts_pending[0],
1607 (pkt_idx - n_free_slot) * sizeof(uintptr_t));
1608 vq->async_pkts_idx = pkt_idx - n_free_slot;
1611 if (likely(vq->shadow_used_idx))
1612 async_flush_shadow_used_ring_split(dev, vq);
1617 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1618 struct rte_mbuf **pkts, uint16_t count)
1620 struct virtio_net *dev = get_device(vid);
1621 struct vhost_virtqueue *vq;
1622 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0;
1623 uint16_t start_idx, pkts_idx, vq_size;
1624 uint16_t n_inflight;
1625 struct async_inflight_info *pkts_info;
1630 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1631 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1632 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1633 dev->vid, __func__, queue_id);
1637 vq = dev->virtqueue[queue_id];
1639 if (unlikely(!vq->async_registered)) {
1640 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
1641 dev->vid, __func__, queue_id);
1645 rte_spinlock_lock(&vq->access_lock);
1647 n_inflight = vq->async_pkts_inflight_n;
1648 pkts_idx = vq->async_pkts_idx;
1649 pkts_info = vq->async_pkts_info;
1651 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
1652 vq_size, vq->async_pkts_inflight_n);
1654 if (count > vq->async_last_pkts_n)
1655 n_pkts_cpl = vq->async_ops.check_completed_copies(vid,
1656 queue_id, 0, count - vq->async_last_pkts_n);
1657 n_pkts_cpl += vq->async_last_pkts_n;
1661 while (likely((n_pkts_put < count) && n_inflight)) {
1662 uint16_t info_idx = (start_idx + n_pkts_put) & (vq_size - 1);
1663 if (n_pkts_cpl && pkts_info[info_idx].segs)
1665 else if (!n_pkts_cpl && pkts_info[info_idx].segs)
1669 n_descs += pkts_info[info_idx].descs;
1672 vq->async_last_pkts_n = n_pkts_cpl;
1675 vq->async_pkts_inflight_n = n_inflight;
1676 if (likely(vq->enabled && vq->access_ok)) {
1677 __atomic_add_fetch(&vq->used->idx,
1678 n_descs, __ATOMIC_RELEASE);
1679 vhost_vring_call_split(dev, vq);
1682 if (start_idx + n_pkts_put <= vq_size) {
1683 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1684 n_pkts_put * sizeof(uintptr_t));
1686 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1687 (vq_size - start_idx) * sizeof(uintptr_t));
1688 rte_memcpy(&pkts[vq_size - start_idx],
1689 vq->async_pkts_pending,
1690 (n_pkts_put + start_idx - vq_size) *
1695 rte_spinlock_unlock(&vq->access_lock);
1700 static __rte_always_inline uint32_t
1701 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1702 struct rte_mbuf **pkts, uint32_t count)
1704 struct vhost_virtqueue *vq;
1706 bool drawback = false;
1708 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1709 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1710 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1711 dev->vid, __func__, queue_id);
1715 vq = dev->virtqueue[queue_id];
1717 rte_spinlock_lock(&vq->access_lock);
1719 if (unlikely(vq->enabled == 0))
1720 goto out_access_unlock;
1722 if (unlikely(!vq->async_registered)) {
1724 goto out_access_unlock;
1727 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1728 vhost_user_iotlb_rd_lock(vq);
1730 if (unlikely(vq->access_ok == 0))
1731 if (unlikely(vring_translate(dev, vq) < 0))
1734 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1738 /* TODO: packed queue not implemented */
1739 if (vq_is_packed(dev))
1742 nb_tx = virtio_dev_rx_async_submit_split(dev,
1743 vq, queue_id, pkts, count);
1746 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1747 vhost_user_iotlb_rd_unlock(vq);
1750 rte_spinlock_unlock(&vq->access_lock);
1753 return rte_vhost_enqueue_burst(dev->vid, queue_id, pkts, count);
1759 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
1760 struct rte_mbuf **pkts, uint16_t count)
1762 struct virtio_net *dev = get_device(vid);
1767 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1769 "(%d) %s: built-in vhost net backend is disabled.\n",
1770 dev->vid, __func__);
1774 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
1778 virtio_net_with_host_offload(struct virtio_net *dev)
1781 ((1ULL << VIRTIO_NET_F_CSUM) |
1782 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1783 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1784 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1785 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1792 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1794 struct rte_ipv4_hdr *ipv4_hdr;
1795 struct rte_ipv6_hdr *ipv6_hdr;
1796 void *l3_hdr = NULL;
1797 struct rte_ether_hdr *eth_hdr;
1800 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1802 m->l2_len = sizeof(struct rte_ether_hdr);
1803 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1805 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1806 struct rte_vlan_hdr *vlan_hdr =
1807 (struct rte_vlan_hdr *)(eth_hdr + 1);
1809 m->l2_len += sizeof(struct rte_vlan_hdr);
1810 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1813 l3_hdr = (char *)eth_hdr + m->l2_len;
1815 switch (ethertype) {
1816 case RTE_ETHER_TYPE_IPV4:
1818 *l4_proto = ipv4_hdr->next_proto_id;
1819 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
1820 *l4_hdr = (char *)l3_hdr + m->l3_len;
1821 m->ol_flags |= PKT_TX_IPV4;
1823 case RTE_ETHER_TYPE_IPV6:
1825 *l4_proto = ipv6_hdr->proto;
1826 m->l3_len = sizeof(struct rte_ipv6_hdr);
1827 *l4_hdr = (char *)l3_hdr + m->l3_len;
1828 m->ol_flags |= PKT_TX_IPV6;
1838 static __rte_always_inline void
1839 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1841 uint16_t l4_proto = 0;
1842 void *l4_hdr = NULL;
1843 struct rte_tcp_hdr *tcp_hdr = NULL;
1845 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1848 parse_ethernet(m, &l4_proto, &l4_hdr);
1849 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1850 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1851 switch (hdr->csum_offset) {
1852 case (offsetof(struct rte_tcp_hdr, cksum)):
1853 if (l4_proto == IPPROTO_TCP)
1854 m->ol_flags |= PKT_TX_TCP_CKSUM;
1856 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1857 if (l4_proto == IPPROTO_UDP)
1858 m->ol_flags |= PKT_TX_UDP_CKSUM;
1860 case (offsetof(struct rte_sctp_hdr, cksum)):
1861 if (l4_proto == IPPROTO_SCTP)
1862 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1870 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1871 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1872 case VIRTIO_NET_HDR_GSO_TCPV4:
1873 case VIRTIO_NET_HDR_GSO_TCPV6:
1875 m->ol_flags |= PKT_TX_TCP_SEG;
1876 m->tso_segsz = hdr->gso_size;
1877 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1879 case VIRTIO_NET_HDR_GSO_UDP:
1880 m->ol_flags |= PKT_TX_UDP_SEG;
1881 m->tso_segsz = hdr->gso_size;
1882 m->l4_len = sizeof(struct rte_udp_hdr);
1885 VHOST_LOG_DATA(WARNING,
1886 "unsupported gso type %u.\n", hdr->gso_type);
1892 static __rte_noinline void
1893 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1894 struct buf_vector *buf_vec)
1897 uint64_t remain = sizeof(struct virtio_net_hdr);
1899 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1902 len = RTE_MIN(remain, buf_vec->buf_len);
1903 src = buf_vec->buf_addr;
1904 rte_memcpy((void *)(uintptr_t)dst,
1905 (void *)(uintptr_t)src, len);
1913 static __rte_always_inline int
1914 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1915 struct buf_vector *buf_vec, uint16_t nr_vec,
1916 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1918 uint32_t buf_avail, buf_offset;
1919 uint64_t buf_addr, buf_len;
1920 uint32_t mbuf_avail, mbuf_offset;
1922 struct rte_mbuf *cur = m, *prev = m;
1923 struct virtio_net_hdr tmp_hdr;
1924 struct virtio_net_hdr *hdr = NULL;
1925 /* A counter to avoid desc dead loop chain */
1926 uint16_t vec_idx = 0;
1927 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1930 buf_addr = buf_vec[vec_idx].buf_addr;
1931 buf_len = buf_vec[vec_idx].buf_len;
1933 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1938 if (virtio_net_with_host_offload(dev)) {
1939 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1941 * No luck, the virtio-net header doesn't fit
1942 * in a contiguous virtual area.
1944 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1947 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1952 * A virtio driver normally uses at least 2 desc buffers
1953 * for Tx: the first for storing the header, and others
1954 * for storing the data.
1956 if (unlikely(buf_len < dev->vhost_hlen)) {
1957 buf_offset = dev->vhost_hlen - buf_len;
1959 buf_addr = buf_vec[vec_idx].buf_addr;
1960 buf_len = buf_vec[vec_idx].buf_len;
1961 buf_avail = buf_len - buf_offset;
1962 } else if (buf_len == dev->vhost_hlen) {
1963 if (unlikely(++vec_idx >= nr_vec))
1965 buf_addr = buf_vec[vec_idx].buf_addr;
1966 buf_len = buf_vec[vec_idx].buf_len;
1969 buf_avail = buf_len;
1971 buf_offset = dev->vhost_hlen;
1972 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1976 (uintptr_t)(buf_addr + buf_offset),
1977 (uint32_t)buf_avail, 0);
1980 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1982 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1984 if (likely(cpy_len > MAX_BATCH_LEN ||
1985 vq->batch_copy_nb_elems >= vq->size ||
1986 (hdr && cur == m))) {
1987 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1989 (void *)((uintptr_t)(buf_addr +
1990 buf_offset)), cpy_len);
1992 batch_copy[vq->batch_copy_nb_elems].dst =
1993 rte_pktmbuf_mtod_offset(cur, void *,
1995 batch_copy[vq->batch_copy_nb_elems].src =
1996 (void *)((uintptr_t)(buf_addr + buf_offset));
1997 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1998 vq->batch_copy_nb_elems++;
2001 mbuf_avail -= cpy_len;
2002 mbuf_offset += cpy_len;
2003 buf_avail -= cpy_len;
2004 buf_offset += cpy_len;
2006 /* This buf reaches to its end, get the next one */
2007 if (buf_avail == 0) {
2008 if (++vec_idx >= nr_vec)
2011 buf_addr = buf_vec[vec_idx].buf_addr;
2012 buf_len = buf_vec[vec_idx].buf_len;
2015 buf_avail = buf_len;
2017 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2018 (uint32_t)buf_avail, 0);
2022 * This mbuf reaches to its end, get a new one
2023 * to hold more data.
2025 if (mbuf_avail == 0) {
2026 cur = rte_pktmbuf_alloc(mbuf_pool);
2027 if (unlikely(cur == NULL)) {
2028 VHOST_LOG_DATA(ERR, "Failed to "
2029 "allocate memory for mbuf.\n");
2035 prev->data_len = mbuf_offset;
2037 m->pkt_len += mbuf_offset;
2041 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2045 prev->data_len = mbuf_offset;
2046 m->pkt_len += mbuf_offset;
2049 vhost_dequeue_offload(hdr, m);
2057 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2063 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2065 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2066 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2071 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2072 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2074 if (unlikely(total_len > UINT16_MAX))
2077 buf_len = total_len;
2078 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2079 if (unlikely(buf == NULL))
2082 /* Initialize shinfo */
2083 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2084 virtio_dev_extbuf_free, buf);
2085 if (unlikely(shinfo == NULL)) {
2087 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2091 iova = rte_malloc_virt2iova(buf);
2092 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2093 rte_pktmbuf_reset_headroom(pkt);
2099 * Allocate a host supported pktmbuf.
2101 static __rte_always_inline struct rte_mbuf *
2102 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
2105 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
2107 if (unlikely(pkt == NULL)) {
2109 "Failed to allocate memory for mbuf.\n");
2113 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2116 /* attach an external buffer if supported */
2117 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2120 /* check if chained buffers are allowed */
2121 if (!dev->linearbuf)
2124 /* Data doesn't fit into the buffer and the host supports
2125 * only linear buffers
2127 rte_pktmbuf_free(pkt);
2132 static __rte_noinline uint16_t
2133 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2134 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2137 uint16_t free_entries;
2138 uint16_t dropped = 0;
2139 static bool allocerr_warned;
2142 * The ordering between avail index and
2143 * desc reads needs to be enforced.
2145 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2147 if (free_entries == 0)
2150 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2152 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2154 count = RTE_MIN(count, MAX_PKT_BURST);
2155 count = RTE_MIN(count, free_entries);
2156 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2159 for (i = 0; i < count; i++) {
2160 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2163 uint16_t nr_vec = 0;
2166 if (unlikely(fill_vec_buf_split(dev, vq,
2167 vq->last_avail_idx + i,
2169 &head_idx, &buf_len,
2170 VHOST_ACCESS_RO) < 0))
2173 update_shadow_used_ring_split(vq, head_idx, 0);
2175 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2176 if (unlikely(pkts[i] == NULL)) {
2178 * mbuf allocation fails for jumbo packets when external
2179 * buffer allocation is not allowed and linear buffer
2180 * is required. Drop this packet.
2182 if (!allocerr_warned) {
2184 "Failed mbuf alloc of size %d from %s on %s.\n",
2185 buf_len, mbuf_pool->name, dev->ifname);
2186 allocerr_warned = true;
2193 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2195 if (unlikely(err)) {
2196 rte_pktmbuf_free(pkts[i]);
2197 if (!allocerr_warned) {
2199 "Failed to copy desc to mbuf on %s.\n",
2201 allocerr_warned = true;
2209 vq->last_avail_idx += i;
2211 do_data_copy_dequeue(vq);
2212 if (unlikely(i < count))
2213 vq->shadow_used_idx = i;
2214 if (likely(vq->shadow_used_idx)) {
2215 flush_shadow_used_ring_split(dev, vq);
2216 vhost_vring_call_split(dev, vq);
2219 return (i - dropped);
2222 static __rte_always_inline int
2223 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2224 struct vhost_virtqueue *vq,
2225 struct rte_mempool *mbuf_pool,
2226 struct rte_mbuf **pkts,
2228 uintptr_t *desc_addrs,
2231 bool wrap = vq->avail_wrap_counter;
2232 struct vring_packed_desc *descs = vq->desc_packed;
2233 struct virtio_net_hdr *hdr;
2234 uint64_t lens[PACKED_BATCH_SIZE];
2235 uint64_t buf_lens[PACKED_BATCH_SIZE];
2236 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2239 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2241 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2244 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2245 flags = descs[avail_idx + i].flags;
2246 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2247 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2248 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2254 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2255 lens[i] = descs[avail_idx + i].len;
2257 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2258 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2259 descs[avail_idx + i].addr,
2260 &lens[i], VHOST_ACCESS_RW);
2263 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2264 if (unlikely(!desc_addrs[i]))
2266 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2270 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2271 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
2276 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2277 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2279 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2280 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2284 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2285 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
2286 pkts[i]->data_len = pkts[i]->pkt_len;
2287 ids[i] = descs[avail_idx + i].id;
2290 if (virtio_net_with_host_offload(dev)) {
2291 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2292 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2293 vhost_dequeue_offload(hdr, pkts[i]);
2300 for (i = 0; i < PACKED_BATCH_SIZE; i++)
2301 rte_pktmbuf_free(pkts[i]);
2306 static __rte_always_inline int
2307 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2308 struct vhost_virtqueue *vq,
2309 struct rte_mempool *mbuf_pool,
2310 struct rte_mbuf **pkts)
2312 uint16_t avail_idx = vq->last_avail_idx;
2313 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2314 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2315 uint16_t ids[PACKED_BATCH_SIZE];
2318 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
2319 avail_idx, desc_addrs, ids))
2322 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2323 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2325 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2326 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2327 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2330 if (virtio_net_is_inorder(dev))
2331 vhost_shadow_dequeue_batch_packed_inorder(vq,
2332 ids[PACKED_BATCH_SIZE - 1]);
2334 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2336 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2341 static __rte_always_inline int
2342 vhost_dequeue_single_packed(struct virtio_net *dev,
2343 struct vhost_virtqueue *vq,
2344 struct rte_mempool *mbuf_pool,
2345 struct rte_mbuf **pkts,
2347 uint16_t *desc_count)
2349 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2351 uint16_t nr_vec = 0;
2353 static bool allocerr_warned;
2355 if (unlikely(fill_vec_buf_packed(dev, vq,
2356 vq->last_avail_idx, desc_count,
2359 VHOST_ACCESS_RO) < 0))
2362 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2363 if (unlikely(*pkts == NULL)) {
2364 if (!allocerr_warned) {
2366 "Failed mbuf alloc of size %d from %s on %s.\n",
2367 buf_len, mbuf_pool->name, dev->ifname);
2368 allocerr_warned = true;
2373 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
2375 if (unlikely(err)) {
2376 if (!allocerr_warned) {
2378 "Failed to copy desc to mbuf on %s.\n",
2380 allocerr_warned = true;
2382 rte_pktmbuf_free(*pkts);
2389 static __rte_always_inline int
2390 virtio_dev_tx_single_packed(struct virtio_net *dev,
2391 struct vhost_virtqueue *vq,
2392 struct rte_mempool *mbuf_pool,
2393 struct rte_mbuf **pkts)
2396 uint16_t buf_id, desc_count = 0;
2399 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2402 if (likely(desc_count > 0)) {
2403 if (virtio_net_is_inorder(dev))
2404 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2407 vhost_shadow_dequeue_single_packed(vq, buf_id,
2410 vq_inc_last_avail_packed(vq, desc_count);
2416 static __rte_noinline uint16_t
2417 virtio_dev_tx_packed(struct virtio_net *dev,
2418 struct vhost_virtqueue *__rte_restrict vq,
2419 struct rte_mempool *mbuf_pool,
2420 struct rte_mbuf **__rte_restrict pkts,
2423 uint32_t pkt_idx = 0;
2424 uint32_t remained = count;
2427 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2429 if (remained >= PACKED_BATCH_SIZE) {
2430 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2432 pkt_idx += PACKED_BATCH_SIZE;
2433 remained -= PACKED_BATCH_SIZE;
2438 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2446 if (vq->shadow_used_idx) {
2447 do_data_copy_dequeue(vq);
2449 vhost_flush_dequeue_shadow_packed(dev, vq);
2450 vhost_vring_call_packed(dev, vq);
2457 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2458 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2460 struct virtio_net *dev;
2461 struct rte_mbuf *rarp_mbuf = NULL;
2462 struct vhost_virtqueue *vq;
2463 int16_t success = 1;
2465 dev = get_device(vid);
2469 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2471 "(%d) %s: built-in vhost net backend is disabled.\n",
2472 dev->vid, __func__);
2476 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2478 "(%d) %s: invalid virtqueue idx %d.\n",
2479 dev->vid, __func__, queue_id);
2483 vq = dev->virtqueue[queue_id];
2485 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2488 if (unlikely(vq->enabled == 0)) {
2490 goto out_access_unlock;
2493 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2494 vhost_user_iotlb_rd_lock(vq);
2496 if (unlikely(vq->access_ok == 0))
2497 if (unlikely(vring_translate(dev, vq) < 0)) {
2503 * Construct a RARP broadcast packet, and inject it to the "pkts"
2504 * array, to looks like that guest actually send such packet.
2506 * Check user_send_rarp() for more information.
2508 * broadcast_rarp shares a cacheline in the virtio_net structure
2509 * with some fields that are accessed during enqueue and
2510 * __atomic_compare_exchange_n causes a write if performed compare
2511 * and exchange. This could result in false sharing between enqueue
2514 * Prevent unnecessary false sharing by reading broadcast_rarp first
2515 * and only performing compare and exchange if the read indicates it
2516 * is likely to be set.
2518 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2519 __atomic_compare_exchange_n(&dev->broadcast_rarp,
2520 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2522 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2523 if (rarp_mbuf == NULL) {
2524 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2531 if (vq_is_packed(dev))
2532 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
2534 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2537 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2538 vhost_user_iotlb_rd_unlock(vq);
2541 rte_spinlock_unlock(&vq->access_lock);
2543 if (unlikely(rarp_mbuf != NULL)) {
2545 * Inject it to the head of "pkts" array, so that switch's mac
2546 * learning table will get updated first.
2548 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2549 pkts[0] = rarp_mbuf;