1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
12 #include <rte_ether.h>
14 #include <rte_vhost.h>
19 #include <rte_spinlock.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost_async.h>
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
34 static __rte_always_inline bool
35 virtio_net_is_inorder(struct virtio_net *dev)
37 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
41 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
43 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
47 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
49 struct batch_copy_elem *elem = vq->batch_copy_elems;
50 uint16_t count = vq->batch_copy_nb_elems;
53 for (i = 0; i < count; i++) {
54 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
55 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
57 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
60 vq->batch_copy_nb_elems = 0;
64 do_data_copy_dequeue(struct vhost_virtqueue *vq)
66 struct batch_copy_elem *elem = vq->batch_copy_elems;
67 uint16_t count = vq->batch_copy_nb_elems;
70 for (i = 0; i < count; i++)
71 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
73 vq->batch_copy_nb_elems = 0;
76 static __rte_always_inline void
77 do_flush_shadow_used_ring_split(struct virtio_net *dev,
78 struct vhost_virtqueue *vq,
79 uint16_t to, uint16_t from, uint16_t size)
81 rte_memcpy(&vq->used->ring[to],
82 &vq->shadow_used_split[from],
83 size * sizeof(struct vring_used_elem));
84 vhost_log_cache_used_vring(dev, vq,
85 offsetof(struct vring_used, ring[to]),
86 size * sizeof(struct vring_used_elem));
89 static __rte_always_inline void
90 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
92 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
94 if (used_idx + vq->shadow_used_idx <= vq->size) {
95 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
100 /* update used ring interval [used_idx, vq->size] */
101 size = vq->size - used_idx;
102 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
104 /* update the left half used ring interval [0, left_size] */
105 do_flush_shadow_used_ring_split(dev, vq, 0, size,
106 vq->shadow_used_idx - size);
108 vq->last_used_idx += vq->shadow_used_idx;
110 vhost_log_cache_sync(dev, vq);
112 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
114 vq->shadow_used_idx = 0;
115 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
116 sizeof(vq->used->idx));
119 static __rte_always_inline void
120 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
121 uint16_t desc_idx, uint32_t len)
123 uint16_t i = vq->shadow_used_idx++;
125 vq->shadow_used_split[i].id = desc_idx;
126 vq->shadow_used_split[i].len = len;
129 static __rte_always_inline void
130 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
131 struct vhost_virtqueue *vq)
134 uint16_t used_idx = vq->last_used_idx;
135 uint16_t head_idx = vq->last_used_idx;
136 uint16_t head_flags = 0;
138 /* Split loop in two to save memory barriers */
139 for (i = 0; i < vq->shadow_used_idx; i++) {
140 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
141 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
143 used_idx += vq->shadow_used_packed[i].count;
144 if (used_idx >= vq->size)
145 used_idx -= vq->size;
148 /* The ordering for storing desc flags needs to be enforced. */
149 rte_atomic_thread_fence(__ATOMIC_RELEASE);
151 for (i = 0; i < vq->shadow_used_idx; i++) {
154 if (vq->shadow_used_packed[i].len)
155 flags = VRING_DESC_F_WRITE;
159 if (vq->used_wrap_counter) {
160 flags |= VRING_DESC_F_USED;
161 flags |= VRING_DESC_F_AVAIL;
163 flags &= ~VRING_DESC_F_USED;
164 flags &= ~VRING_DESC_F_AVAIL;
168 vq->desc_packed[vq->last_used_idx].flags = flags;
170 vhost_log_cache_used_vring(dev, vq,
172 sizeof(struct vring_packed_desc),
173 sizeof(struct vring_packed_desc));
175 head_idx = vq->last_used_idx;
179 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
182 vq->desc_packed[head_idx].flags = head_flags;
184 vhost_log_cache_used_vring(dev, vq,
186 sizeof(struct vring_packed_desc),
187 sizeof(struct vring_packed_desc));
189 vq->shadow_used_idx = 0;
190 vhost_log_cache_sync(dev, vq);
193 static __rte_always_inline void
194 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
195 struct vhost_virtqueue *vq)
197 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
199 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
200 /* desc flags is the synchronization point for virtio packed vring */
201 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
202 used_elem->flags, __ATOMIC_RELEASE);
204 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
205 sizeof(struct vring_packed_desc),
206 sizeof(struct vring_packed_desc));
207 vq->shadow_used_idx = 0;
208 vhost_log_cache_sync(dev, vq);
211 static __rte_always_inline void
212 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
213 struct vhost_virtqueue *vq,
219 uint16_t last_used_idx;
220 struct vring_packed_desc *desc_base;
222 last_used_idx = vq->last_used_idx;
223 desc_base = &vq->desc_packed[last_used_idx];
225 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
227 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
228 desc_base[i].id = ids[i];
229 desc_base[i].len = lens[i];
232 rte_atomic_thread_fence(__ATOMIC_RELEASE);
234 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
235 desc_base[i].flags = flags;
238 vhost_log_cache_used_vring(dev, vq, last_used_idx *
239 sizeof(struct vring_packed_desc),
240 sizeof(struct vring_packed_desc) *
242 vhost_log_cache_sync(dev, vq);
244 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
247 static __rte_always_inline void
248 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
251 vq->shadow_used_packed[0].id = id;
253 if (!vq->shadow_used_idx) {
254 vq->shadow_last_used_idx = vq->last_used_idx;
255 vq->shadow_used_packed[0].flags =
256 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
257 vq->shadow_used_packed[0].len = 0;
258 vq->shadow_used_packed[0].count = 1;
259 vq->shadow_used_idx++;
262 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
265 static __rte_always_inline void
266 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
267 struct vhost_virtqueue *vq,
274 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
276 if (!vq->shadow_used_idx) {
277 vq->shadow_last_used_idx = vq->last_used_idx;
278 vq->shadow_used_packed[0].id = ids[0];
279 vq->shadow_used_packed[0].len = 0;
280 vq->shadow_used_packed[0].count = 1;
281 vq->shadow_used_packed[0].flags = flags;
282 vq->shadow_used_idx++;
287 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
288 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
289 vq->desc_packed[vq->last_used_idx + i].len = 0;
292 rte_atomic_thread_fence(__ATOMIC_RELEASE);
293 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
294 vq->desc_packed[vq->last_used_idx + i].flags = flags;
296 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
297 sizeof(struct vring_packed_desc),
298 sizeof(struct vring_packed_desc) *
300 vhost_log_cache_sync(dev, vq);
302 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
305 static __rte_always_inline void
306 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
312 flags = vq->desc_packed[vq->last_used_idx].flags;
313 if (vq->used_wrap_counter) {
314 flags |= VRING_DESC_F_USED;
315 flags |= VRING_DESC_F_AVAIL;
317 flags &= ~VRING_DESC_F_USED;
318 flags &= ~VRING_DESC_F_AVAIL;
321 if (!vq->shadow_used_idx) {
322 vq->shadow_last_used_idx = vq->last_used_idx;
324 vq->shadow_used_packed[0].id = buf_id;
325 vq->shadow_used_packed[0].len = 0;
326 vq->shadow_used_packed[0].flags = flags;
327 vq->shadow_used_idx++;
329 vq->desc_packed[vq->last_used_idx].id = buf_id;
330 vq->desc_packed[vq->last_used_idx].len = 0;
331 vq->desc_packed[vq->last_used_idx].flags = flags;
334 vq_inc_last_used_packed(vq, count);
337 static __rte_always_inline void
338 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
344 vq->shadow_used_packed[0].id = buf_id;
346 flags = vq->desc_packed[vq->last_used_idx].flags;
347 if (vq->used_wrap_counter) {
348 flags |= VRING_DESC_F_USED;
349 flags |= VRING_DESC_F_AVAIL;
351 flags &= ~VRING_DESC_F_USED;
352 flags &= ~VRING_DESC_F_AVAIL;
355 if (!vq->shadow_used_idx) {
356 vq->shadow_last_used_idx = vq->last_used_idx;
357 vq->shadow_used_packed[0].len = 0;
358 vq->shadow_used_packed[0].flags = flags;
359 vq->shadow_used_idx++;
362 vq_inc_last_used_packed(vq, count);
365 static __rte_always_inline void
366 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
370 uint16_t num_buffers)
374 for (i = 0; i < num_buffers; i++) {
375 /* enqueue shadow flush action aligned with batch num */
376 if (!vq->shadow_used_idx)
377 vq->shadow_aligned_idx = vq->last_used_idx &
379 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
380 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
381 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
382 vq->shadow_aligned_idx += count[i];
383 vq->shadow_used_idx++;
387 static __rte_always_inline void
388 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
389 struct vhost_virtqueue *vq,
393 uint16_t num_buffers)
395 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
397 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
398 do_data_copy_enqueue(dev, vq);
399 vhost_flush_enqueue_shadow_packed(dev, vq);
403 /* avoid write operation when necessary, to lessen cache issues */
404 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
405 if ((var) != (val)) \
409 static __rte_always_inline void
410 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
412 uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
414 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
415 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
418 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
419 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
422 case RTE_MBUF_F_TX_TCP_CKSUM:
423 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
426 case RTE_MBUF_F_TX_UDP_CKSUM:
427 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
430 case RTE_MBUF_F_TX_SCTP_CKSUM:
431 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
436 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
437 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
438 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
441 /* IP cksum verification cannot be bypassed, then calculate here */
442 if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
443 struct rte_ipv4_hdr *ipv4_hdr;
445 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
447 ipv4_hdr->hdr_checksum = 0;
448 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
451 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
452 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
453 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
455 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
456 net_hdr->gso_size = m_buf->tso_segsz;
457 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
459 } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
460 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
461 net_hdr->gso_size = m_buf->tso_segsz;
462 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
465 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
466 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
467 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
471 static __rte_always_inline int
472 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
473 struct buf_vector *buf_vec, uint16_t *vec_idx,
474 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
476 uint16_t vec_id = *vec_idx;
480 uint64_t desc_chunck_len = desc_len;
482 if (unlikely(vec_id >= BUF_VECTOR_MAX))
485 desc_addr = vhost_iova_to_vva(dev, vq,
489 if (unlikely(!desc_addr))
492 rte_prefetch0((void *)(uintptr_t)desc_addr);
494 buf_vec[vec_id].buf_iova = desc_iova;
495 buf_vec[vec_id].buf_addr = desc_addr;
496 buf_vec[vec_id].buf_len = desc_chunck_len;
498 desc_len -= desc_chunck_len;
499 desc_iova += desc_chunck_len;
507 static __rte_always_inline int
508 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
509 uint32_t avail_idx, uint16_t *vec_idx,
510 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
511 uint32_t *desc_chain_len, uint8_t perm)
513 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
514 uint16_t vec_id = *vec_idx;
517 uint32_t nr_descs = vq->size;
519 struct vring_desc *descs = vq->desc;
520 struct vring_desc *idesc = NULL;
522 if (unlikely(idx >= vq->size))
525 *desc_chain_head = idx;
527 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
528 dlen = vq->desc[idx].len;
529 nr_descs = dlen / sizeof(struct vring_desc);
530 if (unlikely(nr_descs > vq->size))
533 descs = (struct vring_desc *)(uintptr_t)
534 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
537 if (unlikely(!descs))
540 if (unlikely(dlen < vq->desc[idx].len)) {
542 * The indirect desc table is not contiguous
543 * in process VA space, we have to copy it.
545 idesc = vhost_alloc_copy_ind_table(dev, vq,
546 vq->desc[idx].addr, vq->desc[idx].len);
547 if (unlikely(!idesc))
557 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
558 free_ind_table(idesc);
562 dlen = descs[idx].len;
565 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
566 descs[idx].addr, dlen,
568 free_ind_table(idesc);
572 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
575 idx = descs[idx].next;
578 *desc_chain_len = len;
581 if (unlikely(!!idesc))
582 free_ind_table(idesc);
588 * Returns -1 on fail, 0 on success
591 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
592 uint32_t size, struct buf_vector *buf_vec,
593 uint16_t *num_buffers, uint16_t avail_head,
597 uint16_t vec_idx = 0;
598 uint16_t max_tries, tries = 0;
600 uint16_t head_idx = 0;
604 cur_idx = vq->last_avail_idx;
606 if (rxvq_is_mergeable(dev))
607 max_tries = vq->size - 1;
612 if (unlikely(cur_idx == avail_head))
615 * if we tried all available ring items, and still
616 * can't get enough buf, it means something abnormal
619 if (unlikely(++tries > max_tries))
622 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
625 VHOST_ACCESS_RW) < 0))
627 len = RTE_MIN(len, size);
628 update_shadow_used_ring_split(vq, head_idx, len);
640 static __rte_always_inline int
641 fill_vec_buf_packed_indirect(struct virtio_net *dev,
642 struct vhost_virtqueue *vq,
643 struct vring_packed_desc *desc, uint16_t *vec_idx,
644 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
648 uint16_t vec_id = *vec_idx;
650 struct vring_packed_desc *descs, *idescs = NULL;
653 descs = (struct vring_packed_desc *)(uintptr_t)
654 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
655 if (unlikely(!descs))
658 if (unlikely(dlen < desc->len)) {
660 * The indirect desc table is not contiguous
661 * in process VA space, we have to copy it.
663 idescs = vhost_alloc_copy_ind_table(dev,
664 vq, desc->addr, desc->len);
665 if (unlikely(!idescs))
671 nr_descs = desc->len / sizeof(struct vring_packed_desc);
672 if (unlikely(nr_descs >= vq->size)) {
673 free_ind_table(idescs);
677 for (i = 0; i < nr_descs; i++) {
678 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
679 free_ind_table(idescs);
685 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
692 if (unlikely(!!idescs))
693 free_ind_table(idescs);
698 static __rte_always_inline int
699 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
700 uint16_t avail_idx, uint16_t *desc_count,
701 struct buf_vector *buf_vec, uint16_t *vec_idx,
702 uint16_t *buf_id, uint32_t *len, uint8_t perm)
704 bool wrap_counter = vq->avail_wrap_counter;
705 struct vring_packed_desc *descs = vq->desc_packed;
706 uint16_t vec_id = *vec_idx;
709 if (avail_idx < vq->last_avail_idx)
713 * Perform a load-acquire barrier in desc_is_avail to
714 * enforce the ordering between desc flags and desc
717 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
724 if (unlikely(vec_id >= BUF_VECTOR_MAX))
727 if (unlikely(*desc_count >= vq->size))
731 *buf_id = descs[avail_idx].id;
733 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
734 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
740 dlen = descs[avail_idx].len;
743 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
744 descs[avail_idx].addr,
750 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
753 if (++avail_idx >= vq->size) {
754 avail_idx -= vq->size;
764 static __rte_noinline void
765 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
766 struct buf_vector *buf_vec,
767 struct virtio_net_hdr_mrg_rxbuf *hdr)
770 uint64_t remain = dev->vhost_hlen;
771 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
772 uint64_t iova = buf_vec->buf_iova;
775 len = RTE_MIN(remain,
777 dst = buf_vec->buf_addr;
778 rte_memcpy((void *)(uintptr_t)dst,
779 (void *)(uintptr_t)src,
782 PRINT_PACKET(dev, (uintptr_t)dst,
784 vhost_log_cache_write_iova(dev, vq,
794 static __rte_always_inline int
795 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
796 struct rte_mbuf *m, struct buf_vector *buf_vec,
797 uint16_t nr_vec, uint16_t num_buffers)
799 uint32_t vec_idx = 0;
800 uint32_t mbuf_offset, mbuf_avail;
801 uint32_t buf_offset, buf_avail;
802 uint64_t buf_addr, buf_iova, buf_len;
805 struct rte_mbuf *hdr_mbuf;
806 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
807 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
810 if (unlikely(m == NULL)) {
815 buf_addr = buf_vec[vec_idx].buf_addr;
816 buf_iova = buf_vec[vec_idx].buf_iova;
817 buf_len = buf_vec[vec_idx].buf_len;
819 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
826 if (unlikely(buf_len < dev->vhost_hlen)) {
827 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
830 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
832 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
833 dev->vid, num_buffers);
835 if (unlikely(buf_len < dev->vhost_hlen)) {
836 buf_offset = dev->vhost_hlen - buf_len;
838 buf_addr = buf_vec[vec_idx].buf_addr;
839 buf_iova = buf_vec[vec_idx].buf_iova;
840 buf_len = buf_vec[vec_idx].buf_len;
841 buf_avail = buf_len - buf_offset;
843 buf_offset = dev->vhost_hlen;
844 buf_avail = buf_len - dev->vhost_hlen;
847 mbuf_avail = rte_pktmbuf_data_len(m);
849 while (mbuf_avail != 0 || m->next != NULL) {
850 /* done with current buf, get the next one */
851 if (buf_avail == 0) {
853 if (unlikely(vec_idx >= nr_vec)) {
858 buf_addr = buf_vec[vec_idx].buf_addr;
859 buf_iova = buf_vec[vec_idx].buf_iova;
860 buf_len = buf_vec[vec_idx].buf_len;
866 /* done with current mbuf, get the next one */
867 if (mbuf_avail == 0) {
871 mbuf_avail = rte_pktmbuf_data_len(m);
875 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
876 if (rxvq_is_mergeable(dev))
877 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
880 if (unlikely(hdr == &tmp_hdr)) {
881 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
883 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
885 vhost_log_cache_write_iova(dev, vq,
893 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
895 if (likely(cpy_len > MAX_BATCH_LEN ||
896 vq->batch_copy_nb_elems >= vq->size)) {
897 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
898 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
900 vhost_log_cache_write_iova(dev, vq,
901 buf_iova + buf_offset,
903 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
906 batch_copy[vq->batch_copy_nb_elems].dst =
907 (void *)((uintptr_t)(buf_addr + buf_offset));
908 batch_copy[vq->batch_copy_nb_elems].src =
909 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
910 batch_copy[vq->batch_copy_nb_elems].log_addr =
911 buf_iova + buf_offset;
912 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
913 vq->batch_copy_nb_elems++;
916 mbuf_avail -= cpy_len;
917 mbuf_offset += cpy_len;
918 buf_avail -= cpy_len;
919 buf_offset += cpy_len;
927 static __rte_always_inline void
928 async_fill_vec(struct iovec *v, void *base, size_t len)
934 static __rte_always_inline void
935 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
936 struct iovec *vec, unsigned long nr_seg)
943 it->nr_segs = nr_seg;
950 static __rte_always_inline void
951 async_fill_desc(struct rte_vhost_async_desc *desc,
952 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
958 static __rte_always_inline int
959 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
960 struct rte_mbuf *m, struct buf_vector *buf_vec,
961 uint16_t nr_vec, uint16_t num_buffers,
962 struct iovec *src_iovec, struct iovec *dst_iovec,
963 struct rte_vhost_iov_iter *src_it,
964 struct rte_vhost_iov_iter *dst_it)
966 struct rte_mbuf *hdr_mbuf;
967 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
968 uint64_t buf_addr, buf_iova;
971 uint32_t vec_idx = 0;
972 uint32_t mbuf_offset, mbuf_avail;
973 uint32_t buf_offset, buf_avail;
974 uint32_t cpy_len, buf_len;
981 if (unlikely(m == NULL)) {
986 buf_addr = buf_vec[vec_idx].buf_addr;
987 buf_iova = buf_vec[vec_idx].buf_iova;
988 buf_len = buf_vec[vec_idx].buf_len;
990 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
997 if (unlikely(buf_len < dev->vhost_hlen)) {
998 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1001 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1003 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1004 dev->vid, num_buffers);
1006 if (unlikely(buf_len < dev->vhost_hlen)) {
1007 buf_offset = dev->vhost_hlen - buf_len;
1009 buf_addr = buf_vec[vec_idx].buf_addr;
1010 buf_iova = buf_vec[vec_idx].buf_iova;
1011 buf_len = buf_vec[vec_idx].buf_len;
1012 buf_avail = buf_len - buf_offset;
1014 buf_offset = dev->vhost_hlen;
1015 buf_avail = buf_len - dev->vhost_hlen;
1018 mbuf_avail = rte_pktmbuf_data_len(m);
1021 while (mbuf_avail != 0 || m->next != NULL) {
1022 /* done with current buf, get the next one */
1023 if (buf_avail == 0) {
1025 if (unlikely(vec_idx >= nr_vec)) {
1030 buf_addr = buf_vec[vec_idx].buf_addr;
1031 buf_iova = buf_vec[vec_idx].buf_iova;
1032 buf_len = buf_vec[vec_idx].buf_len;
1035 buf_avail = buf_len;
1038 /* done with current mbuf, get the next one */
1039 if (mbuf_avail == 0) {
1043 mbuf_avail = rte_pktmbuf_data_len(m);
1047 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1048 if (rxvq_is_mergeable(dev))
1049 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1052 if (unlikely(hdr == &tmp_hdr)) {
1053 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1055 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1056 dev->vhost_hlen, 0);
1057 vhost_log_cache_write_iova(dev, vq,
1058 buf_vec[0].buf_iova,
1065 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1067 while (unlikely(cpy_len)) {
1068 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1069 buf_iova + buf_offset,
1070 cpy_len, &mapped_len);
1071 if (unlikely(!hpa)) {
1072 VHOST_LOG_DATA(ERR, "(%d) %s: failed to get hpa.\n",
1073 dev->vid, __func__);
1078 async_fill_vec(src_iovec + tvec_idx,
1079 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1080 mbuf_offset), (size_t)mapped_len);
1081 async_fill_vec(dst_iovec + tvec_idx,
1082 hpa, (size_t)mapped_len);
1084 tlen += (uint32_t)mapped_len;
1085 cpy_len -= (uint32_t)mapped_len;
1086 mbuf_avail -= (uint32_t)mapped_len;
1087 mbuf_offset += (uint32_t)mapped_len;
1088 buf_avail -= (uint32_t)mapped_len;
1089 buf_offset += (uint32_t)mapped_len;
1094 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1095 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1100 static __rte_always_inline int
1101 vhost_enqueue_single_packed(struct virtio_net *dev,
1102 struct vhost_virtqueue *vq,
1103 struct rte_mbuf *pkt,
1104 struct buf_vector *buf_vec,
1107 uint16_t nr_vec = 0;
1108 uint16_t avail_idx = vq->last_avail_idx;
1109 uint16_t max_tries, tries = 0;
1110 uint16_t buf_id = 0;
1112 uint16_t desc_count;
1113 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1114 uint16_t num_buffers = 0;
1115 uint32_t buffer_len[vq->size];
1116 uint16_t buffer_buf_id[vq->size];
1117 uint16_t buffer_desc_count[vq->size];
1119 if (rxvq_is_mergeable(dev))
1120 max_tries = vq->size - 1;
1126 * if we tried all available ring items, and still
1127 * can't get enough buf, it means something abnormal
1130 if (unlikely(++tries > max_tries))
1133 if (unlikely(fill_vec_buf_packed(dev, vq,
1134 avail_idx, &desc_count,
1137 VHOST_ACCESS_RW) < 0))
1140 len = RTE_MIN(len, size);
1143 buffer_len[num_buffers] = len;
1144 buffer_buf_id[num_buffers] = buf_id;
1145 buffer_desc_count[num_buffers] = desc_count;
1148 *nr_descs += desc_count;
1149 avail_idx += desc_count;
1150 if (avail_idx >= vq->size)
1151 avail_idx -= vq->size;
1154 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1157 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1158 buffer_desc_count, num_buffers);
1163 static __rte_noinline uint32_t
1164 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1165 struct rte_mbuf **pkts, uint32_t count)
1167 uint32_t pkt_idx = 0;
1168 uint16_t num_buffers;
1169 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1170 uint16_t avail_head;
1173 * The ordering between avail index and
1174 * desc reads needs to be enforced.
1176 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1178 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1180 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1181 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1182 uint16_t nr_vec = 0;
1184 if (unlikely(reserve_avail_buf_split(dev, vq,
1185 pkt_len, buf_vec, &num_buffers,
1186 avail_head, &nr_vec) < 0)) {
1187 VHOST_LOG_DATA(DEBUG,
1188 "(%d) failed to get enough desc from vring\n",
1190 vq->shadow_used_idx -= num_buffers;
1194 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1195 dev->vid, vq->last_avail_idx,
1196 vq->last_avail_idx + num_buffers);
1198 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1201 vq->shadow_used_idx -= num_buffers;
1205 vq->last_avail_idx += num_buffers;
1208 do_data_copy_enqueue(dev, vq);
1210 if (likely(vq->shadow_used_idx)) {
1211 flush_shadow_used_ring_split(dev, vq);
1212 vhost_vring_call_split(dev, vq);
1218 static __rte_always_inline int
1219 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1220 struct vhost_virtqueue *vq,
1221 struct rte_mbuf **pkts,
1222 uint64_t *desc_addrs,
1225 bool wrap_counter = vq->avail_wrap_counter;
1226 struct vring_packed_desc *descs = vq->desc_packed;
1227 uint16_t avail_idx = vq->last_avail_idx;
1228 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1231 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1234 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1237 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1238 if (unlikely(pkts[i]->next != NULL))
1240 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1245 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1246 lens[i] = descs[avail_idx + i].len;
1248 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1249 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1253 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1254 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1255 descs[avail_idx + i].addr,
1259 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1260 if (unlikely(!desc_addrs[i]))
1262 if (unlikely(lens[i] != descs[avail_idx + i].len))
1269 static __rte_always_inline void
1270 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1271 struct vhost_virtqueue *vq,
1272 struct rte_mbuf **pkts,
1273 uint64_t *desc_addrs,
1276 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1277 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1278 struct vring_packed_desc *descs = vq->desc_packed;
1279 uint16_t avail_idx = vq->last_avail_idx;
1280 uint16_t ids[PACKED_BATCH_SIZE];
1283 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1284 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1285 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1286 (uintptr_t)desc_addrs[i];
1287 lens[i] = pkts[i]->pkt_len +
1288 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1291 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1292 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1294 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1296 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1297 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1298 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1302 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1303 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1306 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1307 ids[i] = descs[avail_idx + i].id;
1309 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1312 static __rte_always_inline int
1313 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1314 struct vhost_virtqueue *vq,
1315 struct rte_mbuf **pkts)
1317 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1318 uint64_t lens[PACKED_BATCH_SIZE];
1320 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1323 if (vq->shadow_used_idx) {
1324 do_data_copy_enqueue(dev, vq);
1325 vhost_flush_enqueue_shadow_packed(dev, vq);
1328 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1333 static __rte_always_inline int16_t
1334 virtio_dev_rx_single_packed(struct virtio_net *dev,
1335 struct vhost_virtqueue *vq,
1336 struct rte_mbuf *pkt)
1338 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1339 uint16_t nr_descs = 0;
1341 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1343 VHOST_LOG_DATA(DEBUG,
1344 "(%d) failed to get enough desc from vring\n",
1349 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1350 dev->vid, vq->last_avail_idx,
1351 vq->last_avail_idx + nr_descs);
1353 vq_inc_last_avail_packed(vq, nr_descs);
1358 static __rte_noinline uint32_t
1359 virtio_dev_rx_packed(struct virtio_net *dev,
1360 struct vhost_virtqueue *__rte_restrict vq,
1361 struct rte_mbuf **__rte_restrict pkts,
1364 uint32_t pkt_idx = 0;
1367 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1369 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1370 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1372 pkt_idx += PACKED_BATCH_SIZE;
1377 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1381 } while (pkt_idx < count);
1383 if (vq->shadow_used_idx) {
1384 do_data_copy_enqueue(dev, vq);
1385 vhost_flush_enqueue_shadow_packed(dev, vq);
1389 vhost_vring_call_packed(dev, vq);
1394 static __rte_always_inline uint32_t
1395 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1396 struct rte_mbuf **pkts, uint32_t count)
1398 struct vhost_virtqueue *vq;
1401 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1402 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1403 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1404 dev->vid, __func__, queue_id);
1408 vq = dev->virtqueue[queue_id];
1410 rte_spinlock_lock(&vq->access_lock);
1412 if (unlikely(!vq->enabled))
1413 goto out_access_unlock;
1415 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1416 vhost_user_iotlb_rd_lock(vq);
1418 if (unlikely(!vq->access_ok))
1419 if (unlikely(vring_translate(dev, vq) < 0))
1422 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1426 if (vq_is_packed(dev))
1427 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1429 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1432 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1433 vhost_user_iotlb_rd_unlock(vq);
1436 rte_spinlock_unlock(&vq->access_lock);
1442 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1443 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1445 struct virtio_net *dev = get_device(vid);
1450 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1452 "(%d) %s: built-in vhost net backend is disabled.\n",
1453 dev->vid, __func__);
1457 return virtio_dev_rx(dev, queue_id, pkts, count);
1460 static __rte_always_inline uint16_t
1461 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1462 uint16_t vq_size, uint16_t n_inflight)
1464 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1465 (vq_size - n_inflight + pkts_idx) % vq_size;
1468 static __rte_always_inline void
1469 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1470 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1472 size_t elem_size = sizeof(struct vring_used_elem);
1474 if (d_idx + count <= ring_size) {
1475 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1477 uint16_t size = ring_size - d_idx;
1479 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1480 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1484 static __rte_always_inline void
1485 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1486 struct vring_used_elem_packed *d_ring,
1487 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1489 size_t elem_size = sizeof(struct vring_used_elem_packed);
1491 if (d_idx + count <= ring_size) {
1492 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1494 uint16_t size = ring_size - d_idx;
1496 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1497 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1501 static __rte_noinline uint32_t
1502 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1503 struct vhost_virtqueue *vq, uint16_t queue_id,
1504 struct rte_mbuf **pkts, uint32_t count)
1506 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1507 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1508 uint16_t num_buffers;
1509 uint16_t avail_head;
1511 struct vhost_async *async = vq->async;
1512 struct rte_vhost_iov_iter *src_iter = async->src_iov_iter;
1513 struct rte_vhost_iov_iter *dst_iter = async->dst_iov_iter;
1514 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1515 struct iovec *src_iovec = async->src_iovec;
1516 struct iovec *dst_iovec = async->dst_iovec;
1517 struct async_inflight_info *pkts_info = async->pkts_info;
1518 uint32_t n_pkts = 0, pkt_err = 0;
1520 uint16_t iovec_idx = 0, it_idx = 0, slot_idx = 0;
1523 * The ordering between avail index and desc reads need to be enforced.
1525 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1527 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1529 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1530 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1531 uint16_t nr_vec = 0;
1533 if (unlikely(reserve_avail_buf_split(dev, vq,
1534 pkt_len, buf_vec, &num_buffers,
1535 avail_head, &nr_vec) < 0)) {
1536 VHOST_LOG_DATA(DEBUG,
1537 "(%d) failed to get enough desc from vring\n",
1539 vq->shadow_used_idx -= num_buffers;
1543 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1544 dev->vid, vq->last_avail_idx,
1545 vq->last_avail_idx + num_buffers);
1547 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers,
1548 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1549 &src_iter[it_idx], &dst_iter[it_idx]) < 0) {
1550 vq->shadow_used_idx -= num_buffers;
1554 async_fill_desc(&tdes[pkt_burst_idx++], &src_iter[it_idx], &dst_iter[it_idx]);
1556 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1557 pkts_info[slot_idx].descs = num_buffers;
1558 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1560 iovec_idx += src_iter[it_idx].nr_segs;
1563 vq->last_avail_idx += num_buffers;
1566 * condition to trigger async device transfer:
1567 * - unused async iov number is less than max vhost vector
1569 if (unlikely(VHOST_MAX_ASYNC_VEC - iovec_idx < BUF_VECTOR_MAX)) {
1570 n_xfer = async->ops.transfer_data(dev->vid,
1571 queue_id, tdes, 0, pkt_burst_idx);
1572 if (likely(n_xfer >= 0)) {
1576 "(%d) %s: failed to transfer data for queue id %d.\n",
1577 dev->vid, __func__, queue_id);
1584 if (unlikely(n_pkts < pkt_burst_idx)) {
1586 * log error packets number here and do actual
1587 * error processing when applications poll
1590 pkt_err = pkt_burst_idx - n_pkts;
1600 if (pkt_burst_idx) {
1601 n_xfer = async->ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
1602 if (likely(n_xfer >= 0)) {
1605 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1606 dev->vid, __func__, queue_id);
1610 if (unlikely(n_pkts < pkt_burst_idx))
1611 pkt_err = pkt_burst_idx - n_pkts;
1614 if (unlikely(pkt_err)) {
1615 uint16_t num_descs = 0;
1617 /* update number of completed packets */
1620 /* calculate the sum of descriptors to revert */
1621 while (pkt_err-- > 0) {
1622 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1626 /* recover shadow used ring and available ring */
1627 vq->shadow_used_idx -= num_descs;
1628 vq->last_avail_idx -= num_descs;
1631 /* keep used descriptors */
1632 if (likely(vq->shadow_used_idx)) {
1633 uint16_t to = async->desc_idx_split & (vq->size - 1);
1635 store_dma_desc_info_split(vq->shadow_used_split,
1636 async->descs_split, vq->size, 0, to,
1637 vq->shadow_used_idx);
1639 async->desc_idx_split += vq->shadow_used_idx;
1640 async->pkts_idx += pkt_idx;
1641 async->pkts_inflight_n += pkt_idx;
1642 vq->shadow_used_idx = 0;
1648 static __rte_always_inline void
1649 vhost_update_used_packed(struct vhost_virtqueue *vq,
1650 struct vring_used_elem_packed *shadow_ring,
1654 uint16_t used_idx = vq->last_used_idx;
1655 uint16_t head_idx = vq->last_used_idx;
1656 uint16_t head_flags = 0;
1661 /* Split loop in two to save memory barriers */
1662 for (i = 0; i < count; i++) {
1663 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1664 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1666 used_idx += shadow_ring[i].count;
1667 if (used_idx >= vq->size)
1668 used_idx -= vq->size;
1671 /* The ordering for storing desc flags needs to be enforced. */
1672 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1674 for (i = 0; i < count; i++) {
1677 if (vq->shadow_used_packed[i].len)
1678 flags = VRING_DESC_F_WRITE;
1682 if (vq->used_wrap_counter) {
1683 flags |= VRING_DESC_F_USED;
1684 flags |= VRING_DESC_F_AVAIL;
1686 flags &= ~VRING_DESC_F_USED;
1687 flags &= ~VRING_DESC_F_AVAIL;
1691 vq->desc_packed[vq->last_used_idx].flags = flags;
1693 head_idx = vq->last_used_idx;
1697 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1700 vq->desc_packed[head_idx].flags = head_flags;
1703 static __rte_always_inline int
1704 vhost_enqueue_async_packed(struct virtio_net *dev,
1705 struct vhost_virtqueue *vq,
1706 struct rte_mbuf *pkt,
1707 struct buf_vector *buf_vec,
1709 uint16_t *nr_buffers,
1710 struct iovec *src_iovec, struct iovec *dst_iovec,
1711 struct rte_vhost_iov_iter *src_it,
1712 struct rte_vhost_iov_iter *dst_it)
1714 uint16_t nr_vec = 0;
1715 uint16_t avail_idx = vq->last_avail_idx;
1716 uint16_t max_tries, tries = 0;
1717 uint16_t buf_id = 0;
1719 uint16_t desc_count = 0;
1720 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1721 uint32_t buffer_len[vq->size];
1722 uint16_t buffer_buf_id[vq->size];
1723 uint16_t buffer_desc_count[vq->size];
1725 if (rxvq_is_mergeable(dev))
1726 max_tries = vq->size - 1;
1732 * if we tried all available ring items, and still
1733 * can't get enough buf, it means something abnormal
1736 if (unlikely(++tries > max_tries))
1739 if (unlikely(fill_vec_buf_packed(dev, vq,
1740 avail_idx, &desc_count,
1743 VHOST_ACCESS_RW) < 0))
1746 len = RTE_MIN(len, size);
1749 buffer_len[*nr_buffers] = len;
1750 buffer_buf_id[*nr_buffers] = buf_id;
1751 buffer_desc_count[*nr_buffers] = desc_count;
1753 *nr_descs += desc_count;
1754 avail_idx += desc_count;
1755 if (avail_idx >= vq->size)
1756 avail_idx -= vq->size;
1759 if (unlikely(async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec,
1760 *nr_buffers, src_iovec, dst_iovec,
1761 src_it, dst_it) < 0))
1764 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1769 static __rte_always_inline int16_t
1770 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1771 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers,
1772 struct iovec *src_iovec, struct iovec *dst_iovec,
1773 struct rte_vhost_iov_iter *src_it, struct rte_vhost_iov_iter *dst_it)
1775 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1777 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec, nr_descs, nr_buffers,
1778 src_iovec, dst_iovec,
1779 src_it, dst_it) < 0)) {
1780 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1784 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1785 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1790 static __rte_always_inline void
1791 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1792 uint32_t nr_err, uint32_t *pkt_idx)
1794 uint16_t descs_err = 0;
1795 uint16_t buffers_err = 0;
1796 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1799 /* calculate the sum of buffers and descs of DMA-error packets. */
1800 while (nr_err-- > 0) {
1801 descs_err += pkts_info[slot_idx % vq->size].descs;
1802 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1806 if (vq->last_avail_idx >= descs_err) {
1807 vq->last_avail_idx -= descs_err;
1809 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1810 vq->avail_wrap_counter ^= 1;
1813 vq->shadow_used_idx -= buffers_err;
1816 static __rte_noinline uint32_t
1817 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1818 struct vhost_virtqueue *vq, uint16_t queue_id,
1819 struct rte_mbuf **pkts, uint32_t count)
1821 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1822 uint32_t remained = count;
1824 uint16_t num_buffers;
1827 struct vhost_async *async = vq->async;
1828 struct rte_vhost_iov_iter *src_iter = async->src_iov_iter;
1829 struct rte_vhost_iov_iter *dst_iter = async->dst_iov_iter;
1830 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1831 struct iovec *src_iovec = async->src_iovec;
1832 struct iovec *dst_iovec = async->dst_iovec;
1833 struct async_inflight_info *pkts_info = async->pkts_info;
1834 uint32_t n_pkts = 0, pkt_err = 0;
1835 uint16_t slot_idx = 0;
1836 uint16_t iovec_idx = 0, it_idx = 0;
1839 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1843 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1844 &num_descs, &num_buffers,
1845 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1846 &src_iter[it_idx], &dst_iter[it_idx]) < 0))
1849 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1851 async_fill_desc(&tdes[pkt_burst_idx++], &src_iter[it_idx], &dst_iter[it_idx]);
1852 pkts_info[slot_idx].descs = num_descs;
1853 pkts_info[slot_idx].nr_buffers = num_buffers;
1854 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1855 iovec_idx += src_iter[it_idx].nr_segs;
1860 vq_inc_last_avail_packed(vq, num_descs);
1863 * condition to trigger async device transfer:
1864 * - unused async iov number is less than max vhost vector
1866 if (unlikely(VHOST_MAX_ASYNC_VEC - iovec_idx < BUF_VECTOR_MAX)) {
1867 n_xfer = async->ops.transfer_data(dev->vid,
1868 queue_id, tdes, 0, pkt_burst_idx);
1869 if (likely(n_xfer >= 0)) {
1873 "(%d) %s: failed to transfer data for queue id %d.\n",
1874 dev->vid, __func__, queue_id);
1881 if (unlikely(n_pkts < pkt_burst_idx)) {
1883 * log error packets number here and do actual
1884 * error processing when applications poll
1887 pkt_err = pkt_burst_idx - n_pkts;
1894 } while (pkt_idx < count);
1896 if (pkt_burst_idx) {
1897 n_xfer = async->ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
1898 if (likely(n_xfer >= 0)) {
1901 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1902 dev->vid, __func__, queue_id);
1906 if (unlikely(n_pkts < pkt_burst_idx))
1907 pkt_err = pkt_burst_idx - n_pkts;
1910 if (unlikely(pkt_err))
1911 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1913 if (likely(vq->shadow_used_idx)) {
1914 /* keep used descriptors. */
1915 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1916 vq->size, 0, async->buffer_idx_packed,
1917 vq->shadow_used_idx);
1919 async->buffer_idx_packed += vq->shadow_used_idx;
1920 if (async->buffer_idx_packed >= vq->size)
1921 async->buffer_idx_packed -= vq->size;
1923 async->pkts_idx += pkt_idx;
1924 if (async->pkts_idx >= vq->size)
1925 async->pkts_idx -= vq->size;
1927 vq->shadow_used_idx = 0;
1928 async->pkts_inflight_n += pkt_idx;
1934 static __rte_always_inline void
1935 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1937 struct vhost_async *async = vq->async;
1938 uint16_t nr_left = n_descs;
1943 from = async->last_desc_idx_split & (vq->size - 1);
1944 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1945 to = vq->last_used_idx & (vq->size - 1);
1947 if (to + nr_copy <= vq->size) {
1948 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1949 nr_copy * sizeof(struct vring_used_elem));
1951 uint16_t size = vq->size - to;
1953 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1954 size * sizeof(struct vring_used_elem));
1955 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1956 (nr_copy - size) * sizeof(struct vring_used_elem));
1959 async->last_desc_idx_split += nr_copy;
1960 vq->last_used_idx += nr_copy;
1962 } while (nr_left > 0);
1965 static __rte_always_inline void
1966 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1969 struct vhost_async *async = vq->async;
1970 uint16_t nr_left = n_buffers;
1974 from = async->last_buffer_idx_packed;
1975 to = (from + nr_left) % vq->size;
1977 vhost_update_used_packed(vq, async->buffers_packed + from, to - from);
1978 async->last_buffer_idx_packed += nr_left;
1981 vhost_update_used_packed(vq, async->buffers_packed + from,
1983 async->last_buffer_idx_packed = 0;
1984 nr_left -= vq->size - from;
1986 } while (nr_left > 0);
1989 static __rte_always_inline uint16_t
1990 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1991 struct rte_mbuf **pkts, uint16_t count)
1993 struct vhost_virtqueue *vq;
1994 struct vhost_async *async;
1995 struct async_inflight_info *pkts_info;
1997 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0, n_buffers = 0;
1998 uint16_t start_idx, pkts_idx, vq_size;
2001 vq = dev->virtqueue[queue_id];
2003 pkts_idx = async->pkts_idx % vq->size;
2004 pkts_info = async->pkts_info;
2006 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
2007 vq_size, async->pkts_inflight_n);
2009 if (count > async->last_pkts_n) {
2010 n_cpl = async->ops.check_completed_copies(dev->vid,
2011 queue_id, 0, count - async->last_pkts_n);
2012 if (likely(n_cpl >= 0)) {
2016 "(%d) %s: failed to check completed copies for queue id %d.\n",
2017 dev->vid, __func__, queue_id);
2022 n_pkts_cpl += async->last_pkts_n;
2023 n_pkts_put = RTE_MIN(n_pkts_cpl, count);
2024 if (unlikely(n_pkts_put == 0)) {
2025 async->last_pkts_n = n_pkts_cpl;
2029 if (vq_is_packed(dev)) {
2030 for (i = 0; i < n_pkts_put; i++) {
2031 from = (start_idx + i) % vq_size;
2032 n_buffers += pkts_info[from].nr_buffers;
2033 pkts[i] = pkts_info[from].mbuf;
2036 for (i = 0; i < n_pkts_put; i++) {
2037 from = (start_idx + i) & (vq_size - 1);
2038 n_descs += pkts_info[from].descs;
2039 pkts[i] = pkts_info[from].mbuf;
2042 async->last_pkts_n = n_pkts_cpl - n_pkts_put;
2043 async->pkts_inflight_n -= n_pkts_put;
2045 if (likely(vq->enabled && vq->access_ok)) {
2046 if (vq_is_packed(dev)) {
2047 write_back_completed_descs_packed(vq, n_buffers);
2049 vhost_vring_call_packed(dev, vq);
2051 write_back_completed_descs_split(vq, n_descs);
2053 __atomic_add_fetch(&vq->used->idx, n_descs,
2055 vhost_vring_call_split(dev, vq);
2058 if (vq_is_packed(dev)) {
2059 async->last_buffer_idx_packed += n_buffers;
2060 if (async->last_buffer_idx_packed >= vq->size)
2061 async->last_buffer_idx_packed -= vq->size;
2063 async->last_desc_idx_split += n_descs;
2071 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2072 struct rte_mbuf **pkts, uint16_t count)
2074 struct virtio_net *dev = get_device(vid);
2075 struct vhost_virtqueue *vq;
2076 uint16_t n_pkts_cpl = 0;
2081 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2082 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2083 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2084 dev->vid, __func__, queue_id);
2088 vq = dev->virtqueue[queue_id];
2090 if (unlikely(!vq->async)) {
2091 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2092 dev->vid, __func__, queue_id);
2096 rte_spinlock_lock(&vq->access_lock);
2098 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2100 rte_spinlock_unlock(&vq->access_lock);
2106 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2107 struct rte_mbuf **pkts, uint16_t count)
2109 struct virtio_net *dev = get_device(vid);
2110 struct vhost_virtqueue *vq;
2111 uint16_t n_pkts_cpl = 0;
2116 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2117 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2118 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2119 dev->vid, __func__, queue_id);
2123 vq = dev->virtqueue[queue_id];
2125 if (unlikely(!vq->async)) {
2126 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2127 dev->vid, __func__, queue_id);
2131 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2136 static __rte_always_inline uint32_t
2137 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2138 struct rte_mbuf **pkts, uint32_t count)
2140 struct vhost_virtqueue *vq;
2143 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2144 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2145 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2146 dev->vid, __func__, queue_id);
2150 vq = dev->virtqueue[queue_id];
2152 rte_spinlock_lock(&vq->access_lock);
2154 if (unlikely(!vq->enabled || !vq->async))
2155 goto out_access_unlock;
2157 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2158 vhost_user_iotlb_rd_lock(vq);
2160 if (unlikely(!vq->access_ok))
2161 if (unlikely(vring_translate(dev, vq) < 0))
2164 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2168 if (vq_is_packed(dev))
2169 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2172 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2176 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2177 vhost_user_iotlb_rd_unlock(vq);
2180 rte_spinlock_unlock(&vq->access_lock);
2186 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2187 struct rte_mbuf **pkts, uint16_t count)
2189 struct virtio_net *dev = get_device(vid);
2194 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2196 "(%d) %s: built-in vhost net backend is disabled.\n",
2197 dev->vid, __func__);
2201 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2205 virtio_net_with_host_offload(struct virtio_net *dev)
2208 ((1ULL << VIRTIO_NET_F_CSUM) |
2209 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2210 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2211 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2212 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2219 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2221 struct rte_ipv4_hdr *ipv4_hdr;
2222 struct rte_ipv6_hdr *ipv6_hdr;
2223 struct rte_ether_hdr *eth_hdr;
2225 uint16_t data_len = rte_pktmbuf_data_len(m);
2227 if (data_len < sizeof(struct rte_ether_hdr))
2230 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2232 m->l2_len = sizeof(struct rte_ether_hdr);
2233 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2235 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2236 if (data_len < sizeof(struct rte_ether_hdr) +
2237 sizeof(struct rte_vlan_hdr))
2240 struct rte_vlan_hdr *vlan_hdr =
2241 (struct rte_vlan_hdr *)(eth_hdr + 1);
2243 m->l2_len += sizeof(struct rte_vlan_hdr);
2244 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2247 switch (ethertype) {
2248 case RTE_ETHER_TYPE_IPV4:
2249 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2251 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2253 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2254 if (data_len < m->l2_len + m->l3_len)
2256 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2257 *l4_proto = ipv4_hdr->next_proto_id;
2259 case RTE_ETHER_TYPE_IPV6:
2260 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2262 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2264 m->l3_len = sizeof(struct rte_ipv6_hdr);
2265 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2266 *l4_proto = ipv6_hdr->proto;
2269 /* a valid L3 header is needed for further L4 parsing */
2273 /* both CSUM and GSO need a valid L4 header */
2274 switch (*l4_proto) {
2276 if (data_len < m->l2_len + m->l3_len +
2277 sizeof(struct rte_tcp_hdr))
2281 if (data_len < m->l2_len + m->l3_len +
2282 sizeof(struct rte_udp_hdr))
2286 if (data_len < m->l2_len + m->l3_len +
2287 sizeof(struct rte_sctp_hdr))
2303 static __rte_always_inline void
2304 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2306 uint8_t l4_proto = 0;
2307 struct rte_tcp_hdr *tcp_hdr = NULL;
2309 uint16_t data_len = rte_pktmbuf_data_len(m);
2311 if (parse_headers(m, &l4_proto) < 0)
2314 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2315 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2316 switch (hdr->csum_offset) {
2317 case (offsetof(struct rte_tcp_hdr, cksum)):
2318 if (l4_proto != IPPROTO_TCP)
2320 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2322 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2323 if (l4_proto != IPPROTO_UDP)
2325 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2327 case (offsetof(struct rte_sctp_hdr, cksum)):
2328 if (l4_proto != IPPROTO_SCTP)
2330 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2340 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2341 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2342 case VIRTIO_NET_HDR_GSO_TCPV4:
2343 case VIRTIO_NET_HDR_GSO_TCPV6:
2344 if (l4_proto != IPPROTO_TCP)
2346 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2347 struct rte_tcp_hdr *,
2348 m->l2_len + m->l3_len);
2349 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2350 if (data_len < m->l2_len + m->l3_len + tcp_len)
2352 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2353 m->tso_segsz = hdr->gso_size;
2354 m->l4_len = tcp_len;
2356 case VIRTIO_NET_HDR_GSO_UDP:
2357 if (l4_proto != IPPROTO_UDP)
2359 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2360 m->tso_segsz = hdr->gso_size;
2361 m->l4_len = sizeof(struct rte_udp_hdr);
2364 VHOST_LOG_DATA(WARNING,
2365 "unsupported gso type %u.\n", hdr->gso_type);
2377 static __rte_always_inline void
2378 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2379 bool legacy_ol_flags)
2381 struct rte_net_hdr_lens hdr_lens;
2382 int l4_supported = 0;
2385 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2388 if (legacy_ol_flags) {
2389 vhost_dequeue_offload_legacy(hdr, m);
2393 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2395 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2396 m->packet_type = ptype;
2397 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2398 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2399 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2402 /* According to Virtio 1.1 spec, the device only needs to look at
2403 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2404 * This differs from the processing incoming packets path where the
2405 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2408 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2409 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2410 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2412 * 5.1.6.2.2 Device Requirements: Packet Transmission
2413 * The device MUST ignore flag bits that it does not recognize.
2415 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2418 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2419 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2420 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2422 /* Unknown proto or tunnel, do sw cksum. We can assume
2423 * the cksum field is in the first segment since the
2424 * buffers we provided to the host are large enough.
2425 * In case of SCTP, this will be wrong since it's a CRC
2426 * but there's nothing we can do.
2428 uint16_t csum = 0, off;
2430 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2431 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2433 if (likely(csum != 0xffff))
2435 off = hdr->csum_offset + hdr->csum_start;
2436 if (rte_pktmbuf_data_len(m) >= off + 1)
2437 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2441 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2442 if (hdr->gso_size == 0)
2445 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2446 case VIRTIO_NET_HDR_GSO_TCPV4:
2447 case VIRTIO_NET_HDR_GSO_TCPV6:
2448 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2450 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2451 m->tso_segsz = hdr->gso_size;
2453 case VIRTIO_NET_HDR_GSO_UDP:
2454 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2456 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2457 m->tso_segsz = hdr->gso_size;
2465 static __rte_noinline void
2466 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2467 struct buf_vector *buf_vec)
2470 uint64_t remain = sizeof(struct virtio_net_hdr);
2472 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2475 len = RTE_MIN(remain, buf_vec->buf_len);
2476 src = buf_vec->buf_addr;
2477 rte_memcpy((void *)(uintptr_t)dst,
2478 (void *)(uintptr_t)src, len);
2486 static __rte_always_inline int
2487 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2488 struct buf_vector *buf_vec, uint16_t nr_vec,
2489 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2490 bool legacy_ol_flags)
2492 uint32_t buf_avail, buf_offset;
2493 uint64_t buf_addr, buf_len;
2494 uint32_t mbuf_avail, mbuf_offset;
2496 struct rte_mbuf *cur = m, *prev = m;
2497 struct virtio_net_hdr tmp_hdr;
2498 struct virtio_net_hdr *hdr = NULL;
2499 /* A counter to avoid desc dead loop chain */
2500 uint16_t vec_idx = 0;
2501 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2504 buf_addr = buf_vec[vec_idx].buf_addr;
2505 buf_len = buf_vec[vec_idx].buf_len;
2507 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2512 if (virtio_net_with_host_offload(dev)) {
2513 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2515 * No luck, the virtio-net header doesn't fit
2516 * in a contiguous virtual area.
2518 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2521 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2526 * A virtio driver normally uses at least 2 desc buffers
2527 * for Tx: the first for storing the header, and others
2528 * for storing the data.
2530 if (unlikely(buf_len < dev->vhost_hlen)) {
2531 buf_offset = dev->vhost_hlen - buf_len;
2533 buf_addr = buf_vec[vec_idx].buf_addr;
2534 buf_len = buf_vec[vec_idx].buf_len;
2535 buf_avail = buf_len - buf_offset;
2536 } else if (buf_len == dev->vhost_hlen) {
2537 if (unlikely(++vec_idx >= nr_vec))
2539 buf_addr = buf_vec[vec_idx].buf_addr;
2540 buf_len = buf_vec[vec_idx].buf_len;
2543 buf_avail = buf_len;
2545 buf_offset = dev->vhost_hlen;
2546 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2550 (uintptr_t)(buf_addr + buf_offset),
2551 (uint32_t)buf_avail, 0);
2554 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2556 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2558 if (likely(cpy_len > MAX_BATCH_LEN ||
2559 vq->batch_copy_nb_elems >= vq->size ||
2560 (hdr && cur == m))) {
2561 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2563 (void *)((uintptr_t)(buf_addr +
2564 buf_offset)), cpy_len);
2566 batch_copy[vq->batch_copy_nb_elems].dst =
2567 rte_pktmbuf_mtod_offset(cur, void *,
2569 batch_copy[vq->batch_copy_nb_elems].src =
2570 (void *)((uintptr_t)(buf_addr + buf_offset));
2571 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2572 vq->batch_copy_nb_elems++;
2575 mbuf_avail -= cpy_len;
2576 mbuf_offset += cpy_len;
2577 buf_avail -= cpy_len;
2578 buf_offset += cpy_len;
2580 /* This buf reaches to its end, get the next one */
2581 if (buf_avail == 0) {
2582 if (++vec_idx >= nr_vec)
2585 buf_addr = buf_vec[vec_idx].buf_addr;
2586 buf_len = buf_vec[vec_idx].buf_len;
2589 buf_avail = buf_len;
2591 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2592 (uint32_t)buf_avail, 0);
2596 * This mbuf reaches to its end, get a new one
2597 * to hold more data.
2599 if (mbuf_avail == 0) {
2600 cur = rte_pktmbuf_alloc(mbuf_pool);
2601 if (unlikely(cur == NULL)) {
2602 VHOST_LOG_DATA(ERR, "Failed to "
2603 "allocate memory for mbuf.\n");
2609 prev->data_len = mbuf_offset;
2611 m->pkt_len += mbuf_offset;
2615 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2619 prev->data_len = mbuf_offset;
2620 m->pkt_len += mbuf_offset;
2623 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2631 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2637 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2639 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2640 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2645 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2646 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2648 if (unlikely(total_len > UINT16_MAX))
2651 buf_len = total_len;
2652 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2653 if (unlikely(buf == NULL))
2656 /* Initialize shinfo */
2657 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2658 virtio_dev_extbuf_free, buf);
2659 if (unlikely(shinfo == NULL)) {
2661 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2665 iova = rte_malloc_virt2iova(buf);
2666 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2667 rte_pktmbuf_reset_headroom(pkt);
2673 * Prepare a host supported pktmbuf.
2675 static __rte_always_inline int
2676 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2679 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2682 /* attach an external buffer if supported */
2683 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2686 /* check if chained buffers are allowed */
2687 if (!dev->linearbuf)
2695 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2696 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2697 bool legacy_ol_flags)
2700 uint16_t free_entries;
2701 uint16_t dropped = 0;
2702 static bool allocerr_warned;
2705 * The ordering between avail index and
2706 * desc reads needs to be enforced.
2708 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2710 if (free_entries == 0)
2713 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2715 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2717 count = RTE_MIN(count, MAX_PKT_BURST);
2718 count = RTE_MIN(count, free_entries);
2719 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2722 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2725 for (i = 0; i < count; i++) {
2726 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2729 uint16_t nr_vec = 0;
2732 if (unlikely(fill_vec_buf_split(dev, vq,
2733 vq->last_avail_idx + i,
2735 &head_idx, &buf_len,
2736 VHOST_ACCESS_RO) < 0))
2739 update_shadow_used_ring_split(vq, head_idx, 0);
2741 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2742 if (unlikely(err)) {
2744 * mbuf allocation fails for jumbo packets when external
2745 * buffer allocation is not allowed and linear buffer
2746 * is required. Drop this packet.
2748 if (!allocerr_warned) {
2750 "Failed mbuf alloc of size %d from %s on %s.\n",
2751 buf_len, mbuf_pool->name, dev->ifname);
2752 allocerr_warned = true;
2759 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2760 mbuf_pool, legacy_ol_flags);
2761 if (unlikely(err)) {
2762 if (!allocerr_warned) {
2764 "Failed to copy desc to mbuf on %s.\n",
2766 allocerr_warned = true;
2775 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2777 vq->last_avail_idx += i;
2779 do_data_copy_dequeue(vq);
2780 if (unlikely(i < count))
2781 vq->shadow_used_idx = i;
2782 if (likely(vq->shadow_used_idx)) {
2783 flush_shadow_used_ring_split(dev, vq);
2784 vhost_vring_call_split(dev, vq);
2787 return (i - dropped);
2792 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2793 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2794 struct rte_mbuf **pkts, uint16_t count)
2796 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2801 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2802 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2803 struct rte_mbuf **pkts, uint16_t count)
2805 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2808 static __rte_always_inline int
2809 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2810 struct vhost_virtqueue *vq,
2811 struct rte_mbuf **pkts,
2813 uintptr_t *desc_addrs,
2816 bool wrap = vq->avail_wrap_counter;
2817 struct vring_packed_desc *descs = vq->desc_packed;
2818 uint64_t lens[PACKED_BATCH_SIZE];
2819 uint64_t buf_lens[PACKED_BATCH_SIZE];
2820 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2823 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2825 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2828 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2829 flags = descs[avail_idx + i].flags;
2830 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2831 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2832 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2836 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2838 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2839 lens[i] = descs[avail_idx + i].len;
2841 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2842 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2843 descs[avail_idx + i].addr,
2844 &lens[i], VHOST_ACCESS_RW);
2847 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2848 if (unlikely(!desc_addrs[i]))
2850 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2854 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2855 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2859 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2860 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2862 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2863 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2867 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2868 pkts[i]->pkt_len = lens[i] - buf_offset;
2869 pkts[i]->data_len = pkts[i]->pkt_len;
2870 ids[i] = descs[avail_idx + i].id;
2879 static __rte_always_inline int
2880 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2881 struct vhost_virtqueue *vq,
2882 struct rte_mbuf **pkts,
2883 bool legacy_ol_flags)
2885 uint16_t avail_idx = vq->last_avail_idx;
2886 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2887 struct virtio_net_hdr *hdr;
2888 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2889 uint16_t ids[PACKED_BATCH_SIZE];
2892 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2896 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2897 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2899 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2900 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2901 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2904 if (virtio_net_with_host_offload(dev)) {
2905 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2906 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2907 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2911 if (virtio_net_is_inorder(dev))
2912 vhost_shadow_dequeue_batch_packed_inorder(vq,
2913 ids[PACKED_BATCH_SIZE - 1]);
2915 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2917 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2922 static __rte_always_inline int
2923 vhost_dequeue_single_packed(struct virtio_net *dev,
2924 struct vhost_virtqueue *vq,
2925 struct rte_mempool *mbuf_pool,
2926 struct rte_mbuf *pkts,
2928 uint16_t *desc_count,
2929 bool legacy_ol_flags)
2931 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2933 uint16_t nr_vec = 0;
2935 static bool allocerr_warned;
2937 if (unlikely(fill_vec_buf_packed(dev, vq,
2938 vq->last_avail_idx, desc_count,
2941 VHOST_ACCESS_RO) < 0))
2944 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2945 if (!allocerr_warned) {
2947 "Failed mbuf alloc of size %d from %s on %s.\n",
2948 buf_len, mbuf_pool->name, dev->ifname);
2949 allocerr_warned = true;
2954 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2955 mbuf_pool, legacy_ol_flags);
2956 if (unlikely(err)) {
2957 if (!allocerr_warned) {
2959 "Failed to copy desc to mbuf on %s.\n",
2961 allocerr_warned = true;
2969 static __rte_always_inline int
2970 virtio_dev_tx_single_packed(struct virtio_net *dev,
2971 struct vhost_virtqueue *vq,
2972 struct rte_mempool *mbuf_pool,
2973 struct rte_mbuf *pkts,
2974 bool legacy_ol_flags)
2977 uint16_t buf_id, desc_count = 0;
2980 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2981 &desc_count, legacy_ol_flags);
2983 if (likely(desc_count > 0)) {
2984 if (virtio_net_is_inorder(dev))
2985 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2988 vhost_shadow_dequeue_single_packed(vq, buf_id,
2991 vq_inc_last_avail_packed(vq, desc_count);
2999 virtio_dev_tx_packed(struct virtio_net *dev,
3000 struct vhost_virtqueue *__rte_restrict vq,
3001 struct rte_mempool *mbuf_pool,
3002 struct rte_mbuf **__rte_restrict pkts,
3004 bool legacy_ol_flags)
3006 uint32_t pkt_idx = 0;
3008 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
3012 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3014 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3015 if (!virtio_dev_tx_batch_packed(dev, vq,
3018 pkt_idx += PACKED_BATCH_SIZE;
3023 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3028 } while (pkt_idx < count);
3030 if (pkt_idx != count)
3031 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3033 if (vq->shadow_used_idx) {
3034 do_data_copy_dequeue(vq);
3036 vhost_flush_dequeue_shadow_packed(dev, vq);
3037 vhost_vring_call_packed(dev, vq);
3045 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3046 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3047 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3049 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3054 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3055 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3056 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3058 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3062 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3063 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3065 struct virtio_net *dev;
3066 struct rte_mbuf *rarp_mbuf = NULL;
3067 struct vhost_virtqueue *vq;
3068 int16_t success = 1;
3070 dev = get_device(vid);
3074 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3076 "(%d) %s: built-in vhost net backend is disabled.\n",
3077 dev->vid, __func__);
3081 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3083 "(%d) %s: invalid virtqueue idx %d.\n",
3084 dev->vid, __func__, queue_id);
3088 vq = dev->virtqueue[queue_id];
3090 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3093 if (unlikely(!vq->enabled)) {
3095 goto out_access_unlock;
3098 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3099 vhost_user_iotlb_rd_lock(vq);
3101 if (unlikely(!vq->access_ok))
3102 if (unlikely(vring_translate(dev, vq) < 0)) {
3108 * Construct a RARP broadcast packet, and inject it to the "pkts"
3109 * array, to looks like that guest actually send such packet.
3111 * Check user_send_rarp() for more information.
3113 * broadcast_rarp shares a cacheline in the virtio_net structure
3114 * with some fields that are accessed during enqueue and
3115 * __atomic_compare_exchange_n causes a write if performed compare
3116 * and exchange. This could result in false sharing between enqueue
3119 * Prevent unnecessary false sharing by reading broadcast_rarp first
3120 * and only performing compare and exchange if the read indicates it
3121 * is likely to be set.
3123 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3124 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3125 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3127 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3128 if (rarp_mbuf == NULL) {
3129 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3134 * Inject it to the head of "pkts" array, so that switch's mac
3135 * learning table will get updated first.
3137 pkts[0] = rarp_mbuf;
3142 if (vq_is_packed(dev)) {
3143 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3144 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3146 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3148 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3149 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3151 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3155 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3156 vhost_user_iotlb_rd_unlock(vq);
3159 rte_spinlock_unlock(&vq->access_lock);
3161 if (unlikely(rarp_mbuf != NULL))