New upstream version 17.11-rc3
[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 /*
40  * Read packet_type and destination IPV4 addresses from 4 mbufs.
41  */
42 static inline void
43 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
44                 __m128i *dip,
45                 uint32_t *ipv4_flag)
46 {
47         struct ipv4_hdr *ipv4_hdr;
48         struct ether_hdr *eth_hdr;
49         uint32_t x0, x1, x2, x3;
50
51         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
52         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
53         x0 = ipv4_hdr->dst_addr;
54         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
55
56         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
57         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
58         x1 = ipv4_hdr->dst_addr;
59         ipv4_flag[0] &= pkt[1]->packet_type;
60
61         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
62         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
63         x2 = ipv4_hdr->dst_addr;
64         ipv4_flag[0] &= pkt[2]->packet_type;
65
66         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
67         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
68         x3 = ipv4_hdr->dst_addr;
69         ipv4_flag[0] &= pkt[3]->packet_type;
70
71         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
72 }
73
74 /*
75  * Lookup into LPM for destination port.
76  * If lookup fails, use incoming port (portid) as destination port.
77  */
78 static inline void
79 processx4_step2(const struct lcore_conf *qconf,
80                 __m128i dip,
81                 uint32_t ipv4_flag,
82                 uint16_t portid,
83                 struct rte_mbuf *pkt[FWDSTEP],
84                 uint16_t dprt[FWDSTEP])
85 {
86         rte_xmm_t dst;
87         const  __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
88                                                 4, 5, 6, 7, 0, 1, 2, 3);
89
90         /* Byte swap 4 IPV4 addresses. */
91         dip = _mm_shuffle_epi8(dip, bswap_mask);
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                 dst.x = _mm_packs_epi32(dst.x, dst.x);
99                 *(uint64_t *)dprt = dst.u64[0];
100         } else {
101                 dst.x = dip;
102                 dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0], dst.u32[0], portid);
103                 dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1], dst.u32[1], portid);
104                 dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2], dst.u32[2], portid);
105                 dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3], dst.u32[3], portid);
106         }
107 }
108
109 /*
110  * Buffer optimized handling of packets, invoked
111  * from main_loop.
112  */
113 static inline void
114 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
115                         uint16_t portid, struct lcore_conf *qconf)
116 {
117         int32_t j;
118         uint16_t dst_port[MAX_PKT_BURST];
119         __m128i dip[MAX_PKT_BURST / FWDSTEP];
120         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
121         const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
122
123         for (j = 0; j != k; j += FWDSTEP)
124                 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
125                                 &ipv4_flag[j / FWDSTEP]);
126
127         for (j = 0; j != k; j += FWDSTEP)
128                 processx4_step2(qconf, dip[j / FWDSTEP],
129                                 ipv4_flag[j / FWDSTEP], portid, &pkts_burst[j], &dst_port[j]);
130
131         /* Classify last up to 3 packets one by one */
132         switch (nb_rx % FWDSTEP) {
133         case 3:
134                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
135                 j++;
136                 /* fall-through */
137         case 2:
138                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
139                 j++;
140                 /* fall-through */
141         case 1:
142                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
143                 j++;
144         }
145
146         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
147 }
148
149 #endif /* __L3FWD_LPM_SSE_H__ */