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;
174 /* The ordering for storing desc flags needs to be enforced. */
175 rte_atomic_thread_fence(__ATOMIC_RELEASE);
177 for (i = 0; i < vq->shadow_used_idx; i++) {
180 if (vq->shadow_used_packed[i].len)
181 flags = VRING_DESC_F_WRITE;
185 if (vq->used_wrap_counter) {
186 flags |= VRING_DESC_F_USED;
187 flags |= VRING_DESC_F_AVAIL;
189 flags &= ~VRING_DESC_F_USED;
190 flags &= ~VRING_DESC_F_AVAIL;
194 vq->desc_packed[vq->last_used_idx].flags = flags;
196 vhost_log_cache_used_vring(dev, vq,
198 sizeof(struct vring_packed_desc),
199 sizeof(struct vring_packed_desc));
201 head_idx = vq->last_used_idx;
205 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
208 vq->desc_packed[head_idx].flags = head_flags;
210 vhost_log_cache_used_vring(dev, vq,
212 sizeof(struct vring_packed_desc),
213 sizeof(struct vring_packed_desc));
215 vq->shadow_used_idx = 0;
216 vhost_log_cache_sync(dev, vq);
219 static __rte_always_inline void
220 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
221 struct vhost_virtqueue *vq)
223 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
225 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
226 /* desc flags is the synchronization point for virtio packed vring */
227 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
228 used_elem->flags, __ATOMIC_RELEASE);
230 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
231 sizeof(struct vring_packed_desc),
232 sizeof(struct vring_packed_desc));
233 vq->shadow_used_idx = 0;
234 vhost_log_cache_sync(dev, vq);
237 static __rte_always_inline void
238 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
239 struct vhost_virtqueue *vq,
246 if (vq->shadow_used_idx) {
247 do_data_copy_enqueue(dev, vq);
248 vhost_flush_enqueue_shadow_packed(dev, vq);
251 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
253 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
254 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
255 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
258 rte_atomic_thread_fence(__ATOMIC_RELEASE);
260 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
261 vq->desc_packed[vq->last_used_idx + i].flags = flags;
263 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
264 sizeof(struct vring_packed_desc),
265 sizeof(struct vring_packed_desc) *
267 vhost_log_cache_sync(dev, vq);
269 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
272 static __rte_always_inline void
273 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
276 vq->shadow_used_packed[0].id = id;
278 if (!vq->shadow_used_idx) {
279 vq->shadow_last_used_idx = vq->last_used_idx;
280 vq->shadow_used_packed[0].flags =
281 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
282 vq->shadow_used_packed[0].len = 0;
283 vq->shadow_used_packed[0].count = 1;
284 vq->shadow_used_idx++;
287 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
290 static __rte_always_inline void
291 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
292 struct vhost_virtqueue *vq,
299 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
301 if (!vq->shadow_used_idx) {
302 vq->shadow_last_used_idx = vq->last_used_idx;
303 vq->shadow_used_packed[0].id = ids[0];
304 vq->shadow_used_packed[0].len = 0;
305 vq->shadow_used_packed[0].count = 1;
306 vq->shadow_used_packed[0].flags = flags;
307 vq->shadow_used_idx++;
312 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
313 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
314 vq->desc_packed[vq->last_used_idx + i].len = 0;
317 rte_atomic_thread_fence(__ATOMIC_RELEASE);
318 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
319 vq->desc_packed[vq->last_used_idx + i].flags = flags;
321 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
322 sizeof(struct vring_packed_desc),
323 sizeof(struct vring_packed_desc) *
325 vhost_log_cache_sync(dev, vq);
327 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
330 static __rte_always_inline void
331 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
337 flags = vq->desc_packed[vq->last_used_idx].flags;
338 if (vq->used_wrap_counter) {
339 flags |= VRING_DESC_F_USED;
340 flags |= VRING_DESC_F_AVAIL;
342 flags &= ~VRING_DESC_F_USED;
343 flags &= ~VRING_DESC_F_AVAIL;
346 if (!vq->shadow_used_idx) {
347 vq->shadow_last_used_idx = vq->last_used_idx;
349 vq->shadow_used_packed[0].id = buf_id;
350 vq->shadow_used_packed[0].len = 0;
351 vq->shadow_used_packed[0].flags = flags;
352 vq->shadow_used_idx++;
354 vq->desc_packed[vq->last_used_idx].id = buf_id;
355 vq->desc_packed[vq->last_used_idx].len = 0;
356 vq->desc_packed[vq->last_used_idx].flags = flags;
359 vq_inc_last_used_packed(vq, count);
362 static __rte_always_inline void
363 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
369 vq->shadow_used_packed[0].id = buf_id;
371 flags = vq->desc_packed[vq->last_used_idx].flags;
372 if (vq->used_wrap_counter) {
373 flags |= VRING_DESC_F_USED;
374 flags |= VRING_DESC_F_AVAIL;
376 flags &= ~VRING_DESC_F_USED;
377 flags &= ~VRING_DESC_F_AVAIL;
380 if (!vq->shadow_used_idx) {
381 vq->shadow_last_used_idx = vq->last_used_idx;
382 vq->shadow_used_packed[0].len = 0;
383 vq->shadow_used_packed[0].flags = flags;
384 vq->shadow_used_idx++;
387 vq_inc_last_used_packed(vq, count);
390 static __rte_always_inline void
391 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
392 struct vhost_virtqueue *vq,
396 uint16_t num_buffers)
399 for (i = 0; i < num_buffers; i++) {
400 /* enqueue shadow flush action aligned with batch num */
401 if (!vq->shadow_used_idx)
402 vq->shadow_aligned_idx = vq->last_used_idx &
404 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
405 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
406 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
407 vq->shadow_aligned_idx += count[i];
408 vq->shadow_used_idx++;
411 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
412 do_data_copy_enqueue(dev, vq);
413 vhost_flush_enqueue_shadow_packed(dev, vq);
417 /* avoid write operation when necessary, to lessen cache issues */
418 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
419 if ((var) != (val)) \
423 static __rte_always_inline void
424 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
426 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
428 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
429 csum_l4 |= PKT_TX_TCP_CKSUM;
432 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
433 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
436 case PKT_TX_TCP_CKSUM:
437 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
440 case PKT_TX_UDP_CKSUM:
441 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
444 case PKT_TX_SCTP_CKSUM:
445 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
450 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
451 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
452 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
455 /* IP cksum verification cannot be bypassed, then calculate here */
456 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
457 struct rte_ipv4_hdr *ipv4_hdr;
459 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
461 ipv4_hdr->hdr_checksum = 0;
462 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
465 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
466 if (m_buf->ol_flags & PKT_TX_IPV4)
467 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
469 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
470 net_hdr->gso_size = m_buf->tso_segsz;
471 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
473 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
474 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
475 net_hdr->gso_size = m_buf->tso_segsz;
476 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
479 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
480 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
481 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
485 static __rte_always_inline int
486 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
487 struct buf_vector *buf_vec, uint16_t *vec_idx,
488 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
490 uint16_t vec_id = *vec_idx;
494 uint64_t desc_chunck_len = desc_len;
496 if (unlikely(vec_id >= BUF_VECTOR_MAX))
499 desc_addr = vhost_iova_to_vva(dev, vq,
503 if (unlikely(!desc_addr))
506 rte_prefetch0((void *)(uintptr_t)desc_addr);
508 buf_vec[vec_id].buf_iova = desc_iova;
509 buf_vec[vec_id].buf_addr = desc_addr;
510 buf_vec[vec_id].buf_len = desc_chunck_len;
512 desc_len -= desc_chunck_len;
513 desc_iova += desc_chunck_len;
521 static __rte_always_inline int
522 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
523 uint32_t avail_idx, uint16_t *vec_idx,
524 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
525 uint32_t *desc_chain_len, uint8_t perm)
527 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
528 uint16_t vec_id = *vec_idx;
531 uint32_t nr_descs = vq->size;
533 struct vring_desc *descs = vq->desc;
534 struct vring_desc *idesc = NULL;
536 if (unlikely(idx >= vq->size))
539 *desc_chain_head = idx;
541 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
542 dlen = vq->desc[idx].len;
543 nr_descs = dlen / sizeof(struct vring_desc);
544 if (unlikely(nr_descs > vq->size))
547 descs = (struct vring_desc *)(uintptr_t)
548 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
551 if (unlikely(!descs))
554 if (unlikely(dlen < vq->desc[idx].len)) {
556 * The indirect desc table is not contiguous
557 * in process VA space, we have to copy it.
559 idesc = vhost_alloc_copy_ind_table(dev, vq,
560 vq->desc[idx].addr, vq->desc[idx].len);
561 if (unlikely(!idesc))
571 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
572 free_ind_table(idesc);
576 len += descs[idx].len;
578 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
579 descs[idx].addr, descs[idx].len,
581 free_ind_table(idesc);
585 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
588 idx = descs[idx].next;
591 *desc_chain_len = len;
594 if (unlikely(!!idesc))
595 free_ind_table(idesc);
601 * Returns -1 on fail, 0 on success
604 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
605 uint32_t size, struct buf_vector *buf_vec,
606 uint16_t *num_buffers, uint16_t avail_head,
610 uint16_t vec_idx = 0;
611 uint16_t max_tries, tries = 0;
613 uint16_t head_idx = 0;
617 cur_idx = vq->last_avail_idx;
619 if (rxvq_is_mergeable(dev))
620 max_tries = vq->size - 1;
625 if (unlikely(cur_idx == avail_head))
628 * if we tried all available ring items, and still
629 * can't get enough buf, it means something abnormal
632 if (unlikely(++tries > max_tries))
635 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
638 VHOST_ACCESS_RW) < 0))
640 len = RTE_MIN(len, size);
641 update_shadow_used_ring_split(vq, head_idx, len);
653 static __rte_always_inline int
654 fill_vec_buf_packed_indirect(struct virtio_net *dev,
655 struct vhost_virtqueue *vq,
656 struct vring_packed_desc *desc, uint16_t *vec_idx,
657 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
661 uint16_t vec_id = *vec_idx;
663 struct vring_packed_desc *descs, *idescs = NULL;
666 descs = (struct vring_packed_desc *)(uintptr_t)
667 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
668 if (unlikely(!descs))
671 if (unlikely(dlen < desc->len)) {
673 * The indirect desc table is not contiguous
674 * in process VA space, we have to copy it.
676 idescs = vhost_alloc_copy_ind_table(dev,
677 vq, desc->addr, desc->len);
678 if (unlikely(!idescs))
684 nr_descs = desc->len / sizeof(struct vring_packed_desc);
685 if (unlikely(nr_descs >= vq->size)) {
686 free_ind_table(idescs);
690 for (i = 0; i < nr_descs; i++) {
691 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
692 free_ind_table(idescs);
696 *len += descs[i].len;
697 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
698 descs[i].addr, descs[i].len,
704 if (unlikely(!!idescs))
705 free_ind_table(idescs);
710 static __rte_always_inline int
711 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
712 uint16_t avail_idx, uint16_t *desc_count,
713 struct buf_vector *buf_vec, uint16_t *vec_idx,
714 uint16_t *buf_id, uint32_t *len, uint8_t perm)
716 bool wrap_counter = vq->avail_wrap_counter;
717 struct vring_packed_desc *descs = vq->desc_packed;
718 uint16_t vec_id = *vec_idx;
720 if (avail_idx < vq->last_avail_idx)
724 * Perform a load-acquire barrier in desc_is_avail to
725 * enforce the ordering between desc flags and desc
728 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
735 if (unlikely(vec_id >= BUF_VECTOR_MAX))
738 if (unlikely(*desc_count >= vq->size))
742 *buf_id = descs[avail_idx].id;
744 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
745 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
751 *len += descs[avail_idx].len;
753 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
754 descs[avail_idx].addr,
755 descs[avail_idx].len,
760 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
763 if (++avail_idx >= vq->size) {
764 avail_idx -= vq->size;
774 static __rte_noinline void
775 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
776 struct buf_vector *buf_vec,
777 struct virtio_net_hdr_mrg_rxbuf *hdr)
780 uint64_t remain = dev->vhost_hlen;
781 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
782 uint64_t iova = buf_vec->buf_iova;
785 len = RTE_MIN(remain,
787 dst = buf_vec->buf_addr;
788 rte_memcpy((void *)(uintptr_t)dst,
789 (void *)(uintptr_t)src,
792 PRINT_PACKET(dev, (uintptr_t)dst,
794 vhost_log_cache_write_iova(dev, vq,
804 static __rte_always_inline int
805 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
806 struct rte_mbuf *m, struct buf_vector *buf_vec,
807 uint16_t nr_vec, uint16_t num_buffers)
809 uint32_t vec_idx = 0;
810 uint32_t mbuf_offset, mbuf_avail;
811 uint32_t buf_offset, buf_avail;
812 uint64_t buf_addr, buf_iova, buf_len;
815 struct rte_mbuf *hdr_mbuf;
816 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
817 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
820 if (unlikely(m == NULL)) {
825 buf_addr = buf_vec[vec_idx].buf_addr;
826 buf_iova = buf_vec[vec_idx].buf_iova;
827 buf_len = buf_vec[vec_idx].buf_len;
829 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
836 if (unlikely(buf_len < dev->vhost_hlen))
839 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
841 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
842 dev->vid, num_buffers);
844 if (unlikely(buf_len < dev->vhost_hlen)) {
845 buf_offset = dev->vhost_hlen - buf_len;
847 buf_addr = buf_vec[vec_idx].buf_addr;
848 buf_iova = buf_vec[vec_idx].buf_iova;
849 buf_len = buf_vec[vec_idx].buf_len;
850 buf_avail = buf_len - buf_offset;
852 buf_offset = dev->vhost_hlen;
853 buf_avail = buf_len - dev->vhost_hlen;
856 mbuf_avail = rte_pktmbuf_data_len(m);
858 while (mbuf_avail != 0 || m->next != NULL) {
859 /* done with current buf, get the next one */
860 if (buf_avail == 0) {
862 if (unlikely(vec_idx >= nr_vec)) {
867 buf_addr = buf_vec[vec_idx].buf_addr;
868 buf_iova = buf_vec[vec_idx].buf_iova;
869 buf_len = buf_vec[vec_idx].buf_len;
875 /* done with current mbuf, get the next one */
876 if (mbuf_avail == 0) {
880 mbuf_avail = rte_pktmbuf_data_len(m);
884 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
885 if (rxvq_is_mergeable(dev))
886 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
889 if (unlikely(hdr == &tmp_hdr)) {
890 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
892 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
894 vhost_log_cache_write_iova(dev, vq,
902 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
904 if (likely(cpy_len > MAX_BATCH_LEN ||
905 vq->batch_copy_nb_elems >= vq->size)) {
906 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
907 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
909 vhost_log_cache_write_iova(dev, vq,
910 buf_iova + buf_offset,
912 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
915 batch_copy[vq->batch_copy_nb_elems].dst =
916 (void *)((uintptr_t)(buf_addr + buf_offset));
917 batch_copy[vq->batch_copy_nb_elems].src =
918 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
919 batch_copy[vq->batch_copy_nb_elems].log_addr =
920 buf_iova + buf_offset;
921 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
922 vq->batch_copy_nb_elems++;
925 mbuf_avail -= cpy_len;
926 mbuf_offset += cpy_len;
927 buf_avail -= cpy_len;
928 buf_offset += cpy_len;
936 static __rte_always_inline void
937 async_fill_vec(struct iovec *v, void *base, size_t len)
943 static __rte_always_inline void
944 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
945 struct iovec *vec, unsigned long nr_seg)
952 it->nr_segs = nr_seg;
959 static __rte_always_inline void
960 async_fill_desc(struct rte_vhost_async_desc *desc,
961 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
967 static __rte_always_inline int
968 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
969 struct rte_mbuf *m, struct buf_vector *buf_vec,
970 uint16_t nr_vec, uint16_t num_buffers,
971 struct iovec *src_iovec, struct iovec *dst_iovec,
972 struct rte_vhost_iov_iter *src_it,
973 struct rte_vhost_iov_iter *dst_it)
975 uint32_t vec_idx = 0;
976 uint32_t mbuf_offset, mbuf_avail;
977 uint32_t buf_offset, buf_avail;
978 uint64_t buf_addr, buf_iova, buf_len;
979 uint32_t cpy_len, cpy_threshold;
981 struct rte_mbuf *hdr_mbuf;
982 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
983 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
991 if (unlikely(m == NULL)) {
996 cpy_threshold = vq->async_threshold;
998 buf_addr = buf_vec[vec_idx].buf_addr;
999 buf_iova = buf_vec[vec_idx].buf_iova;
1000 buf_len = buf_vec[vec_idx].buf_len;
1002 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1008 hdr_addr = buf_addr;
1009 if (unlikely(buf_len < dev->vhost_hlen))
1012 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1014 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1015 dev->vid, num_buffers);
1017 if (unlikely(buf_len < dev->vhost_hlen)) {
1018 buf_offset = dev->vhost_hlen - buf_len;
1020 buf_addr = buf_vec[vec_idx].buf_addr;
1021 buf_iova = buf_vec[vec_idx].buf_iova;
1022 buf_len = buf_vec[vec_idx].buf_len;
1023 buf_avail = buf_len - buf_offset;
1025 buf_offset = dev->vhost_hlen;
1026 buf_avail = buf_len - dev->vhost_hlen;
1029 mbuf_avail = rte_pktmbuf_data_len(m);
1032 while (mbuf_avail != 0 || m->next != NULL) {
1033 /* done with current buf, get the next one */
1034 if (buf_avail == 0) {
1036 if (unlikely(vec_idx >= nr_vec)) {
1041 buf_addr = buf_vec[vec_idx].buf_addr;
1042 buf_iova = buf_vec[vec_idx].buf_iova;
1043 buf_len = buf_vec[vec_idx].buf_len;
1046 buf_avail = buf_len;
1049 /* done with current mbuf, get the next one */
1050 if (mbuf_avail == 0) {
1054 mbuf_avail = rte_pktmbuf_data_len(m);
1058 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1059 if (rxvq_is_mergeable(dev))
1060 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1063 if (unlikely(hdr == &tmp_hdr)) {
1064 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1066 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1067 dev->vhost_hlen, 0);
1068 vhost_log_cache_write_iova(dev, vq,
1069 buf_vec[0].buf_iova,
1076 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1078 while (unlikely(cpy_len && cpy_len >= cpy_threshold)) {
1079 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1080 buf_iova + buf_offset,
1081 cpy_len, &mapped_len);
1083 if (unlikely(!hpa || mapped_len < cpy_threshold))
1086 async_fill_vec(src_iovec + tvec_idx,
1087 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1088 mbuf_offset), (size_t)mapped_len);
1090 async_fill_vec(dst_iovec + tvec_idx,
1091 hpa, (size_t)mapped_len);
1093 tlen += (uint32_t)mapped_len;
1094 cpy_len -= (uint32_t)mapped_len;
1095 mbuf_avail -= (uint32_t)mapped_len;
1096 mbuf_offset += (uint32_t)mapped_len;
1097 buf_avail -= (uint32_t)mapped_len;
1098 buf_offset += (uint32_t)mapped_len;
1102 if (likely(cpy_len)) {
1103 if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1105 (void *)((uintptr_t)(buf_addr + buf_offset)),
1106 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1110 (uintptr_t)(buf_addr + buf_offset),
1113 batch_copy[vq->batch_copy_nb_elems].dst =
1114 (void *)((uintptr_t)(buf_addr + buf_offset));
1115 batch_copy[vq->batch_copy_nb_elems].src =
1116 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1117 batch_copy[vq->batch_copy_nb_elems].log_addr =
1118 buf_iova + buf_offset;
1119 batch_copy[vq->batch_copy_nb_elems].len =
1121 vq->batch_copy_nb_elems++;
1124 mbuf_avail -= cpy_len;
1125 mbuf_offset += cpy_len;
1126 buf_avail -= cpy_len;
1127 buf_offset += cpy_len;
1133 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1134 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1139 static __rte_always_inline int
1140 vhost_enqueue_single_packed(struct virtio_net *dev,
1141 struct vhost_virtqueue *vq,
1142 struct rte_mbuf *pkt,
1143 struct buf_vector *buf_vec,
1146 uint16_t nr_vec = 0;
1147 uint16_t avail_idx = vq->last_avail_idx;
1148 uint16_t max_tries, tries = 0;
1149 uint16_t buf_id = 0;
1151 uint16_t desc_count;
1152 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1153 uint16_t num_buffers = 0;
1154 uint32_t buffer_len[vq->size];
1155 uint16_t buffer_buf_id[vq->size];
1156 uint16_t buffer_desc_count[vq->size];
1158 if (rxvq_is_mergeable(dev))
1159 max_tries = vq->size - 1;
1165 * if we tried all available ring items, and still
1166 * can't get enough buf, it means something abnormal
1169 if (unlikely(++tries > max_tries))
1172 if (unlikely(fill_vec_buf_packed(dev, vq,
1173 avail_idx, &desc_count,
1176 VHOST_ACCESS_RW) < 0))
1179 len = RTE_MIN(len, size);
1182 buffer_len[num_buffers] = len;
1183 buffer_buf_id[num_buffers] = buf_id;
1184 buffer_desc_count[num_buffers] = desc_count;
1187 *nr_descs += desc_count;
1188 avail_idx += desc_count;
1189 if (avail_idx >= vq->size)
1190 avail_idx -= vq->size;
1193 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1196 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1197 buffer_desc_count, num_buffers);
1202 static __rte_noinline uint32_t
1203 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1204 struct rte_mbuf **pkts, uint32_t count)
1206 uint32_t pkt_idx = 0;
1207 uint16_t num_buffers;
1208 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1209 uint16_t avail_head;
1212 * The ordering between avail index and
1213 * desc reads needs to be enforced.
1215 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1217 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1219 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1220 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1221 uint16_t nr_vec = 0;
1223 if (unlikely(reserve_avail_buf_split(dev, vq,
1224 pkt_len, buf_vec, &num_buffers,
1225 avail_head, &nr_vec) < 0)) {
1226 VHOST_LOG_DATA(DEBUG,
1227 "(%d) failed to get enough desc from vring\n",
1229 vq->shadow_used_idx -= num_buffers;
1233 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1234 dev->vid, vq->last_avail_idx,
1235 vq->last_avail_idx + num_buffers);
1237 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1240 vq->shadow_used_idx -= num_buffers;
1244 vq->last_avail_idx += num_buffers;
1247 do_data_copy_enqueue(dev, vq);
1249 if (likely(vq->shadow_used_idx)) {
1250 flush_shadow_used_ring_split(dev, vq);
1251 vhost_vring_call_split(dev, vq);
1257 static __rte_always_inline int
1258 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1259 struct vhost_virtqueue *vq,
1260 struct rte_mbuf **pkts)
1262 bool wrap_counter = vq->avail_wrap_counter;
1263 struct vring_packed_desc *descs = vq->desc_packed;
1264 uint16_t avail_idx = vq->last_avail_idx;
1265 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1266 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1267 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1268 uint64_t lens[PACKED_BATCH_SIZE];
1269 uint16_t ids[PACKED_BATCH_SIZE];
1272 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1275 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1278 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1279 if (unlikely(pkts[i]->next != NULL))
1281 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1286 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1287 lens[i] = descs[avail_idx + i].len;
1289 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1290 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1294 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1295 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1296 descs[avail_idx + i].addr,
1300 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1301 if (unlikely(!desc_addrs[i]))
1303 if (unlikely(lens[i] != descs[avail_idx + i].len))
1307 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1308 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1309 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1310 (uintptr_t)desc_addrs[i];
1311 lens[i] = pkts[i]->pkt_len +
1312 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1315 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1316 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1318 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1320 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1321 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1322 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1326 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1327 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1330 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1331 ids[i] = descs[avail_idx + i].id;
1333 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1338 static __rte_always_inline int16_t
1339 virtio_dev_rx_single_packed(struct virtio_net *dev,
1340 struct vhost_virtqueue *vq,
1341 struct rte_mbuf *pkt)
1343 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1344 uint16_t nr_descs = 0;
1346 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1348 VHOST_LOG_DATA(DEBUG,
1349 "(%d) failed to get enough desc from vring\n",
1354 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1355 dev->vid, vq->last_avail_idx,
1356 vq->last_avail_idx + nr_descs);
1358 vq_inc_last_avail_packed(vq, nr_descs);
1363 static __rte_noinline uint32_t
1364 virtio_dev_rx_packed(struct virtio_net *dev,
1365 struct vhost_virtqueue *__rte_restrict vq,
1366 struct rte_mbuf **__rte_restrict pkts,
1369 uint32_t pkt_idx = 0;
1370 uint32_t remained = count;
1373 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1375 if (remained >= PACKED_BATCH_SIZE) {
1376 if (!virtio_dev_rx_batch_packed(dev, vq,
1378 pkt_idx += PACKED_BATCH_SIZE;
1379 remained -= PACKED_BATCH_SIZE;
1384 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1389 } while (pkt_idx < count);
1391 if (vq->shadow_used_idx) {
1392 do_data_copy_enqueue(dev, vq);
1393 vhost_flush_enqueue_shadow_packed(dev, vq);
1397 vhost_vring_call_packed(dev, vq);
1402 static __rte_always_inline uint32_t
1403 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1404 struct rte_mbuf **pkts, uint32_t count)
1406 struct vhost_virtqueue *vq;
1409 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1410 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1411 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1412 dev->vid, __func__, queue_id);
1416 vq = dev->virtqueue[queue_id];
1418 rte_spinlock_lock(&vq->access_lock);
1420 if (unlikely(vq->enabled == 0))
1421 goto out_access_unlock;
1423 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1424 vhost_user_iotlb_rd_lock(vq);
1426 if (unlikely(vq->access_ok == 0))
1427 if (unlikely(vring_translate(dev, vq) < 0))
1430 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1434 if (vq_is_packed(dev))
1435 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1437 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1440 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1441 vhost_user_iotlb_rd_unlock(vq);
1444 rte_spinlock_unlock(&vq->access_lock);
1450 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1451 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1453 struct virtio_net *dev = get_device(vid);
1458 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1460 "(%d) %s: built-in vhost net backend is disabled.\n",
1461 dev->vid, __func__);
1465 return virtio_dev_rx(dev, queue_id, pkts, count);
1468 static __rte_always_inline uint16_t
1469 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1470 uint16_t vq_size, uint16_t n_inflight)
1472 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1473 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1476 static __rte_noinline uint32_t
1477 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1478 struct vhost_virtqueue *vq, uint16_t queue_id,
1479 struct rte_mbuf **pkts, uint32_t count)
1481 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1482 uint16_t num_buffers;
1483 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1484 uint16_t avail_head;
1486 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1487 struct iovec *vec_pool = vq->vec_pool;
1488 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1489 struct iovec *src_iovec = vec_pool;
1490 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1491 struct rte_vhost_iov_iter *src_it = it_pool;
1492 struct rte_vhost_iov_iter *dst_it = it_pool + 1;
1493 uint16_t n_free_slot, slot_idx = 0;
1494 uint16_t pkt_err = 0;
1495 uint16_t segs_await = 0;
1496 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1500 * The ordering between avail index and desc reads need to be enforced.
1502 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1504 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1506 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1507 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1508 uint16_t nr_vec = 0;
1510 if (unlikely(reserve_avail_buf_split(dev, vq,
1511 pkt_len, buf_vec, &num_buffers,
1512 avail_head, &nr_vec) < 0)) {
1513 VHOST_LOG_DATA(DEBUG,
1514 "(%d) failed to get enough desc from vring\n",
1516 vq->shadow_used_idx -= num_buffers;
1520 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1521 dev->vid, vq->last_avail_idx,
1522 vq->last_avail_idx + num_buffers);
1524 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1525 buf_vec, nr_vec, num_buffers,
1526 src_iovec, dst_iovec, src_it, dst_it) < 0) {
1527 vq->shadow_used_idx -= num_buffers;
1531 slot_idx = (vq->async_pkts_idx + pkt_idx) & (vq->size - 1);
1532 if (src_it->count) {
1533 async_fill_desc(&tdes[pkt_burst_idx], src_it, dst_it);
1535 pkts_info[slot_idx].descs = num_buffers;
1536 pkts_info[slot_idx].segs = src_it->nr_segs;
1537 src_iovec += src_it->nr_segs;
1538 dst_iovec += dst_it->nr_segs;
1541 segs_await += src_it->nr_segs;
1543 pkts_info[slot_idx].info = num_buffers;
1544 vq->async_pkts_inflight_n++;
1547 vq->last_avail_idx += num_buffers;
1550 * conditions to trigger async device transfer:
1551 * - buffered packet number reaches transfer threshold
1552 * - this is the last packet in the burst enqueue
1553 * - unused async iov number is less than max vhost vector
1555 if (pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1556 (pkt_idx == count - 1 && pkt_burst_idx) ||
1557 (VHOST_MAX_ASYNC_VEC / 2 - segs_await <
1559 n_pkts = vq->async_ops.transfer_data(dev->vid,
1560 queue_id, tdes, 0, pkt_burst_idx);
1561 src_iovec = vec_pool;
1562 dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1564 dst_it = it_pool + 1;
1566 vq->async_pkts_inflight_n += pkt_burst_idx;
1568 if (unlikely(n_pkts < (int)pkt_burst_idx)) {
1570 * log error packets number here and do actual
1571 * error processing when applications poll
1574 pkt_err = pkt_burst_idx - n_pkts;
1583 if (pkt_burst_idx) {
1584 n_pkts = vq->async_ops.transfer_data(dev->vid,
1585 queue_id, tdes, 0, pkt_burst_idx);
1586 vq->async_pkts_inflight_n += pkt_burst_idx;
1588 if (unlikely(n_pkts < (int)pkt_burst_idx))
1589 pkt_err = pkt_burst_idx - n_pkts;
1592 do_data_copy_enqueue(dev, vq);
1594 while (unlikely(pkt_err && pkt_idx)) {
1595 if (pkts_info[slot_idx].segs)
1597 vq->last_avail_idx -= pkts_info[slot_idx].descs;
1598 vq->shadow_used_idx -= pkts_info[slot_idx].descs;
1599 vq->async_pkts_inflight_n--;
1600 slot_idx = (slot_idx - 1) & (vq->size - 1);
1604 n_free_slot = vq->size - vq->async_pkts_idx;
1605 if (n_free_slot > pkt_idx) {
1606 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1607 pkts, pkt_idx * sizeof(uintptr_t));
1608 vq->async_pkts_idx += pkt_idx;
1610 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1611 pkts, n_free_slot * sizeof(uintptr_t));
1612 rte_memcpy(&vq->async_pkts_pending[0],
1614 (pkt_idx - n_free_slot) * sizeof(uintptr_t));
1615 vq->async_pkts_idx = pkt_idx - n_free_slot;
1618 if (likely(vq->shadow_used_idx))
1619 async_flush_shadow_used_ring_split(dev, vq);
1624 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1625 struct rte_mbuf **pkts, uint16_t count)
1627 struct virtio_net *dev = get_device(vid);
1628 struct vhost_virtqueue *vq;
1629 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0;
1630 uint16_t start_idx, pkts_idx, vq_size;
1631 uint16_t n_inflight;
1632 struct async_inflight_info *pkts_info;
1637 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1638 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1639 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1640 dev->vid, __func__, queue_id);
1644 vq = dev->virtqueue[queue_id];
1646 if (unlikely(!vq->async_registered)) {
1647 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
1648 dev->vid, __func__, queue_id);
1652 rte_spinlock_lock(&vq->access_lock);
1654 n_inflight = vq->async_pkts_inflight_n;
1655 pkts_idx = vq->async_pkts_idx;
1656 pkts_info = vq->async_pkts_info;
1658 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
1659 vq_size, vq->async_pkts_inflight_n);
1661 if (count > vq->async_last_pkts_n)
1662 n_pkts_cpl = vq->async_ops.check_completed_copies(vid,
1663 queue_id, 0, count - vq->async_last_pkts_n);
1664 n_pkts_cpl += vq->async_last_pkts_n;
1668 while (likely((n_pkts_put < count) && n_inflight)) {
1669 uint16_t info_idx = (start_idx + n_pkts_put) & (vq_size - 1);
1670 if (n_pkts_cpl && pkts_info[info_idx].segs)
1672 else if (!n_pkts_cpl && pkts_info[info_idx].segs)
1676 n_descs += pkts_info[info_idx].descs;
1679 vq->async_last_pkts_n = n_pkts_cpl;
1682 vq->async_pkts_inflight_n = n_inflight;
1683 if (likely(vq->enabled && vq->access_ok)) {
1684 __atomic_add_fetch(&vq->used->idx,
1685 n_descs, __ATOMIC_RELEASE);
1686 vhost_vring_call_split(dev, vq);
1689 if (start_idx + n_pkts_put <= vq_size) {
1690 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1691 n_pkts_put * sizeof(uintptr_t));
1693 rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1694 (vq_size - start_idx) * sizeof(uintptr_t));
1695 rte_memcpy(&pkts[vq_size - start_idx],
1696 vq->async_pkts_pending,
1697 (n_pkts_put + start_idx - vq_size) *
1702 rte_spinlock_unlock(&vq->access_lock);
1707 static __rte_always_inline uint32_t
1708 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1709 struct rte_mbuf **pkts, uint32_t count)
1711 struct vhost_virtqueue *vq;
1714 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1715 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1716 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1717 dev->vid, __func__, queue_id);
1721 vq = dev->virtqueue[queue_id];
1723 rte_spinlock_lock(&vq->access_lock);
1725 if (unlikely(vq->enabled == 0 || !vq->async_registered))
1726 goto out_access_unlock;
1728 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1729 vhost_user_iotlb_rd_lock(vq);
1731 if (unlikely(vq->access_ok == 0))
1732 if (unlikely(vring_translate(dev, vq) < 0))
1735 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1739 /* TODO: packed queue not implemented */
1740 if (vq_is_packed(dev))
1743 nb_tx = virtio_dev_rx_async_submit_split(dev,
1744 vq, queue_id, pkts, count);
1747 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1748 vhost_user_iotlb_rd_unlock(vq);
1751 rte_spinlock_unlock(&vq->access_lock);
1757 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
1758 struct rte_mbuf **pkts, uint16_t count)
1760 struct virtio_net *dev = get_device(vid);
1765 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1767 "(%d) %s: built-in vhost net backend is disabled.\n",
1768 dev->vid, __func__);
1772 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
1776 virtio_net_with_host_offload(struct virtio_net *dev)
1779 ((1ULL << VIRTIO_NET_F_CSUM) |
1780 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1781 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1782 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1783 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1790 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1792 struct rte_ipv4_hdr *ipv4_hdr;
1793 struct rte_ipv6_hdr *ipv6_hdr;
1794 void *l3_hdr = NULL;
1795 struct rte_ether_hdr *eth_hdr;
1798 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1800 m->l2_len = sizeof(struct rte_ether_hdr);
1801 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1803 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1804 struct rte_vlan_hdr *vlan_hdr =
1805 (struct rte_vlan_hdr *)(eth_hdr + 1);
1807 m->l2_len += sizeof(struct rte_vlan_hdr);
1808 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1811 l3_hdr = (char *)eth_hdr + m->l2_len;
1813 switch (ethertype) {
1814 case RTE_ETHER_TYPE_IPV4:
1816 *l4_proto = ipv4_hdr->next_proto_id;
1817 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
1818 *l4_hdr = (char *)l3_hdr + m->l3_len;
1819 m->ol_flags |= PKT_TX_IPV4;
1821 case RTE_ETHER_TYPE_IPV6:
1823 *l4_proto = ipv6_hdr->proto;
1824 m->l3_len = sizeof(struct rte_ipv6_hdr);
1825 *l4_hdr = (char *)l3_hdr + m->l3_len;
1826 m->ol_flags |= PKT_TX_IPV6;
1836 static __rte_always_inline void
1837 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1839 uint16_t l4_proto = 0;
1840 void *l4_hdr = NULL;
1841 struct rte_tcp_hdr *tcp_hdr = NULL;
1843 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1846 parse_ethernet(m, &l4_proto, &l4_hdr);
1847 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1848 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1849 switch (hdr->csum_offset) {
1850 case (offsetof(struct rte_tcp_hdr, cksum)):
1851 if (l4_proto == IPPROTO_TCP)
1852 m->ol_flags |= PKT_TX_TCP_CKSUM;
1854 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1855 if (l4_proto == IPPROTO_UDP)
1856 m->ol_flags |= PKT_TX_UDP_CKSUM;
1858 case (offsetof(struct rte_sctp_hdr, cksum)):
1859 if (l4_proto == IPPROTO_SCTP)
1860 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1868 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1869 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1870 case VIRTIO_NET_HDR_GSO_TCPV4:
1871 case VIRTIO_NET_HDR_GSO_TCPV6:
1873 m->ol_flags |= PKT_TX_TCP_SEG;
1874 m->tso_segsz = hdr->gso_size;
1875 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1877 case VIRTIO_NET_HDR_GSO_UDP:
1878 m->ol_flags |= PKT_TX_UDP_SEG;
1879 m->tso_segsz = hdr->gso_size;
1880 m->l4_len = sizeof(struct rte_udp_hdr);
1883 VHOST_LOG_DATA(WARNING,
1884 "unsupported gso type %u.\n", hdr->gso_type);
1890 static __rte_noinline void
1891 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1892 struct buf_vector *buf_vec)
1895 uint64_t remain = sizeof(struct virtio_net_hdr);
1897 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1900 len = RTE_MIN(remain, buf_vec->buf_len);
1901 src = buf_vec->buf_addr;
1902 rte_memcpy((void *)(uintptr_t)dst,
1903 (void *)(uintptr_t)src, len);
1911 static __rte_always_inline int
1912 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1913 struct buf_vector *buf_vec, uint16_t nr_vec,
1914 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1916 uint32_t buf_avail, buf_offset;
1917 uint64_t buf_addr, buf_len;
1918 uint32_t mbuf_avail, mbuf_offset;
1920 struct rte_mbuf *cur = m, *prev = m;
1921 struct virtio_net_hdr tmp_hdr;
1922 struct virtio_net_hdr *hdr = NULL;
1923 /* A counter to avoid desc dead loop chain */
1924 uint16_t vec_idx = 0;
1925 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1928 buf_addr = buf_vec[vec_idx].buf_addr;
1929 buf_len = buf_vec[vec_idx].buf_len;
1931 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1936 if (virtio_net_with_host_offload(dev)) {
1937 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1939 * No luck, the virtio-net header doesn't fit
1940 * in a contiguous virtual area.
1942 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1945 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1950 * A virtio driver normally uses at least 2 desc buffers
1951 * for Tx: the first for storing the header, and others
1952 * for storing the data.
1954 if (unlikely(buf_len < dev->vhost_hlen)) {
1955 buf_offset = dev->vhost_hlen - buf_len;
1957 buf_addr = buf_vec[vec_idx].buf_addr;
1958 buf_len = buf_vec[vec_idx].buf_len;
1959 buf_avail = buf_len - buf_offset;
1960 } else if (buf_len == dev->vhost_hlen) {
1961 if (unlikely(++vec_idx >= nr_vec))
1963 buf_addr = buf_vec[vec_idx].buf_addr;
1964 buf_len = buf_vec[vec_idx].buf_len;
1967 buf_avail = buf_len;
1969 buf_offset = dev->vhost_hlen;
1970 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1974 (uintptr_t)(buf_addr + buf_offset),
1975 (uint32_t)buf_avail, 0);
1978 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1980 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1982 if (likely(cpy_len > MAX_BATCH_LEN ||
1983 vq->batch_copy_nb_elems >= vq->size ||
1984 (hdr && cur == m))) {
1985 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1987 (void *)((uintptr_t)(buf_addr +
1988 buf_offset)), cpy_len);
1990 batch_copy[vq->batch_copy_nb_elems].dst =
1991 rte_pktmbuf_mtod_offset(cur, void *,
1993 batch_copy[vq->batch_copy_nb_elems].src =
1994 (void *)((uintptr_t)(buf_addr + buf_offset));
1995 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1996 vq->batch_copy_nb_elems++;
1999 mbuf_avail -= cpy_len;
2000 mbuf_offset += cpy_len;
2001 buf_avail -= cpy_len;
2002 buf_offset += cpy_len;
2004 /* This buf reaches to its end, get the next one */
2005 if (buf_avail == 0) {
2006 if (++vec_idx >= nr_vec)
2009 buf_addr = buf_vec[vec_idx].buf_addr;
2010 buf_len = buf_vec[vec_idx].buf_len;
2013 buf_avail = buf_len;
2015 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2016 (uint32_t)buf_avail, 0);
2020 * This mbuf reaches to its end, get a new one
2021 * to hold more data.
2023 if (mbuf_avail == 0) {
2024 cur = rte_pktmbuf_alloc(mbuf_pool);
2025 if (unlikely(cur == NULL)) {
2026 VHOST_LOG_DATA(ERR, "Failed to "
2027 "allocate memory for mbuf.\n");
2033 prev->data_len = mbuf_offset;
2035 m->pkt_len += mbuf_offset;
2039 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2043 prev->data_len = mbuf_offset;
2044 m->pkt_len += mbuf_offset;
2047 vhost_dequeue_offload(hdr, m);
2055 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2061 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2063 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2064 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2069 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2070 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2072 if (unlikely(total_len > UINT16_MAX))
2075 buf_len = total_len;
2076 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2077 if (unlikely(buf == NULL))
2080 /* Initialize shinfo */
2081 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2082 virtio_dev_extbuf_free, buf);
2083 if (unlikely(shinfo == NULL)) {
2085 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2089 iova = rte_malloc_virt2iova(buf);
2090 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2091 rte_pktmbuf_reset_headroom(pkt);
2097 * Allocate a host supported pktmbuf.
2099 static __rte_always_inline struct rte_mbuf *
2100 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
2103 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
2105 if (unlikely(pkt == NULL)) {
2107 "Failed to allocate memory for mbuf.\n");
2111 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2114 /* attach an external buffer if supported */
2115 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2118 /* check if chained buffers are allowed */
2119 if (!dev->linearbuf)
2122 /* Data doesn't fit into the buffer and the host supports
2123 * only linear buffers
2125 rte_pktmbuf_free(pkt);
2130 static __rte_noinline uint16_t
2131 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2132 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2135 uint16_t free_entries;
2136 uint16_t dropped = 0;
2137 static bool allocerr_warned;
2140 * The ordering between avail index and
2141 * desc reads needs to be enforced.
2143 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2145 if (free_entries == 0)
2148 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2150 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2152 count = RTE_MIN(count, MAX_PKT_BURST);
2153 count = RTE_MIN(count, free_entries);
2154 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2157 for (i = 0; i < count; i++) {
2158 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2161 uint16_t nr_vec = 0;
2164 if (unlikely(fill_vec_buf_split(dev, vq,
2165 vq->last_avail_idx + i,
2167 &head_idx, &buf_len,
2168 VHOST_ACCESS_RO) < 0))
2171 update_shadow_used_ring_split(vq, head_idx, 0);
2173 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2174 if (unlikely(pkts[i] == NULL)) {
2176 * mbuf allocation fails for jumbo packets when external
2177 * buffer allocation is not allowed and linear buffer
2178 * is required. Drop this packet.
2180 if (!allocerr_warned) {
2182 "Failed mbuf alloc of size %d from %s on %s.\n",
2183 buf_len, mbuf_pool->name, dev->ifname);
2184 allocerr_warned = true;
2191 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2193 if (unlikely(err)) {
2194 rte_pktmbuf_free(pkts[i]);
2195 if (!allocerr_warned) {
2197 "Failed to copy desc to mbuf on %s.\n",
2199 allocerr_warned = true;
2207 vq->last_avail_idx += i;
2209 do_data_copy_dequeue(vq);
2210 if (unlikely(i < count))
2211 vq->shadow_used_idx = i;
2212 if (likely(vq->shadow_used_idx)) {
2213 flush_shadow_used_ring_split(dev, vq);
2214 vhost_vring_call_split(dev, vq);
2217 return (i - dropped);
2220 static __rte_always_inline int
2221 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2222 struct vhost_virtqueue *vq,
2223 struct rte_mempool *mbuf_pool,
2224 struct rte_mbuf **pkts,
2226 uintptr_t *desc_addrs,
2229 bool wrap = vq->avail_wrap_counter;
2230 struct vring_packed_desc *descs = vq->desc_packed;
2231 struct virtio_net_hdr *hdr;
2232 uint64_t lens[PACKED_BATCH_SIZE];
2233 uint64_t buf_lens[PACKED_BATCH_SIZE];
2234 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2237 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2239 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2242 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2243 flags = descs[avail_idx + i].flags;
2244 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2245 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2246 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2250 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2252 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2253 lens[i] = descs[avail_idx + i].len;
2255 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2256 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2257 descs[avail_idx + i].addr,
2258 &lens[i], VHOST_ACCESS_RW);
2261 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2262 if (unlikely(!desc_addrs[i]))
2264 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2268 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2269 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
2274 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2275 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2277 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2278 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2282 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2283 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
2284 pkts[i]->data_len = pkts[i]->pkt_len;
2285 ids[i] = descs[avail_idx + i].id;
2288 if (virtio_net_with_host_offload(dev)) {
2289 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2290 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2291 vhost_dequeue_offload(hdr, pkts[i]);
2298 for (i = 0; i < PACKED_BATCH_SIZE; i++)
2299 rte_pktmbuf_free(pkts[i]);
2304 static __rte_always_inline int
2305 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2306 struct vhost_virtqueue *vq,
2307 struct rte_mempool *mbuf_pool,
2308 struct rte_mbuf **pkts)
2310 uint16_t avail_idx = vq->last_avail_idx;
2311 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2312 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2313 uint16_t ids[PACKED_BATCH_SIZE];
2316 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
2317 avail_idx, desc_addrs, ids))
2320 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2321 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2323 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2324 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2325 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2328 if (virtio_net_is_inorder(dev))
2329 vhost_shadow_dequeue_batch_packed_inorder(vq,
2330 ids[PACKED_BATCH_SIZE - 1]);
2332 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2334 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2339 static __rte_always_inline int
2340 vhost_dequeue_single_packed(struct virtio_net *dev,
2341 struct vhost_virtqueue *vq,
2342 struct rte_mempool *mbuf_pool,
2343 struct rte_mbuf **pkts,
2345 uint16_t *desc_count)
2347 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2349 uint16_t nr_vec = 0;
2351 static bool allocerr_warned;
2353 if (unlikely(fill_vec_buf_packed(dev, vq,
2354 vq->last_avail_idx, desc_count,
2357 VHOST_ACCESS_RO) < 0))
2360 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2361 if (unlikely(*pkts == NULL)) {
2362 if (!allocerr_warned) {
2364 "Failed mbuf alloc of size %d from %s on %s.\n",
2365 buf_len, mbuf_pool->name, dev->ifname);
2366 allocerr_warned = true;
2371 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
2373 if (unlikely(err)) {
2374 if (!allocerr_warned) {
2376 "Failed to copy desc to mbuf on %s.\n",
2378 allocerr_warned = true;
2380 rte_pktmbuf_free(*pkts);
2387 static __rte_always_inline int
2388 virtio_dev_tx_single_packed(struct virtio_net *dev,
2389 struct vhost_virtqueue *vq,
2390 struct rte_mempool *mbuf_pool,
2391 struct rte_mbuf **pkts)
2394 uint16_t buf_id, desc_count = 0;
2397 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2400 if (likely(desc_count > 0)) {
2401 if (virtio_net_is_inorder(dev))
2402 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2405 vhost_shadow_dequeue_single_packed(vq, buf_id,
2408 vq_inc_last_avail_packed(vq, desc_count);
2414 static __rte_noinline uint16_t
2415 virtio_dev_tx_packed(struct virtio_net *dev,
2416 struct vhost_virtqueue *__rte_restrict vq,
2417 struct rte_mempool *mbuf_pool,
2418 struct rte_mbuf **__rte_restrict pkts,
2421 uint32_t pkt_idx = 0;
2422 uint32_t remained = count;
2425 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2427 if (remained >= PACKED_BATCH_SIZE) {
2428 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2430 pkt_idx += PACKED_BATCH_SIZE;
2431 remained -= PACKED_BATCH_SIZE;
2436 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2444 if (vq->shadow_used_idx) {
2445 do_data_copy_dequeue(vq);
2447 vhost_flush_dequeue_shadow_packed(dev, vq);
2448 vhost_vring_call_packed(dev, vq);
2455 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2456 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2458 struct virtio_net *dev;
2459 struct rte_mbuf *rarp_mbuf = NULL;
2460 struct vhost_virtqueue *vq;
2461 int16_t success = 1;
2463 dev = get_device(vid);
2467 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2469 "(%d) %s: built-in vhost net backend is disabled.\n",
2470 dev->vid, __func__);
2474 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2476 "(%d) %s: invalid virtqueue idx %d.\n",
2477 dev->vid, __func__, queue_id);
2481 vq = dev->virtqueue[queue_id];
2483 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2486 if (unlikely(vq->enabled == 0)) {
2488 goto out_access_unlock;
2491 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2492 vhost_user_iotlb_rd_lock(vq);
2494 if (unlikely(vq->access_ok == 0))
2495 if (unlikely(vring_translate(dev, vq) < 0)) {
2501 * Construct a RARP broadcast packet, and inject it to the "pkts"
2502 * array, to looks like that guest actually send such packet.
2504 * Check user_send_rarp() for more information.
2506 * broadcast_rarp shares a cacheline in the virtio_net structure
2507 * with some fields that are accessed during enqueue and
2508 * __atomic_compare_exchange_n causes a write if performed compare
2509 * and exchange. This could result in false sharing between enqueue
2512 * Prevent unnecessary false sharing by reading broadcast_rarp first
2513 * and only performing compare and exchange if the read indicates it
2514 * is likely to be set.
2516 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2517 __atomic_compare_exchange_n(&dev->broadcast_rarp,
2518 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2520 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2521 if (rarp_mbuf == NULL) {
2522 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2529 if (vq_is_packed(dev))
2530 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
2532 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2535 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2536 vhost_user_iotlb_rd_unlock(vq);
2539 rte_spinlock_unlock(&vq->access_lock);
2541 if (unlikely(rarp_mbuf != NULL)) {
2543 * Inject it to the head of "pkts" array, so that switch's mac
2544 * learning table will get updated first.
2546 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2547 pkts[0] = rarp_mbuf;