Imported Upstream version 16.04
[deb_dpdk.git] / examples / l3fwd / l3fwd_lpm_sse.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __L3FWD_LPM_SSE_H__
35 #define __L3FWD_LPM_SSE_H__
36
37 #include "l3fwd_sse.h"
38
39 static inline __attribute__((always_inline)) uint16_t
40 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
41                 uint8_t portid)
42 {
43         uint32_t next_hop_ipv4;
44         uint8_t next_hop_ipv6;
45         struct ipv6_hdr *ipv6_hdr;
46         struct ipv4_hdr *ipv4_hdr;
47         struct ether_hdr *eth_hdr;
48
49         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
50
51                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
52                 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
53
54                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
55                                 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop_ipv4) == 0) ?
56                                                 next_hop_ipv4 : portid);
57
58         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
59
60                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
61                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
62
63                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
64                                 ipv6_hdr->dst_addr, &next_hop_ipv6) == 0)
65                                 ? next_hop_ipv6 : portid);
66
67         }
68
69         return portid;
70 }
71
72 /*
73  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
74  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
75  * header and dst_ipv4 value is not used.
76  */
77 static inline __attribute__((always_inline)) uint16_t
78 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
79         uint32_t dst_ipv4, uint8_t portid)
80 {
81         uint32_t next_hop_ipv4;
82         uint8_t next_hop_ipv6;
83         struct ipv6_hdr *ipv6_hdr;
84         struct ether_hdr *eth_hdr;
85
86         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
87                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
88                         &next_hop_ipv4) == 0) ? next_hop_ipv4 : portid);
89
90         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
91
92                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
93                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
94
95                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
96                                 ipv6_hdr->dst_addr, &next_hop_ipv6) == 0)
97                                 ? next_hop_ipv6 : portid);
98
99         }
100
101         return portid;
102
103 }
104
105 /*
106  * Read packet_type and destination IPV4 addresses from 4 mbufs.
107  */
108 static inline void
109 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
110                 __m128i *dip,
111                 uint32_t *ipv4_flag)
112 {
113         struct ipv4_hdr *ipv4_hdr;
114         struct ether_hdr *eth_hdr;
115         uint32_t x0, x1, x2, x3;
116
117         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
118         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
119         x0 = ipv4_hdr->dst_addr;
120         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
121
122         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
123         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
124         x1 = ipv4_hdr->dst_addr;
125         ipv4_flag[0] &= pkt[1]->packet_type;
126
127         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
128         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
129         x2 = ipv4_hdr->dst_addr;
130         ipv4_flag[0] &= pkt[2]->packet_type;
131
132         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
133         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
134         x3 = ipv4_hdr->dst_addr;
135         ipv4_flag[0] &= pkt[3]->packet_type;
136
137         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
138 }
139
140 /*
141  * Lookup into LPM for destination port.
142  * If lookup fails, use incoming port (portid) as destination port.
143  */
144 static inline void
145 processx4_step2(const struct lcore_conf *qconf,
146                 __m128i dip,
147                 uint32_t ipv4_flag,
148                 uint8_t portid,
149                 struct rte_mbuf *pkt[FWDSTEP],
150                 uint16_t dprt[FWDSTEP])
151 {
152         rte_xmm_t dst;
153         const  __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
154                                                 4, 5, 6, 7, 0, 1, 2, 3);
155
156         /* Byte swap 4 IPV4 addresses. */
157         dip = _mm_shuffle_epi8(dip, bswap_mask);
158
159         /* if all 4 packets are IPV4. */
160         if (likely(ipv4_flag)) {
161                 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dst.u32,
162                         portid);
163                 /* get rid of unused upper 16 bit for each dport. */
164                 dst.x = _mm_packs_epi32(dst.x, dst.x);
165                 *(uint64_t *)dprt = dst.u64[0];
166         } else {
167                 dst.x = dip;
168                 dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0], dst.u32[0], portid);
169                 dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1], dst.u32[1], portid);
170                 dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2], dst.u32[2], portid);
171                 dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3], dst.u32[3], portid);
172         }
173 }
174
175 /*
176  * Buffer optimized handling of packets, invoked
177  * from main_loop.
178  */
179 static inline void
180 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
181                         uint8_t portid, struct lcore_conf *qconf)
182 {
183         int32_t j;
184         uint16_t dst_port[MAX_PKT_BURST];
185         __m128i dip[MAX_PKT_BURST / FWDSTEP];
186         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
187         const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
188
189         for (j = 0; j != k; j += FWDSTEP)
190                 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
191                                 &ipv4_flag[j / FWDSTEP]);
192
193         for (j = 0; j != k; j += FWDSTEP)
194                 processx4_step2(qconf, dip[j / FWDSTEP],
195                                 ipv4_flag[j / FWDSTEP], portid, &pkts_burst[j], &dst_port[j]);
196
197         /* Classify last up to 3 packets one by one */
198         switch (nb_rx % FWDSTEP) {
199         case 3:
200                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
201                 j++;
202         case 2:
203                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
204                 j++;
205         case 1:
206                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
207                 j++;
208         }
209
210         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
211 }
212
213 #endif /* __L3FWD_LPM_SSE_H__ */