1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2019 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2021 Intel Corporation
12 * toeplitz hash functions.
20 * Software implementation of the Toeplitz hash function used by RSS.
21 * Can be used either for packet distribution on single queue NIC
22 * or for simulating of RSS computation on specific NIC (for example
23 * after GRE header decapsulating)
27 #include <rte_byteorder.h>
28 #include <rte_config.h>
30 #include <rte_common.h>
32 #if defined(RTE_ARCH_X86) || defined(__ARM_NEON)
37 /* Byte swap mask used for converting IPv6 address
38 * 4-byte chunks to CPU byte order
40 static const __m128i rte_thash_ipv6_bswap_mask = {
41 0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
45 * length in dwords of input tuple to
46 * calculate hash of ipv4 header only
48 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
49 sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
52 * length in dwords of input tuple to
53 * calculate hash of ipv4 header +
56 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
59 * length in dwords of input tuple to
60 * calculate hash of ipv6 header only
62 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
63 sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
66 * length in dwords of input tuple to
67 * calculate hash of ipv6 header +
70 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
74 * addresses and ports/sctp_tag have to be CPU byte order
76 struct rte_ipv4_tuple {
91 * Addresses have to be filled by rte_thash_load_v6_addr()
92 * ports/sctp_tag have to be CPU byte order
94 struct rte_ipv6_tuple {
107 union rte_thash_tuple {
108 struct rte_ipv4_tuple v4;
109 struct rte_ipv6_tuple v6;
111 } __rte_aligned(XMM_SIZE);
117 * Prepare special converted key to use with rte_softrss_be()
119 * pointer to original RSS key
121 * pointer to target RSS key
126 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
130 for (i = 0; i < (len >> 2); i++)
131 targ[i] = rte_be_to_cpu_32(orig[i]);
135 * Prepare and load IPv6 addresses (src and dst)
138 * Pointer to ipv6 header of the original packet
140 * Pointer to rte_ipv6_tuple structure
143 rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig,
144 union rte_thash_tuple *targ)
147 __m128i ipv6 = _mm_loadu_si128((const __m128i *)orig->src_addr);
148 *(__m128i *)targ->v6.src_addr =
149 _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
150 ipv6 = _mm_loadu_si128((const __m128i *)orig->dst_addr);
151 *(__m128i *)targ->v6.dst_addr =
152 _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
153 #elif defined(__ARM_NEON)
154 uint8x16_t ipv6 = vld1q_u8((uint8_t const *)orig->src_addr);
155 vst1q_u8((uint8_t *)targ->v6.src_addr, vrev32q_u8(ipv6));
156 ipv6 = vld1q_u8((uint8_t const *)orig->dst_addr);
157 vst1q_u8((uint8_t *)targ->v6.dst_addr, vrev32q_u8(ipv6));
160 for (i = 0; i < 4; i++) {
161 *((uint32_t *)targ->v6.src_addr + i) =
162 rte_be_to_cpu_32(*((const uint32_t *)orig->src_addr + i));
163 *((uint32_t *)targ->v6.dst_addr + i) =
164 rte_be_to_cpu_32(*((const uint32_t *)orig->dst_addr + i));
170 * Generic implementation. Can be used with original rss_key
172 * Pointer to input tuple
174 * Length of input_tuple in 4-bytes chunks
176 * Pointer to RSS hash key.
178 * Calculated hash value.
180 static inline uint32_t
181 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
182 const uint8_t *rss_key)
184 uint32_t i, j, map, ret = 0;
186 for (j = 0; j < input_len; j++) {
187 for (map = input_tuple[j]; map; map &= (map - 1)) {
189 ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
190 (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
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.
202 * Pointer to input tuple
204 * Length of input_tuple in 4-bytes chunks
206 * Pointer to RSS hash key.
208 * Calculated hash value.
210 static inline uint32_t
211 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
212 const uint8_t *rss_key)
214 uint32_t i, j, map, ret = 0;
216 for (j = 0; j < input_len; j++) {
217 for (map = input_tuple[j]; map; map &= (map - 1)) {
219 ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
220 (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
226 /** @internal Logarithm of minimum size of the RSS ReTa */
227 #define RTE_THASH_RETA_SZ_MIN 2U
228 /** @internal Logarithm of maximum size of the RSS ReTa */
229 #define RTE_THASH_RETA_SZ_MAX 16U
232 * LFSR will ignore if generated m-sequence has more than 2^n -1 bits,
233 * where n is the logarithm of the RSS ReTa size.
235 #define RTE_THASH_IGNORE_PERIOD_OVERFLOW 0x1
237 * Generate minimal required bit (equal to ReTa LSB) sequence into
240 #define RTE_THASH_MINIMAL_SEQ 0x2
242 /** @internal thash context structure. */
243 struct rte_thash_ctx;
244 /** @internal thash helper structure. */
245 struct rte_thash_subtuple_helper;
248 * Create a new thash context.
251 * @b EXPERIMENTAL: this API may change without prior notice.
256 * Length of the toeplitz hash key
258 * Logarithm of the NIC's Redirection Table (ReTa) size,
259 * i.e. number of the LSBs if the hash used to determine
262 * Pointer to the key used to init an internal key state.
263 * Could be NULL, in this case internal key will be inited with random.
265 * Supported flags are:
266 * RTE_THASH_IGNORE_PERIOD_OVERFLOW
267 * RTE_THASH_MINIMAL_SEQ
269 * A pointer to the created context on success
273 struct rte_thash_ctx *
274 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
275 uint8_t *key, uint32_t flags);
278 * Find an existing thash context and return a pointer to it.
281 * @b EXPERIMENTAL: this API may change without prior notice.
284 * Name of the thash context
286 * Pointer to the thash context or NULL if it was not found with rte_errno
287 * set appropriately. Possible rte_errno values include:
288 * - ENOENT - required entry not available to return.
291 struct rte_thash_ctx *
292 rte_thash_find_existing(const char *name);
295 * Free a thash context object
298 * @b EXPERIMENTAL: this API may change without prior notice.
307 rte_thash_free_ctx(struct rte_thash_ctx *ctx);
310 * Add a special properties to the toeplitz hash key inside a thash context.
311 * Creates an internal helper struct which has a complementary table
312 * to calculate toeplitz hash collisions.
313 * This function is not multi-thread safe.
316 * @b EXPERIMENTAL: this API may change without prior notice.
323 * Length in bits of the target subtuple
324 * Must be no shorter than reta_sz passed on rte_thash_init_ctx().
326 * Offset in bits of the subtuple
333 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
337 * Find a helper in the context by the given name
340 * @b EXPERIMENTAL: this API may change without prior notice.
347 * Pointer to the thash helper or NULL if it was not found.
350 struct rte_thash_subtuple_helper *
351 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name);
354 * Get a complementary value for the subtuple to produce a
355 * partial toeplitz hash collision. It must be XOR'ed with the
356 * subtuple to produce the hash value with the desired hash LSB's
357 * This function is multi-thread safe.
360 * Pointer to the helper struct
362 * Toeplitz hash value calculated for the given tuple
363 * @param desired_hash
364 * Desired hash value to find a collision for
366 * A complementary value which must be xored with the corresponding subtuple
370 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
371 uint32_t hash, uint32_t desired_hash);
374 * Get a pointer to the toeplitz hash contained in the context.
375 * It changes after each addition of a helper. It should be installed to
379 * @b EXPERIMENTAL: this API may change without prior notice.
384 * A pointer to the toeplitz hash key
388 rte_thash_get_key(struct rte_thash_ctx *ctx);
391 * Function prototype for the rte_thash_adjust_tuple
392 * to check if adjusted tuple could be used.
393 * Generally it is some kind of lookup function to check
394 * if adjusted tuple is already in use.
397 * @b EXPERIMENTAL: this API may change without prior notice.
400 * Pointer to the userdata. It could be a pointer to the
401 * table with used tuples to search.
403 * Pointer to the tuple to check
409 typedef int (*rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple);
412 * Adjusts tuple in the way to make Toeplitz hash has
413 * desired least significant bits.
414 * This function is multi-thread safe.
417 * @b EXPERIMENTAL: this API may change without prior notice.
422 * Pointer to the helper struct
424 * Pointer to the tuple to be adjusted
426 * Length of the tuple. Must be multiple of 4.
427 * @param desired_value
428 * Desired value of least significant bits of the hash
430 * Number of attempts to adjust tuple with fn() calling
432 * Callback function to check adjusted tuple. Could be NULL
434 * Pointer to the userdata to be passed to fn(). Could be NULL
442 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
443 struct rte_thash_subtuple_helper *h,
444 uint8_t *tuple, unsigned int tuple_len,
445 uint32_t desired_value, unsigned int attempts,
446 rte_thash_check_tuple_t fn, void *userdata);
452 #endif /* _RTE_THASH_H */