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;
77 struct virtio_hw *hw = vq->hw;
80 struct vring_used_elem *rused;
81 struct rte_mbuf **sw_ring;
82 struct rte_mbuf **sw_ring_end;
83 uint16_t nb_pkts_received = 0;
84 __m128i shuf_msk1, shuf_msk2, len_adjust;
86 shuf_msk1 = _mm_set_epi8(
87 0xFF, 0xFF, 0xFF, 0xFF,
88 0xFF, 0xFF, /* vlan tci */
90 0xFF, 0xFF, 5, 4, /* pkt len */
91 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
95 shuf_msk2 = _mm_set_epi8(
96 0xFF, 0xFF, 0xFF, 0xFF,
97 0xFF, 0xFF, /* vlan tci */
99 0xFF, 0xFF, 13, 12, /* pkt len */
100 0xFF, 0xFF, 0xFF, 0xFF /* packet type */
103 /* Subtract the header length.
104 * In which case do we need the header length in used->len ?
106 len_adjust = _mm_set_epi16(
109 (uint16_t)-vq->hw->vtnet_hdr_size,
110 0, (uint16_t)-vq->hw->vtnet_hdr_size,
113 if (unlikely(hw->started == 0))
114 return nb_pkts_received;
116 if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
119 nb_used = VIRTQUEUE_NUSED(vq);
121 rte_compiler_barrier();
123 if (unlikely(nb_used == 0))
126 nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
127 nb_used = RTE_MIN(nb_used, nb_pkts);
129 desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
130 rused = &vq->vq_ring.used->ring[desc_idx];
131 sw_ring = &vq->sw_ring[desc_idx];
132 sw_ring_end = &vq->sw_ring[vq->vq_nentries];
134 rte_prefetch0(rused);
136 if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
137 virtio_rxq_rearm_vec(rxvq);
138 if (unlikely(virtqueue_kick_prepare(vq)))
139 virtqueue_notify(vq);
142 for (nb_pkts_received = 0;
143 nb_pkts_received < nb_used;) {
144 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
145 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
146 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
148 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
149 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
150 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
152 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
153 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
154 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
156 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
157 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
158 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
160 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
161 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
162 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
164 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
165 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
166 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
167 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
168 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
170 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
173 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
174 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
175 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
176 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
177 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
179 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
182 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
183 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
184 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
185 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
186 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
188 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
191 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
192 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
193 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
194 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
195 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
197 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
200 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
201 if (sw_ring + nb_used <= sw_ring_end)
202 nb_pkts_received += nb_used;
204 nb_pkts_received += sw_ring_end - sw_ring;
207 if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
209 nb_pkts_received += sw_ring_end - sw_ring;
212 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
214 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
215 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
216 rused += RTE_VIRTIO_DESC_PER_LOOP;
217 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
222 vq->vq_used_cons_idx += nb_pkts_received;
223 vq->vq_free_cnt += nb_pkts_received;
224 rxvq->stats.packets += nb_pkts_received;
225 return nb_pkts_received;