DPDK  25.03.0
rte_thash.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2019 Vladimir Medvedkin <medvedkinv@gmail.com>
3  * Copyright(c) 2021 Intel Corporation
4  */
5 
6 #ifndef _RTE_THASH_H
7 #define _RTE_THASH_H
8 
18 #include <stdint.h>
19 
20 #include <rte_byteorder.h>
21 #include <rte_ip.h>
22 #include <rte_common.h>
23 #include <rte_thash_gfni.h>
24 
25 #if defined(RTE_ARCH_X86) || defined(__ARM_NEON)
26 #include <rte_vect.h>
27 #endif
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
37 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
38  sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
39 
45 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
46 
51 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
52  sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
53 
59 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
60 
66  uint32_t src_addr;
67  uint32_t dst_addr;
68  union {
69  struct {
70  uint16_t dport;
71  uint16_t sport;
72  };
73  uint32_t sctp_tag;
74  };
75 };
76 
83  struct rte_ipv6_addr src_addr;
84  struct rte_ipv6_addr dst_addr;
85  union {
86  struct {
87  uint16_t dport;
88  uint16_t sport;
89  };
90  uint32_t sctp_tag;
91  };
92 };
93 
94 #ifdef RTE_ARCH_X86
95 union __rte_aligned(XMM_SIZE) rte_thash_tuple {
96 #else
97 union rte_thash_tuple {
98 #endif
99  struct rte_ipv4_tuple v4;
100  struct rte_ipv6_tuple v6;
101 };
102 
112 __rte_internal
113 uint32_t
114 thash_get_rand_poly(uint32_t poly_degree);
115 
119 #define RTE_THASH_KEY_LEN_MAX 52
120 
121 #define RTE_THASH_TUPLE_LEN_MAX (RTE_THASH_KEY_LEN_MAX - sizeof(uint32_t))
122 
132 static inline void
133 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
134 {
135  int i;
136 
137  for (i = 0; i < (len >> 2); i++)
138  targ[i] = rte_be_to_cpu_32(orig[i]);
139 }
140 
149 static inline void
150 rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig,
151  union rte_thash_tuple *targ)
152 {
153 #ifdef RTE_ARCH_X86
154  /* Byte swap mask used for converting IPv6 address
155  * 4-byte chunks to CPU byte order
156  */
157  const __m128i rte_thash_ipv6_bswap_mask = _mm_set_epi64x(
158  0x0C0D0E0F08090A0BULL, 0x0405060700010203ULL);
159  __m128i ipv6 = _mm_loadu_si128((const __m128i *)&orig->src_addr);
160  *(__m128i *)&targ->v6.src_addr =
161  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
162  ipv6 = _mm_loadu_si128((const __m128i *)&orig->dst_addr);
163  *(__m128i *)&targ->v6.dst_addr =
164  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
165 #elif defined(__ARM_NEON)
166  uint8x16_t ipv6 = vld1q_u8(orig->src_addr.a);
167  vst1q_u8(targ->v6.src_addr.a, vrev32q_u8(ipv6));
168  ipv6 = vld1q_u8(orig->dst_addr.a);
169  vst1q_u8(targ->v6.dst_addr.a, vrev32q_u8(ipv6));
170 #else
171  int i;
172  for (i = 0; i < 4; i++) {
173  *((uint32_t *)&targ->v6.src_addr + i) =
174  rte_be_to_cpu_32(*((const uint32_t *)&orig->src_addr + i));
175  *((uint32_t *)&targ->v6.dst_addr + i) =
176  rte_be_to_cpu_32(*((const uint32_t *)&orig->dst_addr + i));
177  }
178 #endif
179 }
180 
192 static inline uint32_t
193 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
194  const uint8_t *rss_key)
195 {
196  uint32_t i, j, map, ret = 0;
197 
198  for (j = 0; j < input_len; j++) {
199  for (map = input_tuple[j]; map; map &= (map - 1)) {
200  i = rte_bsf32(map);
201  ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
202  (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
203  (i + 1));
204  }
205  }
206  return ret;
207 }
208 
222 static inline uint32_t
223 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
224  const uint8_t *rss_key)
225 {
226  uint32_t i, j, map, ret = 0;
227 
228  for (j = 0; j < input_len; j++) {
229  for (map = input_tuple[j]; map; map &= (map - 1)) {
230  i = rte_bsf32(map);
231  ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
232  (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
233  }
234  }
235  return ret;
236 }
237 
245 int
247 
260 void
261 rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key,
262  int size);
263 
265 #define RTE_THASH_RETA_SZ_MIN 2U
266 
267 #define RTE_THASH_RETA_SZ_MAX 16U
268 
273 #define RTE_THASH_IGNORE_PERIOD_OVERFLOW 0x1
274 
278 #define RTE_THASH_MINIMAL_SEQ 0x2
279 
281 struct rte_thash_ctx;
283 struct rte_thash_subtuple_helper;
284 
307 struct rte_thash_ctx *
308 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
309  uint8_t *key, uint32_t flags);
310 
321 struct rte_thash_ctx *
322 rte_thash_find_existing(const char *name);
323 
330 void
331 rte_thash_free_ctx(struct rte_thash_ctx *ctx);
332 
352 int
353 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
354  uint32_t offset);
355 
366 struct rte_thash_subtuple_helper *
367 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name);
368 
384 uint32_t
385 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
386  uint32_t hash, uint32_t desired_hash);
387 
398 const uint8_t *
399 rte_thash_get_key(struct rte_thash_ctx *ctx);
400 
413 const uint64_t *
414 rte_thash_get_gfni_matrices(struct rte_thash_ctx *ctx);
415 
432 typedef int (*rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple);
433 
460 int
461 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
462  struct rte_thash_subtuple_helper *h,
463  uint8_t *tuple, unsigned int tuple_len,
464  uint32_t desired_value, unsigned int attempts,
465  rte_thash_check_tuple_t fn, void *userdata);
466 
491 __rte_experimental
492 int
493 rte_thash_gen_key(uint8_t *key, size_t key_len, size_t reta_sz_log,
494  uint32_t entropy_start, size_t entropy_sz);
495 
496 #ifdef __cplusplus
497 }
498 #endif
499 
500 #endif /* _RTE_THASH_H */
int rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len, uint32_t offset)
int(* rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple)
Definition: rte_thash.h:432
int rte_thash_gfni_supported(void)
const uint64_t * rte_thash_get_gfni_matrices(struct rte_thash_ctx *ctx)
static rte_be32_t rte_cpu_to_be_32(uint32_t x)
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_bitops.h:1192
static void rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
Definition: rte_thash.h:133
uint32_t rte_thash_get_complement(struct rte_thash_subtuple_helper *h, uint32_t hash, uint32_t desired_hash)
static void rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig, union rte_thash_tuple *targ)
Definition: rte_thash.h:150
static uint32_t rte_softrss(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:193
void rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key, int size)
void rte_thash_free_ctx(struct rte_thash_ctx *ctx)
struct rte_thash_subtuple_helper * rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)
int rte_thash_adjust_tuple(struct rte_thash_ctx *ctx, struct rte_thash_subtuple_helper *h, uint8_t *tuple, unsigned int tuple_len, uint32_t desired_value, unsigned int attempts, rte_thash_check_tuple_t fn, void *userdata)
struct rte_thash_ctx * rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz, uint8_t *key, uint32_t flags)
struct rte_thash_ctx * rte_thash_find_existing(const char *name)
static uint32_t rte_be_to_cpu_32(rte_be32_t x)
__rte_experimental int rte_thash_gen_key(uint8_t *key, size_t key_len, size_t reta_sz_log, uint32_t entropy_start, size_t entropy_sz)
static uint32_t rte_softrss_be(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:223
const uint8_t * rte_thash_get_key(struct rte_thash_ctx *ctx)