New upstream version 17.11-rc3
[deb_dpdk.git] / examples / l3fwd / l3fwd_lpm_altivec.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2017 IBM Corporation.
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_ALTIVEC_H__
36 #define __L3FWD_LPM_ALTIVEC_H__
37
38 #include "l3fwd_altivec.h"
39
40 /*
41  * Read packet_type and destination IPV4 addresses from 4 mbufs.
42  */
43 static inline void
44 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
45                 vector unsigned int *dip,
46                 uint32_t *ipv4_flag)
47 {
48         struct ipv4_hdr *ipv4_hdr;
49         struct ether_hdr *eth_hdr;
50         uint32_t x0, x1, x2, x3;
51
52         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
53         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
54         x0 = ipv4_hdr->dst_addr;
55         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
56
57         rte_compiler_barrier();
58         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
59         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
60         x1 = ipv4_hdr->dst_addr;
61         ipv4_flag[0] &= pkt[1]->packet_type;
62
63         rte_compiler_barrier();
64         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
65         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
66         x2 = ipv4_hdr->dst_addr;
67         ipv4_flag[0] &= pkt[2]->packet_type;
68
69         rte_compiler_barrier();
70         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
71         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
72         x3 = ipv4_hdr->dst_addr;
73         ipv4_flag[0] &= pkt[3]->packet_type;
74
75         rte_compiler_barrier();
76         dip[0] = (vector unsigned int){x0, x1, x2, x3};
77 }
78
79 /*
80  * Lookup into LPM for destination port.
81  * If lookup fails, use incoming port (portid) as destination port.
82  */
83 static inline void
84 processx4_step2(const struct lcore_conf *qconf,
85                 vector unsigned int dip,
86                 uint32_t ipv4_flag,
87                 uint8_t portid,
88                 struct rte_mbuf *pkt[FWDSTEP],
89                 uint16_t dprt[FWDSTEP])
90 {
91         rte_xmm_t dst;
92         const vector unsigned char bswap_mask = (vector unsigned char){
93                                                         3, 2, 1, 0,
94                                                         7, 6, 5, 4,
95                                                         11, 10, 9, 8,
96                                                         15, 14, 13, 12};
97
98         /* Byte swap 4 IPV4 addresses. */
99         dip = (vector unsigned int)vec_perm(*(vector unsigned char *)&dip,
100                                         (vector unsigned char){}, bswap_mask);
101
102         /* if all 4 packets are IPV4. */
103         if (likely(ipv4_flag)) {
104                 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, (xmm_t)dip,
105                         (uint32_t *)&dst, portid);
106                 /* get rid of unused upper 16 bit for each dport. */
107                 dst.x = (xmm_t)vec_packs(dst.x, dst.x);
108                 *(uint64_t *)dprt = dst.u64[0];
109         } else {
110                 dst.x = (xmm_t)dip;
111                 dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0],
112                                                         dst.u32[0], portid);
113                 dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1],
114                                                         dst.u32[1], portid);
115                 dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2],
116                                                         dst.u32[2], portid);
117                 dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3],
118                                                         dst.u32[3], portid);
119         }
120 }
121
122 /*
123  * Buffer optimized handling of packets, invoked
124  * from main_loop.
125  */
126 static inline void
127 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
128                         uint8_t portid, struct lcore_conf *qconf)
129 {
130         int32_t j;
131         uint16_t dst_port[MAX_PKT_BURST];
132         vector unsigned int dip[MAX_PKT_BURST / FWDSTEP];
133         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
134         const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
135
136         for (j = 0; j != k; j += FWDSTEP)
137                 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
138                                 &ipv4_flag[j / FWDSTEP]);
139
140         for (j = 0; j != k; j += FWDSTEP)
141                 processx4_step2(qconf, dip[j / FWDSTEP],
142                                 ipv4_flag[j / FWDSTEP],
143                                 portid, &pkts_burst[j], &dst_port[j]);
144
145         /* Classify last up to 3 packets one by one */
146         switch (nb_rx % FWDSTEP) {
147         case 3:
148                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
149                 j++;
150                 /* fall-through */
151         case 2:
152                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
153                 j++;
154                 /* fall-through */
155         case 1:
156                 dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
157                 j++;
158                 /* fall-through */
159         }
160
161         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
162 }
163
164 #endif /* __L3FWD_LPM_ALTIVEC_H__ */