DPDK  19.08.0-rc0
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  */
4 
5 #ifndef _RTE_THASH_H
6 #define _RTE_THASH_H
7 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
25 #include <stdint.h>
26 #include <rte_byteorder.h>
27 #include <rte_config.h>
28 #include <rte_ip.h>
29 #include <rte_common.h>
30 
31 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_NEON)
32 #include <rte_vect.h>
33 #endif
34 
35 #ifdef RTE_ARCH_X86
36 /* Byte swap mask used for converting IPv6 address
37  * 4-byte chunks to CPU byte order
38  */
39 static const __m128i rte_thash_ipv6_bswap_mask = {
40  0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
41 #endif
42 
47 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
48  sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
49 
55 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
56 
61 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
62  sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
63 
69 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
70 
76  uint32_t src_addr;
77  uint32_t dst_addr;
79  union {
80  struct {
81  uint16_t dport;
82  uint16_t sport;
83  };
84  uint32_t sctp_tag;
85  };
86 };
87 
94  uint8_t src_addr[16];
95  uint8_t dst_addr[16];
97  union {
98  struct {
99  uint16_t dport;
100  uint16_t sport;
101  };
102  uint32_t sctp_tag;
103  };
104 };
105 
106 union rte_thash_tuple {
107  struct rte_ipv4_tuple v4;
108  struct rte_ipv6_tuple v6;
109 #ifdef RTE_ARCH_X86
110 } __attribute__((aligned(XMM_SIZE)));
111 #else
112 };
113 #endif
114 
124 static inline void
125 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
126 {
127  int i;
128 
129  for (i = 0; i < (len >> 2); i++)
130  targ[i] = rte_be_to_cpu_32(orig[i]);
131 }
132 
141 static inline void
143  union rte_thash_tuple *targ)
144 {
145 #ifdef RTE_ARCH_X86
146  __m128i ipv6 = _mm_loadu_si128((const __m128i *)orig->src_addr);
147  *(__m128i *)targ->v6.src_addr =
148  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
149  ipv6 = _mm_loadu_si128((const __m128i *)orig->dst_addr);
150  *(__m128i *)targ->v6.dst_addr =
151  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
152 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
153  uint8x16_t ipv6 = vld1q_u8((uint8_t const *)orig->src_addr);
154  vst1q_u8((uint8_t *)targ->v6.src_addr, vrev32q_u8(ipv6));
155  ipv6 = vld1q_u8((uint8_t const *)orig->dst_addr);
156  vst1q_u8((uint8_t *)targ->v6.dst_addr, vrev32q_u8(ipv6));
157 #else
158  int i;
159  for (i = 0; i < 4; i++) {
160  *((uint32_t *)targ->v6.src_addr + i) =
161  rte_be_to_cpu_32(*((const uint32_t *)orig->src_addr + i));
162  *((uint32_t *)targ->v6.dst_addr + i) =
163  rte_be_to_cpu_32(*((const uint32_t *)orig->dst_addr + i));
164  }
165 #endif
166 }
167 
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, map, ret = 0;
184 
185  for (j = 0; j < input_len; j++) {
186  for (map = input_tuple[j]; map; map &= (map - 1)) {
187  i = rte_bsf32(map);
188  ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
189  (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
190  (i + 1));
191  }
192  }
193  return ret;
194 }
195 
209 static inline uint32_t
210 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
211  const uint8_t *rss_key)
212 {
213  uint32_t i, j, map, ret = 0;
214 
215  for (j = 0; j < input_len; j++) {
216  for (map = input_tuple[j]; map; map &= (map - 1)) {
217  i = rte_bsf32(map);
218  ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
219  (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
220  }
221  }
222  return ret;
223 }
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif /* _RTE_THASH_H */
uint8_t dst_addr[16]
Definition: rte_ip.h:353
static rte_be32_t rte_cpu_to_be_32(uint32_t x)
static void rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
Definition: rte_thash.h:125
uint8_t src_addr[16]
Definition: rte_ip.h:352
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:464
static void rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig, union rte_thash_tuple *targ)
Definition: rte_thash.h:142
static uint32_t rte_softrss(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:180
#define RTE_STD_C11
Definition: rte_common.h:40
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:210