4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_malloc.h>
44 #include <rte_cycles.h>
45 #include <rte_random.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
50 #include <rte_string_fns.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57 #include <rte_hash_crc.h>
59 /*******************************************************************************
60 * Hash function performance test configuration section. Each performance test
61 * will be performed HASHTEST_ITERATIONS times.
63 * The five arrays below control what tests are performed. Every combination
64 * from the array entries is tested.
66 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
67 static uint32_t hashtest_initvals[] = {0};
68 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
69 #define MAX_KEYSIZE 64
70 /******************************************************************************/
71 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
74 * Check condition and return an error if true. Assumes that "handle" is the
75 * name of the hash structure pointer to be freed.
77 #define RETURN_IF_ERROR(cond, str, ...) do { \
79 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
80 if (handle) rte_hash_free(handle); \
85 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
87 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
88 if (handle) rte_fbk_hash_free(handle); \
93 /* 5-tuple key type */
100 } __attribute__((packed));
103 * Hash function that always returns the same value, to easily test what
104 * happens when a bucket is full.
106 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
107 __attribute__((unused)) uint32_t key_len,
108 __attribute__((unused)) uint32_t init_val)
114 * Print out result of unit test hash operation.
116 #if defined(UNIT_TEST_HASH_VERBOSE)
117 static void print_key_info(const char *msg, const struct flow_key *key,
120 uint8_t *p = (uint8_t *)key;
123 printf("%s key:0x", msg);
124 for (i = 0; i < sizeof(struct flow_key); i++) {
125 printf("%02X", p[i]);
127 printf(" @ pos %d\n", pos);
130 static void print_key_info(__attribute__((unused)) const char *msg,
131 __attribute__((unused)) const struct flow_key *key,
132 __attribute__((unused)) int32_t pos)
137 /* Keys used by unit test functions */
138 static struct flow_key keys[5] = { {
139 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
140 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
145 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
146 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
151 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
152 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
157 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
158 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
163 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
164 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
170 /* Parameters used for hash table in unit test functions. Name set later. */
171 static struct rte_hash_parameters ut_params = {
173 .key_len = sizeof(struct flow_key), /* 13 */
174 .hash_func = rte_jhash,
175 .hash_func_init_val = 0,
179 #define CRC32_ITERATIONS (1U << 20)
180 #define CRC32_DWORDS (1U << 6)
182 * Test if all CRC32 implementations yield the same hash value
185 test_crc32_hash_alg_equiv(void)
189 uint64_t data64[CRC32_DWORDS];
193 printf("\n# CRC32 implementations equivalence test\n");
194 for (i = 0; i < CRC32_ITERATIONS; i++) {
195 /* Randomizing data_len of data set */
196 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
197 init_val = (uint32_t) rte_rand();
199 /* Fill the data set */
200 for (j = 0; j < CRC32_DWORDS; j++)
201 data64[j] = rte_rand();
203 /* Calculate software CRC32 */
204 rte_hash_crc_set_alg(CRC32_SW);
205 hash_val = rte_hash_crc(data64, data_len, init_val);
207 /* Check against 4-byte-operand sse4.2 CRC32 if available */
208 rte_hash_crc_set_alg(CRC32_SSE42);
209 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
210 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
214 /* Check against 8-byte-operand sse4.2 CRC32 if available */
215 rte_hash_crc_set_alg(CRC32_SSE42_x64);
216 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
217 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
221 /* Check against 8-byte-operand ARM64 CRC32 if available */
222 rte_hash_crc_set_alg(CRC32_ARM64);
223 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
224 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
229 /* Resetting to best available algorithm */
230 rte_hash_crc_set_alg(CRC32_SSE42_x64);
232 if (i == CRC32_ITERATIONS)
235 printf("Failed test data (hex, %zu bytes total):\n", data_len);
236 for (j = 0; j < data_len; j++)
237 printf("%02X%c", ((uint8_t *)data64)[j],
238 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
244 * Test a hash function.
246 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
249 static uint8_t key[MAX_KEYSIZE];
253 for (i = 0; i < key_len; i++)
254 key[i] = (uint8_t) rte_rand();
256 /* just to be on the safe side */
260 f(key, key_len, init_val);
264 * Test all hash functions.
266 static void run_hash_func_tests(void)
271 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
274 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
277 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
279 run_hash_func_test(hashtest_funcs[i],
280 hashtest_initvals[j],
281 hashtest_key_lens[k]);
288 * Basic sequence of operations for a single key:
294 static int test_add_delete(void)
296 struct rte_hash *handle;
297 /* test with standard add/lookup/delete functions */
298 int pos0, expectedPos0;
300 ut_params.name = "test1";
301 handle = rte_hash_create(&ut_params);
302 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
304 pos0 = rte_hash_add_key(handle, &keys[0]);
305 print_key_info("Add", &keys[0], pos0);
306 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
309 pos0 = rte_hash_lookup(handle, &keys[0]);
310 print_key_info("Lkp", &keys[0], pos0);
311 RETURN_IF_ERROR(pos0 != expectedPos0,
312 "failed to find key (pos0=%d)", pos0);
314 pos0 = rte_hash_del_key(handle, &keys[0]);
315 print_key_info("Del", &keys[0], pos0);
316 RETURN_IF_ERROR(pos0 != expectedPos0,
317 "failed to delete key (pos0=%d)", pos0);
319 pos0 = rte_hash_lookup(handle, &keys[0]);
320 print_key_info("Lkp", &keys[0], pos0);
321 RETURN_IF_ERROR(pos0 != -ENOENT,
322 "fail: found key after deleting! (pos0=%d)", pos0);
324 rte_hash_free(handle);
326 /* repeat test with precomputed hash functions */
327 hash_sig_t hash_value;
328 int pos1, expectedPos1;
330 handle = rte_hash_create(&ut_params);
331 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
333 hash_value = rte_hash_hash(handle, &keys[0]);
334 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
335 print_key_info("Add", &keys[0], pos1);
336 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
339 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
340 print_key_info("Lkp", &keys[0], pos1);
341 RETURN_IF_ERROR(pos1 != expectedPos1,
342 "failed to find key (pos1=%d)", pos1);
344 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
345 print_key_info("Del", &keys[0], pos1);
346 RETURN_IF_ERROR(pos1 != expectedPos1,
347 "failed to delete key (pos1=%d)", pos1);
349 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
350 print_key_info("Lkp", &keys[0], pos1);
351 RETURN_IF_ERROR(pos1 != -ENOENT,
352 "fail: found key after deleting! (pos1=%d)", pos1);
354 rte_hash_free(handle);
360 * Sequence of operations for a single key:
365 * - lookup: hit (updated data)
370 static int test_add_update_delete(void)
372 struct rte_hash *handle;
373 int pos0, expectedPos0;
375 ut_params.name = "test2";
376 handle = rte_hash_create(&ut_params);
377 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
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: found non-existent key (pos0=%d)", pos0);
384 pos0 = rte_hash_add_key(handle, &keys[0]);
385 print_key_info("Add", &keys[0], pos0);
386 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
389 pos0 = rte_hash_lookup(handle, &keys[0]);
390 print_key_info("Lkp", &keys[0], pos0);
391 RETURN_IF_ERROR(pos0 != expectedPos0,
392 "failed to find key (pos0=%d)", pos0);
394 pos0 = rte_hash_add_key(handle, &keys[0]);
395 print_key_info("Add", &keys[0], pos0);
396 RETURN_IF_ERROR(pos0 != expectedPos0,
397 "failed to re-add key (pos0=%d)", pos0);
399 pos0 = rte_hash_lookup(handle, &keys[0]);
400 print_key_info("Lkp", &keys[0], pos0);
401 RETURN_IF_ERROR(pos0 != expectedPos0,
402 "failed to find key (pos0=%d)", pos0);
404 pos0 = rte_hash_del_key(handle, &keys[0]);
405 print_key_info("Del", &keys[0], pos0);
406 RETURN_IF_ERROR(pos0 != expectedPos0,
407 "failed to delete key (pos0=%d)", pos0);
409 pos0 = rte_hash_del_key(handle, &keys[0]);
410 print_key_info("Del", &keys[0], pos0);
411 RETURN_IF_ERROR(pos0 != -ENOENT,
412 "fail: deleted already deleted key (pos0=%d)", pos0);
414 pos0 = rte_hash_lookup(handle, &keys[0]);
415 print_key_info("Lkp", &keys[0], pos0);
416 RETURN_IF_ERROR(pos0 != -ENOENT,
417 "fail: found key after deleting! (pos0=%d)", pos0);
419 rte_hash_free(handle);
424 * Sequence of operations for find existing hash table
427 * - find existing table: hit
428 * - find non-existing table: miss
431 static int test_hash_find_existing(void)
433 struct rte_hash *handle = NULL, *result = NULL;
435 /* Create hash table. */
436 ut_params.name = "hash_find_existing";
437 handle = rte_hash_create(&ut_params);
438 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
440 /* Try to find existing hash table */
441 result = rte_hash_find_existing("hash_find_existing");
442 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
444 /* Try to find non-existing hash table */
445 result = rte_hash_find_existing("hash_find_non_existing");
446 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
449 rte_hash_free(handle);
455 * Sequence of operations for 5 keys
458 * - add keys (update)
459 * - lookup keys: hit (updated data)
460 * - delete keys : hit
461 * - lookup keys: miss
463 static int test_five_keys(void)
465 struct rte_hash *handle;
466 const void *key_array[5] = {0};
472 ut_params.name = "test3";
473 handle = rte_hash_create(&ut_params);
474 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
477 for (i = 0; i < 5; i++) {
478 pos[i] = rte_hash_add_key(handle, &keys[i]);
479 print_key_info("Add", &keys[i], pos[i]);
480 RETURN_IF_ERROR(pos[i] < 0,
481 "failed to add key (pos[%u]=%d)", i, pos[i]);
482 expected_pos[i] = pos[i];
486 for(i = 0; i < 5; i++)
487 key_array[i] = &keys[i];
489 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
491 for(i = 0; i < 5; i++) {
492 print_key_info("Lkp", key_array[i], pos[i]);
493 RETURN_IF_ERROR(pos[i] != expected_pos[i],
494 "failed to find key (pos[%u]=%d)", i, pos[i]);
498 for (i = 0; i < 5; i++) {
499 pos[i] = rte_hash_add_key(handle, &keys[i]);
500 print_key_info("Add", &keys[i], pos[i]);
501 RETURN_IF_ERROR(pos[i] != expected_pos[i],
502 "failed to add key (pos[%u]=%d)", i, pos[i]);
506 for (i = 0; i < 5; i++) {
507 pos[i] = rte_hash_lookup(handle, &keys[i]);
508 print_key_info("Lkp", &keys[i], pos[i]);
509 RETURN_IF_ERROR(pos[i] != expected_pos[i],
510 "failed to find key (pos[%u]=%d)", i, pos[i]);
514 for (i = 0; i < 5; i++) {
515 pos[i] = rte_hash_del_key(handle, &keys[i]);
516 print_key_info("Del", &keys[i], pos[i]);
517 RETURN_IF_ERROR(pos[i] != expected_pos[i],
518 "failed to delete key (pos[%u]=%d)", i, pos[i]);
522 for (i = 0; i < 5; i++) {
523 pos[i] = rte_hash_lookup(handle, &keys[i]);
524 print_key_info("Lkp", &keys[i], pos[i]);
525 RETURN_IF_ERROR(pos[i] != -ENOENT,
526 "found non-existent key (pos[%u]=%d)", i, pos[i]);
530 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
532 for (i = 0; i < 5; i++) {
533 print_key_info("Lkp", key_array[i], pos[i]);
534 RETURN_IF_ERROR(pos[i] != -ENOENT,
535 "found not-existent key (pos[%u]=%d)", i, pos[i]);
538 rte_hash_free(handle);
544 * Add keys to the same bucket until bucket full.
545 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
546 * first 4 successful, 5th successful, pushing existing item in bucket
547 * - lookup the 5 keys: 5 hits
548 * - add the 5 keys again: 5 OK
549 * - lookup the 5 keys: 5 hits (updated data)
550 * - delete the 5 keys: 5 OK
551 * - lookup the 5 keys: 5 misses
553 static int test_full_bucket(void)
555 struct rte_hash_parameters params_pseudo_hash = {
558 .key_len = sizeof(struct flow_key), /* 13 */
559 .hash_func = pseudo_hash,
560 .hash_func_init_val = 0,
563 struct rte_hash *handle;
568 handle = rte_hash_create(¶ms_pseudo_hash);
569 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
572 for (i = 0; i < 4; i++) {
573 pos[i] = rte_hash_add_key(handle, &keys[i]);
574 print_key_info("Add", &keys[i], pos[i]);
575 RETURN_IF_ERROR(pos[i] < 0,
576 "failed to add key (pos[%u]=%d)", i, pos[i]);
577 expected_pos[i] = pos[i];
580 * This should work and will push one of the items
581 * in the bucket because it is full
583 pos[4] = rte_hash_add_key(handle, &keys[4]);
584 print_key_info("Add", &keys[4], pos[4]);
585 RETURN_IF_ERROR(pos[4] < 0,
586 "failed to add key (pos[4]=%d)", pos[4]);
587 expected_pos[4] = pos[4];
590 for (i = 0; i < 5; i++) {
591 pos[i] = rte_hash_lookup(handle, &keys[i]);
592 print_key_info("Lkp", &keys[i], pos[i]);
593 RETURN_IF_ERROR(pos[i] != expected_pos[i],
594 "failed to find key (pos[%u]=%d)", i, pos[i]);
598 for (i = 0; i < 5; i++) {
599 pos[i] = rte_hash_add_key(handle, &keys[i]);
600 print_key_info("Add", &keys[i], pos[i]);
601 RETURN_IF_ERROR(pos[i] != expected_pos[i],
602 "failed to add key (pos[%u]=%d)", i, pos[i]);
606 for (i = 0; i < 5; i++) {
607 pos[i] = rte_hash_lookup(handle, &keys[i]);
608 print_key_info("Lkp", &keys[i], pos[i]);
609 RETURN_IF_ERROR(pos[i] != expected_pos[i],
610 "failed to find key (pos[%u]=%d)", i, pos[i]);
613 /* Delete 1 key, check other keys are still found */
614 pos[1] = rte_hash_del_key(handle, &keys[1]);
615 print_key_info("Del", &keys[1], pos[1]);
616 RETURN_IF_ERROR(pos[1] != expected_pos[1],
617 "failed to delete key (pos[1]=%d)", pos[1]);
618 pos[3] = rte_hash_lookup(handle, &keys[3]);
619 print_key_info("Lkp", &keys[3], pos[3]);
620 RETURN_IF_ERROR(pos[3] != expected_pos[3],
621 "failed lookup after deleting key from same bucket "
622 "(pos[3]=%d)", pos[3]);
624 /* Go back to previous state */
625 pos[1] = rte_hash_add_key(handle, &keys[1]);
626 print_key_info("Add", &keys[1], pos[1]);
627 expected_pos[1] = pos[1];
628 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
631 for (i = 0; i < 5; i++) {
632 pos[i] = rte_hash_del_key(handle, &keys[i]);
633 print_key_info("Del", &keys[i], pos[i]);
634 RETURN_IF_ERROR(pos[i] != expected_pos[i],
635 "failed to delete key (pos[%u]=%d)", i, pos[i]);
639 for (i = 0; i < 5; i++) {
640 pos[i] = rte_hash_lookup(handle, &keys[i]);
641 print_key_info("Lkp", &keys[i], pos[i]);
642 RETURN_IF_ERROR(pos[i] != -ENOENT,
643 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
646 rte_hash_free(handle);
648 /* Cover the NULL case. */
653 /******************************************************************************/
655 fbk_hash_unit_test(void)
657 struct rte_fbk_hash_params params = {
658 .name = "fbk_hash_test",
659 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
660 .entries_per_bucket = 4,
664 struct rte_fbk_hash_params invalid_params_1 = {
666 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
667 .entries_per_bucket = 4,
671 struct rte_fbk_hash_params invalid_params_2 = {
674 .entries_per_bucket = 3, /* Not power of 2 */
678 struct rte_fbk_hash_params invalid_params_3 = {
680 .entries = 0, /* Entries is 0 */
681 .entries_per_bucket = 4,
685 struct rte_fbk_hash_params invalid_params_4 = {
687 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
688 .entries_per_bucket = 0, /* Entries per bucket is 0 */
692 struct rte_fbk_hash_params invalid_params_5 = {
695 .entries_per_bucket = 8, /* Entries per bucket > entries */
699 struct rte_fbk_hash_params invalid_params_6 = {
701 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
702 .entries_per_bucket = 4,
706 struct rte_fbk_hash_params invalid_params_7 = {
708 .entries = RTE_FBK_HASH_ENTRIES_MAX,
709 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
713 struct rte_fbk_hash_params invalid_params_8 = {
715 .entries = RTE_FBK_HASH_ENTRIES_MAX,
716 .entries_per_bucket = 4,
717 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
720 /* try to create two hashes with identical names
721 * in this case, trying to create a second one will not
722 * fail but will simply return pointer to the existing
723 * hash with that name. sort of like a "find hash by name" :-)
725 struct rte_fbk_hash_params invalid_params_same_name_1 = {
726 .name = "same_name", /* hash with identical name */
728 .entries_per_bucket = 2,
732 /* trying to create this hash should return a pointer to an existing hash */
733 struct rte_fbk_hash_params invalid_params_same_name_2 = {
734 .name = "same_name", /* hash with identical name */
735 .entries = RTE_FBK_HASH_ENTRIES_MAX,
736 .entries_per_bucket = 4,
740 /* this is a sanity check for "same name" test
741 * creating this hash will check if we are actually able to create
742 * multiple hashes with different names (instead of having just one).
744 struct rte_fbk_hash_params different_name = {
745 .name = "different_name", /* different name */
746 .entries = RTE_FBK_HASH_ENTRIES_MAX,
747 .entries_per_bucket = 4,
751 struct rte_fbk_hash_params params_jhash = {
753 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
754 .entries_per_bucket = 4,
756 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
757 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
760 struct rte_fbk_hash_params params_nohash = {
761 .name = "valid nohash",
762 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
763 .entries_per_bucket = 4,
765 .hash_func = NULL, /* Tests for null hash_func */
766 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
769 struct rte_fbk_hash_table *handle, *tmp;
771 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
772 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
777 /* Try creating hashes with invalid parameters */
778 printf("# Testing hash creation with invalid parameters "
779 "- expect error msgs\n");
780 handle = rte_fbk_hash_create(&invalid_params_1);
781 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
783 handle = rte_fbk_hash_create(&invalid_params_2);
784 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
786 handle = rte_fbk_hash_create(&invalid_params_3);
787 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
789 handle = rte_fbk_hash_create(&invalid_params_4);
790 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
792 handle = rte_fbk_hash_create(&invalid_params_5);
793 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
795 handle = rte_fbk_hash_create(&invalid_params_6);
796 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
798 handle = rte_fbk_hash_create(&invalid_params_7);
799 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
801 handle = rte_fbk_hash_create(&invalid_params_8);
802 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
804 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
805 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
807 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
808 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
810 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
811 rte_fbk_hash_free(handle);
812 rte_fbk_hash_free(tmp);
816 /* we are not freeing tmp or handle here because we need a hash list
817 * to be not empty for the next test */
819 /* create a hash in non-empty list - good for coverage */
820 tmp = rte_fbk_hash_create(&different_name);
821 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
823 /* free both hashes */
824 rte_fbk_hash_free(handle);
825 rte_fbk_hash_free(tmp);
827 /* Create empty jhash hash. */
828 handle = rte_fbk_hash_create(¶ms_jhash);
829 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
832 rte_fbk_hash_free(handle);
834 /* Create empty jhash hash. */
835 handle = rte_fbk_hash_create(¶ms_nohash);
836 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
839 rte_fbk_hash_free(handle);
841 /* Create empty hash. */
842 handle = rte_fbk_hash_create(¶ms);
843 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
845 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
846 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
847 "load factor right after creation is not zero but it should be");
849 for (i = 0; i < 5; i++) {
850 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
851 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
854 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
855 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
856 "load factor now is not as expected");
857 /* Find value of added keys. */
858 for (i = 0; i < 5; i++) {
859 status = rte_fbk_hash_lookup(handle, keys[i]);
860 RETURN_IF_ERROR_FBK(status != vals[i],
861 "fbk hash lookup failed");
864 /* Change value of added keys. */
865 for (i = 0; i < 5; i++) {
866 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
867 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
870 /* Find new values. */
871 for (i = 0; i < 5; i++) {
872 status = rte_fbk_hash_lookup(handle, keys[i]);
873 RETURN_IF_ERROR_FBK(status != vals[4-i],
874 "fbk hash lookup failed");
877 /* Delete keys individually. */
878 for (i = 0; i < 5; i++) {
879 status = rte_fbk_hash_delete_key(handle, keys[i]);
880 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
883 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
884 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
885 "load factor right after deletion is not zero but it should be");
886 /* Lookup should now fail. */
887 for (i = 0; i < 5; i++) {
888 status = rte_fbk_hash_lookup(handle, keys[i]);
889 RETURN_IF_ERROR_FBK(status == 0,
890 "fbk hash lookup should have failed");
893 /* Add keys again. */
894 for (i = 0; i < 5; i++) {
895 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
896 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
899 /* Make sure they were added. */
900 for (i = 0; i < 5; i++) {
901 status = rte_fbk_hash_lookup(handle, keys[i]);
902 RETURN_IF_ERROR_FBK(status != vals[i],
903 "fbk hash lookup failed");
906 /* Clear all entries. */
907 rte_fbk_hash_clear_all(handle);
909 /* Lookup should fail. */
910 for (i = 0; i < 5; i++) {
911 status = rte_fbk_hash_lookup(handle, keys[i]);
912 RETURN_IF_ERROR_FBK(status == 0,
913 "fbk hash lookup should have failed");
918 /* fill up the hash_table */
919 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
920 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
922 /* Find non-existent key in a full hashtable */
923 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
924 RETURN_IF_ERROR_FBK(status != -ENOENT,
925 "fbk hash lookup succeeded");
927 /* Delete non-existent key in a full hashtable */
928 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
929 RETURN_IF_ERROR_FBK(status != -ENOENT,
930 "fbk hash delete succeeded");
932 /* Delete one key from a full hashtable */
933 status = rte_fbk_hash_delete_key(handle, 1);
934 RETURN_IF_ERROR_FBK(status != 0,
935 "fbk hash delete failed");
937 /* Clear all entries. */
938 rte_fbk_hash_clear_all(handle);
941 rte_fbk_hash_free(handle);
943 /* Cover the NULL case. */
944 rte_fbk_hash_free(0);
950 * Sequence of operations for find existing fbk hash table
953 * - find existing table: hit
954 * - find non-existing table: miss
957 static int test_fbk_hash_find_existing(void)
959 struct rte_fbk_hash_params params = {
960 .name = "fbk_hash_find_existing",
961 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
962 .entries_per_bucket = 4,
965 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
967 /* Create hash table. */
968 handle = rte_fbk_hash_create(¶ms);
969 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
971 /* Try to find existing fbk hash table */
972 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
973 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
975 /* Try to find non-existing fbk hash table */
976 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
977 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
980 rte_fbk_hash_free(handle);
985 #define BUCKET_ENTRIES 4
987 * Do tests for hash creation with bad parameters.
989 static int test_hash_creation_with_bad_parameters(void)
991 struct rte_hash *handle;
992 struct rte_hash_parameters params;
994 handle = rte_hash_create(NULL);
995 if (handle != NULL) {
996 rte_hash_free(handle);
997 printf("Impossible creating hash sucessfully without any parameter\n");
1001 memcpy(¶ms, &ut_params, sizeof(params));
1002 params.name = "creation_with_bad_parameters_0";
1003 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1004 handle = rte_hash_create(¶ms);
1005 if (handle != NULL) {
1006 rte_hash_free(handle);
1007 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1011 memcpy(¶ms, &ut_params, sizeof(params));
1012 params.name = "creation_with_bad_parameters_2";
1013 params.entries = BUCKET_ENTRIES - 1;
1014 handle = rte_hash_create(¶ms);
1015 if (handle != NULL) {
1016 rte_hash_free(handle);
1017 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1021 memcpy(¶ms, &ut_params, sizeof(params));
1022 params.name = "creation_with_bad_parameters_3";
1024 handle = rte_hash_create(¶ms);
1025 if (handle != NULL) {
1026 rte_hash_free(handle);
1027 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1031 memcpy(¶ms, &ut_params, sizeof(params));
1032 params.name = "creation_with_bad_parameters_4";
1033 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1034 handle = rte_hash_create(¶ms);
1035 if (handle != NULL) {
1036 rte_hash_free(handle);
1037 printf("Impossible creating hash sucessfully with invalid socket\n");
1041 rte_hash_free(handle);
1042 printf("# Test successful. No more errors expected\n");
1048 * Do tests for hash creation with parameters that look incorrect
1049 * but are actually valid.
1052 test_hash_creation_with_good_parameters(void)
1054 struct rte_hash *handle, *tmp;
1055 struct rte_hash_parameters params;
1057 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1058 memcpy(¶ms, &ut_params, sizeof(params));
1059 params.name = "same_name";
1060 params.hash_func = NULL;
1061 handle = rte_hash_create(¶ms);
1062 if (handle == NULL) {
1063 printf("Creating hash with null hash_func failed\n");
1067 /* this test is trying to create a hash with the same name as previous one.
1068 * this should return a pointer to the hash we previously created.
1069 * the previous hash isn't freed exactly for the purpose of it being in
1072 memcpy(¶ms, &ut_params, sizeof(params));
1073 params.name = "same_name";
1074 tmp = rte_hash_create(¶ms);
1076 /* check if the returned handle is actually equal to the previous hash */
1077 if (handle != tmp) {
1078 rte_hash_free(handle);
1080 printf("Creating hash with existing name was successful\n");
1084 /* try creating hash when there already are hashes in the list.
1085 * the previous hash is not freed to have a non-empty hash list.
1086 * the other hash that's in the list is still pointed to by "handle" var.
1088 memcpy(¶ms, &ut_params, sizeof(params));
1089 params.name = "different_name";
1090 tmp = rte_hash_create(¶ms);
1092 rte_hash_free(handle);
1093 printf("Creating hash with valid parameters failed\n");
1098 rte_hash_free(handle);
1103 #define ITERATIONS 50
1105 * Test to see the average table utilization (entries added/max entries)
1106 * before hitting a random entry that cannot be added
1108 static int test_average_table_utilization(void)
1110 struct rte_hash *handle;
1111 uint8_t simple_key[MAX_KEYSIZE];
1113 unsigned added_keys, average_keys_added = 0;
1116 printf("\n# Running test to determine average utilization"
1117 "\n before adding elements begins to fail\n");
1118 printf("Measuring performance, please wait");
1120 ut_params.entries = 1 << 20;
1121 ut_params.name = "test_average_utilization";
1122 ut_params.hash_func = rte_jhash;
1123 handle = rte_hash_create(&ut_params);
1124 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1126 for (j = 0; j < ITERATIONS; j++) {
1128 /* Add random entries until key cannot be added */
1129 for (added_keys = 0; ret >= 0; added_keys++) {
1130 for (i = 0; i < ut_params.key_len; i++)
1131 simple_key[i] = rte_rand() % 255;
1132 ret = rte_hash_add_key(handle, simple_key);
1134 if (ret != -ENOSPC) {
1135 printf("Unexpected error when adding keys\n");
1136 rte_hash_free(handle);
1140 average_keys_added += added_keys;
1142 /* Reset the table */
1143 rte_hash_reset(handle);
1145 /* Print a dot to show progress on operations */
1150 average_keys_added /= ITERATIONS;
1152 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1153 ((double) average_keys_added / ut_params.entries * 100),
1154 average_keys_added, ut_params.entries);
1155 rte_hash_free(handle);
1160 #define NUM_ENTRIES 1024
1161 static int test_hash_iteration(void)
1163 struct rte_hash *handle;
1165 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1166 const void *next_key;
1168 void *data[NUM_ENTRIES];
1169 unsigned added_keys;
1173 ut_params.entries = NUM_ENTRIES;
1174 ut_params.name = "test_hash_iteration";
1175 ut_params.hash_func = rte_jhash;
1176 ut_params.key_len = 16;
1177 handle = rte_hash_create(&ut_params);
1178 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1180 /* Add random entries until key cannot be added */
1181 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1182 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1183 for (i = 0; i < ut_params.key_len; i++)
1184 keys[added_keys][i] = rte_rand() % 255;
1185 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1190 /* Iterate through the hash table */
1191 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1192 /* Search for the key in the list of keys added */
1193 for (i = 0; i < NUM_ENTRIES; i++) {
1194 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1195 if (next_data != data[i]) {
1196 printf("Data found in the hash table is"
1197 "not the data added with the key\n");
1204 if (i == NUM_ENTRIES) {
1205 printf("Key found in the hash table was not added\n");
1210 /* Check if all keys have been iterated */
1211 if (added_keys != 0) {
1212 printf("There were still %u keys to iterate\n", added_keys);
1216 rte_hash_free(handle);
1220 rte_hash_free(handle);
1224 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1225 0x04, 0x05, 0x06, 0x07,
1226 0x08, 0x09, 0x0a, 0x0b,
1227 0x0c, 0x0d, 0x0e, 0x0f};
1228 static struct rte_hash_parameters hash_params_ex = {
1233 .hash_func_init_val = 0,
1238 * add/delete key with jhash2
1241 test_hash_add_delete_jhash2(void)
1244 struct rte_hash *handle;
1247 hash_params_ex.name = "hash_test_jhash2";
1248 hash_params_ex.key_len = 4;
1249 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1251 handle = rte_hash_create(&hash_params_ex);
1252 if (handle == NULL) {
1253 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1256 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1258 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1262 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1263 if (pos2 < 0 || pos1 != pos2) {
1264 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1271 rte_hash_free(handle);
1277 * add/delete (2) key with jhash2
1280 test_hash_add_delete_2_jhash2(void)
1283 struct rte_hash *handle;
1286 hash_params_ex.name = "hash_test_2_jhash2";
1287 hash_params_ex.key_len = 8;
1288 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1290 handle = rte_hash_create(&hash_params_ex);
1294 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1298 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1299 if (pos2 < 0 || pos1 != pos2)
1306 rte_hash_free(handle);
1312 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1314 const uint32_t *k = key;
1316 RTE_SET_USED(length);
1318 return rte_jhash_1word(k[0], initval);
1322 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1324 const uint32_t *k = key;
1326 RTE_SET_USED(length);
1328 return rte_jhash_2words(k[0], k[1], initval);
1332 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1334 const uint32_t *k = key;
1336 RTE_SET_USED(length);
1338 return rte_jhash_3words(k[0], k[1], k[2], initval);
1342 * add/delete key with jhash 1word
1345 test_hash_add_delete_jhash_1word(void)
1348 struct rte_hash *handle;
1351 hash_params_ex.name = "hash_test_jhash_1word";
1352 hash_params_ex.key_len = 4;
1353 hash_params_ex.hash_func = test_hash_jhash_1word;
1355 handle = rte_hash_create(&hash_params_ex);
1357 goto fail_jhash_1word;
1359 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1361 goto fail_jhash_1word;
1363 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1364 if (pos2 < 0 || pos1 != pos2)
1365 goto fail_jhash_1word;
1371 rte_hash_free(handle);
1377 * add/delete key with jhash 2word
1380 test_hash_add_delete_jhash_2word(void)
1383 struct rte_hash *handle;
1386 hash_params_ex.name = "hash_test_jhash_2word";
1387 hash_params_ex.key_len = 8;
1388 hash_params_ex.hash_func = test_hash_jhash_2word;
1390 handle = rte_hash_create(&hash_params_ex);
1392 goto fail_jhash_2word;
1394 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1396 goto fail_jhash_2word;
1398 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1399 if (pos2 < 0 || pos1 != pos2)
1400 goto fail_jhash_2word;
1406 rte_hash_free(handle);
1412 * add/delete key with jhash 3word
1415 test_hash_add_delete_jhash_3word(void)
1418 struct rte_hash *handle;
1421 hash_params_ex.name = "hash_test_jhash_3word";
1422 hash_params_ex.key_len = 12;
1423 hash_params_ex.hash_func = test_hash_jhash_3word;
1425 handle = rte_hash_create(&hash_params_ex);
1427 goto fail_jhash_3word;
1429 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1431 goto fail_jhash_3word;
1433 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1434 if (pos2 < 0 || pos1 != pos2)
1435 goto fail_jhash_3word;
1441 rte_hash_free(handle);
1447 * Do all unit and performance tests.
1452 if (test_add_delete() < 0)
1454 if (test_hash_add_delete_jhash2() < 0)
1456 if (test_hash_add_delete_2_jhash2() < 0)
1458 if (test_hash_add_delete_jhash_1word() < 0)
1460 if (test_hash_add_delete_jhash_2word() < 0)
1462 if (test_hash_add_delete_jhash_3word() < 0)
1464 if (test_hash_find_existing() < 0)
1466 if (test_add_update_delete() < 0)
1468 if (test_five_keys() < 0)
1470 if (test_full_bucket() < 0)
1473 if (test_fbk_hash_find_existing() < 0)
1475 if (fbk_hash_unit_test() < 0)
1477 if (test_hash_creation_with_bad_parameters() < 0)
1479 if (test_hash_creation_with_good_parameters() < 0)
1481 if (test_average_table_utilization() < 0)
1483 if (test_hash_iteration() < 0)
1486 run_hash_func_tests();
1488 if (test_crc32_hash_alg_equiv() < 0)
1494 static struct test_command hash_cmd = {
1495 .command = "hash_autotest",
1496 .callback = test_hash,
1498 REGISTER_TEST_COMMAND(hash_cmd);