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_cycles.h>
43 #include <rte_random.h>
45 #include <rte_jhash.h>
46 #include <rte_hash_crc.h>
51 * Hash values calculated for key sizes from array "hashtest_key_lens"
52 * and for initial values from array "hashtest_initvals.
53 * Each key will be formed by increasing each byte by 1:
54 * e.g.: key size = 4, key = 0x03020100
55 * key size = 8, key = 0x0706050403020100
57 static uint32_t hash_values_jhash[2][10] = {{
58 0xe4cf1d42, 0xd4ccb93c, 0x5e84eafc, 0x21362cfe,
59 0x2f4775ab, 0x9ff036cc, 0xeca51474, 0xbc9d6816,
60 0x12926a31, 0x1c9fa888
63 0x8270ac65, 0x05fa6668, 0x762df861, 0xda088f2f,
64 0x59614cd4, 0x7a94f690, 0xdc1e4993, 0x30825494,
65 0x91d0e462, 0x768087fc
68 static uint32_t hash_values_crc[2][10] = {{
69 0x91545164, 0x06040eb1, 0x9bb99201, 0xcc4c4fe4,
70 0x14a90993, 0xf8a5dd8c, 0xc62beb31, 0x32bf340e,
71 0x72f9d22b, 0x4a11475e
74 0x98cd4c70, 0xd52c702f, 0x41fc0e1c, 0x3905f65c,
75 0x94bff47f, 0x1bab102d, 0xd2911ed7, 0xe8faa813,
76 0x6bea184b, 0x53028d3e
80 /*******************************************************************************
81 * Hash function performance test configuration section. Each performance test
82 * will be performed HASHTEST_ITERATIONS times.
84 * The three arrays below control what tests are performed. Every combination
85 * from the array entries is tested.
87 #define HASHTEST_ITERATIONS 1000000
88 #define MAX_KEYSIZE 64
89 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
90 static uint32_t hashtest_initvals[] = {0, 0xdeadbeef};
91 static uint32_t hashtest_key_lens[] = {
92 4, 8, 16, 32, 48, 64, /* standard key sizes */
93 9, /* IPv4 SRC + DST + protocol, unpadded */
94 13, /* IPv4 5-tuple, unpadded */
95 37, /* IPv6 5-tuple, unpadded */
96 40 /* IPv6 5-tuple, padded to 8-byte boundary */
98 /******************************************************************************/
101 * To help print out name of hash functions.
104 get_hash_name(rte_hash_function f)
109 if (f == rte_hash_crc)
110 return "rte_hash_crc";
112 return "UnknownHash";
116 * Test a hash function.
119 run_hash_func_perf_test(uint32_t key_len, uint32_t init_val,
122 static uint8_t key[HASHTEST_ITERATIONS][MAX_KEYSIZE];
123 uint64_t ticks, start, end;
126 for (i = 0; i < HASHTEST_ITERATIONS; i++) {
127 for (j = 0; j < key_len; j++)
128 key[i][j] = (uint8_t) rte_rand();
132 for (i = 0; i < HASHTEST_ITERATIONS; i++)
133 f(key[i], key_len, init_val);
137 printf("%-12s, %-18u, %-13u, %.02f\n", get_hash_name(f), (unsigned) key_len,
138 (unsigned) init_val, (double)ticks / HASHTEST_ITERATIONS);
142 * Test all hash functions.
145 run_hash_func_perf_tests(void)
149 printf(" *** Hash function performance test results ***\n");
150 printf(" Number of iterations for each test = %d\n",
151 HASHTEST_ITERATIONS);
152 printf("Hash Func. , Key Length (bytes), Initial value, Ticks/Op.\n");
154 for (i = 0; i < RTE_DIM(hashtest_initvals); i++) {
155 for (j = 0; j < RTE_DIM(hashtest_key_lens); j++) {
156 for (k = 0; k < RTE_DIM(hashtest_funcs); k++) {
157 run_hash_func_perf_test(hashtest_key_lens[j],
158 hashtest_initvals[i],
166 * Verify that hash functions return what they are expected to return
167 * (using precalculated values stored above)
170 verify_precalculated_hash_func_tests(void)
176 for (i = 0; i < 64; i++)
177 key[i] = (uint8_t) i;
179 for (i = 0; i < sizeof(hashtest_key_lens) / sizeof(uint32_t); i++) {
180 for (j = 0; j < sizeof(hashtest_initvals) / sizeof(uint32_t); j++) {
181 hash = rte_jhash(key, hashtest_key_lens[i],
182 hashtest_initvals[j]);
183 if (hash != hash_values_jhash[j][i]) {
184 printf("jhash for %u bytes with initial value 0x%x."
185 "Expected 0x%x, but got 0x%x\n",
186 hashtest_key_lens[i], hashtest_initvals[j],
187 hash_values_jhash[j][i], hash);
191 hash = rte_hash_crc(key, hashtest_key_lens[i],
192 hashtest_initvals[j]);
193 if (hash != hash_values_crc[j][i]) {
194 printf("CRC for %u bytes with initial value 0x%x."
195 "Expected 0x%x, but got 0x%x\n",
196 hashtest_key_lens[i], hashtest_initvals[j],
197 hash_values_crc[j][i], hash);
207 * Verify that rte_jhash and rte_jhash_32b return the same
210 verify_jhash_32bits(void)
214 uint32_t hash, hash32;
216 for (i = 0; i < 64; i++)
217 key[i] = rand() & 0xff;
219 for (i = 0; i < sizeof(hashtest_key_lens) / sizeof(uint32_t); i++) {
220 for (j = 0; j < sizeof(hashtest_initvals) / sizeof(uint32_t); j++) {
221 /* Key size must be multiple of 4 (32 bits) */
222 if ((hashtest_key_lens[i] & 0x3) == 0) {
223 hash = rte_jhash(key, hashtest_key_lens[i],
224 hashtest_initvals[j]);
225 /* Divide key length by 4 in rte_jhash for 32 bits */
226 hash32 = rte_jhash_32b((const unaligned_uint32_t *)key,
227 hashtest_key_lens[i] >> 2,
228 hashtest_initvals[j]);
229 if (hash != hash32) {
230 printf("rte_jhash returns different value (0x%x)"
231 "than rte_jhash_32b (0x%x)\n",
243 * Verify that rte_jhash and rte_jhash_1word, rte_jhash_2words
244 * and rte_jhash_3words return the same
247 verify_jhash_words(void)
251 uint32_t hash, hash_words;
253 for (i = 0; i < 3; i++)
256 /* Test rte_jhash_1word */
257 hash = rte_jhash(key, 4, 0);
258 hash_words = rte_jhash_1word(key[0], 0);
259 if (hash != hash_words) {
260 printf("rte_jhash returns different value (0x%x)"
261 "than rte_jhash_1word (0x%x)\n",
265 /* Test rte_jhash_2words */
266 hash = rte_jhash(key, 8, 0);
267 hash_words = rte_jhash_2words(key[0], key[1], 0);
268 if (hash != hash_words) {
269 printf("rte_jhash returns different value (0x%x)"
270 "than rte_jhash_2words (0x%x)\n",
274 /* Test rte_jhash_3words */
275 hash = rte_jhash(key, 12, 0);
276 hash_words = rte_jhash_3words(key[0], key[1], key[2], 0);
277 if (hash != hash_words) {
278 printf("rte_jhash returns different value (0x%x)"
279 "than rte_jhash_3words (0x%x)\n",
288 * Run all functional tests for hash functions
291 run_hash_func_tests(void)
293 if (verify_precalculated_hash_func_tests() != 0)
296 if (verify_jhash_32bits() != 0)
299 if (verify_jhash_words() != 0)
307 test_hash_functions(void)
309 if (run_hash_func_tests() != 0)
312 run_hash_func_perf_tests();
317 static struct test_command hash_functions_cmd = {
318 .command = "hash_functions_autotest",
319 .callback = test_hash_functions,
321 REGISTER_TEST_COMMAND(hash_functions_cmd);