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)
84 * Print out result of unit test hash operation.
86 #if defined(UNIT_TEST_HASH_VERBOSE)
87 static void print_key_info(const char *msg, const struct flow_key *key,
90 uint8_t *p = (uint8_t *)key;
93 printf("%s key:0x", msg);
94 for (i = 0; i < sizeof(struct flow_key); i++) {
97 printf(" @ pos %d\n", pos);
100 static void print_key_info(__attribute__((unused)) const char *msg,
101 __attribute__((unused)) const struct flow_key *key,
102 __attribute__((unused)) int32_t pos)
107 /* Keys used by unit test functions */
108 static struct flow_key keys[5] = { {
109 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
110 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
115 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
116 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
121 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
122 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
127 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
128 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
133 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
134 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
140 /* Parameters used for hash table in unit test functions. Name set later. */
141 static struct rte_hash_parameters ut_params = {
143 .key_len = sizeof(struct flow_key), /* 13 */
144 .hash_func = rte_jhash,
145 .hash_func_init_val = 0,
149 #define CRC32_ITERATIONS (1U << 10)
150 #define CRC32_DWORDS (1U << 6)
152 * Test if all CRC32 implementations yield the same hash value
155 test_crc32_hash_alg_equiv(void)
159 uint64_t data64[CRC32_DWORDS];
163 printf("\n# CRC32 implementations equivalence test\n");
164 for (i = 0; i < CRC32_ITERATIONS; i++) {
165 /* Randomizing data_len of data set */
166 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
167 init_val = (uint32_t) rte_rand();
169 /* Fill the data set */
170 for (j = 0; j < CRC32_DWORDS; j++)
171 data64[j] = rte_rand();
173 /* Calculate software CRC32 */
174 rte_hash_crc_set_alg(CRC32_SW);
175 hash_val = rte_hash_crc(data64, data_len, init_val);
177 /* Check against 4-byte-operand sse4.2 CRC32 if available */
178 rte_hash_crc_set_alg(CRC32_SSE42);
179 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
180 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
184 /* Check against 8-byte-operand sse4.2 CRC32 if available */
185 rte_hash_crc_set_alg(CRC32_SSE42_x64);
186 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
187 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
191 /* Check against 8-byte-operand ARM64 CRC32 if available */
192 rte_hash_crc_set_alg(CRC32_ARM64);
193 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
194 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
199 /* Resetting to best available algorithm */
200 rte_hash_crc_set_alg(CRC32_SSE42_x64);
202 if (i == CRC32_ITERATIONS)
205 printf("Failed test data (hex, %zu bytes total):\n", data_len);
206 for (j = 0; j < data_len; j++)
207 printf("%02X%c", ((uint8_t *)data64)[j],
208 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
214 * Test a hash function.
216 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
219 static uint8_t key[MAX_KEYSIZE];
223 for (i = 0; i < key_len; i++)
224 key[i] = (uint8_t) rte_rand();
226 /* just to be on the safe side */
230 f(key, key_len, init_val);
234 * Test all hash functions.
236 static void run_hash_func_tests(void)
241 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
244 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
247 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
249 run_hash_func_test(hashtest_funcs[i],
250 hashtest_initvals[j],
251 hashtest_key_lens[k]);
258 * Basic sequence of operations for a single key:
264 static int test_add_delete(void)
266 struct rte_hash *handle;
267 /* test with standard add/lookup/delete functions */
268 int pos0, expectedPos0;
270 ut_params.name = "test1";
271 handle = rte_hash_create(&ut_params);
272 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
274 pos0 = rte_hash_add_key(handle, &keys[0]);
275 print_key_info("Add", &keys[0], pos0);
276 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
279 pos0 = rte_hash_lookup(handle, &keys[0]);
280 print_key_info("Lkp", &keys[0], pos0);
281 RETURN_IF_ERROR(pos0 != expectedPos0,
282 "failed to find key (pos0=%d)", pos0);
284 pos0 = rte_hash_del_key(handle, &keys[0]);
285 print_key_info("Del", &keys[0], pos0);
286 RETURN_IF_ERROR(pos0 != expectedPos0,
287 "failed to delete key (pos0=%d)", pos0);
289 pos0 = rte_hash_lookup(handle, &keys[0]);
290 print_key_info("Lkp", &keys[0], pos0);
291 RETURN_IF_ERROR(pos0 != -ENOENT,
292 "fail: found key after deleting! (pos0=%d)", pos0);
294 rte_hash_free(handle);
296 /* repeat test with precomputed hash functions */
297 hash_sig_t hash_value;
298 int pos1, expectedPos1;
300 handle = rte_hash_create(&ut_params);
301 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
303 hash_value = rte_hash_hash(handle, &keys[0]);
304 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
305 print_key_info("Add", &keys[0], pos1);
306 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
309 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
310 print_key_info("Lkp", &keys[0], pos1);
311 RETURN_IF_ERROR(pos1 != expectedPos1,
312 "failed to find key (pos1=%d)", pos1);
314 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
315 print_key_info("Del", &keys[0], pos1);
316 RETURN_IF_ERROR(pos1 != expectedPos1,
317 "failed to delete key (pos1=%d)", pos1);
319 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
320 print_key_info("Lkp", &keys[0], pos1);
321 RETURN_IF_ERROR(pos1 != -ENOENT,
322 "fail: found key after deleting! (pos1=%d)", pos1);
324 rte_hash_free(handle);
330 * Sequence of operations for a single key:
335 * - lookup: hit (updated data)
340 static int test_add_update_delete(void)
342 struct rte_hash *handle;
343 int pos0, expectedPos0;
345 ut_params.name = "test2";
346 handle = rte_hash_create(&ut_params);
347 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
349 pos0 = rte_hash_del_key(handle, &keys[0]);
350 print_key_info("Del", &keys[0], pos0);
351 RETURN_IF_ERROR(pos0 != -ENOENT,
352 "fail: found non-existent key (pos0=%d)", pos0);
354 pos0 = rte_hash_add_key(handle, &keys[0]);
355 print_key_info("Add", &keys[0], pos0);
356 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
359 pos0 = rte_hash_lookup(handle, &keys[0]);
360 print_key_info("Lkp", &keys[0], pos0);
361 RETURN_IF_ERROR(pos0 != expectedPos0,
362 "failed to find key (pos0=%d)", pos0);
364 pos0 = rte_hash_add_key(handle, &keys[0]);
365 print_key_info("Add", &keys[0], pos0);
366 RETURN_IF_ERROR(pos0 != expectedPos0,
367 "failed to re-add key (pos0=%d)", pos0);
369 pos0 = rte_hash_lookup(handle, &keys[0]);
370 print_key_info("Lkp", &keys[0], pos0);
371 RETURN_IF_ERROR(pos0 != expectedPos0,
372 "failed to find key (pos0=%d)", pos0);
374 pos0 = rte_hash_del_key(handle, &keys[0]);
375 print_key_info("Del", &keys[0], pos0);
376 RETURN_IF_ERROR(pos0 != expectedPos0,
377 "failed to delete key (pos0=%d)", pos0);
379 pos0 = rte_hash_del_key(handle, &keys[0]);
380 print_key_info("Del", &keys[0], pos0);
381 RETURN_IF_ERROR(pos0 != -ENOENT,
382 "fail: deleted already deleted key (pos0=%d)", pos0);
384 pos0 = rte_hash_lookup(handle, &keys[0]);
385 print_key_info("Lkp", &keys[0], pos0);
386 RETURN_IF_ERROR(pos0 != -ENOENT,
387 "fail: found key after deleting! (pos0=%d)", pos0);
389 rte_hash_free(handle);
394 * Sequence of operations for retrieving a key with its position
398 * - get the key with its position: hit
400 * - try to get the deleted key: miss
403 static int test_hash_get_key_with_position(void)
405 struct rte_hash *handle = NULL;
406 int pos, expectedPos, result;
409 ut_params.name = "hash_get_key_w_pos";
410 handle = rte_hash_create(&ut_params);
411 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
413 pos = rte_hash_add_key(handle, &keys[0]);
414 print_key_info("Add", &keys[0], pos);
415 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
418 result = rte_hash_get_key_with_position(handle, pos, &key);
419 RETURN_IF_ERROR(result != 0, "error retrieving a key");
421 pos = rte_hash_del_key(handle, &keys[0]);
422 print_key_info("Del", &keys[0], pos);
423 RETURN_IF_ERROR(pos != expectedPos,
424 "failed to delete key (pos0=%d)", pos);
426 result = rte_hash_get_key_with_position(handle, pos, &key);
427 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
429 rte_hash_free(handle);
434 * Sequence of operations for find existing hash table
437 * - find existing table: hit
438 * - find non-existing table: miss
441 static int test_hash_find_existing(void)
443 struct rte_hash *handle = NULL, *result = NULL;
445 /* Create hash table. */
446 ut_params.name = "hash_find_existing";
447 handle = rte_hash_create(&ut_params);
448 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
450 /* Try to find existing hash table */
451 result = rte_hash_find_existing("hash_find_existing");
452 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
454 /* Try to find non-existing hash table */
455 result = rte_hash_find_existing("hash_find_non_existing");
456 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
459 rte_hash_free(handle);
465 * Sequence of operations for 5 keys
468 * - add keys (update)
469 * - lookup keys: hit (updated data)
470 * - delete keys : hit
471 * - lookup keys: miss
473 static int test_five_keys(void)
475 struct rte_hash *handle;
476 const void *key_array[5] = {0};
482 ut_params.name = "test3";
483 handle = rte_hash_create(&ut_params);
484 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
487 for (i = 0; i < 5; i++) {
488 pos[i] = rte_hash_add_key(handle, &keys[i]);
489 print_key_info("Add", &keys[i], pos[i]);
490 RETURN_IF_ERROR(pos[i] < 0,
491 "failed to add key (pos[%u]=%d)", i, pos[i]);
492 expected_pos[i] = pos[i];
496 for(i = 0; i < 5; i++)
497 key_array[i] = &keys[i];
499 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
501 for(i = 0; i < 5; i++) {
502 print_key_info("Lkp", key_array[i], pos[i]);
503 RETURN_IF_ERROR(pos[i] != expected_pos[i],
504 "failed to find key (pos[%u]=%d)", i, pos[i]);
508 for (i = 0; i < 5; i++) {
509 pos[i] = rte_hash_add_key(handle, &keys[i]);
510 print_key_info("Add", &keys[i], pos[i]);
511 RETURN_IF_ERROR(pos[i] != expected_pos[i],
512 "failed to add key (pos[%u]=%d)", i, pos[i]);
516 for (i = 0; i < 5; i++) {
517 pos[i] = rte_hash_lookup(handle, &keys[i]);
518 print_key_info("Lkp", &keys[i], pos[i]);
519 RETURN_IF_ERROR(pos[i] != expected_pos[i],
520 "failed to find key (pos[%u]=%d)", i, pos[i]);
524 for (i = 0; i < 5; i++) {
525 pos[i] = rte_hash_del_key(handle, &keys[i]);
526 print_key_info("Del", &keys[i], pos[i]);
527 RETURN_IF_ERROR(pos[i] != expected_pos[i],
528 "failed to delete key (pos[%u]=%d)", i, pos[i]);
532 for (i = 0; i < 5; i++) {
533 pos[i] = rte_hash_lookup(handle, &keys[i]);
534 print_key_info("Lkp", &keys[i], pos[i]);
535 RETURN_IF_ERROR(pos[i] != -ENOENT,
536 "found non-existent key (pos[%u]=%d)", i, pos[i]);
540 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
542 for (i = 0; i < 5; i++) {
543 print_key_info("Lkp", key_array[i], pos[i]);
544 RETURN_IF_ERROR(pos[i] != -ENOENT,
545 "found not-existent key (pos[%u]=%d)", i, pos[i]);
548 rte_hash_free(handle);
554 * Add keys to the same bucket until bucket full.
555 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
556 * first 4 successful, 5th successful, pushing existing item in bucket
557 * - lookup the 5 keys: 5 hits
558 * - add the 5 keys again: 5 OK
559 * - lookup the 5 keys: 5 hits (updated data)
560 * - delete the 5 keys: 5 OK
561 * - lookup the 5 keys: 5 misses
563 static int test_full_bucket(void)
565 struct rte_hash_parameters params_pseudo_hash = {
568 .key_len = sizeof(struct flow_key), /* 13 */
569 .hash_func = pseudo_hash,
570 .hash_func_init_val = 0,
573 struct rte_hash *handle;
578 handle = rte_hash_create(¶ms_pseudo_hash);
579 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
582 for (i = 0; i < 4; i++) {
583 pos[i] = rte_hash_add_key(handle, &keys[i]);
584 print_key_info("Add", &keys[i], pos[i]);
585 RETURN_IF_ERROR(pos[i] < 0,
586 "failed to add key (pos[%u]=%d)", i, pos[i]);
587 expected_pos[i] = pos[i];
590 * This should work and will push one of the items
591 * in the bucket because it is full
593 pos[4] = rte_hash_add_key(handle, &keys[4]);
594 print_key_info("Add", &keys[4], pos[4]);
595 RETURN_IF_ERROR(pos[4] < 0,
596 "failed to add key (pos[4]=%d)", pos[4]);
597 expected_pos[4] = pos[4];
600 for (i = 0; i < 5; i++) {
601 pos[i] = rte_hash_lookup(handle, &keys[i]);
602 print_key_info("Lkp", &keys[i], pos[i]);
603 RETURN_IF_ERROR(pos[i] != expected_pos[i],
604 "failed to find key (pos[%u]=%d)", i, pos[i]);
608 for (i = 0; i < 5; i++) {
609 pos[i] = rte_hash_add_key(handle, &keys[i]);
610 print_key_info("Add", &keys[i], pos[i]);
611 RETURN_IF_ERROR(pos[i] != expected_pos[i],
612 "failed to add key (pos[%u]=%d)", i, pos[i]);
616 for (i = 0; i < 5; i++) {
617 pos[i] = rte_hash_lookup(handle, &keys[i]);
618 print_key_info("Lkp", &keys[i], pos[i]);
619 RETURN_IF_ERROR(pos[i] != expected_pos[i],
620 "failed to find key (pos[%u]=%d)", i, pos[i]);
623 /* Delete 1 key, check other keys are still found */
624 pos[1] = rte_hash_del_key(handle, &keys[1]);
625 print_key_info("Del", &keys[1], pos[1]);
626 RETURN_IF_ERROR(pos[1] != expected_pos[1],
627 "failed to delete key (pos[1]=%d)", pos[1]);
628 pos[3] = rte_hash_lookup(handle, &keys[3]);
629 print_key_info("Lkp", &keys[3], pos[3]);
630 RETURN_IF_ERROR(pos[3] != expected_pos[3],
631 "failed lookup after deleting key from same bucket "
632 "(pos[3]=%d)", pos[3]);
634 /* Go back to previous state */
635 pos[1] = rte_hash_add_key(handle, &keys[1]);
636 print_key_info("Add", &keys[1], pos[1]);
637 expected_pos[1] = pos[1];
638 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
641 for (i = 0; i < 5; i++) {
642 pos[i] = rte_hash_del_key(handle, &keys[i]);
643 print_key_info("Del", &keys[i], pos[i]);
644 RETURN_IF_ERROR(pos[i] != expected_pos[i],
645 "failed to delete key (pos[%u]=%d)", i, pos[i]);
649 for (i = 0; i < 5; i++) {
650 pos[i] = rte_hash_lookup(handle, &keys[i]);
651 print_key_info("Lkp", &keys[i], pos[i]);
652 RETURN_IF_ERROR(pos[i] != -ENOENT,
653 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
656 rte_hash_free(handle);
658 /* Cover the NULL case. */
663 /******************************************************************************/
665 fbk_hash_unit_test(void)
667 struct rte_fbk_hash_params params = {
668 .name = "fbk_hash_test",
669 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
670 .entries_per_bucket = 4,
674 struct rte_fbk_hash_params invalid_params_1 = {
676 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
677 .entries_per_bucket = 4,
681 struct rte_fbk_hash_params invalid_params_2 = {
684 .entries_per_bucket = 3, /* Not power of 2 */
688 struct rte_fbk_hash_params invalid_params_3 = {
690 .entries = 0, /* Entries is 0 */
691 .entries_per_bucket = 4,
695 struct rte_fbk_hash_params invalid_params_4 = {
697 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
698 .entries_per_bucket = 0, /* Entries per bucket is 0 */
702 struct rte_fbk_hash_params invalid_params_5 = {
705 .entries_per_bucket = 8, /* Entries per bucket > entries */
709 struct rte_fbk_hash_params invalid_params_6 = {
711 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
712 .entries_per_bucket = 4,
716 struct rte_fbk_hash_params invalid_params_7 = {
718 .entries = RTE_FBK_HASH_ENTRIES_MAX,
719 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
723 struct rte_fbk_hash_params invalid_params_8 = {
725 .entries = RTE_FBK_HASH_ENTRIES_MAX,
726 .entries_per_bucket = 4,
727 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
730 /* try to create two hashes with identical names
731 * in this case, trying to create a second one will not
732 * fail but will simply return pointer to the existing
733 * hash with that name. sort of like a "find hash by name" :-)
735 struct rte_fbk_hash_params invalid_params_same_name_1 = {
736 .name = "same_name", /* hash with identical name */
738 .entries_per_bucket = 2,
742 /* trying to create this hash should return a pointer to an existing hash */
743 struct rte_fbk_hash_params invalid_params_same_name_2 = {
744 .name = "same_name", /* hash with identical name */
745 .entries = RTE_FBK_HASH_ENTRIES_MAX,
746 .entries_per_bucket = 4,
750 /* this is a sanity check for "same name" test
751 * creating this hash will check if we are actually able to create
752 * multiple hashes with different names (instead of having just one).
754 struct rte_fbk_hash_params different_name = {
755 .name = "different_name", /* different name */
756 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
757 .entries_per_bucket = 4,
761 struct rte_fbk_hash_params params_jhash = {
763 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
764 .entries_per_bucket = 4,
766 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
767 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
770 struct rte_fbk_hash_params params_nohash = {
771 .name = "valid nohash",
772 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
773 .entries_per_bucket = 4,
775 .hash_func = NULL, /* Tests for null hash_func */
776 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
779 struct rte_fbk_hash_table *handle, *tmp;
781 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
782 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
787 /* Try creating hashes with invalid parameters */
788 printf("# Testing hash creation with invalid parameters "
789 "- expect error msgs\n");
790 handle = rte_fbk_hash_create(&invalid_params_1);
791 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
793 handle = rte_fbk_hash_create(&invalid_params_2);
794 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
796 handle = rte_fbk_hash_create(&invalid_params_3);
797 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
799 handle = rte_fbk_hash_create(&invalid_params_4);
800 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
802 handle = rte_fbk_hash_create(&invalid_params_5);
803 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
805 handle = rte_fbk_hash_create(&invalid_params_6);
806 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
808 handle = rte_fbk_hash_create(&invalid_params_7);
809 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
811 handle = rte_fbk_hash_create(&invalid_params_8);
812 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
814 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
815 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
817 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
819 rte_fbk_hash_free(tmp);
820 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
822 /* we are not freeing handle here because we need a hash list
823 * to be not empty for the next test */
825 /* create a hash in non-empty list - good for coverage */
826 tmp = rte_fbk_hash_create(&different_name);
827 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
829 /* free both hashes */
830 rte_fbk_hash_free(handle);
831 rte_fbk_hash_free(tmp);
833 /* Create empty jhash hash. */
834 handle = rte_fbk_hash_create(¶ms_jhash);
835 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
838 rte_fbk_hash_free(handle);
840 /* Create empty jhash hash. */
841 handle = rte_fbk_hash_create(¶ms_nohash);
842 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
845 rte_fbk_hash_free(handle);
847 /* Create empty hash. */
848 handle = rte_fbk_hash_create(¶ms);
849 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
851 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
852 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
853 "load factor right after creation is not zero but it should be");
855 for (i = 0; i < 5; i++) {
856 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
857 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
860 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
861 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
862 "load factor now is not as expected");
863 /* Find value of added keys. */
864 for (i = 0; i < 5; i++) {
865 status = rte_fbk_hash_lookup(handle, keys[i]);
866 RETURN_IF_ERROR_FBK(status != vals[i],
867 "fbk hash lookup failed");
870 /* Change value of added keys. */
871 for (i = 0; i < 5; i++) {
872 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
873 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
876 /* Find new values. */
877 for (i = 0; i < 5; i++) {
878 status = rte_fbk_hash_lookup(handle, keys[i]);
879 RETURN_IF_ERROR_FBK(status != vals[4-i],
880 "fbk hash lookup failed");
883 /* Delete keys individually. */
884 for (i = 0; i < 5; i++) {
885 status = rte_fbk_hash_delete_key(handle, keys[i]);
886 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
889 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
890 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
891 "load factor right after deletion is not zero but it should be");
892 /* Lookup should now fail. */
893 for (i = 0; i < 5; i++) {
894 status = rte_fbk_hash_lookup(handle, keys[i]);
895 RETURN_IF_ERROR_FBK(status == 0,
896 "fbk hash lookup should have failed");
899 /* Add keys again. */
900 for (i = 0; i < 5; i++) {
901 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
902 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
905 /* Make sure they were added. */
906 for (i = 0; i < 5; i++) {
907 status = rte_fbk_hash_lookup(handle, keys[i]);
908 RETURN_IF_ERROR_FBK(status != vals[i],
909 "fbk hash lookup failed");
912 /* Clear all entries. */
913 rte_fbk_hash_clear_all(handle);
915 /* Lookup should fail. */
916 for (i = 0; i < 5; i++) {
917 status = rte_fbk_hash_lookup(handle, keys[i]);
918 RETURN_IF_ERROR_FBK(status == 0,
919 "fbk hash lookup should have failed");
924 /* fill up the hash_table */
925 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
926 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
928 /* Find non-existent key in a full hashtable */
929 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
930 RETURN_IF_ERROR_FBK(status != -ENOENT,
931 "fbk hash lookup succeeded");
933 /* Delete non-existent key in a full hashtable */
934 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
935 RETURN_IF_ERROR_FBK(status != -ENOENT,
936 "fbk hash delete succeeded");
938 /* Delete one key from a full hashtable */
939 status = rte_fbk_hash_delete_key(handle, 1);
940 RETURN_IF_ERROR_FBK(status != 0,
941 "fbk hash delete failed");
943 /* Clear all entries. */
944 rte_fbk_hash_clear_all(handle);
947 rte_fbk_hash_free(handle);
949 /* Cover the NULL case. */
950 rte_fbk_hash_free(0);
956 * Sequence of operations for find existing fbk hash table
959 * - find existing table: hit
960 * - find non-existing table: miss
963 static int test_fbk_hash_find_existing(void)
965 struct rte_fbk_hash_params params = {
966 .name = "fbk_hash_find_existing",
967 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
968 .entries_per_bucket = 4,
971 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
973 /* Create hash table. */
974 handle = rte_fbk_hash_create(¶ms);
975 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
977 /* Try to find existing fbk hash table */
978 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
979 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
981 /* Try to find non-existing fbk hash table */
982 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
983 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
986 rte_fbk_hash_free(handle);
991 #define BUCKET_ENTRIES 4
993 * Do tests for hash creation with bad parameters.
995 static int test_hash_creation_with_bad_parameters(void)
997 struct rte_hash *handle, *tmp;
998 struct rte_hash_parameters params;
1000 handle = rte_hash_create(NULL);
1001 if (handle != NULL) {
1002 rte_hash_free(handle);
1003 printf("Impossible creating hash successfully without any parameter\n");
1007 memcpy(¶ms, &ut_params, sizeof(params));
1008 params.name = "creation_with_bad_parameters_0";
1009 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1010 handle = rte_hash_create(¶ms);
1011 if (handle != NULL) {
1012 rte_hash_free(handle);
1013 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1017 memcpy(¶ms, &ut_params, sizeof(params));
1018 params.name = "creation_with_bad_parameters_2";
1019 params.entries = BUCKET_ENTRIES - 1;
1020 handle = rte_hash_create(¶ms);
1021 if (handle != NULL) {
1022 rte_hash_free(handle);
1023 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1027 memcpy(¶ms, &ut_params, sizeof(params));
1028 params.name = "creation_with_bad_parameters_3";
1030 handle = rte_hash_create(¶ms);
1031 if (handle != NULL) {
1032 rte_hash_free(handle);
1033 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1037 memcpy(¶ms, &ut_params, sizeof(params));
1038 params.name = "creation_with_bad_parameters_4";
1039 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1040 handle = rte_hash_create(¶ms);
1041 if (handle != NULL) {
1042 rte_hash_free(handle);
1043 printf("Impossible creating hash successfully with invalid socket\n");
1047 /* test with same name should fail */
1048 memcpy(¶ms, &ut_params, sizeof(params));
1049 params.name = "same_name";
1050 handle = rte_hash_create(¶ms);
1051 if (handle == NULL) {
1052 printf("Cannot create first hash table with 'same_name'\n");
1055 tmp = rte_hash_create(¶ms);
1057 printf("Creation of hash table with same name should fail\n");
1058 rte_hash_free(handle);
1062 rte_hash_free(handle);
1064 printf("# Test successful. No more errors expected\n");
1070 * Do tests for hash creation with parameters that look incorrect
1071 * but are actually valid.
1074 test_hash_creation_with_good_parameters(void)
1076 struct rte_hash *handle;
1077 struct rte_hash_parameters params;
1079 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1080 memcpy(¶ms, &ut_params, sizeof(params));
1081 params.name = "name";
1082 params.hash_func = NULL;
1083 handle = rte_hash_create(¶ms);
1084 if (handle == NULL) {
1085 printf("Creating hash with null hash_func failed\n");
1089 rte_hash_free(handle);
1094 #define ITERATIONS 3
1096 * Test to see the average table utilization (entries added/max entries)
1097 * before hitting a random entry that cannot be added
1099 static int test_average_table_utilization(void)
1101 struct rte_hash *handle;
1102 uint8_t simple_key[MAX_KEYSIZE];
1104 unsigned added_keys, average_keys_added = 0;
1107 printf("\n# Running test to determine average utilization"
1108 "\n before adding elements begins to fail\n");
1109 printf("Measuring performance, please wait");
1111 ut_params.entries = 1 << 16;
1112 ut_params.name = "test_average_utilization";
1113 ut_params.hash_func = rte_jhash;
1114 handle = rte_hash_create(&ut_params);
1115 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1117 for (j = 0; j < ITERATIONS; j++) {
1119 /* Add random entries until key cannot be added */
1120 for (added_keys = 0; ret >= 0; added_keys++) {
1121 for (i = 0; i < ut_params.key_len; i++)
1122 simple_key[i] = rte_rand() % 255;
1123 ret = rte_hash_add_key(handle, simple_key);
1125 if (ret != -ENOSPC) {
1126 printf("Unexpected error when adding keys\n");
1127 rte_hash_free(handle);
1131 average_keys_added += added_keys;
1133 /* Reset the table */
1134 rte_hash_reset(handle);
1136 /* Print a dot to show progress on operations */
1141 average_keys_added /= ITERATIONS;
1143 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1144 ((double) average_keys_added / ut_params.entries * 100),
1145 average_keys_added, ut_params.entries);
1146 rte_hash_free(handle);
1151 #define NUM_ENTRIES 256
1152 static int test_hash_iteration(void)
1154 struct rte_hash *handle;
1156 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1157 const void *next_key;
1159 void *data[NUM_ENTRIES];
1160 unsigned added_keys;
1164 ut_params.entries = NUM_ENTRIES;
1165 ut_params.name = "test_hash_iteration";
1166 ut_params.hash_func = rte_jhash;
1167 ut_params.key_len = 16;
1168 handle = rte_hash_create(&ut_params);
1169 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1171 /* Add random entries until key cannot be added */
1172 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1173 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1174 for (i = 0; i < ut_params.key_len; i++)
1175 keys[added_keys][i] = rte_rand() % 255;
1176 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1181 /* Iterate through the hash table */
1182 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1183 /* Search for the key in the list of keys added */
1184 for (i = 0; i < NUM_ENTRIES; i++) {
1185 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1186 if (next_data != data[i]) {
1187 printf("Data found in the hash table is"
1188 "not the data added with the key\n");
1195 if (i == NUM_ENTRIES) {
1196 printf("Key found in the hash table was not added\n");
1201 /* Check if all keys have been iterated */
1202 if (added_keys != 0) {
1203 printf("There were still %u keys to iterate\n", added_keys);
1207 rte_hash_free(handle);
1211 rte_hash_free(handle);
1215 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1216 0x04, 0x05, 0x06, 0x07,
1217 0x08, 0x09, 0x0a, 0x0b,
1218 0x0c, 0x0d, 0x0e, 0x0f};
1219 static struct rte_hash_parameters hash_params_ex = {
1224 .hash_func_init_val = 0,
1229 * add/delete key with jhash2
1232 test_hash_add_delete_jhash2(void)
1235 struct rte_hash *handle;
1238 hash_params_ex.name = "hash_test_jhash2";
1239 hash_params_ex.key_len = 4;
1240 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1242 handle = rte_hash_create(&hash_params_ex);
1243 if (handle == NULL) {
1244 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1247 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1249 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1253 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1254 if (pos2 < 0 || pos1 != pos2) {
1255 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1262 rte_hash_free(handle);
1268 * add/delete (2) key with jhash2
1271 test_hash_add_delete_2_jhash2(void)
1274 struct rte_hash *handle;
1277 hash_params_ex.name = "hash_test_2_jhash2";
1278 hash_params_ex.key_len = 8;
1279 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1281 handle = rte_hash_create(&hash_params_ex);
1285 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1289 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1290 if (pos2 < 0 || pos1 != pos2)
1297 rte_hash_free(handle);
1303 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1305 const uint32_t *k = key;
1307 RTE_SET_USED(length);
1309 return rte_jhash_1word(k[0], initval);
1313 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1315 const uint32_t *k = key;
1317 RTE_SET_USED(length);
1319 return rte_jhash_2words(k[0], k[1], initval);
1323 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1325 const uint32_t *k = key;
1327 RTE_SET_USED(length);
1329 return rte_jhash_3words(k[0], k[1], k[2], initval);
1333 * add/delete key with jhash 1word
1336 test_hash_add_delete_jhash_1word(void)
1339 struct rte_hash *handle;
1342 hash_params_ex.name = "hash_test_jhash_1word";
1343 hash_params_ex.key_len = 4;
1344 hash_params_ex.hash_func = test_hash_jhash_1word;
1346 handle = rte_hash_create(&hash_params_ex);
1348 goto fail_jhash_1word;
1350 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1352 goto fail_jhash_1word;
1354 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1355 if (pos2 < 0 || pos1 != pos2)
1356 goto fail_jhash_1word;
1362 rte_hash_free(handle);
1368 * add/delete key with jhash 2word
1371 test_hash_add_delete_jhash_2word(void)
1374 struct rte_hash *handle;
1377 hash_params_ex.name = "hash_test_jhash_2word";
1378 hash_params_ex.key_len = 8;
1379 hash_params_ex.hash_func = test_hash_jhash_2word;
1381 handle = rte_hash_create(&hash_params_ex);
1383 goto fail_jhash_2word;
1385 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1387 goto fail_jhash_2word;
1389 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1390 if (pos2 < 0 || pos1 != pos2)
1391 goto fail_jhash_2word;
1397 rte_hash_free(handle);
1403 * add/delete key with jhash 3word
1406 test_hash_add_delete_jhash_3word(void)
1409 struct rte_hash *handle;
1412 hash_params_ex.name = "hash_test_jhash_3word";
1413 hash_params_ex.key_len = 12;
1414 hash_params_ex.hash_func = test_hash_jhash_3word;
1416 handle = rte_hash_create(&hash_params_ex);
1418 goto fail_jhash_3word;
1420 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1422 goto fail_jhash_3word;
1424 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1425 if (pos2 < 0 || pos1 != pos2)
1426 goto fail_jhash_3word;
1432 rte_hash_free(handle);
1438 * Do all unit and performance tests.
1443 if (test_add_delete() < 0)
1445 if (test_hash_add_delete_jhash2() < 0)
1447 if (test_hash_add_delete_2_jhash2() < 0)
1449 if (test_hash_add_delete_jhash_1word() < 0)
1451 if (test_hash_add_delete_jhash_2word() < 0)
1453 if (test_hash_add_delete_jhash_3word() < 0)
1455 if (test_hash_get_key_with_position() < 0)
1457 if (test_hash_find_existing() < 0)
1459 if (test_add_update_delete() < 0)
1461 if (test_five_keys() < 0)
1463 if (test_full_bucket() < 0)
1466 if (test_fbk_hash_find_existing() < 0)
1468 if (fbk_hash_unit_test() < 0)
1470 if (test_hash_creation_with_bad_parameters() < 0)
1472 if (test_hash_creation_with_good_parameters() < 0)
1474 if (test_average_table_utilization() < 0)
1476 if (test_hash_iteration() < 0)
1479 run_hash_func_tests();
1481 if (test_crc32_hash_alg_equiv() < 0)
1487 REGISTER_TEST_COMMAND(hash_autotest, test_hash);