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>
59 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
60 #include <rte_hash_crc.h>
63 /*******************************************************************************
64 * Hash function performance test configuration section. Each performance test
65 * will be performed HASHTEST_ITERATIONS times.
67 * The five arrays below control what tests are performed. Every combination
68 * from the array entries is tested.
70 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
71 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
73 static rte_hash_function hashtest_funcs[] = {rte_jhash};
75 static uint32_t hashtest_initvals[] = {0};
76 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
77 /******************************************************************************/
78 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
81 * Check condition and return an error if true. Assumes that "handle" is the
82 * name of the hash structure pointer to be freed.
84 #define RETURN_IF_ERROR(cond, str, ...) do { \
86 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
87 if (handle) rte_hash_free(handle); \
92 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
94 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
95 if (handle) rte_fbk_hash_free(handle); \
100 /* 5-tuple key type */
107 } __attribute__((packed));
110 * Hash function that always returns the same value, to easily test what
111 * happens when a bucket is full.
113 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
114 __attribute__((unused)) uint32_t key_len,
115 __attribute__((unused)) uint32_t init_val)
121 * Print out result of unit test hash operation.
123 #if defined(UNIT_TEST_HASH_VERBOSE)
124 static void print_key_info(const char *msg, const struct flow_key *key,
127 uint8_t *p = (uint8_t *)key;
130 printf("%s key:0x", msg);
131 for (i = 0; i < sizeof(struct flow_key); i++) {
132 printf("%02X", p[i]);
134 printf(" @ pos %d\n", pos);
137 static void print_key_info(__attribute__((unused)) const char *msg,
138 __attribute__((unused)) const struct flow_key *key,
139 __attribute__((unused)) int32_t pos)
144 /* Keys used by unit test functions */
145 static struct flow_key keys[5] = { {
146 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
147 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
152 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
153 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
158 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
159 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
164 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
165 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
170 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
171 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
177 /* Parameters used for hash table in unit test functions. Name set later. */
178 static struct rte_hash_parameters ut_params = {
181 .key_len = sizeof(struct flow_key), /* 13 */
182 .hash_func = rte_jhash,
183 .hash_func_init_val = 0,
188 * Test a hash function.
190 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
193 static uint8_t key[RTE_HASH_KEY_LENGTH_MAX];
197 for (i = 0; i < key_len; i++)
198 key[i] = (uint8_t) rte_rand();
200 /* just to be on the safe side */
204 f(key, key_len, init_val);
208 * Test all hash functions.
210 static void run_hash_func_tests(void)
215 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
218 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
221 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
223 run_hash_func_test(hashtest_funcs[i],
224 hashtest_initvals[j],
225 hashtest_key_lens[k]);
232 * Basic sequence of operations for a single key:
238 static int test_add_delete(void)
240 struct rte_hash *handle;
241 /* test with standard add/lookup/delete functions */
242 int pos0, expectedPos0;
244 ut_params.name = "test1";
245 handle = rte_hash_create(&ut_params);
246 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
248 pos0 = rte_hash_add_key(handle, &keys[0]);
249 print_key_info("Add", &keys[0], pos0);
250 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
253 pos0 = rte_hash_lookup(handle, &keys[0]);
254 print_key_info("Lkp", &keys[0], pos0);
255 RETURN_IF_ERROR(pos0 != expectedPos0,
256 "failed to find key (pos0=%d)", pos0);
258 pos0 = rte_hash_del_key(handle, &keys[0]);
259 print_key_info("Del", &keys[0], pos0);
260 RETURN_IF_ERROR(pos0 != expectedPos0,
261 "failed to delete key (pos0=%d)", pos0);
263 pos0 = rte_hash_lookup(handle, &keys[0]);
264 print_key_info("Lkp", &keys[0], pos0);
265 RETURN_IF_ERROR(pos0 != -ENOENT,
266 "fail: found key after deleting! (pos0=%d)", pos0);
268 rte_hash_free(handle);
270 /* repeat test with precomputed hash functions */
271 hash_sig_t hash_value;
272 int pos1, expectedPos1;
274 handle = rte_hash_create(&ut_params);
275 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
277 hash_value = rte_hash_hash(handle, &keys[0]);
278 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
279 print_key_info("Add", &keys[0], pos1);
280 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
283 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
284 print_key_info("Lkp", &keys[0], pos1);
285 RETURN_IF_ERROR(pos1 != expectedPos1,
286 "failed to find key (pos1=%d)", pos1);
288 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
289 print_key_info("Del", &keys[0], pos1);
290 RETURN_IF_ERROR(pos1 != expectedPos1,
291 "failed to delete key (pos1=%d)", pos1);
293 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
294 print_key_info("Lkp", &keys[0], pos1);
295 RETURN_IF_ERROR(pos1 != -ENOENT,
296 "fail: found key after deleting! (pos1=%d)", pos1);
298 rte_hash_free(handle);
304 * Sequence of operations for a single key:
309 * - lookup: hit (updated data)
314 static int test_add_update_delete(void)
316 struct rte_hash *handle;
317 int pos0, expectedPos0;
319 ut_params.name = "test2";
320 handle = rte_hash_create(&ut_params);
321 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
323 pos0 = rte_hash_del_key(handle, &keys[0]);
324 print_key_info("Del", &keys[0], pos0);
325 RETURN_IF_ERROR(pos0 != -ENOENT,
326 "fail: found non-existent key (pos0=%d)", pos0);
328 pos0 = rte_hash_add_key(handle, &keys[0]);
329 print_key_info("Add", &keys[0], pos0);
330 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
333 pos0 = rte_hash_lookup(handle, &keys[0]);
334 print_key_info("Lkp", &keys[0], pos0);
335 RETURN_IF_ERROR(pos0 != expectedPos0,
336 "failed to find key (pos0=%d)", pos0);
338 pos0 = rte_hash_add_key(handle, &keys[0]);
339 print_key_info("Add", &keys[0], pos0);
340 RETURN_IF_ERROR(pos0 != expectedPos0,
341 "failed to re-add key (pos0=%d)", pos0);
343 pos0 = rte_hash_lookup(handle, &keys[0]);
344 print_key_info("Lkp", &keys[0], pos0);
345 RETURN_IF_ERROR(pos0 != expectedPos0,
346 "failed to find key (pos0=%d)", pos0);
348 pos0 = rte_hash_del_key(handle, &keys[0]);
349 print_key_info("Del", &keys[0], pos0);
350 RETURN_IF_ERROR(pos0 != expectedPos0,
351 "failed to delete key (pos0=%d)", pos0);
353 pos0 = rte_hash_del_key(handle, &keys[0]);
354 print_key_info("Del", &keys[0], pos0);
355 RETURN_IF_ERROR(pos0 != -ENOENT,
356 "fail: deleted already deleted key (pos0=%d)", pos0);
358 pos0 = rte_hash_lookup(handle, &keys[0]);
359 print_key_info("Lkp", &keys[0], pos0);
360 RETURN_IF_ERROR(pos0 != -ENOENT,
361 "fail: found key after deleting! (pos0=%d)", pos0);
363 rte_hash_free(handle);
368 * Sequence of operations for find existing hash table
371 * - find existing table: hit
372 * - find non-existing table: miss
375 static int test_hash_find_existing(void)
377 struct rte_hash *handle = NULL, *result = NULL;
379 /* Create hash table. */
380 ut_params.name = "hash_find_existing";
381 handle = rte_hash_create(&ut_params);
382 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
384 /* Try to find existing hash table */
385 result = rte_hash_find_existing("hash_find_existing");
386 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
388 /* Try to find non-existing hash table */
389 result = rte_hash_find_existing("hash_find_non_existing");
390 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
393 rte_hash_free(handle);
399 * Sequence of operations for 5 keys
402 * - add keys (update)
403 * - lookup keys: hit (updated data)
404 * - delete keys : hit
405 * - lookup keys: miss
407 static int test_five_keys(void)
409 struct rte_hash *handle;
410 const void *key_array[5] = {0};
416 ut_params.name = "test3";
417 handle = rte_hash_create(&ut_params);
418 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
421 for (i = 0; i < 5; i++) {
422 pos[i] = rte_hash_add_key(handle, &keys[i]);
423 print_key_info("Add", &keys[i], pos[i]);
424 RETURN_IF_ERROR(pos[i] < 0,
425 "failed to add key (pos[%u]=%d)", i, pos[i]);
426 expected_pos[i] = pos[i];
430 for(i = 0; i < 5; i++)
431 key_array[i] = &keys[i];
433 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
435 for(i = 0; i < 5; i++) {
436 print_key_info("Lkp", key_array[i], pos[i]);
437 RETURN_IF_ERROR(pos[i] != expected_pos[i],
438 "failed to find key (pos[%u]=%d)", i, pos[i]);
442 for (i = 0; i < 5; i++) {
443 pos[i] = rte_hash_add_key(handle, &keys[i]);
444 print_key_info("Add", &keys[i], pos[i]);
445 RETURN_IF_ERROR(pos[i] != expected_pos[i],
446 "failed to add key (pos[%u]=%d)", i, pos[i]);
450 for (i = 0; i < 5; i++) {
451 pos[i] = rte_hash_lookup(handle, &keys[i]);
452 print_key_info("Lkp", &keys[i], pos[i]);
453 RETURN_IF_ERROR(pos[i] != expected_pos[i],
454 "failed to find key (pos[%u]=%d)", i, pos[i]);
458 for (i = 0; i < 5; i++) {
459 pos[i] = rte_hash_del_key(handle, &keys[i]);
460 print_key_info("Del", &keys[i], pos[i]);
461 RETURN_IF_ERROR(pos[i] != expected_pos[i],
462 "failed to delete key (pos[%u]=%d)", i, pos[i]);
466 for (i = 0; i < 5; i++) {
467 pos[i] = rte_hash_lookup(handle, &keys[i]);
468 print_key_info("Lkp", &keys[i], pos[i]);
469 RETURN_IF_ERROR(pos[i] != -ENOENT,
470 "failed to find key (pos[%u]=%d)", i, pos[i]);
473 rte_hash_free(handle);
479 * Add keys to the same bucket until bucket full.
480 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
481 * first 4 successful, 5th unsuccessful
482 * - lookup the 5 keys: 4 hits, 1 miss
483 * - add the 5 keys again: 4 OK, one error as bucket is full
484 * - lookup the 5 keys: 4 hits (updated data), 1 miss
485 * - delete the 5 keys: 5 OK (even if the 5th is not in the table)
486 * - lookup the 5 keys: 5 misses
487 * - add the 5th key: OK
488 * - lookup the 5th key: hit
490 static int test_full_bucket(void)
492 struct rte_hash_parameters params_pseudo_hash = {
496 .key_len = sizeof(struct flow_key), /* 13 */
497 .hash_func = pseudo_hash,
498 .hash_func_init_val = 0,
501 struct rte_hash *handle;
506 handle = rte_hash_create(¶ms_pseudo_hash);
507 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
510 for (i = 0; i < 4; i++) {
511 pos[i] = rte_hash_add_key(handle, &keys[i]);
512 print_key_info("Add", &keys[i], pos[i]);
513 RETURN_IF_ERROR(pos[i] < 0,
514 "failed to add key (pos[%u]=%d)", i, pos[i]);
515 expected_pos[i] = pos[i];
517 /* This shouldn't work because the bucket is full */
518 pos[4] = rte_hash_add_key(handle, &keys[4]);
519 print_key_info("Add", &keys[4], pos[4]);
520 RETURN_IF_ERROR(pos[4] != -ENOSPC,
521 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
524 for (i = 0; i < 4; i++) {
525 pos[i] = rte_hash_lookup(handle, &keys[i]);
526 print_key_info("Lkp", &keys[i], pos[i]);
527 RETURN_IF_ERROR(pos[i] != expected_pos[i],
528 "failed to find key (pos[%u]=%d)", i, pos[i]);
530 pos[4] = rte_hash_lookup(handle, &keys[4]);
531 print_key_info("Lkp", &keys[4], pos[4]);
532 RETURN_IF_ERROR(pos[4] != -ENOENT,
533 "fail: found non-existent key (pos[4]=%d)", pos[4]);
536 for (i = 0; i < 4; i++) {
537 pos[i] = rte_hash_add_key(handle, &keys[i]);
538 print_key_info("Add", &keys[i], pos[i]);
539 RETURN_IF_ERROR(pos[i] != expected_pos[i],
540 "failed to add key (pos[%u]=%d)", i, pos[i]);
542 pos[4] = rte_hash_add_key(handle, &keys[4]);
543 print_key_info("Add", &keys[4], pos[4]);
544 RETURN_IF_ERROR(pos[4] != -ENOSPC,
545 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
548 for (i = 0; i < 4; i++) {
549 pos[i] = rte_hash_lookup(handle, &keys[i]);
550 print_key_info("Lkp", &keys[i], pos[i]);
551 RETURN_IF_ERROR(pos[i] != expected_pos[i],
552 "failed to find key (pos[%u]=%d)", i, pos[i]);
554 pos[4] = rte_hash_lookup(handle, &keys[4]);
555 print_key_info("Lkp", &keys[4], pos[4]);
556 RETURN_IF_ERROR(pos[4] != -ENOENT,
557 "fail: found non-existent key (pos[4]=%d)", pos[4]);
559 /* Delete 1 key, check other keys are still found */
560 pos[1] = rte_hash_del_key(handle, &keys[1]);
561 print_key_info("Del", &keys[1], pos[1]);
562 RETURN_IF_ERROR(pos[1] != expected_pos[1],
563 "failed to delete key (pos[1]=%d)", pos[1]);
564 pos[3] = rte_hash_lookup(handle, &keys[3]);
565 print_key_info("Lkp", &keys[3], pos[3]);
566 RETURN_IF_ERROR(pos[3] != expected_pos[3],
567 "failed lookup after deleting key from same bucket "
568 "(pos[3]=%d)", pos[3]);
570 /* Go back to previous state */
571 pos[1] = rte_hash_add_key(handle, &keys[1]);
572 print_key_info("Add", &keys[1], pos[1]);
573 expected_pos[1] = pos[1];
574 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
577 for (i = 0; i < 4; i++) {
578 pos[i] = rte_hash_del_key(handle, &keys[i]);
579 print_key_info("Del", &keys[i], pos[i]);
580 RETURN_IF_ERROR(pos[i] != expected_pos[i],
581 "failed to delete key (pos[%u]=%d)", i, pos[i]);
583 pos[4] = rte_hash_del_key(handle, &keys[4]);
584 print_key_info("Del", &keys[4], pos[4]);
585 RETURN_IF_ERROR(pos[4] != -ENOENT,
586 "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
589 for (i = 0; i < 4; i++) {
590 pos[i] = rte_hash_lookup(handle, &keys[i]);
591 print_key_info("Lkp", &keys[i], pos[i]);
592 RETURN_IF_ERROR(pos[i] != -ENOENT,
593 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
596 /* Add and lookup the 5th key */
597 pos[4] = rte_hash_add_key(handle, &keys[4]);
598 print_key_info("Add", &keys[4], pos[4]);
599 RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
600 expected_pos[4] = pos[4];
601 pos[4] = rte_hash_lookup(handle, &keys[4]);
602 print_key_info("Lkp", &keys[4], pos[4]);
603 RETURN_IF_ERROR(pos[4] != expected_pos[4],
604 "failed to find key (pos[4]=%d)", pos[4]);
606 rte_hash_free(handle);
608 /* Cover the NULL case. */
613 /******************************************************************************/
615 fbk_hash_unit_test(void)
617 struct rte_fbk_hash_params params = {
618 .name = "fbk_hash_test",
619 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
620 .entries_per_bucket = 4,
624 struct rte_fbk_hash_params invalid_params_1 = {
626 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
627 .entries_per_bucket = 4,
631 struct rte_fbk_hash_params invalid_params_2 = {
634 .entries_per_bucket = 3, /* Not power of 2 */
638 struct rte_fbk_hash_params invalid_params_3 = {
640 .entries = 0, /* Entries is 0 */
641 .entries_per_bucket = 4,
645 struct rte_fbk_hash_params invalid_params_4 = {
647 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
648 .entries_per_bucket = 0, /* Entries per bucket is 0 */
652 struct rte_fbk_hash_params invalid_params_5 = {
655 .entries_per_bucket = 8, /* Entries per bucket > entries */
659 struct rte_fbk_hash_params invalid_params_6 = {
661 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
662 .entries_per_bucket = 4,
666 struct rte_fbk_hash_params invalid_params_7 = {
668 .entries = RTE_FBK_HASH_ENTRIES_MAX,
669 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
673 struct rte_fbk_hash_params invalid_params_8 = {
675 .entries = RTE_FBK_HASH_ENTRIES_MAX,
676 .entries_per_bucket = 4,
677 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
680 /* try to create two hashes with identical names
681 * in this case, trying to create a second one will not
682 * fail but will simply return pointer to the existing
683 * hash with that name. sort of like a "find hash by name" :-)
685 struct rte_fbk_hash_params invalid_params_same_name_1 = {
686 .name = "same_name", /* hash with identical name */
688 .entries_per_bucket = 2,
692 /* trying to create this hash should return a pointer to an existing hash */
693 struct rte_fbk_hash_params invalid_params_same_name_2 = {
694 .name = "same_name", /* hash with identical name */
695 .entries = RTE_FBK_HASH_ENTRIES_MAX,
696 .entries_per_bucket = 4,
700 /* this is a sanity check for "same name" test
701 * creating this hash will check if we are actually able to create
702 * multiple hashes with different names (instead of having just one).
704 struct rte_fbk_hash_params different_name = {
705 .name = "different_name", /* different name */
706 .entries = RTE_FBK_HASH_ENTRIES_MAX,
707 .entries_per_bucket = 4,
711 struct rte_fbk_hash_params params_jhash = {
713 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
714 .entries_per_bucket = 4,
716 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
717 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
720 struct rte_fbk_hash_params params_nohash = {
721 .name = "valid nohash",
722 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
723 .entries_per_bucket = 4,
725 .hash_func = NULL, /* Tests for null hash_func */
726 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
729 struct rte_fbk_hash_table *handle, *tmp;
731 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
732 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
737 /* Try creating hashes with invalid parameters */
738 printf("# Testing hash creation with invalid parameters "
739 "- expert error msgs\n");
740 handle = rte_fbk_hash_create(&invalid_params_1);
741 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
743 handle = rte_fbk_hash_create(&invalid_params_2);
744 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
746 handle = rte_fbk_hash_create(&invalid_params_3);
747 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
749 handle = rte_fbk_hash_create(&invalid_params_4);
750 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
752 handle = rte_fbk_hash_create(&invalid_params_5);
753 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
755 handle = rte_fbk_hash_create(&invalid_params_6);
756 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
758 handle = rte_fbk_hash_create(&invalid_params_7);
759 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
761 handle = rte_fbk_hash_create(&invalid_params_8);
762 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
764 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
765 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
767 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
768 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
770 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
771 rte_fbk_hash_free(handle);
772 rte_fbk_hash_free(tmp);
776 /* we are not freeing tmp or handle here because we need a hash list
777 * to be not empty for the next test */
779 /* create a hash in non-empty list - good for coverage */
780 tmp = rte_fbk_hash_create(&different_name);
781 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
783 /* free both hashes */
784 rte_fbk_hash_free(handle);
785 rte_fbk_hash_free(tmp);
787 /* Create empty jhash hash. */
788 handle = rte_fbk_hash_create(¶ms_jhash);
789 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
792 rte_fbk_hash_free(handle);
794 /* Create empty jhash hash. */
795 handle = rte_fbk_hash_create(¶ms_nohash);
796 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
799 rte_fbk_hash_free(handle);
801 /* Create empty hash. */
802 handle = rte_fbk_hash_create(¶ms);
803 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
805 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
806 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
807 "load factor right after creation is not zero but it should be");
809 for (i = 0; i < 5; i++) {
810 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
811 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
814 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
815 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
816 "load factor now is not as expected");
817 /* Find value of added keys. */
818 for (i = 0; i < 5; i++) {
819 status = rte_fbk_hash_lookup(handle, keys[i]);
820 RETURN_IF_ERROR_FBK(status != vals[i],
821 "fbk hash lookup failed");
824 /* Change value of added keys. */
825 for (i = 0; i < 5; i++) {
826 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
827 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
830 /* Find new values. */
831 for (i = 0; i < 5; i++) {
832 status = rte_fbk_hash_lookup(handle, keys[i]);
833 RETURN_IF_ERROR_FBK(status != vals[4-i],
834 "fbk hash lookup failed");
837 /* Delete keys individually. */
838 for (i = 0; i < 5; i++) {
839 status = rte_fbk_hash_delete_key(handle, keys[i]);
840 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
843 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
844 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
845 "load factor right after deletion is not zero but it should be");
846 /* Lookup should now fail. */
847 for (i = 0; i < 5; i++) {
848 status = rte_fbk_hash_lookup(handle, keys[i]);
849 RETURN_IF_ERROR_FBK(status == 0,
850 "fbk hash lookup should have failed");
853 /* Add keys again. */
854 for (i = 0; i < 5; i++) {
855 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
856 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
859 /* Make sure they were added. */
860 for (i = 0; i < 5; i++) {
861 status = rte_fbk_hash_lookup(handle, keys[i]);
862 RETURN_IF_ERROR_FBK(status != vals[i],
863 "fbk hash lookup failed");
866 /* Clear all entries. */
867 rte_fbk_hash_clear_all(handle);
869 /* Lookup should fail. */
870 for (i = 0; i < 5; i++) {
871 status = rte_fbk_hash_lookup(handle, keys[i]);
872 RETURN_IF_ERROR_FBK(status == 0,
873 "fbk hash lookup should have failed");
878 /* fill up the hash_table */
879 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
880 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
882 /* Find non-existent key in a full hashtable */
883 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
884 RETURN_IF_ERROR_FBK(status != -ENOENT,
885 "fbk hash lookup succeeded");
887 /* Delete non-existent key in a full hashtable */
888 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
889 RETURN_IF_ERROR_FBK(status != -ENOENT,
890 "fbk hash delete succeeded");
892 /* Delete one key from a full hashtable */
893 status = rte_fbk_hash_delete_key(handle, 1);
894 RETURN_IF_ERROR_FBK(status != 0,
895 "fbk hash delete failed");
897 /* Clear all entries. */
898 rte_fbk_hash_clear_all(handle);
901 rte_fbk_hash_free(handle);
903 /* Cover the NULL case. */
904 rte_fbk_hash_free(0);
910 * Sequence of operations for find existing fbk hash table
913 * - find existing table: hit
914 * - find non-existing table: miss
917 static int test_fbk_hash_find_existing(void)
919 struct rte_fbk_hash_params params = {
920 .name = "fbk_hash_find_existing",
921 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
922 .entries_per_bucket = 4,
925 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
927 /* Create hash table. */
928 handle = rte_fbk_hash_create(¶ms);
929 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
931 /* Try to find existing fbk hash table */
932 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
933 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
935 /* Try to find non-existing fbk hash table */
936 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
937 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
940 rte_fbk_hash_free(handle);
946 * Do tests for hash creation with bad parameters.
948 static int test_hash_creation_with_bad_parameters(void)
950 struct rte_hash *handle;
951 struct rte_hash_parameters params;
953 handle = rte_hash_create(NULL);
954 if (handle != NULL) {
955 rte_hash_free(handle);
956 printf("Impossible creating hash sucessfully without any parameter\n");
960 memcpy(¶ms, &ut_params, sizeof(params));
961 params.name = "creation_with_bad_parameters_0";
962 params.entries = RTE_HASH_ENTRIES_MAX + 1;
963 handle = rte_hash_create(¶ms);
964 if (handle != NULL) {
965 rte_hash_free(handle);
966 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
970 memcpy(¶ms, &ut_params, sizeof(params));
971 params.name = "creation_with_bad_parameters_1";
972 params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
973 handle = rte_hash_create(¶ms);
974 if (handle != NULL) {
975 rte_hash_free(handle);
976 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
980 memcpy(¶ms, &ut_params, sizeof(params));
981 params.name = "creation_with_bad_parameters_2";
982 params.entries = params.bucket_entries - 1;
983 handle = rte_hash_create(¶ms);
984 if (handle != NULL) {
985 rte_hash_free(handle);
986 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
990 memcpy(¶ms, &ut_params, sizeof(params));
991 params.name = "creation_with_bad_parameters_3";
992 params.entries = params.entries - 1;
993 handle = rte_hash_create(¶ms);
994 if (handle != NULL) {
995 rte_hash_free(handle);
996 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
1000 memcpy(¶ms, &ut_params, sizeof(params));
1001 params.name = "creation_with_bad_parameters_4";
1002 params.bucket_entries = params.bucket_entries - 1;
1003 handle = rte_hash_create(¶ms);
1004 if (handle != NULL) {
1005 rte_hash_free(handle);
1006 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
1010 memcpy(¶ms, &ut_params, sizeof(params));
1011 params.name = "creation_with_bad_parameters_5";
1013 handle = rte_hash_create(¶ms);
1014 if (handle != NULL) {
1015 rte_hash_free(handle);
1016 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1020 memcpy(¶ms, &ut_params, sizeof(params));
1021 params.name = "creation_with_bad_parameters_6";
1022 params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
1023 handle = rte_hash_create(¶ms);
1024 if (handle != NULL) {
1025 rte_hash_free(handle);
1026 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
1030 memcpy(¶ms, &ut_params, sizeof(params));
1031 params.name = "creation_with_bad_parameters_7";
1032 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1033 handle = rte_hash_create(¶ms);
1034 if (handle != NULL) {
1035 rte_hash_free(handle);
1036 printf("Impossible creating hash sucessfully with invalid socket\n");
1040 rte_hash_free(handle);
1046 * Do tests for hash creation with parameters that look incorrect
1047 * but are actually valid.
1050 test_hash_creation_with_good_parameters(void)
1052 struct rte_hash *handle, *tmp;
1053 struct rte_hash_parameters params;
1055 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1056 memcpy(¶ms, &ut_params, sizeof(params));
1057 params.name = "same_name";
1058 params.hash_func = NULL;
1059 handle = rte_hash_create(¶ms);
1060 if (handle == NULL) {
1061 printf("Creating hash with null hash_func failed\n");
1064 if (handle->hash_func == NULL) {
1065 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1069 /* this test is trying to create a hash with the same name as previous one.
1070 * this should return a pointer to the hash we previously created.
1071 * the previous hash isn't freed exactly for the purpose of it being in
1074 memcpy(¶ms, &ut_params, sizeof(params));
1075 params.name = "same_name";
1076 tmp = rte_hash_create(¶ms);
1078 /* check if the returned handle is actually equal to the previous hash */
1079 if (handle != tmp) {
1080 rte_hash_free(handle);
1082 printf("Creating hash with existing name was successful\n");
1086 /* try creating hash when there already are hashes in the list.
1087 * the previous hash is not freed to have a non-empty hash list.
1088 * the other hash that's in the list is still pointed to by "handle" var.
1090 memcpy(¶ms, &ut_params, sizeof(params));
1091 params.name = "different_name";
1092 tmp = rte_hash_create(¶ms);
1094 rte_hash_free(handle);
1095 printf("Creating hash with valid parameters failed\n");
1100 rte_hash_free(handle);
1105 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1106 0x04, 0x05, 0x06, 0x07,
1107 0x08, 0x09, 0x0a, 0x0b,
1108 0x0c, 0x0d, 0x0e, 0x0f};
1109 static struct rte_hash_parameters hash_params_ex = {
1112 .bucket_entries = 4,
1115 .hash_func_init_val = 0,
1120 * add/delete key with jhash2
1123 test_hash_add_delete_jhash2(void)
1126 struct rte_hash *handle;
1129 hash_params_ex.name = "hash_test_jhash2";
1130 hash_params_ex.key_len = 4;
1131 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1133 handle = rte_hash_create(&hash_params_ex);
1134 if (handle == NULL) {
1135 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1138 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1140 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1144 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1145 if (pos2 < 0 || pos1 != pos2) {
1146 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1153 rte_hash_free(handle);
1159 * add/delete (2) key with jhash2
1162 test_hash_add_delete_2_jhash2(void)
1165 struct rte_hash *handle;
1168 hash_params_ex.name = "hash_test_2_jhash2";
1169 hash_params_ex.key_len = 8;
1170 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1172 handle = rte_hash_create(&hash_params_ex);
1176 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1180 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1181 if (pos2 < 0 || pos1 != pos2)
1188 rte_hash_free(handle);
1194 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1196 const uint32_t *k = key;
1198 RTE_SET_USED(length);
1200 return rte_jhash_1word(k[0], initval);
1204 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1206 const uint32_t *k = key;
1208 RTE_SET_USED(length);
1210 return rte_jhash_2words(k[0], k[1], initval);
1214 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1216 const uint32_t *k = key;
1218 RTE_SET_USED(length);
1220 return rte_jhash_3words(k[0], k[1], k[2], initval);
1224 * add/delete key with jhash 1word
1227 test_hash_add_delete_jhash_1word(void)
1230 struct rte_hash *handle;
1233 hash_params_ex.name = "hash_test_jhash_1word";
1234 hash_params_ex.key_len = 4;
1235 hash_params_ex.hash_func = test_hash_jhash_1word;
1237 handle = rte_hash_create(&hash_params_ex);
1239 goto fail_jhash_1word;
1241 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1243 goto fail_jhash_1word;
1245 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1246 if (pos2 < 0 || pos1 != pos2)
1247 goto fail_jhash_1word;
1253 rte_hash_free(handle);
1259 * add/delete key with jhash 2word
1262 test_hash_add_delete_jhash_2word(void)
1265 struct rte_hash *handle;
1268 hash_params_ex.name = "hash_test_jhash_2word";
1269 hash_params_ex.key_len = 8;
1270 hash_params_ex.hash_func = test_hash_jhash_2word;
1272 handle = rte_hash_create(&hash_params_ex);
1274 goto fail_jhash_2word;
1276 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1278 goto fail_jhash_2word;
1280 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1281 if (pos2 < 0 || pos1 != pos2)
1282 goto fail_jhash_2word;
1288 rte_hash_free(handle);
1294 * add/delete key with jhash 3word
1297 test_hash_add_delete_jhash_3word(void)
1300 struct rte_hash *handle;
1303 hash_params_ex.name = "hash_test_jhash_3word";
1304 hash_params_ex.key_len = 12;
1305 hash_params_ex.hash_func = test_hash_jhash_3word;
1307 handle = rte_hash_create(&hash_params_ex);
1309 goto fail_jhash_3word;
1311 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1313 goto fail_jhash_3word;
1315 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1316 if (pos2 < 0 || pos1 != pos2)
1317 goto fail_jhash_3word;
1323 rte_hash_free(handle);
1329 * Do all unit and performance tests.
1334 if (test_add_delete() < 0)
1336 if (test_hash_add_delete_jhash2() < 0)
1338 if (test_hash_add_delete_2_jhash2() < 0)
1340 if (test_hash_add_delete_jhash_1word() < 0)
1342 if (test_hash_add_delete_jhash_2word() < 0)
1344 if (test_hash_add_delete_jhash_3word() < 0)
1346 if (test_hash_find_existing() < 0)
1348 if (test_add_update_delete() < 0)
1350 if (test_five_keys() < 0)
1352 if (test_full_bucket() < 0)
1355 if (test_fbk_hash_find_existing() < 0)
1357 if (fbk_hash_unit_test() < 0)
1359 if (test_hash_creation_with_bad_parameters() < 0)
1361 if (test_hash_creation_with_good_parameters() < 0)
1364 run_hash_func_tests();
1369 static struct test_command hash_cmd = {
1370 .command = "hash_autotest",
1371 .callback = test_hash,
1373 REGISTER_TEST_COMMAND(hash_cmd);