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 #define VHOST_ASYNC_BATCH_THRESHOLD 32
30 static __rte_always_inline bool
31 rxvq_is_mergeable(struct virtio_net *dev)
33 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
36 static __rte_always_inline bool
37 virtio_net_is_inorder(struct virtio_net *dev)
39 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
43 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
45 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
49 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
51 struct batch_copy_elem *elem = vq->batch_copy_elems;
52 uint16_t count = vq->batch_copy_nb_elems;
55 for (i = 0; i < count; i++) {
56 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
57 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
59 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
62 vq->batch_copy_nb_elems = 0;
66 do_data_copy_dequeue(struct vhost_virtqueue *vq)
68 struct batch_copy_elem *elem = vq->batch_copy_elems;
69 uint16_t count = vq->batch_copy_nb_elems;
72 for (i = 0; i < count; i++)
73 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
75 vq->batch_copy_nb_elems = 0;
78 static __rte_always_inline void
79 do_flush_shadow_used_ring_split(struct virtio_net *dev,
80 struct vhost_virtqueue *vq,
81 uint16_t to, uint16_t from, uint16_t size)
83 rte_memcpy(&vq->used->ring[to],
84 &vq->shadow_used_split[from],
85 size * sizeof(struct vring_used_elem));
86 vhost_log_cache_used_vring(dev, vq,
87 offsetof(struct vring_used, ring[to]),
88 size * sizeof(struct vring_used_elem));
91 static __rte_always_inline void
92 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
94 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
96 if (used_idx + vq->shadow_used_idx <= vq->size) {
97 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
102 /* update used ring interval [used_idx, vq->size] */
103 size = vq->size - used_idx;
104 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
106 /* update the left half used ring interval [0, left_size] */
107 do_flush_shadow_used_ring_split(dev, vq, 0, size,
108 vq->shadow_used_idx - size);
110 vq->last_used_idx += vq->shadow_used_idx;
112 vhost_log_cache_sync(dev, vq);
114 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
116 vq->shadow_used_idx = 0;
117 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
118 sizeof(vq->used->idx));
121 static __rte_always_inline void
122 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
123 uint16_t desc_idx, uint32_t len)
125 uint16_t i = vq->shadow_used_idx++;
127 vq->shadow_used_split[i].id = desc_idx;
128 vq->shadow_used_split[i].len = len;
131 static __rte_always_inline void
132 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
133 struct vhost_virtqueue *vq)
136 uint16_t used_idx = vq->last_used_idx;
137 uint16_t head_idx = vq->last_used_idx;
138 uint16_t head_flags = 0;
140 /* Split loop in two to save memory barriers */
141 for (i = 0; i < vq->shadow_used_idx; i++) {
142 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
143 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
145 used_idx += vq->shadow_used_packed[i].count;
146 if (used_idx >= vq->size)
147 used_idx -= vq->size;
150 /* The ordering for storing desc flags needs to be enforced. */
151 rte_atomic_thread_fence(__ATOMIC_RELEASE);
153 for (i = 0; i < vq->shadow_used_idx; i++) {
156 if (vq->shadow_used_packed[i].len)
157 flags = VRING_DESC_F_WRITE;
161 if (vq->used_wrap_counter) {
162 flags |= VRING_DESC_F_USED;
163 flags |= VRING_DESC_F_AVAIL;
165 flags &= ~VRING_DESC_F_USED;
166 flags &= ~VRING_DESC_F_AVAIL;
170 vq->desc_packed[vq->last_used_idx].flags = flags;
172 vhost_log_cache_used_vring(dev, vq,
174 sizeof(struct vring_packed_desc),
175 sizeof(struct vring_packed_desc));
177 head_idx = vq->last_used_idx;
181 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
184 vq->desc_packed[head_idx].flags = head_flags;
186 vhost_log_cache_used_vring(dev, vq,
188 sizeof(struct vring_packed_desc),
189 sizeof(struct vring_packed_desc));
191 vq->shadow_used_idx = 0;
192 vhost_log_cache_sync(dev, vq);
195 static __rte_always_inline void
196 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
197 struct vhost_virtqueue *vq)
199 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
201 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
202 /* desc flags is the synchronization point for virtio packed vring */
203 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
204 used_elem->flags, __ATOMIC_RELEASE);
206 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
207 sizeof(struct vring_packed_desc),
208 sizeof(struct vring_packed_desc));
209 vq->shadow_used_idx = 0;
210 vhost_log_cache_sync(dev, vq);
213 static __rte_always_inline void
214 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
215 struct vhost_virtqueue *vq,
221 uint16_t last_used_idx;
222 struct vring_packed_desc *desc_base;
224 if (vq->shadow_used_idx) {
225 do_data_copy_enqueue(dev, vq);
226 vhost_flush_enqueue_shadow_packed(dev, vq);
229 last_used_idx = vq->last_used_idx;
230 desc_base = &vq->desc_packed[last_used_idx];
232 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
234 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
235 desc_base[i].id = ids[i];
236 desc_base[i].len = lens[i];
239 rte_atomic_thread_fence(__ATOMIC_RELEASE);
241 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
242 desc_base[i].flags = flags;
245 vhost_log_cache_used_vring(dev, vq, last_used_idx *
246 sizeof(struct vring_packed_desc),
247 sizeof(struct vring_packed_desc) *
249 vhost_log_cache_sync(dev, vq);
251 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
254 static __rte_always_inline void
255 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
258 vq->shadow_used_packed[0].id = id;
260 if (!vq->shadow_used_idx) {
261 vq->shadow_last_used_idx = vq->last_used_idx;
262 vq->shadow_used_packed[0].flags =
263 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
264 vq->shadow_used_packed[0].len = 0;
265 vq->shadow_used_packed[0].count = 1;
266 vq->shadow_used_idx++;
269 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
272 static __rte_always_inline void
273 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
274 struct vhost_virtqueue *vq,
281 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
283 if (!vq->shadow_used_idx) {
284 vq->shadow_last_used_idx = vq->last_used_idx;
285 vq->shadow_used_packed[0].id = ids[0];
286 vq->shadow_used_packed[0].len = 0;
287 vq->shadow_used_packed[0].count = 1;
288 vq->shadow_used_packed[0].flags = flags;
289 vq->shadow_used_idx++;
294 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
295 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
296 vq->desc_packed[vq->last_used_idx + i].len = 0;
299 rte_atomic_thread_fence(__ATOMIC_RELEASE);
300 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
301 vq->desc_packed[vq->last_used_idx + i].flags = flags;
303 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
304 sizeof(struct vring_packed_desc),
305 sizeof(struct vring_packed_desc) *
307 vhost_log_cache_sync(dev, vq);
309 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
312 static __rte_always_inline void
313 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
319 flags = vq->desc_packed[vq->last_used_idx].flags;
320 if (vq->used_wrap_counter) {
321 flags |= VRING_DESC_F_USED;
322 flags |= VRING_DESC_F_AVAIL;
324 flags &= ~VRING_DESC_F_USED;
325 flags &= ~VRING_DESC_F_AVAIL;
328 if (!vq->shadow_used_idx) {
329 vq->shadow_last_used_idx = vq->last_used_idx;
331 vq->shadow_used_packed[0].id = buf_id;
332 vq->shadow_used_packed[0].len = 0;
333 vq->shadow_used_packed[0].flags = flags;
334 vq->shadow_used_idx++;
336 vq->desc_packed[vq->last_used_idx].id = buf_id;
337 vq->desc_packed[vq->last_used_idx].len = 0;
338 vq->desc_packed[vq->last_used_idx].flags = flags;
341 vq_inc_last_used_packed(vq, count);
344 static __rte_always_inline void
345 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
351 vq->shadow_used_packed[0].id = buf_id;
353 flags = vq->desc_packed[vq->last_used_idx].flags;
354 if (vq->used_wrap_counter) {
355 flags |= VRING_DESC_F_USED;
356 flags |= VRING_DESC_F_AVAIL;
358 flags &= ~VRING_DESC_F_USED;
359 flags &= ~VRING_DESC_F_AVAIL;
362 if (!vq->shadow_used_idx) {
363 vq->shadow_last_used_idx = vq->last_used_idx;
364 vq->shadow_used_packed[0].len = 0;
365 vq->shadow_used_packed[0].flags = flags;
366 vq->shadow_used_idx++;
369 vq_inc_last_used_packed(vq, count);
372 static __rte_always_inline void
373 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
377 uint16_t num_buffers)
381 for (i = 0; i < num_buffers; i++) {
382 /* enqueue shadow flush action aligned with batch num */
383 if (!vq->shadow_used_idx)
384 vq->shadow_aligned_idx = vq->last_used_idx &
386 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
387 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
388 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
389 vq->shadow_aligned_idx += count[i];
390 vq->shadow_used_idx++;
394 static __rte_always_inline void
395 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
396 struct vhost_virtqueue *vq,
400 uint16_t num_buffers)
402 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
404 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
405 do_data_copy_enqueue(dev, vq);
406 vhost_flush_enqueue_shadow_packed(dev, vq);
410 /* avoid write operation when necessary, to lessen cache issues */
411 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
412 if ((var) != (val)) \
416 static __rte_always_inline void
417 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
419 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
421 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
422 csum_l4 |= PKT_TX_TCP_CKSUM;
425 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
426 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
429 case PKT_TX_TCP_CKSUM:
430 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
433 case PKT_TX_UDP_CKSUM:
434 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
437 case PKT_TX_SCTP_CKSUM:
438 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
443 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
444 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
445 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
448 /* IP cksum verification cannot be bypassed, then calculate here */
449 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
450 struct rte_ipv4_hdr *ipv4_hdr;
452 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
454 ipv4_hdr->hdr_checksum = 0;
455 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
458 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
459 if (m_buf->ol_flags & PKT_TX_IPV4)
460 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
462 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
463 net_hdr->gso_size = m_buf->tso_segsz;
464 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
466 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
467 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
468 net_hdr->gso_size = m_buf->tso_segsz;
469 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
472 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
473 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
474 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
478 static __rte_always_inline int
479 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
480 struct buf_vector *buf_vec, uint16_t *vec_idx,
481 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
483 uint16_t vec_id = *vec_idx;
487 uint64_t desc_chunck_len = desc_len;
489 if (unlikely(vec_id >= BUF_VECTOR_MAX))
492 desc_addr = vhost_iova_to_vva(dev, vq,
496 if (unlikely(!desc_addr))
499 rte_prefetch0((void *)(uintptr_t)desc_addr);
501 buf_vec[vec_id].buf_iova = desc_iova;
502 buf_vec[vec_id].buf_addr = desc_addr;
503 buf_vec[vec_id].buf_len = desc_chunck_len;
505 desc_len -= desc_chunck_len;
506 desc_iova += desc_chunck_len;
514 static __rte_always_inline int
515 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
516 uint32_t avail_idx, uint16_t *vec_idx,
517 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
518 uint32_t *desc_chain_len, uint8_t perm)
520 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
521 uint16_t vec_id = *vec_idx;
524 uint32_t nr_descs = vq->size;
526 struct vring_desc *descs = vq->desc;
527 struct vring_desc *idesc = NULL;
529 if (unlikely(idx >= vq->size))
532 *desc_chain_head = idx;
534 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
535 dlen = vq->desc[idx].len;
536 nr_descs = dlen / sizeof(struct vring_desc);
537 if (unlikely(nr_descs > vq->size))
540 descs = (struct vring_desc *)(uintptr_t)
541 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
544 if (unlikely(!descs))
547 if (unlikely(dlen < vq->desc[idx].len)) {
549 * The indirect desc table is not contiguous
550 * in process VA space, we have to copy it.
552 idesc = vhost_alloc_copy_ind_table(dev, vq,
553 vq->desc[idx].addr, vq->desc[idx].len);
554 if (unlikely(!idesc))
564 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
565 free_ind_table(idesc);
569 dlen = descs[idx].len;
572 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
573 descs[idx].addr, dlen,
575 free_ind_table(idesc);
579 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
582 idx = descs[idx].next;
585 *desc_chain_len = len;
588 if (unlikely(!!idesc))
589 free_ind_table(idesc);
595 * Returns -1 on fail, 0 on success
598 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
599 uint32_t size, struct buf_vector *buf_vec,
600 uint16_t *num_buffers, uint16_t avail_head,
604 uint16_t vec_idx = 0;
605 uint16_t max_tries, tries = 0;
607 uint16_t head_idx = 0;
611 cur_idx = vq->last_avail_idx;
613 if (rxvq_is_mergeable(dev))
614 max_tries = vq->size - 1;
619 if (unlikely(cur_idx == avail_head))
622 * if we tried all available ring items, and still
623 * can't get enough buf, it means something abnormal
626 if (unlikely(++tries > max_tries))
629 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
632 VHOST_ACCESS_RW) < 0))
634 len = RTE_MIN(len, size);
635 update_shadow_used_ring_split(vq, head_idx, len);
647 static __rte_always_inline int
648 fill_vec_buf_packed_indirect(struct virtio_net *dev,
649 struct vhost_virtqueue *vq,
650 struct vring_packed_desc *desc, uint16_t *vec_idx,
651 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
655 uint16_t vec_id = *vec_idx;
657 struct vring_packed_desc *descs, *idescs = NULL;
660 descs = (struct vring_packed_desc *)(uintptr_t)
661 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
662 if (unlikely(!descs))
665 if (unlikely(dlen < desc->len)) {
667 * The indirect desc table is not contiguous
668 * in process VA space, we have to copy it.
670 idescs = vhost_alloc_copy_ind_table(dev,
671 vq, desc->addr, desc->len);
672 if (unlikely(!idescs))
678 nr_descs = desc->len / sizeof(struct vring_packed_desc);
679 if (unlikely(nr_descs >= vq->size)) {
680 free_ind_table(idescs);
684 for (i = 0; i < nr_descs; i++) {
685 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
686 free_ind_table(idescs);
692 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
699 if (unlikely(!!idescs))
700 free_ind_table(idescs);
705 static __rte_always_inline int
706 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
707 uint16_t avail_idx, uint16_t *desc_count,
708 struct buf_vector *buf_vec, uint16_t *vec_idx,
709 uint16_t *buf_id, uint32_t *len, uint8_t perm)
711 bool wrap_counter = vq->avail_wrap_counter;
712 struct vring_packed_desc *descs = vq->desc_packed;
713 uint16_t vec_id = *vec_idx;
716 if (avail_idx < vq->last_avail_idx)
720 * Perform a load-acquire barrier in desc_is_avail to
721 * enforce the ordering between desc flags and desc
724 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
731 if (unlikely(vec_id >= BUF_VECTOR_MAX))
734 if (unlikely(*desc_count >= vq->size))
738 *buf_id = descs[avail_idx].id;
740 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
741 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
747 dlen = descs[avail_idx].len;
750 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
751 descs[avail_idx].addr,
757 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
760 if (++avail_idx >= vq->size) {
761 avail_idx -= vq->size;
771 static __rte_noinline void
772 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
773 struct buf_vector *buf_vec,
774 struct virtio_net_hdr_mrg_rxbuf *hdr)
777 uint64_t remain = dev->vhost_hlen;
778 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
779 uint64_t iova = buf_vec->buf_iova;
782 len = RTE_MIN(remain,
784 dst = buf_vec->buf_addr;
785 rte_memcpy((void *)(uintptr_t)dst,
786 (void *)(uintptr_t)src,
789 PRINT_PACKET(dev, (uintptr_t)dst,
791 vhost_log_cache_write_iova(dev, vq,
801 static __rte_always_inline int
802 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
803 struct rte_mbuf *m, struct buf_vector *buf_vec,
804 uint16_t nr_vec, uint16_t num_buffers)
806 uint32_t vec_idx = 0;
807 uint32_t mbuf_offset, mbuf_avail;
808 uint32_t buf_offset, buf_avail;
809 uint64_t buf_addr, buf_iova, buf_len;
812 struct rte_mbuf *hdr_mbuf;
813 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
814 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
817 if (unlikely(m == NULL)) {
822 buf_addr = buf_vec[vec_idx].buf_addr;
823 buf_iova = buf_vec[vec_idx].buf_iova;
824 buf_len = buf_vec[vec_idx].buf_len;
826 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
833 if (unlikely(buf_len < dev->vhost_hlen)) {
834 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
837 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
839 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
840 dev->vid, num_buffers);
842 if (unlikely(buf_len < dev->vhost_hlen)) {
843 buf_offset = dev->vhost_hlen - buf_len;
845 buf_addr = buf_vec[vec_idx].buf_addr;
846 buf_iova = buf_vec[vec_idx].buf_iova;
847 buf_len = buf_vec[vec_idx].buf_len;
848 buf_avail = buf_len - buf_offset;
850 buf_offset = dev->vhost_hlen;
851 buf_avail = buf_len - dev->vhost_hlen;
854 mbuf_avail = rte_pktmbuf_data_len(m);
856 while (mbuf_avail != 0 || m->next != NULL) {
857 /* done with current buf, get the next one */
858 if (buf_avail == 0) {
860 if (unlikely(vec_idx >= nr_vec)) {
865 buf_addr = buf_vec[vec_idx].buf_addr;
866 buf_iova = buf_vec[vec_idx].buf_iova;
867 buf_len = buf_vec[vec_idx].buf_len;
873 /* done with current mbuf, get the next one */
874 if (mbuf_avail == 0) {
878 mbuf_avail = rte_pktmbuf_data_len(m);
882 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
883 if (rxvq_is_mergeable(dev))
884 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
887 if (unlikely(hdr == &tmp_hdr)) {
888 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
890 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
892 vhost_log_cache_write_iova(dev, vq,
900 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
902 if (likely(cpy_len > MAX_BATCH_LEN ||
903 vq->batch_copy_nb_elems >= vq->size)) {
904 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
905 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
907 vhost_log_cache_write_iova(dev, vq,
908 buf_iova + buf_offset,
910 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
913 batch_copy[vq->batch_copy_nb_elems].dst =
914 (void *)((uintptr_t)(buf_addr + buf_offset));
915 batch_copy[vq->batch_copy_nb_elems].src =
916 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
917 batch_copy[vq->batch_copy_nb_elems].log_addr =
918 buf_iova + buf_offset;
919 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
920 vq->batch_copy_nb_elems++;
923 mbuf_avail -= cpy_len;
924 mbuf_offset += cpy_len;
925 buf_avail -= cpy_len;
926 buf_offset += cpy_len;
934 static __rte_always_inline void
935 async_fill_vec(struct iovec *v, void *base, size_t len)
941 static __rte_always_inline void
942 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
943 struct iovec *vec, unsigned long nr_seg)
950 it->nr_segs = nr_seg;
957 static __rte_always_inline void
958 async_fill_desc(struct rte_vhost_async_desc *desc,
959 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
965 static __rte_always_inline int
966 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
967 struct rte_mbuf *m, struct buf_vector *buf_vec,
968 uint16_t nr_vec, uint16_t num_buffers,
969 struct iovec *src_iovec, struct iovec *dst_iovec,
970 struct rte_vhost_iov_iter *src_it,
971 struct rte_vhost_iov_iter *dst_it)
973 uint32_t vec_idx = 0;
974 uint32_t mbuf_offset, mbuf_avail;
975 uint32_t buf_offset, buf_avail;
976 uint64_t buf_addr, buf_iova, buf_len;
977 uint32_t cpy_len, cpy_threshold;
979 struct rte_mbuf *hdr_mbuf;
980 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
981 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
989 if (unlikely(m == NULL)) {
994 cpy_threshold = vq->async_threshold;
996 buf_addr = buf_vec[vec_idx].buf_addr;
997 buf_iova = buf_vec[vec_idx].buf_iova;
998 buf_len = buf_vec[vec_idx].buf_len;
1000 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1006 hdr_addr = buf_addr;
1007 if (unlikely(buf_len < dev->vhost_hlen)) {
1008 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1011 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1013 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1014 dev->vid, num_buffers);
1016 if (unlikely(buf_len < dev->vhost_hlen)) {
1017 buf_offset = dev->vhost_hlen - buf_len;
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;
1022 buf_avail = buf_len - buf_offset;
1024 buf_offset = dev->vhost_hlen;
1025 buf_avail = buf_len - dev->vhost_hlen;
1028 mbuf_avail = rte_pktmbuf_data_len(m);
1031 while (mbuf_avail != 0 || m->next != NULL) {
1032 /* done with current buf, get the next one */
1033 if (buf_avail == 0) {
1035 if (unlikely(vec_idx >= nr_vec)) {
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;
1045 buf_avail = buf_len;
1048 /* done with current mbuf, get the next one */
1049 if (mbuf_avail == 0) {
1053 mbuf_avail = rte_pktmbuf_data_len(m);
1057 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1058 if (rxvq_is_mergeable(dev))
1059 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1062 if (unlikely(hdr == &tmp_hdr)) {
1063 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1065 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1066 dev->vhost_hlen, 0);
1067 vhost_log_cache_write_iova(dev, vq,
1068 buf_vec[0].buf_iova,
1075 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1077 while (unlikely(cpy_len && cpy_len >= cpy_threshold)) {
1078 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1079 buf_iova + buf_offset,
1080 cpy_len, &mapped_len);
1082 if (unlikely(!hpa || mapped_len < cpy_threshold))
1085 async_fill_vec(src_iovec + tvec_idx,
1086 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1087 mbuf_offset), (size_t)mapped_len);
1089 async_fill_vec(dst_iovec + tvec_idx,
1090 hpa, (size_t)mapped_len);
1092 tlen += (uint32_t)mapped_len;
1093 cpy_len -= (uint32_t)mapped_len;
1094 mbuf_avail -= (uint32_t)mapped_len;
1095 mbuf_offset += (uint32_t)mapped_len;
1096 buf_avail -= (uint32_t)mapped_len;
1097 buf_offset += (uint32_t)mapped_len;
1101 if (likely(cpy_len)) {
1102 if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1104 (void *)((uintptr_t)(buf_addr + buf_offset)),
1105 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1109 (uintptr_t)(buf_addr + buf_offset),
1112 batch_copy[vq->batch_copy_nb_elems].dst =
1113 (void *)((uintptr_t)(buf_addr + buf_offset));
1114 batch_copy[vq->batch_copy_nb_elems].src =
1115 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1116 batch_copy[vq->batch_copy_nb_elems].log_addr =
1117 buf_iova + buf_offset;
1118 batch_copy[vq->batch_copy_nb_elems].len =
1120 vq->batch_copy_nb_elems++;
1123 mbuf_avail -= cpy_len;
1124 mbuf_offset += cpy_len;
1125 buf_avail -= cpy_len;
1126 buf_offset += cpy_len;
1133 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1134 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1142 static __rte_always_inline int
1143 vhost_enqueue_single_packed(struct virtio_net *dev,
1144 struct vhost_virtqueue *vq,
1145 struct rte_mbuf *pkt,
1146 struct buf_vector *buf_vec,
1149 uint16_t nr_vec = 0;
1150 uint16_t avail_idx = vq->last_avail_idx;
1151 uint16_t max_tries, tries = 0;
1152 uint16_t buf_id = 0;
1154 uint16_t desc_count;
1155 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1156 uint16_t num_buffers = 0;
1157 uint32_t buffer_len[vq->size];
1158 uint16_t buffer_buf_id[vq->size];
1159 uint16_t buffer_desc_count[vq->size];
1161 if (rxvq_is_mergeable(dev))
1162 max_tries = vq->size - 1;
1168 * if we tried all available ring items, and still
1169 * can't get enough buf, it means something abnormal
1172 if (unlikely(++tries > max_tries))
1175 if (unlikely(fill_vec_buf_packed(dev, vq,
1176 avail_idx, &desc_count,
1179 VHOST_ACCESS_RW) < 0))
1182 len = RTE_MIN(len, size);
1185 buffer_len[num_buffers] = len;
1186 buffer_buf_id[num_buffers] = buf_id;
1187 buffer_desc_count[num_buffers] = desc_count;
1190 *nr_descs += desc_count;
1191 avail_idx += desc_count;
1192 if (avail_idx >= vq->size)
1193 avail_idx -= vq->size;
1196 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1199 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1200 buffer_desc_count, num_buffers);
1205 static __rte_noinline uint32_t
1206 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1207 struct rte_mbuf **pkts, uint32_t count)
1209 uint32_t pkt_idx = 0;
1210 uint16_t num_buffers;
1211 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1212 uint16_t avail_head;
1215 * The ordering between avail index and
1216 * desc reads needs to be enforced.
1218 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1220 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1222 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1223 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1224 uint16_t nr_vec = 0;
1226 if (unlikely(reserve_avail_buf_split(dev, vq,
1227 pkt_len, buf_vec, &num_buffers,
1228 avail_head, &nr_vec) < 0)) {
1229 VHOST_LOG_DATA(DEBUG,
1230 "(%d) failed to get enough desc from vring\n",
1232 vq->shadow_used_idx -= num_buffers;
1236 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1237 dev->vid, vq->last_avail_idx,
1238 vq->last_avail_idx + num_buffers);
1240 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1243 vq->shadow_used_idx -= num_buffers;
1247 vq->last_avail_idx += num_buffers;
1250 do_data_copy_enqueue(dev, vq);
1252 if (likely(vq->shadow_used_idx)) {
1253 flush_shadow_used_ring_split(dev, vq);
1254 vhost_vring_call_split(dev, vq);
1260 static __rte_always_inline int
1261 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1262 struct vhost_virtqueue *vq,
1263 struct rte_mbuf **pkts)
1265 bool wrap_counter = vq->avail_wrap_counter;
1266 struct vring_packed_desc *descs = vq->desc_packed;
1267 uint16_t avail_idx = vq->last_avail_idx;
1268 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1269 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1270 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1271 uint64_t lens[PACKED_BATCH_SIZE];
1272 uint16_t ids[PACKED_BATCH_SIZE];
1275 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1278 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1281 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1282 if (unlikely(pkts[i]->next != NULL))
1284 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1289 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1290 lens[i] = descs[avail_idx + i].len;
1292 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1293 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1297 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1298 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1299 descs[avail_idx + i].addr,
1303 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1304 if (unlikely(!desc_addrs[i]))
1306 if (unlikely(lens[i] != descs[avail_idx + i].len))
1310 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1311 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1312 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1313 (uintptr_t)desc_addrs[i];
1314 lens[i] = pkts[i]->pkt_len +
1315 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1318 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1319 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1321 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1323 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1324 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1325 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1329 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1330 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1333 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1334 ids[i] = descs[avail_idx + i].id;
1336 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1341 static __rte_always_inline int16_t
1342 virtio_dev_rx_single_packed(struct virtio_net *dev,
1343 struct vhost_virtqueue *vq,
1344 struct rte_mbuf *pkt)
1346 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1347 uint16_t nr_descs = 0;
1349 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1351 VHOST_LOG_DATA(DEBUG,
1352 "(%d) failed to get enough desc from vring\n",
1357 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1358 dev->vid, vq->last_avail_idx,
1359 vq->last_avail_idx + nr_descs);
1361 vq_inc_last_avail_packed(vq, nr_descs);
1366 static __rte_noinline uint32_t
1367 virtio_dev_rx_packed(struct virtio_net *dev,
1368 struct vhost_virtqueue *__rte_restrict vq,
1369 struct rte_mbuf **__rte_restrict pkts,
1372 uint32_t pkt_idx = 0;
1375 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1377 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1378 if (!virtio_dev_rx_batch_packed(dev, vq,
1380 pkt_idx += PACKED_BATCH_SIZE;
1385 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1389 } while (pkt_idx < count);
1391 if (vq->shadow_used_idx) {
1392 do_data_copy_enqueue(dev, vq);
1393 vhost_flush_enqueue_shadow_packed(dev, vq);
1397 vhost_vring_call_packed(dev, vq);
1402 static __rte_always_inline uint32_t
1403 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1404 struct rte_mbuf **pkts, uint32_t count)
1406 struct vhost_virtqueue *vq;
1409 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1410 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1411 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1412 dev->vid, __func__, queue_id);
1416 vq = dev->virtqueue[queue_id];
1418 rte_spinlock_lock(&vq->access_lock);
1420 if (unlikely(!vq->enabled))
1421 goto out_access_unlock;
1423 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1424 vhost_user_iotlb_rd_lock(vq);
1426 if (unlikely(!vq->access_ok))
1427 if (unlikely(vring_translate(dev, vq) < 0))
1430 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1434 if (vq_is_packed(dev))
1435 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1437 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1440 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1441 vhost_user_iotlb_rd_unlock(vq);
1444 rte_spinlock_unlock(&vq->access_lock);
1450 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1451 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1453 struct virtio_net *dev = get_device(vid);
1458 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1460 "(%d) %s: built-in vhost net backend is disabled.\n",
1461 dev->vid, __func__);
1465 return virtio_dev_rx(dev, queue_id, pkts, count);
1468 static __rte_always_inline uint16_t
1469 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1470 uint16_t vq_size, uint16_t n_inflight)
1472 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1473 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1476 static __rte_always_inline void
1477 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1478 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1480 size_t elem_size = sizeof(struct vring_used_elem);
1482 if (d_idx + count <= ring_size) {
1483 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1485 uint16_t size = ring_size - d_idx;
1487 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1488 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1492 static __rte_always_inline void
1493 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1494 struct vring_used_elem_packed *d_ring,
1495 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1497 size_t elem_size = sizeof(struct vring_used_elem_packed);
1499 if (d_idx + count <= ring_size) {
1500 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1502 uint16_t size = ring_size - d_idx;
1504 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1505 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1509 static __rte_noinline uint32_t
1510 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1511 struct vhost_virtqueue *vq, uint16_t queue_id,
1512 struct rte_mbuf **pkts, uint32_t count,
1513 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
1515 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1516 uint16_t num_buffers;
1517 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1518 uint16_t avail_head;
1520 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1521 struct iovec *vec_pool = vq->vec_pool;
1522 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1523 struct iovec *src_iovec = vec_pool;
1524 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1525 uint16_t slot_idx = 0;
1526 uint16_t segs_await = 0;
1527 uint16_t iovec_idx = 0, it_idx = 0;
1528 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1529 uint32_t n_pkts = 0, pkt_err = 0;
1530 uint32_t num_async_pkts = 0, num_done_pkts = 0;
1533 uint16_t last_avail_idx;
1534 } async_pkts_log[MAX_PKT_BURST];
1537 * The ordering between avail index and desc reads need to be enforced.
1539 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1541 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1543 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1544 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1545 uint16_t nr_vec = 0;
1547 if (unlikely(reserve_avail_buf_split(dev, vq,
1548 pkt_len, buf_vec, &num_buffers,
1549 avail_head, &nr_vec) < 0)) {
1550 VHOST_LOG_DATA(DEBUG,
1551 "(%d) failed to get enough desc from vring\n",
1553 vq->shadow_used_idx -= num_buffers;
1557 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1558 dev->vid, vq->last_avail_idx,
1559 vq->last_avail_idx + num_buffers);
1561 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers,
1562 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1563 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0) {
1564 vq->shadow_used_idx -= num_buffers;
1568 slot_idx = (vq->async_pkts_idx + num_async_pkts) &
1570 if (it_pool[it_idx].count) {
1573 async_fill_desc(&tdes[pkt_burst_idx++],
1574 &it_pool[it_idx], &it_pool[it_idx + 1]);
1575 pkts_info[slot_idx].descs = num_buffers;
1576 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1577 async_pkts_log[num_async_pkts].pkt_idx = pkt_idx;
1578 async_pkts_log[num_async_pkts++].last_avail_idx =
1581 iovec_idx += it_pool[it_idx].nr_segs;
1584 segs_await += it_pool[it_idx].nr_segs;
1587 * recover shadow used ring and keep DMA-occupied
1590 from = vq->shadow_used_idx - num_buffers;
1591 to = vq->async_desc_idx_split & (vq->size - 1);
1593 store_dma_desc_info_split(vq->shadow_used_split,
1594 vq->async_descs_split, vq->size, from, to, num_buffers);
1596 vq->async_desc_idx_split += num_buffers;
1597 vq->shadow_used_idx -= num_buffers;
1599 comp_pkts[num_done_pkts++] = pkts[pkt_idx];
1601 vq->last_avail_idx += num_buffers;
1604 * conditions to trigger async device transfer:
1605 * - buffered packet number reaches transfer threshold
1606 * - unused async iov number is less than max vhost vector
1608 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1609 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await <
1611 n_pkts = vq->async_ops.transfer_data(dev->vid,
1612 queue_id, tdes, 0, pkt_burst_idx);
1617 vq->async_pkts_inflight_n += n_pkts;
1619 if (unlikely(n_pkts < pkt_burst_idx)) {
1621 * log error packets number here and do actual
1622 * error processing when applications poll
1625 pkt_err = pkt_burst_idx - n_pkts;
1634 if (pkt_burst_idx) {
1635 n_pkts = vq->async_ops.transfer_data(dev->vid,
1636 queue_id, tdes, 0, pkt_burst_idx);
1637 vq->async_pkts_inflight_n += n_pkts;
1639 if (unlikely(n_pkts < pkt_burst_idx))
1640 pkt_err = pkt_burst_idx - n_pkts;
1643 do_data_copy_enqueue(dev, vq);
1645 if (unlikely(pkt_err)) {
1646 uint16_t num_descs = 0;
1648 num_async_pkts -= pkt_err;
1649 /* calculate the sum of descriptors of DMA-error packets. */
1650 while (pkt_err-- > 0) {
1651 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1654 vq->async_desc_idx_split -= num_descs;
1655 /* recover shadow used ring and available ring */
1656 vq->shadow_used_idx -= (vq->last_avail_idx -
1657 async_pkts_log[num_async_pkts].last_avail_idx -
1659 vq->last_avail_idx =
1660 async_pkts_log[num_async_pkts].last_avail_idx;
1661 pkt_idx = async_pkts_log[num_async_pkts].pkt_idx;
1662 num_done_pkts = pkt_idx - num_async_pkts;
1665 vq->async_pkts_idx += num_async_pkts;
1666 *comp_count = num_done_pkts;
1668 if (likely(vq->shadow_used_idx)) {
1669 flush_shadow_used_ring_split(dev, vq);
1670 vhost_vring_call_split(dev, vq);
1676 static __rte_always_inline void
1677 vhost_update_used_packed(struct vhost_virtqueue *vq,
1678 struct vring_used_elem_packed *shadow_ring,
1682 uint16_t used_idx = vq->last_used_idx;
1683 uint16_t head_idx = vq->last_used_idx;
1684 uint16_t head_flags = 0;
1689 /* Split loop in two to save memory barriers */
1690 for (i = 0; i < count; i++) {
1691 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1692 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1694 used_idx += shadow_ring[i].count;
1695 if (used_idx >= vq->size)
1696 used_idx -= vq->size;
1699 /* The ordering for storing desc flags needs to be enforced. */
1700 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1702 for (i = 0; i < count; i++) {
1705 if (vq->shadow_used_packed[i].len)
1706 flags = VRING_DESC_F_WRITE;
1710 if (vq->used_wrap_counter) {
1711 flags |= VRING_DESC_F_USED;
1712 flags |= VRING_DESC_F_AVAIL;
1714 flags &= ~VRING_DESC_F_USED;
1715 flags &= ~VRING_DESC_F_AVAIL;
1719 vq->desc_packed[vq->last_used_idx].flags = flags;
1721 head_idx = vq->last_used_idx;
1725 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1728 vq->desc_packed[head_idx].flags = head_flags;
1731 static __rte_always_inline int
1732 virtio_dev_rx_async_batch_packed(struct virtio_net *dev,
1733 struct vhost_virtqueue *vq,
1734 struct rte_mbuf **pkts,
1735 struct rte_mbuf **comp_pkts, uint32_t *pkt_done)
1738 uint32_t cpy_threshold = vq->async_threshold;
1740 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1741 if (unlikely(pkts[i]->pkt_len >= cpy_threshold))
1744 if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
1745 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1746 comp_pkts[(*pkt_done)++] = pkts[i];
1754 static __rte_always_inline int
1755 vhost_enqueue_async_single_packed(struct virtio_net *dev,
1756 struct vhost_virtqueue *vq,
1757 struct rte_mbuf *pkt,
1758 struct buf_vector *buf_vec,
1760 uint16_t *nr_buffers,
1761 struct vring_packed_desc *async_descs,
1762 struct iovec *src_iovec, struct iovec *dst_iovec,
1763 struct rte_vhost_iov_iter *src_it,
1764 struct rte_vhost_iov_iter *dst_it)
1766 uint16_t nr_vec = 0;
1767 uint16_t avail_idx = vq->last_avail_idx;
1768 uint16_t max_tries, tries = 0;
1769 uint16_t buf_id = 0;
1771 uint16_t desc_count = 0;
1772 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1773 uint32_t buffer_len[vq->size];
1774 uint16_t buffer_buf_id[vq->size];
1775 uint16_t buffer_desc_count[vq->size];
1777 if (rxvq_is_mergeable(dev))
1778 max_tries = vq->size - 1;
1784 * if we tried all available ring items, and still
1785 * can't get enough buf, it means something abnormal
1788 if (unlikely(++tries > max_tries))
1791 if (unlikely(fill_vec_buf_packed(dev, vq, avail_idx, &desc_count, buf_vec, &nr_vec,
1792 &buf_id, &len, VHOST_ACCESS_RW) < 0))
1795 len = RTE_MIN(len, size);
1798 buffer_len[*nr_buffers] = len;
1799 buffer_buf_id[*nr_buffers] = buf_id;
1800 buffer_desc_count[*nr_buffers] = desc_count;
1803 *nr_descs += desc_count;
1804 avail_idx += desc_count;
1805 if (avail_idx >= vq->size)
1806 avail_idx -= vq->size;
1809 if (async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, src_iovec, dst_iovec,
1810 src_it, dst_it) < 0)
1812 /* store descriptors for DMA */
1813 if (avail_idx >= *nr_descs) {
1814 rte_memcpy(async_descs, &vq->desc_packed[vq->last_avail_idx],
1815 *nr_descs * sizeof(struct vring_packed_desc));
1817 uint16_t nr_copy = vq->size - vq->last_avail_idx;
1819 rte_memcpy(async_descs, &vq->desc_packed[vq->last_avail_idx],
1820 nr_copy * sizeof(struct vring_packed_desc));
1821 rte_memcpy(async_descs + nr_copy, vq->desc_packed,
1822 (*nr_descs - nr_copy) * sizeof(struct vring_packed_desc));
1825 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1830 static __rte_always_inline int16_t
1831 virtio_dev_rx_async_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1832 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers,
1833 struct vring_packed_desc *async_descs,
1834 struct iovec *src_iovec, struct iovec *dst_iovec,
1835 struct rte_vhost_iov_iter *src_it, struct rte_vhost_iov_iter *dst_it)
1837 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1839 if (unlikely(vhost_enqueue_async_single_packed(dev, vq, pkt, buf_vec, nr_descs, nr_buffers,
1840 async_descs, src_iovec, dst_iovec,
1841 src_it, dst_it) < 0)) {
1842 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1846 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1847 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1852 static __rte_always_inline void
1853 dma_error_handler_packed(struct vhost_virtqueue *vq, struct vring_packed_desc *async_descs,
1854 uint16_t async_descs_idx, uint16_t slot_idx, uint32_t nr_err,
1855 uint32_t *pkt_idx, uint32_t *num_async_pkts, uint32_t *num_done_pkts)
1857 uint16_t descs_err = 0;
1858 uint16_t buffers_err = 0;
1859 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1861 *num_async_pkts -= nr_err;
1863 /* calculate the sum of buffers and descs of DMA-error packets. */
1864 while (nr_err-- > 0) {
1865 descs_err += pkts_info[slot_idx % vq->size].descs;
1866 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1870 vq->async_buffer_idx_packed -= buffers_err;
1872 if (vq->last_avail_idx >= descs_err) {
1873 vq->last_avail_idx -= descs_err;
1875 rte_memcpy(&vq->desc_packed[vq->last_avail_idx],
1876 &async_descs[async_descs_idx - descs_err],
1877 descs_err * sizeof(struct vring_packed_desc));
1881 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1882 nr_copy = vq->size - vq->last_avail_idx;
1883 rte_memcpy(&vq->desc_packed[vq->last_avail_idx],
1884 &async_descs[async_descs_idx - descs_err],
1885 nr_copy * sizeof(struct vring_packed_desc));
1886 descs_err -= nr_copy;
1887 rte_memcpy(&vq->desc_packed[0], &async_descs[async_descs_idx - descs_err],
1888 descs_err * sizeof(struct vring_packed_desc));
1889 vq->avail_wrap_counter ^= 1;
1892 *num_done_pkts = *pkt_idx - *num_async_pkts;
1895 static __rte_noinline uint32_t
1896 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1897 struct vhost_virtqueue *vq, uint16_t queue_id,
1898 struct rte_mbuf **pkts, uint32_t count,
1899 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
1901 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1902 uint32_t remained = count;
1903 uint16_t async_descs_idx = 0;
1904 uint16_t num_buffers;
1907 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1908 struct iovec *vec_pool = vq->vec_pool;
1909 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1910 struct iovec *src_iovec = vec_pool;
1911 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1912 uint16_t slot_idx = 0;
1913 uint16_t segs_await = 0;
1914 uint16_t iovec_idx = 0, it_idx = 0;
1915 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1916 uint32_t n_pkts = 0, pkt_err = 0;
1917 uint32_t num_async_pkts = 0, num_done_pkts = 0;
1918 struct vring_packed_desc async_descs[vq->size];
1921 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1922 if (remained >= PACKED_BATCH_SIZE) {
1923 if (!virtio_dev_rx_async_batch_packed(dev, vq,
1924 &pkts[pkt_idx], comp_pkts, &num_done_pkts)) {
1925 pkt_idx += PACKED_BATCH_SIZE;
1926 remained -= PACKED_BATCH_SIZE;
1933 if (unlikely(virtio_dev_rx_async_single_packed(dev, vq, pkts[pkt_idx],
1934 &num_descs, &num_buffers,
1935 &async_descs[async_descs_idx],
1936 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1937 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0))
1940 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1941 dev->vid, vq->last_avail_idx,
1942 vq->last_avail_idx + num_descs);
1944 slot_idx = (vq->async_pkts_idx + num_async_pkts) % vq->size;
1945 if (it_pool[it_idx].count) {
1948 async_descs_idx += num_descs;
1949 async_fill_desc(&tdes[pkt_burst_idx++],
1950 &it_pool[it_idx], &it_pool[it_idx + 1]);
1951 pkts_info[slot_idx].descs = num_descs;
1952 pkts_info[slot_idx].nr_buffers = num_buffers;
1953 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1955 iovec_idx += it_pool[it_idx].nr_segs;
1958 segs_await += it_pool[it_idx].nr_segs;
1961 * recover shadow used ring and keep DMA-occupied
1964 from = vq->shadow_used_idx - num_buffers;
1965 to = vq->async_buffer_idx_packed % vq->size;
1966 store_dma_desc_info_packed(vq->shadow_used_packed,
1967 vq->async_buffers_packed, vq->size, from, to, num_buffers);
1969 vq->async_buffer_idx_packed += num_buffers;
1970 vq->shadow_used_idx -= num_buffers;
1972 comp_pkts[num_done_pkts++] = pkts[pkt_idx];
1977 vq_inc_last_avail_packed(vq, num_descs);
1980 * conditions to trigger async device transfer:
1981 * - buffered packet number reaches transfer threshold
1982 * - unused async iov number is less than max vhost vector
1984 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1985 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await < BUF_VECTOR_MAX))) {
1986 n_pkts = vq->async_ops.transfer_data(dev->vid, queue_id,
1987 tdes, 0, pkt_burst_idx);
1991 vq->async_pkts_inflight_n += n_pkts;
1993 if (unlikely(n_pkts < pkt_burst_idx)) {
1995 * log error packets number here and do actual
1996 * error processing when applications poll
1999 pkt_err = pkt_burst_idx - n_pkts;
2006 } while (pkt_idx < count);
2008 if (pkt_burst_idx) {
2009 n_pkts = vq->async_ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
2010 vq->async_pkts_inflight_n += n_pkts;
2012 if (unlikely(n_pkts < pkt_burst_idx))
2013 pkt_err = pkt_burst_idx - n_pkts;
2016 do_data_copy_enqueue(dev, vq);
2018 if (unlikely(pkt_err))
2019 dma_error_handler_packed(vq, async_descs, async_descs_idx, slot_idx, pkt_err,
2020 &pkt_idx, &num_async_pkts, &num_done_pkts);
2021 vq->async_pkts_idx += num_async_pkts;
2022 *comp_count = num_done_pkts;
2024 if (likely(vq->shadow_used_idx)) {
2025 vhost_flush_enqueue_shadow_packed(dev, vq);
2026 vhost_vring_call_packed(dev, vq);
2032 static __rte_always_inline void
2033 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
2035 uint16_t nr_left = n_descs;
2040 from = vq->last_async_desc_idx_split & (vq->size - 1);
2041 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
2042 to = vq->last_used_idx & (vq->size - 1);
2044 if (to + nr_copy <= vq->size) {
2045 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
2046 nr_copy * sizeof(struct vring_used_elem));
2048 uint16_t size = vq->size - to;
2050 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
2051 size * sizeof(struct vring_used_elem));
2052 rte_memcpy(&vq->used->ring[0], &vq->async_descs_split[from + size],
2053 (nr_copy - size) * sizeof(struct vring_used_elem));
2056 vq->last_async_desc_idx_split += nr_copy;
2057 vq->last_used_idx += nr_copy;
2059 } while (nr_left > 0);
2062 static __rte_always_inline void
2063 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
2066 uint16_t nr_left = n_buffers;
2070 from = vq->last_async_buffer_idx_packed % vq->size;
2071 to = (from + nr_left) % vq->size;
2073 vhost_update_used_packed(vq, vq->async_buffers_packed + from, to - from);
2074 vq->last_async_buffer_idx_packed += nr_left;
2077 vhost_update_used_packed(vq, vq->async_buffers_packed + from,
2079 vq->last_async_buffer_idx_packed += vq->size - from;
2080 nr_left -= vq->size - from;
2082 } while (nr_left > 0);
2085 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2086 struct rte_mbuf **pkts, uint16_t count)
2088 struct virtio_net *dev = get_device(vid);
2089 struct vhost_virtqueue *vq;
2090 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0, n_buffers = 0;
2091 uint16_t start_idx, pkts_idx, vq_size;
2092 struct async_inflight_info *pkts_info;
2098 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2099 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2100 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2101 dev->vid, __func__, queue_id);
2105 vq = dev->virtqueue[queue_id];
2107 if (unlikely(!vq->async_registered)) {
2108 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2109 dev->vid, __func__, queue_id);
2113 rte_spinlock_lock(&vq->access_lock);
2115 pkts_idx = vq->async_pkts_idx % vq->size;
2116 pkts_info = vq->async_pkts_info;
2118 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
2119 vq_size, vq->async_pkts_inflight_n);
2121 if (count > vq->async_last_pkts_n)
2122 n_pkts_cpl = vq->async_ops.check_completed_copies(vid,
2123 queue_id, 0, count - vq->async_last_pkts_n);
2124 n_pkts_cpl += vq->async_last_pkts_n;
2126 n_pkts_put = RTE_MIN(count, n_pkts_cpl);
2127 if (unlikely(n_pkts_put == 0)) {
2128 vq->async_last_pkts_n = n_pkts_cpl;
2132 if (vq_is_packed(dev)) {
2133 for (i = 0; i < n_pkts_put; i++) {
2134 from = (start_idx + i) & (vq_size - 1);
2135 n_buffers += pkts_info[from].nr_buffers;
2136 pkts[i] = pkts_info[from].mbuf;
2139 for (i = 0; i < n_pkts_put; i++) {
2140 from = (start_idx + i) & (vq_size - 1);
2141 n_descs += pkts_info[from].descs;
2142 pkts[i] = pkts_info[from].mbuf;
2146 vq->async_last_pkts_n = n_pkts_cpl - n_pkts_put;
2147 vq->async_pkts_inflight_n -= n_pkts_put;
2149 if (likely(vq->enabled && vq->access_ok)) {
2150 if (vq_is_packed(dev)) {
2151 write_back_completed_descs_packed(vq, n_buffers);
2153 vhost_vring_call_packed(dev, vq);
2155 write_back_completed_descs_split(vq, n_descs);
2157 __atomic_add_fetch(&vq->used->idx, n_descs,
2159 vhost_vring_call_split(dev, vq);
2162 if (vq_is_packed(dev))
2163 vq->last_async_buffer_idx_packed += n_buffers;
2165 vq->last_async_desc_idx_split += n_descs;
2169 rte_spinlock_unlock(&vq->access_lock);
2174 static __rte_always_inline uint32_t
2175 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2176 struct rte_mbuf **pkts, uint32_t count,
2177 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
2179 struct vhost_virtqueue *vq;
2182 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2183 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2184 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2185 dev->vid, __func__, queue_id);
2189 vq = dev->virtqueue[queue_id];
2191 rte_spinlock_lock(&vq->access_lock);
2193 if (unlikely(!vq->enabled || !vq->async_registered))
2194 goto out_access_unlock;
2196 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2197 vhost_user_iotlb_rd_lock(vq);
2199 if (unlikely(!vq->access_ok))
2200 if (unlikely(vring_translate(dev, vq) < 0))
2203 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2207 if (vq_is_packed(dev))
2208 nb_tx = virtio_dev_rx_async_submit_packed(dev,
2209 vq, queue_id, pkts, count, comp_pkts,
2212 nb_tx = virtio_dev_rx_async_submit_split(dev,
2213 vq, queue_id, pkts, count, comp_pkts,
2217 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2218 vhost_user_iotlb_rd_unlock(vq);
2221 rte_spinlock_unlock(&vq->access_lock);
2227 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2228 struct rte_mbuf **pkts, uint16_t count,
2229 struct rte_mbuf **comp_pkts, uint32_t *comp_count)
2231 struct virtio_net *dev = get_device(vid);
2237 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2239 "(%d) %s: built-in vhost net backend is disabled.\n",
2240 dev->vid, __func__);
2244 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, comp_pkts,
2249 virtio_net_with_host_offload(struct virtio_net *dev)
2252 ((1ULL << VIRTIO_NET_F_CSUM) |
2253 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2254 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2255 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2256 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2263 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
2265 struct rte_ipv4_hdr *ipv4_hdr;
2266 struct rte_ipv6_hdr *ipv6_hdr;
2267 void *l3_hdr = NULL;
2268 struct rte_ether_hdr *eth_hdr;
2271 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2273 m->l2_len = sizeof(struct rte_ether_hdr);
2274 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2276 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2277 struct rte_vlan_hdr *vlan_hdr =
2278 (struct rte_vlan_hdr *)(eth_hdr + 1);
2280 m->l2_len += sizeof(struct rte_vlan_hdr);
2281 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2284 l3_hdr = (char *)eth_hdr + m->l2_len;
2286 switch (ethertype) {
2287 case RTE_ETHER_TYPE_IPV4:
2289 *l4_proto = ipv4_hdr->next_proto_id;
2290 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2291 *l4_hdr = (char *)l3_hdr + m->l3_len;
2292 m->ol_flags |= PKT_TX_IPV4;
2294 case RTE_ETHER_TYPE_IPV6:
2296 *l4_proto = ipv6_hdr->proto;
2297 m->l3_len = sizeof(struct rte_ipv6_hdr);
2298 *l4_hdr = (char *)l3_hdr + m->l3_len;
2299 m->ol_flags |= PKT_TX_IPV6;
2309 static __rte_always_inline void
2310 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2312 uint16_t l4_proto = 0;
2313 void *l4_hdr = NULL;
2314 struct rte_tcp_hdr *tcp_hdr = NULL;
2316 parse_ethernet(m, &l4_proto, &l4_hdr);
2317 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2318 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2319 switch (hdr->csum_offset) {
2320 case (offsetof(struct rte_tcp_hdr, cksum)):
2321 if (l4_proto == IPPROTO_TCP)
2322 m->ol_flags |= PKT_TX_TCP_CKSUM;
2324 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2325 if (l4_proto == IPPROTO_UDP)
2326 m->ol_flags |= PKT_TX_UDP_CKSUM;
2328 case (offsetof(struct rte_sctp_hdr, cksum)):
2329 if (l4_proto == IPPROTO_SCTP)
2330 m->ol_flags |= PKT_TX_SCTP_CKSUM;
2338 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2339 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2340 case VIRTIO_NET_HDR_GSO_TCPV4:
2341 case VIRTIO_NET_HDR_GSO_TCPV6:
2343 m->ol_flags |= PKT_TX_TCP_SEG;
2344 m->tso_segsz = hdr->gso_size;
2345 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
2347 case VIRTIO_NET_HDR_GSO_UDP:
2348 m->ol_flags |= PKT_TX_UDP_SEG;
2349 m->tso_segsz = hdr->gso_size;
2350 m->l4_len = sizeof(struct rte_udp_hdr);
2353 VHOST_LOG_DATA(WARNING,
2354 "unsupported gso type %u.\n", hdr->gso_type);
2360 static __rte_always_inline void
2361 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2362 bool legacy_ol_flags)
2364 struct rte_net_hdr_lens hdr_lens;
2365 int l4_supported = 0;
2368 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2371 if (legacy_ol_flags) {
2372 vhost_dequeue_offload_legacy(hdr, m);
2376 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
2378 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2379 m->packet_type = ptype;
2380 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2381 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2382 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2385 /* According to Virtio 1.1 spec, the device only needs to look at
2386 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2387 * This differs from the processing incoming packets path where the
2388 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2391 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2392 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2393 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2395 * 5.1.6.2.2 Device Requirements: Packet Transmission
2396 * The device MUST ignore flag bits that it does not recognize.
2398 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2401 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2402 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2403 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
2405 /* Unknown proto or tunnel, do sw cksum. We can assume
2406 * the cksum field is in the first segment since the
2407 * buffers we provided to the host are large enough.
2408 * In case of SCTP, this will be wrong since it's a CRC
2409 * but there's nothing we can do.
2411 uint16_t csum = 0, off;
2413 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2414 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2416 if (likely(csum != 0xffff))
2418 off = hdr->csum_offset + hdr->csum_start;
2419 if (rte_pktmbuf_data_len(m) >= off + 1)
2420 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2424 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2425 if (hdr->gso_size == 0)
2428 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2429 case VIRTIO_NET_HDR_GSO_TCPV4:
2430 case VIRTIO_NET_HDR_GSO_TCPV6:
2431 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2433 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2434 m->tso_segsz = hdr->gso_size;
2436 case VIRTIO_NET_HDR_GSO_UDP:
2437 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2439 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2440 m->tso_segsz = hdr->gso_size;
2448 static __rte_noinline void
2449 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2450 struct buf_vector *buf_vec)
2453 uint64_t remain = sizeof(struct virtio_net_hdr);
2455 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2458 len = RTE_MIN(remain, buf_vec->buf_len);
2459 src = buf_vec->buf_addr;
2460 rte_memcpy((void *)(uintptr_t)dst,
2461 (void *)(uintptr_t)src, len);
2469 static __rte_always_inline int
2470 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2471 struct buf_vector *buf_vec, uint16_t nr_vec,
2472 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2473 bool legacy_ol_flags)
2475 uint32_t buf_avail, buf_offset;
2476 uint64_t buf_addr, buf_len;
2477 uint32_t mbuf_avail, mbuf_offset;
2479 struct rte_mbuf *cur = m, *prev = m;
2480 struct virtio_net_hdr tmp_hdr;
2481 struct virtio_net_hdr *hdr = NULL;
2482 /* A counter to avoid desc dead loop chain */
2483 uint16_t vec_idx = 0;
2484 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2487 buf_addr = buf_vec[vec_idx].buf_addr;
2488 buf_len = buf_vec[vec_idx].buf_len;
2490 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2495 if (virtio_net_with_host_offload(dev)) {
2496 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2498 * No luck, the virtio-net header doesn't fit
2499 * in a contiguous virtual area.
2501 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2504 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2509 * A virtio driver normally uses at least 2 desc buffers
2510 * for Tx: the first for storing the header, and others
2511 * for storing the data.
2513 if (unlikely(buf_len < dev->vhost_hlen)) {
2514 buf_offset = dev->vhost_hlen - buf_len;
2516 buf_addr = buf_vec[vec_idx].buf_addr;
2517 buf_len = buf_vec[vec_idx].buf_len;
2518 buf_avail = buf_len - buf_offset;
2519 } else if (buf_len == dev->vhost_hlen) {
2520 if (unlikely(++vec_idx >= nr_vec))
2522 buf_addr = buf_vec[vec_idx].buf_addr;
2523 buf_len = buf_vec[vec_idx].buf_len;
2526 buf_avail = buf_len;
2528 buf_offset = dev->vhost_hlen;
2529 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2533 (uintptr_t)(buf_addr + buf_offset),
2534 (uint32_t)buf_avail, 0);
2537 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2539 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2541 if (likely(cpy_len > MAX_BATCH_LEN ||
2542 vq->batch_copy_nb_elems >= vq->size ||
2543 (hdr && cur == m))) {
2544 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2546 (void *)((uintptr_t)(buf_addr +
2547 buf_offset)), cpy_len);
2549 batch_copy[vq->batch_copy_nb_elems].dst =
2550 rte_pktmbuf_mtod_offset(cur, void *,
2552 batch_copy[vq->batch_copy_nb_elems].src =
2553 (void *)((uintptr_t)(buf_addr + buf_offset));
2554 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2555 vq->batch_copy_nb_elems++;
2558 mbuf_avail -= cpy_len;
2559 mbuf_offset += cpy_len;
2560 buf_avail -= cpy_len;
2561 buf_offset += cpy_len;
2563 /* This buf reaches to its end, get the next one */
2564 if (buf_avail == 0) {
2565 if (++vec_idx >= nr_vec)
2568 buf_addr = buf_vec[vec_idx].buf_addr;
2569 buf_len = buf_vec[vec_idx].buf_len;
2572 buf_avail = buf_len;
2574 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2575 (uint32_t)buf_avail, 0);
2579 * This mbuf reaches to its end, get a new one
2580 * to hold more data.
2582 if (mbuf_avail == 0) {
2583 cur = rte_pktmbuf_alloc(mbuf_pool);
2584 if (unlikely(cur == NULL)) {
2585 VHOST_LOG_DATA(ERR, "Failed to "
2586 "allocate memory for mbuf.\n");
2592 prev->data_len = mbuf_offset;
2594 m->pkt_len += mbuf_offset;
2598 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2602 prev->data_len = mbuf_offset;
2603 m->pkt_len += mbuf_offset;
2606 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2614 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2620 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2622 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2623 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2628 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2629 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2631 if (unlikely(total_len > UINT16_MAX))
2634 buf_len = total_len;
2635 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2636 if (unlikely(buf == NULL))
2639 /* Initialize shinfo */
2640 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2641 virtio_dev_extbuf_free, buf);
2642 if (unlikely(shinfo == NULL)) {
2644 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2648 iova = rte_malloc_virt2iova(buf);
2649 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2650 rte_pktmbuf_reset_headroom(pkt);
2656 * Prepare a host supported pktmbuf.
2658 static __rte_always_inline int
2659 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2662 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2665 /* attach an external buffer if supported */
2666 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2669 /* check if chained buffers are allowed */
2670 if (!dev->linearbuf)
2678 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2679 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2680 bool legacy_ol_flags)
2683 uint16_t free_entries;
2684 uint16_t dropped = 0;
2685 static bool allocerr_warned;
2688 * The ordering between avail index and
2689 * desc reads needs to be enforced.
2691 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2693 if (free_entries == 0)
2696 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2698 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2700 count = RTE_MIN(count, MAX_PKT_BURST);
2701 count = RTE_MIN(count, free_entries);
2702 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2705 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2708 for (i = 0; i < count; i++) {
2709 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2712 uint16_t nr_vec = 0;
2715 if (unlikely(fill_vec_buf_split(dev, vq,
2716 vq->last_avail_idx + i,
2718 &head_idx, &buf_len,
2719 VHOST_ACCESS_RO) < 0))
2722 update_shadow_used_ring_split(vq, head_idx, 0);
2724 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2725 if (unlikely(err)) {
2727 * mbuf allocation fails for jumbo packets when external
2728 * buffer allocation is not allowed and linear buffer
2729 * is required. Drop this packet.
2731 if (!allocerr_warned) {
2733 "Failed mbuf alloc of size %d from %s on %s.\n",
2734 buf_len, mbuf_pool->name, dev->ifname);
2735 allocerr_warned = true;
2742 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2743 mbuf_pool, legacy_ol_flags);
2744 if (unlikely(err)) {
2745 if (!allocerr_warned) {
2747 "Failed to copy desc to mbuf on %s.\n",
2749 allocerr_warned = true;
2758 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2760 vq->last_avail_idx += i;
2762 do_data_copy_dequeue(vq);
2763 if (unlikely(i < count))
2764 vq->shadow_used_idx = i;
2765 if (likely(vq->shadow_used_idx)) {
2766 flush_shadow_used_ring_split(dev, vq);
2767 vhost_vring_call_split(dev, vq);
2770 return (i - dropped);
2775 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2776 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2777 struct rte_mbuf **pkts, uint16_t count)
2779 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2784 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2785 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2786 struct rte_mbuf **pkts, uint16_t count)
2788 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2791 static __rte_always_inline int
2792 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2793 struct vhost_virtqueue *vq,
2794 struct rte_mbuf **pkts,
2796 uintptr_t *desc_addrs,
2799 bool wrap = vq->avail_wrap_counter;
2800 struct vring_packed_desc *descs = vq->desc_packed;
2801 uint64_t lens[PACKED_BATCH_SIZE];
2802 uint64_t buf_lens[PACKED_BATCH_SIZE];
2803 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2806 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2808 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2811 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2812 flags = descs[avail_idx + i].flags;
2813 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2814 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2815 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2819 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2821 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2822 lens[i] = descs[avail_idx + i].len;
2824 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2825 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2826 descs[avail_idx + i].addr,
2827 &lens[i], VHOST_ACCESS_RW);
2830 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2831 if (unlikely(!desc_addrs[i]))
2833 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2837 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2838 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2842 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2843 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2845 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2846 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2850 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2851 pkts[i]->pkt_len = lens[i] - buf_offset;
2852 pkts[i]->data_len = pkts[i]->pkt_len;
2853 ids[i] = descs[avail_idx + i].id;
2862 static __rte_always_inline int
2863 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2864 struct vhost_virtqueue *vq,
2865 struct rte_mbuf **pkts,
2866 bool legacy_ol_flags)
2868 uint16_t avail_idx = vq->last_avail_idx;
2869 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2870 struct virtio_net_hdr *hdr;
2871 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2872 uint16_t ids[PACKED_BATCH_SIZE];
2875 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2879 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2880 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2882 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2883 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2884 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2887 if (virtio_net_with_host_offload(dev)) {
2888 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2889 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2890 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2894 if (virtio_net_is_inorder(dev))
2895 vhost_shadow_dequeue_batch_packed_inorder(vq,
2896 ids[PACKED_BATCH_SIZE - 1]);
2898 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2900 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2905 static __rte_always_inline int
2906 vhost_dequeue_single_packed(struct virtio_net *dev,
2907 struct vhost_virtqueue *vq,
2908 struct rte_mempool *mbuf_pool,
2909 struct rte_mbuf *pkts,
2911 uint16_t *desc_count,
2912 bool legacy_ol_flags)
2914 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2916 uint16_t nr_vec = 0;
2918 static bool allocerr_warned;
2920 if (unlikely(fill_vec_buf_packed(dev, vq,
2921 vq->last_avail_idx, desc_count,
2924 VHOST_ACCESS_RO) < 0))
2927 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2928 if (!allocerr_warned) {
2930 "Failed mbuf alloc of size %d from %s on %s.\n",
2931 buf_len, mbuf_pool->name, dev->ifname);
2932 allocerr_warned = true;
2937 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2938 mbuf_pool, legacy_ol_flags);
2939 if (unlikely(err)) {
2940 if (!allocerr_warned) {
2942 "Failed to copy desc to mbuf on %s.\n",
2944 allocerr_warned = true;
2952 static __rte_always_inline int
2953 virtio_dev_tx_single_packed(struct virtio_net *dev,
2954 struct vhost_virtqueue *vq,
2955 struct rte_mempool *mbuf_pool,
2956 struct rte_mbuf *pkts,
2957 bool legacy_ol_flags)
2960 uint16_t buf_id, desc_count = 0;
2963 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2964 &desc_count, legacy_ol_flags);
2966 if (likely(desc_count > 0)) {
2967 if (virtio_net_is_inorder(dev))
2968 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2971 vhost_shadow_dequeue_single_packed(vq, buf_id,
2974 vq_inc_last_avail_packed(vq, desc_count);
2982 virtio_dev_tx_packed(struct virtio_net *dev,
2983 struct vhost_virtqueue *__rte_restrict vq,
2984 struct rte_mempool *mbuf_pool,
2985 struct rte_mbuf **__rte_restrict pkts,
2987 bool legacy_ol_flags)
2989 uint32_t pkt_idx = 0;
2991 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2995 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2997 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2998 if (!virtio_dev_tx_batch_packed(dev, vq,
3001 pkt_idx += PACKED_BATCH_SIZE;
3006 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3011 } while (pkt_idx < count);
3013 if (pkt_idx != count)
3014 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3016 if (vq->shadow_used_idx) {
3017 do_data_copy_dequeue(vq);
3019 vhost_flush_dequeue_shadow_packed(dev, vq);
3020 vhost_vring_call_packed(dev, vq);
3028 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3029 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3030 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3032 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3037 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3038 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3039 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3041 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3045 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3046 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3048 struct virtio_net *dev;
3049 struct rte_mbuf *rarp_mbuf = NULL;
3050 struct vhost_virtqueue *vq;
3051 int16_t success = 1;
3053 dev = get_device(vid);
3057 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3059 "(%d) %s: built-in vhost net backend is disabled.\n",
3060 dev->vid, __func__);
3064 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3066 "(%d) %s: invalid virtqueue idx %d.\n",
3067 dev->vid, __func__, queue_id);
3071 vq = dev->virtqueue[queue_id];
3073 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3076 if (unlikely(!vq->enabled)) {
3078 goto out_access_unlock;
3081 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3082 vhost_user_iotlb_rd_lock(vq);
3084 if (unlikely(!vq->access_ok))
3085 if (unlikely(vring_translate(dev, vq) < 0)) {
3091 * Construct a RARP broadcast packet, and inject it to the "pkts"
3092 * array, to looks like that guest actually send such packet.
3094 * Check user_send_rarp() for more information.
3096 * broadcast_rarp shares a cacheline in the virtio_net structure
3097 * with some fields that are accessed during enqueue and
3098 * __atomic_compare_exchange_n causes a write if performed compare
3099 * and exchange. This could result in false sharing between enqueue
3102 * Prevent unnecessary false sharing by reading broadcast_rarp first
3103 * and only performing compare and exchange if the read indicates it
3104 * is likely to be set.
3106 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3107 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3108 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3110 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3111 if (rarp_mbuf == NULL) {
3112 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3119 if (vq_is_packed(dev)) {
3120 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3121 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3123 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3125 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3126 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3128 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3132 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3133 vhost_user_iotlb_rd_unlock(vq);
3136 rte_spinlock_unlock(&vq->access_lock);
3138 if (unlikely(rarp_mbuf != NULL)) {
3140 * Inject it to the head of "pkts" array, so that switch's mac
3141 * learning table will get updated first.
3143 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
3144 pkts[0] = rarp_mbuf;