1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
11 #include <tmmintrin.h>
13 #include <rte_byteorder.h>
14 #include <rte_branch_prediction.h>
15 #include <rte_cycles.h>
16 #include <rte_ether.h>
17 #include <rte_ethdev_driver.h>
18 #include <rte_errno.h>
19 #include <rte_memory.h>
20 #include <rte_mempool.h>
21 #include <rte_malloc.h>
23 #include <rte_prefetch.h>
24 #include <rte_string_fns.h>
26 #include "virtio_rxtx_simple.h"
28 #define RTE_VIRTIO_DESC_PER_LOOP 8
30 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
32 * This routine is for non-mergeable RX, one desc for each guest buffer.
33 * This routine is based on the RX ring layout optimization. Each entry in the
34 * avail ring points to the desc with the same index in the desc ring and this
35 * will never be changed in the driver.
37 * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
40 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
43 struct virtnet_rx *rxvq = rx_queue;
44 struct virtqueue *vq = rxvq->vq;
45 struct virtio_hw *hw = vq->hw;
46 uint16_t nb_used, nb_total;
48 struct vring_used_elem *rused;
49 struct rte_mbuf **sw_ring;
50 struct rte_mbuf **sw_ring_end;
51 struct rte_mbuf **ref_rx_pkts;
52 uint16_t nb_pkts_received = 0;
53 __m128i shuf_msk1, shuf_msk2, len_adjust;
55 shuf_msk1 = _mm_set_epi8(
56 0xFF, 0xFF, 0xFF, 0xFF,
57 0xFF, 0xFF, /* vlan tci */
59 0xFF, 0xFF, 5, 4, /* pkt len */
60 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
64 shuf_msk2 = _mm_set_epi8(
65 0xFF, 0xFF, 0xFF, 0xFF,
66 0xFF, 0xFF, /* vlan tci */
68 0xFF, 0xFF, 13, 12, /* pkt len */
69 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
72 /* Subtract the header length.
73 * In which case do we need the header length in used->len ?
75 len_adjust = _mm_set_epi16(
78 (uint16_t)-vq->hw->vtnet_hdr_size,
79 0, (uint16_t)-vq->hw->vtnet_hdr_size,
82 if (unlikely(hw->started == 0))
83 return nb_pkts_received;
85 if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
88 nb_used = virtqueue_nused(vq);
90 if (unlikely(nb_used == 0))
93 nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
94 nb_used = RTE_MIN(nb_used, nb_pkts);
96 desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
97 rused = &vq->vq_split.ring.used->ring[desc_idx];
98 sw_ring = &vq->sw_ring[desc_idx];
99 sw_ring_end = &vq->sw_ring[vq->vq_nentries];
101 rte_prefetch0(rused);
103 if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
104 virtio_rxq_rearm_vec(rxvq);
105 if (unlikely(virtqueue_kick_prepare(vq)))
106 virtqueue_notify(vq);
110 ref_rx_pkts = rx_pkts;
111 for (nb_pkts_received = 0;
112 nb_pkts_received < nb_total;) {
113 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
114 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
115 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
117 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
118 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
119 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
121 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
122 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
123 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
125 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
126 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
127 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
129 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
130 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
131 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
133 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
134 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
135 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
136 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
137 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
139 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
142 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
143 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
144 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
145 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
146 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
148 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
151 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
152 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
153 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
154 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
155 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
157 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
160 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
161 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
162 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
163 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
164 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
166 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
169 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
170 if (sw_ring + nb_used <= sw_ring_end)
171 nb_pkts_received += nb_used;
173 nb_pkts_received += sw_ring_end - sw_ring;
176 if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
178 nb_pkts_received += sw_ring_end - sw_ring;
181 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
183 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
184 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
185 rused += RTE_VIRTIO_DESC_PER_LOOP;
186 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
191 vq->vq_used_cons_idx += nb_pkts_received;
192 vq->vq_free_cnt += nb_pkts_received;
193 rxvq->stats.packets += nb_pkts_received;
194 for (nb_used = 0; nb_used < nb_pkts_received; nb_used++)
195 virtio_update_packet_stats(&rxvq->stats, ref_rx_pkts[nb_used]);
197 return nb_pkts_received;