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>
12 #include <rte_ether.h>
14 #include <rte_dmadev.h>
15 #include <rte_vhost.h>
20 #include <rte_spinlock.h>
21 #include <rte_malloc.h>
22 #include <rte_vhost_async.h>
27 #define MAX_BATCH_LEN 256
29 /* DMA device copy operation tracking array. */
30 struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
32 static __rte_always_inline bool
33 rxvq_is_mergeable(struct virtio_net *dev)
35 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
38 static __rte_always_inline bool
39 virtio_net_is_inorder(struct virtio_net *dev)
41 return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
45 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
47 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
50 static __rte_always_inline int64_t
51 vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,
52 int16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,
53 struct vhost_iov_iter *pkt)
55 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
56 uint16_t ring_mask = dma_info->ring_mask;
57 static bool vhost_async_dma_copy_log;
60 struct vhost_iovec *iov = pkt->iov;
62 uint32_t nr_segs = pkt->nr_segs;
65 if (rte_dma_burst_capacity(dma_id, vchan_id) < nr_segs)
68 for (i = 0; i < nr_segs; i++) {
69 copy_idx = rte_dma_copy(dma_id, vchan_id, (rte_iova_t)iov[i].src_addr,
70 (rte_iova_t)iov[i].dst_addr, iov[i].len, RTE_DMA_OP_FLAG_LLC);
72 * Since all memory is pinned and DMA vChannel
73 * ring has enough space, failure should be a
74 * rare case. If failure happens, it means DMA
75 * device encounters serious errors; in this
76 * case, please stop async data-path and check
77 * what has happened to DMA device.
79 if (unlikely(copy_idx < 0)) {
80 if (!vhost_async_dma_copy_log) {
81 VHOST_LOG_DATA(ERR, "(%s) DMA copy failed for channel %d:%u\n",
82 dev->ifname, dma_id, vchan_id);
83 vhost_async_dma_copy_log = true;
90 * Only store packet completion flag address in the last copy's
91 * slot, and other slots are set to NULL.
93 dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = &vq->async->pkts_cmpl_flag[flag_idx];
98 static __rte_always_inline uint16_t
99 vhost_async_dma_transfer(struct virtio_net *dev, struct vhost_virtqueue *vq,
100 int16_t dma_id, uint16_t vchan_id, uint16_t head_idx,
101 struct vhost_iov_iter *pkts, uint16_t nr_pkts)
103 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
104 int64_t ret, nr_copies = 0;
107 rte_spinlock_lock(&dma_info->dma_lock);
109 for (pkt_idx = 0; pkt_idx < nr_pkts; pkt_idx++) {
110 ret = vhost_async_dma_transfer_one(dev, vq, dma_id, vchan_id, head_idx,
112 if (unlikely(ret < 0))
117 if (head_idx >= vq->size)
118 head_idx -= vq->size;
121 if (likely(nr_copies > 0))
122 rte_dma_submit(dma_id, vchan_id);
124 rte_spinlock_unlock(&dma_info->dma_lock);
129 static __rte_always_inline uint16_t
130 vhost_async_dma_check_completed(struct virtio_net *dev, int16_t dma_id, uint16_t vchan_id,
133 struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
134 uint16_t ring_mask = dma_info->ring_mask;
135 uint16_t last_idx = 0;
139 bool has_error = false;
140 static bool vhost_async_dma_complete_log;
142 rte_spinlock_lock(&dma_info->dma_lock);
145 * Print error log for debugging, if DMA reports error during
146 * DMA transfer. We do not handle error in vhost level.
148 nr_copies = rte_dma_completed(dma_id, vchan_id, max_pkts, &last_idx, &has_error);
149 if (unlikely(!vhost_async_dma_complete_log && has_error)) {
150 VHOST_LOG_DATA(ERR, "(%s) DMA completion failure on channel %d:%u\n", dev->ifname,
152 vhost_async_dma_complete_log = true;
153 } else if (nr_copies == 0) {
157 copy_idx = last_idx - nr_copies + 1;
158 for (i = 0; i < nr_copies; i++) {
161 flag = dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask];
164 * Mark the packet flag as received. The flag
165 * could belong to another virtqueue but write
169 dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = NULL;
175 rte_spinlock_unlock(&dma_info->dma_lock);
180 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
182 struct batch_copy_elem *elem = vq->batch_copy_elems;
183 uint16_t count = vq->batch_copy_nb_elems;
186 for (i = 0; i < count; i++) {
187 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
188 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
190 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
193 vq->batch_copy_nb_elems = 0;
197 do_data_copy_dequeue(struct vhost_virtqueue *vq)
199 struct batch_copy_elem *elem = vq->batch_copy_elems;
200 uint16_t count = vq->batch_copy_nb_elems;
203 for (i = 0; i < count; i++)
204 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
206 vq->batch_copy_nb_elems = 0;
209 static __rte_always_inline void
210 do_flush_shadow_used_ring_split(struct virtio_net *dev,
211 struct vhost_virtqueue *vq,
212 uint16_t to, uint16_t from, uint16_t size)
214 rte_memcpy(&vq->used->ring[to],
215 &vq->shadow_used_split[from],
216 size * sizeof(struct vring_used_elem));
217 vhost_log_cache_used_vring(dev, vq,
218 offsetof(struct vring_used, ring[to]),
219 size * sizeof(struct vring_used_elem));
222 static __rte_always_inline void
223 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
225 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
227 if (used_idx + vq->shadow_used_idx <= vq->size) {
228 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
229 vq->shadow_used_idx);
233 /* update used ring interval [used_idx, vq->size] */
234 size = vq->size - used_idx;
235 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
237 /* update the left half used ring interval [0, left_size] */
238 do_flush_shadow_used_ring_split(dev, vq, 0, size,
239 vq->shadow_used_idx - size);
241 vq->last_used_idx += vq->shadow_used_idx;
243 vhost_log_cache_sync(dev, vq);
245 __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
247 vq->shadow_used_idx = 0;
248 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
249 sizeof(vq->used->idx));
252 static __rte_always_inline void
253 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
254 uint16_t desc_idx, uint32_t len)
256 uint16_t i = vq->shadow_used_idx++;
258 vq->shadow_used_split[i].id = desc_idx;
259 vq->shadow_used_split[i].len = len;
262 static __rte_always_inline void
263 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
264 struct vhost_virtqueue *vq)
267 uint16_t used_idx = vq->last_used_idx;
268 uint16_t head_idx = vq->last_used_idx;
269 uint16_t head_flags = 0;
271 /* Split loop in two to save memory barriers */
272 for (i = 0; i < vq->shadow_used_idx; i++) {
273 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
274 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
276 used_idx += vq->shadow_used_packed[i].count;
277 if (used_idx >= vq->size)
278 used_idx -= vq->size;
281 /* The ordering for storing desc flags needs to be enforced. */
282 rte_atomic_thread_fence(__ATOMIC_RELEASE);
284 for (i = 0; i < vq->shadow_used_idx; i++) {
287 if (vq->shadow_used_packed[i].len)
288 flags = VRING_DESC_F_WRITE;
292 if (vq->used_wrap_counter) {
293 flags |= VRING_DESC_F_USED;
294 flags |= VRING_DESC_F_AVAIL;
296 flags &= ~VRING_DESC_F_USED;
297 flags &= ~VRING_DESC_F_AVAIL;
301 vq->desc_packed[vq->last_used_idx].flags = flags;
303 vhost_log_cache_used_vring(dev, vq,
305 sizeof(struct vring_packed_desc),
306 sizeof(struct vring_packed_desc));
308 head_idx = vq->last_used_idx;
312 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
315 vq->desc_packed[head_idx].flags = head_flags;
317 vhost_log_cache_used_vring(dev, vq,
319 sizeof(struct vring_packed_desc),
320 sizeof(struct vring_packed_desc));
322 vq->shadow_used_idx = 0;
323 vhost_log_cache_sync(dev, vq);
326 static __rte_always_inline void
327 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
328 struct vhost_virtqueue *vq)
330 struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
332 vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
333 /* desc flags is the synchronization point for virtio packed vring */
334 __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
335 used_elem->flags, __ATOMIC_RELEASE);
337 vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
338 sizeof(struct vring_packed_desc),
339 sizeof(struct vring_packed_desc));
340 vq->shadow_used_idx = 0;
341 vhost_log_cache_sync(dev, vq);
344 static __rte_always_inline void
345 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
346 struct vhost_virtqueue *vq,
352 uint16_t last_used_idx;
353 struct vring_packed_desc *desc_base;
355 last_used_idx = vq->last_used_idx;
356 desc_base = &vq->desc_packed[last_used_idx];
358 flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
360 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
361 desc_base[i].id = ids[i];
362 desc_base[i].len = lens[i];
365 rte_atomic_thread_fence(__ATOMIC_RELEASE);
367 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
368 desc_base[i].flags = flags;
371 vhost_log_cache_used_vring(dev, vq, last_used_idx *
372 sizeof(struct vring_packed_desc),
373 sizeof(struct vring_packed_desc) *
375 vhost_log_cache_sync(dev, vq);
377 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
380 static __rte_always_inline void
381 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
384 vq->shadow_used_packed[0].id = id;
386 if (!vq->shadow_used_idx) {
387 vq->shadow_last_used_idx = vq->last_used_idx;
388 vq->shadow_used_packed[0].flags =
389 PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
390 vq->shadow_used_packed[0].len = 0;
391 vq->shadow_used_packed[0].count = 1;
392 vq->shadow_used_idx++;
395 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
398 static __rte_always_inline void
399 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
400 struct vhost_virtqueue *vq,
407 flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
409 if (!vq->shadow_used_idx) {
410 vq->shadow_last_used_idx = vq->last_used_idx;
411 vq->shadow_used_packed[0].id = ids[0];
412 vq->shadow_used_packed[0].len = 0;
413 vq->shadow_used_packed[0].count = 1;
414 vq->shadow_used_packed[0].flags = flags;
415 vq->shadow_used_idx++;
420 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
421 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
422 vq->desc_packed[vq->last_used_idx + i].len = 0;
425 rte_atomic_thread_fence(__ATOMIC_RELEASE);
426 vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
427 vq->desc_packed[vq->last_used_idx + i].flags = flags;
429 vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
430 sizeof(struct vring_packed_desc),
431 sizeof(struct vring_packed_desc) *
433 vhost_log_cache_sync(dev, vq);
435 vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
438 static __rte_always_inline void
439 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
445 flags = vq->desc_packed[vq->last_used_idx].flags;
446 if (vq->used_wrap_counter) {
447 flags |= VRING_DESC_F_USED;
448 flags |= VRING_DESC_F_AVAIL;
450 flags &= ~VRING_DESC_F_USED;
451 flags &= ~VRING_DESC_F_AVAIL;
454 if (!vq->shadow_used_idx) {
455 vq->shadow_last_used_idx = vq->last_used_idx;
457 vq->shadow_used_packed[0].id = buf_id;
458 vq->shadow_used_packed[0].len = 0;
459 vq->shadow_used_packed[0].flags = flags;
460 vq->shadow_used_idx++;
462 vq->desc_packed[vq->last_used_idx].id = buf_id;
463 vq->desc_packed[vq->last_used_idx].len = 0;
464 vq->desc_packed[vq->last_used_idx].flags = flags;
467 vq_inc_last_used_packed(vq, count);
470 static __rte_always_inline void
471 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
477 vq->shadow_used_packed[0].id = buf_id;
479 flags = vq->desc_packed[vq->last_used_idx].flags;
480 if (vq->used_wrap_counter) {
481 flags |= VRING_DESC_F_USED;
482 flags |= VRING_DESC_F_AVAIL;
484 flags &= ~VRING_DESC_F_USED;
485 flags &= ~VRING_DESC_F_AVAIL;
488 if (!vq->shadow_used_idx) {
489 vq->shadow_last_used_idx = vq->last_used_idx;
490 vq->shadow_used_packed[0].len = 0;
491 vq->shadow_used_packed[0].flags = flags;
492 vq->shadow_used_idx++;
495 vq_inc_last_used_packed(vq, count);
498 static __rte_always_inline void
499 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
503 uint16_t num_buffers)
507 for (i = 0; i < num_buffers; i++) {
508 /* enqueue shadow flush action aligned with batch num */
509 if (!vq->shadow_used_idx)
510 vq->shadow_aligned_idx = vq->last_used_idx &
512 vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
513 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
514 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
515 vq->shadow_aligned_idx += count[i];
516 vq->shadow_used_idx++;
520 static __rte_always_inline void
521 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
522 struct vhost_virtqueue *vq,
526 uint16_t num_buffers)
528 vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
530 if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
531 do_data_copy_enqueue(dev, vq);
532 vhost_flush_enqueue_shadow_packed(dev, vq);
536 /* avoid write operation when necessary, to lessen cache issues */
537 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
538 if ((var) != (val)) \
542 static __rte_always_inline void
543 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
545 uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
547 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
548 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
551 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
552 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
555 case RTE_MBUF_F_TX_TCP_CKSUM:
556 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
559 case RTE_MBUF_F_TX_UDP_CKSUM:
560 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
563 case RTE_MBUF_F_TX_SCTP_CKSUM:
564 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
569 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
570 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
571 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
574 /* IP cksum verification cannot be bypassed, then calculate here */
575 if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
576 struct rte_ipv4_hdr *ipv4_hdr;
578 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
580 ipv4_hdr->hdr_checksum = 0;
581 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
584 if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
585 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
586 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
588 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
589 net_hdr->gso_size = m_buf->tso_segsz;
590 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
592 } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
593 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
594 net_hdr->gso_size = m_buf->tso_segsz;
595 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
598 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
599 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
600 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
604 static __rte_always_inline int
605 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
606 struct buf_vector *buf_vec, uint16_t *vec_idx,
607 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
609 uint16_t vec_id = *vec_idx;
613 uint64_t desc_chunck_len = desc_len;
615 if (unlikely(vec_id >= BUF_VECTOR_MAX))
618 desc_addr = vhost_iova_to_vva(dev, vq,
622 if (unlikely(!desc_addr))
625 rte_prefetch0((void *)(uintptr_t)desc_addr);
627 buf_vec[vec_id].buf_iova = desc_iova;
628 buf_vec[vec_id].buf_addr = desc_addr;
629 buf_vec[vec_id].buf_len = desc_chunck_len;
631 desc_len -= desc_chunck_len;
632 desc_iova += desc_chunck_len;
640 static __rte_always_inline int
641 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
642 uint32_t avail_idx, uint16_t *vec_idx,
643 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
644 uint32_t *desc_chain_len, uint8_t perm)
646 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
647 uint16_t vec_id = *vec_idx;
650 uint32_t nr_descs = vq->size;
652 struct vring_desc *descs = vq->desc;
653 struct vring_desc *idesc = NULL;
655 if (unlikely(idx >= vq->size))
658 *desc_chain_head = idx;
660 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
661 dlen = vq->desc[idx].len;
662 nr_descs = dlen / sizeof(struct vring_desc);
663 if (unlikely(nr_descs > vq->size))
666 descs = (struct vring_desc *)(uintptr_t)
667 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
670 if (unlikely(!descs))
673 if (unlikely(dlen < vq->desc[idx].len)) {
675 * The indirect desc table is not contiguous
676 * in process VA space, we have to copy it.
678 idesc = vhost_alloc_copy_ind_table(dev, vq,
679 vq->desc[idx].addr, vq->desc[idx].len);
680 if (unlikely(!idesc))
690 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
691 free_ind_table(idesc);
695 dlen = descs[idx].len;
698 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
699 descs[idx].addr, dlen,
701 free_ind_table(idesc);
705 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
708 idx = descs[idx].next;
711 *desc_chain_len = len;
714 if (unlikely(!!idesc))
715 free_ind_table(idesc);
721 * Returns -1 on fail, 0 on success
724 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
725 uint32_t size, struct buf_vector *buf_vec,
726 uint16_t *num_buffers, uint16_t avail_head,
730 uint16_t vec_idx = 0;
731 uint16_t max_tries, tries = 0;
733 uint16_t head_idx = 0;
737 cur_idx = vq->last_avail_idx;
739 if (rxvq_is_mergeable(dev))
740 max_tries = vq->size - 1;
745 if (unlikely(cur_idx == avail_head))
748 * if we tried all available ring items, and still
749 * can't get enough buf, it means something abnormal
752 if (unlikely(++tries > max_tries))
755 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
758 VHOST_ACCESS_RW) < 0))
760 len = RTE_MIN(len, size);
761 update_shadow_used_ring_split(vq, head_idx, len);
773 static __rte_always_inline int
774 fill_vec_buf_packed_indirect(struct virtio_net *dev,
775 struct vhost_virtqueue *vq,
776 struct vring_packed_desc *desc, uint16_t *vec_idx,
777 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
781 uint16_t vec_id = *vec_idx;
783 struct vring_packed_desc *descs, *idescs = NULL;
786 descs = (struct vring_packed_desc *)(uintptr_t)
787 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
788 if (unlikely(!descs))
791 if (unlikely(dlen < desc->len)) {
793 * The indirect desc table is not contiguous
794 * in process VA space, we have to copy it.
796 idescs = vhost_alloc_copy_ind_table(dev,
797 vq, desc->addr, desc->len);
798 if (unlikely(!idescs))
804 nr_descs = desc->len / sizeof(struct vring_packed_desc);
805 if (unlikely(nr_descs >= vq->size)) {
806 free_ind_table(idescs);
810 for (i = 0; i < nr_descs; i++) {
811 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
812 free_ind_table(idescs);
818 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
825 if (unlikely(!!idescs))
826 free_ind_table(idescs);
831 static __rte_always_inline int
832 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
833 uint16_t avail_idx, uint16_t *desc_count,
834 struct buf_vector *buf_vec, uint16_t *vec_idx,
835 uint16_t *buf_id, uint32_t *len, uint8_t perm)
837 bool wrap_counter = vq->avail_wrap_counter;
838 struct vring_packed_desc *descs = vq->desc_packed;
839 uint16_t vec_id = *vec_idx;
842 if (avail_idx < vq->last_avail_idx)
846 * Perform a load-acquire barrier in desc_is_avail to
847 * enforce the ordering between desc flags and desc
850 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
857 if (unlikely(vec_id >= BUF_VECTOR_MAX))
860 if (unlikely(*desc_count >= vq->size))
864 *buf_id = descs[avail_idx].id;
866 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
867 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
873 dlen = descs[avail_idx].len;
876 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
877 descs[avail_idx].addr,
883 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
886 if (++avail_idx >= vq->size) {
887 avail_idx -= vq->size;
897 static __rte_noinline void
898 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
899 struct buf_vector *buf_vec,
900 struct virtio_net_hdr_mrg_rxbuf *hdr)
903 uint64_t remain = dev->vhost_hlen;
904 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
905 uint64_t iova = buf_vec->buf_iova;
908 len = RTE_MIN(remain,
910 dst = buf_vec->buf_addr;
911 rte_memcpy((void *)(uintptr_t)dst,
912 (void *)(uintptr_t)src,
915 PRINT_PACKET(dev, (uintptr_t)dst,
917 vhost_log_cache_write_iova(dev, vq,
927 static __rte_always_inline int
928 async_iter_initialize(struct virtio_net *dev, struct vhost_async *async)
930 struct vhost_iov_iter *iter;
932 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
933 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
937 iter = async->iov_iter + async->iter_idx;
938 iter->iov = async->iovec + async->iovec_idx;
944 static __rte_always_inline int
945 async_iter_add_iovec(struct virtio_net *dev, struct vhost_async *async,
946 void *src, void *dst, size_t len)
948 struct vhost_iov_iter *iter;
949 struct vhost_iovec *iovec;
951 if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
952 static bool vhost_max_async_vec_log;
954 if (!vhost_max_async_vec_log) {
955 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
956 vhost_max_async_vec_log = true;
962 iter = async->iov_iter + async->iter_idx;
963 iovec = async->iovec + async->iovec_idx;
965 iovec->src_addr = src;
966 iovec->dst_addr = dst;
975 static __rte_always_inline void
976 async_iter_finalize(struct vhost_async *async)
981 static __rte_always_inline void
982 async_iter_cancel(struct vhost_async *async)
984 struct vhost_iov_iter *iter;
986 iter = async->iov_iter + async->iter_idx;
987 async->iovec_idx -= iter->nr_segs;
992 static __rte_always_inline void
993 async_iter_reset(struct vhost_async *async)
996 async->iovec_idx = 0;
999 static __rte_always_inline int
1000 async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1001 struct rte_mbuf *m, uint32_t mbuf_offset,
1002 uint64_t buf_iova, uint32_t cpy_len)
1004 struct vhost_async *async = vq->async;
1005 uint64_t mapped_len;
1006 uint32_t buf_offset = 0;
1010 host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1011 buf_iova + buf_offset, cpy_len, &mapped_len);
1012 if (unlikely(!host_iova)) {
1013 VHOST_LOG_DATA(ERR, "(%s) %s: failed to get host iova.\n",
1014 dev->ifname, __func__);
1018 if (unlikely(async_iter_add_iovec(dev, async,
1019 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1021 host_iova, (size_t)mapped_len)))
1024 cpy_len -= (uint32_t)mapped_len;
1025 mbuf_offset += (uint32_t)mapped_len;
1026 buf_offset += (uint32_t)mapped_len;
1032 static __rte_always_inline void
1033 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1034 struct rte_mbuf *m, uint32_t mbuf_offset,
1035 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
1037 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1039 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
1040 rte_memcpy((void *)((uintptr_t)(buf_addr)),
1041 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1043 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1044 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1046 batch_copy[vq->batch_copy_nb_elems].dst =
1047 (void *)((uintptr_t)(buf_addr));
1048 batch_copy[vq->batch_copy_nb_elems].src =
1049 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1050 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1051 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1052 vq->batch_copy_nb_elems++;
1056 static __rte_always_inline int
1057 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1058 struct rte_mbuf *m, struct buf_vector *buf_vec,
1059 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1061 uint32_t vec_idx = 0;
1062 uint32_t mbuf_offset, mbuf_avail;
1063 uint32_t buf_offset, buf_avail;
1064 uint64_t buf_addr, buf_iova, buf_len;
1067 struct rte_mbuf *hdr_mbuf;
1068 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1069 struct vhost_async *async = vq->async;
1071 if (unlikely(m == NULL))
1074 buf_addr = buf_vec[vec_idx].buf_addr;
1075 buf_iova = buf_vec[vec_idx].buf_iova;
1076 buf_len = buf_vec[vec_idx].buf_len;
1078 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1082 hdr_addr = buf_addr;
1083 if (unlikely(buf_len < dev->vhost_hlen)) {
1084 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1087 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1089 VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
1090 dev->ifname, num_buffers);
1092 if (unlikely(buf_len < dev->vhost_hlen)) {
1093 buf_offset = dev->vhost_hlen - buf_len;
1095 buf_addr = buf_vec[vec_idx].buf_addr;
1096 buf_iova = buf_vec[vec_idx].buf_iova;
1097 buf_len = buf_vec[vec_idx].buf_len;
1098 buf_avail = buf_len - buf_offset;
1100 buf_offset = dev->vhost_hlen;
1101 buf_avail = buf_len - dev->vhost_hlen;
1104 mbuf_avail = rte_pktmbuf_data_len(m);
1108 if (async_iter_initialize(dev, async))
1112 while (mbuf_avail != 0 || m->next != NULL) {
1113 /* done with current buf, get the next one */
1114 if (buf_avail == 0) {
1116 if (unlikely(vec_idx >= nr_vec))
1119 buf_addr = buf_vec[vec_idx].buf_addr;
1120 buf_iova = buf_vec[vec_idx].buf_iova;
1121 buf_len = buf_vec[vec_idx].buf_len;
1124 buf_avail = buf_len;
1127 /* done with current mbuf, get the next one */
1128 if (mbuf_avail == 0) {
1132 mbuf_avail = rte_pktmbuf_data_len(m);
1136 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1137 if (rxvq_is_mergeable(dev))
1138 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1141 if (unlikely(hdr == &tmp_hdr)) {
1142 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1144 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1145 dev->vhost_hlen, 0);
1146 vhost_log_cache_write_iova(dev, vq,
1147 buf_vec[0].buf_iova,
1154 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1157 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1158 buf_iova + buf_offset, cpy_len) < 0)
1161 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1162 buf_addr + buf_offset,
1163 buf_iova + buf_offset, cpy_len);
1166 mbuf_avail -= cpy_len;
1167 mbuf_offset += cpy_len;
1168 buf_avail -= cpy_len;
1169 buf_offset += cpy_len;
1173 async_iter_finalize(async);
1178 async_iter_cancel(async);
1183 static __rte_always_inline int
1184 vhost_enqueue_single_packed(struct virtio_net *dev,
1185 struct vhost_virtqueue *vq,
1186 struct rte_mbuf *pkt,
1187 struct buf_vector *buf_vec,
1190 uint16_t nr_vec = 0;
1191 uint16_t avail_idx = vq->last_avail_idx;
1192 uint16_t max_tries, tries = 0;
1193 uint16_t buf_id = 0;
1195 uint16_t desc_count;
1196 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1197 uint16_t num_buffers = 0;
1198 uint32_t buffer_len[vq->size];
1199 uint16_t buffer_buf_id[vq->size];
1200 uint16_t buffer_desc_count[vq->size];
1202 if (rxvq_is_mergeable(dev))
1203 max_tries = vq->size - 1;
1209 * if we tried all available ring items, and still
1210 * can't get enough buf, it means something abnormal
1213 if (unlikely(++tries > max_tries))
1216 if (unlikely(fill_vec_buf_packed(dev, vq,
1217 avail_idx, &desc_count,
1220 VHOST_ACCESS_RW) < 0))
1223 len = RTE_MIN(len, size);
1226 buffer_len[num_buffers] = len;
1227 buffer_buf_id[num_buffers] = buf_id;
1228 buffer_desc_count[num_buffers] = desc_count;
1231 *nr_descs += desc_count;
1232 avail_idx += desc_count;
1233 if (avail_idx >= vq->size)
1234 avail_idx -= vq->size;
1237 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1240 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1241 buffer_desc_count, num_buffers);
1246 static __rte_noinline uint32_t
1247 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1248 struct rte_mbuf **pkts, uint32_t count)
1250 uint32_t pkt_idx = 0;
1251 uint16_t num_buffers;
1252 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1253 uint16_t avail_head;
1256 * The ordering between avail index and
1257 * desc reads needs to be enforced.
1259 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1261 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1263 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1264 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1265 uint16_t nr_vec = 0;
1267 if (unlikely(reserve_avail_buf_split(dev, vq,
1268 pkt_len, buf_vec, &num_buffers,
1269 avail_head, &nr_vec) < 0)) {
1270 VHOST_LOG_DATA(DEBUG,
1271 "(%s) failed to get enough desc from vring\n",
1273 vq->shadow_used_idx -= num_buffers;
1277 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1278 dev->ifname, vq->last_avail_idx,
1279 vq->last_avail_idx + num_buffers);
1281 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1282 num_buffers, false) < 0) {
1283 vq->shadow_used_idx -= num_buffers;
1287 vq->last_avail_idx += num_buffers;
1290 do_data_copy_enqueue(dev, vq);
1292 if (likely(vq->shadow_used_idx)) {
1293 flush_shadow_used_ring_split(dev, vq);
1294 vhost_vring_call_split(dev, vq);
1300 static __rte_always_inline int
1301 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1302 struct vhost_virtqueue *vq,
1303 struct rte_mbuf **pkts,
1304 uint64_t *desc_addrs,
1307 bool wrap_counter = vq->avail_wrap_counter;
1308 struct vring_packed_desc *descs = vq->desc_packed;
1309 uint16_t avail_idx = vq->last_avail_idx;
1310 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1313 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1316 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1319 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1320 if (unlikely(pkts[i]->next != NULL))
1322 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1327 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1328 lens[i] = descs[avail_idx + i].len;
1330 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1331 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1335 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1336 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1337 descs[avail_idx + i].addr,
1341 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1342 if (unlikely(!desc_addrs[i]))
1344 if (unlikely(lens[i] != descs[avail_idx + i].len))
1351 static __rte_always_inline void
1352 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1353 struct vhost_virtqueue *vq,
1354 struct rte_mbuf **pkts,
1355 uint64_t *desc_addrs,
1358 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1359 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1360 struct vring_packed_desc *descs = vq->desc_packed;
1361 uint16_t avail_idx = vq->last_avail_idx;
1362 uint16_t ids[PACKED_BATCH_SIZE];
1365 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1366 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1367 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1368 (uintptr_t)desc_addrs[i];
1369 lens[i] = pkts[i]->pkt_len +
1370 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1373 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1374 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1376 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1378 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1379 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1380 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1384 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1385 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1388 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1389 ids[i] = descs[avail_idx + i].id;
1391 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1394 static __rte_always_inline int
1395 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1396 struct vhost_virtqueue *vq,
1397 struct rte_mbuf **pkts)
1399 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1400 uint64_t lens[PACKED_BATCH_SIZE];
1402 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1405 if (vq->shadow_used_idx) {
1406 do_data_copy_enqueue(dev, vq);
1407 vhost_flush_enqueue_shadow_packed(dev, vq);
1410 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1415 static __rte_always_inline int16_t
1416 virtio_dev_rx_single_packed(struct virtio_net *dev,
1417 struct vhost_virtqueue *vq,
1418 struct rte_mbuf *pkt)
1420 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1421 uint16_t nr_descs = 0;
1423 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1425 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1430 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1431 dev->ifname, vq->last_avail_idx,
1432 vq->last_avail_idx + nr_descs);
1434 vq_inc_last_avail_packed(vq, nr_descs);
1439 static __rte_noinline uint32_t
1440 virtio_dev_rx_packed(struct virtio_net *dev,
1441 struct vhost_virtqueue *__rte_restrict vq,
1442 struct rte_mbuf **__rte_restrict pkts,
1445 uint32_t pkt_idx = 0;
1448 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1450 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1451 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1453 pkt_idx += PACKED_BATCH_SIZE;
1458 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1462 } while (pkt_idx < count);
1464 if (vq->shadow_used_idx) {
1465 do_data_copy_enqueue(dev, vq);
1466 vhost_flush_enqueue_shadow_packed(dev, vq);
1470 vhost_vring_call_packed(dev, vq);
1475 static __rte_always_inline uint32_t
1476 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1477 struct rte_mbuf **pkts, uint32_t count)
1479 struct vhost_virtqueue *vq;
1482 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1483 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1484 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1485 dev->ifname, __func__, queue_id);
1489 vq = dev->virtqueue[queue_id];
1491 rte_spinlock_lock(&vq->access_lock);
1493 if (unlikely(!vq->enabled))
1494 goto out_access_unlock;
1496 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1497 vhost_user_iotlb_rd_lock(vq);
1499 if (unlikely(!vq->access_ok))
1500 if (unlikely(vring_translate(dev, vq) < 0))
1503 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1507 if (vq_is_packed(dev))
1508 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1510 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1513 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1514 vhost_user_iotlb_rd_unlock(vq);
1517 rte_spinlock_unlock(&vq->access_lock);
1523 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1524 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1526 struct virtio_net *dev = get_device(vid);
1531 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1532 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1533 dev->ifname, __func__);
1537 return virtio_dev_rx(dev, queue_id, pkts, count);
1540 static __rte_always_inline uint16_t
1541 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1543 struct vhost_async *async = vq->async;
1545 if (async->pkts_idx >= async->pkts_inflight_n)
1546 return async->pkts_idx - async->pkts_inflight_n;
1548 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1551 static __rte_always_inline void
1552 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1553 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1555 size_t elem_size = sizeof(struct vring_used_elem);
1557 if (d_idx + count <= ring_size) {
1558 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1560 uint16_t size = ring_size - d_idx;
1562 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1563 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1567 static __rte_always_inline void
1568 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1569 struct vring_used_elem_packed *d_ring,
1570 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1572 size_t elem_size = sizeof(struct vring_used_elem_packed);
1574 if (d_idx + count <= ring_size) {
1575 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1577 uint16_t size = ring_size - d_idx;
1579 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1580 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1584 static __rte_noinline uint32_t
1585 virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1586 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1587 int16_t dma_id, uint16_t vchan_id)
1589 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1590 uint32_t pkt_idx = 0;
1591 uint16_t num_buffers;
1592 uint16_t avail_head;
1594 struct vhost_async *async = vq->async;
1595 struct async_inflight_info *pkts_info = async->pkts_info;
1596 uint32_t pkt_err = 0;
1598 uint16_t slot_idx = 0;
1601 * The ordering between avail index and desc reads need to be enforced.
1603 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1605 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1607 async_iter_reset(async);
1609 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1610 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1611 uint16_t nr_vec = 0;
1613 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1614 &num_buffers, avail_head, &nr_vec) < 0)) {
1615 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1617 vq->shadow_used_idx -= num_buffers;
1621 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1622 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1624 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1625 vq->shadow_used_idx -= num_buffers;
1629 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1630 pkts_info[slot_idx].descs = num_buffers;
1631 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1633 vq->last_avail_idx += num_buffers;
1636 if (unlikely(pkt_idx == 0))
1639 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1640 async->iov_iter, pkt_idx);
1642 pkt_err = pkt_idx - n_xfer;
1643 if (unlikely(pkt_err)) {
1644 uint16_t num_descs = 0;
1646 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1647 dev->ifname, __func__, pkt_err, queue_id);
1649 /* update number of completed packets */
1652 /* calculate the sum of descriptors to revert */
1653 while (pkt_err-- > 0) {
1654 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1658 /* recover shadow used ring and available ring */
1659 vq->shadow_used_idx -= num_descs;
1660 vq->last_avail_idx -= num_descs;
1663 /* keep used descriptors */
1664 if (likely(vq->shadow_used_idx)) {
1665 uint16_t to = async->desc_idx_split & (vq->size - 1);
1667 store_dma_desc_info_split(vq->shadow_used_split,
1668 async->descs_split, vq->size, 0, to,
1669 vq->shadow_used_idx);
1671 async->desc_idx_split += vq->shadow_used_idx;
1673 async->pkts_idx += pkt_idx;
1674 if (async->pkts_idx >= vq->size)
1675 async->pkts_idx -= vq->size;
1677 async->pkts_inflight_n += pkt_idx;
1678 vq->shadow_used_idx = 0;
1685 static __rte_always_inline int
1686 vhost_enqueue_async_packed(struct virtio_net *dev,
1687 struct vhost_virtqueue *vq,
1688 struct rte_mbuf *pkt,
1689 struct buf_vector *buf_vec,
1691 uint16_t *nr_buffers)
1693 uint16_t nr_vec = 0;
1694 uint16_t avail_idx = vq->last_avail_idx;
1695 uint16_t max_tries, tries = 0;
1696 uint16_t buf_id = 0;
1698 uint16_t desc_count = 0;
1699 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1700 uint32_t buffer_len[vq->size];
1701 uint16_t buffer_buf_id[vq->size];
1702 uint16_t buffer_desc_count[vq->size];
1704 if (rxvq_is_mergeable(dev))
1705 max_tries = vq->size - 1;
1711 * if we tried all available ring items, and still
1712 * can't get enough buf, it means something abnormal
1715 if (unlikely(++tries > max_tries))
1718 if (unlikely(fill_vec_buf_packed(dev, vq,
1719 avail_idx, &desc_count,
1722 VHOST_ACCESS_RW) < 0))
1725 len = RTE_MIN(len, size);
1728 buffer_len[*nr_buffers] = len;
1729 buffer_buf_id[*nr_buffers] = buf_id;
1730 buffer_desc_count[*nr_buffers] = desc_count;
1732 *nr_descs += desc_count;
1733 avail_idx += desc_count;
1734 if (avail_idx >= vq->size)
1735 avail_idx -= vq->size;
1738 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1741 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1746 static __rte_always_inline int16_t
1747 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1748 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1750 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1752 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1753 nr_descs, nr_buffers) < 0)) {
1754 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1758 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1759 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1764 static __rte_always_inline void
1765 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1766 uint32_t nr_err, uint32_t *pkt_idx)
1768 uint16_t descs_err = 0;
1769 uint16_t buffers_err = 0;
1770 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1773 /* calculate the sum of buffers and descs of DMA-error packets. */
1774 while (nr_err-- > 0) {
1775 descs_err += pkts_info[slot_idx % vq->size].descs;
1776 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1780 if (vq->last_avail_idx >= descs_err) {
1781 vq->last_avail_idx -= descs_err;
1783 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1784 vq->avail_wrap_counter ^= 1;
1787 vq->shadow_used_idx -= buffers_err;
1790 static __rte_noinline uint32_t
1791 virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1792 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1793 int16_t dma_id, uint16_t vchan_id)
1795 uint32_t pkt_idx = 0;
1796 uint32_t remained = count;
1798 uint16_t num_buffers;
1801 struct vhost_async *async = vq->async;
1802 struct async_inflight_info *pkts_info = async->pkts_info;
1803 uint32_t pkt_err = 0;
1804 uint16_t slot_idx = 0;
1807 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1811 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1812 &num_descs, &num_buffers) < 0))
1815 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1817 pkts_info[slot_idx].descs = num_descs;
1818 pkts_info[slot_idx].nr_buffers = num_buffers;
1819 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1823 vq_inc_last_avail_packed(vq, num_descs);
1824 } while (pkt_idx < count);
1826 if (unlikely(pkt_idx == 0))
1829 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1830 async->iov_iter, pkt_idx);
1832 async_iter_reset(async);
1834 pkt_err = pkt_idx - n_xfer;
1835 if (unlikely(pkt_err)) {
1836 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1837 dev->ifname, __func__, pkt_err, queue_id);
1838 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1841 if (likely(vq->shadow_used_idx)) {
1842 /* keep used descriptors. */
1843 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1844 vq->size, 0, async->buffer_idx_packed,
1845 vq->shadow_used_idx);
1847 async->buffer_idx_packed += vq->shadow_used_idx;
1848 if (async->buffer_idx_packed >= vq->size)
1849 async->buffer_idx_packed -= vq->size;
1851 async->pkts_idx += pkt_idx;
1852 if (async->pkts_idx >= vq->size)
1853 async->pkts_idx -= vq->size;
1855 vq->shadow_used_idx = 0;
1856 async->pkts_inflight_n += pkt_idx;
1862 static __rte_always_inline void
1863 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1865 struct vhost_async *async = vq->async;
1866 uint16_t nr_left = n_descs;
1871 from = async->last_desc_idx_split & (vq->size - 1);
1872 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1873 to = vq->last_used_idx & (vq->size - 1);
1875 if (to + nr_copy <= vq->size) {
1876 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1877 nr_copy * sizeof(struct vring_used_elem));
1879 uint16_t size = vq->size - to;
1881 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1882 size * sizeof(struct vring_used_elem));
1883 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1884 (nr_copy - size) * sizeof(struct vring_used_elem));
1887 async->last_desc_idx_split += nr_copy;
1888 vq->last_used_idx += nr_copy;
1890 } while (nr_left > 0);
1893 static __rte_always_inline void
1894 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1897 struct vhost_async *async = vq->async;
1898 uint16_t from = async->last_buffer_idx_packed;
1899 uint16_t used_idx = vq->last_used_idx;
1900 uint16_t head_idx = vq->last_used_idx;
1901 uint16_t head_flags = 0;
1904 /* Split loop in two to save memory barriers */
1905 for (i = 0; i < n_buffers; i++) {
1906 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1907 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1909 used_idx += async->buffers_packed[from].count;
1910 if (used_idx >= vq->size)
1911 used_idx -= vq->size;
1914 if (from >= vq->size)
1918 /* The ordering for storing desc flags needs to be enforced. */
1919 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1921 from = async->last_buffer_idx_packed;
1923 for (i = 0; i < n_buffers; i++) {
1926 if (async->buffers_packed[from].len)
1927 flags = VRING_DESC_F_WRITE;
1931 if (vq->used_wrap_counter) {
1932 flags |= VRING_DESC_F_USED;
1933 flags |= VRING_DESC_F_AVAIL;
1935 flags &= ~VRING_DESC_F_USED;
1936 flags &= ~VRING_DESC_F_AVAIL;
1940 vq->desc_packed[vq->last_used_idx].flags = flags;
1942 head_idx = vq->last_used_idx;
1946 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
1949 if (from == vq->size)
1953 vq->desc_packed[head_idx].flags = head_flags;
1954 async->last_buffer_idx_packed = from;
1957 static __rte_always_inline uint16_t
1958 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1959 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
1962 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1963 struct vhost_async *async = vq->async;
1964 struct async_inflight_info *pkts_info = async->pkts_info;
1965 uint16_t nr_cpl_pkts = 0;
1966 uint16_t n_descs = 0, n_buffers = 0;
1967 uint16_t start_idx, from, i;
1969 /* Check completed copies for the given DMA vChannel */
1970 vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
1972 start_idx = async_get_first_inflight_pkt_idx(vq);
1974 * Calculate the number of copy completed packets.
1975 * Note that there may be completed packets even if
1976 * no copies are reported done by the given DMA vChannel,
1977 * as it's possible that a virtqueue uses multiple DMA
1981 while (vq->async->pkts_cmpl_flag[from] && count--) {
1982 vq->async->pkts_cmpl_flag[from] = false;
1984 if (from >= vq->size)
1989 if (nr_cpl_pkts == 0)
1992 for (i = 0; i < nr_cpl_pkts; i++) {
1993 from = (start_idx + i) % vq->size;
1994 /* Only used with packed ring */
1995 n_buffers += pkts_info[from].nr_buffers;
1996 /* Only used with split ring */
1997 n_descs += pkts_info[from].descs;
1998 pkts[i] = pkts_info[from].mbuf;
2001 async->pkts_inflight_n -= nr_cpl_pkts;
2003 if (likely(vq->enabled && vq->access_ok)) {
2004 if (vq_is_packed(dev)) {
2005 write_back_completed_descs_packed(vq, n_buffers);
2006 vhost_vring_call_packed(dev, vq);
2008 write_back_completed_descs_split(vq, n_descs);
2009 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
2010 vhost_vring_call_split(dev, vq);
2013 if (vq_is_packed(dev)) {
2014 async->last_buffer_idx_packed += n_buffers;
2015 if (async->last_buffer_idx_packed >= vq->size)
2016 async->last_buffer_idx_packed -= vq->size;
2018 async->last_desc_idx_split += n_descs;
2026 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2027 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2030 struct virtio_net *dev = get_device(vid);
2031 struct vhost_virtqueue *vq;
2032 uint16_t n_pkts_cpl = 0;
2037 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2038 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2039 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2040 dev->ifname, __func__, queue_id);
2044 if (unlikely(!dma_copy_track[dma_id].vchans ||
2045 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2046 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2051 vq = dev->virtqueue[queue_id];
2053 if (!rte_spinlock_trylock(&vq->access_lock)) {
2054 VHOST_LOG_DATA(DEBUG, "(%s) %s: virtqueue %u is busy.\n", dev->ifname, __func__,
2059 if (unlikely(!vq->async)) {
2060 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for virtqueue %d.\n",
2061 dev->ifname, __func__, queue_id);
2065 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2068 rte_spinlock_unlock(&vq->access_lock);
2074 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2075 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2078 struct virtio_net *dev = get_device(vid);
2079 struct vhost_virtqueue *vq;
2080 uint16_t n_pkts_cpl = 0;
2085 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2086 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2087 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2088 dev->ifname, __func__, queue_id);
2092 vq = dev->virtqueue[queue_id];
2094 if (unlikely(!vq->async)) {
2095 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
2096 dev->ifname, __func__, queue_id);
2100 if (unlikely(!dma_copy_track[dma_id].vchans ||
2101 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2102 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2107 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2112 static __rte_always_inline uint32_t
2113 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2114 struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2116 struct vhost_virtqueue *vq;
2119 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2120 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2121 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2122 dev->ifname, __func__, queue_id);
2126 if (unlikely(!dma_copy_track[dma_id].vchans ||
2127 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2128 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2133 vq = dev->virtqueue[queue_id];
2135 rte_spinlock_lock(&vq->access_lock);
2137 if (unlikely(!vq->enabled || !vq->async))
2138 goto out_access_unlock;
2140 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2141 vhost_user_iotlb_rd_lock(vq);
2143 if (unlikely(!vq->access_ok))
2144 if (unlikely(vring_translate(dev, vq) < 0))
2147 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2151 if (vq_is_packed(dev))
2152 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2153 pkts, count, dma_id, vchan_id);
2155 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2156 pkts, count, dma_id, vchan_id);
2159 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2160 vhost_user_iotlb_rd_unlock(vq);
2163 rte_spinlock_unlock(&vq->access_lock);
2169 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2170 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2173 struct virtio_net *dev = get_device(vid);
2178 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2179 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2180 dev->ifname, __func__);
2184 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, dma_id, vchan_id);
2188 virtio_net_with_host_offload(struct virtio_net *dev)
2191 ((1ULL << VIRTIO_NET_F_CSUM) |
2192 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2193 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2194 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2195 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2202 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2204 struct rte_ipv4_hdr *ipv4_hdr;
2205 struct rte_ipv6_hdr *ipv6_hdr;
2206 struct rte_ether_hdr *eth_hdr;
2208 uint16_t data_len = rte_pktmbuf_data_len(m);
2210 if (data_len < sizeof(struct rte_ether_hdr))
2213 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2215 m->l2_len = sizeof(struct rte_ether_hdr);
2216 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2218 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2219 if (data_len < sizeof(struct rte_ether_hdr) +
2220 sizeof(struct rte_vlan_hdr))
2223 struct rte_vlan_hdr *vlan_hdr =
2224 (struct rte_vlan_hdr *)(eth_hdr + 1);
2226 m->l2_len += sizeof(struct rte_vlan_hdr);
2227 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2230 switch (ethertype) {
2231 case RTE_ETHER_TYPE_IPV4:
2232 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2234 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2236 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2237 if (data_len < m->l2_len + m->l3_len)
2239 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2240 *l4_proto = ipv4_hdr->next_proto_id;
2242 case RTE_ETHER_TYPE_IPV6:
2243 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2245 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2247 m->l3_len = sizeof(struct rte_ipv6_hdr);
2248 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2249 *l4_proto = ipv6_hdr->proto;
2252 /* a valid L3 header is needed for further L4 parsing */
2256 /* both CSUM and GSO need a valid L4 header */
2257 switch (*l4_proto) {
2259 if (data_len < m->l2_len + m->l3_len +
2260 sizeof(struct rte_tcp_hdr))
2264 if (data_len < m->l2_len + m->l3_len +
2265 sizeof(struct rte_udp_hdr))
2269 if (data_len < m->l2_len + m->l3_len +
2270 sizeof(struct rte_sctp_hdr))
2286 static __rte_always_inline void
2287 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2290 uint8_t l4_proto = 0;
2291 struct rte_tcp_hdr *tcp_hdr = NULL;
2293 uint16_t data_len = rte_pktmbuf_data_len(m);
2295 if (parse_headers(m, &l4_proto) < 0)
2298 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2299 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2300 switch (hdr->csum_offset) {
2301 case (offsetof(struct rte_tcp_hdr, cksum)):
2302 if (l4_proto != IPPROTO_TCP)
2304 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2306 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2307 if (l4_proto != IPPROTO_UDP)
2309 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2311 case (offsetof(struct rte_sctp_hdr, cksum)):
2312 if (l4_proto != IPPROTO_SCTP)
2314 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2324 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2325 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2326 case VIRTIO_NET_HDR_GSO_TCPV4:
2327 case VIRTIO_NET_HDR_GSO_TCPV6:
2328 if (l4_proto != IPPROTO_TCP)
2330 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2331 struct rte_tcp_hdr *,
2332 m->l2_len + m->l3_len);
2333 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2334 if (data_len < m->l2_len + m->l3_len + tcp_len)
2336 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2337 m->tso_segsz = hdr->gso_size;
2338 m->l4_len = tcp_len;
2340 case VIRTIO_NET_HDR_GSO_UDP:
2341 if (l4_proto != IPPROTO_UDP)
2343 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2344 m->tso_segsz = hdr->gso_size;
2345 m->l4_len = sizeof(struct rte_udp_hdr);
2348 VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2349 dev->ifname, hdr->gso_type);
2361 static __rte_always_inline void
2362 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2363 struct rte_mbuf *m, bool legacy_ol_flags)
2365 struct rte_net_hdr_lens hdr_lens;
2366 int l4_supported = 0;
2369 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2372 if (legacy_ol_flags) {
2373 vhost_dequeue_offload_legacy(dev, hdr, m);
2377 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2379 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2380 m->packet_type = ptype;
2381 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2382 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2383 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2386 /* According to Virtio 1.1 spec, the device only needs to look at
2387 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2388 * This differs from the processing incoming packets path where the
2389 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2392 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2393 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2394 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2396 * 5.1.6.2.2 Device Requirements: Packet Transmission
2397 * The device MUST ignore flag bits that it does not recognize.
2399 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2402 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2403 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2404 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2406 /* Unknown proto or tunnel, do sw cksum. We can assume
2407 * the cksum field is in the first segment since the
2408 * buffers we provided to the host are large enough.
2409 * In case of SCTP, this will be wrong since it's a CRC
2410 * but there's nothing we can do.
2412 uint16_t csum = 0, off;
2414 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2415 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2417 if (likely(csum != 0xffff))
2419 off = hdr->csum_offset + hdr->csum_start;
2420 if (rte_pktmbuf_data_len(m) >= off + 1)
2421 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2425 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2426 if (hdr->gso_size == 0)
2429 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2430 case VIRTIO_NET_HDR_GSO_TCPV4:
2431 case VIRTIO_NET_HDR_GSO_TCPV6:
2432 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2434 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2435 m->tso_segsz = hdr->gso_size;
2437 case VIRTIO_NET_HDR_GSO_UDP:
2438 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2440 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2441 m->tso_segsz = hdr->gso_size;
2449 static __rte_noinline void
2450 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2451 struct buf_vector *buf_vec)
2454 uint64_t remain = sizeof(struct virtio_net_hdr);
2456 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2459 len = RTE_MIN(remain, buf_vec->buf_len);
2460 src = buf_vec->buf_addr;
2461 rte_memcpy((void *)(uintptr_t)dst,
2462 (void *)(uintptr_t)src, len);
2470 static __rte_always_inline int
2471 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2472 struct buf_vector *buf_vec, uint16_t nr_vec,
2473 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2474 bool legacy_ol_flags)
2476 uint32_t buf_avail, buf_offset;
2477 uint64_t buf_addr, buf_len;
2478 uint32_t mbuf_avail, mbuf_offset;
2480 struct rte_mbuf *cur = m, *prev = m;
2481 struct virtio_net_hdr tmp_hdr;
2482 struct virtio_net_hdr *hdr = NULL;
2483 /* A counter to avoid desc dead loop chain */
2484 uint16_t vec_idx = 0;
2485 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2488 buf_addr = buf_vec[vec_idx].buf_addr;
2489 buf_len = buf_vec[vec_idx].buf_len;
2491 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2496 if (virtio_net_with_host_offload(dev)) {
2497 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2499 * No luck, the virtio-net header doesn't fit
2500 * in a contiguous virtual area.
2502 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2505 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2510 * A virtio driver normally uses at least 2 desc buffers
2511 * for Tx: the first for storing the header, and others
2512 * for storing the data.
2514 if (unlikely(buf_len < dev->vhost_hlen)) {
2515 buf_offset = dev->vhost_hlen - buf_len;
2517 buf_addr = buf_vec[vec_idx].buf_addr;
2518 buf_len = buf_vec[vec_idx].buf_len;
2519 buf_avail = buf_len - buf_offset;
2520 } else if (buf_len == dev->vhost_hlen) {
2521 if (unlikely(++vec_idx >= nr_vec))
2523 buf_addr = buf_vec[vec_idx].buf_addr;
2524 buf_len = buf_vec[vec_idx].buf_len;
2527 buf_avail = buf_len;
2529 buf_offset = dev->vhost_hlen;
2530 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2534 (uintptr_t)(buf_addr + buf_offset),
2535 (uint32_t)buf_avail, 0);
2538 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2540 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2542 if (likely(cpy_len > MAX_BATCH_LEN ||
2543 vq->batch_copy_nb_elems >= vq->size ||
2544 (hdr && cur == m))) {
2545 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2547 (void *)((uintptr_t)(buf_addr +
2548 buf_offset)), cpy_len);
2550 batch_copy[vq->batch_copy_nb_elems].dst =
2551 rte_pktmbuf_mtod_offset(cur, void *,
2553 batch_copy[vq->batch_copy_nb_elems].src =
2554 (void *)((uintptr_t)(buf_addr + buf_offset));
2555 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2556 vq->batch_copy_nb_elems++;
2559 mbuf_avail -= cpy_len;
2560 mbuf_offset += cpy_len;
2561 buf_avail -= cpy_len;
2562 buf_offset += cpy_len;
2564 /* This buf reaches to its end, get the next one */
2565 if (buf_avail == 0) {
2566 if (++vec_idx >= nr_vec)
2569 buf_addr = buf_vec[vec_idx].buf_addr;
2570 buf_len = buf_vec[vec_idx].buf_len;
2573 buf_avail = buf_len;
2575 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2576 (uint32_t)buf_avail, 0);
2580 * This mbuf reaches to its end, get a new one
2581 * to hold more data.
2583 if (mbuf_avail == 0) {
2584 cur = rte_pktmbuf_alloc(mbuf_pool);
2585 if (unlikely(cur == NULL)) {
2586 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2593 prev->data_len = mbuf_offset;
2595 m->pkt_len += mbuf_offset;
2599 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2603 prev->data_len = mbuf_offset;
2604 m->pkt_len += mbuf_offset;
2607 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2615 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2621 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2623 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2624 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2629 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2630 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2632 if (unlikely(total_len > UINT16_MAX))
2635 buf_len = total_len;
2636 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2637 if (unlikely(buf == NULL))
2640 /* Initialize shinfo */
2641 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2642 virtio_dev_extbuf_free, buf);
2643 if (unlikely(shinfo == NULL)) {
2645 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2649 iova = rte_malloc_virt2iova(buf);
2650 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2651 rte_pktmbuf_reset_headroom(pkt);
2657 * Prepare a host supported pktmbuf.
2659 static __rte_always_inline int
2660 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2663 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2666 /* attach an external buffer if supported */
2667 if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2670 /* check if chained buffers are allowed */
2671 if (!dev->linearbuf)
2679 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2680 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2681 bool legacy_ol_flags)
2684 uint16_t free_entries;
2685 uint16_t dropped = 0;
2686 static bool allocerr_warned;
2689 * The ordering between avail index and
2690 * desc reads needs to be enforced.
2692 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2694 if (free_entries == 0)
2697 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2699 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2701 count = RTE_MIN(count, MAX_PKT_BURST);
2702 count = RTE_MIN(count, free_entries);
2703 VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2704 dev->ifname, count);
2706 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2709 for (i = 0; i < count; i++) {
2710 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2713 uint16_t nr_vec = 0;
2716 if (unlikely(fill_vec_buf_split(dev, vq,
2717 vq->last_avail_idx + i,
2719 &head_idx, &buf_len,
2720 VHOST_ACCESS_RO) < 0))
2723 update_shadow_used_ring_split(vq, head_idx, 0);
2725 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2726 if (unlikely(err)) {
2728 * mbuf allocation fails for jumbo packets when external
2729 * buffer allocation is not allowed and linear buffer
2730 * is required. Drop this packet.
2732 if (!allocerr_warned) {
2733 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2734 dev->ifname, buf_len, mbuf_pool->name);
2735 allocerr_warned = true;
2742 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2743 mbuf_pool, legacy_ol_flags);
2744 if (unlikely(err)) {
2745 if (!allocerr_warned) {
2746 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2748 allocerr_warned = true;
2757 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2759 vq->last_avail_idx += i;
2761 do_data_copy_dequeue(vq);
2762 if (unlikely(i < count))
2763 vq->shadow_used_idx = i;
2764 if (likely(vq->shadow_used_idx)) {
2765 flush_shadow_used_ring_split(dev, vq);
2766 vhost_vring_call_split(dev, vq);
2769 return (i - dropped);
2774 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2775 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2776 struct rte_mbuf **pkts, uint16_t count)
2778 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2783 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2784 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2785 struct rte_mbuf **pkts, uint16_t count)
2787 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2790 static __rte_always_inline int
2791 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2792 struct vhost_virtqueue *vq,
2793 struct rte_mbuf **pkts,
2795 uintptr_t *desc_addrs,
2798 bool wrap = vq->avail_wrap_counter;
2799 struct vring_packed_desc *descs = vq->desc_packed;
2800 uint64_t lens[PACKED_BATCH_SIZE];
2801 uint64_t buf_lens[PACKED_BATCH_SIZE];
2802 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2805 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2807 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2810 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2811 flags = descs[avail_idx + i].flags;
2812 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2813 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2814 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2818 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2820 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2821 lens[i] = descs[avail_idx + i].len;
2823 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2824 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2825 descs[avail_idx + i].addr,
2826 &lens[i], VHOST_ACCESS_RW);
2829 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2830 if (unlikely(!desc_addrs[i]))
2832 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2836 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2837 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2841 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2842 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2844 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2845 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2849 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2850 pkts[i]->pkt_len = lens[i] - buf_offset;
2851 pkts[i]->data_len = pkts[i]->pkt_len;
2852 ids[i] = descs[avail_idx + i].id;
2861 static __rte_always_inline int
2862 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2863 struct vhost_virtqueue *vq,
2864 struct rte_mbuf **pkts,
2865 bool legacy_ol_flags)
2867 uint16_t avail_idx = vq->last_avail_idx;
2868 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2869 struct virtio_net_hdr *hdr;
2870 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2871 uint16_t ids[PACKED_BATCH_SIZE];
2874 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2878 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2879 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2881 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2882 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2883 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2886 if (virtio_net_with_host_offload(dev)) {
2887 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2888 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2889 vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2893 if (virtio_net_is_inorder(dev))
2894 vhost_shadow_dequeue_batch_packed_inorder(vq,
2895 ids[PACKED_BATCH_SIZE - 1]);
2897 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2899 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2904 static __rte_always_inline int
2905 vhost_dequeue_single_packed(struct virtio_net *dev,
2906 struct vhost_virtqueue *vq,
2907 struct rte_mempool *mbuf_pool,
2908 struct rte_mbuf *pkts,
2910 uint16_t *desc_count,
2911 bool legacy_ol_flags)
2913 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2915 uint16_t nr_vec = 0;
2917 static bool allocerr_warned;
2919 if (unlikely(fill_vec_buf_packed(dev, vq,
2920 vq->last_avail_idx, desc_count,
2923 VHOST_ACCESS_RO) < 0))
2926 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2927 if (!allocerr_warned) {
2928 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2929 dev->ifname, buf_len, mbuf_pool->name);
2930 allocerr_warned = true;
2935 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2936 mbuf_pool, legacy_ol_flags);
2937 if (unlikely(err)) {
2938 if (!allocerr_warned) {
2939 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2941 allocerr_warned = true;
2949 static __rte_always_inline int
2950 virtio_dev_tx_single_packed(struct virtio_net *dev,
2951 struct vhost_virtqueue *vq,
2952 struct rte_mempool *mbuf_pool,
2953 struct rte_mbuf *pkts,
2954 bool legacy_ol_flags)
2957 uint16_t buf_id, desc_count = 0;
2960 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2961 &desc_count, legacy_ol_flags);
2963 if (likely(desc_count > 0)) {
2964 if (virtio_net_is_inorder(dev))
2965 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2968 vhost_shadow_dequeue_single_packed(vq, buf_id,
2971 vq_inc_last_avail_packed(vq, desc_count);
2979 virtio_dev_tx_packed(struct virtio_net *dev,
2980 struct vhost_virtqueue *__rte_restrict vq,
2981 struct rte_mempool *mbuf_pool,
2982 struct rte_mbuf **__rte_restrict pkts,
2984 bool legacy_ol_flags)
2986 uint32_t pkt_idx = 0;
2988 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2992 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2994 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2995 if (!virtio_dev_tx_batch_packed(dev, vq,
2998 pkt_idx += PACKED_BATCH_SIZE;
3003 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3008 } while (pkt_idx < count);
3010 if (pkt_idx != count)
3011 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3013 if (vq->shadow_used_idx) {
3014 do_data_copy_dequeue(vq);
3016 vhost_flush_dequeue_shadow_packed(dev, vq);
3017 vhost_vring_call_packed(dev, vq);
3025 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3026 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3027 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3029 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3034 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3035 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3036 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3038 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3042 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3043 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3045 struct virtio_net *dev;
3046 struct rte_mbuf *rarp_mbuf = NULL;
3047 struct vhost_virtqueue *vq;
3048 int16_t success = 1;
3050 dev = get_device(vid);
3054 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3055 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
3056 dev->ifname, __func__);
3060 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3061 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
3062 dev->ifname, __func__, queue_id);
3066 vq = dev->virtqueue[queue_id];
3068 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3071 if (unlikely(!vq->enabled)) {
3073 goto out_access_unlock;
3076 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3077 vhost_user_iotlb_rd_lock(vq);
3079 if (unlikely(!vq->access_ok))
3080 if (unlikely(vring_translate(dev, vq) < 0)) {
3086 * Construct a RARP broadcast packet, and inject it to the "pkts"
3087 * array, to looks like that guest actually send such packet.
3089 * Check user_send_rarp() for more information.
3091 * broadcast_rarp shares a cacheline in the virtio_net structure
3092 * with some fields that are accessed during enqueue and
3093 * __atomic_compare_exchange_n causes a write if performed compare
3094 * and exchange. This could result in false sharing between enqueue
3097 * Prevent unnecessary false sharing by reading broadcast_rarp first
3098 * and only performing compare and exchange if the read indicates it
3099 * is likely to be set.
3101 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3102 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3103 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3105 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3106 if (rarp_mbuf == NULL) {
3107 VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
3112 * Inject it to the head of "pkts" array, so that switch's mac
3113 * learning table will get updated first.
3115 pkts[0] = rarp_mbuf;
3120 if (vq_is_packed(dev)) {
3121 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3122 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3124 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3126 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3127 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3129 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3133 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3134 vhost_user_iotlb_rd_unlock(vq);
3137 rte_spinlock_unlock(&vq->access_lock);
3139 if (unlikely(rarp_mbuf != NULL))