net/virtio: cleanup conditional compilation
[dpdk.git] / drivers / net / virtio / virtio_rxtx_simple.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include <rte_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
46 #include <rte_mbuf.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_prefetch.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
52 #include <rte_byteorder.h>
53
54 #include "virtio_logs.h"
55 #include "virtio_ethdev.h"
56 #include "virtqueue.h"
57 #include "virtio_rxtx.h"
58
59 #define RTE_VIRTIO_VPMD_RX_BURST 32
60 #define RTE_VIRTIO_DESC_PER_LOOP 8
61 #define RTE_VIRTIO_VPMD_RX_REARM_THRESH RTE_VIRTIO_VPMD_RX_BURST
62
63 #ifndef __INTEL_COMPILER
64 #pragma GCC diagnostic ignored "-Wcast-qual"
65 #endif
66
67 int __attribute__((cold))
68 virtqueue_enqueue_recv_refill_simple(struct virtqueue *vq,
69         struct rte_mbuf *cookie)
70 {
71         struct vq_desc_extra *dxp;
72         struct vring_desc *start_dp;
73         uint16_t desc_idx;
74
75         desc_idx = vq->vq_avail_idx & (vq->vq_nentries - 1);
76         dxp = &vq->vq_descx[desc_idx];
77         dxp->cookie = (void *)cookie;
78         vq->sw_ring[desc_idx] = cookie;
79
80         start_dp = vq->vq_ring.desc;
81         start_dp[desc_idx].addr =
82                 VIRTIO_MBUF_ADDR(cookie, vq) +
83                 RTE_PKTMBUF_HEADROOM - vq->hw->vtnet_hdr_size;
84         start_dp[desc_idx].len = cookie->buf_len -
85                 RTE_PKTMBUF_HEADROOM + vq->hw->vtnet_hdr_size;
86
87         vq->vq_free_cnt--;
88         vq->vq_avail_idx++;
89
90         return 0;
91 }
92
93 static inline void
94 virtio_rxq_rearm_vec(struct virtnet_rx *rxvq)
95 {
96         int i;
97         uint16_t desc_idx;
98         struct rte_mbuf **sw_ring;
99         struct vring_desc *start_dp;
100         int ret;
101         struct virtqueue *vq = rxvq->vq;
102
103         desc_idx = vq->vq_avail_idx & (vq->vq_nentries - 1);
104         sw_ring = &vq->sw_ring[desc_idx];
105         start_dp = &vq->vq_ring.desc[desc_idx];
106
107         ret = rte_mempool_get_bulk(rxvq->mpool, (void **)sw_ring,
108                 RTE_VIRTIO_VPMD_RX_REARM_THRESH);
109         if (unlikely(ret)) {
110                 rte_eth_devices[rxvq->port_id].data->rx_mbuf_alloc_failed +=
111                         RTE_VIRTIO_VPMD_RX_REARM_THRESH;
112                 return;
113         }
114
115         for (i = 0; i < RTE_VIRTIO_VPMD_RX_REARM_THRESH; i++) {
116                 uintptr_t p;
117
118                 p = (uintptr_t)&sw_ring[i]->rearm_data;
119                 *(uint64_t *)p = rxvq->mbuf_initializer;
120
121                 start_dp[i].addr =
122                         VIRTIO_MBUF_ADDR(sw_ring[i], vq) +
123                         RTE_PKTMBUF_HEADROOM - vq->hw->vtnet_hdr_size;
124                 start_dp[i].len = sw_ring[i]->buf_len -
125                         RTE_PKTMBUF_HEADROOM + vq->hw->vtnet_hdr_size;
126         }
127
128         vq->vq_avail_idx += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
129         vq->vq_free_cnt -= RTE_VIRTIO_VPMD_RX_REARM_THRESH;
130         vq_update_avail_idx(vq);
131 }
132
133 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
134
135 #include <tmmintrin.h>
136
137 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
138  *
139  * This routine is for non-mergeable RX, one desc for each guest buffer.
140  * This routine is based on the RX ring layout optimization. Each entry in the
141  * avail ring points to the desc with the same index in the desc ring and this
142  * will never be changed in the driver.
143  *
144  * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
145  */
146 uint16_t
147 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
148         uint16_t nb_pkts)
149 {
150         struct virtnet_rx *rxvq = rx_queue;
151         struct virtqueue *vq = rxvq->vq;
152         uint16_t nb_used;
153         uint16_t desc_idx;
154         struct vring_used_elem *rused;
155         struct rte_mbuf **sw_ring;
156         struct rte_mbuf **sw_ring_end;
157         uint16_t nb_pkts_received;
158         __m128i shuf_msk1, shuf_msk2, len_adjust;
159
160         shuf_msk1 = _mm_set_epi8(
161                 0xFF, 0xFF, 0xFF, 0xFF,
162                 0xFF, 0xFF,             /* vlan tci */
163                 5, 4,                   /* dat len */
164                 0xFF, 0xFF, 5, 4,       /* pkt len */
165                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
166
167         );
168
169         shuf_msk2 = _mm_set_epi8(
170                 0xFF, 0xFF, 0xFF, 0xFF,
171                 0xFF, 0xFF,             /* vlan tci */
172                 13, 12,                 /* dat len */
173                 0xFF, 0xFF, 13, 12,     /* pkt len */
174                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
175         );
176
177         /* Subtract the header length.
178         *  In which case do we need the header length in used->len ?
179         */
180         len_adjust = _mm_set_epi16(
181                 0, 0,
182                 0,
183                 (uint16_t)-vq->hw->vtnet_hdr_size,
184                 0, (uint16_t)-vq->hw->vtnet_hdr_size,
185                 0, 0);
186
187         if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
188                 return 0;
189
190         nb_used = VIRTQUEUE_NUSED(vq);
191
192         rte_compiler_barrier();
193
194         if (unlikely(nb_used == 0))
195                 return 0;
196
197         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
198         nb_used = RTE_MIN(nb_used, nb_pkts);
199
200         desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
201         rused = &vq->vq_ring.used->ring[desc_idx];
202         sw_ring  = &vq->sw_ring[desc_idx];
203         sw_ring_end = &vq->sw_ring[vq->vq_nentries];
204
205         rte_prefetch0(rused);
206
207         if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
208                 virtio_rxq_rearm_vec(rxvq);
209                 if (unlikely(virtqueue_kick_prepare(vq)))
210                         virtqueue_notify(vq);
211         }
212
213         for (nb_pkts_received = 0;
214                 nb_pkts_received < nb_used;) {
215                 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
216                 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
217                 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
218
219                 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
220                 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
221                 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
222
223                 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
224                 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
225                 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
226
227                 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
228                 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
229                 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
230
231                 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
232                 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
233                 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
234
235                 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
236                 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
237                 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
238                 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
239                 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
240                         pkt_mb[1]);
241                 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
242                         pkt_mb[0]);
243
244                 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
245                 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
246                 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
247                 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
248                 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
249                         pkt_mb[3]);
250                 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
251                         pkt_mb[2]);
252
253                 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
254                 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
255                 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
256                 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
257                 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
258                         pkt_mb[5]);
259                 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
260                         pkt_mb[4]);
261
262                 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
263                 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
264                 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
265                 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
266                 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
267                         pkt_mb[7]);
268                 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
269                         pkt_mb[6]);
270
271                 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
272                         if (sw_ring + nb_used <= sw_ring_end)
273                                 nb_pkts_received += nb_used;
274                         else
275                                 nb_pkts_received += sw_ring_end - sw_ring;
276                         break;
277                 } else {
278                         if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
279                                 sw_ring_end)) {
280                                 nb_pkts_received += sw_ring_end - sw_ring;
281                                 break;
282                         } else {
283                                 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
284
285                                 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
286                                 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
287                                 rused   += RTE_VIRTIO_DESC_PER_LOOP;
288                                 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
289                         }
290                 }
291         }
292
293         vq->vq_used_cons_idx += nb_pkts_received;
294         vq->vq_free_cnt += nb_pkts_received;
295         rxvq->stats.packets += nb_pkts_received;
296         return nb_pkts_received;
297 }
298
299 #endif
300
301 #define VIRTIO_TX_FREE_THRESH 32
302 #define VIRTIO_TX_MAX_FREE_BUF_SZ 32
303 #define VIRTIO_TX_FREE_NR 32
304 /* TODO: vq->tx_free_cnt could mean num of free slots so we could avoid shift */
305 static inline void
306 virtio_xmit_cleanup(struct virtqueue *vq)
307 {
308         uint16_t i, desc_idx;
309         uint32_t nb_free = 0;
310         struct rte_mbuf *m, *free[VIRTIO_TX_MAX_FREE_BUF_SZ];
311
312         desc_idx = (uint16_t)(vq->vq_used_cons_idx &
313                    ((vq->vq_nentries >> 1) - 1));
314         m = (struct rte_mbuf *)vq->vq_descx[desc_idx++].cookie;
315         m = __rte_pktmbuf_prefree_seg(m);
316         if (likely(m != NULL)) {
317                 free[0] = m;
318                 nb_free = 1;
319                 for (i = 1; i < VIRTIO_TX_FREE_NR; i++) {
320                         m = (struct rte_mbuf *)vq->vq_descx[desc_idx++].cookie;
321                         m = __rte_pktmbuf_prefree_seg(m);
322                         if (likely(m != NULL)) {
323                                 if (likely(m->pool == free[0]->pool))
324                                         free[nb_free++] = m;
325                                 else {
326                                         rte_mempool_put_bulk(free[0]->pool,
327                                                 (void **)free,
328                                                 RTE_MIN(RTE_DIM(free),
329                                                         nb_free));
330                                         free[0] = m;
331                                         nb_free = 1;
332                                 }
333                         }
334                 }
335                 rte_mempool_put_bulk(free[0]->pool, (void **)free,
336                         RTE_MIN(RTE_DIM(free), nb_free));
337         } else {
338                 for (i = 1; i < VIRTIO_TX_FREE_NR; i++) {
339                         m = (struct rte_mbuf *)vq->vq_descx[desc_idx++].cookie;
340                         m = __rte_pktmbuf_prefree_seg(m);
341                         if (m != NULL)
342                                 rte_mempool_put(m->pool, m);
343                 }
344         }
345
346         vq->vq_used_cons_idx += VIRTIO_TX_FREE_NR;
347         vq->vq_free_cnt += (VIRTIO_TX_FREE_NR << 1);
348 }
349
350 uint16_t
351 virtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
352         uint16_t nb_pkts)
353 {
354         struct virtnet_tx *txvq = tx_queue;
355         struct virtqueue *vq = txvq->vq;
356         uint16_t nb_used;
357         uint16_t desc_idx;
358         struct vring_desc *start_dp;
359         uint16_t nb_tail, nb_commit;
360         int i;
361         uint16_t desc_idx_max = (vq->vq_nentries >> 1) - 1;
362
363         nb_used = VIRTQUEUE_NUSED(vq);
364         rte_compiler_barrier();
365
366         if (nb_used >= VIRTIO_TX_FREE_THRESH)
367                 virtio_xmit_cleanup(vq);
368
369         nb_commit = nb_pkts = RTE_MIN((vq->vq_free_cnt >> 1), nb_pkts);
370         desc_idx = (uint16_t)(vq->vq_avail_idx & desc_idx_max);
371         start_dp = vq->vq_ring.desc;
372         nb_tail = (uint16_t) (desc_idx_max + 1 - desc_idx);
373
374         if (nb_commit >= nb_tail) {
375                 for (i = 0; i < nb_tail; i++)
376                         vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
377                 for (i = 0; i < nb_tail; i++) {
378                         start_dp[desc_idx].addr =
379                                 VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
380                         start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
381                         tx_pkts++;
382                         desc_idx++;
383                 }
384                 nb_commit -= nb_tail;
385                 desc_idx = 0;
386         }
387         for (i = 0; i < nb_commit; i++)
388                 vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
389         for (i = 0; i < nb_commit; i++) {
390                 start_dp[desc_idx].addr =
391                         VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
392                 start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
393                 tx_pkts++;
394                 desc_idx++;
395         }
396
397         rte_compiler_barrier();
398
399         vq->vq_free_cnt -= (uint16_t)(nb_pkts << 1);
400         vq->vq_avail_idx += nb_pkts;
401         vq->vq_ring.avail->idx = vq->vq_avail_idx;
402         txvq->stats.packets += nb_pkts;
403
404         if (likely(nb_pkts)) {
405                 if (unlikely(virtqueue_kick_prepare(vq)))
406                         virtqueue_notify(vq);
407         }
408
409         return nb_pkts;
410 }
411
412 int __attribute__((cold))
413 virtio_rxq_vec_setup(struct virtnet_rx *rxq)
414 {
415         uintptr_t p;
416         struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
417
418         mb_def.nb_segs = 1;
419         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
420         mb_def.port = rxq->port_id;
421         rte_mbuf_refcnt_set(&mb_def, 1);
422
423         /* prevent compiler reordering: rearm_data covers previous fields */
424         rte_compiler_barrier();
425         p = (uintptr_t)&mb_def.rearm_data;
426         rxq->mbuf_initializer = *(uint64_t *)p;
427
428         return 0;
429 }