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)
113 * Print out result of unit test hash operation.
115 #if defined(UNIT_TEST_HASH_VERBOSE)
116 static void print_key_info(const char *msg, const struct flow_key *key,
119 uint8_t *p = (uint8_t *)key;
122 printf("%s key:0x", msg);
123 for (i = 0; i < sizeof(struct flow_key); i++) {
124 printf("%02X", p[i]);
126 printf(" @ pos %d\n", pos);
129 static void print_key_info(__attribute__((unused)) const char *msg,
130 __attribute__((unused)) const struct flow_key *key,
131 __attribute__((unused)) int32_t pos)
136 /* Keys used by unit test functions */
137 static struct flow_key keys[5] = { {
138 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
139 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
144 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
145 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
150 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
151 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
156 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
157 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
162 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
163 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
169 /* Parameters used for hash table in unit test functions. Name set later. */
170 static struct rte_hash_parameters ut_params = {
172 .key_len = sizeof(struct flow_key), /* 13 */
173 .hash_func = rte_jhash,
174 .hash_func_init_val = 0,
178 #define CRC32_ITERATIONS (1U << 10)
179 #define CRC32_DWORDS (1U << 6)
181 * Test if all CRC32 implementations yield the same hash value
184 test_crc32_hash_alg_equiv(void)
188 uint64_t data64[CRC32_DWORDS];
192 printf("\n# CRC32 implementations equivalence test\n");
193 for (i = 0; i < CRC32_ITERATIONS; i++) {
194 /* Randomizing data_len of data set */
195 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
196 init_val = (uint32_t) rte_rand();
198 /* Fill the data set */
199 for (j = 0; j < CRC32_DWORDS; j++)
200 data64[j] = rte_rand();
202 /* Calculate software CRC32 */
203 rte_hash_crc_set_alg(CRC32_SW);
204 hash_val = rte_hash_crc(data64, data_len, init_val);
206 /* Check against 4-byte-operand sse4.2 CRC32 if available */
207 rte_hash_crc_set_alg(CRC32_SSE42);
208 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
209 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
213 /* Check against 8-byte-operand sse4.2 CRC32 if available */
214 rte_hash_crc_set_alg(CRC32_SSE42_x64);
215 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
216 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
220 /* Check against 8-byte-operand ARM64 CRC32 if available */
221 rte_hash_crc_set_alg(CRC32_ARM64);
222 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
223 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
228 /* Resetting to best available algorithm */
229 rte_hash_crc_set_alg(CRC32_SSE42_x64);
231 if (i == CRC32_ITERATIONS)
234 printf("Failed test data (hex, %zu bytes total):\n", data_len);
235 for (j = 0; j < data_len; j++)
236 printf("%02X%c", ((uint8_t *)data64)[j],
237 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
243 * Test a hash function.
245 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
248 static uint8_t key[MAX_KEYSIZE];
252 for (i = 0; i < key_len; i++)
253 key[i] = (uint8_t) rte_rand();
255 /* just to be on the safe side */
259 f(key, key_len, init_val);
263 * Test all hash functions.
265 static void run_hash_func_tests(void)
270 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
273 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
276 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
278 run_hash_func_test(hashtest_funcs[i],
279 hashtest_initvals[j],
280 hashtest_key_lens[k]);
287 * Basic sequence of operations for a single key:
293 static int test_add_delete(void)
295 struct rte_hash *handle;
296 /* test with standard add/lookup/delete functions */
297 int pos0, expectedPos0;
299 ut_params.name = "test1";
300 handle = rte_hash_create(&ut_params);
301 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
303 pos0 = rte_hash_add_key(handle, &keys[0]);
304 print_key_info("Add", &keys[0], pos0);
305 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
308 pos0 = rte_hash_lookup(handle, &keys[0]);
309 print_key_info("Lkp", &keys[0], pos0);
310 RETURN_IF_ERROR(pos0 != expectedPos0,
311 "failed to find key (pos0=%d)", pos0);
313 pos0 = rte_hash_del_key(handle, &keys[0]);
314 print_key_info("Del", &keys[0], pos0);
315 RETURN_IF_ERROR(pos0 != expectedPos0,
316 "failed to delete key (pos0=%d)", pos0);
318 pos0 = rte_hash_lookup(handle, &keys[0]);
319 print_key_info("Lkp", &keys[0], pos0);
320 RETURN_IF_ERROR(pos0 != -ENOENT,
321 "fail: found key after deleting! (pos0=%d)", pos0);
323 rte_hash_free(handle);
325 /* repeat test with precomputed hash functions */
326 hash_sig_t hash_value;
327 int pos1, expectedPos1;
329 handle = rte_hash_create(&ut_params);
330 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
332 hash_value = rte_hash_hash(handle, &keys[0]);
333 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
334 print_key_info("Add", &keys[0], pos1);
335 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
338 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
339 print_key_info("Lkp", &keys[0], pos1);
340 RETURN_IF_ERROR(pos1 != expectedPos1,
341 "failed to find key (pos1=%d)", pos1);
343 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
344 print_key_info("Del", &keys[0], pos1);
345 RETURN_IF_ERROR(pos1 != expectedPos1,
346 "failed to delete key (pos1=%d)", pos1);
348 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
349 print_key_info("Lkp", &keys[0], pos1);
350 RETURN_IF_ERROR(pos1 != -ENOENT,
351 "fail: found key after deleting! (pos1=%d)", pos1);
353 rte_hash_free(handle);
359 * Sequence of operations for a single key:
364 * - lookup: hit (updated data)
369 static int test_add_update_delete(void)
371 struct rte_hash *handle;
372 int pos0, expectedPos0;
374 ut_params.name = "test2";
375 handle = rte_hash_create(&ut_params);
376 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
378 pos0 = rte_hash_del_key(handle, &keys[0]);
379 print_key_info("Del", &keys[0], pos0);
380 RETURN_IF_ERROR(pos0 != -ENOENT,
381 "fail: found non-existent key (pos0=%d)", pos0);
383 pos0 = rte_hash_add_key(handle, &keys[0]);
384 print_key_info("Add", &keys[0], pos0);
385 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
388 pos0 = rte_hash_lookup(handle, &keys[0]);
389 print_key_info("Lkp", &keys[0], pos0);
390 RETURN_IF_ERROR(pos0 != expectedPos0,
391 "failed to find key (pos0=%d)", pos0);
393 pos0 = rte_hash_add_key(handle, &keys[0]);
394 print_key_info("Add", &keys[0], pos0);
395 RETURN_IF_ERROR(pos0 != expectedPos0,
396 "failed to re-add key (pos0=%d)", pos0);
398 pos0 = rte_hash_lookup(handle, &keys[0]);
399 print_key_info("Lkp", &keys[0], pos0);
400 RETURN_IF_ERROR(pos0 != expectedPos0,
401 "failed to find key (pos0=%d)", pos0);
403 pos0 = rte_hash_del_key(handle, &keys[0]);
404 print_key_info("Del", &keys[0], pos0);
405 RETURN_IF_ERROR(pos0 != expectedPos0,
406 "failed to delete key (pos0=%d)", pos0);
408 pos0 = rte_hash_del_key(handle, &keys[0]);
409 print_key_info("Del", &keys[0], pos0);
410 RETURN_IF_ERROR(pos0 != -ENOENT,
411 "fail: deleted already deleted key (pos0=%d)", pos0);
413 pos0 = rte_hash_lookup(handle, &keys[0]);
414 print_key_info("Lkp", &keys[0], pos0);
415 RETURN_IF_ERROR(pos0 != -ENOENT,
416 "fail: found key after deleting! (pos0=%d)", pos0);
418 rte_hash_free(handle);
423 * Sequence of operations for retrieving a key with its position
427 * - get the key with its position: hit
429 * - try to get the deleted key: miss
432 static int test_hash_get_key_with_position(void)
434 struct rte_hash *handle = NULL;
435 int pos, expectedPos, result;
438 ut_params.name = "hash_get_key_w_pos";
439 handle = rte_hash_create(&ut_params);
440 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
442 pos = rte_hash_add_key(handle, &keys[0]);
443 print_key_info("Add", &keys[0], pos);
444 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
447 result = rte_hash_get_key_with_position(handle, pos, &key);
448 RETURN_IF_ERROR(result != 0, "error retrieving a key");
450 pos = rte_hash_del_key(handle, &keys[0]);
451 print_key_info("Del", &keys[0], pos);
452 RETURN_IF_ERROR(pos != expectedPos,
453 "failed to delete key (pos0=%d)", pos);
455 result = rte_hash_get_key_with_position(handle, pos, &key);
456 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
458 rte_hash_free(handle);
463 * Sequence of operations for find existing hash table
466 * - find existing table: hit
467 * - find non-existing table: miss
470 static int test_hash_find_existing(void)
472 struct rte_hash *handle = NULL, *result = NULL;
474 /* Create hash table. */
475 ut_params.name = "hash_find_existing";
476 handle = rte_hash_create(&ut_params);
477 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
479 /* Try to find existing hash table */
480 result = rte_hash_find_existing("hash_find_existing");
481 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
483 /* Try to find non-existing hash table */
484 result = rte_hash_find_existing("hash_find_non_existing");
485 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
488 rte_hash_free(handle);
494 * Sequence of operations for 5 keys
497 * - add keys (update)
498 * - lookup keys: hit (updated data)
499 * - delete keys : hit
500 * - lookup keys: miss
502 static int test_five_keys(void)
504 struct rte_hash *handle;
505 const void *key_array[5] = {0};
511 ut_params.name = "test3";
512 handle = rte_hash_create(&ut_params);
513 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
516 for (i = 0; i < 5; i++) {
517 pos[i] = rte_hash_add_key(handle, &keys[i]);
518 print_key_info("Add", &keys[i], pos[i]);
519 RETURN_IF_ERROR(pos[i] < 0,
520 "failed to add key (pos[%u]=%d)", i, pos[i]);
521 expected_pos[i] = pos[i];
525 for(i = 0; i < 5; i++)
526 key_array[i] = &keys[i];
528 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
530 for(i = 0; i < 5; i++) {
531 print_key_info("Lkp", key_array[i], pos[i]);
532 RETURN_IF_ERROR(pos[i] != expected_pos[i],
533 "failed to find key (pos[%u]=%d)", i, pos[i]);
537 for (i = 0; i < 5; i++) {
538 pos[i] = rte_hash_add_key(handle, &keys[i]);
539 print_key_info("Add", &keys[i], pos[i]);
540 RETURN_IF_ERROR(pos[i] != expected_pos[i],
541 "failed to add key (pos[%u]=%d)", i, pos[i]);
545 for (i = 0; i < 5; i++) {
546 pos[i] = rte_hash_lookup(handle, &keys[i]);
547 print_key_info("Lkp", &keys[i], pos[i]);
548 RETURN_IF_ERROR(pos[i] != expected_pos[i],
549 "failed to find key (pos[%u]=%d)", i, pos[i]);
553 for (i = 0; i < 5; i++) {
554 pos[i] = rte_hash_del_key(handle, &keys[i]);
555 print_key_info("Del", &keys[i], pos[i]);
556 RETURN_IF_ERROR(pos[i] != expected_pos[i],
557 "failed to delete key (pos[%u]=%d)", i, pos[i]);
561 for (i = 0; i < 5; i++) {
562 pos[i] = rte_hash_lookup(handle, &keys[i]);
563 print_key_info("Lkp", &keys[i], pos[i]);
564 RETURN_IF_ERROR(pos[i] != -ENOENT,
565 "found non-existent key (pos[%u]=%d)", i, pos[i]);
569 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
571 for (i = 0; i < 5; i++) {
572 print_key_info("Lkp", key_array[i], pos[i]);
573 RETURN_IF_ERROR(pos[i] != -ENOENT,
574 "found not-existent key (pos[%u]=%d)", i, pos[i]);
577 rte_hash_free(handle);
583 * Add keys to the same bucket until bucket full.
584 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
585 * first 4 successful, 5th successful, pushing existing item in bucket
586 * - lookup the 5 keys: 5 hits
587 * - add the 5 keys again: 5 OK
588 * - lookup the 5 keys: 5 hits (updated data)
589 * - delete the 5 keys: 5 OK
590 * - lookup the 5 keys: 5 misses
592 static int test_full_bucket(void)
594 struct rte_hash_parameters params_pseudo_hash = {
597 .key_len = sizeof(struct flow_key), /* 13 */
598 .hash_func = pseudo_hash,
599 .hash_func_init_val = 0,
602 struct rte_hash *handle;
607 handle = rte_hash_create(¶ms_pseudo_hash);
608 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
611 for (i = 0; i < 4; i++) {
612 pos[i] = rte_hash_add_key(handle, &keys[i]);
613 print_key_info("Add", &keys[i], pos[i]);
614 RETURN_IF_ERROR(pos[i] < 0,
615 "failed to add key (pos[%u]=%d)", i, pos[i]);
616 expected_pos[i] = pos[i];
619 * This should work and will push one of the items
620 * in the bucket because it is full
622 pos[4] = rte_hash_add_key(handle, &keys[4]);
623 print_key_info("Add", &keys[4], pos[4]);
624 RETURN_IF_ERROR(pos[4] < 0,
625 "failed to add key (pos[4]=%d)", pos[4]);
626 expected_pos[4] = pos[4];
629 for (i = 0; i < 5; i++) {
630 pos[i] = rte_hash_lookup(handle, &keys[i]);
631 print_key_info("Lkp", &keys[i], pos[i]);
632 RETURN_IF_ERROR(pos[i] != expected_pos[i],
633 "failed to find key (pos[%u]=%d)", i, pos[i]);
637 for (i = 0; i < 5; i++) {
638 pos[i] = rte_hash_add_key(handle, &keys[i]);
639 print_key_info("Add", &keys[i], pos[i]);
640 RETURN_IF_ERROR(pos[i] != expected_pos[i],
641 "failed to add key (pos[%u]=%d)", i, pos[i]);
645 for (i = 0; i < 5; i++) {
646 pos[i] = rte_hash_lookup(handle, &keys[i]);
647 print_key_info("Lkp", &keys[i], pos[i]);
648 RETURN_IF_ERROR(pos[i] != expected_pos[i],
649 "failed to find key (pos[%u]=%d)", i, pos[i]);
652 /* Delete 1 key, check other keys are still found */
653 pos[1] = rte_hash_del_key(handle, &keys[1]);
654 print_key_info("Del", &keys[1], pos[1]);
655 RETURN_IF_ERROR(pos[1] != expected_pos[1],
656 "failed to delete key (pos[1]=%d)", pos[1]);
657 pos[3] = rte_hash_lookup(handle, &keys[3]);
658 print_key_info("Lkp", &keys[3], pos[3]);
659 RETURN_IF_ERROR(pos[3] != expected_pos[3],
660 "failed lookup after deleting key from same bucket "
661 "(pos[3]=%d)", pos[3]);
663 /* Go back to previous state */
664 pos[1] = rte_hash_add_key(handle, &keys[1]);
665 print_key_info("Add", &keys[1], pos[1]);
666 expected_pos[1] = pos[1];
667 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
670 for (i = 0; i < 5; i++) {
671 pos[i] = rte_hash_del_key(handle, &keys[i]);
672 print_key_info("Del", &keys[i], pos[i]);
673 RETURN_IF_ERROR(pos[i] != expected_pos[i],
674 "failed to delete key (pos[%u]=%d)", i, pos[i]);
678 for (i = 0; i < 5; i++) {
679 pos[i] = rte_hash_lookup(handle, &keys[i]);
680 print_key_info("Lkp", &keys[i], pos[i]);
681 RETURN_IF_ERROR(pos[i] != -ENOENT,
682 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
685 rte_hash_free(handle);
687 /* Cover the NULL case. */
692 /******************************************************************************/
694 fbk_hash_unit_test(void)
696 struct rte_fbk_hash_params params = {
697 .name = "fbk_hash_test",
698 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
699 .entries_per_bucket = 4,
703 struct rte_fbk_hash_params invalid_params_1 = {
705 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
706 .entries_per_bucket = 4,
710 struct rte_fbk_hash_params invalid_params_2 = {
713 .entries_per_bucket = 3, /* Not power of 2 */
717 struct rte_fbk_hash_params invalid_params_3 = {
719 .entries = 0, /* Entries is 0 */
720 .entries_per_bucket = 4,
724 struct rte_fbk_hash_params invalid_params_4 = {
726 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
727 .entries_per_bucket = 0, /* Entries per bucket is 0 */
731 struct rte_fbk_hash_params invalid_params_5 = {
734 .entries_per_bucket = 8, /* Entries per bucket > entries */
738 struct rte_fbk_hash_params invalid_params_6 = {
740 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
741 .entries_per_bucket = 4,
745 struct rte_fbk_hash_params invalid_params_7 = {
747 .entries = RTE_FBK_HASH_ENTRIES_MAX,
748 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
752 struct rte_fbk_hash_params invalid_params_8 = {
754 .entries = RTE_FBK_HASH_ENTRIES_MAX,
755 .entries_per_bucket = 4,
756 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
759 /* try to create two hashes with identical names
760 * in this case, trying to create a second one will not
761 * fail but will simply return pointer to the existing
762 * hash with that name. sort of like a "find hash by name" :-)
764 struct rte_fbk_hash_params invalid_params_same_name_1 = {
765 .name = "same_name", /* hash with identical name */
767 .entries_per_bucket = 2,
771 /* trying to create this hash should return a pointer to an existing hash */
772 struct rte_fbk_hash_params invalid_params_same_name_2 = {
773 .name = "same_name", /* hash with identical name */
774 .entries = RTE_FBK_HASH_ENTRIES_MAX,
775 .entries_per_bucket = 4,
779 /* this is a sanity check for "same name" test
780 * creating this hash will check if we are actually able to create
781 * multiple hashes with different names (instead of having just one).
783 struct rte_fbk_hash_params different_name = {
784 .name = "different_name", /* different name */
785 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
786 .entries_per_bucket = 4,
790 struct rte_fbk_hash_params params_jhash = {
792 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
793 .entries_per_bucket = 4,
795 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
796 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
799 struct rte_fbk_hash_params params_nohash = {
800 .name = "valid nohash",
801 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
802 .entries_per_bucket = 4,
804 .hash_func = NULL, /* Tests for null hash_func */
805 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
808 struct rte_fbk_hash_table *handle, *tmp;
810 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
811 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
816 /* Try creating hashes with invalid parameters */
817 printf("# Testing hash creation with invalid parameters "
818 "- expect error msgs\n");
819 handle = rte_fbk_hash_create(&invalid_params_1);
820 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
822 handle = rte_fbk_hash_create(&invalid_params_2);
823 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
825 handle = rte_fbk_hash_create(&invalid_params_3);
826 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
828 handle = rte_fbk_hash_create(&invalid_params_4);
829 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
831 handle = rte_fbk_hash_create(&invalid_params_5);
832 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
834 handle = rte_fbk_hash_create(&invalid_params_6);
835 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
837 handle = rte_fbk_hash_create(&invalid_params_7);
838 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
840 handle = rte_fbk_hash_create(&invalid_params_8);
841 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
843 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
844 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
846 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
848 rte_fbk_hash_free(tmp);
849 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
851 /* we are not freeing handle here because we need a hash list
852 * to be not empty for the next test */
854 /* create a hash in non-empty list - good for coverage */
855 tmp = rte_fbk_hash_create(&different_name);
856 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
858 /* free both hashes */
859 rte_fbk_hash_free(handle);
860 rte_fbk_hash_free(tmp);
862 /* Create empty jhash hash. */
863 handle = rte_fbk_hash_create(¶ms_jhash);
864 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
867 rte_fbk_hash_free(handle);
869 /* Create empty jhash hash. */
870 handle = rte_fbk_hash_create(¶ms_nohash);
871 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
874 rte_fbk_hash_free(handle);
876 /* Create empty hash. */
877 handle = rte_fbk_hash_create(¶ms);
878 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
880 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
881 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
882 "load factor right after creation is not zero but it should be");
884 for (i = 0; i < 5; i++) {
885 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
886 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
889 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
890 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
891 "load factor now is not as expected");
892 /* Find value of added keys. */
893 for (i = 0; i < 5; i++) {
894 status = rte_fbk_hash_lookup(handle, keys[i]);
895 RETURN_IF_ERROR_FBK(status != vals[i],
896 "fbk hash lookup failed");
899 /* Change value of added keys. */
900 for (i = 0; i < 5; i++) {
901 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
902 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
905 /* Find new values. */
906 for (i = 0; i < 5; i++) {
907 status = rte_fbk_hash_lookup(handle, keys[i]);
908 RETURN_IF_ERROR_FBK(status != vals[4-i],
909 "fbk hash lookup failed");
912 /* Delete keys individually. */
913 for (i = 0; i < 5; i++) {
914 status = rte_fbk_hash_delete_key(handle, keys[i]);
915 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
918 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
919 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
920 "load factor right after deletion is not zero but it should be");
921 /* Lookup should now fail. */
922 for (i = 0; i < 5; i++) {
923 status = rte_fbk_hash_lookup(handle, keys[i]);
924 RETURN_IF_ERROR_FBK(status == 0,
925 "fbk hash lookup should have failed");
928 /* Add keys again. */
929 for (i = 0; i < 5; i++) {
930 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
931 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
934 /* Make sure they were added. */
935 for (i = 0; i < 5; i++) {
936 status = rte_fbk_hash_lookup(handle, keys[i]);
937 RETURN_IF_ERROR_FBK(status != vals[i],
938 "fbk hash lookup failed");
941 /* Clear all entries. */
942 rte_fbk_hash_clear_all(handle);
944 /* Lookup should fail. */
945 for (i = 0; i < 5; i++) {
946 status = rte_fbk_hash_lookup(handle, keys[i]);
947 RETURN_IF_ERROR_FBK(status == 0,
948 "fbk hash lookup should have failed");
953 /* fill up the hash_table */
954 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
955 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
957 /* Find non-existent key in a full hashtable */
958 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
959 RETURN_IF_ERROR_FBK(status != -ENOENT,
960 "fbk hash lookup succeeded");
962 /* Delete non-existent key in a full hashtable */
963 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
964 RETURN_IF_ERROR_FBK(status != -ENOENT,
965 "fbk hash delete succeeded");
967 /* Delete one key from a full hashtable */
968 status = rte_fbk_hash_delete_key(handle, 1);
969 RETURN_IF_ERROR_FBK(status != 0,
970 "fbk hash delete failed");
972 /* Clear all entries. */
973 rte_fbk_hash_clear_all(handle);
976 rte_fbk_hash_free(handle);
978 /* Cover the NULL case. */
979 rte_fbk_hash_free(0);
985 * Sequence of operations for find existing fbk hash table
988 * - find existing table: hit
989 * - find non-existing table: miss
992 static int test_fbk_hash_find_existing(void)
994 struct rte_fbk_hash_params params = {
995 .name = "fbk_hash_find_existing",
996 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
997 .entries_per_bucket = 4,
1000 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
1002 /* Create hash table. */
1003 handle = rte_fbk_hash_create(¶ms);
1004 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1006 /* Try to find existing fbk hash table */
1007 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1008 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1010 /* Try to find non-existing fbk hash table */
1011 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1012 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1015 rte_fbk_hash_free(handle);
1020 #define BUCKET_ENTRIES 4
1022 * Do tests for hash creation with bad parameters.
1024 static int test_hash_creation_with_bad_parameters(void)
1026 struct rte_hash *handle, *tmp;
1027 struct rte_hash_parameters params;
1029 handle = rte_hash_create(NULL);
1030 if (handle != NULL) {
1031 rte_hash_free(handle);
1032 printf("Impossible creating hash successfully without any parameter\n");
1036 memcpy(¶ms, &ut_params, sizeof(params));
1037 params.name = "creation_with_bad_parameters_0";
1038 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1039 handle = rte_hash_create(¶ms);
1040 if (handle != NULL) {
1041 rte_hash_free(handle);
1042 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1046 memcpy(¶ms, &ut_params, sizeof(params));
1047 params.name = "creation_with_bad_parameters_2";
1048 params.entries = BUCKET_ENTRIES - 1;
1049 handle = rte_hash_create(¶ms);
1050 if (handle != NULL) {
1051 rte_hash_free(handle);
1052 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1056 memcpy(¶ms, &ut_params, sizeof(params));
1057 params.name = "creation_with_bad_parameters_3";
1059 handle = rte_hash_create(¶ms);
1060 if (handle != NULL) {
1061 rte_hash_free(handle);
1062 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1066 memcpy(¶ms, &ut_params, sizeof(params));
1067 params.name = "creation_with_bad_parameters_4";
1068 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1069 handle = rte_hash_create(¶ms);
1070 if (handle != NULL) {
1071 rte_hash_free(handle);
1072 printf("Impossible creating hash successfully with invalid socket\n");
1076 /* test with same name should fail */
1077 memcpy(¶ms, &ut_params, sizeof(params));
1078 params.name = "same_name";
1079 handle = rte_hash_create(¶ms);
1080 if (handle == NULL) {
1081 printf("Cannot create first hash table with 'same_name'\n");
1084 tmp = rte_hash_create(¶ms);
1086 printf("Creation of hash table with same name should fail\n");
1087 rte_hash_free(handle);
1091 rte_hash_free(handle);
1093 printf("# Test successful. No more errors expected\n");
1099 * Do tests for hash creation with parameters that look incorrect
1100 * but are actually valid.
1103 test_hash_creation_with_good_parameters(void)
1105 struct rte_hash *handle;
1106 struct rte_hash_parameters params;
1108 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1109 memcpy(¶ms, &ut_params, sizeof(params));
1110 params.name = "name";
1111 params.hash_func = NULL;
1112 handle = rte_hash_create(¶ms);
1113 if (handle == NULL) {
1114 printf("Creating hash with null hash_func failed\n");
1118 rte_hash_free(handle);
1123 #define ITERATIONS 3
1125 * Test to see the average table utilization (entries added/max entries)
1126 * before hitting a random entry that cannot be added
1128 static int test_average_table_utilization(void)
1130 struct rte_hash *handle;
1131 uint8_t simple_key[MAX_KEYSIZE];
1133 unsigned added_keys, average_keys_added = 0;
1136 printf("\n# Running test to determine average utilization"
1137 "\n before adding elements begins to fail\n");
1138 printf("Measuring performance, please wait");
1140 ut_params.entries = 1 << 16;
1141 ut_params.name = "test_average_utilization";
1142 ut_params.hash_func = rte_jhash;
1143 handle = rte_hash_create(&ut_params);
1144 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1146 for (j = 0; j < ITERATIONS; j++) {
1148 /* Add random entries until key cannot be added */
1149 for (added_keys = 0; ret >= 0; added_keys++) {
1150 for (i = 0; i < ut_params.key_len; i++)
1151 simple_key[i] = rte_rand() % 255;
1152 ret = rte_hash_add_key(handle, simple_key);
1154 if (ret != -ENOSPC) {
1155 printf("Unexpected error when adding keys\n");
1156 rte_hash_free(handle);
1160 average_keys_added += added_keys;
1162 /* Reset the table */
1163 rte_hash_reset(handle);
1165 /* Print a dot to show progress on operations */
1170 average_keys_added /= ITERATIONS;
1172 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1173 ((double) average_keys_added / ut_params.entries * 100),
1174 average_keys_added, ut_params.entries);
1175 rte_hash_free(handle);
1180 #define NUM_ENTRIES 256
1181 static int test_hash_iteration(void)
1183 struct rte_hash *handle;
1185 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1186 const void *next_key;
1188 void *data[NUM_ENTRIES];
1189 unsigned added_keys;
1193 ut_params.entries = NUM_ENTRIES;
1194 ut_params.name = "test_hash_iteration";
1195 ut_params.hash_func = rte_jhash;
1196 ut_params.key_len = 16;
1197 handle = rte_hash_create(&ut_params);
1198 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1200 /* Add random entries until key cannot be added */
1201 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1202 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1203 for (i = 0; i < ut_params.key_len; i++)
1204 keys[added_keys][i] = rte_rand() % 255;
1205 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1210 /* Iterate through the hash table */
1211 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1212 /* Search for the key in the list of keys added */
1213 for (i = 0; i < NUM_ENTRIES; i++) {
1214 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1215 if (next_data != data[i]) {
1216 printf("Data found in the hash table is"
1217 "not the data added with the key\n");
1224 if (i == NUM_ENTRIES) {
1225 printf("Key found in the hash table was not added\n");
1230 /* Check if all keys have been iterated */
1231 if (added_keys != 0) {
1232 printf("There were still %u keys to iterate\n", added_keys);
1236 rte_hash_free(handle);
1240 rte_hash_free(handle);
1244 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1245 0x04, 0x05, 0x06, 0x07,
1246 0x08, 0x09, 0x0a, 0x0b,
1247 0x0c, 0x0d, 0x0e, 0x0f};
1248 static struct rte_hash_parameters hash_params_ex = {
1253 .hash_func_init_val = 0,
1258 * add/delete key with jhash2
1261 test_hash_add_delete_jhash2(void)
1264 struct rte_hash *handle;
1267 hash_params_ex.name = "hash_test_jhash2";
1268 hash_params_ex.key_len = 4;
1269 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1271 handle = rte_hash_create(&hash_params_ex);
1272 if (handle == NULL) {
1273 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1276 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1278 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1282 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1283 if (pos2 < 0 || pos1 != pos2) {
1284 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1291 rte_hash_free(handle);
1297 * add/delete (2) key with jhash2
1300 test_hash_add_delete_2_jhash2(void)
1303 struct rte_hash *handle;
1306 hash_params_ex.name = "hash_test_2_jhash2";
1307 hash_params_ex.key_len = 8;
1308 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1310 handle = rte_hash_create(&hash_params_ex);
1314 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1318 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1319 if (pos2 < 0 || pos1 != pos2)
1326 rte_hash_free(handle);
1332 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1334 const uint32_t *k = key;
1336 RTE_SET_USED(length);
1338 return rte_jhash_1word(k[0], initval);
1342 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1344 const uint32_t *k = key;
1346 RTE_SET_USED(length);
1348 return rte_jhash_2words(k[0], k[1], initval);
1352 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1354 const uint32_t *k = key;
1356 RTE_SET_USED(length);
1358 return rte_jhash_3words(k[0], k[1], k[2], initval);
1362 * add/delete key with jhash 1word
1365 test_hash_add_delete_jhash_1word(void)
1368 struct rte_hash *handle;
1371 hash_params_ex.name = "hash_test_jhash_1word";
1372 hash_params_ex.key_len = 4;
1373 hash_params_ex.hash_func = test_hash_jhash_1word;
1375 handle = rte_hash_create(&hash_params_ex);
1377 goto fail_jhash_1word;
1379 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1381 goto fail_jhash_1word;
1383 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1384 if (pos2 < 0 || pos1 != pos2)
1385 goto fail_jhash_1word;
1391 rte_hash_free(handle);
1397 * add/delete key with jhash 2word
1400 test_hash_add_delete_jhash_2word(void)
1403 struct rte_hash *handle;
1406 hash_params_ex.name = "hash_test_jhash_2word";
1407 hash_params_ex.key_len = 8;
1408 hash_params_ex.hash_func = test_hash_jhash_2word;
1410 handle = rte_hash_create(&hash_params_ex);
1412 goto fail_jhash_2word;
1414 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1416 goto fail_jhash_2word;
1418 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1419 if (pos2 < 0 || pos1 != pos2)
1420 goto fail_jhash_2word;
1426 rte_hash_free(handle);
1432 * add/delete key with jhash 3word
1435 test_hash_add_delete_jhash_3word(void)
1438 struct rte_hash *handle;
1441 hash_params_ex.name = "hash_test_jhash_3word";
1442 hash_params_ex.key_len = 12;
1443 hash_params_ex.hash_func = test_hash_jhash_3word;
1445 handle = rte_hash_create(&hash_params_ex);
1447 goto fail_jhash_3word;
1449 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1451 goto fail_jhash_3word;
1453 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1454 if (pos2 < 0 || pos1 != pos2)
1455 goto fail_jhash_3word;
1461 rte_hash_free(handle);
1467 * Do all unit and performance tests.
1472 if (test_add_delete() < 0)
1474 if (test_hash_add_delete_jhash2() < 0)
1476 if (test_hash_add_delete_2_jhash2() < 0)
1478 if (test_hash_add_delete_jhash_1word() < 0)
1480 if (test_hash_add_delete_jhash_2word() < 0)
1482 if (test_hash_add_delete_jhash_3word() < 0)
1484 if (test_hash_get_key_with_position() < 0)
1486 if (test_hash_find_existing() < 0)
1488 if (test_add_update_delete() < 0)
1490 if (test_five_keys() < 0)
1492 if (test_full_bucket() < 0)
1495 if (test_fbk_hash_find_existing() < 0)
1497 if (fbk_hash_unit_test() < 0)
1499 if (test_hash_creation_with_bad_parameters() < 0)
1501 if (test_hash_creation_with_good_parameters() < 0)
1503 if (test_average_table_utilization() < 0)
1505 if (test_hash_iteration() < 0)
1508 run_hash_func_tests();
1510 if (test_crc32_hash_alg_equiv() < 0)
1516 REGISTER_TEST_COMMAND(hash_autotest, test_hash);