4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <tmmintrin.h>
42 #include <rte_byteorder.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_cycles.h>
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_errno.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_mempool.h>
51 #include <rte_malloc.h>
53 #include <rte_prefetch.h>
54 #include <rte_string_fns.h>
56 #include "virtio_rxtx_simple.h"
58 #define RTE_VIRTIO_VPMD_RX_BURST 32
59 #define RTE_VIRTIO_DESC_PER_LOOP 8
60 #define RTE_VIRTIO_VPMD_RX_REARM_THRESH RTE_VIRTIO_VPMD_RX_BURST
62 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
64 * This routine is for non-mergeable RX, one desc for each guest buffer.
65 * This routine is based on the RX ring layout optimization. Each entry in the
66 * avail ring points to the desc with the same index in the desc ring and this
67 * will never be changed in the driver.
69 * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
72 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
75 struct virtnet_rx *rxvq = rx_queue;
76 struct virtqueue *vq = rxvq->vq;
79 struct vring_used_elem *rused;
80 struct rte_mbuf **sw_ring;
81 struct rte_mbuf **sw_ring_end;
82 uint16_t nb_pkts_received;
83 __m128i shuf_msk1, shuf_msk2, len_adjust;
85 shuf_msk1 = _mm_set_epi8(
86 0xFF, 0xFF, 0xFF, 0xFF,
87 0xFF, 0xFF, /* vlan tci */
89 0xFF, 0xFF, 5, 4, /* pkt len */
90 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
94 shuf_msk2 = _mm_set_epi8(
95 0xFF, 0xFF, 0xFF, 0xFF,
96 0xFF, 0xFF, /* vlan tci */
98 0xFF, 0xFF, 13, 12, /* pkt len */
99 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
102 /* Subtract the header length.
103 * In which case do we need the header length in used->len ?
105 len_adjust = _mm_set_epi16(
108 (uint16_t)-vq->hw->vtnet_hdr_size,
109 0, (uint16_t)-vq->hw->vtnet_hdr_size,
112 if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
115 nb_used = VIRTQUEUE_NUSED(vq);
117 rte_compiler_barrier();
119 if (unlikely(nb_used == 0))
122 nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
123 nb_used = RTE_MIN(nb_used, nb_pkts);
125 desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
126 rused = &vq->vq_ring.used->ring[desc_idx];
127 sw_ring = &vq->sw_ring[desc_idx];
128 sw_ring_end = &vq->sw_ring[vq->vq_nentries];
130 rte_prefetch0(rused);
132 if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
133 virtio_rxq_rearm_vec(rxvq);
134 if (unlikely(virtqueue_kick_prepare(vq)))
135 virtqueue_notify(vq);
138 for (nb_pkts_received = 0;
139 nb_pkts_received < nb_used;) {
140 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
141 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
142 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
144 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
145 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
146 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
148 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
149 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
150 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
152 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
153 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
154 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
156 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
157 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
158 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
160 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
161 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
162 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
163 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
164 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
166 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
169 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
170 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
171 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
172 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
173 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
175 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
178 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
179 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
180 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
181 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
182 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
184 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
187 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
188 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
189 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
190 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
191 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
193 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
196 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
197 if (sw_ring + nb_used <= sw_ring_end)
198 nb_pkts_received += nb_used;
200 nb_pkts_received += sw_ring_end - sw_ring;
203 if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
205 nb_pkts_received += sw_ring_end - sw_ring;
208 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
210 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
211 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
212 rused += RTE_VIRTIO_DESC_PER_LOOP;
213 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
218 vq->vq_used_cons_idx += nb_pkts_received;
219 vq->vq_free_cnt += nb_pkts_received;
220 rxvq->stats.packets += nb_pkts_received;
221 return nb_pkts_received;