net: add rte prefix to ether structures
[dpdk.git] / examples / l3fwd / l3fwd_neon.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation.
3  * Copyright(c) 2017-2018 Linaro Limited.
4  */
5
6 #ifndef _L3FWD_NEON_H_
7 #define _L3FWD_NEON_H_
8
9 #include "l3fwd.h"
10 #include "l3fwd_common.h"
11
12 /*
13  * Update source and destination MAC addresses in the ethernet header.
14  * Perform RFC1812 checks and updates for IPV4 packets.
15  */
16 static inline void
17 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
18 {
19         uint32x4_t te[FWDSTEP];
20         uint32x4_t ve[FWDSTEP];
21         uint32_t *p[FWDSTEP];
22
23         p[0] = rte_pktmbuf_mtod(pkt[0], uint32_t *);
24         p[1] = rte_pktmbuf_mtod(pkt[1], uint32_t *);
25         p[2] = rte_pktmbuf_mtod(pkt[2], uint32_t *);
26         p[3] = rte_pktmbuf_mtod(pkt[3], uint32_t *);
27
28         ve[0] = vreinterpretq_u32_s32(val_eth[dst_port[0]]);
29         te[0] = vld1q_u32(p[0]);
30
31         ve[1] = vreinterpretq_u32_s32(val_eth[dst_port[1]]);
32         te[1] = vld1q_u32(p[1]);
33
34         ve[2] = vreinterpretq_u32_s32(val_eth[dst_port[2]]);
35         te[2] = vld1q_u32(p[2]);
36
37         ve[3] = vreinterpretq_u32_s32(val_eth[dst_port[3]]);
38         te[3] = vld1q_u32(p[3]);
39
40         /* Update last 4 bytes */
41         ve[0] = vsetq_lane_u32(vgetq_lane_u32(te[0], 3), ve[0], 3);
42         ve[1] = vsetq_lane_u32(vgetq_lane_u32(te[1], 3), ve[1], 3);
43         ve[2] = vsetq_lane_u32(vgetq_lane_u32(te[2], 3), ve[2], 3);
44         ve[3] = vsetq_lane_u32(vgetq_lane_u32(te[3], 3), ve[3], 3);
45
46         vst1q_u32(p[0], ve[0]);
47         vst1q_u32(p[1], ve[1]);
48         vst1q_u32(p[2], ve[2]);
49         vst1q_u32(p[3], ve[3]);
50
51         rfc1812_process((struct ipv4_hdr *)((struct rte_ether_hdr *)p[0] + 1),
52                 &dst_port[0], pkt[0]->packet_type);
53         rfc1812_process((struct ipv4_hdr *)((struct rte_ether_hdr *)p[1] + 1),
54                 &dst_port[1], pkt[1]->packet_type);
55         rfc1812_process((struct ipv4_hdr *)((struct rte_ether_hdr *)p[2] + 1),
56                 &dst_port[2], pkt[2]->packet_type);
57         rfc1812_process((struct ipv4_hdr *)((struct rte_ether_hdr *)p[3] + 1),
58                 &dst_port[3], pkt[3]->packet_type);
59 }
60
61 /*
62  * Group consecutive packets with the same destination port in bursts of 4.
63  * Suppose we have array of destionation ports:
64  * dst_port[] = {a, b, c, d,, e, ... }
65  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
66  * We doing 4 comparisons at once and the result is 4 bit mask.
67  * This mask is used as an index into prebuild array of pnum values.
68  */
69 static inline uint16_t *
70 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, uint16x8_t dp1,
71              uint16x8_t dp2)
72 {
73         union {
74                 uint16_t u16[FWDSTEP + 1];
75                 uint64_t u64;
76         } *pnum = (void *)pn;
77
78         int32_t v;
79         uint16x8_t mask = {1, 2, 4, 8, 0, 0, 0, 0};
80
81         dp1 = vceqq_u16(dp1, dp2);
82         dp1 = vandq_u16(dp1, mask);
83         v = vaddvq_u16(dp1);
84
85         /* update last port counter. */
86         lp[0] += gptbl[v].lpv;
87         rte_compiler_barrier();
88
89         /* if dest port value has changed. */
90         if (v != GRPMSK) {
91                 pnum->u64 = gptbl[v].pnum;
92                 pnum->u16[FWDSTEP] = 1;
93                 lp = pnum->u16 + gptbl[v].idx;
94         }
95
96         return lp;
97 }
98
99 /**
100  * Process one packet:
101  * Update source and destination MAC addresses in the ethernet header.
102  * Perform RFC1812 checks and updates for IPV4 packets.
103  */
104 static inline void
105 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
106 {
107         struct rte_ether_hdr *eth_hdr;
108         uint32x4_t te, ve;
109
110         eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
111
112         te = vld1q_u32((uint32_t *)eth_hdr);
113         ve = vreinterpretq_u32_s32(val_eth[dst_port[0]]);
114
115
116         rfc1812_process((struct ipv4_hdr *)(eth_hdr + 1), dst_port,
117                         pkt->packet_type);
118
119         ve = vcopyq_laneq_u32(ve, 3, te, 3);
120         vst1q_u32((uint32_t *)eth_hdr, ve);
121 }
122
123 /**
124  * Send packets burst from pkts_burst to the ports in dst_port array
125  */
126 static __rte_always_inline void
127 send_packets_multi(struct lcore_conf *qconf, struct rte_mbuf **pkts_burst,
128                 uint16_t dst_port[MAX_PKT_BURST], int nb_rx)
129 {
130         int32_t k;
131         int j = 0;
132         uint16_t dlp;
133         uint16_t *lp;
134         uint16_t pnum[MAX_PKT_BURST + 1];
135
136         /*
137          * Finish packet processing and group consecutive
138          * packets with the same destination port.
139          */
140         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
141         if (k != 0) {
142                 uint16x8_t dp1, dp2;
143
144                 lp = pnum;
145                 lp[0] = 1;
146
147                 processx4_step3(pkts_burst, dst_port);
148
149                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
150                 dp1 = vld1q_u16(dst_port);
151
152                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
153                         processx4_step3(&pkts_burst[j], &dst_port[j]);
154
155                         /*
156                          * dp2:
157                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
158                          */
159                         dp2 = vld1q_u16(&dst_port[j - FWDSTEP + 1]);
160                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
161
162                         /*
163                          * dp1:
164                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
165                          */
166                         dp1 = vextq_u16(dp2, dp1, FWDSTEP - 1);
167                 }
168
169                 /*
170                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
171                  */
172                 dp2 = vextq_u16(dp1, dp1, 1);
173                 dp2 = vsetq_lane_u16(vgetq_lane_u16(dp2, 2), dp2, 3);
174                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
175
176                 /*
177                  * remove values added by the last repeated
178                  * dst port.
179                  */
180                 lp[0]--;
181                 dlp = dst_port[j - 1];
182         } else {
183                 /* set dlp and lp to the never used values. */
184                 dlp = BAD_PORT - 1;
185                 lp = pnum + MAX_PKT_BURST;
186         }
187
188         /* Process up to last 3 packets one by one. */
189         switch (nb_rx % FWDSTEP) {
190         case 3:
191                 process_packet(pkts_burst[j], dst_port + j);
192                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
193                 j++;
194                 /* fallthrough */
195         case 2:
196                 process_packet(pkts_burst[j], dst_port + j);
197                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
198                 j++;
199                 /* fallthrough */
200         case 1:
201                 process_packet(pkts_burst[j], dst_port + j);
202                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
203                 j++;
204         }
205
206         /*
207          * Send packets out, through destination port.
208          * Consecutive packets with the same destination port
209          * are already grouped together.
210          * If destination port for the packet equals BAD_PORT,
211          * then free the packet without sending it out.
212          */
213         for (j = 0; j < nb_rx; j += k) {
214
215                 int32_t m;
216                 uint16_t pn;
217
218                 pn = dst_port[j];
219                 k = pnum[j];
220
221                 if (likely(pn != BAD_PORT))
222                         send_packetsx4(qconf, pn, pkts_burst + j, k);
223                 else
224                         for (m = j; m != j + k; m++)
225                                 rte_pktmbuf_free(pkts_burst[m]);
226
227         }
228 }
229
230 #endif /* _L3FWD_NEON_H_ */