4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_malloc.h>
44 #include <rte_cycles.h>
45 #include <rte_random.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
50 #include <rte_string_fns.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57 #include <rte_hash_crc.h>
59 /*******************************************************************************
60 * Hash function performance test configuration section. Each performance test
61 * will be performed HASHTEST_ITERATIONS times.
63 * The five arrays below control what tests are performed. Every combination
64 * from the array entries is tested.
66 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
67 static uint32_t hashtest_initvals[] = {0};
68 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
69 #define MAX_KEYSIZE 64
70 /******************************************************************************/
71 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
74 * Check condition and return an error if true. Assumes that "handle" is the
75 * name of the hash structure pointer to be freed.
77 #define RETURN_IF_ERROR(cond, str, ...) do { \
79 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
80 if (handle) rte_hash_free(handle); \
85 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \
87 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
88 if (handle) rte_fbk_hash_free(handle); \
93 /* 5-tuple key type */
100 } __attribute__((packed));
103 * Hash function that always returns the same value, to easily test what
104 * happens when a bucket is full.
106 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
107 __attribute__((unused)) uint32_t key_len,
108 __attribute__((unused)) uint32_t init_val)
114 * Print out result of unit test hash operation.
116 #if defined(UNIT_TEST_HASH_VERBOSE)
117 static void print_key_info(const char *msg, const struct flow_key *key,
120 uint8_t *p = (uint8_t *)key;
123 printf("%s key:0x", msg);
124 for (i = 0; i < sizeof(struct flow_key); i++) {
125 printf("%02X", p[i]);
127 printf(" @ pos %d\n", pos);
130 static void print_key_info(__attribute__((unused)) const char *msg,
131 __attribute__((unused)) const struct flow_key *key,
132 __attribute__((unused)) int32_t pos)
137 /* Keys used by unit test functions */
138 static struct flow_key keys[5] = { {
139 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
140 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
145 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
146 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
151 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
152 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
157 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
158 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
163 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
164 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
170 /* Parameters used for hash table in unit test functions. Name set later. */
171 static struct rte_hash_parameters ut_params = {
173 .key_len = sizeof(struct flow_key), /* 13 */
174 .hash_func = rte_jhash,
175 .hash_func_init_val = 0,
179 #define CRC32_ITERATIONS (1U << 20)
180 #define CRC32_DWORDS (1U << 6)
182 * Test if all CRC32 implementations yield the same hash value
185 test_crc32_hash_alg_equiv(void)
189 uint64_t data64[CRC32_DWORDS];
193 printf("\n# CRC32 implementations equivalence test\n");
194 for (i = 0; i < CRC32_ITERATIONS; i++) {
195 /* Randomizing data_len of data set */
196 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
197 init_val = (uint32_t) rte_rand();
199 /* Fill the data set */
200 for (j = 0; j < CRC32_DWORDS; j++)
201 data64[j] = rte_rand();
203 /* Calculate software CRC32 */
204 rte_hash_crc_set_alg(CRC32_SW);
205 hash_val = rte_hash_crc(data64, data_len, init_val);
207 /* Check against 4-byte-operand sse4.2 CRC32 if available */
208 rte_hash_crc_set_alg(CRC32_SSE42);
209 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
210 printf("Failed checking CRC32_SW against CRC32_SSE42\n");
214 /* Check against 8-byte-operand sse4.2 CRC32 if available */
215 rte_hash_crc_set_alg(CRC32_SSE42_x64);
216 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
217 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
222 /* Resetting to best available algorithm */
223 rte_hash_crc_set_alg(CRC32_SSE42_x64);
225 if (i == CRC32_ITERATIONS)
228 printf("Failed test data (hex, %zu bytes total):\n", data_len);
229 for (j = 0; j < data_len; j++)
230 printf("%02X%c", ((uint8_t *)data64)[j],
231 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
237 * Test a hash function.
239 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
242 static uint8_t key[MAX_KEYSIZE];
246 for (i = 0; i < key_len; i++)
247 key[i] = (uint8_t) rte_rand();
249 /* just to be on the safe side */
253 f(key, key_len, init_val);
257 * Test all hash functions.
259 static void run_hash_func_tests(void)
264 i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
267 j < sizeof(hashtest_initvals) / sizeof(uint32_t);
270 k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
272 run_hash_func_test(hashtest_funcs[i],
273 hashtest_initvals[j],
274 hashtest_key_lens[k]);
281 * Basic sequence of operations for a single key:
287 static int test_add_delete(void)
289 struct rte_hash *handle;
290 /* test with standard add/lookup/delete functions */
291 int pos0, expectedPos0;
293 ut_params.name = "test1";
294 handle = rte_hash_create(&ut_params);
295 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
297 pos0 = rte_hash_add_key(handle, &keys[0]);
298 print_key_info("Add", &keys[0], pos0);
299 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
302 pos0 = rte_hash_lookup(handle, &keys[0]);
303 print_key_info("Lkp", &keys[0], pos0);
304 RETURN_IF_ERROR(pos0 != expectedPos0,
305 "failed to find key (pos0=%d)", pos0);
307 pos0 = rte_hash_del_key(handle, &keys[0]);
308 print_key_info("Del", &keys[0], pos0);
309 RETURN_IF_ERROR(pos0 != expectedPos0,
310 "failed to delete key (pos0=%d)", pos0);
312 pos0 = rte_hash_lookup(handle, &keys[0]);
313 print_key_info("Lkp", &keys[0], pos0);
314 RETURN_IF_ERROR(pos0 != -ENOENT,
315 "fail: found key after deleting! (pos0=%d)", pos0);
317 rte_hash_free(handle);
319 /* repeat test with precomputed hash functions */
320 hash_sig_t hash_value;
321 int pos1, expectedPos1;
323 handle = rte_hash_create(&ut_params);
324 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
326 hash_value = rte_hash_hash(handle, &keys[0]);
327 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
328 print_key_info("Add", &keys[0], pos1);
329 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
332 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
333 print_key_info("Lkp", &keys[0], pos1);
334 RETURN_IF_ERROR(pos1 != expectedPos1,
335 "failed to find key (pos1=%d)", pos1);
337 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
338 print_key_info("Del", &keys[0], pos1);
339 RETURN_IF_ERROR(pos1 != expectedPos1,
340 "failed to delete key (pos1=%d)", pos1);
342 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
343 print_key_info("Lkp", &keys[0], pos1);
344 RETURN_IF_ERROR(pos1 != -ENOENT,
345 "fail: found key after deleting! (pos1=%d)", pos1);
347 rte_hash_free(handle);
353 * Sequence of operations for a single key:
358 * - lookup: hit (updated data)
363 static int test_add_update_delete(void)
365 struct rte_hash *handle;
366 int pos0, expectedPos0;
368 ut_params.name = "test2";
369 handle = rte_hash_create(&ut_params);
370 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
372 pos0 = rte_hash_del_key(handle, &keys[0]);
373 print_key_info("Del", &keys[0], pos0);
374 RETURN_IF_ERROR(pos0 != -ENOENT,
375 "fail: found non-existent key (pos0=%d)", pos0);
377 pos0 = rte_hash_add_key(handle, &keys[0]);
378 print_key_info("Add", &keys[0], pos0);
379 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
382 pos0 = rte_hash_lookup(handle, &keys[0]);
383 print_key_info("Lkp", &keys[0], pos0);
384 RETURN_IF_ERROR(pos0 != expectedPos0,
385 "failed to find key (pos0=%d)", pos0);
387 pos0 = rte_hash_add_key(handle, &keys[0]);
388 print_key_info("Add", &keys[0], pos0);
389 RETURN_IF_ERROR(pos0 != expectedPos0,
390 "failed to re-add key (pos0=%d)", pos0);
392 pos0 = rte_hash_lookup(handle, &keys[0]);
393 print_key_info("Lkp", &keys[0], pos0);
394 RETURN_IF_ERROR(pos0 != expectedPos0,
395 "failed to find key (pos0=%d)", pos0);
397 pos0 = rte_hash_del_key(handle, &keys[0]);
398 print_key_info("Del", &keys[0], pos0);
399 RETURN_IF_ERROR(pos0 != expectedPos0,
400 "failed to delete key (pos0=%d)", pos0);
402 pos0 = rte_hash_del_key(handle, &keys[0]);
403 print_key_info("Del", &keys[0], pos0);
404 RETURN_IF_ERROR(pos0 != -ENOENT,
405 "fail: deleted already deleted key (pos0=%d)", pos0);
407 pos0 = rte_hash_lookup(handle, &keys[0]);
408 print_key_info("Lkp", &keys[0], pos0);
409 RETURN_IF_ERROR(pos0 != -ENOENT,
410 "fail: found key after deleting! (pos0=%d)", pos0);
412 rte_hash_free(handle);
417 * Sequence of operations for find existing hash table
420 * - find existing table: hit
421 * - find non-existing table: miss
424 static int test_hash_find_existing(void)
426 struct rte_hash *handle = NULL, *result = NULL;
428 /* Create hash table. */
429 ut_params.name = "hash_find_existing";
430 handle = rte_hash_create(&ut_params);
431 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
433 /* Try to find existing hash table */
434 result = rte_hash_find_existing("hash_find_existing");
435 RETURN_IF_ERROR(result != handle, "could not find existing hash table");
437 /* Try to find non-existing hash table */
438 result = rte_hash_find_existing("hash_find_non_existing");
439 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
442 rte_hash_free(handle);
448 * Sequence of operations for 5 keys
451 * - add keys (update)
452 * - lookup keys: hit (updated data)
453 * - delete keys : hit
454 * - lookup keys: miss
456 static int test_five_keys(void)
458 struct rte_hash *handle;
459 const void *key_array[5] = {0};
465 ut_params.name = "test3";
466 handle = rte_hash_create(&ut_params);
467 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
470 for (i = 0; i < 5; i++) {
471 pos[i] = rte_hash_add_key(handle, &keys[i]);
472 print_key_info("Add", &keys[i], pos[i]);
473 RETURN_IF_ERROR(pos[i] < 0,
474 "failed to add key (pos[%u]=%d)", i, pos[i]);
475 expected_pos[i] = pos[i];
479 for(i = 0; i < 5; i++)
480 key_array[i] = &keys[i];
482 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
484 for(i = 0; i < 5; i++) {
485 print_key_info("Lkp", key_array[i], pos[i]);
486 RETURN_IF_ERROR(pos[i] != expected_pos[i],
487 "failed to find key (pos[%u]=%d)", i, pos[i]);
491 for (i = 0; i < 5; i++) {
492 pos[i] = rte_hash_add_key(handle, &keys[i]);
493 print_key_info("Add", &keys[i], pos[i]);
494 RETURN_IF_ERROR(pos[i] != expected_pos[i],
495 "failed to add key (pos[%u]=%d)", i, pos[i]);
499 for (i = 0; i < 5; i++) {
500 pos[i] = rte_hash_lookup(handle, &keys[i]);
501 print_key_info("Lkp", &keys[i], pos[i]);
502 RETURN_IF_ERROR(pos[i] != expected_pos[i],
503 "failed to find key (pos[%u]=%d)", i, pos[i]);
507 for (i = 0; i < 5; i++) {
508 pos[i] = rte_hash_del_key(handle, &keys[i]);
509 print_key_info("Del", &keys[i], pos[i]);
510 RETURN_IF_ERROR(pos[i] != expected_pos[i],
511 "failed to delete key (pos[%u]=%d)", i, pos[i]);
515 for (i = 0; i < 5; i++) {
516 pos[i] = rte_hash_lookup(handle, &keys[i]);
517 print_key_info("Lkp", &keys[i], pos[i]);
518 RETURN_IF_ERROR(pos[i] != -ENOENT,
519 "found non-existent key (pos[%u]=%d)", i, pos[i]);
523 ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
525 for (i = 0; i < 5; i++) {
526 print_key_info("Lkp", key_array[i], pos[i]);
527 RETURN_IF_ERROR(pos[i] != -ENOENT,
528 "found not-existent key (pos[%u]=%d)", i, pos[i]);
531 rte_hash_free(handle);
537 * Add keys to the same bucket until bucket full.
538 * - add 5 keys to the same bucket (hash created with 4 keys per bucket):
539 * first 4 successful, 5th successful, pushing existing item in bucket
540 * - lookup the 5 keys: 5 hits
541 * - add the 5 keys again: 5 OK
542 * - lookup the 5 keys: 5 hits (updated data)
543 * - delete the 5 keys: 5 OK
544 * - lookup the 5 keys: 5 misses
546 static int test_full_bucket(void)
548 struct rte_hash_parameters params_pseudo_hash = {
551 .key_len = sizeof(struct flow_key), /* 13 */
552 .hash_func = pseudo_hash,
553 .hash_func_init_val = 0,
556 struct rte_hash *handle;
561 handle = rte_hash_create(¶ms_pseudo_hash);
562 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
565 for (i = 0; i < 4; i++) {
566 pos[i] = rte_hash_add_key(handle, &keys[i]);
567 print_key_info("Add", &keys[i], pos[i]);
568 RETURN_IF_ERROR(pos[i] < 0,
569 "failed to add key (pos[%u]=%d)", i, pos[i]);
570 expected_pos[i] = pos[i];
573 * This should work and will push one of the items
574 * in the bucket because it is full
576 pos[4] = rte_hash_add_key(handle, &keys[4]);
577 print_key_info("Add", &keys[4], pos[4]);
578 RETURN_IF_ERROR(pos[4] < 0,
579 "failed to add key (pos[4]=%d)", pos[4]);
580 expected_pos[4] = pos[4];
583 for (i = 0; i < 5; i++) {
584 pos[i] = rte_hash_lookup(handle, &keys[i]);
585 print_key_info("Lkp", &keys[i], pos[i]);
586 RETURN_IF_ERROR(pos[i] != expected_pos[i],
587 "failed to find key (pos[%u]=%d)", i, pos[i]);
591 for (i = 0; i < 5; i++) {
592 pos[i] = rte_hash_add_key(handle, &keys[i]);
593 print_key_info("Add", &keys[i], pos[i]);
594 RETURN_IF_ERROR(pos[i] != expected_pos[i],
595 "failed to add key (pos[%u]=%d)", i, pos[i]);
599 for (i = 0; i < 5; i++) {
600 pos[i] = rte_hash_lookup(handle, &keys[i]);
601 print_key_info("Lkp", &keys[i], pos[i]);
602 RETURN_IF_ERROR(pos[i] != expected_pos[i],
603 "failed to find key (pos[%u]=%d)", i, pos[i]);
606 /* Delete 1 key, check other keys are still found */
607 pos[1] = rte_hash_del_key(handle, &keys[1]);
608 print_key_info("Del", &keys[1], pos[1]);
609 RETURN_IF_ERROR(pos[1] != expected_pos[1],
610 "failed to delete key (pos[1]=%d)", pos[1]);
611 pos[3] = rte_hash_lookup(handle, &keys[3]);
612 print_key_info("Lkp", &keys[3], pos[3]);
613 RETURN_IF_ERROR(pos[3] != expected_pos[3],
614 "failed lookup after deleting key from same bucket "
615 "(pos[3]=%d)", pos[3]);
617 /* Go back to previous state */
618 pos[1] = rte_hash_add_key(handle, &keys[1]);
619 print_key_info("Add", &keys[1], pos[1]);
620 expected_pos[1] = pos[1];
621 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
624 for (i = 0; i < 5; i++) {
625 pos[i] = rte_hash_del_key(handle, &keys[i]);
626 print_key_info("Del", &keys[i], pos[i]);
627 RETURN_IF_ERROR(pos[i] != expected_pos[i],
628 "failed to delete key (pos[%u]=%d)", i, pos[i]);
632 for (i = 0; i < 5; i++) {
633 pos[i] = rte_hash_lookup(handle, &keys[i]);
634 print_key_info("Lkp", &keys[i], pos[i]);
635 RETURN_IF_ERROR(pos[i] != -ENOENT,
636 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
639 rte_hash_free(handle);
641 /* Cover the NULL case. */
646 /******************************************************************************/
648 fbk_hash_unit_test(void)
650 struct rte_fbk_hash_params params = {
651 .name = "fbk_hash_test",
652 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
653 .entries_per_bucket = 4,
657 struct rte_fbk_hash_params invalid_params_1 = {
659 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
660 .entries_per_bucket = 4,
664 struct rte_fbk_hash_params invalid_params_2 = {
667 .entries_per_bucket = 3, /* Not power of 2 */
671 struct rte_fbk_hash_params invalid_params_3 = {
673 .entries = 0, /* Entries is 0 */
674 .entries_per_bucket = 4,
678 struct rte_fbk_hash_params invalid_params_4 = {
680 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
681 .entries_per_bucket = 0, /* Entries per bucket is 0 */
685 struct rte_fbk_hash_params invalid_params_5 = {
688 .entries_per_bucket = 8, /* Entries per bucket > entries */
692 struct rte_fbk_hash_params invalid_params_6 = {
694 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */
695 .entries_per_bucket = 4,
699 struct rte_fbk_hash_params invalid_params_7 = {
701 .entries = RTE_FBK_HASH_ENTRIES_MAX,
702 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */
706 struct rte_fbk_hash_params invalid_params_8 = {
708 .entries = RTE_FBK_HASH_ENTRIES_MAX,
709 .entries_per_bucket = 4,
710 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
713 /* try to create two hashes with identical names
714 * in this case, trying to create a second one will not
715 * fail but will simply return pointer to the existing
716 * hash with that name. sort of like a "find hash by name" :-)
718 struct rte_fbk_hash_params invalid_params_same_name_1 = {
719 .name = "same_name", /* hash with identical name */
721 .entries_per_bucket = 2,
725 /* trying to create this hash should return a pointer to an existing hash */
726 struct rte_fbk_hash_params invalid_params_same_name_2 = {
727 .name = "same_name", /* hash with identical name */
728 .entries = RTE_FBK_HASH_ENTRIES_MAX,
729 .entries_per_bucket = 4,
733 /* this is a sanity check for "same name" test
734 * creating this hash will check if we are actually able to create
735 * multiple hashes with different names (instead of having just one).
737 struct rte_fbk_hash_params different_name = {
738 .name = "different_name", /* different name */
739 .entries = RTE_FBK_HASH_ENTRIES_MAX,
740 .entries_per_bucket = 4,
744 struct rte_fbk_hash_params params_jhash = {
746 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
747 .entries_per_bucket = 4,
749 .hash_func = rte_jhash_1word, /* Tests for different hash_func */
750 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
753 struct rte_fbk_hash_params params_nohash = {
754 .name = "valid nohash",
755 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
756 .entries_per_bucket = 4,
758 .hash_func = NULL, /* Tests for null hash_func */
759 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
762 struct rte_fbk_hash_table *handle, *tmp;
764 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
765 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
770 /* Try creating hashes with invalid parameters */
771 printf("# Testing hash creation with invalid parameters "
772 "- expect error msgs\n");
773 handle = rte_fbk_hash_create(&invalid_params_1);
774 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
776 handle = rte_fbk_hash_create(&invalid_params_2);
777 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
779 handle = rte_fbk_hash_create(&invalid_params_3);
780 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
782 handle = rte_fbk_hash_create(&invalid_params_4);
783 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
785 handle = rte_fbk_hash_create(&invalid_params_5);
786 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
788 handle = rte_fbk_hash_create(&invalid_params_6);
789 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
791 handle = rte_fbk_hash_create(&invalid_params_7);
792 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
794 handle = rte_fbk_hash_create(&invalid_params_8);
795 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
797 handle = rte_fbk_hash_create(&invalid_params_same_name_1);
798 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
800 tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
801 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
803 printf("ERROR line %d: hashes should have been the same\n", __LINE__);
804 rte_fbk_hash_free(handle);
805 rte_fbk_hash_free(tmp);
809 /* we are not freeing tmp or handle here because we need a hash list
810 * to be not empty for the next test */
812 /* create a hash in non-empty list - good for coverage */
813 tmp = rte_fbk_hash_create(&different_name);
814 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
816 /* free both hashes */
817 rte_fbk_hash_free(handle);
818 rte_fbk_hash_free(tmp);
820 /* Create empty jhash hash. */
821 handle = rte_fbk_hash_create(¶ms_jhash);
822 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
825 rte_fbk_hash_free(handle);
827 /* Create empty jhash hash. */
828 handle = rte_fbk_hash_create(¶ms_nohash);
829 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
832 rte_fbk_hash_free(handle);
834 /* Create empty hash. */
835 handle = rte_fbk_hash_create(¶ms);
836 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
838 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
839 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
840 "load factor right after creation is not zero but it should be");
842 for (i = 0; i < 5; i++) {
843 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
844 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
847 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
848 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
849 "load factor now is not as expected");
850 /* Find value of added keys. */
851 for (i = 0; i < 5; i++) {
852 status = rte_fbk_hash_lookup(handle, keys[i]);
853 RETURN_IF_ERROR_FBK(status != vals[i],
854 "fbk hash lookup failed");
857 /* Change value of added keys. */
858 for (i = 0; i < 5; i++) {
859 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
860 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
863 /* Find new values. */
864 for (i = 0; i < 5; i++) {
865 status = rte_fbk_hash_lookup(handle, keys[i]);
866 RETURN_IF_ERROR_FBK(status != vals[4-i],
867 "fbk hash lookup failed");
870 /* Delete keys individually. */
871 for (i = 0; i < 5; i++) {
872 status = rte_fbk_hash_delete_key(handle, keys[i]);
873 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
876 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
877 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
878 "load factor right after deletion is not zero but it should be");
879 /* Lookup should now fail. */
880 for (i = 0; i < 5; i++) {
881 status = rte_fbk_hash_lookup(handle, keys[i]);
882 RETURN_IF_ERROR_FBK(status == 0,
883 "fbk hash lookup should have failed");
886 /* Add keys again. */
887 for (i = 0; i < 5; i++) {
888 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
889 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
892 /* Make sure they were added. */
893 for (i = 0; i < 5; i++) {
894 status = rte_fbk_hash_lookup(handle, keys[i]);
895 RETURN_IF_ERROR_FBK(status != vals[i],
896 "fbk hash lookup failed");
899 /* Clear all entries. */
900 rte_fbk_hash_clear_all(handle);
902 /* Lookup should fail. */
903 for (i = 0; i < 5; i++) {
904 status = rte_fbk_hash_lookup(handle, keys[i]);
905 RETURN_IF_ERROR_FBK(status == 0,
906 "fbk hash lookup should have failed");
911 /* fill up the hash_table */
912 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
913 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
915 /* Find non-existent key in a full hashtable */
916 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
917 RETURN_IF_ERROR_FBK(status != -ENOENT,
918 "fbk hash lookup succeeded");
920 /* Delete non-existent key in a full hashtable */
921 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
922 RETURN_IF_ERROR_FBK(status != -ENOENT,
923 "fbk hash delete succeeded");
925 /* Delete one key from a full hashtable */
926 status = rte_fbk_hash_delete_key(handle, 1);
927 RETURN_IF_ERROR_FBK(status != 0,
928 "fbk hash delete failed");
930 /* Clear all entries. */
931 rte_fbk_hash_clear_all(handle);
934 rte_fbk_hash_free(handle);
936 /* Cover the NULL case. */
937 rte_fbk_hash_free(0);
943 * Sequence of operations for find existing fbk hash table
946 * - find existing table: hit
947 * - find non-existing table: miss
950 static int test_fbk_hash_find_existing(void)
952 struct rte_fbk_hash_params params = {
953 .name = "fbk_hash_find_existing",
954 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
955 .entries_per_bucket = 4,
958 struct rte_fbk_hash_table *handle = NULL, *result = NULL;
960 /* Create hash table. */
961 handle = rte_fbk_hash_create(¶ms);
962 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
964 /* Try to find existing fbk hash table */
965 result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
966 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
968 /* Try to find non-existing fbk hash table */
969 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
970 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
973 rte_fbk_hash_free(handle);
978 #define BUCKET_ENTRIES 4
980 * Do tests for hash creation with bad parameters.
982 static int test_hash_creation_with_bad_parameters(void)
984 struct rte_hash *handle;
985 struct rte_hash_parameters params;
987 handle = rte_hash_create(NULL);
988 if (handle != NULL) {
989 rte_hash_free(handle);
990 printf("Impossible creating hash sucessfully without any parameter\n");
994 memcpy(¶ms, &ut_params, sizeof(params));
995 params.name = "creation_with_bad_parameters_0";
996 params.entries = RTE_HASH_ENTRIES_MAX + 1;
997 handle = rte_hash_create(¶ms);
998 if (handle != NULL) {
999 rte_hash_free(handle);
1000 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1004 memcpy(¶ms, &ut_params, sizeof(params));
1005 params.name = "creation_with_bad_parameters_2";
1006 params.entries = 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 entries less than bucket_entries in parameter\n");
1014 memcpy(¶ms, &ut_params, sizeof(params));
1015 params.name = "creation_with_bad_parameters_3";
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_4";
1026 params.socket_id = RTE_MAX_NUMA_NODES + 1;
1027 handle = rte_hash_create(¶ms);
1028 if (handle != NULL) {
1029 rte_hash_free(handle);
1030 printf("Impossible creating hash sucessfully with invalid socket\n");
1034 rte_hash_free(handle);
1035 printf("# Test successful. No more errors expected\n");
1041 * Do tests for hash creation with parameters that look incorrect
1042 * but are actually valid.
1045 test_hash_creation_with_good_parameters(void)
1047 struct rte_hash *handle, *tmp;
1048 struct rte_hash_parameters params;
1050 /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1051 memcpy(¶ms, &ut_params, sizeof(params));
1052 params.name = "same_name";
1053 params.hash_func = NULL;
1054 handle = rte_hash_create(¶ms);
1055 if (handle == NULL) {
1056 printf("Creating hash with null hash_func failed\n");
1060 /* this test is trying to create a hash with the same name as previous one.
1061 * this should return a pointer to the hash we previously created.
1062 * the previous hash isn't freed exactly for the purpose of it being in
1065 memcpy(¶ms, &ut_params, sizeof(params));
1066 params.name = "same_name";
1067 tmp = rte_hash_create(¶ms);
1069 /* check if the returned handle is actually equal to the previous hash */
1070 if (handle != tmp) {
1071 rte_hash_free(handle);
1073 printf("Creating hash with existing name was successful\n");
1077 /* try creating hash when there already are hashes in the list.
1078 * the previous hash is not freed to have a non-empty hash list.
1079 * the other hash that's in the list is still pointed to by "handle" var.
1081 memcpy(¶ms, &ut_params, sizeof(params));
1082 params.name = "different_name";
1083 tmp = rte_hash_create(¶ms);
1085 rte_hash_free(handle);
1086 printf("Creating hash with valid parameters failed\n");
1091 rte_hash_free(handle);
1096 #define ITERATIONS 50
1098 * Test to see the average table utilization (entries added/max entries)
1099 * before hitting a random entry that cannot be added
1101 static int test_average_table_utilization(void)
1103 struct rte_hash *handle;
1104 uint8_t simple_key[MAX_KEYSIZE];
1106 unsigned added_keys, average_keys_added = 0;
1109 printf("\n# Running test to determine average utilization"
1110 "\n before adding elements begins to fail\n");
1111 printf("Measuring performance, please wait");
1113 ut_params.entries = 1 << 20;
1114 ut_params.name = "test_average_utilization";
1115 ut_params.hash_func = rte_jhash;
1116 handle = rte_hash_create(&ut_params);
1117 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1119 for (j = 0; j < ITERATIONS; j++) {
1121 /* Add random entries until key cannot be added */
1122 for (added_keys = 0; ret >= 0; added_keys++) {
1123 for (i = 0; i < ut_params.key_len; i++)
1124 simple_key[i] = rte_rand() % 255;
1125 ret = rte_hash_add_key(handle, simple_key);
1127 if (ret != -ENOSPC) {
1128 printf("Unexpected error when adding keys\n");
1129 rte_hash_free(handle);
1133 average_keys_added += added_keys;
1135 /* Reset the table */
1136 rte_hash_reset(handle);
1138 /* Print a dot to show progress on operations */
1143 average_keys_added /= ITERATIONS;
1145 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1146 ((double) average_keys_added / ut_params.entries * 100),
1147 average_keys_added, ut_params.entries);
1148 rte_hash_free(handle);
1153 #define NUM_ENTRIES 1024
1154 static int test_hash_iteration(void)
1156 struct rte_hash *handle;
1158 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1159 const void *next_key;
1161 void *data[NUM_ENTRIES];
1162 unsigned added_keys;
1166 ut_params.entries = NUM_ENTRIES;
1167 ut_params.name = "test_hash_iteration";
1168 ut_params.hash_func = rte_jhash;
1169 ut_params.key_len = 16;
1170 handle = rte_hash_create(&ut_params);
1171 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1173 /* Add random entries until key cannot be added */
1174 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1175 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1176 for (i = 0; i < ut_params.key_len; i++)
1177 keys[added_keys][i] = rte_rand() % 255;
1178 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1183 /* Iterate through the hash table */
1184 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1185 /* Search for the key in the list of keys added */
1186 for (i = 0; i < NUM_ENTRIES; i++) {
1187 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1188 if (next_data != data[i]) {
1189 printf("Data found in the hash table is"
1190 "not the data added with the key\n");
1197 if (i == NUM_ENTRIES) {
1198 printf("Key found in the hash table was not added\n");
1203 /* Check if all keys have been iterated */
1204 if (added_keys != 0) {
1205 printf("There were still %u keys to iterate\n", added_keys);
1209 rte_hash_free(handle);
1213 rte_hash_free(handle);
1217 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1218 0x04, 0x05, 0x06, 0x07,
1219 0x08, 0x09, 0x0a, 0x0b,
1220 0x0c, 0x0d, 0x0e, 0x0f};
1221 static struct rte_hash_parameters hash_params_ex = {
1226 .hash_func_init_val = 0,
1231 * add/delete key with jhash2
1234 test_hash_add_delete_jhash2(void)
1237 struct rte_hash *handle;
1240 hash_params_ex.name = "hash_test_jhash2";
1241 hash_params_ex.key_len = 4;
1242 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1244 handle = rte_hash_create(&hash_params_ex);
1245 if (handle == NULL) {
1246 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1249 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1251 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1255 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1256 if (pos2 < 0 || pos1 != pos2) {
1257 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1264 rte_hash_free(handle);
1270 * add/delete (2) key with jhash2
1273 test_hash_add_delete_2_jhash2(void)
1276 struct rte_hash *handle;
1279 hash_params_ex.name = "hash_test_2_jhash2";
1280 hash_params_ex.key_len = 8;
1281 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1283 handle = rte_hash_create(&hash_params_ex);
1287 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1291 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1292 if (pos2 < 0 || pos1 != pos2)
1299 rte_hash_free(handle);
1305 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1307 const uint32_t *k = key;
1309 RTE_SET_USED(length);
1311 return rte_jhash_1word(k[0], initval);
1315 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1317 const uint32_t *k = key;
1319 RTE_SET_USED(length);
1321 return rte_jhash_2words(k[0], k[1], initval);
1325 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1327 const uint32_t *k = key;
1329 RTE_SET_USED(length);
1331 return rte_jhash_3words(k[0], k[1], k[2], initval);
1335 * add/delete key with jhash 1word
1338 test_hash_add_delete_jhash_1word(void)
1341 struct rte_hash *handle;
1344 hash_params_ex.name = "hash_test_jhash_1word";
1345 hash_params_ex.key_len = 4;
1346 hash_params_ex.hash_func = test_hash_jhash_1word;
1348 handle = rte_hash_create(&hash_params_ex);
1350 goto fail_jhash_1word;
1352 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1354 goto fail_jhash_1word;
1356 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1357 if (pos2 < 0 || pos1 != pos2)
1358 goto fail_jhash_1word;
1364 rte_hash_free(handle);
1370 * add/delete key with jhash 2word
1373 test_hash_add_delete_jhash_2word(void)
1376 struct rte_hash *handle;
1379 hash_params_ex.name = "hash_test_jhash_2word";
1380 hash_params_ex.key_len = 8;
1381 hash_params_ex.hash_func = test_hash_jhash_2word;
1383 handle = rte_hash_create(&hash_params_ex);
1385 goto fail_jhash_2word;
1387 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1389 goto fail_jhash_2word;
1391 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1392 if (pos2 < 0 || pos1 != pos2)
1393 goto fail_jhash_2word;
1399 rte_hash_free(handle);
1405 * add/delete key with jhash 3word
1408 test_hash_add_delete_jhash_3word(void)
1411 struct rte_hash *handle;
1414 hash_params_ex.name = "hash_test_jhash_3word";
1415 hash_params_ex.key_len = 12;
1416 hash_params_ex.hash_func = test_hash_jhash_3word;
1418 handle = rte_hash_create(&hash_params_ex);
1420 goto fail_jhash_3word;
1422 pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1424 goto fail_jhash_3word;
1426 pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1427 if (pos2 < 0 || pos1 != pos2)
1428 goto fail_jhash_3word;
1434 rte_hash_free(handle);
1440 * Do all unit and performance tests.
1445 if (test_add_delete() < 0)
1447 if (test_hash_add_delete_jhash2() < 0)
1449 if (test_hash_add_delete_2_jhash2() < 0)
1451 if (test_hash_add_delete_jhash_1word() < 0)
1453 if (test_hash_add_delete_jhash_2word() < 0)
1455 if (test_hash_add_delete_jhash_3word() < 0)
1457 if (test_hash_find_existing() < 0)
1459 if (test_add_update_delete() < 0)
1461 if (test_five_keys() < 0)
1463 if (test_full_bucket() < 0)
1466 if (test_fbk_hash_find_existing() < 0)
1468 if (fbk_hash_unit_test() < 0)
1470 if (test_hash_creation_with_bad_parameters() < 0)
1472 if (test_hash_creation_with_good_parameters() < 0)
1474 if (test_average_table_utilization() < 0)
1476 if (test_hash_iteration() < 0)
1479 run_hash_func_tests();
1481 if (test_crc32_hash_alg_equiv() < 0)
1487 static struct test_command hash_cmd = {
1488 .command = "hash_autotest",
1489 .callback = test_hash,
1491 REGISTER_TEST_COMMAND(hash_cmd);