New upstream version 17.11-rc3
[deb_dpdk.git] / examples / l3fwd / l3fwd_neon.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2017, Linaro Limited
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35
36 #ifndef _L3FWD_NEON_H_
37 #define _L3FWD_NEON_H_
38
39 #include "l3fwd.h"
40 #include "l3fwd_common.h"
41
42 /*
43  * Update source and destination MAC addresses in the ethernet header.
44  * Perform RFC1812 checks and updates for IPV4 packets.
45  */
46 static inline void
47 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
48 {
49         uint32x4_t te[FWDSTEP];
50         uint32x4_t ve[FWDSTEP];
51         uint32_t *p[FWDSTEP];
52
53         p[0] = rte_pktmbuf_mtod(pkt[0], uint32_t *);
54         p[1] = rte_pktmbuf_mtod(pkt[1], uint32_t *);
55         p[2] = rte_pktmbuf_mtod(pkt[2], uint32_t *);
56         p[3] = rte_pktmbuf_mtod(pkt[3], uint32_t *);
57
58         ve[0] = vreinterpretq_u32_s32(val_eth[dst_port[0]]);
59         te[0] = vld1q_u32(p[0]);
60
61         ve[1] = vreinterpretq_u32_s32(val_eth[dst_port[1]]);
62         te[1] = vld1q_u32(p[1]);
63
64         ve[2] = vreinterpretq_u32_s32(val_eth[dst_port[2]]);
65         te[2] = vld1q_u32(p[2]);
66
67         ve[3] = vreinterpretq_u32_s32(val_eth[dst_port[3]]);
68         te[3] = vld1q_u32(p[3]);
69
70         /* Update last 4 bytes */
71         ve[0] = vsetq_lane_u32(vgetq_lane_u32(te[0], 3), ve[0], 3);
72         ve[1] = vsetq_lane_u32(vgetq_lane_u32(te[1], 3), ve[1], 3);
73         ve[2] = vsetq_lane_u32(vgetq_lane_u32(te[2], 3), ve[2], 3);
74         ve[3] = vsetq_lane_u32(vgetq_lane_u32(te[3], 3), ve[3], 3);
75
76         vst1q_u32(p[0], ve[0]);
77         vst1q_u32(p[1], ve[1]);
78         vst1q_u32(p[2], ve[2]);
79         vst1q_u32(p[3], ve[3]);
80
81         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
82                 &dst_port[0], pkt[0]->packet_type);
83         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
84                 &dst_port[1], pkt[1]->packet_type);
85         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
86                 &dst_port[2], pkt[2]->packet_type);
87         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
88                 &dst_port[3], pkt[3]->packet_type);
89 }
90
91 /*
92  * Group consecutive packets with the same destination port in bursts of 4.
93  * Suppose we have array of destionation ports:
94  * dst_port[] = {a, b, c, d,, e, ... }
95  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
96  * We doing 4 comparisons at once and the result is 4 bit mask.
97  * This mask is used as an index into prebuild array of pnum values.
98  */
99 static inline uint16_t *
100 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, uint16x8_t dp1,
101              uint16x8_t dp2)
102 {
103         union {
104                 uint16_t u16[FWDSTEP + 1];
105                 uint64_t u64;
106         } *pnum = (void *)pn;
107
108         int32_t v;
109         uint16x8_t mask = {1, 2, 4, 8, 0, 0, 0, 0};
110
111         dp1 = vceqq_u16(dp1, dp2);
112         dp1 = vandq_u16(dp1, mask);
113         v = vaddvq_u16(dp1);
114
115         /* update last port counter. */
116         lp[0] += gptbl[v].lpv;
117         rte_compiler_barrier();
118
119         /* if dest port value has changed. */
120         if (v != GRPMSK) {
121                 pnum->u64 = gptbl[v].pnum;
122                 pnum->u16[FWDSTEP] = 1;
123                 lp = pnum->u16 + gptbl[v].idx;
124         }
125
126         return lp;
127 }
128
129 /**
130  * Process one packet:
131  * Update source and destination MAC addresses in the ethernet header.
132  * Perform RFC1812 checks and updates for IPV4 packets.
133  */
134 static inline void
135 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
136 {
137         struct ether_hdr *eth_hdr;
138         uint32x4_t te, ve;
139
140         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
141
142         te = vld1q_u32((uint32_t *)eth_hdr);
143         ve = vreinterpretq_u32_s32(val_eth[dst_port[0]]);
144
145
146         rfc1812_process((struct ipv4_hdr *)(eth_hdr + 1), dst_port,
147                         pkt->packet_type);
148
149         ve = vcopyq_laneq_u32(ve, 3, te, 3);
150         vst1q_u32((uint32_t *)eth_hdr, ve);
151 }
152
153 /**
154  * Send packets burst from pkts_burst to the ports in dst_port array
155  */
156 static __rte_always_inline void
157 send_packets_multi(struct lcore_conf *qconf, struct rte_mbuf **pkts_burst,
158                 uint16_t dst_port[MAX_PKT_BURST], int nb_rx)
159 {
160         int32_t k;
161         int j = 0;
162         uint16_t dlp;
163         uint16_t *lp;
164         uint16_t pnum[MAX_PKT_BURST + 1];
165
166         /*
167          * Finish packet processing and group consecutive
168          * packets with the same destination port.
169          */
170         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
171         if (k != 0) {
172                 uint16x8_t dp1, dp2;
173
174                 lp = pnum;
175                 lp[0] = 1;
176
177                 processx4_step3(pkts_burst, dst_port);
178
179                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
180                 dp1 = vld1q_u16(dst_port);
181
182                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
183                         processx4_step3(&pkts_burst[j], &dst_port[j]);
184
185                         /*
186                          * dp2:
187                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
188                          */
189                         dp2 = vld1q_u16(&dst_port[j - FWDSTEP + 1]);
190                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
191
192                         /*
193                          * dp1:
194                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
195                          */
196                         dp1 = vextq_u16(dp2, dp1, FWDSTEP - 1);
197                 }
198
199                 /*
200                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
201                  */
202                 dp2 = vextq_u16(dp1, dp1, 1);
203                 dp2 = vsetq_lane_u16(vgetq_lane_u16(dp2, 2), dp2, 3);
204                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
205
206                 /*
207                  * remove values added by the last repeated
208                  * dst port.
209                  */
210                 lp[0]--;
211                 dlp = dst_port[j - 1];
212         } else {
213                 /* set dlp and lp to the never used values. */
214                 dlp = BAD_PORT - 1;
215                 lp = pnum + MAX_PKT_BURST;
216         }
217
218         /* Process up to last 3 packets one by one. */
219         switch (nb_rx % FWDSTEP) {
220         case 3:
221                 process_packet(pkts_burst[j], dst_port + j);
222                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
223                 j++;
224                 /* fallthrough */
225         case 2:
226                 process_packet(pkts_burst[j], dst_port + j);
227                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
228                 j++;
229                 /* fallthrough */
230         case 1:
231                 process_packet(pkts_burst[j], dst_port + j);
232                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
233                 j++;
234         }
235
236         /*
237          * Send packets out, through destination port.
238          * Consecutive packets with the same destination port
239          * are already grouped together.
240          * If destination port for the packet equals BAD_PORT,
241          * then free the packet without sending it out.
242          */
243         for (j = 0; j < nb_rx; j += k) {
244
245                 int32_t m;
246                 uint16_t pn;
247
248                 pn = dst_port[j];
249                 k = pnum[j];
250
251                 if (likely(pn != BAD_PORT))
252                         send_packetsx4(qconf, pn, pkts_burst + j, k);
253                 else
254                         for (m = j; m != j + k; m++)
255                                 rte_pktmbuf_free(pkts_burst[m]);
256
257         }
258 }
259
260 #endif /* _L3FWD_NEON_H_ */