1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
24 #define MAX_PKT_BURST 32
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
37 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
40 static __rte_always_inline void
41 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42 struct vhost_virtqueue *vq,
43 uint16_t to, uint16_t from, uint16_t size)
45 rte_memcpy(&vq->used->ring[to],
46 &vq->shadow_used_split[from],
47 size * sizeof(struct vring_used_elem));
48 vhost_log_cache_used_vring(dev, vq,
49 offsetof(struct vring_used, ring[to]),
50 size * sizeof(struct vring_used_elem));
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
56 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
58 if (used_idx + vq->shadow_used_idx <= vq->size) {
59 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
64 /* update used ring interval [used_idx, vq->size] */
65 size = vq->size - used_idx;
66 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
68 /* update the left half used ring interval [0, left_size] */
69 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70 vq->shadow_used_idx - size);
72 vq->last_used_idx += vq->shadow_used_idx;
76 vhost_log_cache_sync(dev, vq);
78 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79 vq->shadow_used_idx = 0;
80 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81 sizeof(vq->used->idx));
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86 uint16_t desc_idx, uint32_t len)
88 uint16_t i = vq->shadow_used_idx++;
90 vq->shadow_used_split[i].id = desc_idx;
91 vq->shadow_used_split[i].len = len;
94 static __rte_always_inline void
95 flush_shadow_used_ring_packed(struct virtio_net *dev,
96 struct vhost_virtqueue *vq)
99 uint16_t used_idx = vq->last_used_idx;
100 uint16_t head_idx = vq->last_used_idx;
101 uint16_t head_flags = 0;
103 /* Split loop in two to save memory barriers */
104 for (i = 0; i < vq->shadow_used_idx; i++) {
105 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
108 used_idx += vq->shadow_used_packed[i].count;
109 if (used_idx >= vq->size)
110 used_idx -= vq->size;
113 for (i = 0; i < vq->shadow_used_idx; i++) {
116 if (vq->shadow_used_packed[i].len)
117 flags = VRING_DESC_F_WRITE;
121 if (vq->used_wrap_counter) {
122 flags |= VRING_DESC_F_USED;
123 flags |= VRING_DESC_F_AVAIL;
125 flags &= ~VRING_DESC_F_USED;
126 flags &= ~VRING_DESC_F_AVAIL;
130 vq->desc_packed[vq->last_used_idx].flags = flags;
132 vhost_log_cache_used_vring(dev, vq,
134 sizeof(struct vring_packed_desc),
135 sizeof(struct vring_packed_desc));
137 head_idx = vq->last_used_idx;
141 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
144 __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
147 vhost_log_cache_used_vring(dev, vq,
149 sizeof(struct vring_packed_desc),
150 sizeof(struct vring_packed_desc));
152 vq->shadow_used_idx = 0;
153 vhost_log_cache_sync(dev, vq);
156 static __rte_always_inline void
157 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
158 uint16_t desc_idx, uint32_t len, uint16_t count)
160 uint16_t i = vq->shadow_used_idx++;
162 vq->shadow_used_packed[i].id = desc_idx;
163 vq->shadow_used_packed[i].len = len;
164 vq->shadow_used_packed[i].count = count;
168 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
170 struct batch_copy_elem *elem = vq->batch_copy_elems;
171 uint16_t count = vq->batch_copy_nb_elems;
174 for (i = 0; i < count; i++) {
175 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
176 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
178 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
181 vq->batch_copy_nb_elems = 0;
185 do_data_copy_dequeue(struct vhost_virtqueue *vq)
187 struct batch_copy_elem *elem = vq->batch_copy_elems;
188 uint16_t count = vq->batch_copy_nb_elems;
191 for (i = 0; i < count; i++)
192 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
194 vq->batch_copy_nb_elems = 0;
197 /* avoid write operation when necessary, to lessen cache issues */
198 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
199 if ((var) != (val)) \
203 static __rte_always_inline void
204 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
206 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
208 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
209 csum_l4 |= PKT_TX_TCP_CKSUM;
212 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
213 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
216 case PKT_TX_TCP_CKSUM:
217 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
220 case PKT_TX_UDP_CKSUM:
221 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
224 case PKT_TX_SCTP_CKSUM:
225 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
230 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
231 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
232 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
235 /* IP cksum verification cannot be bypassed, then calculate here */
236 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
237 struct rte_ipv4_hdr *ipv4_hdr;
239 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
241 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
244 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
245 if (m_buf->ol_flags & PKT_TX_IPV4)
246 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
248 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
249 net_hdr->gso_size = m_buf->tso_segsz;
250 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
252 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
253 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
254 net_hdr->gso_size = m_buf->tso_segsz;
255 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
258 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
259 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
260 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
264 static __rte_always_inline int
265 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
266 struct buf_vector *buf_vec, uint16_t *vec_idx,
267 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
269 uint16_t vec_id = *vec_idx;
273 uint64_t desc_chunck_len = desc_len;
275 if (unlikely(vec_id >= BUF_VECTOR_MAX))
278 desc_addr = vhost_iova_to_vva(dev, vq,
282 if (unlikely(!desc_addr))
285 rte_prefetch0((void *)(uintptr_t)desc_addr);
287 buf_vec[vec_id].buf_iova = desc_iova;
288 buf_vec[vec_id].buf_addr = desc_addr;
289 buf_vec[vec_id].buf_len = desc_chunck_len;
291 desc_len -= desc_chunck_len;
292 desc_iova += desc_chunck_len;
300 static __rte_always_inline int
301 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
302 uint32_t avail_idx, uint16_t *vec_idx,
303 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
304 uint32_t *desc_chain_len, uint8_t perm)
306 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
307 uint16_t vec_id = *vec_idx;
310 uint32_t nr_descs = vq->size;
312 struct vring_desc *descs = vq->desc;
313 struct vring_desc *idesc = NULL;
315 if (unlikely(idx >= vq->size))
318 *desc_chain_head = idx;
320 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
321 dlen = vq->desc[idx].len;
322 nr_descs = dlen / sizeof(struct vring_desc);
323 if (unlikely(nr_descs > vq->size))
326 descs = (struct vring_desc *)(uintptr_t)
327 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
330 if (unlikely(!descs))
333 if (unlikely(dlen < vq->desc[idx].len)) {
335 * The indirect desc table is not contiguous
336 * in process VA space, we have to copy it.
338 idesc = vhost_alloc_copy_ind_table(dev, vq,
339 vq->desc[idx].addr, vq->desc[idx].len);
340 if (unlikely(!idesc))
350 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
351 free_ind_table(idesc);
355 len += descs[idx].len;
357 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
358 descs[idx].addr, descs[idx].len,
360 free_ind_table(idesc);
364 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
367 idx = descs[idx].next;
370 *desc_chain_len = len;
373 if (unlikely(!!idesc))
374 free_ind_table(idesc);
380 * Returns -1 on fail, 0 on success
383 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
384 uint32_t size, struct buf_vector *buf_vec,
385 uint16_t *num_buffers, uint16_t avail_head,
389 uint16_t vec_idx = 0;
390 uint16_t max_tries, tries = 0;
392 uint16_t head_idx = 0;
396 cur_idx = vq->last_avail_idx;
398 if (rxvq_is_mergeable(dev))
399 max_tries = vq->size - 1;
404 if (unlikely(cur_idx == avail_head))
407 * if we tried all available ring items, and still
408 * can't get enough buf, it means something abnormal
411 if (unlikely(++tries > max_tries))
414 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
417 VHOST_ACCESS_RW) < 0))
419 len = RTE_MIN(len, size);
420 update_shadow_used_ring_split(vq, head_idx, len);
432 static __rte_always_inline int
433 fill_vec_buf_packed_indirect(struct virtio_net *dev,
434 struct vhost_virtqueue *vq,
435 struct vring_packed_desc *desc, uint16_t *vec_idx,
436 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
440 uint16_t vec_id = *vec_idx;
442 struct vring_packed_desc *descs, *idescs = NULL;
445 descs = (struct vring_packed_desc *)(uintptr_t)
446 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
447 if (unlikely(!descs))
450 if (unlikely(dlen < desc->len)) {
452 * The indirect desc table is not contiguous
453 * in process VA space, we have to copy it.
455 idescs = vhost_alloc_copy_ind_table(dev,
456 vq, desc->addr, desc->len);
457 if (unlikely(!idescs))
463 nr_descs = desc->len / sizeof(struct vring_packed_desc);
464 if (unlikely(nr_descs >= vq->size)) {
465 free_ind_table(idescs);
469 for (i = 0; i < nr_descs; i++) {
470 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
471 free_ind_table(idescs);
475 *len += descs[i].len;
476 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
477 descs[i].addr, descs[i].len,
483 if (unlikely(!!idescs))
484 free_ind_table(idescs);
489 static __rte_always_inline int
490 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
491 uint16_t avail_idx, uint16_t *desc_count,
492 struct buf_vector *buf_vec, uint16_t *vec_idx,
493 uint16_t *buf_id, uint32_t *len, uint8_t perm)
495 bool wrap_counter = vq->avail_wrap_counter;
496 struct vring_packed_desc *descs = vq->desc_packed;
497 uint16_t vec_id = *vec_idx;
499 if (avail_idx < vq->last_avail_idx)
503 * Perform a load-acquire barrier in desc_is_avail to
504 * enforce the ordering between desc flags and desc
507 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
514 if (unlikely(vec_id >= BUF_VECTOR_MAX))
517 if (unlikely(*desc_count >= vq->size))
521 *buf_id = descs[avail_idx].id;
523 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
524 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
530 *len += descs[avail_idx].len;
532 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
533 descs[avail_idx].addr,
534 descs[avail_idx].len,
539 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
542 if (++avail_idx >= vq->size) {
543 avail_idx -= vq->size;
554 * Returns -1 on fail, 0 on success
557 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
558 uint32_t size, struct buf_vector *buf_vec,
559 uint16_t *nr_vec, uint16_t *num_buffers,
563 uint16_t vec_idx = 0;
564 uint16_t max_tries, tries = 0;
571 avail_idx = vq->last_avail_idx;
573 if (rxvq_is_mergeable(dev))
574 max_tries = vq->size - 1;
580 * if we tried all available ring items, and still
581 * can't get enough buf, it means something abnormal
584 if (unlikely(++tries > max_tries))
587 if (unlikely(fill_vec_buf_packed(dev, vq,
588 avail_idx, &desc_count,
591 VHOST_ACCESS_RW) < 0))
594 len = RTE_MIN(len, size);
595 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
598 avail_idx += desc_count;
599 if (avail_idx >= vq->size)
600 avail_idx -= vq->size;
602 *nr_descs += desc_count;
611 static __rte_noinline void
612 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
613 struct buf_vector *buf_vec,
614 struct virtio_net_hdr_mrg_rxbuf *hdr)
617 uint64_t remain = dev->vhost_hlen;
618 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
619 uint64_t iova = buf_vec->buf_iova;
622 len = RTE_MIN(remain,
624 dst = buf_vec->buf_addr;
625 rte_memcpy((void *)(uintptr_t)dst,
626 (void *)(uintptr_t)src,
629 PRINT_PACKET(dev, (uintptr_t)dst,
631 vhost_log_cache_write_iova(dev, vq,
641 static __rte_always_inline int
642 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
643 struct rte_mbuf *m, struct buf_vector *buf_vec,
644 uint16_t nr_vec, uint16_t num_buffers)
646 uint32_t vec_idx = 0;
647 uint32_t mbuf_offset, mbuf_avail;
648 uint32_t buf_offset, buf_avail;
649 uint64_t buf_addr, buf_iova, buf_len;
652 struct rte_mbuf *hdr_mbuf;
653 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
654 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
657 if (unlikely(m == NULL)) {
662 buf_addr = buf_vec[vec_idx].buf_addr;
663 buf_iova = buf_vec[vec_idx].buf_iova;
664 buf_len = buf_vec[vec_idx].buf_len;
666 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
673 if (unlikely(buf_len < dev->vhost_hlen))
676 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
678 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
679 dev->vid, num_buffers);
681 if (unlikely(buf_len < dev->vhost_hlen)) {
682 buf_offset = dev->vhost_hlen - buf_len;
684 buf_addr = buf_vec[vec_idx].buf_addr;
685 buf_iova = buf_vec[vec_idx].buf_iova;
686 buf_len = buf_vec[vec_idx].buf_len;
687 buf_avail = buf_len - buf_offset;
689 buf_offset = dev->vhost_hlen;
690 buf_avail = buf_len - dev->vhost_hlen;
693 mbuf_avail = rte_pktmbuf_data_len(m);
695 while (mbuf_avail != 0 || m->next != NULL) {
696 /* done with current buf, get the next one */
697 if (buf_avail == 0) {
699 if (unlikely(vec_idx >= nr_vec)) {
704 buf_addr = buf_vec[vec_idx].buf_addr;
705 buf_iova = buf_vec[vec_idx].buf_iova;
706 buf_len = buf_vec[vec_idx].buf_len;
712 /* done with current mbuf, get the next one */
713 if (mbuf_avail == 0) {
717 mbuf_avail = rte_pktmbuf_data_len(m);
721 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
722 if (rxvq_is_mergeable(dev))
723 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
726 if (unlikely(hdr == &tmp_hdr)) {
727 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
729 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
731 vhost_log_cache_write_iova(dev, vq,
739 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
741 if (likely(cpy_len > MAX_BATCH_LEN ||
742 vq->batch_copy_nb_elems >= vq->size)) {
743 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
744 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
746 vhost_log_cache_write_iova(dev, vq,
747 buf_iova + buf_offset,
749 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
752 batch_copy[vq->batch_copy_nb_elems].dst =
753 (void *)((uintptr_t)(buf_addr + buf_offset));
754 batch_copy[vq->batch_copy_nb_elems].src =
755 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
756 batch_copy[vq->batch_copy_nb_elems].log_addr =
757 buf_iova + buf_offset;
758 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
759 vq->batch_copy_nb_elems++;
762 mbuf_avail -= cpy_len;
763 mbuf_offset += cpy_len;
764 buf_avail -= cpy_len;
765 buf_offset += cpy_len;
773 static __rte_always_inline int
774 vhost_enqueue_single_packed(struct virtio_net *dev,
775 struct vhost_virtqueue *vq,
776 struct rte_mbuf *pkt,
777 struct buf_vector *buf_vec,
781 uint16_t avail_idx = vq->last_avail_idx;
782 uint16_t max_tries, tries = 0;
786 uint32_t size = pkt->pkt_len + dev->vhost_hlen;
787 uint16_t num_buffers = 0;
789 if (rxvq_is_mergeable(dev))
790 max_tries = vq->size - 1;
796 * if we tried all available ring items, and still
797 * can't get enough buf, it means something abnormal
800 if (unlikely(++tries > max_tries))
803 if (unlikely(fill_vec_buf_packed(dev, vq,
804 avail_idx, &desc_count,
807 VHOST_ACCESS_RW) < 0))
810 len = RTE_MIN(len, size);
815 *nr_descs += desc_count;
816 avail_idx += desc_count;
817 if (avail_idx >= vq->size)
818 avail_idx -= vq->size;
821 if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
827 static __rte_noinline uint32_t
828 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
829 struct rte_mbuf **pkts, uint32_t count)
831 uint32_t pkt_idx = 0;
832 uint16_t num_buffers;
833 struct buf_vector buf_vec[BUF_VECTOR_MAX];
836 avail_head = *((volatile uint16_t *)&vq->avail->idx);
839 * The ordering between avail index and
840 * desc reads needs to be enforced.
844 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
846 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
847 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
850 if (unlikely(reserve_avail_buf_split(dev, vq,
851 pkt_len, buf_vec, &num_buffers,
852 avail_head, &nr_vec) < 0)) {
853 VHOST_LOG_DEBUG(VHOST_DATA,
854 "(%d) failed to get enough desc from vring\n",
856 vq->shadow_used_idx -= num_buffers;
860 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
861 dev->vid, vq->last_avail_idx,
862 vq->last_avail_idx + num_buffers);
864 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
867 vq->shadow_used_idx -= num_buffers;
871 vq->last_avail_idx += num_buffers;
874 do_data_copy_enqueue(dev, vq);
876 if (likely(vq->shadow_used_idx)) {
877 flush_shadow_used_ring_split(dev, vq);
878 vhost_vring_call_split(dev, vq);
884 static __rte_unused int16_t
885 virtio_dev_rx_single_packed(struct virtio_net *dev,
886 struct vhost_virtqueue *vq,
887 struct rte_mbuf *pkt)
889 struct buf_vector buf_vec[BUF_VECTOR_MAX];
890 uint16_t nr_descs = 0;
893 if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
895 VHOST_LOG_DEBUG(VHOST_DATA,
896 "(%d) failed to get enough desc from vring\n",
901 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
902 dev->vid, vq->last_avail_idx,
903 vq->last_avail_idx + nr_descs);
905 vq_inc_last_avail_packed(vq, nr_descs);
910 static __rte_noinline uint32_t
911 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
912 struct rte_mbuf **pkts, uint32_t count)
914 uint32_t pkt_idx = 0;
915 uint16_t num_buffers;
916 struct buf_vector buf_vec[BUF_VECTOR_MAX];
918 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
919 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
921 uint16_t nr_descs = 0;
923 if (unlikely(reserve_avail_buf_packed(dev, vq,
924 pkt_len, buf_vec, &nr_vec,
925 &num_buffers, &nr_descs) < 0)) {
926 VHOST_LOG_DEBUG(VHOST_DATA,
927 "(%d) failed to get enough desc from vring\n",
929 vq->shadow_used_idx -= num_buffers;
933 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
934 dev->vid, vq->last_avail_idx,
935 vq->last_avail_idx + num_buffers);
937 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
940 vq->shadow_used_idx -= num_buffers;
944 vq_inc_last_avail_packed(vq, nr_descs);
947 do_data_copy_enqueue(dev, vq);
949 if (likely(vq->shadow_used_idx)) {
950 flush_shadow_used_ring_packed(dev, vq);
951 vhost_vring_call_packed(dev, vq);
957 static __rte_always_inline uint32_t
958 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
959 struct rte_mbuf **pkts, uint32_t count)
961 struct vhost_virtqueue *vq;
964 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
965 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
966 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
967 dev->vid, __func__, queue_id);
971 vq = dev->virtqueue[queue_id];
973 rte_spinlock_lock(&vq->access_lock);
975 if (unlikely(vq->enabled == 0))
976 goto out_access_unlock;
978 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
979 vhost_user_iotlb_rd_lock(vq);
981 if (unlikely(vq->access_ok == 0))
982 if (unlikely(vring_translate(dev, vq) < 0))
985 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
989 if (vq_is_packed(dev))
990 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
992 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
995 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
996 vhost_user_iotlb_rd_unlock(vq);
999 rte_spinlock_unlock(&vq->access_lock);
1005 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1006 struct rte_mbuf **pkts, uint16_t count)
1008 struct virtio_net *dev = get_device(vid);
1013 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1014 RTE_LOG(ERR, VHOST_DATA,
1015 "(%d) %s: built-in vhost net backend is disabled.\n",
1016 dev->vid, __func__);
1020 return virtio_dev_rx(dev, queue_id, pkts, count);
1024 virtio_net_with_host_offload(struct virtio_net *dev)
1027 ((1ULL << VIRTIO_NET_F_CSUM) |
1028 (1ULL << VIRTIO_NET_F_HOST_ECN) |
1029 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1030 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1031 (1ULL << VIRTIO_NET_F_HOST_UFO)))
1038 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1040 struct rte_ipv4_hdr *ipv4_hdr;
1041 struct rte_ipv6_hdr *ipv6_hdr;
1042 void *l3_hdr = NULL;
1043 struct rte_ether_hdr *eth_hdr;
1046 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1048 m->l2_len = sizeof(struct rte_ether_hdr);
1049 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1051 if (ethertype == RTE_ETHER_TYPE_VLAN) {
1052 struct rte_vlan_hdr *vlan_hdr =
1053 (struct rte_vlan_hdr *)(eth_hdr + 1);
1055 m->l2_len += sizeof(struct rte_vlan_hdr);
1056 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1059 l3_hdr = (char *)eth_hdr + m->l2_len;
1061 switch (ethertype) {
1062 case RTE_ETHER_TYPE_IPV4:
1064 *l4_proto = ipv4_hdr->next_proto_id;
1065 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1066 *l4_hdr = (char *)l3_hdr + m->l3_len;
1067 m->ol_flags |= PKT_TX_IPV4;
1069 case RTE_ETHER_TYPE_IPV6:
1071 *l4_proto = ipv6_hdr->proto;
1072 m->l3_len = sizeof(struct rte_ipv6_hdr);
1073 *l4_hdr = (char *)l3_hdr + m->l3_len;
1074 m->ol_flags |= PKT_TX_IPV6;
1084 static __rte_always_inline void
1085 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1087 uint16_t l4_proto = 0;
1088 void *l4_hdr = NULL;
1089 struct rte_tcp_hdr *tcp_hdr = NULL;
1091 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1094 parse_ethernet(m, &l4_proto, &l4_hdr);
1095 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1096 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1097 switch (hdr->csum_offset) {
1098 case (offsetof(struct rte_tcp_hdr, cksum)):
1099 if (l4_proto == IPPROTO_TCP)
1100 m->ol_flags |= PKT_TX_TCP_CKSUM;
1102 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1103 if (l4_proto == IPPROTO_UDP)
1104 m->ol_flags |= PKT_TX_UDP_CKSUM;
1106 case (offsetof(struct rte_sctp_hdr, cksum)):
1107 if (l4_proto == IPPROTO_SCTP)
1108 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1116 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1117 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1118 case VIRTIO_NET_HDR_GSO_TCPV4:
1119 case VIRTIO_NET_HDR_GSO_TCPV6:
1121 m->ol_flags |= PKT_TX_TCP_SEG;
1122 m->tso_segsz = hdr->gso_size;
1123 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1125 case VIRTIO_NET_HDR_GSO_UDP:
1126 m->ol_flags |= PKT_TX_UDP_SEG;
1127 m->tso_segsz = hdr->gso_size;
1128 m->l4_len = sizeof(struct rte_udp_hdr);
1131 RTE_LOG(WARNING, VHOST_DATA,
1132 "unsupported gso type %u.\n", hdr->gso_type);
1138 static __rte_noinline void
1139 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1140 struct buf_vector *buf_vec)
1143 uint64_t remain = sizeof(struct virtio_net_hdr);
1145 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1148 len = RTE_MIN(remain, buf_vec->buf_len);
1149 src = buf_vec->buf_addr;
1150 rte_memcpy((void *)(uintptr_t)dst,
1151 (void *)(uintptr_t)src, len);
1159 static __rte_always_inline int
1160 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1161 struct buf_vector *buf_vec, uint16_t nr_vec,
1162 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1164 uint32_t buf_avail, buf_offset;
1165 uint64_t buf_addr, buf_iova, buf_len;
1166 uint32_t mbuf_avail, mbuf_offset;
1168 struct rte_mbuf *cur = m, *prev = m;
1169 struct virtio_net_hdr tmp_hdr;
1170 struct virtio_net_hdr *hdr = NULL;
1171 /* A counter to avoid desc dead loop chain */
1172 uint16_t vec_idx = 0;
1173 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1176 buf_addr = buf_vec[vec_idx].buf_addr;
1177 buf_iova = buf_vec[vec_idx].buf_iova;
1178 buf_len = buf_vec[vec_idx].buf_len;
1180 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1185 if (virtio_net_with_host_offload(dev)) {
1186 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1188 * No luck, the virtio-net header doesn't fit
1189 * in a contiguous virtual area.
1191 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1194 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1199 * A virtio driver normally uses at least 2 desc buffers
1200 * for Tx: the first for storing the header, and others
1201 * for storing the data.
1203 if (unlikely(buf_len < dev->vhost_hlen)) {
1204 buf_offset = dev->vhost_hlen - buf_len;
1206 buf_addr = buf_vec[vec_idx].buf_addr;
1207 buf_iova = buf_vec[vec_idx].buf_iova;
1208 buf_len = buf_vec[vec_idx].buf_len;
1209 buf_avail = buf_len - buf_offset;
1210 } else if (buf_len == dev->vhost_hlen) {
1211 if (unlikely(++vec_idx >= nr_vec))
1213 buf_addr = buf_vec[vec_idx].buf_addr;
1214 buf_iova = buf_vec[vec_idx].buf_iova;
1215 buf_len = buf_vec[vec_idx].buf_len;
1218 buf_avail = buf_len;
1220 buf_offset = dev->vhost_hlen;
1221 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1225 (uintptr_t)(buf_addr + buf_offset),
1226 (uint32_t)buf_avail, 0);
1229 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1233 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1236 * A desc buf might across two host physical pages that are
1237 * not continuous. In such case (gpa_to_hpa returns 0), data
1238 * will be copied even though zero copy is enabled.
1240 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1241 buf_iova + buf_offset, cpy_len)))) {
1242 cur->data_len = cpy_len;
1245 (void *)(uintptr_t)(buf_addr + buf_offset);
1246 cur->buf_iova = hpa;
1249 * In zero copy mode, one mbuf can only reference data
1250 * for one or partial of one desc buff.
1252 mbuf_avail = cpy_len;
1254 if (likely(cpy_len > MAX_BATCH_LEN ||
1255 vq->batch_copy_nb_elems >= vq->size ||
1256 (hdr && cur == m))) {
1257 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1259 (void *)((uintptr_t)(buf_addr +
1263 batch_copy[vq->batch_copy_nb_elems].dst =
1264 rte_pktmbuf_mtod_offset(cur, void *,
1266 batch_copy[vq->batch_copy_nb_elems].src =
1267 (void *)((uintptr_t)(buf_addr +
1269 batch_copy[vq->batch_copy_nb_elems].len =
1271 vq->batch_copy_nb_elems++;
1275 mbuf_avail -= cpy_len;
1276 mbuf_offset += cpy_len;
1277 buf_avail -= cpy_len;
1278 buf_offset += cpy_len;
1280 /* This buf reaches to its end, get the next one */
1281 if (buf_avail == 0) {
1282 if (++vec_idx >= nr_vec)
1285 buf_addr = buf_vec[vec_idx].buf_addr;
1286 buf_iova = buf_vec[vec_idx].buf_iova;
1287 buf_len = buf_vec[vec_idx].buf_len;
1290 buf_avail = buf_len;
1292 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1293 (uint32_t)buf_avail, 0);
1297 * This mbuf reaches to its end, get a new one
1298 * to hold more data.
1300 if (mbuf_avail == 0) {
1301 cur = rte_pktmbuf_alloc(mbuf_pool);
1302 if (unlikely(cur == NULL)) {
1303 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1304 "allocate memory for mbuf.\n");
1308 if (unlikely(dev->dequeue_zero_copy))
1309 rte_mbuf_refcnt_update(cur, 1);
1312 prev->data_len = mbuf_offset;
1314 m->pkt_len += mbuf_offset;
1318 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1322 prev->data_len = mbuf_offset;
1323 m->pkt_len += mbuf_offset;
1326 vhost_dequeue_offload(hdr, m);
1333 static __rte_always_inline struct zcopy_mbuf *
1334 get_zmbuf(struct vhost_virtqueue *vq)
1340 /* search [last_zmbuf_idx, zmbuf_size) */
1341 i = vq->last_zmbuf_idx;
1342 last = vq->zmbuf_size;
1345 for (; i < last; i++) {
1346 if (vq->zmbufs[i].in_use == 0) {
1347 vq->last_zmbuf_idx = i + 1;
1348 vq->zmbufs[i].in_use = 1;
1349 return &vq->zmbufs[i];
1355 /* search [0, last_zmbuf_idx) */
1357 last = vq->last_zmbuf_idx;
1365 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1371 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1373 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1374 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1379 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1380 * required, otherwise store shinfo in the new buffer.
1382 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1383 shinfo = rte_pktmbuf_mtod(pkt,
1384 struct rte_mbuf_ext_shared_info *);
1386 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1387 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1390 if (unlikely(total_len > UINT16_MAX))
1393 buf_len = total_len;
1394 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1395 if (unlikely(buf == NULL))
1398 /* Initialize shinfo */
1400 shinfo->free_cb = virtio_dev_extbuf_free;
1401 shinfo->fcb_opaque = buf;
1402 rte_mbuf_ext_refcnt_set(shinfo, 1);
1404 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1405 virtio_dev_extbuf_free, buf);
1406 if (unlikely(shinfo == NULL)) {
1408 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1413 iova = rte_malloc_virt2iova(buf);
1414 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1415 rte_pktmbuf_reset_headroom(pkt);
1421 * Allocate a host supported pktmbuf.
1423 static __rte_always_inline struct rte_mbuf *
1424 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1427 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1429 if (unlikely(pkt == NULL))
1432 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1435 /* attach an external buffer if supported */
1436 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1439 /* check if chained buffers are allowed */
1440 if (!dev->linearbuf)
1443 /* Data doesn't fit into the buffer and the host supports
1444 * only linear buffers
1446 rte_pktmbuf_free(pkt);
1451 static __rte_noinline uint16_t
1452 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1453 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1456 uint16_t free_entries;
1458 if (unlikely(dev->dequeue_zero_copy)) {
1459 struct zcopy_mbuf *zmbuf, *next;
1461 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1462 zmbuf != NULL; zmbuf = next) {
1463 next = TAILQ_NEXT(zmbuf, next);
1465 if (mbuf_is_consumed(zmbuf->mbuf)) {
1466 update_shadow_used_ring_split(vq,
1467 zmbuf->desc_idx, 0);
1468 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1469 restore_mbuf(zmbuf->mbuf);
1470 rte_pktmbuf_free(zmbuf->mbuf);
1476 if (likely(vq->shadow_used_idx)) {
1477 flush_shadow_used_ring_split(dev, vq);
1478 vhost_vring_call_split(dev, vq);
1482 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1484 if (free_entries == 0)
1488 * The ordering between avail index and
1489 * desc reads needs to be enforced.
1493 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1495 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1497 count = RTE_MIN(count, MAX_PKT_BURST);
1498 count = RTE_MIN(count, free_entries);
1499 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1502 for (i = 0; i < count; i++) {
1503 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1506 uint16_t nr_vec = 0;
1509 if (unlikely(fill_vec_buf_split(dev, vq,
1510 vq->last_avail_idx + i,
1512 &head_idx, &buf_len,
1513 VHOST_ACCESS_RO) < 0))
1516 if (likely(dev->dequeue_zero_copy == 0))
1517 update_shadow_used_ring_split(vq, head_idx, 0);
1519 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1520 if (unlikely(pkts[i] == NULL))
1523 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1525 if (unlikely(err)) {
1526 rte_pktmbuf_free(pkts[i]);
1530 if (unlikely(dev->dequeue_zero_copy)) {
1531 struct zcopy_mbuf *zmbuf;
1533 zmbuf = get_zmbuf(vq);
1535 rte_pktmbuf_free(pkts[i]);
1538 zmbuf->mbuf = pkts[i];
1539 zmbuf->desc_idx = head_idx;
1542 * Pin lock the mbuf; we will check later to see
1543 * whether the mbuf is freed (when we are the last
1544 * user) or not. If that's the case, we then could
1545 * update the used ring safely.
1547 rte_mbuf_refcnt_update(pkts[i], 1);
1550 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1553 vq->last_avail_idx += i;
1555 if (likely(dev->dequeue_zero_copy == 0)) {
1556 do_data_copy_dequeue(vq);
1557 if (unlikely(i < count))
1558 vq->shadow_used_idx = i;
1559 if (likely(vq->shadow_used_idx)) {
1560 flush_shadow_used_ring_split(dev, vq);
1561 vhost_vring_call_split(dev, vq);
1568 static __rte_noinline uint16_t
1569 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1570 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1574 if (unlikely(dev->dequeue_zero_copy)) {
1575 struct zcopy_mbuf *zmbuf, *next;
1577 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1578 zmbuf != NULL; zmbuf = next) {
1579 next = TAILQ_NEXT(zmbuf, next);
1581 if (mbuf_is_consumed(zmbuf->mbuf)) {
1582 update_shadow_used_ring_packed(vq,
1587 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1588 restore_mbuf(zmbuf->mbuf);
1589 rte_pktmbuf_free(zmbuf->mbuf);
1595 if (likely(vq->shadow_used_idx)) {
1596 flush_shadow_used_ring_packed(dev, vq);
1597 vhost_vring_call_packed(dev, vq);
1601 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1603 count = RTE_MIN(count, MAX_PKT_BURST);
1604 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1607 for (i = 0; i < count; i++) {
1608 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1611 uint16_t desc_count, nr_vec = 0;
1614 if (unlikely(fill_vec_buf_packed(dev, vq,
1615 vq->last_avail_idx, &desc_count,
1618 VHOST_ACCESS_RO) < 0))
1621 if (likely(dev->dequeue_zero_copy == 0))
1622 update_shadow_used_ring_packed(vq, buf_id, 0,
1625 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1626 if (unlikely(pkts[i] == NULL))
1629 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1631 if (unlikely(err)) {
1632 rte_pktmbuf_free(pkts[i]);
1636 if (unlikely(dev->dequeue_zero_copy)) {
1637 struct zcopy_mbuf *zmbuf;
1639 zmbuf = get_zmbuf(vq);
1641 rte_pktmbuf_free(pkts[i]);
1644 zmbuf->mbuf = pkts[i];
1645 zmbuf->desc_idx = buf_id;
1646 zmbuf->desc_count = desc_count;
1649 * Pin lock the mbuf; we will check later to see
1650 * whether the mbuf is freed (when we are the last
1651 * user) or not. If that's the case, we then could
1652 * update the used ring safely.
1654 rte_mbuf_refcnt_update(pkts[i], 1);
1657 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1660 vq_inc_last_avail_packed(vq, desc_count);
1663 if (likely(dev->dequeue_zero_copy == 0)) {
1664 do_data_copy_dequeue(vq);
1665 if (unlikely(i < count))
1666 vq->shadow_used_idx = i;
1667 if (likely(vq->shadow_used_idx)) {
1668 flush_shadow_used_ring_packed(dev, vq);
1669 vhost_vring_call_packed(dev, vq);
1677 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1678 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1680 struct virtio_net *dev;
1681 struct rte_mbuf *rarp_mbuf = NULL;
1682 struct vhost_virtqueue *vq;
1684 dev = get_device(vid);
1688 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1689 RTE_LOG(ERR, VHOST_DATA,
1690 "(%d) %s: built-in vhost net backend is disabled.\n",
1691 dev->vid, __func__);
1695 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1696 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1697 dev->vid, __func__, queue_id);
1701 vq = dev->virtqueue[queue_id];
1703 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1706 if (unlikely(vq->enabled == 0)) {
1708 goto out_access_unlock;
1711 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1712 vhost_user_iotlb_rd_lock(vq);
1714 if (unlikely(vq->access_ok == 0))
1715 if (unlikely(vring_translate(dev, vq) < 0)) {
1721 * Construct a RARP broadcast packet, and inject it to the "pkts"
1722 * array, to looks like that guest actually send such packet.
1724 * Check user_send_rarp() for more information.
1726 * broadcast_rarp shares a cacheline in the virtio_net structure
1727 * with some fields that are accessed during enqueue and
1728 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1729 * result in false sharing between enqueue and dequeue.
1731 * Prevent unnecessary false sharing by reading broadcast_rarp first
1732 * and only performing cmpset if the read indicates it is likely to
1735 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1736 rte_atomic16_cmpset((volatile uint16_t *)
1737 &dev->broadcast_rarp.cnt, 1, 0))) {
1739 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1740 if (rarp_mbuf == NULL) {
1741 RTE_LOG(ERR, VHOST_DATA,
1742 "Failed to make RARP packet.\n");
1749 if (vq_is_packed(dev))
1750 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1752 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1755 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1756 vhost_user_iotlb_rd_unlock(vq);
1759 rte_spinlock_unlock(&vq->access_lock);
1761 if (unlikely(rarp_mbuf != NULL)) {
1763 * Inject it to the head of "pkts" array, so that switch's mac
1764 * learning table will get updated first.
1766 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1767 pkts[0] = rarp_mbuf;