eal: add and use unaligned integer types
[dpdk.git] / app / test / test_hash_functions.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_cycles.h>
43 #include <rte_random.h>
44 #include <rte_hash.h>
45 #include <rte_jhash.h>
46 #include <rte_hash_crc.h>
47
48 #include "test.h"
49
50 /*
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
56  */
57 static uint32_t hash_values_jhash[2][10] = {{
58         0xe4cf1d42, 0xd4ccb93c, 0x5e84eafc, 0x21362cfe,
59         0x2f4775ab, 0x9ff036cc, 0xeca51474, 0xbc9d6816,
60         0x12926a31, 0x1c9fa888
61 },
62 {
63         0x8270ac65, 0x05fa6668, 0x762df861, 0xda088f2f,
64         0x59614cd4, 0x7a94f690, 0xdc1e4993, 0x30825494,
65         0x91d0e462, 0x768087fc
66 }
67 };
68 static uint32_t hash_values_crc[2][10] = {{
69         0x91545164, 0x06040eb1, 0x9bb99201, 0xcc4c4fe4,
70         0x14a90993, 0xf8a5dd8c, 0xc62beb31, 0x32bf340e,
71         0x72f9d22b, 0x4a11475e
72 },
73 {
74         0x98cd4c70, 0xd52c702f, 0x41fc0e1c, 0x3905f65c,
75         0x94bff47f, 0x1bab102d, 0xd2911ed7, 0xe8faa813,
76         0x6bea184b, 0x53028d3e
77 }
78 };
79
80 /*******************************************************************************
81  * Hash function performance test configuration section. Each performance test
82  * will be performed HASHTEST_ITERATIONS times.
83  *
84  * The three arrays below control what tests are performed. Every combination
85  * from the array entries is tested.
86  */
87 #define HASHTEST_ITERATIONS 1000000
88
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 */
97 };
98 /******************************************************************************/
99
100 /*
101  * To help print out name of hash functions.
102  */
103 static const char *
104 get_hash_name(rte_hash_function f)
105 {
106         if (f == rte_jhash)
107                 return "jhash";
108
109         if (f == rte_hash_crc)
110                 return "rte_hash_crc";
111
112         return "UnknownHash";
113 }
114
115 /*
116  * Test a hash function.
117  */
118 static void
119 run_hash_func_perf_test(uint32_t key_len, uint32_t init_val,
120                 rte_hash_function f)
121 {
122         static uint8_t key[HASHTEST_ITERATIONS][RTE_HASH_KEY_LENGTH_MAX];
123         uint64_t ticks, start, end;
124         unsigned i, j;
125
126         for (i = 0; i < HASHTEST_ITERATIONS; i++) {
127                 for (j = 0; j < key_len; j++)
128                         key[i][j] = (uint8_t) rte_rand();
129         }
130
131         start = rte_rdtsc();
132         for (i = 0; i < HASHTEST_ITERATIONS; i++)
133                 f(key[i], key_len, init_val);
134         end = rte_rdtsc();
135         ticks = end - start;
136
137         printf("%-12s, %-18u, %-13u, %.02f\n", get_hash_name(f), (unsigned) key_len,
138                         (unsigned) init_val, (double)ticks / HASHTEST_ITERATIONS);
139 }
140
141 /*
142  * Test all hash functions.
143  */
144 static void
145 run_hash_func_perf_tests(void)
146 {
147         unsigned i, j, k;
148
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");
153
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],
159                                                 hashtest_funcs[k]);
160                         }
161                 }
162         }
163 }
164
165 /*
166  * Verify that hash functions return what they are expected to return
167  * (using precalculated values stored above)
168  */
169 static int
170 verify_precalculated_hash_func_tests(void)
171 {
172         unsigned i, j;
173         uint8_t key[64];
174         uint32_t hash;
175
176         for (i = 0; i < 64; i++)
177                 key[i] = (uint8_t) i;
178
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);
188                                 return -1;
189                         }
190
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);
198                                 return -1;
199                         }
200                 }
201         }
202
203         return 0;
204 }
205
206 /*
207  * Verify that rte_jhash and rte_jhash_32b return the same
208  */
209 static int
210 verify_jhash_32bits(void)
211 {
212         unsigned i, j;
213         uint8_t key[64];
214         uint32_t hash, hash32;
215
216         for (i = 0; i < 64; i++)
217                 key[i] = rand() & 0xff;
218
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",
232                                                hash, hash32);
233                                         return -1;
234                                 }
235                         }
236                 }
237         }
238
239         return 0;
240 }
241
242 /*
243  * Verify that rte_jhash and rte_jhash_1word, rte_jhash_2words
244  * and rte_jhash_3words return the same
245  */
246 static int
247 verify_jhash_words(void)
248 {
249         unsigned i;
250         uint32_t key[3];
251         uint32_t hash, hash_words;
252
253         for (i = 0; i < 3; i++)
254                 key[i] = rand();
255
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",
262                        hash, hash_words);
263                 return -1;
264         }
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",
271                        hash, hash_words);
272                 return -1;
273         }
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",
280                        hash, hash_words);
281                 return -1;
282         }
283
284         return 0;
285 }
286
287 /*
288  * Run all functional tests for hash functions
289  */
290 static int
291 run_hash_func_tests(void)
292 {
293         if (verify_precalculated_hash_func_tests() != 0)
294                 return -1;
295
296         if (verify_jhash_32bits() != 0)
297                 return -1;
298
299         if (verify_jhash_words() != 0)
300                 return -1;
301
302         return 0;
303
304 }
305
306 static int
307 test_hash_functions(void)
308 {
309         if (run_hash_func_tests() != 0)
310                 return -1;
311
312         run_hash_func_perf_tests();
313
314         return 0;
315 }
316
317 static struct test_command hash_functions_cmd = {
318         .command = "hash_functions_autotest",
319         .callback = test_hash_functions,
320 };
321 REGISTER_TEST_COMMAND(hash_functions_cmd);