1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
11 #include <sys/queue.h>
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>
20 #include <rte_string_fns.h>
25 #include <rte_fbk_hash.h>
26 #include <rte_jhash.h>
27 #include <rte_hash_crc.h>
29 /*******************************************************************************
30 * Hash function performance test configuration section. Each performance test
31 * will be performed HASHTEST_ITERATIONS times.
33 * The five arrays below control what tests are performed. Every combination
34 * from the array entries is tested.
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)
44 * Check condition and return an error if true. Assumes that "handle" is the
45 * name of the hash structure pointer to be freed.
47 #define RETURN_IF_ERROR(cond, str, ...) do { \
49 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
50 if (handle) rte_hash_free(handle); \
55 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
57 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
58 if (handle) rte_fbk_hash_free(handle); \
63 /* 5-tuple key type */
70 } __attribute__((packed));
73 * Hash function that always returns the same value, to easily test what
74 * happens when a bucket is full.
76 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
77 __attribute__((unused)) uint32_t key_len,
78 __attribute__((unused)) uint32_t init_val)
83 #define UNIT_TEST_HASH_VERBOSE 0
85 * Print out result of unit test hash operation.
87 static void print_key_info(const char *msg, const struct flow_key *key,
90 if (UNIT_TEST_HASH_VERBOSE) {
91 const uint8_t *p = (const uint8_t *)key;
94 printf("%s key:0x", msg);
95 for (i = 0; i < sizeof(struct flow_key); i++)
97 printf(" @ pos %d\n", pos);
101 /* Keys used by unit test functions */
102 static struct flow_key keys[5] = { {
103 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
104 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
109 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
110 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
115 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
116 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
121 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
122 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
127 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
128 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
134 /* Parameters used for hash table in unit test functions. Name set later. */
135 static struct rte_hash_parameters ut_params = {
137 .key_len = sizeof(struct flow_key), /* 13 */
138 .hash_func = rte_jhash,
139 .hash_func_init_val = 0,
143 #define CRC32_ITERATIONS (1U << 10)
144 #define CRC32_DWORDS (1U << 6)
146 * Test if all CRC32 implementations yield the same hash value
149 test_crc32_hash_alg_equiv(void)
153 uint64_t data64[CRC32_DWORDS];
157 printf("\n# CRC32 implementations equivalence test\n");
158 for (i = 0; i < CRC32_ITERATIONS; i++) {
159 /* Randomizing data_len of data set */
160 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
161 init_val = (uint32_t) rte_rand();
163 /* Fill the data set */
164 for (j = 0; j < CRC32_DWORDS; j++)
165 data64[j] = rte_rand();
167 /* Calculate software CRC32 */
168 rte_hash_crc_set_alg(CRC32_SW);
169 hash_val = rte_hash_crc(data64, data_len, init_val);
171 /* Check against 4-byte-operand sse4.2 CRC32 if available */
172 rte_hash_crc_set_alg(CRC32_SSE42);
173 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
174 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
178 /* Check against 8-byte-operand sse4.2 CRC32 if available */
179 rte_hash_crc_set_alg(CRC32_SSE42_x64);
180 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
181 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
185 /* Check against 8-byte-operand ARM64 CRC32 if available */
186 rte_hash_crc_set_alg(CRC32_ARM64);
187 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
188 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
193 /* Resetting to best available algorithm */
194 rte_hash_crc_set_alg(CRC32_SSE42_x64);
196 if (i == CRC32_ITERATIONS)
199 printf("Failed test data (hex, %zu bytes total):\n", data_len);
200 for (j = 0; j < data_len; j++)
201 printf("%02X%c", ((uint8_t *)data64)[j],
202 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
208 * Test a hash function.
210 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
213 static uint8_t key[MAX_KEYSIZE];
217 for (i = 0; i < key_len; i++)
218 key[i] = (uint8_t) rte_rand();
220 /* just to be on the safe side */
224 f(key, key_len, init_val);
228 * Test all hash functions.
230 static void run_hash_func_tests(void)
235 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
238 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
241 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
243 run_hash_func_test(hashtest_funcs[i],
244 hashtest_initvals[j],
245 hashtest_key_lens[k]);
252 * Basic sequence of operations for a single key:
258 * Repeat the test case when 'free on delete' is disabled.
265 static int test_add_delete(void)
267 struct rte_hash *handle;
268 /* test with standard add/lookup/delete functions */
269 int pos0, expectedPos0;
271 ut_params.name = "test1";
272 handle = rte_hash_create(&ut_params);
273 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
275 pos0 = rte_hash_add_key(handle, &keys[0]);
276 print_key_info("Add", &keys[0], pos0);
277 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
280 pos0 = rte_hash_lookup(handle, &keys[0]);
281 print_key_info("Lkp", &keys[0], pos0);
282 RETURN_IF_ERROR(pos0 != expectedPos0,
283 "failed to find key (pos0=%d)", pos0);
285 pos0 = rte_hash_del_key(handle, &keys[0]);
286 print_key_info("Del", &keys[0], pos0);
287 RETURN_IF_ERROR(pos0 != expectedPos0,
288 "failed to delete key (pos0=%d)", pos0);
290 pos0 = rte_hash_lookup(handle, &keys[0]);
291 print_key_info("Lkp", &keys[0], pos0);
292 RETURN_IF_ERROR(pos0 != -ENOENT,
293 "fail: found key after deleting! (pos0=%d)", pos0);
295 rte_hash_free(handle);
297 /* repeat test with precomputed hash functions */
298 hash_sig_t hash_value;
299 int pos1, expectedPos1, delPos1;
301 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
302 handle = rte_hash_create(&ut_params);
303 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
304 ut_params.extra_flag = 0;
306 hash_value = rte_hash_hash(handle, &keys[0]);
307 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
308 print_key_info("Add", &keys[0], pos1);
309 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
312 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
313 print_key_info("Lkp", &keys[0], pos1);
314 RETURN_IF_ERROR(pos1 != expectedPos1,
315 "failed to find key (pos1=%d)", pos1);
317 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
318 print_key_info("Del", &keys[0], pos1);
319 RETURN_IF_ERROR(pos1 != expectedPos1,
320 "failed to delete key (pos1=%d)", pos1);
323 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
324 print_key_info("Lkp", &keys[0], pos1);
325 RETURN_IF_ERROR(pos1 != -ENOENT,
326 "fail: found key after deleting! (pos1=%d)", pos1);
328 pos1 = rte_hash_free_key_with_position(handle, delPos1);
329 print_key_info("Free", &keys[0], delPos1);
330 RETURN_IF_ERROR(pos1 != 0,
331 "failed to free key (pos1=%d)", delPos1);
333 rte_hash_free(handle);
339 * Sequence of operations for a single key:
344 * - lookup: hit (updated data)
349 static int test_add_update_delete(void)
351 struct rte_hash *handle;
352 int pos0, expectedPos0;
354 ut_params.name = "test2";
355 handle = rte_hash_create(&ut_params);
356 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
358 pos0 = rte_hash_del_key(handle, &keys[0]);
359 print_key_info("Del", &keys[0], pos0);
360 RETURN_IF_ERROR(pos0 != -ENOENT,
361 "fail: found non-existent key (pos0=%d)", pos0);
363 pos0 = rte_hash_add_key(handle, &keys[0]);
364 print_key_info("Add", &keys[0], pos0);
365 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
368 pos0 = rte_hash_lookup(handle, &keys[0]);
369 print_key_info("Lkp", &keys[0], pos0);
370 RETURN_IF_ERROR(pos0 != expectedPos0,
371 "failed to find key (pos0=%d)", pos0);
373 pos0 = rte_hash_add_key(handle, &keys[0]);
374 print_key_info("Add", &keys[0], pos0);
375 RETURN_IF_ERROR(pos0 != expectedPos0,
376 "failed to re-add key (pos0=%d)", pos0);
378 pos0 = rte_hash_lookup(handle, &keys[0]);
379 print_key_info("Lkp", &keys[0], pos0);
380 RETURN_IF_ERROR(pos0 != expectedPos0,
381 "failed to find key (pos0=%d)", pos0);
383 pos0 = rte_hash_del_key(handle, &keys[0]);
384 print_key_info("Del", &keys[0], pos0);
385 RETURN_IF_ERROR(pos0 != expectedPos0,
386 "failed to delete key (pos0=%d)", pos0);
388 pos0 = rte_hash_del_key(handle, &keys[0]);
389 print_key_info("Del", &keys[0], pos0);
390 RETURN_IF_ERROR(pos0 != -ENOENT,
391 "fail: deleted already deleted key (pos0=%d)", pos0);
393 pos0 = rte_hash_lookup(handle, &keys[0]);
394 print_key_info("Lkp", &keys[0], pos0);
395 RETURN_IF_ERROR(pos0 != -ENOENT,
396 "fail: found key after deleting! (pos0=%d)", pos0);
398 rte_hash_free(handle);
403 * Sequence of operations for a single key with 'disable free on del' set:
408 * - lookup: hit (updated data)
415 static int test_add_update_delete_free(void)
417 struct rte_hash *handle;
418 int pos0, expectedPos0, delPos0, result;
420 ut_params.name = "test2";
421 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
422 handle = rte_hash_create(&ut_params);
423 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
424 ut_params.extra_flag = 0;
426 pos0 = rte_hash_del_key(handle, &keys[0]);
427 print_key_info("Del", &keys[0], pos0);
428 RETURN_IF_ERROR(pos0 != -ENOENT,
429 "fail: found non-existent key (pos0=%d)", pos0);
431 pos0 = rte_hash_add_key(handle, &keys[0]);
432 print_key_info("Add", &keys[0], pos0);
433 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
436 pos0 = rte_hash_lookup(handle, &keys[0]);
437 print_key_info("Lkp", &keys[0], pos0);
438 RETURN_IF_ERROR(pos0 != expectedPos0,
439 "failed to find key (pos0=%d)", pos0);
441 pos0 = rte_hash_add_key(handle, &keys[0]);
442 print_key_info("Add", &keys[0], pos0);
443 RETURN_IF_ERROR(pos0 != expectedPos0,
444 "failed to re-add key (pos0=%d)", pos0);
446 pos0 = rte_hash_lookup(handle, &keys[0]);
447 print_key_info("Lkp", &keys[0], pos0);
448 RETURN_IF_ERROR(pos0 != expectedPos0,
449 "failed to find key (pos0=%d)", pos0);
451 delPos0 = rte_hash_del_key(handle, &keys[0]);
452 print_key_info("Del", &keys[0], delPos0);
453 RETURN_IF_ERROR(delPos0 != expectedPos0,
454 "failed to delete key (pos0=%d)", delPos0);
456 pos0 = rte_hash_del_key(handle, &keys[0]);
457 print_key_info("Del", &keys[0], pos0);
458 RETURN_IF_ERROR(pos0 != -ENOENT,
459 "fail: deleted already deleted key (pos0=%d)", pos0);
461 pos0 = rte_hash_lookup(handle, &keys[0]);
462 print_key_info("Lkp", &keys[0], pos0);
463 RETURN_IF_ERROR(pos0 != -ENOENT,
464 "fail: found key after deleting! (pos0=%d)", pos0);
466 result = rte_hash_free_key_with_position(handle, delPos0);
467 print_key_info("Free", &keys[0], delPos0);
468 RETURN_IF_ERROR(result != 0,
469 "failed to free key (pos1=%d)", delPos0);
471 pos0 = rte_hash_lookup(handle, &keys[0]);
472 print_key_info("Lkp", &keys[0], pos0);
473 RETURN_IF_ERROR(pos0 != -ENOENT,
474 "fail: found key after deleting! (pos0=%d)", pos0);
476 rte_hash_free(handle);
481 * Sequence of operations for retrieving a key with its position
485 * - get the key with its position: hit
487 * - try to get the deleted key: miss
489 * Repeat the test case when 'free on delete' is disabled.
492 * - get the key with its position: hit
494 * - try to get the deleted key: hit
496 * - try to get the deleted key: miss
499 static int test_hash_get_key_with_position(void)
501 struct rte_hash *handle = NULL;
502 int pos, expectedPos, delPos, result;
505 ut_params.name = "hash_get_key_w_pos";
506 handle = rte_hash_create(&ut_params);
507 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
509 pos = rte_hash_add_key(handle, &keys[0]);
510 print_key_info("Add", &keys[0], pos);
511 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
514 result = rte_hash_get_key_with_position(handle, pos, &key);
515 RETURN_IF_ERROR(result != 0, "error retrieving a key");
517 pos = rte_hash_del_key(handle, &keys[0]);
518 print_key_info("Del", &keys[0], pos);
519 RETURN_IF_ERROR(pos != expectedPos,
520 "failed to delete key (pos0=%d)", pos);
522 result = rte_hash_get_key_with_position(handle, pos, &key);
523 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
525 rte_hash_free(handle);
527 ut_params.name = "hash_get_key_w_pos";
528 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
529 handle = rte_hash_create(&ut_params);
530 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
531 ut_params.extra_flag = 0;
533 pos = rte_hash_add_key(handle, &keys[0]);
534 print_key_info("Add", &keys[0], pos);
535 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
538 result = rte_hash_get_key_with_position(handle, pos, &key);
539 RETURN_IF_ERROR(result != 0, "error retrieving a key");
541 delPos = rte_hash_del_key(handle, &keys[0]);
542 print_key_info("Del", &keys[0], delPos);
543 RETURN_IF_ERROR(delPos != expectedPos,
544 "failed to delete key (pos0=%d)", delPos);
546 result = rte_hash_get_key_with_position(handle, delPos, &key);
547 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
549 result = rte_hash_free_key_with_position(handle, delPos);
550 print_key_info("Free", &keys[0], delPos);
551 RETURN_IF_ERROR(result != 0,
552 "failed to free key (pos1=%d)", delPos);
554 result = rte_hash_get_key_with_position(handle, delPos, &key);
555 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
557 rte_hash_free(handle);
562 * Sequence of operations for find existing hash table
565 * - find existing table: hit
566 * - find non-existing table: miss
569 static int test_hash_find_existing(void)
571 struct rte_hash *handle = NULL, *result = NULL;
573 /* Create hash table. */
574 ut_params.name = "hash_find_existing";
575 handle = rte_hash_create(&ut_params);
576 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
578 /* Try to find existing hash table */
579 result = rte_hash_find_existing("hash_find_existing");
580 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
582 /* Try to find non-existing hash table */
583 result = rte_hash_find_existing("hash_find_non_existing");
584 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
587 rte_hash_free(handle);
593 * Sequence of operations for 5 keys
596 * - add keys (update)
597 * - lookup keys: hit (updated data)
598 * - delete keys : hit
599 * - lookup keys: miss
601 static int test_five_keys(void)
603 struct rte_hash *handle;
604 const void *key_array[5] = {0};
610 ut_params.name = "test3";
611 handle = rte_hash_create(&ut_params);
612 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
615 for (i = 0; i < 5; i++) {
616 pos[i] = rte_hash_add_key(handle, &keys[i]);
617 print_key_info("Add", &keys[i], pos[i]);
618 RETURN_IF_ERROR(pos[i] < 0,
619 "failed to add key (pos[%u]=%d)", i, pos[i]);
620 expected_pos[i] = pos[i];
624 for(i = 0; i < 5; i++)
625 key_array[i] = &keys[i];
627 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
629 for(i = 0; i < 5; i++) {
630 print_key_info("Lkp", key_array[i], pos[i]);
631 RETURN_IF_ERROR(pos[i] != expected_pos[i],
632 "failed to find key (pos[%u]=%d)", i, pos[i]);
636 for (i = 0; i < 5; i++) {
637 pos[i] = rte_hash_add_key(handle, &keys[i]);
638 print_key_info("Add", &keys[i], pos[i]);
639 RETURN_IF_ERROR(pos[i] != expected_pos[i],
640 "failed to add key (pos[%u]=%d)", i, pos[i]);
644 for (i = 0; i < 5; i++) {
645 pos[i] = rte_hash_lookup(handle, &keys[i]);
646 print_key_info("Lkp", &keys[i], pos[i]);
647 RETURN_IF_ERROR(pos[i] != expected_pos[i],
648 "failed to find key (pos[%u]=%d)", i, pos[i]);
652 for (i = 0; i < 5; i++) {
653 pos[i] = rte_hash_del_key(handle, &keys[i]);
654 print_key_info("Del", &keys[i], pos[i]);
655 RETURN_IF_ERROR(pos[i] != expected_pos[i],
656 "failed to delete key (pos[%u]=%d)", i, pos[i]);
660 for (i = 0; i < 5; i++) {
661 pos[i] = rte_hash_lookup(handle, &keys[i]);
662 print_key_info("Lkp", &keys[i], pos[i]);
663 RETURN_IF_ERROR(pos[i] != -ENOENT,
664 "found non-existent key (pos[%u]=%d)", i, pos[i]);
668 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
670 for (i = 0; i < 5; i++) {
671 print_key_info("Lkp", key_array[i], pos[i]);
672 RETURN_IF_ERROR(pos[i] != -ENOENT,
673 "found not-existent key (pos[%u]=%d)", i, pos[i]);
676 rte_hash_free(handle);
682 * Add keys to the same bucket until bucket full.
683 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
684 * first 4 successful, 5th successful, pushing existing item in bucket
685 * - lookup the 5 keys: 5 hits
686 * - add the 5 keys again: 5 OK
687 * - lookup the 5 keys: 5 hits (updated data)
688 * - delete the 5 keys: 5 OK
689 * - lookup the 5 keys: 5 misses
691 static int test_full_bucket(void)
693 struct rte_hash_parameters params_pseudo_hash = {
696 .key_len = sizeof(struct flow_key), /* 13 */
697 .hash_func = pseudo_hash,
698 .hash_func_init_val = 0,
701 struct rte_hash *handle;
706 handle = rte_hash_create(¶ms_pseudo_hash);
707 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
710 for (i = 0; i < 4; i++) {
711 pos[i] = rte_hash_add_key(handle, &keys[i]);
712 print_key_info("Add", &keys[i], pos[i]);
713 RETURN_IF_ERROR(pos[i] < 0,
714 "failed to add key (pos[%u]=%d)", i, pos[i]);
715 expected_pos[i] = pos[i];
718 * This should work and will push one of the items
719 * in the bucket because it is full
721 pos[4] = rte_hash_add_key(handle, &keys[4]);
722 print_key_info("Add", &keys[4], pos[4]);
723 RETURN_IF_ERROR(pos[4] < 0,
724 "failed to add key (pos[4]=%d)", pos[4]);
725 expected_pos[4] = pos[4];
728 for (i = 0; i < 5; i++) {
729 pos[i] = rte_hash_lookup(handle, &keys[i]);
730 print_key_info("Lkp", &keys[i], pos[i]);
731 RETURN_IF_ERROR(pos[i] != expected_pos[i],
732 "failed to find key (pos[%u]=%d)", i, pos[i]);
736 for (i = 0; i < 5; i++) {
737 pos[i] = rte_hash_add_key(handle, &keys[i]);
738 print_key_info("Add", &keys[i], pos[i]);
739 RETURN_IF_ERROR(pos[i] != expected_pos[i],
740 "failed to add key (pos[%u]=%d)", i, pos[i]);
744 for (i = 0; i < 5; i++) {
745 pos[i] = rte_hash_lookup(handle, &keys[i]);
746 print_key_info("Lkp", &keys[i], pos[i]);
747 RETURN_IF_ERROR(pos[i] != expected_pos[i],
748 "failed to find key (pos[%u]=%d)", i, pos[i]);
751 /* Delete 1 key, check other keys are still found */
752 pos[1] = rte_hash_del_key(handle, &keys[1]);
753 print_key_info("Del", &keys[1], pos[1]);
754 RETURN_IF_ERROR(pos[1] != expected_pos[1],
755 "failed to delete key (pos[1]=%d)", pos[1]);
756 pos[3] = rte_hash_lookup(handle, &keys[3]);
757 print_key_info("Lkp", &keys[3], pos[3]);
758 RETURN_IF_ERROR(pos[3] != expected_pos[3],
759 "failed lookup after deleting key from same bucket "
760 "(pos[3]=%d)", pos[3]);
762 /* Go back to previous state */
763 pos[1] = rte_hash_add_key(handle, &keys[1]);
764 print_key_info("Add", &keys[1], pos[1]);
765 expected_pos[1] = pos[1];
766 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
769 for (i = 0; i < 5; i++) {
770 pos[i] = rte_hash_del_key(handle, &keys[i]);
771 print_key_info("Del", &keys[i], pos[i]);
772 RETURN_IF_ERROR(pos[i] != expected_pos[i],
773 "failed to delete key (pos[%u]=%d)", i, pos[i]);
777 for (i = 0; i < 5; i++) {
778 pos[i] = rte_hash_lookup(handle, &keys[i]);
779 print_key_info("Lkp", &keys[i], pos[i]);
780 RETURN_IF_ERROR(pos[i] != -ENOENT,
781 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
784 rte_hash_free(handle);
786 /* Cover the NULL case. */
792 * Similar to the test above (full bucket test), but for extendable buckets.
794 static int test_extendable_bucket(void)
796 struct rte_hash_parameters params_pseudo_hash = {
799 .key_len = sizeof(struct flow_key), /* 13 */
800 .hash_func = pseudo_hash,
801 .hash_func_init_val = 0,
803 .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE
805 struct rte_hash *handle;
807 int expected_pos[64];
809 struct flow_key rand_keys[64];
811 for (i = 0; i < 64; i++) {
812 rand_keys[i].port_dst = i;
813 rand_keys[i].port_src = i+1;
816 handle = rte_hash_create(¶ms_pseudo_hash);
817 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
820 for (i = 0; i < 64; i++) {
821 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
822 print_key_info("Add", &rand_keys[i], pos[i]);
823 RETURN_IF_ERROR(pos[i] < 0,
824 "failed to add key (pos[%u]=%d)", i, pos[i]);
825 expected_pos[i] = pos[i];
829 for (i = 0; i < 64; i++) {
830 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
831 print_key_info("Lkp", &rand_keys[i], pos[i]);
832 RETURN_IF_ERROR(pos[i] != expected_pos[i],
833 "failed to find key (pos[%u]=%d)", i, pos[i]);
837 for (i = 0; i < 64; i++) {
838 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
839 print_key_info("Add", &rand_keys[i], pos[i]);
840 RETURN_IF_ERROR(pos[i] != expected_pos[i],
841 "failed to add key (pos[%u]=%d)", i, pos[i]);
845 for (i = 0; i < 64; i++) {
846 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
847 print_key_info("Lkp", &rand_keys[i], pos[i]);
848 RETURN_IF_ERROR(pos[i] != expected_pos[i],
849 "failed to find key (pos[%u]=%d)", i, pos[i]);
852 /* Delete 1 key, check other keys are still found */
853 pos[35] = rte_hash_del_key(handle, &rand_keys[35]);
854 print_key_info("Del", &rand_keys[35], pos[35]);
855 RETURN_IF_ERROR(pos[35] != expected_pos[35],
856 "failed to delete key (pos[1]=%d)", pos[35]);
857 pos[20] = rte_hash_lookup(handle, &rand_keys[20]);
858 print_key_info("Lkp", &rand_keys[20], pos[20]);
859 RETURN_IF_ERROR(pos[20] != expected_pos[20],
860 "failed lookup after deleting key from same bucket "
861 "(pos[20]=%d)", pos[20]);
863 /* Go back to previous state */
864 pos[35] = rte_hash_add_key(handle, &rand_keys[35]);
865 print_key_info("Add", &rand_keys[35], pos[35]);
866 expected_pos[35] = pos[35];
867 RETURN_IF_ERROR(pos[35] < 0, "failed to add key (pos[1]=%d)", pos[35]);
870 for (i = 0; i < 64; i++) {
871 pos[i] = rte_hash_del_key(handle, &rand_keys[i]);
872 print_key_info("Del", &rand_keys[i], pos[i]);
873 RETURN_IF_ERROR(pos[i] != expected_pos[i],
874 "failed to delete key (pos[%u]=%d)", i, pos[i]);
878 for (i = 0; i < 64; i++) {
879 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
880 print_key_info("Lkp", &rand_keys[i], pos[i]);
881 RETURN_IF_ERROR(pos[i] != -ENOENT,
882 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
886 for (i = 0; i < 64; i++) {
887 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
888 print_key_info("Add", &rand_keys[i], pos[i]);
889 RETURN_IF_ERROR(pos[i] < 0,
890 "failed to add key (pos[%u]=%d)", i, pos[i]);
891 expected_pos[i] = pos[i];
894 rte_hash_free(handle);
896 /* Cover the NULL case. */
901 /******************************************************************************/
903 fbk_hash_unit_test(void)
905 struct rte_fbk_hash_params params = {
906 .name = "fbk_hash_test",
907 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
908 .entries_per_bucket = 4,
912 struct rte_fbk_hash_params invalid_params_1 = {
914 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
915 .entries_per_bucket = 4,
919 struct rte_fbk_hash_params invalid_params_2 = {
922 .entries_per_bucket = 3, /* Not power of 2 */
926 struct rte_fbk_hash_params invalid_params_3 = {
928 .entries = 0, /* Entries is 0 */
929 .entries_per_bucket = 4,
933 struct rte_fbk_hash_params invalid_params_4 = {
935 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
936 .entries_per_bucket = 0, /* Entries per bucket is 0 */
940 struct rte_fbk_hash_params invalid_params_5 = {
943 .entries_per_bucket = 8, /* Entries per bucket > entries */
947 struct rte_fbk_hash_params invalid_params_6 = {
949 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
950 .entries_per_bucket = 4,
954 struct rte_fbk_hash_params invalid_params_7 = {
956 .entries = RTE_FBK_HASH_ENTRIES_MAX,
957 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
961 struct rte_fbk_hash_params invalid_params_8 = {
963 .entries = RTE_FBK_HASH_ENTRIES_MAX,
964 .entries_per_bucket = 4,
965 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
968 /* try to create two hashes with identical names
969 * in this case, trying to create a second one will not
970 * fail but will simply return pointer to the existing
971 * hash with that name. sort of like a "find hash by name" :-)
973 struct rte_fbk_hash_params invalid_params_same_name_1 = {
974 .name = "same_name", /* hash with identical name */
976 .entries_per_bucket = 2,
980 /* trying to create this hash should return a pointer to an existing hash */
981 struct rte_fbk_hash_params invalid_params_same_name_2 = {
982 .name = "same_name", /* hash with identical name */
983 .entries = RTE_FBK_HASH_ENTRIES_MAX,
984 .entries_per_bucket = 4,
988 /* this is a sanity check for "same name" test
989 * creating this hash will check if we are actually able to create
990 * multiple hashes with different names (instead of having just one).
992 struct rte_fbk_hash_params different_name = {
993 .name = "different_name", /* different name */
994 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
995 .entries_per_bucket = 4,
999 struct rte_fbk_hash_params params_jhash = {
1001 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1002 .entries_per_bucket = 4,
1004 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
1005 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1008 struct rte_fbk_hash_params params_nohash = {
1009 .name = "valid nohash",
1010 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1011 .entries_per_bucket = 4,
1013 .hash_func = NULL, /* Tests for null hash_func */
1014 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1017 struct rte_fbk_hash_table *handle, *tmp;
1019 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
1020 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
1023 double used_entries;
1025 /* Try creating hashes with invalid parameters */
1026 printf("# Testing hash creation with invalid parameters "
1027 "- expect error msgs\n");
1028 handle = rte_fbk_hash_create(&invalid_params_1);
1029 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1031 handle = rte_fbk_hash_create(&invalid_params_2);
1032 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1034 handle = rte_fbk_hash_create(&invalid_params_3);
1035 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1037 handle = rte_fbk_hash_create(&invalid_params_4);
1038 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1040 handle = rte_fbk_hash_create(&invalid_params_5);
1041 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1043 handle = rte_fbk_hash_create(&invalid_params_6);
1044 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1046 handle = rte_fbk_hash_create(&invalid_params_7);
1047 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1049 handle = rte_fbk_hash_create(&invalid_params_8);
1050 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1052 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
1053 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
1055 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
1057 rte_fbk_hash_free(tmp);
1058 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
1060 /* we are not freeing handle here because we need a hash list
1061 * to be not empty for the next test */
1063 /* create a hash in non-empty list - good for coverage */
1064 tmp = rte_fbk_hash_create(&different_name);
1065 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
1067 /* free both hashes */
1068 rte_fbk_hash_free(handle);
1069 rte_fbk_hash_free(tmp);
1071 /* Create empty jhash hash. */
1072 handle = rte_fbk_hash_create(¶ms_jhash);
1073 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
1076 rte_fbk_hash_free(handle);
1078 /* Create empty jhash hash. */
1079 handle = rte_fbk_hash_create(¶ms_nohash);
1080 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
1083 rte_fbk_hash_free(handle);
1085 /* Create empty hash. */
1086 handle = rte_fbk_hash_create(¶ms);
1087 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1089 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1090 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1091 "load factor right after creation is not zero but it should be");
1093 for (i = 0; i < 5; i++) {
1094 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1095 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1098 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1099 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
1100 "load factor now is not as expected");
1101 /* Find value of added keys. */
1102 for (i = 0; i < 5; i++) {
1103 status = rte_fbk_hash_lookup(handle, keys[i]);
1104 RETURN_IF_ERROR_FBK(status != vals[i],
1105 "fbk hash lookup failed");
1108 /* Change value of added keys. */
1109 for (i = 0; i < 5; i++) {
1110 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
1111 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
1114 /* Find new values. */
1115 for (i = 0; i < 5; i++) {
1116 status = rte_fbk_hash_lookup(handle, keys[i]);
1117 RETURN_IF_ERROR_FBK(status != vals[4-i],
1118 "fbk hash lookup failed");
1121 /* Delete keys individually. */
1122 for (i = 0; i < 5; i++) {
1123 status = rte_fbk_hash_delete_key(handle, keys[i]);
1124 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
1127 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1128 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1129 "load factor right after deletion is not zero but it should be");
1130 /* Lookup should now fail. */
1131 for (i = 0; i < 5; i++) {
1132 status = rte_fbk_hash_lookup(handle, keys[i]);
1133 RETURN_IF_ERROR_FBK(status == 0,
1134 "fbk hash lookup should have failed");
1137 /* Add keys again. */
1138 for (i = 0; i < 5; i++) {
1139 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1140 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1143 /* Make sure they were added. */
1144 for (i = 0; i < 5; i++) {
1145 status = rte_fbk_hash_lookup(handle, keys[i]);
1146 RETURN_IF_ERROR_FBK(status != vals[i],
1147 "fbk hash lookup failed");
1150 /* Clear all entries. */
1151 rte_fbk_hash_clear_all(handle);
1153 /* Lookup should fail. */
1154 for (i = 0; i < 5; i++) {
1155 status = rte_fbk_hash_lookup(handle, keys[i]);
1156 RETURN_IF_ERROR_FBK(status == 0,
1157 "fbk hash lookup should have failed");
1162 /* fill up the hash_table */
1163 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
1164 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
1166 /* Find non-existent key in a full hashtable */
1167 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1168 RETURN_IF_ERROR_FBK(status != -ENOENT,
1169 "fbk hash lookup succeeded");
1171 /* Delete non-existent key in a full hashtable */
1172 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1173 RETURN_IF_ERROR_FBK(status != -ENOENT,
1174 "fbk hash delete succeeded");
1176 /* Delete one key from a full hashtable */
1177 status = rte_fbk_hash_delete_key(handle, 1);
1178 RETURN_IF_ERROR_FBK(status != 0,
1179 "fbk hash delete failed");
1181 /* Clear all entries. */
1182 rte_fbk_hash_clear_all(handle);
1185 rte_fbk_hash_free(handle);
1187 /* Cover the NULL case. */
1188 rte_fbk_hash_free(0);
1194 * Sequence of operations for find existing fbk hash table
1197 * - find existing table: hit
1198 * - find non-existing table: miss
1201 static int test_fbk_hash_find_existing(void)
1203 struct rte_fbk_hash_params params = {
1204 .name = "fbk_hash_find_existing",
1205 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1206 .entries_per_bucket = 4,
1209 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
1211 /* Create hash table. */
1212 handle = rte_fbk_hash_create(¶ms);
1213 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1215 /* Try to find existing fbk hash table */
1216 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1217 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1219 /* Try to find non-existing fbk hash table */
1220 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1221 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1224 rte_fbk_hash_free(handle);
1229 #define BUCKET_ENTRIES 4
1231 * Do tests for hash creation with bad parameters.
1233 static int test_hash_creation_with_bad_parameters(void)
1235 struct rte_hash *handle, *tmp;
1236 struct rte_hash_parameters params;
1238 handle = rte_hash_create(NULL);
1239 if (handle != NULL) {
1240 rte_hash_free(handle);
1241 printf("Impossible creating hash successfully without any parameter\n");
1245 memcpy(¶ms, &ut_params, sizeof(params));
1246 params.name = "creation_with_bad_parameters_0";
1247 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1248 handle = rte_hash_create(¶ms);
1249 if (handle != NULL) {
1250 rte_hash_free(handle);
1251 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1255 memcpy(¶ms, &ut_params, sizeof(params));
1256 params.name = "creation_with_bad_parameters_2";
1257 params.entries = BUCKET_ENTRIES - 1;
1258 handle = rte_hash_create(¶ms);
1259 if (handle != NULL) {
1260 rte_hash_free(handle);
1261 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1265 memcpy(¶ms, &ut_params, sizeof(params));
1266 params.name = "creation_with_bad_parameters_3";
1268 handle = rte_hash_create(¶ms);
1269 if (handle != NULL) {
1270 rte_hash_free(handle);
1271 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1275 memcpy(¶ms, &ut_params, sizeof(params));
1276 params.name = "creation_with_bad_parameters_4";
1277 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1278 handle = rte_hash_create(¶ms);
1279 if (handle != NULL) {
1280 rte_hash_free(handle);
1281 printf("Impossible creating hash successfully with invalid socket\n");
1285 /* test with same name should fail */
1286 memcpy(¶ms, &ut_params, sizeof(params));
1287 params.name = "same_name";
1288 handle = rte_hash_create(¶ms);
1289 if (handle == NULL) {
1290 printf("Cannot create first hash table with 'same_name'\n");
1293 tmp = rte_hash_create(¶ms);
1295 printf("Creation of hash table with same name should fail\n");
1296 rte_hash_free(handle);
1300 rte_hash_free(handle);
1302 printf("# Test successful. No more errors expected\n");
1308 * Do tests for hash creation with parameters that look incorrect
1309 * but are actually valid.
1312 test_hash_creation_with_good_parameters(void)
1314 struct rte_hash *handle;
1315 struct rte_hash_parameters params;
1317 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1318 memcpy(¶ms, &ut_params, sizeof(params));
1319 params.name = "name";
1320 params.hash_func = NULL;
1321 handle = rte_hash_create(¶ms);
1322 if (handle == NULL) {
1323 printf("Creating hash with null hash_func failed\n");
1327 rte_hash_free(handle);
1332 #define ITERATIONS 3
1334 * Test to see the average table utilization (entries added/max entries)
1335 * before hitting a random entry that cannot be added
1337 static int test_average_table_utilization(uint32_t ext_table)
1339 struct rte_hash *handle;
1340 uint8_t simple_key[MAX_KEYSIZE];
1342 unsigned added_keys, average_keys_added = 0;
1346 printf("\n# Running test to determine average utilization"
1347 "\n before adding elements begins to fail\n");
1349 printf("ext table is enabled\n");
1351 printf("ext table is disabled\n");
1353 printf("Measuring performance, please wait");
1355 ut_params.entries = 1 << 16;
1356 ut_params.name = "test_average_utilization";
1357 ut_params.hash_func = rte_jhash;
1359 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1361 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1363 handle = rte_hash_create(&ut_params);
1365 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1367 for (j = 0; j < ITERATIONS; j++) {
1369 /* Add random entries until key cannot be added */
1370 for (added_keys = 0; ret >= 0; added_keys++) {
1371 for (i = 0; i < ut_params.key_len; i++)
1372 simple_key[i] = rte_rand() % 255;
1373 ret = rte_hash_add_key(handle, simple_key);
1378 if (ret != -ENOSPC) {
1379 printf("Unexpected error when adding keys\n");
1380 rte_hash_free(handle);
1384 cnt = rte_hash_count(handle);
1385 if (cnt != added_keys) {
1386 printf("rte_hash_count returned wrong value %u, %u,"
1387 "%u\n", j, added_keys, cnt);
1388 rte_hash_free(handle);
1392 if (cnt != ut_params.entries) {
1393 printf("rte_hash_count returned wrong value "
1394 "%u, %u, %u\n", j, added_keys, cnt);
1395 rte_hash_free(handle);
1400 average_keys_added += added_keys;
1402 /* Reset the table */
1403 rte_hash_reset(handle);
1405 /* Print a dot to show progress on operations */
1410 average_keys_added /= ITERATIONS;
1412 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1413 ((double) average_keys_added / ut_params.entries * 100),
1414 average_keys_added, ut_params.entries);
1415 rte_hash_free(handle);
1420 #define NUM_ENTRIES 256
1421 static int test_hash_iteration(uint32_t ext_table)
1423 struct rte_hash *handle;
1425 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1426 const void *next_key;
1428 void *data[NUM_ENTRIES];
1429 unsigned added_keys;
1433 ut_params.entries = NUM_ENTRIES;
1434 ut_params.name = "test_hash_iteration";
1435 ut_params.hash_func = rte_jhash;
1436 ut_params.key_len = 16;
1438 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1440 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1442 handle = rte_hash_create(&ut_params);
1443 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1445 /* Add random entries until key cannot be added */
1446 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1447 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1448 for (i = 0; i < ut_params.key_len; i++)
1449 keys[added_keys][i] = rte_rand() % 255;
1450 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1453 printf("Insertion failed for ext table\n");
1460 /* Iterate through the hash table */
1461 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1462 /* Search for the key in the list of keys added */
1463 for (i = 0; i < NUM_ENTRIES; i++) {
1464 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1465 if (next_data != data[i]) {
1466 printf("Data found in the hash table is"
1467 "not the data added with the key\n");
1474 if (i == NUM_ENTRIES) {
1475 printf("Key found in the hash table was not added\n");
1480 /* Check if all keys have been iterated */
1481 if (added_keys != 0) {
1482 printf("There were still %u keys to iterate\n", added_keys);
1486 rte_hash_free(handle);
1490 rte_hash_free(handle);
1494 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1495 0x04, 0x05, 0x06, 0x07,
1496 0x08, 0x09, 0x0a, 0x0b,
1497 0x0c, 0x0d, 0x0e, 0x0f};
1498 static struct rte_hash_parameters hash_params_ex = {
1503 .hash_func_init_val = 0,
1508 * add/delete key with jhash2
1511 test_hash_add_delete_jhash2(void)
1514 struct rte_hash *handle;
1517 hash_params_ex.name = "hash_test_jhash2";
1518 hash_params_ex.key_len = 4;
1519 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1521 handle = rte_hash_create(&hash_params_ex);
1522 if (handle == NULL) {
1523 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1526 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1528 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1532 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1533 if (pos2 < 0 || pos1 != pos2) {
1534 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1541 rte_hash_free(handle);
1547 * add/delete (2) key with jhash2
1550 test_hash_add_delete_2_jhash2(void)
1553 struct rte_hash *handle;
1556 hash_params_ex.name = "hash_test_2_jhash2";
1557 hash_params_ex.key_len = 8;
1558 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1560 handle = rte_hash_create(&hash_params_ex);
1564 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1568 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1569 if (pos2 < 0 || pos1 != pos2)
1576 rte_hash_free(handle);
1582 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1584 const uint32_t *k = key;
1586 RTE_SET_USED(length);
1588 return rte_jhash_1word(k[0], initval);
1592 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1594 const uint32_t *k = key;
1596 RTE_SET_USED(length);
1598 return rte_jhash_2words(k[0], k[1], initval);
1602 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1604 const uint32_t *k = key;
1606 RTE_SET_USED(length);
1608 return rte_jhash_3words(k[0], k[1], k[2], initval);
1612 * add/delete key with jhash 1word
1615 test_hash_add_delete_jhash_1word(void)
1618 struct rte_hash *handle;
1621 hash_params_ex.name = "hash_test_jhash_1word";
1622 hash_params_ex.key_len = 4;
1623 hash_params_ex.hash_func = test_hash_jhash_1word;
1625 handle = rte_hash_create(&hash_params_ex);
1627 goto fail_jhash_1word;
1629 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1631 goto fail_jhash_1word;
1633 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1634 if (pos2 < 0 || pos1 != pos2)
1635 goto fail_jhash_1word;
1641 rte_hash_free(handle);
1647 * add/delete key with jhash 2word
1650 test_hash_add_delete_jhash_2word(void)
1653 struct rte_hash *handle;
1656 hash_params_ex.name = "hash_test_jhash_2word";
1657 hash_params_ex.key_len = 8;
1658 hash_params_ex.hash_func = test_hash_jhash_2word;
1660 handle = rte_hash_create(&hash_params_ex);
1662 goto fail_jhash_2word;
1664 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1666 goto fail_jhash_2word;
1668 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1669 if (pos2 < 0 || pos1 != pos2)
1670 goto fail_jhash_2word;
1676 rte_hash_free(handle);
1682 * add/delete key with jhash 3word
1685 test_hash_add_delete_jhash_3word(void)
1688 struct rte_hash *handle;
1691 hash_params_ex.name = "hash_test_jhash_3word";
1692 hash_params_ex.key_len = 12;
1693 hash_params_ex.hash_func = test_hash_jhash_3word;
1695 handle = rte_hash_create(&hash_params_ex);
1697 goto fail_jhash_3word;
1699 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1701 goto fail_jhash_3word;
1703 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1704 if (pos2 < 0 || pos1 != pos2)
1705 goto fail_jhash_3word;
1711 rte_hash_free(handle);
1717 * Do all unit and performance tests.
1722 if (test_add_delete() < 0)
1724 if (test_hash_add_delete_jhash2() < 0)
1726 if (test_hash_add_delete_2_jhash2() < 0)
1728 if (test_hash_add_delete_jhash_1word() < 0)
1730 if (test_hash_add_delete_jhash_2word() < 0)
1732 if (test_hash_add_delete_jhash_3word() < 0)
1734 if (test_hash_get_key_with_position() < 0)
1736 if (test_hash_find_existing() < 0)
1738 if (test_add_update_delete() < 0)
1740 if (test_add_update_delete_free() < 0)
1742 if (test_five_keys() < 0)
1744 if (test_full_bucket() < 0)
1746 if (test_extendable_bucket() < 0)
1749 if (test_fbk_hash_find_existing() < 0)
1751 if (fbk_hash_unit_test() < 0)
1753 if (test_hash_creation_with_bad_parameters() < 0)
1755 if (test_hash_creation_with_good_parameters() < 0)
1758 /* ext table disabled */
1759 if (test_average_table_utilization(0) < 0)
1761 if (test_hash_iteration(0) < 0)
1764 /* ext table enabled */
1765 if (test_average_table_utilization(1) < 0)
1767 if (test_hash_iteration(1) < 0)
1770 run_hash_func_tests();
1772 if (test_crc32_hash_alg_equiv() < 0)
1778 REGISTER_TEST_COMMAND(hash_autotest, test_hash);