DPDK  24.07.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 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 
24 #include <rte_byteorder.h>
25 #include <rte_ip.h>
26 #include <rte_common.h>
27 #include <rte_thash_gfni.h>
28 
29 #if defined(RTE_ARCH_X86) || defined(__ARM_NEON)
30 #include <rte_vect.h>
31 #endif
32 
33 #ifdef RTE_ARCH_X86
34 /* Byte swap mask used for converting IPv6 address
35  * 4-byte chunks to CPU byte order
36  */
37 static const __m128i rte_thash_ipv6_bswap_mask = {
38  0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
39 #endif
40 
45 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
46  sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
47 
53 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
54 
59 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
60  sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
61 
67 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
68 
74  uint32_t src_addr;
75  uint32_t dst_addr;
76  union {
77  struct {
78  uint16_t dport;
79  uint16_t sport;
80  };
81  uint32_t sctp_tag;
82  };
83 };
84 
91  uint8_t src_addr[16];
92  uint8_t dst_addr[16];
93  union {
94  struct {
95  uint16_t dport;
96  uint16_t sport;
97  };
98  uint32_t sctp_tag;
99  };
100 };
101 
102 #ifdef RTE_ARCH_X86
103 union __rte_aligned(XMM_SIZE) rte_thash_tuple {
104 #else
105 union rte_thash_tuple {
106 #endif
107  struct rte_ipv4_tuple v4;
108  struct rte_ipv6_tuple v6;
109 };
110 
120 static inline void
121 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
122 {
123  int i;
124 
125  for (i = 0; i < (len >> 2); i++)
126  targ[i] = rte_be_to_cpu_32(orig[i]);
127 }
128 
137 static inline void
139  union rte_thash_tuple *targ)
140 {
141 #ifdef RTE_ARCH_X86
142  __m128i ipv6 = _mm_loadu_si128((const __m128i *)orig->src_addr);
143  *(__m128i *)targ->v6.src_addr =
144  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
145  ipv6 = _mm_loadu_si128((const __m128i *)orig->dst_addr);
146  *(__m128i *)targ->v6.dst_addr =
147  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
148 #elif defined(__ARM_NEON)
149  uint8x16_t ipv6 = vld1q_u8((uint8_t const *)orig->src_addr);
150  vst1q_u8((uint8_t *)targ->v6.src_addr, vrev32q_u8(ipv6));
151  ipv6 = vld1q_u8((uint8_t const *)orig->dst_addr);
152  vst1q_u8((uint8_t *)targ->v6.dst_addr, vrev32q_u8(ipv6));
153 #else
154  int i;
155  for (i = 0; i < 4; i++) {
156  *((uint32_t *)targ->v6.src_addr + i) =
157  rte_be_to_cpu_32(*((const uint32_t *)orig->src_addr + i));
158  *((uint32_t *)targ->v6.dst_addr + i) =
159  rte_be_to_cpu_32(*((const uint32_t *)orig->dst_addr + i));
160  }
161 #endif
162 }
163 
175 static inline uint32_t
176 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
177  const uint8_t *rss_key)
178 {
179  uint32_t i, j, map, ret = 0;
180 
181  for (j = 0; j < input_len; j++) {
182  for (map = input_tuple[j]; map; map &= (map - 1)) {
183  i = rte_bsf32(map);
184  ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
185  (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
186  (i + 1));
187  }
188  }
189  return ret;
190 }
191 
205 static inline uint32_t
206 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
207  const uint8_t *rss_key)
208 {
209  uint32_t i, j, map, ret = 0;
210 
211  for (j = 0; j < input_len; j++) {
212  for (map = input_tuple[j]; map; map &= (map - 1)) {
213  i = rte_bsf32(map);
214  ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
215  (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
216  }
217  }
218  return ret;
219 }
220 
228 int
230 
243 void
244 rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key,
245  int size);
246 
248 #define RTE_THASH_RETA_SZ_MIN 2U
249 
250 #define RTE_THASH_RETA_SZ_MAX 16U
251 
256 #define RTE_THASH_IGNORE_PERIOD_OVERFLOW 0x1
257 
261 #define RTE_THASH_MINIMAL_SEQ 0x2
262 
264 struct rte_thash_ctx;
266 struct rte_thash_subtuple_helper;
267 
290 struct rte_thash_ctx *
291 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
292  uint8_t *key, uint32_t flags);
293 
304 struct rte_thash_ctx *
305 rte_thash_find_existing(const char *name);
306 
313 void
314 rte_thash_free_ctx(struct rte_thash_ctx *ctx);
315 
335 int
336 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
337  uint32_t offset);
338 
349 struct rte_thash_subtuple_helper *
350 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name);
351 
367 uint32_t
368 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
369  uint32_t hash, uint32_t desired_hash);
370 
381 const uint8_t *
382 rte_thash_get_key(struct rte_thash_ctx *ctx);
383 
396 const uint64_t *
397 rte_thash_get_gfni_matrices(struct rte_thash_ctx *ctx);
398 
415 typedef int (*rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple);
416 
443 int
444 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
445  struct rte_thash_subtuple_helper *h,
446  uint8_t *tuple, unsigned int tuple_len,
447  uint32_t desired_value, unsigned int attempts,
448  rte_thash_check_tuple_t fn, void *userdata);
449 
450 #ifdef __cplusplus
451 }
452 #endif
453 
454 #endif /* _RTE_THASH_H */
int rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len, uint32_t offset)
uint8_t dst_addr[16]
Definition: rte_ip.h:534
int(* rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple)
Definition: rte_thash.h:415
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:553
static void rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
Definition: rte_thash.h:121
uint8_t src_addr[16]
Definition: rte_ip.h:533
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:138
static uint32_t rte_softrss(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:176
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)
static uint32_t rte_softrss_be(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:206
const uint8_t * rte_thash_get_key(struct rte_thash_ctx *ctx)