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));
72 int hash_logtype_test;
75 * Hash function that always returns the same value, to easily test what
76 * happens when a bucket is full.
78 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
79 __attribute__((unused)) uint32_t key_len,
80 __attribute__((unused)) uint32_t init_val)
85 RTE_INIT(test_hash_init_log)
87 hash_logtype_test = rte_log_register("test.hash");
91 * Print out result of unit test hash operation.
93 static void print_key_info(const char *msg, const struct flow_key *key,
96 const uint8_t *p = (const uint8_t *)key;
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);
105 /* Keys used by unit test functions */
106 static struct flow_key keys[5] = { {
107 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
108 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
113 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
114 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
119 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
120 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
125 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
126 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
131 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
132 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
138 /* Parameters used for hash table in unit test functions. Name set later. */
139 static struct rte_hash_parameters ut_params = {
141 .key_len = sizeof(struct flow_key), /* 13 */
142 .hash_func = rte_jhash,
143 .hash_func_init_val = 0,
147 #define CRC32_ITERATIONS (1U << 10)
148 #define CRC32_DWORDS (1U << 6)
150 * Test if all CRC32 implementations yield the same hash value
153 test_crc32_hash_alg_equiv(void)
157 uint64_t data64[CRC32_DWORDS];
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();
167 /* Fill the data set */
168 for (j = 0; j < CRC32_DWORDS; j++)
169 data64[j] = rte_rand();
171 /* Calculate software CRC32 */
172 rte_hash_crc_set_alg(CRC32_SW);
173 hash_val = rte_hash_crc(data64, data_len, init_val);
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");
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");
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");
197 /* Resetting to best available algorithm */
198 rte_hash_crc_set_alg(CRC32_SSE42_x64);
200 if (i == CRC32_ITERATIONS)
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' : ' ');
212 * Test a hash function.
214 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
217 static uint8_t key[MAX_KEYSIZE];
221 for (i = 0; i < key_len; i++)
222 key[i] = (uint8_t) rte_rand();
224 /* just to be on the safe side */
228 f(key, key_len, init_val);
232 * Test all hash functions.
234 static void run_hash_func_tests(void)
239 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
242 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
245 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
247 run_hash_func_test(hashtest_funcs[i],
248 hashtest_initvals[j],
249 hashtest_key_lens[k]);
256 * Basic sequence of operations for a single key:
262 * Repeat the test case when 'free on delete' is disabled.
269 static int test_add_delete(void)
271 struct rte_hash *handle;
272 /* test with standard add/lookup/delete functions */
273 int pos0, expectedPos0;
275 ut_params.name = "test1";
276 handle = rte_hash_create(&ut_params);
277 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
279 pos0 = rte_hash_add_key(handle, &keys[0]);
280 print_key_info("Add", &keys[0], pos0);
281 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
284 pos0 = rte_hash_lookup(handle, &keys[0]);
285 print_key_info("Lkp", &keys[0], pos0);
286 RETURN_IF_ERROR(pos0 != expectedPos0,
287 "failed to find key (pos0=%d)", pos0);
289 pos0 = rte_hash_del_key(handle, &keys[0]);
290 print_key_info("Del", &keys[0], pos0);
291 RETURN_IF_ERROR(pos0 != expectedPos0,
292 "failed to delete key (pos0=%d)", pos0);
294 pos0 = rte_hash_lookup(handle, &keys[0]);
295 print_key_info("Lkp", &keys[0], pos0);
296 RETURN_IF_ERROR(pos0 != -ENOENT,
297 "fail: found key after deleting! (pos0=%d)", pos0);
299 rte_hash_free(handle);
301 /* repeat test with precomputed hash functions */
302 hash_sig_t hash_value;
303 int pos1, expectedPos1, delPos1;
305 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
306 handle = rte_hash_create(&ut_params);
307 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
308 ut_params.extra_flag = 0;
310 hash_value = rte_hash_hash(handle, &keys[0]);
311 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
312 print_key_info("Add", &keys[0], pos1);
313 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
316 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
317 print_key_info("Lkp", &keys[0], pos1);
318 RETURN_IF_ERROR(pos1 != expectedPos1,
319 "failed to find key (pos1=%d)", pos1);
321 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
322 print_key_info("Del", &keys[0], pos1);
323 RETURN_IF_ERROR(pos1 != expectedPos1,
324 "failed to delete key (pos1=%d)", pos1);
327 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
328 print_key_info("Lkp", &keys[0], pos1);
329 RETURN_IF_ERROR(pos1 != -ENOENT,
330 "fail: found key after deleting! (pos1=%d)", pos1);
332 pos1 = rte_hash_free_key_with_position(handle, delPos1);
333 print_key_info("Free", &keys[0], delPos1);
334 RETURN_IF_ERROR(pos1 != 0,
335 "failed to free key (pos1=%d)", delPos1);
337 rte_hash_free(handle);
343 * Sequence of operations for a single key:
348 * - lookup: hit (updated data)
353 static int test_add_update_delete(void)
355 struct rte_hash *handle;
356 int pos0, expectedPos0;
358 ut_params.name = "test2";
359 handle = rte_hash_create(&ut_params);
360 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
362 pos0 = rte_hash_del_key(handle, &keys[0]);
363 print_key_info("Del", &keys[0], pos0);
364 RETURN_IF_ERROR(pos0 != -ENOENT,
365 "fail: found non-existent key (pos0=%d)", pos0);
367 pos0 = rte_hash_add_key(handle, &keys[0]);
368 print_key_info("Add", &keys[0], pos0);
369 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
372 pos0 = rte_hash_lookup(handle, &keys[0]);
373 print_key_info("Lkp", &keys[0], pos0);
374 RETURN_IF_ERROR(pos0 != expectedPos0,
375 "failed to find key (pos0=%d)", pos0);
377 pos0 = rte_hash_add_key(handle, &keys[0]);
378 print_key_info("Add", &keys[0], pos0);
379 RETURN_IF_ERROR(pos0 != expectedPos0,
380 "failed to re-add key (pos0=%d)", pos0);
382 pos0 = rte_hash_lookup(handle, &keys[0]);
383 print_key_info("Lkp", &keys[0], pos0);
384 RETURN_IF_ERROR(pos0 != expectedPos0,
385 "failed to find key (pos0=%d)", pos0);
387 pos0 = rte_hash_del_key(handle, &keys[0]);
388 print_key_info("Del", &keys[0], pos0);
389 RETURN_IF_ERROR(pos0 != expectedPos0,
390 "failed to delete key (pos0=%d)", pos0);
392 pos0 = rte_hash_del_key(handle, &keys[0]);
393 print_key_info("Del", &keys[0], pos0);
394 RETURN_IF_ERROR(pos0 != -ENOENT,
395 "fail: deleted already deleted key (pos0=%d)", pos0);
397 pos0 = rte_hash_lookup(handle, &keys[0]);
398 print_key_info("Lkp", &keys[0], pos0);
399 RETURN_IF_ERROR(pos0 != -ENOENT,
400 "fail: found key after deleting! (pos0=%d)", pos0);
402 rte_hash_free(handle);
407 * Sequence of operations for a single key with 'disable free on del' set:
412 * - lookup: hit (updated data)
419 static int test_add_update_delete_free(void)
421 struct rte_hash *handle;
422 int pos0, expectedPos0, delPos0, result;
424 ut_params.name = "test2";
425 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
426 handle = rte_hash_create(&ut_params);
427 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
428 ut_params.extra_flag = 0;
430 pos0 = rte_hash_del_key(handle, &keys[0]);
431 print_key_info("Del", &keys[0], pos0);
432 RETURN_IF_ERROR(pos0 != -ENOENT,
433 "fail: found non-existent key (pos0=%d)", pos0);
435 pos0 = rte_hash_add_key(handle, &keys[0]);
436 print_key_info("Add", &keys[0], pos0);
437 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
440 pos0 = rte_hash_lookup(handle, &keys[0]);
441 print_key_info("Lkp", &keys[0], pos0);
442 RETURN_IF_ERROR(pos0 != expectedPos0,
443 "failed to find key (pos0=%d)", pos0);
445 pos0 = rte_hash_add_key(handle, &keys[0]);
446 print_key_info("Add", &keys[0], pos0);
447 RETURN_IF_ERROR(pos0 != expectedPos0,
448 "failed to re-add key (pos0=%d)", pos0);
450 pos0 = rte_hash_lookup(handle, &keys[0]);
451 print_key_info("Lkp", &keys[0], pos0);
452 RETURN_IF_ERROR(pos0 != expectedPos0,
453 "failed to find key (pos0=%d)", pos0);
455 delPos0 = rte_hash_del_key(handle, &keys[0]);
456 print_key_info("Del", &keys[0], delPos0);
457 RETURN_IF_ERROR(delPos0 != expectedPos0,
458 "failed to delete key (pos0=%d)", delPos0);
460 pos0 = rte_hash_del_key(handle, &keys[0]);
461 print_key_info("Del", &keys[0], pos0);
462 RETURN_IF_ERROR(pos0 != -ENOENT,
463 "fail: deleted already deleted key (pos0=%d)", pos0);
465 pos0 = rte_hash_lookup(handle, &keys[0]);
466 print_key_info("Lkp", &keys[0], pos0);
467 RETURN_IF_ERROR(pos0 != -ENOENT,
468 "fail: found key after deleting! (pos0=%d)", pos0);
470 result = rte_hash_free_key_with_position(handle, delPos0);
471 print_key_info("Free", &keys[0], delPos0);
472 RETURN_IF_ERROR(result != 0,
473 "failed to free key (pos1=%d)", delPos0);
475 pos0 = rte_hash_lookup(handle, &keys[0]);
476 print_key_info("Lkp", &keys[0], pos0);
477 RETURN_IF_ERROR(pos0 != -ENOENT,
478 "fail: found key after deleting! (pos0=%d)", pos0);
480 rte_hash_free(handle);
485 * Sequence of operations for retrieving a key with its position
489 * - get the key with its position: hit
491 * - try to get the deleted key: miss
493 * Repeat the test case when 'free on delete' is disabled.
496 * - get the key with its position: hit
498 * - try to get the deleted key: hit
500 * - try to get the deleted key: miss
503 static int test_hash_get_key_with_position(void)
505 struct rte_hash *handle = NULL;
506 int pos, expectedPos, delPos, result;
509 ut_params.name = "hash_get_key_w_pos";
510 handle = rte_hash_create(&ut_params);
511 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
513 pos = rte_hash_add_key(handle, &keys[0]);
514 print_key_info("Add", &keys[0], pos);
515 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
518 result = rte_hash_get_key_with_position(handle, pos, &key);
519 RETURN_IF_ERROR(result != 0, "error retrieving a key");
521 pos = rte_hash_del_key(handle, &keys[0]);
522 print_key_info("Del", &keys[0], pos);
523 RETURN_IF_ERROR(pos != expectedPos,
524 "failed to delete key (pos0=%d)", pos);
526 result = rte_hash_get_key_with_position(handle, pos, &key);
527 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
529 rte_hash_free(handle);
531 ut_params.name = "hash_get_key_w_pos";
532 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL;
533 handle = rte_hash_create(&ut_params);
534 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
535 ut_params.extra_flag = 0;
537 pos = rte_hash_add_key(handle, &keys[0]);
538 print_key_info("Add", &keys[0], pos);
539 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
542 result = rte_hash_get_key_with_position(handle, pos, &key);
543 RETURN_IF_ERROR(result != 0, "error retrieving a key");
545 delPos = rte_hash_del_key(handle, &keys[0]);
546 print_key_info("Del", &keys[0], delPos);
547 RETURN_IF_ERROR(delPos != expectedPos,
548 "failed to delete key (pos0=%d)", delPos);
550 result = rte_hash_get_key_with_position(handle, delPos, &key);
551 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
553 result = rte_hash_free_key_with_position(handle, delPos);
554 print_key_info("Free", &keys[0], delPos);
555 RETURN_IF_ERROR(result != 0,
556 "failed to free key (pos1=%d)", delPos);
558 result = rte_hash_get_key_with_position(handle, delPos, &key);
559 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
561 rte_hash_free(handle);
566 * Sequence of operations for find existing hash table
569 * - find existing table: hit
570 * - find non-existing table: miss
573 static int test_hash_find_existing(void)
575 struct rte_hash *handle = NULL, *result = NULL;
577 /* Create hash table. */
578 ut_params.name = "hash_find_existing";
579 handle = rte_hash_create(&ut_params);
580 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
582 /* Try to find existing hash table */
583 result = rte_hash_find_existing("hash_find_existing");
584 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
586 /* Try to find non-existing hash table */
587 result = rte_hash_find_existing("hash_find_non_existing");
588 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
591 rte_hash_free(handle);
597 * Sequence of operations for 5 keys
600 * - add keys (update)
601 * - lookup keys: hit (updated data)
602 * - delete keys : hit
603 * - lookup keys: miss
605 static int test_five_keys(void)
607 struct rte_hash *handle;
608 const void *key_array[5] = {0};
614 ut_params.name = "test3";
615 handle = rte_hash_create(&ut_params);
616 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
619 for (i = 0; i < 5; i++) {
620 pos[i] = rte_hash_add_key(handle, &keys[i]);
621 print_key_info("Add", &keys[i], pos[i]);
622 RETURN_IF_ERROR(pos[i] < 0,
623 "failed to add key (pos[%u]=%d)", i, pos[i]);
624 expected_pos[i] = pos[i];
628 for(i = 0; i < 5; i++)
629 key_array[i] = &keys[i];
631 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
633 for(i = 0; i < 5; i++) {
634 print_key_info("Lkp", key_array[i], pos[i]);
635 RETURN_IF_ERROR(pos[i] != expected_pos[i],
636 "failed to find key (pos[%u]=%d)", i, pos[i]);
640 for (i = 0; i < 5; i++) {
641 pos[i] = rte_hash_add_key(handle, &keys[i]);
642 print_key_info("Add", &keys[i], pos[i]);
643 RETURN_IF_ERROR(pos[i] != expected_pos[i],
644 "failed to add key (pos[%u]=%d)", i, pos[i]);
648 for (i = 0; i < 5; i++) {
649 pos[i] = rte_hash_lookup(handle, &keys[i]);
650 print_key_info("Lkp", &keys[i], pos[i]);
651 RETURN_IF_ERROR(pos[i] != expected_pos[i],
652 "failed to find key (pos[%u]=%d)", i, pos[i]);
656 for (i = 0; i < 5; i++) {
657 pos[i] = rte_hash_del_key(handle, &keys[i]);
658 print_key_info("Del", &keys[i], pos[i]);
659 RETURN_IF_ERROR(pos[i] != expected_pos[i],
660 "failed to delete key (pos[%u]=%d)", i, pos[i]);
664 for (i = 0; i < 5; i++) {
665 pos[i] = rte_hash_lookup(handle, &keys[i]);
666 print_key_info("Lkp", &keys[i], pos[i]);
667 RETURN_IF_ERROR(pos[i] != -ENOENT,
668 "found non-existent key (pos[%u]=%d)", i, pos[i]);
672 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
674 for (i = 0; i < 5; i++) {
675 print_key_info("Lkp", key_array[i], pos[i]);
676 RETURN_IF_ERROR(pos[i] != -ENOENT,
677 "found not-existent key (pos[%u]=%d)", i, pos[i]);
680 rte_hash_free(handle);
686 * Add keys to the same bucket until bucket full.
687 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
688 * first 4 successful, 5th successful, pushing existing item in bucket
689 * - lookup the 5 keys: 5 hits
690 * - add the 5 keys again: 5 OK
691 * - lookup the 5 keys: 5 hits (updated data)
692 * - delete the 5 keys: 5 OK
693 * - lookup the 5 keys: 5 misses
695 static int test_full_bucket(void)
697 struct rte_hash_parameters params_pseudo_hash = {
700 .key_len = sizeof(struct flow_key), /* 13 */
701 .hash_func = pseudo_hash,
702 .hash_func_init_val = 0,
705 struct rte_hash *handle;
710 handle = rte_hash_create(¶ms_pseudo_hash);
711 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
714 for (i = 0; i < 4; i++) {
715 pos[i] = rte_hash_add_key(handle, &keys[i]);
716 print_key_info("Add", &keys[i], pos[i]);
717 RETURN_IF_ERROR(pos[i] < 0,
718 "failed to add key (pos[%u]=%d)", i, pos[i]);
719 expected_pos[i] = pos[i];
722 * This should work and will push one of the items
723 * in the bucket because it is full
725 pos[4] = rte_hash_add_key(handle, &keys[4]);
726 print_key_info("Add", &keys[4], pos[4]);
727 RETURN_IF_ERROR(pos[4] < 0,
728 "failed to add key (pos[4]=%d)", pos[4]);
729 expected_pos[4] = pos[4];
732 for (i = 0; i < 5; i++) {
733 pos[i] = rte_hash_lookup(handle, &keys[i]);
734 print_key_info("Lkp", &keys[i], pos[i]);
735 RETURN_IF_ERROR(pos[i] != expected_pos[i],
736 "failed to find key (pos[%u]=%d)", i, pos[i]);
740 for (i = 0; i < 5; i++) {
741 pos[i] = rte_hash_add_key(handle, &keys[i]);
742 print_key_info("Add", &keys[i], pos[i]);
743 RETURN_IF_ERROR(pos[i] != expected_pos[i],
744 "failed to add key (pos[%u]=%d)", i, pos[i]);
748 for (i = 0; i < 5; i++) {
749 pos[i] = rte_hash_lookup(handle, &keys[i]);
750 print_key_info("Lkp", &keys[i], pos[i]);
751 RETURN_IF_ERROR(pos[i] != expected_pos[i],
752 "failed to find key (pos[%u]=%d)", i, pos[i]);
755 /* Delete 1 key, check other keys are still found */
756 pos[1] = rte_hash_del_key(handle, &keys[1]);
757 print_key_info("Del", &keys[1], pos[1]);
758 RETURN_IF_ERROR(pos[1] != expected_pos[1],
759 "failed to delete key (pos[1]=%d)", pos[1]);
760 pos[3] = rte_hash_lookup(handle, &keys[3]);
761 print_key_info("Lkp", &keys[3], pos[3]);
762 RETURN_IF_ERROR(pos[3] != expected_pos[3],
763 "failed lookup after deleting key from same bucket "
764 "(pos[3]=%d)", pos[3]);
766 /* Go back to previous state */
767 pos[1] = rte_hash_add_key(handle, &keys[1]);
768 print_key_info("Add", &keys[1], pos[1]);
769 expected_pos[1] = pos[1];
770 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
773 for (i = 0; i < 5; i++) {
774 pos[i] = rte_hash_del_key(handle, &keys[i]);
775 print_key_info("Del", &keys[i], pos[i]);
776 RETURN_IF_ERROR(pos[i] != expected_pos[i],
777 "failed to delete key (pos[%u]=%d)", i, pos[i]);
781 for (i = 0; i < 5; i++) {
782 pos[i] = rte_hash_lookup(handle, &keys[i]);
783 print_key_info("Lkp", &keys[i], pos[i]);
784 RETURN_IF_ERROR(pos[i] != -ENOENT,
785 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
788 rte_hash_free(handle);
790 /* Cover the NULL case. */
796 * Similar to the test above (full bucket test), but for extendable buckets.
798 static int test_extendable_bucket(void)
800 struct rte_hash_parameters params_pseudo_hash = {
803 .key_len = sizeof(struct flow_key), /* 13 */
804 .hash_func = pseudo_hash,
805 .hash_func_init_val = 0,
807 .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE
809 struct rte_hash *handle;
811 int expected_pos[64];
813 struct flow_key rand_keys[64];
815 for (i = 0; i < 64; i++) {
816 rand_keys[i].port_dst = i;
817 rand_keys[i].port_src = i+1;
820 handle = rte_hash_create(¶ms_pseudo_hash);
821 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
824 for (i = 0; i < 64; i++) {
825 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
826 print_key_info("Add", &rand_keys[i], pos[i]);
827 RETURN_IF_ERROR(pos[i] < 0,
828 "failed to add key (pos[%u]=%d)", i, pos[i]);
829 expected_pos[i] = pos[i];
833 for (i = 0; i < 64; i++) {
834 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
835 print_key_info("Lkp", &rand_keys[i], pos[i]);
836 RETURN_IF_ERROR(pos[i] != expected_pos[i],
837 "failed to find key (pos[%u]=%d)", i, pos[i]);
841 for (i = 0; i < 64; i++) {
842 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
843 print_key_info("Add", &rand_keys[i], pos[i]);
844 RETURN_IF_ERROR(pos[i] != expected_pos[i],
845 "failed to add key (pos[%u]=%d)", i, pos[i]);
849 for (i = 0; i < 64; i++) {
850 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
851 print_key_info("Lkp", &rand_keys[i], pos[i]);
852 RETURN_IF_ERROR(pos[i] != expected_pos[i],
853 "failed to find key (pos[%u]=%d)", i, pos[i]);
856 /* Delete 1 key, check other keys are still found */
857 pos[35] = rte_hash_del_key(handle, &rand_keys[35]);
858 print_key_info("Del", &rand_keys[35], pos[35]);
859 RETURN_IF_ERROR(pos[35] != expected_pos[35],
860 "failed to delete key (pos[1]=%d)", pos[35]);
861 pos[20] = rte_hash_lookup(handle, &rand_keys[20]);
862 print_key_info("Lkp", &rand_keys[20], pos[20]);
863 RETURN_IF_ERROR(pos[20] != expected_pos[20],
864 "failed lookup after deleting key from same bucket "
865 "(pos[20]=%d)", pos[20]);
867 /* Go back to previous state */
868 pos[35] = rte_hash_add_key(handle, &rand_keys[35]);
869 print_key_info("Add", &rand_keys[35], pos[35]);
870 expected_pos[35] = pos[35];
871 RETURN_IF_ERROR(pos[35] < 0, "failed to add key (pos[1]=%d)", pos[35]);
874 for (i = 0; i < 64; i++) {
875 pos[i] = rte_hash_del_key(handle, &rand_keys[i]);
876 print_key_info("Del", &rand_keys[i], pos[i]);
877 RETURN_IF_ERROR(pos[i] != expected_pos[i],
878 "failed to delete key (pos[%u]=%d)", i, pos[i]);
882 for (i = 0; i < 64; i++) {
883 pos[i] = rte_hash_lookup(handle, &rand_keys[i]);
884 print_key_info("Lkp", &rand_keys[i], pos[i]);
885 RETURN_IF_ERROR(pos[i] != -ENOENT,
886 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
890 for (i = 0; i < 64; i++) {
891 pos[i] = rte_hash_add_key(handle, &rand_keys[i]);
892 print_key_info("Add", &rand_keys[i], pos[i]);
893 RETURN_IF_ERROR(pos[i] < 0,
894 "failed to add key (pos[%u]=%d)", i, pos[i]);
895 expected_pos[i] = pos[i];
898 rte_hash_free(handle);
900 /* Cover the NULL case. */
905 /******************************************************************************/
907 fbk_hash_unit_test(void)
909 struct rte_fbk_hash_params params = {
910 .name = "fbk_hash_test",
911 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
912 .entries_per_bucket = 4,
916 struct rte_fbk_hash_params invalid_params_1 = {
918 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
919 .entries_per_bucket = 4,
923 struct rte_fbk_hash_params invalid_params_2 = {
926 .entries_per_bucket = 3, /* Not power of 2 */
930 struct rte_fbk_hash_params invalid_params_3 = {
932 .entries = 0, /* Entries is 0 */
933 .entries_per_bucket = 4,
937 struct rte_fbk_hash_params invalid_params_4 = {
939 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
940 .entries_per_bucket = 0, /* Entries per bucket is 0 */
944 struct rte_fbk_hash_params invalid_params_5 = {
947 .entries_per_bucket = 8, /* Entries per bucket > entries */
951 struct rte_fbk_hash_params invalid_params_6 = {
953 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
954 .entries_per_bucket = 4,
958 struct rte_fbk_hash_params invalid_params_7 = {
960 .entries = RTE_FBK_HASH_ENTRIES_MAX,
961 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
965 struct rte_fbk_hash_params invalid_params_8 = {
967 .entries = RTE_FBK_HASH_ENTRIES_MAX,
968 .entries_per_bucket = 4,
969 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
972 /* try to create two hashes with identical names
973 * in this case, trying to create a second one will not
974 * fail but will simply return pointer to the existing
975 * hash with that name. sort of like a "find hash by name" :-)
977 struct rte_fbk_hash_params invalid_params_same_name_1 = {
978 .name = "same_name", /* hash with identical name */
980 .entries_per_bucket = 2,
984 /* trying to create this hash should return a pointer to an existing hash */
985 struct rte_fbk_hash_params invalid_params_same_name_2 = {
986 .name = "same_name", /* hash with identical name */
987 .entries = RTE_FBK_HASH_ENTRIES_MAX,
988 .entries_per_bucket = 4,
992 /* this is a sanity check for "same name" test
993 * creating this hash will check if we are actually able to create
994 * multiple hashes with different names (instead of having just one).
996 struct rte_fbk_hash_params different_name = {
997 .name = "different_name", /* different name */
998 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
999 .entries_per_bucket = 4,
1003 struct rte_fbk_hash_params params_jhash = {
1005 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1006 .entries_per_bucket = 4,
1008 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
1009 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1012 struct rte_fbk_hash_params params_nohash = {
1013 .name = "valid nohash",
1014 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1015 .entries_per_bucket = 4,
1017 .hash_func = NULL, /* Tests for null hash_func */
1018 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
1021 struct rte_fbk_hash_table *handle, *tmp;
1023 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
1024 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
1027 double used_entries;
1029 /* Try creating hashes with invalid parameters */
1030 printf("# Testing hash creation with invalid parameters "
1031 "- expect error msgs\n");
1032 handle = rte_fbk_hash_create(&invalid_params_1);
1033 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1035 handle = rte_fbk_hash_create(&invalid_params_2);
1036 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1038 handle = rte_fbk_hash_create(&invalid_params_3);
1039 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1041 handle = rte_fbk_hash_create(&invalid_params_4);
1042 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1044 handle = rte_fbk_hash_create(&invalid_params_5);
1045 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1047 handle = rte_fbk_hash_create(&invalid_params_6);
1048 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1050 handle = rte_fbk_hash_create(&invalid_params_7);
1051 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1053 handle = rte_fbk_hash_create(&invalid_params_8);
1054 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
1056 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
1057 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
1059 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
1061 rte_fbk_hash_free(tmp);
1062 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
1064 /* we are not freeing handle here because we need a hash list
1065 * to be not empty for the next test */
1067 /* create a hash in non-empty list - good for coverage */
1068 tmp = rte_fbk_hash_create(&different_name);
1069 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
1071 /* free both hashes */
1072 rte_fbk_hash_free(handle);
1073 rte_fbk_hash_free(tmp);
1075 /* Create empty jhash hash. */
1076 handle = rte_fbk_hash_create(¶ms_jhash);
1077 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
1080 rte_fbk_hash_free(handle);
1082 /* Create empty jhash hash. */
1083 handle = rte_fbk_hash_create(¶ms_nohash);
1084 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
1087 rte_fbk_hash_free(handle);
1089 /* Create empty hash. */
1090 handle = rte_fbk_hash_create(¶ms);
1091 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1093 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1094 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1095 "load factor right after creation is not zero but it should be");
1097 for (i = 0; i < 5; i++) {
1098 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1099 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1102 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1103 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
1104 "load factor now is not as expected");
1105 /* Find value of added keys. */
1106 for (i = 0; i < 5; i++) {
1107 status = rte_fbk_hash_lookup(handle, keys[i]);
1108 RETURN_IF_ERROR_FBK(status != vals[i],
1109 "fbk hash lookup failed");
1112 /* Change value of added keys. */
1113 for (i = 0; i < 5; i++) {
1114 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
1115 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
1118 /* Find new values. */
1119 for (i = 0; i < 5; i++) {
1120 status = rte_fbk_hash_lookup(handle, keys[i]);
1121 RETURN_IF_ERROR_FBK(status != vals[4-i],
1122 "fbk hash lookup failed");
1125 /* Delete keys individually. */
1126 for (i = 0; i < 5; i++) {
1127 status = rte_fbk_hash_delete_key(handle, keys[i]);
1128 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
1131 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
1132 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
1133 "load factor right after deletion is not zero but it should be");
1134 /* Lookup should now fail. */
1135 for (i = 0; i < 5; i++) {
1136 status = rte_fbk_hash_lookup(handle, keys[i]);
1137 RETURN_IF_ERROR_FBK(status == 0,
1138 "fbk hash lookup should have failed");
1141 /* Add keys again. */
1142 for (i = 0; i < 5; i++) {
1143 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
1144 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
1147 /* Make sure they were added. */
1148 for (i = 0; i < 5; i++) {
1149 status = rte_fbk_hash_lookup(handle, keys[i]);
1150 RETURN_IF_ERROR_FBK(status != vals[i],
1151 "fbk hash lookup failed");
1154 /* Clear all entries. */
1155 rte_fbk_hash_clear_all(handle);
1157 /* Lookup should fail. */
1158 for (i = 0; i < 5; i++) {
1159 status = rte_fbk_hash_lookup(handle, keys[i]);
1160 RETURN_IF_ERROR_FBK(status == 0,
1161 "fbk hash lookup should have failed");
1166 /* fill up the hash_table */
1167 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
1168 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
1170 /* Find non-existent key in a full hashtable */
1171 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1172 RETURN_IF_ERROR_FBK(status != -ENOENT,
1173 "fbk hash lookup succeeded");
1175 /* Delete non-existent key in a full hashtable */
1176 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
1177 RETURN_IF_ERROR_FBK(status != -ENOENT,
1178 "fbk hash delete succeeded");
1180 /* Delete one key from a full hashtable */
1181 status = rte_fbk_hash_delete_key(handle, 1);
1182 RETURN_IF_ERROR_FBK(status != 0,
1183 "fbk hash delete failed");
1185 /* Clear all entries. */
1186 rte_fbk_hash_clear_all(handle);
1189 rte_fbk_hash_free(handle);
1191 /* Cover the NULL case. */
1192 rte_fbk_hash_free(0);
1198 * Sequence of operations for find existing fbk hash table
1201 * - find existing table: hit
1202 * - find non-existing table: miss
1205 static int test_fbk_hash_find_existing(void)
1207 struct rte_fbk_hash_params params = {
1208 .name = "fbk_hash_find_existing",
1209 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
1210 .entries_per_bucket = 4,
1213 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
1215 /* Create hash table. */
1216 handle = rte_fbk_hash_create(¶ms);
1217 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1219 /* Try to find existing fbk hash table */
1220 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1221 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1223 /* Try to find non-existing fbk hash table */
1224 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1225 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1228 rte_fbk_hash_free(handle);
1233 #define BUCKET_ENTRIES 4
1235 * Do tests for hash creation with bad parameters.
1237 static int test_hash_creation_with_bad_parameters(void)
1239 struct rte_hash *handle, *tmp;
1240 struct rte_hash_parameters params;
1242 handle = rte_hash_create(NULL);
1243 if (handle != NULL) {
1244 rte_hash_free(handle);
1245 printf("Impossible creating hash successfully without any parameter\n");
1249 memcpy(¶ms, &ut_params, sizeof(params));
1250 params.name = "creation_with_bad_parameters_0";
1251 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1252 handle = rte_hash_create(¶ms);
1253 if (handle != NULL) {
1254 rte_hash_free(handle);
1255 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1259 memcpy(¶ms, &ut_params, sizeof(params));
1260 params.name = "creation_with_bad_parameters_2";
1261 params.entries = BUCKET_ENTRIES - 1;
1262 handle = rte_hash_create(¶ms);
1263 if (handle != NULL) {
1264 rte_hash_free(handle);
1265 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1269 memcpy(¶ms, &ut_params, sizeof(params));
1270 params.name = "creation_with_bad_parameters_3";
1272 handle = rte_hash_create(¶ms);
1273 if (handle != NULL) {
1274 rte_hash_free(handle);
1275 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1279 memcpy(¶ms, &ut_params, sizeof(params));
1280 params.name = "creation_with_bad_parameters_4";
1281 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1282 handle = rte_hash_create(¶ms);
1283 if (handle != NULL) {
1284 rte_hash_free(handle);
1285 printf("Impossible creating hash successfully with invalid socket\n");
1289 /* test with same name should fail */
1290 memcpy(¶ms, &ut_params, sizeof(params));
1291 params.name = "same_name";
1292 handle = rte_hash_create(¶ms);
1293 if (handle == NULL) {
1294 printf("Cannot create first hash table with 'same_name'\n");
1297 tmp = rte_hash_create(¶ms);
1299 printf("Creation of hash table with same name should fail\n");
1300 rte_hash_free(handle);
1304 rte_hash_free(handle);
1306 printf("# Test successful. No more errors expected\n");
1312 * Do tests for hash creation with parameters that look incorrect
1313 * but are actually valid.
1316 test_hash_creation_with_good_parameters(void)
1318 struct rte_hash *handle;
1319 struct rte_hash_parameters params;
1321 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1322 memcpy(¶ms, &ut_params, sizeof(params));
1323 params.name = "name";
1324 params.hash_func = NULL;
1325 handle = rte_hash_create(¶ms);
1326 if (handle == NULL) {
1327 printf("Creating hash with null hash_func failed\n");
1331 rte_hash_free(handle);
1336 #define ITERATIONS 3
1338 * Test to see the average table utilization (entries added/max entries)
1339 * before hitting a random entry that cannot be added
1341 static int test_average_table_utilization(uint32_t ext_table)
1343 struct rte_hash *handle;
1344 uint8_t simple_key[MAX_KEYSIZE];
1346 unsigned added_keys, average_keys_added = 0;
1350 printf("\n# Running test to determine average utilization"
1351 "\n before adding elements begins to fail\n");
1353 printf("ext table is enabled\n");
1355 printf("ext table is disabled\n");
1357 printf("Measuring performance, please wait");
1359 ut_params.entries = 1 << 16;
1360 ut_params.name = "test_average_utilization";
1361 ut_params.hash_func = rte_jhash;
1363 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1365 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1367 handle = rte_hash_create(&ut_params);
1369 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1371 for (j = 0; j < ITERATIONS; j++) {
1373 /* Add random entries until key cannot be added */
1374 for (added_keys = 0; ret >= 0; added_keys++) {
1375 for (i = 0; i < ut_params.key_len; i++)
1376 simple_key[i] = rte_rand() % 255;
1377 ret = rte_hash_add_key(handle, simple_key);
1382 if (ret != -ENOSPC) {
1383 printf("Unexpected error when adding keys\n");
1384 rte_hash_free(handle);
1388 cnt = rte_hash_count(handle);
1389 if (cnt != added_keys) {
1390 printf("rte_hash_count returned wrong value %u, %u,"
1391 "%u\n", j, added_keys, cnt);
1392 rte_hash_free(handle);
1396 if (cnt != ut_params.entries) {
1397 printf("rte_hash_count returned wrong value "
1398 "%u, %u, %u\n", j, added_keys, cnt);
1399 rte_hash_free(handle);
1404 average_keys_added += added_keys;
1406 /* Reset the table */
1407 rte_hash_reset(handle);
1409 /* Print a dot to show progress on operations */
1414 average_keys_added /= ITERATIONS;
1416 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1417 ((double) average_keys_added / ut_params.entries * 100),
1418 average_keys_added, ut_params.entries);
1419 rte_hash_free(handle);
1424 #define NUM_ENTRIES 256
1425 static int test_hash_iteration(uint32_t ext_table)
1427 struct rte_hash *handle;
1429 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1430 const void *next_key;
1432 void *data[NUM_ENTRIES];
1433 unsigned added_keys;
1437 ut_params.entries = NUM_ENTRIES;
1438 ut_params.name = "test_hash_iteration";
1439 ut_params.hash_func = rte_jhash;
1440 ut_params.key_len = 16;
1442 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1444 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
1446 handle = rte_hash_create(&ut_params);
1447 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1449 /* Add random entries until key cannot be added */
1450 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1451 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1452 for (i = 0; i < ut_params.key_len; i++)
1453 keys[added_keys][i] = rte_rand() % 255;
1454 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1457 printf("Insertion failed for ext table\n");
1464 /* Iterate through the hash table */
1465 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1466 /* Search for the key in the list of keys added */
1467 for (i = 0; i < NUM_ENTRIES; i++) {
1468 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1469 if (next_data != data[i]) {
1470 printf("Data found in the hash table is"
1471 "not the data added with the key\n");
1478 if (i == NUM_ENTRIES) {
1479 printf("Key found in the hash table was not added\n");
1484 /* Check if all keys have been iterated */
1485 if (added_keys != 0) {
1486 printf("There were still %u keys to iterate\n", added_keys);
1490 rte_hash_free(handle);
1494 rte_hash_free(handle);
1498 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1499 0x04, 0x05, 0x06, 0x07,
1500 0x08, 0x09, 0x0a, 0x0b,
1501 0x0c, 0x0d, 0x0e, 0x0f};
1502 static struct rte_hash_parameters hash_params_ex = {
1507 .hash_func_init_val = 0,
1512 * add/delete key with jhash2
1515 test_hash_add_delete_jhash2(void)
1518 struct rte_hash *handle;
1521 hash_params_ex.name = "hash_test_jhash2";
1522 hash_params_ex.key_len = 4;
1523 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1525 handle = rte_hash_create(&hash_params_ex);
1526 if (handle == NULL) {
1527 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1530 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1532 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1536 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1537 if (pos2 < 0 || pos1 != pos2) {
1538 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1545 rte_hash_free(handle);
1551 * add/delete (2) key with jhash2
1554 test_hash_add_delete_2_jhash2(void)
1557 struct rte_hash *handle;
1560 hash_params_ex.name = "hash_test_2_jhash2";
1561 hash_params_ex.key_len = 8;
1562 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1564 handle = rte_hash_create(&hash_params_ex);
1568 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1572 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1573 if (pos2 < 0 || pos1 != pos2)
1580 rte_hash_free(handle);
1586 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1588 const uint32_t *k = key;
1590 RTE_SET_USED(length);
1592 return rte_jhash_1word(k[0], initval);
1596 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1598 const uint32_t *k = key;
1600 RTE_SET_USED(length);
1602 return rte_jhash_2words(k[0], k[1], initval);
1606 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1608 const uint32_t *k = key;
1610 RTE_SET_USED(length);
1612 return rte_jhash_3words(k[0], k[1], k[2], initval);
1616 * add/delete key with jhash 1word
1619 test_hash_add_delete_jhash_1word(void)
1622 struct rte_hash *handle;
1625 hash_params_ex.name = "hash_test_jhash_1word";
1626 hash_params_ex.key_len = 4;
1627 hash_params_ex.hash_func = test_hash_jhash_1word;
1629 handle = rte_hash_create(&hash_params_ex);
1631 goto fail_jhash_1word;
1633 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1635 goto fail_jhash_1word;
1637 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1638 if (pos2 < 0 || pos1 != pos2)
1639 goto fail_jhash_1word;
1645 rte_hash_free(handle);
1651 * add/delete key with jhash 2word
1654 test_hash_add_delete_jhash_2word(void)
1657 struct rte_hash *handle;
1660 hash_params_ex.name = "hash_test_jhash_2word";
1661 hash_params_ex.key_len = 8;
1662 hash_params_ex.hash_func = test_hash_jhash_2word;
1664 handle = rte_hash_create(&hash_params_ex);
1666 goto fail_jhash_2word;
1668 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1670 goto fail_jhash_2word;
1672 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1673 if (pos2 < 0 || pos1 != pos2)
1674 goto fail_jhash_2word;
1680 rte_hash_free(handle);
1686 * add/delete key with jhash 3word
1689 test_hash_add_delete_jhash_3word(void)
1692 struct rte_hash *handle;
1695 hash_params_ex.name = "hash_test_jhash_3word";
1696 hash_params_ex.key_len = 12;
1697 hash_params_ex.hash_func = test_hash_jhash_3word;
1699 handle = rte_hash_create(&hash_params_ex);
1701 goto fail_jhash_3word;
1703 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1705 goto fail_jhash_3word;
1707 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1708 if (pos2 < 0 || pos1 != pos2)
1709 goto fail_jhash_3word;
1715 rte_hash_free(handle);
1721 * Do all unit and performance tests.
1726 if (test_add_delete() < 0)
1728 if (test_hash_add_delete_jhash2() < 0)
1730 if (test_hash_add_delete_2_jhash2() < 0)
1732 if (test_hash_add_delete_jhash_1word() < 0)
1734 if (test_hash_add_delete_jhash_2word() < 0)
1736 if (test_hash_add_delete_jhash_3word() < 0)
1738 if (test_hash_get_key_with_position() < 0)
1740 if (test_hash_find_existing() < 0)
1742 if (test_add_update_delete() < 0)
1744 if (test_add_update_delete_free() < 0)
1746 if (test_five_keys() < 0)
1748 if (test_full_bucket() < 0)
1750 if (test_extendable_bucket() < 0)
1753 if (test_fbk_hash_find_existing() < 0)
1755 if (fbk_hash_unit_test() < 0)
1757 if (test_hash_creation_with_bad_parameters() < 0)
1759 if (test_hash_creation_with_good_parameters() < 0)
1762 /* ext table disabled */
1763 if (test_average_table_utilization(0) < 0)
1765 if (test_hash_iteration(0) < 0)
1768 /* ext table enabled */
1769 if (test_average_table_utilization(1) < 0)
1771 if (test_hash_iteration(1) < 0)
1774 run_hash_func_tests();
1776 if (test_crc32_hash_alg_equiv() < 0)
1782 REGISTER_TEST_COMMAND(hash_autotest, test_hash);