Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2015-2019 Vladimir Medvedkin <medvedkinv@gmail.com>
3 : : */
4 : :
5 : : #include <rte_common.h>
6 : : #include <rte_eal.h>
7 : : #include <rte_ip.h>
8 : : #include <rte_random.h>
9 : : #include <rte_malloc.h>
10 : : #include <rte_byteorder.h>
11 : :
12 : : #include "test.h"
13 : :
14 : : #include <rte_thash.h>
15 : :
16 : : #define HASH_MSK(reta_sz) ((1 << reta_sz) - 1)
17 : : #define TUPLE_SZ (RTE_THASH_V4_L4_LEN * 4)
18 : :
19 : : struct test_thash_v4 {
20 : : uint32_t dst_ip;
21 : : uint32_t src_ip;
22 : : uint16_t dst_port;
23 : : uint16_t src_port;
24 : : uint32_t hash_l3;
25 : : uint32_t hash_l3l4;
26 : : };
27 : :
28 : : struct test_thash_v6 {
29 : : struct rte_ipv6_addr dst_ip;
30 : : struct rte_ipv6_addr src_ip;
31 : : uint16_t dst_port;
32 : : uint16_t src_port;
33 : : uint32_t hash_l3;
34 : : uint32_t hash_l3l4;
35 : : };
36 : :
37 : : /*From 82599 Datasheet 7.1.2.8.3 RSS Verification Suite*/
38 : : struct test_thash_v4 v4_tbl[] = {
39 : : {RTE_IPV4(161, 142, 100, 80), RTE_IPV4(66, 9, 149, 187),
40 : : 1766, 2794, 0x323e8fc2, 0x51ccc178},
41 : : {RTE_IPV4(65, 69, 140, 83), RTE_IPV4(199, 92, 111, 2),
42 : : 4739, 14230, 0xd718262a, 0xc626b0ea},
43 : : {RTE_IPV4(12, 22, 207, 184), RTE_IPV4(24, 19, 198, 95),
44 : : 38024, 12898, 0xd2d0a5de, 0x5c2b394a},
45 : : {RTE_IPV4(209, 142, 163, 6), RTE_IPV4(38, 27, 205, 30),
46 : : 2217, 48228, 0x82989176, 0xafc7327f},
47 : : {RTE_IPV4(202, 188, 127, 2), RTE_IPV4(153, 39, 163, 191),
48 : : 1303, 44251, 0x5d1809c5, 0x10e828a2},
49 : : };
50 : :
51 : : struct test_thash_v6 v6_tbl[] = {
52 : : /*3ffe:2501:200:3::1*/
53 : : {RTE_IPV6(0x3ffe, 0x2501, 0x0200, 0x0003, 0, 0, 0, 0x0001),
54 : : /*3ffe:2501:200:1fff::7*/
55 : : RTE_IPV6(0x3ffe, 0x2501, 0x0200, 0x1fff, 0, 0, 0, 0x0007),
56 : : 1766, 2794, 0x2cc18cd5, 0x40207d3d},
57 : : /*ff02::1*/
58 : : {RTE_IPV6(0xff02, 0, 0, 0, 0, 0, 0, 0x0001),
59 : : /*3ffe:501:8::260:97ff:fe40:efab*/
60 : : RTE_IPV6(0x3ffe, 0x0501, 0x0008, 0, 0x0260, 0x97ff, 0xfe40, 0xefab),
61 : : 4739, 14230, 0x0f0c461c, 0xdde51bbf},
62 : : /*fe80::200:f8ff:fe21:67cf*/
63 : : {RTE_IPV6(0xfe80, 0, 0, 0, 0x0200, 0xf8ff, 0xfe21, 0x67cf),
64 : : /*3ffe:1900:4545:3:200:f8ff:fe21:67cf*/
65 : : RTE_IPV6(0x3ffe, 0x1900, 0x4545, 0x0003, 0x0200, 0xf8ff, 0xfe21, 0x67cf),
66 : : 38024, 44251, 0x4b61e985, 0x02d1feef},
67 : : };
68 : :
69 : : uint8_t default_rss_key[] = {
70 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
71 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
72 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
73 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
74 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
75 : : };
76 : :
77 : : static const uint8_t big_rss_key[] = {
78 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
79 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
80 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
81 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
82 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
83 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
84 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
85 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
86 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
87 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
88 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
89 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
90 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
91 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
92 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
93 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
94 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
95 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
96 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
97 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
98 : : 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
99 : : 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
100 : : 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
101 : : 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
102 : : 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
103 : : };
104 : :
105 : : static int
106 : 1 : test_toeplitz_hash_calc(void)
107 : : {
108 : : uint32_t i;
109 : : union rte_thash_tuple tuple;
110 : : uint32_t rss_l3, rss_l3l4;
111 : : uint8_t rss_key_be[RTE_DIM(default_rss_key)];
112 : : struct rte_ipv6_hdr ipv6_hdr;
113 : :
114 : : /* Convert RSS key*/
115 : : rte_convert_rss_key((uint32_t *)&default_rss_key,
116 : : (uint32_t *)rss_key_be, RTE_DIM(default_rss_key));
117 : :
118 : :
119 [ + + ]: 6 : for (i = 0; i < RTE_DIM(v4_tbl); i++) {
120 : 5 : tuple.v4.src_addr = v4_tbl[i].src_ip;
121 : 5 : tuple.v4.dst_addr = v4_tbl[i].dst_ip;
122 : 5 : tuple.v4.sport = v4_tbl[i].src_port;
123 : 5 : tuple.v4.dport = v4_tbl[i].dst_port;
124 : : /*Calculate hash with original key*/
125 : 5 : rss_l3 = rte_softrss((uint32_t *)&tuple,
126 : : RTE_THASH_V4_L3_LEN, default_rss_key);
127 : 5 : rss_l3l4 = rte_softrss((uint32_t *)&tuple,
128 : : RTE_THASH_V4_L4_LEN, default_rss_key);
129 [ + - ]: 5 : if ((rss_l3 != v4_tbl[i].hash_l3) ||
130 [ + - ]: 5 : (rss_l3l4 != v4_tbl[i].hash_l3l4))
131 : : return -TEST_FAILED;
132 : : /*Calculate hash with converted key*/
133 : 5 : rss_l3 = rte_softrss_be((uint32_t *)&tuple,
134 : : RTE_THASH_V4_L3_LEN, rss_key_be);
135 : 5 : rss_l3l4 = rte_softrss_be((uint32_t *)&tuple,
136 : : RTE_THASH_V4_L4_LEN, rss_key_be);
137 [ + - + - ]: 5 : if ((rss_l3 != v4_tbl[i].hash_l3) ||
138 : : (rss_l3l4 != v4_tbl[i].hash_l3l4))
139 : : return -TEST_FAILED;
140 : : }
141 [ + + ]: 4 : for (i = 0; i < RTE_DIM(v6_tbl); i++) {
142 : : /*Fill ipv6 hdr*/
143 : 3 : ipv6_hdr.src_addr = v6_tbl[i].src_ip;
144 : 3 : ipv6_hdr.dst_addr = v6_tbl[i].dst_ip;
145 : : /*Load and convert ipv6 address into tuple*/
146 : : rte_thash_load_v6_addrs(&ipv6_hdr, &tuple);
147 : 3 : tuple.v6.sport = v6_tbl[i].src_port;
148 : 3 : tuple.v6.dport = v6_tbl[i].dst_port;
149 : : /*Calculate hash with original key*/
150 : 3 : rss_l3 = rte_softrss((uint32_t *)&tuple,
151 : : RTE_THASH_V6_L3_LEN, default_rss_key);
152 : 3 : rss_l3l4 = rte_softrss((uint32_t *)&tuple,
153 : : RTE_THASH_V6_L4_LEN, default_rss_key);
154 [ + - ]: 3 : if ((rss_l3 != v6_tbl[i].hash_l3) ||
155 [ + - ]: 3 : (rss_l3l4 != v6_tbl[i].hash_l3l4))
156 : : return -TEST_FAILED;
157 : : /*Calculate hash with converted key*/
158 : 3 : rss_l3 = rte_softrss_be((uint32_t *)&tuple,
159 : : RTE_THASH_V6_L3_LEN, rss_key_be);
160 : 3 : rss_l3l4 = rte_softrss_be((uint32_t *)&tuple,
161 : : RTE_THASH_V6_L4_LEN, rss_key_be);
162 [ + - + - ]: 3 : if ((rss_l3 != v6_tbl[i].hash_l3) ||
163 : : (rss_l3l4 != v6_tbl[i].hash_l3l4))
164 : : return -TEST_FAILED;
165 : : }
166 : : return TEST_SUCCESS;
167 : : }
168 : :
169 : : static int
170 : 1 : test_toeplitz_hash_gfni(void)
171 : : {
172 : : uint32_t i;
173 : : union rte_thash_tuple tuple;
174 : : uint32_t rss_l3, rss_l3l4;
175 : : uint64_t rss_key_matrixes[RTE_DIM(default_rss_key)];
176 : :
177 [ - + ]: 1 : if (!rte_thash_gfni_supported())
178 : : return TEST_SKIPPED;
179 : :
180 : : /* Convert RSS key into matrixes */
181 : 0 : rte_thash_complete_matrix(rss_key_matrixes, default_rss_key,
182 : : RTE_DIM(default_rss_key));
183 : :
184 [ # # ]: 0 : for (i = 0; i < RTE_DIM(v4_tbl); i++) {
185 [ # # ]: 0 : tuple.v4.src_addr = rte_cpu_to_be_32(v4_tbl[i].src_ip);
186 [ # # ]: 0 : tuple.v4.dst_addr = rte_cpu_to_be_32(v4_tbl[i].dst_ip);
187 [ # # ]: 0 : tuple.v4.sport = rte_cpu_to_be_16(v4_tbl[i].dst_port);
188 [ # # ]: 0 : tuple.v4.dport = rte_cpu_to_be_16(v4_tbl[i].src_port);
189 : :
190 : : rss_l3 = rte_thash_gfni(rss_key_matrixes, (uint8_t *)&tuple,
191 : : RTE_THASH_V4_L3_LEN * 4);
192 : : rss_l3l4 = rte_thash_gfni(rss_key_matrixes, (uint8_t *)&tuple,
193 : : RTE_THASH_V4_L4_LEN * 4);
194 [ # # ]: 0 : if ((rss_l3 != v4_tbl[i].hash_l3) ||
195 [ # # ]: 0 : (rss_l3l4 != v4_tbl[i].hash_l3l4))
196 : : return -TEST_FAILED;
197 : : }
198 : :
199 [ # # ]: 0 : for (i = 0; i < RTE_DIM(v6_tbl); i++) {
200 : 0 : tuple.v6.src_addr = v6_tbl[i].src_ip;
201 : 0 : tuple.v6.dst_addr = v6_tbl[i].dst_ip;
202 [ # # ]: 0 : tuple.v6.sport = rte_cpu_to_be_16(v6_tbl[i].dst_port);
203 [ # # ]: 0 : tuple.v6.dport = rte_cpu_to_be_16(v6_tbl[i].src_port);
204 : : rss_l3 = rte_thash_gfni(rss_key_matrixes, (uint8_t *)&tuple,
205 : : RTE_THASH_V6_L3_LEN * 4);
206 : : rss_l3l4 = rte_thash_gfni(rss_key_matrixes, (uint8_t *)&tuple,
207 : : RTE_THASH_V6_L4_LEN * 4);
208 [ # # ]: 0 : if ((rss_l3 != v6_tbl[i].hash_l3) ||
209 [ # # ]: 0 : (rss_l3l4 != v6_tbl[i].hash_l3l4))
210 : : return -TEST_FAILED;
211 : : }
212 : :
213 : : return TEST_SUCCESS;
214 : : }
215 : :
216 : : #define DATA_SZ 4
217 : : #define ITER 1000
218 : :
219 : : enum {
220 : : SCALAR_DATA_BUF_1_HASH_IDX = 0,
221 : : SCALAR_DATA_BUF_2_HASH_IDX,
222 : : GFNI_DATA_BUF_1_HASH_IDX,
223 : : GFNI_DATA_BUF_2_HASH_IDX,
224 : : GFNI_BULK_DATA_BUF_1_HASH_IDX,
225 : : GFNI_BULK_DATA_BUF_2_HASH_IDX,
226 : : HASH_IDXES
227 : : };
228 : :
229 : : static int
230 : 1 : test_toeplitz_hash_rand_data(void)
231 : : {
232 : : uint32_t data[2][DATA_SZ];
233 : : uint32_t scalar_data[2][DATA_SZ];
234 : 1 : uint32_t hash[HASH_IDXES] = { 0 };
235 : : uint64_t rss_key_matrixes[RTE_DIM(default_rss_key)];
236 : : int i, j;
237 : : uint8_t *bulk_data[2];
238 : :
239 [ - + ]: 1 : if (!rte_thash_gfni_supported())
240 : : return TEST_SKIPPED;
241 : :
242 : 0 : rte_thash_complete_matrix(rss_key_matrixes, default_rss_key,
243 : : RTE_DIM(default_rss_key));
244 : :
245 [ # # ]: 0 : for (i = 0; i < 2; i++)
246 : 0 : bulk_data[i] = (uint8_t *)data[i];
247 : :
248 [ # # ]: 0 : for (i = 0; i < ITER; i++) {
249 [ # # ]: 0 : for (j = 0; j < DATA_SZ; j++) {
250 : 0 : data[0][j] = rte_rand();
251 : 0 : data[1][j] = rte_rand();
252 [ # # ]: 0 : scalar_data[0][j] = rte_cpu_to_be_32(data[0][j]);
253 [ # # ]: 0 : scalar_data[1][j] = rte_cpu_to_be_32(data[1][j]);
254 : : }
255 : :
256 : 0 : hash[SCALAR_DATA_BUF_1_HASH_IDX] = rte_softrss(scalar_data[0],
257 : : DATA_SZ, default_rss_key);
258 : 0 : hash[SCALAR_DATA_BUF_2_HASH_IDX] = rte_softrss(scalar_data[1],
259 : : DATA_SZ, default_rss_key);
260 : 0 : hash[GFNI_DATA_BUF_1_HASH_IDX] = rte_thash_gfni(
261 : : rss_key_matrixes, (uint8_t *)data[0],
262 : : DATA_SZ * sizeof(uint32_t));
263 : 0 : hash[GFNI_DATA_BUF_2_HASH_IDX] = rte_thash_gfni(
264 : : rss_key_matrixes, (uint8_t *)data[1],
265 : : DATA_SZ * sizeof(uint32_t));
266 : : rte_thash_gfni_bulk(rss_key_matrixes,
267 : : DATA_SZ * sizeof(uint32_t), bulk_data,
268 : : &hash[GFNI_BULK_DATA_BUF_1_HASH_IDX], 2);
269 : :
270 : 0 : if ((hash[SCALAR_DATA_BUF_1_HASH_IDX] !=
271 [ # # ]: 0 : hash[GFNI_DATA_BUF_1_HASH_IDX]) ||
272 : : (hash[SCALAR_DATA_BUF_1_HASH_IDX] !=
273 [ # # ]: 0 : hash[GFNI_BULK_DATA_BUF_1_HASH_IDX]) ||
274 : 0 : (hash[SCALAR_DATA_BUF_2_HASH_IDX] !=
275 [ # # ]: 0 : hash[GFNI_DATA_BUF_2_HASH_IDX]) ||
276 : : (hash[SCALAR_DATA_BUF_2_HASH_IDX] !=
277 [ # # ]: 0 : hash[GFNI_BULK_DATA_BUF_2_HASH_IDX]))
278 : :
279 : : return -TEST_FAILED;
280 : : }
281 : :
282 : : return TEST_SUCCESS;
283 : : }
284 : :
285 : : enum {
286 : : RSS_V4_IDX,
287 : : RSS_V6_IDX
288 : : };
289 : :
290 : : static int
291 : 1 : test_toeplitz_hash_gfni_bulk(void)
292 : : {
293 : : uint32_t i;
294 : : union rte_thash_tuple tuple[2];
295 : : uint8_t *tuples[2];
296 : 1 : uint32_t rss[2] = { 0 };
297 : : uint64_t rss_key_matrixes[RTE_DIM(default_rss_key)];
298 : :
299 [ - + ]: 1 : if (!rte_thash_gfni_supported())
300 : : return TEST_SKIPPED;
301 : :
302 : : /* Convert RSS key into matrixes */
303 : 0 : rte_thash_complete_matrix(rss_key_matrixes, default_rss_key,
304 : : RTE_DIM(default_rss_key));
305 : :
306 [ # # ]: 0 : for (i = 0; i < RTE_DIM(tuples); i++) {
307 : : /* allocate memory enough for a biggest tuple */
308 : 0 : tuples[i] = rte_zmalloc(NULL, RTE_THASH_V6_L4_LEN * 4, 0);
309 [ # # ]: 0 : if (tuples[i] == NULL)
310 : : return -TEST_FAILED;
311 : : }
312 : :
313 [ # # ]: 0 : for (i = 0; i < RTE_MIN(RTE_DIM(v4_tbl), RTE_DIM(v6_tbl)); i++) {
314 : : /*Load IPv4 headers and copy it into the corresponding tuple*/
315 [ # # ]: 0 : tuple[0].v4.src_addr = rte_cpu_to_be_32(v4_tbl[i].src_ip);
316 [ # # ]: 0 : tuple[0].v4.dst_addr = rte_cpu_to_be_32(v4_tbl[i].dst_ip);
317 [ # # ]: 0 : tuple[0].v4.sport = rte_cpu_to_be_16(v4_tbl[i].dst_port);
318 [ # # ]: 0 : tuple[0].v4.dport = rte_cpu_to_be_16(v4_tbl[i].src_port);
319 [ # # ]: 0 : rte_memcpy(tuples[0], &tuple[0], RTE_THASH_V4_L4_LEN * 4);
320 : :
321 : : /*Load IPv6 headers and copy it into the corresponding tuple*/
322 : 0 : tuple[1].v6.src_addr = v6_tbl[i].src_ip;
323 : 0 : tuple[1].v6.dst_addr = v6_tbl[i].dst_ip;
324 [ # # ]: 0 : tuple[1].v6.sport = rte_cpu_to_be_16(v6_tbl[i].dst_port);
325 [ # # ]: 0 : tuple[1].v6.dport = rte_cpu_to_be_16(v6_tbl[i].src_port);
326 [ # # ]: 0 : rte_memcpy(tuples[1], &tuple[1], RTE_THASH_V6_L4_LEN * 4);
327 : :
328 : : rte_thash_gfni_bulk(rss_key_matrixes, RTE_THASH_V6_L4_LEN * 4,
329 : : tuples, rss, 2);
330 : :
331 [ # # ]: 0 : if ((rss[RSS_V4_IDX] != v4_tbl[i].hash_l3l4) ||
332 [ # # ]: 0 : (rss[RSS_V6_IDX] != v6_tbl[i].hash_l3l4))
333 : : return -TEST_FAILED;
334 : : }
335 : :
336 : : return TEST_SUCCESS;
337 : : }
338 : :
339 : : static int
340 : 1 : test_big_tuple_gfni(void)
341 : : {
342 : : uint32_t arr[16];
343 : : uint32_t arr_softrss[16];
344 : : uint32_t hash_1, hash_2;
345 : : uint64_t rss_key_matrixes[RTE_DIM(big_rss_key)];
346 : : unsigned int i, size = RTE_DIM(arr) * sizeof(uint32_t);
347 : :
348 [ - + ]: 1 : if (!rte_thash_gfni_supported())
349 : : return TEST_SKIPPED;
350 : :
351 : : /* Convert RSS key into matrixes */
352 : 0 : rte_thash_complete_matrix(rss_key_matrixes, big_rss_key,
353 : : RTE_DIM(big_rss_key));
354 : :
355 [ # # ]: 0 : for (i = 0; i < RTE_DIM(arr); i++) {
356 : 0 : arr[i] = rte_rand();
357 [ # # ]: 0 : arr_softrss[i] = rte_be_to_cpu_32(arr[i]);
358 : : }
359 : :
360 : 0 : hash_1 = rte_softrss(arr_softrss, RTE_DIM(arr), big_rss_key);
361 : : hash_2 = rte_thash_gfni(rss_key_matrixes, (uint8_t *)arr, size);
362 : :
363 [ # # ]: 0 : if (hash_1 != hash_2)
364 : 0 : return -TEST_FAILED;
365 : :
366 : : return TEST_SUCCESS;
367 : : }
368 : :
369 : : static int
370 : 1 : test_create_invalid(void)
371 : : {
372 : : struct rte_thash_ctx *ctx;
373 : : int key_len = 40;
374 : : int reta_sz = 7;
375 : :
376 : 1 : ctx = rte_thash_init_ctx(NULL, key_len, reta_sz, NULL, 0);
377 [ - + ]: 1 : RTE_TEST_ASSERT(ctx == NULL,
378 : : "Call succeeded with invalid parameters\n");
379 : :
380 : 1 : ctx = rte_thash_init_ctx("test", 0, reta_sz, NULL, 0);
381 [ - + ]: 1 : RTE_TEST_ASSERT(ctx == NULL,
382 : : "Call succeeded with invalid parameters\n");
383 : :
384 : 1 : ctx = rte_thash_init_ctx(NULL, key_len, 1, NULL, 0);
385 [ - + ]: 1 : RTE_TEST_ASSERT(ctx == NULL,
386 : : "Call succeeded with invalid parameters\n");
387 : :
388 : 1 : ctx = rte_thash_init_ctx(NULL, key_len, 17, NULL, 0);
389 [ - + ]: 1 : RTE_TEST_ASSERT(ctx == NULL,
390 : : "Call succeeded with invalid parameters\n");
391 : :
392 : : return TEST_SUCCESS;
393 : : }
394 : :
395 : : static int
396 : 1 : test_multiple_create(void)
397 : : {
398 : : struct rte_thash_ctx *ctx;
399 : : int key_len = 40;
400 : : int reta_sz = 7;
401 : : int i;
402 : :
403 [ + + ]: 101 : for (i = 0; i < 100; i++) {
404 : 100 : ctx = rte_thash_init_ctx("test", key_len, reta_sz, NULL, 0);
405 [ - + ]: 100 : RTE_TEST_ASSERT(ctx != NULL, "Can not create CTX\n");
406 : :
407 : 100 : rte_thash_free_ctx(ctx);
408 : : }
409 : :
410 : : return TEST_SUCCESS;
411 : : }
412 : :
413 : : static int
414 : 1 : test_free_null(void)
415 : : {
416 : : struct rte_thash_ctx *ctx;
417 : :
418 : 1 : ctx = rte_thash_init_ctx("test", 40, 7, NULL, 0);
419 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "Can not create CTX\n");
420 : :
421 : 1 : rte_thash_free_ctx(ctx);
422 : 1 : rte_thash_free_ctx(NULL);
423 : :
424 : 1 : return TEST_SUCCESS;
425 : : }
426 : :
427 : : static int
428 : 1 : test_add_invalid_helper(void)
429 : : {
430 : : struct rte_thash_ctx *ctx;
431 : : const int key_len = 40;
432 : : int reta_sz = 7;
433 : : int ret;
434 : :
435 : 1 : ctx = rte_thash_init_ctx("test", key_len, reta_sz, NULL, 0);
436 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
437 : :
438 : 1 : ret = rte_thash_add_helper(NULL, "test", reta_sz, 0);
439 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EINVAL,
440 : : "Call succeeded with invalid parameters\n");
441 : :
442 : 1 : ret = rte_thash_add_helper(ctx, NULL, reta_sz, 0);
443 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EINVAL,
444 : : "Call succeeded with invalid parameters\n");
445 : :
446 : 1 : ret = rte_thash_add_helper(ctx, "test", reta_sz - 1, 0);
447 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EINVAL,
448 : : "Call succeeded with invalid parameters\n");
449 : :
450 : 1 : ret = rte_thash_add_helper(ctx, "test", reta_sz, key_len * 8);
451 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EINVAL,
452 : : "Call succeeded with invalid parameters\n");
453 : :
454 : 1 : ret = rte_thash_add_helper(ctx, "first_range", reta_sz, 0);
455 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "Can not create helper\n");
456 : :
457 : 1 : ret = rte_thash_add_helper(ctx, "first_range", reta_sz, 0);
458 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EEXIST,
459 : : "Call succeeded with duplicated name\n");
460 : :
461 : : /*
462 : : * Create second helper with offset 3 * reta_sz.
463 : : * Note first_range helper created range in key:
464 : : * [0, 32 + length{= reta_sz} - 1), i.e [0, 37).
465 : : * second range is [44, 81)
466 : : */
467 : 1 : ret = rte_thash_add_helper(ctx, "second_range", reta_sz,
468 : : 32 + 2 * reta_sz);
469 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "Can not create helper\n");
470 : :
471 : : /*
472 : : * Try to create overlapping with first_ and second_ ranges,
473 : : * i.e. [6, 49)
474 : : */
475 : 1 : ret = rte_thash_add_helper(ctx, "third_range", 2 * reta_sz, reta_sz);
476 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EEXIST,
477 : : "Call succeeded with overlapping ranges\n");
478 : :
479 : 1 : rte_thash_free_ctx(ctx);
480 : :
481 : 1 : return TEST_SUCCESS;
482 : : }
483 : :
484 : : static int
485 : 1 : test_find_existing(void)
486 : : {
487 : : struct rte_thash_ctx *ctx, *ret_ctx;
488 : :
489 : 1 : ctx = rte_thash_init_ctx("test", 40, 7, NULL, 0);
490 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
491 : :
492 : 1 : ret_ctx = rte_thash_find_existing("test");
493 [ - + ]: 1 : RTE_TEST_ASSERT(ret_ctx != NULL, "can not find existing ctx\n");
494 : :
495 : 1 : rte_thash_free_ctx(ctx);
496 : :
497 : 1 : return TEST_SUCCESS;
498 : : }
499 : :
500 : : static int
501 : 1 : test_get_helper(void)
502 : : {
503 : : struct rte_thash_ctx *ctx;
504 : : struct rte_thash_subtuple_helper *h;
505 : : int ret;
506 : :
507 : 1 : ctx = rte_thash_init_ctx("test", 40, 7, NULL, 0);
508 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "Can not create thash ctx\n");
509 : :
510 : 1 : h = rte_thash_get_helper(NULL, "first_range");
511 [ - + ]: 1 : RTE_TEST_ASSERT(h == NULL, "Call succeeded with invalid parameters\n");
512 : :
513 : 1 : h = rte_thash_get_helper(ctx, NULL);
514 [ - + ]: 1 : RTE_TEST_ASSERT(h == NULL, "Call succeeded with invalid parameters\n");
515 : :
516 : 1 : ret = rte_thash_add_helper(ctx, "first_range", 8, 0);
517 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "Can not create helper\n");
518 : :
519 : 1 : h = rte_thash_get_helper(ctx, "first_range");
520 [ - + ]: 1 : RTE_TEST_ASSERT(h != NULL, "Can not find helper\n");
521 : :
522 : 1 : rte_thash_free_ctx(ctx);
523 : :
524 : 1 : return TEST_SUCCESS;
525 : : }
526 : :
527 : : static int
528 : 1 : test_period_overflow(void)
529 : : {
530 : : struct rte_thash_ctx *ctx;
531 : : int reta_sz = 7; /* reflects polynomial degree */
532 : : int ret;
533 : :
534 : : /* first create without RTE_THASH_IGNORE_PERIOD_OVERFLOW flag */
535 : 1 : ctx = rte_thash_init_ctx("test", 40, reta_sz, NULL, 0);
536 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "Can not create thash ctx\n");
537 : :
538 : : /* requested range > (2^reta_sz) - 1 */
539 : 1 : ret = rte_thash_add_helper(ctx, "test", (1 << reta_sz), 0);
540 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -ENOSPC,
541 : : "Call succeeded with invalid parameters\n");
542 : :
543 : : /* requested range == len + 32 - 1, smaller than (2^reta_sz) - 1 */
544 : 1 : ret = rte_thash_add_helper(ctx, "test", (1 << reta_sz) - 32, 0);
545 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "Can not create helper\n");
546 : :
547 : 1 : rte_thash_free_ctx(ctx);
548 : :
549 : : /* create with RTE_THASH_IGNORE_PERIOD_OVERFLOW flag */
550 : 1 : ctx = rte_thash_init_ctx("test", 40, reta_sz, NULL,
551 : : RTE_THASH_IGNORE_PERIOD_OVERFLOW);
552 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "Can not create thash ctx\n");
553 : :
554 : : /* requested range > (2^reta_sz - 1) */
555 : 1 : ret = rte_thash_add_helper(ctx, "test", (1 << reta_sz) + 10, 0);
556 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "Can not create helper\n");
557 : :
558 : 1 : rte_thash_free_ctx(ctx);
559 : :
560 : 1 : return TEST_SUCCESS;
561 : : }
562 : :
563 : : static int
564 : 1 : test_predictable_rss_min_seq(void)
565 : : {
566 : : struct rte_thash_ctx *ctx;
567 : : struct rte_thash_subtuple_helper *h;
568 : : int reta_sz = 6;
569 : : uint8_t initial_key[40];
570 : : const uint8_t *new_key;
571 : : int ret;
572 : : union rte_thash_tuple tuple;
573 : : uint32_t orig_hash, adj_hash, adj;
574 : : unsigned int desired_value = 27 & HASH_MSK(reta_sz);
575 : : uint16_t port_value = 22;
576 : :
577 : : memset(initial_key, 0, RTE_DIM(initial_key));
578 : :
579 : 1 : ctx = rte_thash_init_ctx("test", RTE_DIM(initial_key), reta_sz, initial_key,
580 : : RTE_THASH_MINIMAL_SEQ);
581 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
582 : :
583 : 1 : ret = rte_thash_add_helper(ctx, "snat", sizeof(uint16_t) * 8,
584 : : offsetof(union rte_thash_tuple, v4.sport) * 8);
585 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "can not add helper, ret %d\n", ret);
586 : :
587 : 1 : h = rte_thash_get_helper(ctx, "snat");
588 [ - + ]: 1 : RTE_TEST_ASSERT(h != NULL, "can not find helper\n");
589 : :
590 : 1 : new_key = rte_thash_get_key(ctx);
591 : 1 : tuple.v4.src_addr = RTE_IPV4(0, 0, 0, 0);
592 : 1 : tuple.v4.dst_addr = RTE_IPV4(0, 0, 0, 0);
593 : : tuple.v4.sport = 0;
594 : : tuple.v4.sport = rte_cpu_to_be_16(port_value);
595 : : tuple.v4.dport = 0;
596 : 1 : tuple.v4.sctp_tag = rte_be_to_cpu_32(tuple.v4.sctp_tag);
597 : :
598 : 1 : orig_hash = rte_softrss((uint32_t *)&tuple,
599 : : RTE_THASH_V4_L4_LEN, new_key);
600 : 1 : adj = rte_thash_get_complement(h, orig_hash, desired_value);
601 : :
602 [ + - ]: 1 : tuple.v4.sctp_tag = rte_cpu_to_be_32(tuple.v4.sctp_tag);
603 [ - + ]: 1 : tuple.v4.sport ^= rte_cpu_to_be_16(adj);
604 [ - + ]: 1 : tuple.v4.sctp_tag = rte_be_to_cpu_32(tuple.v4.sctp_tag);
605 : :
606 : 1 : adj_hash = rte_softrss((uint32_t *)&tuple,
607 : : RTE_THASH_V4_L4_LEN, new_key);
608 [ - + ]: 1 : RTE_TEST_ASSERT((adj_hash & HASH_MSK(reta_sz)) ==
609 : : desired_value, "bad desired value\n");
610 : :
611 : 1 : rte_thash_free_ctx(ctx);
612 : :
613 : 1 : return TEST_SUCCESS;
614 : : }
615 : :
616 : : /*
617 : : * This test creates 7 subranges in the following order:
618 : : * range_one = [56, 95), len = 8, offset = 56
619 : : * range_two = [64, 103), len = 8, offset = 64
620 : : * range_three = [120, 159), len = 8, offset = 120
621 : : * range_four = [48, 87), len = 8, offset = 48
622 : : * range_five = [57, 95), len = 7, offset = 57
623 : : * range_six = [40, 111), len = 40, offset = 40
624 : : * range_seven = [0, 39), len = 8, offset = 0
625 : : */
626 : : struct range {
627 : : const char *name;
628 : : int len;
629 : : int offset;
630 : : int byte_idx;
631 : : };
632 : :
633 : : struct range rng_arr[] = {
634 : : {"one", 8, 56, 7},
635 : : {"two", 8, 64, 8},
636 : : {"three", 8, 120, 15},
637 : : {"four", 8, 48, 6},
638 : : {"six", 40, 40, 9},
639 : : {"five", 7, 57, 7},
640 : : {"seven", 8, 0, 0}
641 : : };
642 : :
643 : : static int
644 : 1 : test_predictable_rss_multirange(void)
645 : : {
646 : : struct rte_thash_ctx *ctx;
647 : : struct rte_thash_subtuple_helper *h[RTE_DIM(rng_arr)];
648 : : const uint8_t *new_key;
649 : : const int key_len = 40;
650 : : int reta_sz = 7;
651 : : unsigned int i, j, k;
652 : : int ret;
653 : 1 : uint32_t desired_value = rte_rand() & HASH_MSK(reta_sz);
654 : 1 : uint8_t tuples[RTE_DIM(rng_arr)][16] = { {0} };
655 : : uint32_t *ptr;
656 : : uint32_t hashes[RTE_DIM(rng_arr)];
657 : : uint32_t adj_hashes[RTE_DIM(rng_arr)];
658 : : uint32_t adj;
659 : :
660 : 1 : ctx = rte_thash_init_ctx("test", key_len, reta_sz, NULL, 0);
661 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
662 : :
663 [ + + ]: 8 : for (i = 0; i < RTE_DIM(rng_arr); i++) {
664 : 7 : ret = rte_thash_add_helper(ctx, rng_arr[i].name,
665 : 7 : rng_arr[i].len, rng_arr[i].offset);
666 [ - + ]: 7 : RTE_TEST_ASSERT(ret == 0, "can not add helper\n");
667 : :
668 : 7 : h[i] = rte_thash_get_helper(ctx, rng_arr[i].name);
669 [ - + ]: 7 : RTE_TEST_ASSERT(h[i] != NULL, "can not find helper\n");
670 : : }
671 : 1 : new_key = rte_thash_get_key(ctx);
672 : :
673 : : /*
674 : : * calculate hashes, complements, then adjust keys with
675 : : * complements and recalculate hashes
676 : : */
677 [ + + ]: 8 : for (i = 0; i < RTE_DIM(rng_arr); i++) {
678 [ + + ]: 707 : for (k = 0; k < 100; k++) {
679 : : /* init with random keys */
680 : 700 : ptr = (uint32_t *)&tuples[i][0];
681 [ + + ]: 3500 : for (j = 0; j < 4; j++)
682 : 2800 : ptr[j] = rte_rand();
683 : : /* convert keys from BE to CPU byte order */
684 [ + + ]: 3500 : for (j = 0; j < 4; j++)
685 [ - + ]: 5600 : ptr[j] = rte_be_to_cpu_32(ptr[j]);
686 : :
687 : 700 : hashes[i] = rte_softrss(ptr, 4, new_key);
688 : 700 : adj = rte_thash_get_complement(h[i], hashes[i],
689 : : desired_value);
690 : : /* convert back to BE to adjust the value */
691 [ + + ]: 3500 : for (j = 0; j < 4; j++)
692 [ - + ]: 5600 : ptr[j] = rte_cpu_to_be_32(ptr[j]);
693 : :
694 : 700 : tuples[i][rng_arr[i].byte_idx] ^= adj;
695 : :
696 [ + + ]: 3500 : for (j = 0; j < 4; j++)
697 [ - + ]: 5600 : ptr[j] = rte_be_to_cpu_32(ptr[j]);
698 : :
699 : 700 : adj_hashes[i] = rte_softrss(ptr, 4, new_key);
700 [ - + ]: 700 : RTE_TEST_ASSERT((adj_hashes[i] & HASH_MSK(reta_sz)) ==
701 : : desired_value,
702 : : "bad desired value for %d tuple\n", i);
703 : : }
704 : : }
705 : :
706 : 1 : rte_thash_free_ctx(ctx);
707 : :
708 : 1 : return TEST_SUCCESS;
709 : : }
710 : :
711 : : static int
712 : 3 : cmp_tuple_eq(void *userdata, uint8_t *tuple)
713 : : {
714 : 3 : return memcmp(userdata, tuple, TUPLE_SZ);
715 : : }
716 : :
717 : : static int
718 : 1 : test_adjust_tuple(void)
719 : : {
720 : : struct rte_thash_ctx *ctx;
721 : : struct rte_thash_subtuple_helper *h;
722 : : const int key_len = 40;
723 : : const uint8_t *new_key;
724 : : uint8_t tuple[TUPLE_SZ];
725 : : uint32_t tmp_tuple[TUPLE_SZ / sizeof(uint32_t)];
726 : : uint32_t tuple_copy[TUPLE_SZ / sizeof(uint32_t)];
727 : : uint32_t hash;
728 : : int reta_sz = CHAR_BIT;
729 : : int ret;
730 : 1 : unsigned int i, desired_value = rte_rand() & HASH_MSK(reta_sz);
731 : :
732 : : memset(tuple, 0xab, TUPLE_SZ);
733 : :
734 : 1 : ctx = rte_thash_init_ctx("test", key_len, reta_sz, NULL, 0);
735 [ - + ]: 1 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
736 : :
737 : : /*
738 : : * set offset to be in the middle of a byte
739 : : * set size of the subtuple to be 2 * rets_sz
740 : : * to have the room for random bits
741 : : */
742 : 1 : ret = rte_thash_add_helper(ctx, "test", reta_sz * 2,
743 : : (5 * CHAR_BIT) + 4);
744 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "can not add helper, ret %d\n", ret);
745 : :
746 : 1 : new_key = rte_thash_get_key(ctx);
747 : :
748 : 1 : h = rte_thash_get_helper(ctx, "test");
749 [ - + ]: 1 : RTE_TEST_ASSERT(h != NULL, "can not find helper\n");
750 : :
751 : 1 : ret = rte_thash_adjust_tuple(ctx, h, tuple, TUPLE_SZ, desired_value,
752 : : 1, NULL, NULL);
753 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "can not adjust tuple, ret %d\n", ret);
754 : :
755 [ + + ]: 4 : for (i = 0; i < (TUPLE_SZ / 4); i++)
756 : 3 : tmp_tuple[i] =
757 [ - + ]: 3 : rte_be_to_cpu_32(*(uint32_t *)&tuple[i * 4]);
758 : :
759 : 1 : hash = rte_softrss(tmp_tuple, TUPLE_SZ / 4, new_key);
760 [ - + ]: 1 : RTE_TEST_ASSERT((hash & HASH_MSK(reta_sz)) ==
761 : : desired_value, "bad desired value\n");
762 : :
763 : :
764 : : /* Pass previously calculated tuple to callback function */
765 : : memcpy(tuple_copy, tuple, TUPLE_SZ);
766 : :
767 : : memset(tuple, 0xab, TUPLE_SZ);
768 : 1 : ret = rte_thash_adjust_tuple(ctx, h, tuple, TUPLE_SZ, desired_value,
769 : : 1, cmp_tuple_eq, tuple_copy);
770 [ - + ]: 1 : RTE_TEST_ASSERT(ret == -EEXIST,
771 : : "adjust tuple didn't indicate collision\n");
772 : :
773 : : /*
774 : : * Make the function to generate random bits into subtuple
775 : : * after first adjustment attempt.
776 : : */
777 : : memset(tuple, 0xab, TUPLE_SZ);
778 : 1 : ret = rte_thash_adjust_tuple(ctx, h, tuple, TUPLE_SZ, desired_value,
779 : : 2, cmp_tuple_eq, tuple_copy);
780 [ - + ]: 1 : RTE_TEST_ASSERT(ret == 0, "can not adjust tuple, ret %d\n", ret);
781 : :
782 [ + + ]: 4 : for (i = 0; i < (TUPLE_SZ / 4); i++)
783 : 3 : tmp_tuple[i] =
784 [ - + ]: 3 : rte_be_to_cpu_32(*(uint32_t *)&tuple[i * 4]);
785 : :
786 : 1 : hash = rte_softrss(tmp_tuple, TUPLE_SZ / 4, new_key);
787 [ - + ]: 1 : RTE_TEST_ASSERT((hash & HASH_MSK(reta_sz)) ==
788 : : desired_value, "bad desired value\n");
789 : :
790 : 1 : rte_thash_free_ctx(ctx);
791 : :
792 : 1 : return TEST_SUCCESS;
793 : : }
794 : :
795 : : static uint32_t
796 : 180 : calc_tuple_hash(const uint8_t tuple[TUPLE_SZ], const uint8_t *key)
797 : : {
798 : : uint32_t i, hash;
799 : : uint32_t tmp[TUPLE_SZ / sizeof(uint32_t)];
800 : :
801 [ + + ]: 720 : for (i = 0; i < RTE_DIM(tmp); i++)
802 [ - + ]: 1080 : tmp[i] = rte_be_to_cpu_32(
803 : : *(const uint32_t *)&tuple[i * sizeof(uint32_t)]);
804 : :
805 : 180 : hash = rte_softrss(tmp, RTE_DIM(tmp), key);
806 : 180 : return hash;
807 : : }
808 : :
809 : : static int
810 : 120 : check_adj_tuple(const uint8_t tuple[TUPLE_SZ], const uint8_t *key,
811 : : uint32_t dhv, uint32_t ohv, uint32_t adjust, uint32_t reta_sz,
812 : : const char *prefix)
813 : : {
814 : : uint32_t hash, hashlsb;
815 : :
816 : 120 : hash = calc_tuple_hash(tuple, key);
817 : 120 : hashlsb = hash & HASH_MSK(reta_sz);
818 : :
819 : : printf("%s(%s) for tuple:\n", __func__, prefix);
820 : 120 : rte_memdump(stdout, NULL, tuple, TUPLE_SZ);
821 : : printf("\treta_sz: %u,\n"
822 : : "\torig hash: %#x,\n"
823 : : "\tdesired: %#x,\n"
824 : : "\tadjust: %#x,\n"
825 : : "\tactual: %#x,\n",
826 : : reta_sz, ohv, dhv, adjust, hashlsb);
827 : :
828 [ + - ]: 120 : if (dhv == hashlsb) {
829 : : printf("\t***Succeeded\n");
830 : 120 : return 0;
831 : : }
832 : :
833 : : printf("\t***Failed\n");
834 : 0 : return -1;
835 : : }
836 : :
837 : : static int
838 : 60 : test_adjust_tuple_mb(uint32_t reta_sz, uint32_t bofs)
839 : : {
840 : : struct rte_thash_ctx *ctx;
841 : : struct rte_thash_subtuple_helper *h;
842 : : const int key_len = 40;
843 : : const uint8_t *new_key;
844 : : uint8_t orig_tuple[TUPLE_SZ];
845 : : uint8_t tuple_1[TUPLE_SZ];
846 : : uint8_t tuple_2[TUPLE_SZ];
847 : : uint32_t orig_hash;
848 : : int rc, ret;
849 : : uint32_t adj_bits;
850 : 60 : unsigned int random = rte_rand();
851 : 60 : unsigned int desired_value = random & HASH_MSK(reta_sz);
852 : :
853 : : const uint32_t h_offset = offsetof(union rte_thash_tuple, v4.dport) * CHAR_BIT;
854 : 60 : const uint32_t h_size = sizeof(uint16_t) * CHAR_BIT - bofs;
855 : :
856 : : printf("===%s(reta_sz=%u,bofs=%u)===\n", __func__, reta_sz, bofs);
857 : :
858 : : memset(orig_tuple, 0xab, sizeof(orig_tuple));
859 : :
860 : 60 : ctx = rte_thash_init_ctx("test", key_len, reta_sz, NULL, 0);
861 [ - + ]: 60 : RTE_TEST_ASSERT(ctx != NULL, "can not create thash ctx\n");
862 : :
863 : 60 : ret = rte_thash_add_helper(ctx, "test", h_size, h_offset);
864 [ - + ]: 60 : RTE_TEST_ASSERT(ret == 0, "can not add helper, ret %d\n", ret);
865 : :
866 : 60 : new_key = rte_thash_get_key(ctx);
867 : :
868 : 60 : h = rte_thash_get_helper(ctx, "test");
869 : :
870 : 60 : orig_hash = calc_tuple_hash(orig_tuple, new_key);
871 : :
872 : 60 : adj_bits = rte_thash_get_complement(h, orig_hash, desired_value);
873 : :
874 : : /* use method #1, update tuple manually */
875 : : memcpy(tuple_1, orig_tuple, sizeof(tuple_1));
876 : : {
877 : : uint16_t nv, ov, *p;
878 : :
879 : : p = (uint16_t *)(tuple_1 + h_offset / CHAR_BIT);
880 : 60 : ov = p[0];
881 [ - + ]: 60 : nv = ov ^ rte_cpu_to_be_16(adj_bits << bofs);
882 : 60 : printf("%s#%d: ov=%#hx, nv=%#hx, adj=%#x;\n",
883 : : __func__, __LINE__, ov, nv, adj_bits);
884 : 60 : p[0] = nv;
885 : : }
886 : :
887 : 60 : rc = check_adj_tuple(tuple_1, new_key, desired_value, orig_hash,
888 : : adj_bits, reta_sz, "method #1");
889 : : if (h_offset % CHAR_BIT == 0)
890 : 60 : ret |= rc;
891 : :
892 : : /* use method #2, use library function to adjust tuple */
893 : : memcpy(tuple_2, orig_tuple, sizeof(tuple_2));
894 : :
895 : 60 : rte_thash_adjust_tuple(ctx, h, tuple_2, sizeof(tuple_2),
896 : : desired_value, 1, NULL, NULL);
897 : 60 : ret |= check_adj_tuple(tuple_2, new_key, desired_value, orig_hash,
898 : : adj_bits, reta_sz, "method #2");
899 : :
900 : 60 : rte_thash_free_ctx(ctx);
901 : :
902 : 60 : ret |= memcmp(tuple_1, tuple_2, sizeof(tuple_1));
903 : :
904 : : printf("%s EXIT=======\n", __func__);
905 : 60 : return ret;
906 : : }
907 : :
908 : : static int
909 : 1 : test_adjust_tuple_mult_reta(void)
910 : : {
911 : : uint32_t i, j, np, nt;
912 : :
913 : : nt = 0, np = 0;
914 [ + + ]: 9 : for (i = 0; i < CHAR_BIT; i++) {
915 [ + + ]: 68 : for (j = 6; j <= RTE_THASH_RETA_SZ_MAX - i; j++) {
916 : 60 : np += (test_adjust_tuple_mb(j, i) == 0);
917 : 60 : nt++;
918 : : }
919 : : }
920 : :
921 : : printf("%s: tests executed: %u, test passed: %u\n", __func__, nt, np);
922 [ - + ]: 1 : RTE_TEST_ASSERT(nt == np, "%u subtests failed", nt - np);
923 : : return TEST_SUCCESS;
924 : : }
925 : :
926 : : #define RETA_SZ_LOG 11
927 : : #define RSS_KEY_SZ 40
928 : : #define RETA_SZ (1 << RETA_SZ_LOG)
929 : : #define NB_HASH_ITER RETA_SZ
930 : : #define NB_TEST_ITER 10
931 : :
932 : : static inline void
933 : 30 : run_hash_calc_loop(uint8_t *key, union rte_thash_tuple *tuple,
934 : : unsigned int *rss_reta_hits)
935 : : {
936 : : uint32_t rss_hash;
937 : : int i;
938 : :
939 [ + + ]: 61470 : for (i = 0; i < NB_HASH_ITER; i++) {
940 : : /* variable part starts from the most significant bit */
941 : 61440 : tuple->v4.dport = (i << (sizeof(tuple->v4.dport) * CHAR_BIT -
942 : : RETA_SZ_LOG));
943 : : /*
944 : : * swap sport and dport on LE arch since rte_softrss()
945 : : * works with host byte order uint32_t values
946 : : */
947 [ - + ]: 61440 : tuple->v4.dport = rte_cpu_to_be_16(tuple->v4.dport);
948 [ - + ]: 61440 : tuple->v4.sctp_tag = rte_be_to_cpu_32(tuple->v4.sctp_tag);
949 : 61440 : rss_hash = rte_softrss((uint32_t *)tuple,
950 : : RTE_THASH_V4_L4_LEN, key);
951 : : /* unroll swap, required only for sport */
952 [ - + ]: 61440 : tuple->v4.sctp_tag = rte_cpu_to_be_32(tuple->v4.sctp_tag);
953 : 61440 : rss_reta_hits[rss_hash & (RETA_SZ - 1)]++;
954 : : }
955 : 30 : }
956 : :
957 : : static int
958 : 10 : hash_calc_iteration(unsigned int *min_before, unsigned int *max_before,
959 : : unsigned int *min_after, unsigned int *max_after,
960 : : unsigned int *min_default, unsigned int *max_default)
961 : : {
962 : 10 : uint8_t key[RSS_KEY_SZ] = {0};
963 : : union rte_thash_tuple tuple;
964 : 10 : unsigned int rss_reta_hits_before_adjust[RETA_SZ] = {0};
965 : 10 : unsigned int rss_reta_hits_after_adjust[RETA_SZ] = {0};
966 : 10 : unsigned int rss_reta_hits_default_key[RETA_SZ] = {0};
967 : : int i;
968 : :
969 [ + + ]: 410 : for (i = 0; i < RSS_KEY_SZ; i++)
970 : 400 : key[i] = rte_rand();
971 : :
972 : 10 : tuple.v4.src_addr = rte_rand();
973 : 10 : tuple.v4.dst_addr = rte_rand();
974 : 10 : tuple.v4.sport = rte_rand();
975 : :
976 : 10 : run_hash_calc_loop(key, &tuple, rss_reta_hits_before_adjust);
977 : :
978 : 10 : int ret = rte_thash_gen_key(key, RSS_KEY_SZ, RETA_SZ_LOG,
979 : : offsetof(union rte_thash_tuple, v4.dport)*CHAR_BIT,
980 : : RETA_SZ_LOG);
981 : :
982 [ - + ]: 10 : if (ret) {
983 : : printf("Can't generate key\n");
984 : 0 : return -1;
985 : : }
986 : :
987 : 10 : run_hash_calc_loop(key, &tuple, rss_reta_hits_after_adjust);
988 : :
989 : 10 : run_hash_calc_loop(default_rss_key, &tuple, rss_reta_hits_default_key);
990 : :
991 [ + + ]: 20490 : for (i = 0; i < RETA_SZ; i++) {
992 : 20480 : *min_before = RTE_MIN(*min_before, rss_reta_hits_before_adjust[i]);
993 : 20480 : *max_before = RTE_MAX(*max_before, rss_reta_hits_before_adjust[i]);
994 : 20480 : *min_after = RTE_MIN(*min_after, rss_reta_hits_after_adjust[i]);
995 : 20480 : *max_after = RTE_MAX(*max_after, rss_reta_hits_after_adjust[i]);
996 : 20480 : *min_default = RTE_MIN(*min_default, rss_reta_hits_default_key[i]);
997 : 20480 : *max_default = RTE_MAX(*max_default, rss_reta_hits_default_key[i]);
998 : : }
999 : :
1000 : : return 0;
1001 : : }
1002 : :
1003 : : static int
1004 : 1 : test_keygen(void)
1005 : : {
1006 : : int i, ret;
1007 : 1 : unsigned int min_before = UINT32_MAX;
1008 : 1 : unsigned int min_after = UINT32_MAX;
1009 : 1 : unsigned int min_default = UINT32_MAX;
1010 : 1 : unsigned int max_before = 0;
1011 : 1 : unsigned int max_after = 0;
1012 : 1 : unsigned int max_default = 0;
1013 : :
1014 [ + + ]: 11 : for (i = 0; i < NB_TEST_ITER; i++) {
1015 : : /* calculates the worst distribution for each key */
1016 : 10 : ret = hash_calc_iteration(&min_before, &max_before, &min_after,
1017 : : &max_after, &min_default, &max_default);
1018 [ - + ]: 10 : if (ret)
1019 : 0 : return ret;
1020 : : }
1021 : :
1022 : 1 : printf("RSS before key adjustment: min=%d, max=%d\n",
1023 : : min_before, max_before);
1024 : 1 : printf("RSS after key adjustment: min=%d, max=%d\n",
1025 : : min_after, max_after);
1026 : 1 : printf("RSS default key: min=%d, max=%d\n",
1027 : : min_default, max_default);
1028 : :
1029 : 1 : return TEST_SUCCESS;
1030 : : }
1031 : :
1032 : : static struct unit_test_suite thash_tests = {
1033 : : .suite_name = "thash autotest",
1034 : : .setup = NULL,
1035 : : .teardown = NULL,
1036 : : .unit_test_cases = {
1037 : : TEST_CASE(test_toeplitz_hash_calc),
1038 : : TEST_CASE(test_toeplitz_hash_gfni),
1039 : : TEST_CASE(test_toeplitz_hash_rand_data),
1040 : : TEST_CASE(test_toeplitz_hash_gfni_bulk),
1041 : : TEST_CASE(test_big_tuple_gfni),
1042 : : TEST_CASE(test_create_invalid),
1043 : : TEST_CASE(test_multiple_create),
1044 : : TEST_CASE(test_free_null),
1045 : : TEST_CASE(test_add_invalid_helper),
1046 : : TEST_CASE(test_find_existing),
1047 : : TEST_CASE(test_get_helper),
1048 : : TEST_CASE(test_period_overflow),
1049 : : TEST_CASE(test_predictable_rss_min_seq),
1050 : : TEST_CASE(test_predictable_rss_multirange),
1051 : : TEST_CASE(test_adjust_tuple),
1052 : : TEST_CASE(test_adjust_tuple_mult_reta),
1053 : : TEST_CASE(test_keygen),
1054 : : TEST_CASES_END()
1055 : : }
1056 : : };
1057 : :
1058 : : static int
1059 : 1 : test_thash(void)
1060 : : {
1061 : 1 : return unit_test_suite_runner(&thash_tests);
1062 : : }
1063 : :
1064 : 252 : REGISTER_FAST_TEST(thash_autotest, true, true, test_thash);
|