4 * Copyright(c) 2010-2014 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 = {
173 .key_len = sizeof(struct flow_key), /* 13 */
174 .hash_func = rte_jhash,
175 .hash_func_init_val = 0,
179 #define CRC32_ITERATIONS (1U << 20)
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("# 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");
222 /* Resetting to best available algorithm */
223 rte_hash_crc_set_alg(CRC32_SSE42_x64);
225 if (i == CRC32_ITERATIONS)
228 printf("Failed test data (hex, %zu bytes total):\n", data_len);
229 for (j = 0; j < data_len; j++)
230 printf("%02X%c", ((uint8_t *)data64)[j],
231 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
237 * Test a hash function.
239 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
242 static uint8_t key[RTE_HASH_KEY_LENGTH_MAX];
246 for (i = 0; i < key_len; i++)
247 key[i] = (uint8_t) rte_rand();
249 /* just to be on the safe side */
253 f(key, key_len, init_val);
257 * Test all hash functions.
259 static void run_hash_func_tests(void)
264 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
267 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
270 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
272 run_hash_func_test(hashtest_funcs[i],
273 hashtest_initvals[j],
274 hashtest_key_lens[k]);
281 * Basic sequence of operations for a single key:
287 static int test_add_delete(void)
289 struct rte_hash *handle;
290 /* test with standard add/lookup/delete functions */
291 int pos0, expectedPos0;
293 ut_params.name = "test1";
294 handle = rte_hash_create(&ut_params);
295 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
297 pos0 = rte_hash_add_key(handle, &keys[0]);
298 print_key_info("Add", &keys[0], pos0);
299 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
302 pos0 = rte_hash_lookup(handle, &keys[0]);
303 print_key_info("Lkp", &keys[0], pos0);
304 RETURN_IF_ERROR(pos0 != expectedPos0,
305 "failed to find key (pos0=%d)", pos0);
307 pos0 = rte_hash_del_key(handle, &keys[0]);
308 print_key_info("Del", &keys[0], pos0);
309 RETURN_IF_ERROR(pos0 != expectedPos0,
310 "failed to delete key (pos0=%d)", pos0);
312 pos0 = rte_hash_lookup(handle, &keys[0]);
313 print_key_info("Lkp", &keys[0], pos0);
314 RETURN_IF_ERROR(pos0 != -ENOENT,
315 "fail: found key after deleting! (pos0=%d)", pos0);
317 rte_hash_free(handle);
319 /* repeat test with precomputed hash functions */
320 hash_sig_t hash_value;
321 int pos1, expectedPos1;
323 handle = rte_hash_create(&ut_params);
324 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
326 hash_value = rte_hash_hash(handle, &keys[0]);
327 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
328 print_key_info("Add", &keys[0], pos1);
329 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
332 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
333 print_key_info("Lkp", &keys[0], pos1);
334 RETURN_IF_ERROR(pos1 != expectedPos1,
335 "failed to find key (pos1=%d)", pos1);
337 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
338 print_key_info("Del", &keys[0], pos1);
339 RETURN_IF_ERROR(pos1 != expectedPos1,
340 "failed to delete key (pos1=%d)", pos1);
342 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
343 print_key_info("Lkp", &keys[0], pos1);
344 RETURN_IF_ERROR(pos1 != -ENOENT,
345 "fail: found key after deleting! (pos1=%d)", pos1);
347 rte_hash_free(handle);
353 * Sequence of operations for a single key:
358 * - lookup: hit (updated data)
363 static int test_add_update_delete(void)
365 struct rte_hash *handle;
366 int pos0, expectedPos0;
368 ut_params.name = "test2";
369 handle = rte_hash_create(&ut_params);
370 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
372 pos0 = rte_hash_del_key(handle, &keys[0]);
373 print_key_info("Del", &keys[0], pos0);
374 RETURN_IF_ERROR(pos0 != -ENOENT,
375 "fail: found non-existent key (pos0=%d)", pos0);
377 pos0 = rte_hash_add_key(handle, &keys[0]);
378 print_key_info("Add", &keys[0], pos0);
379 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
382 pos0 = rte_hash_lookup(handle, &keys[0]);
383 print_key_info("Lkp", &keys[0], pos0);
384 RETURN_IF_ERROR(pos0 != expectedPos0,
385 "failed to find key (pos0=%d)", pos0);
387 pos0 = rte_hash_add_key(handle, &keys[0]);
388 print_key_info("Add", &keys[0], pos0);
389 RETURN_IF_ERROR(pos0 != expectedPos0,
390 "failed to re-add key (pos0=%d)", pos0);
392 pos0 = rte_hash_lookup(handle, &keys[0]);
393 print_key_info("Lkp", &keys[0], pos0);
394 RETURN_IF_ERROR(pos0 != expectedPos0,
395 "failed to find key (pos0=%d)", pos0);
397 pos0 = rte_hash_del_key(handle, &keys[0]);
398 print_key_info("Del", &keys[0], pos0);
399 RETURN_IF_ERROR(pos0 != expectedPos0,
400 "failed to delete key (pos0=%d)", pos0);
402 pos0 = rte_hash_del_key(handle, &keys[0]);
403 print_key_info("Del", &keys[0], pos0);
404 RETURN_IF_ERROR(pos0 != -ENOENT,
405 "fail: deleted already deleted key (pos0=%d)", pos0);
407 pos0 = rte_hash_lookup(handle, &keys[0]);
408 print_key_info("Lkp", &keys[0], pos0);
409 RETURN_IF_ERROR(pos0 != -ENOENT,
410 "fail: found key after deleting! (pos0=%d)", pos0);
412 rte_hash_free(handle);
417 * Sequence of operations for find existing hash table
420 * - find existing table: hit
421 * - find non-existing table: miss
424 static int test_hash_find_existing(void)
426 struct rte_hash *handle = NULL, *result = NULL;
428 /* Create hash table. */
429 ut_params.name = "hash_find_existing";
430 handle = rte_hash_create(&ut_params);
431 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
433 /* Try to find existing hash table */
434 result = rte_hash_find_existing("hash_find_existing");
435 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
437 /* Try to find non-existing hash table */
438 result = rte_hash_find_existing("hash_find_non_existing");
439 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
442 rte_hash_free(handle);
448 * Sequence of operations for 5 keys
451 * - add keys (update)
452 * - lookup keys: hit (updated data)
453 * - delete keys : hit
454 * - lookup keys: miss
456 static int test_five_keys(void)
458 struct rte_hash *handle;
459 const void *key_array[5] = {0};
465 ut_params.name = "test3";
466 handle = rte_hash_create(&ut_params);
467 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
470 for (i = 0; i < 5; i++) {
471 pos[i] = rte_hash_add_key(handle, &keys[i]);
472 print_key_info("Add", &keys[i], pos[i]);
473 RETURN_IF_ERROR(pos[i] < 0,
474 "failed to add key (pos[%u]=%d)", i, pos[i]);
475 expected_pos[i] = pos[i];
479 for(i = 0; i < 5; i++)
480 key_array[i] = &keys[i];
482 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
484 for(i = 0; i < 5; i++) {
485 print_key_info("Lkp", key_array[i], pos[i]);
486 RETURN_IF_ERROR(pos[i] != expected_pos[i],
487 "failed to find key (pos[%u]=%d)", i, pos[i]);
491 for (i = 0; i < 5; i++) {
492 pos[i] = rte_hash_add_key(handle, &keys[i]);
493 print_key_info("Add", &keys[i], pos[i]);
494 RETURN_IF_ERROR(pos[i] != expected_pos[i],
495 "failed to add key (pos[%u]=%d)", i, pos[i]);
499 for (i = 0; i < 5; i++) {
500 pos[i] = rte_hash_lookup(handle, &keys[i]);
501 print_key_info("Lkp", &keys[i], pos[i]);
502 RETURN_IF_ERROR(pos[i] != expected_pos[i],
503 "failed to find key (pos[%u]=%d)", i, pos[i]);
507 for (i = 0; i < 5; i++) {
508 pos[i] = rte_hash_del_key(handle, &keys[i]);
509 print_key_info("Del", &keys[i], pos[i]);
510 RETURN_IF_ERROR(pos[i] != expected_pos[i],
511 "failed to delete key (pos[%u]=%d)", i, pos[i]);
515 for (i = 0; i < 5; i++) {
516 pos[i] = rte_hash_lookup(handle, &keys[i]);
517 print_key_info("Lkp", &keys[i], pos[i]);
518 RETURN_IF_ERROR(pos[i] != -ENOENT,
519 "failed to find key (pos[%u]=%d)", i, pos[i]);
522 rte_hash_free(handle);
528 * Add keys to the same bucket until bucket full.
529 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
530 * first 4 successful, 5th unsuccessful
531 * - lookup the 5 keys: 4 hits, 1 miss
532 * - add the 5 keys again: 4 OK, one error as bucket is full
533 * - lookup the 5 keys: 4 hits (updated data), 1 miss
534 * - delete the 5 keys: 5 OK (even if the 5th is not in the table)
535 * - lookup the 5 keys: 5 misses
536 * - add the 5th key: OK
537 * - lookup the 5th key: hit
539 static int test_full_bucket(void)
541 struct rte_hash_parameters params_pseudo_hash = {
545 .key_len = sizeof(struct flow_key), /* 13 */
546 .hash_func = pseudo_hash,
547 .hash_func_init_val = 0,
550 struct rte_hash *handle;
555 handle = rte_hash_create(¶ms_pseudo_hash);
556 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
559 for (i = 0; i < 4; i++) {
560 pos[i] = rte_hash_add_key(handle, &keys[i]);
561 print_key_info("Add", &keys[i], pos[i]);
562 RETURN_IF_ERROR(pos[i] < 0,
563 "failed to add key (pos[%u]=%d)", i, pos[i]);
564 expected_pos[i] = pos[i];
566 /* This shouldn't work because the bucket is full */
567 pos[4] = rte_hash_add_key(handle, &keys[4]);
568 print_key_info("Add", &keys[4], pos[4]);
569 RETURN_IF_ERROR(pos[4] != -ENOSPC,
570 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
573 for (i = 0; i < 4; i++) {
574 pos[i] = rte_hash_lookup(handle, &keys[i]);
575 print_key_info("Lkp", &keys[i], pos[i]);
576 RETURN_IF_ERROR(pos[i] != expected_pos[i],
577 "failed to find key (pos[%u]=%d)", i, pos[i]);
579 pos[4] = rte_hash_lookup(handle, &keys[4]);
580 print_key_info("Lkp", &keys[4], pos[4]);
581 RETURN_IF_ERROR(pos[4] != -ENOENT,
582 "fail: found non-existent key (pos[4]=%d)", pos[4]);
585 for (i = 0; i < 4; i++) {
586 pos[i] = rte_hash_add_key(handle, &keys[i]);
587 print_key_info("Add", &keys[i], pos[i]);
588 RETURN_IF_ERROR(pos[i] != expected_pos[i],
589 "failed to add key (pos[%u]=%d)", i, pos[i]);
591 pos[4] = rte_hash_add_key(handle, &keys[4]);
592 print_key_info("Add", &keys[4], pos[4]);
593 RETURN_IF_ERROR(pos[4] != -ENOSPC,
594 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
597 for (i = 0; i < 4; i++) {
598 pos[i] = rte_hash_lookup(handle, &keys[i]);
599 print_key_info("Lkp", &keys[i], pos[i]);
600 RETURN_IF_ERROR(pos[i] != expected_pos[i],
601 "failed to find key (pos[%u]=%d)", i, pos[i]);
603 pos[4] = rte_hash_lookup(handle, &keys[4]);
604 print_key_info("Lkp", &keys[4], pos[4]);
605 RETURN_IF_ERROR(pos[4] != -ENOENT,
606 "fail: found non-existent key (pos[4]=%d)", pos[4]);
608 /* Delete 1 key, check other keys are still found */
609 pos[1] = rte_hash_del_key(handle, &keys[1]);
610 print_key_info("Del", &keys[1], pos[1]);
611 RETURN_IF_ERROR(pos[1] != expected_pos[1],
612 "failed to delete key (pos[1]=%d)", pos[1]);
613 pos[3] = rte_hash_lookup(handle, &keys[3]);
614 print_key_info("Lkp", &keys[3], pos[3]);
615 RETURN_IF_ERROR(pos[3] != expected_pos[3],
616 "failed lookup after deleting key from same bucket "
617 "(pos[3]=%d)", pos[3]);
619 /* Go back to previous state */
620 pos[1] = rte_hash_add_key(handle, &keys[1]);
621 print_key_info("Add", &keys[1], pos[1]);
622 expected_pos[1] = pos[1];
623 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
626 for (i = 0; i < 4; i++) {
627 pos[i] = rte_hash_del_key(handle, &keys[i]);
628 print_key_info("Del", &keys[i], pos[i]);
629 RETURN_IF_ERROR(pos[i] != expected_pos[i],
630 "failed to delete key (pos[%u]=%d)", i, pos[i]);
632 pos[4] = rte_hash_del_key(handle, &keys[4]);
633 print_key_info("Del", &keys[4], pos[4]);
634 RETURN_IF_ERROR(pos[4] != -ENOENT,
635 "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
638 for (i = 0; i < 4; i++) {
639 pos[i] = rte_hash_lookup(handle, &keys[i]);
640 print_key_info("Lkp", &keys[i], pos[i]);
641 RETURN_IF_ERROR(pos[i] != -ENOENT,
642 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
645 /* Add and lookup the 5th key */
646 pos[4] = rte_hash_add_key(handle, &keys[4]);
647 print_key_info("Add", &keys[4], pos[4]);
648 RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
649 expected_pos[4] = pos[4];
650 pos[4] = rte_hash_lookup(handle, &keys[4]);
651 print_key_info("Lkp", &keys[4], pos[4]);
652 RETURN_IF_ERROR(pos[4] != expected_pos[4],
653 "failed to find key (pos[4]=%d)", pos[4]);
655 rte_hash_free(handle);
657 /* Cover the NULL case. */
662 /******************************************************************************/
664 fbk_hash_unit_test(void)
666 struct rte_fbk_hash_params params = {
667 .name = "fbk_hash_test",
668 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
669 .entries_per_bucket = 4,
673 struct rte_fbk_hash_params invalid_params_1 = {
675 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
676 .entries_per_bucket = 4,
680 struct rte_fbk_hash_params invalid_params_2 = {
683 .entries_per_bucket = 3, /* Not power of 2 */
687 struct rte_fbk_hash_params invalid_params_3 = {
689 .entries = 0, /* Entries is 0 */
690 .entries_per_bucket = 4,
694 struct rte_fbk_hash_params invalid_params_4 = {
696 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
697 .entries_per_bucket = 0, /* Entries per bucket is 0 */
701 struct rte_fbk_hash_params invalid_params_5 = {
704 .entries_per_bucket = 8, /* Entries per bucket > entries */
708 struct rte_fbk_hash_params invalid_params_6 = {
710 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
711 .entries_per_bucket = 4,
715 struct rte_fbk_hash_params invalid_params_7 = {
717 .entries = RTE_FBK_HASH_ENTRIES_MAX,
718 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
722 struct rte_fbk_hash_params invalid_params_8 = {
724 .entries = RTE_FBK_HASH_ENTRIES_MAX,
725 .entries_per_bucket = 4,
726 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
729 /* try to create two hashes with identical names
730 * in this case, trying to create a second one will not
731 * fail but will simply return pointer to the existing
732 * hash with that name. sort of like a "find hash by name" :-)
734 struct rte_fbk_hash_params invalid_params_same_name_1 = {
735 .name = "same_name", /* hash with identical name */
737 .entries_per_bucket = 2,
741 /* trying to create this hash should return a pointer to an existing hash */
742 struct rte_fbk_hash_params invalid_params_same_name_2 = {
743 .name = "same_name", /* hash with identical name */
744 .entries = RTE_FBK_HASH_ENTRIES_MAX,
745 .entries_per_bucket = 4,
749 /* this is a sanity check for "same name" test
750 * creating this hash will check if we are actually able to create
751 * multiple hashes with different names (instead of having just one).
753 struct rte_fbk_hash_params different_name = {
754 .name = "different_name", /* different name */
755 .entries = RTE_FBK_HASH_ENTRIES_MAX,
756 .entries_per_bucket = 4,
760 struct rte_fbk_hash_params params_jhash = {
762 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
763 .entries_per_bucket = 4,
765 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
766 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
769 struct rte_fbk_hash_params params_nohash = {
770 .name = "valid nohash",
771 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
772 .entries_per_bucket = 4,
774 .hash_func = NULL, /* Tests for null hash_func */
775 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
778 struct rte_fbk_hash_table *handle, *tmp;
780 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
781 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
786 /* Try creating hashes with invalid parameters */
787 printf("# Testing hash creation with invalid parameters "
788 "- expert error msgs\n");
789 handle = rte_fbk_hash_create(&invalid_params_1);
790 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
792 handle = rte_fbk_hash_create(&invalid_params_2);
793 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
795 handle = rte_fbk_hash_create(&invalid_params_3);
796 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
798 handle = rte_fbk_hash_create(&invalid_params_4);
799 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
801 handle = rte_fbk_hash_create(&invalid_params_5);
802 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
804 handle = rte_fbk_hash_create(&invalid_params_6);
805 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
807 handle = rte_fbk_hash_create(&invalid_params_7);
808 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
810 handle = rte_fbk_hash_create(&invalid_params_8);
811 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
813 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
814 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
816 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
817 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
819 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
820 rte_fbk_hash_free(handle);
821 rte_fbk_hash_free(tmp);
825 /* we are not freeing tmp or handle here because we need a hash list
826 * to be not empty for the next test */
828 /* create a hash in non-empty list - good for coverage */
829 tmp = rte_fbk_hash_create(&different_name);
830 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
832 /* free both hashes */
833 rte_fbk_hash_free(handle);
834 rte_fbk_hash_free(tmp);
836 /* Create empty jhash hash. */
837 handle = rte_fbk_hash_create(¶ms_jhash);
838 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
841 rte_fbk_hash_free(handle);
843 /* Create empty jhash hash. */
844 handle = rte_fbk_hash_create(¶ms_nohash);
845 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
848 rte_fbk_hash_free(handle);
850 /* Create empty hash. */
851 handle = rte_fbk_hash_create(¶ms);
852 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
854 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
855 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
856 "load factor right after creation is not zero but it should be");
858 for (i = 0; i < 5; i++) {
859 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
860 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
863 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
864 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
865 "load factor now is not as expected");
866 /* Find value of added keys. */
867 for (i = 0; i < 5; i++) {
868 status = rte_fbk_hash_lookup(handle, keys[i]);
869 RETURN_IF_ERROR_FBK(status != vals[i],
870 "fbk hash lookup failed");
873 /* Change value of added keys. */
874 for (i = 0; i < 5; i++) {
875 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
876 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
879 /* Find new values. */
880 for (i = 0; i < 5; i++) {
881 status = rte_fbk_hash_lookup(handle, keys[i]);
882 RETURN_IF_ERROR_FBK(status != vals[4-i],
883 "fbk hash lookup failed");
886 /* Delete keys individually. */
887 for (i = 0; i < 5; i++) {
888 status = rte_fbk_hash_delete_key(handle, keys[i]);
889 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
892 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
893 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
894 "load factor right after deletion is not zero but it should be");
895 /* Lookup should now fail. */
896 for (i = 0; i < 5; i++) {
897 status = rte_fbk_hash_lookup(handle, keys[i]);
898 RETURN_IF_ERROR_FBK(status == 0,
899 "fbk hash lookup should have failed");
902 /* Add keys again. */
903 for (i = 0; i < 5; i++) {
904 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
905 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
908 /* Make sure they were added. */
909 for (i = 0; i < 5; i++) {
910 status = rte_fbk_hash_lookup(handle, keys[i]);
911 RETURN_IF_ERROR_FBK(status != vals[i],
912 "fbk hash lookup failed");
915 /* Clear all entries. */
916 rte_fbk_hash_clear_all(handle);
918 /* Lookup should fail. */
919 for (i = 0; i < 5; i++) {
920 status = rte_fbk_hash_lookup(handle, keys[i]);
921 RETURN_IF_ERROR_FBK(status == 0,
922 "fbk hash lookup should have failed");
927 /* fill up the hash_table */
928 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
929 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
931 /* Find non-existent key in a full hashtable */
932 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
933 RETURN_IF_ERROR_FBK(status != -ENOENT,
934 "fbk hash lookup succeeded");
936 /* Delete non-existent key in a full hashtable */
937 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
938 RETURN_IF_ERROR_FBK(status != -ENOENT,
939 "fbk hash delete succeeded");
941 /* Delete one key from a full hashtable */
942 status = rte_fbk_hash_delete_key(handle, 1);
943 RETURN_IF_ERROR_FBK(status != 0,
944 "fbk hash delete failed");
946 /* Clear all entries. */
947 rte_fbk_hash_clear_all(handle);
950 rte_fbk_hash_free(handle);
952 /* Cover the NULL case. */
953 rte_fbk_hash_free(0);
959 * Sequence of operations for find existing fbk hash table
962 * - find existing table: hit
963 * - find non-existing table: miss
966 static int test_fbk_hash_find_existing(void)
968 struct rte_fbk_hash_params params = {
969 .name = "fbk_hash_find_existing",
970 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
971 .entries_per_bucket = 4,
974 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
976 /* Create hash table. */
977 handle = rte_fbk_hash_create(¶ms);
978 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
980 /* Try to find existing fbk hash table */
981 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
982 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
984 /* Try to find non-existing fbk hash table */
985 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
986 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
989 rte_fbk_hash_free(handle);
995 * Do tests for hash creation with bad parameters.
997 static int test_hash_creation_with_bad_parameters(void)
999 struct rte_hash *handle;
1000 struct rte_hash_parameters params;
1002 handle = rte_hash_create(NULL);
1003 if (handle != NULL) {
1004 rte_hash_free(handle);
1005 printf("Impossible creating hash sucessfully without any parameter\n");
1009 memcpy(¶ms, &ut_params, sizeof(params));
1010 params.name = "creation_with_bad_parameters_0";
1011 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1012 handle = rte_hash_create(¶ms);
1013 if (handle != NULL) {
1014 rte_hash_free(handle);
1015 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1019 memcpy(¶ms, &ut_params, sizeof(params));
1020 params.name = "creation_with_bad_parameters_1";
1021 params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
1022 handle = rte_hash_create(¶ms);
1023 if (handle != NULL) {
1024 rte_hash_free(handle);
1025 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
1029 memcpy(¶ms, &ut_params, sizeof(params));
1030 params.name = "creation_with_bad_parameters_2";
1031 params.entries = params.bucket_entries - 1;
1032 handle = rte_hash_create(¶ms);
1033 if (handle != NULL) {
1034 rte_hash_free(handle);
1035 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1039 memcpy(¶ms, &ut_params, sizeof(params));
1040 params.name = "creation_with_bad_parameters_3";
1041 params.entries = params.entries - 1;
1042 handle = rte_hash_create(¶ms);
1043 if (handle != NULL) {
1044 rte_hash_free(handle);
1045 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
1049 memcpy(¶ms, &ut_params, sizeof(params));
1050 params.name = "creation_with_bad_parameters_4";
1051 params.bucket_entries = params.bucket_entries - 1;
1052 handle = rte_hash_create(¶ms);
1053 if (handle != NULL) {
1054 rte_hash_free(handle);
1055 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
1059 memcpy(¶ms, &ut_params, sizeof(params));
1060 params.name = "creation_with_bad_parameters_5";
1062 handle = rte_hash_create(¶ms);
1063 if (handle != NULL) {
1064 rte_hash_free(handle);
1065 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1069 memcpy(¶ms, &ut_params, sizeof(params));
1070 params.name = "creation_with_bad_parameters_6";
1071 params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
1072 handle = rte_hash_create(¶ms);
1073 if (handle != NULL) {
1074 rte_hash_free(handle);
1075 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
1079 memcpy(¶ms, &ut_params, sizeof(params));
1080 params.name = "creation_with_bad_parameters_7";
1081 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1082 handle = rte_hash_create(¶ms);
1083 if (handle != NULL) {
1084 rte_hash_free(handle);
1085 printf("Impossible creating hash sucessfully with invalid socket\n");
1089 rte_hash_free(handle);
1095 * Do tests for hash creation with parameters that look incorrect
1096 * but are actually valid.
1099 test_hash_creation_with_good_parameters(void)
1101 struct rte_hash *handle, *tmp;
1102 struct rte_hash_parameters params;
1104 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1105 memcpy(¶ms, &ut_params, sizeof(params));
1106 params.name = "same_name";
1107 params.hash_func = NULL;
1108 handle = rte_hash_create(¶ms);
1109 if (handle == NULL) {
1110 printf("Creating hash with null hash_func failed\n");
1113 if (handle->hash_func == NULL) {
1114 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1118 /* this test is trying to create a hash with the same name as previous one.
1119 * this should return a pointer to the hash we previously created.
1120 * the previous hash isn't freed exactly for the purpose of it being in
1123 memcpy(¶ms, &ut_params, sizeof(params));
1124 params.name = "same_name";
1125 tmp = rte_hash_create(¶ms);
1127 /* check if the returned handle is actually equal to the previous hash */
1128 if (handle != tmp) {
1129 rte_hash_free(handle);
1131 printf("Creating hash with existing name was successful\n");
1135 /* try creating hash when there already are hashes in the list.
1136 * the previous hash is not freed to have a non-empty hash list.
1137 * the other hash that's in the list is still pointed to by "handle" var.
1139 memcpy(¶ms, &ut_params, sizeof(params));
1140 params.name = "different_name";
1141 tmp = rte_hash_create(¶ms);
1143 rte_hash_free(handle);
1144 printf("Creating hash with valid parameters failed\n");
1149 rte_hash_free(handle);
1154 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1155 0x04, 0x05, 0x06, 0x07,
1156 0x08, 0x09, 0x0a, 0x0b,
1157 0x0c, 0x0d, 0x0e, 0x0f};
1158 static struct rte_hash_parameters hash_params_ex = {
1161 .bucket_entries = 4,
1164 .hash_func_init_val = 0,
1169 * add/delete key with jhash2
1172 test_hash_add_delete_jhash2(void)
1175 struct rte_hash *handle;
1178 hash_params_ex.name = "hash_test_jhash2";
1179 hash_params_ex.key_len = 4;
1180 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1182 handle = rte_hash_create(&hash_params_ex);
1183 if (handle == NULL) {
1184 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1187 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1189 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1193 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1194 if (pos2 < 0 || pos1 != pos2) {
1195 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1202 rte_hash_free(handle);
1208 * add/delete (2) key with jhash2
1211 test_hash_add_delete_2_jhash2(void)
1214 struct rte_hash *handle;
1217 hash_params_ex.name = "hash_test_2_jhash2";
1218 hash_params_ex.key_len = 8;
1219 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1221 handle = rte_hash_create(&hash_params_ex);
1225 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1229 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1230 if (pos2 < 0 || pos1 != pos2)
1237 rte_hash_free(handle);
1243 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1245 const uint32_t *k = key;
1247 RTE_SET_USED(length);
1249 return rte_jhash_1word(k[0], initval);
1253 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1255 const uint32_t *k = key;
1257 RTE_SET_USED(length);
1259 return rte_jhash_2words(k[0], k[1], initval);
1263 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1265 const uint32_t *k = key;
1267 RTE_SET_USED(length);
1269 return rte_jhash_3words(k[0], k[1], k[2], initval);
1273 * add/delete key with jhash 1word
1276 test_hash_add_delete_jhash_1word(void)
1279 struct rte_hash *handle;
1282 hash_params_ex.name = "hash_test_jhash_1word";
1283 hash_params_ex.key_len = 4;
1284 hash_params_ex.hash_func = test_hash_jhash_1word;
1286 handle = rte_hash_create(&hash_params_ex);
1288 goto fail_jhash_1word;
1290 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1292 goto fail_jhash_1word;
1294 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1295 if (pos2 < 0 || pos1 != pos2)
1296 goto fail_jhash_1word;
1302 rte_hash_free(handle);
1308 * add/delete key with jhash 2word
1311 test_hash_add_delete_jhash_2word(void)
1314 struct rte_hash *handle;
1317 hash_params_ex.name = "hash_test_jhash_2word";
1318 hash_params_ex.key_len = 8;
1319 hash_params_ex.hash_func = test_hash_jhash_2word;
1321 handle = rte_hash_create(&hash_params_ex);
1323 goto fail_jhash_2word;
1325 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1327 goto fail_jhash_2word;
1329 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1330 if (pos2 < 0 || pos1 != pos2)
1331 goto fail_jhash_2word;
1337 rte_hash_free(handle);
1343 * add/delete key with jhash 3word
1346 test_hash_add_delete_jhash_3word(void)
1349 struct rte_hash *handle;
1352 hash_params_ex.name = "hash_test_jhash_3word";
1353 hash_params_ex.key_len = 12;
1354 hash_params_ex.hash_func = test_hash_jhash_3word;
1356 handle = rte_hash_create(&hash_params_ex);
1358 goto fail_jhash_3word;
1360 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1362 goto fail_jhash_3word;
1364 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1365 if (pos2 < 0 || pos1 != pos2)
1366 goto fail_jhash_3word;
1372 rte_hash_free(handle);
1378 * Do all unit and performance tests.
1383 if (test_add_delete() < 0)
1385 if (test_hash_add_delete_jhash2() < 0)
1387 if (test_hash_add_delete_2_jhash2() < 0)
1389 if (test_hash_add_delete_jhash_1word() < 0)
1391 if (test_hash_add_delete_jhash_2word() < 0)
1393 if (test_hash_add_delete_jhash_3word() < 0)
1395 if (test_hash_find_existing() < 0)
1397 if (test_add_update_delete() < 0)
1399 if (test_five_keys() < 0)
1401 if (test_full_bucket() < 0)
1404 if (test_fbk_hash_find_existing() < 0)
1406 if (fbk_hash_unit_test() < 0)
1408 if (test_hash_creation_with_bad_parameters() < 0)
1410 if (test_hash_creation_with_good_parameters() < 0)
1413 run_hash_func_tests();
1415 if (test_crc32_hash_alg_equiv() < 0)
1421 static struct test_command hash_cmd = {
1422 .command = "hash_autotest",
1423 .callback = test_hash,
1425 REGISTER_TEST_COMMAND(hash_cmd);