net/virtio: fix Rx stats with vectorized functions
[dpdk.git] / drivers / net / virtio / virtio_rxtx_simple_neon.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Cavium, Inc
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10
11 #include <rte_byteorder.h>
12 #include <rte_branch_prediction.h>
13 #include <rte_cycles.h>
14 #include <rte_ether.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_errno.h>
17 #include <rte_memory.h>
18 #include <rte_mempool.h>
19 #include <rte_malloc.h>
20 #include <rte_mbuf.h>
21 #include <rte_prefetch.h>
22 #include <rte_string_fns.h>
23 #include <rte_vect.h>
24
25 #include "virtio_rxtx_simple.h"
26
27 #define RTE_VIRTIO_DESC_PER_LOOP 8
28
29 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
30  *
31  * This routine is for non-mergeable RX, one desc for each guest buffer.
32  * This routine is based on the RX ring layout optimization. Each entry in the
33  * avail ring points to the desc with the same index in the desc ring and this
34  * will never be changed in the driver.
35  *
36  * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
37  */
38 uint16_t
39 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
40         uint16_t nb_pkts)
41 {
42         struct virtnet_rx *rxvq = rx_queue;
43         struct virtqueue *vq = rxvq->vq;
44         struct virtio_hw *hw = vq->hw;
45         uint16_t nb_used;
46         uint16_t desc_idx;
47         struct vring_used_elem *rused;
48         struct rte_mbuf **sw_ring;
49         struct rte_mbuf **sw_ring_end;
50         struct rte_mbuf **ref_rx_pkts;
51         uint16_t nb_pkts_received = 0;
52
53         uint8x16_t shuf_msk1 = {
54                 0xFF, 0xFF, 0xFF, 0xFF, /* packet type */
55                 4, 5, 0xFF, 0xFF,       /* pkt len */
56                 4, 5,                   /* dat len */
57                 0xFF, 0xFF,             /* vlan tci */
58                 0xFF, 0xFF, 0xFF, 0xFF
59         };
60
61         uint8x16_t shuf_msk2 = {
62                 0xFF, 0xFF, 0xFF, 0xFF, /* packet type */
63                 12, 13, 0xFF, 0xFF,     /* pkt len */
64                 12, 13,                 /* dat len */
65                 0xFF, 0xFF,             /* vlan tci */
66                 0xFF, 0xFF, 0xFF, 0xFF
67         };
68
69         /* Subtract the header length.
70          *  In which case do we need the header length in used->len ?
71          */
72         uint16x8_t len_adjust = {
73                 0, 0,
74                 (uint16_t)vq->hw->vtnet_hdr_size, 0,
75                 (uint16_t)vq->hw->vtnet_hdr_size,
76                 0,
77                 0, 0
78         };
79
80         if (unlikely(hw->started == 0))
81                 return nb_pkts_received;
82
83         if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
84                 return 0;
85
86         nb_used = VIRTQUEUE_NUSED(vq);
87
88         rte_rmb();
89
90         if (unlikely(nb_used == 0))
91                 return 0;
92
93         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
94         nb_used = RTE_MIN(nb_used, nb_pkts);
95
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];
100
101         rte_prefetch_non_temporal(rused);
102
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);
107         }
108
109         ref_rx_pkts = rx_pkts;
110         for (nb_pkts_received = 0;
111                 nb_pkts_received < nb_used;) {
112                 uint64x2_t desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
113                 uint64x2_t mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
114                 uint64x2_t pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
115
116                 mbp[0] = vld1q_u64((uint64_t *)(sw_ring + 0));
117                 desc[0] = vld1q_u64((uint64_t *)(rused + 0));
118                 vst1q_u64((uint64_t *)&rx_pkts[0], mbp[0]);
119
120                 mbp[1] = vld1q_u64((uint64_t *)(sw_ring + 2));
121                 desc[1] = vld1q_u64((uint64_t *)(rused + 2));
122                 vst1q_u64((uint64_t *)&rx_pkts[2], mbp[1]);
123
124                 mbp[2] = vld1q_u64((uint64_t *)(sw_ring + 4));
125                 desc[2] = vld1q_u64((uint64_t *)(rused + 4));
126                 vst1q_u64((uint64_t *)&rx_pkts[4], mbp[2]);
127
128                 mbp[3] = vld1q_u64((uint64_t *)(sw_ring + 6));
129                 desc[3] = vld1q_u64((uint64_t *)(rused + 6));
130                 vst1q_u64((uint64_t *)&rx_pkts[6], mbp[3]);
131
132                 pkt_mb[1] = vreinterpretq_u64_u8(vqtbl1q_u8(
133                                 vreinterpretq_u8_u64(desc[0]), shuf_msk2));
134                 pkt_mb[0] = vreinterpretq_u64_u8(vqtbl1q_u8(
135                                 vreinterpretq_u8_u64(desc[0]), shuf_msk1));
136                 pkt_mb[1] = vreinterpretq_u64_u16(vsubq_u16(
137                                 vreinterpretq_u16_u64(pkt_mb[1]), len_adjust));
138                 pkt_mb[0] = vreinterpretq_u64_u16(vsubq_u16(
139                                 vreinterpretq_u16_u64(pkt_mb[0]), len_adjust));
140                 vst1q_u64((void *)&rx_pkts[1]->rx_descriptor_fields1,
141                         pkt_mb[1]);
142                 vst1q_u64((void *)&rx_pkts[0]->rx_descriptor_fields1,
143                         pkt_mb[0]);
144
145                 pkt_mb[3] = vreinterpretq_u64_u8(vqtbl1q_u8(
146                                 vreinterpretq_u8_u64(desc[1]), shuf_msk2));
147                 pkt_mb[2] = vreinterpretq_u64_u8(vqtbl1q_u8(
148                                 vreinterpretq_u8_u64(desc[1]), shuf_msk1));
149                 pkt_mb[3] = vreinterpretq_u64_u16(vsubq_u16(
150                                 vreinterpretq_u16_u64(pkt_mb[3]), len_adjust));
151                 pkt_mb[2] = vreinterpretq_u64_u16(vsubq_u16(
152                                 vreinterpretq_u16_u64(pkt_mb[2]), len_adjust));
153                 vst1q_u64((void *)&rx_pkts[3]->rx_descriptor_fields1,
154                         pkt_mb[3]);
155                 vst1q_u64((void *)&rx_pkts[2]->rx_descriptor_fields1,
156                         pkt_mb[2]);
157
158                 pkt_mb[5] = vreinterpretq_u64_u8(vqtbl1q_u8(
159                                 vreinterpretq_u8_u64(desc[2]), shuf_msk2));
160                 pkt_mb[4] = vreinterpretq_u64_u8(vqtbl1q_u8(
161                                 vreinterpretq_u8_u64(desc[2]), shuf_msk1));
162                 pkt_mb[5] = vreinterpretq_u64_u16(vsubq_u16(
163                                 vreinterpretq_u16_u64(pkt_mb[5]), len_adjust));
164                 pkt_mb[4] = vreinterpretq_u64_u16(vsubq_u16(
165                                 vreinterpretq_u16_u64(pkt_mb[4]), len_adjust));
166                 vst1q_u64((void *)&rx_pkts[5]->rx_descriptor_fields1,
167                         pkt_mb[5]);
168                 vst1q_u64((void *)&rx_pkts[4]->rx_descriptor_fields1,
169                         pkt_mb[4]);
170
171                 pkt_mb[7] = vreinterpretq_u64_u8(vqtbl1q_u8(
172                                 vreinterpretq_u8_u64(desc[3]), shuf_msk2));
173                 pkt_mb[6] = vreinterpretq_u64_u8(vqtbl1q_u8(
174                                 vreinterpretq_u8_u64(desc[3]), shuf_msk1));
175                 pkt_mb[7] = vreinterpretq_u64_u16(vsubq_u16(
176                                 vreinterpretq_u16_u64(pkt_mb[7]), len_adjust));
177                 pkt_mb[6] = vreinterpretq_u64_u16(vsubq_u16(
178                                 vreinterpretq_u16_u64(pkt_mb[6]), len_adjust));
179                 vst1q_u64((void *)&rx_pkts[7]->rx_descriptor_fields1,
180                         pkt_mb[7]);
181                 vst1q_u64((void *)&rx_pkts[6]->rx_descriptor_fields1,
182                         pkt_mb[6]);
183
184                 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
185                         if (sw_ring + nb_used <= sw_ring_end)
186                                 nb_pkts_received += nb_used;
187                         else
188                                 nb_pkts_received += sw_ring_end - sw_ring;
189                         break;
190                 } else {
191                         if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
192                                 sw_ring_end)) {
193                                 nb_pkts_received += sw_ring_end - sw_ring;
194                                 break;
195                         } else {
196                                 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
197
198                                 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
199                                 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
200                                 rused   += RTE_VIRTIO_DESC_PER_LOOP;
201                                 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
202                         }
203                 }
204         }
205
206         vq->vq_used_cons_idx += nb_pkts_received;
207         vq->vq_free_cnt += nb_pkts_received;
208         rxvq->stats.packets += nb_pkts_received;
209         for (nb_used = 0; nb_used < nb_pkts_received; nb_used++)
210                 virtio_update_packet_stats(&rxvq->stats, ref_rx_pkts[nb_used]);
211
212         return nb_pkts_received;
213 }