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 = rte_ipv4_cksum(ipv4_hdr);
451 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
452 if (m_buf->ol_flags & PKT_TX_IPV4)
453 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
455 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
456 net_hdr->gso_size = m_buf->tso_segsz;
457 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
459 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
460 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
461 net_hdr->gso_size = m_buf->tso_segsz;
462 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
465 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
466 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
467 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
471 static __rte_always_inline int
472 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
473 struct buf_vector *buf_vec, uint16_t *vec_idx,
474 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
476 uint16_t vec_id = *vec_idx;
480 uint64_t desc_chunck_len = desc_len;
482 if (unlikely(vec_id >= BUF_VECTOR_MAX))
485 desc_addr = vhost_iova_to_vva(dev, vq,
489 if (unlikely(!desc_addr))
492 rte_prefetch0((void *)(uintptr_t)desc_addr);
494 buf_vec[vec_id].buf_iova = desc_iova;
495 buf_vec[vec_id].buf_addr = desc_addr;
496 buf_vec[vec_id].buf_len = desc_chunck_len;
498 desc_len -= desc_chunck_len;
499 desc_iova += desc_chunck_len;
507 static __rte_always_inline int
508 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
509 uint32_t avail_idx, uint16_t *vec_idx,
510 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
511 uint32_t *desc_chain_len, uint8_t perm)
513 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
514 uint16_t vec_id = *vec_idx;
517 uint32_t nr_descs = vq->size;
519 struct vring_desc *descs = vq->desc;
520 struct vring_desc *idesc = NULL;
522 if (unlikely(idx >= vq->size))
525 *desc_chain_head = idx;
527 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
528 dlen = vq->desc[idx].len;
529 nr_descs = dlen / sizeof(struct vring_desc);
530 if (unlikely(nr_descs > vq->size))
533 descs = (struct vring_desc *)(uintptr_t)
534 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
537 if (unlikely(!descs))
540 if (unlikely(dlen < vq->desc[idx].len)) {
542 * The indirect desc table is not contiguous
543 * in process VA space, we have to copy it.
545 idesc = vhost_alloc_copy_ind_table(dev, vq,
546 vq->desc[idx].addr, vq->desc[idx].len);
547 if (unlikely(!idesc))
557 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
558 free_ind_table(idesc);
562 len += descs[idx].len;
564 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
565 descs[idx].addr, descs[idx].len,
567 free_ind_table(idesc);
571 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
574 idx = descs[idx].next;
577 *desc_chain_len = len;
580 if (unlikely(!!idesc))
581 free_ind_table(idesc);
587 * Returns -1 on fail, 0 on success
590 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
591 uint32_t size, struct buf_vector *buf_vec,
592 uint16_t *num_buffers, uint16_t avail_head,
596 uint16_t vec_idx = 0;
597 uint16_t max_tries, tries = 0;
599 uint16_t head_idx = 0;
603 cur_idx = vq->last_avail_idx;
605 if (rxvq_is_mergeable(dev))
606 max_tries = vq->size - 1;
611 if (unlikely(cur_idx == avail_head))
614 * if we tried all available ring items, and still
615 * can't get enough buf, it means something abnormal
618 if (unlikely(++tries > max_tries))
621 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
624 VHOST_ACCESS_RW) < 0))
626 len = RTE_MIN(len, size);
627 update_shadow_used_ring_split(vq, head_idx, len);
639 static __rte_always_inline int
640 fill_vec_buf_packed_indirect(struct virtio_net *dev,
641 struct vhost_virtqueue *vq,
642 struct vring_packed_desc *desc, uint16_t *vec_idx,
643 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
647 uint16_t vec_id = *vec_idx;
649 struct vring_packed_desc *descs, *idescs = NULL;
652 descs = (struct vring_packed_desc *)(uintptr_t)
653 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
654 if (unlikely(!descs))
657 if (unlikely(dlen < desc->len)) {
659 * The indirect desc table is not contiguous
660 * in process VA space, we have to copy it.
662 idescs = vhost_alloc_copy_ind_table(dev,
663 vq, desc->addr, desc->len);
664 if (unlikely(!idescs))
670 nr_descs = desc->len / sizeof(struct vring_packed_desc);
671 if (unlikely(nr_descs >= vq->size)) {
672 free_ind_table(idescs);
676 for (i = 0; i < nr_descs; i++) {
677 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
678 free_ind_table(idescs);
682 *len += descs[i].len;
683 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
684 descs[i].addr, descs[i].len,
690 if (unlikely(!!idescs))
691 free_ind_table(idescs);
696 static __rte_always_inline int
697 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
698 uint16_t avail_idx, uint16_t *desc_count,
699 struct buf_vector *buf_vec, uint16_t *vec_idx,
700 uint16_t *buf_id, uint32_t *len, uint8_t perm)
702 bool wrap_counter = vq->avail_wrap_counter;
703 struct vring_packed_desc *descs = vq->desc_packed;
704 uint16_t vec_id = *vec_idx;
706 if (avail_idx < vq->last_avail_idx)
710 * Perform a load-acquire barrier in desc_is_avail to
711 * enforce the ordering between desc flags and desc
714 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
721 if (unlikely(vec_id >= BUF_VECTOR_MAX))
724 if (unlikely(*desc_count >= vq->size))
728 *buf_id = descs[avail_idx].id;
730 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
731 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
737 *len += descs[avail_idx].len;
739 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
740 descs[avail_idx].addr,
741 descs[avail_idx].len,
746 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
749 if (++avail_idx >= vq->size) {
750 avail_idx -= vq->size;
760 static __rte_noinline void
761 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
762 struct buf_vector *buf_vec,
763 struct virtio_net_hdr_mrg_rxbuf *hdr)
766 uint64_t remain = dev->vhost_hlen;
767 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
768 uint64_t iova = buf_vec->buf_iova;
771 len = RTE_MIN(remain,
773 dst = buf_vec->buf_addr;
774 rte_memcpy((void *)(uintptr_t)dst,
775 (void *)(uintptr_t)src,
778 PRINT_PACKET(dev, (uintptr_t)dst,
780 vhost_log_cache_write_iova(dev, vq,
790 static __rte_always_inline int
791 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
792 struct rte_mbuf *m, struct buf_vector *buf_vec,
793 uint16_t nr_vec, uint16_t num_buffers)
795 uint32_t vec_idx = 0;
796 uint32_t mbuf_offset, mbuf_avail;
797 uint32_t buf_offset, buf_avail;
798 uint64_t buf_addr, buf_iova, buf_len;
801 struct rte_mbuf *hdr_mbuf;
802 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
803 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
806 if (unlikely(m == NULL)) {
811 buf_addr = buf_vec[vec_idx].buf_addr;
812 buf_iova = buf_vec[vec_idx].buf_iova;
813 buf_len = buf_vec[vec_idx].buf_len;
815 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
822 if (unlikely(buf_len < dev->vhost_hlen))
825 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
827 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
828 dev->vid, num_buffers);
830 if (unlikely(buf_len < dev->vhost_hlen)) {
831 buf_offset = dev->vhost_hlen - buf_len;
833 buf_addr = buf_vec[vec_idx].buf_addr;
834 buf_iova = buf_vec[vec_idx].buf_iova;
835 buf_len = buf_vec[vec_idx].buf_len;
836 buf_avail = buf_len - buf_offset;
838 buf_offset = dev->vhost_hlen;
839 buf_avail = buf_len - dev->vhost_hlen;
842 mbuf_avail = rte_pktmbuf_data_len(m);
844 while (mbuf_avail != 0 || m->next != NULL) {
845 /* done with current buf, get the next one */
846 if (buf_avail == 0) {
848 if (unlikely(vec_idx >= nr_vec)) {
853 buf_addr = buf_vec[vec_idx].buf_addr;
854 buf_iova = buf_vec[vec_idx].buf_iova;
855 buf_len = buf_vec[vec_idx].buf_len;
861 /* done with current mbuf, get the next one */
862 if (mbuf_avail == 0) {
866 mbuf_avail = rte_pktmbuf_data_len(m);
870 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
871 if (rxvq_is_mergeable(dev))
872 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
875 if (unlikely(hdr == &tmp_hdr)) {
876 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
878 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
880 vhost_log_cache_write_iova(dev, vq,
888 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
890 if (likely(cpy_len > MAX_BATCH_LEN ||
891 vq->batch_copy_nb_elems >= vq->size)) {
892 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
893 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
895 vhost_log_cache_write_iova(dev, vq,
896 buf_iova + buf_offset,
898 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
901 batch_copy[vq->batch_copy_nb_elems].dst =
902 (void *)((uintptr_t)(buf_addr + buf_offset));
903 batch_copy[vq->batch_copy_nb_elems].src =
904 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
905 batch_copy[vq->batch_copy_nb_elems].log_addr =
906 buf_iova + buf_offset;
907 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
908 vq->batch_copy_nb_elems++;
911 mbuf_avail -= cpy_len;
912 mbuf_offset += cpy_len;
913 buf_avail -= cpy_len;
914 buf_offset += cpy_len;
922 static __rte_always_inline int
923 vhost_enqueue_single_packed(struct virtio_net *dev,
924 struct vhost_virtqueue *vq,
925 struct rte_mbuf *pkt,
926 struct buf_vector *buf_vec,
930 uint16_t avail_idx = vq->last_avail_idx;
931 uint16_t max_tries, tries = 0;
935 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
936 uint16_t num_buffers = 0;
937 uint32_t buffer_len[vq->size];
938 uint16_t buffer_buf_id[vq->size];
939 uint16_t buffer_desc_count[vq->size];
941 if (rxvq_is_mergeable(dev))
942 max_tries = vq->size - 1;
948 * if we tried all available ring items, and still
949 * can't get enough buf, it means something abnormal
952 if (unlikely(++tries > max_tries))
955 if (unlikely(fill_vec_buf_packed(dev, vq,
956 avail_idx, &desc_count,
959 VHOST_ACCESS_RW) < 0))
962 len = RTE_MIN(len, size);
965 buffer_len[num_buffers] = len;
966 buffer_buf_id[num_buffers] = buf_id;
967 buffer_desc_count[num_buffers] = desc_count;
970 *nr_descs += desc_count;
971 avail_idx += desc_count;
972 if (avail_idx >= vq->size)
973 avail_idx -= vq->size;
976 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
979 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
980 buffer_desc_count, num_buffers);
985 static __rte_noinline uint32_t
986 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
987 struct rte_mbuf **pkts, uint32_t count)
989 uint32_t pkt_idx = 0;
990 uint16_t num_buffers;
991 struct buf_vector buf_vec[BUF_VECTOR_MAX];
994 avail_head = *((volatile uint16_t *)&vq->avail->idx);
997 * The ordering between avail index and
998 * desc reads needs to be enforced.
1002 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1004 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1005 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1006 uint16_t nr_vec = 0;
1008 if (unlikely(reserve_avail_buf_split(dev, vq,
1009 pkt_len, buf_vec, &num_buffers,
1010 avail_head, &nr_vec) < 0)) {
1011 VHOST_LOG_DEBUG(VHOST_DATA,
1012 "(%d) failed to get enough desc from vring\n",
1014 vq->shadow_used_idx -= num_buffers;
1018 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1019 dev->vid, vq->last_avail_idx,
1020 vq->last_avail_idx + num_buffers);
1022 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1025 vq->shadow_used_idx -= num_buffers;
1029 vq->last_avail_idx += num_buffers;
1032 do_data_copy_enqueue(dev, vq);
1034 if (likely(vq->shadow_used_idx)) {
1035 flush_shadow_used_ring_split(dev, vq);
1036 vhost_vring_call_split(dev, vq);
1042 static __rte_always_inline int
1043 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1044 struct vhost_virtqueue *vq,
1045 struct rte_mbuf **pkts)
1047 bool wrap_counter = vq->avail_wrap_counter;
1048 struct vring_packed_desc *descs = vq->desc_packed;
1049 uint16_t avail_idx = vq->last_avail_idx;
1050 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1051 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1052 uint32_t buf_offset = dev->vhost_hlen;
1053 uint64_t lens[PACKED_BATCH_SIZE];
1054 uint16_t ids[PACKED_BATCH_SIZE];
1057 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1060 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1063 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1064 if (unlikely(pkts[i]->next != NULL))
1066 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1073 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1074 lens[i] = descs[avail_idx + i].len;
1076 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1077 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1081 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1082 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1083 descs[avail_idx + i].addr,
1087 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1088 if (unlikely(lens[i] != descs[avail_idx + i].len))
1092 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1093 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1094 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1095 (uintptr_t)desc_addrs[i];
1096 lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
1099 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1100 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1102 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1104 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1105 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1106 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1110 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1111 ids[i] = descs[avail_idx + i].id;
1113 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1118 static __rte_always_inline int16_t
1119 virtio_dev_rx_single_packed(struct virtio_net *dev,
1120 struct vhost_virtqueue *vq,
1121 struct rte_mbuf *pkt)
1123 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1124 uint16_t nr_descs = 0;
1127 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1129 VHOST_LOG_DEBUG(VHOST_DATA,
1130 "(%d) failed to get enough desc from vring\n",
1135 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
1136 dev->vid, vq->last_avail_idx,
1137 vq->last_avail_idx + nr_descs);
1139 vq_inc_last_avail_packed(vq, nr_descs);
1144 static __rte_noinline uint32_t
1145 virtio_dev_rx_packed(struct virtio_net *dev,
1146 struct vhost_virtqueue *vq,
1147 struct rte_mbuf **pkts,
1150 uint32_t pkt_idx = 0;
1151 uint32_t remained = count;
1154 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1156 if (remained >= PACKED_BATCH_SIZE) {
1157 if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
1158 pkt_idx += PACKED_BATCH_SIZE;
1159 remained -= PACKED_BATCH_SIZE;
1164 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1169 } while (pkt_idx < count);
1171 if (vq->shadow_used_idx) {
1172 do_data_copy_enqueue(dev, vq);
1173 vhost_flush_enqueue_shadow_packed(dev, vq);
1177 vhost_vring_call_packed(dev, vq);
1182 static __rte_always_inline uint32_t
1183 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1184 struct rte_mbuf **pkts, uint32_t count)
1186 struct vhost_virtqueue *vq;
1189 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1190 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1191 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1192 dev->vid, __func__, queue_id);
1196 vq = dev->virtqueue[queue_id];
1198 rte_spinlock_lock(&vq->access_lock);
1200 if (unlikely(vq->enabled == 0))
1201 goto out_access_unlock;
1203 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1204 vhost_user_iotlb_rd_lock(vq);
1206 if (unlikely(vq->access_ok == 0))
1207 if (unlikely(vring_translate(dev, vq) < 0))
1210 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1214 if (vq_is_packed(dev))
1215 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1217 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1220 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1221 vhost_user_iotlb_rd_unlock(vq);
1224 rte_spinlock_unlock(&vq->access_lock);
1230 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1231 struct rte_mbuf **pkts, uint16_t count)
1233 struct virtio_net *dev = get_device(vid);
1238 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1239 RTE_LOG(ERR, VHOST_DATA,
1240 "(%d) %s: built-in vhost net backend is disabled.\n",
1241 dev->vid, __func__);
1245 return virtio_dev_rx(dev, queue_id, pkts, count);
1249 virtio_net_with_host_offload(struct virtio_net *dev)
1252 ((1ULL << VIRTIO_NET_F_CSUM) |
1253 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1254 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1255 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1256 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1263 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1265 struct rte_ipv4_hdr *ipv4_hdr;
1266 struct rte_ipv6_hdr *ipv6_hdr;
1267 void *l3_hdr = NULL;
1268 struct rte_ether_hdr *eth_hdr;
1271 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1273 m->l2_len = sizeof(struct rte_ether_hdr);
1274 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1276 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1277 struct rte_vlan_hdr *vlan_hdr =
1278 (struct rte_vlan_hdr *)(eth_hdr + 1);
1280 m->l2_len += sizeof(struct rte_vlan_hdr);
1281 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1284 l3_hdr = (char *)eth_hdr + m->l2_len;
1286 switch (ethertype) {
1287 case RTE_ETHER_TYPE_IPV4:
1289 *l4_proto = ipv4_hdr->next_proto_id;
1290 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1291 *l4_hdr = (char *)l3_hdr + m->l3_len;
1292 m->ol_flags |= PKT_TX_IPV4;
1294 case RTE_ETHER_TYPE_IPV6:
1296 *l4_proto = ipv6_hdr->proto;
1297 m->l3_len = sizeof(struct rte_ipv6_hdr);
1298 *l4_hdr = (char *)l3_hdr + m->l3_len;
1299 m->ol_flags |= PKT_TX_IPV6;
1309 static __rte_always_inline void
1310 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1312 uint16_t l4_proto = 0;
1313 void *l4_hdr = NULL;
1314 struct rte_tcp_hdr *tcp_hdr = NULL;
1316 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1319 parse_ethernet(m, &l4_proto, &l4_hdr);
1320 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1321 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1322 switch (hdr->csum_offset) {
1323 case (offsetof(struct rte_tcp_hdr, cksum)):
1324 if (l4_proto == IPPROTO_TCP)
1325 m->ol_flags |= PKT_TX_TCP_CKSUM;
1327 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1328 if (l4_proto == IPPROTO_UDP)
1329 m->ol_flags |= PKT_TX_UDP_CKSUM;
1331 case (offsetof(struct rte_sctp_hdr, cksum)):
1332 if (l4_proto == IPPROTO_SCTP)
1333 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1341 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1342 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1343 case VIRTIO_NET_HDR_GSO_TCPV4:
1344 case VIRTIO_NET_HDR_GSO_TCPV6:
1346 m->ol_flags |= PKT_TX_TCP_SEG;
1347 m->tso_segsz = hdr->gso_size;
1348 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1350 case VIRTIO_NET_HDR_GSO_UDP:
1351 m->ol_flags |= PKT_TX_UDP_SEG;
1352 m->tso_segsz = hdr->gso_size;
1353 m->l4_len = sizeof(struct rte_udp_hdr);
1356 RTE_LOG(WARNING, VHOST_DATA,
1357 "unsupported gso type %u.\n", hdr->gso_type);
1363 static __rte_noinline void
1364 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1365 struct buf_vector *buf_vec)
1368 uint64_t remain = sizeof(struct virtio_net_hdr);
1370 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1373 len = RTE_MIN(remain, buf_vec->buf_len);
1374 src = buf_vec->buf_addr;
1375 rte_memcpy((void *)(uintptr_t)dst,
1376 (void *)(uintptr_t)src, len);
1384 static __rte_always_inline int
1385 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1386 struct buf_vector *buf_vec, uint16_t nr_vec,
1387 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1389 uint32_t buf_avail, buf_offset;
1390 uint64_t buf_addr, buf_iova, buf_len;
1391 uint32_t mbuf_avail, mbuf_offset;
1393 struct rte_mbuf *cur = m, *prev = m;
1394 struct virtio_net_hdr tmp_hdr;
1395 struct virtio_net_hdr *hdr = NULL;
1396 /* A counter to avoid desc dead loop chain */
1397 uint16_t vec_idx = 0;
1398 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1401 buf_addr = buf_vec[vec_idx].buf_addr;
1402 buf_iova = buf_vec[vec_idx].buf_iova;
1403 buf_len = buf_vec[vec_idx].buf_len;
1405 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1410 if (virtio_net_with_host_offload(dev)) {
1411 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1413 * No luck, the virtio-net header doesn't fit
1414 * in a contiguous virtual area.
1416 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1419 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1424 * A virtio driver normally uses at least 2 desc buffers
1425 * for Tx: the first for storing the header, and others
1426 * for storing the data.
1428 if (unlikely(buf_len < dev->vhost_hlen)) {
1429 buf_offset = dev->vhost_hlen - buf_len;
1431 buf_addr = buf_vec[vec_idx].buf_addr;
1432 buf_iova = buf_vec[vec_idx].buf_iova;
1433 buf_len = buf_vec[vec_idx].buf_len;
1434 buf_avail = buf_len - buf_offset;
1435 } else if (buf_len == dev->vhost_hlen) {
1436 if (unlikely(++vec_idx >= nr_vec))
1438 buf_addr = buf_vec[vec_idx].buf_addr;
1439 buf_iova = buf_vec[vec_idx].buf_iova;
1440 buf_len = buf_vec[vec_idx].buf_len;
1443 buf_avail = buf_len;
1445 buf_offset = dev->vhost_hlen;
1446 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1450 (uintptr_t)(buf_addr + buf_offset),
1451 (uint32_t)buf_avail, 0);
1454 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1458 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1461 * A desc buf might across two host physical pages that are
1462 * not continuous. In such case (gpa_to_hpa returns 0), data
1463 * will be copied even though zero copy is enabled.
1465 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1466 buf_iova + buf_offset, cpy_len)))) {
1467 cur->data_len = cpy_len;
1470 (void *)(uintptr_t)(buf_addr + buf_offset);
1471 cur->buf_iova = hpa;
1474 * In zero copy mode, one mbuf can only reference data
1475 * for one or partial of one desc buff.
1477 mbuf_avail = cpy_len;
1479 if (likely(cpy_len > MAX_BATCH_LEN ||
1480 vq->batch_copy_nb_elems >= vq->size ||
1481 (hdr && cur == m))) {
1482 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1484 (void *)((uintptr_t)(buf_addr +
1488 batch_copy[vq->batch_copy_nb_elems].dst =
1489 rte_pktmbuf_mtod_offset(cur, void *,
1491 batch_copy[vq->batch_copy_nb_elems].src =
1492 (void *)((uintptr_t)(buf_addr +
1494 batch_copy[vq->batch_copy_nb_elems].len =
1496 vq->batch_copy_nb_elems++;
1500 mbuf_avail -= cpy_len;
1501 mbuf_offset += cpy_len;
1502 buf_avail -= cpy_len;
1503 buf_offset += cpy_len;
1505 /* This buf reaches to its end, get the next one */
1506 if (buf_avail == 0) {
1507 if (++vec_idx >= nr_vec)
1510 buf_addr = buf_vec[vec_idx].buf_addr;
1511 buf_iova = buf_vec[vec_idx].buf_iova;
1512 buf_len = buf_vec[vec_idx].buf_len;
1515 buf_avail = buf_len;
1517 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1518 (uint32_t)buf_avail, 0);
1522 * This mbuf reaches to its end, get a new one
1523 * to hold more data.
1525 if (mbuf_avail == 0) {
1526 cur = rte_pktmbuf_alloc(mbuf_pool);
1527 if (unlikely(cur == NULL)) {
1528 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1529 "allocate memory for mbuf.\n");
1533 if (unlikely(dev->dequeue_zero_copy))
1534 rte_mbuf_refcnt_update(cur, 1);
1537 prev->data_len = mbuf_offset;
1539 m->pkt_len += mbuf_offset;
1543 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1547 prev->data_len = mbuf_offset;
1548 m->pkt_len += mbuf_offset;
1551 vhost_dequeue_offload(hdr, m);
1558 static __rte_always_inline struct zcopy_mbuf *
1559 get_zmbuf(struct vhost_virtqueue *vq)
1565 /* search [last_zmbuf_idx, zmbuf_size) */
1566 i = vq->last_zmbuf_idx;
1567 last = vq->zmbuf_size;
1570 for (; i < last; i++) {
1571 if (vq->zmbufs[i].in_use == 0) {
1572 vq->last_zmbuf_idx = i + 1;
1573 vq->zmbufs[i].in_use = 1;
1574 return &vq->zmbufs[i];
1580 /* search [0, last_zmbuf_idx) */
1582 last = vq->last_zmbuf_idx;
1590 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1596 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1598 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1599 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1604 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1605 * required, otherwise store shinfo in the new buffer.
1607 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1608 shinfo = rte_pktmbuf_mtod(pkt,
1609 struct rte_mbuf_ext_shared_info *);
1611 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1612 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1615 if (unlikely(total_len > UINT16_MAX))
1618 buf_len = total_len;
1619 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1620 if (unlikely(buf == NULL))
1623 /* Initialize shinfo */
1625 shinfo->free_cb = virtio_dev_extbuf_free;
1626 shinfo->fcb_opaque = buf;
1627 rte_mbuf_ext_refcnt_set(shinfo, 1);
1629 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1630 virtio_dev_extbuf_free, buf);
1631 if (unlikely(shinfo == NULL)) {
1633 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1638 iova = rte_malloc_virt2iova(buf);
1639 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1640 rte_pktmbuf_reset_headroom(pkt);
1646 * Allocate a host supported pktmbuf.
1648 static __rte_always_inline struct rte_mbuf *
1649 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1652 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1654 if (unlikely(pkt == NULL)) {
1655 RTE_LOG(ERR, VHOST_DATA,
1656 "Failed to allocate memory for mbuf.\n");
1660 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1663 /* attach an external buffer if supported */
1664 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1667 /* check if chained buffers are allowed */
1668 if (!dev->linearbuf)
1671 /* Data doesn't fit into the buffer and the host supports
1672 * only linear buffers
1674 rte_pktmbuf_free(pkt);
1679 static __rte_noinline uint16_t
1680 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1681 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1684 uint16_t free_entries;
1686 if (unlikely(dev->dequeue_zero_copy)) {
1687 struct zcopy_mbuf *zmbuf, *next;
1689 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1690 zmbuf != NULL; zmbuf = next) {
1691 next = TAILQ_NEXT(zmbuf, next);
1693 if (mbuf_is_consumed(zmbuf->mbuf)) {
1694 update_shadow_used_ring_split(vq,
1695 zmbuf->desc_idx, 0);
1696 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1697 restore_mbuf(zmbuf->mbuf);
1698 rte_pktmbuf_free(zmbuf->mbuf);
1704 if (likely(vq->shadow_used_idx)) {
1705 flush_shadow_used_ring_split(dev, vq);
1706 vhost_vring_call_split(dev, vq);
1710 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1712 if (free_entries == 0)
1716 * The ordering between avail index and
1717 * desc reads needs to be enforced.
1721 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1723 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1725 count = RTE_MIN(count, MAX_PKT_BURST);
1726 count = RTE_MIN(count, free_entries);
1727 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1730 for (i = 0; i < count; i++) {
1731 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1734 uint16_t nr_vec = 0;
1737 if (unlikely(fill_vec_buf_split(dev, vq,
1738 vq->last_avail_idx + i,
1740 &head_idx, &buf_len,
1741 VHOST_ACCESS_RO) < 0))
1744 if (likely(dev->dequeue_zero_copy == 0))
1745 update_shadow_used_ring_split(vq, head_idx, 0);
1747 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1748 if (unlikely(pkts[i] == NULL))
1751 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1753 if (unlikely(err)) {
1754 rte_pktmbuf_free(pkts[i]);
1758 if (unlikely(dev->dequeue_zero_copy)) {
1759 struct zcopy_mbuf *zmbuf;
1761 zmbuf = get_zmbuf(vq);
1763 rte_pktmbuf_free(pkts[i]);
1766 zmbuf->mbuf = pkts[i];
1767 zmbuf->desc_idx = head_idx;
1770 * Pin lock the mbuf; we will check later to see
1771 * whether the mbuf is freed (when we are the last
1772 * user) or not. If that's the case, we then could
1773 * update the used ring safely.
1775 rte_mbuf_refcnt_update(pkts[i], 1);
1778 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1781 vq->last_avail_idx += i;
1783 if (likely(dev->dequeue_zero_copy == 0)) {
1784 do_data_copy_dequeue(vq);
1785 if (unlikely(i < count))
1786 vq->shadow_used_idx = i;
1787 if (likely(vq->shadow_used_idx)) {
1788 flush_shadow_used_ring_split(dev, vq);
1789 vhost_vring_call_split(dev, vq);
1796 static __rte_always_inline int
1797 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
1798 struct vhost_virtqueue *vq,
1799 struct rte_mempool *mbuf_pool,
1800 struct rte_mbuf **pkts,
1802 uintptr_t *desc_addrs,
1805 bool wrap = vq->avail_wrap_counter;
1806 struct vring_packed_desc *descs = vq->desc_packed;
1807 struct virtio_net_hdr *hdr;
1808 uint64_t lens[PACKED_BATCH_SIZE];
1809 uint64_t buf_lens[PACKED_BATCH_SIZE];
1810 uint32_t buf_offset = dev->vhost_hlen;
1813 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1815 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1818 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1819 flags = descs[avail_idx + i].flags;
1820 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
1821 (wrap == !!(flags & VRING_DESC_F_USED)) ||
1822 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
1828 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1829 lens[i] = descs[avail_idx + i].len;
1831 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1832 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1833 descs[avail_idx + i].addr,
1834 &lens[i], VHOST_ACCESS_RW);
1837 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1838 if (unlikely((lens[i] != descs[avail_idx + i].len)))
1842 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1843 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
1848 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1849 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
1851 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1852 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
1856 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1857 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
1858 pkts[i]->data_len = pkts[i]->pkt_len;
1859 ids[i] = descs[avail_idx + i].id;
1862 if (virtio_net_with_host_offload(dev)) {
1863 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1864 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
1865 vhost_dequeue_offload(hdr, pkts[i]);
1872 for (i = 0; i < PACKED_BATCH_SIZE; i++)
1873 rte_pktmbuf_free(pkts[i]);
1878 static __rte_always_inline int
1879 virtio_dev_tx_batch_packed(struct virtio_net *dev,
1880 struct vhost_virtqueue *vq,
1881 struct rte_mempool *mbuf_pool,
1882 struct rte_mbuf **pkts)
1884 uint16_t avail_idx = vq->last_avail_idx;
1885 uint32_t buf_offset = dev->vhost_hlen;
1886 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1887 uint16_t ids[PACKED_BATCH_SIZE];
1890 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1891 avail_idx, desc_addrs, ids))
1894 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1895 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1897 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1898 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1899 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1902 if (virtio_net_is_inorder(dev))
1903 vhost_shadow_dequeue_batch_packed_inorder(vq,
1904 ids[PACKED_BATCH_SIZE - 1]);
1906 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
1908 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1913 static __rte_always_inline int
1914 vhost_dequeue_single_packed(struct virtio_net *dev,
1915 struct vhost_virtqueue *vq,
1916 struct rte_mempool *mbuf_pool,
1917 struct rte_mbuf **pkts,
1919 uint16_t *desc_count)
1921 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1923 uint16_t nr_vec = 0;
1926 if (unlikely(fill_vec_buf_packed(dev, vq,
1927 vq->last_avail_idx, desc_count,
1930 VHOST_ACCESS_RO) < 0))
1933 *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1934 if (unlikely(*pkts == NULL)) {
1935 RTE_LOG(ERR, VHOST_DATA,
1936 "Failed to allocate memory for mbuf.\n");
1940 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
1942 if (unlikely(err)) {
1943 rte_pktmbuf_free(*pkts);
1950 static __rte_always_inline int
1951 virtio_dev_tx_single_packed(struct virtio_net *dev,
1952 struct vhost_virtqueue *vq,
1953 struct rte_mempool *mbuf_pool,
1954 struct rte_mbuf **pkts)
1957 uint16_t buf_id, desc_count;
1959 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
1963 if (virtio_net_is_inorder(dev))
1964 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
1967 vhost_shadow_dequeue_single_packed(vq, buf_id, desc_count);
1969 vq_inc_last_avail_packed(vq, desc_count);
1974 static __rte_always_inline int
1975 virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,
1976 struct vhost_virtqueue *vq,
1977 struct rte_mempool *mbuf_pool,
1978 struct rte_mbuf **pkts)
1980 struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE];
1981 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
1982 uint16_t ids[PACKED_BATCH_SIZE];
1985 uint16_t avail_idx = vq->last_avail_idx;
1987 if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
1988 avail_idx, desc_addrs, ids))
1991 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1992 zmbufs[i] = get_zmbuf(vq);
1994 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1999 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2000 zmbufs[i]->mbuf = pkts[i];
2001 zmbufs[i]->desc_idx = avail_idx + i;
2002 zmbufs[i]->desc_count = 1;
2005 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2006 rte_mbuf_refcnt_update(pkts[i], 1);
2008 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2009 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbufs[i], next);
2011 vq->nr_zmbuf += PACKED_BATCH_SIZE;
2012 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2017 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2018 rte_pktmbuf_free(pkts[i]);
2023 static __rte_always_inline int
2024 virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev,
2025 struct vhost_virtqueue *vq,
2026 struct rte_mempool *mbuf_pool,
2027 struct rte_mbuf **pkts)
2029 uint16_t buf_id, desc_count;
2030 struct zcopy_mbuf *zmbuf;
2032 if (vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2036 zmbuf = get_zmbuf(vq);
2038 rte_pktmbuf_free(*pkts);
2041 zmbuf->mbuf = *pkts;
2042 zmbuf->desc_idx = vq->last_avail_idx;
2043 zmbuf->desc_count = desc_count;
2045 rte_mbuf_refcnt_update(*pkts, 1);
2048 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
2050 vq_inc_last_avail_packed(vq, desc_count);
2054 static __rte_always_inline void
2055 free_zmbuf(struct vhost_virtqueue *vq)
2057 struct zcopy_mbuf *next = NULL;
2058 struct zcopy_mbuf *zmbuf;
2060 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
2061 zmbuf != NULL; zmbuf = next) {
2062 next = TAILQ_NEXT(zmbuf, next);
2064 uint16_t last_used_idx = vq->last_used_idx;
2066 if (mbuf_is_consumed(zmbuf->mbuf)) {
2068 flags = vq->desc_packed[last_used_idx].flags;
2069 if (vq->used_wrap_counter) {
2070 flags |= VRING_DESC_F_USED;
2071 flags |= VRING_DESC_F_AVAIL;
2073 flags &= ~VRING_DESC_F_USED;
2074 flags &= ~VRING_DESC_F_AVAIL;
2077 vq->desc_packed[last_used_idx].id = zmbuf->desc_idx;
2078 vq->desc_packed[last_used_idx].len = 0;
2081 vq->desc_packed[last_used_idx].flags = flags;
2083 vq_inc_last_used_packed(vq, zmbuf->desc_count);
2085 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
2086 restore_mbuf(zmbuf->mbuf);
2087 rte_pktmbuf_free(zmbuf->mbuf);
2094 static __rte_noinline uint16_t
2095 virtio_dev_tx_packed_zmbuf(struct virtio_net *dev,
2096 struct vhost_virtqueue *vq,
2097 struct rte_mempool *mbuf_pool,
2098 struct rte_mbuf **pkts,
2101 uint32_t pkt_idx = 0;
2102 uint32_t remained = count;
2107 if (remained >= PACKED_BATCH_SIZE) {
2108 if (!virtio_dev_tx_batch_packed_zmbuf(dev, vq,
2109 mbuf_pool, &pkts[pkt_idx])) {
2110 pkt_idx += PACKED_BATCH_SIZE;
2111 remained -= PACKED_BATCH_SIZE;
2116 if (virtio_dev_tx_single_packed_zmbuf(dev, vq, mbuf_pool,
2125 vhost_vring_call_packed(dev, vq);
2130 static __rte_noinline uint16_t
2131 virtio_dev_tx_packed(struct virtio_net *dev,
2132 struct vhost_virtqueue *vq,
2133 struct rte_mempool *mbuf_pool,
2134 struct rte_mbuf **pkts,
2137 uint32_t pkt_idx = 0;
2138 uint32_t remained = count;
2141 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2143 if (remained >= PACKED_BATCH_SIZE) {
2144 if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2146 vhost_flush_dequeue_packed(dev, vq);
2147 pkt_idx += PACKED_BATCH_SIZE;
2148 remained -= PACKED_BATCH_SIZE;
2153 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2156 vhost_flush_dequeue_packed(dev, vq);
2162 if (vq->shadow_used_idx)
2163 do_data_copy_dequeue(vq);
2169 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2170 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2172 struct virtio_net *dev;
2173 struct rte_mbuf *rarp_mbuf = NULL;
2174 struct vhost_virtqueue *vq;
2176 dev = get_device(vid);
2180 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2181 RTE_LOG(ERR, VHOST_DATA,
2182 "(%d) %s: built-in vhost net backend is disabled.\n",
2183 dev->vid, __func__);
2187 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2188 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
2189 dev->vid, __func__, queue_id);
2193 vq = dev->virtqueue[queue_id];
2195 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2198 if (unlikely(vq->enabled == 0)) {
2200 goto out_access_unlock;
2203 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2204 vhost_user_iotlb_rd_lock(vq);
2206 if (unlikely(vq->access_ok == 0))
2207 if (unlikely(vring_translate(dev, vq) < 0)) {
2213 * Construct a RARP broadcast packet, and inject it to the "pkts"
2214 * array, to looks like that guest actually send such packet.
2216 * Check user_send_rarp() for more information.
2218 * broadcast_rarp shares a cacheline in the virtio_net structure
2219 * with some fields that are accessed during enqueue and
2220 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
2221 * result in false sharing between enqueue and dequeue.
2223 * Prevent unnecessary false sharing by reading broadcast_rarp first
2224 * and only performing cmpset if the read indicates it is likely to
2227 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
2228 rte_atomic16_cmpset((volatile uint16_t *)
2229 &dev->broadcast_rarp.cnt, 1, 0))) {
2231 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2232 if (rarp_mbuf == NULL) {
2233 RTE_LOG(ERR, VHOST_DATA,
2234 "Failed to make RARP packet.\n");
2241 if (vq_is_packed(dev)) {
2242 if (unlikely(dev->dequeue_zero_copy))
2243 count = virtio_dev_tx_packed_zmbuf(dev, vq, mbuf_pool,
2246 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts,
2249 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2252 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2253 vhost_user_iotlb_rd_unlock(vq);
2256 rte_spinlock_unlock(&vq->access_lock);
2258 if (unlikely(rarp_mbuf != NULL)) {
2260 * Inject it to the head of "pkts" array, so that switch's mac
2261 * learning table will get updated first.
2263 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2264 pkts[0] = rarp_mbuf;