examples/l3fwd: add NEON implementation
[dpdk.git] / examples / l3fwd / l3fwd_em_hlm.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 #ifndef __L3FWD_EM_HLM_H__
36 #define __L3FWD_EM_HLM_H__
37
38 #if defined RTE_ARCH_X86
39 #include "l3fwd_sse.h"
40 #include "l3fwd_em_hlm_sse.h"
41 #elif defined RTE_MACHINE_CPUFLAG_NEON
42 #include "l3fwd_neon.h"
43 #include "l3fwd_em_hlm_neon.h"
44 #endif
45
46 static __rte_always_inline void
47 em_get_dst_port_ipv4x8(struct lcore_conf *qconf, struct rte_mbuf *m[8],
48                 uint8_t portid, uint16_t dst_port[8])
49 {
50         int32_t ret[8];
51         union ipv4_5tuple_host key[8];
52
53         get_ipv4_5tuple(m[0], mask0.x, &key[0]);
54         get_ipv4_5tuple(m[1], mask0.x, &key[1]);
55         get_ipv4_5tuple(m[2], mask0.x, &key[2]);
56         get_ipv4_5tuple(m[3], mask0.x, &key[3]);
57         get_ipv4_5tuple(m[4], mask0.x, &key[4]);
58         get_ipv4_5tuple(m[5], mask0.x, &key[5]);
59         get_ipv4_5tuple(m[6], mask0.x, &key[6]);
60         get_ipv4_5tuple(m[7], mask0.x, &key[7]);
61
62         const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
63                                 &key[4], &key[5], &key[6], &key[7]};
64
65         rte_hash_lookup_bulk(qconf->ipv4_lookup_struct, &key_array[0], 8, ret);
66
67         dst_port[0] = (uint8_t) ((ret[0] < 0) ?
68                         portid : ipv4_l3fwd_out_if[ret[0]]);
69         dst_port[1] = (uint8_t) ((ret[1] < 0) ?
70                         portid : ipv4_l3fwd_out_if[ret[1]]);
71         dst_port[2] = (uint8_t) ((ret[2] < 0) ?
72                         portid : ipv4_l3fwd_out_if[ret[2]]);
73         dst_port[3] = (uint8_t) ((ret[3] < 0) ?
74                         portid : ipv4_l3fwd_out_if[ret[3]]);
75         dst_port[4] = (uint8_t) ((ret[4] < 0) ?
76                         portid : ipv4_l3fwd_out_if[ret[4]]);
77         dst_port[5] = (uint8_t) ((ret[5] < 0) ?
78                         portid : ipv4_l3fwd_out_if[ret[5]]);
79         dst_port[6] = (uint8_t) ((ret[6] < 0) ?
80                         portid : ipv4_l3fwd_out_if[ret[6]]);
81         dst_port[7] = (uint8_t) ((ret[7] < 0) ?
82                         portid : ipv4_l3fwd_out_if[ret[7]]);
83
84         if (dst_port[0] >= RTE_MAX_ETHPORTS ||
85                         (enabled_port_mask & 1 << dst_port[0]) == 0)
86                 dst_port[0] = portid;
87
88         if (dst_port[1] >= RTE_MAX_ETHPORTS ||
89                         (enabled_port_mask & 1 << dst_port[1]) == 0)
90                 dst_port[1] = portid;
91
92         if (dst_port[2] >= RTE_MAX_ETHPORTS ||
93                         (enabled_port_mask & 1 << dst_port[2]) == 0)
94                 dst_port[2] = portid;
95
96         if (dst_port[3] >= RTE_MAX_ETHPORTS ||
97                         (enabled_port_mask & 1 << dst_port[3]) == 0)
98                 dst_port[3] = portid;
99
100         if (dst_port[4] >= RTE_MAX_ETHPORTS ||
101                         (enabled_port_mask & 1 << dst_port[4]) == 0)
102                 dst_port[4] = portid;
103
104         if (dst_port[5] >= RTE_MAX_ETHPORTS ||
105                         (enabled_port_mask & 1 << dst_port[5]) == 0)
106                 dst_port[5] = portid;
107
108         if (dst_port[6] >= RTE_MAX_ETHPORTS ||
109                         (enabled_port_mask & 1 << dst_port[6]) == 0)
110                 dst_port[6] = portid;
111
112         if (dst_port[7] >= RTE_MAX_ETHPORTS ||
113                         (enabled_port_mask & 1 << dst_port[7]) == 0)
114                 dst_port[7] = portid;
115
116 }
117
118 static __rte_always_inline void
119 em_get_dst_port_ipv6x8(struct lcore_conf *qconf, struct rte_mbuf *m[8],
120                 uint8_t portid, uint16_t dst_port[8])
121 {
122         int32_t ret[8];
123         union ipv6_5tuple_host key[8];
124
125         get_ipv6_5tuple(m[0], mask1.x, mask2.x, &key[0]);
126         get_ipv6_5tuple(m[1], mask1.x, mask2.x, &key[1]);
127         get_ipv6_5tuple(m[2], mask1.x, mask2.x, &key[2]);
128         get_ipv6_5tuple(m[3], mask1.x, mask2.x, &key[3]);
129         get_ipv6_5tuple(m[4], mask1.x, mask2.x, &key[4]);
130         get_ipv6_5tuple(m[5], mask1.x, mask2.x, &key[5]);
131         get_ipv6_5tuple(m[6], mask1.x, mask2.x, &key[6]);
132         get_ipv6_5tuple(m[7], mask1.x, mask2.x, &key[7]);
133
134         const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
135                         &key[4], &key[5], &key[6], &key[7]};
136
137         rte_hash_lookup_bulk(qconf->ipv6_lookup_struct, &key_array[0], 8, ret);
138
139         dst_port[0] = (uint8_t) ((ret[0] < 0) ?
140                         portid : ipv6_l3fwd_out_if[ret[0]]);
141         dst_port[1] = (uint8_t) ((ret[1] < 0) ?
142                         portid : ipv6_l3fwd_out_if[ret[1]]);
143         dst_port[2] = (uint8_t) ((ret[2] < 0) ?
144                         portid : ipv6_l3fwd_out_if[ret[2]]);
145         dst_port[3] = (uint8_t) ((ret[3] < 0) ?
146                         portid : ipv6_l3fwd_out_if[ret[3]]);
147         dst_port[4] = (uint8_t) ((ret[4] < 0) ?
148                         portid : ipv6_l3fwd_out_if[ret[4]]);
149         dst_port[5] = (uint8_t) ((ret[5] < 0) ?
150                         portid : ipv6_l3fwd_out_if[ret[5]]);
151         dst_port[6] = (uint8_t) ((ret[6] < 0) ?
152                         portid : ipv6_l3fwd_out_if[ret[6]]);
153         dst_port[7] = (uint8_t) ((ret[7] < 0) ?
154                         portid : ipv6_l3fwd_out_if[ret[7]]);
155
156         if (dst_port[0] >= RTE_MAX_ETHPORTS ||
157                         (enabled_port_mask & 1 << dst_port[0]) == 0)
158                 dst_port[0] = portid;
159
160         if (dst_port[1] >= RTE_MAX_ETHPORTS ||
161                         (enabled_port_mask & 1 << dst_port[1]) == 0)
162                 dst_port[1] = portid;
163
164         if (dst_port[2] >= RTE_MAX_ETHPORTS ||
165                         (enabled_port_mask & 1 << dst_port[2]) == 0)
166                 dst_port[2] = portid;
167
168         if (dst_port[3] >= RTE_MAX_ETHPORTS ||
169                         (enabled_port_mask & 1 << dst_port[3]) == 0)
170                 dst_port[3] = portid;
171
172         if (dst_port[4] >= RTE_MAX_ETHPORTS ||
173                         (enabled_port_mask & 1 << dst_port[4]) == 0)
174                 dst_port[4] = portid;
175
176         if (dst_port[5] >= RTE_MAX_ETHPORTS ||
177                         (enabled_port_mask & 1 << dst_port[5]) == 0)
178                 dst_port[5] = portid;
179
180         if (dst_port[6] >= RTE_MAX_ETHPORTS ||
181                         (enabled_port_mask & 1 << dst_port[6]) == 0)
182                 dst_port[6] = portid;
183
184         if (dst_port[7] >= RTE_MAX_ETHPORTS ||
185                         (enabled_port_mask & 1 << dst_port[7]) == 0)
186                 dst_port[7] = portid;
187
188 }
189
190 static __rte_always_inline uint16_t
191 em_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
192                 uint8_t portid)
193 {
194         uint8_t next_hop;
195         struct ipv4_hdr *ipv4_hdr;
196         struct ipv6_hdr *ipv6_hdr;
197         uint32_t tcp_or_udp;
198         uint32_t l3_ptypes;
199
200         tcp_or_udp = pkt->packet_type & (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP);
201         l3_ptypes = pkt->packet_type & RTE_PTYPE_L3_MASK;
202
203         if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV4)) {
204
205                 /* Handle IPv4 headers.*/
206                 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
207                                 sizeof(struct ether_hdr));
208
209                 next_hop = em_get_ipv4_dst_port(ipv4_hdr, portid,
210                                 qconf->ipv4_lookup_struct);
211
212                 if (next_hop >= RTE_MAX_ETHPORTS ||
213                                 (enabled_port_mask & 1 << next_hop) == 0)
214                         next_hop = portid;
215
216                 return next_hop;
217
218         } else if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV6)) {
219
220                 /* Handle IPv6 headers.*/
221                 ipv6_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv6_hdr *,
222                                 sizeof(struct ether_hdr));
223
224                 next_hop = em_get_ipv6_dst_port(ipv6_hdr, portid,
225                                 qconf->ipv6_lookup_struct);
226
227                 if (next_hop >= RTE_MAX_ETHPORTS ||
228                                 (enabled_port_mask & 1 << next_hop) == 0)
229                         next_hop = portid;
230
231                 return next_hop;
232
233         }
234
235         return portid;
236 }
237
238 /*
239  * Buffer optimized handling of packets, invoked
240  * from main_loop.
241  */
242 static inline void
243 l3fwd_em_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
244                 uint8_t portid, struct lcore_conf *qconf)
245 {
246         int32_t i, j, pos;
247         uint16_t dst_port[MAX_PKT_BURST];
248
249         /*
250          * Send nb_rx - nb_rx%8 packets
251          * in groups of 8.
252          */
253         int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
254
255         for (j = 0; j < 8 && j < nb_rx; j++) {
256                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j],
257                                                struct ether_hdr *) + 1);
258         }
259
260         for (j = 0; j < n; j += 8) {
261
262                 uint32_t pkt_type =
263                         pkts_burst[j]->packet_type &
264                         pkts_burst[j+1]->packet_type &
265                         pkts_burst[j+2]->packet_type &
266                         pkts_burst[j+3]->packet_type &
267                         pkts_burst[j+4]->packet_type &
268                         pkts_burst[j+5]->packet_type &
269                         pkts_burst[j+6]->packet_type &
270                         pkts_burst[j+7]->packet_type;
271
272                 uint32_t l3_type = pkt_type & RTE_PTYPE_L3_MASK;
273                 uint32_t tcp_or_udp = pkt_type &
274                         (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP);
275
276                 for (i = 0, pos = j + 8; i < 8 && pos < nb_rx; i++, pos++) {
277                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[pos],
278                                                        struct ether_hdr *) + 1);
279                 }
280
281                 if (tcp_or_udp && (l3_type == RTE_PTYPE_L3_IPV4)) {
282
283                         em_get_dst_port_ipv4x8(qconf, &pkts_burst[j], portid,
284                                                &dst_port[j]);
285
286                 } else if (tcp_or_udp && (l3_type == RTE_PTYPE_L3_IPV6)) {
287
288                         em_get_dst_port_ipv6x8(qconf, &pkts_burst[j], portid,
289                                                &dst_port[j]);
290
291                 } else {
292                         dst_port[j]   = em_get_dst_port(qconf, pkts_burst[j],
293                                                         portid);
294                         dst_port[j+1] = em_get_dst_port(qconf, pkts_burst[j+1],
295                                                         portid);
296                         dst_port[j+2] = em_get_dst_port(qconf, pkts_burst[j+2],
297                                                         portid);
298                         dst_port[j+3] = em_get_dst_port(qconf, pkts_burst[j+3],
299                                                         portid);
300                         dst_port[j+4] = em_get_dst_port(qconf, pkts_burst[j+4],
301                                                         portid);
302                         dst_port[j+5] = em_get_dst_port(qconf, pkts_burst[j+5],
303                                                         portid);
304                         dst_port[j+6] = em_get_dst_port(qconf, pkts_burst[j+6],
305                                                         portid);
306                         dst_port[j+7] = em_get_dst_port(qconf, pkts_burst[j+7],
307                                                         portid);
308                 }
309         }
310
311         for (; j < nb_rx; j++)
312                 dst_port[j] = em_get_dst_port(qconf, pkts_burst[j], portid);
313
314         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
315
316 }
317 #endif /* __L3FWD_EM_HLM_H__ */