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->last_used_idx += vq->shadow_used_packed[i].count;
142 if (vq->last_used_idx >= vq->size) {
143 vq->used_wrap_counter ^= 1;
144 vq->last_used_idx -= vq->size;
148 __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
151 vhost_log_cache_used_vring(dev, vq,
153 sizeof(struct vring_packed_desc),
154 sizeof(struct vring_packed_desc));
156 vq->shadow_used_idx = 0;
157 vhost_log_cache_sync(dev, vq);
160 static __rte_always_inline void
161 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
162 uint16_t desc_idx, uint32_t len, uint16_t count)
164 uint16_t i = vq->shadow_used_idx++;
166 vq->shadow_used_packed[i].id = desc_idx;
167 vq->shadow_used_packed[i].len = len;
168 vq->shadow_used_packed[i].count = count;
172 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
174 struct batch_copy_elem *elem = vq->batch_copy_elems;
175 uint16_t count = vq->batch_copy_nb_elems;
178 for (i = 0; i < count; i++) {
179 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
180 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
182 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
185 vq->batch_copy_nb_elems = 0;
189 do_data_copy_dequeue(struct vhost_virtqueue *vq)
191 struct batch_copy_elem *elem = vq->batch_copy_elems;
192 uint16_t count = vq->batch_copy_nb_elems;
195 for (i = 0; i < count; i++)
196 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
198 vq->batch_copy_nb_elems = 0;
201 /* avoid write operation when necessary, to lessen cache issues */
202 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
203 if ((var) != (val)) \
207 static __rte_always_inline void
208 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
210 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
212 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
213 csum_l4 |= PKT_TX_TCP_CKSUM;
216 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
217 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
220 case PKT_TX_TCP_CKSUM:
221 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
224 case PKT_TX_UDP_CKSUM:
225 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
228 case PKT_TX_SCTP_CKSUM:
229 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
234 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
235 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
236 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
239 /* IP cksum verification cannot be bypassed, then calculate here */
240 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
241 struct rte_ipv4_hdr *ipv4_hdr;
243 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
245 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
248 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
249 if (m_buf->ol_flags & PKT_TX_IPV4)
250 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
252 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
253 net_hdr->gso_size = m_buf->tso_segsz;
254 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
256 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
257 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
258 net_hdr->gso_size = m_buf->tso_segsz;
259 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
262 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
263 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
264 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
268 static __rte_always_inline int
269 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
270 struct buf_vector *buf_vec, uint16_t *vec_idx,
271 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
273 uint16_t vec_id = *vec_idx;
277 uint64_t desc_chunck_len = desc_len;
279 if (unlikely(vec_id >= BUF_VECTOR_MAX))
282 desc_addr = vhost_iova_to_vva(dev, vq,
286 if (unlikely(!desc_addr))
289 rte_prefetch0((void *)(uintptr_t)desc_addr);
291 buf_vec[vec_id].buf_iova = desc_iova;
292 buf_vec[vec_id].buf_addr = desc_addr;
293 buf_vec[vec_id].buf_len = desc_chunck_len;
295 desc_len -= desc_chunck_len;
296 desc_iova += desc_chunck_len;
304 static __rte_always_inline int
305 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
306 uint32_t avail_idx, uint16_t *vec_idx,
307 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
308 uint32_t *desc_chain_len, uint8_t perm)
310 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
311 uint16_t vec_id = *vec_idx;
314 uint32_t nr_descs = vq->size;
316 struct vring_desc *descs = vq->desc;
317 struct vring_desc *idesc = NULL;
319 if (unlikely(idx >= vq->size))
322 *desc_chain_head = idx;
324 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
325 dlen = vq->desc[idx].len;
326 nr_descs = dlen / sizeof(struct vring_desc);
327 if (unlikely(nr_descs > vq->size))
330 descs = (struct vring_desc *)(uintptr_t)
331 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
334 if (unlikely(!descs))
337 if (unlikely(dlen < vq->desc[idx].len)) {
339 * The indirect desc table is not contiguous
340 * in process VA space, we have to copy it.
342 idesc = vhost_alloc_copy_ind_table(dev, vq,
343 vq->desc[idx].addr, vq->desc[idx].len);
344 if (unlikely(!idesc))
354 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
355 free_ind_table(idesc);
359 len += descs[idx].len;
361 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
362 descs[idx].addr, descs[idx].len,
364 free_ind_table(idesc);
368 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
371 idx = descs[idx].next;
374 *desc_chain_len = len;
377 if (unlikely(!!idesc))
378 free_ind_table(idesc);
384 * Returns -1 on fail, 0 on success
387 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
388 uint32_t size, struct buf_vector *buf_vec,
389 uint16_t *num_buffers, uint16_t avail_head,
393 uint16_t vec_idx = 0;
394 uint16_t max_tries, tries = 0;
396 uint16_t head_idx = 0;
400 cur_idx = vq->last_avail_idx;
402 if (rxvq_is_mergeable(dev))
403 max_tries = vq->size - 1;
408 if (unlikely(cur_idx == avail_head))
411 * if we tried all available ring items, and still
412 * can't get enough buf, it means something abnormal
415 if (unlikely(++tries > max_tries))
418 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
421 VHOST_ACCESS_RW) < 0))
423 len = RTE_MIN(len, size);
424 update_shadow_used_ring_split(vq, head_idx, len);
436 static __rte_always_inline int
437 fill_vec_buf_packed_indirect(struct virtio_net *dev,
438 struct vhost_virtqueue *vq,
439 struct vring_packed_desc *desc, uint16_t *vec_idx,
440 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
444 uint16_t vec_id = *vec_idx;
446 struct vring_packed_desc *descs, *idescs = NULL;
449 descs = (struct vring_packed_desc *)(uintptr_t)
450 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
451 if (unlikely(!descs))
454 if (unlikely(dlen < desc->len)) {
456 * The indirect desc table is not contiguous
457 * in process VA space, we have to copy it.
459 idescs = vhost_alloc_copy_ind_table(dev,
460 vq, desc->addr, desc->len);
461 if (unlikely(!idescs))
467 nr_descs = desc->len / sizeof(struct vring_packed_desc);
468 if (unlikely(nr_descs >= vq->size)) {
469 free_ind_table(idescs);
473 for (i = 0; i < nr_descs; i++) {
474 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
475 free_ind_table(idescs);
479 *len += descs[i].len;
480 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
481 descs[i].addr, descs[i].len,
487 if (unlikely(!!idescs))
488 free_ind_table(idescs);
493 static __rte_always_inline int
494 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
495 uint16_t avail_idx, uint16_t *desc_count,
496 struct buf_vector *buf_vec, uint16_t *vec_idx,
497 uint16_t *buf_id, uint32_t *len, uint8_t perm)
499 bool wrap_counter = vq->avail_wrap_counter;
500 struct vring_packed_desc *descs = vq->desc_packed;
501 uint16_t vec_id = *vec_idx;
503 if (avail_idx < vq->last_avail_idx)
507 * Perform a load-acquire barrier in desc_is_avail to
508 * enforce the ordering between desc flags and desc
511 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
518 if (unlikely(vec_id >= BUF_VECTOR_MAX))
521 if (unlikely(*desc_count >= vq->size))
525 *buf_id = descs[avail_idx].id;
527 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
528 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
534 *len += descs[avail_idx].len;
536 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
537 descs[avail_idx].addr,
538 descs[avail_idx].len,
543 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
546 if (++avail_idx >= vq->size) {
547 avail_idx -= vq->size;
558 * Returns -1 on fail, 0 on success
561 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
562 uint32_t size, struct buf_vector *buf_vec,
563 uint16_t *nr_vec, uint16_t *num_buffers,
567 uint16_t vec_idx = 0;
568 uint16_t max_tries, tries = 0;
575 avail_idx = vq->last_avail_idx;
577 if (rxvq_is_mergeable(dev))
578 max_tries = vq->size - 1;
584 * if we tried all available ring items, and still
585 * can't get enough buf, it means something abnormal
588 if (unlikely(++tries > max_tries))
591 if (unlikely(fill_vec_buf_packed(dev, vq,
592 avail_idx, &desc_count,
595 VHOST_ACCESS_RW) < 0))
598 len = RTE_MIN(len, size);
599 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
602 avail_idx += desc_count;
603 if (avail_idx >= vq->size)
604 avail_idx -= vq->size;
606 *nr_descs += desc_count;
615 static __rte_noinline void
616 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
617 struct buf_vector *buf_vec,
618 struct virtio_net_hdr_mrg_rxbuf *hdr)
621 uint64_t remain = dev->vhost_hlen;
622 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
623 uint64_t iova = buf_vec->buf_iova;
626 len = RTE_MIN(remain,
628 dst = buf_vec->buf_addr;
629 rte_memcpy((void *)(uintptr_t)dst,
630 (void *)(uintptr_t)src,
633 PRINT_PACKET(dev, (uintptr_t)dst,
635 vhost_log_cache_write_iova(dev, vq,
645 static __rte_always_inline int
646 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
647 struct rte_mbuf *m, struct buf_vector *buf_vec,
648 uint16_t nr_vec, uint16_t num_buffers)
650 uint32_t vec_idx = 0;
651 uint32_t mbuf_offset, mbuf_avail;
652 uint32_t buf_offset, buf_avail;
653 uint64_t buf_addr, buf_iova, buf_len;
656 struct rte_mbuf *hdr_mbuf;
657 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
658 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
661 if (unlikely(m == NULL)) {
666 buf_addr = buf_vec[vec_idx].buf_addr;
667 buf_iova = buf_vec[vec_idx].buf_iova;
668 buf_len = buf_vec[vec_idx].buf_len;
670 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
677 if (unlikely(buf_len < dev->vhost_hlen))
680 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
682 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
683 dev->vid, num_buffers);
685 if (unlikely(buf_len < dev->vhost_hlen)) {
686 buf_offset = dev->vhost_hlen - buf_len;
688 buf_addr = buf_vec[vec_idx].buf_addr;
689 buf_iova = buf_vec[vec_idx].buf_iova;
690 buf_len = buf_vec[vec_idx].buf_len;
691 buf_avail = buf_len - buf_offset;
693 buf_offset = dev->vhost_hlen;
694 buf_avail = buf_len - dev->vhost_hlen;
697 mbuf_avail = rte_pktmbuf_data_len(m);
699 while (mbuf_avail != 0 || m->next != NULL) {
700 /* done with current buf, get the next one */
701 if (buf_avail == 0) {
703 if (unlikely(vec_idx >= nr_vec)) {
708 buf_addr = buf_vec[vec_idx].buf_addr;
709 buf_iova = buf_vec[vec_idx].buf_iova;
710 buf_len = buf_vec[vec_idx].buf_len;
716 /* done with current mbuf, get the next one */
717 if (mbuf_avail == 0) {
721 mbuf_avail = rte_pktmbuf_data_len(m);
725 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
726 if (rxvq_is_mergeable(dev))
727 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
730 if (unlikely(hdr == &tmp_hdr)) {
731 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
733 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
735 vhost_log_cache_write_iova(dev, vq,
743 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
745 if (likely(cpy_len > MAX_BATCH_LEN ||
746 vq->batch_copy_nb_elems >= vq->size)) {
747 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
748 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
750 vhost_log_cache_write_iova(dev, vq,
751 buf_iova + buf_offset,
753 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
756 batch_copy[vq->batch_copy_nb_elems].dst =
757 (void *)((uintptr_t)(buf_addr + buf_offset));
758 batch_copy[vq->batch_copy_nb_elems].src =
759 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
760 batch_copy[vq->batch_copy_nb_elems].log_addr =
761 buf_iova + buf_offset;
762 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
763 vq->batch_copy_nb_elems++;
766 mbuf_avail -= cpy_len;
767 mbuf_offset += cpy_len;
768 buf_avail -= cpy_len;
769 buf_offset += cpy_len;
777 static __rte_noinline uint32_t
778 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
779 struct rte_mbuf **pkts, uint32_t count)
781 uint32_t pkt_idx = 0;
782 uint16_t num_buffers;
783 struct buf_vector buf_vec[BUF_VECTOR_MAX];
786 avail_head = *((volatile uint16_t *)&vq->avail->idx);
789 * The ordering between avail index and
790 * desc reads needs to be enforced.
794 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
796 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
797 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
800 if (unlikely(reserve_avail_buf_split(dev, vq,
801 pkt_len, buf_vec, &num_buffers,
802 avail_head, &nr_vec) < 0)) {
803 VHOST_LOG_DEBUG(VHOST_DATA,
804 "(%d) failed to get enough desc from vring\n",
806 vq->shadow_used_idx -= num_buffers;
810 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
811 dev->vid, vq->last_avail_idx,
812 vq->last_avail_idx + num_buffers);
814 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
817 vq->shadow_used_idx -= num_buffers;
821 vq->last_avail_idx += num_buffers;
824 do_data_copy_enqueue(dev, vq);
826 if (likely(vq->shadow_used_idx)) {
827 flush_shadow_used_ring_split(dev, vq);
828 vhost_vring_call_split(dev, vq);
834 static __rte_noinline uint32_t
835 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
836 struct rte_mbuf **pkts, uint32_t count)
838 uint32_t pkt_idx = 0;
839 uint16_t num_buffers;
840 struct buf_vector buf_vec[BUF_VECTOR_MAX];
842 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
843 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
845 uint16_t nr_descs = 0;
847 if (unlikely(reserve_avail_buf_packed(dev, vq,
848 pkt_len, buf_vec, &nr_vec,
849 &num_buffers, &nr_descs) < 0)) {
850 VHOST_LOG_DEBUG(VHOST_DATA,
851 "(%d) failed to get enough desc from vring\n",
853 vq->shadow_used_idx -= num_buffers;
857 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
858 dev->vid, vq->last_avail_idx,
859 vq->last_avail_idx + num_buffers);
861 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
864 vq->shadow_used_idx -= num_buffers;
868 vq->last_avail_idx += nr_descs;
869 if (vq->last_avail_idx >= vq->size) {
870 vq->last_avail_idx -= vq->size;
871 vq->avail_wrap_counter ^= 1;
875 do_data_copy_enqueue(dev, vq);
877 if (likely(vq->shadow_used_idx)) {
878 flush_shadow_used_ring_packed(dev, vq);
879 vhost_vring_call_packed(dev, vq);
885 static __rte_always_inline uint32_t
886 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
887 struct rte_mbuf **pkts, uint32_t count)
889 struct vhost_virtqueue *vq;
892 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
893 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
894 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
895 dev->vid, __func__, queue_id);
899 vq = dev->virtqueue[queue_id];
901 rte_spinlock_lock(&vq->access_lock);
903 if (unlikely(vq->enabled == 0))
904 goto out_access_unlock;
906 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
907 vhost_user_iotlb_rd_lock(vq);
909 if (unlikely(vq->access_ok == 0))
910 if (unlikely(vring_translate(dev, vq) < 0))
913 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
917 if (vq_is_packed(dev))
918 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
920 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
923 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
924 vhost_user_iotlb_rd_unlock(vq);
927 rte_spinlock_unlock(&vq->access_lock);
933 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
934 struct rte_mbuf **pkts, uint16_t count)
936 struct virtio_net *dev = get_device(vid);
941 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
942 RTE_LOG(ERR, VHOST_DATA,
943 "(%d) %s: built-in vhost net backend is disabled.\n",
948 return virtio_dev_rx(dev, queue_id, pkts, count);
952 virtio_net_with_host_offload(struct virtio_net *dev)
955 ((1ULL << VIRTIO_NET_F_CSUM) |
956 (1ULL << VIRTIO_NET_F_HOST_ECN) |
957 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
958 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
959 (1ULL << VIRTIO_NET_F_HOST_UFO)))
966 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
968 struct rte_ipv4_hdr *ipv4_hdr;
969 struct rte_ipv6_hdr *ipv6_hdr;
971 struct rte_ether_hdr *eth_hdr;
974 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
976 m->l2_len = sizeof(struct rte_ether_hdr);
977 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
979 if (ethertype == RTE_ETHER_TYPE_VLAN) {
980 struct rte_vlan_hdr *vlan_hdr =
981 (struct rte_vlan_hdr *)(eth_hdr + 1);
983 m->l2_len += sizeof(struct rte_vlan_hdr);
984 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
987 l3_hdr = (char *)eth_hdr + m->l2_len;
990 case RTE_ETHER_TYPE_IPV4:
992 *l4_proto = ipv4_hdr->next_proto_id;
993 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
994 *l4_hdr = (char *)l3_hdr + m->l3_len;
995 m->ol_flags |= PKT_TX_IPV4;
997 case RTE_ETHER_TYPE_IPV6:
999 *l4_proto = ipv6_hdr->proto;
1000 m->l3_len = sizeof(struct rte_ipv6_hdr);
1001 *l4_hdr = (char *)l3_hdr + m->l3_len;
1002 m->ol_flags |= PKT_TX_IPV6;
1012 static __rte_always_inline void
1013 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1015 uint16_t l4_proto = 0;
1016 void *l4_hdr = NULL;
1017 struct rte_tcp_hdr *tcp_hdr = NULL;
1019 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1022 parse_ethernet(m, &l4_proto, &l4_hdr);
1023 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1024 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1025 switch (hdr->csum_offset) {
1026 case (offsetof(struct rte_tcp_hdr, cksum)):
1027 if (l4_proto == IPPROTO_TCP)
1028 m->ol_flags |= PKT_TX_TCP_CKSUM;
1030 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1031 if (l4_proto == IPPROTO_UDP)
1032 m->ol_flags |= PKT_TX_UDP_CKSUM;
1034 case (offsetof(struct rte_sctp_hdr, cksum)):
1035 if (l4_proto == IPPROTO_SCTP)
1036 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1044 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1045 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1046 case VIRTIO_NET_HDR_GSO_TCPV4:
1047 case VIRTIO_NET_HDR_GSO_TCPV6:
1049 m->ol_flags |= PKT_TX_TCP_SEG;
1050 m->tso_segsz = hdr->gso_size;
1051 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1053 case VIRTIO_NET_HDR_GSO_UDP:
1054 m->ol_flags |= PKT_TX_UDP_SEG;
1055 m->tso_segsz = hdr->gso_size;
1056 m->l4_len = sizeof(struct rte_udp_hdr);
1059 RTE_LOG(WARNING, VHOST_DATA,
1060 "unsupported gso type %u.\n", hdr->gso_type);
1066 static __rte_noinline void
1067 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1068 struct buf_vector *buf_vec)
1071 uint64_t remain = sizeof(struct virtio_net_hdr);
1073 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1076 len = RTE_MIN(remain, buf_vec->buf_len);
1077 src = buf_vec->buf_addr;
1078 rte_memcpy((void *)(uintptr_t)dst,
1079 (void *)(uintptr_t)src, len);
1087 static __rte_always_inline int
1088 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1089 struct buf_vector *buf_vec, uint16_t nr_vec,
1090 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1092 uint32_t buf_avail, buf_offset;
1093 uint64_t buf_addr, buf_iova, buf_len;
1094 uint32_t mbuf_avail, mbuf_offset;
1096 struct rte_mbuf *cur = m, *prev = m;
1097 struct virtio_net_hdr tmp_hdr;
1098 struct virtio_net_hdr *hdr = NULL;
1099 /* A counter to avoid desc dead loop chain */
1100 uint16_t vec_idx = 0;
1101 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1104 buf_addr = buf_vec[vec_idx].buf_addr;
1105 buf_iova = buf_vec[vec_idx].buf_iova;
1106 buf_len = buf_vec[vec_idx].buf_len;
1108 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1113 if (virtio_net_with_host_offload(dev)) {
1114 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1116 * No luck, the virtio-net header doesn't fit
1117 * in a contiguous virtual area.
1119 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1122 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1127 * A virtio driver normally uses at least 2 desc buffers
1128 * for Tx: the first for storing the header, and others
1129 * for storing the data.
1131 if (unlikely(buf_len < dev->vhost_hlen)) {
1132 buf_offset = dev->vhost_hlen - buf_len;
1134 buf_addr = buf_vec[vec_idx].buf_addr;
1135 buf_iova = buf_vec[vec_idx].buf_iova;
1136 buf_len = buf_vec[vec_idx].buf_len;
1137 buf_avail = buf_len - buf_offset;
1138 } else if (buf_len == dev->vhost_hlen) {
1139 if (unlikely(++vec_idx >= nr_vec))
1141 buf_addr = buf_vec[vec_idx].buf_addr;
1142 buf_iova = buf_vec[vec_idx].buf_iova;
1143 buf_len = buf_vec[vec_idx].buf_len;
1146 buf_avail = buf_len;
1148 buf_offset = dev->vhost_hlen;
1149 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1153 (uintptr_t)(buf_addr + buf_offset),
1154 (uint32_t)buf_avail, 0);
1157 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1161 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1164 * A desc buf might across two host physical pages that are
1165 * not continuous. In such case (gpa_to_hpa returns 0), data
1166 * will be copied even though zero copy is enabled.
1168 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1169 buf_iova + buf_offset, cpy_len)))) {
1170 cur->data_len = cpy_len;
1173 (void *)(uintptr_t)(buf_addr + buf_offset);
1174 cur->buf_iova = hpa;
1177 * In zero copy mode, one mbuf can only reference data
1178 * for one or partial of one desc buff.
1180 mbuf_avail = cpy_len;
1182 if (likely(cpy_len > MAX_BATCH_LEN ||
1183 vq->batch_copy_nb_elems >= vq->size ||
1184 (hdr && cur == m))) {
1185 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1187 (void *)((uintptr_t)(buf_addr +
1191 batch_copy[vq->batch_copy_nb_elems].dst =
1192 rte_pktmbuf_mtod_offset(cur, void *,
1194 batch_copy[vq->batch_copy_nb_elems].src =
1195 (void *)((uintptr_t)(buf_addr +
1197 batch_copy[vq->batch_copy_nb_elems].len =
1199 vq->batch_copy_nb_elems++;
1203 mbuf_avail -= cpy_len;
1204 mbuf_offset += cpy_len;
1205 buf_avail -= cpy_len;
1206 buf_offset += cpy_len;
1208 /* This buf reaches to its end, get the next one */
1209 if (buf_avail == 0) {
1210 if (++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 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1221 (uint32_t)buf_avail, 0);
1225 * This mbuf reaches to its end, get a new one
1226 * to hold more data.
1228 if (mbuf_avail == 0) {
1229 cur = rte_pktmbuf_alloc(mbuf_pool);
1230 if (unlikely(cur == NULL)) {
1231 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1232 "allocate memory for mbuf.\n");
1236 if (unlikely(dev->dequeue_zero_copy))
1237 rte_mbuf_refcnt_update(cur, 1);
1240 prev->data_len = mbuf_offset;
1242 m->pkt_len += mbuf_offset;
1246 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1250 prev->data_len = mbuf_offset;
1251 m->pkt_len += mbuf_offset;
1254 vhost_dequeue_offload(hdr, m);
1261 static __rte_always_inline struct zcopy_mbuf *
1262 get_zmbuf(struct vhost_virtqueue *vq)
1268 /* search [last_zmbuf_idx, zmbuf_size) */
1269 i = vq->last_zmbuf_idx;
1270 last = vq->zmbuf_size;
1273 for (; i < last; i++) {
1274 if (vq->zmbufs[i].in_use == 0) {
1275 vq->last_zmbuf_idx = i + 1;
1276 vq->zmbufs[i].in_use = 1;
1277 return &vq->zmbufs[i];
1283 /* search [0, last_zmbuf_idx) */
1285 last = vq->last_zmbuf_idx;
1293 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
1299 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
1301 struct rte_mbuf_ext_shared_info *shinfo = NULL;
1302 uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
1307 /* Try to use pkt buffer to store shinfo to reduce the amount of memory
1308 * required, otherwise store shinfo in the new buffer.
1310 if (rte_pktmbuf_tailroom(pkt) >= sizeof(*shinfo))
1311 shinfo = rte_pktmbuf_mtod(pkt,
1312 struct rte_mbuf_ext_shared_info *);
1314 total_len += sizeof(*shinfo) + sizeof(uintptr_t);
1315 total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
1318 if (unlikely(total_len > UINT16_MAX))
1321 buf_len = total_len;
1322 buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
1323 if (unlikely(buf == NULL))
1326 /* Initialize shinfo */
1328 shinfo->free_cb = virtio_dev_extbuf_free;
1329 shinfo->fcb_opaque = buf;
1330 rte_mbuf_ext_refcnt_set(shinfo, 1);
1332 shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
1333 virtio_dev_extbuf_free, buf);
1334 if (unlikely(shinfo == NULL)) {
1336 RTE_LOG(ERR, VHOST_DATA, "Failed to init shinfo\n");
1341 iova = rte_malloc_virt2iova(buf);
1342 rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
1343 rte_pktmbuf_reset_headroom(pkt);
1349 * Allocate a host supported pktmbuf.
1351 static __rte_always_inline struct rte_mbuf *
1352 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
1355 struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
1357 if (unlikely(pkt == NULL))
1360 if (rte_pktmbuf_tailroom(pkt) >= data_len)
1363 /* attach an external buffer if supported */
1364 if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
1367 /* check if chained buffers are allowed */
1368 if (!dev->linearbuf)
1371 /* Data doesn't fit into the buffer and the host supports
1372 * only linear buffers
1374 rte_pktmbuf_free(pkt);
1379 static __rte_noinline uint16_t
1380 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1381 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1384 uint16_t free_entries;
1386 if (unlikely(dev->dequeue_zero_copy)) {
1387 struct zcopy_mbuf *zmbuf, *next;
1389 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1390 zmbuf != NULL; zmbuf = next) {
1391 next = TAILQ_NEXT(zmbuf, next);
1393 if (mbuf_is_consumed(zmbuf->mbuf)) {
1394 update_shadow_used_ring_split(vq,
1395 zmbuf->desc_idx, 0);
1396 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1397 restore_mbuf(zmbuf->mbuf);
1398 rte_pktmbuf_free(zmbuf->mbuf);
1404 if (likely(vq->shadow_used_idx)) {
1405 flush_shadow_used_ring_split(dev, vq);
1406 vhost_vring_call_split(dev, vq);
1410 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1412 if (free_entries == 0)
1416 * The ordering between avail index and
1417 * desc reads needs to be enforced.
1421 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1423 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1425 count = RTE_MIN(count, MAX_PKT_BURST);
1426 count = RTE_MIN(count, free_entries);
1427 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1430 for (i = 0; i < count; i++) {
1431 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1434 uint16_t nr_vec = 0;
1437 if (unlikely(fill_vec_buf_split(dev, vq,
1438 vq->last_avail_idx + i,
1440 &head_idx, &buf_len,
1441 VHOST_ACCESS_RO) < 0))
1444 if (likely(dev->dequeue_zero_copy == 0))
1445 update_shadow_used_ring_split(vq, head_idx, 0);
1447 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1448 if (unlikely(pkts[i] == NULL))
1451 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1453 if (unlikely(err)) {
1454 rte_pktmbuf_free(pkts[i]);
1458 if (unlikely(dev->dequeue_zero_copy)) {
1459 struct zcopy_mbuf *zmbuf;
1461 zmbuf = get_zmbuf(vq);
1463 rte_pktmbuf_free(pkts[i]);
1466 zmbuf->mbuf = pkts[i];
1467 zmbuf->desc_idx = head_idx;
1470 * Pin lock the mbuf; we will check later to see
1471 * whether the mbuf is freed (when we are the last
1472 * user) or not. If that's the case, we then could
1473 * update the used ring safely.
1475 rte_mbuf_refcnt_update(pkts[i], 1);
1478 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1481 vq->last_avail_idx += i;
1483 if (likely(dev->dequeue_zero_copy == 0)) {
1484 do_data_copy_dequeue(vq);
1485 if (unlikely(i < count))
1486 vq->shadow_used_idx = i;
1487 if (likely(vq->shadow_used_idx)) {
1488 flush_shadow_used_ring_split(dev, vq);
1489 vhost_vring_call_split(dev, vq);
1496 static __rte_noinline uint16_t
1497 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1498 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1502 if (unlikely(dev->dequeue_zero_copy)) {
1503 struct zcopy_mbuf *zmbuf, *next;
1505 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1506 zmbuf != NULL; zmbuf = next) {
1507 next = TAILQ_NEXT(zmbuf, next);
1509 if (mbuf_is_consumed(zmbuf->mbuf)) {
1510 update_shadow_used_ring_packed(vq,
1515 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1516 restore_mbuf(zmbuf->mbuf);
1517 rte_pktmbuf_free(zmbuf->mbuf);
1523 if (likely(vq->shadow_used_idx)) {
1524 flush_shadow_used_ring_packed(dev, vq);
1525 vhost_vring_call_packed(dev, vq);
1529 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1531 count = RTE_MIN(count, MAX_PKT_BURST);
1532 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1535 for (i = 0; i < count; i++) {
1536 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1539 uint16_t desc_count, nr_vec = 0;
1542 if (unlikely(fill_vec_buf_packed(dev, vq,
1543 vq->last_avail_idx, &desc_count,
1546 VHOST_ACCESS_RO) < 0))
1549 if (likely(dev->dequeue_zero_copy == 0))
1550 update_shadow_used_ring_packed(vq, buf_id, 0,
1553 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
1554 if (unlikely(pkts[i] == NULL))
1557 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1559 if (unlikely(err)) {
1560 rte_pktmbuf_free(pkts[i]);
1564 if (unlikely(dev->dequeue_zero_copy)) {
1565 struct zcopy_mbuf *zmbuf;
1567 zmbuf = get_zmbuf(vq);
1569 rte_pktmbuf_free(pkts[i]);
1572 zmbuf->mbuf = pkts[i];
1573 zmbuf->desc_idx = buf_id;
1574 zmbuf->desc_count = desc_count;
1577 * Pin lock the mbuf; we will check later to see
1578 * whether the mbuf is freed (when we are the last
1579 * user) or not. If that's the case, we then could
1580 * update the used ring safely.
1582 rte_mbuf_refcnt_update(pkts[i], 1);
1585 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1588 vq->last_avail_idx += desc_count;
1589 if (vq->last_avail_idx >= vq->size) {
1590 vq->last_avail_idx -= vq->size;
1591 vq->avail_wrap_counter ^= 1;
1595 if (likely(dev->dequeue_zero_copy == 0)) {
1596 do_data_copy_dequeue(vq);
1597 if (unlikely(i < count))
1598 vq->shadow_used_idx = i;
1599 if (likely(vq->shadow_used_idx)) {
1600 flush_shadow_used_ring_packed(dev, vq);
1601 vhost_vring_call_packed(dev, vq);
1609 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1610 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1612 struct virtio_net *dev;
1613 struct rte_mbuf *rarp_mbuf = NULL;
1614 struct vhost_virtqueue *vq;
1616 dev = get_device(vid);
1620 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1621 RTE_LOG(ERR, VHOST_DATA,
1622 "(%d) %s: built-in vhost net backend is disabled.\n",
1623 dev->vid, __func__);
1627 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1628 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1629 dev->vid, __func__, queue_id);
1633 vq = dev->virtqueue[queue_id];
1635 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1638 if (unlikely(vq->enabled == 0)) {
1640 goto out_access_unlock;
1643 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1644 vhost_user_iotlb_rd_lock(vq);
1646 if (unlikely(vq->access_ok == 0))
1647 if (unlikely(vring_translate(dev, vq) < 0)) {
1653 * Construct a RARP broadcast packet, and inject it to the "pkts"
1654 * array, to looks like that guest actually send such packet.
1656 * Check user_send_rarp() for more information.
1658 * broadcast_rarp shares a cacheline in the virtio_net structure
1659 * with some fields that are accessed during enqueue and
1660 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1661 * result in false sharing between enqueue and dequeue.
1663 * Prevent unnecessary false sharing by reading broadcast_rarp first
1664 * and only performing cmpset if the read indicates it is likely to
1667 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1668 rte_atomic16_cmpset((volatile uint16_t *)
1669 &dev->broadcast_rarp.cnt, 1, 0))) {
1671 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1672 if (rarp_mbuf == NULL) {
1673 RTE_LOG(ERR, VHOST_DATA,
1674 "Failed to make RARP packet.\n");
1681 if (vq_is_packed(dev))
1682 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1684 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1687 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1688 vhost_user_iotlb_rd_unlock(vq);
1691 rte_spinlock_unlock(&vq->access_lock);
1693 if (unlikely(rarp_mbuf != NULL)) {
1695 * Inject it to the head of "pkts" array, so that switch's mac
1696 * learning table will get updated first.
1698 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1699 pkts[0] = rarp_mbuf;