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 int
928 async_iter_initialize(struct vhost_async *async)
930 struct rte_vhost_iov_iter *iter;
932 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
933 VHOST_LOG_DATA(ERR, "no more async iovec available\n");
937 iter = async->iov_iter + async->iter_idx;
938 iter->iov = async->iovec + async->iovec_idx;
944 static __rte_always_inline int
945 async_iter_add_iovec(struct vhost_async *async, void *src, void *dst, size_t len)
947 struct rte_vhost_iov_iter *iter;
948 struct rte_vhost_iovec *iovec;
950 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
951 static bool vhost_max_async_vec_log;
953 if (!vhost_max_async_vec_log) {
954 VHOST_LOG_DATA(ERR, "no more async iovec available\n");
955 vhost_max_async_vec_log = true;
961 iter = async->iov_iter + async->iter_idx;
962 iovec = async->iovec + async->iovec_idx;
964 iovec->src_addr = src;
965 iovec->dst_addr = dst;
974 static __rte_always_inline void
975 async_iter_finalize(struct vhost_async *async)
980 static __rte_always_inline void
981 async_iter_cancel(struct vhost_async *async)
983 struct rte_vhost_iov_iter *iter;
985 iter = async->iov_iter + async->iter_idx;
986 async->iovec_idx -= iter->nr_segs;
991 static __rte_always_inline void
992 async_iter_reset(struct vhost_async *async)
995 async->iovec_idx = 0;
998 static __rte_always_inline int
999 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1000 struct rte_mbuf *m, struct buf_vector *buf_vec,
1001 uint16_t nr_vec, uint16_t num_buffers)
1003 struct vhost_async *async = vq->async;
1004 struct rte_mbuf *hdr_mbuf;
1005 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1006 uint64_t buf_addr, buf_iova;
1008 uint64_t mapped_len;
1009 uint32_t vec_idx = 0;
1010 uint32_t mbuf_offset, mbuf_avail;
1011 uint32_t buf_offset, buf_avail;
1012 uint32_t cpy_len, buf_len;
1016 if (unlikely(m == NULL))
1019 buf_addr = buf_vec[vec_idx].buf_addr;
1020 buf_iova = buf_vec[vec_idx].buf_iova;
1021 buf_len = buf_vec[vec_idx].buf_len;
1023 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1027 hdr_addr = buf_addr;
1028 if (unlikely(buf_len < dev->vhost_hlen)) {
1029 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1032 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1034 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1035 dev->vid, num_buffers);
1037 if (unlikely(buf_len < dev->vhost_hlen)) {
1038 buf_offset = dev->vhost_hlen - buf_len;
1040 buf_addr = buf_vec[vec_idx].buf_addr;
1041 buf_iova = buf_vec[vec_idx].buf_iova;
1042 buf_len = buf_vec[vec_idx].buf_len;
1043 buf_avail = buf_len - buf_offset;
1045 buf_offset = dev->vhost_hlen;
1046 buf_avail = buf_len - dev->vhost_hlen;
1049 mbuf_avail = rte_pktmbuf_data_len(m);
1052 if (async_iter_initialize(async))
1055 while (mbuf_avail != 0 || m->next != NULL) {
1056 /* done with current buf, get the next one */
1057 if (buf_avail == 0) {
1059 if (unlikely(vec_idx >= nr_vec))
1062 buf_addr = buf_vec[vec_idx].buf_addr;
1063 buf_iova = buf_vec[vec_idx].buf_iova;
1064 buf_len = buf_vec[vec_idx].buf_len;
1067 buf_avail = buf_len;
1070 /* done with current mbuf, get the next one */
1071 if (mbuf_avail == 0) {
1075 mbuf_avail = rte_pktmbuf_data_len(m);
1079 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1080 if (rxvq_is_mergeable(dev))
1081 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1084 if (unlikely(hdr == &tmp_hdr)) {
1085 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1087 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1088 dev->vhost_hlen, 0);
1089 vhost_log_cache_write_iova(dev, vq,
1090 buf_vec[0].buf_iova,
1097 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1099 while (unlikely(cpy_len)) {
1100 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1101 buf_iova + buf_offset,
1102 cpy_len, &mapped_len);
1103 if (unlikely(!hpa)) {
1104 VHOST_LOG_DATA(ERR, "(%d) %s: failed to get hpa.\n",
1105 dev->vid, __func__);
1109 if (unlikely(async_iter_add_iovec(async,
1110 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1112 hpa, (size_t)mapped_len)))
1115 cpy_len -= (uint32_t)mapped_len;
1116 mbuf_avail -= (uint32_t)mapped_len;
1117 mbuf_offset += (uint32_t)mapped_len;
1118 buf_avail -= (uint32_t)mapped_len;
1119 buf_offset += (uint32_t)mapped_len;
1123 async_iter_finalize(async);
1127 async_iter_cancel(async);
1132 static __rte_always_inline int
1133 vhost_enqueue_single_packed(struct virtio_net *dev,
1134 struct vhost_virtqueue *vq,
1135 struct rte_mbuf *pkt,
1136 struct buf_vector *buf_vec,
1139 uint16_t nr_vec = 0;
1140 uint16_t avail_idx = vq->last_avail_idx;
1141 uint16_t max_tries, tries = 0;
1142 uint16_t buf_id = 0;
1144 uint16_t desc_count;
1145 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1146 uint16_t num_buffers = 0;
1147 uint32_t buffer_len[vq->size];
1148 uint16_t buffer_buf_id[vq->size];
1149 uint16_t buffer_desc_count[vq->size];
1151 if (rxvq_is_mergeable(dev))
1152 max_tries = vq->size - 1;
1158 * if we tried all available ring items, and still
1159 * can't get enough buf, it means something abnormal
1162 if (unlikely(++tries > max_tries))
1165 if (unlikely(fill_vec_buf_packed(dev, vq,
1166 avail_idx, &desc_count,
1169 VHOST_ACCESS_RW) < 0))
1172 len = RTE_MIN(len, size);
1175 buffer_len[num_buffers] = len;
1176 buffer_buf_id[num_buffers] = buf_id;
1177 buffer_desc_count[num_buffers] = desc_count;
1180 *nr_descs += desc_count;
1181 avail_idx += desc_count;
1182 if (avail_idx >= vq->size)
1183 avail_idx -= vq->size;
1186 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1189 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1190 buffer_desc_count, num_buffers);
1195 static __rte_noinline uint32_t
1196 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1197 struct rte_mbuf **pkts, uint32_t count)
1199 uint32_t pkt_idx = 0;
1200 uint16_t num_buffers;
1201 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1202 uint16_t avail_head;
1205 * The ordering between avail index and
1206 * desc reads needs to be enforced.
1208 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1210 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1212 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1213 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1214 uint16_t nr_vec = 0;
1216 if (unlikely(reserve_avail_buf_split(dev, vq,
1217 pkt_len, buf_vec, &num_buffers,
1218 avail_head, &nr_vec) < 0)) {
1219 VHOST_LOG_DATA(DEBUG,
1220 "(%d) failed to get enough desc from vring\n",
1222 vq->shadow_used_idx -= num_buffers;
1226 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1227 dev->vid, vq->last_avail_idx,
1228 vq->last_avail_idx + num_buffers);
1230 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1233 vq->shadow_used_idx -= num_buffers;
1237 vq->last_avail_idx += num_buffers;
1240 do_data_copy_enqueue(dev, vq);
1242 if (likely(vq->shadow_used_idx)) {
1243 flush_shadow_used_ring_split(dev, vq);
1244 vhost_vring_call_split(dev, vq);
1250 static __rte_always_inline int
1251 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1252 struct vhost_virtqueue *vq,
1253 struct rte_mbuf **pkts,
1254 uint64_t *desc_addrs,
1257 bool wrap_counter = vq->avail_wrap_counter;
1258 struct vring_packed_desc *descs = vq->desc_packed;
1259 uint16_t avail_idx = vq->last_avail_idx;
1260 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1263 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1266 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1269 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1270 if (unlikely(pkts[i]->next != NULL))
1272 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1277 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1278 lens[i] = descs[avail_idx + i].len;
1280 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1281 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1285 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1286 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1287 descs[avail_idx + i].addr,
1291 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1292 if (unlikely(!desc_addrs[i]))
1294 if (unlikely(lens[i] != descs[avail_idx + i].len))
1301 static __rte_always_inline void
1302 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1303 struct vhost_virtqueue *vq,
1304 struct rte_mbuf **pkts,
1305 uint64_t *desc_addrs,
1308 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1309 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1310 struct vring_packed_desc *descs = vq->desc_packed;
1311 uint16_t avail_idx = vq->last_avail_idx;
1312 uint16_t ids[PACKED_BATCH_SIZE];
1315 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1316 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1317 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1318 (uintptr_t)desc_addrs[i];
1319 lens[i] = pkts[i]->pkt_len +
1320 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1323 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1324 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1326 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1328 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1329 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1330 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1334 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1335 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1338 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1339 ids[i] = descs[avail_idx + i].id;
1341 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1344 static __rte_always_inline int
1345 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1346 struct vhost_virtqueue *vq,
1347 struct rte_mbuf **pkts)
1349 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1350 uint64_t lens[PACKED_BATCH_SIZE];
1352 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1355 if (vq->shadow_used_idx) {
1356 do_data_copy_enqueue(dev, vq);
1357 vhost_flush_enqueue_shadow_packed(dev, vq);
1360 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1365 static __rte_always_inline int16_t
1366 virtio_dev_rx_single_packed(struct virtio_net *dev,
1367 struct vhost_virtqueue *vq,
1368 struct rte_mbuf *pkt)
1370 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1371 uint16_t nr_descs = 0;
1373 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1375 VHOST_LOG_DATA(DEBUG,
1376 "(%d) failed to get enough desc from vring\n",
1381 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1382 dev->vid, vq->last_avail_idx,
1383 vq->last_avail_idx + nr_descs);
1385 vq_inc_last_avail_packed(vq, nr_descs);
1390 static __rte_noinline uint32_t
1391 virtio_dev_rx_packed(struct virtio_net *dev,
1392 struct vhost_virtqueue *__rte_restrict vq,
1393 struct rte_mbuf **__rte_restrict pkts,
1396 uint32_t pkt_idx = 0;
1399 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1401 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1402 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1404 pkt_idx += PACKED_BATCH_SIZE;
1409 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1413 } while (pkt_idx < count);
1415 if (vq->shadow_used_idx) {
1416 do_data_copy_enqueue(dev, vq);
1417 vhost_flush_enqueue_shadow_packed(dev, vq);
1421 vhost_vring_call_packed(dev, vq);
1426 static __rte_always_inline uint32_t
1427 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1428 struct rte_mbuf **pkts, uint32_t count)
1430 struct vhost_virtqueue *vq;
1433 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1434 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1435 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1436 dev->vid, __func__, queue_id);
1440 vq = dev->virtqueue[queue_id];
1442 rte_spinlock_lock(&vq->access_lock);
1444 if (unlikely(!vq->enabled))
1445 goto out_access_unlock;
1447 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1448 vhost_user_iotlb_rd_lock(vq);
1450 if (unlikely(!vq->access_ok))
1451 if (unlikely(vring_translate(dev, vq) < 0))
1454 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1458 if (vq_is_packed(dev))
1459 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1461 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1464 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1465 vhost_user_iotlb_rd_unlock(vq);
1468 rte_spinlock_unlock(&vq->access_lock);
1474 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1475 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1477 struct virtio_net *dev = get_device(vid);
1482 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1484 "(%d) %s: built-in vhost net backend is disabled.\n",
1485 dev->vid, __func__);
1489 return virtio_dev_rx(dev, queue_id, pkts, count);
1492 static __rte_always_inline uint16_t
1493 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1494 uint16_t vq_size, uint16_t n_inflight)
1496 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1497 (vq_size - n_inflight + pkts_idx) % vq_size;
1500 static __rte_always_inline void
1501 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1502 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1504 size_t elem_size = sizeof(struct vring_used_elem);
1506 if (d_idx + count <= ring_size) {
1507 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1509 uint16_t size = ring_size - d_idx;
1511 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1512 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1516 static __rte_always_inline void
1517 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1518 struct vring_used_elem_packed *d_ring,
1519 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1521 size_t elem_size = sizeof(struct vring_used_elem_packed);
1523 if (d_idx + count <= ring_size) {
1524 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1526 uint16_t size = ring_size - d_idx;
1528 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1529 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1533 static __rte_noinline uint32_t
1534 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1535 struct vhost_virtqueue *vq, uint16_t queue_id,
1536 struct rte_mbuf **pkts, uint32_t count)
1538 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1539 uint32_t pkt_idx = 0;
1540 uint16_t num_buffers;
1541 uint16_t avail_head;
1543 struct vhost_async *async = vq->async;
1544 struct async_inflight_info *pkts_info = async->pkts_info;
1545 uint32_t pkt_err = 0;
1547 uint16_t slot_idx = 0;
1550 * The ordering between avail index and desc reads need to be enforced.
1552 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1554 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1556 async_iter_reset(async);
1558 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1559 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1560 uint16_t nr_vec = 0;
1562 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1563 &num_buffers, avail_head, &nr_vec) < 0)) {
1564 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n",
1566 vq->shadow_used_idx -= num_buffers;
1570 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1571 dev->vid, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1573 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers) < 0) {
1574 vq->shadow_used_idx -= num_buffers;
1578 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1579 pkts_info[slot_idx].descs = num_buffers;
1580 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1582 vq->last_avail_idx += num_buffers;
1585 if (unlikely(pkt_idx == 0))
1588 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1589 if (unlikely(n_xfer < 0)) {
1590 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1591 dev->vid, __func__, queue_id);
1595 pkt_err = pkt_idx - n_xfer;
1596 if (unlikely(pkt_err)) {
1597 uint16_t num_descs = 0;
1599 /* update number of completed packets */
1602 /* calculate the sum of descriptors to revert */
1603 while (pkt_err-- > 0) {
1604 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1608 /* recover shadow used ring and available ring */
1609 vq->shadow_used_idx -= num_descs;
1610 vq->last_avail_idx -= num_descs;
1613 /* keep used descriptors */
1614 if (likely(vq->shadow_used_idx)) {
1615 uint16_t to = async->desc_idx_split & (vq->size - 1);
1617 store_dma_desc_info_split(vq->shadow_used_split,
1618 async->descs_split, vq->size, 0, to,
1619 vq->shadow_used_idx);
1621 async->desc_idx_split += vq->shadow_used_idx;
1623 async->pkts_idx += pkt_idx;
1624 if (async->pkts_idx >= vq->size)
1625 async->pkts_idx -= vq->size;
1627 async->pkts_inflight_n += pkt_idx;
1628 vq->shadow_used_idx = 0;
1634 static __rte_always_inline void
1635 vhost_update_used_packed(struct vhost_virtqueue *vq,
1636 struct vring_used_elem_packed *shadow_ring,
1640 uint16_t used_idx = vq->last_used_idx;
1641 uint16_t head_idx = vq->last_used_idx;
1642 uint16_t head_flags = 0;
1647 /* Split loop in two to save memory barriers */
1648 for (i = 0; i < count; i++) {
1649 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1650 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1652 used_idx += shadow_ring[i].count;
1653 if (used_idx >= vq->size)
1654 used_idx -= vq->size;
1657 /* The ordering for storing desc flags needs to be enforced. */
1658 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1660 for (i = 0; i < count; i++) {
1663 if (vq->shadow_used_packed[i].len)
1664 flags = VRING_DESC_F_WRITE;
1668 if (vq->used_wrap_counter) {
1669 flags |= VRING_DESC_F_USED;
1670 flags |= VRING_DESC_F_AVAIL;
1672 flags &= ~VRING_DESC_F_USED;
1673 flags &= ~VRING_DESC_F_AVAIL;
1677 vq->desc_packed[vq->last_used_idx].flags = flags;
1679 head_idx = vq->last_used_idx;
1683 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1686 vq->desc_packed[head_idx].flags = head_flags;
1689 static __rte_always_inline int
1690 vhost_enqueue_async_packed(struct virtio_net *dev,
1691 struct vhost_virtqueue *vq,
1692 struct rte_mbuf *pkt,
1693 struct buf_vector *buf_vec,
1695 uint16_t *nr_buffers)
1697 uint16_t nr_vec = 0;
1698 uint16_t avail_idx = vq->last_avail_idx;
1699 uint16_t max_tries, tries = 0;
1700 uint16_t buf_id = 0;
1702 uint16_t desc_count = 0;
1703 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1704 uint32_t buffer_len[vq->size];
1705 uint16_t buffer_buf_id[vq->size];
1706 uint16_t buffer_desc_count[vq->size];
1708 if (rxvq_is_mergeable(dev))
1709 max_tries = vq->size - 1;
1715 * if we tried all available ring items, and still
1716 * can't get enough buf, it means something abnormal
1719 if (unlikely(++tries > max_tries))
1722 if (unlikely(fill_vec_buf_packed(dev, vq,
1723 avail_idx, &desc_count,
1726 VHOST_ACCESS_RW) < 0))
1729 len = RTE_MIN(len, size);
1732 buffer_len[*nr_buffers] = len;
1733 buffer_buf_id[*nr_buffers] = buf_id;
1734 buffer_desc_count[*nr_buffers] = desc_count;
1736 *nr_descs += desc_count;
1737 avail_idx += desc_count;
1738 if (avail_idx >= vq->size)
1739 avail_idx -= vq->size;
1742 if (unlikely(async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec,
1746 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1751 static __rte_always_inline int16_t
1752 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1753 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1755 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1757 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1758 nr_descs, nr_buffers) < 0)) {
1759 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1763 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1764 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1769 static __rte_always_inline void
1770 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1771 uint32_t nr_err, uint32_t *pkt_idx)
1773 uint16_t descs_err = 0;
1774 uint16_t buffers_err = 0;
1775 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1778 /* calculate the sum of buffers and descs of DMA-error packets. */
1779 while (nr_err-- > 0) {
1780 descs_err += pkts_info[slot_idx % vq->size].descs;
1781 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1785 if (vq->last_avail_idx >= descs_err) {
1786 vq->last_avail_idx -= descs_err;
1788 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1789 vq->avail_wrap_counter ^= 1;
1792 vq->shadow_used_idx -= buffers_err;
1795 static __rte_noinline uint32_t
1796 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1797 struct vhost_virtqueue *vq, uint16_t queue_id,
1798 struct rte_mbuf **pkts, uint32_t count)
1800 uint32_t pkt_idx = 0;
1801 uint32_t remained = count;
1803 uint16_t num_buffers;
1806 struct vhost_async *async = vq->async;
1807 struct async_inflight_info *pkts_info = async->pkts_info;
1808 uint32_t pkt_err = 0;
1809 uint16_t slot_idx = 0;
1812 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1816 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1817 &num_descs, &num_buffers) < 0))
1820 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1822 pkts_info[slot_idx].descs = num_descs;
1823 pkts_info[slot_idx].nr_buffers = num_buffers;
1824 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1828 vq_inc_last_avail_packed(vq, num_descs);
1829 } while (pkt_idx < count);
1831 if (unlikely(pkt_idx == 0))
1834 n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1835 if (unlikely(n_xfer < 0)) {
1836 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1837 dev->vid, __func__, queue_id);
1841 pkt_err = pkt_idx - n_xfer;
1843 async_iter_reset(async);
1845 if (unlikely(pkt_err))
1846 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1848 if (likely(vq->shadow_used_idx)) {
1849 /* keep used descriptors. */
1850 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1851 vq->size, 0, async->buffer_idx_packed,
1852 vq->shadow_used_idx);
1854 async->buffer_idx_packed += vq->shadow_used_idx;
1855 if (async->buffer_idx_packed >= vq->size)
1856 async->buffer_idx_packed -= vq->size;
1858 async->pkts_idx += pkt_idx;
1859 if (async->pkts_idx >= vq->size)
1860 async->pkts_idx -= vq->size;
1862 vq->shadow_used_idx = 0;
1863 async->pkts_inflight_n += pkt_idx;
1869 static __rte_always_inline void
1870 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1872 struct vhost_async *async = vq->async;
1873 uint16_t nr_left = n_descs;
1878 from = async->last_desc_idx_split & (vq->size - 1);
1879 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1880 to = vq->last_used_idx & (vq->size - 1);
1882 if (to + nr_copy <= vq->size) {
1883 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1884 nr_copy * sizeof(struct vring_used_elem));
1886 uint16_t size = vq->size - to;
1888 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1889 size * sizeof(struct vring_used_elem));
1890 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1891 (nr_copy - size) * sizeof(struct vring_used_elem));
1894 async->last_desc_idx_split += nr_copy;
1895 vq->last_used_idx += nr_copy;
1897 } while (nr_left > 0);
1900 static __rte_always_inline void
1901 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1904 struct vhost_async *async = vq->async;
1905 uint16_t nr_left = n_buffers;
1909 from = async->last_buffer_idx_packed;
1910 to = (from + nr_left) % vq->size;
1912 vhost_update_used_packed(vq, async->buffers_packed + from, to - from);
1913 async->last_buffer_idx_packed += nr_left;
1916 vhost_update_used_packed(vq, async->buffers_packed + from,
1918 async->last_buffer_idx_packed = 0;
1919 nr_left -= vq->size - from;
1921 } while (nr_left > 0);
1924 static __rte_always_inline uint16_t
1925 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1926 struct rte_mbuf **pkts, uint16_t count)
1928 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1929 struct vhost_async *async = vq->async;
1930 struct async_inflight_info *pkts_info = async->pkts_info;
1932 uint16_t n_descs = 0, n_buffers = 0;
1933 uint16_t start_idx, from, i;
1935 start_idx = virtio_dev_rx_async_get_info_idx(async->pkts_idx,
1936 vq->size, async->pkts_inflight_n);
1938 n_cpl = async->ops.check_completed_copies(dev->vid, queue_id, 0, count);
1939 if (unlikely(n_cpl < 0)) {
1940 VHOST_LOG_DATA(ERR, "(%d) %s: failed to check completed copies for queue id %d.\n",
1941 dev->vid, __func__, queue_id);
1948 for (i = 0; i < n_cpl; i++) {
1949 from = (start_idx + i) % vq->size;
1950 /* Only used with packed ring */
1951 n_buffers += pkts_info[from].nr_buffers;
1952 /* Only used with split ring */
1953 n_descs += pkts_info[from].descs;
1954 pkts[i] = pkts_info[from].mbuf;
1957 async->pkts_inflight_n -= n_cpl;
1959 if (likely(vq->enabled && vq->access_ok)) {
1960 if (vq_is_packed(dev)) {
1961 write_back_completed_descs_packed(vq, n_buffers);
1962 vhost_vring_call_packed(dev, vq);
1964 write_back_completed_descs_split(vq, n_descs);
1965 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1966 vhost_vring_call_split(dev, vq);
1969 if (vq_is_packed(dev)) {
1970 async->last_buffer_idx_packed += n_buffers;
1971 if (async->last_buffer_idx_packed >= vq->size)
1972 async->last_buffer_idx_packed -= vq->size;
1974 async->last_desc_idx_split += n_descs;
1982 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1983 struct rte_mbuf **pkts, uint16_t count)
1985 struct virtio_net *dev = get_device(vid);
1986 struct vhost_virtqueue *vq;
1987 uint16_t n_pkts_cpl = 0;
1992 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1993 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1994 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1995 dev->vid, __func__, queue_id);
1999 vq = dev->virtqueue[queue_id];
2001 if (unlikely(!vq->async)) {
2002 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2003 dev->vid, __func__, queue_id);
2007 rte_spinlock_lock(&vq->access_lock);
2009 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2011 rte_spinlock_unlock(&vq->access_lock);
2017 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2018 struct rte_mbuf **pkts, uint16_t count)
2020 struct virtio_net *dev = get_device(vid);
2021 struct vhost_virtqueue *vq;
2022 uint16_t n_pkts_cpl = 0;
2027 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2028 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2029 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2030 dev->vid, __func__, queue_id);
2034 vq = dev->virtqueue[queue_id];
2036 if (unlikely(!vq->async)) {
2037 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2038 dev->vid, __func__, queue_id);
2042 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2047 static __rte_always_inline uint32_t
2048 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2049 struct rte_mbuf **pkts, uint32_t count)
2051 struct vhost_virtqueue *vq;
2054 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2055 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2056 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2057 dev->vid, __func__, queue_id);
2061 vq = dev->virtqueue[queue_id];
2063 rte_spinlock_lock(&vq->access_lock);
2065 if (unlikely(!vq->enabled || !vq->async))
2066 goto out_access_unlock;
2068 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2069 vhost_user_iotlb_rd_lock(vq);
2071 if (unlikely(!vq->access_ok))
2072 if (unlikely(vring_translate(dev, vq) < 0))
2075 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2079 if (vq_is_packed(dev))
2080 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2083 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2087 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2088 vhost_user_iotlb_rd_unlock(vq);
2091 rte_spinlock_unlock(&vq->access_lock);
2097 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2098 struct rte_mbuf **pkts, uint16_t count)
2100 struct virtio_net *dev = get_device(vid);
2105 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2107 "(%d) %s: built-in vhost net backend is disabled.\n",
2108 dev->vid, __func__);
2112 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2116 virtio_net_with_host_offload(struct virtio_net *dev)
2119 ((1ULL << VIRTIO_NET_F_CSUM) |
2120 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2121 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2122 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2123 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2130 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2132 struct rte_ipv4_hdr *ipv4_hdr;
2133 struct rte_ipv6_hdr *ipv6_hdr;
2134 struct rte_ether_hdr *eth_hdr;
2136 uint16_t data_len = rte_pktmbuf_data_len(m);
2138 if (data_len < sizeof(struct rte_ether_hdr))
2141 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2143 m->l2_len = sizeof(struct rte_ether_hdr);
2144 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2146 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2147 if (data_len < sizeof(struct rte_ether_hdr) +
2148 sizeof(struct rte_vlan_hdr))
2151 struct rte_vlan_hdr *vlan_hdr =
2152 (struct rte_vlan_hdr *)(eth_hdr + 1);
2154 m->l2_len += sizeof(struct rte_vlan_hdr);
2155 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2158 switch (ethertype) {
2159 case RTE_ETHER_TYPE_IPV4:
2160 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2162 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2164 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2165 if (data_len < m->l2_len + m->l3_len)
2167 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2168 *l4_proto = ipv4_hdr->next_proto_id;
2170 case RTE_ETHER_TYPE_IPV6:
2171 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2173 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2175 m->l3_len = sizeof(struct rte_ipv6_hdr);
2176 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2177 *l4_proto = ipv6_hdr->proto;
2180 /* a valid L3 header is needed for further L4 parsing */
2184 /* both CSUM and GSO need a valid L4 header */
2185 switch (*l4_proto) {
2187 if (data_len < m->l2_len + m->l3_len +
2188 sizeof(struct rte_tcp_hdr))
2192 if (data_len < m->l2_len + m->l3_len +
2193 sizeof(struct rte_udp_hdr))
2197 if (data_len < m->l2_len + m->l3_len +
2198 sizeof(struct rte_sctp_hdr))
2214 static __rte_always_inline void
2215 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2217 uint8_t l4_proto = 0;
2218 struct rte_tcp_hdr *tcp_hdr = NULL;
2220 uint16_t data_len = rte_pktmbuf_data_len(m);
2222 if (parse_headers(m, &l4_proto) < 0)
2225 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2226 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2227 switch (hdr->csum_offset) {
2228 case (offsetof(struct rte_tcp_hdr, cksum)):
2229 if (l4_proto != IPPROTO_TCP)
2231 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2233 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2234 if (l4_proto != IPPROTO_UDP)
2236 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2238 case (offsetof(struct rte_sctp_hdr, cksum)):
2239 if (l4_proto != IPPROTO_SCTP)
2241 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2251 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2252 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2253 case VIRTIO_NET_HDR_GSO_TCPV4:
2254 case VIRTIO_NET_HDR_GSO_TCPV6:
2255 if (l4_proto != IPPROTO_TCP)
2257 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2258 struct rte_tcp_hdr *,
2259 m->l2_len + m->l3_len);
2260 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2261 if (data_len < m->l2_len + m->l3_len + tcp_len)
2263 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2264 m->tso_segsz = hdr->gso_size;
2265 m->l4_len = tcp_len;
2267 case VIRTIO_NET_HDR_GSO_UDP:
2268 if (l4_proto != IPPROTO_UDP)
2270 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2271 m->tso_segsz = hdr->gso_size;
2272 m->l4_len = sizeof(struct rte_udp_hdr);
2275 VHOST_LOG_DATA(WARNING,
2276 "unsupported gso type %u.\n", hdr->gso_type);
2288 static __rte_always_inline void
2289 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2290 bool legacy_ol_flags)
2292 struct rte_net_hdr_lens hdr_lens;
2293 int l4_supported = 0;
2296 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2299 if (legacy_ol_flags) {
2300 vhost_dequeue_offload_legacy(hdr, m);
2304 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2306 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2307 m->packet_type = ptype;
2308 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2309 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2310 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2313 /* According to Virtio 1.1 spec, the device only needs to look at
2314 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2315 * This differs from the processing incoming packets path where the
2316 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2319 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2320 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2321 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2323 * 5.1.6.2.2 Device Requirements: Packet Transmission
2324 * The device MUST ignore flag bits that it does not recognize.
2326 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2329 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2330 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2331 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2333 /* Unknown proto or tunnel, do sw cksum. We can assume
2334 * the cksum field is in the first segment since the
2335 * buffers we provided to the host are large enough.
2336 * In case of SCTP, this will be wrong since it's a CRC
2337 * but there's nothing we can do.
2339 uint16_t csum = 0, off;
2341 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2342 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2344 if (likely(csum != 0xffff))
2346 off = hdr->csum_offset + hdr->csum_start;
2347 if (rte_pktmbuf_data_len(m) >= off + 1)
2348 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2352 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2353 if (hdr->gso_size == 0)
2356 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2357 case VIRTIO_NET_HDR_GSO_TCPV4:
2358 case VIRTIO_NET_HDR_GSO_TCPV6:
2359 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2361 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2362 m->tso_segsz = hdr->gso_size;
2364 case VIRTIO_NET_HDR_GSO_UDP:
2365 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2367 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2368 m->tso_segsz = hdr->gso_size;
2376 static __rte_noinline void
2377 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2378 struct buf_vector *buf_vec)
2381 uint64_t remain = sizeof(struct virtio_net_hdr);
2383 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2386 len = RTE_MIN(remain, buf_vec->buf_len);
2387 src = buf_vec->buf_addr;
2388 rte_memcpy((void *)(uintptr_t)dst,
2389 (void *)(uintptr_t)src, len);
2397 static __rte_always_inline int
2398 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2399 struct buf_vector *buf_vec, uint16_t nr_vec,
2400 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2401 bool legacy_ol_flags)
2403 uint32_t buf_avail, buf_offset;
2404 uint64_t buf_addr, buf_len;
2405 uint32_t mbuf_avail, mbuf_offset;
2407 struct rte_mbuf *cur = m, *prev = m;
2408 struct virtio_net_hdr tmp_hdr;
2409 struct virtio_net_hdr *hdr = NULL;
2410 /* A counter to avoid desc dead loop chain */
2411 uint16_t vec_idx = 0;
2412 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2415 buf_addr = buf_vec[vec_idx].buf_addr;
2416 buf_len = buf_vec[vec_idx].buf_len;
2418 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2423 if (virtio_net_with_host_offload(dev)) {
2424 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2426 * No luck, the virtio-net header doesn't fit
2427 * in a contiguous virtual area.
2429 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2432 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2437 * A virtio driver normally uses at least 2 desc buffers
2438 * for Tx: the first for storing the header, and others
2439 * for storing the data.
2441 if (unlikely(buf_len < dev->vhost_hlen)) {
2442 buf_offset = dev->vhost_hlen - buf_len;
2444 buf_addr = buf_vec[vec_idx].buf_addr;
2445 buf_len = buf_vec[vec_idx].buf_len;
2446 buf_avail = buf_len - buf_offset;
2447 } else if (buf_len == dev->vhost_hlen) {
2448 if (unlikely(++vec_idx >= nr_vec))
2450 buf_addr = buf_vec[vec_idx].buf_addr;
2451 buf_len = buf_vec[vec_idx].buf_len;
2454 buf_avail = buf_len;
2456 buf_offset = dev->vhost_hlen;
2457 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2461 (uintptr_t)(buf_addr + buf_offset),
2462 (uint32_t)buf_avail, 0);
2465 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2467 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2469 if (likely(cpy_len > MAX_BATCH_LEN ||
2470 vq->batch_copy_nb_elems >= vq->size ||
2471 (hdr && cur == m))) {
2472 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2474 (void *)((uintptr_t)(buf_addr +
2475 buf_offset)), cpy_len);
2477 batch_copy[vq->batch_copy_nb_elems].dst =
2478 rte_pktmbuf_mtod_offset(cur, void *,
2480 batch_copy[vq->batch_copy_nb_elems].src =
2481 (void *)((uintptr_t)(buf_addr + buf_offset));
2482 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2483 vq->batch_copy_nb_elems++;
2486 mbuf_avail -= cpy_len;
2487 mbuf_offset += cpy_len;
2488 buf_avail -= cpy_len;
2489 buf_offset += cpy_len;
2491 /* This buf reaches to its end, get the next one */
2492 if (buf_avail == 0) {
2493 if (++vec_idx >= nr_vec)
2496 buf_addr = buf_vec[vec_idx].buf_addr;
2497 buf_len = buf_vec[vec_idx].buf_len;
2500 buf_avail = buf_len;
2502 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2503 (uint32_t)buf_avail, 0);
2507 * This mbuf reaches to its end, get a new one
2508 * to hold more data.
2510 if (mbuf_avail == 0) {
2511 cur = rte_pktmbuf_alloc(mbuf_pool);
2512 if (unlikely(cur == NULL)) {
2513 VHOST_LOG_DATA(ERR, "Failed to "
2514 "allocate memory for mbuf.\n");
2520 prev->data_len = mbuf_offset;
2522 m->pkt_len += mbuf_offset;
2526 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2530 prev->data_len = mbuf_offset;
2531 m->pkt_len += mbuf_offset;
2534 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2542 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2548 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2550 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2551 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2556 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2557 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2559 if (unlikely(total_len > UINT16_MAX))
2562 buf_len = total_len;
2563 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2564 if (unlikely(buf == NULL))
2567 /* Initialize shinfo */
2568 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2569 virtio_dev_extbuf_free, buf);
2570 if (unlikely(shinfo == NULL)) {
2572 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2576 iova = rte_malloc_virt2iova(buf);
2577 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2578 rte_pktmbuf_reset_headroom(pkt);
2584 * Prepare a host supported pktmbuf.
2586 static __rte_always_inline int
2587 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2590 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2593 /* attach an external buffer if supported */
2594 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2597 /* check if chained buffers are allowed */
2598 if (!dev->linearbuf)
2606 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2607 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2608 bool legacy_ol_flags)
2611 uint16_t free_entries;
2612 uint16_t dropped = 0;
2613 static bool allocerr_warned;
2616 * The ordering between avail index and
2617 * desc reads needs to be enforced.
2619 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2621 if (free_entries == 0)
2624 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2626 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2628 count = RTE_MIN(count, MAX_PKT_BURST);
2629 count = RTE_MIN(count, free_entries);
2630 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2633 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2636 for (i = 0; i < count; i++) {
2637 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2640 uint16_t nr_vec = 0;
2643 if (unlikely(fill_vec_buf_split(dev, vq,
2644 vq->last_avail_idx + i,
2646 &head_idx, &buf_len,
2647 VHOST_ACCESS_RO) < 0))
2650 update_shadow_used_ring_split(vq, head_idx, 0);
2652 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2653 if (unlikely(err)) {
2655 * mbuf allocation fails for jumbo packets when external
2656 * buffer allocation is not allowed and linear buffer
2657 * is required. Drop this packet.
2659 if (!allocerr_warned) {
2661 "Failed mbuf alloc of size %d from %s on %s.\n",
2662 buf_len, mbuf_pool->name, dev->ifname);
2663 allocerr_warned = true;
2670 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2671 mbuf_pool, legacy_ol_flags);
2672 if (unlikely(err)) {
2673 if (!allocerr_warned) {
2675 "Failed to copy desc to mbuf on %s.\n",
2677 allocerr_warned = true;
2686 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2688 vq->last_avail_idx += i;
2690 do_data_copy_dequeue(vq);
2691 if (unlikely(i < count))
2692 vq->shadow_used_idx = i;
2693 if (likely(vq->shadow_used_idx)) {
2694 flush_shadow_used_ring_split(dev, vq);
2695 vhost_vring_call_split(dev, vq);
2698 return (i - dropped);
2703 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2704 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2705 struct rte_mbuf **pkts, uint16_t count)
2707 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2712 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2713 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2714 struct rte_mbuf **pkts, uint16_t count)
2716 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2719 static __rte_always_inline int
2720 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2721 struct vhost_virtqueue *vq,
2722 struct rte_mbuf **pkts,
2724 uintptr_t *desc_addrs,
2727 bool wrap = vq->avail_wrap_counter;
2728 struct vring_packed_desc *descs = vq->desc_packed;
2729 uint64_t lens[PACKED_BATCH_SIZE];
2730 uint64_t buf_lens[PACKED_BATCH_SIZE];
2731 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2734 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2736 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2739 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2740 flags = descs[avail_idx + i].flags;
2741 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2742 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2743 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2747 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2749 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2750 lens[i] = descs[avail_idx + i].len;
2752 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2753 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2754 descs[avail_idx + i].addr,
2755 &lens[i], VHOST_ACCESS_RW);
2758 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2759 if (unlikely(!desc_addrs[i]))
2761 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2765 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2766 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2770 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2771 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2773 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2774 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2778 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2779 pkts[i]->pkt_len = lens[i] - buf_offset;
2780 pkts[i]->data_len = pkts[i]->pkt_len;
2781 ids[i] = descs[avail_idx + i].id;
2790 static __rte_always_inline int
2791 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2792 struct vhost_virtqueue *vq,
2793 struct rte_mbuf **pkts,
2794 bool legacy_ol_flags)
2796 uint16_t avail_idx = vq->last_avail_idx;
2797 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2798 struct virtio_net_hdr *hdr;
2799 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2800 uint16_t ids[PACKED_BATCH_SIZE];
2803 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2807 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2808 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2810 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2811 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2812 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2815 if (virtio_net_with_host_offload(dev)) {
2816 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2817 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2818 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2822 if (virtio_net_is_inorder(dev))
2823 vhost_shadow_dequeue_batch_packed_inorder(vq,
2824 ids[PACKED_BATCH_SIZE - 1]);
2826 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2828 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2833 static __rte_always_inline int
2834 vhost_dequeue_single_packed(struct virtio_net *dev,
2835 struct vhost_virtqueue *vq,
2836 struct rte_mempool *mbuf_pool,
2837 struct rte_mbuf *pkts,
2839 uint16_t *desc_count,
2840 bool legacy_ol_flags)
2842 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2844 uint16_t nr_vec = 0;
2846 static bool allocerr_warned;
2848 if (unlikely(fill_vec_buf_packed(dev, vq,
2849 vq->last_avail_idx, desc_count,
2852 VHOST_ACCESS_RO) < 0))
2855 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2856 if (!allocerr_warned) {
2858 "Failed mbuf alloc of size %d from %s on %s.\n",
2859 buf_len, mbuf_pool->name, dev->ifname);
2860 allocerr_warned = true;
2865 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2866 mbuf_pool, legacy_ol_flags);
2867 if (unlikely(err)) {
2868 if (!allocerr_warned) {
2870 "Failed to copy desc to mbuf on %s.\n",
2872 allocerr_warned = true;
2880 static __rte_always_inline int
2881 virtio_dev_tx_single_packed(struct virtio_net *dev,
2882 struct vhost_virtqueue *vq,
2883 struct rte_mempool *mbuf_pool,
2884 struct rte_mbuf *pkts,
2885 bool legacy_ol_flags)
2888 uint16_t buf_id, desc_count = 0;
2891 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2892 &desc_count, legacy_ol_flags);
2894 if (likely(desc_count > 0)) {
2895 if (virtio_net_is_inorder(dev))
2896 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2899 vhost_shadow_dequeue_single_packed(vq, buf_id,
2902 vq_inc_last_avail_packed(vq, desc_count);
2910 virtio_dev_tx_packed(struct virtio_net *dev,
2911 struct vhost_virtqueue *__rte_restrict vq,
2912 struct rte_mempool *mbuf_pool,
2913 struct rte_mbuf **__rte_restrict pkts,
2915 bool legacy_ol_flags)
2917 uint32_t pkt_idx = 0;
2919 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2923 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2925 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2926 if (!virtio_dev_tx_batch_packed(dev, vq,
2929 pkt_idx += PACKED_BATCH_SIZE;
2934 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2939 } while (pkt_idx < count);
2941 if (pkt_idx != count)
2942 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
2944 if (vq->shadow_used_idx) {
2945 do_data_copy_dequeue(vq);
2947 vhost_flush_dequeue_shadow_packed(dev, vq);
2948 vhost_vring_call_packed(dev, vq);
2956 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
2957 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2958 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2960 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
2965 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
2966 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2967 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2969 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
2973 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2974 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2976 struct virtio_net *dev;
2977 struct rte_mbuf *rarp_mbuf = NULL;
2978 struct vhost_virtqueue *vq;
2979 int16_t success = 1;
2981 dev = get_device(vid);
2985 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2987 "(%d) %s: built-in vhost net backend is disabled.\n",
2988 dev->vid, __func__);
2992 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2994 "(%d) %s: invalid virtqueue idx %d.\n",
2995 dev->vid, __func__, queue_id);
2999 vq = dev->virtqueue[queue_id];
3001 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3004 if (unlikely(!vq->enabled)) {
3006 goto out_access_unlock;
3009 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3010 vhost_user_iotlb_rd_lock(vq);
3012 if (unlikely(!vq->access_ok))
3013 if (unlikely(vring_translate(dev, vq) < 0)) {
3019 * Construct a RARP broadcast packet, and inject it to the "pkts"
3020 * array, to looks like that guest actually send such packet.
3022 * Check user_send_rarp() for more information.
3024 * broadcast_rarp shares a cacheline in the virtio_net structure
3025 * with some fields that are accessed during enqueue and
3026 * __atomic_compare_exchange_n causes a write if performed compare
3027 * and exchange. This could result in false sharing between enqueue
3030 * Prevent unnecessary false sharing by reading broadcast_rarp first
3031 * and only performing compare and exchange if the read indicates it
3032 * is likely to be set.
3034 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3035 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3036 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3038 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3039 if (rarp_mbuf == NULL) {
3040 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3045 * Inject it to the head of "pkts" array, so that switch's mac
3046 * learning table will get updated first.
3048 pkts[0] = rarp_mbuf;
3053 if (vq_is_packed(dev)) {
3054 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3055 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3057 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3059 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3060 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3062 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3066 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3067 vhost_user_iotlb_rd_unlock(vq);
3070 rte_spinlock_unlock(&vq->access_lock);
3072 if (unlikely(rarp_mbuf != NULL))