42d50d3c267a890239dd7b2fc7b69c7dcba265bd
[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
118         /* if dest port value has changed. */
119         if (v != GRPMSK) {
120                 pnum->u64 = gptbl[v].pnum;
121                 pnum->u16[FWDSTEP] = 1;
122                 lp = pnum->u16 + gptbl[v].idx;
123         }
124
125         return lp;
126 }
127
128 /**
129  * Process one packet:
130  * Update source and destination MAC addresses in the ethernet header.
131  * Perform RFC1812 checks and updates for IPV4 packets.
132  */
133 static inline void
134 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
135 {
136         struct ether_hdr *eth_hdr;
137         uint32x4_t te, ve;
138
139         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
140
141         te = vld1q_u32((uint32_t *)eth_hdr);
142         ve = vreinterpretq_u32_s32(val_eth[dst_port[0]]);
143
144
145         rfc1812_process((struct ipv4_hdr *)(eth_hdr + 1), dst_port,
146                         pkt->packet_type);
147
148         ve = vcopyq_laneq_u32(ve, 3, te, 3);
149         vst1q_u32((uint32_t *)eth_hdr, ve);
150 }
151
152 /**
153  * Send packets burst from pkts_burst to the ports in dst_port array
154  */
155 static __rte_always_inline void
156 send_packets_multi(struct lcore_conf *qconf, struct rte_mbuf **pkts_burst,
157                 uint16_t dst_port[MAX_PKT_BURST], int nb_rx)
158 {
159         int32_t k;
160         int j = 0;
161         uint16_t dlp;
162         uint16_t *lp;
163         uint16_t pnum[MAX_PKT_BURST + 1];
164
165         /*
166          * Finish packet processing and group consecutive
167          * packets with the same destination port.
168          */
169         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
170         if (k != 0) {
171                 uint16x8_t dp1, dp2;
172
173                 lp = pnum;
174                 lp[0] = 1;
175
176                 processx4_step3(pkts_burst, dst_port);
177
178                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
179                 dp1 = vld1q_u16(dst_port);
180
181                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
182                         processx4_step3(&pkts_burst[j], &dst_port[j]);
183
184                         /*
185                          * dp2:
186                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
187                          */
188                         dp2 = vld1q_u16(&dst_port[j - FWDSTEP + 1]);
189                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
190
191                         /*
192                          * dp1:
193                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
194                          */
195                         dp1 = vextq_u16(dp1, dp1, FWDSTEP - 1);
196                 }
197
198                 /*
199                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
200                  */
201                 dp2 = vextq_u16(dp1, dp1, 1);
202                 dp2 = vsetq_lane_u16(vgetq_lane_u16(dp2, 2), dp2, 3);
203                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
204
205                 /*
206                  * remove values added by the last repeated
207                  * dst port.
208                  */
209                 lp[0]--;
210                 dlp = dst_port[j - 1];
211         } else {
212                 /* set dlp and lp to the never used values. */
213                 dlp = BAD_PORT - 1;
214                 lp = pnum + MAX_PKT_BURST;
215         }
216
217         /* Process up to last 3 packets one by one. */
218         switch (nb_rx % FWDSTEP) {
219         case 3:
220                 process_packet(pkts_burst[j], dst_port + j);
221                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
222                 j++;
223                 /* fallthrough */
224         case 2:
225                 process_packet(pkts_burst[j], dst_port + j);
226                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
227                 j++;
228                 /* fallthrough */
229         case 1:
230                 process_packet(pkts_burst[j], dst_port + j);
231                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
232                 j++;
233         }
234
235         /*
236          * Send packets out, through destination port.
237          * Consecutive packets with the same destination port
238          * are already grouped together.
239          * If destination port for the packet equals BAD_PORT,
240          * then free the packet without sending it out.
241          */
242         for (j = 0; j < nb_rx; j += k) {
243
244                 int32_t m;
245                 uint16_t pn;
246
247                 pn = dst_port[j];
248                 k = pnum[j];
249
250                 if (likely(pn != BAD_PORT))
251                         send_packetsx4(qconf, pn, pkts_burst + j, k);
252                 else
253                         for (m = j; m != j + k; m++)
254                                 rte_pktmbuf_free(pkts_burst[m]);
255
256         }
257 }
258
259 #endif /* _L3FWD_NEON_H_ */