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