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>
48 #include <rte_tailq.h>
51 #include <rte_string_fns.h>
56 #include <rte_fbk_hash.h>
57 #include <rte_jhash.h>
58 #include <rte_hash_crc.h>
60 /*******************************************************************************
61 * Hash function performance test configuration section. Each performance test
62 * will be performed HASHTEST_ITERATIONS times.
64 * The five arrays below control what tests are performed. Every combination
65 * from the array entries is tested.
67 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
68 static uint32_t hashtest_initvals[] = {0};
69 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 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 = {
174 .key_len = sizeof(struct flow_key), /* 13 */
175 .hash_func = rte_jhash,
176 .hash_func_init_val = 0,
180 #define CRC32_ITERATIONS (1U << 20)
181 #define CRC32_DWORDS (1U << 6)
183 * Test if all CRC32 implementations yield the same hash value
186 test_crc32_hash_alg_equiv(void)
190 uint64_t data64[CRC32_DWORDS];
194 printf("# CRC32 implementations equivalence test\n");
195 for (i = 0; i < CRC32_ITERATIONS; i++) {
196 /* Randomizing data_len of data set */
197 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
198 init_val = (uint32_t) rte_rand();
200 /* Fill the data set */
201 for (j = 0; j < CRC32_DWORDS; j++)
202 data64[j] = rte_rand();
204 /* Calculate software CRC32 */
205 rte_hash_crc_set_alg(CRC32_SW);
206 hash_val = rte_hash_crc(data64, data_len, init_val);
208 /* Check against 4-byte-operand sse4.2 CRC32 if available */
209 rte_hash_crc_set_alg(CRC32_SSE42);
210 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
211 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
215 /* Check against 8-byte-operand sse4.2 CRC32 if available */
216 rte_hash_crc_set_alg(CRC32_SSE42_x64);
217 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
218 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
223 /* Resetting to best available algorithm */
224 rte_hash_crc_set_alg(CRC32_SSE42_x64);
226 if (i == CRC32_ITERATIONS)
229 printf("Failed test data (hex, %zu bytes total):\n", data_len);
230 for (j = 0; j < data_len; j++)
231 printf("%02X%c", ((uint8_t *)data64)[j],
232 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
238 * Test a hash function.
240 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
243 static uint8_t key[RTE_HASH_KEY_LENGTH_MAX];
247 for (i = 0; i < key_len; i++)
248 key[i] = (uint8_t) rte_rand();
250 /* just to be on the safe side */
254 f(key, key_len, init_val);
258 * Test all hash functions.
260 static void run_hash_func_tests(void)
265 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
268 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
271 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
273 run_hash_func_test(hashtest_funcs[i],
274 hashtest_initvals[j],
275 hashtest_key_lens[k]);
282 * Basic sequence of operations for a single key:
288 static int test_add_delete(void)
290 struct rte_hash *handle;
291 /* test with standard add/lookup/delete functions */
292 int pos0, expectedPos0;
294 ut_params.name = "test1";
295 handle = rte_hash_create(&ut_params);
296 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
298 pos0 = rte_hash_add_key(handle, &keys[0]);
299 print_key_info("Add", &keys[0], pos0);
300 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
303 pos0 = rte_hash_lookup(handle, &keys[0]);
304 print_key_info("Lkp", &keys[0], pos0);
305 RETURN_IF_ERROR(pos0 != expectedPos0,
306 "failed to find key (pos0=%d)", pos0);
308 pos0 = rte_hash_del_key(handle, &keys[0]);
309 print_key_info("Del", &keys[0], pos0);
310 RETURN_IF_ERROR(pos0 != expectedPos0,
311 "failed to delete key (pos0=%d)", pos0);
313 pos0 = rte_hash_lookup(handle, &keys[0]);
314 print_key_info("Lkp", &keys[0], pos0);
315 RETURN_IF_ERROR(pos0 != -ENOENT,
316 "fail: found key after deleting! (pos0=%d)", pos0);
318 rte_hash_free(handle);
320 /* repeat test with precomputed hash functions */
321 hash_sig_t hash_value;
322 int pos1, expectedPos1;
324 handle = rte_hash_create(&ut_params);
325 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
327 hash_value = rte_hash_hash(handle, &keys[0]);
328 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
329 print_key_info("Add", &keys[0], pos1);
330 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
333 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
334 print_key_info("Lkp", &keys[0], pos1);
335 RETURN_IF_ERROR(pos1 != expectedPos1,
336 "failed to find key (pos1=%d)", pos1);
338 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
339 print_key_info("Del", &keys[0], pos1);
340 RETURN_IF_ERROR(pos1 != expectedPos1,
341 "failed to delete key (pos1=%d)", pos1);
343 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
344 print_key_info("Lkp", &keys[0], pos1);
345 RETURN_IF_ERROR(pos1 != -ENOENT,
346 "fail: found key after deleting! (pos1=%d)", pos1);
348 rte_hash_free(handle);
354 * Sequence of operations for a single key:
359 * - lookup: hit (updated data)
364 static int test_add_update_delete(void)
366 struct rte_hash *handle;
367 int pos0, expectedPos0;
369 ut_params.name = "test2";
370 handle = rte_hash_create(&ut_params);
371 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
373 pos0 = rte_hash_del_key(handle, &keys[0]);
374 print_key_info("Del", &keys[0], pos0);
375 RETURN_IF_ERROR(pos0 != -ENOENT,
376 "fail: found non-existent key (pos0=%d)", pos0);
378 pos0 = rte_hash_add_key(handle, &keys[0]);
379 print_key_info("Add", &keys[0], pos0);
380 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
383 pos0 = rte_hash_lookup(handle, &keys[0]);
384 print_key_info("Lkp", &keys[0], pos0);
385 RETURN_IF_ERROR(pos0 != expectedPos0,
386 "failed to find key (pos0=%d)", pos0);
388 pos0 = rte_hash_add_key(handle, &keys[0]);
389 print_key_info("Add", &keys[0], pos0);
390 RETURN_IF_ERROR(pos0 != expectedPos0,
391 "failed to re-add key (pos0=%d)", pos0);
393 pos0 = rte_hash_lookup(handle, &keys[0]);
394 print_key_info("Lkp", &keys[0], pos0);
395 RETURN_IF_ERROR(pos0 != expectedPos0,
396 "failed to find key (pos0=%d)", pos0);
398 pos0 = rte_hash_del_key(handle, &keys[0]);
399 print_key_info("Del", &keys[0], pos0);
400 RETURN_IF_ERROR(pos0 != expectedPos0,
401 "failed to delete key (pos0=%d)", pos0);
403 pos0 = rte_hash_del_key(handle, &keys[0]);
404 print_key_info("Del", &keys[0], pos0);
405 RETURN_IF_ERROR(pos0 != -ENOENT,
406 "fail: deleted already deleted key (pos0=%d)", pos0);
408 pos0 = rte_hash_lookup(handle, &keys[0]);
409 print_key_info("Lkp", &keys[0], pos0);
410 RETURN_IF_ERROR(pos0 != -ENOENT,
411 "fail: found key after deleting! (pos0=%d)", pos0);
413 rte_hash_free(handle);
418 * Sequence of operations for find existing hash table
421 * - find existing table: hit
422 * - find non-existing table: miss
425 static int test_hash_find_existing(void)
427 struct rte_hash *handle = NULL, *result = NULL;
429 /* Create hash table. */
430 ut_params.name = "hash_find_existing";
431 handle = rte_hash_create(&ut_params);
432 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
434 /* Try to find existing hash table */
435 result = rte_hash_find_existing("hash_find_existing");
436 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
438 /* Try to find non-existing hash table */
439 result = rte_hash_find_existing("hash_find_non_existing");
440 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
443 rte_hash_free(handle);
449 * Sequence of operations for 5 keys
452 * - add keys (update)
453 * - lookup keys: hit (updated data)
454 * - delete keys : hit
455 * - lookup keys: miss
457 static int test_five_keys(void)
459 struct rte_hash *handle;
460 const void *key_array[5] = {0};
466 ut_params.name = "test3";
467 handle = rte_hash_create(&ut_params);
468 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
471 for (i = 0; i < 5; i++) {
472 pos[i] = rte_hash_add_key(handle, &keys[i]);
473 print_key_info("Add", &keys[i], pos[i]);
474 RETURN_IF_ERROR(pos[i] < 0,
475 "failed to add key (pos[%u]=%d)", i, pos[i]);
476 expected_pos[i] = pos[i];
480 for(i = 0; i < 5; i++)
481 key_array[i] = &keys[i];
483 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
485 for(i = 0; i < 5; i++) {
486 print_key_info("Lkp", key_array[i], pos[i]);
487 RETURN_IF_ERROR(pos[i] != expected_pos[i],
488 "failed to find key (pos[%u]=%d)", i, pos[i]);
492 for (i = 0; i < 5; i++) {
493 pos[i] = rte_hash_add_key(handle, &keys[i]);
494 print_key_info("Add", &keys[i], pos[i]);
495 RETURN_IF_ERROR(pos[i] != expected_pos[i],
496 "failed to add key (pos[%u]=%d)", i, pos[i]);
500 for (i = 0; i < 5; i++) {
501 pos[i] = rte_hash_lookup(handle, &keys[i]);
502 print_key_info("Lkp", &keys[i], pos[i]);
503 RETURN_IF_ERROR(pos[i] != expected_pos[i],
504 "failed to find key (pos[%u]=%d)", i, pos[i]);
508 for (i = 0; i < 5; i++) {
509 pos[i] = rte_hash_del_key(handle, &keys[i]);
510 print_key_info("Del", &keys[i], pos[i]);
511 RETURN_IF_ERROR(pos[i] != expected_pos[i],
512 "failed to delete key (pos[%u]=%d)", i, pos[i]);
516 for (i = 0; i < 5; i++) {
517 pos[i] = rte_hash_lookup(handle, &keys[i]);
518 print_key_info("Lkp", &keys[i], pos[i]);
519 RETURN_IF_ERROR(pos[i] != -ENOENT,
520 "failed to find key (pos[%u]=%d)", i, pos[i]);
523 rte_hash_free(handle);
529 * Add keys to the same bucket until bucket full.
530 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
531 * first 4 successful, 5th unsuccessful
532 * - lookup the 5 keys: 4 hits, 1 miss
533 * - add the 5 keys again: 4 OK, one error as bucket is full
534 * - lookup the 5 keys: 4 hits (updated data), 1 miss
535 * - delete the 5 keys: 5 OK (even if the 5th is not in the table)
536 * - lookup the 5 keys: 5 misses
537 * - add the 5th key: OK
538 * - lookup the 5th key: hit
540 static int test_full_bucket(void)
542 struct rte_hash_parameters params_pseudo_hash = {
546 .key_len = sizeof(struct flow_key), /* 13 */
547 .hash_func = pseudo_hash,
548 .hash_func_init_val = 0,
551 struct rte_hash *handle;
556 handle = rte_hash_create(¶ms_pseudo_hash);
557 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
560 for (i = 0; i < 4; i++) {
561 pos[i] = rte_hash_add_key(handle, &keys[i]);
562 print_key_info("Add", &keys[i], pos[i]);
563 RETURN_IF_ERROR(pos[i] < 0,
564 "failed to add key (pos[%u]=%d)", i, pos[i]);
565 expected_pos[i] = pos[i];
567 /* This shouldn't work because the bucket is full */
568 pos[4] = rte_hash_add_key(handle, &keys[4]);
569 print_key_info("Add", &keys[4], pos[4]);
570 RETURN_IF_ERROR(pos[4] != -ENOSPC,
571 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
574 for (i = 0; i < 4; i++) {
575 pos[i] = rte_hash_lookup(handle, &keys[i]);
576 print_key_info("Lkp", &keys[i], pos[i]);
577 RETURN_IF_ERROR(pos[i] != expected_pos[i],
578 "failed to find key (pos[%u]=%d)", i, pos[i]);
580 pos[4] = rte_hash_lookup(handle, &keys[4]);
581 print_key_info("Lkp", &keys[4], pos[4]);
582 RETURN_IF_ERROR(pos[4] != -ENOENT,
583 "fail: found non-existent key (pos[4]=%d)", pos[4]);
586 for (i = 0; i < 4; i++) {
587 pos[i] = rte_hash_add_key(handle, &keys[i]);
588 print_key_info("Add", &keys[i], pos[i]);
589 RETURN_IF_ERROR(pos[i] != expected_pos[i],
590 "failed to add key (pos[%u]=%d)", i, pos[i]);
592 pos[4] = rte_hash_add_key(handle, &keys[4]);
593 print_key_info("Add", &keys[4], pos[4]);
594 RETURN_IF_ERROR(pos[4] != -ENOSPC,
595 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
598 for (i = 0; i < 4; 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]);
604 pos[4] = rte_hash_lookup(handle, &keys[4]);
605 print_key_info("Lkp", &keys[4], pos[4]);
606 RETURN_IF_ERROR(pos[4] != -ENOENT,
607 "fail: found non-existent key (pos[4]=%d)", pos[4]);
609 /* Delete 1 key, check other keys are still found */
610 pos[1] = rte_hash_del_key(handle, &keys[1]);
611 print_key_info("Del", &keys[1], pos[1]);
612 RETURN_IF_ERROR(pos[1] != expected_pos[1],
613 "failed to delete key (pos[1]=%d)", pos[1]);
614 pos[3] = rte_hash_lookup(handle, &keys[3]);
615 print_key_info("Lkp", &keys[3], pos[3]);
616 RETURN_IF_ERROR(pos[3] != expected_pos[3],
617 "failed lookup after deleting key from same bucket "
618 "(pos[3]=%d)", pos[3]);
620 /* Go back to previous state */
621 pos[1] = rte_hash_add_key(handle, &keys[1]);
622 print_key_info("Add", &keys[1], pos[1]);
623 expected_pos[1] = pos[1];
624 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
627 for (i = 0; i < 4; i++) {
628 pos[i] = rte_hash_del_key(handle, &keys[i]);
629 print_key_info("Del", &keys[i], pos[i]);
630 RETURN_IF_ERROR(pos[i] != expected_pos[i],
631 "failed to delete key (pos[%u]=%d)", i, pos[i]);
633 pos[4] = rte_hash_del_key(handle, &keys[4]);
634 print_key_info("Del", &keys[4], pos[4]);
635 RETURN_IF_ERROR(pos[4] != -ENOENT,
636 "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
639 for (i = 0; i < 4; i++) {
640 pos[i] = rte_hash_lookup(handle, &keys[i]);
641 print_key_info("Lkp", &keys[i], pos[i]);
642 RETURN_IF_ERROR(pos[i] != -ENOENT,
643 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
646 /* Add and lookup the 5th key */
647 pos[4] = rte_hash_add_key(handle, &keys[4]);
648 print_key_info("Add", &keys[4], pos[4]);
649 RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
650 expected_pos[4] = pos[4];
651 pos[4] = rte_hash_lookup(handle, &keys[4]);
652 print_key_info("Lkp", &keys[4], pos[4]);
653 RETURN_IF_ERROR(pos[4] != expected_pos[4],
654 "failed to find key (pos[4]=%d)", pos[4]);
656 rte_hash_free(handle);
658 /* Cover the NULL case. */
663 /******************************************************************************/
665 fbk_hash_unit_test(void)
667 struct rte_fbk_hash_params params = {
668 .name = "fbk_hash_test",
669 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
670 .entries_per_bucket = 4,
674 struct rte_fbk_hash_params invalid_params_1 = {
676 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
677 .entries_per_bucket = 4,
681 struct rte_fbk_hash_params invalid_params_2 = {
684 .entries_per_bucket = 3, /* Not power of 2 */
688 struct rte_fbk_hash_params invalid_params_3 = {
690 .entries = 0, /* Entries is 0 */
691 .entries_per_bucket = 4,
695 struct rte_fbk_hash_params invalid_params_4 = {
697 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
698 .entries_per_bucket = 0, /* Entries per bucket is 0 */
702 struct rte_fbk_hash_params invalid_params_5 = {
705 .entries_per_bucket = 8, /* Entries per bucket > entries */
709 struct rte_fbk_hash_params invalid_params_6 = {
711 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
712 .entries_per_bucket = 4,
716 struct rte_fbk_hash_params invalid_params_7 = {
718 .entries = RTE_FBK_HASH_ENTRIES_MAX,
719 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
723 struct rte_fbk_hash_params invalid_params_8 = {
725 .entries = RTE_FBK_HASH_ENTRIES_MAX,
726 .entries_per_bucket = 4,
727 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
730 /* try to create two hashes with identical names
731 * in this case, trying to create a second one will not
732 * fail but will simply return pointer to the existing
733 * hash with that name. sort of like a "find hash by name" :-)
735 struct rte_fbk_hash_params invalid_params_same_name_1 = {
736 .name = "same_name", /* hash with identical name */
738 .entries_per_bucket = 2,
742 /* trying to create this hash should return a pointer to an existing hash */
743 struct rte_fbk_hash_params invalid_params_same_name_2 = {
744 .name = "same_name", /* hash with identical name */
745 .entries = RTE_FBK_HASH_ENTRIES_MAX,
746 .entries_per_bucket = 4,
750 /* this is a sanity check for "same name" test
751 * creating this hash will check if we are actually able to create
752 * multiple hashes with different names (instead of having just one).
754 struct rte_fbk_hash_params different_name = {
755 .name = "different_name", /* different name */
756 .entries = RTE_FBK_HASH_ENTRIES_MAX,
757 .entries_per_bucket = 4,
761 struct rte_fbk_hash_params params_jhash = {
763 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
764 .entries_per_bucket = 4,
766 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
767 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
770 struct rte_fbk_hash_params params_nohash = {
771 .name = "valid nohash",
772 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
773 .entries_per_bucket = 4,
775 .hash_func = NULL, /* Tests for null hash_func */
776 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
779 struct rte_fbk_hash_table *handle, *tmp;
781 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
782 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
787 /* Try creating hashes with invalid parameters */
788 printf("# Testing hash creation with invalid parameters "
789 "- expert error msgs\n");
790 handle = rte_fbk_hash_create(&invalid_params_1);
791 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
793 handle = rte_fbk_hash_create(&invalid_params_2);
794 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
796 handle = rte_fbk_hash_create(&invalid_params_3);
797 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
799 handle = rte_fbk_hash_create(&invalid_params_4);
800 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
802 handle = rte_fbk_hash_create(&invalid_params_5);
803 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
805 handle = rte_fbk_hash_create(&invalid_params_6);
806 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
808 handle = rte_fbk_hash_create(&invalid_params_7);
809 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
811 handle = rte_fbk_hash_create(&invalid_params_8);
812 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
814 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
815 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
817 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
818 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
820 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
821 rte_fbk_hash_free(handle);
822 rte_fbk_hash_free(tmp);
826 /* we are not freeing tmp or handle here because we need a hash list
827 * to be not empty for the next test */
829 /* create a hash in non-empty list - good for coverage */
830 tmp = rte_fbk_hash_create(&different_name);
831 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
833 /* free both hashes */
834 rte_fbk_hash_free(handle);
835 rte_fbk_hash_free(tmp);
837 /* Create empty jhash hash. */
838 handle = rte_fbk_hash_create(¶ms_jhash);
839 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
842 rte_fbk_hash_free(handle);
844 /* Create empty jhash hash. */
845 handle = rte_fbk_hash_create(¶ms_nohash);
846 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
849 rte_fbk_hash_free(handle);
851 /* Create empty hash. */
852 handle = rte_fbk_hash_create(¶ms);
853 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
855 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
856 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
857 "load factor right after creation is not zero but it should be");
859 for (i = 0; i < 5; i++) {
860 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
861 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
864 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
865 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
866 "load factor now is not as expected");
867 /* Find value of added keys. */
868 for (i = 0; i < 5; i++) {
869 status = rte_fbk_hash_lookup(handle, keys[i]);
870 RETURN_IF_ERROR_FBK(status != vals[i],
871 "fbk hash lookup failed");
874 /* Change value of added keys. */
875 for (i = 0; i < 5; i++) {
876 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
877 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
880 /* Find new values. */
881 for (i = 0; i < 5; i++) {
882 status = rte_fbk_hash_lookup(handle, keys[i]);
883 RETURN_IF_ERROR_FBK(status != vals[4-i],
884 "fbk hash lookup failed");
887 /* Delete keys individually. */
888 for (i = 0; i < 5; i++) {
889 status = rte_fbk_hash_delete_key(handle, keys[i]);
890 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
893 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
894 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
895 "load factor right after deletion is not zero but it should be");
896 /* Lookup should now fail. */
897 for (i = 0; i < 5; i++) {
898 status = rte_fbk_hash_lookup(handle, keys[i]);
899 RETURN_IF_ERROR_FBK(status == 0,
900 "fbk hash lookup should have failed");
903 /* Add keys again. */
904 for (i = 0; i < 5; i++) {
905 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
906 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
909 /* Make sure they were added. */
910 for (i = 0; i < 5; i++) {
911 status = rte_fbk_hash_lookup(handle, keys[i]);
912 RETURN_IF_ERROR_FBK(status != vals[i],
913 "fbk hash lookup failed");
916 /* Clear all entries. */
917 rte_fbk_hash_clear_all(handle);
919 /* Lookup should fail. */
920 for (i = 0; i < 5; i++) {
921 status = rte_fbk_hash_lookup(handle, keys[i]);
922 RETURN_IF_ERROR_FBK(status == 0,
923 "fbk hash lookup should have failed");
928 /* fill up the hash_table */
929 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
930 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
932 /* Find non-existent key in a full hashtable */
933 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
934 RETURN_IF_ERROR_FBK(status != -ENOENT,
935 "fbk hash lookup succeeded");
937 /* Delete non-existent key in a full hashtable */
938 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
939 RETURN_IF_ERROR_FBK(status != -ENOENT,
940 "fbk hash delete succeeded");
942 /* Delete one key from a full hashtable */
943 status = rte_fbk_hash_delete_key(handle, 1);
944 RETURN_IF_ERROR_FBK(status != 0,
945 "fbk hash delete failed");
947 /* Clear all entries. */
948 rte_fbk_hash_clear_all(handle);
951 rte_fbk_hash_free(handle);
953 /* Cover the NULL case. */
954 rte_fbk_hash_free(0);
960 * Sequence of operations for find existing fbk hash table
963 * - find existing table: hit
964 * - find non-existing table: miss
967 static int test_fbk_hash_find_existing(void)
969 struct rte_fbk_hash_params params = {
970 .name = "fbk_hash_find_existing",
971 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
972 .entries_per_bucket = 4,
975 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
977 /* Create hash table. */
978 handle = rte_fbk_hash_create(¶ms);
979 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
981 /* Try to find existing fbk hash table */
982 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
983 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
985 /* Try to find non-existing fbk hash table */
986 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
987 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
990 rte_fbk_hash_free(handle);
996 * Do tests for hash creation with bad parameters.
998 static int test_hash_creation_with_bad_parameters(void)
1000 struct rte_hash *handle;
1001 struct rte_hash_parameters params;
1003 handle = rte_hash_create(NULL);
1004 if (handle != NULL) {
1005 rte_hash_free(handle);
1006 printf("Impossible creating hash sucessfully without any parameter\n");
1010 memcpy(¶ms, &ut_params, sizeof(params));
1011 params.name = "creation_with_bad_parameters_0";
1012 params.entries = RTE_HASH_ENTRIES_MAX + 1;
1013 handle = rte_hash_create(¶ms);
1014 if (handle != NULL) {
1015 rte_hash_free(handle);
1016 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1020 memcpy(¶ms, &ut_params, sizeof(params));
1021 params.name = "creation_with_bad_parameters_1";
1022 params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
1023 handle = rte_hash_create(¶ms);
1024 if (handle != NULL) {
1025 rte_hash_free(handle);
1026 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
1030 memcpy(¶ms, &ut_params, sizeof(params));
1031 params.name = "creation_with_bad_parameters_2";
1032 params.entries = params.bucket_entries - 1;
1033 handle = rte_hash_create(¶ms);
1034 if (handle != NULL) {
1035 rte_hash_free(handle);
1036 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1040 memcpy(¶ms, &ut_params, sizeof(params));
1041 params.name = "creation_with_bad_parameters_3";
1042 params.entries = params.entries - 1;
1043 handle = rte_hash_create(¶ms);
1044 if (handle != NULL) {
1045 rte_hash_free(handle);
1046 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
1050 memcpy(¶ms, &ut_params, sizeof(params));
1051 params.name = "creation_with_bad_parameters_4";
1052 params.bucket_entries = params.bucket_entries - 1;
1053 handle = rte_hash_create(¶ms);
1054 if (handle != NULL) {
1055 rte_hash_free(handle);
1056 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
1060 memcpy(¶ms, &ut_params, sizeof(params));
1061 params.name = "creation_with_bad_parameters_5";
1063 handle = rte_hash_create(¶ms);
1064 if (handle != NULL) {
1065 rte_hash_free(handle);
1066 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1070 memcpy(¶ms, &ut_params, sizeof(params));
1071 params.name = "creation_with_bad_parameters_6";
1072 params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
1073 handle = rte_hash_create(¶ms);
1074 if (handle != NULL) {
1075 rte_hash_free(handle);
1076 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
1080 memcpy(¶ms, &ut_params, sizeof(params));
1081 params.name = "creation_with_bad_parameters_7";
1082 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1083 handle = rte_hash_create(¶ms);
1084 if (handle != NULL) {
1085 rte_hash_free(handle);
1086 printf("Impossible creating hash sucessfully with invalid socket\n");
1090 rte_hash_free(handle);
1096 * Do tests for hash creation with parameters that look incorrect
1097 * but are actually valid.
1100 test_hash_creation_with_good_parameters(void)
1102 struct rte_hash *handle, *tmp;
1103 struct rte_hash_parameters params;
1105 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1106 memcpy(¶ms, &ut_params, sizeof(params));
1107 params.name = "same_name";
1108 params.hash_func = NULL;
1109 handle = rte_hash_create(¶ms);
1110 if (handle == NULL) {
1111 printf("Creating hash with null hash_func failed\n");
1114 if (handle->hash_func == NULL) {
1115 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1119 /* this test is trying to create a hash with the same name as previous one.
1120 * this should return a pointer to the hash we previously created.
1121 * the previous hash isn't freed exactly for the purpose of it being in
1124 memcpy(¶ms, &ut_params, sizeof(params));
1125 params.name = "same_name";
1126 tmp = rte_hash_create(¶ms);
1128 /* check if the returned handle is actually equal to the previous hash */
1129 if (handle != tmp) {
1130 rte_hash_free(handle);
1132 printf("Creating hash with existing name was successful\n");
1136 /* try creating hash when there already are hashes in the list.
1137 * the previous hash is not freed to have a non-empty hash list.
1138 * the other hash that's in the list is still pointed to by "handle" var.
1140 memcpy(¶ms, &ut_params, sizeof(params));
1141 params.name = "different_name";
1142 tmp = rte_hash_create(¶ms);
1144 rte_hash_free(handle);
1145 printf("Creating hash with valid parameters failed\n");
1150 rte_hash_free(handle);
1155 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1156 0x04, 0x05, 0x06, 0x07,
1157 0x08, 0x09, 0x0a, 0x0b,
1158 0x0c, 0x0d, 0x0e, 0x0f};
1159 static struct rte_hash_parameters hash_params_ex = {
1162 .bucket_entries = 4,
1165 .hash_func_init_val = 0,
1170 * add/delete key with jhash2
1173 test_hash_add_delete_jhash2(void)
1176 struct rte_hash *handle;
1179 hash_params_ex.name = "hash_test_jhash2";
1180 hash_params_ex.key_len = 4;
1181 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1183 handle = rte_hash_create(&hash_params_ex);
1184 if (handle == NULL) {
1185 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1188 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1190 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1194 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1195 if (pos2 < 0 || pos1 != pos2) {
1196 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1203 rte_hash_free(handle);
1209 * add/delete (2) key with jhash2
1212 test_hash_add_delete_2_jhash2(void)
1215 struct rte_hash *handle;
1218 hash_params_ex.name = "hash_test_2_jhash2";
1219 hash_params_ex.key_len = 8;
1220 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1222 handle = rte_hash_create(&hash_params_ex);
1226 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1230 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1231 if (pos2 < 0 || pos1 != pos2)
1238 rte_hash_free(handle);
1244 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1246 const uint32_t *k = key;
1248 RTE_SET_USED(length);
1250 return rte_jhash_1word(k[0], initval);
1254 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1256 const uint32_t *k = key;
1258 RTE_SET_USED(length);
1260 return rte_jhash_2words(k[0], k[1], initval);
1264 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1266 const uint32_t *k = key;
1268 RTE_SET_USED(length);
1270 return rte_jhash_3words(k[0], k[1], k[2], initval);
1274 * add/delete key with jhash 1word
1277 test_hash_add_delete_jhash_1word(void)
1280 struct rte_hash *handle;
1283 hash_params_ex.name = "hash_test_jhash_1word";
1284 hash_params_ex.key_len = 4;
1285 hash_params_ex.hash_func = test_hash_jhash_1word;
1287 handle = rte_hash_create(&hash_params_ex);
1289 goto fail_jhash_1word;
1291 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1293 goto fail_jhash_1word;
1295 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1296 if (pos2 < 0 || pos1 != pos2)
1297 goto fail_jhash_1word;
1303 rte_hash_free(handle);
1309 * add/delete key with jhash 2word
1312 test_hash_add_delete_jhash_2word(void)
1315 struct rte_hash *handle;
1318 hash_params_ex.name = "hash_test_jhash_2word";
1319 hash_params_ex.key_len = 8;
1320 hash_params_ex.hash_func = test_hash_jhash_2word;
1322 handle = rte_hash_create(&hash_params_ex);
1324 goto fail_jhash_2word;
1326 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1328 goto fail_jhash_2word;
1330 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1331 if (pos2 < 0 || pos1 != pos2)
1332 goto fail_jhash_2word;
1338 rte_hash_free(handle);
1344 * add/delete key with jhash 3word
1347 test_hash_add_delete_jhash_3word(void)
1350 struct rte_hash *handle;
1353 hash_params_ex.name = "hash_test_jhash_3word";
1354 hash_params_ex.key_len = 12;
1355 hash_params_ex.hash_func = test_hash_jhash_3word;
1357 handle = rte_hash_create(&hash_params_ex);
1359 goto fail_jhash_3word;
1361 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1363 goto fail_jhash_3word;
1365 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1366 if (pos2 < 0 || pos1 != pos2)
1367 goto fail_jhash_3word;
1373 rte_hash_free(handle);
1379 * Do all unit and performance tests.
1384 if (test_add_delete() < 0)
1386 if (test_hash_add_delete_jhash2() < 0)
1388 if (test_hash_add_delete_2_jhash2() < 0)
1390 if (test_hash_add_delete_jhash_1word() < 0)
1392 if (test_hash_add_delete_jhash_2word() < 0)
1394 if (test_hash_add_delete_jhash_3word() < 0)
1396 if (test_hash_find_existing() < 0)
1398 if (test_add_update_delete() < 0)
1400 if (test_five_keys() < 0)
1402 if (test_full_bucket() < 0)
1405 if (test_fbk_hash_find_existing() < 0)
1407 if (fbk_hash_unit_test() < 0)
1409 if (test_hash_creation_with_bad_parameters() < 0)
1411 if (test_hash_creation_with_good_parameters() < 0)
1414 run_hash_func_tests();
1416 if (test_crc32_hash_alg_equiv() < 0)
1422 static struct test_command hash_cmd = {
1423 .command = "hash_autotest",
1424 .callback = test_hash,
1426 REGISTER_TEST_COMMAND(hash_cmd);