DPDK  18.11.8
rte_thash.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2015 Vladimir Medvedkin <medvedkinv@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_THASH_H
35 #define _RTE_THASH_H
36 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
54 #include <stdint.h>
55 #include <rte_byteorder.h>
56 #include <rte_config.h>
57 #include <rte_ip.h>
58 #include <rte_common.h>
59 
60 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_NEON) || \
61  defined(RTE_ARCH_MIPS_64)
62 #include <rte_vect.h>
63 #endif
64 
65 #ifdef RTE_ARCH_X86
66 /* Byte swap mask used for converting IPv6 address
67  * 4-byte chunks to CPU byte order
68  */
69 static const __m128i rte_thash_ipv6_bswap_mask = {
70  0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
71 #elif defined(RTE_ARCH_MIPS_64)
72 static const xmm_t rte_thash_ipv6_bswap_mask = (xmm_t){.u64 = {
73  0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL,} };
74 #endif
75 
80 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
81  sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
82 
88 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
89 
94 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
95  sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
96 
102 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
103 
109  uint32_t src_addr;
110  uint32_t dst_addr;
112  union {
113  struct {
114  uint16_t dport;
115  uint16_t sport;
116  };
117  uint32_t sctp_tag;
118  };
119 };
120 
127  uint8_t src_addr[16];
128  uint8_t dst_addr[16];
130  union {
131  struct {
132  uint16_t dport;
133  uint16_t sport;
134  };
135  uint32_t sctp_tag;
136  };
137 };
138 
139 union rte_thash_tuple {
140  struct rte_ipv4_tuple v4;
141  struct rte_ipv6_tuple v6;
142 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_MIPS_64)
143 } __attribute__((aligned(XMM_SIZE)));
144 #else
145 };
146 #endif
147 
157 static inline void
158 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
159 {
160  int i;
161 
162  for (i = 0; i < (len >> 2); i++)
163  targ[i] = rte_be_to_cpu_32(orig[i]);
164 }
165 
174 static inline void
175 rte_thash_load_v6_addrs(const struct ipv6_hdr *orig, union rte_thash_tuple *targ)
176 {
177 #ifdef RTE_ARCH_X86
178  __m128i ipv6 = _mm_loadu_si128((const __m128i *)orig->src_addr);
179  *(__m128i *)targ->v6.src_addr =
180  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
181  ipv6 = _mm_loadu_si128((const __m128i *)orig->dst_addr);
182  *(__m128i *)targ->v6.dst_addr =
183  _mm_shuffle_epi8(ipv6, rte_thash_ipv6_bswap_mask);
184 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
185  uint8x16_t ipv6 = vld1q_u8((uint8_t const *)orig->src_addr);
186  vst1q_u8((uint8_t *)targ->v6.src_addr, vrev32q_u8(ipv6));
187  ipv6 = vld1q_u8((uint8_t const *)orig->dst_addr);
188  vst1q_u8((uint8_t *)targ->v6.dst_addr, vrev32q_u8(ipv6));
189 #elif defined(RTE_ARCH_MIPS_64)
190  xmm_t ipv6, v_zeros = {.u64 = {0, 0} };
191  ipv6.v2i64 = __msa_ld_d((const xmm_t *)orig->src_addr, 0);
192  ((xmm_t *)targ->v6.src_addr)->v16i8 =
193  __msa_vshf_b(rte_thash_ipv6_bswap_mask.v16i8,
194  v_zeros.v16i8, ipv6.v16i8);
195  ipv6.v2i64 = __msa_ld_d((const xmm_t *)orig->dst_addr, 0);
196  ((xmm_t *)targ->v6.dst_addr)->v16i8 =
197  __msa_vshf_b(rte_thash_ipv6_bswap_mask.v16i8,
198  v_zeros.v16i8, ipv6.v16i8);
199 #else
200  int i;
201  for (i = 0; i < 4; i++) {
202  *((uint32_t *)targ->v6.src_addr + i) =
203  rte_be_to_cpu_32(*((const uint32_t *)orig->src_addr + i));
204  *((uint32_t *)targ->v6.dst_addr + i) =
205  rte_be_to_cpu_32(*((const uint32_t *)orig->dst_addr + i));
206  }
207 #endif
208 }
209 
221 static inline uint32_t
222 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
223  const uint8_t *rss_key)
224 {
225  uint32_t i, j, map, ret = 0;
226 
227  for (j = 0; j < input_len; j++) {
228  for (map = input_tuple[j]; map; map &= (map - 1)) {
229  i = rte_bsf32(map);
230  ret ^= rte_cpu_to_be_32(((const uint32_t *)rss_key)[j]) << (31 - i) |
231  (uint32_t)((uint64_t)(rte_cpu_to_be_32(((const uint32_t *)rss_key)[j + 1])) >>
232  (i + 1));
233  }
234  }
235  return ret;
236 }
237 
251 static inline uint32_t
252 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
253  const uint8_t *rss_key)
254 {
255  uint32_t i, j, map, ret = 0;
256 
257  for (j = 0; j < input_len; j++) {
258  for (map = input_tuple[j]; map; map &= (map - 1)) {
259  i = rte_bsf32(map);
260  ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
261  (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
262  }
263  }
264  return ret;
265 }
266 
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif /* _RTE_THASH_H */
static rte_be32_t rte_cpu_to_be_32(uint32_t x)
uint8_t dst_addr[16]
Definition: rte_ip.h:355
static void rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
Definition: rte_thash.h:158
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:445
static uint32_t rte_softrss(uint32_t *input_tuple, uint32_t input_len, const uint8_t *rss_key)
Definition: rte_thash.h:222
uint8_t src_addr[16]
Definition: rte_ip.h:354
#define RTE_STD_C11
Definition: rte_common.h:37
static void rte_thash_load_v6_addrs(const struct ipv6_hdr *orig, union rte_thash_tuple *targ)
Definition: rte_thash.h:175
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:252