1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2019 Vladimir Medvedkin <medvedkinv@gmail.com>
11 * toeplitz hash functions.
19 * Software implementation of the Toeplitz hash function used by RSS.
20 * Can be used either for packet distribution on single queue NIC
21 * or for simulating of RSS computation on specific NIC (for example
22 * after GRE header decapsulating)
26 #include <rte_byteorder.h>
27 #include <rte_config.h>
29 #include <rte_common.h>
31 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_NEON)
36 /* Byte swap mask used for converting IPv6 address
37 * 4-byte chunks to CPU byte order
39 static const __m128i rte_thash_ipv6_bswap_mask = {
40 0x0405060700010203ULL, 0x0C0D0E0F08090A0BULL};
44 * length in dwords of input tuple to
45 * calculate hash of ipv4 header only
47 #define RTE_THASH_V4_L3_LEN ((sizeof(struct rte_ipv4_tuple) - \
48 sizeof(((struct rte_ipv4_tuple *)0)->sctp_tag)) / 4)
51 * length in dwords of input tuple to
52 * calculate hash of ipv4 header +
55 #define RTE_THASH_V4_L4_LEN ((sizeof(struct rte_ipv4_tuple)) / 4)
58 * length in dwords of input tuple to
59 * calculate hash of ipv6 header only
61 #define RTE_THASH_V6_L3_LEN ((sizeof(struct rte_ipv6_tuple) - \
62 sizeof(((struct rte_ipv6_tuple *)0)->sctp_tag)) / 4)
65 * length in dwords of input tuple to
66 * calculate hash of ipv6 header +
69 #define RTE_THASH_V6_L4_LEN ((sizeof(struct rte_ipv6_tuple)) / 4)
73 * addresses and ports/sctp_tag have to be CPU byte order
75 struct rte_ipv4_tuple {
90 * Addresses have to be filled by rte_thash_load_v6_addr()
91 * ports/sctp_tag have to be CPU byte order
93 struct rte_ipv6_tuple {
106 union rte_thash_tuple {
107 struct rte_ipv4_tuple v4;
108 struct rte_ipv6_tuple v6;
110 } __rte_aligned(XMM_SIZE);
116 * Prepare special converted key to use with rte_softrss_be()
118 * pointer to original RSS key
120 * pointer to target RSS key
125 rte_convert_rss_key(const uint32_t *orig, uint32_t *targ, int len)
129 for (i = 0; i < (len >> 2); i++)
130 targ[i] = rte_be_to_cpu_32(orig[i]);
134 * Prepare and load IPv6 addresses (src and dst)
137 * Pointer to ipv6 header of the original packet
139 * Pointer to rte_ipv6_tuple structure
142 rte_thash_load_v6_addrs(const struct rte_ipv6_hdr *orig,
143 union rte_thash_tuple *targ)
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));
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));
169 * Generic implementation. Can be used with original rss_key
171 * Pointer to input tuple
173 * Length of input_tuple in 4-bytes chunks
175 * Pointer to RSS hash key.
177 * Calculated hash value.
179 static inline uint32_t
180 rte_softrss(uint32_t *input_tuple, uint32_t input_len,
181 const uint8_t *rss_key)
183 uint32_t i, j, map, ret = 0;
185 for (j = 0; j < input_len; j++) {
186 for (map = input_tuple[j]; map; map &= (map - 1)) {
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])) >>
197 * Optimized implementation.
198 * If you want the calculated hash value matches NIC RSS value
199 * you have to use special converted key with rte_convert_rss_key() fn.
201 * Pointer to input tuple
203 * Length of input_tuple in 4-bytes chunks
205 * Pointer to RSS hash key.
207 * Calculated hash value.
209 static inline uint32_t
210 rte_softrss_be(uint32_t *input_tuple, uint32_t input_len,
211 const uint8_t *rss_key)
213 uint32_t i, j, map, ret = 0;
215 for (j = 0; j < input_len; j++) {
216 for (map = input_tuple[j]; map; map &= (map - 1)) {
218 ret ^= ((const uint32_t *)rss_key)[j] << (31 - i) |
219 (uint32_t)((uint64_t)(((const uint32_t *)rss_key)[j + 1]) >> (i + 1));
229 #endif /* _RTE_THASH_H */