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)
113 #define UNIT_TEST_HASH_VERBOSE 0
115 * Print out result of unit test hash operation.
117 static void print_key_info(const char *msg, const struct flow_key *key,
120 if (UNIT_TEST_HASH_VERBOSE) {
121 const uint8_t *p = (const uint8_t *)key;
124 printf("%s key:0x", msg);
125 for (i = 0; i < sizeof(struct flow_key); i++)
126 printf("%02X", p[i]);
127 printf(" @ pos %d\n", pos);
131 /* Keys used by unit test functions */
132 static struct flow_key keys[5] = { {
133 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
134 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
139 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
140 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
145 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
146 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
151 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
152 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
157 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
158 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
164 /* Parameters used for hash table in unit test functions. Name set later. */
165 static struct rte_hash_parameters ut_params = {
167 .key_len = sizeof(struct flow_key), /* 13 */
168 .hash_func = rte_jhash,
169 .hash_func_init_val = 0,
173 #define CRC32_ITERATIONS (1U << 10)
174 #define CRC32_DWORDS (1U << 6)
176 * Test if all CRC32 implementations yield the same hash value
179 test_crc32_hash_alg_equiv(void)
183 uint64_t data64[CRC32_DWORDS];
187 printf("\n# CRC32 implementations equivalence test\n");
188 for (i = 0; i < CRC32_ITERATIONS; i++) {
189 /* Randomizing data_len of data set */
190 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
191 init_val = (uint32_t) rte_rand();
193 /* Fill the data set */
194 for (j = 0; j < CRC32_DWORDS; j++)
195 data64[j] = rte_rand();
197 /* Calculate software CRC32 */
198 rte_hash_crc_set_alg(CRC32_SW);
199 hash_val = rte_hash_crc(data64, data_len, init_val);
201 /* Check against 4-byte-operand sse4.2 CRC32 if available */
202 rte_hash_crc_set_alg(CRC32_SSE42);
203 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
204 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
208 /* Check against 8-byte-operand sse4.2 CRC32 if available */
209 rte_hash_crc_set_alg(CRC32_SSE42_x64);
210 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
211 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
215 /* Check against 8-byte-operand ARM64 CRC32 if available */
216 rte_hash_crc_set_alg(CRC32_ARM64);
217 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
218 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
223 /* Resetting to best available algorithm */
224 rte_hash_crc_set_alg(CRC32_SSE42_x64);
226 if (i == CRC32_ITERATIONS)
229 printf("Failed test data (hex, %zu bytes total):\n", data_len);
230 for (j = 0; j < data_len; j++)
231 printf("%02X%c", ((uint8_t *)data64)[j],
232 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
238 * Test a hash function.
240 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
243 static uint8_t key[MAX_KEYSIZE];
247 for (i = 0; i < key_len; i++)
248 key[i] = (uint8_t) rte_rand();
250 /* just to be on the safe side */
254 f(key, key_len, init_val);
258 * Test all hash functions.
260 static void run_hash_func_tests(void)
265 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
268 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
271 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
273 run_hash_func_test(hashtest_funcs[i],
274 hashtest_initvals[j],
275 hashtest_key_lens[k]);
282 * Basic sequence of operations for a single key:
288 static int test_add_delete(void)
290 struct rte_hash *handle;
291 /* test with standard add/lookup/delete functions */
292 int pos0, expectedPos0;
294 ut_params.name = "test1";
295 handle = rte_hash_create(&ut_params);
296 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
298 pos0 = rte_hash_add_key(handle, &keys[0]);
299 print_key_info("Add", &keys[0], pos0);
300 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
303 pos0 = rte_hash_lookup(handle, &keys[0]);
304 print_key_info("Lkp", &keys[0], pos0);
305 RETURN_IF_ERROR(pos0 != expectedPos0,
306 "failed to find key (pos0=%d)", pos0);
308 pos0 = rte_hash_del_key(handle, &keys[0]);
309 print_key_info("Del", &keys[0], pos0);
310 RETURN_IF_ERROR(pos0 != expectedPos0,
311 "failed to delete key (pos0=%d)", pos0);
313 pos0 = rte_hash_lookup(handle, &keys[0]);
314 print_key_info("Lkp", &keys[0], pos0);
315 RETURN_IF_ERROR(pos0 != -ENOENT,
316 "fail: found key after deleting! (pos0=%d)", pos0);
318 rte_hash_free(handle);
320 /* repeat test with precomputed hash functions */
321 hash_sig_t hash_value;
322 int pos1, expectedPos1;
324 handle = rte_hash_create(&ut_params);
325 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
327 hash_value = rte_hash_hash(handle, &keys[0]);
328 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
329 print_key_info("Add", &keys[0], pos1);
330 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
333 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
334 print_key_info("Lkp", &keys[0], pos1);
335 RETURN_IF_ERROR(pos1 != expectedPos1,
336 "failed to find key (pos1=%d)", pos1);
338 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
339 print_key_info("Del", &keys[0], pos1);
340 RETURN_IF_ERROR(pos1 != expectedPos1,
341 "failed to delete key (pos1=%d)", pos1);
343 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
344 print_key_info("Lkp", &keys[0], pos1);
345 RETURN_IF_ERROR(pos1 != -ENOENT,
346 "fail: found key after deleting! (pos1=%d)", pos1);
348 rte_hash_free(handle);
354 * Sequence of operations for a single key:
359 * - lookup: hit (updated data)
364 static int test_add_update_delete(void)
366 struct rte_hash *handle;
367 int pos0, expectedPos0;
369 ut_params.name = "test2";
370 handle = rte_hash_create(&ut_params);
371 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
373 pos0 = rte_hash_del_key(handle, &keys[0]);
374 print_key_info("Del", &keys[0], pos0);
375 RETURN_IF_ERROR(pos0 != -ENOENT,
376 "fail: found non-existent key (pos0=%d)", pos0);
378 pos0 = rte_hash_add_key(handle, &keys[0]);
379 print_key_info("Add", &keys[0], pos0);
380 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
383 pos0 = rte_hash_lookup(handle, &keys[0]);
384 print_key_info("Lkp", &keys[0], pos0);
385 RETURN_IF_ERROR(pos0 != expectedPos0,
386 "failed to find key (pos0=%d)", pos0);
388 pos0 = rte_hash_add_key(handle, &keys[0]);
389 print_key_info("Add", &keys[0], pos0);
390 RETURN_IF_ERROR(pos0 != expectedPos0,
391 "failed to re-add key (pos0=%d)", pos0);
393 pos0 = rte_hash_lookup(handle, &keys[0]);
394 print_key_info("Lkp", &keys[0], pos0);
395 RETURN_IF_ERROR(pos0 != expectedPos0,
396 "failed to find key (pos0=%d)", pos0);
398 pos0 = rte_hash_del_key(handle, &keys[0]);
399 print_key_info("Del", &keys[0], pos0);
400 RETURN_IF_ERROR(pos0 != expectedPos0,
401 "failed to delete 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 != -ENOENT,
406 "fail: deleted already deleted key (pos0=%d)", pos0);
408 pos0 = rte_hash_lookup(handle, &keys[0]);
409 print_key_info("Lkp", &keys[0], pos0);
410 RETURN_IF_ERROR(pos0 != -ENOENT,
411 "fail: found key after deleting! (pos0=%d)", pos0);
413 rte_hash_free(handle);
418 * Sequence of operations for retrieving a key with its position
422 * - get the key with its position: hit
424 * - try to get the deleted key: miss
427 static int test_hash_get_key_with_position(void)
429 struct rte_hash *handle = NULL;
430 int pos, expectedPos, result;
433 ut_params.name = "hash_get_key_w_pos";
434 handle = rte_hash_create(&ut_params);
435 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
437 pos = rte_hash_add_key(handle, &keys[0]);
438 print_key_info("Add", &keys[0], pos);
439 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
442 result = rte_hash_get_key_with_position(handle, pos, &key);
443 RETURN_IF_ERROR(result != 0, "error retrieving a key");
445 pos = rte_hash_del_key(handle, &keys[0]);
446 print_key_info("Del", &keys[0], pos);
447 RETURN_IF_ERROR(pos != expectedPos,
448 "failed to delete key (pos0=%d)", pos);
450 result = rte_hash_get_key_with_position(handle, pos, &key);
451 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
453 rte_hash_free(handle);
458 * Sequence of operations for find existing hash table
461 * - find existing table: hit
462 * - find non-existing table: miss
465 static int test_hash_find_existing(void)
467 struct rte_hash *handle = NULL, *result = NULL;
469 /* Create hash table. */
470 ut_params.name = "hash_find_existing";
471 handle = rte_hash_create(&ut_params);
472 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
474 /* Try to find existing hash table */
475 result = rte_hash_find_existing("hash_find_existing");
476 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
478 /* Try to find non-existing hash table */
479 result = rte_hash_find_existing("hash_find_non_existing");
480 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
483 rte_hash_free(handle);
489 * Sequence of operations for 5 keys
492 * - add keys (update)
493 * - lookup keys: hit (updated data)
494 * - delete keys : hit
495 * - lookup keys: miss
497 static int test_five_keys(void)
499 struct rte_hash *handle;
500 const void *key_array[5] = {0};
506 ut_params.name = "test3";
507 handle = rte_hash_create(&ut_params);
508 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
511 for (i = 0; i < 5; i++) {
512 pos[i] = rte_hash_add_key(handle, &keys[i]);
513 print_key_info("Add", &keys[i], pos[i]);
514 RETURN_IF_ERROR(pos[i] < 0,
515 "failed to add key (pos[%u]=%d)", i, pos[i]);
516 expected_pos[i] = pos[i];
520 for(i = 0; i < 5; i++)
521 key_array[i] = &keys[i];
523 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
525 for(i = 0; i < 5; i++) {
526 print_key_info("Lkp", key_array[i], pos[i]);
527 RETURN_IF_ERROR(pos[i] != expected_pos[i],
528 "failed to find key (pos[%u]=%d)", i, pos[i]);
532 for (i = 0; i < 5; i++) {
533 pos[i] = rte_hash_add_key(handle, &keys[i]);
534 print_key_info("Add", &keys[i], pos[i]);
535 RETURN_IF_ERROR(pos[i] != expected_pos[i],
536 "failed to add key (pos[%u]=%d)", i, pos[i]);
540 for (i = 0; i < 5; i++) {
541 pos[i] = rte_hash_lookup(handle, &keys[i]);
542 print_key_info("Lkp", &keys[i], pos[i]);
543 RETURN_IF_ERROR(pos[i] != expected_pos[i],
544 "failed to find key (pos[%u]=%d)", i, pos[i]);
548 for (i = 0; i < 5; i++) {
549 pos[i] = rte_hash_del_key(handle, &keys[i]);
550 print_key_info("Del", &keys[i], pos[i]);
551 RETURN_IF_ERROR(pos[i] != expected_pos[i],
552 "failed to delete key (pos[%u]=%d)", i, pos[i]);
556 for (i = 0; i < 5; i++) {
557 pos[i] = rte_hash_lookup(handle, &keys[i]);
558 print_key_info("Lkp", &keys[i], pos[i]);
559 RETURN_IF_ERROR(pos[i] != -ENOENT,
560 "found non-existent key (pos[%u]=%d)", i, pos[i]);
564 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
566 for (i = 0; i < 5; i++) {
567 print_key_info("Lkp", key_array[i], pos[i]);
568 RETURN_IF_ERROR(pos[i] != -ENOENT,
569 "found not-existent key (pos[%u]=%d)", i, pos[i]);
572 rte_hash_free(handle);
578 * Add keys to the same bucket until bucket full.
579 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
580 * first 4 successful, 5th successful, pushing existing item in bucket
581 * - lookup the 5 keys: 5 hits
582 * - add the 5 keys again: 5 OK
583 * - lookup the 5 keys: 5 hits (updated data)
584 * - delete the 5 keys: 5 OK
585 * - lookup the 5 keys: 5 misses
587 static int test_full_bucket(void)
589 struct rte_hash_parameters params_pseudo_hash = {
592 .key_len = sizeof(struct flow_key), /* 13 */
593 .hash_func = pseudo_hash,
594 .hash_func_init_val = 0,
597 struct rte_hash *handle;
602 handle = rte_hash_create(¶ms_pseudo_hash);
603 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
606 for (i = 0; i < 4; i++) {
607 pos[i] = rte_hash_add_key(handle, &keys[i]);
608 print_key_info("Add", &keys[i], pos[i]);
609 RETURN_IF_ERROR(pos[i] < 0,
610 "failed to add key (pos[%u]=%d)", i, pos[i]);
611 expected_pos[i] = pos[i];
614 * This should work and will push one of the items
615 * in the bucket because it is full
617 pos[4] = rte_hash_add_key(handle, &keys[4]);
618 print_key_info("Add", &keys[4], pos[4]);
619 RETURN_IF_ERROR(pos[4] < 0,
620 "failed to add key (pos[4]=%d)", pos[4]);
621 expected_pos[4] = pos[4];
624 for (i = 0; i < 5; i++) {
625 pos[i] = rte_hash_lookup(handle, &keys[i]);
626 print_key_info("Lkp", &keys[i], pos[i]);
627 RETURN_IF_ERROR(pos[i] != expected_pos[i],
628 "failed to find key (pos[%u]=%d)", i, pos[i]);
632 for (i = 0; i < 5; i++) {
633 pos[i] = rte_hash_add_key(handle, &keys[i]);
634 print_key_info("Add", &keys[i], pos[i]);
635 RETURN_IF_ERROR(pos[i] != expected_pos[i],
636 "failed to add key (pos[%u]=%d)", i, pos[i]);
640 for (i = 0; i < 5; i++) {
641 pos[i] = rte_hash_lookup(handle, &keys[i]);
642 print_key_info("Lkp", &keys[i], pos[i]);
643 RETURN_IF_ERROR(pos[i] != expected_pos[i],
644 "failed to find key (pos[%u]=%d)", i, pos[i]);
647 /* Delete 1 key, check other keys are still found */
648 pos[1] = rte_hash_del_key(handle, &keys[1]);
649 print_key_info("Del", &keys[1], pos[1]);
650 RETURN_IF_ERROR(pos[1] != expected_pos[1],
651 "failed to delete key (pos[1]=%d)", pos[1]);
652 pos[3] = rte_hash_lookup(handle, &keys[3]);
653 print_key_info("Lkp", &keys[3], pos[3]);
654 RETURN_IF_ERROR(pos[3] != expected_pos[3],
655 "failed lookup after deleting key from same bucket "
656 "(pos[3]=%d)", pos[3]);
658 /* Go back to previous state */
659 pos[1] = rte_hash_add_key(handle, &keys[1]);
660 print_key_info("Add", &keys[1], pos[1]);
661 expected_pos[1] = pos[1];
662 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
665 for (i = 0; i < 5; i++) {
666 pos[i] = rte_hash_del_key(handle, &keys[i]);
667 print_key_info("Del", &keys[i], pos[i]);
668 RETURN_IF_ERROR(pos[i] != expected_pos[i],
669 "failed to delete key (pos[%u]=%d)", i, pos[i]);
673 for (i = 0; i < 5; i++) {
674 pos[i] = rte_hash_lookup(handle, &keys[i]);
675 print_key_info("Lkp", &keys[i], pos[i]);
676 RETURN_IF_ERROR(pos[i] != -ENOENT,
677 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
680 rte_hash_free(handle);
682 /* Cover the NULL case. */
687 /******************************************************************************/
689 fbk_hash_unit_test(void)
691 struct rte_fbk_hash_params params = {
692 .name = "fbk_hash_test",
693 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
694 .entries_per_bucket = 4,
698 struct rte_fbk_hash_params invalid_params_1 = {
700 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
701 .entries_per_bucket = 4,
705 struct rte_fbk_hash_params invalid_params_2 = {
708 .entries_per_bucket = 3, /* Not power of 2 */
712 struct rte_fbk_hash_params invalid_params_3 = {
714 .entries = 0, /* Entries is 0 */
715 .entries_per_bucket = 4,
719 struct rte_fbk_hash_params invalid_params_4 = {
721 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
722 .entries_per_bucket = 0, /* Entries per bucket is 0 */
726 struct rte_fbk_hash_params invalid_params_5 = {
729 .entries_per_bucket = 8, /* Entries per bucket > entries */
733 struct rte_fbk_hash_params invalid_params_6 = {
735 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
736 .entries_per_bucket = 4,
740 struct rte_fbk_hash_params invalid_params_7 = {
742 .entries = RTE_FBK_HASH_ENTRIES_MAX,
743 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
747 struct rte_fbk_hash_params invalid_params_8 = {
749 .entries = RTE_FBK_HASH_ENTRIES_MAX,
750 .entries_per_bucket = 4,
751 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
754 /* try to create two hashes with identical names
755 * in this case, trying to create a second one will not
756 * fail but will simply return pointer to the existing
757 * hash with that name. sort of like a "find hash by name" :-)
759 struct rte_fbk_hash_params invalid_params_same_name_1 = {
760 .name = "same_name", /* hash with identical name */
762 .entries_per_bucket = 2,
766 /* trying to create this hash should return a pointer to an existing hash */
767 struct rte_fbk_hash_params invalid_params_same_name_2 = {
768 .name = "same_name", /* hash with identical name */
769 .entries = RTE_FBK_HASH_ENTRIES_MAX,
770 .entries_per_bucket = 4,
774 /* this is a sanity check for "same name" test
775 * creating this hash will check if we are actually able to create
776 * multiple hashes with different names (instead of having just one).
778 struct rte_fbk_hash_params different_name = {
779 .name = "different_name", /* different name */
780 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
781 .entries_per_bucket = 4,
785 struct rte_fbk_hash_params params_jhash = {
787 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
788 .entries_per_bucket = 4,
790 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
791 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
794 struct rte_fbk_hash_params params_nohash = {
795 .name = "valid nohash",
796 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
797 .entries_per_bucket = 4,
799 .hash_func = NULL, /* Tests for null hash_func */
800 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
803 struct rte_fbk_hash_table *handle, *tmp;
805 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
806 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
811 /* Try creating hashes with invalid parameters */
812 printf("# Testing hash creation with invalid parameters "
813 "- expect error msgs\n");
814 handle = rte_fbk_hash_create(&invalid_params_1);
815 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
817 handle = rte_fbk_hash_create(&invalid_params_2);
818 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
820 handle = rte_fbk_hash_create(&invalid_params_3);
821 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
823 handle = rte_fbk_hash_create(&invalid_params_4);
824 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
826 handle = rte_fbk_hash_create(&invalid_params_5);
827 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
829 handle = rte_fbk_hash_create(&invalid_params_6);
830 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
832 handle = rte_fbk_hash_create(&invalid_params_7);
833 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
835 handle = rte_fbk_hash_create(&invalid_params_8);
836 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
838 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
839 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
841 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
843 rte_fbk_hash_free(tmp);
844 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
846 /* we are not freeing handle here because we need a hash list
847 * to be not empty for the next test */
849 /* create a hash in non-empty list - good for coverage */
850 tmp = rte_fbk_hash_create(&different_name);
851 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
853 /* free both hashes */
854 rte_fbk_hash_free(handle);
855 rte_fbk_hash_free(tmp);
857 /* Create empty jhash hash. */
858 handle = rte_fbk_hash_create(¶ms_jhash);
859 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
862 rte_fbk_hash_free(handle);
864 /* Create empty jhash hash. */
865 handle = rte_fbk_hash_create(¶ms_nohash);
866 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
869 rte_fbk_hash_free(handle);
871 /* Create empty hash. */
872 handle = rte_fbk_hash_create(¶ms);
873 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
875 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
876 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
877 "load factor right after creation is not zero but it should be");
879 for (i = 0; i < 5; i++) {
880 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
881 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
884 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
885 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
886 "load factor now is not as expected");
887 /* Find value of added keys. */
888 for (i = 0; i < 5; i++) {
889 status = rte_fbk_hash_lookup(handle, keys[i]);
890 RETURN_IF_ERROR_FBK(status != vals[i],
891 "fbk hash lookup failed");
894 /* Change value of added keys. */
895 for (i = 0; i < 5; i++) {
896 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
897 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
900 /* Find new values. */
901 for (i = 0; i < 5; i++) {
902 status = rte_fbk_hash_lookup(handle, keys[i]);
903 RETURN_IF_ERROR_FBK(status != vals[4-i],
904 "fbk hash lookup failed");
907 /* Delete keys individually. */
908 for (i = 0; i < 5; i++) {
909 status = rte_fbk_hash_delete_key(handle, keys[i]);
910 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
913 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
914 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
915 "load factor right after deletion is not zero but it should be");
916 /* Lookup should now fail. */
917 for (i = 0; i < 5; i++) {
918 status = rte_fbk_hash_lookup(handle, keys[i]);
919 RETURN_IF_ERROR_FBK(status == 0,
920 "fbk hash lookup should have failed");
923 /* Add keys again. */
924 for (i = 0; i < 5; i++) {
925 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
926 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
929 /* Make sure they were added. */
930 for (i = 0; i < 5; i++) {
931 status = rte_fbk_hash_lookup(handle, keys[i]);
932 RETURN_IF_ERROR_FBK(status != vals[i],
933 "fbk hash lookup failed");
936 /* Clear all entries. */
937 rte_fbk_hash_clear_all(handle);
939 /* Lookup should fail. */
940 for (i = 0; i < 5; i++) {
941 status = rte_fbk_hash_lookup(handle, keys[i]);
942 RETURN_IF_ERROR_FBK(status == 0,
943 "fbk hash lookup should have failed");
948 /* fill up the hash_table */
949 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
950 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
952 /* Find non-existent key in a full hashtable */
953 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
954 RETURN_IF_ERROR_FBK(status != -ENOENT,
955 "fbk hash lookup succeeded");
957 /* Delete non-existent key in a full hashtable */
958 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
959 RETURN_IF_ERROR_FBK(status != -ENOENT,
960 "fbk hash delete succeeded");
962 /* Delete one key from a full hashtable */
963 status = rte_fbk_hash_delete_key(handle, 1);
964 RETURN_IF_ERROR_FBK(status != 0,
965 "fbk hash delete failed");
967 /* Clear all entries. */
968 rte_fbk_hash_clear_all(handle);
971 rte_fbk_hash_free(handle);
973 /* Cover the NULL case. */
974 rte_fbk_hash_free(0);
980 * Sequence of operations for find existing fbk hash table
983 * - find existing table: hit
984 * - find non-existing table: miss
987 static int test_fbk_hash_find_existing(void)
989 struct rte_fbk_hash_params params = {
990 .name = "fbk_hash_find_existing",
991 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
992 .entries_per_bucket = 4,
995 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
997 /* Create hash table. */
998 handle = rte_fbk_hash_create(¶ms);
999 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1001 /* Try to find existing fbk hash table */
1002 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1003 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1005 /* Try to find non-existing fbk hash table */
1006 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1007 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1010 rte_fbk_hash_free(handle);
1015 #define BUCKET_ENTRIES 4
1017 * Do tests for hash creation with bad parameters.
1019 static int test_hash_creation_with_bad_parameters(void)
1021 struct rte_hash *handle, *tmp;
1022 struct rte_hash_parameters params;
1024 handle = rte_hash_create(NULL);
1025 if (handle != NULL) {
1026 rte_hash_free(handle);
1027 printf("Impossible creating hash sucessfully without any parameter\n");
1031 memcpy(¶ms, &ut_params, sizeof(params));
1032 params.name = "creation_with_bad_parameters_0";
1033 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1034 handle = rte_hash_create(¶ms);
1035 if (handle != NULL) {
1036 rte_hash_free(handle);
1037 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1041 memcpy(¶ms, &ut_params, sizeof(params));
1042 params.name = "creation_with_bad_parameters_2";
1043 params.entries = BUCKET_ENTRIES - 1;
1044 handle = rte_hash_create(¶ms);
1045 if (handle != NULL) {
1046 rte_hash_free(handle);
1047 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1051 memcpy(¶ms, &ut_params, sizeof(params));
1052 params.name = "creation_with_bad_parameters_3";
1054 handle = rte_hash_create(¶ms);
1055 if (handle != NULL) {
1056 rte_hash_free(handle);
1057 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1061 memcpy(¶ms, &ut_params, sizeof(params));
1062 params.name = "creation_with_bad_parameters_4";
1063 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1064 handle = rte_hash_create(¶ms);
1065 if (handle != NULL) {
1066 rte_hash_free(handle);
1067 printf("Impossible creating hash sucessfully with invalid socket\n");
1071 /* test with same name should fail */
1072 memcpy(¶ms, &ut_params, sizeof(params));
1073 params.name = "same_name";
1074 handle = rte_hash_create(¶ms);
1075 if (handle == NULL) {
1076 printf("Cannot create first hash table with 'same_name'\n");
1079 tmp = rte_hash_create(¶ms);
1081 printf("Creation of hash table with same name should fail\n");
1082 rte_hash_free(handle);
1086 rte_hash_free(handle);
1088 printf("# Test successful. No more errors expected\n");
1094 * Do tests for hash creation with parameters that look incorrect
1095 * but are actually valid.
1098 test_hash_creation_with_good_parameters(void)
1100 struct rte_hash *handle;
1101 struct rte_hash_parameters params;
1103 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1104 memcpy(¶ms, &ut_params, sizeof(params));
1105 params.name = "name";
1106 params.hash_func = NULL;
1107 handle = rte_hash_create(¶ms);
1108 if (handle == NULL) {
1109 printf("Creating hash with null hash_func failed\n");
1113 rte_hash_free(handle);
1118 #define ITERATIONS 3
1120 * Test to see the average table utilization (entries added/max entries)
1121 * before hitting a random entry that cannot be added
1123 static int test_average_table_utilization(void)
1125 struct rte_hash *handle;
1126 uint8_t simple_key[MAX_KEYSIZE];
1128 unsigned added_keys, average_keys_added = 0;
1131 printf("\n# Running test to determine average utilization"
1132 "\n before adding elements begins to fail\n");
1133 printf("Measuring performance, please wait");
1135 ut_params.entries = 1 << 16;
1136 ut_params.name = "test_average_utilization";
1137 ut_params.hash_func = rte_jhash;
1138 handle = rte_hash_create(&ut_params);
1139 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1141 for (j = 0; j < ITERATIONS; j++) {
1143 /* Add random entries until key cannot be added */
1144 for (added_keys = 0; ret >= 0; added_keys++) {
1145 for (i = 0; i < ut_params.key_len; i++)
1146 simple_key[i] = rte_rand() % 255;
1147 ret = rte_hash_add_key(handle, simple_key);
1149 if (ret != -ENOSPC) {
1150 printf("Unexpected error when adding keys\n");
1151 rte_hash_free(handle);
1155 average_keys_added += added_keys;
1157 /* Reset the table */
1158 rte_hash_reset(handle);
1160 /* Print a dot to show progress on operations */
1165 average_keys_added /= ITERATIONS;
1167 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1168 ((double) average_keys_added / ut_params.entries * 100),
1169 average_keys_added, ut_params.entries);
1170 rte_hash_free(handle);
1175 #define NUM_ENTRIES 256
1176 static int test_hash_iteration(void)
1178 struct rte_hash *handle;
1180 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1181 const void *next_key;
1183 void *data[NUM_ENTRIES];
1184 unsigned added_keys;
1188 ut_params.entries = NUM_ENTRIES;
1189 ut_params.name = "test_hash_iteration";
1190 ut_params.hash_func = rte_jhash;
1191 ut_params.key_len = 16;
1192 handle = rte_hash_create(&ut_params);
1193 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1195 /* Add random entries until key cannot be added */
1196 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1197 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1198 for (i = 0; i < ut_params.key_len; i++)
1199 keys[added_keys][i] = rte_rand() % 255;
1200 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1205 /* Iterate through the hash table */
1206 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1207 /* Search for the key in the list of keys added */
1208 for (i = 0; i < NUM_ENTRIES; i++) {
1209 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1210 if (next_data != data[i]) {
1211 printf("Data found in the hash table is"
1212 "not the data added with the key\n");
1219 if (i == NUM_ENTRIES) {
1220 printf("Key found in the hash table was not added\n");
1225 /* Check if all keys have been iterated */
1226 if (added_keys != 0) {
1227 printf("There were still %u keys to iterate\n", added_keys);
1231 rte_hash_free(handle);
1235 rte_hash_free(handle);
1239 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1240 0x04, 0x05, 0x06, 0x07,
1241 0x08, 0x09, 0x0a, 0x0b,
1242 0x0c, 0x0d, 0x0e, 0x0f};
1243 static struct rte_hash_parameters hash_params_ex = {
1248 .hash_func_init_val = 0,
1253 * add/delete key with jhash2
1256 test_hash_add_delete_jhash2(void)
1259 struct rte_hash *handle;
1262 hash_params_ex.name = "hash_test_jhash2";
1263 hash_params_ex.key_len = 4;
1264 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1266 handle = rte_hash_create(&hash_params_ex);
1267 if (handle == NULL) {
1268 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1271 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1273 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1277 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1278 if (pos2 < 0 || pos1 != pos2) {
1279 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1286 rte_hash_free(handle);
1292 * add/delete (2) key with jhash2
1295 test_hash_add_delete_2_jhash2(void)
1298 struct rte_hash *handle;
1301 hash_params_ex.name = "hash_test_2_jhash2";
1302 hash_params_ex.key_len = 8;
1303 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1305 handle = rte_hash_create(&hash_params_ex);
1309 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1313 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1314 if (pos2 < 0 || pos1 != pos2)
1321 rte_hash_free(handle);
1327 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1329 const uint32_t *k = key;
1331 RTE_SET_USED(length);
1333 return rte_jhash_1word(k[0], initval);
1337 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1339 const uint32_t *k = key;
1341 RTE_SET_USED(length);
1343 return rte_jhash_2words(k[0], k[1], initval);
1347 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1349 const uint32_t *k = key;
1351 RTE_SET_USED(length);
1353 return rte_jhash_3words(k[0], k[1], k[2], initval);
1357 * add/delete key with jhash 1word
1360 test_hash_add_delete_jhash_1word(void)
1363 struct rte_hash *handle;
1366 hash_params_ex.name = "hash_test_jhash_1word";
1367 hash_params_ex.key_len = 4;
1368 hash_params_ex.hash_func = test_hash_jhash_1word;
1370 handle = rte_hash_create(&hash_params_ex);
1372 goto fail_jhash_1word;
1374 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1376 goto fail_jhash_1word;
1378 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1379 if (pos2 < 0 || pos1 != pos2)
1380 goto fail_jhash_1word;
1386 rte_hash_free(handle);
1392 * add/delete key with jhash 2word
1395 test_hash_add_delete_jhash_2word(void)
1398 struct rte_hash *handle;
1401 hash_params_ex.name = "hash_test_jhash_2word";
1402 hash_params_ex.key_len = 8;
1403 hash_params_ex.hash_func = test_hash_jhash_2word;
1405 handle = rte_hash_create(&hash_params_ex);
1407 goto fail_jhash_2word;
1409 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1411 goto fail_jhash_2word;
1413 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1414 if (pos2 < 0 || pos1 != pos2)
1415 goto fail_jhash_2word;
1421 rte_hash_free(handle);
1427 * add/delete key with jhash 3word
1430 test_hash_add_delete_jhash_3word(void)
1433 struct rte_hash *handle;
1436 hash_params_ex.name = "hash_test_jhash_3word";
1437 hash_params_ex.key_len = 12;
1438 hash_params_ex.hash_func = test_hash_jhash_3word;
1440 handle = rte_hash_create(&hash_params_ex);
1442 goto fail_jhash_3word;
1444 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1446 goto fail_jhash_3word;
1448 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1449 if (pos2 < 0 || pos1 != pos2)
1450 goto fail_jhash_3word;
1456 rte_hash_free(handle);
1462 * Do all unit and performance tests.
1467 if (test_add_delete() < 0)
1469 if (test_hash_add_delete_jhash2() < 0)
1471 if (test_hash_add_delete_2_jhash2() < 0)
1473 if (test_hash_add_delete_jhash_1word() < 0)
1475 if (test_hash_add_delete_jhash_2word() < 0)
1477 if (test_hash_add_delete_jhash_3word() < 0)
1479 if (test_hash_get_key_with_position() < 0)
1481 if (test_hash_find_existing() < 0)
1483 if (test_add_update_delete() < 0)
1485 if (test_five_keys() < 0)
1487 if (test_full_bucket() < 0)
1490 if (test_fbk_hash_find_existing() < 0)
1492 if (fbk_hash_unit_test() < 0)
1494 if (test_hash_creation_with_bad_parameters() < 0)
1496 if (test_hash_creation_with_good_parameters() < 0)
1498 if (test_average_table_utilization() < 0)
1500 if (test_hash_iteration() < 0)
1503 run_hash_func_tests();
1505 if (test_crc32_hash_alg_equiv() < 0)
1511 REGISTER_TEST_COMMAND(hash_autotest, test_hash);