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_bulk(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_bulk(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);
809 rte_fbk_hash_free(tmp);
810 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
812 /* we are not freeing handle here because we need a hash list
813 * to be not empty for the next test */
815 /* create a hash in non-empty list - good for coverage */
816 tmp = rte_fbk_hash_create(&different_name);
817 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
819 /* free both hashes */
820 rte_fbk_hash_free(handle);
821 rte_fbk_hash_free(tmp);
823 /* Create empty jhash hash. */
824 handle = rte_fbk_hash_create(¶ms_jhash);
825 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
828 rte_fbk_hash_free(handle);
830 /* Create empty jhash hash. */
831 handle = rte_fbk_hash_create(¶ms_nohash);
832 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
835 rte_fbk_hash_free(handle);
837 /* Create empty hash. */
838 handle = rte_fbk_hash_create(¶ms);
839 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
841 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
842 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
843 "load factor right after creation is not zero but it should be");
845 for (i = 0; i < 5; i++) {
846 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
847 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
850 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
851 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
852 "load factor now is not as expected");
853 /* Find value of added keys. */
854 for (i = 0; i < 5; i++) {
855 status = rte_fbk_hash_lookup(handle, keys[i]);
856 RETURN_IF_ERROR_FBK(status != vals[i],
857 "fbk hash lookup failed");
860 /* Change value of added keys. */
861 for (i = 0; i < 5; i++) {
862 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
863 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
866 /* Find new values. */
867 for (i = 0; i < 5; i++) {
868 status = rte_fbk_hash_lookup(handle, keys[i]);
869 RETURN_IF_ERROR_FBK(status != vals[4-i],
870 "fbk hash lookup failed");
873 /* Delete keys individually. */
874 for (i = 0; i < 5; i++) {
875 status = rte_fbk_hash_delete_key(handle, keys[i]);
876 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
879 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
880 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
881 "load factor right after deletion is not zero but it should be");
882 /* Lookup should now fail. */
883 for (i = 0; i < 5; i++) {
884 status = rte_fbk_hash_lookup(handle, keys[i]);
885 RETURN_IF_ERROR_FBK(status == 0,
886 "fbk hash lookup should have failed");
889 /* Add keys again. */
890 for (i = 0; i < 5; i++) {
891 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
892 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
895 /* Make sure they were added. */
896 for (i = 0; i < 5; i++) {
897 status = rte_fbk_hash_lookup(handle, keys[i]);
898 RETURN_IF_ERROR_FBK(status != vals[i],
899 "fbk hash lookup failed");
902 /* Clear all entries. */
903 rte_fbk_hash_clear_all(handle);
905 /* Lookup should fail. */
906 for (i = 0; i < 5; i++) {
907 status = rte_fbk_hash_lookup(handle, keys[i]);
908 RETURN_IF_ERROR_FBK(status == 0,
909 "fbk hash lookup should have failed");
914 /* fill up the hash_table */
915 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
916 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
918 /* Find non-existent key in a full hashtable */
919 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
920 RETURN_IF_ERROR_FBK(status != -ENOENT,
921 "fbk hash lookup succeeded");
923 /* Delete non-existent key in a full hashtable */
924 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
925 RETURN_IF_ERROR_FBK(status != -ENOENT,
926 "fbk hash delete succeeded");
928 /* Delete one key from a full hashtable */
929 status = rte_fbk_hash_delete_key(handle, 1);
930 RETURN_IF_ERROR_FBK(status != 0,
931 "fbk hash delete failed");
933 /* Clear all entries. */
934 rte_fbk_hash_clear_all(handle);
937 rte_fbk_hash_free(handle);
939 /* Cover the NULL case. */
940 rte_fbk_hash_free(0);
946 * Sequence of operations for find existing fbk hash table
949 * - find existing table: hit
950 * - find non-existing table: miss
953 static int test_fbk_hash_find_existing(void)
955 struct rte_fbk_hash_params params = {
956 .name = "fbk_hash_find_existing",
957 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
958 .entries_per_bucket = 4,
961 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
963 /* Create hash table. */
964 handle = rte_fbk_hash_create(¶ms);
965 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
967 /* Try to find existing fbk hash table */
968 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
969 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
971 /* Try to find non-existing fbk hash table */
972 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
973 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
976 rte_fbk_hash_free(handle);
981 #define BUCKET_ENTRIES 4
983 * Do tests for hash creation with bad parameters.
985 static int test_hash_creation_with_bad_parameters(void)
987 struct rte_hash *handle, *tmp;
988 struct rte_hash_parameters params;
990 handle = rte_hash_create(NULL);
991 if (handle != NULL) {
992 rte_hash_free(handle);
993 printf("Impossible creating hash sucessfully without any parameter\n");
997 memcpy(¶ms, &ut_params, sizeof(params));
998 params.name = "creation_with_bad_parameters_0";
999 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1000 handle = rte_hash_create(¶ms);
1001 if (handle != NULL) {
1002 rte_hash_free(handle);
1003 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1007 memcpy(¶ms, &ut_params, sizeof(params));
1008 params.name = "creation_with_bad_parameters_2";
1009 params.entries = BUCKET_ENTRIES - 1;
1010 handle = rte_hash_create(¶ms);
1011 if (handle != NULL) {
1012 rte_hash_free(handle);
1013 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1017 memcpy(¶ms, &ut_params, sizeof(params));
1018 params.name = "creation_with_bad_parameters_3";
1020 handle = rte_hash_create(¶ms);
1021 if (handle != NULL) {
1022 rte_hash_free(handle);
1023 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1027 memcpy(¶ms, &ut_params, sizeof(params));
1028 params.name = "creation_with_bad_parameters_4";
1029 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1030 handle = rte_hash_create(¶ms);
1031 if (handle != NULL) {
1032 rte_hash_free(handle);
1033 printf("Impossible creating hash sucessfully with invalid socket\n");
1037 /* test with same name should fail */
1038 memcpy(¶ms, &ut_params, sizeof(params));
1039 params.name = "same_name";
1040 handle = rte_hash_create(¶ms);
1041 if (handle == NULL) {
1042 printf("Cannot create first hash table with 'same_name'\n");
1045 tmp = rte_hash_create(¶ms);
1047 printf("Creation of hash table with same name should fail\n");
1048 rte_hash_free(handle);
1052 rte_hash_free(handle);
1054 printf("# Test successful. No more errors expected\n");
1060 * Do tests for hash creation with parameters that look incorrect
1061 * but are actually valid.
1064 test_hash_creation_with_good_parameters(void)
1066 struct rte_hash *handle;
1067 struct rte_hash_parameters params;
1069 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1070 memcpy(¶ms, &ut_params, sizeof(params));
1071 params.name = "name";
1072 params.hash_func = NULL;
1073 handle = rte_hash_create(¶ms);
1074 if (handle == NULL) {
1075 printf("Creating hash with null hash_func failed\n");
1079 rte_hash_free(handle);
1084 #define ITERATIONS 50
1086 * Test to see the average table utilization (entries added/max entries)
1087 * before hitting a random entry that cannot be added
1089 static int test_average_table_utilization(void)
1091 struct rte_hash *handle;
1092 uint8_t simple_key[MAX_KEYSIZE];
1094 unsigned added_keys, average_keys_added = 0;
1097 printf("\n# Running test to determine average utilization"
1098 "\n before adding elements begins to fail\n");
1099 printf("Measuring performance, please wait");
1101 ut_params.entries = 1 << 20;
1102 ut_params.name = "test_average_utilization";
1103 ut_params.hash_func = rte_jhash;
1104 handle = rte_hash_create(&ut_params);
1105 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1107 for (j = 0; j < ITERATIONS; j++) {
1109 /* Add random entries until key cannot be added */
1110 for (added_keys = 0; ret >= 0; added_keys++) {
1111 for (i = 0; i < ut_params.key_len; i++)
1112 simple_key[i] = rte_rand() % 255;
1113 ret = rte_hash_add_key(handle, simple_key);
1115 if (ret != -ENOSPC) {
1116 printf("Unexpected error when adding keys\n");
1117 rte_hash_free(handle);
1121 average_keys_added += added_keys;
1123 /* Reset the table */
1124 rte_hash_reset(handle);
1126 /* Print a dot to show progress on operations */
1131 average_keys_added /= ITERATIONS;
1133 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1134 ((double) average_keys_added / ut_params.entries * 100),
1135 average_keys_added, ut_params.entries);
1136 rte_hash_free(handle);
1141 #define NUM_ENTRIES 1024
1142 static int test_hash_iteration(void)
1144 struct rte_hash *handle;
1146 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1147 const void *next_key;
1149 void *data[NUM_ENTRIES];
1150 unsigned added_keys;
1154 ut_params.entries = NUM_ENTRIES;
1155 ut_params.name = "test_hash_iteration";
1156 ut_params.hash_func = rte_jhash;
1157 ut_params.key_len = 16;
1158 handle = rte_hash_create(&ut_params);
1159 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1161 /* Add random entries until key cannot be added */
1162 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1163 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1164 for (i = 0; i < ut_params.key_len; i++)
1165 keys[added_keys][i] = rte_rand() % 255;
1166 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1171 /* Iterate through the hash table */
1172 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1173 /* Search for the key in the list of keys added */
1174 for (i = 0; i < NUM_ENTRIES; i++) {
1175 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1176 if (next_data != data[i]) {
1177 printf("Data found in the hash table is"
1178 "not the data added with the key\n");
1185 if (i == NUM_ENTRIES) {
1186 printf("Key found in the hash table was not added\n");
1191 /* Check if all keys have been iterated */
1192 if (added_keys != 0) {
1193 printf("There were still %u keys to iterate\n", added_keys);
1197 rte_hash_free(handle);
1201 rte_hash_free(handle);
1205 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1206 0x04, 0x05, 0x06, 0x07,
1207 0x08, 0x09, 0x0a, 0x0b,
1208 0x0c, 0x0d, 0x0e, 0x0f};
1209 static struct rte_hash_parameters hash_params_ex = {
1214 .hash_func_init_val = 0,
1219 * add/delete key with jhash2
1222 test_hash_add_delete_jhash2(void)
1225 struct rte_hash *handle;
1228 hash_params_ex.name = "hash_test_jhash2";
1229 hash_params_ex.key_len = 4;
1230 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1232 handle = rte_hash_create(&hash_params_ex);
1233 if (handle == NULL) {
1234 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1237 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1239 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1243 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1244 if (pos2 < 0 || pos1 != pos2) {
1245 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1252 rte_hash_free(handle);
1258 * add/delete (2) key with jhash2
1261 test_hash_add_delete_2_jhash2(void)
1264 struct rte_hash *handle;
1267 hash_params_ex.name = "hash_test_2_jhash2";
1268 hash_params_ex.key_len = 8;
1269 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1271 handle = rte_hash_create(&hash_params_ex);
1275 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1279 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1280 if (pos2 < 0 || pos1 != pos2)
1287 rte_hash_free(handle);
1293 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1295 const uint32_t *k = key;
1297 RTE_SET_USED(length);
1299 return rte_jhash_1word(k[0], initval);
1303 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1305 const uint32_t *k = key;
1307 RTE_SET_USED(length);
1309 return rte_jhash_2words(k[0], k[1], initval);
1313 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1315 const uint32_t *k = key;
1317 RTE_SET_USED(length);
1319 return rte_jhash_3words(k[0], k[1], k[2], initval);
1323 * add/delete key with jhash 1word
1326 test_hash_add_delete_jhash_1word(void)
1329 struct rte_hash *handle;
1332 hash_params_ex.name = "hash_test_jhash_1word";
1333 hash_params_ex.key_len = 4;
1334 hash_params_ex.hash_func = test_hash_jhash_1word;
1336 handle = rte_hash_create(&hash_params_ex);
1338 goto fail_jhash_1word;
1340 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1342 goto fail_jhash_1word;
1344 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1345 if (pos2 < 0 || pos1 != pos2)
1346 goto fail_jhash_1word;
1352 rte_hash_free(handle);
1358 * add/delete key with jhash 2word
1361 test_hash_add_delete_jhash_2word(void)
1364 struct rte_hash *handle;
1367 hash_params_ex.name = "hash_test_jhash_2word";
1368 hash_params_ex.key_len = 8;
1369 hash_params_ex.hash_func = test_hash_jhash_2word;
1371 handle = rte_hash_create(&hash_params_ex);
1373 goto fail_jhash_2word;
1375 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1377 goto fail_jhash_2word;
1379 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1380 if (pos2 < 0 || pos1 != pos2)
1381 goto fail_jhash_2word;
1387 rte_hash_free(handle);
1393 * add/delete key with jhash 3word
1396 test_hash_add_delete_jhash_3word(void)
1399 struct rte_hash *handle;
1402 hash_params_ex.name = "hash_test_jhash_3word";
1403 hash_params_ex.key_len = 12;
1404 hash_params_ex.hash_func = test_hash_jhash_3word;
1406 handle = rte_hash_create(&hash_params_ex);
1408 goto fail_jhash_3word;
1410 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1412 goto fail_jhash_3word;
1414 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1415 if (pos2 < 0 || pos1 != pos2)
1416 goto fail_jhash_3word;
1422 rte_hash_free(handle);
1428 * Do all unit and performance tests.
1433 if (test_add_delete() < 0)
1435 if (test_hash_add_delete_jhash2() < 0)
1437 if (test_hash_add_delete_2_jhash2() < 0)
1439 if (test_hash_add_delete_jhash_1word() < 0)
1441 if (test_hash_add_delete_jhash_2word() < 0)
1443 if (test_hash_add_delete_jhash_3word() < 0)
1445 if (test_hash_find_existing() < 0)
1447 if (test_add_update_delete() < 0)
1449 if (test_five_keys() < 0)
1451 if (test_full_bucket() < 0)
1454 if (test_fbk_hash_find_existing() < 0)
1456 if (fbk_hash_unit_test() < 0)
1458 if (test_hash_creation_with_bad_parameters() < 0)
1460 if (test_hash_creation_with_good_parameters() < 0)
1462 if (test_average_table_utilization() < 0)
1464 if (test_hash_iteration() < 0)
1467 run_hash_func_tests();
1469 if (test_crc32_hash_alg_equiv() < 0)
1475 static struct test_command hash_cmd = {
1476 .command = "hash_autotest",
1477 .callback = test_hash,
1479 REGISTER_TEST_COMMAND(hash_cmd);