77326c1ae1ebd9a5ee089cf7e3eedb867cea7099
[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         uint8_t next_hop;
44         struct ipv6_hdr *ipv6_hdr;
45         struct ipv4_hdr *ipv4_hdr;
46         struct ether_hdr *eth_hdr;
47
48         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
49
50                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
51                 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
52
53                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
54                                 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0) ?
55                                                 next_hop : portid);
56
57         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
58
59                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
60                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
61
62                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
63                                 ipv6_hdr->dst_addr, &next_hop) == 0) ? next_hop : portid);
64
65         }
66
67         return portid;
68 }
69
70 /*
71  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
72  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
73  * header and dst_ipv4 value is not used.
74  */
75 static inline __attribute__((always_inline)) uint16_t
76 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
77         uint32_t dst_ipv4, uint8_t portid)
78 {
79         uint8_t next_hop;
80         struct ipv6_hdr *ipv6_hdr;
81         struct ether_hdr *eth_hdr;
82
83         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
84                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
85                         &next_hop) == 0) ? next_hop : portid);
86
87         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
88
89                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
90                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
91
92                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
93                                 ipv6_hdr->dst_addr, &next_hop) == 0) ? next_hop : portid);
94
95         }
96
97         return portid;
98
99 }
100
101 /*
102  * Read packet_type and destination IPV4 addresses from 4 mbufs.
103  */
104 static inline void
105 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
106                 __m128i *dip,
107                 uint32_t *ipv4_flag)
108 {
109         struct ipv4_hdr *ipv4_hdr;
110         struct ether_hdr *eth_hdr;
111         uint32_t x0, x1, x2, x3;
112
113         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
114         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
115         x0 = ipv4_hdr->dst_addr;
116         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
117
118         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
119         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
120         x1 = ipv4_hdr->dst_addr;
121         ipv4_flag[0] &= pkt[1]->packet_type;
122
123         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
124         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
125         x2 = ipv4_hdr->dst_addr;
126         ipv4_flag[0] &= pkt[2]->packet_type;
127
128         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
129         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
130         x3 = ipv4_hdr->dst_addr;
131         ipv4_flag[0] &= pkt[3]->packet_type;
132
133         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
134 }
135
136 /*
137  * Lookup into LPM for destination port.
138  * If lookup fails, use incoming port (portid) as destination port.
139  */
140 static inline void
141 processx4_step2(const struct lcore_conf *qconf,
142                 __m128i dip,
143                 uint32_t ipv4_flag,
144                 uint8_t portid,
145                 struct rte_mbuf *pkt[FWDSTEP],
146                 uint16_t dprt[FWDSTEP])
147 {
148         rte_xmm_t dst;
149         const  __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
150                                                 4, 5, 6, 7, 0, 1, 2, 3);
151
152         /* Byte swap 4 IPV4 addresses. */
153         dip = _mm_shuffle_epi8(dip, bswap_mask);
154
155         /* if all 4 packets are IPV4. */
156         if (likely(ipv4_flag)) {
157                 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
158         } else {
159                 dst.x = dip;
160                 dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0], dst.u32[0], portid);
161                 dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1], dst.u32[1], portid);
162                 dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2], dst.u32[2], portid);
163                 dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3], dst.u32[3], portid);
164         }
165 }
166
167 /*
168  * Buffer optimized handling of packets, invoked
169  * from main_loop.
170  */
171 static inline void
172 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
173                         uint8_t portid, struct lcore_conf *qconf)
174 {
175         int32_t j;
176         uint16_t dst_port[MAX_PKT_BURST];
177         __m128i dip[MAX_PKT_BURST / FWDSTEP];
178         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
179         const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
180
181         for (j = 0; j != k; j += FWDSTEP)
182                 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
183                                 &ipv4_flag[j / FWDSTEP]);
184
185         for (j = 0; j != k; j += FWDSTEP)
186                 processx4_step2(qconf, dip[j / FWDSTEP],
187                                 ipv4_flag[j / FWDSTEP], portid, &pkts_burst[j], &dst_port[j]);
188
189         /* Classify last up to 3 packets one by one */
190         switch (nb_rx % FWDSTEP) {
191         case 3:
192                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
193                 j++;
194         case 2:
195                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
196                 j++;
197         case 1:
198                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
199                 j++;
200         }
201
202         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
203 }
204
205 #endif /* __L3FWD_LPM_SSE_H__ */