1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
24 #define MAX_PKT_BURST 32
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
37 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
40 static __rte_always_inline void
41 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42 struct vhost_virtqueue *vq,
43 uint16_t to, uint16_t from, uint16_t size)
45 rte_memcpy(&vq->used->ring[to],
46 &vq->shadow_used_split[from],
47 size * sizeof(struct vring_used_elem));
48 vhost_log_cache_used_vring(dev, vq,
49 offsetof(struct vring_used, ring[to]),
50 size * sizeof(struct vring_used_elem));
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
56 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
58 if (used_idx + vq->shadow_used_idx <= vq->size) {
59 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
64 /* update used ring interval [used_idx, vq->size] */
65 size = vq->size - used_idx;
66 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
68 /* update the left half used ring interval [0, left_size] */
69 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70 vq->shadow_used_idx - size);
72 vq->last_used_idx += vq->shadow_used_idx;
76 vhost_log_cache_sync(dev, vq);
78 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79 vq->shadow_used_idx = 0;
80 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81 sizeof(vq->used->idx));
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86 uint16_t desc_idx, uint32_t len)
88 uint16_t i = vq->shadow_used_idx++;
90 vq->shadow_used_split[i].id = desc_idx;
91 vq->shadow_used_split[i].len = len;
94 static __rte_always_inline void
95 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
96 struct vhost_virtqueue *vq)
99 uint16_t used_idx = vq->last_used_idx;
100 uint16_t head_idx = vq->last_used_idx;
101 uint16_t head_flags = 0;
103 /* Split loop in two to save memory barriers */
104 for (i = 0; i < vq->shadow_used_idx; i++) {
105 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
108 used_idx += vq->shadow_used_packed[i].count;
109 if (used_idx >= vq->size)
110 used_idx -= vq->size;
115 for (i = 0; i < vq->shadow_used_idx; i++) {
118 if (vq->shadow_used_packed[i].len)
119 flags = VRING_DESC_F_WRITE;
123 if (vq->used_wrap_counter) {
124 flags |= VRING_DESC_F_USED;
125 flags |= VRING_DESC_F_AVAIL;
127 flags &= ~VRING_DESC_F_USED;
128 flags &= ~VRING_DESC_F_AVAIL;
132 vq->desc_packed[vq->last_used_idx].flags = flags;
134 vhost_log_cache_used_vring(dev, vq,
136 sizeof(struct vring_packed_desc),
137 sizeof(struct vring_packed_desc));
139 head_idx = vq->last_used_idx;
143 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
146 vq->desc_packed[head_idx].flags = head_flags;
148 vhost_log_cache_used_vring(dev, vq,
150 sizeof(struct vring_packed_desc),
151 sizeof(struct vring_packed_desc));
153 vq->shadow_used_idx = 0;
154 vhost_log_cache_sync(dev, vq);
157 static __rte_always_inline void
158 flush_shadow_used_ring_packed(struct virtio_net *dev,
159 struct vhost_virtqueue *vq)
162 uint16_t used_idx = vq->last_used_idx;
163 uint16_t head_idx = vq->last_used_idx;
164 uint16_t head_flags = 0;
166 /* Split loop in two to save memory barriers */
167 for (i = 0; i < vq->shadow_used_idx; i++) {
168 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
169 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
171 used_idx += vq->shadow_used_packed[i].count;
172 if (used_idx >= vq->size)
173 used_idx -= vq->size;
176 for (i = 0; i < vq->shadow_used_idx; i++) {
179 if (vq->shadow_used_packed[i].len)
180 flags = VRING_DESC_F_WRITE;
184 if (vq->used_wrap_counter) {
185 flags |= VRING_DESC_F_USED;
186 flags |= VRING_DESC_F_AVAIL;
188 flags &= ~VRING_DESC_F_USED;
189 flags &= ~VRING_DESC_F_AVAIL;
193 vq->desc_packed[vq->last_used_idx].flags = flags;
195 vhost_log_cache_used_vring(dev, vq,
197 sizeof(struct vring_packed_desc),
198 sizeof(struct vring_packed_desc));
200 head_idx = vq->last_used_idx;
204 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
207 __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
210 vhost_log_cache_used_vring(dev, vq,
212 sizeof(struct vring_packed_desc),
213 sizeof(struct vring_packed_desc));
215 vq->shadow_used_idx = 0;
216 vhost_log_cache_sync(dev, vq);
219 static __rte_always_inline void
220 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
221 uint16_t desc_idx, uint32_t len, uint16_t count)
223 uint16_t i = vq->shadow_used_idx++;
225 vq->shadow_used_packed[i].id = desc_idx;
226 vq->shadow_used_packed[i].len = len;
227 vq->shadow_used_packed[i].count = count;
231 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
233 struct batch_copy_elem *elem = vq->batch_copy_elems;
234 uint16_t count = vq->batch_copy_nb_elems;
237 for (i = 0; i < count; i++) {
238 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
239 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
241 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
244 vq->batch_copy_nb_elems = 0;
248 do_data_copy_dequeue(struct vhost_virtqueue *vq)
250 struct batch_copy_elem *elem = vq->batch_copy_elems;
251 uint16_t count = vq->batch_copy_nb_elems;
254 for (i = 0; i < count; i++)
255 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
257 vq->batch_copy_nb_elems = 0;
260 static __rte_always_inline void
261 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
262 struct vhost_virtqueue *vq,
266 uint16_t num_buffers)
269 for (i = 0; i < num_buffers; i++) {
270 /* enqueue shadow flush action aligned with batch num */
271 if (!vq->shadow_used_idx)
272 vq->shadow_aligned_idx = vq->last_used_idx &
274 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
275 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
276 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
277 vq->shadow_aligned_idx += count[i];
278 vq->shadow_used_idx++;
281 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
282 do_data_copy_enqueue(dev, vq);
283 vhost_flush_enqueue_shadow_packed(dev, vq);
287 /* avoid write operation when necessary, to lessen cache issues */
288 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
289 if ((var) != (val)) \
293 static __rte_always_inline void
294 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
296 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
298 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
299 csum_l4 |= PKT_TX_TCP_CKSUM;
302 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
303 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
306 case PKT_TX_TCP_CKSUM:
307 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
310 case PKT_TX_UDP_CKSUM:
311 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
314 case PKT_TX_SCTP_CKSUM:
315 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
320 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
321 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
322 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
325 /* IP cksum verification cannot be bypassed, then calculate here */
326 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
327 struct rte_ipv4_hdr *ipv4_hdr;
329 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
331 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
334 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
335 if (m_buf->ol_flags & PKT_TX_IPV4)
336 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
338 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
339 net_hdr->gso_size = m_buf->tso_segsz;
340 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
342 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
343 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
344 net_hdr->gso_size = m_buf->tso_segsz;
345 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
348 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
349 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
350 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
354 static __rte_always_inline int
355 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
356 struct buf_vector *buf_vec, uint16_t *vec_idx,
357 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
359 uint16_t vec_id = *vec_idx;
363 uint64_t desc_chunck_len = desc_len;
365 if (unlikely(vec_id >= BUF_VECTOR_MAX))
368 desc_addr = vhost_iova_to_vva(dev, vq,
372 if (unlikely(!desc_addr))
375 rte_prefetch0((void *)(uintptr_t)desc_addr);
377 buf_vec[vec_id].buf_iova = desc_iova;
378 buf_vec[vec_id].buf_addr = desc_addr;
379 buf_vec[vec_id].buf_len = desc_chunck_len;
381 desc_len -= desc_chunck_len;
382 desc_iova += desc_chunck_len;
390 static __rte_always_inline int
391 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
392 uint32_t avail_idx, uint16_t *vec_idx,
393 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
394 uint32_t *desc_chain_len, uint8_t perm)
396 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
397 uint16_t vec_id = *vec_idx;
400 uint32_t nr_descs = vq->size;
402 struct vring_desc *descs = vq->desc;
403 struct vring_desc *idesc = NULL;
405 if (unlikely(idx >= vq->size))
408 *desc_chain_head = idx;
410 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
411 dlen = vq->desc[idx].len;
412 nr_descs = dlen / sizeof(struct vring_desc);
413 if (unlikely(nr_descs > vq->size))
416 descs = (struct vring_desc *)(uintptr_t)
417 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
420 if (unlikely(!descs))
423 if (unlikely(dlen < vq->desc[idx].len)) {
425 * The indirect desc table is not contiguous
426 * in process VA space, we have to copy it.
428 idesc = vhost_alloc_copy_ind_table(dev, vq,
429 vq->desc[idx].addr, vq->desc[idx].len);
430 if (unlikely(!idesc))
440 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
441 free_ind_table(idesc);
445 len += descs[idx].len;
447 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
448 descs[idx].addr, descs[idx].len,
450 free_ind_table(idesc);
454 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
457 idx = descs[idx].next;
460 *desc_chain_len = len;
463 if (unlikely(!!idesc))
464 free_ind_table(idesc);
470 * Returns -1 on fail, 0 on success
473 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
474 uint32_t size, struct buf_vector *buf_vec,
475 uint16_t *num_buffers, uint16_t avail_head,
479 uint16_t vec_idx = 0;
480 uint16_t max_tries, tries = 0;
482 uint16_t head_idx = 0;
486 cur_idx = vq->last_avail_idx;
488 if (rxvq_is_mergeable(dev))
489 max_tries = vq->size - 1;
494 if (unlikely(cur_idx == avail_head))
497 * if we tried all available ring items, and still
498 * can't get enough buf, it means something abnormal
501 if (unlikely(++tries > max_tries))
504 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
507 VHOST_ACCESS_RW) < 0))
509 len = RTE_MIN(len, size);
510 update_shadow_used_ring_split(vq, head_idx, len);
522 static __rte_always_inline int
523 fill_vec_buf_packed_indirect(struct virtio_net *dev,
524 struct vhost_virtqueue *vq,
525 struct vring_packed_desc *desc, uint16_t *vec_idx,
526 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
530 uint16_t vec_id = *vec_idx;
532 struct vring_packed_desc *descs, *idescs = NULL;
535 descs = (struct vring_packed_desc *)(uintptr_t)
536 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
537 if (unlikely(!descs))
540 if (unlikely(dlen < desc->len)) {
542 * The indirect desc table is not contiguous
543 * in process VA space, we have to copy it.
545 idescs = vhost_alloc_copy_ind_table(dev,
546 vq, desc->addr, desc->len);
547 if (unlikely(!idescs))
553 nr_descs = desc->len / sizeof(struct vring_packed_desc);
554 if (unlikely(nr_descs >= vq->size)) {
555 free_ind_table(idescs);
559 for (i = 0; i < nr_descs; i++) {
560 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
561 free_ind_table(idescs);
565 *len += descs[i].len;
566 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
567 descs[i].addr, descs[i].len,
573 if (unlikely(!!idescs))
574 free_ind_table(idescs);
579 static __rte_always_inline int
580 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
581 uint16_t avail_idx, uint16_t *desc_count,
582 struct buf_vector *buf_vec, uint16_t *vec_idx,
583 uint16_t *buf_id, uint32_t *len, uint8_t perm)
585 bool wrap_counter = vq->avail_wrap_counter;
586 struct vring_packed_desc *descs = vq->desc_packed;
587 uint16_t vec_id = *vec_idx;
589 if (avail_idx < vq->last_avail_idx)
593 * Perform a load-acquire barrier in desc_is_avail to
594 * enforce the ordering between desc flags and desc
597 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
604 if (unlikely(vec_id >= BUF_VECTOR_MAX))
607 if (unlikely(*desc_count >= vq->size))
611 *buf_id = descs[avail_idx].id;
613 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
614 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
620 *len += descs[avail_idx].len;
622 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
623 descs[avail_idx].addr,
624 descs[avail_idx].len,
629 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
632 if (++avail_idx >= vq->size) {
633 avail_idx -= vq->size;
644 * Returns -1 on fail, 0 on success
647 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
648 uint32_t size, struct buf_vector *buf_vec,
649 uint16_t *nr_vec, uint16_t *num_buffers,
653 uint16_t vec_idx = 0;
654 uint16_t max_tries, tries = 0;
661 avail_idx = vq->last_avail_idx;
663 if (rxvq_is_mergeable(dev))
664 max_tries = vq->size - 1;
670 * if we tried all available ring items, and still
671 * can't get enough buf, it means something abnormal
674 if (unlikely(++tries > max_tries))
677 if (unlikely(fill_vec_buf_packed(dev, vq,
678 avail_idx, &desc_count,
681 VHOST_ACCESS_RW) < 0))
684 len = RTE_MIN(len, size);
685 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
688 avail_idx += desc_count;
689 if (avail_idx >= vq->size)
690 avail_idx -= vq->size;
692 *nr_descs += desc_count;
701 static __rte_noinline void
702 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
703 struct buf_vector *buf_vec,
704 struct virtio_net_hdr_mrg_rxbuf *hdr)
707 uint64_t remain = dev->vhost_hlen;
708 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
709 uint64_t iova = buf_vec->buf_iova;
712 len = RTE_MIN(remain,
714 dst = buf_vec->buf_addr;
715 rte_memcpy((void *)(uintptr_t)dst,
716 (void *)(uintptr_t)src,
719 PRINT_PACKET(dev, (uintptr_t)dst,
721 vhost_log_cache_write_iova(dev, vq,
731 static __rte_always_inline int
732 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
733 struct rte_mbuf *m, struct buf_vector *buf_vec,
734 uint16_t nr_vec, uint16_t num_buffers)
736 uint32_t vec_idx = 0;
737 uint32_t mbuf_offset, mbuf_avail;
738 uint32_t buf_offset, buf_avail;
739 uint64_t buf_addr, buf_iova, buf_len;
742 struct rte_mbuf *hdr_mbuf;
743 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
744 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
747 if (unlikely(m == NULL)) {
752 buf_addr = buf_vec[vec_idx].buf_addr;
753 buf_iova = buf_vec[vec_idx].buf_iova;
754 buf_len = buf_vec[vec_idx].buf_len;
756 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
763 if (unlikely(buf_len < dev->vhost_hlen))
766 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
768 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
769 dev->vid, num_buffers);
771 if (unlikely(buf_len < dev->vhost_hlen)) {
772 buf_offset = dev->vhost_hlen - buf_len;
774 buf_addr = buf_vec[vec_idx].buf_addr;
775 buf_iova = buf_vec[vec_idx].buf_iova;
776 buf_len = buf_vec[vec_idx].buf_len;
777 buf_avail = buf_len - buf_offset;
779 buf_offset = dev->vhost_hlen;
780 buf_avail = buf_len - dev->vhost_hlen;
783 mbuf_avail = rte_pktmbuf_data_len(m);
785 while (mbuf_avail != 0 || m->next != NULL) {
786 /* done with current buf, get the next one */
787 if (buf_avail == 0) {
789 if (unlikely(vec_idx >= nr_vec)) {
794 buf_addr = buf_vec[vec_idx].buf_addr;
795 buf_iova = buf_vec[vec_idx].buf_iova;
796 buf_len = buf_vec[vec_idx].buf_len;
802 /* done with current mbuf, get the next one */
803 if (mbuf_avail == 0) {
807 mbuf_avail = rte_pktmbuf_data_len(m);
811 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
812 if (rxvq_is_mergeable(dev))
813 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
816 if (unlikely(hdr == &tmp_hdr)) {
817 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
819 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
821 vhost_log_cache_write_iova(dev, vq,
829 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
831 if (likely(cpy_len > MAX_BATCH_LEN ||
832 vq->batch_copy_nb_elems >= vq->size)) {
833 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
834 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
836 vhost_log_cache_write_iova(dev, vq,
837 buf_iova + buf_offset,
839 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
842 batch_copy[vq->batch_copy_nb_elems].dst =
843 (void *)((uintptr_t)(buf_addr + buf_offset));
844 batch_copy[vq->batch_copy_nb_elems].src =
845 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
846 batch_copy[vq->batch_copy_nb_elems].log_addr =
847 buf_iova + buf_offset;
848 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
849 vq->batch_copy_nb_elems++;
852 mbuf_avail -= cpy_len;
853 mbuf_offset += cpy_len;
854 buf_avail -= cpy_len;
855 buf_offset += cpy_len;
863 static __rte_always_inline int
864 vhost_enqueue_single_packed(struct virtio_net *dev,
865 struct vhost_virtqueue *vq,
866 struct rte_mbuf *pkt,
867 struct buf_vector *buf_vec,
871 uint16_t avail_idx = vq->last_avail_idx;
872 uint16_t max_tries, tries = 0;
876 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
877 uint16_t num_buffers = 0;
878 uint32_t buffer_len[vq->size];
879 uint16_t buffer_buf_id[vq->size];
880 uint16_t buffer_desc_count[vq->size];
882 if (rxvq_is_mergeable(dev))
883 max_tries = vq->size - 1;
889 * if we tried all available ring items, and still
890 * can't get enough buf, it means something abnormal
893 if (unlikely(++tries > max_tries))
896 if (unlikely(fill_vec_buf_packed(dev, vq,
897 avail_idx, &desc_count,
900 VHOST_ACCESS_RW) < 0))
903 len = RTE_MIN(len, size);
906 buffer_len[num_buffers] = len;
907 buffer_buf_id[num_buffers] = buf_id;
908 buffer_desc_count[num_buffers] = desc_count;
911 *nr_descs += desc_count;
912 avail_idx += desc_count;
913 if (avail_idx >= vq->size)
914 avail_idx -= vq->size;
917 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
920 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
921 buffer_desc_count, num_buffers);
926 static __rte_noinline uint32_t
927 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
928 struct rte_mbuf **pkts, uint32_t count)
930 uint32_t pkt_idx = 0;
931 uint16_t num_buffers;
932 struct buf_vector buf_vec[BUF_VECTOR_MAX];
935 avail_head = *((volatile uint16_t *)&vq->avail->idx);
938 * The ordering between avail index and
939 * desc reads needs to be enforced.
943 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
945 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
946 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
949 if (unlikely(reserve_avail_buf_split(dev, vq,
950 pkt_len, buf_vec, &num_buffers,
951 avail_head, &nr_vec) < 0)) {
952 VHOST_LOG_DEBUG(VHOST_DATA,
953 "(%d) failed to get enough desc from vring\n",
955 vq->shadow_used_idx -= num_buffers;
959 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
960 dev->vid, vq->last_avail_idx,
961 vq->last_avail_idx + num_buffers);
963 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
966 vq->shadow_used_idx -= num_buffers;
970 vq->last_avail_idx += num_buffers;
973 do_data_copy_enqueue(dev, vq);
975 if (likely(vq->shadow_used_idx)) {
976 flush_shadow_used_ring_split(dev, vq);
977 vhost_vring_call_split(dev, vq);
983 static __rte_unused int
984 virtio_dev_rx_batch_packed(struct virtio_net *dev,
985 struct vhost_virtqueue *vq,
986 struct rte_mbuf **pkts)
988 bool wrap_counter = vq->avail_wrap_counter;
989 struct vring_packed_desc *descs = vq->desc_packed;
990 uint16_t avail_idx = vq->last_avail_idx;
991 uint64_t desc_addrs[PACKED_BATCH_SIZE];
992 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
993 uint32_t buf_offset = dev->vhost_hlen;
994 uint64_t lens[PACKED_BATCH_SIZE];
997 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1000 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1003 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1004 if (unlikely(pkts[i]->next != NULL))
1006 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1013 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1014 lens[i] = descs[avail_idx + i].len;
1016 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1017 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1021 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1022 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1023 descs[avail_idx + i].addr,
1027 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1028 if (unlikely(lens[i] != descs[avail_idx + i].len))
1032 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1033 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1034 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1035 (uintptr_t)desc_addrs[i];
1036 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1039 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1040 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1042 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1044 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1045 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1046 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1053 static __rte_unused int16_t
1054 virtio_dev_rx_single_packed(struct virtio_net *dev,
1055 struct vhost_virtqueue *vq,
1056 struct rte_mbuf *pkt)
1058 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1059 uint16_t nr_descs = 0;
1062 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1064 VHOST_LOG_DEBUG(VHOST_DATA,
1065 "(%d) failed to get enough desc from vring\n",
1070 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1071 dev->vid, vq->last_avail_idx,
1072 vq->last_avail_idx + nr_descs);
1074 vq_inc_last_avail_packed(vq, nr_descs);
1079 static __rte_noinline uint32_t
1080 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1081 struct rte_mbuf **pkts, uint32_t count)
1083 uint32_t pkt_idx = 0;
1084 uint16_t num_buffers;
1085 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1087 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1088 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1089 uint16_t nr_vec = 0;
1090 uint16_t nr_descs = 0;
1092 if (unlikely(reserve_avail_buf_packed(dev, vq,
1093 pkt_len, buf_vec, &nr_vec,
1094 &num_buffers, &nr_descs) < 0)) {
1095 VHOST_LOG_DEBUG(VHOST_DATA,
1096 "(%d) failed to get enough desc from vring\n",
1098 vq->shadow_used_idx -= num_buffers;
1102 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1103 dev->vid, vq->last_avail_idx,
1104 vq->last_avail_idx + num_buffers);
1106 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1109 vq->shadow_used_idx -= num_buffers;
1113 vq_inc_last_avail_packed(vq, nr_descs);
1116 do_data_copy_enqueue(dev, vq);
1118 if (likely(vq->shadow_used_idx)) {
1119 vhost_flush_enqueue_shadow_packed(dev, vq);
1120 vhost_vring_call_packed(dev, vq);
1126 static __rte_always_inline uint32_t
1127 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1128 struct rte_mbuf **pkts, uint32_t count)
1130 struct vhost_virtqueue *vq;
1133 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1134 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1135 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1136 dev->vid, __func__, queue_id);
1140 vq = dev->virtqueue[queue_id];
1142 rte_spinlock_lock(&vq->access_lock);
1144 if (unlikely(vq->enabled == 0))
1145 goto out_access_unlock;
1147 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1148 vhost_user_iotlb_rd_lock(vq);
1150 if (unlikely(vq->access_ok == 0))
1151 if (unlikely(vring_translate(dev, vq) < 0))
1154 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1158 if (vq_is_packed(dev))
1159 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1161 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1164 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1165 vhost_user_iotlb_rd_unlock(vq);
1168 rte_spinlock_unlock(&vq->access_lock);
1174 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1175 struct rte_mbuf **pkts, uint16_t count)
1177 struct virtio_net *dev = get_device(vid);
1182 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1183 RTE_LOG(ERR, VHOST_DATA,
1184 "(%d) %s: built-in vhost net backend is disabled.\n",
1185 dev->vid, __func__);
1189 return virtio_dev_rx(dev, queue_id, pkts, count);
1193 virtio_net_with_host_offload(struct virtio_net *dev)
1196 ((1ULL << VIRTIO_NET_F_CSUM) |
1197 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1198 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1199 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1200 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1207 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1209 struct rte_ipv4_hdr *ipv4_hdr;
1210 struct rte_ipv6_hdr *ipv6_hdr;
1211 void *l3_hdr = NULL;
1212 struct rte_ether_hdr *eth_hdr;
1215 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1217 m->l2_len = sizeof(struct rte_ether_hdr);
1218 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1220 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1221 struct rte_vlan_hdr *vlan_hdr =
1222 (struct rte_vlan_hdr *)(eth_hdr + 1);
1224 m->l2_len += sizeof(struct rte_vlan_hdr);
1225 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1228 l3_hdr = (char *)eth_hdr + m->l2_len;
1230 switch (ethertype) {
1231 case RTE_ETHER_TYPE_IPV4:
1233 *l4_proto = ipv4_hdr->next_proto_id;
1234 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1235 *l4_hdr = (char *)l3_hdr + m->l3_len;
1236 m->ol_flags |= PKT_TX_IPV4;
1238 case RTE_ETHER_TYPE_IPV6:
1240 *l4_proto = ipv6_hdr->proto;
1241 m->l3_len = sizeof(struct rte_ipv6_hdr);
1242 *l4_hdr = (char *)l3_hdr + m->l3_len;
1243 m->ol_flags |= PKT_TX_IPV6;
1253 static __rte_always_inline void
1254 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1256 uint16_t l4_proto = 0;
1257 void *l4_hdr = NULL;
1258 struct rte_tcp_hdr *tcp_hdr = NULL;
1260 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1263 parse_ethernet(m, &l4_proto, &l4_hdr);
1264 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1265 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1266 switch (hdr->csum_offset) {
1267 case (offsetof(struct rte_tcp_hdr, cksum)):
1268 if (l4_proto == IPPROTO_TCP)
1269 m->ol_flags |= PKT_TX_TCP_CKSUM;
1271 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1272 if (l4_proto == IPPROTO_UDP)
1273 m->ol_flags |= PKT_TX_UDP_CKSUM;
1275 case (offsetof(struct rte_sctp_hdr, cksum)):
1276 if (l4_proto == IPPROTO_SCTP)
1277 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1285 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1286 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1287 case VIRTIO_NET_HDR_GSO_TCPV4:
1288 case VIRTIO_NET_HDR_GSO_TCPV6:
1290 m->ol_flags |= PKT_TX_TCP_SEG;
1291 m->tso_segsz = hdr->gso_size;
1292 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1294 case VIRTIO_NET_HDR_GSO_UDP:
1295 m->ol_flags |= PKT_TX_UDP_SEG;
1296 m->tso_segsz = hdr->gso_size;
1297 m->l4_len = sizeof(struct rte_udp_hdr);
1300 RTE_LOG(WARNING, VHOST_DATA,
1301 "unsupported gso type %u.\n", hdr->gso_type);
1307 static __rte_noinline void
1308 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1309 struct buf_vector *buf_vec)
1312 uint64_t remain = sizeof(struct virtio_net_hdr);
1314 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1317 len = RTE_MIN(remain, buf_vec->buf_len);
1318 src = buf_vec->buf_addr;
1319 rte_memcpy((void *)(uintptr_t)dst,
1320 (void *)(uintptr_t)src, len);
1328 static __rte_always_inline int
1329 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1330 struct buf_vector *buf_vec, uint16_t nr_vec,
1331 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1333 uint32_t buf_avail, buf_offset;
1334 uint64_t buf_addr, buf_iova, buf_len;
1335 uint32_t mbuf_avail, mbuf_offset;
1337 struct rte_mbuf *cur = m, *prev = m;
1338 struct virtio_net_hdr tmp_hdr;
1339 struct virtio_net_hdr *hdr = NULL;
1340 /* A counter to avoid desc dead loop chain */
1341 uint16_t vec_idx = 0;
1342 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1345 buf_addr = buf_vec[vec_idx].buf_addr;
1346 buf_iova = buf_vec[vec_idx].buf_iova;
1347 buf_len = buf_vec[vec_idx].buf_len;
1349 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1354 if (virtio_net_with_host_offload(dev)) {
1355 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1357 * No luck, the virtio-net header doesn't fit
1358 * in a contiguous virtual area.
1360 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1363 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1368 * A virtio driver normally uses at least 2 desc buffers
1369 * for Tx: the first for storing the header, and others
1370 * for storing the data.
1372 if (unlikely(buf_len < dev->vhost_hlen)) {
1373 buf_offset = dev->vhost_hlen - buf_len;
1375 buf_addr = buf_vec[vec_idx].buf_addr;
1376 buf_iova = buf_vec[vec_idx].buf_iova;
1377 buf_len = buf_vec[vec_idx].buf_len;
1378 buf_avail = buf_len - buf_offset;
1379 } else if (buf_len == dev->vhost_hlen) {
1380 if (unlikely(++vec_idx >= nr_vec))
1382 buf_addr = buf_vec[vec_idx].buf_addr;
1383 buf_iova = buf_vec[vec_idx].buf_iova;
1384 buf_len = buf_vec[vec_idx].buf_len;
1387 buf_avail = buf_len;
1389 buf_offset = dev->vhost_hlen;
1390 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1394 (uintptr_t)(buf_addr + buf_offset),
1395 (uint32_t)buf_avail, 0);
1398 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1402 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1405 * A desc buf might across two host physical pages that are
1406 * not continuous. In such case (gpa_to_hpa returns 0), data
1407 * will be copied even though zero copy is enabled.
1409 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1410 buf_iova + buf_offset, cpy_len)))) {
1411 cur->data_len = cpy_len;
1414 (void *)(uintptr_t)(buf_addr + buf_offset);
1415 cur->buf_iova = hpa;
1418 * In zero copy mode, one mbuf can only reference data
1419 * for one or partial of one desc buff.
1421 mbuf_avail = cpy_len;
1423 if (likely(cpy_len > MAX_BATCH_LEN ||
1424 vq->batch_copy_nb_elems >= vq->size ||
1425 (hdr && cur == m))) {
1426 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1428 (void *)((uintptr_t)(buf_addr +
1432 batch_copy[vq->batch_copy_nb_elems].dst =
1433 rte_pktmbuf_mtod_offset(cur, void *,
1435 batch_copy[vq->batch_copy_nb_elems].src =
1436 (void *)((uintptr_t)(buf_addr +
1438 batch_copy[vq->batch_copy_nb_elems].len =
1440 vq->batch_copy_nb_elems++;
1444 mbuf_avail -= cpy_len;
1445 mbuf_offset += cpy_len;
1446 buf_avail -= cpy_len;
1447 buf_offset += cpy_len;
1449 /* This buf reaches to its end, get the next one */
1450 if (buf_avail == 0) {
1451 if (++vec_idx >= nr_vec)
1454 buf_addr = buf_vec[vec_idx].buf_addr;
1455 buf_iova = buf_vec[vec_idx].buf_iova;
1456 buf_len = buf_vec[vec_idx].buf_len;
1459 buf_avail = buf_len;
1461 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1462 (uint32_t)buf_avail, 0);
1466 * This mbuf reaches to its end, get a new one
1467 * to hold more data.
1469 if (mbuf_avail == 0) {
1470 cur = rte_pktmbuf_alloc(mbuf_pool);
1471 if (unlikely(cur == NULL)) {
1472 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1473 "allocate memory for mbuf.\n");
1477 if (unlikely(dev->dequeue_zero_copy))
1478 rte_mbuf_refcnt_update(cur, 1);
1481 prev->data_len = mbuf_offset;
1483 m->pkt_len += mbuf_offset;
1487 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1491 prev->data_len = mbuf_offset;
1492 m->pkt_len += mbuf_offset;
1495 vhost_dequeue_offload(hdr, m);
1502 static __rte_always_inline struct zcopy_mbuf *
1503 get_zmbuf(struct vhost_virtqueue *vq)
1509 /* search [last_zmbuf_idx, zmbuf_size) */
1510 i = vq->last_zmbuf_idx;
1511 last = vq->zmbuf_size;
1514 for (; i < last; i++) {
1515 if (vq->zmbufs[i].in_use == 0) {
1516 vq->last_zmbuf_idx = i + 1;
1517 vq->zmbufs[i].in_use = 1;
1518 return &vq->zmbufs[i];
1524 /* search [0, last_zmbuf_idx) */
1526 last = vq->last_zmbuf_idx;
1534 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1540 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1542 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1543 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1548 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1549 * required, otherwise store shinfo in the new buffer.
1551 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1552 shinfo = rte_pktmbuf_mtod(pkt,
1553 struct rte_mbuf_ext_shared_info *);
1555 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1556 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1559 if (unlikely(total_len > UINT16_MAX))
1562 buf_len = total_len;
1563 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1564 if (unlikely(buf == NULL))
1567 /* Initialize shinfo */
1569 shinfo->free_cb = virtio_dev_extbuf_free;
1570 shinfo->fcb_opaque = buf;
1571 rte_mbuf_ext_refcnt_set(shinfo, 1);
1573 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1574 virtio_dev_extbuf_free, buf);
1575 if (unlikely(shinfo == NULL)) {
1577 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1582 iova = rte_malloc_virt2iova(buf);
1583 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1584 rte_pktmbuf_reset_headroom(pkt);
1590 * Allocate a host supported pktmbuf.
1592 static __rte_always_inline struct rte_mbuf *
1593 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1596 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1598 if (unlikely(pkt == NULL))
1601 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1604 /* attach an external buffer if supported */
1605 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1608 /* check if chained buffers are allowed */
1609 if (!dev->linearbuf)
1612 /* Data doesn't fit into the buffer and the host supports
1613 * only linear buffers
1615 rte_pktmbuf_free(pkt);
1620 static __rte_noinline uint16_t
1621 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1622 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1625 uint16_t free_entries;
1627 if (unlikely(dev->dequeue_zero_copy)) {
1628 struct zcopy_mbuf *zmbuf, *next;
1630 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1631 zmbuf != NULL; zmbuf = next) {
1632 next = TAILQ_NEXT(zmbuf, next);
1634 if (mbuf_is_consumed(zmbuf->mbuf)) {
1635 update_shadow_used_ring_split(vq,
1636 zmbuf->desc_idx, 0);
1637 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1638 restore_mbuf(zmbuf->mbuf);
1639 rte_pktmbuf_free(zmbuf->mbuf);
1645 if (likely(vq->shadow_used_idx)) {
1646 flush_shadow_used_ring_split(dev, vq);
1647 vhost_vring_call_split(dev, vq);
1651 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1653 if (free_entries == 0)
1657 * The ordering between avail index and
1658 * desc reads needs to be enforced.
1662 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1664 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1666 count = RTE_MIN(count, MAX_PKT_BURST);
1667 count = RTE_MIN(count, free_entries);
1668 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1671 for (i = 0; i < count; i++) {
1672 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1675 uint16_t nr_vec = 0;
1678 if (unlikely(fill_vec_buf_split(dev, vq,
1679 vq->last_avail_idx + i,
1681 &head_idx, &buf_len,
1682 VHOST_ACCESS_RO) < 0))
1685 if (likely(dev->dequeue_zero_copy == 0))
1686 update_shadow_used_ring_split(vq, head_idx, 0);
1688 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1689 if (unlikely(pkts[i] == NULL))
1692 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1694 if (unlikely(err)) {
1695 rte_pktmbuf_free(pkts[i]);
1699 if (unlikely(dev->dequeue_zero_copy)) {
1700 struct zcopy_mbuf *zmbuf;
1702 zmbuf = get_zmbuf(vq);
1704 rte_pktmbuf_free(pkts[i]);
1707 zmbuf->mbuf = pkts[i];
1708 zmbuf->desc_idx = head_idx;
1711 * Pin lock the mbuf; we will check later to see
1712 * whether the mbuf is freed (when we are the last
1713 * user) or not. If that's the case, we then could
1714 * update the used ring safely.
1716 rte_mbuf_refcnt_update(pkts[i], 1);
1719 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1722 vq->last_avail_idx += i;
1724 if (likely(dev->dequeue_zero_copy == 0)) {
1725 do_data_copy_dequeue(vq);
1726 if (unlikely(i < count))
1727 vq->shadow_used_idx = i;
1728 if (likely(vq->shadow_used_idx)) {
1729 flush_shadow_used_ring_split(dev, vq);
1730 vhost_vring_call_split(dev, vq);
1737 static __rte_always_inline int
1738 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1739 struct vhost_virtqueue *vq,
1740 struct rte_mempool *mbuf_pool,
1741 struct rte_mbuf **pkts,
1743 uintptr_t *desc_addrs,
1746 bool wrap = vq->avail_wrap_counter;
1747 struct vring_packed_desc *descs = vq->desc_packed;
1748 struct virtio_net_hdr *hdr;
1749 uint64_t lens[PACKED_BATCH_SIZE];
1750 uint64_t buf_lens[PACKED_BATCH_SIZE];
1751 uint32_t buf_offset = dev->vhost_hlen;
1754 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1756 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1759 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1760 flags = descs[avail_idx + i].flags;
1761 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1762 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1763 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1769 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1770 lens[i] = descs[avail_idx + i].len;
1772 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1773 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1774 descs[avail_idx + i].addr,
1775 &lens[i], VHOST_ACCESS_RW);
1778 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1779 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1783 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1784 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1789 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1790 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1792 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1793 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1797 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1798 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1799 pkts[i]->data_len = pkts[i]->pkt_len;
1800 ids[i] = descs[avail_idx + i].id;
1803 if (virtio_net_with_host_offload(dev)) {
1804 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1805 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1806 vhost_dequeue_offload(hdr, pkts[i]);
1813 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1814 rte_pktmbuf_free(pkts[i]);
1819 static __rte_unused int
1820 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1821 struct vhost_virtqueue *vq,
1822 struct rte_mempool *mbuf_pool,
1823 struct rte_mbuf **pkts)
1825 uint16_t avail_idx = vq->last_avail_idx;
1826 uint32_t buf_offset = dev->vhost_hlen;
1827 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1828 uint16_t ids[PACKED_BATCH_SIZE];
1831 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1832 avail_idx, desc_addrs, ids))
1835 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1836 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1838 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1839 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1840 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1843 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1848 static __rte_always_inline int
1849 vhost_dequeue_single_packed(struct virtio_net *dev,
1850 struct vhost_virtqueue *vq,
1851 struct rte_mempool *mbuf_pool,
1852 struct rte_mbuf **pkts,
1854 uint16_t *desc_count)
1856 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1858 uint16_t nr_vec = 0;
1861 if (unlikely(fill_vec_buf_packed(dev, vq,
1862 vq->last_avail_idx, desc_count,
1865 VHOST_ACCESS_RO) < 0))
1868 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1869 if (unlikely(*pkts == NULL)) {
1870 RTE_LOG(ERR, VHOST_DATA,
1871 "Failed to allocate memory for mbuf.\n");
1875 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1877 if (unlikely(err)) {
1878 rte_pktmbuf_free(*pkts);
1885 static __rte_unused int
1886 virtio_dev_tx_single_packed(struct virtio_net *dev,
1887 struct vhost_virtqueue *vq,
1888 struct rte_mempool *mbuf_pool,
1889 struct rte_mbuf **pkts)
1892 uint16_t buf_id, desc_count;
1894 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1898 vq_inc_last_avail_packed(vq, desc_count);
1903 static __rte_noinline uint16_t
1904 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1905 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1909 if (unlikely(dev->dequeue_zero_copy)) {
1910 struct zcopy_mbuf *zmbuf, *next;
1912 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1913 zmbuf != NULL; zmbuf = next) {
1914 next = TAILQ_NEXT(zmbuf, next);
1916 if (mbuf_is_consumed(zmbuf->mbuf)) {
1917 update_shadow_used_ring_packed(vq,
1922 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1923 restore_mbuf(zmbuf->mbuf);
1924 rte_pktmbuf_free(zmbuf->mbuf);
1930 if (likely(vq->shadow_used_idx)) {
1931 flush_shadow_used_ring_packed(dev, vq);
1932 vhost_vring_call_packed(dev, vq);
1936 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1938 count = RTE_MIN(count, MAX_PKT_BURST);
1939 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1942 for (i = 0; i < count; i++) {
1943 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1946 uint16_t desc_count, nr_vec = 0;
1949 if (unlikely(fill_vec_buf_packed(dev, vq,
1950 vq->last_avail_idx, &desc_count,
1953 VHOST_ACCESS_RO) < 0))
1956 if (likely(dev->dequeue_zero_copy == 0))
1957 update_shadow_used_ring_packed(vq, buf_id, 0,
1960 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1961 if (unlikely(pkts[i] == NULL))
1964 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1966 if (unlikely(err)) {
1967 rte_pktmbuf_free(pkts[i]);
1971 if (unlikely(dev->dequeue_zero_copy)) {
1972 struct zcopy_mbuf *zmbuf;
1974 zmbuf = get_zmbuf(vq);
1976 rte_pktmbuf_free(pkts[i]);
1979 zmbuf->mbuf = pkts[i];
1980 zmbuf->desc_idx = buf_id;
1981 zmbuf->desc_count = desc_count;
1984 * Pin lock the mbuf; we will check later to see
1985 * whether the mbuf is freed (when we are the last
1986 * user) or not. If that's the case, we then could
1987 * update the used ring safely.
1989 rte_mbuf_refcnt_update(pkts[i], 1);
1992 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1995 vq_inc_last_avail_packed(vq, desc_count);
1998 if (likely(dev->dequeue_zero_copy == 0)) {
1999 do_data_copy_dequeue(vq);
2000 if (unlikely(i < count))
2001 vq->shadow_used_idx = i;
2002 if (likely(vq->shadow_used_idx)) {
2003 flush_shadow_used_ring_packed(dev, vq);
2004 vhost_vring_call_packed(dev, vq);
2012 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2013 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2015 struct virtio_net *dev;
2016 struct rte_mbuf *rarp_mbuf = NULL;
2017 struct vhost_virtqueue *vq;
2019 dev = get_device(vid);
2023 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2024 RTE_LOG(ERR, VHOST_DATA,
2025 "(%d) %s: built-in vhost net backend is disabled.\n",
2026 dev->vid, __func__);
2030 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2031 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
2032 dev->vid, __func__, queue_id);
2036 vq = dev->virtqueue[queue_id];
2038 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2041 if (unlikely(vq->enabled == 0)) {
2043 goto out_access_unlock;
2046 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2047 vhost_user_iotlb_rd_lock(vq);
2049 if (unlikely(vq->access_ok == 0))
2050 if (unlikely(vring_translate(dev, vq) < 0)) {
2056 * Construct a RARP broadcast packet, and inject it to the "pkts"
2057 * array, to looks like that guest actually send such packet.
2059 * Check user_send_rarp() for more information.
2061 * broadcast_rarp shares a cacheline in the virtio_net structure
2062 * with some fields that are accessed during enqueue and
2063 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2064 * result in false sharing between enqueue and dequeue.
2066 * Prevent unnecessary false sharing by reading broadcast_rarp first
2067 * and only performing cmpset if the read indicates it is likely to
2070 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2071 rte_atomic16_cmpset((volatile uint16_t *)
2072 &dev->broadcast_rarp.cnt, 1, 0))) {
2074 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2075 if (rarp_mbuf == NULL) {
2076 RTE_LOG(ERR, VHOST_DATA,
2077 "Failed to make RARP packet.\n");
2084 if (vq_is_packed(dev))
2085 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
2087 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2090 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2091 vhost_user_iotlb_rd_unlock(vq);
2094 rte_spinlock_unlock(&vq->access_lock);
2096 if (unlikely(rarp_mbuf != NULL)) {
2098 * Inject it to the head of "pkts" array, so that switch's mac
2099 * learning table will get updated first.
2101 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2102 pkts[0] = rarp_mbuf;