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