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 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
159 struct vhost_virtqueue *vq)
161 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
163 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
165 vq->desc_packed[vq->shadow_last_used_idx].flags = used_elem->flags;
167 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
168 sizeof(struct vring_packed_desc),
169 sizeof(struct vring_packed_desc));
170 vq->shadow_used_idx = 0;
171 vhost_log_cache_sync(dev, vq);
174 static __rte_always_inline void
175 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
176 struct vhost_virtqueue *vq,
183 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
185 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
186 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
187 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
192 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
193 vq->desc_packed[vq->last_used_idx + i].flags = flags;
195 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
196 sizeof(struct vring_packed_desc),
197 sizeof(struct vring_packed_desc) *
199 vhost_log_cache_sync(dev, vq);
201 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
204 static __rte_always_inline void
205 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
206 struct vhost_virtqueue *vq,
213 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
215 if (!vq->shadow_used_idx) {
216 vq->shadow_last_used_idx = vq->last_used_idx;
217 vq->shadow_used_packed[0].id = ids[0];
218 vq->shadow_used_packed[0].len = 0;
219 vq->shadow_used_packed[0].count = 1;
220 vq->shadow_used_packed[0].flags = flags;
221 vq->shadow_used_idx++;
226 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
227 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
228 vq->desc_packed[vq->last_used_idx + i].len = 0;
232 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
233 vq->desc_packed[vq->last_used_idx + i].flags = flags;
235 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
236 sizeof(struct vring_packed_desc),
237 sizeof(struct vring_packed_desc) *
239 vhost_log_cache_sync(dev, vq);
241 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
244 static __rte_always_inline void
245 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
251 flags = vq->desc_packed[vq->last_used_idx].flags;
252 if (vq->used_wrap_counter) {
253 flags |= VRING_DESC_F_USED;
254 flags |= VRING_DESC_F_AVAIL;
256 flags &= ~VRING_DESC_F_USED;
257 flags &= ~VRING_DESC_F_AVAIL;
260 if (!vq->shadow_used_idx) {
261 vq->shadow_last_used_idx = vq->last_used_idx;
263 vq->shadow_used_packed[0].id = buf_id;
264 vq->shadow_used_packed[0].len = 0;
265 vq->shadow_used_packed[0].flags = flags;
266 vq->shadow_used_idx++;
268 vq->desc_packed[vq->last_used_idx].id = buf_id;
269 vq->desc_packed[vq->last_used_idx].len = 0;
270 vq->desc_packed[vq->last_used_idx].flags = flags;
273 vq_inc_last_used_packed(vq, count);
277 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
279 struct batch_copy_elem *elem = vq->batch_copy_elems;
280 uint16_t count = vq->batch_copy_nb_elems;
283 for (i = 0; i < count; i++) {
284 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
285 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
287 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
290 vq->batch_copy_nb_elems = 0;
294 do_data_copy_dequeue(struct vhost_virtqueue *vq)
296 struct batch_copy_elem *elem = vq->batch_copy_elems;
297 uint16_t count = vq->batch_copy_nb_elems;
300 for (i = 0; i < count; i++)
301 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
303 vq->batch_copy_nb_elems = 0;
306 static __rte_always_inline void
307 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
308 struct vhost_virtqueue *vq,
312 uint16_t num_buffers)
315 for (i = 0; i < num_buffers; i++) {
316 /* enqueue shadow flush action aligned with batch num */
317 if (!vq->shadow_used_idx)
318 vq->shadow_aligned_idx = vq->last_used_idx &
320 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
321 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
322 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
323 vq->shadow_aligned_idx += count[i];
324 vq->shadow_used_idx++;
327 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
328 do_data_copy_enqueue(dev, vq);
329 vhost_flush_enqueue_shadow_packed(dev, vq);
333 static __rte_always_inline void
334 vhost_flush_dequeue_packed(struct virtio_net *dev,
335 struct vhost_virtqueue *vq)
338 if (!vq->shadow_used_idx)
341 shadow_count = vq->last_used_idx - vq->shadow_last_used_idx;
342 if (shadow_count <= 0)
343 shadow_count += vq->size;
345 if ((uint32_t)shadow_count >= (vq->size - MAX_PKT_BURST)) {
346 do_data_copy_dequeue(vq);
347 vhost_flush_dequeue_shadow_packed(dev, vq);
348 vhost_vring_call_packed(dev, vq);
352 /* avoid write operation when necessary, to lessen cache issues */
353 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
354 if ((var) != (val)) \
358 static __rte_always_inline void
359 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
361 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
363 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
364 csum_l4 |= PKT_TX_TCP_CKSUM;
367 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
368 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
371 case PKT_TX_TCP_CKSUM:
372 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
375 case PKT_TX_UDP_CKSUM:
376 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
379 case PKT_TX_SCTP_CKSUM:
380 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
385 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
386 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
387 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
390 /* IP cksum verification cannot be bypassed, then calculate here */
391 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
392 struct rte_ipv4_hdr *ipv4_hdr;
394 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
396 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
399 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
400 if (m_buf->ol_flags & PKT_TX_IPV4)
401 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
403 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
404 net_hdr->gso_size = m_buf->tso_segsz;
405 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
407 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
408 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
409 net_hdr->gso_size = m_buf->tso_segsz;
410 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
413 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
414 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
415 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
419 static __rte_always_inline int
420 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
421 struct buf_vector *buf_vec, uint16_t *vec_idx,
422 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
424 uint16_t vec_id = *vec_idx;
428 uint64_t desc_chunck_len = desc_len;
430 if (unlikely(vec_id >= BUF_VECTOR_MAX))
433 desc_addr = vhost_iova_to_vva(dev, vq,
437 if (unlikely(!desc_addr))
440 rte_prefetch0((void *)(uintptr_t)desc_addr);
442 buf_vec[vec_id].buf_iova = desc_iova;
443 buf_vec[vec_id].buf_addr = desc_addr;
444 buf_vec[vec_id].buf_len = desc_chunck_len;
446 desc_len -= desc_chunck_len;
447 desc_iova += desc_chunck_len;
455 static __rte_always_inline int
456 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
457 uint32_t avail_idx, uint16_t *vec_idx,
458 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
459 uint32_t *desc_chain_len, uint8_t perm)
461 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
462 uint16_t vec_id = *vec_idx;
465 uint32_t nr_descs = vq->size;
467 struct vring_desc *descs = vq->desc;
468 struct vring_desc *idesc = NULL;
470 if (unlikely(idx >= vq->size))
473 *desc_chain_head = idx;
475 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
476 dlen = vq->desc[idx].len;
477 nr_descs = dlen / sizeof(struct vring_desc);
478 if (unlikely(nr_descs > vq->size))
481 descs = (struct vring_desc *)(uintptr_t)
482 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
485 if (unlikely(!descs))
488 if (unlikely(dlen < vq->desc[idx].len)) {
490 * The indirect desc table is not contiguous
491 * in process VA space, we have to copy it.
493 idesc = vhost_alloc_copy_ind_table(dev, vq,
494 vq->desc[idx].addr, vq->desc[idx].len);
495 if (unlikely(!idesc))
505 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
506 free_ind_table(idesc);
510 len += descs[idx].len;
512 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
513 descs[idx].addr, descs[idx].len,
515 free_ind_table(idesc);
519 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
522 idx = descs[idx].next;
525 *desc_chain_len = len;
528 if (unlikely(!!idesc))
529 free_ind_table(idesc);
535 * Returns -1 on fail, 0 on success
538 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
539 uint32_t size, struct buf_vector *buf_vec,
540 uint16_t *num_buffers, uint16_t avail_head,
544 uint16_t vec_idx = 0;
545 uint16_t max_tries, tries = 0;
547 uint16_t head_idx = 0;
551 cur_idx = vq->last_avail_idx;
553 if (rxvq_is_mergeable(dev))
554 max_tries = vq->size - 1;
559 if (unlikely(cur_idx == avail_head))
562 * if we tried all available ring items, and still
563 * can't get enough buf, it means something abnormal
566 if (unlikely(++tries > max_tries))
569 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
572 VHOST_ACCESS_RW) < 0))
574 len = RTE_MIN(len, size);
575 update_shadow_used_ring_split(vq, head_idx, len);
587 static __rte_always_inline int
588 fill_vec_buf_packed_indirect(struct virtio_net *dev,
589 struct vhost_virtqueue *vq,
590 struct vring_packed_desc *desc, uint16_t *vec_idx,
591 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
595 uint16_t vec_id = *vec_idx;
597 struct vring_packed_desc *descs, *idescs = NULL;
600 descs = (struct vring_packed_desc *)(uintptr_t)
601 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
602 if (unlikely(!descs))
605 if (unlikely(dlen < desc->len)) {
607 * The indirect desc table is not contiguous
608 * in process VA space, we have to copy it.
610 idescs = vhost_alloc_copy_ind_table(dev,
611 vq, desc->addr, desc->len);
612 if (unlikely(!idescs))
618 nr_descs = desc->len / sizeof(struct vring_packed_desc);
619 if (unlikely(nr_descs >= vq->size)) {
620 free_ind_table(idescs);
624 for (i = 0; i < nr_descs; i++) {
625 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
626 free_ind_table(idescs);
630 *len += descs[i].len;
631 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
632 descs[i].addr, descs[i].len,
638 if (unlikely(!!idescs))
639 free_ind_table(idescs);
644 static __rte_always_inline int
645 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
646 uint16_t avail_idx, uint16_t *desc_count,
647 struct buf_vector *buf_vec, uint16_t *vec_idx,
648 uint16_t *buf_id, uint32_t *len, uint8_t perm)
650 bool wrap_counter = vq->avail_wrap_counter;
651 struct vring_packed_desc *descs = vq->desc_packed;
652 uint16_t vec_id = *vec_idx;
654 if (avail_idx < vq->last_avail_idx)
658 * Perform a load-acquire barrier in desc_is_avail to
659 * enforce the ordering between desc flags and desc
662 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
669 if (unlikely(vec_id >= BUF_VECTOR_MAX))
672 if (unlikely(*desc_count >= vq->size))
676 *buf_id = descs[avail_idx].id;
678 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
679 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
685 *len += descs[avail_idx].len;
687 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
688 descs[avail_idx].addr,
689 descs[avail_idx].len,
694 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
697 if (++avail_idx >= vq->size) {
698 avail_idx -= vq->size;
708 static __rte_noinline void
709 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
710 struct buf_vector *buf_vec,
711 struct virtio_net_hdr_mrg_rxbuf *hdr)
714 uint64_t remain = dev->vhost_hlen;
715 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
716 uint64_t iova = buf_vec->buf_iova;
719 len = RTE_MIN(remain,
721 dst = buf_vec->buf_addr;
722 rte_memcpy((void *)(uintptr_t)dst,
723 (void *)(uintptr_t)src,
726 PRINT_PACKET(dev, (uintptr_t)dst,
728 vhost_log_cache_write_iova(dev, vq,
738 static __rte_always_inline int
739 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
740 struct rte_mbuf *m, struct buf_vector *buf_vec,
741 uint16_t nr_vec, uint16_t num_buffers)
743 uint32_t vec_idx = 0;
744 uint32_t mbuf_offset, mbuf_avail;
745 uint32_t buf_offset, buf_avail;
746 uint64_t buf_addr, buf_iova, buf_len;
749 struct rte_mbuf *hdr_mbuf;
750 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
751 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
754 if (unlikely(m == NULL)) {
759 buf_addr = buf_vec[vec_idx].buf_addr;
760 buf_iova = buf_vec[vec_idx].buf_iova;
761 buf_len = buf_vec[vec_idx].buf_len;
763 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
770 if (unlikely(buf_len < dev->vhost_hlen))
773 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
775 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
776 dev->vid, num_buffers);
778 if (unlikely(buf_len < dev->vhost_hlen)) {
779 buf_offset = dev->vhost_hlen - buf_len;
781 buf_addr = buf_vec[vec_idx].buf_addr;
782 buf_iova = buf_vec[vec_idx].buf_iova;
783 buf_len = buf_vec[vec_idx].buf_len;
784 buf_avail = buf_len - buf_offset;
786 buf_offset = dev->vhost_hlen;
787 buf_avail = buf_len - dev->vhost_hlen;
790 mbuf_avail = rte_pktmbuf_data_len(m);
792 while (mbuf_avail != 0 || m->next != NULL) {
793 /* done with current buf, get the next one */
794 if (buf_avail == 0) {
796 if (unlikely(vec_idx >= nr_vec)) {
801 buf_addr = buf_vec[vec_idx].buf_addr;
802 buf_iova = buf_vec[vec_idx].buf_iova;
803 buf_len = buf_vec[vec_idx].buf_len;
809 /* done with current mbuf, get the next one */
810 if (mbuf_avail == 0) {
814 mbuf_avail = rte_pktmbuf_data_len(m);
818 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
819 if (rxvq_is_mergeable(dev))
820 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
823 if (unlikely(hdr == &tmp_hdr)) {
824 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
826 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
828 vhost_log_cache_write_iova(dev, vq,
836 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
838 if (likely(cpy_len > MAX_BATCH_LEN ||
839 vq->batch_copy_nb_elems >= vq->size)) {
840 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
841 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
843 vhost_log_cache_write_iova(dev, vq,
844 buf_iova + buf_offset,
846 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
849 batch_copy[vq->batch_copy_nb_elems].dst =
850 (void *)((uintptr_t)(buf_addr + buf_offset));
851 batch_copy[vq->batch_copy_nb_elems].src =
852 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
853 batch_copy[vq->batch_copy_nb_elems].log_addr =
854 buf_iova + buf_offset;
855 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
856 vq->batch_copy_nb_elems++;
859 mbuf_avail -= cpy_len;
860 mbuf_offset += cpy_len;
861 buf_avail -= cpy_len;
862 buf_offset += cpy_len;
870 static __rte_always_inline int
871 vhost_enqueue_single_packed(struct virtio_net *dev,
872 struct vhost_virtqueue *vq,
873 struct rte_mbuf *pkt,
874 struct buf_vector *buf_vec,
878 uint16_t avail_idx = vq->last_avail_idx;
879 uint16_t max_tries, tries = 0;
883 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
884 uint16_t num_buffers = 0;
885 uint32_t buffer_len[vq->size];
886 uint16_t buffer_buf_id[vq->size];
887 uint16_t buffer_desc_count[vq->size];
889 if (rxvq_is_mergeable(dev))
890 max_tries = vq->size - 1;
896 * if we tried all available ring items, and still
897 * can't get enough buf, it means something abnormal
900 if (unlikely(++tries > max_tries))
903 if (unlikely(fill_vec_buf_packed(dev, vq,
904 avail_idx, &desc_count,
907 VHOST_ACCESS_RW) < 0))
910 len = RTE_MIN(len, size);
913 buffer_len[num_buffers] = len;
914 buffer_buf_id[num_buffers] = buf_id;
915 buffer_desc_count[num_buffers] = desc_count;
918 *nr_descs += desc_count;
919 avail_idx += desc_count;
920 if (avail_idx >= vq->size)
921 avail_idx -= vq->size;
924 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
927 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
928 buffer_desc_count, num_buffers);
933 static __rte_noinline uint32_t
934 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
935 struct rte_mbuf **pkts, uint32_t count)
937 uint32_t pkt_idx = 0;
938 uint16_t num_buffers;
939 struct buf_vector buf_vec[BUF_VECTOR_MAX];
942 avail_head = *((volatile uint16_t *)&vq->avail->idx);
945 * The ordering between avail index and
946 * desc reads needs to be enforced.
950 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
952 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
953 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
956 if (unlikely(reserve_avail_buf_split(dev, vq,
957 pkt_len, buf_vec, &num_buffers,
958 avail_head, &nr_vec) < 0)) {
959 VHOST_LOG_DEBUG(VHOST_DATA,
960 "(%d) failed to get enough desc from vring\n",
962 vq->shadow_used_idx -= num_buffers;
966 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
967 dev->vid, vq->last_avail_idx,
968 vq->last_avail_idx + num_buffers);
970 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
973 vq->shadow_used_idx -= num_buffers;
977 vq->last_avail_idx += num_buffers;
980 do_data_copy_enqueue(dev, vq);
982 if (likely(vq->shadow_used_idx)) {
983 flush_shadow_used_ring_split(dev, vq);
984 vhost_vring_call_split(dev, vq);
990 static __rte_always_inline int
991 virtio_dev_rx_batch_packed(struct virtio_net *dev,
992 struct vhost_virtqueue *vq,
993 struct rte_mbuf **pkts)
995 bool wrap_counter = vq->avail_wrap_counter;
996 struct vring_packed_desc *descs = vq->desc_packed;
997 uint16_t avail_idx = vq->last_avail_idx;
998 uint64_t desc_addrs[PACKED_BATCH_SIZE];
999 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1000 uint32_t buf_offset = dev->vhost_hlen;
1001 uint64_t lens[PACKED_BATCH_SIZE];
1002 uint16_t ids[PACKED_BATCH_SIZE];
1005 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1008 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1011 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1012 if (unlikely(pkts[i]->next != NULL))
1014 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1021 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1022 lens[i] = descs[avail_idx + i].len;
1024 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1025 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1029 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1030 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1031 descs[avail_idx + i].addr,
1035 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1036 if (unlikely(lens[i] != descs[avail_idx + i].len))
1040 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1041 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1042 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1043 (uintptr_t)desc_addrs[i];
1044 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1047 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1048 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1050 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1052 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1053 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1054 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1058 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1059 ids[i] = descs[avail_idx + i].id;
1061 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1066 static __rte_always_inline int16_t
1067 virtio_dev_rx_single_packed(struct virtio_net *dev,
1068 struct vhost_virtqueue *vq,
1069 struct rte_mbuf *pkt)
1071 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1072 uint16_t nr_descs = 0;
1075 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1077 VHOST_LOG_DEBUG(VHOST_DATA,
1078 "(%d) failed to get enough desc from vring\n",
1083 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1084 dev->vid, vq->last_avail_idx,
1085 vq->last_avail_idx + nr_descs);
1087 vq_inc_last_avail_packed(vq, nr_descs);
1092 static __rte_noinline uint32_t
1093 virtio_dev_rx_packed(struct virtio_net *dev,
1094 struct vhost_virtqueue *vq,
1095 struct rte_mbuf **pkts,
1098 uint32_t pkt_idx = 0;
1099 uint32_t remained = count;
1102 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1104 if (remained >= PACKED_BATCH_SIZE) {
1105 if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
1106 pkt_idx += PACKED_BATCH_SIZE;
1107 remained -= PACKED_BATCH_SIZE;
1112 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1117 } while (pkt_idx < count);
1119 if (vq->shadow_used_idx) {
1120 do_data_copy_enqueue(dev, vq);
1121 vhost_flush_enqueue_shadow_packed(dev, vq);
1125 vhost_vring_call_packed(dev, vq);
1130 static __rte_always_inline uint32_t
1131 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1132 struct rte_mbuf **pkts, uint32_t count)
1134 struct vhost_virtqueue *vq;
1137 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1138 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1139 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1140 dev->vid, __func__, queue_id);
1144 vq = dev->virtqueue[queue_id];
1146 rte_spinlock_lock(&vq->access_lock);
1148 if (unlikely(vq->enabled == 0))
1149 goto out_access_unlock;
1151 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1152 vhost_user_iotlb_rd_lock(vq);
1154 if (unlikely(vq->access_ok == 0))
1155 if (unlikely(vring_translate(dev, vq) < 0))
1158 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1162 if (vq_is_packed(dev))
1163 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1165 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1168 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1169 vhost_user_iotlb_rd_unlock(vq);
1172 rte_spinlock_unlock(&vq->access_lock);
1178 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1179 struct rte_mbuf **pkts, uint16_t count)
1181 struct virtio_net *dev = get_device(vid);
1186 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1187 RTE_LOG(ERR, VHOST_DATA,
1188 "(%d) %s: built-in vhost net backend is disabled.\n",
1189 dev->vid, __func__);
1193 return virtio_dev_rx(dev, queue_id, pkts, count);
1197 virtio_net_with_host_offload(struct virtio_net *dev)
1200 ((1ULL << VIRTIO_NET_F_CSUM) |
1201 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1202 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1203 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1204 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1211 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1213 struct rte_ipv4_hdr *ipv4_hdr;
1214 struct rte_ipv6_hdr *ipv6_hdr;
1215 void *l3_hdr = NULL;
1216 struct rte_ether_hdr *eth_hdr;
1219 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1221 m->l2_len = sizeof(struct rte_ether_hdr);
1222 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1224 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1225 struct rte_vlan_hdr *vlan_hdr =
1226 (struct rte_vlan_hdr *)(eth_hdr + 1);
1228 m->l2_len += sizeof(struct rte_vlan_hdr);
1229 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1232 l3_hdr = (char *)eth_hdr + m->l2_len;
1234 switch (ethertype) {
1235 case RTE_ETHER_TYPE_IPV4:
1237 *l4_proto = ipv4_hdr->next_proto_id;
1238 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1239 *l4_hdr = (char *)l3_hdr + m->l3_len;
1240 m->ol_flags |= PKT_TX_IPV4;
1242 case RTE_ETHER_TYPE_IPV6:
1244 *l4_proto = ipv6_hdr->proto;
1245 m->l3_len = sizeof(struct rte_ipv6_hdr);
1246 *l4_hdr = (char *)l3_hdr + m->l3_len;
1247 m->ol_flags |= PKT_TX_IPV6;
1257 static __rte_always_inline void
1258 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1260 uint16_t l4_proto = 0;
1261 void *l4_hdr = NULL;
1262 struct rte_tcp_hdr *tcp_hdr = NULL;
1264 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1267 parse_ethernet(m, &l4_proto, &l4_hdr);
1268 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1269 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1270 switch (hdr->csum_offset) {
1271 case (offsetof(struct rte_tcp_hdr, cksum)):
1272 if (l4_proto == IPPROTO_TCP)
1273 m->ol_flags |= PKT_TX_TCP_CKSUM;
1275 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1276 if (l4_proto == IPPROTO_UDP)
1277 m->ol_flags |= PKT_TX_UDP_CKSUM;
1279 case (offsetof(struct rte_sctp_hdr, cksum)):
1280 if (l4_proto == IPPROTO_SCTP)
1281 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1289 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1290 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1291 case VIRTIO_NET_HDR_GSO_TCPV4:
1292 case VIRTIO_NET_HDR_GSO_TCPV6:
1294 m->ol_flags |= PKT_TX_TCP_SEG;
1295 m->tso_segsz = hdr->gso_size;
1296 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1298 case VIRTIO_NET_HDR_GSO_UDP:
1299 m->ol_flags |= PKT_TX_UDP_SEG;
1300 m->tso_segsz = hdr->gso_size;
1301 m->l4_len = sizeof(struct rte_udp_hdr);
1304 RTE_LOG(WARNING, VHOST_DATA,
1305 "unsupported gso type %u.\n", hdr->gso_type);
1311 static __rte_noinline void
1312 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1313 struct buf_vector *buf_vec)
1316 uint64_t remain = sizeof(struct virtio_net_hdr);
1318 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1321 len = RTE_MIN(remain, buf_vec->buf_len);
1322 src = buf_vec->buf_addr;
1323 rte_memcpy((void *)(uintptr_t)dst,
1324 (void *)(uintptr_t)src, len);
1332 static __rte_always_inline int
1333 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1334 struct buf_vector *buf_vec, uint16_t nr_vec,
1335 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1337 uint32_t buf_avail, buf_offset;
1338 uint64_t buf_addr, buf_iova, buf_len;
1339 uint32_t mbuf_avail, mbuf_offset;
1341 struct rte_mbuf *cur = m, *prev = m;
1342 struct virtio_net_hdr tmp_hdr;
1343 struct virtio_net_hdr *hdr = NULL;
1344 /* A counter to avoid desc dead loop chain */
1345 uint16_t vec_idx = 0;
1346 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1349 buf_addr = buf_vec[vec_idx].buf_addr;
1350 buf_iova = buf_vec[vec_idx].buf_iova;
1351 buf_len = buf_vec[vec_idx].buf_len;
1353 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1358 if (virtio_net_with_host_offload(dev)) {
1359 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1361 * No luck, the virtio-net header doesn't fit
1362 * in a contiguous virtual area.
1364 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1367 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1372 * A virtio driver normally uses at least 2 desc buffers
1373 * for Tx: the first for storing the header, and others
1374 * for storing the data.
1376 if (unlikely(buf_len < dev->vhost_hlen)) {
1377 buf_offset = dev->vhost_hlen - buf_len;
1379 buf_addr = buf_vec[vec_idx].buf_addr;
1380 buf_iova = buf_vec[vec_idx].buf_iova;
1381 buf_len = buf_vec[vec_idx].buf_len;
1382 buf_avail = buf_len - buf_offset;
1383 } else if (buf_len == dev->vhost_hlen) {
1384 if (unlikely(++vec_idx >= nr_vec))
1386 buf_addr = buf_vec[vec_idx].buf_addr;
1387 buf_iova = buf_vec[vec_idx].buf_iova;
1388 buf_len = buf_vec[vec_idx].buf_len;
1391 buf_avail = buf_len;
1393 buf_offset = dev->vhost_hlen;
1394 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1398 (uintptr_t)(buf_addr + buf_offset),
1399 (uint32_t)buf_avail, 0);
1402 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1406 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1409 * A desc buf might across two host physical pages that are
1410 * not continuous. In such case (gpa_to_hpa returns 0), data
1411 * will be copied even though zero copy is enabled.
1413 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1414 buf_iova + buf_offset, cpy_len)))) {
1415 cur->data_len = cpy_len;
1418 (void *)(uintptr_t)(buf_addr + buf_offset);
1419 cur->buf_iova = hpa;
1422 * In zero copy mode, one mbuf can only reference data
1423 * for one or partial of one desc buff.
1425 mbuf_avail = cpy_len;
1427 if (likely(cpy_len > MAX_BATCH_LEN ||
1428 vq->batch_copy_nb_elems >= vq->size ||
1429 (hdr && cur == m))) {
1430 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1432 (void *)((uintptr_t)(buf_addr +
1436 batch_copy[vq->batch_copy_nb_elems].dst =
1437 rte_pktmbuf_mtod_offset(cur, void *,
1439 batch_copy[vq->batch_copy_nb_elems].src =
1440 (void *)((uintptr_t)(buf_addr +
1442 batch_copy[vq->batch_copy_nb_elems].len =
1444 vq->batch_copy_nb_elems++;
1448 mbuf_avail -= cpy_len;
1449 mbuf_offset += cpy_len;
1450 buf_avail -= cpy_len;
1451 buf_offset += cpy_len;
1453 /* This buf reaches to its end, get the next one */
1454 if (buf_avail == 0) {
1455 if (++vec_idx >= nr_vec)
1458 buf_addr = buf_vec[vec_idx].buf_addr;
1459 buf_iova = buf_vec[vec_idx].buf_iova;
1460 buf_len = buf_vec[vec_idx].buf_len;
1463 buf_avail = buf_len;
1465 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1466 (uint32_t)buf_avail, 0);
1470 * This mbuf reaches to its end, get a new one
1471 * to hold more data.
1473 if (mbuf_avail == 0) {
1474 cur = rte_pktmbuf_alloc(mbuf_pool);
1475 if (unlikely(cur == NULL)) {
1476 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1477 "allocate memory for mbuf.\n");
1481 if (unlikely(dev->dequeue_zero_copy))
1482 rte_mbuf_refcnt_update(cur, 1);
1485 prev->data_len = mbuf_offset;
1487 m->pkt_len += mbuf_offset;
1491 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1495 prev->data_len = mbuf_offset;
1496 m->pkt_len += mbuf_offset;
1499 vhost_dequeue_offload(hdr, m);
1506 static __rte_always_inline struct zcopy_mbuf *
1507 get_zmbuf(struct vhost_virtqueue *vq)
1513 /* search [last_zmbuf_idx, zmbuf_size) */
1514 i = vq->last_zmbuf_idx;
1515 last = vq->zmbuf_size;
1518 for (; i < last; i++) {
1519 if (vq->zmbufs[i].in_use == 0) {
1520 vq->last_zmbuf_idx = i + 1;
1521 vq->zmbufs[i].in_use = 1;
1522 return &vq->zmbufs[i];
1528 /* search [0, last_zmbuf_idx) */
1530 last = vq->last_zmbuf_idx;
1538 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1544 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1546 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1547 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1552 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1553 * required, otherwise store shinfo in the new buffer.
1555 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1556 shinfo = rte_pktmbuf_mtod(pkt,
1557 struct rte_mbuf_ext_shared_info *);
1559 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1560 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1563 if (unlikely(total_len > UINT16_MAX))
1566 buf_len = total_len;
1567 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1568 if (unlikely(buf == NULL))
1571 /* Initialize shinfo */
1573 shinfo->free_cb = virtio_dev_extbuf_free;
1574 shinfo->fcb_opaque = buf;
1575 rte_mbuf_ext_refcnt_set(shinfo, 1);
1577 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1578 virtio_dev_extbuf_free, buf);
1579 if (unlikely(shinfo == NULL)) {
1581 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1586 iova = rte_malloc_virt2iova(buf);
1587 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1588 rte_pktmbuf_reset_headroom(pkt);
1594 * Allocate a host supported pktmbuf.
1596 static __rte_always_inline struct rte_mbuf *
1597 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1600 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1602 if (unlikely(pkt == NULL))
1605 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1608 /* attach an external buffer if supported */
1609 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1612 /* check if chained buffers are allowed */
1613 if (!dev->linearbuf)
1616 /* Data doesn't fit into the buffer and the host supports
1617 * only linear buffers
1619 rte_pktmbuf_free(pkt);
1624 static __rte_noinline uint16_t
1625 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1626 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1629 uint16_t free_entries;
1631 if (unlikely(dev->dequeue_zero_copy)) {
1632 struct zcopy_mbuf *zmbuf, *next;
1634 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1635 zmbuf != NULL; zmbuf = next) {
1636 next = TAILQ_NEXT(zmbuf, next);
1638 if (mbuf_is_consumed(zmbuf->mbuf)) {
1639 update_shadow_used_ring_split(vq,
1640 zmbuf->desc_idx, 0);
1641 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1642 restore_mbuf(zmbuf->mbuf);
1643 rte_pktmbuf_free(zmbuf->mbuf);
1649 if (likely(vq->shadow_used_idx)) {
1650 flush_shadow_used_ring_split(dev, vq);
1651 vhost_vring_call_split(dev, vq);
1655 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1657 if (free_entries == 0)
1661 * The ordering between avail index and
1662 * desc reads needs to be enforced.
1666 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1668 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1670 count = RTE_MIN(count, MAX_PKT_BURST);
1671 count = RTE_MIN(count, free_entries);
1672 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1675 for (i = 0; i < count; i++) {
1676 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1679 uint16_t nr_vec = 0;
1682 if (unlikely(fill_vec_buf_split(dev, vq,
1683 vq->last_avail_idx + i,
1685 &head_idx, &buf_len,
1686 VHOST_ACCESS_RO) < 0))
1689 if (likely(dev->dequeue_zero_copy == 0))
1690 update_shadow_used_ring_split(vq, head_idx, 0);
1692 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1693 if (unlikely(pkts[i] == NULL))
1696 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1698 if (unlikely(err)) {
1699 rte_pktmbuf_free(pkts[i]);
1703 if (unlikely(dev->dequeue_zero_copy)) {
1704 struct zcopy_mbuf *zmbuf;
1706 zmbuf = get_zmbuf(vq);
1708 rte_pktmbuf_free(pkts[i]);
1711 zmbuf->mbuf = pkts[i];
1712 zmbuf->desc_idx = head_idx;
1715 * Pin lock the mbuf; we will check later to see
1716 * whether the mbuf is freed (when we are the last
1717 * user) or not. If that's the case, we then could
1718 * update the used ring safely.
1720 rte_mbuf_refcnt_update(pkts[i], 1);
1723 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1726 vq->last_avail_idx += i;
1728 if (likely(dev->dequeue_zero_copy == 0)) {
1729 do_data_copy_dequeue(vq);
1730 if (unlikely(i < count))
1731 vq->shadow_used_idx = i;
1732 if (likely(vq->shadow_used_idx)) {
1733 flush_shadow_used_ring_split(dev, vq);
1734 vhost_vring_call_split(dev, vq);
1741 static __rte_always_inline int
1742 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1743 struct vhost_virtqueue *vq,
1744 struct rte_mempool *mbuf_pool,
1745 struct rte_mbuf **pkts,
1747 uintptr_t *desc_addrs,
1750 bool wrap = vq->avail_wrap_counter;
1751 struct vring_packed_desc *descs = vq->desc_packed;
1752 struct virtio_net_hdr *hdr;
1753 uint64_t lens[PACKED_BATCH_SIZE];
1754 uint64_t buf_lens[PACKED_BATCH_SIZE];
1755 uint32_t buf_offset = dev->vhost_hlen;
1758 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1760 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1763 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1764 flags = descs[avail_idx + i].flags;
1765 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1766 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1767 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1773 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1774 lens[i] = descs[avail_idx + i].len;
1776 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1777 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1778 descs[avail_idx + i].addr,
1779 &lens[i], VHOST_ACCESS_RW);
1782 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1783 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1787 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1788 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1793 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1794 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1796 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1797 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1801 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1802 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1803 pkts[i]->data_len = pkts[i]->pkt_len;
1804 ids[i] = descs[avail_idx + i].id;
1807 if (virtio_net_with_host_offload(dev)) {
1808 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1809 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1810 vhost_dequeue_offload(hdr, pkts[i]);
1817 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1818 rte_pktmbuf_free(pkts[i]);
1823 static __rte_always_inline int
1824 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1825 struct vhost_virtqueue *vq,
1826 struct rte_mempool *mbuf_pool,
1827 struct rte_mbuf **pkts)
1829 uint16_t avail_idx = vq->last_avail_idx;
1830 uint32_t buf_offset = dev->vhost_hlen;
1831 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1832 uint16_t ids[PACKED_BATCH_SIZE];
1835 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1836 avail_idx, desc_addrs, ids))
1839 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1840 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1842 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1843 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1844 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1847 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
1849 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1854 static __rte_always_inline int
1855 vhost_dequeue_single_packed(struct virtio_net *dev,
1856 struct vhost_virtqueue *vq,
1857 struct rte_mempool *mbuf_pool,
1858 struct rte_mbuf **pkts,
1860 uint16_t *desc_count)
1862 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1864 uint16_t nr_vec = 0;
1867 if (unlikely(fill_vec_buf_packed(dev, vq,
1868 vq->last_avail_idx, desc_count,
1871 VHOST_ACCESS_RO) < 0))
1874 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1875 if (unlikely(*pkts == NULL)) {
1876 RTE_LOG(ERR, VHOST_DATA,
1877 "Failed to allocate memory for mbuf.\n");
1881 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1883 if (unlikely(err)) {
1884 rte_pktmbuf_free(*pkts);
1891 static __rte_always_inline int
1892 virtio_dev_tx_single_packed(struct virtio_net *dev,
1893 struct vhost_virtqueue *vq,
1894 struct rte_mempool *mbuf_pool,
1895 struct rte_mbuf **pkts)
1898 uint16_t buf_id, desc_count;
1900 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1904 vhost_shadow_dequeue_single_packed(vq, buf_id, desc_count);
1906 vq_inc_last_avail_packed(vq, desc_count);
1911 static __rte_always_inline int
1912 virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,
1913 struct vhost_virtqueue *vq,
1914 struct rte_mempool *mbuf_pool,
1915 struct rte_mbuf **pkts)
1917 struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE];
1918 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1919 uint16_t ids[PACKED_BATCH_SIZE];
1922 uint16_t avail_idx = vq->last_avail_idx;
1924 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1925 avail_idx, desc_addrs, ids))
1928 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1929 zmbufs[i] = get_zmbuf(vq);
1931 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1936 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1937 zmbufs[i]->mbuf = pkts[i];
1938 zmbufs[i]->desc_idx = avail_idx + i;
1939 zmbufs[i]->desc_count = 1;
1942 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1943 rte_mbuf_refcnt_update(pkts[i], 1);
1945 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1946 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbufs[i], next);
1948 vq->nr_zmbuf += PACKED_BATCH_SIZE;
1949 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1954 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1955 rte_pktmbuf_free(pkts[i]);
1960 static __rte_always_inline int
1961 virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev,
1962 struct vhost_virtqueue *vq,
1963 struct rte_mempool *mbuf_pool,
1964 struct rte_mbuf **pkts)
1966 uint16_t buf_id, desc_count;
1967 struct zcopy_mbuf *zmbuf;
1969 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1973 zmbuf = get_zmbuf(vq);
1975 rte_pktmbuf_free(*pkts);
1978 zmbuf->mbuf = *pkts;
1979 zmbuf->desc_idx = vq->last_avail_idx;
1980 zmbuf->desc_count = desc_count;
1982 rte_mbuf_refcnt_update(*pkts, 1);
1985 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1987 vq_inc_last_avail_packed(vq, desc_count);
1991 static __rte_always_inline void
1992 free_zmbuf(struct vhost_virtqueue *vq)
1994 struct zcopy_mbuf *next = NULL;
1995 struct zcopy_mbuf *zmbuf;
1997 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1998 zmbuf != NULL; zmbuf = next) {
1999 next = TAILQ_NEXT(zmbuf, next);
2001 uint16_t last_used_idx = vq->last_used_idx;
2003 if (mbuf_is_consumed(zmbuf->mbuf)) {
2005 flags = vq->desc_packed[last_used_idx].flags;
2006 if (vq->used_wrap_counter) {
2007 flags |= VRING_DESC_F_USED;
2008 flags |= VRING_DESC_F_AVAIL;
2010 flags &= ~VRING_DESC_F_USED;
2011 flags &= ~VRING_DESC_F_AVAIL;
2014 vq->desc_packed[last_used_idx].id = zmbuf->desc_idx;
2015 vq->desc_packed[last_used_idx].len = 0;
2018 vq->desc_packed[last_used_idx].flags = flags;
2020 vq_inc_last_used_packed(vq, zmbuf->desc_count);
2022 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2023 restore_mbuf(zmbuf->mbuf);
2024 rte_pktmbuf_free(zmbuf->mbuf);
2031 static __rte_noinline uint16_t
2032 virtio_dev_tx_packed_zmbuf(struct virtio_net *dev,
2033 struct vhost_virtqueue *vq,
2034 struct rte_mempool *mbuf_pool,
2035 struct rte_mbuf **pkts,
2038 uint32_t pkt_idx = 0;
2039 uint32_t remained = count;
2044 if (remained >= PACKED_BATCH_SIZE) {
2045 if (!virtio_dev_tx_batch_packed_zmbuf(dev, vq,
2046 mbuf_pool, &pkts[pkt_idx])) {
2047 pkt_idx += PACKED_BATCH_SIZE;
2048 remained -= PACKED_BATCH_SIZE;
2053 if (virtio_dev_tx_single_packed_zmbuf(dev, vq, mbuf_pool,
2062 vhost_vring_call_packed(dev, vq);
2067 static __rte_noinline uint16_t
2068 virtio_dev_tx_packed(struct virtio_net *dev,
2069 struct vhost_virtqueue *vq,
2070 struct rte_mempool *mbuf_pool,
2071 struct rte_mbuf **pkts,
2074 uint32_t pkt_idx = 0;
2075 uint32_t remained = count;
2078 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2080 if (remained >= PACKED_BATCH_SIZE) {
2081 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2083 vhost_flush_dequeue_packed(dev, vq);
2084 pkt_idx += PACKED_BATCH_SIZE;
2085 remained -= PACKED_BATCH_SIZE;
2090 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2093 vhost_flush_dequeue_packed(dev, vq);
2099 if (vq->shadow_used_idx)
2100 do_data_copy_dequeue(vq);
2106 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2107 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2109 struct virtio_net *dev;
2110 struct rte_mbuf *rarp_mbuf = NULL;
2111 struct vhost_virtqueue *vq;
2113 dev = get_device(vid);
2117 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2118 RTE_LOG(ERR, VHOST_DATA,
2119 "(%d) %s: built-in vhost net backend is disabled.\n",
2120 dev->vid, __func__);
2124 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2125 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
2126 dev->vid, __func__, queue_id);
2130 vq = dev->virtqueue[queue_id];
2132 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2135 if (unlikely(vq->enabled == 0)) {
2137 goto out_access_unlock;
2140 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2141 vhost_user_iotlb_rd_lock(vq);
2143 if (unlikely(vq->access_ok == 0))
2144 if (unlikely(vring_translate(dev, vq) < 0)) {
2150 * Construct a RARP broadcast packet, and inject it to the "pkts"
2151 * array, to looks like that guest actually send such packet.
2153 * Check user_send_rarp() for more information.
2155 * broadcast_rarp shares a cacheline in the virtio_net structure
2156 * with some fields that are accessed during enqueue and
2157 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2158 * result in false sharing between enqueue and dequeue.
2160 * Prevent unnecessary false sharing by reading broadcast_rarp first
2161 * and only performing cmpset if the read indicates it is likely to
2164 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2165 rte_atomic16_cmpset((volatile uint16_t *)
2166 &dev->broadcast_rarp.cnt, 1, 0))) {
2168 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2169 if (rarp_mbuf == NULL) {
2170 RTE_LOG(ERR, VHOST_DATA,
2171 "Failed to make RARP packet.\n");
2178 if (vq_is_packed(dev)) {
2179 if (unlikely(dev->dequeue_zero_copy))
2180 count = virtio_dev_tx_packed_zmbuf(dev, vq, mbuf_pool,
2183 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts,
2186 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2189 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2190 vhost_user_iotlb_rd_unlock(vq);
2193 rte_spinlock_unlock(&vq->access_lock);
2195 if (unlikely(rarp_mbuf != NULL)) {
2197 * Inject it to the head of "pkts" array, so that switch's mac
2198 * learning table will get updated first.
2200 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2201 pkts[0] = rarp_mbuf;