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>
49 #include <rte_string_fns.h>
54 #include <rte_fbk_hash.h>
55 #include <rte_jhash.h>
56 #include <rte_hash_crc.h>
58 /*******************************************************************************
59 * Hash function performance test configuration section. Each performance test
60 * will be performed HASHTEST_ITERATIONS times.
62 * The five arrays below control what tests are performed. Every combination
63 * from the array entries is tested.
65 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
66 static uint32_t hashtest_initvals[] = {0};
67 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
68 #define MAX_KEYSIZE 64
69 /******************************************************************************/
70 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
73 * Check condition and return an error if true. Assumes that "handle" is the
74 * name of the hash structure pointer to be freed.
76 #define RETURN_IF_ERROR(cond, str, ...) do { \
78 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
79 if (handle) rte_hash_free(handle); \
84 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
86 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
87 if (handle) rte_fbk_hash_free(handle); \
92 /* 5-tuple key type */
99 } __attribute__((packed));
102 * Hash function that always returns the same value, to easily test what
103 * happens when a bucket is full.
105 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
106 __attribute__((unused)) uint32_t key_len,
107 __attribute__((unused)) uint32_t init_val)
112 #define UNIT_TEST_HASH_VERBOSE 0
114 * Print out result of unit test hash operation.
116 static void print_key_info(const char *msg, const struct flow_key *key,
119 if (UNIT_TEST_HASH_VERBOSE) {
120 const uint8_t *p = (const uint8_t *)key;
123 printf("%s key:0x", msg);
124 for (i = 0; i < sizeof(struct flow_key); i++)
125 printf("%02X", p[i]);
126 printf(" @ pos %d\n", pos);
130 /* Keys used by unit test functions */
131 static struct flow_key keys[5] = { {
132 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
133 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
138 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
139 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
144 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
145 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
150 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
151 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
156 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
157 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
163 /* Parameters used for hash table in unit test functions. Name set later. */
164 static struct rte_hash_parameters ut_params = {
166 .key_len = sizeof(struct flow_key), /* 13 */
167 .hash_func = rte_jhash,
168 .hash_func_init_val = 0,
172 #define CRC32_ITERATIONS (1U << 10)
173 #define CRC32_DWORDS (1U << 6)
175 * Test if all CRC32 implementations yield the same hash value
178 test_crc32_hash_alg_equiv(void)
182 uint64_t data64[CRC32_DWORDS];
186 printf("\n# CRC32 implementations equivalence test\n");
187 for (i = 0; i < CRC32_ITERATIONS; i++) {
188 /* Randomizing data_len of data set */
189 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
190 init_val = (uint32_t) rte_rand();
192 /* Fill the data set */
193 for (j = 0; j < CRC32_DWORDS; j++)
194 data64[j] = rte_rand();
196 /* Calculate software CRC32 */
197 rte_hash_crc_set_alg(CRC32_SW);
198 hash_val = rte_hash_crc(data64, data_len, init_val);
200 /* Check against 4-byte-operand sse4.2 CRC32 if available */
201 rte_hash_crc_set_alg(CRC32_SSE42);
202 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
203 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
207 /* Check against 8-byte-operand sse4.2 CRC32 if available */
208 rte_hash_crc_set_alg(CRC32_SSE42_x64);
209 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
210 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
214 /* Check against 8-byte-operand ARM64 CRC32 if available */
215 rte_hash_crc_set_alg(CRC32_ARM64);
216 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
217 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
222 /* Resetting to best available algorithm */
223 rte_hash_crc_set_alg(CRC32_SSE42_x64);
225 if (i == CRC32_ITERATIONS)
228 printf("Failed test data (hex, %zu bytes total):\n", data_len);
229 for (j = 0; j < data_len; j++)
230 printf("%02X%c", ((uint8_t *)data64)[j],
231 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
237 * Test a hash function.
239 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
242 static uint8_t key[MAX_KEYSIZE];
246 for (i = 0; i < key_len; i++)
247 key[i] = (uint8_t) rte_rand();
249 /* just to be on the safe side */
253 f(key, key_len, init_val);
257 * Test all hash functions.
259 static void run_hash_func_tests(void)
264 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
267 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
270 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
272 run_hash_func_test(hashtest_funcs[i],
273 hashtest_initvals[j],
274 hashtest_key_lens[k]);
281 * Basic sequence of operations for a single key:
287 static int test_add_delete(void)
289 struct rte_hash *handle;
290 /* test with standard add/lookup/delete functions */
291 int pos0, expectedPos0;
293 ut_params.name = "test1";
294 handle = rte_hash_create(&ut_params);
295 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
297 pos0 = rte_hash_add_key(handle, &keys[0]);
298 print_key_info("Add", &keys[0], pos0);
299 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
302 pos0 = rte_hash_lookup(handle, &keys[0]);
303 print_key_info("Lkp", &keys[0], pos0);
304 RETURN_IF_ERROR(pos0 != expectedPos0,
305 "failed to find key (pos0=%d)", pos0);
307 pos0 = rte_hash_del_key(handle, &keys[0]);
308 print_key_info("Del", &keys[0], pos0);
309 RETURN_IF_ERROR(pos0 != expectedPos0,
310 "failed to delete key (pos0=%d)", pos0);
312 pos0 = rte_hash_lookup(handle, &keys[0]);
313 print_key_info("Lkp", &keys[0], pos0);
314 RETURN_IF_ERROR(pos0 != -ENOENT,
315 "fail: found key after deleting! (pos0=%d)", pos0);
317 rte_hash_free(handle);
319 /* repeat test with precomputed hash functions */
320 hash_sig_t hash_value;
321 int pos1, expectedPos1;
323 handle = rte_hash_create(&ut_params);
324 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
326 hash_value = rte_hash_hash(handle, &keys[0]);
327 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
328 print_key_info("Add", &keys[0], pos1);
329 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
332 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
333 print_key_info("Lkp", &keys[0], pos1);
334 RETURN_IF_ERROR(pos1 != expectedPos1,
335 "failed to find key (pos1=%d)", pos1);
337 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
338 print_key_info("Del", &keys[0], pos1);
339 RETURN_IF_ERROR(pos1 != expectedPos1,
340 "failed to delete key (pos1=%d)", pos1);
342 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
343 print_key_info("Lkp", &keys[0], pos1);
344 RETURN_IF_ERROR(pos1 != -ENOENT,
345 "fail: found key after deleting! (pos1=%d)", pos1);
347 rte_hash_free(handle);
353 * Sequence of operations for a single key:
358 * - lookup: hit (updated data)
363 static int test_add_update_delete(void)
365 struct rte_hash *handle;
366 int pos0, expectedPos0;
368 ut_params.name = "test2";
369 handle = rte_hash_create(&ut_params);
370 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
372 pos0 = rte_hash_del_key(handle, &keys[0]);
373 print_key_info("Del", &keys[0], pos0);
374 RETURN_IF_ERROR(pos0 != -ENOENT,
375 "fail: found non-existent key (pos0=%d)", pos0);
377 pos0 = rte_hash_add_key(handle, &keys[0]);
378 print_key_info("Add", &keys[0], pos0);
379 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
382 pos0 = rte_hash_lookup(handle, &keys[0]);
383 print_key_info("Lkp", &keys[0], pos0);
384 RETURN_IF_ERROR(pos0 != expectedPos0,
385 "failed to find key (pos0=%d)", pos0);
387 pos0 = rte_hash_add_key(handle, &keys[0]);
388 print_key_info("Add", &keys[0], pos0);
389 RETURN_IF_ERROR(pos0 != expectedPos0,
390 "failed to re-add key (pos0=%d)", pos0);
392 pos0 = rte_hash_lookup(handle, &keys[0]);
393 print_key_info("Lkp", &keys[0], pos0);
394 RETURN_IF_ERROR(pos0 != expectedPos0,
395 "failed to find key (pos0=%d)", pos0);
397 pos0 = rte_hash_del_key(handle, &keys[0]);
398 print_key_info("Del", &keys[0], pos0);
399 RETURN_IF_ERROR(pos0 != expectedPos0,
400 "failed to delete key (pos0=%d)", pos0);
402 pos0 = rte_hash_del_key(handle, &keys[0]);
403 print_key_info("Del", &keys[0], pos0);
404 RETURN_IF_ERROR(pos0 != -ENOENT,
405 "fail: deleted already deleted key (pos0=%d)", pos0);
407 pos0 = rte_hash_lookup(handle, &keys[0]);
408 print_key_info("Lkp", &keys[0], pos0);
409 RETURN_IF_ERROR(pos0 != -ENOENT,
410 "fail: found key after deleting! (pos0=%d)", pos0);
412 rte_hash_free(handle);
417 * Sequence of operations for retrieving a key with its position
421 * - get the key with its position: hit
423 * - try to get the deleted key: miss
426 static int test_hash_get_key_with_position(void)
428 struct rte_hash *handle = NULL;
429 int pos, expectedPos, result;
432 ut_params.name = "hash_get_key_w_pos";
433 handle = rte_hash_create(&ut_params);
434 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
436 pos = rte_hash_add_key(handle, &keys[0]);
437 print_key_info("Add", &keys[0], pos);
438 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
441 result = rte_hash_get_key_with_position(handle, pos, &key);
442 RETURN_IF_ERROR(result != 0, "error retrieving a key");
444 pos = rte_hash_del_key(handle, &keys[0]);
445 print_key_info("Del", &keys[0], pos);
446 RETURN_IF_ERROR(pos != expectedPos,
447 "failed to delete key (pos0=%d)", pos);
449 result = rte_hash_get_key_with_position(handle, pos, &key);
450 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
452 rte_hash_free(handle);
457 * Sequence of operations for find existing hash table
460 * - find existing table: hit
461 * - find non-existing table: miss
464 static int test_hash_find_existing(void)
466 struct rte_hash *handle = NULL, *result = NULL;
468 /* Create hash table. */
469 ut_params.name = "hash_find_existing";
470 handle = rte_hash_create(&ut_params);
471 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
473 /* Try to find existing hash table */
474 result = rte_hash_find_existing("hash_find_existing");
475 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
477 /* Try to find non-existing hash table */
478 result = rte_hash_find_existing("hash_find_non_existing");
479 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
482 rte_hash_free(handle);
488 * Sequence of operations for 5 keys
491 * - add keys (update)
492 * - lookup keys: hit (updated data)
493 * - delete keys : hit
494 * - lookup keys: miss
496 static int test_five_keys(void)
498 struct rte_hash *handle;
499 const void *key_array[5] = {0};
505 ut_params.name = "test3";
506 handle = rte_hash_create(&ut_params);
507 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
510 for (i = 0; i < 5; i++) {
511 pos[i] = rte_hash_add_key(handle, &keys[i]);
512 print_key_info("Add", &keys[i], pos[i]);
513 RETURN_IF_ERROR(pos[i] < 0,
514 "failed to add key (pos[%u]=%d)", i, pos[i]);
515 expected_pos[i] = pos[i];
519 for(i = 0; i < 5; i++)
520 key_array[i] = &keys[i];
522 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
524 for(i = 0; i < 5; i++) {
525 print_key_info("Lkp", key_array[i], pos[i]);
526 RETURN_IF_ERROR(pos[i] != expected_pos[i],
527 "failed to find key (pos[%u]=%d)", i, pos[i]);
531 for (i = 0; i < 5; i++) {
532 pos[i] = rte_hash_add_key(handle, &keys[i]);
533 print_key_info("Add", &keys[i], pos[i]);
534 RETURN_IF_ERROR(pos[i] != expected_pos[i],
535 "failed to add key (pos[%u]=%d)", i, pos[i]);
539 for (i = 0; i < 5; i++) {
540 pos[i] = rte_hash_lookup(handle, &keys[i]);
541 print_key_info("Lkp", &keys[i], pos[i]);
542 RETURN_IF_ERROR(pos[i] != expected_pos[i],
543 "failed to find key (pos[%u]=%d)", i, pos[i]);
547 for (i = 0; i < 5; i++) {
548 pos[i] = rte_hash_del_key(handle, &keys[i]);
549 print_key_info("Del", &keys[i], pos[i]);
550 RETURN_IF_ERROR(pos[i] != expected_pos[i],
551 "failed to delete key (pos[%u]=%d)", i, pos[i]);
555 for (i = 0; i < 5; i++) {
556 pos[i] = rte_hash_lookup(handle, &keys[i]);
557 print_key_info("Lkp", &keys[i], pos[i]);
558 RETURN_IF_ERROR(pos[i] != -ENOENT,
559 "found non-existent key (pos[%u]=%d)", i, pos[i]);
563 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
565 for (i = 0; i < 5; i++) {
566 print_key_info("Lkp", key_array[i], pos[i]);
567 RETURN_IF_ERROR(pos[i] != -ENOENT,
568 "found not-existent key (pos[%u]=%d)", i, pos[i]);
571 rte_hash_free(handle);
577 * Add keys to the same bucket until bucket full.
578 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
579 * first 4 successful, 5th successful, pushing existing item in bucket
580 * - lookup the 5 keys: 5 hits
581 * - add the 5 keys again: 5 OK
582 * - lookup the 5 keys: 5 hits (updated data)
583 * - delete the 5 keys: 5 OK
584 * - lookup the 5 keys: 5 misses
586 static int test_full_bucket(void)
588 struct rte_hash_parameters params_pseudo_hash = {
591 .key_len = sizeof(struct flow_key), /* 13 */
592 .hash_func = pseudo_hash,
593 .hash_func_init_val = 0,
596 struct rte_hash *handle;
601 handle = rte_hash_create(¶ms_pseudo_hash);
602 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
605 for (i = 0; i < 4; i++) {
606 pos[i] = rte_hash_add_key(handle, &keys[i]);
607 print_key_info("Add", &keys[i], pos[i]);
608 RETURN_IF_ERROR(pos[i] < 0,
609 "failed to add key (pos[%u]=%d)", i, pos[i]);
610 expected_pos[i] = pos[i];
613 * This should work and will push one of the items
614 * in the bucket because it is full
616 pos[4] = rte_hash_add_key(handle, &keys[4]);
617 print_key_info("Add", &keys[4], pos[4]);
618 RETURN_IF_ERROR(pos[4] < 0,
619 "failed to add key (pos[4]=%d)", pos[4]);
620 expected_pos[4] = pos[4];
623 for (i = 0; i < 5; i++) {
624 pos[i] = rte_hash_lookup(handle, &keys[i]);
625 print_key_info("Lkp", &keys[i], pos[i]);
626 RETURN_IF_ERROR(pos[i] != expected_pos[i],
627 "failed to find key (pos[%u]=%d)", i, pos[i]);
631 for (i = 0; i < 5; i++) {
632 pos[i] = rte_hash_add_key(handle, &keys[i]);
633 print_key_info("Add", &keys[i], pos[i]);
634 RETURN_IF_ERROR(pos[i] != expected_pos[i],
635 "failed to add 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] != expected_pos[i],
643 "failed to find key (pos[%u]=%d)", i, pos[i]);
646 /* Delete 1 key, check other keys are still found */
647 pos[1] = rte_hash_del_key(handle, &keys[1]);
648 print_key_info("Del", &keys[1], pos[1]);
649 RETURN_IF_ERROR(pos[1] != expected_pos[1],
650 "failed to delete key (pos[1]=%d)", pos[1]);
651 pos[3] = rte_hash_lookup(handle, &keys[3]);
652 print_key_info("Lkp", &keys[3], pos[3]);
653 RETURN_IF_ERROR(pos[3] != expected_pos[3],
654 "failed lookup after deleting key from same bucket "
655 "(pos[3]=%d)", pos[3]);
657 /* Go back to previous state */
658 pos[1] = rte_hash_add_key(handle, &keys[1]);
659 print_key_info("Add", &keys[1], pos[1]);
660 expected_pos[1] = pos[1];
661 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
664 for (i = 0; i < 5; i++) {
665 pos[i] = rte_hash_del_key(handle, &keys[i]);
666 print_key_info("Del", &keys[i], pos[i]);
667 RETURN_IF_ERROR(pos[i] != expected_pos[i],
668 "failed to delete key (pos[%u]=%d)", i, pos[i]);
672 for (i = 0; i < 5; i++) {
673 pos[i] = rte_hash_lookup(handle, &keys[i]);
674 print_key_info("Lkp", &keys[i], pos[i]);
675 RETURN_IF_ERROR(pos[i] != -ENOENT,
676 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
679 rte_hash_free(handle);
681 /* Cover the NULL case. */
686 /******************************************************************************/
688 fbk_hash_unit_test(void)
690 struct rte_fbk_hash_params params = {
691 .name = "fbk_hash_test",
692 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
693 .entries_per_bucket = 4,
697 struct rte_fbk_hash_params invalid_params_1 = {
699 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
700 .entries_per_bucket = 4,
704 struct rte_fbk_hash_params invalid_params_2 = {
707 .entries_per_bucket = 3, /* Not power of 2 */
711 struct rte_fbk_hash_params invalid_params_3 = {
713 .entries = 0, /* Entries is 0 */
714 .entries_per_bucket = 4,
718 struct rte_fbk_hash_params invalid_params_4 = {
720 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
721 .entries_per_bucket = 0, /* Entries per bucket is 0 */
725 struct rte_fbk_hash_params invalid_params_5 = {
728 .entries_per_bucket = 8, /* Entries per bucket > entries */
732 struct rte_fbk_hash_params invalid_params_6 = {
734 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
735 .entries_per_bucket = 4,
739 struct rte_fbk_hash_params invalid_params_7 = {
741 .entries = RTE_FBK_HASH_ENTRIES_MAX,
742 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
746 struct rte_fbk_hash_params invalid_params_8 = {
748 .entries = RTE_FBK_HASH_ENTRIES_MAX,
749 .entries_per_bucket = 4,
750 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
753 /* try to create two hashes with identical names
754 * in this case, trying to create a second one will not
755 * fail but will simply return pointer to the existing
756 * hash with that name. sort of like a "find hash by name" :-)
758 struct rte_fbk_hash_params invalid_params_same_name_1 = {
759 .name = "same_name", /* hash with identical name */
761 .entries_per_bucket = 2,
765 /* trying to create this hash should return a pointer to an existing hash */
766 struct rte_fbk_hash_params invalid_params_same_name_2 = {
767 .name = "same_name", /* hash with identical name */
768 .entries = RTE_FBK_HASH_ENTRIES_MAX,
769 .entries_per_bucket = 4,
773 /* this is a sanity check for "same name" test
774 * creating this hash will check if we are actually able to create
775 * multiple hashes with different names (instead of having just one).
777 struct rte_fbk_hash_params different_name = {
778 .name = "different_name", /* different name */
779 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
780 .entries_per_bucket = 4,
784 struct rte_fbk_hash_params params_jhash = {
786 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
787 .entries_per_bucket = 4,
789 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
790 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
793 struct rte_fbk_hash_params params_nohash = {
794 .name = "valid nohash",
795 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
796 .entries_per_bucket = 4,
798 .hash_func = NULL, /* Tests for null hash_func */
799 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
802 struct rte_fbk_hash_table *handle, *tmp;
804 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
805 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
810 /* Try creating hashes with invalid parameters */
811 printf("# Testing hash creation with invalid parameters "
812 "- expect error msgs\n");
813 handle = rte_fbk_hash_create(&invalid_params_1);
814 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
816 handle = rte_fbk_hash_create(&invalid_params_2);
817 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
819 handle = rte_fbk_hash_create(&invalid_params_3);
820 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
822 handle = rte_fbk_hash_create(&invalid_params_4);
823 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
825 handle = rte_fbk_hash_create(&invalid_params_5);
826 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
828 handle = rte_fbk_hash_create(&invalid_params_6);
829 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
831 handle = rte_fbk_hash_create(&invalid_params_7);
832 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
834 handle = rte_fbk_hash_create(&invalid_params_8);
835 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
837 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
838 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
840 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
842 rte_fbk_hash_free(tmp);
843 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
845 /* we are not freeing handle here because we need a hash list
846 * to be not empty for the next test */
848 /* create a hash in non-empty list - good for coverage */
849 tmp = rte_fbk_hash_create(&different_name);
850 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
852 /* free both hashes */
853 rte_fbk_hash_free(handle);
854 rte_fbk_hash_free(tmp);
856 /* Create empty jhash hash. */
857 handle = rte_fbk_hash_create(¶ms_jhash);
858 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
861 rte_fbk_hash_free(handle);
863 /* Create empty jhash hash. */
864 handle = rte_fbk_hash_create(¶ms_nohash);
865 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
868 rte_fbk_hash_free(handle);
870 /* Create empty hash. */
871 handle = rte_fbk_hash_create(¶ms);
872 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
874 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
875 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
876 "load factor right after creation is not zero but it should be");
878 for (i = 0; i < 5; i++) {
879 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
880 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
883 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
884 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
885 "load factor now is not as expected");
886 /* Find value of added keys. */
887 for (i = 0; i < 5; i++) {
888 status = rte_fbk_hash_lookup(handle, keys[i]);
889 RETURN_IF_ERROR_FBK(status != vals[i],
890 "fbk hash lookup failed");
893 /* Change value of added keys. */
894 for (i = 0; i < 5; i++) {
895 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
896 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
899 /* Find new values. */
900 for (i = 0; i < 5; i++) {
901 status = rte_fbk_hash_lookup(handle, keys[i]);
902 RETURN_IF_ERROR_FBK(status != vals[4-i],
903 "fbk hash lookup failed");
906 /* Delete keys individually. */
907 for (i = 0; i < 5; i++) {
908 status = rte_fbk_hash_delete_key(handle, keys[i]);
909 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
912 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
913 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
914 "load factor right after deletion is not zero but it should be");
915 /* Lookup should now 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");
922 /* Add keys again. */
923 for (i = 0; i < 5; i++) {
924 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
925 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
928 /* Make sure they were added. */
929 for (i = 0; i < 5; i++) {
930 status = rte_fbk_hash_lookup(handle, keys[i]);
931 RETURN_IF_ERROR_FBK(status != vals[i],
932 "fbk hash lookup failed");
935 /* Clear all entries. */
936 rte_fbk_hash_clear_all(handle);
938 /* Lookup should fail. */
939 for (i = 0; i < 5; i++) {
940 status = rte_fbk_hash_lookup(handle, keys[i]);
941 RETURN_IF_ERROR_FBK(status == 0,
942 "fbk hash lookup should have failed");
947 /* fill up the hash_table */
948 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
949 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
951 /* Find non-existent key in a full hashtable */
952 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
953 RETURN_IF_ERROR_FBK(status != -ENOENT,
954 "fbk hash lookup succeeded");
956 /* Delete non-existent key in a full hashtable */
957 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
958 RETURN_IF_ERROR_FBK(status != -ENOENT,
959 "fbk hash delete succeeded");
961 /* Delete one key from a full hashtable */
962 status = rte_fbk_hash_delete_key(handle, 1);
963 RETURN_IF_ERROR_FBK(status != 0,
964 "fbk hash delete failed");
966 /* Clear all entries. */
967 rte_fbk_hash_clear_all(handle);
970 rte_fbk_hash_free(handle);
972 /* Cover the NULL case. */
973 rte_fbk_hash_free(0);
979 * Sequence of operations for find existing fbk hash table
982 * - find existing table: hit
983 * - find non-existing table: miss
986 static int test_fbk_hash_find_existing(void)
988 struct rte_fbk_hash_params params = {
989 .name = "fbk_hash_find_existing",
990 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
991 .entries_per_bucket = 4,
994 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
996 /* Create hash table. */
997 handle = rte_fbk_hash_create(¶ms);
998 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1000 /* Try to find existing fbk hash table */
1001 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1002 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1004 /* Try to find non-existing fbk hash table */
1005 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1006 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1009 rte_fbk_hash_free(handle);
1014 #define BUCKET_ENTRIES 4
1016 * Do tests for hash creation with bad parameters.
1018 static int test_hash_creation_with_bad_parameters(void)
1020 struct rte_hash *handle, *tmp;
1021 struct rte_hash_parameters params;
1023 handle = rte_hash_create(NULL);
1024 if (handle != NULL) {
1025 rte_hash_free(handle);
1026 printf("Impossible creating hash successfully without any parameter\n");
1030 memcpy(¶ms, &ut_params, sizeof(params));
1031 params.name = "creation_with_bad_parameters_0";
1032 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1033 handle = rte_hash_create(¶ms);
1034 if (handle != NULL) {
1035 rte_hash_free(handle);
1036 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1040 memcpy(¶ms, &ut_params, sizeof(params));
1041 params.name = "creation_with_bad_parameters_2";
1042 params.entries = BUCKET_ENTRIES - 1;
1043 handle = rte_hash_create(¶ms);
1044 if (handle != NULL) {
1045 rte_hash_free(handle);
1046 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1050 memcpy(¶ms, &ut_params, sizeof(params));
1051 params.name = "creation_with_bad_parameters_3";
1053 handle = rte_hash_create(¶ms);
1054 if (handle != NULL) {
1055 rte_hash_free(handle);
1056 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1060 memcpy(¶ms, &ut_params, sizeof(params));
1061 params.name = "creation_with_bad_parameters_4";
1062 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1063 handle = rte_hash_create(¶ms);
1064 if (handle != NULL) {
1065 rte_hash_free(handle);
1066 printf("Impossible creating hash successfully with invalid socket\n");
1070 /* test with same name should fail */
1071 memcpy(¶ms, &ut_params, sizeof(params));
1072 params.name = "same_name";
1073 handle = rte_hash_create(¶ms);
1074 if (handle == NULL) {
1075 printf("Cannot create first hash table with 'same_name'\n");
1078 tmp = rte_hash_create(¶ms);
1080 printf("Creation of hash table with same name should fail\n");
1081 rte_hash_free(handle);
1085 rte_hash_free(handle);
1087 printf("# Test successful. No more errors expected\n");
1093 * Do tests for hash creation with parameters that look incorrect
1094 * but are actually valid.
1097 test_hash_creation_with_good_parameters(void)
1099 struct rte_hash *handle;
1100 struct rte_hash_parameters params;
1102 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1103 memcpy(¶ms, &ut_params, sizeof(params));
1104 params.name = "name";
1105 params.hash_func = NULL;
1106 handle = rte_hash_create(¶ms);
1107 if (handle == NULL) {
1108 printf("Creating hash with null hash_func failed\n");
1112 rte_hash_free(handle);
1117 #define ITERATIONS 3
1119 * Test to see the average table utilization (entries added/max entries)
1120 * before hitting a random entry that cannot be added
1122 static int test_average_table_utilization(void)
1124 struct rte_hash *handle;
1125 uint8_t simple_key[MAX_KEYSIZE];
1127 unsigned added_keys, average_keys_added = 0;
1130 printf("\n# Running test to determine average utilization"
1131 "\n before adding elements begins to fail\n");
1132 printf("Measuring performance, please wait");
1134 ut_params.entries = 1 << 16;
1135 ut_params.name = "test_average_utilization";
1136 ut_params.hash_func = rte_jhash;
1137 handle = rte_hash_create(&ut_params);
1138 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1140 for (j = 0; j < ITERATIONS; j++) {
1142 /* Add random entries until key cannot be added */
1143 for (added_keys = 0; ret >= 0; added_keys++) {
1144 for (i = 0; i < ut_params.key_len; i++)
1145 simple_key[i] = rte_rand() % 255;
1146 ret = rte_hash_add_key(handle, simple_key);
1148 if (ret != -ENOSPC) {
1149 printf("Unexpected error when adding keys\n");
1150 rte_hash_free(handle);
1154 average_keys_added += added_keys;
1156 /* Reset the table */
1157 rte_hash_reset(handle);
1159 /* Print a dot to show progress on operations */
1164 average_keys_added /= ITERATIONS;
1166 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1167 ((double) average_keys_added / ut_params.entries * 100),
1168 average_keys_added, ut_params.entries);
1169 rte_hash_free(handle);
1174 #define NUM_ENTRIES 256
1175 static int test_hash_iteration(void)
1177 struct rte_hash *handle;
1179 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1180 const void *next_key;
1182 void *data[NUM_ENTRIES];
1183 unsigned added_keys;
1187 ut_params.entries = NUM_ENTRIES;
1188 ut_params.name = "test_hash_iteration";
1189 ut_params.hash_func = rte_jhash;
1190 ut_params.key_len = 16;
1191 handle = rte_hash_create(&ut_params);
1192 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1194 /* Add random entries until key cannot be added */
1195 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1196 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1197 for (i = 0; i < ut_params.key_len; i++)
1198 keys[added_keys][i] = rte_rand() % 255;
1199 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1204 /* Iterate through the hash table */
1205 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1206 /* Search for the key in the list of keys added */
1207 for (i = 0; i < NUM_ENTRIES; i++) {
1208 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1209 if (next_data != data[i]) {
1210 printf("Data found in the hash table is"
1211 "not the data added with the key\n");
1218 if (i == NUM_ENTRIES) {
1219 printf("Key found in the hash table was not added\n");
1224 /* Check if all keys have been iterated */
1225 if (added_keys != 0) {
1226 printf("There were still %u keys to iterate\n", added_keys);
1230 rte_hash_free(handle);
1234 rte_hash_free(handle);
1238 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1239 0x04, 0x05, 0x06, 0x07,
1240 0x08, 0x09, 0x0a, 0x0b,
1241 0x0c, 0x0d, 0x0e, 0x0f};
1242 static struct rte_hash_parameters hash_params_ex = {
1247 .hash_func_init_val = 0,
1252 * add/delete key with jhash2
1255 test_hash_add_delete_jhash2(void)
1258 struct rte_hash *handle;
1261 hash_params_ex.name = "hash_test_jhash2";
1262 hash_params_ex.key_len = 4;
1263 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1265 handle = rte_hash_create(&hash_params_ex);
1266 if (handle == NULL) {
1267 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1270 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1272 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1276 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1277 if (pos2 < 0 || pos1 != pos2) {
1278 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1285 rte_hash_free(handle);
1291 * add/delete (2) key with jhash2
1294 test_hash_add_delete_2_jhash2(void)
1297 struct rte_hash *handle;
1300 hash_params_ex.name = "hash_test_2_jhash2";
1301 hash_params_ex.key_len = 8;
1302 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1304 handle = rte_hash_create(&hash_params_ex);
1308 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1312 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1313 if (pos2 < 0 || pos1 != pos2)
1320 rte_hash_free(handle);
1326 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1328 const uint32_t *k = key;
1330 RTE_SET_USED(length);
1332 return rte_jhash_1word(k[0], initval);
1336 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1338 const uint32_t *k = key;
1340 RTE_SET_USED(length);
1342 return rte_jhash_2words(k[0], k[1], initval);
1346 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1348 const uint32_t *k = key;
1350 RTE_SET_USED(length);
1352 return rte_jhash_3words(k[0], k[1], k[2], initval);
1356 * add/delete key with jhash 1word
1359 test_hash_add_delete_jhash_1word(void)
1362 struct rte_hash *handle;
1365 hash_params_ex.name = "hash_test_jhash_1word";
1366 hash_params_ex.key_len = 4;
1367 hash_params_ex.hash_func = test_hash_jhash_1word;
1369 handle = rte_hash_create(&hash_params_ex);
1371 goto fail_jhash_1word;
1373 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1375 goto fail_jhash_1word;
1377 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1378 if (pos2 < 0 || pos1 != pos2)
1379 goto fail_jhash_1word;
1385 rte_hash_free(handle);
1391 * add/delete key with jhash 2word
1394 test_hash_add_delete_jhash_2word(void)
1397 struct rte_hash *handle;
1400 hash_params_ex.name = "hash_test_jhash_2word";
1401 hash_params_ex.key_len = 8;
1402 hash_params_ex.hash_func = test_hash_jhash_2word;
1404 handle = rte_hash_create(&hash_params_ex);
1406 goto fail_jhash_2word;
1408 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1410 goto fail_jhash_2word;
1412 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1413 if (pos2 < 0 || pos1 != pos2)
1414 goto fail_jhash_2word;
1420 rte_hash_free(handle);
1426 * add/delete key with jhash 3word
1429 test_hash_add_delete_jhash_3word(void)
1432 struct rte_hash *handle;
1435 hash_params_ex.name = "hash_test_jhash_3word";
1436 hash_params_ex.key_len = 12;
1437 hash_params_ex.hash_func = test_hash_jhash_3word;
1439 handle = rte_hash_create(&hash_params_ex);
1441 goto fail_jhash_3word;
1443 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1445 goto fail_jhash_3word;
1447 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1448 if (pos2 < 0 || pos1 != pos2)
1449 goto fail_jhash_3word;
1455 rte_hash_free(handle);
1461 * Do all unit and performance tests.
1466 if (test_add_delete() < 0)
1468 if (test_hash_add_delete_jhash2() < 0)
1470 if (test_hash_add_delete_2_jhash2() < 0)
1472 if (test_hash_add_delete_jhash_1word() < 0)
1474 if (test_hash_add_delete_jhash_2word() < 0)
1476 if (test_hash_add_delete_jhash_3word() < 0)
1478 if (test_hash_get_key_with_position() < 0)
1480 if (test_hash_find_existing() < 0)
1482 if (test_add_update_delete() < 0)
1484 if (test_five_keys() < 0)
1486 if (test_full_bucket() < 0)
1489 if (test_fbk_hash_find_existing() < 0)
1491 if (fbk_hash_unit_test() < 0)
1493 if (test_hash_creation_with_bad_parameters() < 0)
1495 if (test_hash_creation_with_good_parameters() < 0)
1497 if (test_average_table_utilization() < 0)
1499 if (test_hash_iteration() < 0)
1502 run_hash_func_tests();
1504 if (test_crc32_hash_alg_equiv() < 0)
1510 REGISTER_TEST_COMMAND(hash_autotest, test_hash);