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 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1011 buf_iova + buf_offset, cpy_len, &mapped_len);
1012 if (unlikely(!hpa)) {
1013 VHOST_LOG_DATA(ERR, "(%s) %s: failed to get hpa.\n", dev->ifname, __func__);
1017 if (unlikely(async_iter_add_iovec(dev, async,
1018 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1020 hpa, (size_t)mapped_len)))
1023 cpy_len -= (uint32_t)mapped_len;
1024 mbuf_offset += (uint32_t)mapped_len;
1025 buf_offset += (uint32_t)mapped_len;
1031 static __rte_always_inline void
1032 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1033 struct rte_mbuf *m, uint32_t mbuf_offset,
1034 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
1036 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1038 if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
1039 rte_memcpy((void *)((uintptr_t)(buf_addr)),
1040 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1042 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1043 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1045 batch_copy[vq->batch_copy_nb_elems].dst =
1046 (void *)((uintptr_t)(buf_addr));
1047 batch_copy[vq->batch_copy_nb_elems].src =
1048 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1049 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1050 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1051 vq->batch_copy_nb_elems++;
1055 static __rte_always_inline int
1056 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1057 struct rte_mbuf *m, struct buf_vector *buf_vec,
1058 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1060 uint32_t vec_idx = 0;
1061 uint32_t mbuf_offset, mbuf_avail;
1062 uint32_t buf_offset, buf_avail;
1063 uint64_t buf_addr, buf_iova, buf_len;
1066 struct rte_mbuf *hdr_mbuf;
1067 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1068 struct vhost_async *async = vq->async;
1070 if (unlikely(m == NULL))
1073 buf_addr = buf_vec[vec_idx].buf_addr;
1074 buf_iova = buf_vec[vec_idx].buf_iova;
1075 buf_len = buf_vec[vec_idx].buf_len;
1077 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1081 hdr_addr = buf_addr;
1082 if (unlikely(buf_len < dev->vhost_hlen)) {
1083 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1086 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1088 VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
1089 dev->ifname, num_buffers);
1091 if (unlikely(buf_len < dev->vhost_hlen)) {
1092 buf_offset = dev->vhost_hlen - buf_len;
1094 buf_addr = buf_vec[vec_idx].buf_addr;
1095 buf_iova = buf_vec[vec_idx].buf_iova;
1096 buf_len = buf_vec[vec_idx].buf_len;
1097 buf_avail = buf_len - buf_offset;
1099 buf_offset = dev->vhost_hlen;
1100 buf_avail = buf_len - dev->vhost_hlen;
1103 mbuf_avail = rte_pktmbuf_data_len(m);
1107 if (async_iter_initialize(dev, async))
1111 while (mbuf_avail != 0 || m->next != NULL) {
1112 /* done with current buf, get the next one */
1113 if (buf_avail == 0) {
1115 if (unlikely(vec_idx >= nr_vec))
1118 buf_addr = buf_vec[vec_idx].buf_addr;
1119 buf_iova = buf_vec[vec_idx].buf_iova;
1120 buf_len = buf_vec[vec_idx].buf_len;
1123 buf_avail = buf_len;
1126 /* done with current mbuf, get the next one */
1127 if (mbuf_avail == 0) {
1131 mbuf_avail = rte_pktmbuf_data_len(m);
1135 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1136 if (rxvq_is_mergeable(dev))
1137 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1140 if (unlikely(hdr == &tmp_hdr)) {
1141 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1143 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1144 dev->vhost_hlen, 0);
1145 vhost_log_cache_write_iova(dev, vq,
1146 buf_vec[0].buf_iova,
1153 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1156 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1157 buf_iova + buf_offset, cpy_len) < 0)
1160 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1161 buf_addr + buf_offset,
1162 buf_iova + buf_offset, cpy_len);
1165 mbuf_avail -= cpy_len;
1166 mbuf_offset += cpy_len;
1167 buf_avail -= cpy_len;
1168 buf_offset += cpy_len;
1172 async_iter_finalize(async);
1177 async_iter_cancel(async);
1182 static __rte_always_inline int
1183 vhost_enqueue_single_packed(struct virtio_net *dev,
1184 struct vhost_virtqueue *vq,
1185 struct rte_mbuf *pkt,
1186 struct buf_vector *buf_vec,
1189 uint16_t nr_vec = 0;
1190 uint16_t avail_idx = vq->last_avail_idx;
1191 uint16_t max_tries, tries = 0;
1192 uint16_t buf_id = 0;
1194 uint16_t desc_count;
1195 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1196 uint16_t num_buffers = 0;
1197 uint32_t buffer_len[vq->size];
1198 uint16_t buffer_buf_id[vq->size];
1199 uint16_t buffer_desc_count[vq->size];
1201 if (rxvq_is_mergeable(dev))
1202 max_tries = vq->size - 1;
1208 * if we tried all available ring items, and still
1209 * can't get enough buf, it means something abnormal
1212 if (unlikely(++tries > max_tries))
1215 if (unlikely(fill_vec_buf_packed(dev, vq,
1216 avail_idx, &desc_count,
1219 VHOST_ACCESS_RW) < 0))
1222 len = RTE_MIN(len, size);
1225 buffer_len[num_buffers] = len;
1226 buffer_buf_id[num_buffers] = buf_id;
1227 buffer_desc_count[num_buffers] = desc_count;
1230 *nr_descs += desc_count;
1231 avail_idx += desc_count;
1232 if (avail_idx >= vq->size)
1233 avail_idx -= vq->size;
1236 if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1239 vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1240 buffer_desc_count, num_buffers);
1245 static __rte_noinline uint32_t
1246 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1247 struct rte_mbuf **pkts, uint32_t count)
1249 uint32_t pkt_idx = 0;
1250 uint16_t num_buffers;
1251 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1252 uint16_t avail_head;
1255 * The ordering between avail index and
1256 * desc reads needs to be enforced.
1258 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1260 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1262 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1263 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1264 uint16_t nr_vec = 0;
1266 if (unlikely(reserve_avail_buf_split(dev, vq,
1267 pkt_len, buf_vec, &num_buffers,
1268 avail_head, &nr_vec) < 0)) {
1269 VHOST_LOG_DATA(DEBUG,
1270 "(%s) failed to get enough desc from vring\n",
1272 vq->shadow_used_idx -= num_buffers;
1276 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1277 dev->ifname, vq->last_avail_idx,
1278 vq->last_avail_idx + num_buffers);
1280 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1281 num_buffers, false) < 0) {
1282 vq->shadow_used_idx -= num_buffers;
1286 vq->last_avail_idx += num_buffers;
1289 do_data_copy_enqueue(dev, vq);
1291 if (likely(vq->shadow_used_idx)) {
1292 flush_shadow_used_ring_split(dev, vq);
1293 vhost_vring_call_split(dev, vq);
1299 static __rte_always_inline int
1300 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1301 struct vhost_virtqueue *vq,
1302 struct rte_mbuf **pkts,
1303 uint64_t *desc_addrs,
1306 bool wrap_counter = vq->avail_wrap_counter;
1307 struct vring_packed_desc *descs = vq->desc_packed;
1308 uint16_t avail_idx = vq->last_avail_idx;
1309 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1312 if (unlikely(avail_idx & PACKED_BATCH_MASK))
1315 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1318 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1319 if (unlikely(pkts[i]->next != NULL))
1321 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1326 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1327 lens[i] = descs[avail_idx + i].len;
1329 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1330 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1334 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1335 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1336 descs[avail_idx + i].addr,
1340 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1341 if (unlikely(!desc_addrs[i]))
1343 if (unlikely(lens[i] != descs[avail_idx + i].len))
1350 static __rte_always_inline void
1351 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1352 struct vhost_virtqueue *vq,
1353 struct rte_mbuf **pkts,
1354 uint64_t *desc_addrs,
1357 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1358 struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1359 struct vring_packed_desc *descs = vq->desc_packed;
1360 uint16_t avail_idx = vq->last_avail_idx;
1361 uint16_t ids[PACKED_BATCH_SIZE];
1364 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1365 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1366 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1367 (uintptr_t)desc_addrs[i];
1368 lens[i] = pkts[i]->pkt_len +
1369 sizeof(struct virtio_net_hdr_mrg_rxbuf);
1372 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1373 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1375 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1377 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1378 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1379 rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1383 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1384 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1387 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1388 ids[i] = descs[avail_idx + i].id;
1390 vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1393 static __rte_always_inline int
1394 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1395 struct vhost_virtqueue *vq,
1396 struct rte_mbuf **pkts)
1398 uint64_t desc_addrs[PACKED_BATCH_SIZE];
1399 uint64_t lens[PACKED_BATCH_SIZE];
1401 if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1404 if (vq->shadow_used_idx) {
1405 do_data_copy_enqueue(dev, vq);
1406 vhost_flush_enqueue_shadow_packed(dev, vq);
1409 virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1414 static __rte_always_inline int16_t
1415 virtio_dev_rx_single_packed(struct virtio_net *dev,
1416 struct vhost_virtqueue *vq,
1417 struct rte_mbuf *pkt)
1419 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1420 uint16_t nr_descs = 0;
1422 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1424 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1429 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1430 dev->ifname, vq->last_avail_idx,
1431 vq->last_avail_idx + nr_descs);
1433 vq_inc_last_avail_packed(vq, nr_descs);
1438 static __rte_noinline uint32_t
1439 virtio_dev_rx_packed(struct virtio_net *dev,
1440 struct vhost_virtqueue *__rte_restrict vq,
1441 struct rte_mbuf **__rte_restrict pkts,
1444 uint32_t pkt_idx = 0;
1447 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1449 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1450 if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1452 pkt_idx += PACKED_BATCH_SIZE;
1457 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1461 } while (pkt_idx < count);
1463 if (vq->shadow_used_idx) {
1464 do_data_copy_enqueue(dev, vq);
1465 vhost_flush_enqueue_shadow_packed(dev, vq);
1469 vhost_vring_call_packed(dev, vq);
1474 static __rte_always_inline uint32_t
1475 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1476 struct rte_mbuf **pkts, uint32_t count)
1478 struct vhost_virtqueue *vq;
1481 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1482 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1483 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1484 dev->ifname, __func__, queue_id);
1488 vq = dev->virtqueue[queue_id];
1490 rte_spinlock_lock(&vq->access_lock);
1492 if (unlikely(!vq->enabled))
1493 goto out_access_unlock;
1495 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1496 vhost_user_iotlb_rd_lock(vq);
1498 if (unlikely(!vq->access_ok))
1499 if (unlikely(vring_translate(dev, vq) < 0))
1502 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1506 if (vq_is_packed(dev))
1507 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1509 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1512 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1513 vhost_user_iotlb_rd_unlock(vq);
1516 rte_spinlock_unlock(&vq->access_lock);
1522 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1523 struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1525 struct virtio_net *dev = get_device(vid);
1530 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1531 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1532 dev->ifname, __func__);
1536 return virtio_dev_rx(dev, queue_id, pkts, count);
1539 static __rte_always_inline uint16_t
1540 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1542 struct vhost_async *async = vq->async;
1544 if (async->pkts_idx >= async->pkts_inflight_n)
1545 return async->pkts_idx - async->pkts_inflight_n;
1547 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1550 static __rte_always_inline void
1551 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1552 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1554 size_t elem_size = sizeof(struct vring_used_elem);
1556 if (d_idx + count <= ring_size) {
1557 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1559 uint16_t size = ring_size - d_idx;
1561 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1562 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1566 static __rte_always_inline void
1567 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1568 struct vring_used_elem_packed *d_ring,
1569 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1571 size_t elem_size = sizeof(struct vring_used_elem_packed);
1573 if (d_idx + count <= ring_size) {
1574 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1576 uint16_t size = ring_size - d_idx;
1578 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1579 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1583 static __rte_noinline uint32_t
1584 virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1585 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1586 int16_t dma_id, uint16_t vchan_id)
1588 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1589 uint32_t pkt_idx = 0;
1590 uint16_t num_buffers;
1591 uint16_t avail_head;
1593 struct vhost_async *async = vq->async;
1594 struct async_inflight_info *pkts_info = async->pkts_info;
1595 uint32_t pkt_err = 0;
1597 uint16_t slot_idx = 0;
1600 * The ordering between avail index and desc reads need to be enforced.
1602 avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1604 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1606 async_iter_reset(async);
1608 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1609 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1610 uint16_t nr_vec = 0;
1612 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1613 &num_buffers, avail_head, &nr_vec) < 0)) {
1614 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1616 vq->shadow_used_idx -= num_buffers;
1620 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1621 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1623 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1624 vq->shadow_used_idx -= num_buffers;
1628 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1629 pkts_info[slot_idx].descs = num_buffers;
1630 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1632 vq->last_avail_idx += num_buffers;
1635 if (unlikely(pkt_idx == 0))
1638 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1639 async->iov_iter, pkt_idx);
1641 pkt_err = pkt_idx - n_xfer;
1642 if (unlikely(pkt_err)) {
1643 uint16_t num_descs = 0;
1645 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1646 dev->ifname, __func__, pkt_err, queue_id);
1648 /* update number of completed packets */
1651 /* calculate the sum of descriptors to revert */
1652 while (pkt_err-- > 0) {
1653 num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1657 /* recover shadow used ring and available ring */
1658 vq->shadow_used_idx -= num_descs;
1659 vq->last_avail_idx -= num_descs;
1662 /* keep used descriptors */
1663 if (likely(vq->shadow_used_idx)) {
1664 uint16_t to = async->desc_idx_split & (vq->size - 1);
1666 store_dma_desc_info_split(vq->shadow_used_split,
1667 async->descs_split, vq->size, 0, to,
1668 vq->shadow_used_idx);
1670 async->desc_idx_split += vq->shadow_used_idx;
1672 async->pkts_idx += pkt_idx;
1673 if (async->pkts_idx >= vq->size)
1674 async->pkts_idx -= vq->size;
1676 async->pkts_inflight_n += pkt_idx;
1677 vq->shadow_used_idx = 0;
1684 static __rte_always_inline int
1685 vhost_enqueue_async_packed(struct virtio_net *dev,
1686 struct vhost_virtqueue *vq,
1687 struct rte_mbuf *pkt,
1688 struct buf_vector *buf_vec,
1690 uint16_t *nr_buffers)
1692 uint16_t nr_vec = 0;
1693 uint16_t avail_idx = vq->last_avail_idx;
1694 uint16_t max_tries, tries = 0;
1695 uint16_t buf_id = 0;
1697 uint16_t desc_count = 0;
1698 uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1699 uint32_t buffer_len[vq->size];
1700 uint16_t buffer_buf_id[vq->size];
1701 uint16_t buffer_desc_count[vq->size];
1703 if (rxvq_is_mergeable(dev))
1704 max_tries = vq->size - 1;
1710 * if we tried all available ring items, and still
1711 * can't get enough buf, it means something abnormal
1714 if (unlikely(++tries > max_tries))
1717 if (unlikely(fill_vec_buf_packed(dev, vq,
1718 avail_idx, &desc_count,
1721 VHOST_ACCESS_RW) < 0))
1724 len = RTE_MIN(len, size);
1727 buffer_len[*nr_buffers] = len;
1728 buffer_buf_id[*nr_buffers] = buf_id;
1729 buffer_desc_count[*nr_buffers] = desc_count;
1731 *nr_descs += desc_count;
1732 avail_idx += desc_count;
1733 if (avail_idx >= vq->size)
1734 avail_idx -= vq->size;
1737 if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1740 vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1745 static __rte_always_inline int16_t
1746 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1747 struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1749 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1751 if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1752 nr_descs, nr_buffers) < 0)) {
1753 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1757 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1758 dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1763 static __rte_always_inline void
1764 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1765 uint32_t nr_err, uint32_t *pkt_idx)
1767 uint16_t descs_err = 0;
1768 uint16_t buffers_err = 0;
1769 struct async_inflight_info *pkts_info = vq->async->pkts_info;
1772 /* calculate the sum of buffers and descs of DMA-error packets. */
1773 while (nr_err-- > 0) {
1774 descs_err += pkts_info[slot_idx % vq->size].descs;
1775 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1779 if (vq->last_avail_idx >= descs_err) {
1780 vq->last_avail_idx -= descs_err;
1782 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1783 vq->avail_wrap_counter ^= 1;
1786 vq->shadow_used_idx -= buffers_err;
1789 static __rte_noinline uint32_t
1790 virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1791 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1792 int16_t dma_id, uint16_t vchan_id)
1794 uint32_t pkt_idx = 0;
1795 uint32_t remained = count;
1797 uint16_t num_buffers;
1800 struct vhost_async *async = vq->async;
1801 struct async_inflight_info *pkts_info = async->pkts_info;
1802 uint32_t pkt_err = 0;
1803 uint16_t slot_idx = 0;
1806 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1810 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1811 &num_descs, &num_buffers) < 0))
1814 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1816 pkts_info[slot_idx].descs = num_descs;
1817 pkts_info[slot_idx].nr_buffers = num_buffers;
1818 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1822 vq_inc_last_avail_packed(vq, num_descs);
1823 } while (pkt_idx < count);
1825 if (unlikely(pkt_idx == 0))
1828 n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1829 async->iov_iter, pkt_idx);
1831 async_iter_reset(async);
1833 pkt_err = pkt_idx - n_xfer;
1834 if (unlikely(pkt_err)) {
1835 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1836 dev->ifname, __func__, pkt_err, queue_id);
1837 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1840 if (likely(vq->shadow_used_idx)) {
1841 /* keep used descriptors. */
1842 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1843 vq->size, 0, async->buffer_idx_packed,
1844 vq->shadow_used_idx);
1846 async->buffer_idx_packed += vq->shadow_used_idx;
1847 if (async->buffer_idx_packed >= vq->size)
1848 async->buffer_idx_packed -= vq->size;
1850 async->pkts_idx += pkt_idx;
1851 if (async->pkts_idx >= vq->size)
1852 async->pkts_idx -= vq->size;
1854 vq->shadow_used_idx = 0;
1855 async->pkts_inflight_n += pkt_idx;
1861 static __rte_always_inline void
1862 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1864 struct vhost_async *async = vq->async;
1865 uint16_t nr_left = n_descs;
1870 from = async->last_desc_idx_split & (vq->size - 1);
1871 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1872 to = vq->last_used_idx & (vq->size - 1);
1874 if (to + nr_copy <= vq->size) {
1875 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1876 nr_copy * sizeof(struct vring_used_elem));
1878 uint16_t size = vq->size - to;
1880 rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1881 size * sizeof(struct vring_used_elem));
1882 rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1883 (nr_copy - size) * sizeof(struct vring_used_elem));
1886 async->last_desc_idx_split += nr_copy;
1887 vq->last_used_idx += nr_copy;
1889 } while (nr_left > 0);
1892 static __rte_always_inline void
1893 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1896 struct vhost_async *async = vq->async;
1897 uint16_t from = async->last_buffer_idx_packed;
1898 uint16_t used_idx = vq->last_used_idx;
1899 uint16_t head_idx = vq->last_used_idx;
1900 uint16_t head_flags = 0;
1903 /* Split loop in two to save memory barriers */
1904 for (i = 0; i < n_buffers; i++) {
1905 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1906 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1908 used_idx += async->buffers_packed[from].count;
1909 if (used_idx >= vq->size)
1910 used_idx -= vq->size;
1913 if (from >= vq->size)
1917 /* The ordering for storing desc flags needs to be enforced. */
1918 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1920 from = async->last_buffer_idx_packed;
1922 for (i = 0; i < n_buffers; i++) {
1925 if (async->buffers_packed[from].len)
1926 flags = VRING_DESC_F_WRITE;
1930 if (vq->used_wrap_counter) {
1931 flags |= VRING_DESC_F_USED;
1932 flags |= VRING_DESC_F_AVAIL;
1934 flags &= ~VRING_DESC_F_USED;
1935 flags &= ~VRING_DESC_F_AVAIL;
1939 vq->desc_packed[vq->last_used_idx].flags = flags;
1941 head_idx = vq->last_used_idx;
1945 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
1948 if (from == vq->size)
1952 vq->desc_packed[head_idx].flags = head_flags;
1953 async->last_buffer_idx_packed = from;
1956 static __rte_always_inline uint16_t
1957 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1958 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
1961 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1962 struct vhost_async *async = vq->async;
1963 struct async_inflight_info *pkts_info = async->pkts_info;
1964 uint16_t nr_cpl_pkts = 0;
1965 uint16_t n_descs = 0, n_buffers = 0;
1966 uint16_t start_idx, from, i;
1968 /* Check completed copies for the given DMA vChannel */
1969 vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
1971 start_idx = async_get_first_inflight_pkt_idx(vq);
1973 * Calculate the number of copy completed packets.
1974 * Note that there may be completed packets even if
1975 * no copies are reported done by the given DMA vChannel,
1976 * as it's possible that a virtqueue uses multiple DMA
1980 while (vq->async->pkts_cmpl_flag[from] && count--) {
1981 vq->async->pkts_cmpl_flag[from] = false;
1983 if (from >= vq->size)
1988 if (nr_cpl_pkts == 0)
1991 for (i = 0; i < nr_cpl_pkts; i++) {
1992 from = (start_idx + i) % vq->size;
1993 /* Only used with packed ring */
1994 n_buffers += pkts_info[from].nr_buffers;
1995 /* Only used with split ring */
1996 n_descs += pkts_info[from].descs;
1997 pkts[i] = pkts_info[from].mbuf;
2000 async->pkts_inflight_n -= nr_cpl_pkts;
2002 if (likely(vq->enabled && vq->access_ok)) {
2003 if (vq_is_packed(dev)) {
2004 write_back_completed_descs_packed(vq, n_buffers);
2005 vhost_vring_call_packed(dev, vq);
2007 write_back_completed_descs_split(vq, n_descs);
2008 __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
2009 vhost_vring_call_split(dev, vq);
2012 if (vq_is_packed(dev)) {
2013 async->last_buffer_idx_packed += n_buffers;
2014 if (async->last_buffer_idx_packed >= vq->size)
2015 async->last_buffer_idx_packed -= vq->size;
2017 async->last_desc_idx_split += n_descs;
2025 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2026 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2029 struct virtio_net *dev = get_device(vid);
2030 struct vhost_virtqueue *vq;
2031 uint16_t n_pkts_cpl = 0;
2036 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2037 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2038 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2039 dev->ifname, __func__, queue_id);
2043 if (unlikely(!dma_copy_track[dma_id].vchans ||
2044 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2045 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2050 vq = dev->virtqueue[queue_id];
2052 if (!rte_spinlock_trylock(&vq->access_lock)) {
2053 VHOST_LOG_DATA(DEBUG, "(%s) %s: virtqueue %u is busy.\n", dev->ifname, __func__,
2058 if (unlikely(!vq->async)) {
2059 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for virtqueue %d.\n",
2060 dev->ifname, __func__, queue_id);
2064 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2067 rte_spinlock_unlock(&vq->access_lock);
2073 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2074 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2077 struct virtio_net *dev = get_device(vid);
2078 struct vhost_virtqueue *vq;
2079 uint16_t n_pkts_cpl = 0;
2084 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2085 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2086 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2087 dev->ifname, __func__, queue_id);
2091 vq = dev->virtqueue[queue_id];
2093 if (unlikely(!vq->async)) {
2094 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
2095 dev->ifname, __func__, queue_id);
2099 if (unlikely(!dma_copy_track[dma_id].vchans ||
2100 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2101 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2106 n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2111 static __rte_always_inline uint32_t
2112 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2113 struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2115 struct vhost_virtqueue *vq;
2118 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2119 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2120 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2121 dev->ifname, __func__, queue_id);
2125 if (unlikely(!dma_copy_track[dma_id].vchans ||
2126 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2127 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2132 vq = dev->virtqueue[queue_id];
2134 rte_spinlock_lock(&vq->access_lock);
2136 if (unlikely(!vq->enabled || !vq->async))
2137 goto out_access_unlock;
2139 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2140 vhost_user_iotlb_rd_lock(vq);
2142 if (unlikely(!vq->access_ok))
2143 if (unlikely(vring_translate(dev, vq) < 0))
2146 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2150 if (vq_is_packed(dev))
2151 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2152 pkts, count, dma_id, vchan_id);
2154 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2155 pkts, count, dma_id, vchan_id);
2158 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2159 vhost_user_iotlb_rd_unlock(vq);
2162 rte_spinlock_unlock(&vq->access_lock);
2168 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2169 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2172 struct virtio_net *dev = get_device(vid);
2177 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2178 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2179 dev->ifname, __func__);
2183 return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, dma_id, vchan_id);
2187 virtio_net_with_host_offload(struct virtio_net *dev)
2190 ((1ULL << VIRTIO_NET_F_CSUM) |
2191 (1ULL << VIRTIO_NET_F_HOST_ECN) |
2192 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2193 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2194 (1ULL << VIRTIO_NET_F_HOST_UFO)))
2201 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2203 struct rte_ipv4_hdr *ipv4_hdr;
2204 struct rte_ipv6_hdr *ipv6_hdr;
2205 struct rte_ether_hdr *eth_hdr;
2207 uint16_t data_len = rte_pktmbuf_data_len(m);
2209 if (data_len < sizeof(struct rte_ether_hdr))
2212 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2214 m->l2_len = sizeof(struct rte_ether_hdr);
2215 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2217 if (ethertype == RTE_ETHER_TYPE_VLAN) {
2218 if (data_len < sizeof(struct rte_ether_hdr) +
2219 sizeof(struct rte_vlan_hdr))
2222 struct rte_vlan_hdr *vlan_hdr =
2223 (struct rte_vlan_hdr *)(eth_hdr + 1);
2225 m->l2_len += sizeof(struct rte_vlan_hdr);
2226 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2229 switch (ethertype) {
2230 case RTE_ETHER_TYPE_IPV4:
2231 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2233 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2235 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2236 if (data_len < m->l2_len + m->l3_len)
2238 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2239 *l4_proto = ipv4_hdr->next_proto_id;
2241 case RTE_ETHER_TYPE_IPV6:
2242 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2244 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2246 m->l3_len = sizeof(struct rte_ipv6_hdr);
2247 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2248 *l4_proto = ipv6_hdr->proto;
2251 /* a valid L3 header is needed for further L4 parsing */
2255 /* both CSUM and GSO need a valid L4 header */
2256 switch (*l4_proto) {
2258 if (data_len < m->l2_len + m->l3_len +
2259 sizeof(struct rte_tcp_hdr))
2263 if (data_len < m->l2_len + m->l3_len +
2264 sizeof(struct rte_udp_hdr))
2268 if (data_len < m->l2_len + m->l3_len +
2269 sizeof(struct rte_sctp_hdr))
2285 static __rte_always_inline void
2286 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2289 uint8_t l4_proto = 0;
2290 struct rte_tcp_hdr *tcp_hdr = NULL;
2292 uint16_t data_len = rte_pktmbuf_data_len(m);
2294 if (parse_headers(m, &l4_proto) < 0)
2297 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2298 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2299 switch (hdr->csum_offset) {
2300 case (offsetof(struct rte_tcp_hdr, cksum)):
2301 if (l4_proto != IPPROTO_TCP)
2303 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2305 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2306 if (l4_proto != IPPROTO_UDP)
2308 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2310 case (offsetof(struct rte_sctp_hdr, cksum)):
2311 if (l4_proto != IPPROTO_SCTP)
2313 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2323 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2324 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2325 case VIRTIO_NET_HDR_GSO_TCPV4:
2326 case VIRTIO_NET_HDR_GSO_TCPV6:
2327 if (l4_proto != IPPROTO_TCP)
2329 tcp_hdr = rte_pktmbuf_mtod_offset(m,
2330 struct rte_tcp_hdr *,
2331 m->l2_len + m->l3_len);
2332 tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2333 if (data_len < m->l2_len + m->l3_len + tcp_len)
2335 m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2336 m->tso_segsz = hdr->gso_size;
2337 m->l4_len = tcp_len;
2339 case VIRTIO_NET_HDR_GSO_UDP:
2340 if (l4_proto != IPPROTO_UDP)
2342 m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2343 m->tso_segsz = hdr->gso_size;
2344 m->l4_len = sizeof(struct rte_udp_hdr);
2347 VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2348 dev->ifname, hdr->gso_type);
2360 static __rte_always_inline void
2361 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2362 struct rte_mbuf *m, bool legacy_ol_flags)
2364 struct rte_net_hdr_lens hdr_lens;
2365 int l4_supported = 0;
2368 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2371 if (legacy_ol_flags) {
2372 vhost_dequeue_offload_legacy(dev, hdr, m);
2376 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2378 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2379 m->packet_type = ptype;
2380 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2381 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2382 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2385 /* According to Virtio 1.1 spec, the device only needs to look at
2386 * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2387 * This differs from the processing incoming packets path where the
2388 * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2391 * 5.1.6.2.1 Driver Requirements: Packet Transmission
2392 * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2393 * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2395 * 5.1.6.2.2 Device Requirements: Packet Transmission
2396 * The device MUST ignore flag bits that it does not recognize.
2398 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2401 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2402 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2403 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2405 /* Unknown proto or tunnel, do sw cksum. We can assume
2406 * the cksum field is in the first segment since the
2407 * buffers we provided to the host are large enough.
2408 * In case of SCTP, this will be wrong since it's a CRC
2409 * but there's nothing we can do.
2411 uint16_t csum = 0, off;
2413 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2414 rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2416 if (likely(csum != 0xffff))
2418 off = hdr->csum_offset + hdr->csum_start;
2419 if (rte_pktmbuf_data_len(m) >= off + 1)
2420 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2424 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2425 if (hdr->gso_size == 0)
2428 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2429 case VIRTIO_NET_HDR_GSO_TCPV4:
2430 case VIRTIO_NET_HDR_GSO_TCPV6:
2431 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2433 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2434 m->tso_segsz = hdr->gso_size;
2436 case VIRTIO_NET_HDR_GSO_UDP:
2437 if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2439 m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2440 m->tso_segsz = hdr->gso_size;
2448 static __rte_noinline void
2449 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2450 struct buf_vector *buf_vec)
2453 uint64_t remain = sizeof(struct virtio_net_hdr);
2455 uint64_t dst = (uint64_t)(uintptr_t)hdr;
2458 len = RTE_MIN(remain, buf_vec->buf_len);
2459 src = buf_vec->buf_addr;
2460 rte_memcpy((void *)(uintptr_t)dst,
2461 (void *)(uintptr_t)src, len);
2469 static __rte_always_inline int
2470 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2471 struct buf_vector *buf_vec, uint16_t nr_vec,
2472 struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2473 bool legacy_ol_flags)
2475 uint32_t buf_avail, buf_offset;
2476 uint64_t buf_addr, buf_len;
2477 uint32_t mbuf_avail, mbuf_offset;
2479 struct rte_mbuf *cur = m, *prev = m;
2480 struct virtio_net_hdr tmp_hdr;
2481 struct virtio_net_hdr *hdr = NULL;
2482 /* A counter to avoid desc dead loop chain */
2483 uint16_t vec_idx = 0;
2484 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2487 buf_addr = buf_vec[vec_idx].buf_addr;
2488 buf_len = buf_vec[vec_idx].buf_len;
2490 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2495 if (virtio_net_with_host_offload(dev)) {
2496 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2498 * No luck, the virtio-net header doesn't fit
2499 * in a contiguous virtual area.
2501 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2504 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2509 * A virtio driver normally uses at least 2 desc buffers
2510 * for Tx: the first for storing the header, and others
2511 * for storing the data.
2513 if (unlikely(buf_len < dev->vhost_hlen)) {
2514 buf_offset = dev->vhost_hlen - buf_len;
2516 buf_addr = buf_vec[vec_idx].buf_addr;
2517 buf_len = buf_vec[vec_idx].buf_len;
2518 buf_avail = buf_len - buf_offset;
2519 } else if (buf_len == dev->vhost_hlen) {
2520 if (unlikely(++vec_idx >= nr_vec))
2522 buf_addr = buf_vec[vec_idx].buf_addr;
2523 buf_len = buf_vec[vec_idx].buf_len;
2526 buf_avail = buf_len;
2528 buf_offset = dev->vhost_hlen;
2529 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2533 (uintptr_t)(buf_addr + buf_offset),
2534 (uint32_t)buf_avail, 0);
2537 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2539 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2541 if (likely(cpy_len > MAX_BATCH_LEN ||
2542 vq->batch_copy_nb_elems >= vq->size ||
2543 (hdr && cur == m))) {
2544 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2546 (void *)((uintptr_t)(buf_addr +
2547 buf_offset)), cpy_len);
2549 batch_copy[vq->batch_copy_nb_elems].dst =
2550 rte_pktmbuf_mtod_offset(cur, void *,
2552 batch_copy[vq->batch_copy_nb_elems].src =
2553 (void *)((uintptr_t)(buf_addr + buf_offset));
2554 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2555 vq->batch_copy_nb_elems++;
2558 mbuf_avail -= cpy_len;
2559 mbuf_offset += cpy_len;
2560 buf_avail -= cpy_len;
2561 buf_offset += cpy_len;
2563 /* This buf reaches to its end, get the next one */
2564 if (buf_avail == 0) {
2565 if (++vec_idx >= nr_vec)
2568 buf_addr = buf_vec[vec_idx].buf_addr;
2569 buf_len = buf_vec[vec_idx].buf_len;
2572 buf_avail = buf_len;
2574 PRINT_PACKET(dev, (uintptr_t)buf_addr,
2575 (uint32_t)buf_avail, 0);
2579 * This mbuf reaches to its end, get a new one
2580 * to hold more data.
2582 if (mbuf_avail == 0) {
2583 cur = rte_pktmbuf_alloc(mbuf_pool);
2584 if (unlikely(cur == NULL)) {
2585 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2592 prev->data_len = mbuf_offset;
2594 m->pkt_len += mbuf_offset;
2598 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2602 prev->data_len = mbuf_offset;
2603 m->pkt_len += mbuf_offset;
2606 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2614 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2620 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2622 struct rte_mbuf_ext_shared_info *shinfo = NULL;
2623 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2628 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2629 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2631 if (unlikely(total_len > UINT16_MAX))
2634 buf_len = total_len;
2635 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2636 if (unlikely(buf == NULL))
2639 /* Initialize shinfo */
2640 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2641 virtio_dev_extbuf_free, buf);
2642 if (unlikely(shinfo == NULL)) {
2644 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2648 iova = rte_malloc_virt2iova(buf);
2649 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2650 rte_pktmbuf_reset_headroom(pkt);
2656 * Prepare a host supported pktmbuf.
2658 static __rte_always_inline int
2659 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2662 if (rte_pktmbuf_tailroom(pkt) >= data_len)
2665 /* attach an external buffer if supported */
2666 if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2669 /* check if chained buffers are allowed */
2670 if (!dev->linearbuf)
2678 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2679 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2680 bool legacy_ol_flags)
2683 uint16_t free_entries;
2684 uint16_t dropped = 0;
2685 static bool allocerr_warned;
2688 * The ordering between avail index and
2689 * desc reads needs to be enforced.
2691 free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2693 if (free_entries == 0)
2696 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2698 VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2700 count = RTE_MIN(count, MAX_PKT_BURST);
2701 count = RTE_MIN(count, free_entries);
2702 VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2703 dev->ifname, count);
2705 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2708 for (i = 0; i < count; i++) {
2709 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2712 uint16_t nr_vec = 0;
2715 if (unlikely(fill_vec_buf_split(dev, vq,
2716 vq->last_avail_idx + i,
2718 &head_idx, &buf_len,
2719 VHOST_ACCESS_RO) < 0))
2722 update_shadow_used_ring_split(vq, head_idx, 0);
2724 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2725 if (unlikely(err)) {
2727 * mbuf allocation fails for jumbo packets when external
2728 * buffer allocation is not allowed and linear buffer
2729 * is required. Drop this packet.
2731 if (!allocerr_warned) {
2732 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2733 dev->ifname, buf_len, mbuf_pool->name);
2734 allocerr_warned = true;
2741 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2742 mbuf_pool, legacy_ol_flags);
2743 if (unlikely(err)) {
2744 if (!allocerr_warned) {
2745 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2747 allocerr_warned = true;
2756 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2758 vq->last_avail_idx += i;
2760 do_data_copy_dequeue(vq);
2761 if (unlikely(i < count))
2762 vq->shadow_used_idx = i;
2763 if (likely(vq->shadow_used_idx)) {
2764 flush_shadow_used_ring_split(dev, vq);
2765 vhost_vring_call_split(dev, vq);
2768 return (i - dropped);
2773 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2774 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2775 struct rte_mbuf **pkts, uint16_t count)
2777 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2782 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2783 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2784 struct rte_mbuf **pkts, uint16_t count)
2786 return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2789 static __rte_always_inline int
2790 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2791 struct vhost_virtqueue *vq,
2792 struct rte_mbuf **pkts,
2794 uintptr_t *desc_addrs,
2797 bool wrap = vq->avail_wrap_counter;
2798 struct vring_packed_desc *descs = vq->desc_packed;
2799 uint64_t lens[PACKED_BATCH_SIZE];
2800 uint64_t buf_lens[PACKED_BATCH_SIZE];
2801 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2804 if (unlikely(avail_idx & PACKED_BATCH_MASK))
2806 if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2809 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2810 flags = descs[avail_idx + i].flags;
2811 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2812 (wrap == !!(flags & VRING_DESC_F_USED)) ||
2813 (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2817 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2819 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2820 lens[i] = descs[avail_idx + i].len;
2822 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2823 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2824 descs[avail_idx + i].addr,
2825 &lens[i], VHOST_ACCESS_RW);
2828 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2829 if (unlikely(!desc_addrs[i]))
2831 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2835 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2836 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2840 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2841 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2843 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2844 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2848 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2849 pkts[i]->pkt_len = lens[i] - buf_offset;
2850 pkts[i]->data_len = pkts[i]->pkt_len;
2851 ids[i] = descs[avail_idx + i].id;
2860 static __rte_always_inline int
2861 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2862 struct vhost_virtqueue *vq,
2863 struct rte_mbuf **pkts,
2864 bool legacy_ol_flags)
2866 uint16_t avail_idx = vq->last_avail_idx;
2867 uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2868 struct virtio_net_hdr *hdr;
2869 uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2870 uint16_t ids[PACKED_BATCH_SIZE];
2873 if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2877 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2878 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2880 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2881 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2882 (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2885 if (virtio_net_with_host_offload(dev)) {
2886 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2887 hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2888 vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2892 if (virtio_net_is_inorder(dev))
2893 vhost_shadow_dequeue_batch_packed_inorder(vq,
2894 ids[PACKED_BATCH_SIZE - 1]);
2896 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2898 vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2903 static __rte_always_inline int
2904 vhost_dequeue_single_packed(struct virtio_net *dev,
2905 struct vhost_virtqueue *vq,
2906 struct rte_mempool *mbuf_pool,
2907 struct rte_mbuf *pkts,
2909 uint16_t *desc_count,
2910 bool legacy_ol_flags)
2912 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2914 uint16_t nr_vec = 0;
2916 static bool allocerr_warned;
2918 if (unlikely(fill_vec_buf_packed(dev, vq,
2919 vq->last_avail_idx, desc_count,
2922 VHOST_ACCESS_RO) < 0))
2925 if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2926 if (!allocerr_warned) {
2927 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2928 dev->ifname, buf_len, mbuf_pool->name);
2929 allocerr_warned = true;
2934 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2935 mbuf_pool, legacy_ol_flags);
2936 if (unlikely(err)) {
2937 if (!allocerr_warned) {
2938 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2940 allocerr_warned = true;
2948 static __rte_always_inline int
2949 virtio_dev_tx_single_packed(struct virtio_net *dev,
2950 struct vhost_virtqueue *vq,
2951 struct rte_mempool *mbuf_pool,
2952 struct rte_mbuf *pkts,
2953 bool legacy_ol_flags)
2956 uint16_t buf_id, desc_count = 0;
2959 ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2960 &desc_count, legacy_ol_flags);
2962 if (likely(desc_count > 0)) {
2963 if (virtio_net_is_inorder(dev))
2964 vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2967 vhost_shadow_dequeue_single_packed(vq, buf_id,
2970 vq_inc_last_avail_packed(vq, desc_count);
2978 virtio_dev_tx_packed(struct virtio_net *dev,
2979 struct vhost_virtqueue *__rte_restrict vq,
2980 struct rte_mempool *mbuf_pool,
2981 struct rte_mbuf **__rte_restrict pkts,
2983 bool legacy_ol_flags)
2985 uint32_t pkt_idx = 0;
2987 if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2991 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2993 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2994 if (!virtio_dev_tx_batch_packed(dev, vq,
2997 pkt_idx += PACKED_BATCH_SIZE;
3002 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3007 } while (pkt_idx < count);
3009 if (pkt_idx != count)
3010 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3012 if (vq->shadow_used_idx) {
3013 do_data_copy_dequeue(vq);
3015 vhost_flush_dequeue_shadow_packed(dev, vq);
3016 vhost_vring_call_packed(dev, vq);
3024 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3025 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3026 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3028 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3033 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3034 struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3035 struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3037 return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3041 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3042 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3044 struct virtio_net *dev;
3045 struct rte_mbuf *rarp_mbuf = NULL;
3046 struct vhost_virtqueue *vq;
3047 int16_t success = 1;
3049 dev = get_device(vid);
3053 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3054 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
3055 dev->ifname, __func__);
3059 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3060 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
3061 dev->ifname, __func__, queue_id);
3065 vq = dev->virtqueue[queue_id];
3067 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3070 if (unlikely(!vq->enabled)) {
3072 goto out_access_unlock;
3075 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3076 vhost_user_iotlb_rd_lock(vq);
3078 if (unlikely(!vq->access_ok))
3079 if (unlikely(vring_translate(dev, vq) < 0)) {
3085 * Construct a RARP broadcast packet, and inject it to the "pkts"
3086 * array, to looks like that guest actually send such packet.
3088 * Check user_send_rarp() for more information.
3090 * broadcast_rarp shares a cacheline in the virtio_net structure
3091 * with some fields that are accessed during enqueue and
3092 * __atomic_compare_exchange_n causes a write if performed compare
3093 * and exchange. This could result in false sharing between enqueue
3096 * Prevent unnecessary false sharing by reading broadcast_rarp first
3097 * and only performing compare and exchange if the read indicates it
3098 * is likely to be set.
3100 if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3101 __atomic_compare_exchange_n(&dev->broadcast_rarp,
3102 &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3104 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3105 if (rarp_mbuf == NULL) {
3106 VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
3111 * Inject it to the head of "pkts" array, so that switch's mac
3112 * learning table will get updated first.
3114 pkts[0] = rarp_mbuf;
3119 if (vq_is_packed(dev)) {
3120 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3121 count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3123 count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3125 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3126 count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3128 count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3132 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3133 vhost_user_iotlb_rd_unlock(vq);
3136 rte_spinlock_unlock(&vq->access_lock);
3138 if (unlikely(rarp_mbuf != NULL))