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 /******************************************************************************/
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 << 20)
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");
221 /* Resetting to best available algorithm */
222 rte_hash_crc_set_alg(CRC32_SSE42_x64);
224 if (i == CRC32_ITERATIONS)
227 printf("Failed test data (hex, %zu bytes total):\n", data_len);
228 for (j = 0; j < data_len; j++)
229 printf("%02X%c", ((uint8_t *)data64)[j],
230 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
236 * Test a hash function.
238 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
241 static uint8_t key[RTE_HASH_KEY_LENGTH_MAX];
245 for (i = 0; i < key_len; i++)
246 key[i] = (uint8_t) rte_rand();
248 /* just to be on the safe side */
252 f(key, key_len, init_val);
256 * Test all hash functions.
258 static void run_hash_func_tests(void)
263 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
266 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
269 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
271 run_hash_func_test(hashtest_funcs[i],
272 hashtest_initvals[j],
273 hashtest_key_lens[k]);
280 * Basic sequence of operations for a single key:
286 static int test_add_delete(void)
288 struct rte_hash *handle;
289 /* test with standard add/lookup/delete functions */
290 int pos0, expectedPos0;
292 ut_params.name = "test1";
293 handle = rte_hash_create(&ut_params);
294 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
296 pos0 = rte_hash_add_key(handle, &keys[0]);
297 print_key_info("Add", &keys[0], pos0);
298 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
301 pos0 = rte_hash_lookup(handle, &keys[0]);
302 print_key_info("Lkp", &keys[0], pos0);
303 RETURN_IF_ERROR(pos0 != expectedPos0,
304 "failed to find key (pos0=%d)", pos0);
306 pos0 = rte_hash_del_key(handle, &keys[0]);
307 print_key_info("Del", &keys[0], pos0);
308 RETURN_IF_ERROR(pos0 != expectedPos0,
309 "failed to delete key (pos0=%d)", pos0);
311 pos0 = rte_hash_lookup(handle, &keys[0]);
312 print_key_info("Lkp", &keys[0], pos0);
313 RETURN_IF_ERROR(pos0 != -ENOENT,
314 "fail: found key after deleting! (pos0=%d)", pos0);
316 rte_hash_free(handle);
318 /* repeat test with precomputed hash functions */
319 hash_sig_t hash_value;
320 int pos1, expectedPos1;
322 handle = rte_hash_create(&ut_params);
323 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
325 hash_value = rte_hash_hash(handle, &keys[0]);
326 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
327 print_key_info("Add", &keys[0], pos1);
328 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
331 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
332 print_key_info("Lkp", &keys[0], pos1);
333 RETURN_IF_ERROR(pos1 != expectedPos1,
334 "failed to find key (pos1=%d)", pos1);
336 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
337 print_key_info("Del", &keys[0], pos1);
338 RETURN_IF_ERROR(pos1 != expectedPos1,
339 "failed to delete key (pos1=%d)", pos1);
341 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
342 print_key_info("Lkp", &keys[0], pos1);
343 RETURN_IF_ERROR(pos1 != -ENOENT,
344 "fail: found key after deleting! (pos1=%d)", pos1);
346 rte_hash_free(handle);
352 * Sequence of operations for a single key:
357 * - lookup: hit (updated data)
362 static int test_add_update_delete(void)
364 struct rte_hash *handle;
365 int pos0, expectedPos0;
367 ut_params.name = "test2";
368 handle = rte_hash_create(&ut_params);
369 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
371 pos0 = rte_hash_del_key(handle, &keys[0]);
372 print_key_info("Del", &keys[0], pos0);
373 RETURN_IF_ERROR(pos0 != -ENOENT,
374 "fail: found non-existent key (pos0=%d)", pos0);
376 pos0 = rte_hash_add_key(handle, &keys[0]);
377 print_key_info("Add", &keys[0], pos0);
378 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
381 pos0 = rte_hash_lookup(handle, &keys[0]);
382 print_key_info("Lkp", &keys[0], pos0);
383 RETURN_IF_ERROR(pos0 != expectedPos0,
384 "failed to find key (pos0=%d)", pos0);
386 pos0 = rte_hash_add_key(handle, &keys[0]);
387 print_key_info("Add", &keys[0], pos0);
388 RETURN_IF_ERROR(pos0 != expectedPos0,
389 "failed to re-add key (pos0=%d)", pos0);
391 pos0 = rte_hash_lookup(handle, &keys[0]);
392 print_key_info("Lkp", &keys[0], pos0);
393 RETURN_IF_ERROR(pos0 != expectedPos0,
394 "failed to find key (pos0=%d)", pos0);
396 pos0 = rte_hash_del_key(handle, &keys[0]);
397 print_key_info("Del", &keys[0], pos0);
398 RETURN_IF_ERROR(pos0 != expectedPos0,
399 "failed to delete key (pos0=%d)", pos0);
401 pos0 = rte_hash_del_key(handle, &keys[0]);
402 print_key_info("Del", &keys[0], pos0);
403 RETURN_IF_ERROR(pos0 != -ENOENT,
404 "fail: deleted already deleted key (pos0=%d)", pos0);
406 pos0 = rte_hash_lookup(handle, &keys[0]);
407 print_key_info("Lkp", &keys[0], pos0);
408 RETURN_IF_ERROR(pos0 != -ENOENT,
409 "fail: found key after deleting! (pos0=%d)", pos0);
411 rte_hash_free(handle);
416 * Sequence of operations for find existing hash table
419 * - find existing table: hit
420 * - find non-existing table: miss
423 static int test_hash_find_existing(void)
425 struct rte_hash *handle = NULL, *result = NULL;
427 /* Create hash table. */
428 ut_params.name = "hash_find_existing";
429 handle = rte_hash_create(&ut_params);
430 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
432 /* Try to find existing hash table */
433 result = rte_hash_find_existing("hash_find_existing");
434 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
436 /* Try to find non-existing hash table */
437 result = rte_hash_find_existing("hash_find_non_existing");
438 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
441 rte_hash_free(handle);
447 * Sequence of operations for 5 keys
450 * - add keys (update)
451 * - lookup keys: hit (updated data)
452 * - delete keys : hit
453 * - lookup keys: miss
455 static int test_five_keys(void)
457 struct rte_hash *handle;
458 const void *key_array[5] = {0};
464 ut_params.name = "test3";
465 handle = rte_hash_create(&ut_params);
466 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
469 for (i = 0; i < 5; i++) {
470 pos[i] = rte_hash_add_key(handle, &keys[i]);
471 print_key_info("Add", &keys[i], pos[i]);
472 RETURN_IF_ERROR(pos[i] < 0,
473 "failed to add key (pos[%u]=%d)", i, pos[i]);
474 expected_pos[i] = pos[i];
478 for(i = 0; i < 5; i++)
479 key_array[i] = &keys[i];
481 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
483 for(i = 0; i < 5; i++) {
484 print_key_info("Lkp", key_array[i], pos[i]);
485 RETURN_IF_ERROR(pos[i] != expected_pos[i],
486 "failed to find key (pos[%u]=%d)", i, pos[i]);
490 for (i = 0; i < 5; i++) {
491 pos[i] = rte_hash_add_key(handle, &keys[i]);
492 print_key_info("Add", &keys[i], pos[i]);
493 RETURN_IF_ERROR(pos[i] != expected_pos[i],
494 "failed to add key (pos[%u]=%d)", i, pos[i]);
498 for (i = 0; i < 5; i++) {
499 pos[i] = rte_hash_lookup(handle, &keys[i]);
500 print_key_info("Lkp", &keys[i], pos[i]);
501 RETURN_IF_ERROR(pos[i] != expected_pos[i],
502 "failed to find key (pos[%u]=%d)", i, pos[i]);
506 for (i = 0; i < 5; i++) {
507 pos[i] = rte_hash_del_key(handle, &keys[i]);
508 print_key_info("Del", &keys[i], pos[i]);
509 RETURN_IF_ERROR(pos[i] != expected_pos[i],
510 "failed to delete key (pos[%u]=%d)", i, pos[i]);
514 for (i = 0; i < 5; i++) {
515 pos[i] = rte_hash_lookup(handle, &keys[i]);
516 print_key_info("Lkp", &keys[i], pos[i]);
517 RETURN_IF_ERROR(pos[i] != -ENOENT,
518 "found non-existent key (pos[%u]=%d)", i, pos[i]);
522 ret = rte_hash_lookup_multi(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] != -ENOENT,
527 "found not-existent key (pos[%u]=%d)", i, pos[i]);
530 rte_hash_free(handle);
536 * Add keys to the same bucket until bucket full.
537 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
538 * first 4 successful, 5th successful, pushing existing item in bucket
539 * - lookup the 5 keys: 5 hits
540 * - add the 5 keys again: 5 OK
541 * - lookup the 5 keys: 5 hits (updated data)
542 * - delete the 5 keys: 5 OK
543 * - lookup the 5 keys: 5 misses
545 static int test_full_bucket(void)
547 struct rte_hash_parameters params_pseudo_hash = {
550 .key_len = sizeof(struct flow_key), /* 13 */
551 .hash_func = pseudo_hash,
552 .hash_func_init_val = 0,
555 struct rte_hash *handle;
560 handle = rte_hash_create(¶ms_pseudo_hash);
561 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
564 for (i = 0; i < 4; i++) {
565 pos[i] = rte_hash_add_key(handle, &keys[i]);
566 print_key_info("Add", &keys[i], pos[i]);
567 RETURN_IF_ERROR(pos[i] < 0,
568 "failed to add key (pos[%u]=%d)", i, pos[i]);
569 expected_pos[i] = pos[i];
572 * This should work and will push one of the items
573 * in the bucket because it is full
575 pos[4] = rte_hash_add_key(handle, &keys[4]);
576 print_key_info("Add", &keys[4], pos[4]);
577 RETURN_IF_ERROR(pos[4] < 0,
578 "failed to add key (pos[4]=%d)", pos[4]);
579 expected_pos[4] = pos[4];
582 for (i = 0; i < 5; i++) {
583 pos[i] = rte_hash_lookup(handle, &keys[i]);
584 print_key_info("Lkp", &keys[i], pos[i]);
585 RETURN_IF_ERROR(pos[i] != expected_pos[i],
586 "failed to find key (pos[%u]=%d)", i, pos[i]);
590 for (i = 0; i < 5; i++) {
591 pos[i] = rte_hash_add_key(handle, &keys[i]);
592 print_key_info("Add", &keys[i], pos[i]);
593 RETURN_IF_ERROR(pos[i] != expected_pos[i],
594 "failed to add key (pos[%u]=%d)", i, pos[i]);
598 for (i = 0; i < 5; i++) {
599 pos[i] = rte_hash_lookup(handle, &keys[i]);
600 print_key_info("Lkp", &keys[i], pos[i]);
601 RETURN_IF_ERROR(pos[i] != expected_pos[i],
602 "failed to find key (pos[%u]=%d)", i, pos[i]);
605 /* Delete 1 key, check other keys are still found */
606 pos[1] = rte_hash_del_key(handle, &keys[1]);
607 print_key_info("Del", &keys[1], pos[1]);
608 RETURN_IF_ERROR(pos[1] != expected_pos[1],
609 "failed to delete key (pos[1]=%d)", pos[1]);
610 pos[3] = rte_hash_lookup(handle, &keys[3]);
611 print_key_info("Lkp", &keys[3], pos[3]);
612 RETURN_IF_ERROR(pos[3] != expected_pos[3],
613 "failed lookup after deleting key from same bucket "
614 "(pos[3]=%d)", pos[3]);
616 /* Go back to previous state */
617 pos[1] = rte_hash_add_key(handle, &keys[1]);
618 print_key_info("Add", &keys[1], pos[1]);
619 expected_pos[1] = pos[1];
620 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
623 for (i = 0; i < 5; i++) {
624 pos[i] = rte_hash_del_key(handle, &keys[i]);
625 print_key_info("Del", &keys[i], pos[i]);
626 RETURN_IF_ERROR(pos[i] != expected_pos[i],
627 "failed to delete key (pos[%u]=%d)", i, pos[i]);
631 for (i = 0; i < 5; i++) {
632 pos[i] = rte_hash_lookup(handle, &keys[i]);
633 print_key_info("Lkp", &keys[i], pos[i]);
634 RETURN_IF_ERROR(pos[i] != -ENOENT,
635 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
638 rte_hash_free(handle);
640 /* Cover the NULL case. */
645 /******************************************************************************/
647 fbk_hash_unit_test(void)
649 struct rte_fbk_hash_params params = {
650 .name = "fbk_hash_test",
651 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
652 .entries_per_bucket = 4,
656 struct rte_fbk_hash_params invalid_params_1 = {
658 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
659 .entries_per_bucket = 4,
663 struct rte_fbk_hash_params invalid_params_2 = {
666 .entries_per_bucket = 3, /* Not power of 2 */
670 struct rte_fbk_hash_params invalid_params_3 = {
672 .entries = 0, /* Entries is 0 */
673 .entries_per_bucket = 4,
677 struct rte_fbk_hash_params invalid_params_4 = {
679 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
680 .entries_per_bucket = 0, /* Entries per bucket is 0 */
684 struct rte_fbk_hash_params invalid_params_5 = {
687 .entries_per_bucket = 8, /* Entries per bucket > entries */
691 struct rte_fbk_hash_params invalid_params_6 = {
693 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
694 .entries_per_bucket = 4,
698 struct rte_fbk_hash_params invalid_params_7 = {
700 .entries = RTE_FBK_HASH_ENTRIES_MAX,
701 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
705 struct rte_fbk_hash_params invalid_params_8 = {
707 .entries = RTE_FBK_HASH_ENTRIES_MAX,
708 .entries_per_bucket = 4,
709 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
712 /* try to create two hashes with identical names
713 * in this case, trying to create a second one will not
714 * fail but will simply return pointer to the existing
715 * hash with that name. sort of like a "find hash by name" :-)
717 struct rte_fbk_hash_params invalid_params_same_name_1 = {
718 .name = "same_name", /* hash with identical name */
720 .entries_per_bucket = 2,
724 /* trying to create this hash should return a pointer to an existing hash */
725 struct rte_fbk_hash_params invalid_params_same_name_2 = {
726 .name = "same_name", /* hash with identical name */
727 .entries = RTE_FBK_HASH_ENTRIES_MAX,
728 .entries_per_bucket = 4,
732 /* this is a sanity check for "same name" test
733 * creating this hash will check if we are actually able to create
734 * multiple hashes with different names (instead of having just one).
736 struct rte_fbk_hash_params different_name = {
737 .name = "different_name", /* different name */
738 .entries = RTE_FBK_HASH_ENTRIES_MAX,
739 .entries_per_bucket = 4,
743 struct rte_fbk_hash_params params_jhash = {
745 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
746 .entries_per_bucket = 4,
748 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
749 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
752 struct rte_fbk_hash_params params_nohash = {
753 .name = "valid nohash",
754 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
755 .entries_per_bucket = 4,
757 .hash_func = NULL, /* Tests for null hash_func */
758 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
761 struct rte_fbk_hash_table *handle, *tmp;
763 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
764 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
769 /* Try creating hashes with invalid parameters */
770 printf("# Testing hash creation with invalid parameters "
771 "- expect error msgs\n");
772 handle = rte_fbk_hash_create(&invalid_params_1);
773 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
775 handle = rte_fbk_hash_create(&invalid_params_2);
776 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
778 handle = rte_fbk_hash_create(&invalid_params_3);
779 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
781 handle = rte_fbk_hash_create(&invalid_params_4);
782 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
784 handle = rte_fbk_hash_create(&invalid_params_5);
785 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
787 handle = rte_fbk_hash_create(&invalid_params_6);
788 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
790 handle = rte_fbk_hash_create(&invalid_params_7);
791 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
793 handle = rte_fbk_hash_create(&invalid_params_8);
794 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
796 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
797 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
799 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
800 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
802 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
803 rte_fbk_hash_free(handle);
804 rte_fbk_hash_free(tmp);
808 /* we are not freeing tmp or handle here because we need a hash list
809 * to be not empty for the next test */
811 /* create a hash in non-empty list - good for coverage */
812 tmp = rte_fbk_hash_create(&different_name);
813 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
815 /* free both hashes */
816 rte_fbk_hash_free(handle);
817 rte_fbk_hash_free(tmp);
819 /* Create empty jhash hash. */
820 handle = rte_fbk_hash_create(¶ms_jhash);
821 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
824 rte_fbk_hash_free(handle);
826 /* Create empty jhash hash. */
827 handle = rte_fbk_hash_create(¶ms_nohash);
828 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
831 rte_fbk_hash_free(handle);
833 /* Create empty hash. */
834 handle = rte_fbk_hash_create(¶ms);
835 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
837 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
838 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
839 "load factor right after creation is not zero but it should be");
841 for (i = 0; i < 5; i++) {
842 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
843 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
846 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
847 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
848 "load factor now is not as expected");
849 /* Find value of added keys. */
850 for (i = 0; i < 5; i++) {
851 status = rte_fbk_hash_lookup(handle, keys[i]);
852 RETURN_IF_ERROR_FBK(status != vals[i],
853 "fbk hash lookup failed");
856 /* Change value of added keys. */
857 for (i = 0; i < 5; i++) {
858 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
859 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
862 /* Find new values. */
863 for (i = 0; i < 5; i++) {
864 status = rte_fbk_hash_lookup(handle, keys[i]);
865 RETURN_IF_ERROR_FBK(status != vals[4-i],
866 "fbk hash lookup failed");
869 /* Delete keys individually. */
870 for (i = 0; i < 5; i++) {
871 status = rte_fbk_hash_delete_key(handle, keys[i]);
872 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete 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 deletion is not zero but it should be");
878 /* Lookup should now fail. */
879 for (i = 0; i < 5; i++) {
880 status = rte_fbk_hash_lookup(handle, keys[i]);
881 RETURN_IF_ERROR_FBK(status == 0,
882 "fbk hash lookup should have failed");
885 /* Add keys again. */
886 for (i = 0; i < 5; i++) {
887 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
888 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
891 /* Make sure they were added. */
892 for (i = 0; i < 5; i++) {
893 status = rte_fbk_hash_lookup(handle, keys[i]);
894 RETURN_IF_ERROR_FBK(status != vals[i],
895 "fbk hash lookup failed");
898 /* Clear all entries. */
899 rte_fbk_hash_clear_all(handle);
901 /* Lookup should fail. */
902 for (i = 0; i < 5; i++) {
903 status = rte_fbk_hash_lookup(handle, keys[i]);
904 RETURN_IF_ERROR_FBK(status == 0,
905 "fbk hash lookup should have failed");
910 /* fill up the hash_table */
911 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
912 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
914 /* Find non-existent key in a full hashtable */
915 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
916 RETURN_IF_ERROR_FBK(status != -ENOENT,
917 "fbk hash lookup succeeded");
919 /* Delete non-existent key in a full hashtable */
920 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
921 RETURN_IF_ERROR_FBK(status != -ENOENT,
922 "fbk hash delete succeeded");
924 /* Delete one key from a full hashtable */
925 status = rte_fbk_hash_delete_key(handle, 1);
926 RETURN_IF_ERROR_FBK(status != 0,
927 "fbk hash delete failed");
929 /* Clear all entries. */
930 rte_fbk_hash_clear_all(handle);
933 rte_fbk_hash_free(handle);
935 /* Cover the NULL case. */
936 rte_fbk_hash_free(0);
942 * Sequence of operations for find existing fbk hash table
945 * - find existing table: hit
946 * - find non-existing table: miss
949 static int test_fbk_hash_find_existing(void)
951 struct rte_fbk_hash_params params = {
952 .name = "fbk_hash_find_existing",
953 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
954 .entries_per_bucket = 4,
957 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
959 /* Create hash table. */
960 handle = rte_fbk_hash_create(¶ms);
961 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
963 /* Try to find existing fbk hash table */
964 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
965 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
967 /* Try to find non-existing fbk hash table */
968 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
969 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
972 rte_fbk_hash_free(handle);
977 #define BUCKET_ENTRIES 4
979 * Do tests for hash creation with bad parameters.
981 static int test_hash_creation_with_bad_parameters(void)
983 struct rte_hash *handle;
984 struct rte_hash_parameters params;
986 handle = rte_hash_create(NULL);
987 if (handle != NULL) {
988 rte_hash_free(handle);
989 printf("Impossible creating hash sucessfully without any parameter\n");
993 memcpy(¶ms, &ut_params, sizeof(params));
994 params.name = "creation_with_bad_parameters_0";
995 params.entries = RTE_HASH_ENTRIES_MAX + 1;
996 handle = rte_hash_create(¶ms);
997 if (handle != NULL) {
998 rte_hash_free(handle);
999 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1003 memcpy(¶ms, &ut_params, sizeof(params));
1004 params.name = "creation_with_bad_parameters_2";
1005 params.entries = BUCKET_ENTRIES - 1;
1006 handle = rte_hash_create(¶ms);
1007 if (handle != NULL) {
1008 rte_hash_free(handle);
1009 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1013 memcpy(¶ms, &ut_params, sizeof(params));
1014 params.name = "creation_with_bad_parameters_3";
1016 handle = rte_hash_create(¶ms);
1017 if (handle != NULL) {
1018 rte_hash_free(handle);
1019 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1023 memcpy(¶ms, &ut_params, sizeof(params));
1024 params.name = "creation_with_bad_parameters_4";
1025 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1026 handle = rte_hash_create(¶ms);
1027 if (handle != NULL) {
1028 rte_hash_free(handle);
1029 printf("Impossible creating hash sucessfully with invalid socket\n");
1033 rte_hash_free(handle);
1034 printf("# Test successful. No more errors expected\n");
1040 * Do tests for hash creation with parameters that look incorrect
1041 * but are actually valid.
1044 test_hash_creation_with_good_parameters(void)
1046 struct rte_hash *handle, *tmp;
1047 struct rte_hash_parameters params;
1049 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1050 memcpy(¶ms, &ut_params, sizeof(params));
1051 params.name = "same_name";
1052 params.hash_func = NULL;
1053 handle = rte_hash_create(¶ms);
1054 if (handle == NULL) {
1055 printf("Creating hash with null hash_func failed\n");
1059 /* this test is trying to create a hash with the same name as previous one.
1060 * this should return a pointer to the hash we previously created.
1061 * the previous hash isn't freed exactly for the purpose of it being in
1064 memcpy(¶ms, &ut_params, sizeof(params));
1065 params.name = "same_name";
1066 tmp = rte_hash_create(¶ms);
1068 /* check if the returned handle is actually equal to the previous hash */
1069 if (handle != tmp) {
1070 rte_hash_free(handle);
1072 printf("Creating hash with existing name was successful\n");
1076 /* try creating hash when there already are hashes in the list.
1077 * the previous hash is not freed to have a non-empty hash list.
1078 * the other hash that's in the list is still pointed to by "handle" var.
1080 memcpy(¶ms, &ut_params, sizeof(params));
1081 params.name = "different_name";
1082 tmp = rte_hash_create(¶ms);
1084 rte_hash_free(handle);
1085 printf("Creating hash with valid parameters failed\n");
1090 rte_hash_free(handle);
1095 #define ITERATIONS 50
1097 * Test to see the average table utilization (entries added/max entries)
1098 * before hitting a random entry that cannot be added
1100 static int test_average_table_utilization(void)
1102 struct rte_hash *handle;
1103 uint8_t simple_key[RTE_HASH_KEY_LENGTH_MAX];
1105 unsigned added_keys, average_keys_added = 0;
1108 printf("\n# Running test to determine average utilization"
1109 "\n before adding elements begins to fail\n");
1110 printf("Measuring performance, please wait");
1112 ut_params.entries = 1 << 20;
1113 ut_params.name = "test_average_utilization";
1114 ut_params.hash_func = rte_jhash;
1115 handle = rte_hash_create(&ut_params);
1116 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1118 for (j = 0; j < ITERATIONS; j++) {
1120 /* Add random entries until key cannot be added */
1121 for (added_keys = 0; ret >= 0; added_keys++) {
1122 for (i = 0; i < ut_params.key_len; i++)
1123 simple_key[i] = rte_rand() % 255;
1124 ret = rte_hash_add_key(handle, simple_key);
1126 if (ret != -ENOSPC) {
1127 printf("Unexpected error when adding keys\n");
1128 rte_hash_free(handle);
1132 average_keys_added += added_keys;
1134 /* Reset the table */
1135 rte_hash_reset(handle);
1137 /* Print a dot to show progress on operations */
1142 average_keys_added /= ITERATIONS;
1144 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1145 ((double) average_keys_added / ut_params.entries * 100),
1146 average_keys_added, ut_params.entries);
1147 rte_hash_free(handle);
1152 #define NUM_ENTRIES 1024
1153 static int test_hash_iteration(void)
1155 struct rte_hash *handle;
1157 uint8_t keys[NUM_ENTRIES][RTE_HASH_KEY_LENGTH_MAX];
1158 const void *next_key;
1160 void *data[NUM_ENTRIES];
1161 unsigned added_keys;
1165 ut_params.entries = NUM_ENTRIES;
1166 ut_params.name = "test_hash_iteration";
1167 ut_params.hash_func = rte_jhash;
1168 ut_params.key_len = 16;
1169 handle = rte_hash_create(&ut_params);
1170 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1172 /* Add random entries until key cannot be added */
1173 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1174 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1175 for (i = 0; i < ut_params.key_len; i++)
1176 keys[added_keys][i] = rte_rand() % 255;
1177 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1182 /* Iterate through the hash table */
1183 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1184 /* Search for the key in the list of keys added */
1185 for (i = 0; i < NUM_ENTRIES; i++) {
1186 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1187 if (next_data != data[i]) {
1188 printf("Data found in the hash table is"
1189 "not the data added with the key\n");
1196 if (i == NUM_ENTRIES) {
1197 printf("Key found in the hash table was not added\n");
1202 /* Check if all keys have been iterated */
1203 if (added_keys != 0) {
1204 printf("There were still %u keys to iterate\n", added_keys);
1208 rte_hash_free(handle);
1212 rte_hash_free(handle);
1216 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1217 0x04, 0x05, 0x06, 0x07,
1218 0x08, 0x09, 0x0a, 0x0b,
1219 0x0c, 0x0d, 0x0e, 0x0f};
1220 static struct rte_hash_parameters hash_params_ex = {
1225 .hash_func_init_val = 0,
1230 * add/delete key with jhash2
1233 test_hash_add_delete_jhash2(void)
1236 struct rte_hash *handle;
1239 hash_params_ex.name = "hash_test_jhash2";
1240 hash_params_ex.key_len = 4;
1241 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1243 handle = rte_hash_create(&hash_params_ex);
1244 if (handle == NULL) {
1245 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1248 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1250 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1254 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1255 if (pos2 < 0 || pos1 != pos2) {
1256 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1263 rte_hash_free(handle);
1269 * add/delete (2) key with jhash2
1272 test_hash_add_delete_2_jhash2(void)
1275 struct rte_hash *handle;
1278 hash_params_ex.name = "hash_test_2_jhash2";
1279 hash_params_ex.key_len = 8;
1280 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1282 handle = rte_hash_create(&hash_params_ex);
1286 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1290 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1291 if (pos2 < 0 || pos1 != pos2)
1298 rte_hash_free(handle);
1304 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1306 const uint32_t *k = key;
1308 RTE_SET_USED(length);
1310 return rte_jhash_1word(k[0], initval);
1314 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1316 const uint32_t *k = key;
1318 RTE_SET_USED(length);
1320 return rte_jhash_2words(k[0], k[1], initval);
1324 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1326 const uint32_t *k = key;
1328 RTE_SET_USED(length);
1330 return rte_jhash_3words(k[0], k[1], k[2], initval);
1334 * add/delete key with jhash 1word
1337 test_hash_add_delete_jhash_1word(void)
1340 struct rte_hash *handle;
1343 hash_params_ex.name = "hash_test_jhash_1word";
1344 hash_params_ex.key_len = 4;
1345 hash_params_ex.hash_func = test_hash_jhash_1word;
1347 handle = rte_hash_create(&hash_params_ex);
1349 goto fail_jhash_1word;
1351 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1353 goto fail_jhash_1word;
1355 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1356 if (pos2 < 0 || pos1 != pos2)
1357 goto fail_jhash_1word;
1363 rte_hash_free(handle);
1369 * add/delete key with jhash 2word
1372 test_hash_add_delete_jhash_2word(void)
1375 struct rte_hash *handle;
1378 hash_params_ex.name = "hash_test_jhash_2word";
1379 hash_params_ex.key_len = 8;
1380 hash_params_ex.hash_func = test_hash_jhash_2word;
1382 handle = rte_hash_create(&hash_params_ex);
1384 goto fail_jhash_2word;
1386 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1388 goto fail_jhash_2word;
1390 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1391 if (pos2 < 0 || pos1 != pos2)
1392 goto fail_jhash_2word;
1398 rte_hash_free(handle);
1404 * add/delete key with jhash 3word
1407 test_hash_add_delete_jhash_3word(void)
1410 struct rte_hash *handle;
1413 hash_params_ex.name = "hash_test_jhash_3word";
1414 hash_params_ex.key_len = 12;
1415 hash_params_ex.hash_func = test_hash_jhash_3word;
1417 handle = rte_hash_create(&hash_params_ex);
1419 goto fail_jhash_3word;
1421 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1423 goto fail_jhash_3word;
1425 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1426 if (pos2 < 0 || pos1 != pos2)
1427 goto fail_jhash_3word;
1433 rte_hash_free(handle);
1439 * Do all unit and performance tests.
1444 if (test_add_delete() < 0)
1446 if (test_hash_add_delete_jhash2() < 0)
1448 if (test_hash_add_delete_2_jhash2() < 0)
1450 if (test_hash_add_delete_jhash_1word() < 0)
1452 if (test_hash_add_delete_jhash_2word() < 0)
1454 if (test_hash_add_delete_jhash_3word() < 0)
1456 if (test_hash_find_existing() < 0)
1458 if (test_add_update_delete() < 0)
1460 if (test_five_keys() < 0)
1462 if (test_full_bucket() < 0)
1465 if (test_fbk_hash_find_existing() < 0)
1467 if (fbk_hash_unit_test() < 0)
1469 if (test_hash_creation_with_bad_parameters() < 0)
1471 if (test_hash_creation_with_good_parameters() < 0)
1473 if (test_average_table_utilization() < 0)
1475 if (test_hash_iteration() < 0)
1478 run_hash_func_tests();
1480 if (test_crc32_hash_alg_equiv() < 0)
1486 static struct test_command hash_cmd = {
1487 .command = "hash_autotest",
1488 .callback = test_hash,
1490 REGISTER_TEST_COMMAND(hash_cmd);