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_DEBUG(VHOST_DATA, "(%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_DEBUG(VHOST_DATA,
1013 "(%d) failed to get enough desc from vring\n",
1015 vq->shadow_used_idx -= num_buffers;
1019 VHOST_LOG_DEBUG(VHOST_DATA, "(%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_DEBUG(VHOST_DATA,
1135 "(%d) failed to get enough desc from vring\n",
1140 VHOST_LOG_DEBUG(VHOST_DATA, "(%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, pkts)) {
1163 pkt_idx += PACKED_BATCH_SIZE;
1164 remained -= PACKED_BATCH_SIZE;
1169 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1174 } while (pkt_idx < count);
1176 if (vq->shadow_used_idx) {
1177 do_data_copy_enqueue(dev, vq);
1178 vhost_flush_enqueue_shadow_packed(dev, vq);
1182 vhost_vring_call_packed(dev, vq);
1187 static __rte_always_inline uint32_t
1188 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1189 struct rte_mbuf **pkts, uint32_t count)
1191 struct vhost_virtqueue *vq;
1194 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1195 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1196 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1197 dev->vid, __func__, queue_id);
1201 vq = dev->virtqueue[queue_id];
1203 rte_spinlock_lock(&vq->access_lock);
1205 if (unlikely(vq->enabled == 0))
1206 goto out_access_unlock;
1208 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1209 vhost_user_iotlb_rd_lock(vq);
1211 if (unlikely(vq->access_ok == 0))
1212 if (unlikely(vring_translate(dev, vq) < 0))
1215 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1219 if (vq_is_packed(dev))
1220 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1222 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1225 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1226 vhost_user_iotlb_rd_unlock(vq);
1229 rte_spinlock_unlock(&vq->access_lock);
1235 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1236 struct rte_mbuf **pkts, uint16_t count)
1238 struct virtio_net *dev = get_device(vid);
1243 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1244 RTE_LOG(ERR, VHOST_DATA,
1245 "(%d) %s: built-in vhost net backend is disabled.\n",
1246 dev->vid, __func__);
1250 return virtio_dev_rx(dev, queue_id, pkts, count);
1254 virtio_net_with_host_offload(struct virtio_net *dev)
1257 ((1ULL << VIRTIO_NET_F_CSUM) |
1258 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1259 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1260 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1261 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1268 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1270 struct rte_ipv4_hdr *ipv4_hdr;
1271 struct rte_ipv6_hdr *ipv6_hdr;
1272 void *l3_hdr = NULL;
1273 struct rte_ether_hdr *eth_hdr;
1276 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1278 m->l2_len = sizeof(struct rte_ether_hdr);
1279 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1281 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1282 struct rte_vlan_hdr *vlan_hdr =
1283 (struct rte_vlan_hdr *)(eth_hdr + 1);
1285 m->l2_len += sizeof(struct rte_vlan_hdr);
1286 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1289 l3_hdr = (char *)eth_hdr + m->l2_len;
1291 switch (ethertype) {
1292 case RTE_ETHER_TYPE_IPV4:
1294 *l4_proto = ipv4_hdr->next_proto_id;
1295 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1296 *l4_hdr = (char *)l3_hdr + m->l3_len;
1297 m->ol_flags |= PKT_TX_IPV4;
1299 case RTE_ETHER_TYPE_IPV6:
1301 *l4_proto = ipv6_hdr->proto;
1302 m->l3_len = sizeof(struct rte_ipv6_hdr);
1303 *l4_hdr = (char *)l3_hdr + m->l3_len;
1304 m->ol_flags |= PKT_TX_IPV6;
1314 static __rte_always_inline void
1315 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1317 uint16_t l4_proto = 0;
1318 void *l4_hdr = NULL;
1319 struct rte_tcp_hdr *tcp_hdr = NULL;
1321 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1324 parse_ethernet(m, &l4_proto, &l4_hdr);
1325 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1326 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1327 switch (hdr->csum_offset) {
1328 case (offsetof(struct rte_tcp_hdr, cksum)):
1329 if (l4_proto == IPPROTO_TCP)
1330 m->ol_flags |= PKT_TX_TCP_CKSUM;
1332 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1333 if (l4_proto == IPPROTO_UDP)
1334 m->ol_flags |= PKT_TX_UDP_CKSUM;
1336 case (offsetof(struct rte_sctp_hdr, cksum)):
1337 if (l4_proto == IPPROTO_SCTP)
1338 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1346 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1347 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1348 case VIRTIO_NET_HDR_GSO_TCPV4:
1349 case VIRTIO_NET_HDR_GSO_TCPV6:
1351 m->ol_flags |= PKT_TX_TCP_SEG;
1352 m->tso_segsz = hdr->gso_size;
1353 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1355 case VIRTIO_NET_HDR_GSO_UDP:
1356 m->ol_flags |= PKT_TX_UDP_SEG;
1357 m->tso_segsz = hdr->gso_size;
1358 m->l4_len = sizeof(struct rte_udp_hdr);
1361 RTE_LOG(WARNING, VHOST_DATA,
1362 "unsupported gso type %u.\n", hdr->gso_type);
1368 static __rte_noinline void
1369 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1370 struct buf_vector *buf_vec)
1373 uint64_t remain = sizeof(struct virtio_net_hdr);
1375 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1378 len = RTE_MIN(remain, buf_vec->buf_len);
1379 src = buf_vec->buf_addr;
1380 rte_memcpy((void *)(uintptr_t)dst,
1381 (void *)(uintptr_t)src, len);
1389 static __rte_always_inline int
1390 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1391 struct buf_vector *buf_vec, uint16_t nr_vec,
1392 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1394 uint32_t buf_avail, buf_offset;
1395 uint64_t buf_addr, buf_iova, buf_len;
1396 uint32_t mbuf_avail, mbuf_offset;
1398 struct rte_mbuf *cur = m, *prev = m;
1399 struct virtio_net_hdr tmp_hdr;
1400 struct virtio_net_hdr *hdr = NULL;
1401 /* A counter to avoid desc dead loop chain */
1402 uint16_t vec_idx = 0;
1403 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1406 buf_addr = buf_vec[vec_idx].buf_addr;
1407 buf_iova = buf_vec[vec_idx].buf_iova;
1408 buf_len = buf_vec[vec_idx].buf_len;
1410 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1415 if (virtio_net_with_host_offload(dev)) {
1416 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1418 * No luck, the virtio-net header doesn't fit
1419 * in a contiguous virtual area.
1421 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1424 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1429 * A virtio driver normally uses at least 2 desc buffers
1430 * for Tx: the first for storing the header, and others
1431 * for storing the data.
1433 if (unlikely(buf_len < dev->vhost_hlen)) {
1434 buf_offset = dev->vhost_hlen - buf_len;
1436 buf_addr = buf_vec[vec_idx].buf_addr;
1437 buf_iova = buf_vec[vec_idx].buf_iova;
1438 buf_len = buf_vec[vec_idx].buf_len;
1439 buf_avail = buf_len - buf_offset;
1440 } else if (buf_len == dev->vhost_hlen) {
1441 if (unlikely(++vec_idx >= nr_vec))
1443 buf_addr = buf_vec[vec_idx].buf_addr;
1444 buf_iova = buf_vec[vec_idx].buf_iova;
1445 buf_len = buf_vec[vec_idx].buf_len;
1448 buf_avail = buf_len;
1450 buf_offset = dev->vhost_hlen;
1451 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1455 (uintptr_t)(buf_addr + buf_offset),
1456 (uint32_t)buf_avail, 0);
1459 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1463 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1466 * A desc buf might across two host physical pages that are
1467 * not continuous. In such case (gpa_to_hpa returns 0), data
1468 * will be copied even though zero copy is enabled.
1470 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1471 buf_iova + buf_offset, cpy_len)))) {
1472 cur->data_len = cpy_len;
1475 (void *)(uintptr_t)(buf_addr + buf_offset);
1476 cur->buf_iova = hpa;
1479 * In zero copy mode, one mbuf can only reference data
1480 * for one or partial of one desc buff.
1482 mbuf_avail = cpy_len;
1484 if (likely(cpy_len > MAX_BATCH_LEN ||
1485 vq->batch_copy_nb_elems >= vq->size ||
1486 (hdr && cur == m))) {
1487 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1489 (void *)((uintptr_t)(buf_addr +
1493 batch_copy[vq->batch_copy_nb_elems].dst =
1494 rte_pktmbuf_mtod_offset(cur, void *,
1496 batch_copy[vq->batch_copy_nb_elems].src =
1497 (void *)((uintptr_t)(buf_addr +
1499 batch_copy[vq->batch_copy_nb_elems].len =
1501 vq->batch_copy_nb_elems++;
1505 mbuf_avail -= cpy_len;
1506 mbuf_offset += cpy_len;
1507 buf_avail -= cpy_len;
1508 buf_offset += cpy_len;
1510 /* This buf reaches to its end, get the next one */
1511 if (buf_avail == 0) {
1512 if (++vec_idx >= nr_vec)
1515 buf_addr = buf_vec[vec_idx].buf_addr;
1516 buf_iova = buf_vec[vec_idx].buf_iova;
1517 buf_len = buf_vec[vec_idx].buf_len;
1520 buf_avail = buf_len;
1522 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1523 (uint32_t)buf_avail, 0);
1527 * This mbuf reaches to its end, get a new one
1528 * to hold more data.
1530 if (mbuf_avail == 0) {
1531 cur = rte_pktmbuf_alloc(mbuf_pool);
1532 if (unlikely(cur == NULL)) {
1533 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1534 "allocate memory for mbuf.\n");
1538 if (unlikely(dev->dequeue_zero_copy))
1539 rte_mbuf_refcnt_update(cur, 1);
1542 prev->data_len = mbuf_offset;
1544 m->pkt_len += mbuf_offset;
1548 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1552 prev->data_len = mbuf_offset;
1553 m->pkt_len += mbuf_offset;
1556 vhost_dequeue_offload(hdr, m);
1563 static __rte_always_inline struct zcopy_mbuf *
1564 get_zmbuf(struct vhost_virtqueue *vq)
1570 /* search [last_zmbuf_idx, zmbuf_size) */
1571 i = vq->last_zmbuf_idx;
1572 last = vq->zmbuf_size;
1575 for (; i < last; i++) {
1576 if (vq->zmbufs[i].in_use == 0) {
1577 vq->last_zmbuf_idx = i + 1;
1578 vq->zmbufs[i].in_use = 1;
1579 return &vq->zmbufs[i];
1585 /* search [0, last_zmbuf_idx) */
1587 last = vq->last_zmbuf_idx;
1595 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1601 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1603 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1604 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1609 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1610 * required, otherwise store shinfo in the new buffer.
1612 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1613 shinfo = rte_pktmbuf_mtod(pkt,
1614 struct rte_mbuf_ext_shared_info *);
1616 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1617 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1620 if (unlikely(total_len > UINT16_MAX))
1623 buf_len = total_len;
1624 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1625 if (unlikely(buf == NULL))
1628 /* Initialize shinfo */
1630 shinfo->free_cb = virtio_dev_extbuf_free;
1631 shinfo->fcb_opaque = buf;
1632 rte_mbuf_ext_refcnt_set(shinfo, 1);
1634 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1635 virtio_dev_extbuf_free, buf);
1636 if (unlikely(shinfo == NULL)) {
1638 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1643 iova = rte_malloc_virt2iova(buf);
1644 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1645 rte_pktmbuf_reset_headroom(pkt);
1651 * Allocate a host supported pktmbuf.
1653 static __rte_always_inline struct rte_mbuf *
1654 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1657 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1659 if (unlikely(pkt == NULL)) {
1660 RTE_LOG(ERR, VHOST_DATA,
1661 "Failed to allocate memory for mbuf.\n");
1665 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1668 /* attach an external buffer if supported */
1669 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1672 /* check if chained buffers are allowed */
1673 if (!dev->linearbuf)
1676 /* Data doesn't fit into the buffer and the host supports
1677 * only linear buffers
1679 rte_pktmbuf_free(pkt);
1684 static __rte_noinline uint16_t
1685 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1686 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1689 uint16_t free_entries;
1691 if (unlikely(dev->dequeue_zero_copy)) {
1692 struct zcopy_mbuf *zmbuf, *next;
1694 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1695 zmbuf != NULL; zmbuf = next) {
1696 next = TAILQ_NEXT(zmbuf, next);
1698 if (mbuf_is_consumed(zmbuf->mbuf)) {
1699 update_shadow_used_ring_split(vq,
1700 zmbuf->desc_idx, 0);
1701 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1702 restore_mbuf(zmbuf->mbuf);
1703 rte_pktmbuf_free(zmbuf->mbuf);
1709 if (likely(vq->shadow_used_idx)) {
1710 flush_shadow_used_ring_split(dev, vq);
1711 vhost_vring_call_split(dev, vq);
1715 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1717 if (free_entries == 0)
1721 * The ordering between avail index and
1722 * desc reads needs to be enforced.
1726 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1728 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1730 count = RTE_MIN(count, MAX_PKT_BURST);
1731 count = RTE_MIN(count, free_entries);
1732 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1735 for (i = 0; i < count; i++) {
1736 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1739 uint16_t nr_vec = 0;
1742 if (unlikely(fill_vec_buf_split(dev, vq,
1743 vq->last_avail_idx + i,
1745 &head_idx, &buf_len,
1746 VHOST_ACCESS_RO) < 0))
1749 if (likely(dev->dequeue_zero_copy == 0))
1750 update_shadow_used_ring_split(vq, head_idx, 0);
1752 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1753 if (unlikely(pkts[i] == NULL))
1756 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1758 if (unlikely(err)) {
1759 rte_pktmbuf_free(pkts[i]);
1763 if (unlikely(dev->dequeue_zero_copy)) {
1764 struct zcopy_mbuf *zmbuf;
1766 zmbuf = get_zmbuf(vq);
1768 rte_pktmbuf_free(pkts[i]);
1771 zmbuf->mbuf = pkts[i];
1772 zmbuf->desc_idx = head_idx;
1775 * Pin lock the mbuf; we will check later to see
1776 * whether the mbuf is freed (when we are the last
1777 * user) or not. If that's the case, we then could
1778 * update the used ring safely.
1780 rte_mbuf_refcnt_update(pkts[i], 1);
1783 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1786 vq->last_avail_idx += i;
1788 if (likely(dev->dequeue_zero_copy == 0)) {
1789 do_data_copy_dequeue(vq);
1790 if (unlikely(i < count))
1791 vq->shadow_used_idx = i;
1792 if (likely(vq->shadow_used_idx)) {
1793 flush_shadow_used_ring_split(dev, vq);
1794 vhost_vring_call_split(dev, vq);
1801 static __rte_always_inline int
1802 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1803 struct vhost_virtqueue *vq,
1804 struct rte_mempool *mbuf_pool,
1805 struct rte_mbuf **pkts,
1807 uintptr_t *desc_addrs,
1810 bool wrap = vq->avail_wrap_counter;
1811 struct vring_packed_desc *descs = vq->desc_packed;
1812 struct virtio_net_hdr *hdr;
1813 uint64_t lens[PACKED_BATCH_SIZE];
1814 uint64_t buf_lens[PACKED_BATCH_SIZE];
1815 uint32_t buf_offset = dev->vhost_hlen;
1818 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1820 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1823 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1824 flags = descs[avail_idx + i].flags;
1825 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1826 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1827 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1833 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1834 lens[i] = descs[avail_idx + i].len;
1836 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1837 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1838 descs[avail_idx + i].addr,
1839 &lens[i], VHOST_ACCESS_RW);
1842 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1843 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1847 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1848 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1853 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1854 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1856 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1857 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1861 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1862 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1863 pkts[i]->data_len = pkts[i]->pkt_len;
1864 ids[i] = descs[avail_idx + i].id;
1867 if (virtio_net_with_host_offload(dev)) {
1868 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1869 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1870 vhost_dequeue_offload(hdr, pkts[i]);
1877 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1878 rte_pktmbuf_free(pkts[i]);
1883 static __rte_always_inline int
1884 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1885 struct vhost_virtqueue *vq,
1886 struct rte_mempool *mbuf_pool,
1887 struct rte_mbuf **pkts)
1889 uint16_t avail_idx = vq->last_avail_idx;
1890 uint32_t buf_offset = dev->vhost_hlen;
1891 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1892 uint16_t ids[PACKED_BATCH_SIZE];
1895 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1896 avail_idx, desc_addrs, ids))
1899 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1900 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1902 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1903 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1904 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1907 if (virtio_net_is_inorder(dev))
1908 vhost_shadow_dequeue_batch_packed_inorder(vq,
1909 ids[PACKED_BATCH_SIZE - 1]);
1911 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
1913 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1918 static __rte_always_inline int
1919 vhost_dequeue_single_packed(struct virtio_net *dev,
1920 struct vhost_virtqueue *vq,
1921 struct rte_mempool *mbuf_pool,
1922 struct rte_mbuf **pkts,
1924 uint16_t *desc_count)
1926 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1928 uint16_t nr_vec = 0;
1931 if (unlikely(fill_vec_buf_packed(dev, vq,
1932 vq->last_avail_idx, desc_count,
1935 VHOST_ACCESS_RO) < 0))
1938 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1939 if (unlikely(*pkts == NULL)) {
1940 RTE_LOG(ERR, VHOST_DATA,
1941 "Failed to allocate memory for mbuf.\n");
1945 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1947 if (unlikely(err)) {
1948 rte_pktmbuf_free(*pkts);
1955 static __rte_always_inline int
1956 virtio_dev_tx_single_packed(struct virtio_net *dev,
1957 struct vhost_virtqueue *vq,
1958 struct rte_mempool *mbuf_pool,
1959 struct rte_mbuf **pkts)
1962 uint16_t buf_id, desc_count;
1964 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1968 if (virtio_net_is_inorder(dev))
1969 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
1972 vhost_shadow_dequeue_single_packed(vq, buf_id, desc_count);
1974 vq_inc_last_avail_packed(vq, desc_count);
1979 static __rte_always_inline int
1980 virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,
1981 struct vhost_virtqueue *vq,
1982 struct rte_mempool *mbuf_pool,
1983 struct rte_mbuf **pkts)
1985 struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE];
1986 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1987 uint16_t ids[PACKED_BATCH_SIZE];
1990 uint16_t avail_idx = vq->last_avail_idx;
1992 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1993 avail_idx, desc_addrs, ids))
1996 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1997 zmbufs[i] = get_zmbuf(vq);
1999 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2004 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2005 zmbufs[i]->mbuf = pkts[i];
2006 zmbufs[i]->desc_idx = avail_idx + i;
2007 zmbufs[i]->desc_count = 1;
2010 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2011 rte_mbuf_refcnt_update(pkts[i], 1);
2013 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2014 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbufs[i], next);
2016 vq->nr_zmbuf += PACKED_BATCH_SIZE;
2017 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2022 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2023 rte_pktmbuf_free(pkts[i]);
2028 static __rte_always_inline int
2029 virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev,
2030 struct vhost_virtqueue *vq,
2031 struct rte_mempool *mbuf_pool,
2032 struct rte_mbuf **pkts)
2034 uint16_t buf_id, desc_count;
2035 struct zcopy_mbuf *zmbuf;
2037 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2041 zmbuf = get_zmbuf(vq);
2043 rte_pktmbuf_free(*pkts);
2046 zmbuf->mbuf = *pkts;
2047 zmbuf->desc_idx = vq->last_avail_idx;
2048 zmbuf->desc_count = desc_count;
2050 rte_mbuf_refcnt_update(*pkts, 1);
2053 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2055 vq_inc_last_avail_packed(vq, desc_count);
2059 static __rte_always_inline void
2060 free_zmbuf(struct vhost_virtqueue *vq)
2062 struct zcopy_mbuf *next = NULL;
2063 struct zcopy_mbuf *zmbuf;
2065 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
2066 zmbuf != NULL; zmbuf = next) {
2067 next = TAILQ_NEXT(zmbuf, next);
2069 uint16_t last_used_idx = vq->last_used_idx;
2071 if (mbuf_is_consumed(zmbuf->mbuf)) {
2073 flags = vq->desc_packed[last_used_idx].flags;
2074 if (vq->used_wrap_counter) {
2075 flags |= VRING_DESC_F_USED;
2076 flags |= VRING_DESC_F_AVAIL;
2078 flags &= ~VRING_DESC_F_USED;
2079 flags &= ~VRING_DESC_F_AVAIL;
2082 vq->desc_packed[last_used_idx].id = zmbuf->desc_idx;
2083 vq->desc_packed[last_used_idx].len = 0;
2086 vq->desc_packed[last_used_idx].flags = flags;
2088 vq_inc_last_used_packed(vq, zmbuf->desc_count);
2090 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2091 restore_mbuf(zmbuf->mbuf);
2092 rte_pktmbuf_free(zmbuf->mbuf);
2099 static __rte_noinline uint16_t
2100 virtio_dev_tx_packed_zmbuf(struct virtio_net *dev,
2101 struct vhost_virtqueue *vq,
2102 struct rte_mempool *mbuf_pool,
2103 struct rte_mbuf **pkts,
2106 uint32_t pkt_idx = 0;
2107 uint32_t remained = count;
2112 if (remained >= PACKED_BATCH_SIZE) {
2113 if (!virtio_dev_tx_batch_packed_zmbuf(dev, vq,
2114 mbuf_pool, &pkts[pkt_idx])) {
2115 pkt_idx += PACKED_BATCH_SIZE;
2116 remained -= PACKED_BATCH_SIZE;
2121 if (virtio_dev_tx_single_packed_zmbuf(dev, vq, mbuf_pool,
2130 vhost_vring_call_packed(dev, vq);
2135 static __rte_noinline uint16_t
2136 virtio_dev_tx_packed(struct virtio_net *dev,
2137 struct vhost_virtqueue *vq,
2138 struct rte_mempool *mbuf_pool,
2139 struct rte_mbuf **pkts,
2142 uint32_t pkt_idx = 0;
2143 uint32_t remained = count;
2146 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2148 if (remained >= PACKED_BATCH_SIZE) {
2149 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2151 vhost_flush_dequeue_packed(dev, vq);
2152 pkt_idx += PACKED_BATCH_SIZE;
2153 remained -= PACKED_BATCH_SIZE;
2158 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2161 vhost_flush_dequeue_packed(dev, vq);
2167 if (vq->shadow_used_idx)
2168 do_data_copy_dequeue(vq);
2174 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2175 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2177 struct virtio_net *dev;
2178 struct rte_mbuf *rarp_mbuf = NULL;
2179 struct vhost_virtqueue *vq;
2181 dev = get_device(vid);
2185 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2186 RTE_LOG(ERR, VHOST_DATA,
2187 "(%d) %s: built-in vhost net backend is disabled.\n",
2188 dev->vid, __func__);
2192 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2193 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
2194 dev->vid, __func__, queue_id);
2198 vq = dev->virtqueue[queue_id];
2200 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2203 if (unlikely(vq->enabled == 0)) {
2205 goto out_access_unlock;
2208 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2209 vhost_user_iotlb_rd_lock(vq);
2211 if (unlikely(vq->access_ok == 0))
2212 if (unlikely(vring_translate(dev, vq) < 0)) {
2218 * Construct a RARP broadcast packet, and inject it to the "pkts"
2219 * array, to looks like that guest actually send such packet.
2221 * Check user_send_rarp() for more information.
2223 * broadcast_rarp shares a cacheline in the virtio_net structure
2224 * with some fields that are accessed during enqueue and
2225 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2226 * result in false sharing between enqueue and dequeue.
2228 * Prevent unnecessary false sharing by reading broadcast_rarp first
2229 * and only performing cmpset if the read indicates it is likely to
2232 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2233 rte_atomic16_cmpset((volatile uint16_t *)
2234 &dev->broadcast_rarp.cnt, 1, 0))) {
2236 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2237 if (rarp_mbuf == NULL) {
2238 RTE_LOG(ERR, VHOST_DATA,
2239 "Failed to make RARP packet.\n");
2246 if (vq_is_packed(dev)) {
2247 if (unlikely(dev->dequeue_zero_copy))
2248 count = virtio_dev_tx_packed_zmbuf(dev, vq, mbuf_pool,
2251 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts,
2254 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2257 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2258 vhost_user_iotlb_rd_unlock(vq);
2261 rte_spinlock_unlock(&vq->access_lock);
2263 if (unlikely(rarp_mbuf != NULL)) {
2265 * Inject it to the head of "pkts" array, so that switch's mac
2266 * learning table will get updated first.
2268 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2269 pkts[0] = rarp_mbuf;