]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/app/test/test_hash.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / dpdk / app / test / test_hash.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <errno.h>
11 #include <sys/queue.h>
12
13 #include <rte_common.h>
14 #include <rte_malloc.h>
15 #include <rte_cycles.h>
16 #include <rte_random.h>
17 #include <rte_memory.h>
18 #include <rte_eal.h>
19 #include <rte_ip.h>
20 #include <rte_string_fns.h>
21
22 #include "test.h"
23
24 #include <rte_hash.h>
25 #include <rte_fbk_hash.h>
26 #include <rte_jhash.h>
27 #include <rte_hash_crc.h>
28
29 /*******************************************************************************
30 * Hash function performance test configuration section. Each performance test
31 * will be performed HASHTEST_ITERATIONS times.
32 *
33 * The five arrays below control what tests are performed. Every combination
34 * from the array entries is tested.
35 */
36 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
37 static uint32_t hashtest_initvals[] = {0};
38 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
39 #define MAX_KEYSIZE 64
40 /******************************************************************************/
41 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
42
43 /*
44 * Check condition and return an error if true. Assumes that "handle" is the
45 * name of the hash structure pointer to be freed.
46 */
47 #define RETURN_IF_ERROR(cond, str, ...) do { \
48 if (cond) { \
49 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
50 if (handle) rte_hash_free(handle); \
51 return -1; \
52 } \
53 } while(0)
54
55 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
56 if (cond) { \
57 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
58 if (handle) rte_fbk_hash_free(handle); \
59 return -1; \
60 } \
61 } while(0)
62
63 /* 5-tuple key type */
64 struct flow_key {
65 uint32_t ip_src;
66 uint32_t ip_dst;
67 uint16_t port_src;
68 uint16_t port_dst;
69 uint8_t proto;
70 } __rte_packed;
71
72 int hash_logtype_test;
73
74 /*
75 * Hash function that always returns the same value, to easily test what
76 * happens when a bucket is full.
77 */
78 static uint32_t pseudo_hash(__rte_unused const void *keys,
79 __rte_unused uint32_t key_len,
80 __rte_unused uint32_t init_val)
81 {
82 return 3;
83 }
84
85 RTE_INIT(test_hash_init_log)
86 {
87 hash_logtype_test = rte_log_register("test.hash");
88 }
89
90 /*
91 * Print out result of unit test hash operation.
92 */
93 static void print_key_info(const char *msg, const struct flow_key *key,
94 int32_t pos)
95 {
96 const uint8_t *p = (const uint8_t *)key;
97 unsigned int i;
98
99 rte_log(RTE_LOG_DEBUG, hash_logtype_test, "%s key:0x", msg);
100 for (i = 0; i < sizeof(struct flow_key); i++)
101 rte_log(RTE_LOG_DEBUG, hash_logtype_test, "%02X", p[i]);
102 rte_log(RTE_LOG_DEBUG, hash_logtype_test, " @ pos %d\n", pos);
103 }
104
105 /* Keys used by unit test functions */
106 static struct flow_key keys[5] = { {
107 .ip_src = RTE_IPV4(0x03, 0x02, 0x01, 0x00),
108 .ip_dst = RTE_IPV4(0x07, 0x06, 0x05, 0x04),
109 .port_src = 0x0908,
110 .port_dst = 0x0b0a,
111 .proto = 0x0c,
112 }, {
113 .ip_src = RTE_IPV4(0x13, 0x12, 0x11, 0x10),
114 .ip_dst = RTE_IPV4(0x17, 0x16, 0x15, 0x14),
115 .port_src = 0x1918,
116 .port_dst = 0x1b1a,
117 .proto = 0x1c,
118 }, {
119 .ip_src = RTE_IPV4(0x23, 0x22, 0x21, 0x20),
120 .ip_dst = RTE_IPV4(0x27, 0x26, 0x25, 0x24),
121 .port_src = 0x2928,
122 .port_dst = 0x2b2a,
123 .proto = 0x2c,
124 }, {
125 .ip_src = RTE_IPV4(0x33, 0x32, 0x31, 0x30),
126 .ip_dst = RTE_IPV4(0x37, 0x36, 0x35, 0x34),
127 .port_src = 0x3938,
128 .port_dst = 0x3b3a,
129 .proto = 0x3c,
130 }, {
131 .ip_src = RTE_IPV4(0x43, 0x42, 0x41, 0x40),
132 .ip_dst = RTE_IPV4(0x47, 0x46, 0x45, 0x44),
133 .port_src = 0x4948,
134 .port_dst = 0x4b4a,
135 .proto = 0x4c,
136 } };
137
138 /* Parameters used for hash table in unit test functions. Name set later. */
139 static struct rte_hash_parameters ut_params = {
140 .entries = 64,
141 .key_len = sizeof(struct flow_key), /* 13 */
142 .hash_func = rte_jhash,
143 .hash_func_init_val = 0,
144 .socket_id = 0,
145 };
146
147 #define CRC32_ITERATIONS (1U << 10)
148 #define CRC32_DWORDS (1U << 6)
149 /*
150 * Test if all CRC32 implementations yield the same hash value
151 */
152 static int
153 test_crc32_hash_alg_equiv(void)
154 {
155 uint32_t hash_val;
156 uint32_t init_val;
157 uint64_t data64[CRC32_DWORDS];
158 unsigned i, j;
159 size_t data_len;
160
161 printf("\n# CRC32 implementations equivalence test\n");
162 for (i = 0; i < CRC32_ITERATIONS; i++) {
163 /* Randomizing data_len of data set */
164 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
165 init_val = (uint32_t) rte_rand();
166
167 /* Fill the data set */
168 for (j = 0; j < CRC32_DWORDS; j++)
169 data64[j] = rte_rand();
170
171 /* Calculate software CRC32 */
172 rte_hash_crc_set_alg(CRC32_SW);
173 hash_val = rte_hash_crc(data64, data_len, init_val);
174
175 /* Check against 4-byte-operand sse4.2 CRC32 if available */
176 rte_hash_crc_set_alg(CRC32_SSE42);
177 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
178 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
179 break;
180 }
181
182 /* Check against 8-byte-operand sse4.2 CRC32 if available */
183 rte_hash_crc_set_alg(CRC32_SSE42_x64);
184 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
185 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
186 break;
187 }
188
189 /* Check against 8-byte-operand ARM64 CRC32 if available */
190 rte_hash_crc_set_alg(CRC32_ARM64);
191 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
192 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
193 break;
194 }
195 }
196
197 /* Resetting to best available algorithm */
198 rte_hash_crc_set_alg(CRC32_SSE42_x64);
199
200 if (i == CRC32_ITERATIONS)
201 return 0;
202
203 printf("Failed test data (hex, %zu bytes total):\n", data_len);
204 for (j = 0; j < data_len; j++)
205 printf("%02X%c", ((uint8_t *)data64)[j],
206 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
207
208 return -1;
209 }
210
211 /*
212 * Test a hash function.
213 */
214 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
215 uint32_t key_len)
216 {
217 static uint8_t key[MAX_KEYSIZE];
218 unsigned i;
219
220
221 for (i = 0; i < key_len; i++)
222 key[i] = (uint8_t) rte_rand();
223
224 /* just to be on the safe side */
225 if (!f)
226 return;
227
228 f(key, key_len, init_val);
229 }
230
231 /*
232 * Test all hash functions.
233 */
234 static void run_hash_func_tests(void)
235 {
236 unsigned i, j, k;
237
238 for (i = 0; i < RTE_DIM(hashtest_funcs); i++) {
239 for (j = 0; j < RTE_DIM(hashtest_initvals); j++) {
240 for (k = 0; k < RTE_DIM(hashtest_key_lens); k++) {
241 run_hash_func_test(hashtest_funcs[i],
242 hashtest_initvals[j],
243 hashtest_key_lens[k]);
244 }
245 }
246 }
247 }
248
249 /*
250 * Basic sequence of operations for a single key:
251 * - add
252 * - lookup (hit)
253 * - delete
254 * - lookup (miss)
255 *
256 * Repeat the test case when 'free on delete' is disabled.
257 * - add
258 * - lookup (hit)
259 * - delete
260 * - lookup (miss)
261 * - free
262 */
263 static int test_add_delete(void)
264 {
265 struct rte_hash *handle;
266 /* test with standard add/lookup/delete functions */
267 int pos0, expectedPos0;
268
269 ut_params.name = "test1";
270 handle = rte_hash_create(&ut_params);
271 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
272
273 pos0 = rte_hash_add_key(handle, &keys[0]);
274 print_key_info("Add", &keys[0], pos0);
275 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
276 expectedPos0 = pos0;
277
278 pos0 = rte_hash_lookup(handle, &keys[0]);
279 print_key_info("Lkp", &keys[0], pos0);
280 RETURN_IF_ERROR(pos0 != expectedPos0,
281 "failed to find key (pos0=%d)", pos0);
282
283 pos0 = rte_hash_del_key(handle, &keys[0]);
284 print_key_info("Del", &keys[0], pos0);
285 RETURN_IF_ERROR(pos0 != expectedPos0,
286 "failed to delete key (pos0=%d)", pos0);
287
288 pos0 = rte_hash_lookup(handle, &keys[0]);
289 print_key_info("Lkp", &keys[0], pos0);
290 RETURN_IF_ERROR(pos0 != -ENOENT,
291 "fail: found key after deleting! (pos0=%d)", pos0);
292
293 rte_hash_free(handle);
294
295 /* repeat test with precomputed hash functions */
296 hash_sig_t hash_value;
297 int pos1, expectedPos1, delPos1;
298
299 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
300 handle = rte_hash_create(&ut_params);
301 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
302 ut_params.extra_flag = 0;
303
304 hash_value = rte_hash_hash(handle, &keys[0]);
305 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
306 print_key_info("Add", &keys[0], pos1);
307 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
308 expectedPos1 = pos1;
309
310 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
311 print_key_info("Lkp", &keys[0], pos1);
312 RETURN_IF_ERROR(pos1 != expectedPos1,
313 "failed to find key (pos1=%d)", pos1);
314
315 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
316 print_key_info("Del", &keys[0], pos1);
317 RETURN_IF_ERROR(pos1 != expectedPos1,
318 "failed to delete key (pos1=%d)", pos1);
319 delPos1 = pos1;
320
321 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
322 print_key_info("Lkp", &keys[0], pos1);
323 RETURN_IF_ERROR(pos1 != -ENOENT,
324 "fail: found key after deleting! (pos1=%d)", pos1);
325
326 pos1 = rte_hash_free_key_with_position(handle, delPos1);
327 print_key_info("Free", &keys[0], delPos1);
328 RETURN_IF_ERROR(pos1 != 0,
329 "failed to free key (pos1=%d)", delPos1);
330
331 rte_hash_free(handle);
332
333 return 0;
334 }
335
336 /*
337 * Sequence of operations for a single key:
338 * - delete: miss
339 * - add
340 * - lookup: hit
341 * - add: update
342 * - lookup: hit (updated data)
343 * - delete: hit
344 * - delete: miss
345 * - lookup: miss
346 */
347 static int test_add_update_delete(void)
348 {
349 struct rte_hash *handle;
350 int pos0, expectedPos0;
351
352 ut_params.name = "test2";
353 handle = rte_hash_create(&ut_params);
354 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
355
356 pos0 = rte_hash_del_key(handle, &keys[0]);
357 print_key_info("Del", &keys[0], pos0);
358 RETURN_IF_ERROR(pos0 != -ENOENT,
359 "fail: found non-existent key (pos0=%d)", pos0);
360
361 pos0 = rte_hash_add_key(handle, &keys[0]);
362 print_key_info("Add", &keys[0], pos0);
363 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
364 expectedPos0 = pos0;
365
366 pos0 = rte_hash_lookup(handle, &keys[0]);
367 print_key_info("Lkp", &keys[0], pos0);
368 RETURN_IF_ERROR(pos0 != expectedPos0,
369 "failed to find key (pos0=%d)", pos0);
370
371 pos0 = rte_hash_add_key(handle, &keys[0]);
372 print_key_info("Add", &keys[0], pos0);
373 RETURN_IF_ERROR(pos0 != expectedPos0,
374 "failed to re-add key (pos0=%d)", pos0);
375
376 pos0 = rte_hash_lookup(handle, &keys[0]);
377 print_key_info("Lkp", &keys[0], pos0);
378 RETURN_IF_ERROR(pos0 != expectedPos0,
379 "failed to find key (pos0=%d)", pos0);
380
381 pos0 = rte_hash_del_key(handle, &keys[0]);
382 print_key_info("Del", &keys[0], pos0);
383 RETURN_IF_ERROR(pos0 != expectedPos0,
384 "failed to delete key (pos0=%d)", pos0);
385
386 pos0 = rte_hash_del_key(handle, &keys[0]);
387 print_key_info("Del", &keys[0], pos0);
388 RETURN_IF_ERROR(pos0 != -ENOENT,
389 "fail: deleted already deleted key (pos0=%d)", pos0);
390
391 pos0 = rte_hash_lookup(handle, &keys[0]);
392 print_key_info("Lkp", &keys[0], pos0);
393 RETURN_IF_ERROR(pos0 != -ENOENT,
394 "fail: found key after deleting! (pos0=%d)", pos0);
395
396 rte_hash_free(handle);
397 return 0;
398 }
399
400 /*
401 * Sequence of operations for a single key with 'disable free on del' set:
402 * - delete: miss
403 * - add
404 * - lookup: hit
405 * - add: update
406 * - lookup: hit (updated data)
407 * - delete: hit
408 * - delete: miss
409 * - lookup: miss
410 * - free: hit
411 * - lookup: miss
412 */
413 static int test_add_update_delete_free(void)
414 {
415 struct rte_hash *handle;
416 int pos0, expectedPos0, delPos0, result;
417
418 ut_params.name = "test2";
419 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
420 handle = rte_hash_create(&ut_params);
421 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
422 ut_params.extra_flag = 0;
423
424 pos0 = rte_hash_del_key(handle, &keys[0]);
425 print_key_info("Del", &keys[0], pos0);
426 RETURN_IF_ERROR(pos0 != -ENOENT,
427 "fail: found non-existent key (pos0=%d)", pos0);
428
429 pos0 = rte_hash_add_key(handle, &keys[0]);
430 print_key_info("Add", &keys[0], pos0);
431 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
432 expectedPos0 = pos0;
433
434 pos0 = rte_hash_lookup(handle, &keys[0]);
435 print_key_info("Lkp", &keys[0], pos0);
436 RETURN_IF_ERROR(pos0 != expectedPos0,
437 "failed to find key (pos0=%d)", pos0);
438
439 pos0 = rte_hash_add_key(handle, &keys[0]);
440 print_key_info("Add", &keys[0], pos0);
441 RETURN_IF_ERROR(pos0 != expectedPos0,
442 "failed to re-add key (pos0=%d)", pos0);
443
444 pos0 = rte_hash_lookup(handle, &keys[0]);
445 print_key_info("Lkp", &keys[0], pos0);
446 RETURN_IF_ERROR(pos0 != expectedPos0,
447 "failed to find key (pos0=%d)", pos0);
448
449 delPos0 = rte_hash_del_key(handle, &keys[0]);
450 print_key_info("Del", &keys[0], delPos0);
451 RETURN_IF_ERROR(delPos0 != expectedPos0,
452 "failed to delete key (pos0=%d)", delPos0);
453
454 pos0 = rte_hash_del_key(handle, &keys[0]);
455 print_key_info("Del", &keys[0], pos0);
456 RETURN_IF_ERROR(pos0 != -ENOENT,
457 "fail: deleted already deleted key (pos0=%d)", pos0);
458
459 pos0 = rte_hash_lookup(handle, &keys[0]);
460 print_key_info("Lkp", &keys[0], pos0);
461 RETURN_IF_ERROR(pos0 != -ENOENT,
462 "fail: found key after deleting! (pos0=%d)", pos0);
463
464 result = rte_hash_free_key_with_position(handle, delPos0);
465 print_key_info("Free", &keys[0], delPos0);
466 RETURN_IF_ERROR(result != 0,
467 "failed to free key (pos1=%d)", delPos0);
468
469 pos0 = rte_hash_lookup(handle, &keys[0]);
470 print_key_info("Lkp", &keys[0], pos0);
471 RETURN_IF_ERROR(pos0 != -ENOENT,
472 "fail: found key after deleting! (pos0=%d)", pos0);
473
474 rte_hash_free(handle);
475 return 0;
476 }
477
478 /*
479 * Sequence of operations for a single key with 'rw concurrency lock free' set:
480 * - add
481 * - delete: hit
482 * - free: hit
483 * Repeat the test case when 'multi writer add' is enabled.
484 * - add
485 * - delete: hit
486 * - free: hit
487 */
488 static int test_add_delete_free_lf(void)
489 {
490 /* Should match the #define LCORE_CACHE_SIZE value in rte_cuckoo_hash.h */
491 #define LCORE_CACHE_SIZE 64
492 struct rte_hash *handle;
493 hash_sig_t hash_value;
494 int pos, expectedPos, delPos;
495 uint8_t extra_flag;
496 uint32_t i, ip_src;
497
498 extra_flag = ut_params.extra_flag;
499 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF;
500 handle = rte_hash_create(&ut_params);
501 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
502 ut_params.extra_flag = extra_flag;
503
504 /*
505 * The number of iterations is at least the same as the number of slots
506 * rte_hash allocates internally. This is to reveal potential issues of
507 * not freeing keys successfully.
508 */
509 for (i = 0; i < ut_params.entries + 1; i++) {
510 hash_value = rte_hash_hash(handle, &keys[0]);
511 pos = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
512 print_key_info("Add", &keys[0], pos);
513 RETURN_IF_ERROR(pos < 0, "failed to add key (pos=%d)", pos);
514 expectedPos = pos;
515
516 pos = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
517 print_key_info("Del", &keys[0], pos);
518 RETURN_IF_ERROR(pos != expectedPos,
519 "failed to delete key (pos=%d)", pos);
520 delPos = pos;
521
522 pos = rte_hash_free_key_with_position(handle, delPos);
523 print_key_info("Free", &keys[0], delPos);
524 RETURN_IF_ERROR(pos != 0,
525 "failed to free key (pos=%d)", delPos);
526 }
527
528 rte_hash_free(handle);
529
530 extra_flag = ut_params.extra_flag;
531 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF |
532 RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
533 handle = rte_hash_create(&ut_params);
534 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
535 ut_params.extra_flag = extra_flag;
536
537 ip_src = keys[0].ip_src;
538 /*
539 * The number of iterations is at least the same as the number of slots
540 * rte_hash allocates internally. This is to reveal potential issues of
541 * not freeing keys successfully.
542 */
543 for (i = 0; i < ut_params.entries + (RTE_MAX_LCORE - 1) *
544 (LCORE_CACHE_SIZE - 1) + 1; i++) {
545 keys[0].ip_src++;
546 hash_value = rte_hash_hash(handle, &keys[0]);
547 pos = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
548 print_key_info("Add", &keys[0], pos);
549 RETURN_IF_ERROR(pos < 0, "failed to add key (pos=%d)", pos);
550 expectedPos = pos;
551
552 pos = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
553 print_key_info("Del", &keys[0], pos);
554 RETURN_IF_ERROR(pos != expectedPos,
555 "failed to delete key (pos=%d)", pos);
556 delPos = pos;
557
558 pos = rte_hash_free_key_with_position(handle, delPos);
559 print_key_info("Free", &keys[0], delPos);
560 RETURN_IF_ERROR(pos != 0,
561 "failed to free key (pos=%d)", delPos);
562 }
563 keys[0].ip_src = ip_src;
564
565 rte_hash_free(handle);
566
567 return 0;
568 }
569
570 /*
571 * Sequence of operations for retrieving a key with its position
572 *
573 * - create table
574 * - add key
575 * - get the key with its position: hit
576 * - delete key
577 * - try to get the deleted key: miss
578 *
579 * Repeat the test case when 'free on delete' is disabled.
580 * - create table
581 * - add key
582 * - get the key with its position: hit
583 * - delete key
584 * - try to get the deleted key: hit
585 * - free key
586 * - try to get the deleted key: miss
587 *
588 */
589 static int test_hash_get_key_with_position(void)
590 {
591 struct rte_hash *handle = NULL;
592 int pos, expectedPos, delPos, result;
593 void *key;
594
595 ut_params.name = "hash_get_key_w_pos";
596 handle = rte_hash_create(&ut_params);
597 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
598
599 pos = rte_hash_add_key(handle, &keys[0]);
600 print_key_info("Add", &keys[0], pos);
601 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
602 expectedPos = pos;
603
604 result = rte_hash_get_key_with_position(handle, pos, &key);
605 RETURN_IF_ERROR(result != 0, "error retrieving a key");
606
607 pos = rte_hash_del_key(handle, &keys[0]);
608 print_key_info("Del", &keys[0], pos);
609 RETURN_IF_ERROR(pos != expectedPos,
610 "failed to delete key (pos0=%d)", pos);
611
612 result = rte_hash_get_key_with_position(handle, pos, &key);
613 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
614
615 rte_hash_free(handle);
616
617 ut_params.name = "hash_get_key_w_pos";
618 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
619 handle = rte_hash_create(&ut_params);
620 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
621 ut_params.extra_flag = 0;
622
623 pos = rte_hash_add_key(handle, &keys[0]);
624 print_key_info("Add", &keys[0], pos);
625 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
626 expectedPos = pos;
627
628 result = rte_hash_get_key_with_position(handle, pos, &key);
629 RETURN_IF_ERROR(result != 0, "error retrieving a key");
630
631 delPos = rte_hash_del_key(handle, &keys[0]);
632 print_key_info("Del", &keys[0], delPos);
633 RETURN_IF_ERROR(delPos != expectedPos,
634 "failed to delete key (pos0=%d)", delPos);
635
636 result = rte_hash_get_key_with_position(handle, delPos, &key);
637 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
638
639 result = rte_hash_free_key_with_position(handle, delPos);
640 print_key_info("Free", &keys[0], delPos);
641 RETURN_IF_ERROR(result != 0,
642 "failed to free key (pos1=%d)", delPos);
643
644 result = rte_hash_get_key_with_position(handle, delPos, &key);
645 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
646
647 rte_hash_free(handle);
648 return 0;
649 }
650
651 /*
652 * Sequence of operations for find existing hash table
653 *
654 * - create table
655 * - find existing table: hit
656 * - find non-existing table: miss
657 *
658 */
659 static int test_hash_find_existing(void)
660 {
661 struct rte_hash *handle = NULL, *result = NULL;
662
663 /* Create hash table. */
664 ut_params.name = "hash_find_existing";
665 handle = rte_hash_create(&ut_params);
666 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
667
668 /* Try to find existing hash table */
669 result = rte_hash_find_existing("hash_find_existing");
670 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
671
672 /* Try to find non-existing hash table */
673 result = rte_hash_find_existing("hash_find_non_existing");
674 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
675
676 /* Cleanup. */
677 rte_hash_free(handle);
678
679 return 0;
680 }
681
682 /*
683 * Sequence of operations for 5 keys
684 * - add keys
685 * - lookup keys: hit
686 * - add keys (update)
687 * - lookup keys: hit (updated data)
688 * - delete keys : hit
689 * - lookup keys: miss
690 */
691 static int test_five_keys(void)
692 {
693 struct rte_hash *handle;
694 const void *key_array[5] = {0};
695 int pos[5];
696 int expected_pos[5];
697 unsigned i;
698 int ret;
699
700 ut_params.name = "test3";
701 handle = rte_hash_create(&ut_params);
702 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
703
704 /* Add */
705 for (i = 0; i < 5; i++) {
706 pos[i] = rte_hash_add_key(handle, &keys[i]);
707 print_key_info("Add", &keys[i], pos[i]);
708 RETURN_IF_ERROR(pos[i] < 0,
709 "failed to add key (pos[%u]=%d)", i, pos[i]);
710 expected_pos[i] = pos[i];
711 }
712
713 /* Lookup */
714 for(i = 0; i < 5; i++)
715 key_array[i] = &keys[i];
716
717 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
718 if(ret == 0)
719 for(i = 0; i < 5; i++) {
720 print_key_info("Lkp", key_array[i], pos[i]);
721 RETURN_IF_ERROR(pos[i] != expected_pos[i],
722 "failed to find key (pos[%u]=%d)", i, pos[i]);
723 }
724
725 /* Add - update */
726 for (i = 0; i < 5; i++) {
727 pos[i] = rte_hash_add_key(handle, &keys[i]);
728 print_key_info("Add", &keys[i], pos[i]);
729 RETURN_IF_ERROR(pos[i] != expected_pos[i],
730 "failed to add key (pos[%u]=%d)", i, pos[i]);
731 }
732
733 /* Lookup */
734 for (i = 0; i < 5; i++) {
735 pos[i] = rte_hash_lookup(handle, &keys[i]);
736 print_key_info("Lkp", &keys[i], pos[i]);
737 RETURN_IF_ERROR(pos[i] != expected_pos[i],
738 "failed to find key (pos[%u]=%d)", i, pos[i]);
739 }
740
741 /* Delete */
742 for (i = 0; i < 5; i++) {
743 pos[i] = rte_hash_del_key(handle, &keys[i]);
744 print_key_info("Del", &keys[i], pos[i]);
745 RETURN_IF_ERROR(pos[i] != expected_pos[i],
746 "failed to delete key (pos[%u]=%d)", i, pos[i]);
747 }
748
749 /* Lookup */
750 for (i = 0; i < 5; i++) {
751 pos[i] = rte_hash_lookup(handle, &keys[i]);
752 print_key_info("Lkp", &keys[i], pos[i]);
753 RETURN_IF_ERROR(pos[i] != -ENOENT,
754 "found non-existent key (pos[%u]=%d)", i, pos[i]);
755 }
756
757 /* Lookup multi */
758 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
759 if (ret == 0)
760 for (i = 0; i < 5; i++) {
761 print_key_info("Lkp", key_array[i], pos[i]);
762 RETURN_IF_ERROR(pos[i] != -ENOENT,
763 "found not-existent key (pos[%u]=%d)", i, pos[i]);
764 }
765
766 rte_hash_free(handle);
767
768 return 0;
769 }
770
771 /*
772 * Add keys to the same bucket until bucket full.
773 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
774 * first 4 successful, 5th successful, pushing existing item in bucket
775 * - lookup the 5 keys: 5 hits
776 * - add the 5 keys again: 5 OK
777 * - lookup the 5 keys: 5 hits (updated data)
778 * - delete the 5 keys: 5 OK
779 * - lookup the 5 keys: 5 misses
780 */
781 static int test_full_bucket(void)
782 {
783 struct rte_hash_parameters params_pseudo_hash = {
784 .name = "test4",
785 .entries = 64,
786 .key_len = sizeof(struct flow_key), /* 13 */
787 .hash_func = pseudo_hash,
788 .hash_func_init_val = 0,
789 .socket_id = 0,
790 };
791 struct rte_hash *handle;
792 int pos[5];
793 int expected_pos[5];
794 unsigned i;
795
796 handle = rte_hash_create(&params_pseudo_hash);
797 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
798
799 /* Fill bucket */
800 for (i = 0; i < 4; i++) {
801 pos[i] = rte_hash_add_key(handle, &keys[i]);
802 print_key_info("Add", &keys[i], pos[i]);
803 RETURN_IF_ERROR(pos[i] < 0,
804 "failed to add key (pos[%u]=%d)", i, pos[i]);
805 expected_pos[i] = pos[i];
806 }
807 /*
808 * This should work and will push one of the items
809 * in the bucket because it is full
810 */
811 pos[4] = rte_hash_add_key(handle, &keys[4]);
812 print_key_info("Add", &keys[4], pos[4]);
813 RETURN_IF_ERROR(pos[4] < 0,
814 "failed to add key (pos[4]=%d)", pos[4]);
815 expected_pos[4] = pos[4];
816
817 /* Lookup */
818 for (i = 0; i < 5; i++) {
819 pos[i] = rte_hash_lookup(handle, &keys[i]);
820 print_key_info("Lkp", &keys[i], pos[i]);
821 RETURN_IF_ERROR(pos[i] != expected_pos[i],
822 "failed to find key (pos[%u]=%d)", i, pos[i]);
823 }
824
825 /* Add - update */
826 for (i = 0; i < 5; i++) {
827 pos[i] = rte_hash_add_key(handle, &keys[i]);
828 print_key_info("Add", &keys[i], pos[i]);
829 RETURN_IF_ERROR(pos[i] != expected_pos[i],
830 "failed to add key (pos[%u]=%d)", i, pos[i]);
831 }
832
833 /* Lookup */
834 for (i = 0; i < 5; i++) {
835 pos[i] = rte_hash_lookup(handle, &keys[i]);
836 print_key_info("Lkp", &keys[i], pos[i]);
837 RETURN_IF_ERROR(pos[i] != expected_pos[i],
838 "failed to find key (pos[%u]=%d)", i, pos[i]);
839 }
840
841 /* Delete 1 key, check other keys are still found */
842 pos[1] = rte_hash_del_key(handle, &keys[1]);
843 print_key_info("Del", &keys[1], pos[1]);
844 RETURN_IF_ERROR(pos[1] != expected_pos[1],
845 "failed to delete key (pos[1]=%d)", pos[1]);
846 pos[3] = rte_hash_lookup(handle, &keys[3]);
847 print_key_info("Lkp", &keys[3], pos[3]);
848 RETURN_IF_ERROR(pos[3] != expected_pos[3],
849 "failed lookup after deleting key from same bucket "
850 "(pos[3]=%d)", pos[3]);
851
852 /* Go back to previous state */
853 pos[1] = rte_hash_add_key(handle, &keys[1]);
854 print_key_info("Add", &keys[1], pos[1]);
855 expected_pos[1] = pos[1];
856 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
857
858 /* Delete */
859 for (i = 0; i < 5; i++) {
860 pos[i] = rte_hash_del_key(handle, &keys[i]);
861 print_key_info("Del", &keys[i], pos[i]);
862 RETURN_IF_ERROR(pos[i] != expected_pos[i],
863 "failed to delete key (pos[%u]=%d)", i, pos[i]);
864 }
865
866 /* Lookup */
867 for (i = 0; i < 5; i++) {
868 pos[i] = rte_hash_lookup(handle, &keys[i]);
869 print_key_info("Lkp", &keys[i], pos[i]);
870 RETURN_IF_ERROR(pos[i] != -ENOENT,
871 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
872 }
873
874 rte_hash_free(handle);
875
876 /* Cover the NULL case. */
877 rte_hash_free(0);
878 return 0;
879 }
880
881 /*
882 * Similar to the test above (full bucket test), but for extendable buckets.
883 */
884 static int test_extendable_bucket(void)
885 {
886 struct rte_hash_parameters params_pseudo_hash = {
887 .name = "test5",
888 .entries = 64,
889 .key_len = sizeof(struct flow_key), /* 13 */
890 .hash_func = pseudo_hash,
891 .hash_func_init_val = 0,
892 .socket_id = 0,
893 .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE
894 };
895 struct rte_hash *handle;
896 int pos[64];
897 int expected_pos[64];
898 unsigned int i;
899 struct flow_key rand_keys[64];
900
901 for (i = 0; i < 64; i++) {
902 rand_keys[i].port_dst = i;
903 rand_keys[i].port_src = i+1;
904 }
905
906 handle = rte_hash_create(&params_pseudo_hash);
907 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
908
909 /* Fill bucket */
910 for (i = 0; i < 64; i++) {
911 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
912 print_key_info("Add", &rand_keys[i], pos[i]);
913 RETURN_IF_ERROR(pos[i] < 0,
914 "failed to add key (pos[%u]=%d)", i, pos[i]);
915 expected_pos[i] = pos[i];
916 }
917
918 /* Lookup */
919 for (i = 0; i < 64; i++) {
920 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
921 print_key_info("Lkp", &rand_keys[i], pos[i]);
922 RETURN_IF_ERROR(pos[i] != expected_pos[i],
923 "failed to find key (pos[%u]=%d)", i, pos[i]);
924 }
925
926 /* Add - update */
927 for (i = 0; i < 64; i++) {
928 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
929 print_key_info("Add", &rand_keys[i], pos[i]);
930 RETURN_IF_ERROR(pos[i] != expected_pos[i],
931 "failed to add key (pos[%u]=%d)", i, pos[i]);
932 }
933
934 /* Lookup */
935 for (i = 0; i < 64; i++) {
936 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
937 print_key_info("Lkp", &rand_keys[i], pos[i]);
938 RETURN_IF_ERROR(pos[i] != expected_pos[i],
939 "failed to find key (pos[%u]=%d)", i, pos[i]);
940 }
941
942 /* Delete 1 key, check other keys are still found */
943 pos[35] = rte_hash_del_key(handle, &rand_keys[35]);
944 print_key_info("Del", &rand_keys[35], pos[35]);
945 RETURN_IF_ERROR(pos[35] != expected_pos[35],
946 "failed to delete key (pos[1]=%d)", pos[35]);
947 pos[20] = rte_hash_lookup(handle, &rand_keys[20]);
948 print_key_info("Lkp", &rand_keys[20], pos[20]);
949 RETURN_IF_ERROR(pos[20] != expected_pos[20],
950 "failed lookup after deleting key from same bucket "
951 "(pos[20]=%d)", pos[20]);
952
953 /* Go back to previous state */
954 pos[35] = rte_hash_add_key(handle, &rand_keys[35]);
955 print_key_info("Add", &rand_keys[35], pos[35]);
956 expected_pos[35] = pos[35];
957 RETURN_IF_ERROR(pos[35] < 0, "failed to add key (pos[1]=%d)", pos[35]);
958
959 /* Delete */
960 for (i = 0; i < 64; i++) {
961 pos[i] = rte_hash_del_key(handle, &rand_keys[i]);
962 print_key_info("Del", &rand_keys[i], pos[i]);
963 RETURN_IF_ERROR(pos[i] != expected_pos[i],
964 "failed to delete key (pos[%u]=%d)", i, pos[i]);
965 }
966
967 /* Lookup */
968 for (i = 0; i < 64; i++) {
969 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
970 print_key_info("Lkp", &rand_keys[i], pos[i]);
971 RETURN_IF_ERROR(pos[i] != -ENOENT,
972 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
973 }
974
975 /* Add again */
976 for (i = 0; i < 64; i++) {
977 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
978 print_key_info("Add", &rand_keys[i], pos[i]);
979 RETURN_IF_ERROR(pos[i] < 0,
980 "failed to add key (pos[%u]=%d)", i, pos[i]);
981 expected_pos[i] = pos[i];
982 }
983
984 rte_hash_free(handle);
985
986 /* Cover the NULL case. */
987 rte_hash_free(0);
988 return 0;
989 }
990
991 /******************************************************************************/
992 static int
993 fbk_hash_unit_test(void)
994 {
995 struct rte_fbk_hash_params params = {
996 .name = "fbk_hash_test",
997 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
998 .entries_per_bucket = 4,
999 .socket_id = 0,
1000 };
1001
1002 struct rte_fbk_hash_params invalid_params_1 = {
1003 .name = "invalid_1",
1004 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
1005 .entries_per_bucket = 4,
1006 .socket_id = 0,
1007 };
1008
1009 struct rte_fbk_hash_params invalid_params_2 = {
1010 .name = "invalid_2",
1011 .entries = 4,
1012 .entries_per_bucket = 3, /* Not power of 2 */
1013 .socket_id = 0,
1014 };
1015
1016 struct rte_fbk_hash_params invalid_params_3 = {
1017 .name = "invalid_3",
1018 .entries = 0, /* Entries is 0 */
1019 .entries_per_bucket = 4,
1020 .socket_id = 0,
1021 };
1022
1023 struct rte_fbk_hash_params invalid_params_4 = {
1024 .name = "invalid_4",
1025 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1026 .entries_per_bucket = 0, /* Entries per bucket is 0 */
1027 .socket_id = 0,
1028 };
1029
1030 struct rte_fbk_hash_params invalid_params_5 = {
1031 .name = "invalid_5",
1032 .entries = 4,
1033 .entries_per_bucket = 8, /* Entries per bucket > entries */
1034 .socket_id = 0,
1035 };
1036
1037 struct rte_fbk_hash_params invalid_params_6 = {
1038 .name = "invalid_6",
1039 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
1040 .entries_per_bucket = 4,
1041 .socket_id = 0,
1042 };
1043
1044 struct rte_fbk_hash_params invalid_params_7 = {
1045 .name = "invalid_7",
1046 .entries = RTE_FBK_HASH_ENTRIES_MAX,
1047 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
1048 .socket_id = 0,
1049 };
1050
1051 struct rte_fbk_hash_params invalid_params_8 = {
1052 .name = "invalid_7",
1053 .entries = RTE_FBK_HASH_ENTRIES_MAX,
1054 .entries_per_bucket = 4,
1055 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
1056 };
1057
1058 /* try to create two hashes with identical names
1059 * in this case, trying to create a second one will not
1060 * fail but will simply return pointer to the existing
1061 * hash with that name. sort of like a "find hash by name" :-)
1062 */
1063 struct rte_fbk_hash_params invalid_params_same_name_1 = {
1064 .name = "same_name", /* hash with identical name */
1065 .entries = 4,
1066 .entries_per_bucket = 2,
1067 .socket_id = 0,
1068 };
1069
1070 /* trying to create this hash should return a pointer to an existing hash */
1071 struct rte_fbk_hash_params invalid_params_same_name_2 = {
1072 .name = "same_name", /* hash with identical name */
1073 .entries = RTE_FBK_HASH_ENTRIES_MAX,
1074 .entries_per_bucket = 4,
1075 .socket_id = 0,
1076 };
1077
1078 /* this is a sanity check for "same name" test
1079 * creating this hash will check if we are actually able to create
1080 * multiple hashes with different names (instead of having just one).
1081 */
1082 struct rte_fbk_hash_params different_name = {
1083 .name = "different_name", /* different name */
1084 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1085 .entries_per_bucket = 4,
1086 .socket_id = 0,
1087 };
1088
1089 struct rte_fbk_hash_params params_jhash = {
1090 .name = "valid",
1091 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1092 .entries_per_bucket = 4,
1093 .socket_id = 0,
1094 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
1095 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1096 };
1097
1098 struct rte_fbk_hash_params params_nohash = {
1099 .name = "valid nohash",
1100 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1101 .entries_per_bucket = 4,
1102 .socket_id = 0,
1103 .hash_func = NULL, /* Tests for null hash_func */
1104 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1105 };
1106
1107 struct rte_fbk_hash_table *handle, *tmp;
1108 uint32_t keys[5] =
1109 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
1110 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
1111 int status;
1112 unsigned i;
1113 double used_entries;
1114
1115 /* Try creating hashes with invalid parameters */
1116 printf("# Testing hash creation with invalid parameters "
1117 "- expect error msgs\n");
1118 handle = rte_fbk_hash_create(&invalid_params_1);
1119 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1120
1121 handle = rte_fbk_hash_create(&invalid_params_2);
1122 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1123
1124 handle = rte_fbk_hash_create(&invalid_params_3);
1125 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1126
1127 handle = rte_fbk_hash_create(&invalid_params_4);
1128 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1129
1130 handle = rte_fbk_hash_create(&invalid_params_5);
1131 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1132
1133 handle = rte_fbk_hash_create(&invalid_params_6);
1134 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1135
1136 handle = rte_fbk_hash_create(&invalid_params_7);
1137 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1138
1139 if (rte_eal_has_hugepages()) {
1140 handle = rte_fbk_hash_create(&invalid_params_8);
1141 RETURN_IF_ERROR_FBK(handle != NULL,
1142 "fbk hash creation should have failed");
1143 }
1144
1145 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
1146 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
1147
1148 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
1149 if (tmp != NULL)
1150 rte_fbk_hash_free(tmp);
1151 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
1152
1153 /* we are not freeing handle here because we need a hash list
1154 * to be not empty for the next test */
1155
1156 /* create a hash in non-empty list - good for coverage */
1157 tmp = rte_fbk_hash_create(&different_name);
1158 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
1159
1160 /* free both hashes */
1161 rte_fbk_hash_free(handle);
1162 rte_fbk_hash_free(tmp);
1163
1164 /* Create empty jhash hash. */
1165 handle = rte_fbk_hash_create(&params_jhash);
1166 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
1167
1168 /* Cleanup. */
1169 rte_fbk_hash_free(handle);
1170
1171 /* Create empty jhash hash. */
1172 handle = rte_fbk_hash_create(&params_nohash);
1173 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
1174
1175 /* Cleanup. */
1176 rte_fbk_hash_free(handle);
1177
1178 /* Create empty hash. */
1179 handle = rte_fbk_hash_create(&params);
1180 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1181
1182 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1183 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1184 "load factor right after creation is not zero but it should be");
1185 /* Add keys. */
1186 for (i = 0; i < 5; i++) {
1187 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1188 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1189 }
1190
1191 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1192 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
1193 "load factor now is not as expected");
1194 /* Find value of added keys. */
1195 for (i = 0; i < 5; i++) {
1196 status = rte_fbk_hash_lookup(handle, keys[i]);
1197 RETURN_IF_ERROR_FBK(status != vals[i],
1198 "fbk hash lookup failed");
1199 }
1200
1201 /* Change value of added keys. */
1202 for (i = 0; i < 5; i++) {
1203 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
1204 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
1205 }
1206
1207 /* Find new values. */
1208 for (i = 0; i < 5; i++) {
1209 status = rte_fbk_hash_lookup(handle, keys[i]);
1210 RETURN_IF_ERROR_FBK(status != vals[4-i],
1211 "fbk hash lookup failed");
1212 }
1213
1214 /* Delete keys individually. */
1215 for (i = 0; i < 5; i++) {
1216 status = rte_fbk_hash_delete_key(handle, keys[i]);
1217 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
1218 }
1219
1220 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1221 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1222 "load factor right after deletion is not zero but it should be");
1223 /* Lookup should now fail. */
1224 for (i = 0; i < 5; i++) {
1225 status = rte_fbk_hash_lookup(handle, keys[i]);
1226 RETURN_IF_ERROR_FBK(status == 0,
1227 "fbk hash lookup should have failed");
1228 }
1229
1230 /* Add keys again. */
1231 for (i = 0; i < 5; i++) {
1232 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1233 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1234 }
1235
1236 /* Make sure they were added. */
1237 for (i = 0; i < 5; i++) {
1238 status = rte_fbk_hash_lookup(handle, keys[i]);
1239 RETURN_IF_ERROR_FBK(status != vals[i],
1240 "fbk hash lookup failed");
1241 }
1242
1243 /* Clear all entries. */
1244 rte_fbk_hash_clear_all(handle);
1245
1246 /* Lookup should fail. */
1247 for (i = 0; i < 5; i++) {
1248 status = rte_fbk_hash_lookup(handle, keys[i]);
1249 RETURN_IF_ERROR_FBK(status == 0,
1250 "fbk hash lookup should have failed");
1251 }
1252
1253 /* coverage */
1254
1255 /* fill up the hash_table */
1256 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
1257 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
1258
1259 /* Find non-existent key in a full hashtable */
1260 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1261 RETURN_IF_ERROR_FBK(status != -ENOENT,
1262 "fbk hash lookup succeeded");
1263
1264 /* Delete non-existent key in a full hashtable */
1265 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1266 RETURN_IF_ERROR_FBK(status != -ENOENT,
1267 "fbk hash delete succeeded");
1268
1269 /* Delete one key from a full hashtable */
1270 status = rte_fbk_hash_delete_key(handle, 1);
1271 RETURN_IF_ERROR_FBK(status != 0,
1272 "fbk hash delete failed");
1273
1274 /* Clear all entries. */
1275 rte_fbk_hash_clear_all(handle);
1276
1277 /* Cleanup. */
1278 rte_fbk_hash_free(handle);
1279
1280 /* Cover the NULL case. */
1281 rte_fbk_hash_free(0);
1282
1283 return 0;
1284 }
1285
1286 /*
1287 * Sequence of operations for find existing fbk hash table
1288 *
1289 * - create table
1290 * - find existing table: hit
1291 * - find non-existing table: miss
1292 *
1293 */
1294 static int test_fbk_hash_find_existing(void)
1295 {
1296 struct rte_fbk_hash_params params = {
1297 .name = "fbk_hash_find_existing",
1298 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1299 .entries_per_bucket = 4,
1300 .socket_id = 0,
1301 };
1302 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
1303
1304 /* Create hash table. */
1305 handle = rte_fbk_hash_create(&params);
1306 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1307
1308 /* Try to find existing fbk hash table */
1309 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1310 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1311
1312 /* Try to find non-existing fbk hash table */
1313 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1314 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1315
1316 /* Cleanup. */
1317 rte_fbk_hash_free(handle);
1318
1319 return 0;
1320 }
1321
1322 #define BUCKET_ENTRIES 4
1323 /*
1324 * Do tests for hash creation with bad parameters.
1325 */
1326 static int test_hash_creation_with_bad_parameters(void)
1327 {
1328 struct rte_hash *handle, *tmp;
1329 struct rte_hash_parameters params;
1330
1331 handle = rte_hash_create(NULL);
1332 if (handle != NULL) {
1333 rte_hash_free(handle);
1334 printf("Impossible creating hash successfully without any parameter\n");
1335 return -1;
1336 }
1337
1338 memcpy(&params, &ut_params, sizeof(params));
1339 params.name = "creation_with_bad_parameters_0";
1340 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1341 handle = rte_hash_create(&params);
1342 if (handle != NULL) {
1343 rte_hash_free(handle);
1344 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1345 return -1;
1346 }
1347
1348 memcpy(&params, &ut_params, sizeof(params));
1349 params.name = "creation_with_bad_parameters_2";
1350 params.entries = BUCKET_ENTRIES - 1;
1351 handle = rte_hash_create(&params);
1352 if (handle != NULL) {
1353 rte_hash_free(handle);
1354 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1355 return -1;
1356 }
1357
1358 memcpy(&params, &ut_params, sizeof(params));
1359 params.name = "creation_with_bad_parameters_3";
1360 params.key_len = 0;
1361 handle = rte_hash_create(&params);
1362 if (handle != NULL) {
1363 rte_hash_free(handle);
1364 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1365 return -1;
1366 }
1367
1368 memcpy(&params, &ut_params, sizeof(params));
1369 params.name = "creation_with_bad_parameters_4";
1370 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1371 handle = rte_hash_create(&params);
1372 if (handle != NULL) {
1373 rte_hash_free(handle);
1374 printf("Impossible creating hash successfully with invalid socket\n");
1375 return -1;
1376 }
1377
1378 /* test with same name should fail */
1379 memcpy(&params, &ut_params, sizeof(params));
1380 params.name = "same_name";
1381 handle = rte_hash_create(&params);
1382 if (handle == NULL) {
1383 printf("Cannot create first hash table with 'same_name'\n");
1384 return -1;
1385 }
1386 tmp = rte_hash_create(&params);
1387 if (tmp != NULL) {
1388 printf("Creation of hash table with same name should fail\n");
1389 rte_hash_free(handle);
1390 rte_hash_free(tmp);
1391 return -1;
1392 }
1393 rte_hash_free(handle);
1394
1395 printf("# Test successful. No more errors expected\n");
1396
1397 return 0;
1398 }
1399
1400 /*
1401 * Do tests for hash creation with parameters that look incorrect
1402 * but are actually valid.
1403 */
1404 static int
1405 test_hash_creation_with_good_parameters(void)
1406 {
1407 struct rte_hash *handle;
1408 struct rte_hash_parameters params;
1409
1410 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1411 memcpy(&params, &ut_params, sizeof(params));
1412 params.name = "name";
1413 params.hash_func = NULL;
1414 handle = rte_hash_create(&params);
1415 if (handle == NULL) {
1416 printf("Creating hash with null hash_func failed\n");
1417 return -1;
1418 }
1419
1420 rte_hash_free(handle);
1421
1422 return 0;
1423 }
1424
1425 #define ITERATIONS 3
1426 /*
1427 * Test to see the average table utilization (entries added/max entries)
1428 * before hitting a random entry that cannot be added
1429 */
1430 static int test_average_table_utilization(uint32_t ext_table)
1431 {
1432 struct rte_hash *handle;
1433 uint8_t simple_key[MAX_KEYSIZE];
1434 unsigned i, j;
1435 unsigned added_keys, average_keys_added = 0;
1436 int ret;
1437 unsigned int cnt;
1438
1439 printf("\n# Running test to determine average utilization"
1440 "\n before adding elements begins to fail\n");
1441 if (ext_table)
1442 printf("ext table is enabled\n");
1443 else
1444 printf("ext table is disabled\n");
1445
1446 printf("Measuring performance, please wait");
1447 fflush(stdout);
1448 ut_params.entries = 1 << 16;
1449 ut_params.name = "test_average_utilization";
1450 ut_params.hash_func = rte_jhash;
1451 if (ext_table)
1452 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1453 else
1454 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1455
1456 handle = rte_hash_create(&ut_params);
1457
1458 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1459
1460 for (j = 0; j < ITERATIONS; j++) {
1461 ret = 0;
1462 /* Add random entries until key cannot be added */
1463 for (added_keys = 0; ret >= 0; added_keys++) {
1464 for (i = 0; i < ut_params.key_len; i++)
1465 simple_key[i] = rte_rand() % 255;
1466 ret = rte_hash_add_key(handle, simple_key);
1467 if (ret < 0)
1468 break;
1469 }
1470
1471 if (ret != -ENOSPC) {
1472 printf("Unexpected error when adding keys\n");
1473 rte_hash_free(handle);
1474 return -1;
1475 }
1476
1477 cnt = rte_hash_count(handle);
1478 if (cnt != added_keys) {
1479 printf("rte_hash_count returned wrong value %u, %u,"
1480 "%u\n", j, added_keys, cnt);
1481 rte_hash_free(handle);
1482 return -1;
1483 }
1484 if (ext_table) {
1485 if (cnt != ut_params.entries) {
1486 printf("rte_hash_count returned wrong value "
1487 "%u, %u, %u\n", j, added_keys, cnt);
1488 rte_hash_free(handle);
1489 return -1;
1490 }
1491 }
1492
1493 average_keys_added += added_keys;
1494
1495 /* Reset the table */
1496 rte_hash_reset(handle);
1497
1498 /* Print a dot to show progress on operations */
1499 printf(".");
1500 fflush(stdout);
1501 }
1502
1503 average_keys_added /= ITERATIONS;
1504
1505 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1506 ((double) average_keys_added / ut_params.entries * 100),
1507 average_keys_added, ut_params.entries);
1508 rte_hash_free(handle);
1509
1510 return 0;
1511 }
1512
1513 #define NUM_ENTRIES 256
1514 static int test_hash_iteration(uint32_t ext_table)
1515 {
1516 struct rte_hash *handle;
1517 unsigned i;
1518 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1519 const void *next_key;
1520 void *next_data;
1521 void *data[NUM_ENTRIES];
1522 unsigned added_keys;
1523 uint32_t iter = 0;
1524 int ret = 0;
1525
1526 ut_params.entries = NUM_ENTRIES;
1527 ut_params.name = "test_hash_iteration";
1528 ut_params.hash_func = rte_jhash;
1529 ut_params.key_len = 16;
1530 if (ext_table)
1531 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1532 else
1533 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1534
1535 handle = rte_hash_create(&ut_params);
1536 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1537
1538 /* Add random entries until key cannot be added */
1539 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1540 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1541 for (i = 0; i < ut_params.key_len; i++)
1542 keys[added_keys][i] = rte_rand() % 255;
1543 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1544 if (ret < 0) {
1545 if (ext_table) {
1546 printf("Insertion failed for ext table\n");
1547 goto err;
1548 }
1549 break;
1550 }
1551 }
1552
1553 /* Iterate through the hash table */
1554 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1555 /* Search for the key in the list of keys added */
1556 for (i = 0; i < NUM_ENTRIES; i++) {
1557 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1558 if (next_data != data[i]) {
1559 printf("Data found in the hash table is"
1560 "not the data added with the key\n");
1561 goto err;
1562 }
1563 added_keys--;
1564 break;
1565 }
1566 }
1567 if (i == NUM_ENTRIES) {
1568 printf("Key found in the hash table was not added\n");
1569 goto err;
1570 }
1571 }
1572
1573 /* Check if all keys have been iterated */
1574 if (added_keys != 0) {
1575 printf("There were still %u keys to iterate\n", added_keys);
1576 goto err;
1577 }
1578
1579 rte_hash_free(handle);
1580 return 0;
1581
1582 err:
1583 rte_hash_free(handle);
1584 return -1;
1585 }
1586
1587 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1588 0x04, 0x05, 0x06, 0x07,
1589 0x08, 0x09, 0x0a, 0x0b,
1590 0x0c, 0x0d, 0x0e, 0x0f};
1591 static struct rte_hash_parameters hash_params_ex = {
1592 .name = NULL,
1593 .entries = 64,
1594 .key_len = 0,
1595 .hash_func = NULL,
1596 .hash_func_init_val = 0,
1597 .socket_id = 0,
1598 };
1599
1600 /*
1601 * add/delete key with jhash2
1602 */
1603 static int
1604 test_hash_add_delete_jhash2(void)
1605 {
1606 int ret = -1;
1607 struct rte_hash *handle;
1608 int32_t pos1, pos2;
1609
1610 hash_params_ex.name = "hash_test_jhash2";
1611 hash_params_ex.key_len = 4;
1612 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1613
1614 handle = rte_hash_create(&hash_params_ex);
1615 if (handle == NULL) {
1616 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1617 goto fail_jhash2;
1618 }
1619 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1620 if (pos1 < 0) {
1621 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1622 goto fail_jhash2;
1623 }
1624
1625 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1626 if (pos2 < 0 || pos1 != pos2) {
1627 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1628 goto fail_jhash2;
1629 }
1630 ret = 0;
1631
1632 fail_jhash2:
1633 if (handle != NULL)
1634 rte_hash_free(handle);
1635
1636 return ret;
1637 }
1638
1639 /*
1640 * add/delete (2) key with jhash2
1641 */
1642 static int
1643 test_hash_add_delete_2_jhash2(void)
1644 {
1645 int ret = -1;
1646 struct rte_hash *handle;
1647 int32_t pos1, pos2;
1648
1649 hash_params_ex.name = "hash_test_2_jhash2";
1650 hash_params_ex.key_len = 8;
1651 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1652
1653 handle = rte_hash_create(&hash_params_ex);
1654 if (handle == NULL)
1655 goto fail_2_jhash2;
1656
1657 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1658 if (pos1 < 0)
1659 goto fail_2_jhash2;
1660
1661 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1662 if (pos2 < 0 || pos1 != pos2)
1663 goto fail_2_jhash2;
1664
1665 ret = 0;
1666
1667 fail_2_jhash2:
1668 if (handle != NULL)
1669 rte_hash_free(handle);
1670
1671 return ret;
1672 }
1673
1674 static uint32_t
1675 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1676 {
1677 const uint32_t *k = key;
1678
1679 RTE_SET_USED(length);
1680
1681 return rte_jhash_1word(k[0], initval);
1682 }
1683
1684 static uint32_t
1685 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1686 {
1687 const uint32_t *k = key;
1688
1689 RTE_SET_USED(length);
1690
1691 return rte_jhash_2words(k[0], k[1], initval);
1692 }
1693
1694 static uint32_t
1695 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1696 {
1697 const uint32_t *k = key;
1698
1699 RTE_SET_USED(length);
1700
1701 return rte_jhash_3words(k[0], k[1], k[2], initval);
1702 }
1703
1704 /*
1705 * add/delete key with jhash 1word
1706 */
1707 static int
1708 test_hash_add_delete_jhash_1word(void)
1709 {
1710 int ret = -1;
1711 struct rte_hash *handle;
1712 int32_t pos1, pos2;
1713
1714 hash_params_ex.name = "hash_test_jhash_1word";
1715 hash_params_ex.key_len = 4;
1716 hash_params_ex.hash_func = test_hash_jhash_1word;
1717
1718 handle = rte_hash_create(&hash_params_ex);
1719 if (handle == NULL)
1720 goto fail_jhash_1word;
1721
1722 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1723 if (pos1 < 0)
1724 goto fail_jhash_1word;
1725
1726 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1727 if (pos2 < 0 || pos1 != pos2)
1728 goto fail_jhash_1word;
1729
1730 ret = 0;
1731
1732 fail_jhash_1word:
1733 if (handle != NULL)
1734 rte_hash_free(handle);
1735
1736 return ret;
1737 }
1738
1739 /*
1740 * add/delete key with jhash 2word
1741 */
1742 static int
1743 test_hash_add_delete_jhash_2word(void)
1744 {
1745 int ret = -1;
1746 struct rte_hash *handle;
1747 int32_t pos1, pos2;
1748
1749 hash_params_ex.name = "hash_test_jhash_2word";
1750 hash_params_ex.key_len = 8;
1751 hash_params_ex.hash_func = test_hash_jhash_2word;
1752
1753 handle = rte_hash_create(&hash_params_ex);
1754 if (handle == NULL)
1755 goto fail_jhash_2word;
1756
1757 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1758 if (pos1 < 0)
1759 goto fail_jhash_2word;
1760
1761 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1762 if (pos2 < 0 || pos1 != pos2)
1763 goto fail_jhash_2word;
1764
1765 ret = 0;
1766
1767 fail_jhash_2word:
1768 if (handle != NULL)
1769 rte_hash_free(handle);
1770
1771 return ret;
1772 }
1773
1774 /*
1775 * add/delete key with jhash 3word
1776 */
1777 static int
1778 test_hash_add_delete_jhash_3word(void)
1779 {
1780 int ret = -1;
1781 struct rte_hash *handle;
1782 int32_t pos1, pos2;
1783
1784 hash_params_ex.name = "hash_test_jhash_3word";
1785 hash_params_ex.key_len = 12;
1786 hash_params_ex.hash_func = test_hash_jhash_3word;
1787
1788 handle = rte_hash_create(&hash_params_ex);
1789 if (handle == NULL)
1790 goto fail_jhash_3word;
1791
1792 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1793 if (pos1 < 0)
1794 goto fail_jhash_3word;
1795
1796 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1797 if (pos2 < 0 || pos1 != pos2)
1798 goto fail_jhash_3word;
1799
1800 ret = 0;
1801
1802 fail_jhash_3word:
1803 if (handle != NULL)
1804 rte_hash_free(handle);
1805
1806 return ret;
1807 }
1808
1809 /*
1810 * Do all unit and performance tests.
1811 */
1812 static int
1813 test_hash(void)
1814 {
1815 if (test_add_delete() < 0)
1816 return -1;
1817 if (test_hash_add_delete_jhash2() < 0)
1818 return -1;
1819 if (test_hash_add_delete_2_jhash2() < 0)
1820 return -1;
1821 if (test_hash_add_delete_jhash_1word() < 0)
1822 return -1;
1823 if (test_hash_add_delete_jhash_2word() < 0)
1824 return -1;
1825 if (test_hash_add_delete_jhash_3word() < 0)
1826 return -1;
1827 if (test_hash_get_key_with_position() < 0)
1828 return -1;
1829 if (test_hash_find_existing() < 0)
1830 return -1;
1831 if (test_add_update_delete() < 0)
1832 return -1;
1833 if (test_add_update_delete_free() < 0)
1834 return -1;
1835 if (test_add_delete_free_lf() < 0)
1836 return -1;
1837 if (test_five_keys() < 0)
1838 return -1;
1839 if (test_full_bucket() < 0)
1840 return -1;
1841 if (test_extendable_bucket() < 0)
1842 return -1;
1843
1844 if (test_fbk_hash_find_existing() < 0)
1845 return -1;
1846 if (fbk_hash_unit_test() < 0)
1847 return -1;
1848 if (test_hash_creation_with_bad_parameters() < 0)
1849 return -1;
1850 if (test_hash_creation_with_good_parameters() < 0)
1851 return -1;
1852
1853 /* ext table disabled */
1854 if (test_average_table_utilization(0) < 0)
1855 return -1;
1856 if (test_hash_iteration(0) < 0)
1857 return -1;
1858
1859 /* ext table enabled */
1860 if (test_average_table_utilization(1) < 0)
1861 return -1;
1862 if (test_hash_iteration(1) < 0)
1863 return -1;
1864
1865 run_hash_func_tests();
1866
1867 if (test_crc32_hash_alg_equiv() < 0)
1868 return -1;
1869
1870 return 0;
1871 }
1872
1873 REGISTER_TEST_COMMAND(hash_autotest, test_hash);