New upstream version 17.11.1
[deb_dpdk.git] / lib / librte_hash / rte_thash.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Vladimir Medvedkin <medvedkinv@gmail.com>
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 _RTE_THASH_H
35 #define _RTE_THASH_H
36
37 /**
38  * @file
39  *
40  * toeplitz hash functions.
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /**
48  * Software implementation of the Toeplitz hash function used by RSS.
49  * Can be used either for packet distribution on single queue NIC
50  * or for simulating of RSS computation on specific NIC (for example
51  * after GRE header decapsulating)
52  */
53
54 #include <stdint.h>
55 #include <rte_byteorder.h>
56 #include <rte_config.h>
57 #include <rte_ip.h>
58 #include <rte_common.h>
59
60 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_NEON)
61 #include <rte_vect.h>
62 #endif
63
64 #ifdef RTE_ARCH_X86
65 /* Byte swap mask used for converting IPv6 address
66  * 4-byte chunks to CPU byte order
67  */
68 static const __m128i rte_thash_ipv6_bswap_mask = {
69                 0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
70 #endif
71
72 /**
73  * length in dwords of input tuple to
74  * calculate hash of ipv4 header only
75  */
76 #define RTE_THASH_V4_L3_LEN     ((sizeof(struct rte_ipv4_tuple) -       \
77                         sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
78
79 /**
80  * length in dwords of input tuple to
81  * calculate hash of ipv4 header +
82  * transport header
83  */
84 #define RTE_THASH_V4_L4_LEN      ((sizeof(struct rte_ipv4_tuple)) / 4)
85
86 /**
87  * length in dwords of input tuple to
88  * calculate hash of ipv6 header only
89  */
90 #define RTE_THASH_V6_L3_LEN     ((sizeof(struct rte_ipv6_tuple) -       \
91                         sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
92
93 /**
94  * length in dwords of input tuple to
95  * calculate hash of ipv6 header +
96  * transport header
97  */
98 #define RTE_THASH_V6_L4_LEN     ((sizeof(struct rte_ipv6_tuple)) / 4)
99
100 /**
101  * IPv4 tuple
102  * addresses and ports/sctp_tag have to be CPU byte order
103  */
104 struct rte_ipv4_tuple {
105         uint32_t        src_addr;
106         uint32_t        dst_addr;
107         RTE_STD_C11
108         union {
109                 struct {
110                         uint16_t dport;
111                         uint16_t sport;
112                 };
113                 uint32_t        sctp_tag;
114         };
115 };
116
117 /**
118  * IPv6 tuple
119  * Addresses have to be filled by rte_thash_load_v6_addr()
120  * ports/sctp_tag have to be CPU byte order
121  */
122 struct rte_ipv6_tuple {
123         uint8_t         src_addr[16];
124         uint8_t         dst_addr[16];
125         RTE_STD_C11
126         union {
127                 struct {
128                         uint16_t dport;
129                         uint16_t sport;
130                 };
131                 uint32_t        sctp_tag;
132         };
133 };
134
135 union rte_thash_tuple {
136         struct rte_ipv4_tuple   v4;
137         struct rte_ipv6_tuple   v6;
138 #ifdef RTE_ARCH_X86
139 } __attribute__((aligned(XMM_SIZE)));
140 #else
141 };
142 #endif
143
144 /**
145  * Prepare special converted key to use with rte_softrss_be()
146  * @param orig
147  *   pointer to original RSS key
148  * @param targ
149  *   pointer to target RSS key
150  * @param len
151  *   RSS key length
152  */
153 static inline void
154 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
155 {
156         int i;
157
158         for (i = 0; i < (len >> 2); i++)
159                 targ[i] = rte_be_to_cpu_32(orig[i]);
160 }
161
162 /**
163  * Prepare and load IPv6 addresses (src and dst)
164  * into target tuple
165  * @param orig
166  *   Pointer to ipv6 header of the original packet
167  * @param targ
168  *   Pointer to rte_ipv6_tuple structure
169  */
170 static inline void
171 rte_thash_load_v6_addrs(const struct ipv6_hdr *orig, union rte_thash_tuple *targ)
172 {
173 #ifdef RTE_ARCH_X86
174         __m128i ipv6 = _mm_loadu_si128((const __m128i *)orig->src_addr);
175         *(__m128i *)targ->v6.src_addr =
176                         _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
177         ipv6 = _mm_loadu_si128((const __m128i *)orig->dst_addr);
178         *(__m128i *)targ->v6.dst_addr =
179                         _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
180 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
181         uint8x16_t ipv6 = vld1q_u8((uint8_t const *)orig->src_addr);
182         vst1q_u8((uint8_t *)targ->v6.src_addr, vrev32q_u8(ipv6));
183         ipv6 = vld1q_u8((uint8_t const *)orig->dst_addr);
184         vst1q_u8((uint8_t *)targ->v6.dst_addr, vrev32q_u8(ipv6));
185 #else
186         int i;
187         for (i = 0; i < 4; i++) {
188                 *((uint32_t *)targ->v6.src_addr + i) =
189                         rte_be_to_cpu_32(*((const uint32_t *)orig->src_addr + i));
190                 *((uint32_t *)targ->v6.dst_addr + i) =
191                         rte_be_to_cpu_32(*((const uint32_t *)orig->dst_addr + i));
192         }
193 #endif
194 }
195
196 /**
197  * Generic implementation. Can be used with original rss_key
198  * @param input_tuple
199  *   Pointer to input tuple
200  * @param input_len
201  *   Length of input_tuple in 4-bytes chunks
202  * @param rss_key
203  *   Pointer to RSS hash key.
204  * @return
205  *   Calculated hash value.
206  */
207 static inline uint32_t
208 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
209                 const uint8_t *rss_key)
210 {
211         uint32_t i, j, map, ret = 0;
212
213         for (j = 0; j < input_len; j++) {
214                 for (map = input_tuple[j]; map; map &= (map - 1)) {
215                         i = rte_bsf32(map);
216                         ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
217                                         (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
218                                         (i + 1));
219                 }
220         }
221         return ret;
222 }
223
224 /**
225  * Optimized implementation.
226  * If you want the calculated hash value matches NIC RSS value
227  * you have to use special converted key with rte_convert_rss_key() fn.
228  * @param input_tuple
229  *   Pointer to input tuple
230  * @param input_len
231  *   Length of input_tuple in 4-bytes chunks
232  * @param *rss_key
233  *   Pointer to RSS hash key.
234  * @return
235  *   Calculated hash value.
236  */
237 static inline uint32_t
238 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
239                 const uint8_t *rss_key)
240 {
241         uint32_t i, j, map, ret = 0;
242
243         for (j = 0; j < input_len; j++) {
244                 for (map = input_tuple[j]; map; map &= (map - 1)) {
245                         i = rte_bsf32(map);
246                         ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
247                                 (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
248                 }
249         }
250         return ret;
251 }
252
253 #ifdef __cplusplus
254 }
255 #endif
256
257 #endif /* _RTE_THASH_H */