New upstream version 17.11-rc3
[deb_dpdk.git] / examples / l3fwd / l3fwd_lpm_neon.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-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_LPM_NEON_H__
36 #define __L3FWD_LPM_NEON_H__
37
38 #include <arm_neon.h>
39
40 #include "l3fwd_neon.h"
41
42 /*
43  * Read packet_type and destination IPV4 addresses from 4 mbufs.
44  */
45 static inline void
46 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
47                 int32x4_t *dip,
48                 uint32_t *ipv4_flag)
49 {
50         struct ipv4_hdr *ipv4_hdr;
51         struct ether_hdr *eth_hdr;
52         int32_t dst[FWDSTEP];
53
54         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
55         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
56         dst[0] = ipv4_hdr->dst_addr;
57         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
58
59         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
60         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
61         dst[1] = ipv4_hdr->dst_addr;
62         ipv4_flag[0] &= pkt[1]->packet_type;
63
64         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
65         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
66         dst[2] = ipv4_hdr->dst_addr;
67         ipv4_flag[0] &= pkt[2]->packet_type;
68
69         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
70         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
71         dst[3] = ipv4_hdr->dst_addr;
72         ipv4_flag[0] &= pkt[3]->packet_type;
73
74         dip[0] = vld1q_s32(dst);
75 }
76
77 /*
78  * Lookup into LPM for destination port.
79  * If lookup fails, use incoming port (portid) as destination port.
80  */
81 static inline void
82 processx4_step2(const struct lcore_conf *qconf,
83                 int32x4_t dip,
84                 uint32_t ipv4_flag,
85                 uint16_t portid,
86                 struct rte_mbuf *pkt[FWDSTEP],
87                 uint16_t dprt[FWDSTEP])
88 {
89         rte_xmm_t dst;
90
91         dip = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(dip)));
92
93         /* if all 4 packets are IPV4. */
94         if (likely(ipv4_flag)) {
95                 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dst.u32,
96                         portid);
97                 /* get rid of unused upper 16 bit for each dport. */
98                 vst1_s16((int16_t *)dprt, vqmovn_s32(dst.x));
99         } else {
100                 dst.x = dip;
101                 dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0],
102                                                      dst.u32[0], portid);
103                 dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1],
104                                                      dst.u32[1], portid);
105                 dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2],
106                                                      dst.u32[2], portid);
107                 dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3],
108                                                      dst.u32[3], portid);
109         }
110 }
111
112 /*
113  * Buffer optimized handling of packets, invoked
114  * from main_loop.
115  */
116 static inline void
117 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
118                         uint16_t portid, struct lcore_conf *qconf)
119 {
120         int32_t i = 0, j = 0;
121         uint16_t dst_port[MAX_PKT_BURST];
122         int32x4_t dip;
123         uint32_t ipv4_flag;
124         const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
125         const int32_t m = nb_rx % FWDSTEP;
126
127         if (k) {
128                 for (i = 0; i < FWDSTEP; i++) {
129                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i],
130                                                 struct ether_hdr *) + 1);
131                 }
132
133                 for (j = 0; j != k - FWDSTEP; j += FWDSTEP) {
134                         for (i = 0; i < FWDSTEP; i++) {
135                                 rte_prefetch0(rte_pktmbuf_mtod(
136                                                 pkts_burst[j + i + FWDSTEP],
137                                                 struct ether_hdr *) + 1);
138                         }
139
140                         processx4_step1(&pkts_burst[j], &dip, &ipv4_flag);
141                         processx4_step2(qconf, dip, ipv4_flag, portid,
142                                         &pkts_burst[j], &dst_port[j]);
143                 }
144
145                 processx4_step1(&pkts_burst[j], &dip, &ipv4_flag);
146                 processx4_step2(qconf, dip, ipv4_flag, portid, &pkts_burst[j],
147                                 &dst_port[j]);
148
149                 j += FWDSTEP;
150         }
151
152         if (m) {
153                 /* Prefetch last up to 3 packets one by one */
154                 switch (m) {
155                 case 3:
156                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j],
157                                                 struct ether_hdr *) + 1);
158                         j++;
159                         /* fallthrough */
160                 case 2:
161                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j],
162                                                 struct ether_hdr *) + 1);
163                         j++;
164                         /* fallthrough */
165                 case 1:
166                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j],
167                                                 struct ether_hdr *) + 1);
168                         j++;
169                 }
170
171                 j -= m;
172                 /* Classify last up to 3 packets one by one */
173                 switch (m) {
174                 case 3:
175                         dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j],
176                                                        portid);
177                         j++;
178                         /* fallthrough */
179                 case 2:
180                         dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j],
181                                                        portid);
182                         j++;
183                         /* fallthrough */
184                 case 1:
185                         dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j],
186                                                        portid);
187                 }
188         }
189
190         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
191 }
192
193 #endif /* __L3FWD_LPM_NEON_H__ */