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