1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
11 #include <sys/queue.h>
13 #include <rte_cycles.h>
14 #include <rte_random.h>
16 #include <rte_jhash.h>
17 #include <rte_hash_crc.h>
22 * Hash values calculated for key sizes from array "hashtest_key_lens"
23 * and for initial values from array "hashtest_initvals.
24 * Each key will be formed by increasing each byte by 1:
25 * e.g.: key size = 4, key = 0x03020100
26 * key size = 8, key = 0x0706050403020100
28 static uint32_t hash_values_jhash[2][12] = {{
29 0x8ba9414b, 0xdf0d39c9,
30 0xe4cf1d42, 0xd4ccb93c, 0x5e84eafc, 0x21362cfe,
31 0x2f4775ab, 0x9ff036cc, 0xeca51474, 0xbc9d6816,
32 0x12926a31, 0x1c9fa888
35 0x5c62c303, 0x1b8cf784,
36 0x8270ac65, 0x05fa6668, 0x762df861, 0xda088f2f,
37 0x59614cd4, 0x7a94f690, 0xdc1e4993, 0x30825494,
38 0x91d0e462, 0x768087fc
41 static uint32_t hash_values_crc[2][12] = {{
42 0x00000000, 0xf26b8303,
43 0x91545164, 0x06040eb1, 0x9bb99201, 0xcc4c4fe4,
44 0x14a90993, 0xf8a5dd8c, 0xcaa1ad0b, 0x7ac1e03e,
45 0x43f44466, 0x4a11475e
48 0xbdfd3980, 0x70204542,
49 0x98cd4c70, 0xd52c702f, 0x41fc0e1c, 0x3905f65c,
50 0x94bff47f, 0x1bab102d, 0xf4a2c645, 0xbf441539,
51 0x789c104f, 0x53028d3e
55 /*******************************************************************************
56 * Hash function performance test configuration section. Each performance test
57 * will be performed HASHTEST_ITERATIONS times.
59 * The three arrays below control what tests are performed. Every combination
60 * from the array entries is tested.
62 #define HASHTEST_ITERATIONS 1000000
63 #define MAX_KEYSIZE 64
64 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
65 static uint32_t hashtest_initvals[] = {0, 0xdeadbeef};
66 static uint32_t hashtest_key_lens[] = {
67 1, 2, /* Unusual key sizes */
68 4, 8, 16, 32, 48, 64, /* standard key sizes */
69 9, /* IPv4 SRC + DST + protocol, unpadded */
70 13, /* IPv4 5-tuple, unpadded */
71 37, /* IPv6 5-tuple, unpadded */
72 40 /* IPv6 5-tuple, padded to 8-byte boundary */
74 /******************************************************************************/
77 * To help print out name of hash functions.
80 get_hash_name(rte_hash_function f)
85 if (f == rte_hash_crc)
86 return "rte_hash_crc";
92 * Test a hash function.
95 run_hash_func_perf_test(uint32_t key_len, uint32_t init_val,
98 static uint8_t key[HASHTEST_ITERATIONS][MAX_KEYSIZE];
99 uint64_t ticks, start, end;
102 for (i = 0; i < HASHTEST_ITERATIONS; i++) {
103 for (j = 0; j < key_len; j++)
104 key[i][j] = (uint8_t) rte_rand();
108 for (i = 0; i < HASHTEST_ITERATIONS; i++)
109 f(key[i], key_len, init_val);
113 printf("%-12s, %-18u, %-13u, %.02f\n", get_hash_name(f), (unsigned) key_len,
114 (unsigned) init_val, (double)ticks / HASHTEST_ITERATIONS);
118 * Test all hash functions.
121 run_hash_func_perf_tests(void)
125 printf(" *** Hash function performance test results ***\n");
126 printf(" Number of iterations for each test = %d\n",
127 HASHTEST_ITERATIONS);
128 printf("Hash Func. , Key Length (bytes), Initial value, Ticks/Op.\n");
130 for (i = 0; i < RTE_DIM(hashtest_initvals); i++) {
131 for (j = 0; j < RTE_DIM(hashtest_key_lens); j++) {
132 for (k = 0; k < RTE_DIM(hashtest_funcs); k++) {
133 run_hash_func_perf_test(hashtest_key_lens[j],
134 hashtest_initvals[i],
142 * Verify that hash functions return what they are expected to return
143 * (using precalculated values stored above)
146 verify_precalculated_hash_func_tests(void)
152 for (i = 0; i < 64; i++)
153 key[i] = (uint8_t) i;
155 for (i = 0; i < RTE_DIM(hashtest_key_lens); i++) {
156 for (j = 0; j < RTE_DIM(hashtest_initvals); j++) {
157 hash = rte_jhash(key, hashtest_key_lens[i],
158 hashtest_initvals[j]);
159 if (hash != hash_values_jhash[j][i]) {
160 printf("jhash for %u bytes with initial value 0x%x."
161 "Expected 0x%x, but got 0x%x\n",
162 hashtest_key_lens[i], hashtest_initvals[j],
163 hash_values_jhash[j][i], hash);
167 hash = rte_hash_crc(key, hashtest_key_lens[i],
168 hashtest_initvals[j]);
169 if (hash != hash_values_crc[j][i]) {
170 printf("CRC for %u bytes with initial value 0x%x."
171 "Expected 0x%x, but got 0x%x\n",
172 hashtest_key_lens[i], hashtest_initvals[j],
173 hash_values_crc[j][i], hash);
183 * Verify that rte_jhash and rte_jhash_32b return the same
186 verify_jhash_32bits(void)
190 uint32_t hash, hash32;
192 for (i = 0; i < 64; i++)
193 key[i] = rand() & 0xff;
195 for (i = 0; i < RTE_DIM(hashtest_key_lens); i++) {
196 for (j = 0; j < RTE_DIM(hashtest_initvals); j++) {
197 /* Key size must be multiple of 4 (32 bits) */
198 if ((hashtest_key_lens[i] & 0x3) == 0) {
199 hash = rte_jhash(key, hashtest_key_lens[i],
200 hashtest_initvals[j]);
201 /* Divide key length by 4 in rte_jhash for 32 bits */
202 hash32 = rte_jhash_32b((const unaligned_uint32_t *)key,
203 hashtest_key_lens[i] >> 2,
204 hashtest_initvals[j]);
205 if (hash != hash32) {
206 printf("rte_jhash returns different value (0x%x)"
207 "than rte_jhash_32b (0x%x)\n",
219 * Verify that rte_jhash and rte_jhash_1word, rte_jhash_2words
220 * and rte_jhash_3words return the same
223 verify_jhash_words(void)
227 uint32_t hash, hash_words;
229 for (i = 0; i < 3; i++)
232 /* Test rte_jhash_1word */
233 hash = rte_jhash(key, 4, 0);
234 hash_words = rte_jhash_1word(key[0], 0);
235 if (hash != hash_words) {
236 printf("rte_jhash returns different value (0x%x)"
237 "than rte_jhash_1word (0x%x)\n",
241 /* Test rte_jhash_2words */
242 hash = rte_jhash(key, 8, 0);
243 hash_words = rte_jhash_2words(key[0], key[1], 0);
244 if (hash != hash_words) {
245 printf("rte_jhash returns different value (0x%x)"
246 "than rte_jhash_2words (0x%x)\n",
250 /* Test rte_jhash_3words */
251 hash = rte_jhash(key, 12, 0);
252 hash_words = rte_jhash_3words(key[0], key[1], key[2], 0);
253 if (hash != hash_words) {
254 printf("rte_jhash returns different value (0x%x)"
255 "than rte_jhash_3words (0x%x)\n",
264 * Run all functional tests for hash functions
267 run_hash_func_tests(void)
269 if (verify_precalculated_hash_func_tests() != 0)
272 if (verify_jhash_32bits() != 0)
275 if (verify_jhash_words() != 0)
283 test_hash_functions(void)
285 if (run_hash_func_tests() != 0)
288 run_hash_func_perf_tests();
293 REGISTER_TEST_COMMAND(hash_functions_autotest, test_hash_functions);