1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
24 #define MAX_PKT_BURST 32
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
37 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
40 static __rte_always_inline void
41 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42 struct vhost_virtqueue *vq,
43 uint16_t to, uint16_t from, uint16_t size)
45 rte_memcpy(&vq->used->ring[to],
46 &vq->shadow_used_split[from],
47 size * sizeof(struct vring_used_elem));
48 vhost_log_cache_used_vring(dev, vq,
49 offsetof(struct vring_used, ring[to]),
50 size * sizeof(struct vring_used_elem));
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
56 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
58 if (used_idx + vq->shadow_used_idx <= vq->size) {
59 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
64 /* update used ring interval [used_idx, vq->size] */
65 size = vq->size - used_idx;
66 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
68 /* update the left half used ring interval [0, left_size] */
69 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70 vq->shadow_used_idx - size);
72 vq->last_used_idx += vq->shadow_used_idx;
76 vhost_log_cache_sync(dev, vq);
78 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79 vq->shadow_used_idx = 0;
80 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81 sizeof(vq->used->idx));
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86 uint16_t desc_idx, uint32_t len)
88 uint16_t i = vq->shadow_used_idx++;
90 vq->shadow_used_split[i].id = desc_idx;
91 vq->shadow_used_split[i].len = len;
94 static __rte_always_inline void
95 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
96 struct vhost_virtqueue *vq)
99 uint16_t used_idx = vq->last_used_idx;
100 uint16_t head_idx = vq->last_used_idx;
101 uint16_t head_flags = 0;
103 /* Split loop in two to save memory barriers */
104 for (i = 0; i < vq->shadow_used_idx; i++) {
105 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
108 used_idx += vq->shadow_used_packed[i].count;
109 if (used_idx >= vq->size)
110 used_idx -= vq->size;
115 for (i = 0; i < vq->shadow_used_idx; i++) {
118 if (vq->shadow_used_packed[i].len)
119 flags = VRING_DESC_F_WRITE;
123 if (vq->used_wrap_counter) {
124 flags |= VRING_DESC_F_USED;
125 flags |= VRING_DESC_F_AVAIL;
127 flags &= ~VRING_DESC_F_USED;
128 flags &= ~VRING_DESC_F_AVAIL;
132 vq->desc_packed[vq->last_used_idx].flags = flags;
134 vhost_log_cache_used_vring(dev, vq,
136 sizeof(struct vring_packed_desc),
137 sizeof(struct vring_packed_desc));
139 head_idx = vq->last_used_idx;
143 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
146 vq->desc_packed[head_idx].flags = head_flags;
148 vhost_log_cache_used_vring(dev, vq,
150 sizeof(struct vring_packed_desc),
151 sizeof(struct vring_packed_desc));
153 vq->shadow_used_idx = 0;
154 vhost_log_cache_sync(dev, vq);
157 static __rte_always_inline void
158 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
159 struct vhost_virtqueue *vq)
161 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
163 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
165 vq->desc_packed[vq->shadow_last_used_idx].flags = used_elem->flags;
167 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
168 sizeof(struct vring_packed_desc),
169 sizeof(struct vring_packed_desc));
170 vq->shadow_used_idx = 0;
171 vhost_log_cache_sync(dev, vq);
174 static __rte_always_inline void
175 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
176 struct vhost_virtqueue *vq,
183 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
185 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
186 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
187 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
192 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
193 vq->desc_packed[vq->last_used_idx + i].flags = flags;
195 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
196 sizeof(struct vring_packed_desc),
197 sizeof(struct vring_packed_desc) *
199 vhost_log_cache_sync(dev, vq);
201 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
204 static __rte_always_inline void
205 flush_shadow_used_ring_packed(struct virtio_net *dev,
206 struct vhost_virtqueue *vq)
209 uint16_t used_idx = vq->last_used_idx;
210 uint16_t head_idx = vq->last_used_idx;
211 uint16_t head_flags = 0;
213 /* Split loop in two to save memory barriers */
214 for (i = 0; i < vq->shadow_used_idx; i++) {
215 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
216 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
218 used_idx += vq->shadow_used_packed[i].count;
219 if (used_idx >= vq->size)
220 used_idx -= vq->size;
223 for (i = 0; i < vq->shadow_used_idx; i++) {
226 if (vq->shadow_used_packed[i].len)
227 flags = VRING_DESC_F_WRITE;
231 if (vq->used_wrap_counter) {
232 flags |= VRING_DESC_F_USED;
233 flags |= VRING_DESC_F_AVAIL;
235 flags &= ~VRING_DESC_F_USED;
236 flags &= ~VRING_DESC_F_AVAIL;
240 vq->desc_packed[vq->last_used_idx].flags = flags;
242 vhost_log_cache_used_vring(dev, vq,
244 sizeof(struct vring_packed_desc),
245 sizeof(struct vring_packed_desc));
247 head_idx = vq->last_used_idx;
251 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
254 __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
257 vhost_log_cache_used_vring(dev, vq,
259 sizeof(struct vring_packed_desc),
260 sizeof(struct vring_packed_desc));
262 vq->shadow_used_idx = 0;
263 vhost_log_cache_sync(dev, vq);
266 static __rte_always_inline void
267 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
268 struct vhost_virtqueue *vq,
275 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
277 if (!vq->shadow_used_idx) {
278 vq->shadow_last_used_idx = vq->last_used_idx;
279 vq->shadow_used_packed[0].id = ids[0];
280 vq->shadow_used_packed[0].len = 0;
281 vq->shadow_used_packed[0].count = 1;
282 vq->shadow_used_packed[0].flags = flags;
283 vq->shadow_used_idx++;
288 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
289 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
290 vq->desc_packed[vq->last_used_idx + i].len = 0;
294 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
295 vq->desc_packed[vq->last_used_idx + i].flags = flags;
297 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
298 sizeof(struct vring_packed_desc),
299 sizeof(struct vring_packed_desc) *
301 vhost_log_cache_sync(dev, vq);
303 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
306 static __rte_always_inline void
307 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
313 flags = vq->desc_packed[vq->last_used_idx].flags;
314 if (vq->used_wrap_counter) {
315 flags |= VRING_DESC_F_USED;
316 flags |= VRING_DESC_F_AVAIL;
318 flags &= ~VRING_DESC_F_USED;
319 flags &= ~VRING_DESC_F_AVAIL;
322 if (!vq->shadow_used_idx) {
323 vq->shadow_last_used_idx = vq->last_used_idx;
325 vq->shadow_used_packed[0].id = buf_id;
326 vq->shadow_used_packed[0].len = 0;
327 vq->shadow_used_packed[0].flags = flags;
328 vq->shadow_used_idx++;
330 vq->desc_packed[vq->last_used_idx].id = buf_id;
331 vq->desc_packed[vq->last_used_idx].len = 0;
332 vq->desc_packed[vq->last_used_idx].flags = flags;
335 vq_inc_last_used_packed(vq, count);
338 static __rte_always_inline void
339 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
340 uint16_t desc_idx, uint32_t len, uint16_t count)
342 uint16_t i = vq->shadow_used_idx++;
344 vq->shadow_used_packed[i].id = desc_idx;
345 vq->shadow_used_packed[i].len = len;
346 vq->shadow_used_packed[i].count = count;
350 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
352 struct batch_copy_elem *elem = vq->batch_copy_elems;
353 uint16_t count = vq->batch_copy_nb_elems;
356 for (i = 0; i < count; i++) {
357 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
358 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
360 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
363 vq->batch_copy_nb_elems = 0;
367 do_data_copy_dequeue(struct vhost_virtqueue *vq)
369 struct batch_copy_elem *elem = vq->batch_copy_elems;
370 uint16_t count = vq->batch_copy_nb_elems;
373 for (i = 0; i < count; i++)
374 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
376 vq->batch_copy_nb_elems = 0;
379 static __rte_always_inline void
380 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
381 struct vhost_virtqueue *vq,
385 uint16_t num_buffers)
388 for (i = 0; i < num_buffers; i++) {
389 /* enqueue shadow flush action aligned with batch num */
390 if (!vq->shadow_used_idx)
391 vq->shadow_aligned_idx = vq->last_used_idx &
393 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
394 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
395 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
396 vq->shadow_aligned_idx += count[i];
397 vq->shadow_used_idx++;
400 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
401 do_data_copy_enqueue(dev, vq);
402 vhost_flush_enqueue_shadow_packed(dev, vq);
406 static __rte_unused void
407 vhost_flush_dequeue_packed(struct virtio_net *dev,
408 struct vhost_virtqueue *vq)
411 if (!vq->shadow_used_idx)
414 shadow_count = vq->last_used_idx - vq->shadow_last_used_idx;
415 if (shadow_count <= 0)
416 shadow_count += vq->size;
418 if ((uint32_t)shadow_count >= (vq->size - MAX_PKT_BURST)) {
419 do_data_copy_dequeue(vq);
420 vhost_flush_dequeue_shadow_packed(dev, vq);
421 vhost_vring_call_packed(dev, vq);
425 /* avoid write operation when necessary, to lessen cache issues */
426 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
427 if ((var) != (val)) \
431 static __rte_always_inline void
432 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
434 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
436 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
437 csum_l4 |= PKT_TX_TCP_CKSUM;
440 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
441 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
444 case PKT_TX_TCP_CKSUM:
445 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
448 case PKT_TX_UDP_CKSUM:
449 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
452 case PKT_TX_SCTP_CKSUM:
453 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
458 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
459 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
460 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
463 /* IP cksum verification cannot be bypassed, then calculate here */
464 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
465 struct rte_ipv4_hdr *ipv4_hdr;
467 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
469 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
472 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
473 if (m_buf->ol_flags & PKT_TX_IPV4)
474 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
476 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
477 net_hdr->gso_size = m_buf->tso_segsz;
478 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
480 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
481 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
482 net_hdr->gso_size = m_buf->tso_segsz;
483 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
486 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
487 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
488 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
492 static __rte_always_inline int
493 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
494 struct buf_vector *buf_vec, uint16_t *vec_idx,
495 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
497 uint16_t vec_id = *vec_idx;
501 uint64_t desc_chunck_len = desc_len;
503 if (unlikely(vec_id >= BUF_VECTOR_MAX))
506 desc_addr = vhost_iova_to_vva(dev, vq,
510 if (unlikely(!desc_addr))
513 rte_prefetch0((void *)(uintptr_t)desc_addr);
515 buf_vec[vec_id].buf_iova = desc_iova;
516 buf_vec[vec_id].buf_addr = desc_addr;
517 buf_vec[vec_id].buf_len = desc_chunck_len;
519 desc_len -= desc_chunck_len;
520 desc_iova += desc_chunck_len;
528 static __rte_always_inline int
529 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
530 uint32_t avail_idx, uint16_t *vec_idx,
531 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
532 uint32_t *desc_chain_len, uint8_t perm)
534 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
535 uint16_t vec_id = *vec_idx;
538 uint32_t nr_descs = vq->size;
540 struct vring_desc *descs = vq->desc;
541 struct vring_desc *idesc = NULL;
543 if (unlikely(idx >= vq->size))
546 *desc_chain_head = idx;
548 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
549 dlen = vq->desc[idx].len;
550 nr_descs = dlen / sizeof(struct vring_desc);
551 if (unlikely(nr_descs > vq->size))
554 descs = (struct vring_desc *)(uintptr_t)
555 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
558 if (unlikely(!descs))
561 if (unlikely(dlen < vq->desc[idx].len)) {
563 * The indirect desc table is not contiguous
564 * in process VA space, we have to copy it.
566 idesc = vhost_alloc_copy_ind_table(dev, vq,
567 vq->desc[idx].addr, vq->desc[idx].len);
568 if (unlikely(!idesc))
578 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
579 free_ind_table(idesc);
583 len += descs[idx].len;
585 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
586 descs[idx].addr, descs[idx].len,
588 free_ind_table(idesc);
592 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
595 idx = descs[idx].next;
598 *desc_chain_len = len;
601 if (unlikely(!!idesc))
602 free_ind_table(idesc);
608 * Returns -1 on fail, 0 on success
611 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
612 uint32_t size, struct buf_vector *buf_vec,
613 uint16_t *num_buffers, uint16_t avail_head,
617 uint16_t vec_idx = 0;
618 uint16_t max_tries, tries = 0;
620 uint16_t head_idx = 0;
624 cur_idx = vq->last_avail_idx;
626 if (rxvq_is_mergeable(dev))
627 max_tries = vq->size - 1;
632 if (unlikely(cur_idx == avail_head))
635 * if we tried all available ring items, and still
636 * can't get enough buf, it means something abnormal
639 if (unlikely(++tries > max_tries))
642 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
645 VHOST_ACCESS_RW) < 0))
647 len = RTE_MIN(len, size);
648 update_shadow_used_ring_split(vq, head_idx, len);
660 static __rte_always_inline int
661 fill_vec_buf_packed_indirect(struct virtio_net *dev,
662 struct vhost_virtqueue *vq,
663 struct vring_packed_desc *desc, uint16_t *vec_idx,
664 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
668 uint16_t vec_id = *vec_idx;
670 struct vring_packed_desc *descs, *idescs = NULL;
673 descs = (struct vring_packed_desc *)(uintptr_t)
674 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
675 if (unlikely(!descs))
678 if (unlikely(dlen < desc->len)) {
680 * The indirect desc table is not contiguous
681 * in process VA space, we have to copy it.
683 idescs = vhost_alloc_copy_ind_table(dev,
684 vq, desc->addr, desc->len);
685 if (unlikely(!idescs))
691 nr_descs = desc->len / sizeof(struct vring_packed_desc);
692 if (unlikely(nr_descs >= vq->size)) {
693 free_ind_table(idescs);
697 for (i = 0; i < nr_descs; i++) {
698 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
699 free_ind_table(idescs);
703 *len += descs[i].len;
704 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
705 descs[i].addr, descs[i].len,
711 if (unlikely(!!idescs))
712 free_ind_table(idescs);
717 static __rte_always_inline int
718 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
719 uint16_t avail_idx, uint16_t *desc_count,
720 struct buf_vector *buf_vec, uint16_t *vec_idx,
721 uint16_t *buf_id, uint32_t *len, uint8_t perm)
723 bool wrap_counter = vq->avail_wrap_counter;
724 struct vring_packed_desc *descs = vq->desc_packed;
725 uint16_t vec_id = *vec_idx;
727 if (avail_idx < vq->last_avail_idx)
731 * Perform a load-acquire barrier in desc_is_avail to
732 * enforce the ordering between desc flags and desc
735 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
742 if (unlikely(vec_id >= BUF_VECTOR_MAX))
745 if (unlikely(*desc_count >= vq->size))
749 *buf_id = descs[avail_idx].id;
751 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
752 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
758 *len += descs[avail_idx].len;
760 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
761 descs[avail_idx].addr,
762 descs[avail_idx].len,
767 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
770 if (++avail_idx >= vq->size) {
771 avail_idx -= vq->size;
781 static __rte_noinline void
782 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
783 struct buf_vector *buf_vec,
784 struct virtio_net_hdr_mrg_rxbuf *hdr)
787 uint64_t remain = dev->vhost_hlen;
788 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
789 uint64_t iova = buf_vec->buf_iova;
792 len = RTE_MIN(remain,
794 dst = buf_vec->buf_addr;
795 rte_memcpy((void *)(uintptr_t)dst,
796 (void *)(uintptr_t)src,
799 PRINT_PACKET(dev, (uintptr_t)dst,
801 vhost_log_cache_write_iova(dev, vq,
811 static __rte_always_inline int
812 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
813 struct rte_mbuf *m, struct buf_vector *buf_vec,
814 uint16_t nr_vec, uint16_t num_buffers)
816 uint32_t vec_idx = 0;
817 uint32_t mbuf_offset, mbuf_avail;
818 uint32_t buf_offset, buf_avail;
819 uint64_t buf_addr, buf_iova, buf_len;
822 struct rte_mbuf *hdr_mbuf;
823 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
824 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
827 if (unlikely(m == NULL)) {
832 buf_addr = buf_vec[vec_idx].buf_addr;
833 buf_iova = buf_vec[vec_idx].buf_iova;
834 buf_len = buf_vec[vec_idx].buf_len;
836 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
843 if (unlikely(buf_len < dev->vhost_hlen))
846 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
848 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
849 dev->vid, num_buffers);
851 if (unlikely(buf_len < dev->vhost_hlen)) {
852 buf_offset = dev->vhost_hlen - buf_len;
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;
857 buf_avail = buf_len - buf_offset;
859 buf_offset = dev->vhost_hlen;
860 buf_avail = buf_len - dev->vhost_hlen;
863 mbuf_avail = rte_pktmbuf_data_len(m);
865 while (mbuf_avail != 0 || m->next != NULL) {
866 /* done with current buf, get the next one */
867 if (buf_avail == 0) {
869 if (unlikely(vec_idx >= nr_vec)) {
874 buf_addr = buf_vec[vec_idx].buf_addr;
875 buf_iova = buf_vec[vec_idx].buf_iova;
876 buf_len = buf_vec[vec_idx].buf_len;
882 /* done with current mbuf, get the next one */
883 if (mbuf_avail == 0) {
887 mbuf_avail = rte_pktmbuf_data_len(m);
891 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
892 if (rxvq_is_mergeable(dev))
893 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
896 if (unlikely(hdr == &tmp_hdr)) {
897 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
899 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
901 vhost_log_cache_write_iova(dev, vq,
909 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
911 if (likely(cpy_len > MAX_BATCH_LEN ||
912 vq->batch_copy_nb_elems >= vq->size)) {
913 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
914 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
916 vhost_log_cache_write_iova(dev, vq,
917 buf_iova + buf_offset,
919 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
922 batch_copy[vq->batch_copy_nb_elems].dst =
923 (void *)((uintptr_t)(buf_addr + buf_offset));
924 batch_copy[vq->batch_copy_nb_elems].src =
925 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
926 batch_copy[vq->batch_copy_nb_elems].log_addr =
927 buf_iova + buf_offset;
928 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
929 vq->batch_copy_nb_elems++;
932 mbuf_avail -= cpy_len;
933 mbuf_offset += cpy_len;
934 buf_avail -= cpy_len;
935 buf_offset += cpy_len;
943 static __rte_always_inline int
944 vhost_enqueue_single_packed(struct virtio_net *dev,
945 struct vhost_virtqueue *vq,
946 struct rte_mbuf *pkt,
947 struct buf_vector *buf_vec,
951 uint16_t avail_idx = vq->last_avail_idx;
952 uint16_t max_tries, tries = 0;
956 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
957 uint16_t num_buffers = 0;
958 uint32_t buffer_len[vq->size];
959 uint16_t buffer_buf_id[vq->size];
960 uint16_t buffer_desc_count[vq->size];
962 if (rxvq_is_mergeable(dev))
963 max_tries = vq->size - 1;
969 * if we tried all available ring items, and still
970 * can't get enough buf, it means something abnormal
973 if (unlikely(++tries > max_tries))
976 if (unlikely(fill_vec_buf_packed(dev, vq,
977 avail_idx, &desc_count,
980 VHOST_ACCESS_RW) < 0))
983 len = RTE_MIN(len, size);
986 buffer_len[num_buffers] = len;
987 buffer_buf_id[num_buffers] = buf_id;
988 buffer_desc_count[num_buffers] = desc_count;
991 *nr_descs += desc_count;
992 avail_idx += desc_count;
993 if (avail_idx >= vq->size)
994 avail_idx -= vq->size;
997 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1000 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1001 buffer_desc_count, num_buffers);
1006 static __rte_noinline uint32_t
1007 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1008 struct rte_mbuf **pkts, uint32_t count)
1010 uint32_t pkt_idx = 0;
1011 uint16_t num_buffers;
1012 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1013 uint16_t avail_head;
1015 avail_head = *((volatile uint16_t *)&vq->avail->idx);
1018 * The ordering between avail index and
1019 * desc reads needs to be enforced.
1023 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1025 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1026 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1027 uint16_t nr_vec = 0;
1029 if (unlikely(reserve_avail_buf_split(dev, vq,
1030 pkt_len, buf_vec, &num_buffers,
1031 avail_head, &nr_vec) < 0)) {
1032 VHOST_LOG_DEBUG(VHOST_DATA,
1033 "(%d) failed to get enough desc from vring\n",
1035 vq->shadow_used_idx -= num_buffers;
1039 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1040 dev->vid, vq->last_avail_idx,
1041 vq->last_avail_idx + num_buffers);
1043 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1046 vq->shadow_used_idx -= num_buffers;
1050 vq->last_avail_idx += num_buffers;
1053 do_data_copy_enqueue(dev, vq);
1055 if (likely(vq->shadow_used_idx)) {
1056 flush_shadow_used_ring_split(dev, vq);
1057 vhost_vring_call_split(dev, vq);
1063 static __rte_always_inline int
1064 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1065 struct vhost_virtqueue *vq,
1066 struct rte_mbuf **pkts)
1068 bool wrap_counter = vq->avail_wrap_counter;
1069 struct vring_packed_desc *descs = vq->desc_packed;
1070 uint16_t avail_idx = vq->last_avail_idx;
1071 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1072 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1073 uint32_t buf_offset = dev->vhost_hlen;
1074 uint64_t lens[PACKED_BATCH_SIZE];
1075 uint16_t ids[PACKED_BATCH_SIZE];
1078 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1081 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1084 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1085 if (unlikely(pkts[i]->next != NULL))
1087 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1094 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1095 lens[i] = descs[avail_idx + i].len;
1097 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1098 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1102 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1103 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1104 descs[avail_idx + i].addr,
1108 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1109 if (unlikely(lens[i] != descs[avail_idx + i].len))
1113 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1114 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1115 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1116 (uintptr_t)desc_addrs[i];
1117 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1120 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1121 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1123 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1125 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1126 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1127 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1131 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1132 ids[i] = descs[avail_idx + i].id;
1134 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1139 static __rte_always_inline int16_t
1140 virtio_dev_rx_single_packed(struct virtio_net *dev,
1141 struct vhost_virtqueue *vq,
1142 struct rte_mbuf *pkt)
1144 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1145 uint16_t nr_descs = 0;
1148 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1150 VHOST_LOG_DEBUG(VHOST_DATA,
1151 "(%d) failed to get enough desc from vring\n",
1156 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1157 dev->vid, vq->last_avail_idx,
1158 vq->last_avail_idx + nr_descs);
1160 vq_inc_last_avail_packed(vq, nr_descs);
1165 static __rte_noinline uint32_t
1166 virtio_dev_rx_packed(struct virtio_net *dev,
1167 struct vhost_virtqueue *vq,
1168 struct rte_mbuf **pkts,
1171 uint32_t pkt_idx = 0;
1172 uint32_t remained = count;
1175 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1177 if (remained >= PACKED_BATCH_SIZE) {
1178 if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
1179 pkt_idx += PACKED_BATCH_SIZE;
1180 remained -= PACKED_BATCH_SIZE;
1185 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1190 } while (pkt_idx < count);
1192 if (vq->shadow_used_idx) {
1193 do_data_copy_enqueue(dev, vq);
1194 vhost_flush_enqueue_shadow_packed(dev, vq);
1198 vhost_vring_call_packed(dev, vq);
1203 static __rte_always_inline uint32_t
1204 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1205 struct rte_mbuf **pkts, uint32_t count)
1207 struct vhost_virtqueue *vq;
1210 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1211 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1212 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1213 dev->vid, __func__, queue_id);
1217 vq = dev->virtqueue[queue_id];
1219 rte_spinlock_lock(&vq->access_lock);
1221 if (unlikely(vq->enabled == 0))
1222 goto out_access_unlock;
1224 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1225 vhost_user_iotlb_rd_lock(vq);
1227 if (unlikely(vq->access_ok == 0))
1228 if (unlikely(vring_translate(dev, vq) < 0))
1231 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1235 if (vq_is_packed(dev))
1236 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1238 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1241 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1242 vhost_user_iotlb_rd_unlock(vq);
1245 rte_spinlock_unlock(&vq->access_lock);
1251 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1252 struct rte_mbuf **pkts, uint16_t count)
1254 struct virtio_net *dev = get_device(vid);
1259 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1260 RTE_LOG(ERR, VHOST_DATA,
1261 "(%d) %s: built-in vhost net backend is disabled.\n",
1262 dev->vid, __func__);
1266 return virtio_dev_rx(dev, queue_id, pkts, count);
1270 virtio_net_with_host_offload(struct virtio_net *dev)
1273 ((1ULL << VIRTIO_NET_F_CSUM) |
1274 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1275 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1276 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1277 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1284 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1286 struct rte_ipv4_hdr *ipv4_hdr;
1287 struct rte_ipv6_hdr *ipv6_hdr;
1288 void *l3_hdr = NULL;
1289 struct rte_ether_hdr *eth_hdr;
1292 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1294 m->l2_len = sizeof(struct rte_ether_hdr);
1295 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1297 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1298 struct rte_vlan_hdr *vlan_hdr =
1299 (struct rte_vlan_hdr *)(eth_hdr + 1);
1301 m->l2_len += sizeof(struct rte_vlan_hdr);
1302 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1305 l3_hdr = (char *)eth_hdr + m->l2_len;
1307 switch (ethertype) {
1308 case RTE_ETHER_TYPE_IPV4:
1310 *l4_proto = ipv4_hdr->next_proto_id;
1311 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1312 *l4_hdr = (char *)l3_hdr + m->l3_len;
1313 m->ol_flags |= PKT_TX_IPV4;
1315 case RTE_ETHER_TYPE_IPV6:
1317 *l4_proto = ipv6_hdr->proto;
1318 m->l3_len = sizeof(struct rte_ipv6_hdr);
1319 *l4_hdr = (char *)l3_hdr + m->l3_len;
1320 m->ol_flags |= PKT_TX_IPV6;
1330 static __rte_always_inline void
1331 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1333 uint16_t l4_proto = 0;
1334 void *l4_hdr = NULL;
1335 struct rte_tcp_hdr *tcp_hdr = NULL;
1337 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1340 parse_ethernet(m, &l4_proto, &l4_hdr);
1341 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1342 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1343 switch (hdr->csum_offset) {
1344 case (offsetof(struct rte_tcp_hdr, cksum)):
1345 if (l4_proto == IPPROTO_TCP)
1346 m->ol_flags |= PKT_TX_TCP_CKSUM;
1348 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1349 if (l4_proto == IPPROTO_UDP)
1350 m->ol_flags |= PKT_TX_UDP_CKSUM;
1352 case (offsetof(struct rte_sctp_hdr, cksum)):
1353 if (l4_proto == IPPROTO_SCTP)
1354 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1362 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1363 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1364 case VIRTIO_NET_HDR_GSO_TCPV4:
1365 case VIRTIO_NET_HDR_GSO_TCPV6:
1367 m->ol_flags |= PKT_TX_TCP_SEG;
1368 m->tso_segsz = hdr->gso_size;
1369 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1371 case VIRTIO_NET_HDR_GSO_UDP:
1372 m->ol_flags |= PKT_TX_UDP_SEG;
1373 m->tso_segsz = hdr->gso_size;
1374 m->l4_len = sizeof(struct rte_udp_hdr);
1377 RTE_LOG(WARNING, VHOST_DATA,
1378 "unsupported gso type %u.\n", hdr->gso_type);
1384 static __rte_noinline void
1385 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1386 struct buf_vector *buf_vec)
1389 uint64_t remain = sizeof(struct virtio_net_hdr);
1391 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1394 len = RTE_MIN(remain, buf_vec->buf_len);
1395 src = buf_vec->buf_addr;
1396 rte_memcpy((void *)(uintptr_t)dst,
1397 (void *)(uintptr_t)src, len);
1405 static __rte_always_inline int
1406 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1407 struct buf_vector *buf_vec, uint16_t nr_vec,
1408 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1410 uint32_t buf_avail, buf_offset;
1411 uint64_t buf_addr, buf_iova, buf_len;
1412 uint32_t mbuf_avail, mbuf_offset;
1414 struct rte_mbuf *cur = m, *prev = m;
1415 struct virtio_net_hdr tmp_hdr;
1416 struct virtio_net_hdr *hdr = NULL;
1417 /* A counter to avoid desc dead loop chain */
1418 uint16_t vec_idx = 0;
1419 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1422 buf_addr = buf_vec[vec_idx].buf_addr;
1423 buf_iova = buf_vec[vec_idx].buf_iova;
1424 buf_len = buf_vec[vec_idx].buf_len;
1426 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1431 if (virtio_net_with_host_offload(dev)) {
1432 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1434 * No luck, the virtio-net header doesn't fit
1435 * in a contiguous virtual area.
1437 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1440 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1445 * A virtio driver normally uses at least 2 desc buffers
1446 * for Tx: the first for storing the header, and others
1447 * for storing the data.
1449 if (unlikely(buf_len < dev->vhost_hlen)) {
1450 buf_offset = dev->vhost_hlen - buf_len;
1452 buf_addr = buf_vec[vec_idx].buf_addr;
1453 buf_iova = buf_vec[vec_idx].buf_iova;
1454 buf_len = buf_vec[vec_idx].buf_len;
1455 buf_avail = buf_len - buf_offset;
1456 } else if (buf_len == dev->vhost_hlen) {
1457 if (unlikely(++vec_idx >= nr_vec))
1459 buf_addr = buf_vec[vec_idx].buf_addr;
1460 buf_iova = buf_vec[vec_idx].buf_iova;
1461 buf_len = buf_vec[vec_idx].buf_len;
1464 buf_avail = buf_len;
1466 buf_offset = dev->vhost_hlen;
1467 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1471 (uintptr_t)(buf_addr + buf_offset),
1472 (uint32_t)buf_avail, 0);
1475 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1479 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1482 * A desc buf might across two host physical pages that are
1483 * not continuous. In such case (gpa_to_hpa returns 0), data
1484 * will be copied even though zero copy is enabled.
1486 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1487 buf_iova + buf_offset, cpy_len)))) {
1488 cur->data_len = cpy_len;
1491 (void *)(uintptr_t)(buf_addr + buf_offset);
1492 cur->buf_iova = hpa;
1495 * In zero copy mode, one mbuf can only reference data
1496 * for one or partial of one desc buff.
1498 mbuf_avail = cpy_len;
1500 if (likely(cpy_len > MAX_BATCH_LEN ||
1501 vq->batch_copy_nb_elems >= vq->size ||
1502 (hdr && cur == m))) {
1503 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1505 (void *)((uintptr_t)(buf_addr +
1509 batch_copy[vq->batch_copy_nb_elems].dst =
1510 rte_pktmbuf_mtod_offset(cur, void *,
1512 batch_copy[vq->batch_copy_nb_elems].src =
1513 (void *)((uintptr_t)(buf_addr +
1515 batch_copy[vq->batch_copy_nb_elems].len =
1517 vq->batch_copy_nb_elems++;
1521 mbuf_avail -= cpy_len;
1522 mbuf_offset += cpy_len;
1523 buf_avail -= cpy_len;
1524 buf_offset += cpy_len;
1526 /* This buf reaches to its end, get the next one */
1527 if (buf_avail == 0) {
1528 if (++vec_idx >= nr_vec)
1531 buf_addr = buf_vec[vec_idx].buf_addr;
1532 buf_iova = buf_vec[vec_idx].buf_iova;
1533 buf_len = buf_vec[vec_idx].buf_len;
1536 buf_avail = buf_len;
1538 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1539 (uint32_t)buf_avail, 0);
1543 * This mbuf reaches to its end, get a new one
1544 * to hold more data.
1546 if (mbuf_avail == 0) {
1547 cur = rte_pktmbuf_alloc(mbuf_pool);
1548 if (unlikely(cur == NULL)) {
1549 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1550 "allocate memory for mbuf.\n");
1554 if (unlikely(dev->dequeue_zero_copy))
1555 rte_mbuf_refcnt_update(cur, 1);
1558 prev->data_len = mbuf_offset;
1560 m->pkt_len += mbuf_offset;
1564 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1568 prev->data_len = mbuf_offset;
1569 m->pkt_len += mbuf_offset;
1572 vhost_dequeue_offload(hdr, m);
1579 static __rte_always_inline struct zcopy_mbuf *
1580 get_zmbuf(struct vhost_virtqueue *vq)
1586 /* search [last_zmbuf_idx, zmbuf_size) */
1587 i = vq->last_zmbuf_idx;
1588 last = vq->zmbuf_size;
1591 for (; i < last; i++) {
1592 if (vq->zmbufs[i].in_use == 0) {
1593 vq->last_zmbuf_idx = i + 1;
1594 vq->zmbufs[i].in_use = 1;
1595 return &vq->zmbufs[i];
1601 /* search [0, last_zmbuf_idx) */
1603 last = vq->last_zmbuf_idx;
1611 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1617 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1619 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1620 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1625 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1626 * required, otherwise store shinfo in the new buffer.
1628 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1629 shinfo = rte_pktmbuf_mtod(pkt,
1630 struct rte_mbuf_ext_shared_info *);
1632 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1633 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1636 if (unlikely(total_len > UINT16_MAX))
1639 buf_len = total_len;
1640 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1641 if (unlikely(buf == NULL))
1644 /* Initialize shinfo */
1646 shinfo->free_cb = virtio_dev_extbuf_free;
1647 shinfo->fcb_opaque = buf;
1648 rte_mbuf_ext_refcnt_set(shinfo, 1);
1650 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1651 virtio_dev_extbuf_free, buf);
1652 if (unlikely(shinfo == NULL)) {
1654 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1659 iova = rte_malloc_virt2iova(buf);
1660 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1661 rte_pktmbuf_reset_headroom(pkt);
1667 * Allocate a host supported pktmbuf.
1669 static __rte_always_inline struct rte_mbuf *
1670 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1673 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1675 if (unlikely(pkt == NULL))
1678 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1681 /* attach an external buffer if supported */
1682 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1685 /* check if chained buffers are allowed */
1686 if (!dev->linearbuf)
1689 /* Data doesn't fit into the buffer and the host supports
1690 * only linear buffers
1692 rte_pktmbuf_free(pkt);
1697 static __rte_noinline uint16_t
1698 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1699 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1702 uint16_t free_entries;
1704 if (unlikely(dev->dequeue_zero_copy)) {
1705 struct zcopy_mbuf *zmbuf, *next;
1707 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1708 zmbuf != NULL; zmbuf = next) {
1709 next = TAILQ_NEXT(zmbuf, next);
1711 if (mbuf_is_consumed(zmbuf->mbuf)) {
1712 update_shadow_used_ring_split(vq,
1713 zmbuf->desc_idx, 0);
1714 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1715 restore_mbuf(zmbuf->mbuf);
1716 rte_pktmbuf_free(zmbuf->mbuf);
1722 if (likely(vq->shadow_used_idx)) {
1723 flush_shadow_used_ring_split(dev, vq);
1724 vhost_vring_call_split(dev, vq);
1728 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1730 if (free_entries == 0)
1734 * The ordering between avail index and
1735 * desc reads needs to be enforced.
1739 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1741 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1743 count = RTE_MIN(count, MAX_PKT_BURST);
1744 count = RTE_MIN(count, free_entries);
1745 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1748 for (i = 0; i < count; i++) {
1749 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1752 uint16_t nr_vec = 0;
1755 if (unlikely(fill_vec_buf_split(dev, vq,
1756 vq->last_avail_idx + i,
1758 &head_idx, &buf_len,
1759 VHOST_ACCESS_RO) < 0))
1762 if (likely(dev->dequeue_zero_copy == 0))
1763 update_shadow_used_ring_split(vq, head_idx, 0);
1765 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1766 if (unlikely(pkts[i] == NULL))
1769 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1771 if (unlikely(err)) {
1772 rte_pktmbuf_free(pkts[i]);
1776 if (unlikely(dev->dequeue_zero_copy)) {
1777 struct zcopy_mbuf *zmbuf;
1779 zmbuf = get_zmbuf(vq);
1781 rte_pktmbuf_free(pkts[i]);
1784 zmbuf->mbuf = pkts[i];
1785 zmbuf->desc_idx = head_idx;
1788 * Pin lock the mbuf; we will check later to see
1789 * whether the mbuf is freed (when we are the last
1790 * user) or not. If that's the case, we then could
1791 * update the used ring safely.
1793 rte_mbuf_refcnt_update(pkts[i], 1);
1796 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1799 vq->last_avail_idx += i;
1801 if (likely(dev->dequeue_zero_copy == 0)) {
1802 do_data_copy_dequeue(vq);
1803 if (unlikely(i < count))
1804 vq->shadow_used_idx = i;
1805 if (likely(vq->shadow_used_idx)) {
1806 flush_shadow_used_ring_split(dev, vq);
1807 vhost_vring_call_split(dev, vq);
1814 static __rte_always_inline int
1815 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1816 struct vhost_virtqueue *vq,
1817 struct rte_mempool *mbuf_pool,
1818 struct rte_mbuf **pkts,
1820 uintptr_t *desc_addrs,
1823 bool wrap = vq->avail_wrap_counter;
1824 struct vring_packed_desc *descs = vq->desc_packed;
1825 struct virtio_net_hdr *hdr;
1826 uint64_t lens[PACKED_BATCH_SIZE];
1827 uint64_t buf_lens[PACKED_BATCH_SIZE];
1828 uint32_t buf_offset = dev->vhost_hlen;
1831 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1833 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1836 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1837 flags = descs[avail_idx + i].flags;
1838 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1839 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1840 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1846 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1847 lens[i] = descs[avail_idx + i].len;
1849 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1850 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1851 descs[avail_idx + i].addr,
1852 &lens[i], VHOST_ACCESS_RW);
1855 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1856 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1860 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1861 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1866 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1867 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1869 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1870 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1874 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1875 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1876 pkts[i]->data_len = pkts[i]->pkt_len;
1877 ids[i] = descs[avail_idx + i].id;
1880 if (virtio_net_with_host_offload(dev)) {
1881 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1882 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1883 vhost_dequeue_offload(hdr, pkts[i]);
1890 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1891 rte_pktmbuf_free(pkts[i]);
1896 static __rte_unused int
1897 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1898 struct vhost_virtqueue *vq,
1899 struct rte_mempool *mbuf_pool,
1900 struct rte_mbuf **pkts)
1902 uint16_t avail_idx = vq->last_avail_idx;
1903 uint32_t buf_offset = dev->vhost_hlen;
1904 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1905 uint16_t ids[PACKED_BATCH_SIZE];
1908 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1909 avail_idx, desc_addrs, ids))
1912 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1913 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1915 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1916 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1917 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1920 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
1922 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1927 static __rte_always_inline int
1928 vhost_dequeue_single_packed(struct virtio_net *dev,
1929 struct vhost_virtqueue *vq,
1930 struct rte_mempool *mbuf_pool,
1931 struct rte_mbuf **pkts,
1933 uint16_t *desc_count)
1935 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1937 uint16_t nr_vec = 0;
1940 if (unlikely(fill_vec_buf_packed(dev, vq,
1941 vq->last_avail_idx, desc_count,
1944 VHOST_ACCESS_RO) < 0))
1947 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1948 if (unlikely(*pkts == NULL)) {
1949 RTE_LOG(ERR, VHOST_DATA,
1950 "Failed to allocate memory for mbuf.\n");
1954 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1956 if (unlikely(err)) {
1957 rte_pktmbuf_free(*pkts);
1964 static __rte_unused int
1965 virtio_dev_tx_single_packed(struct virtio_net *dev,
1966 struct vhost_virtqueue *vq,
1967 struct rte_mempool *mbuf_pool,
1968 struct rte_mbuf **pkts)
1971 uint16_t buf_id, desc_count;
1973 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1977 vhost_shadow_dequeue_single_packed(vq, buf_id, desc_count);
1979 vq_inc_last_avail_packed(vq, desc_count);
1984 static __rte_noinline uint16_t
1985 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1986 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1990 if (unlikely(dev->dequeue_zero_copy)) {
1991 struct zcopy_mbuf *zmbuf, *next;
1993 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1994 zmbuf != NULL; zmbuf = next) {
1995 next = TAILQ_NEXT(zmbuf, next);
1997 if (mbuf_is_consumed(zmbuf->mbuf)) {
1998 update_shadow_used_ring_packed(vq,
2003 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2004 restore_mbuf(zmbuf->mbuf);
2005 rte_pktmbuf_free(zmbuf->mbuf);
2011 if (likely(vq->shadow_used_idx)) {
2012 flush_shadow_used_ring_packed(dev, vq);
2013 vhost_vring_call_packed(dev, vq);
2017 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
2019 count = RTE_MIN(count, MAX_PKT_BURST);
2020 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
2023 for (i = 0; i < count; i++) {
2024 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2027 uint16_t desc_count, nr_vec = 0;
2030 if (unlikely(fill_vec_buf_packed(dev, vq,
2031 vq->last_avail_idx, &desc_count,
2034 VHOST_ACCESS_RO) < 0))
2037 if (likely(dev->dequeue_zero_copy == 0))
2038 update_shadow_used_ring_packed(vq, buf_id, 0,
2041 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2042 if (unlikely(pkts[i] == NULL))
2045 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2047 if (unlikely(err)) {
2048 rte_pktmbuf_free(pkts[i]);
2052 if (unlikely(dev->dequeue_zero_copy)) {
2053 struct zcopy_mbuf *zmbuf;
2055 zmbuf = get_zmbuf(vq);
2057 rte_pktmbuf_free(pkts[i]);
2060 zmbuf->mbuf = pkts[i];
2061 zmbuf->desc_idx = buf_id;
2062 zmbuf->desc_count = desc_count;
2065 * Pin lock the mbuf; we will check later to see
2066 * whether the mbuf is freed (when we are the last
2067 * user) or not. If that's the case, we then could
2068 * update the used ring safely.
2070 rte_mbuf_refcnt_update(pkts[i], 1);
2073 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2076 vq_inc_last_avail_packed(vq, desc_count);
2079 if (likely(dev->dequeue_zero_copy == 0)) {
2080 do_data_copy_dequeue(vq);
2081 if (unlikely(i < count))
2082 vq->shadow_used_idx = i;
2083 if (likely(vq->shadow_used_idx)) {
2084 flush_shadow_used_ring_packed(dev, vq);
2085 vhost_vring_call_packed(dev, vq);
2093 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2094 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2096 struct virtio_net *dev;
2097 struct rte_mbuf *rarp_mbuf = NULL;
2098 struct vhost_virtqueue *vq;
2100 dev = get_device(vid);
2104 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2105 RTE_LOG(ERR, VHOST_DATA,
2106 "(%d) %s: built-in vhost net backend is disabled.\n",
2107 dev->vid, __func__);
2111 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2112 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
2113 dev->vid, __func__, queue_id);
2117 vq = dev->virtqueue[queue_id];
2119 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2122 if (unlikely(vq->enabled == 0)) {
2124 goto out_access_unlock;
2127 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2128 vhost_user_iotlb_rd_lock(vq);
2130 if (unlikely(vq->access_ok == 0))
2131 if (unlikely(vring_translate(dev, vq) < 0)) {
2137 * Construct a RARP broadcast packet, and inject it to the "pkts"
2138 * array, to looks like that guest actually send such packet.
2140 * Check user_send_rarp() for more information.
2142 * broadcast_rarp shares a cacheline in the virtio_net structure
2143 * with some fields that are accessed during enqueue and
2144 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2145 * result in false sharing between enqueue and dequeue.
2147 * Prevent unnecessary false sharing by reading broadcast_rarp first
2148 * and only performing cmpset if the read indicates it is likely to
2151 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2152 rte_atomic16_cmpset((volatile uint16_t *)
2153 &dev->broadcast_rarp.cnt, 1, 0))) {
2155 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2156 if (rarp_mbuf == NULL) {
2157 RTE_LOG(ERR, VHOST_DATA,
2158 "Failed to make RARP packet.\n");
2165 if (vq_is_packed(dev))
2166 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
2168 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2171 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2172 vhost_user_iotlb_rd_unlock(vq);
2175 rte_spinlock_unlock(&vq->access_lock);
2177 if (unlikely(rarp_mbuf != NULL)) {
2179 * Inject it to the head of "pkts" array, so that switch's mac
2180 * learning table will get updated first.
2182 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2183 pkts[0] = rarp_mbuf;