4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef __L3FWD_LPM_SSE_H__
35 #define __L3FWD_LPM_SSE_H__
37 #include "l3fwd_sse.h"
39 static inline __attribute__((always_inline)) uint16_t
40 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
44 struct ipv6_hdr *ipv6_hdr;
45 struct ipv4_hdr *ipv4_hdr;
46 struct ether_hdr *eth_hdr;
48 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
50 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
51 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
54 (rte_lpm_lookup(qconf->ipv4_lookup_struct,
55 rte_be_to_cpu_32(ipv4_hdr->dst_addr),
59 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
61 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
62 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
64 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
65 ipv6_hdr->dst_addr, &next_hop) == 0)
74 * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
75 * precalculated. If packet is ipv6 dst_addr is taken directly from packet
76 * header and dst_ipv4 value is not used.
78 static inline __attribute__((always_inline)) uint16_t
79 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
80 uint32_t dst_ipv4, uint8_t portid)
83 struct ipv6_hdr *ipv6_hdr;
84 struct ether_hdr *eth_hdr;
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) == 0) ? next_hop : portid);
90 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
92 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
93 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
95 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
96 ipv6_hdr->dst_addr, &next_hop) == 0)
106 * Read packet_type and destination IPV4 addresses from 4 mbufs.
109 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
113 struct ipv4_hdr *ipv4_hdr;
114 struct ether_hdr *eth_hdr;
115 uint32_t x0, x1, x2, x3;
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;
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;
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;
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;
137 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
141 * Lookup into LPM for destination port.
142 * If lookup fails, use incoming port (portid) as destination port.
145 processx4_step2(const struct lcore_conf *qconf,
149 struct rte_mbuf *pkt[FWDSTEP],
150 uint16_t dprt[FWDSTEP])
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);
156 /* Byte swap 4 IPV4 addresses. */
157 dip = _mm_shuffle_epi8(dip, bswap_mask);
159 /* if all 4 packets are IPV4. */
160 if (likely(ipv4_flag)) {
161 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dst.u32,
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];
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);
176 * Buffer optimized handling of packets, invoked
180 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
181 uint8_t portid, struct lcore_conf *qconf)
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);
189 for (j = 0; j != k; j += FWDSTEP)
190 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
191 &ipv4_flag[j / FWDSTEP]);
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]);
197 /* Classify last up to 3 packets one by one */
198 switch (nb_rx % FWDSTEP) {
200 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
203 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
206 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
210 send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
213 #endif /* __L3FWD_LPM_SSE_H__ */