DPDK  21.08.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 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
26 #include <stdint.h>
27 #include <rte_byteorder.h>
28 #include <rte_config.h>
29 #include <rte_ip.h>
30 #include <rte_common.h>
31 
32 #if defined(RTE_ARCH_X86) || defined(__ARM_NEON)
33 #include <rte_vect.h>
34 #endif
35 
36 #ifdef RTE_ARCH_X86
37 /* Byte swap mask used for converting IPv6 address
38  * 4-byte chunks to CPU byte order
39  */
40 static const __m128i rte_thash_ipv6_bswap_mask = {
41  0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
42 #endif
43 
48 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
49  sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
50 
56 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
57 
62 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
63  sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
64 
70 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
71 
77  uint32_t src_addr;
78  uint32_t dst_addr;
80  union {
81  struct {
82  uint16_t dport;
83  uint16_t sport;
84  };
85  uint32_t sctp_tag;
86  };
87 };
88 
95  uint8_t src_addr[16];
96  uint8_t dst_addr[16];
98  union {
99  struct {
100  uint16_t dport;
101  uint16_t sport;
102  };
103  uint32_t sctp_tag;
104  };
105 };
106 
107 union rte_thash_tuple {
108  struct rte_ipv4_tuple v4;
109  struct rte_ipv6_tuple v6;
110 #ifdef RTE_ARCH_X86
111 } __rte_aligned(XMM_SIZE);
112 #else
113 };
114 #endif
115 
125 static inline void
126 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
127 {
128  int i;
129 
130  for (i = 0; i < (len >> 2); i++)
131  targ[i] = rte_be_to_cpu_32(orig[i]);
132 }
133 
142 static inline void
144  union rte_thash_tuple *targ)
145 {
146 #ifdef RTE_ARCH_X86
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));
158 #else
159  int i;
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));
165  }
166 #endif
167 }
168 
180 static inline uint32_t
181 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
182  const uint8_t *rss_key)
183 {
184  uint32_t i, j, map, ret = 0;
185 
186  for (j = 0; j < input_len; j++) {
187  for (map = input_tuple[j]; map; map &= (map - 1)) {
188  i = rte_bsf32(map);
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])) >>
191  (i + 1));
192  }
193  }
194  return ret;
195 }
196 
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, map, ret = 0;
215 
216  for (j = 0; j < input_len; j++) {
217  for (map = input_tuple[j]; map; map &= (map - 1)) {
218  i = rte_bsf32(map);
219  ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
220  (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
221  }
222  }
223  return ret;
224 }
225 
227 #define RTE_THASH_RETA_SZ_MIN 2U
228 
229 #define RTE_THASH_RETA_SZ_MAX 16U
230 
235 #define RTE_THASH_IGNORE_PERIOD_OVERFLOW 0x1
236 
240 #define RTE_THASH_MINIMAL_SEQ 0x2
241 
243 struct rte_thash_ctx;
245 struct rte_thash_subtuple_helper;
246 
272 __rte_experimental
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);
276 
290 __rte_experimental
291 struct rte_thash_ctx *
292 rte_thash_find_existing(const char *name);
293 
305 __rte_experimental
306 void
307 rte_thash_free_ctx(struct rte_thash_ctx *ctx);
308 
331 __rte_experimental
332 int
333 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
334  uint32_t offset);
335 
349 __rte_experimental
350 struct rte_thash_subtuple_helper *
351 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name);
352 
368 __rte_experimental
369 uint32_t
370 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
371  uint32_t hash, uint32_t desired_hash);
372 
386 __rte_experimental
387 const uint8_t *
388 rte_thash_get_key(struct rte_thash_ctx *ctx);
389 
409 typedef int (*rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple);
410 
440 __rte_experimental
441 int
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);
447 
448 #ifdef __cplusplus
449 }
450 #endif
451 
452 #endif /* _RTE_THASH_H */
uint8_t dst_addr[16]
Definition: rte_ip.h:439
int(* rte_thash_check_tuple_t)(void *userdata, uint8_t *tuple)
Definition: rte_thash.h:409
static rte_be32_t rte_cpu_to_be_32(uint32_t x)
__rte_experimental int rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len, uint32_t offset)
static void rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
Definition: rte_thash.h:126
uint8_t src_addr[16]
Definition: rte_ip.h:438
__rte_experimental 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)
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:606
static void rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig, union rte_thash_tuple *targ)
Definition: rte_thash.h:143
static uint32_t rte_softrss(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:181
#define RTE_STD_C11
Definition: rte_common.h:42
static uint32_t rte_be_to_cpu_32(rte_be32_t x)
__rte_experimental struct rte_thash_ctx * rte_thash_find_existing(const char *name)
static uint32_t rte_softrss_be(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:211
__extension__ struct rte_eth_link __rte_aligned(8)
__rte_experimental uint32_t rte_thash_get_complement(struct rte_thash_subtuple_helper *h, uint32_t hash, uint32_t desired_hash)
__rte_experimental const uint8_t * rte_thash_get_key(struct rte_thash_ctx *ctx)
__rte_experimental void rte_thash_free_ctx(struct rte_thash_ctx *ctx)
__rte_experimental 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)
__rte_experimental struct rte_thash_subtuple_helper * rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)