4 * Copyright(c) 2010-2013 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.
41 #include <sys/queue.h>
43 #include <rte_common.h>
44 #include <rte_malloc.h>
45 #include <rte_cycles.h>
46 #include <rte_random.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
52 #include <rte_string_fns.h>
53 #include <cmdline_parse.h>
57 #ifdef RTE_LIBRTE_HASH
60 #include <rte_fbk_hash.h>
61 #include <rte_jhash.h>
63 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
64 #include <rte_hash_crc.h>
67 /*******************************************************************************
68 * Hash function performance test configuration section. Each performance test
69 * will be performed HASHTEST_ITERATIONS times.
71 * The five arrays below control what tests are performed. Every combination
72 * from the array entries is tested.
74 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
75 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
77 static rte_hash_function hashtest_funcs[] = {rte_jhash};
79 static uint32_t hashtest_initvals[] = {0};
80 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
81 /******************************************************************************/
82 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
85 * Check condition and return an error if true. Assumes that "handle" is the
86 * name of the hash structure pointer to be freed.
88 #define RETURN_IF_ERROR(cond, str, ...) do { \
90 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
91 if (handle) rte_hash_free(handle); \
96 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
98 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
99 if (handle) rte_fbk_hash_free(handle); \
104 /* 5-tuple key type */
111 } __attribute__((packed));
114 * Hash function that always returns the same value, to easily test what
115 * happens when a bucket is full.
117 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
118 __attribute__((unused)) uint32_t key_len,
119 __attribute__((unused)) uint32_t init_val)
125 * Print out result of unit test hash operation.
127 #if defined(UNIT_TEST_HASH_VERBOSE)
128 static void print_key_info(const char *msg, const struct flow_key *key,
131 uint8_t *p = (uint8_t *)key;
134 printf("%s key:0x", msg);
135 for (i = 0; i < sizeof(struct flow_key); i++) {
136 printf("%02X", p[i]);
138 printf(" @ pos %d\n", pos);
141 static void print_key_info(__attribute__((unused)) const char *msg,
142 __attribute__((unused)) const struct flow_key *key,
143 __attribute__((unused)) int32_t pos)
148 /* Keys used by unit test functions */
149 static struct flow_key keys[5] = { {
150 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
151 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
156 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
157 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
162 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
163 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
168 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
169 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
174 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
175 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
181 /* Parameters used for hash table in unit test functions. Name set later. */
182 static struct rte_hash_parameters ut_params = {
185 .key_len = sizeof(struct flow_key), /* 13 */
186 .hash_func = rte_jhash,
187 .hash_func_init_val = 0,
192 * Test a hash function.
194 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
197 static uint8_t key[RTE_HASH_KEY_LENGTH_MAX];
201 for (i = 0; i < key_len; i++)
202 key[i] = (uint8_t) rte_rand();
204 /* just to be on the safe side */
208 f(key, key_len, init_val);
212 * Test all hash functions.
214 static void run_hash_func_tests(void)
219 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
222 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
225 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
227 run_hash_func_test(hashtest_funcs[i],
228 hashtest_initvals[j],
229 hashtest_key_lens[k]);
236 * Basic sequence of operations for a single key:
242 static int test_add_delete(void)
244 struct rte_hash *handle;
245 /* test with standard add/lookup/delete functions */
246 int pos0, expectedPos0;
248 ut_params.name = "test1";
249 handle = rte_hash_create(&ut_params);
250 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
252 pos0 = rte_hash_add_key(handle, &keys[0]);
253 print_key_info("Add", &keys[0], pos0);
254 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
257 pos0 = rte_hash_lookup(handle, &keys[0]);
258 print_key_info("Lkp", &keys[0], pos0);
259 RETURN_IF_ERROR(pos0 != expectedPos0,
260 "failed to find key (pos0=%d)", pos0);
262 pos0 = rte_hash_del_key(handle, &keys[0]);
263 print_key_info("Del", &keys[0], pos0);
264 RETURN_IF_ERROR(pos0 != expectedPos0,
265 "failed to delete key (pos0=%d)", pos0);
267 pos0 = rte_hash_lookup(handle, &keys[0]);
268 print_key_info("Lkp", &keys[0], pos0);
269 RETURN_IF_ERROR(pos0 != -ENOENT,
270 "fail: found key after deleting! (pos0=%d)", pos0);
272 rte_hash_free(handle);
274 /* repeat test with precomputed hash functions */
275 hash_sig_t hash_value;
276 int pos1, expectedPos1;
278 handle = rte_hash_create(&ut_params);
279 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
281 hash_value = rte_hash_hash(handle, &keys[0]);
282 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
283 print_key_info("Add", &keys[0], pos1);
284 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
287 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
288 print_key_info("Lkp", &keys[0], pos1);
289 RETURN_IF_ERROR(pos1 != expectedPos1,
290 "failed to find key (pos1=%d)", pos1);
292 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
293 print_key_info("Del", &keys[0], pos1);
294 RETURN_IF_ERROR(pos1 != expectedPos1,
295 "failed to delete key (pos1=%d)", pos1);
297 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
298 print_key_info("Lkp", &keys[0], pos1);
299 RETURN_IF_ERROR(pos1 != -ENOENT,
300 "fail: found key after deleting! (pos1=%d)", pos1);
302 rte_hash_free(handle);
308 * Sequence of operations for a single key:
313 * - lookup: hit (updated data)
318 static int test_add_update_delete(void)
320 struct rte_hash *handle;
321 int pos0, expectedPos0;
323 ut_params.name = "test2";
324 handle = rte_hash_create(&ut_params);
325 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
327 pos0 = rte_hash_del_key(handle, &keys[0]);
328 print_key_info("Del", &keys[0], pos0);
329 RETURN_IF_ERROR(pos0 != -ENOENT,
330 "fail: found non-existent key (pos0=%d)", pos0);
332 pos0 = rte_hash_add_key(handle, &keys[0]);
333 print_key_info("Add", &keys[0], pos0);
334 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
337 pos0 = rte_hash_lookup(handle, &keys[0]);
338 print_key_info("Lkp", &keys[0], pos0);
339 RETURN_IF_ERROR(pos0 != expectedPos0,
340 "failed to find key (pos0=%d)", pos0);
342 pos0 = rte_hash_add_key(handle, &keys[0]);
343 print_key_info("Add", &keys[0], pos0);
344 RETURN_IF_ERROR(pos0 != expectedPos0,
345 "failed to re-add key (pos0=%d)", pos0);
347 pos0 = rte_hash_lookup(handle, &keys[0]);
348 print_key_info("Lkp", &keys[0], pos0);
349 RETURN_IF_ERROR(pos0 != expectedPos0,
350 "failed to find key (pos0=%d)", pos0);
352 pos0 = rte_hash_del_key(handle, &keys[0]);
353 print_key_info("Del", &keys[0], pos0);
354 RETURN_IF_ERROR(pos0 != expectedPos0,
355 "failed to delete key (pos0=%d)", pos0);
357 pos0 = rte_hash_del_key(handle, &keys[0]);
358 print_key_info("Del", &keys[0], pos0);
359 RETURN_IF_ERROR(pos0 != -ENOENT,
360 "fail: deleted already deleted key (pos0=%d)", pos0);
362 pos0 = rte_hash_lookup(handle, &keys[0]);
363 print_key_info("Lkp", &keys[0], pos0);
364 RETURN_IF_ERROR(pos0 != -ENOENT,
365 "fail: found key after deleting! (pos0=%d)", pos0);
367 rte_hash_free(handle);
372 * Sequence of operations for find existing hash table
375 * - find existing table: hit
376 * - find non-existing table: miss
379 static int test_hash_find_existing(void)
381 struct rte_hash *handle = NULL, *result = NULL;
383 /* Create hash table. */
384 ut_params.name = "hash_find_existing";
385 handle = rte_hash_create(&ut_params);
386 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
388 /* Try to find existing hash table */
389 result = rte_hash_find_existing("hash_find_existing");
390 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
392 /* Try to find non-existing hash table */
393 result = rte_hash_find_existing("hash_find_non_existing");
394 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
397 rte_hash_free(handle);
403 * Sequence of operations for 5 keys
406 * - add keys (update)
407 * - lookup keys: hit (updated data)
408 * - delete keys : hit
409 * - lookup keys: miss
411 static int test_five_keys(void)
413 struct rte_hash *handle;
414 const void *key_array[5] = {0};
420 ut_params.name = "test3";
421 handle = rte_hash_create(&ut_params);
422 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
425 for (i = 0; i < 5; i++) {
426 pos[i] = rte_hash_add_key(handle, &keys[i]);
427 print_key_info("Add", &keys[i], pos[i]);
428 RETURN_IF_ERROR(pos[i] < 0,
429 "failed to add key (pos[%u]=%d)", i, pos[i]);
430 expected_pos[i] = pos[i];
434 for(i = 0; i < 5; i++)
435 key_array[i] = &keys[i];
437 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
439 for(i = 0; i < 5; i++) {
440 print_key_info("Lkp", key_array[i], pos[i]);
441 RETURN_IF_ERROR(pos[i] != expected_pos[i],
442 "failed to find key (pos[%u]=%d)", i, pos[i]);
446 for (i = 0; i < 5; i++) {
447 pos[i] = rte_hash_add_key(handle, &keys[i]);
448 print_key_info("Add", &keys[i], pos[i]);
449 RETURN_IF_ERROR(pos[i] != expected_pos[i],
450 "failed to add key (pos[%u]=%d)", i, pos[i]);
454 for (i = 0; i < 5; i++) {
455 pos[i] = rte_hash_lookup(handle, &keys[i]);
456 print_key_info("Lkp", &keys[i], pos[i]);
457 RETURN_IF_ERROR(pos[i] != expected_pos[i],
458 "failed to find key (pos[%u]=%d)", i, pos[i]);
462 for (i = 0; i < 5; i++) {
463 pos[i] = rte_hash_del_key(handle, &keys[i]);
464 print_key_info("Del", &keys[i], pos[i]);
465 RETURN_IF_ERROR(pos[i] != expected_pos[i],
466 "failed to delete key (pos[%u]=%d)", i, pos[i]);
470 for (i = 0; i < 5; i++) {
471 pos[i] = rte_hash_lookup(handle, &keys[i]);
472 print_key_info("Lkp", &keys[i], pos[i]);
473 RETURN_IF_ERROR(pos[i] != -ENOENT,
474 "failed to find key (pos[%u]=%d)", i, pos[i]);
477 rte_hash_free(handle);
483 * Add keys to the same bucket until bucket full.
484 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
485 * first 4 successful, 5th unsuccessful
486 * - lookup the 5 keys: 4 hits, 1 miss
487 * - add the 5 keys again: 4 OK, one error as bucket is full
488 * - lookup the 5 keys: 4 hits (updated data), 1 miss
489 * - delete the 5 keys: 5 OK (even if the 5th is not in the table)
490 * - lookup the 5 keys: 5 misses
491 * - add the 5th key: OK
492 * - lookup the 5th key: hit
494 static int test_full_bucket(void)
496 struct rte_hash_parameters params_pseudo_hash = {
500 .key_len = sizeof(struct flow_key), /* 13 */
501 .hash_func = pseudo_hash,
502 .hash_func_init_val = 0,
505 struct rte_hash *handle;
510 handle = rte_hash_create(¶ms_pseudo_hash);
511 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
514 for (i = 0; i < 4; i++) {
515 pos[i] = rte_hash_add_key(handle, &keys[i]);
516 print_key_info("Add", &keys[i], pos[i]);
517 RETURN_IF_ERROR(pos[i] < 0,
518 "failed to add key (pos[%u]=%d)", i, pos[i]);
519 expected_pos[i] = pos[i];
521 /* This shouldn't work because the bucket is full */
522 pos[4] = rte_hash_add_key(handle, &keys[4]);
523 print_key_info("Add", &keys[4], pos[4]);
524 RETURN_IF_ERROR(pos[4] != -ENOSPC,
525 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
528 for (i = 0; i < 4; i++) {
529 pos[i] = rte_hash_lookup(handle, &keys[i]);
530 print_key_info("Lkp", &keys[i], pos[i]);
531 RETURN_IF_ERROR(pos[i] != expected_pos[i],
532 "failed to find key (pos[%u]=%d)", i, pos[i]);
534 pos[4] = rte_hash_lookup(handle, &keys[4]);
535 print_key_info("Lkp", &keys[4], pos[4]);
536 RETURN_IF_ERROR(pos[4] != -ENOENT,
537 "fail: found non-existent key (pos[4]=%d)", pos[4]);
540 for (i = 0; i < 4; i++) {
541 pos[i] = rte_hash_add_key(handle, &keys[i]);
542 print_key_info("Add", &keys[i], pos[i]);
543 RETURN_IF_ERROR(pos[i] != expected_pos[i],
544 "failed to add key (pos[%u]=%d)", i, pos[i]);
546 pos[4] = rte_hash_add_key(handle, &keys[4]);
547 print_key_info("Add", &keys[4], pos[4]);
548 RETURN_IF_ERROR(pos[4] != -ENOSPC,
549 "fail: added key to full bucket (pos[4]=%d)", pos[4]);
552 for (i = 0; i < 4; i++) {
553 pos[i] = rte_hash_lookup(handle, &keys[i]);
554 print_key_info("Lkp", &keys[i], pos[i]);
555 RETURN_IF_ERROR(pos[i] != expected_pos[i],
556 "failed to find key (pos[%u]=%d)", i, pos[i]);
558 pos[4] = rte_hash_lookup(handle, &keys[4]);
559 print_key_info("Lkp", &keys[4], pos[4]);
560 RETURN_IF_ERROR(pos[4] != -ENOENT,
561 "fail: found non-existent key (pos[4]=%d)", pos[4]);
563 /* Delete 1 key, check other keys are still found */
564 pos[1] = rte_hash_del_key(handle, &keys[1]);
565 print_key_info("Del", &keys[1], pos[1]);
566 RETURN_IF_ERROR(pos[1] != expected_pos[1],
567 "failed to delete key (pos[1]=%d)", pos[1]);
568 pos[3] = rte_hash_lookup(handle, &keys[3]);
569 print_key_info("Lkp", &keys[3], pos[3]);
570 RETURN_IF_ERROR(pos[3] != expected_pos[3],
571 "failed lookup after deleting key from same bucket "
572 "(pos[3]=%d)", pos[3]);
574 /* Go back to previous state */
575 pos[1] = rte_hash_add_key(handle, &keys[1]);
576 print_key_info("Add", &keys[1], pos[1]);
577 expected_pos[1] = pos[1];
578 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
581 for (i = 0; i < 4; i++) {
582 pos[i] = rte_hash_del_key(handle, &keys[i]);
583 print_key_info("Del", &keys[i], pos[i]);
584 RETURN_IF_ERROR(pos[i] != expected_pos[i],
585 "failed to delete key (pos[%u]=%d)", i, pos[i]);
587 pos[4] = rte_hash_del_key(handle, &keys[4]);
588 print_key_info("Del", &keys[4], pos[4]);
589 RETURN_IF_ERROR(pos[4] != -ENOENT,
590 "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
593 for (i = 0; i < 4; i++) {
594 pos[i] = rte_hash_lookup(handle, &keys[i]);
595 print_key_info("Lkp", &keys[i], pos[i]);
596 RETURN_IF_ERROR(pos[i] != -ENOENT,
597 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
600 /* Add and lookup the 5th key */
601 pos[4] = rte_hash_add_key(handle, &keys[4]);
602 print_key_info("Add", &keys[4], pos[4]);
603 RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
604 expected_pos[4] = pos[4];
605 pos[4] = rte_hash_lookup(handle, &keys[4]);
606 print_key_info("Lkp", &keys[4], pos[4]);
607 RETURN_IF_ERROR(pos[4] != expected_pos[4],
608 "failed to find key (pos[4]=%d)", pos[4]);
610 rte_hash_free(handle);
612 /* Cover the NULL case. */
617 /******************************************************************************/
619 fbk_hash_unit_test(void)
621 struct rte_fbk_hash_params params = {
622 .name = "fbk_hash_test",
623 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
624 .entries_per_bucket = 4,
628 struct rte_fbk_hash_params invalid_params_1 = {
630 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
631 .entries_per_bucket = 4,
635 struct rte_fbk_hash_params invalid_params_2 = {
638 .entries_per_bucket = 3, /* Not power of 2 */
642 struct rte_fbk_hash_params invalid_params_3 = {
644 .entries = 0, /* Entries is 0 */
645 .entries_per_bucket = 4,
649 struct rte_fbk_hash_params invalid_params_4 = {
651 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
652 .entries_per_bucket = 0, /* Entries per bucket is 0 */
656 struct rte_fbk_hash_params invalid_params_5 = {
659 .entries_per_bucket = 8, /* Entries per bucket > entries */
663 struct rte_fbk_hash_params invalid_params_6 = {
665 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
666 .entries_per_bucket = 4,
670 struct rte_fbk_hash_params invalid_params_7 = {
672 .entries = RTE_FBK_HASH_ENTRIES_MAX,
673 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
677 struct rte_fbk_hash_params invalid_params_8 = {
679 .entries = RTE_FBK_HASH_ENTRIES_MAX,
680 .entries_per_bucket = 4,
681 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
684 /* try to create two hashes with identical names
685 * in this case, trying to create a second one will not
686 * fail but will simply return pointer to the existing
687 * hash with that name. sort of like a "find hash by name" :-)
689 struct rte_fbk_hash_params invalid_params_same_name_1 = {
690 .name = "same_name", /* hash with identical name */
692 .entries_per_bucket = 2,
696 /* trying to create this hash should return a pointer to an existing hash */
697 struct rte_fbk_hash_params invalid_params_same_name_2 = {
698 .name = "same_name", /* hash with identical name */
699 .entries = RTE_FBK_HASH_ENTRIES_MAX,
700 .entries_per_bucket = 4,
704 /* this is a sanity check for "same name" test
705 * creating this hash will check if we are actually able to create
706 * multiple hashes with different names (instead of having just one).
708 struct rte_fbk_hash_params different_name = {
709 .name = "different_name", /* different name */
710 .entries = RTE_FBK_HASH_ENTRIES_MAX,
711 .entries_per_bucket = 4,
715 struct rte_fbk_hash_params params_jhash = {
717 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
718 .entries_per_bucket = 4,
720 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
721 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
724 struct rte_fbk_hash_params params_nohash = {
725 .name = "valid nohash",
726 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
727 .entries_per_bucket = 4,
729 .hash_func = NULL, /* Tests for null hash_func */
730 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
733 struct rte_fbk_hash_table *handle, *tmp;
735 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
736 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
741 /* Try creating hashes with invalid parameters */
742 printf("# Testing hash creation with invalid parameters "
743 "- expert error msgs\n");
744 handle = rte_fbk_hash_create(&invalid_params_1);
745 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
747 handle = rte_fbk_hash_create(&invalid_params_2);
748 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
750 handle = rte_fbk_hash_create(&invalid_params_3);
751 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
753 handle = rte_fbk_hash_create(&invalid_params_4);
754 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
756 handle = rte_fbk_hash_create(&invalid_params_5);
757 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
759 handle = rte_fbk_hash_create(&invalid_params_6);
760 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
762 handle = rte_fbk_hash_create(&invalid_params_7);
763 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
765 handle = rte_fbk_hash_create(&invalid_params_8);
766 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
768 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
769 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
771 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
772 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
774 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
775 rte_fbk_hash_free(handle);
776 rte_fbk_hash_free(tmp);
780 /* we are not freeing tmp or handle here because we need a hash list
781 * to be not empty for the next test */
783 /* create a hash in non-empty list - good for coverage */
784 tmp = rte_fbk_hash_create(&different_name);
785 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
787 /* free both hashes */
788 rte_fbk_hash_free(handle);
789 rte_fbk_hash_free(tmp);
791 /* Create empty jhash hash. */
792 handle = rte_fbk_hash_create(¶ms_jhash);
793 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
796 rte_fbk_hash_free(handle);
798 /* Create empty jhash hash. */
799 handle = rte_fbk_hash_create(¶ms_nohash);
800 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
803 rte_fbk_hash_free(handle);
805 /* Create empty hash. */
806 handle = rte_fbk_hash_create(¶ms);
807 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
809 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
810 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
811 "load factor right after creation is not zero but it should be");
813 for (i = 0; i < 5; i++) {
814 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
815 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
818 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
819 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
820 "load factor now is not as expected");
821 /* Find value of added keys. */
822 for (i = 0; i < 5; i++) {
823 status = rte_fbk_hash_lookup(handle, keys[i]);
824 RETURN_IF_ERROR_FBK(status != vals[i],
825 "fbk hash lookup failed");
828 /* Change value of added keys. */
829 for (i = 0; i < 5; i++) {
830 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
831 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
834 /* Find new values. */
835 for (i = 0; i < 5; i++) {
836 status = rte_fbk_hash_lookup(handle, keys[i]);
837 RETURN_IF_ERROR_FBK(status != vals[4-i],
838 "fbk hash lookup failed");
841 /* Delete keys individually. */
842 for (i = 0; i < 5; i++) {
843 status = rte_fbk_hash_delete_key(handle, keys[i]);
844 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
847 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
848 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
849 "load factor right after deletion is not zero but it should be");
850 /* Lookup should now fail. */
851 for (i = 0; i < 5; i++) {
852 status = rte_fbk_hash_lookup(handle, keys[i]);
853 RETURN_IF_ERROR_FBK(status == 0,
854 "fbk hash lookup should have failed");
857 /* Add keys again. */
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 /* Make sure they were added. */
864 for (i = 0; i < 5; i++) {
865 status = rte_fbk_hash_lookup(handle, keys[i]);
866 RETURN_IF_ERROR_FBK(status != vals[i],
867 "fbk hash lookup failed");
870 /* Clear all entries. */
871 rte_fbk_hash_clear_all(handle);
873 /* Lookup should fail. */
874 for (i = 0; i < 5; i++) {
875 status = rte_fbk_hash_lookup(handle, keys[i]);
876 RETURN_IF_ERROR_FBK(status == 0,
877 "fbk hash lookup should have failed");
882 /* fill up the hash_table */
883 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
884 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
886 /* Find non-existent key in a full hashtable */
887 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
888 RETURN_IF_ERROR_FBK(status != -ENOENT,
889 "fbk hash lookup succeeded");
891 /* Delete non-existent key in a full hashtable */
892 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
893 RETURN_IF_ERROR_FBK(status != -ENOENT,
894 "fbk hash delete succeeded");
896 /* Delete one key from a full hashtable */
897 status = rte_fbk_hash_delete_key(handle, 1);
898 RETURN_IF_ERROR_FBK(status != 0,
899 "fbk hash delete failed");
901 /* Clear all entries. */
902 rte_fbk_hash_clear_all(handle);
905 rte_fbk_hash_free(handle);
907 /* Cover the NULL case. */
908 rte_fbk_hash_free(0);
914 * Sequence of operations for find existing fbk hash table
917 * - find existing table: hit
918 * - find non-existing table: miss
921 static int test_fbk_hash_find_existing(void)
923 struct rte_fbk_hash_params params = {
924 .name = "fbk_hash_find_existing",
925 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
926 .entries_per_bucket = 4,
929 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
931 /* Create hash table. */
932 handle = rte_fbk_hash_create(¶ms);
933 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
935 /* Try to find existing fbk hash table */
936 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
937 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
939 /* Try to find non-existing fbk hash table */
940 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
941 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
944 rte_fbk_hash_free(handle);
950 * Do tests for hash creation with bad parameters.
952 static int test_hash_creation_with_bad_parameters(void)
954 struct rte_hash *handle;
955 struct rte_hash_parameters params;
957 handle = rte_hash_create(NULL);
958 if (handle != NULL) {
959 rte_hash_free(handle);
960 printf("Impossible creating hash sucessfully without any parameter\n");
964 memcpy(¶ms, &ut_params, sizeof(params));
965 params.name = "creation_with_bad_parameters_0";
966 params.entries = RTE_HASH_ENTRIES_MAX + 1;
967 handle = rte_hash_create(¶ms);
968 if (handle != NULL) {
969 rte_hash_free(handle);
970 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
974 memcpy(¶ms, &ut_params, sizeof(params));
975 params.name = "creation_with_bad_parameters_1";
976 params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
977 handle = rte_hash_create(¶ms);
978 if (handle != NULL) {
979 rte_hash_free(handle);
980 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
984 memcpy(¶ms, &ut_params, sizeof(params));
985 params.name = "creation_with_bad_parameters_2";
986 params.entries = params.bucket_entries - 1;
987 handle = rte_hash_create(¶ms);
988 if (handle != NULL) {
989 rte_hash_free(handle);
990 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
994 memcpy(¶ms, &ut_params, sizeof(params));
995 params.name = "creation_with_bad_parameters_3";
996 params.entries = params.entries - 1;
997 handle = rte_hash_create(¶ms);
998 if (handle != NULL) {
999 rte_hash_free(handle);
1000 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
1004 memcpy(¶ms, &ut_params, sizeof(params));
1005 params.name = "creation_with_bad_parameters_4";
1006 params.bucket_entries = params.bucket_entries - 1;
1007 handle = rte_hash_create(¶ms);
1008 if (handle != NULL) {
1009 rte_hash_free(handle);
1010 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
1014 memcpy(¶ms, &ut_params, sizeof(params));
1015 params.name = "creation_with_bad_parameters_5";
1017 handle = rte_hash_create(¶ms);
1018 if (handle != NULL) {
1019 rte_hash_free(handle);
1020 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1024 memcpy(¶ms, &ut_params, sizeof(params));
1025 params.name = "creation_with_bad_parameters_6";
1026 params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
1027 handle = rte_hash_create(¶ms);
1028 if (handle != NULL) {
1029 rte_hash_free(handle);
1030 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
1034 memcpy(¶ms, &ut_params, sizeof(params));
1035 params.name = "creation_with_bad_parameters_7";
1036 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1037 handle = rte_hash_create(¶ms);
1038 if (handle != NULL) {
1039 rte_hash_free(handle);
1040 printf("Impossible creating hash sucessfully with invalid socket\n");
1044 rte_hash_free(handle);
1050 * Do tests for hash creation with parameters that look incorrect
1051 * but are actually valid.
1054 test_hash_creation_with_good_parameters(void)
1056 struct rte_hash *handle, *tmp;
1057 struct rte_hash_parameters params;
1059 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1060 memcpy(¶ms, &ut_params, sizeof(params));
1061 params.name = "same_name";
1062 params.hash_func = NULL;
1063 handle = rte_hash_create(¶ms);
1064 if (handle == NULL) {
1065 printf("Creating hash with null hash_func failed\n");
1068 if (handle->hash_func == NULL) {
1069 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1073 /* this test is trying to create a hash with the same name as previous one.
1074 * this should return a pointer to the hash we previously created.
1075 * the previous hash isn't freed exactly for the purpose of it being in
1078 memcpy(¶ms, &ut_params, sizeof(params));
1079 params.name = "same_name";
1080 tmp = rte_hash_create(¶ms);
1082 /* check if the returned handle is actually equal to the previous hash */
1083 if (handle != tmp) {
1084 rte_hash_free(handle);
1086 printf("Creating hash with existing name was successful\n");
1090 /* try creating hash when there already are hashes in the list.
1091 * the previous hash is not freed to have a non-empty hash list.
1092 * the other hash that's in the list is still pointed to by "handle" var.
1094 memcpy(¶ms, &ut_params, sizeof(params));
1095 params.name = "different_name";
1096 tmp = rte_hash_create(¶ms);
1098 rte_hash_free(handle);
1099 printf("Creating hash with valid parameters failed\n");
1104 rte_hash_free(handle);
1109 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1110 0x04, 0x05, 0x06, 0x07,
1111 0x08, 0x09, 0x0a, 0x0b,
1112 0x0c, 0x0d, 0x0e, 0x0f};
1113 static struct rte_hash_parameters hash_params_ex = {
1116 .bucket_entries = 4,
1119 .hash_func_init_val = 0,
1124 * add/delete key with jhash2
1127 test_hash_add_delete_jhash2(void)
1130 struct rte_hash *handle;
1133 hash_params_ex.name = "hash_test_jhash2";
1134 hash_params_ex.key_len = 4;
1135 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1137 handle = rte_hash_create(&hash_params_ex);
1138 if (handle == NULL) {
1139 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1142 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1144 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1148 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1149 if (pos2 < 0 || pos1 != pos2) {
1150 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1157 rte_hash_free(handle);
1163 * add/delete (2) key with jhash2
1166 test_hash_add_delete_2_jhash2(void)
1169 struct rte_hash *handle;
1172 hash_params_ex.name = "hash_test_2_jhash2";
1173 hash_params_ex.key_len = 8;
1174 hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1176 handle = rte_hash_create(&hash_params_ex);
1180 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1184 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1185 if (pos2 < 0 || pos1 != pos2)
1192 rte_hash_free(handle);
1198 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1200 const uint32_t *k = key;
1202 RTE_SET_USED(length);
1204 return rte_jhash_1word(k[0], initval);
1208 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1210 const uint32_t *k = key;
1212 RTE_SET_USED(length);
1214 return rte_jhash_2words(k[0], k[1], initval);
1218 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1220 const uint32_t *k = key;
1222 RTE_SET_USED(length);
1224 return rte_jhash_3words(k[0], k[1], k[2], initval);
1228 * add/delete key with jhash 1word
1231 test_hash_add_delete_jhash_1word(void)
1234 struct rte_hash *handle;
1237 hash_params_ex.name = "hash_test_jhash_1word";
1238 hash_params_ex.key_len = 4;
1239 hash_params_ex.hash_func = test_hash_jhash_1word;
1241 handle = rte_hash_create(&hash_params_ex);
1243 goto fail_jhash_1word;
1245 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1247 goto fail_jhash_1word;
1249 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1250 if (pos2 < 0 || pos1 != pos2)
1251 goto fail_jhash_1word;
1257 rte_hash_free(handle);
1263 * add/delete key with jhash 2word
1266 test_hash_add_delete_jhash_2word(void)
1269 struct rte_hash *handle;
1272 hash_params_ex.name = "hash_test_jhash_2word";
1273 hash_params_ex.key_len = 8;
1274 hash_params_ex.hash_func = test_hash_jhash_2word;
1276 handle = rte_hash_create(&hash_params_ex);
1278 goto fail_jhash_2word;
1280 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1282 goto fail_jhash_2word;
1284 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1285 if (pos2 < 0 || pos1 != pos2)
1286 goto fail_jhash_2word;
1292 rte_hash_free(handle);
1298 * add/delete key with jhash 3word
1301 test_hash_add_delete_jhash_3word(void)
1304 struct rte_hash *handle;
1307 hash_params_ex.name = "hash_test_jhash_3word";
1308 hash_params_ex.key_len = 12;
1309 hash_params_ex.hash_func = test_hash_jhash_3word;
1311 handle = rte_hash_create(&hash_params_ex);
1313 goto fail_jhash_3word;
1315 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1317 goto fail_jhash_3word;
1319 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1320 if (pos2 < 0 || pos1 != pos2)
1321 goto fail_jhash_3word;
1327 rte_hash_free(handle);
1333 * Do all unit and performance tests.
1337 if (test_add_delete() < 0)
1339 if (test_hash_add_delete_jhash2() < 0)
1341 if (test_hash_add_delete_2_jhash2() < 0)
1343 if (test_hash_add_delete_jhash_1word() < 0)
1345 if (test_hash_add_delete_jhash_2word() < 0)
1347 if (test_hash_add_delete_jhash_3word() < 0)
1349 if (test_hash_find_existing() < 0)
1351 if (test_add_update_delete() < 0)
1353 if (test_five_keys() < 0)
1355 if (test_full_bucket() < 0)
1358 if (test_fbk_hash_find_existing() < 0)
1360 if (fbk_hash_unit_test() < 0)
1362 if (test_hash_creation_with_bad_parameters() < 0)
1364 if (test_hash_creation_with_good_parameters() < 0)
1367 run_hash_func_tests();
1371 #else /* RTE_LIBRTE_HASH */
1376 printf("The Hash library is not included in this build\n");
1380 #endif /* RTE_LIBRTE_HASH */