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);
34 static __rte_always_inline bool
35 virtio_net_is_inorder(struct virtio_net *dev)
37 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
41 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
43 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
46 static __rte_always_inline void
47 do_flush_shadow_used_ring_split(struct virtio_net *dev,
48 struct vhost_virtqueue *vq,
49 uint16_t to, uint16_t from, uint16_t size)
51 rte_memcpy(&vq->used->ring[to],
52 &vq->shadow_used_split[from],
53 size * sizeof(struct vring_used_elem));
54 vhost_log_cache_used_vring(dev, vq,
55 offsetof(struct vring_used, ring[to]),
56 size * sizeof(struct vring_used_elem));
59 static __rte_always_inline void
60 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
62 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
64 if (used_idx + vq->shadow_used_idx <= vq->size) {
65 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
70 /* update used ring interval [used_idx, vq->size] */
71 size = vq->size - used_idx;
72 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
74 /* update the left half used ring interval [0, left_size] */
75 do_flush_shadow_used_ring_split(dev, vq, 0, size,
76 vq->shadow_used_idx - size);
78 vq->last_used_idx += vq->shadow_used_idx;
82 vhost_log_cache_sync(dev, vq);
84 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
85 vq->shadow_used_idx = 0;
86 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
87 sizeof(vq->used->idx));
90 static __rte_always_inline void
91 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
92 uint16_t desc_idx, uint32_t len)
94 uint16_t i = vq->shadow_used_idx++;
96 vq->shadow_used_split[i].id = desc_idx;
97 vq->shadow_used_split[i].len = len;
100 static __rte_always_inline void
101 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
102 struct vhost_virtqueue *vq)
105 uint16_t used_idx = vq->last_used_idx;
106 uint16_t head_idx = vq->last_used_idx;
107 uint16_t head_flags = 0;
109 /* Split loop in two to save memory barriers */
110 for (i = 0; i < vq->shadow_used_idx; i++) {
111 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
112 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
114 used_idx += vq->shadow_used_packed[i].count;
115 if (used_idx >= vq->size)
116 used_idx -= vq->size;
121 for (i = 0; i < vq->shadow_used_idx; i++) {
124 if (vq->shadow_used_packed[i].len)
125 flags = VRING_DESC_F_WRITE;
129 if (vq->used_wrap_counter) {
130 flags |= VRING_DESC_F_USED;
131 flags |= VRING_DESC_F_AVAIL;
133 flags &= ~VRING_DESC_F_USED;
134 flags &= ~VRING_DESC_F_AVAIL;
138 vq->desc_packed[vq->last_used_idx].flags = flags;
140 vhost_log_cache_used_vring(dev, vq,
142 sizeof(struct vring_packed_desc),
143 sizeof(struct vring_packed_desc));
145 head_idx = vq->last_used_idx;
149 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
152 vq->desc_packed[head_idx].flags = head_flags;
154 vhost_log_cache_used_vring(dev, vq,
156 sizeof(struct vring_packed_desc),
157 sizeof(struct vring_packed_desc));
159 vq->shadow_used_idx = 0;
160 vhost_log_cache_sync(dev, vq);
163 static __rte_always_inline void
164 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
165 struct vhost_virtqueue *vq)
167 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
169 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
171 vq->desc_packed[vq->shadow_last_used_idx].flags = used_elem->flags;
173 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
174 sizeof(struct vring_packed_desc),
175 sizeof(struct vring_packed_desc));
176 vq->shadow_used_idx = 0;
177 vhost_log_cache_sync(dev, vq);
180 static __rte_always_inline void
181 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
182 struct vhost_virtqueue *vq,
189 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
191 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
192 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
193 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
198 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
199 vq->desc_packed[vq->last_used_idx + i].flags = flags;
201 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
202 sizeof(struct vring_packed_desc),
203 sizeof(struct vring_packed_desc) *
205 vhost_log_cache_sync(dev, vq);
207 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
210 static __rte_always_inline void
211 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
214 vq->shadow_used_packed[0].id = id;
216 if (!vq->shadow_used_idx) {
217 vq->shadow_last_used_idx = vq->last_used_idx;
218 vq->shadow_used_packed[0].flags =
219 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
220 vq->shadow_used_packed[0].len = 0;
221 vq->shadow_used_packed[0].count = 1;
222 vq->shadow_used_idx++;
225 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
228 static __rte_always_inline void
229 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
230 struct vhost_virtqueue *vq,
237 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
239 if (!vq->shadow_used_idx) {
240 vq->shadow_last_used_idx = vq->last_used_idx;
241 vq->shadow_used_packed[0].id = ids[0];
242 vq->shadow_used_packed[0].len = 0;
243 vq->shadow_used_packed[0].count = 1;
244 vq->shadow_used_packed[0].flags = flags;
245 vq->shadow_used_idx++;
250 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
251 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
252 vq->desc_packed[vq->last_used_idx + i].len = 0;
256 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
257 vq->desc_packed[vq->last_used_idx + i].flags = flags;
259 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
260 sizeof(struct vring_packed_desc),
261 sizeof(struct vring_packed_desc) *
263 vhost_log_cache_sync(dev, vq);
265 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
268 static __rte_always_inline void
269 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
275 flags = vq->desc_packed[vq->last_used_idx].flags;
276 if (vq->used_wrap_counter) {
277 flags |= VRING_DESC_F_USED;
278 flags |= VRING_DESC_F_AVAIL;
280 flags &= ~VRING_DESC_F_USED;
281 flags &= ~VRING_DESC_F_AVAIL;
284 if (!vq->shadow_used_idx) {
285 vq->shadow_last_used_idx = vq->last_used_idx;
287 vq->shadow_used_packed[0].id = buf_id;
288 vq->shadow_used_packed[0].len = 0;
289 vq->shadow_used_packed[0].flags = flags;
290 vq->shadow_used_idx++;
292 vq->desc_packed[vq->last_used_idx].id = buf_id;
293 vq->desc_packed[vq->last_used_idx].len = 0;
294 vq->desc_packed[vq->last_used_idx].flags = flags;
297 vq_inc_last_used_packed(vq, count);
300 static __rte_always_inline void
301 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
307 vq->shadow_used_packed[0].id = buf_id;
309 flags = vq->desc_packed[vq->last_used_idx].flags;
310 if (vq->used_wrap_counter) {
311 flags |= VRING_DESC_F_USED;
312 flags |= VRING_DESC_F_AVAIL;
314 flags &= ~VRING_DESC_F_USED;
315 flags &= ~VRING_DESC_F_AVAIL;
318 if (!vq->shadow_used_idx) {
319 vq->shadow_last_used_idx = vq->last_used_idx;
320 vq->shadow_used_packed[0].len = 0;
321 vq->shadow_used_packed[0].flags = flags;
322 vq->shadow_used_idx++;
325 vq_inc_last_used_packed(vq, count);
329 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
331 struct batch_copy_elem *elem = vq->batch_copy_elems;
332 uint16_t count = vq->batch_copy_nb_elems;
335 for (i = 0; i < count; i++) {
336 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
337 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
339 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
342 vq->batch_copy_nb_elems = 0;
346 do_data_copy_dequeue(struct vhost_virtqueue *vq)
348 struct batch_copy_elem *elem = vq->batch_copy_elems;
349 uint16_t count = vq->batch_copy_nb_elems;
352 for (i = 0; i < count; i++)
353 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
355 vq->batch_copy_nb_elems = 0;
358 static __rte_always_inline void
359 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
360 struct vhost_virtqueue *vq,
364 uint16_t num_buffers)
367 for (i = 0; i < num_buffers; i++) {
368 /* enqueue shadow flush action aligned with batch num */
369 if (!vq->shadow_used_idx)
370 vq->shadow_aligned_idx = vq->last_used_idx &
372 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
373 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
374 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
375 vq->shadow_aligned_idx += count[i];
376 vq->shadow_used_idx++;
379 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
380 do_data_copy_enqueue(dev, vq);
381 vhost_flush_enqueue_shadow_packed(dev, vq);
385 static __rte_always_inline void
386 vhost_flush_dequeue_packed(struct virtio_net *dev,
387 struct vhost_virtqueue *vq)
390 if (!vq->shadow_used_idx)
393 shadow_count = vq->last_used_idx - vq->shadow_last_used_idx;
394 if (shadow_count <= 0)
395 shadow_count += vq->size;
397 if ((uint32_t)shadow_count >= (vq->size - MAX_PKT_BURST)) {
398 do_data_copy_dequeue(vq);
399 vhost_flush_dequeue_shadow_packed(dev, vq);
400 vhost_vring_call_packed(dev, vq);
404 /* avoid write operation when necessary, to lessen cache issues */
405 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
406 if ((var) != (val)) \
410 static __rte_always_inline void
411 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
413 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
415 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
416 csum_l4 |= PKT_TX_TCP_CKSUM;
419 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
420 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
423 case PKT_TX_TCP_CKSUM:
424 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
427 case PKT_TX_UDP_CKSUM:
428 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
431 case PKT_TX_SCTP_CKSUM:
432 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
437 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
438 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
439 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
442 /* IP cksum verification cannot be bypassed, then calculate here */
443 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
444 struct rte_ipv4_hdr *ipv4_hdr;
446 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
448 ipv4_hdr->hdr_checksum = 0;
449 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
452 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
453 if (m_buf->ol_flags & PKT_TX_IPV4)
454 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
456 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
457 net_hdr->gso_size = m_buf->tso_segsz;
458 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
460 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
461 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
462 net_hdr->gso_size = m_buf->tso_segsz;
463 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
466 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
467 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
468 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
472 static __rte_always_inline int
473 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
474 struct buf_vector *buf_vec, uint16_t *vec_idx,
475 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
477 uint16_t vec_id = *vec_idx;
481 uint64_t desc_chunck_len = desc_len;
483 if (unlikely(vec_id >= BUF_VECTOR_MAX))
486 desc_addr = vhost_iova_to_vva(dev, vq,
490 if (unlikely(!desc_addr))
493 rte_prefetch0((void *)(uintptr_t)desc_addr);
495 buf_vec[vec_id].buf_iova = desc_iova;
496 buf_vec[vec_id].buf_addr = desc_addr;
497 buf_vec[vec_id].buf_len = desc_chunck_len;
499 desc_len -= desc_chunck_len;
500 desc_iova += desc_chunck_len;
508 static __rte_always_inline int
509 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
510 uint32_t avail_idx, uint16_t *vec_idx,
511 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
512 uint32_t *desc_chain_len, uint8_t perm)
514 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
515 uint16_t vec_id = *vec_idx;
518 uint32_t nr_descs = vq->size;
520 struct vring_desc *descs = vq->desc;
521 struct vring_desc *idesc = NULL;
523 if (unlikely(idx >= vq->size))
526 *desc_chain_head = idx;
528 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
529 dlen = vq->desc[idx].len;
530 nr_descs = dlen / sizeof(struct vring_desc);
531 if (unlikely(nr_descs > vq->size))
534 descs = (struct vring_desc *)(uintptr_t)
535 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
538 if (unlikely(!descs))
541 if (unlikely(dlen < vq->desc[idx].len)) {
543 * The indirect desc table is not contiguous
544 * in process VA space, we have to copy it.
546 idesc = vhost_alloc_copy_ind_table(dev, vq,
547 vq->desc[idx].addr, vq->desc[idx].len);
548 if (unlikely(!idesc))
558 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
559 free_ind_table(idesc);
563 len += descs[idx].len;
565 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
566 descs[idx].addr, descs[idx].len,
568 free_ind_table(idesc);
572 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
575 idx = descs[idx].next;
578 *desc_chain_len = len;
581 if (unlikely(!!idesc))
582 free_ind_table(idesc);
588 * Returns -1 on fail, 0 on success
591 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
592 uint32_t size, struct buf_vector *buf_vec,
593 uint16_t *num_buffers, uint16_t avail_head,
597 uint16_t vec_idx = 0;
598 uint16_t max_tries, tries = 0;
600 uint16_t head_idx = 0;
604 cur_idx = vq->last_avail_idx;
606 if (rxvq_is_mergeable(dev))
607 max_tries = vq->size - 1;
612 if (unlikely(cur_idx == avail_head))
615 * if we tried all available ring items, and still
616 * can't get enough buf, it means something abnormal
619 if (unlikely(++tries > max_tries))
622 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
625 VHOST_ACCESS_RW) < 0))
627 len = RTE_MIN(len, size);
628 update_shadow_used_ring_split(vq, head_idx, len);
640 static __rte_always_inline int
641 fill_vec_buf_packed_indirect(struct virtio_net *dev,
642 struct vhost_virtqueue *vq,
643 struct vring_packed_desc *desc, uint16_t *vec_idx,
644 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
648 uint16_t vec_id = *vec_idx;
650 struct vring_packed_desc *descs, *idescs = NULL;
653 descs = (struct vring_packed_desc *)(uintptr_t)
654 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
655 if (unlikely(!descs))
658 if (unlikely(dlen < desc->len)) {
660 * The indirect desc table is not contiguous
661 * in process VA space, we have to copy it.
663 idescs = vhost_alloc_copy_ind_table(dev,
664 vq, desc->addr, desc->len);
665 if (unlikely(!idescs))
671 nr_descs = desc->len / sizeof(struct vring_packed_desc);
672 if (unlikely(nr_descs >= vq->size)) {
673 free_ind_table(idescs);
677 for (i = 0; i < nr_descs; i++) {
678 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
679 free_ind_table(idescs);
683 *len += descs[i].len;
684 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
685 descs[i].addr, descs[i].len,
691 if (unlikely(!!idescs))
692 free_ind_table(idescs);
697 static __rte_always_inline int
698 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
699 uint16_t avail_idx, uint16_t *desc_count,
700 struct buf_vector *buf_vec, uint16_t *vec_idx,
701 uint16_t *buf_id, uint32_t *len, uint8_t perm)
703 bool wrap_counter = vq->avail_wrap_counter;
704 struct vring_packed_desc *descs = vq->desc_packed;
705 uint16_t vec_id = *vec_idx;
707 if (avail_idx < vq->last_avail_idx)
711 * Perform a load-acquire barrier in desc_is_avail to
712 * enforce the ordering between desc flags and desc
715 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
722 if (unlikely(vec_id >= BUF_VECTOR_MAX))
725 if (unlikely(*desc_count >= vq->size))
729 *buf_id = descs[avail_idx].id;
731 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
732 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
738 *len += descs[avail_idx].len;
740 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
741 descs[avail_idx].addr,
742 descs[avail_idx].len,
747 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
750 if (++avail_idx >= vq->size) {
751 avail_idx -= vq->size;
761 static __rte_noinline void
762 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
763 struct buf_vector *buf_vec,
764 struct virtio_net_hdr_mrg_rxbuf *hdr)
767 uint64_t remain = dev->vhost_hlen;
768 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
769 uint64_t iova = buf_vec->buf_iova;
772 len = RTE_MIN(remain,
774 dst = buf_vec->buf_addr;
775 rte_memcpy((void *)(uintptr_t)dst,
776 (void *)(uintptr_t)src,
779 PRINT_PACKET(dev, (uintptr_t)dst,
781 vhost_log_cache_write_iova(dev, vq,
791 static __rte_always_inline int
792 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
793 struct rte_mbuf *m, struct buf_vector *buf_vec,
794 uint16_t nr_vec, uint16_t num_buffers)
796 uint32_t vec_idx = 0;
797 uint32_t mbuf_offset, mbuf_avail;
798 uint32_t buf_offset, buf_avail;
799 uint64_t buf_addr, buf_iova, buf_len;
802 struct rte_mbuf *hdr_mbuf;
803 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
804 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
807 if (unlikely(m == NULL)) {
812 buf_addr = buf_vec[vec_idx].buf_addr;
813 buf_iova = buf_vec[vec_idx].buf_iova;
814 buf_len = buf_vec[vec_idx].buf_len;
816 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
823 if (unlikely(buf_len < dev->vhost_hlen))
826 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
828 VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
829 dev->vid, num_buffers);
831 if (unlikely(buf_len < dev->vhost_hlen)) {
832 buf_offset = dev->vhost_hlen - buf_len;
834 buf_addr = buf_vec[vec_idx].buf_addr;
835 buf_iova = buf_vec[vec_idx].buf_iova;
836 buf_len = buf_vec[vec_idx].buf_len;
837 buf_avail = buf_len - buf_offset;
839 buf_offset = dev->vhost_hlen;
840 buf_avail = buf_len - dev->vhost_hlen;
843 mbuf_avail = rte_pktmbuf_data_len(m);
845 while (mbuf_avail != 0 || m->next != NULL) {
846 /* done with current buf, get the next one */
847 if (buf_avail == 0) {
849 if (unlikely(vec_idx >= nr_vec)) {
854 buf_addr = buf_vec[vec_idx].buf_addr;
855 buf_iova = buf_vec[vec_idx].buf_iova;
856 buf_len = buf_vec[vec_idx].buf_len;
862 /* done with current mbuf, get the next one */
863 if (mbuf_avail == 0) {
867 mbuf_avail = rte_pktmbuf_data_len(m);
871 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
872 if (rxvq_is_mergeable(dev))
873 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
876 if (unlikely(hdr == &tmp_hdr)) {
877 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
879 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
881 vhost_log_cache_write_iova(dev, vq,
889 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
891 if (likely(cpy_len > MAX_BATCH_LEN ||
892 vq->batch_copy_nb_elems >= vq->size)) {
893 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
894 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
896 vhost_log_cache_write_iova(dev, vq,
897 buf_iova + buf_offset,
899 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
902 batch_copy[vq->batch_copy_nb_elems].dst =
903 (void *)((uintptr_t)(buf_addr + buf_offset));
904 batch_copy[vq->batch_copy_nb_elems].src =
905 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
906 batch_copy[vq->batch_copy_nb_elems].log_addr =
907 buf_iova + buf_offset;
908 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
909 vq->batch_copy_nb_elems++;
912 mbuf_avail -= cpy_len;
913 mbuf_offset += cpy_len;
914 buf_avail -= cpy_len;
915 buf_offset += cpy_len;
923 static __rte_always_inline int
924 vhost_enqueue_single_packed(struct virtio_net *dev,
925 struct vhost_virtqueue *vq,
926 struct rte_mbuf *pkt,
927 struct buf_vector *buf_vec,
931 uint16_t avail_idx = vq->last_avail_idx;
932 uint16_t max_tries, tries = 0;
936 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
937 uint16_t num_buffers = 0;
938 uint32_t buffer_len[vq->size];
939 uint16_t buffer_buf_id[vq->size];
940 uint16_t buffer_desc_count[vq->size];
942 if (rxvq_is_mergeable(dev))
943 max_tries = vq->size - 1;
949 * if we tried all available ring items, and still
950 * can't get enough buf, it means something abnormal
953 if (unlikely(++tries > max_tries))
956 if (unlikely(fill_vec_buf_packed(dev, vq,
957 avail_idx, &desc_count,
960 VHOST_ACCESS_RW) < 0))
963 len = RTE_MIN(len, size);
966 buffer_len[num_buffers] = len;
967 buffer_buf_id[num_buffers] = buf_id;
968 buffer_desc_count[num_buffers] = desc_count;
971 *nr_descs += desc_count;
972 avail_idx += desc_count;
973 if (avail_idx >= vq->size)
974 avail_idx -= vq->size;
977 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
980 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
981 buffer_desc_count, num_buffers);
986 static __rte_noinline uint32_t
987 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
988 struct rte_mbuf **pkts, uint32_t count)
990 uint32_t pkt_idx = 0;
991 uint16_t num_buffers;
992 struct buf_vector buf_vec[BUF_VECTOR_MAX];
995 avail_head = *((volatile uint16_t *)&vq->avail->idx);
998 * The ordering between avail index and
999 * desc reads needs to be enforced.
1003 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1005 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1006 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1007 uint16_t nr_vec = 0;
1009 if (unlikely(reserve_avail_buf_split(dev, vq,
1010 pkt_len, buf_vec, &num_buffers,
1011 avail_head, &nr_vec) < 0)) {
1012 VHOST_LOG_DATA(DEBUG,
1013 "(%d) failed to get enough desc from vring\n",
1015 vq->shadow_used_idx -= num_buffers;
1019 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1020 dev->vid, vq->last_avail_idx,
1021 vq->last_avail_idx + num_buffers);
1023 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1026 vq->shadow_used_idx -= num_buffers;
1030 vq->last_avail_idx += num_buffers;
1033 do_data_copy_enqueue(dev, vq);
1035 if (likely(vq->shadow_used_idx)) {
1036 flush_shadow_used_ring_split(dev, vq);
1037 vhost_vring_call_split(dev, vq);
1043 static __rte_always_inline int
1044 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1045 struct vhost_virtqueue *vq,
1046 struct rte_mbuf **pkts)
1048 bool wrap_counter = vq->avail_wrap_counter;
1049 struct vring_packed_desc *descs = vq->desc_packed;
1050 uint16_t avail_idx = vq->last_avail_idx;
1051 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1052 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1053 uint32_t buf_offset = dev->vhost_hlen;
1054 uint64_t lens[PACKED_BATCH_SIZE];
1055 uint16_t ids[PACKED_BATCH_SIZE];
1058 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1061 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1064 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1065 if (unlikely(pkts[i]->next != NULL))
1067 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1074 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1075 lens[i] = descs[avail_idx + i].len;
1077 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1078 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1082 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1083 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1084 descs[avail_idx + i].addr,
1088 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1089 if (unlikely(lens[i] != descs[avail_idx + i].len))
1093 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1094 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1095 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1096 (uintptr_t)desc_addrs[i];
1097 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1100 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1101 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1103 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1105 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1106 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1107 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1111 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1112 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1115 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1116 ids[i] = descs[avail_idx + i].id;
1118 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1123 static __rte_always_inline int16_t
1124 virtio_dev_rx_single_packed(struct virtio_net *dev,
1125 struct vhost_virtqueue *vq,
1126 struct rte_mbuf *pkt)
1128 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1129 uint16_t nr_descs = 0;
1132 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1134 VHOST_LOG_DATA(DEBUG,
1135 "(%d) failed to get enough desc from vring\n",
1140 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1141 dev->vid, vq->last_avail_idx,
1142 vq->last_avail_idx + nr_descs);
1144 vq_inc_last_avail_packed(vq, nr_descs);
1149 static __rte_noinline uint32_t
1150 virtio_dev_rx_packed(struct virtio_net *dev,
1151 struct vhost_virtqueue *vq,
1152 struct rte_mbuf **pkts,
1155 uint32_t pkt_idx = 0;
1156 uint32_t remained = count;
1159 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1161 if (remained >= PACKED_BATCH_SIZE) {
1162 if (!virtio_dev_rx_batch_packed(dev, vq,
1164 pkt_idx += PACKED_BATCH_SIZE;
1165 remained -= PACKED_BATCH_SIZE;
1170 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1175 } while (pkt_idx < count);
1177 if (vq->shadow_used_idx) {
1178 do_data_copy_enqueue(dev, vq);
1179 vhost_flush_enqueue_shadow_packed(dev, vq);
1183 vhost_vring_call_packed(dev, vq);
1188 static __rte_always_inline uint32_t
1189 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1190 struct rte_mbuf **pkts, uint32_t count)
1192 struct vhost_virtqueue *vq;
1195 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1196 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1197 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1198 dev->vid, __func__, queue_id);
1202 vq = dev->virtqueue[queue_id];
1204 rte_spinlock_lock(&vq->access_lock);
1206 if (unlikely(vq->enabled == 0))
1207 goto out_access_unlock;
1209 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1210 vhost_user_iotlb_rd_lock(vq);
1212 if (unlikely(vq->access_ok == 0))
1213 if (unlikely(vring_translate(dev, vq) < 0))
1216 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1220 if (vq_is_packed(dev))
1221 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1223 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1226 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1227 vhost_user_iotlb_rd_unlock(vq);
1230 rte_spinlock_unlock(&vq->access_lock);
1236 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1237 struct rte_mbuf **pkts, uint16_t count)
1239 struct virtio_net *dev = get_device(vid);
1244 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1246 "(%d) %s: built-in vhost net backend is disabled.\n",
1247 dev->vid, __func__);
1251 return virtio_dev_rx(dev, queue_id, pkts, count);
1255 virtio_net_with_host_offload(struct virtio_net *dev)
1258 ((1ULL << VIRTIO_NET_F_CSUM) |
1259 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1260 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1261 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1262 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1269 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1271 struct rte_ipv4_hdr *ipv4_hdr;
1272 struct rte_ipv6_hdr *ipv6_hdr;
1273 void *l3_hdr = NULL;
1274 struct rte_ether_hdr *eth_hdr;
1277 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1279 m->l2_len = sizeof(struct rte_ether_hdr);
1280 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1282 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1283 struct rte_vlan_hdr *vlan_hdr =
1284 (struct rte_vlan_hdr *)(eth_hdr + 1);
1286 m->l2_len += sizeof(struct rte_vlan_hdr);
1287 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1290 l3_hdr = (char *)eth_hdr + m->l2_len;
1292 switch (ethertype) {
1293 case RTE_ETHER_TYPE_IPV4:
1295 *l4_proto = ipv4_hdr->next_proto_id;
1296 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1297 *l4_hdr = (char *)l3_hdr + m->l3_len;
1298 m->ol_flags |= PKT_TX_IPV4;
1300 case RTE_ETHER_TYPE_IPV6:
1302 *l4_proto = ipv6_hdr->proto;
1303 m->l3_len = sizeof(struct rte_ipv6_hdr);
1304 *l4_hdr = (char *)l3_hdr + m->l3_len;
1305 m->ol_flags |= PKT_TX_IPV6;
1315 static __rte_always_inline void
1316 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1318 uint16_t l4_proto = 0;
1319 void *l4_hdr = NULL;
1320 struct rte_tcp_hdr *tcp_hdr = NULL;
1322 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1325 parse_ethernet(m, &l4_proto, &l4_hdr);
1326 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1327 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1328 switch (hdr->csum_offset) {
1329 case (offsetof(struct rte_tcp_hdr, cksum)):
1330 if (l4_proto == IPPROTO_TCP)
1331 m->ol_flags |= PKT_TX_TCP_CKSUM;
1333 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1334 if (l4_proto == IPPROTO_UDP)
1335 m->ol_flags |= PKT_TX_UDP_CKSUM;
1337 case (offsetof(struct rte_sctp_hdr, cksum)):
1338 if (l4_proto == IPPROTO_SCTP)
1339 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1347 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1348 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1349 case VIRTIO_NET_HDR_GSO_TCPV4:
1350 case VIRTIO_NET_HDR_GSO_TCPV6:
1352 m->ol_flags |= PKT_TX_TCP_SEG;
1353 m->tso_segsz = hdr->gso_size;
1354 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1356 case VIRTIO_NET_HDR_GSO_UDP:
1357 m->ol_flags |= PKT_TX_UDP_SEG;
1358 m->tso_segsz = hdr->gso_size;
1359 m->l4_len = sizeof(struct rte_udp_hdr);
1362 VHOST_LOG_DATA(WARNING,
1363 "unsupported gso type %u.\n", hdr->gso_type);
1369 static __rte_noinline void
1370 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1371 struct buf_vector *buf_vec)
1374 uint64_t remain = sizeof(struct virtio_net_hdr);
1376 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1379 len = RTE_MIN(remain, buf_vec->buf_len);
1380 src = buf_vec->buf_addr;
1381 rte_memcpy((void *)(uintptr_t)dst,
1382 (void *)(uintptr_t)src, len);
1390 static __rte_always_inline int
1391 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1392 struct buf_vector *buf_vec, uint16_t nr_vec,
1393 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1395 uint32_t buf_avail, buf_offset;
1396 uint64_t buf_addr, buf_iova, buf_len;
1397 uint32_t mbuf_avail, mbuf_offset;
1399 struct rte_mbuf *cur = m, *prev = m;
1400 struct virtio_net_hdr tmp_hdr;
1401 struct virtio_net_hdr *hdr = NULL;
1402 /* A counter to avoid desc dead loop chain */
1403 uint16_t vec_idx = 0;
1404 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1407 buf_addr = buf_vec[vec_idx].buf_addr;
1408 buf_iova = buf_vec[vec_idx].buf_iova;
1409 buf_len = buf_vec[vec_idx].buf_len;
1411 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1416 if (virtio_net_with_host_offload(dev)) {
1417 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1419 * No luck, the virtio-net header doesn't fit
1420 * in a contiguous virtual area.
1422 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1425 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1430 * A virtio driver normally uses at least 2 desc buffers
1431 * for Tx: the first for storing the header, and others
1432 * for storing the data.
1434 if (unlikely(buf_len < dev->vhost_hlen)) {
1435 buf_offset = dev->vhost_hlen - buf_len;
1437 buf_addr = buf_vec[vec_idx].buf_addr;
1438 buf_iova = buf_vec[vec_idx].buf_iova;
1439 buf_len = buf_vec[vec_idx].buf_len;
1440 buf_avail = buf_len - buf_offset;
1441 } else if (buf_len == dev->vhost_hlen) {
1442 if (unlikely(++vec_idx >= nr_vec))
1444 buf_addr = buf_vec[vec_idx].buf_addr;
1445 buf_iova = buf_vec[vec_idx].buf_iova;
1446 buf_len = buf_vec[vec_idx].buf_len;
1449 buf_avail = buf_len;
1451 buf_offset = dev->vhost_hlen;
1452 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1456 (uintptr_t)(buf_addr + buf_offset),
1457 (uint32_t)buf_avail, 0);
1460 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1464 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1467 * A desc buf might across two host physical pages that are
1468 * not continuous. In such case (gpa_to_hpa returns 0), data
1469 * will be copied even though zero copy is enabled.
1471 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1472 buf_iova + buf_offset, cpy_len)))) {
1473 cur->data_len = cpy_len;
1476 (void *)(uintptr_t)(buf_addr + buf_offset);
1477 cur->buf_iova = hpa;
1480 * In zero copy mode, one mbuf can only reference data
1481 * for one or partial of one desc buff.
1483 mbuf_avail = cpy_len;
1485 if (likely(cpy_len > MAX_BATCH_LEN ||
1486 vq->batch_copy_nb_elems >= vq->size ||
1487 (hdr && cur == m))) {
1488 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1490 (void *)((uintptr_t)(buf_addr +
1494 batch_copy[vq->batch_copy_nb_elems].dst =
1495 rte_pktmbuf_mtod_offset(cur, void *,
1497 batch_copy[vq->batch_copy_nb_elems].src =
1498 (void *)((uintptr_t)(buf_addr +
1500 batch_copy[vq->batch_copy_nb_elems].len =
1502 vq->batch_copy_nb_elems++;
1506 mbuf_avail -= cpy_len;
1507 mbuf_offset += cpy_len;
1508 buf_avail -= cpy_len;
1509 buf_offset += cpy_len;
1511 /* This buf reaches to its end, get the next one */
1512 if (buf_avail == 0) {
1513 if (++vec_idx >= nr_vec)
1516 buf_addr = buf_vec[vec_idx].buf_addr;
1517 buf_iova = buf_vec[vec_idx].buf_iova;
1518 buf_len = buf_vec[vec_idx].buf_len;
1521 buf_avail = buf_len;
1523 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1524 (uint32_t)buf_avail, 0);
1528 * This mbuf reaches to its end, get a new one
1529 * to hold more data.
1531 if (mbuf_avail == 0) {
1532 cur = rte_pktmbuf_alloc(mbuf_pool);
1533 if (unlikely(cur == NULL)) {
1534 VHOST_LOG_DATA(ERR, "Failed to "
1535 "allocate memory for mbuf.\n");
1539 if (unlikely(dev->dequeue_zero_copy))
1540 rte_mbuf_refcnt_update(cur, 1);
1543 prev->data_len = mbuf_offset;
1545 m->pkt_len += mbuf_offset;
1549 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1553 prev->data_len = mbuf_offset;
1554 m->pkt_len += mbuf_offset;
1557 vhost_dequeue_offload(hdr, m);
1564 static __rte_always_inline struct zcopy_mbuf *
1565 get_zmbuf(struct vhost_virtqueue *vq)
1571 /* search [last_zmbuf_idx, zmbuf_size) */
1572 i = vq->last_zmbuf_idx;
1573 last = vq->zmbuf_size;
1576 for (; i < last; i++) {
1577 if (vq->zmbufs[i].in_use == 0) {
1578 vq->last_zmbuf_idx = i + 1;
1579 vq->zmbufs[i].in_use = 1;
1580 return &vq->zmbufs[i];
1586 /* search [0, last_zmbuf_idx) */
1588 last = vq->last_zmbuf_idx;
1596 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1602 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1604 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1605 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1610 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1611 * required, otherwise store shinfo in the new buffer.
1613 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1614 shinfo = rte_pktmbuf_mtod(pkt,
1615 struct rte_mbuf_ext_shared_info *);
1617 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1618 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1621 if (unlikely(total_len > UINT16_MAX))
1624 buf_len = total_len;
1625 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1626 if (unlikely(buf == NULL))
1629 /* Initialize shinfo */
1631 shinfo->free_cb = virtio_dev_extbuf_free;
1632 shinfo->fcb_opaque = buf;
1633 rte_mbuf_ext_refcnt_set(shinfo, 1);
1635 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1636 virtio_dev_extbuf_free, buf);
1637 if (unlikely(shinfo == NULL)) {
1639 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
1644 iova = rte_malloc_virt2iova(buf);
1645 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1646 rte_pktmbuf_reset_headroom(pkt);
1652 * Allocate a host supported pktmbuf.
1654 static __rte_always_inline struct rte_mbuf *
1655 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1658 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1660 if (unlikely(pkt == NULL)) {
1662 "Failed to allocate memory for mbuf.\n");
1666 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1669 /* attach an external buffer if supported */
1670 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1673 /* check if chained buffers are allowed */
1674 if (!dev->linearbuf)
1677 /* Data doesn't fit into the buffer and the host supports
1678 * only linear buffers
1680 rte_pktmbuf_free(pkt);
1685 static __rte_noinline uint16_t
1686 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1687 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1690 uint16_t free_entries;
1692 if (unlikely(dev->dequeue_zero_copy)) {
1693 struct zcopy_mbuf *zmbuf, *next;
1695 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1696 zmbuf != NULL; zmbuf = next) {
1697 next = TAILQ_NEXT(zmbuf, next);
1699 if (mbuf_is_consumed(zmbuf->mbuf)) {
1700 update_shadow_used_ring_split(vq,
1701 zmbuf->desc_idx, 0);
1702 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1703 restore_mbuf(zmbuf->mbuf);
1704 rte_pktmbuf_free(zmbuf->mbuf);
1710 if (likely(vq->shadow_used_idx)) {
1711 flush_shadow_used_ring_split(dev, vq);
1712 vhost_vring_call_split(dev, vq);
1716 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1718 if (free_entries == 0)
1722 * The ordering between avail index and
1723 * desc reads needs to be enforced.
1727 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1729 VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1731 count = RTE_MIN(count, MAX_PKT_BURST);
1732 count = RTE_MIN(count, free_entries);
1733 VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
1736 for (i = 0; i < count; i++) {
1737 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1740 uint16_t nr_vec = 0;
1743 if (unlikely(fill_vec_buf_split(dev, vq,
1744 vq->last_avail_idx + i,
1746 &head_idx, &buf_len,
1747 VHOST_ACCESS_RO) < 0))
1750 if (likely(dev->dequeue_zero_copy == 0))
1751 update_shadow_used_ring_split(vq, head_idx, 0);
1753 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1754 if (unlikely(pkts[i] == NULL))
1757 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1759 if (unlikely(err)) {
1760 rte_pktmbuf_free(pkts[i]);
1764 if (unlikely(dev->dequeue_zero_copy)) {
1765 struct zcopy_mbuf *zmbuf;
1767 zmbuf = get_zmbuf(vq);
1769 rte_pktmbuf_free(pkts[i]);
1772 zmbuf->mbuf = pkts[i];
1773 zmbuf->desc_idx = head_idx;
1776 * Pin lock the mbuf; we will check later to see
1777 * whether the mbuf is freed (when we are the last
1778 * user) or not. If that's the case, we then could
1779 * update the used ring safely.
1781 rte_mbuf_refcnt_update(pkts[i], 1);
1784 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1787 vq->last_avail_idx += i;
1789 if (likely(dev->dequeue_zero_copy == 0)) {
1790 do_data_copy_dequeue(vq);
1791 if (unlikely(i < count))
1792 vq->shadow_used_idx = i;
1793 if (likely(vq->shadow_used_idx)) {
1794 flush_shadow_used_ring_split(dev, vq);
1795 vhost_vring_call_split(dev, vq);
1802 static __rte_always_inline int
1803 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1804 struct vhost_virtqueue *vq,
1805 struct rte_mempool *mbuf_pool,
1806 struct rte_mbuf **pkts,
1808 uintptr_t *desc_addrs,
1811 bool wrap = vq->avail_wrap_counter;
1812 struct vring_packed_desc *descs = vq->desc_packed;
1813 struct virtio_net_hdr *hdr;
1814 uint64_t lens[PACKED_BATCH_SIZE];
1815 uint64_t buf_lens[PACKED_BATCH_SIZE];
1816 uint32_t buf_offset = dev->vhost_hlen;
1819 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1821 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1824 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1825 flags = descs[avail_idx + i].flags;
1826 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1827 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1828 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1834 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1835 lens[i] = descs[avail_idx + i].len;
1837 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1838 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1839 descs[avail_idx + i].addr,
1840 &lens[i], VHOST_ACCESS_RW);
1843 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1844 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1848 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1849 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1854 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1855 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1857 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1858 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1862 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1863 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1864 pkts[i]->data_len = pkts[i]->pkt_len;
1865 ids[i] = descs[avail_idx + i].id;
1868 if (virtio_net_with_host_offload(dev)) {
1869 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1870 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1871 vhost_dequeue_offload(hdr, pkts[i]);
1878 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1879 rte_pktmbuf_free(pkts[i]);
1884 static __rte_always_inline int
1885 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1886 struct vhost_virtqueue *vq,
1887 struct rte_mempool *mbuf_pool,
1888 struct rte_mbuf **pkts)
1890 uint16_t avail_idx = vq->last_avail_idx;
1891 uint32_t buf_offset = dev->vhost_hlen;
1892 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1893 uint16_t ids[PACKED_BATCH_SIZE];
1896 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1897 avail_idx, desc_addrs, ids))
1900 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1901 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1903 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1904 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1905 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1908 if (virtio_net_is_inorder(dev))
1909 vhost_shadow_dequeue_batch_packed_inorder(vq,
1910 ids[PACKED_BATCH_SIZE - 1]);
1912 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
1914 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1919 static __rte_always_inline int
1920 vhost_dequeue_single_packed(struct virtio_net *dev,
1921 struct vhost_virtqueue *vq,
1922 struct rte_mempool *mbuf_pool,
1923 struct rte_mbuf **pkts,
1925 uint16_t *desc_count)
1927 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1929 uint16_t nr_vec = 0;
1932 if (unlikely(fill_vec_buf_packed(dev, vq,
1933 vq->last_avail_idx, desc_count,
1936 VHOST_ACCESS_RO) < 0))
1939 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1940 if (unlikely(*pkts == NULL)) {
1942 "Failed to allocate memory for mbuf.\n");
1946 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1948 if (unlikely(err)) {
1949 rte_pktmbuf_free(*pkts);
1956 static __rte_always_inline int
1957 virtio_dev_tx_single_packed(struct virtio_net *dev,
1958 struct vhost_virtqueue *vq,
1959 struct rte_mempool *mbuf_pool,
1960 struct rte_mbuf **pkts)
1963 uint16_t buf_id, desc_count;
1965 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1969 if (virtio_net_is_inorder(dev))
1970 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
1973 vhost_shadow_dequeue_single_packed(vq, buf_id, desc_count);
1975 vq_inc_last_avail_packed(vq, desc_count);
1980 static __rte_always_inline int
1981 virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,
1982 struct vhost_virtqueue *vq,
1983 struct rte_mempool *mbuf_pool,
1984 struct rte_mbuf **pkts)
1986 struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE];
1987 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1988 uint16_t ids[PACKED_BATCH_SIZE];
1991 uint16_t avail_idx = vq->last_avail_idx;
1993 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1994 avail_idx, desc_addrs, ids))
1997 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1998 zmbufs[i] = get_zmbuf(vq);
2000 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2005 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2006 zmbufs[i]->mbuf = pkts[i];
2007 zmbufs[i]->desc_idx = avail_idx + i;
2008 zmbufs[i]->desc_count = 1;
2011 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2012 rte_mbuf_refcnt_update(pkts[i], 1);
2014 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2015 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbufs[i], next);
2017 vq->nr_zmbuf += PACKED_BATCH_SIZE;
2018 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2023 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2024 rte_pktmbuf_free(pkts[i]);
2029 static __rte_always_inline int
2030 virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev,
2031 struct vhost_virtqueue *vq,
2032 struct rte_mempool *mbuf_pool,
2033 struct rte_mbuf **pkts)
2035 uint16_t buf_id, desc_count;
2036 struct zcopy_mbuf *zmbuf;
2038 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2042 zmbuf = get_zmbuf(vq);
2044 rte_pktmbuf_free(*pkts);
2047 zmbuf->mbuf = *pkts;
2048 zmbuf->desc_idx = vq->last_avail_idx;
2049 zmbuf->desc_count = desc_count;
2051 rte_mbuf_refcnt_update(*pkts, 1);
2054 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2056 vq_inc_last_avail_packed(vq, desc_count);
2060 static __rte_always_inline void
2061 free_zmbuf(struct vhost_virtqueue *vq)
2063 struct zcopy_mbuf *next = NULL;
2064 struct zcopy_mbuf *zmbuf;
2066 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
2067 zmbuf != NULL; zmbuf = next) {
2068 next = TAILQ_NEXT(zmbuf, next);
2070 uint16_t last_used_idx = vq->last_used_idx;
2072 if (mbuf_is_consumed(zmbuf->mbuf)) {
2074 flags = vq->desc_packed[last_used_idx].flags;
2075 if (vq->used_wrap_counter) {
2076 flags |= VRING_DESC_F_USED;
2077 flags |= VRING_DESC_F_AVAIL;
2079 flags &= ~VRING_DESC_F_USED;
2080 flags &= ~VRING_DESC_F_AVAIL;
2083 vq->desc_packed[last_used_idx].id = zmbuf->desc_idx;
2084 vq->desc_packed[last_used_idx].len = 0;
2087 vq->desc_packed[last_used_idx].flags = flags;
2089 vq_inc_last_used_packed(vq, zmbuf->desc_count);
2091 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2092 restore_mbuf(zmbuf->mbuf);
2093 rte_pktmbuf_free(zmbuf->mbuf);
2100 static __rte_noinline uint16_t
2101 virtio_dev_tx_packed_zmbuf(struct virtio_net *dev,
2102 struct vhost_virtqueue *vq,
2103 struct rte_mempool *mbuf_pool,
2104 struct rte_mbuf **pkts,
2107 uint32_t pkt_idx = 0;
2108 uint32_t remained = count;
2113 if (remained >= PACKED_BATCH_SIZE) {
2114 if (!virtio_dev_tx_batch_packed_zmbuf(dev, vq,
2115 mbuf_pool, &pkts[pkt_idx])) {
2116 pkt_idx += PACKED_BATCH_SIZE;
2117 remained -= PACKED_BATCH_SIZE;
2122 if (virtio_dev_tx_single_packed_zmbuf(dev, vq, mbuf_pool,
2131 vhost_vring_call_packed(dev, vq);
2136 static __rte_always_inline bool
2137 next_desc_is_avail(const struct vhost_virtqueue *vq)
2139 bool wrap_counter = vq->avail_wrap_counter;
2140 uint16_t next_used_idx = vq->last_used_idx + 1;
2142 if (next_used_idx >= vq->size) {
2143 next_used_idx -= vq->size;
2147 return desc_is_avail(&vq->desc_packed[next_used_idx], wrap_counter);
2150 static __rte_noinline uint16_t
2151 virtio_dev_tx_packed(struct virtio_net *dev,
2152 struct vhost_virtqueue *vq,
2153 struct rte_mempool *mbuf_pool,
2154 struct rte_mbuf **pkts,
2157 uint32_t pkt_idx = 0;
2158 uint32_t remained = count;
2161 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2163 if (remained >= PACKED_BATCH_SIZE) {
2164 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2166 vhost_flush_dequeue_packed(dev, vq);
2167 pkt_idx += PACKED_BATCH_SIZE;
2168 remained -= PACKED_BATCH_SIZE;
2173 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2176 vhost_flush_dequeue_packed(dev, vq);
2182 if (vq->shadow_used_idx) {
2183 do_data_copy_dequeue(vq);
2185 if (remained && !next_desc_is_avail(vq)) {
2187 * The guest may be waiting to TX some buffers to
2188 * enqueue more to avoid bufferfloat, so we try to
2189 * reduce latency here.
2191 vhost_flush_dequeue_shadow_packed(dev, vq);
2192 vhost_vring_call_packed(dev, vq);
2200 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2201 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2203 struct virtio_net *dev;
2204 struct rte_mbuf *rarp_mbuf = NULL;
2205 struct vhost_virtqueue *vq;
2207 dev = get_device(vid);
2211 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2213 "(%d) %s: built-in vhost net backend is disabled.\n",
2214 dev->vid, __func__);
2218 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2220 "(%d) %s: invalid virtqueue idx %d.\n",
2221 dev->vid, __func__, queue_id);
2225 vq = dev->virtqueue[queue_id];
2227 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2230 if (unlikely(vq->enabled == 0)) {
2232 goto out_access_unlock;
2235 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2236 vhost_user_iotlb_rd_lock(vq);
2238 if (unlikely(vq->access_ok == 0))
2239 if (unlikely(vring_translate(dev, vq) < 0)) {
2245 * Construct a RARP broadcast packet, and inject it to the "pkts"
2246 * array, to looks like that guest actually send such packet.
2248 * Check user_send_rarp() for more information.
2250 * broadcast_rarp shares a cacheline in the virtio_net structure
2251 * with some fields that are accessed during enqueue and
2252 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2253 * result in false sharing between enqueue and dequeue.
2255 * Prevent unnecessary false sharing by reading broadcast_rarp first
2256 * and only performing cmpset if the read indicates it is likely to
2259 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2260 rte_atomic16_cmpset((volatile uint16_t *)
2261 &dev->broadcast_rarp.cnt, 1, 0))) {
2263 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2264 if (rarp_mbuf == NULL) {
2265 VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2272 if (vq_is_packed(dev)) {
2273 if (unlikely(dev->dequeue_zero_copy))
2274 count = virtio_dev_tx_packed_zmbuf(dev, vq, mbuf_pool,
2277 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts,
2280 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2283 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2284 vhost_user_iotlb_rd_unlock(vq);
2287 rte_spinlock_unlock(&vq->access_lock);
2289 if (unlikely(rarp_mbuf != NULL)) {
2291 * Inject it to the head of "pkts" array, so that switch's mac
2292 * learning table will get updated first.
2294 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2295 pkts[0] = rarp_mbuf;