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 last_used_idx = vq->last_used_idx;
225 desc_base = &vq->desc_packed[last_used_idx];
227 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
229 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
230 desc_base[i].id = ids[i];
231 desc_base[i].len = lens[i];
234 rte_atomic_thread_fence(__ATOMIC_RELEASE);
236 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
237 desc_base[i].flags = flags;
240 vhost_log_cache_used_vring(dev, vq, last_used_idx *
241 sizeof(struct vring_packed_desc),
242 sizeof(struct vring_packed_desc) *
244 vhost_log_cache_sync(dev, vq);
246 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
249 static __rte_always_inline void
250 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
253 vq->shadow_used_packed[0].id = id;
255 if (!vq->shadow_used_idx) {
256 vq->shadow_last_used_idx = vq->last_used_idx;
257 vq->shadow_used_packed[0].flags =
258 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
259 vq->shadow_used_packed[0].len = 0;
260 vq->shadow_used_packed[0].count = 1;
261 vq->shadow_used_idx++;
264 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
267 static __rte_always_inline void
268 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
269 struct vhost_virtqueue *vq,
276 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
278 if (!vq->shadow_used_idx) {
279 vq->shadow_last_used_idx = vq->last_used_idx;
280 vq->shadow_used_packed[0].id = ids[0];
281 vq->shadow_used_packed[0].len = 0;
282 vq->shadow_used_packed[0].count = 1;
283 vq->shadow_used_packed[0].flags = flags;
284 vq->shadow_used_idx++;
289 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
290 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
291 vq->desc_packed[vq->last_used_idx + i].len = 0;
294 rte_atomic_thread_fence(__ATOMIC_RELEASE);
295 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
296 vq->desc_packed[vq->last_used_idx + i].flags = flags;
298 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
299 sizeof(struct vring_packed_desc),
300 sizeof(struct vring_packed_desc) *
302 vhost_log_cache_sync(dev, vq);
304 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
307 static __rte_always_inline void
308 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
314 flags = vq->desc_packed[vq->last_used_idx].flags;
315 if (vq->used_wrap_counter) {
316 flags |= VRING_DESC_F_USED;
317 flags |= VRING_DESC_F_AVAIL;
319 flags &= ~VRING_DESC_F_USED;
320 flags &= ~VRING_DESC_F_AVAIL;
323 if (!vq->shadow_used_idx) {
324 vq->shadow_last_used_idx = vq->last_used_idx;
326 vq->shadow_used_packed[0].id = buf_id;
327 vq->shadow_used_packed[0].len = 0;
328 vq->shadow_used_packed[0].flags = flags;
329 vq->shadow_used_idx++;
331 vq->desc_packed[vq->last_used_idx].id = buf_id;
332 vq->desc_packed[vq->last_used_idx].len = 0;
333 vq->desc_packed[vq->last_used_idx].flags = flags;
336 vq_inc_last_used_packed(vq, count);
339 static __rte_always_inline void
340 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
346 vq->shadow_used_packed[0].id = buf_id;
348 flags = vq->desc_packed[vq->last_used_idx].flags;
349 if (vq->used_wrap_counter) {
350 flags |= VRING_DESC_F_USED;
351 flags |= VRING_DESC_F_AVAIL;
353 flags &= ~VRING_DESC_F_USED;
354 flags &= ~VRING_DESC_F_AVAIL;
357 if (!vq->shadow_used_idx) {
358 vq->shadow_last_used_idx = vq->last_used_idx;
359 vq->shadow_used_packed[0].len = 0;
360 vq->shadow_used_packed[0].flags = flags;
361 vq->shadow_used_idx++;
364 vq_inc_last_used_packed(vq, count);
367 static __rte_always_inline void
368 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
372 uint16_t num_buffers)
376 for (i = 0; i < num_buffers; i++) {
377 /* enqueue shadow flush action aligned with batch num */
378 if (!vq->shadow_used_idx)
379 vq->shadow_aligned_idx = vq->last_used_idx &
381 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
382 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
383 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
384 vq->shadow_aligned_idx += count[i];
385 vq->shadow_used_idx++;
389 static __rte_always_inline void
390 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
391 struct vhost_virtqueue *vq,
395 uint16_t num_buffers)
397 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
399 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
400 do_data_copy_enqueue(dev, vq);
401 vhost_flush_enqueue_shadow_packed(dev, vq);
405 /* avoid write operation when necessary, to lessen cache issues */
406 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
407 if ((var) != (val)) \
411 static __rte_always_inline void
412 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
414 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
416 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
417 csum_l4 |= PKT_TX_TCP_CKSUM;
420 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
421 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
424 case PKT_TX_TCP_CKSUM:
425 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
428 case PKT_TX_UDP_CKSUM:
429 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
432 case PKT_TX_SCTP_CKSUM:
433 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
438 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
439 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
440 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
443 /* IP cksum verification cannot be bypassed, then calculate here */
444 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
445 struct rte_ipv4_hdr *ipv4_hdr;
447 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
449 ipv4_hdr->hdr_checksum = 0;
450 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
453 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
454 if (m_buf->ol_flags & PKT_TX_IPV4)
455 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
457 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
458 net_hdr->gso_size = m_buf->tso_segsz;
459 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
461 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
462 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
463 net_hdr->gso_size = m_buf->tso_segsz;
464 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
467 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
468 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
469 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
473 static __rte_always_inline int
474 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
475 struct buf_vector *buf_vec, uint16_t *vec_idx,
476 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
478 uint16_t vec_id = *vec_idx;
482 uint64_t desc_chunck_len = desc_len;
484 if (unlikely(vec_id >= BUF_VECTOR_MAX))
487 desc_addr = vhost_iova_to_vva(dev, vq,
491 if (unlikely(!desc_addr))
494 rte_prefetch0((void *)(uintptr_t)desc_addr);
496 buf_vec[vec_id].buf_iova = desc_iova;
497 buf_vec[vec_id].buf_addr = desc_addr;
498 buf_vec[vec_id].buf_len = desc_chunck_len;
500 desc_len -= desc_chunck_len;
501 desc_iova += desc_chunck_len;
509 static __rte_always_inline int
510 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
511 uint32_t avail_idx, uint16_t *vec_idx,
512 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
513 uint32_t *desc_chain_len, uint8_t perm)
515 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
516 uint16_t vec_id = *vec_idx;
519 uint32_t nr_descs = vq->size;
521 struct vring_desc *descs = vq->desc;
522 struct vring_desc *idesc = NULL;
524 if (unlikely(idx >= vq->size))
527 *desc_chain_head = idx;
529 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
530 dlen = vq->desc[idx].len;
531 nr_descs = dlen / sizeof(struct vring_desc);
532 if (unlikely(nr_descs > vq->size))
535 descs = (struct vring_desc *)(uintptr_t)
536 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
539 if (unlikely(!descs))
542 if (unlikely(dlen < vq->desc[idx].len)) {
544 * The indirect desc table is not contiguous
545 * in process VA space, we have to copy it.
547 idesc = vhost_alloc_copy_ind_table(dev, vq,
548 vq->desc[idx].addr, vq->desc[idx].len);
549 if (unlikely(!idesc))
559 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
560 free_ind_table(idesc);
564 dlen = descs[idx].len;
567 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
568 descs[idx].addr, dlen,
570 free_ind_table(idesc);
574 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
577 idx = descs[idx].next;
580 *desc_chain_len = len;
583 if (unlikely(!!idesc))
584 free_ind_table(idesc);
590 * Returns -1 on fail, 0 on success
593 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
594 uint32_t size, struct buf_vector *buf_vec,
595 uint16_t *num_buffers, uint16_t avail_head,
599 uint16_t vec_idx = 0;
600 uint16_t max_tries, tries = 0;
602 uint16_t head_idx = 0;
606 cur_idx = vq->last_avail_idx;
608 if (rxvq_is_mergeable(dev))
609 max_tries = vq->size - 1;
614 if (unlikely(cur_idx == avail_head))
617 * if we tried all available ring items, and still
618 * can't get enough buf, it means something abnormal
621 if (unlikely(++tries > max_tries))
624 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
627 VHOST_ACCESS_RW) < 0))
629 len = RTE_MIN(len, size);
630 update_shadow_used_ring_split(vq, head_idx, len);
642 static __rte_always_inline int
643 fill_vec_buf_packed_indirect(struct virtio_net *dev,
644 struct vhost_virtqueue *vq,
645 struct vring_packed_desc *desc, uint16_t *vec_idx,
646 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
650 uint16_t vec_id = *vec_idx;
652 struct vring_packed_desc *descs, *idescs = NULL;
655 descs = (struct vring_packed_desc *)(uintptr_t)
656 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
657 if (unlikely(!descs))
660 if (unlikely(dlen < desc->len)) {
662 * The indirect desc table is not contiguous
663 * in process VA space, we have to copy it.
665 idescs = vhost_alloc_copy_ind_table(dev,
666 vq, desc->addr, desc->len);
667 if (unlikely(!idescs))
673 nr_descs = desc->len / sizeof(struct vring_packed_desc);
674 if (unlikely(nr_descs >= vq->size)) {
675 free_ind_table(idescs);
679 for (i = 0; i < nr_descs; i++) {
680 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
681 free_ind_table(idescs);
687 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
694 if (unlikely(!!idescs))
695 free_ind_table(idescs);
700 static __rte_always_inline int
701 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
702 uint16_t avail_idx, uint16_t *desc_count,
703 struct buf_vector *buf_vec, uint16_t *vec_idx,
704 uint16_t *buf_id, uint32_t *len, uint8_t perm)
706 bool wrap_counter = vq->avail_wrap_counter;
707 struct vring_packed_desc *descs = vq->desc_packed;
708 uint16_t vec_id = *vec_idx;
711 if (avail_idx < vq->last_avail_idx)
715 * Perform a load-acquire barrier in desc_is_avail to
716 * enforce the ordering between desc flags and desc
719 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
726 if (unlikely(vec_id >= BUF_VECTOR_MAX))
729 if (unlikely(*desc_count >= vq->size))
733 *buf_id = descs[avail_idx].id;
735 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
736 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
742 dlen = descs[avail_idx].len;
745 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
746 descs[avail_idx].addr,
752 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
755 if (++avail_idx >= vq->size) {
756 avail_idx -= vq->size;
766 static __rte_noinline void
767 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
768 struct buf_vector *buf_vec,
769 struct virtio_net_hdr_mrg_rxbuf *hdr)
772 uint64_t remain = dev->vhost_hlen;
773 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
774 uint64_t iova = buf_vec->buf_iova;
777 len = RTE_MIN(remain,
779 dst = buf_vec->buf_addr;
780 rte_memcpy((void *)(uintptr_t)dst,
781 (void *)(uintptr_t)src,
784 PRINT_PACKET(dev, (uintptr_t)dst,
786 vhost_log_cache_write_iova(dev, vq,
796 static __rte_always_inline int
797 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
798 struct rte_mbuf *m, struct buf_vector *buf_vec,
799 uint16_t nr_vec, uint16_t num_buffers)
801 uint32_t vec_idx = 0;
802 uint32_t mbuf_offset, mbuf_avail;
803 uint32_t buf_offset, buf_avail;
804 uint64_t buf_addr, buf_iova, buf_len;
807 struct rte_mbuf *hdr_mbuf;
808 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
809 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
812 if (unlikely(m == NULL)) {
817 buf_addr = buf_vec[vec_idx].buf_addr;
818 buf_iova = buf_vec[vec_idx].buf_iova;
819 buf_len = buf_vec[vec_idx].buf_len;
821 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
828 if (unlikely(buf_len < dev->vhost_hlen)) {
829 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
832 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
834 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
835 dev->vid, num_buffers);
837 if (unlikely(buf_len < dev->vhost_hlen)) {
838 buf_offset = dev->vhost_hlen - buf_len;
840 buf_addr = buf_vec[vec_idx].buf_addr;
841 buf_iova = buf_vec[vec_idx].buf_iova;
842 buf_len = buf_vec[vec_idx].buf_len;
843 buf_avail = buf_len - buf_offset;
845 buf_offset = dev->vhost_hlen;
846 buf_avail = buf_len - dev->vhost_hlen;
849 mbuf_avail = rte_pktmbuf_data_len(m);
851 while (mbuf_avail != 0 || m->next != NULL) {
852 /* done with current buf, get the next one */
853 if (buf_avail == 0) {
855 if (unlikely(vec_idx >= nr_vec)) {
860 buf_addr = buf_vec[vec_idx].buf_addr;
861 buf_iova = buf_vec[vec_idx].buf_iova;
862 buf_len = buf_vec[vec_idx].buf_len;
868 /* done with current mbuf, get the next one */
869 if (mbuf_avail == 0) {
873 mbuf_avail = rte_pktmbuf_data_len(m);
877 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
878 if (rxvq_is_mergeable(dev))
879 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
882 if (unlikely(hdr == &tmp_hdr)) {
883 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
885 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
887 vhost_log_cache_write_iova(dev, vq,
895 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
897 if (likely(cpy_len > MAX_BATCH_LEN ||
898 vq->batch_copy_nb_elems >= vq->size)) {
899 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
900 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
902 vhost_log_cache_write_iova(dev, vq,
903 buf_iova + buf_offset,
905 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
908 batch_copy[vq->batch_copy_nb_elems].dst =
909 (void *)((uintptr_t)(buf_addr + buf_offset));
910 batch_copy[vq->batch_copy_nb_elems].src =
911 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
912 batch_copy[vq->batch_copy_nb_elems].log_addr =
913 buf_iova + buf_offset;
914 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
915 vq->batch_copy_nb_elems++;
918 mbuf_avail -= cpy_len;
919 mbuf_offset += cpy_len;
920 buf_avail -= cpy_len;
921 buf_offset += cpy_len;
929 static __rte_always_inline void
930 async_fill_vec(struct iovec *v, void *base, size_t len)
936 static __rte_always_inline void
937 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
938 struct iovec *vec, unsigned long nr_seg)
945 it->nr_segs = nr_seg;
952 static __rte_always_inline void
953 async_fill_desc(struct rte_vhost_async_desc *desc,
954 struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
960 static __rte_always_inline int
961 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
962 struct rte_mbuf *m, struct buf_vector *buf_vec,
963 uint16_t nr_vec, uint16_t num_buffers,
964 struct iovec *src_iovec, struct iovec *dst_iovec,
965 struct rte_vhost_iov_iter *src_it,
966 struct rte_vhost_iov_iter *dst_it)
968 struct rte_mbuf *hdr_mbuf;
969 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
970 uint64_t buf_addr, buf_iova;
973 uint32_t vec_idx = 0;
974 uint32_t mbuf_offset, mbuf_avail;
975 uint32_t buf_offset, buf_avail;
976 uint32_t cpy_len, buf_len;
983 if (unlikely(m == NULL)) {
988 buf_addr = buf_vec[vec_idx].buf_addr;
989 buf_iova = buf_vec[vec_idx].buf_iova;
990 buf_len = buf_vec[vec_idx].buf_len;
992 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
999 if (unlikely(buf_len < dev->vhost_hlen)) {
1000 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1003 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1005 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1006 dev->vid, num_buffers);
1008 if (unlikely(buf_len < dev->vhost_hlen)) {
1009 buf_offset = dev->vhost_hlen - buf_len;
1011 buf_addr = buf_vec[vec_idx].buf_addr;
1012 buf_iova = buf_vec[vec_idx].buf_iova;
1013 buf_len = buf_vec[vec_idx].buf_len;
1014 buf_avail = buf_len - buf_offset;
1016 buf_offset = dev->vhost_hlen;
1017 buf_avail = buf_len - dev->vhost_hlen;
1020 mbuf_avail = rte_pktmbuf_data_len(m);
1023 while (mbuf_avail != 0 || m->next != NULL) {
1024 /* done with current buf, get the next one */
1025 if (buf_avail == 0) {
1027 if (unlikely(vec_idx >= nr_vec)) {
1032 buf_addr = buf_vec[vec_idx].buf_addr;
1033 buf_iova = buf_vec[vec_idx].buf_iova;
1034 buf_len = buf_vec[vec_idx].buf_len;
1037 buf_avail = buf_len;
1040 /* done with current mbuf, get the next one */
1041 if (mbuf_avail == 0) {
1045 mbuf_avail = rte_pktmbuf_data_len(m);
1049 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1050 if (rxvq_is_mergeable(dev))
1051 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1054 if (unlikely(hdr == &tmp_hdr)) {
1055 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1057 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1058 dev->vhost_hlen, 0);
1059 vhost_log_cache_write_iova(dev, vq,
1060 buf_vec[0].buf_iova,
1067 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1069 while (unlikely(cpy_len)) {
1070 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1071 buf_iova + buf_offset,
1072 cpy_len, &mapped_len);
1073 if (unlikely(!hpa)) {
1074 VHOST_LOG_DATA(ERR, "(%d) %s: failed to get hpa.\n",
1075 dev->vid, __func__);
1080 async_fill_vec(src_iovec + tvec_idx,
1081 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1082 mbuf_offset), (size_t)mapped_len);
1083 async_fill_vec(dst_iovec + tvec_idx,
1084 hpa, (size_t)mapped_len);
1086 tlen += (uint32_t)mapped_len;
1087 cpy_len -= (uint32_t)mapped_len;
1088 mbuf_avail -= (uint32_t)mapped_len;
1089 mbuf_offset += (uint32_t)mapped_len;
1090 buf_avail -= (uint32_t)mapped_len;
1091 buf_offset += (uint32_t)mapped_len;
1096 async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1097 async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1102 static __rte_always_inline int
1103 vhost_enqueue_single_packed(struct virtio_net *dev,
1104 struct vhost_virtqueue *vq,
1105 struct rte_mbuf *pkt,
1106 struct buf_vector *buf_vec,
1109 uint16_t nr_vec = 0;
1110 uint16_t avail_idx = vq->last_avail_idx;
1111 uint16_t max_tries, tries = 0;
1112 uint16_t buf_id = 0;
1114 uint16_t desc_count;
1115 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1116 uint16_t num_buffers = 0;
1117 uint32_t buffer_len[vq->size];
1118 uint16_t buffer_buf_id[vq->size];
1119 uint16_t buffer_desc_count[vq->size];
1121 if (rxvq_is_mergeable(dev))
1122 max_tries = vq->size - 1;
1128 * if we tried all available ring items, and still
1129 * can't get enough buf, it means something abnormal
1132 if (unlikely(++tries > max_tries))
1135 if (unlikely(fill_vec_buf_packed(dev, vq,
1136 avail_idx, &desc_count,
1139 VHOST_ACCESS_RW) < 0))
1142 len = RTE_MIN(len, size);
1145 buffer_len[num_buffers] = len;
1146 buffer_buf_id[num_buffers] = buf_id;
1147 buffer_desc_count[num_buffers] = desc_count;
1150 *nr_descs += desc_count;
1151 avail_idx += desc_count;
1152 if (avail_idx >= vq->size)
1153 avail_idx -= vq->size;
1156 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1159 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1160 buffer_desc_count, num_buffers);
1165 static __rte_noinline uint32_t
1166 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1167 struct rte_mbuf **pkts, uint32_t count)
1169 uint32_t pkt_idx = 0;
1170 uint16_t num_buffers;
1171 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1172 uint16_t avail_head;
1175 * The ordering between avail index and
1176 * desc reads needs to be enforced.
1178 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1180 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1182 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1183 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1184 uint16_t nr_vec = 0;
1186 if (unlikely(reserve_avail_buf_split(dev, vq,
1187 pkt_len, buf_vec, &num_buffers,
1188 avail_head, &nr_vec) < 0)) {
1189 VHOST_LOG_DATA(DEBUG,
1190 "(%d) failed to get enough desc from vring\n",
1192 vq->shadow_used_idx -= num_buffers;
1196 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1197 dev->vid, vq->last_avail_idx,
1198 vq->last_avail_idx + num_buffers);
1200 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1203 vq->shadow_used_idx -= num_buffers;
1207 vq->last_avail_idx += num_buffers;
1210 do_data_copy_enqueue(dev, vq);
1212 if (likely(vq->shadow_used_idx)) {
1213 flush_shadow_used_ring_split(dev, vq);
1214 vhost_vring_call_split(dev, vq);
1220 static __rte_always_inline int
1221 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1222 struct vhost_virtqueue *vq,
1223 struct rte_mbuf **pkts,
1224 uint64_t *desc_addrs,
1227 bool wrap_counter = vq->avail_wrap_counter;
1228 struct vring_packed_desc *descs = vq->desc_packed;
1229 uint16_t avail_idx = vq->last_avail_idx;
1230 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1233 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1236 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1239 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1240 if (unlikely(pkts[i]->next != NULL))
1242 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1247 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1248 lens[i] = descs[avail_idx + i].len;
1250 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1251 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1255 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1256 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1257 descs[avail_idx + i].addr,
1261 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1262 if (unlikely(!desc_addrs[i]))
1264 if (unlikely(lens[i] != descs[avail_idx + i].len))
1271 static __rte_always_inline void
1272 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1273 struct vhost_virtqueue *vq,
1274 struct rte_mbuf **pkts,
1275 uint64_t *desc_addrs,
1278 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1279 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1280 struct vring_packed_desc *descs = vq->desc_packed;
1281 uint16_t avail_idx = vq->last_avail_idx;
1282 uint16_t ids[PACKED_BATCH_SIZE];
1285 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1286 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1287 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1288 (uintptr_t)desc_addrs[i];
1289 lens[i] = pkts[i]->pkt_len +
1290 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1293 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1294 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1296 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1298 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1299 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1300 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1304 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1305 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1308 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1309 ids[i] = descs[avail_idx + i].id;
1311 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1314 static __rte_always_inline int
1315 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1316 struct vhost_virtqueue *vq,
1317 struct rte_mbuf **pkts)
1319 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1320 uint64_t lens[PACKED_BATCH_SIZE];
1322 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1325 if (vq->shadow_used_idx) {
1326 do_data_copy_enqueue(dev, vq);
1327 vhost_flush_enqueue_shadow_packed(dev, vq);
1330 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1335 static __rte_always_inline int16_t
1336 virtio_dev_rx_single_packed(struct virtio_net *dev,
1337 struct vhost_virtqueue *vq,
1338 struct rte_mbuf *pkt)
1340 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1341 uint16_t nr_descs = 0;
1343 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1345 VHOST_LOG_DATA(DEBUG,
1346 "(%d) failed to get enough desc from vring\n",
1351 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1352 dev->vid, vq->last_avail_idx,
1353 vq->last_avail_idx + nr_descs);
1355 vq_inc_last_avail_packed(vq, nr_descs);
1360 static __rte_noinline uint32_t
1361 virtio_dev_rx_packed(struct virtio_net *dev,
1362 struct vhost_virtqueue *__rte_restrict vq,
1363 struct rte_mbuf **__rte_restrict pkts,
1366 uint32_t pkt_idx = 0;
1369 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1371 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1372 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1374 pkt_idx += PACKED_BATCH_SIZE;
1379 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1383 } while (pkt_idx < count);
1385 if (vq->shadow_used_idx) {
1386 do_data_copy_enqueue(dev, vq);
1387 vhost_flush_enqueue_shadow_packed(dev, vq);
1391 vhost_vring_call_packed(dev, vq);
1396 static __rte_always_inline uint32_t
1397 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1398 struct rte_mbuf **pkts, uint32_t count)
1400 struct vhost_virtqueue *vq;
1403 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1404 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1405 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1406 dev->vid, __func__, queue_id);
1410 vq = dev->virtqueue[queue_id];
1412 rte_spinlock_lock(&vq->access_lock);
1414 if (unlikely(!vq->enabled))
1415 goto out_access_unlock;
1417 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1418 vhost_user_iotlb_rd_lock(vq);
1420 if (unlikely(!vq->access_ok))
1421 if (unlikely(vring_translate(dev, vq) < 0))
1424 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1428 if (vq_is_packed(dev))
1429 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1431 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1434 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1435 vhost_user_iotlb_rd_unlock(vq);
1438 rte_spinlock_unlock(&vq->access_lock);
1444 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1445 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1447 struct virtio_net *dev = get_device(vid);
1452 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1454 "(%d) %s: built-in vhost net backend is disabled.\n",
1455 dev->vid, __func__);
1459 return virtio_dev_rx(dev, queue_id, pkts, count);
1462 static __rte_always_inline uint16_t
1463 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1464 uint16_t vq_size, uint16_t n_inflight)
1466 return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1467 (vq_size - n_inflight + pkts_idx) % vq_size;
1470 static __rte_always_inline void
1471 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1472 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1474 size_t elem_size = sizeof(struct vring_used_elem);
1476 if (d_idx + count <= ring_size) {
1477 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1479 uint16_t size = ring_size - d_idx;
1481 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1482 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1486 static __rte_always_inline void
1487 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1488 struct vring_used_elem_packed *d_ring,
1489 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1491 size_t elem_size = sizeof(struct vring_used_elem_packed);
1493 if (d_idx + count <= ring_size) {
1494 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1496 uint16_t size = ring_size - d_idx;
1498 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1499 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1503 static __rte_noinline uint32_t
1504 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1505 struct vhost_virtqueue *vq, uint16_t queue_id,
1506 struct rte_mbuf **pkts, uint32_t count)
1508 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1509 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1510 uint16_t num_buffers;
1511 uint16_t avail_head;
1513 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1514 struct iovec *vec_pool = vq->vec_pool;
1515 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1516 struct iovec *src_iovec = vec_pool;
1517 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1518 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1519 uint32_t n_pkts = 0, pkt_err = 0;
1521 uint16_t segs_await = 0;
1522 uint16_t iovec_idx = 0, it_idx = 0, slot_idx = 0;
1525 * The ordering between avail index and desc reads need to be enforced.
1527 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1529 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1531 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1532 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1533 uint16_t nr_vec = 0;
1535 if (unlikely(reserve_avail_buf_split(dev, vq,
1536 pkt_len, buf_vec, &num_buffers,
1537 avail_head, &nr_vec) < 0)) {
1538 VHOST_LOG_DATA(DEBUG,
1539 "(%d) failed to get enough desc from vring\n",
1541 vq->shadow_used_idx -= num_buffers;
1545 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1546 dev->vid, vq->last_avail_idx,
1547 vq->last_avail_idx + num_buffers);
1549 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers,
1550 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1551 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0) {
1552 vq->shadow_used_idx -= num_buffers;
1556 async_fill_desc(&tdes[pkt_burst_idx++], &it_pool[it_idx],
1557 &it_pool[it_idx + 1]);
1559 slot_idx = (vq->async_pkts_idx + pkt_idx) & (vq->size - 1);
1560 pkts_info[slot_idx].descs = num_buffers;
1561 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1563 iovec_idx += it_pool[it_idx].nr_segs;
1564 segs_await += it_pool[it_idx].nr_segs;
1567 vq->last_avail_idx += num_buffers;
1570 * conditions to trigger async device transfer:
1571 * - buffered packet number reaches transfer threshold
1572 * - unused async iov number is less than max vhost vector
1574 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1575 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await <
1577 n_xfer = vq->async_ops.transfer_data(dev->vid,
1578 queue_id, tdes, 0, pkt_burst_idx);
1579 if (likely(n_xfer >= 0)) {
1583 "(%d) %s: failed to transfer data for queue id %d.\n",
1584 dev->vid, __func__, queue_id);
1592 if (unlikely(n_pkts < pkt_burst_idx)) {
1594 * log error packets number here and do actual
1595 * error processing when applications poll
1598 pkt_err = pkt_burst_idx - n_pkts;
1608 if (pkt_burst_idx) {
1609 n_xfer = vq->async_ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
1610 if (likely(n_xfer >= 0)) {
1613 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1614 dev->vid, __func__, queue_id);
1618 if (unlikely(n_pkts < pkt_burst_idx))
1619 pkt_err = pkt_burst_idx - n_pkts;
1622 if (unlikely(pkt_err)) {
1623 uint16_t num_descs = 0;
1625 /* update number of completed packets */
1628 /* calculate the sum of descriptors to revert */
1629 while (pkt_err-- > 0) {
1630 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1634 /* recover shadow used ring and available ring */
1635 vq->shadow_used_idx -= num_descs;
1636 vq->last_avail_idx -= num_descs;
1639 /* keep used descriptors */
1640 if (likely(vq->shadow_used_idx)) {
1641 uint16_t to = vq->async_desc_idx_split & (vq->size - 1);
1643 store_dma_desc_info_split(vq->shadow_used_split,
1644 vq->async_descs_split, vq->size, 0, to,
1645 vq->shadow_used_idx);
1647 vq->async_desc_idx_split += vq->shadow_used_idx;
1648 vq->async_pkts_idx += pkt_idx;
1649 vq->async_pkts_inflight_n += pkt_idx;
1650 vq->shadow_used_idx = 0;
1656 static __rte_always_inline void
1657 vhost_update_used_packed(struct vhost_virtqueue *vq,
1658 struct vring_used_elem_packed *shadow_ring,
1662 uint16_t used_idx = vq->last_used_idx;
1663 uint16_t head_idx = vq->last_used_idx;
1664 uint16_t head_flags = 0;
1669 /* Split loop in two to save memory barriers */
1670 for (i = 0; i < count; i++) {
1671 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1672 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1674 used_idx += shadow_ring[i].count;
1675 if (used_idx >= vq->size)
1676 used_idx -= vq->size;
1679 /* The ordering for storing desc flags needs to be enforced. */
1680 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1682 for (i = 0; i < count; i++) {
1685 if (vq->shadow_used_packed[i].len)
1686 flags = VRING_DESC_F_WRITE;
1690 if (vq->used_wrap_counter) {
1691 flags |= VRING_DESC_F_USED;
1692 flags |= VRING_DESC_F_AVAIL;
1694 flags &= ~VRING_DESC_F_USED;
1695 flags &= ~VRING_DESC_F_AVAIL;
1699 vq->desc_packed[vq->last_used_idx].flags = flags;
1701 head_idx = vq->last_used_idx;
1705 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1708 vq->desc_packed[head_idx].flags = head_flags;
1711 static __rte_always_inline int
1712 vhost_enqueue_async_packed(struct virtio_net *dev,
1713 struct vhost_virtqueue *vq,
1714 struct rte_mbuf *pkt,
1715 struct buf_vector *buf_vec,
1717 uint16_t *nr_buffers,
1718 struct iovec *src_iovec, struct iovec *dst_iovec,
1719 struct rte_vhost_iov_iter *src_it,
1720 struct rte_vhost_iov_iter *dst_it)
1722 uint16_t nr_vec = 0;
1723 uint16_t avail_idx = vq->last_avail_idx;
1724 uint16_t max_tries, tries = 0;
1725 uint16_t buf_id = 0;
1727 uint16_t desc_count = 0;
1728 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1729 uint32_t buffer_len[vq->size];
1730 uint16_t buffer_buf_id[vq->size];
1731 uint16_t buffer_desc_count[vq->size];
1733 if (rxvq_is_mergeable(dev))
1734 max_tries = vq->size - 1;
1740 * if we tried all available ring items, and still
1741 * can't get enough buf, it means something abnormal
1744 if (unlikely(++tries > max_tries))
1747 if (unlikely(fill_vec_buf_packed(dev, vq, avail_idx, &desc_count, buf_vec, &nr_vec,
1748 &buf_id, &len, VHOST_ACCESS_RW) < 0))
1751 len = RTE_MIN(len, size);
1754 buffer_len[*nr_buffers] = len;
1755 buffer_buf_id[*nr_buffers] = buf_id;
1756 buffer_desc_count[*nr_buffers] = desc_count;
1758 *nr_descs += desc_count;
1759 avail_idx += desc_count;
1760 if (avail_idx >= vq->size)
1761 avail_idx -= vq->size;
1764 if (unlikely(async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec,
1765 *nr_buffers, src_iovec, dst_iovec,
1766 src_it, dst_it) < 0))
1769 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1774 static __rte_always_inline int16_t
1775 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1776 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers,
1777 struct iovec *src_iovec, struct iovec *dst_iovec,
1778 struct rte_vhost_iov_iter *src_it, struct rte_vhost_iov_iter *dst_it)
1780 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1782 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec, nr_descs, nr_buffers,
1783 src_iovec, dst_iovec,
1784 src_it, dst_it) < 0)) {
1785 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1789 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1790 dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1795 static __rte_always_inline void
1796 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1797 uint32_t nr_err, uint32_t *pkt_idx)
1799 uint16_t descs_err = 0;
1800 uint16_t buffers_err = 0;
1801 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1804 /* calculate the sum of buffers and descs of DMA-error packets. */
1805 while (nr_err-- > 0) {
1806 descs_err += pkts_info[slot_idx % vq->size].descs;
1807 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1811 if (vq->last_avail_idx >= descs_err) {
1812 vq->last_avail_idx -= descs_err;
1814 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1815 vq->avail_wrap_counter ^= 1;
1818 vq->shadow_used_idx -= buffers_err;
1821 static __rte_noinline uint32_t
1822 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1823 struct vhost_virtqueue *vq, uint16_t queue_id,
1824 struct rte_mbuf **pkts, uint32_t count)
1826 uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1827 uint32_t remained = count;
1829 uint16_t num_buffers;
1832 struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1833 struct iovec *vec_pool = vq->vec_pool;
1834 struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1835 struct iovec *src_iovec = vec_pool;
1836 struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1837 struct async_inflight_info *pkts_info = vq->async_pkts_info;
1838 uint32_t n_pkts = 0, pkt_err = 0;
1839 uint16_t slot_idx = 0;
1840 uint16_t segs_await = 0;
1841 uint16_t iovec_idx = 0, it_idx = 0;
1844 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1848 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1849 &num_descs, &num_buffers,
1850 &src_iovec[iovec_idx], &dst_iovec[iovec_idx],
1851 &it_pool[it_idx], &it_pool[it_idx + 1]) < 0))
1854 slot_idx = (vq->async_pkts_idx + pkt_idx) % vq->size;
1856 async_fill_desc(&tdes[pkt_burst_idx++], &it_pool[it_idx],
1857 &it_pool[it_idx + 1]);
1858 pkts_info[slot_idx].descs = num_descs;
1859 pkts_info[slot_idx].nr_buffers = num_buffers;
1860 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1861 iovec_idx += it_pool[it_idx].nr_segs;
1862 segs_await += it_pool[it_idx].nr_segs;
1867 vq_inc_last_avail_packed(vq, num_descs);
1870 * conditions to trigger async device transfer:
1871 * - buffered packet number reaches transfer threshold
1872 * - unused async iov number is less than max vhost vector
1874 if (unlikely(pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1875 ((VHOST_MAX_ASYNC_VEC >> 1) - segs_await < BUF_VECTOR_MAX))) {
1876 n_xfer = vq->async_ops.transfer_data(dev->vid,
1877 queue_id, tdes, 0, pkt_burst_idx);
1878 if (likely(n_xfer >= 0)) {
1882 "(%d) %s: failed to transfer data for queue id %d.\n",
1883 dev->vid, __func__, queue_id);
1891 if (unlikely(n_pkts < pkt_burst_idx)) {
1893 * log error packets number here and do actual
1894 * error processing when applications poll
1897 pkt_err = pkt_burst_idx - n_pkts;
1904 } while (pkt_idx < count);
1906 if (pkt_burst_idx) {
1907 n_xfer = vq->async_ops.transfer_data(dev->vid, queue_id, tdes, 0, pkt_burst_idx);
1908 if (likely(n_xfer >= 0)) {
1911 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1912 dev->vid, __func__, queue_id);
1916 if (unlikely(n_pkts < pkt_burst_idx))
1917 pkt_err = pkt_burst_idx - n_pkts;
1920 if (unlikely(pkt_err))
1921 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1923 if (likely(vq->shadow_used_idx)) {
1924 /* keep used descriptors. */
1925 store_dma_desc_info_packed(vq->shadow_used_packed, vq->async_buffers_packed,
1926 vq->size, 0, vq->async_buffer_idx_packed,
1927 vq->shadow_used_idx);
1929 vq->async_buffer_idx_packed += vq->shadow_used_idx;
1930 if (vq->async_buffer_idx_packed >= vq->size)
1931 vq->async_buffer_idx_packed -= vq->size;
1933 vq->async_pkts_idx += pkt_idx;
1934 if (vq->async_pkts_idx >= vq->size)
1935 vq->async_pkts_idx -= vq->size;
1937 vq->shadow_used_idx = 0;
1938 vq->async_pkts_inflight_n += pkt_idx;
1944 static __rte_always_inline void
1945 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1947 uint16_t nr_left = n_descs;
1952 from = vq->last_async_desc_idx_split & (vq->size - 1);
1953 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1954 to = vq->last_used_idx & (vq->size - 1);
1956 if (to + nr_copy <= vq->size) {
1957 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
1958 nr_copy * sizeof(struct vring_used_elem));
1960 uint16_t size = vq->size - to;
1962 rte_memcpy(&vq->used->ring[to], &vq->async_descs_split[from],
1963 size * sizeof(struct vring_used_elem));
1964 rte_memcpy(&vq->used->ring[0], &vq->async_descs_split[from + size],
1965 (nr_copy - size) * sizeof(struct vring_used_elem));
1968 vq->last_async_desc_idx_split += nr_copy;
1969 vq->last_used_idx += nr_copy;
1971 } while (nr_left > 0);
1974 static __rte_always_inline void
1975 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1978 uint16_t nr_left = n_buffers;
1982 from = vq->last_async_buffer_idx_packed;
1983 to = (from + nr_left) % vq->size;
1985 vhost_update_used_packed(vq, vq->async_buffers_packed + from, to - from);
1986 vq->last_async_buffer_idx_packed += nr_left;
1989 vhost_update_used_packed(vq, vq->async_buffers_packed + from,
1991 vq->last_async_buffer_idx_packed = 0;
1992 nr_left -= vq->size - from;
1994 } while (nr_left > 0);
1997 static __rte_always_inline uint16_t
1998 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1999 struct rte_mbuf **pkts, uint16_t count)
2001 struct vhost_virtqueue *vq;
2002 struct async_inflight_info *pkts_info;
2004 uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0, n_buffers = 0;
2005 uint16_t start_idx, pkts_idx, vq_size;
2008 vq = dev->virtqueue[queue_id];
2009 pkts_idx = vq->async_pkts_idx % vq->size;
2010 pkts_info = vq->async_pkts_info;
2012 start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
2013 vq_size, vq->async_pkts_inflight_n);
2015 if (count > vq->async_last_pkts_n) {
2016 n_cpl = vq->async_ops.check_completed_copies(dev->vid,
2017 queue_id, 0, count - vq->async_last_pkts_n);
2018 if (likely(n_cpl >= 0)) {
2022 "(%d) %s: failed to check completed copies for queue id %d.\n",
2023 dev->vid, __func__, queue_id);
2028 n_pkts_cpl += vq->async_last_pkts_n;
2029 n_pkts_put = RTE_MIN(n_pkts_cpl, count);
2030 if (unlikely(n_pkts_put == 0)) {
2031 vq->async_last_pkts_n = n_pkts_cpl;
2035 if (vq_is_packed(dev)) {
2036 for (i = 0; i < n_pkts_put; i++) {
2037 from = (start_idx + i) % vq_size;
2038 n_buffers += pkts_info[from].nr_buffers;
2039 pkts[i] = pkts_info[from].mbuf;
2042 for (i = 0; i < n_pkts_put; i++) {
2043 from = (start_idx + i) & (vq_size - 1);
2044 n_descs += pkts_info[from].descs;
2045 pkts[i] = pkts_info[from].mbuf;
2048 vq->async_last_pkts_n = n_pkts_cpl - n_pkts_put;
2049 vq->async_pkts_inflight_n -= n_pkts_put;
2051 if (likely(vq->enabled && vq->access_ok)) {
2052 if (vq_is_packed(dev)) {
2053 write_back_completed_descs_packed(vq, n_buffers);
2055 vhost_vring_call_packed(dev, vq);
2057 write_back_completed_descs_split(vq, n_descs);
2059 __atomic_add_fetch(&vq->used->idx, n_descs,
2061 vhost_vring_call_split(dev, vq);
2064 if (vq_is_packed(dev)) {
2065 vq->last_async_buffer_idx_packed += n_buffers;
2066 if (vq->last_async_buffer_idx_packed >= vq->size)
2067 vq->last_async_buffer_idx_packed -= vq->size;
2069 vq->last_async_desc_idx_split += n_descs;
2077 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2078 struct rte_mbuf **pkts, uint16_t count)
2080 struct virtio_net *dev = get_device(vid);
2081 struct vhost_virtqueue *vq;
2082 uint16_t n_pkts_cpl = 0;
2087 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2088 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2089 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2090 dev->vid, __func__, queue_id);
2094 vq = dev->virtqueue[queue_id];
2096 if (unlikely(!vq->async_registered)) {
2097 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2098 dev->vid, __func__, queue_id);
2102 rte_spinlock_lock(&vq->access_lock);
2104 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2106 rte_spinlock_unlock(&vq->access_lock);
2112 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2113 struct rte_mbuf **pkts, uint16_t count)
2115 struct virtio_net *dev = get_device(vid);
2116 struct vhost_virtqueue *vq;
2117 uint16_t n_pkts_cpl = 0;
2122 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2123 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2124 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2125 dev->vid, __func__, queue_id);
2129 vq = dev->virtqueue[queue_id];
2131 if (unlikely(!vq->async_registered)) {
2132 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2133 dev->vid, __func__, queue_id);
2137 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2142 static __rte_always_inline uint32_t
2143 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2144 struct rte_mbuf **pkts, uint32_t count)
2146 struct vhost_virtqueue *vq;
2149 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2150 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2151 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2152 dev->vid, __func__, queue_id);
2156 vq = dev->virtqueue[queue_id];
2158 rte_spinlock_lock(&vq->access_lock);
2160 if (unlikely(!vq->enabled || !vq->async_registered))
2161 goto out_access_unlock;
2163 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2164 vhost_user_iotlb_rd_lock(vq);
2166 if (unlikely(!vq->access_ok))
2167 if (unlikely(vring_translate(dev, vq) < 0))
2170 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2174 if (vq_is_packed(dev))
2175 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2178 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2182 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2183 vhost_user_iotlb_rd_unlock(vq);
2186 rte_spinlock_unlock(&vq->access_lock);
2192 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2193 struct rte_mbuf **pkts, uint16_t count)
2195 struct virtio_net *dev = get_device(vid);
2200 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2202 "(%d) %s: built-in vhost net backend is disabled.\n",
2203 dev->vid, __func__);
2207 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2211 virtio_net_with_host_offload(struct virtio_net *dev)
2214 ((1ULL << VIRTIO_NET_F_CSUM) |
2215 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2216 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2217 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2218 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2225 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2227 struct rte_ipv4_hdr *ipv4_hdr;
2228 struct rte_ipv6_hdr *ipv6_hdr;
2229 struct rte_ether_hdr *eth_hdr;
2231 uint16_t data_len = rte_pktmbuf_data_len(m);
2233 if (data_len < sizeof(struct rte_ether_hdr))
2236 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2238 m->l2_len = sizeof(struct rte_ether_hdr);
2239 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2241 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2242 if (data_len < sizeof(struct rte_ether_hdr) +
2243 sizeof(struct rte_vlan_hdr))
2246 struct rte_vlan_hdr *vlan_hdr =
2247 (struct rte_vlan_hdr *)(eth_hdr + 1);
2249 m->l2_len += sizeof(struct rte_vlan_hdr);
2250 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2253 switch (ethertype) {
2254 case RTE_ETHER_TYPE_IPV4:
2255 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2257 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2259 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2260 if (data_len < m->l2_len + m->l3_len)
2262 m->ol_flags |= PKT_TX_IPV4;
2263 *l4_proto = ipv4_hdr->next_proto_id;
2265 case RTE_ETHER_TYPE_IPV6:
2266 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2268 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2270 m->l3_len = sizeof(struct rte_ipv6_hdr);
2271 m->ol_flags |= PKT_TX_IPV6;
2272 *l4_proto = ipv6_hdr->proto;
2275 /* a valid L3 header is needed for further L4 parsing */
2279 /* both CSUM and GSO need a valid L4 header */
2280 switch (*l4_proto) {
2282 if (data_len < m->l2_len + m->l3_len +
2283 sizeof(struct rte_tcp_hdr))
2287 if (data_len < m->l2_len + m->l3_len +
2288 sizeof(struct rte_udp_hdr))
2292 if (data_len < m->l2_len + m->l3_len +
2293 sizeof(struct rte_sctp_hdr))
2309 static __rte_always_inline void
2310 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2312 uint8_t l4_proto = 0;
2313 struct rte_tcp_hdr *tcp_hdr = NULL;
2315 uint16_t data_len = rte_pktmbuf_data_len(m);
2317 if (parse_headers(m, &l4_proto) < 0)
2320 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2321 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2322 switch (hdr->csum_offset) {
2323 case (offsetof(struct rte_tcp_hdr, cksum)):
2324 if (l4_proto != IPPROTO_TCP)
2326 m->ol_flags |= PKT_TX_TCP_CKSUM;
2328 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2329 if (l4_proto != IPPROTO_UDP)
2331 m->ol_flags |= PKT_TX_UDP_CKSUM;
2333 case (offsetof(struct rte_sctp_hdr, cksum)):
2334 if (l4_proto != IPPROTO_SCTP)
2336 m->ol_flags |= PKT_TX_SCTP_CKSUM;
2346 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2347 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2348 case VIRTIO_NET_HDR_GSO_TCPV4:
2349 case VIRTIO_NET_HDR_GSO_TCPV6:
2350 if (l4_proto != IPPROTO_TCP)
2352 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2353 struct rte_tcp_hdr *,
2354 m->l2_len + m->l3_len);
2355 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2356 if (data_len < m->l2_len + m->l3_len + tcp_len)
2358 m->ol_flags |= PKT_TX_TCP_SEG;
2359 m->tso_segsz = hdr->gso_size;
2360 m->l4_len = tcp_len;
2362 case VIRTIO_NET_HDR_GSO_UDP:
2363 if (l4_proto != IPPROTO_UDP)
2365 m->ol_flags |= PKT_TX_UDP_SEG;
2366 m->tso_segsz = hdr->gso_size;
2367 m->l4_len = sizeof(struct rte_udp_hdr);
2370 VHOST_LOG_DATA(WARNING,
2371 "unsupported gso type %u.\n", hdr->gso_type);
2383 static __rte_always_inline void
2384 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2385 bool legacy_ol_flags)
2387 struct rte_net_hdr_lens hdr_lens;
2388 int l4_supported = 0;
2391 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2394 if (legacy_ol_flags) {
2395 vhost_dequeue_offload_legacy(hdr, m);
2399 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
2401 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2402 m->packet_type = ptype;
2403 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2404 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2405 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2408 /* According to Virtio 1.1 spec, the device only needs to look at
2409 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2410 * This differs from the processing incoming packets path where the
2411 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2414 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2415 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2416 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2418 * 5.1.6.2.2 Device Requirements: Packet Transmission
2419 * The device MUST ignore flag bits that it does not recognize.
2421 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2424 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2425 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2426 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
2428 /* Unknown proto or tunnel, do sw cksum. We can assume
2429 * the cksum field is in the first segment since the
2430 * buffers we provided to the host are large enough.
2431 * In case of SCTP, this will be wrong since it's a CRC
2432 * but there's nothing we can do.
2434 uint16_t csum = 0, off;
2436 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2437 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2439 if (likely(csum != 0xffff))
2441 off = hdr->csum_offset + hdr->csum_start;
2442 if (rte_pktmbuf_data_len(m) >= off + 1)
2443 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2447 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2448 if (hdr->gso_size == 0)
2451 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2452 case VIRTIO_NET_HDR_GSO_TCPV4:
2453 case VIRTIO_NET_HDR_GSO_TCPV6:
2454 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2456 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2457 m->tso_segsz = hdr->gso_size;
2459 case VIRTIO_NET_HDR_GSO_UDP:
2460 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2462 m->ol_flags |= PKT_RX_LRO | PKT_RX_L4_CKSUM_NONE;
2463 m->tso_segsz = hdr->gso_size;
2471 static __rte_noinline void
2472 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2473 struct buf_vector *buf_vec)
2476 uint64_t remain = sizeof(struct virtio_net_hdr);
2478 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2481 len = RTE_MIN(remain, buf_vec->buf_len);
2482 src = buf_vec->buf_addr;
2483 rte_memcpy((void *)(uintptr_t)dst,
2484 (void *)(uintptr_t)src, len);
2492 static __rte_always_inline int
2493 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2494 struct buf_vector *buf_vec, uint16_t nr_vec,
2495 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2496 bool legacy_ol_flags)
2498 uint32_t buf_avail, buf_offset;
2499 uint64_t buf_addr, buf_len;
2500 uint32_t mbuf_avail, mbuf_offset;
2502 struct rte_mbuf *cur = m, *prev = m;
2503 struct virtio_net_hdr tmp_hdr;
2504 struct virtio_net_hdr *hdr = NULL;
2505 /* A counter to avoid desc dead loop chain */
2506 uint16_t vec_idx = 0;
2507 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2510 buf_addr = buf_vec[vec_idx].buf_addr;
2511 buf_len = buf_vec[vec_idx].buf_len;
2513 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2518 if (virtio_net_with_host_offload(dev)) {
2519 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2521 * No luck, the virtio-net header doesn't fit
2522 * in a contiguous virtual area.
2524 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2527 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2532 * A virtio driver normally uses at least 2 desc buffers
2533 * for Tx: the first for storing the header, and others
2534 * for storing the data.
2536 if (unlikely(buf_len < dev->vhost_hlen)) {
2537 buf_offset = dev->vhost_hlen - buf_len;
2539 buf_addr = buf_vec[vec_idx].buf_addr;
2540 buf_len = buf_vec[vec_idx].buf_len;
2541 buf_avail = buf_len - buf_offset;
2542 } else if (buf_len == dev->vhost_hlen) {
2543 if (unlikely(++vec_idx >= nr_vec))
2545 buf_addr = buf_vec[vec_idx].buf_addr;
2546 buf_len = buf_vec[vec_idx].buf_len;
2549 buf_avail = buf_len;
2551 buf_offset = dev->vhost_hlen;
2552 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2556 (uintptr_t)(buf_addr + buf_offset),
2557 (uint32_t)buf_avail, 0);
2560 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2562 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2564 if (likely(cpy_len > MAX_BATCH_LEN ||
2565 vq->batch_copy_nb_elems >= vq->size ||
2566 (hdr && cur == m))) {
2567 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2569 (void *)((uintptr_t)(buf_addr +
2570 buf_offset)), cpy_len);
2572 batch_copy[vq->batch_copy_nb_elems].dst =
2573 rte_pktmbuf_mtod_offset(cur, void *,
2575 batch_copy[vq->batch_copy_nb_elems].src =
2576 (void *)((uintptr_t)(buf_addr + buf_offset));
2577 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2578 vq->batch_copy_nb_elems++;
2581 mbuf_avail -= cpy_len;
2582 mbuf_offset += cpy_len;
2583 buf_avail -= cpy_len;
2584 buf_offset += cpy_len;
2586 /* This buf reaches to its end, get the next one */
2587 if (buf_avail == 0) {
2588 if (++vec_idx >= nr_vec)
2591 buf_addr = buf_vec[vec_idx].buf_addr;
2592 buf_len = buf_vec[vec_idx].buf_len;
2595 buf_avail = buf_len;
2597 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2598 (uint32_t)buf_avail, 0);
2602 * This mbuf reaches to its end, get a new one
2603 * to hold more data.
2605 if (mbuf_avail == 0) {
2606 cur = rte_pktmbuf_alloc(mbuf_pool);
2607 if (unlikely(cur == NULL)) {
2608 VHOST_LOG_DATA(ERR, "Failed to "
2609 "allocate memory for mbuf.\n");
2615 prev->data_len = mbuf_offset;
2617 m->pkt_len += mbuf_offset;
2621 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2625 prev->data_len = mbuf_offset;
2626 m->pkt_len += mbuf_offset;
2629 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2637 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2643 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2645 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2646 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2651 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2652 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2654 if (unlikely(total_len > UINT16_MAX))
2657 buf_len = total_len;
2658 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2659 if (unlikely(buf == NULL))
2662 /* Initialize shinfo */
2663 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2664 virtio_dev_extbuf_free, buf);
2665 if (unlikely(shinfo == NULL)) {
2667 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2671 iova = rte_malloc_virt2iova(buf);
2672 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2673 rte_pktmbuf_reset_headroom(pkt);
2679 * Prepare a host supported pktmbuf.
2681 static __rte_always_inline int
2682 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2685 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2688 /* attach an external buffer if supported */
2689 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2692 /* check if chained buffers are allowed */
2693 if (!dev->linearbuf)
2701 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2702 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2703 bool legacy_ol_flags)
2706 uint16_t free_entries;
2707 uint16_t dropped = 0;
2708 static bool allocerr_warned;
2711 * The ordering between avail index and
2712 * desc reads needs to be enforced.
2714 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2716 if (free_entries == 0)
2719 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2721 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2723 count = RTE_MIN(count, MAX_PKT_BURST);
2724 count = RTE_MIN(count, free_entries);
2725 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2728 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2731 for (i = 0; i < count; i++) {
2732 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2735 uint16_t nr_vec = 0;
2738 if (unlikely(fill_vec_buf_split(dev, vq,
2739 vq->last_avail_idx + i,
2741 &head_idx, &buf_len,
2742 VHOST_ACCESS_RO) < 0))
2745 update_shadow_used_ring_split(vq, head_idx, 0);
2747 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2748 if (unlikely(err)) {
2750 * mbuf allocation fails for jumbo packets when external
2751 * buffer allocation is not allowed and linear buffer
2752 * is required. Drop this packet.
2754 if (!allocerr_warned) {
2756 "Failed mbuf alloc of size %d from %s on %s.\n",
2757 buf_len, mbuf_pool->name, dev->ifname);
2758 allocerr_warned = true;
2765 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2766 mbuf_pool, legacy_ol_flags);
2767 if (unlikely(err)) {
2768 if (!allocerr_warned) {
2770 "Failed to copy desc to mbuf on %s.\n",
2772 allocerr_warned = true;
2781 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2783 vq->last_avail_idx += i;
2785 do_data_copy_dequeue(vq);
2786 if (unlikely(i < count))
2787 vq->shadow_used_idx = i;
2788 if (likely(vq->shadow_used_idx)) {
2789 flush_shadow_used_ring_split(dev, vq);
2790 vhost_vring_call_split(dev, vq);
2793 return (i - dropped);
2798 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2799 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2800 struct rte_mbuf **pkts, uint16_t count)
2802 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2807 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2808 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2809 struct rte_mbuf **pkts, uint16_t count)
2811 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2814 static __rte_always_inline int
2815 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2816 struct vhost_virtqueue *vq,
2817 struct rte_mbuf **pkts,
2819 uintptr_t *desc_addrs,
2822 bool wrap = vq->avail_wrap_counter;
2823 struct vring_packed_desc *descs = vq->desc_packed;
2824 uint64_t lens[PACKED_BATCH_SIZE];
2825 uint64_t buf_lens[PACKED_BATCH_SIZE];
2826 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2829 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2831 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2834 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2835 flags = descs[avail_idx + i].flags;
2836 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2837 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2838 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2842 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2844 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2845 lens[i] = descs[avail_idx + i].len;
2847 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2848 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2849 descs[avail_idx + i].addr,
2850 &lens[i], VHOST_ACCESS_RW);
2853 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2854 if (unlikely(!desc_addrs[i]))
2856 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2860 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2861 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2865 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2866 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2868 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2869 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2873 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2874 pkts[i]->pkt_len = lens[i] - buf_offset;
2875 pkts[i]->data_len = pkts[i]->pkt_len;
2876 ids[i] = descs[avail_idx + i].id;
2885 static __rte_always_inline int
2886 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2887 struct vhost_virtqueue *vq,
2888 struct rte_mbuf **pkts,
2889 bool legacy_ol_flags)
2891 uint16_t avail_idx = vq->last_avail_idx;
2892 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2893 struct virtio_net_hdr *hdr;
2894 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2895 uint16_t ids[PACKED_BATCH_SIZE];
2898 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2902 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2903 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2905 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2906 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2907 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2910 if (virtio_net_with_host_offload(dev)) {
2911 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2912 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2913 vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2917 if (virtio_net_is_inorder(dev))
2918 vhost_shadow_dequeue_batch_packed_inorder(vq,
2919 ids[PACKED_BATCH_SIZE - 1]);
2921 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2923 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2928 static __rte_always_inline int
2929 vhost_dequeue_single_packed(struct virtio_net *dev,
2930 struct vhost_virtqueue *vq,
2931 struct rte_mempool *mbuf_pool,
2932 struct rte_mbuf *pkts,
2934 uint16_t *desc_count,
2935 bool legacy_ol_flags)
2937 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2939 uint16_t nr_vec = 0;
2941 static bool allocerr_warned;
2943 if (unlikely(fill_vec_buf_packed(dev, vq,
2944 vq->last_avail_idx, desc_count,
2947 VHOST_ACCESS_RO) < 0))
2950 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2951 if (!allocerr_warned) {
2953 "Failed mbuf alloc of size %d from %s on %s.\n",
2954 buf_len, mbuf_pool->name, dev->ifname);
2955 allocerr_warned = true;
2960 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2961 mbuf_pool, legacy_ol_flags);
2962 if (unlikely(err)) {
2963 if (!allocerr_warned) {
2965 "Failed to copy desc to mbuf on %s.\n",
2967 allocerr_warned = true;
2975 static __rte_always_inline int
2976 virtio_dev_tx_single_packed(struct virtio_net *dev,
2977 struct vhost_virtqueue *vq,
2978 struct rte_mempool *mbuf_pool,
2979 struct rte_mbuf *pkts,
2980 bool legacy_ol_flags)
2983 uint16_t buf_id, desc_count = 0;
2986 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2987 &desc_count, legacy_ol_flags);
2989 if (likely(desc_count > 0)) {
2990 if (virtio_net_is_inorder(dev))
2991 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2994 vhost_shadow_dequeue_single_packed(vq, buf_id,
2997 vq_inc_last_avail_packed(vq, desc_count);
3005 virtio_dev_tx_packed(struct virtio_net *dev,
3006 struct vhost_virtqueue *__rte_restrict vq,
3007 struct rte_mempool *mbuf_pool,
3008 struct rte_mbuf **__rte_restrict pkts,
3010 bool legacy_ol_flags)
3012 uint32_t pkt_idx = 0;
3014 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
3018 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3020 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3021 if (!virtio_dev_tx_batch_packed(dev, vq,
3024 pkt_idx += PACKED_BATCH_SIZE;
3029 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3034 } while (pkt_idx < count);
3036 if (pkt_idx != count)
3037 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3039 if (vq->shadow_used_idx) {
3040 do_data_copy_dequeue(vq);
3042 vhost_flush_dequeue_shadow_packed(dev, vq);
3043 vhost_vring_call_packed(dev, vq);
3051 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3052 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3053 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3055 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3060 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3061 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3062 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3064 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3068 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3069 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3071 struct virtio_net *dev;
3072 struct rte_mbuf *rarp_mbuf = NULL;
3073 struct vhost_virtqueue *vq;
3074 int16_t success = 1;
3076 dev = get_device(vid);
3080 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3082 "(%d) %s: built-in vhost net backend is disabled.\n",
3083 dev->vid, __func__);
3087 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3089 "(%d) %s: invalid virtqueue idx %d.\n",
3090 dev->vid, __func__, queue_id);
3094 vq = dev->virtqueue[queue_id];
3096 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3099 if (unlikely(!vq->enabled)) {
3101 goto out_access_unlock;
3104 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3105 vhost_user_iotlb_rd_lock(vq);
3107 if (unlikely(!vq->access_ok))
3108 if (unlikely(vring_translate(dev, vq) < 0)) {
3114 * Construct a RARP broadcast packet, and inject it to the "pkts"
3115 * array, to looks like that guest actually send such packet.
3117 * Check user_send_rarp() for more information.
3119 * broadcast_rarp shares a cacheline in the virtio_net structure
3120 * with some fields that are accessed during enqueue and
3121 * __atomic_compare_exchange_n causes a write if performed compare
3122 * and exchange. This could result in false sharing between enqueue
3125 * Prevent unnecessary false sharing by reading broadcast_rarp first
3126 * and only performing compare and exchange if the read indicates it
3127 * is likely to be set.
3129 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3130 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3131 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3133 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3134 if (rarp_mbuf == NULL) {
3135 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3140 * Inject it to the head of "pkts" array, so that switch's mac
3141 * learning table will get updated first.
3143 pkts[0] = rarp_mbuf;
3148 if (vq_is_packed(dev)) {
3149 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3150 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3152 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3154 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3155 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3157 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3161 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3162 vhost_user_iotlb_rd_unlock(vq);
3165 rte_spinlock_unlock(&vq->access_lock);
3167 if (unlikely(rarp_mbuf != NULL))