4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_malloc.h>
44 #include <rte_cycles.h>
45 #include <rte_random.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
50 #include <rte_string_fns.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57 #include <rte_hash_crc.h>
59 /*******************************************************************************
60 * Hash function performance test configuration section. Each performance test
61 * will be performed HASHTEST_ITERATIONS times.
63 * The five arrays below control what tests are performed. Every combination
64 * from the array entries is tested.
66 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
67 static uint32_t hashtest_initvals[] = {0};
68 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
69 #define MAX_KEYSIZE 64
70 /******************************************************************************/
71 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
74 * Check condition and return an error if true. Assumes that "handle" is the
75 * name of the hash structure pointer to be freed.
77 #define RETURN_IF_ERROR(cond, str, ...) do { \
79 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
80 if (handle) rte_hash_free(handle); \
85 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
87 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
88 if (handle) rte_fbk_hash_free(handle); \
93 /* 5-tuple key type */
100 } __attribute__((packed));
103 * Hash function that always returns the same value, to easily test what
104 * happens when a bucket is full.
106 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
107 __attribute__((unused)) uint32_t key_len,
108 __attribute__((unused)) uint32_t init_val)
114 * Print out result of unit test hash operation.
116 #if defined(UNIT_TEST_HASH_VERBOSE)
117 static void print_key_info(const char *msg, const struct flow_key *key,
120 uint8_t *p = (uint8_t *)key;
123 printf("%s key:0x", msg);
124 for (i = 0; i < sizeof(struct flow_key); i++) {
125 printf("%02X", p[i]);
127 printf(" @ pos %d\n", pos);
130 static void print_key_info(__attribute__((unused)) const char *msg,
131 __attribute__((unused)) const struct flow_key *key,
132 __attribute__((unused)) int32_t pos)
137 /* Keys used by unit test functions */
138 static struct flow_key keys[5] = { {
139 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
140 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
145 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
146 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
151 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
152 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
157 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
158 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
163 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
164 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
170 /* Parameters used for hash table in unit test functions. Name set later. */
171 static struct rte_hash_parameters ut_params = {
173 .key_len = sizeof(struct flow_key), /* 13 */
174 .hash_func = rte_jhash,
175 .hash_func_init_val = 0,
179 #define CRC32_ITERATIONS (1U << 10)
180 #define CRC32_DWORDS (1U << 6)
182 * Test if all CRC32 implementations yield the same hash value
185 test_crc32_hash_alg_equiv(void)
189 uint64_t data64[CRC32_DWORDS];
193 printf("\n# CRC32 implementations equivalence test\n");
194 for (i = 0; i < CRC32_ITERATIONS; i++) {
195 /* Randomizing data_len of data set */
196 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
197 init_val = (uint32_t) rte_rand();
199 /* Fill the data set */
200 for (j = 0; j < CRC32_DWORDS; j++)
201 data64[j] = rte_rand();
203 /* Calculate software CRC32 */
204 rte_hash_crc_set_alg(CRC32_SW);
205 hash_val = rte_hash_crc(data64, data_len, init_val);
207 /* Check against 4-byte-operand sse4.2 CRC32 if available */
208 rte_hash_crc_set_alg(CRC32_SSE42);
209 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
210 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
214 /* Check against 8-byte-operand sse4.2 CRC32 if available */
215 rte_hash_crc_set_alg(CRC32_SSE42_x64);
216 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
217 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
221 /* Check against 8-byte-operand ARM64 CRC32 if available */
222 rte_hash_crc_set_alg(CRC32_ARM64);
223 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
224 printf("Failed checking CRC32_SW against CRC32_ARM64\n");
229 /* Resetting to best available algorithm */
230 rte_hash_crc_set_alg(CRC32_SSE42_x64);
232 if (i == CRC32_ITERATIONS)
235 printf("Failed test data (hex, %zu bytes total):\n", data_len);
236 for (j = 0; j < data_len; j++)
237 printf("%02X%c", ((uint8_t *)data64)[j],
238 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
244 * Test a hash function.
246 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
249 static uint8_t key[MAX_KEYSIZE];
253 for (i = 0; i < key_len; i++)
254 key[i] = (uint8_t) rte_rand();
256 /* just to be on the safe side */
260 f(key, key_len, init_val);
264 * Test all hash functions.
266 static void run_hash_func_tests(void)
271 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
274 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
277 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
279 run_hash_func_test(hashtest_funcs[i],
280 hashtest_initvals[j],
281 hashtest_key_lens[k]);
288 * Basic sequence of operations for a single key:
294 static int test_add_delete(void)
296 struct rte_hash *handle;
297 /* test with standard add/lookup/delete functions */
298 int pos0, expectedPos0;
300 ut_params.name = "test1";
301 handle = rte_hash_create(&ut_params);
302 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
304 pos0 = rte_hash_add_key(handle, &keys[0]);
305 print_key_info("Add", &keys[0], pos0);
306 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
309 pos0 = rte_hash_lookup(handle, &keys[0]);
310 print_key_info("Lkp", &keys[0], pos0);
311 RETURN_IF_ERROR(pos0 != expectedPos0,
312 "failed to find key (pos0=%d)", pos0);
314 pos0 = rte_hash_del_key(handle, &keys[0]);
315 print_key_info("Del", &keys[0], pos0);
316 RETURN_IF_ERROR(pos0 != expectedPos0,
317 "failed to delete key (pos0=%d)", pos0);
319 pos0 = rte_hash_lookup(handle, &keys[0]);
320 print_key_info("Lkp", &keys[0], pos0);
321 RETURN_IF_ERROR(pos0 != -ENOENT,
322 "fail: found key after deleting! (pos0=%d)", pos0);
324 rte_hash_free(handle);
326 /* repeat test with precomputed hash functions */
327 hash_sig_t hash_value;
328 int pos1, expectedPos1;
330 handle = rte_hash_create(&ut_params);
331 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
333 hash_value = rte_hash_hash(handle, &keys[0]);
334 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
335 print_key_info("Add", &keys[0], pos1);
336 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
339 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
340 print_key_info("Lkp", &keys[0], pos1);
341 RETURN_IF_ERROR(pos1 != expectedPos1,
342 "failed to find key (pos1=%d)", pos1);
344 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
345 print_key_info("Del", &keys[0], pos1);
346 RETURN_IF_ERROR(pos1 != expectedPos1,
347 "failed to delete key (pos1=%d)", pos1);
349 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
350 print_key_info("Lkp", &keys[0], pos1);
351 RETURN_IF_ERROR(pos1 != -ENOENT,
352 "fail: found key after deleting! (pos1=%d)", pos1);
354 rte_hash_free(handle);
360 * Sequence of operations for a single key:
365 * - lookup: hit (updated data)
370 static int test_add_update_delete(void)
372 struct rte_hash *handle;
373 int pos0, expectedPos0;
375 ut_params.name = "test2";
376 handle = rte_hash_create(&ut_params);
377 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
379 pos0 = rte_hash_del_key(handle, &keys[0]);
380 print_key_info("Del", &keys[0], pos0);
381 RETURN_IF_ERROR(pos0 != -ENOENT,
382 "fail: found non-existent key (pos0=%d)", pos0);
384 pos0 = rte_hash_add_key(handle, &keys[0]);
385 print_key_info("Add", &keys[0], pos0);
386 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
389 pos0 = rte_hash_lookup(handle, &keys[0]);
390 print_key_info("Lkp", &keys[0], pos0);
391 RETURN_IF_ERROR(pos0 != expectedPos0,
392 "failed to find key (pos0=%d)", pos0);
394 pos0 = rte_hash_add_key(handle, &keys[0]);
395 print_key_info("Add", &keys[0], pos0);
396 RETURN_IF_ERROR(pos0 != expectedPos0,
397 "failed to re-add key (pos0=%d)", pos0);
399 pos0 = rte_hash_lookup(handle, &keys[0]);
400 print_key_info("Lkp", &keys[0], pos0);
401 RETURN_IF_ERROR(pos0 != expectedPos0,
402 "failed to find key (pos0=%d)", pos0);
404 pos0 = rte_hash_del_key(handle, &keys[0]);
405 print_key_info("Del", &keys[0], pos0);
406 RETURN_IF_ERROR(pos0 != expectedPos0,
407 "failed to delete key (pos0=%d)", pos0);
409 pos0 = rte_hash_del_key(handle, &keys[0]);
410 print_key_info("Del", &keys[0], pos0);
411 RETURN_IF_ERROR(pos0 != -ENOENT,
412 "fail: deleted already deleted key (pos0=%d)", pos0);
414 pos0 = rte_hash_lookup(handle, &keys[0]);
415 print_key_info("Lkp", &keys[0], pos0);
416 RETURN_IF_ERROR(pos0 != -ENOENT,
417 "fail: found key after deleting! (pos0=%d)", pos0);
419 rte_hash_free(handle);
424 * Sequence of operations for retrieving a key with its position
428 * - get the key with its position: hit
430 * - try to get the deleted key: miss
433 static int test_hash_get_key_with_position(void)
435 struct rte_hash *handle = NULL;
436 int pos, expectedPos, result;
439 ut_params.name = "hash_get_key_w_pos";
440 handle = rte_hash_create(&ut_params);
441 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
443 pos = rte_hash_add_key(handle, &keys[0]);
444 print_key_info("Add", &keys[0], pos);
445 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos);
448 result = rte_hash_get_key_with_position(handle, pos, &key);
449 RETURN_IF_ERROR(result != 0, "error retrieving a key");
451 pos = rte_hash_del_key(handle, &keys[0]);
452 print_key_info("Del", &keys[0], pos);
453 RETURN_IF_ERROR(pos != expectedPos,
454 "failed to delete key (pos0=%d)", pos);
456 result = rte_hash_get_key_with_position(handle, pos, &key);
457 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved");
459 rte_hash_free(handle);
464 * Sequence of operations for find existing hash table
467 * - find existing table: hit
468 * - find non-existing table: miss
471 static int test_hash_find_existing(void)
473 struct rte_hash *handle = NULL, *result = NULL;
475 /* Create hash table. */
476 ut_params.name = "hash_find_existing";
477 handle = rte_hash_create(&ut_params);
478 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
480 /* Try to find existing hash table */
481 result = rte_hash_find_existing("hash_find_existing");
482 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
484 /* Try to find non-existing hash table */
485 result = rte_hash_find_existing("hash_find_non_existing");
486 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
489 rte_hash_free(handle);
495 * Sequence of operations for 5 keys
498 * - add keys (update)
499 * - lookup keys: hit (updated data)
500 * - delete keys : hit
501 * - lookup keys: miss
503 static int test_five_keys(void)
505 struct rte_hash *handle;
506 const void *key_array[5] = {0};
512 ut_params.name = "test3";
513 handle = rte_hash_create(&ut_params);
514 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
517 for (i = 0; i < 5; i++) {
518 pos[i] = rte_hash_add_key(handle, &keys[i]);
519 print_key_info("Add", &keys[i], pos[i]);
520 RETURN_IF_ERROR(pos[i] < 0,
521 "failed to add key (pos[%u]=%d)", i, pos[i]);
522 expected_pos[i] = pos[i];
526 for(i = 0; i < 5; i++)
527 key_array[i] = &keys[i];
529 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
531 for(i = 0; i < 5; i++) {
532 print_key_info("Lkp", key_array[i], pos[i]);
533 RETURN_IF_ERROR(pos[i] != expected_pos[i],
534 "failed to find key (pos[%u]=%d)", i, pos[i]);
538 for (i = 0; i < 5; i++) {
539 pos[i] = rte_hash_add_key(handle, &keys[i]);
540 print_key_info("Add", &keys[i], pos[i]);
541 RETURN_IF_ERROR(pos[i] != expected_pos[i],
542 "failed to add key (pos[%u]=%d)", i, pos[i]);
546 for (i = 0; i < 5; i++) {
547 pos[i] = rte_hash_lookup(handle, &keys[i]);
548 print_key_info("Lkp", &keys[i], pos[i]);
549 RETURN_IF_ERROR(pos[i] != expected_pos[i],
550 "failed to find key (pos[%u]=%d)", i, pos[i]);
554 for (i = 0; i < 5; i++) {
555 pos[i] = rte_hash_del_key(handle, &keys[i]);
556 print_key_info("Del", &keys[i], pos[i]);
557 RETURN_IF_ERROR(pos[i] != expected_pos[i],
558 "failed to delete key (pos[%u]=%d)", i, pos[i]);
562 for (i = 0; i < 5; i++) {
563 pos[i] = rte_hash_lookup(handle, &keys[i]);
564 print_key_info("Lkp", &keys[i], pos[i]);
565 RETURN_IF_ERROR(pos[i] != -ENOENT,
566 "found non-existent key (pos[%u]=%d)", i, pos[i]);
570 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
572 for (i = 0; i < 5; i++) {
573 print_key_info("Lkp", key_array[i], pos[i]);
574 RETURN_IF_ERROR(pos[i] != -ENOENT,
575 "found not-existent key (pos[%u]=%d)", i, pos[i]);
578 rte_hash_free(handle);
584 * Add keys to the same bucket until bucket full.
585 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
586 * first 4 successful, 5th successful, pushing existing item in bucket
587 * - lookup the 5 keys: 5 hits
588 * - add the 5 keys again: 5 OK
589 * - lookup the 5 keys: 5 hits (updated data)
590 * - delete the 5 keys: 5 OK
591 * - lookup the 5 keys: 5 misses
593 static int test_full_bucket(void)
595 struct rte_hash_parameters params_pseudo_hash = {
598 .key_len = sizeof(struct flow_key), /* 13 */
599 .hash_func = pseudo_hash,
600 .hash_func_init_val = 0,
603 struct rte_hash *handle;
608 handle = rte_hash_create(¶ms_pseudo_hash);
609 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
612 for (i = 0; i < 4; i++) {
613 pos[i] = rte_hash_add_key(handle, &keys[i]);
614 print_key_info("Add", &keys[i], pos[i]);
615 RETURN_IF_ERROR(pos[i] < 0,
616 "failed to add key (pos[%u]=%d)", i, pos[i]);
617 expected_pos[i] = pos[i];
620 * This should work and will push one of the items
621 * in the bucket because it is full
623 pos[4] = rte_hash_add_key(handle, &keys[4]);
624 print_key_info("Add", &keys[4], pos[4]);
625 RETURN_IF_ERROR(pos[4] < 0,
626 "failed to add key (pos[4]=%d)", pos[4]);
627 expected_pos[4] = pos[4];
630 for (i = 0; i < 5; i++) {
631 pos[i] = rte_hash_lookup(handle, &keys[i]);
632 print_key_info("Lkp", &keys[i], pos[i]);
633 RETURN_IF_ERROR(pos[i] != expected_pos[i],
634 "failed to find key (pos[%u]=%d)", i, pos[i]);
638 for (i = 0; i < 5; i++) {
639 pos[i] = rte_hash_add_key(handle, &keys[i]);
640 print_key_info("Add", &keys[i], pos[i]);
641 RETURN_IF_ERROR(pos[i] != expected_pos[i],
642 "failed to add key (pos[%u]=%d)", i, pos[i]);
646 for (i = 0; i < 5; i++) {
647 pos[i] = rte_hash_lookup(handle, &keys[i]);
648 print_key_info("Lkp", &keys[i], pos[i]);
649 RETURN_IF_ERROR(pos[i] != expected_pos[i],
650 "failed to find key (pos[%u]=%d)", i, pos[i]);
653 /* Delete 1 key, check other keys are still found */
654 pos[1] = rte_hash_del_key(handle, &keys[1]);
655 print_key_info("Del", &keys[1], pos[1]);
656 RETURN_IF_ERROR(pos[1] != expected_pos[1],
657 "failed to delete key (pos[1]=%d)", pos[1]);
658 pos[3] = rte_hash_lookup(handle, &keys[3]);
659 print_key_info("Lkp", &keys[3], pos[3]);
660 RETURN_IF_ERROR(pos[3] != expected_pos[3],
661 "failed lookup after deleting key from same bucket "
662 "(pos[3]=%d)", pos[3]);
664 /* Go back to previous state */
665 pos[1] = rte_hash_add_key(handle, &keys[1]);
666 print_key_info("Add", &keys[1], pos[1]);
667 expected_pos[1] = pos[1];
668 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
671 for (i = 0; i < 5; i++) {
672 pos[i] = rte_hash_del_key(handle, &keys[i]);
673 print_key_info("Del", &keys[i], pos[i]);
674 RETURN_IF_ERROR(pos[i] != expected_pos[i],
675 "failed to delete key (pos[%u]=%d)", i, pos[i]);
679 for (i = 0; i < 5; i++) {
680 pos[i] = rte_hash_lookup(handle, &keys[i]);
681 print_key_info("Lkp", &keys[i], pos[i]);
682 RETURN_IF_ERROR(pos[i] != -ENOENT,
683 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
686 rte_hash_free(handle);
688 /* Cover the NULL case. */
693 /******************************************************************************/
695 fbk_hash_unit_test(void)
697 struct rte_fbk_hash_params params = {
698 .name = "fbk_hash_test",
699 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
700 .entries_per_bucket = 4,
704 struct rte_fbk_hash_params invalid_params_1 = {
706 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
707 .entries_per_bucket = 4,
711 struct rte_fbk_hash_params invalid_params_2 = {
714 .entries_per_bucket = 3, /* Not power of 2 */
718 struct rte_fbk_hash_params invalid_params_3 = {
720 .entries = 0, /* Entries is 0 */
721 .entries_per_bucket = 4,
725 struct rte_fbk_hash_params invalid_params_4 = {
727 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
728 .entries_per_bucket = 0, /* Entries per bucket is 0 */
732 struct rte_fbk_hash_params invalid_params_5 = {
735 .entries_per_bucket = 8, /* Entries per bucket > entries */
739 struct rte_fbk_hash_params invalid_params_6 = {
741 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
742 .entries_per_bucket = 4,
746 struct rte_fbk_hash_params invalid_params_7 = {
748 .entries = RTE_FBK_HASH_ENTRIES_MAX,
749 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
753 struct rte_fbk_hash_params invalid_params_8 = {
755 .entries = RTE_FBK_HASH_ENTRIES_MAX,
756 .entries_per_bucket = 4,
757 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
760 /* try to create two hashes with identical names
761 * in this case, trying to create a second one will not
762 * fail but will simply return pointer to the existing
763 * hash with that name. sort of like a "find hash by name" :-)
765 struct rte_fbk_hash_params invalid_params_same_name_1 = {
766 .name = "same_name", /* hash with identical name */
768 .entries_per_bucket = 2,
772 /* trying to create this hash should return a pointer to an existing hash */
773 struct rte_fbk_hash_params invalid_params_same_name_2 = {
774 .name = "same_name", /* hash with identical name */
775 .entries = RTE_FBK_HASH_ENTRIES_MAX,
776 .entries_per_bucket = 4,
780 /* this is a sanity check for "same name" test
781 * creating this hash will check if we are actually able to create
782 * multiple hashes with different names (instead of having just one).
784 struct rte_fbk_hash_params different_name = {
785 .name = "different_name", /* different name */
786 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
787 .entries_per_bucket = 4,
791 struct rte_fbk_hash_params params_jhash = {
793 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
794 .entries_per_bucket = 4,
796 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
797 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
800 struct rte_fbk_hash_params params_nohash = {
801 .name = "valid nohash",
802 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
803 .entries_per_bucket = 4,
805 .hash_func = NULL, /* Tests for null hash_func */
806 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
809 struct rte_fbk_hash_table *handle, *tmp;
811 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
812 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
817 /* Try creating hashes with invalid parameters */
818 printf("# Testing hash creation with invalid parameters "
819 "- expect error msgs\n");
820 handle = rte_fbk_hash_create(&invalid_params_1);
821 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
823 handle = rte_fbk_hash_create(&invalid_params_2);
824 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
826 handle = rte_fbk_hash_create(&invalid_params_3);
827 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
829 handle = rte_fbk_hash_create(&invalid_params_4);
830 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
832 handle = rte_fbk_hash_create(&invalid_params_5);
833 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
835 handle = rte_fbk_hash_create(&invalid_params_6);
836 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
838 handle = rte_fbk_hash_create(&invalid_params_7);
839 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
841 handle = rte_fbk_hash_create(&invalid_params_8);
842 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
844 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
845 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
847 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
849 rte_fbk_hash_free(tmp);
850 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
852 /* we are not freeing handle here because we need a hash list
853 * to be not empty for the next test */
855 /* create a hash in non-empty list - good for coverage */
856 tmp = rte_fbk_hash_create(&different_name);
857 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
859 /* free both hashes */
860 rte_fbk_hash_free(handle);
861 rte_fbk_hash_free(tmp);
863 /* Create empty jhash hash. */
864 handle = rte_fbk_hash_create(¶ms_jhash);
865 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
868 rte_fbk_hash_free(handle);
870 /* Create empty jhash hash. */
871 handle = rte_fbk_hash_create(¶ms_nohash);
872 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
875 rte_fbk_hash_free(handle);
877 /* Create empty hash. */
878 handle = rte_fbk_hash_create(¶ms);
879 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
881 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
882 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
883 "load factor right after creation is not zero but it should be");
885 for (i = 0; i < 5; i++) {
886 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
887 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
890 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
891 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
892 "load factor now is not as expected");
893 /* Find value of added keys. */
894 for (i = 0; i < 5; i++) {
895 status = rte_fbk_hash_lookup(handle, keys[i]);
896 RETURN_IF_ERROR_FBK(status != vals[i],
897 "fbk hash lookup failed");
900 /* Change value of added keys. */
901 for (i = 0; i < 5; i++) {
902 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
903 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
906 /* Find new values. */
907 for (i = 0; i < 5; i++) {
908 status = rte_fbk_hash_lookup(handle, keys[i]);
909 RETURN_IF_ERROR_FBK(status != vals[4-i],
910 "fbk hash lookup failed");
913 /* Delete keys individually. */
914 for (i = 0; i < 5; i++) {
915 status = rte_fbk_hash_delete_key(handle, keys[i]);
916 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
919 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
920 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
921 "load factor right after deletion is not zero but it should be");
922 /* Lookup should now fail. */
923 for (i = 0; i < 5; i++) {
924 status = rte_fbk_hash_lookup(handle, keys[i]);
925 RETURN_IF_ERROR_FBK(status == 0,
926 "fbk hash lookup should have failed");
929 /* Add keys again. */
930 for (i = 0; i < 5; i++) {
931 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
932 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
935 /* Make sure they were added. */
936 for (i = 0; i < 5; i++) {
937 status = rte_fbk_hash_lookup(handle, keys[i]);
938 RETURN_IF_ERROR_FBK(status != vals[i],
939 "fbk hash lookup failed");
942 /* Clear all entries. */
943 rte_fbk_hash_clear_all(handle);
945 /* Lookup should fail. */
946 for (i = 0; i < 5; i++) {
947 status = rte_fbk_hash_lookup(handle, keys[i]);
948 RETURN_IF_ERROR_FBK(status == 0,
949 "fbk hash lookup should have failed");
954 /* fill up the hash_table */
955 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
956 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
958 /* Find non-existent key in a full hashtable */
959 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
960 RETURN_IF_ERROR_FBK(status != -ENOENT,
961 "fbk hash lookup succeeded");
963 /* Delete non-existent key in a full hashtable */
964 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
965 RETURN_IF_ERROR_FBK(status != -ENOENT,
966 "fbk hash delete succeeded");
968 /* Delete one key from a full hashtable */
969 status = rte_fbk_hash_delete_key(handle, 1);
970 RETURN_IF_ERROR_FBK(status != 0,
971 "fbk hash delete failed");
973 /* Clear all entries. */
974 rte_fbk_hash_clear_all(handle);
977 rte_fbk_hash_free(handle);
979 /* Cover the NULL case. */
980 rte_fbk_hash_free(0);
986 * Sequence of operations for find existing fbk hash table
989 * - find existing table: hit
990 * - find non-existing table: miss
993 static int test_fbk_hash_find_existing(void)
995 struct rte_fbk_hash_params params = {
996 .name = "fbk_hash_find_existing",
997 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
998 .entries_per_bucket = 4,
1001 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
1003 /* Create hash table. */
1004 handle = rte_fbk_hash_create(¶ms);
1005 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
1007 /* Try to find existing fbk hash table */
1008 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
1009 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
1011 /* Try to find non-existing fbk hash table */
1012 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
1013 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
1016 rte_fbk_hash_free(handle);
1021 #define BUCKET_ENTRIES 4
1023 * Do tests for hash creation with bad parameters.
1025 static int test_hash_creation_with_bad_parameters(void)
1027 struct rte_hash *handle, *tmp;
1028 struct rte_hash_parameters params;
1030 handle = rte_hash_create(NULL);
1031 if (handle != NULL) {
1032 rte_hash_free(handle);
1033 printf("Impossible creating hash sucessfully without any parameter\n");
1037 memcpy(¶ms, &ut_params, sizeof(params));
1038 params.name = "creation_with_bad_parameters_0";
1039 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1040 handle = rte_hash_create(¶ms);
1041 if (handle != NULL) {
1042 rte_hash_free(handle);
1043 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1047 memcpy(¶ms, &ut_params, sizeof(params));
1048 params.name = "creation_with_bad_parameters_2";
1049 params.entries = BUCKET_ENTRIES - 1;
1050 handle = rte_hash_create(¶ms);
1051 if (handle != NULL) {
1052 rte_hash_free(handle);
1053 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1057 memcpy(¶ms, &ut_params, sizeof(params));
1058 params.name = "creation_with_bad_parameters_3";
1060 handle = rte_hash_create(¶ms);
1061 if (handle != NULL) {
1062 rte_hash_free(handle);
1063 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1067 memcpy(¶ms, &ut_params, sizeof(params));
1068 params.name = "creation_with_bad_parameters_4";
1069 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1070 handle = rte_hash_create(¶ms);
1071 if (handle != NULL) {
1072 rte_hash_free(handle);
1073 printf("Impossible creating hash sucessfully with invalid socket\n");
1077 /* test with same name should fail */
1078 memcpy(¶ms, &ut_params, sizeof(params));
1079 params.name = "same_name";
1080 handle = rte_hash_create(¶ms);
1081 if (handle == NULL) {
1082 printf("Cannot create first hash table with 'same_name'\n");
1085 tmp = rte_hash_create(¶ms);
1087 printf("Creation of hash table with same name should fail\n");
1088 rte_hash_free(handle);
1092 rte_hash_free(handle);
1094 printf("# Test successful. No more errors expected\n");
1100 * Do tests for hash creation with parameters that look incorrect
1101 * but are actually valid.
1104 test_hash_creation_with_good_parameters(void)
1106 struct rte_hash *handle;
1107 struct rte_hash_parameters params;
1109 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1110 memcpy(¶ms, &ut_params, sizeof(params));
1111 params.name = "name";
1112 params.hash_func = NULL;
1113 handle = rte_hash_create(¶ms);
1114 if (handle == NULL) {
1115 printf("Creating hash with null hash_func failed\n");
1119 rte_hash_free(handle);
1124 #define ITERATIONS 3
1126 * Test to see the average table utilization (entries added/max entries)
1127 * before hitting a random entry that cannot be added
1129 static int test_average_table_utilization(void)
1131 struct rte_hash *handle;
1132 uint8_t simple_key[MAX_KEYSIZE];
1134 unsigned added_keys, average_keys_added = 0;
1137 printf("\n# Running test to determine average utilization"
1138 "\n before adding elements begins to fail\n");
1139 printf("Measuring performance, please wait");
1141 ut_params.entries = 1 << 16;
1142 ut_params.name = "test_average_utilization";
1143 ut_params.hash_func = rte_jhash;
1144 handle = rte_hash_create(&ut_params);
1145 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1147 for (j = 0; j < ITERATIONS; j++) {
1149 /* Add random entries until key cannot be added */
1150 for (added_keys = 0; ret >= 0; added_keys++) {
1151 for (i = 0; i < ut_params.key_len; i++)
1152 simple_key[i] = rte_rand() % 255;
1153 ret = rte_hash_add_key(handle, simple_key);
1155 if (ret != -ENOSPC) {
1156 printf("Unexpected error when adding keys\n");
1157 rte_hash_free(handle);
1161 average_keys_added += added_keys;
1163 /* Reset the table */
1164 rte_hash_reset(handle);
1166 /* Print a dot to show progress on operations */
1171 average_keys_added /= ITERATIONS;
1173 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1174 ((double) average_keys_added / ut_params.entries * 100),
1175 average_keys_added, ut_params.entries);
1176 rte_hash_free(handle);
1181 #define NUM_ENTRIES 256
1182 static int test_hash_iteration(void)
1184 struct rte_hash *handle;
1186 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1187 const void *next_key;
1189 void *data[NUM_ENTRIES];
1190 unsigned added_keys;
1194 ut_params.entries = NUM_ENTRIES;
1195 ut_params.name = "test_hash_iteration";
1196 ut_params.hash_func = rte_jhash;
1197 ut_params.key_len = 16;
1198 handle = rte_hash_create(&ut_params);
1199 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1201 /* Add random entries until key cannot be added */
1202 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1203 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1204 for (i = 0; i < ut_params.key_len; i++)
1205 keys[added_keys][i] = rte_rand() % 255;
1206 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1211 /* Iterate through the hash table */
1212 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1213 /* Search for the key in the list of keys added */
1214 for (i = 0; i < NUM_ENTRIES; i++) {
1215 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1216 if (next_data != data[i]) {
1217 printf("Data found in the hash table is"
1218 "not the data added with the key\n");
1225 if (i == NUM_ENTRIES) {
1226 printf("Key found in the hash table was not added\n");
1231 /* Check if all keys have been iterated */
1232 if (added_keys != 0) {
1233 printf("There were still %u keys to iterate\n", added_keys);
1237 rte_hash_free(handle);
1241 rte_hash_free(handle);
1245 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1246 0x04, 0x05, 0x06, 0x07,
1247 0x08, 0x09, 0x0a, 0x0b,
1248 0x0c, 0x0d, 0x0e, 0x0f};
1249 static struct rte_hash_parameters hash_params_ex = {
1254 .hash_func_init_val = 0,
1259 * add/delete key with jhash2
1262 test_hash_add_delete_jhash2(void)
1265 struct rte_hash *handle;
1268 hash_params_ex.name = "hash_test_jhash2";
1269 hash_params_ex.key_len = 4;
1270 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1272 handle = rte_hash_create(&hash_params_ex);
1273 if (handle == NULL) {
1274 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1277 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1279 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1283 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1284 if (pos2 < 0 || pos1 != pos2) {
1285 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1292 rte_hash_free(handle);
1298 * add/delete (2) key with jhash2
1301 test_hash_add_delete_2_jhash2(void)
1304 struct rte_hash *handle;
1307 hash_params_ex.name = "hash_test_2_jhash2";
1308 hash_params_ex.key_len = 8;
1309 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1311 handle = rte_hash_create(&hash_params_ex);
1315 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1319 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1320 if (pos2 < 0 || pos1 != pos2)
1327 rte_hash_free(handle);
1333 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1335 const uint32_t *k = key;
1337 RTE_SET_USED(length);
1339 return rte_jhash_1word(k[0], initval);
1343 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1345 const uint32_t *k = key;
1347 RTE_SET_USED(length);
1349 return rte_jhash_2words(k[0], k[1], initval);
1353 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1355 const uint32_t *k = key;
1357 RTE_SET_USED(length);
1359 return rte_jhash_3words(k[0], k[1], k[2], initval);
1363 * add/delete key with jhash 1word
1366 test_hash_add_delete_jhash_1word(void)
1369 struct rte_hash *handle;
1372 hash_params_ex.name = "hash_test_jhash_1word";
1373 hash_params_ex.key_len = 4;
1374 hash_params_ex.hash_func = test_hash_jhash_1word;
1376 handle = rte_hash_create(&hash_params_ex);
1378 goto fail_jhash_1word;
1380 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1382 goto fail_jhash_1word;
1384 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1385 if (pos2 < 0 || pos1 != pos2)
1386 goto fail_jhash_1word;
1392 rte_hash_free(handle);
1398 * add/delete key with jhash 2word
1401 test_hash_add_delete_jhash_2word(void)
1404 struct rte_hash *handle;
1407 hash_params_ex.name = "hash_test_jhash_2word";
1408 hash_params_ex.key_len = 8;
1409 hash_params_ex.hash_func = test_hash_jhash_2word;
1411 handle = rte_hash_create(&hash_params_ex);
1413 goto fail_jhash_2word;
1415 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1417 goto fail_jhash_2word;
1419 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1420 if (pos2 < 0 || pos1 != pos2)
1421 goto fail_jhash_2word;
1427 rte_hash_free(handle);
1433 * add/delete key with jhash 3word
1436 test_hash_add_delete_jhash_3word(void)
1439 struct rte_hash *handle;
1442 hash_params_ex.name = "hash_test_jhash_3word";
1443 hash_params_ex.key_len = 12;
1444 hash_params_ex.hash_func = test_hash_jhash_3word;
1446 handle = rte_hash_create(&hash_params_ex);
1448 goto fail_jhash_3word;
1450 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1452 goto fail_jhash_3word;
1454 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1455 if (pos2 < 0 || pos1 != pos2)
1456 goto fail_jhash_3word;
1462 rte_hash_free(handle);
1468 * Do all unit and performance tests.
1473 if (test_add_delete() < 0)
1475 if (test_hash_add_delete_jhash2() < 0)
1477 if (test_hash_add_delete_2_jhash2() < 0)
1479 if (test_hash_add_delete_jhash_1word() < 0)
1481 if (test_hash_add_delete_jhash_2word() < 0)
1483 if (test_hash_add_delete_jhash_3word() < 0)
1485 if (test_hash_get_key_with_position() < 0)
1487 if (test_hash_find_existing() < 0)
1489 if (test_add_update_delete() < 0)
1491 if (test_five_keys() < 0)
1493 if (test_full_bucket() < 0)
1496 if (test_fbk_hash_find_existing() < 0)
1498 if (fbk_hash_unit_test() < 0)
1500 if (test_hash_creation_with_bad_parameters() < 0)
1502 if (test_hash_creation_with_good_parameters() < 0)
1504 if (test_average_table_utilization() < 0)
1506 if (test_hash_iteration() < 0)
1509 run_hash_func_tests();
1511 if (test_crc32_hash_alg_equiv() < 0)
1517 REGISTER_TEST_COMMAND(hash_autotest, test_hash);