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 0x821cc2db, 0xa491f494, 0xace4cd87, 0x9e867842,
59 0xd32442d6, 0x5fbafeab, 0x9cac434c, 0xecad9b0d,
60 0x2dcf235e, 0xaab655d0
63 0xc1111b14, 0x9a95039e, 0x84f208a0, 0xfa28f3fb,
64 0xfa13f7d3, 0xc7aed470, 0x74caa938, 0xa9288066,
65 0xd0140735, 0xbf00519d
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
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][RTE_HASH_KEY_LENGTH_MAX];
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_jhash2 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_jhash2((const 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_jhash2 (0x%x)\n",
243 * Run all functional tests for hash functions
246 run_hash_func_tests(void)
248 if (verify_precalculated_hash_func_tests() != 0)
251 if (verify_jhash_32bits() != 0)
259 test_hash_functions(void)
261 if (run_hash_func_tests() != 0)
264 run_hash_func_perf_tests();
269 static struct test_command hash_functions_cmd = {
270 .command = "hash_functions_autotest",
271 .callback = test_hash_functions,
273 REGISTER_TEST_COMMAND(hash_functions_cmd);