app/test: shorten execution time
[dpdk.git] / app / test / test_hash.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_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>
48 #include <rte_eal.h>
49 #include <rte_ip.h>
50 #include <rte_string_fns.h>
51
52 #include "test.h"
53
54 #include <rte_hash.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57 #include <rte_hash_crc.h>
58
59 /*******************************************************************************
60  * Hash function performance test configuration section. Each performance test
61  * will be performed HASHTEST_ITERATIONS times.
62  *
63  * The five arrays below control what tests are performed. Every combination
64  * from the array entries is tested.
65  */
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)
72
73 /*
74  * Check condition and return an error if true. Assumes that "handle" is the
75  * name of the hash structure pointer to be freed.
76  */
77 #define RETURN_IF_ERROR(cond, str, ...) do {                            \
78         if (cond) {                                                     \
79                 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
80                 if (handle) rte_hash_free(handle);                      \
81                 return -1;                                              \
82         }                                                               \
83 } while(0)
84
85 #define RETURN_IF_ERROR_FBK(cond, str, ...) do {                                \
86         if (cond) {                                                     \
87                 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
88                 if (handle) rte_fbk_hash_free(handle);                  \
89                 return -1;                                              \
90         }                                                               \
91 } while(0)
92
93 /* 5-tuple key type */
94 struct flow_key {
95         uint32_t ip_src;
96         uint32_t ip_dst;
97         uint16_t port_src;
98         uint16_t port_dst;
99         uint8_t proto;
100 } __attribute__((packed));
101
102 /*
103  * Hash function that always returns the same value, to easily test what
104  * happens when a bucket is full.
105  */
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)
109 {
110         return 3;
111 }
112
113 /*
114  * Print out result of unit test hash operation.
115  */
116 #if defined(UNIT_TEST_HASH_VERBOSE)
117 static void print_key_info(const char *msg, const struct flow_key *key,
118                                                                 int32_t pos)
119 {
120         uint8_t *p = (uint8_t *)key;
121         unsigned i;
122
123         printf("%s key:0x", msg);
124         for (i = 0; i < sizeof(struct flow_key); i++) {
125                 printf("%02X", p[i]);
126         }
127         printf(" @ pos %d\n", pos);
128 }
129 #else
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)
133 {
134 }
135 #endif
136
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),
141         .port_src = 0x0908,
142         .port_dst = 0x0b0a,
143         .proto = 0x0c,
144 }, {
145         .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
146         .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
147         .port_src = 0x1918,
148         .port_dst = 0x1b1a,
149         .proto = 0x1c,
150 }, {
151         .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
152         .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
153         .port_src = 0x2928,
154         .port_dst = 0x2b2a,
155         .proto = 0x2c,
156 }, {
157         .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
158         .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
159         .port_src = 0x3938,
160         .port_dst = 0x3b3a,
161         .proto = 0x3c,
162 }, {
163         .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
164         .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
165         .port_src = 0x4948,
166         .port_dst = 0x4b4a,
167         .proto = 0x4c,
168 } };
169
170 /* Parameters used for hash table in unit test functions. Name set later. */
171 static struct rte_hash_parameters ut_params = {
172         .entries = 64,
173         .key_len = sizeof(struct flow_key), /* 13 */
174         .hash_func = rte_jhash,
175         .hash_func_init_val = 0,
176         .socket_id = 0,
177 };
178
179 #define CRC32_ITERATIONS (1U << 10)
180 #define CRC32_DWORDS (1U << 6)
181 /*
182  * Test if all CRC32 implementations yield the same hash value
183  */
184 static int
185 test_crc32_hash_alg_equiv(void)
186 {
187         uint32_t hash_val;
188         uint32_t init_val;
189         uint64_t data64[CRC32_DWORDS];
190         unsigned i, j;
191         size_t data_len;
192
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();
198
199                 /* Fill the data set */
200                 for (j = 0; j < CRC32_DWORDS; j++)
201                         data64[j] = rte_rand();
202
203                 /* Calculate software CRC32 */
204                 rte_hash_crc_set_alg(CRC32_SW);
205                 hash_val = rte_hash_crc(data64, data_len, init_val);
206
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");
211                         break;
212                 }
213
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");
218                         break;
219                 }
220
221                 /* Check against 8-byte-operand ARM64 CRC32 if available */
222                 rte_hash_crc_set_alg(CRC32_ARM64);
223                 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
224                         printf("Failed checking CRC32_SW against CRC32_ARM64\n");
225                         break;
226                 }
227         }
228
229         /* Resetting to best available algorithm */
230         rte_hash_crc_set_alg(CRC32_SSE42_x64);
231
232         if (i == CRC32_ITERATIONS)
233                 return 0;
234
235         printf("Failed test data (hex, %zu bytes total):\n", data_len);
236         for (j = 0; j < data_len; j++)
237                 printf("%02X%c", ((uint8_t *)data64)[j],
238                                 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' ');
239
240         return -1;
241 }
242
243 /*
244  * Test a hash function.
245  */
246 static void run_hash_func_test(rte_hash_function f, uint32_t init_val,
247                 uint32_t key_len)
248 {
249         static uint8_t key[MAX_KEYSIZE];
250         unsigned i;
251
252
253         for (i = 0; i < key_len; i++)
254                 key[i] = (uint8_t) rte_rand();
255
256         /* just to be on the safe side */
257         if (!f)
258                 return;
259
260         f(key, key_len, init_val);
261 }
262
263 /*
264  * Test all hash functions.
265  */
266 static void run_hash_func_tests(void)
267 {
268         unsigned i, j, k;
269
270         for (i = 0;
271              i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
272              i++) {
273                 for (j = 0;
274                      j < sizeof(hashtest_initvals) / sizeof(uint32_t);
275                      j++) {
276                         for (k = 0;
277                              k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
278                              k++) {
279                                 run_hash_func_test(hashtest_funcs[i],
280                                                 hashtest_initvals[j],
281                                                 hashtest_key_lens[k]);
282                         }
283                 }
284         }
285 }
286
287 /*
288  * Basic sequence of operations for a single key:
289  *      - add
290  *      - lookup (hit)
291  *      - delete
292  *      - lookup (miss)
293  */
294 static int test_add_delete(void)
295 {
296         struct rte_hash *handle;
297         /* test with standard add/lookup/delete functions */
298         int pos0, expectedPos0;
299
300         ut_params.name = "test1";
301         handle = rte_hash_create(&ut_params);
302         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
303
304         pos0 = rte_hash_add_key(handle, &keys[0]);
305         print_key_info("Add", &keys[0], pos0);
306         RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
307         expectedPos0 = pos0;
308
309         pos0 = rte_hash_lookup(handle, &keys[0]);
310         print_key_info("Lkp", &keys[0], pos0);
311         RETURN_IF_ERROR(pos0 != expectedPos0,
312                         "failed to find key (pos0=%d)", pos0);
313
314         pos0 = rte_hash_del_key(handle, &keys[0]);
315         print_key_info("Del", &keys[0], pos0);
316         RETURN_IF_ERROR(pos0 != expectedPos0,
317                         "failed to delete key (pos0=%d)", pos0);
318
319         pos0 = rte_hash_lookup(handle, &keys[0]);
320         print_key_info("Lkp", &keys[0], pos0);
321         RETURN_IF_ERROR(pos0 != -ENOENT,
322                         "fail: found key after deleting! (pos0=%d)", pos0);
323
324         rte_hash_free(handle);
325
326         /* repeat test with precomputed hash functions */
327         hash_sig_t hash_value;
328         int pos1, expectedPos1;
329
330         handle = rte_hash_create(&ut_params);
331         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
332
333         hash_value = rte_hash_hash(handle, &keys[0]);
334         pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value);
335         print_key_info("Add", &keys[0], pos1);
336         RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1);
337         expectedPos1 = pos1;
338
339         pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
340         print_key_info("Lkp", &keys[0], pos1);
341         RETURN_IF_ERROR(pos1 != expectedPos1,
342                         "failed to find key (pos1=%d)", pos1);
343
344         pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value);
345         print_key_info("Del", &keys[0], pos1);
346         RETURN_IF_ERROR(pos1 != expectedPos1,
347                         "failed to delete key (pos1=%d)", pos1);
348
349         pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value);
350         print_key_info("Lkp", &keys[0], pos1);
351         RETURN_IF_ERROR(pos1 != -ENOENT,
352                         "fail: found key after deleting! (pos1=%d)", pos1);
353
354         rte_hash_free(handle);
355
356         return 0;
357 }
358
359 /*
360  * Sequence of operations for a single key:
361  *      - delete: miss
362  *      - add
363  *      - lookup: hit
364  *      - add: update
365  *      - lookup: hit (updated data)
366  *      - delete: hit
367  *      - delete: miss
368  *      - lookup: miss
369  */
370 static int test_add_update_delete(void)
371 {
372         struct rte_hash *handle;
373         int pos0, expectedPos0;
374
375         ut_params.name = "test2";
376         handle = rte_hash_create(&ut_params);
377         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
378
379         pos0 = rte_hash_del_key(handle, &keys[0]);
380         print_key_info("Del", &keys[0], pos0);
381         RETURN_IF_ERROR(pos0 != -ENOENT,
382                         "fail: found non-existent key (pos0=%d)", pos0);
383
384         pos0 = rte_hash_add_key(handle, &keys[0]);
385         print_key_info("Add", &keys[0], pos0);
386         RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
387         expectedPos0 = pos0;
388
389         pos0 = rte_hash_lookup(handle, &keys[0]);
390         print_key_info("Lkp", &keys[0], pos0);
391         RETURN_IF_ERROR(pos0 != expectedPos0,
392                         "failed to find key (pos0=%d)", pos0);
393
394         pos0 = rte_hash_add_key(handle, &keys[0]);
395         print_key_info("Add", &keys[0], pos0);
396         RETURN_IF_ERROR(pos0 != expectedPos0,
397                         "failed to re-add key (pos0=%d)", pos0);
398
399         pos0 = rte_hash_lookup(handle, &keys[0]);
400         print_key_info("Lkp", &keys[0], pos0);
401         RETURN_IF_ERROR(pos0 != expectedPos0,
402                         "failed to find key (pos0=%d)", pos0);
403
404         pos0 = rte_hash_del_key(handle, &keys[0]);
405         print_key_info("Del", &keys[0], pos0);
406         RETURN_IF_ERROR(pos0 != expectedPos0,
407                         "failed to delete key (pos0=%d)", pos0);
408
409         pos0 = rte_hash_del_key(handle, &keys[0]);
410         print_key_info("Del", &keys[0], pos0);
411         RETURN_IF_ERROR(pos0 != -ENOENT,
412                         "fail: deleted already deleted key (pos0=%d)", pos0);
413
414         pos0 = rte_hash_lookup(handle, &keys[0]);
415         print_key_info("Lkp", &keys[0], pos0);
416         RETURN_IF_ERROR(pos0 != -ENOENT,
417                         "fail: found key after deleting! (pos0=%d)", pos0);
418
419         rte_hash_free(handle);
420         return 0;
421 }
422
423 /*
424  * Sequence of operations for find existing hash table
425  *
426  *  - create table
427  *  - find existing table: hit
428  *  - find non-existing table: miss
429  *
430  */
431 static int test_hash_find_existing(void)
432 {
433         struct rte_hash *handle = NULL, *result = NULL;
434
435         /* Create hash table. */
436         ut_params.name = "hash_find_existing";
437         handle = rte_hash_create(&ut_params);
438         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
439
440         /* Try to find existing hash table */
441         result = rte_hash_find_existing("hash_find_existing");
442         RETURN_IF_ERROR(result != handle, "could not find existing hash table");
443
444         /* Try to find non-existing hash table */
445         result = rte_hash_find_existing("hash_find_non_existing");
446         RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
447
448         /* Cleanup. */
449         rte_hash_free(handle);
450
451         return 0;
452 }
453
454 /*
455  * Sequence of operations for 5 keys
456  *      - add keys
457  *      - lookup keys: hit
458  *      - add keys (update)
459  *      - lookup keys: hit (updated data)
460  *      - delete keys : hit
461  *      - lookup keys: miss
462  */
463 static int test_five_keys(void)
464 {
465         struct rte_hash *handle;
466         const void *key_array[5] = {0};
467         int pos[5];
468         int expected_pos[5];
469         unsigned i;
470         int ret;
471
472         ut_params.name = "test3";
473         handle = rte_hash_create(&ut_params);
474         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
475
476         /* Add */
477         for (i = 0; i < 5; i++) {
478                 pos[i] = rte_hash_add_key(handle, &keys[i]);
479                 print_key_info("Add", &keys[i], pos[i]);
480                 RETURN_IF_ERROR(pos[i] < 0,
481                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
482                 expected_pos[i] = pos[i];
483         }
484
485         /* Lookup */
486         for(i = 0; i < 5; i++)
487                 key_array[i] = &keys[i];
488
489         ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
490         if(ret == 0)
491                 for(i = 0; i < 5; i++) {
492                         print_key_info("Lkp", key_array[i], pos[i]);
493                         RETURN_IF_ERROR(pos[i] != expected_pos[i],
494                                         "failed to find key (pos[%u]=%d)", i, pos[i]);
495                 }
496
497         /* Add - update */
498         for (i = 0; i < 5; i++) {
499                 pos[i] = rte_hash_add_key(handle, &keys[i]);
500                 print_key_info("Add", &keys[i], pos[i]);
501                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
502                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
503         }
504
505         /* Lookup */
506         for (i = 0; i < 5; i++) {
507                 pos[i] = rte_hash_lookup(handle, &keys[i]);
508                 print_key_info("Lkp", &keys[i], pos[i]);
509                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
510                                 "failed to find key (pos[%u]=%d)", i, pos[i]);
511         }
512
513         /* Delete */
514         for (i = 0; i < 5; i++) {
515                 pos[i] = rte_hash_del_key(handle, &keys[i]);
516                 print_key_info("Del", &keys[i], pos[i]);
517                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
518                                 "failed to delete key (pos[%u]=%d)", i, pos[i]);
519         }
520
521         /* Lookup */
522         for (i = 0; i < 5; i++) {
523                 pos[i] = rte_hash_lookup(handle, &keys[i]);
524                 print_key_info("Lkp", &keys[i], pos[i]);
525                 RETURN_IF_ERROR(pos[i] != -ENOENT,
526                                 "found non-existent key (pos[%u]=%d)", i, pos[i]);
527         }
528
529         /* Lookup multi */
530         ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos);
531         if (ret == 0)
532                 for (i = 0; i < 5; i++) {
533                         print_key_info("Lkp", key_array[i], pos[i]);
534                         RETURN_IF_ERROR(pos[i] != -ENOENT,
535                                         "found not-existent key (pos[%u]=%d)", i, pos[i]);
536                 }
537
538         rte_hash_free(handle);
539
540         return 0;
541 }
542
543 /*
544  * Add keys to the same bucket until bucket full.
545  *      - add 5 keys to the same bucket (hash created with 4 keys per bucket):
546  *        first 4 successful, 5th successful, pushing existing item in bucket
547  *      - lookup the 5 keys: 5 hits
548  *      - add the 5 keys again: 5 OK
549  *      - lookup the 5 keys: 5 hits (updated data)
550  *      - delete the 5 keys: 5 OK
551  *      - lookup the 5 keys: 5 misses
552  */
553 static int test_full_bucket(void)
554 {
555         struct rte_hash_parameters params_pseudo_hash = {
556                 .name = "test4",
557                 .entries = 64,
558                 .key_len = sizeof(struct flow_key), /* 13 */
559                 .hash_func = pseudo_hash,
560                 .hash_func_init_val = 0,
561                 .socket_id = 0,
562         };
563         struct rte_hash *handle;
564         int pos[5];
565         int expected_pos[5];
566         unsigned i;
567
568         handle = rte_hash_create(&params_pseudo_hash);
569         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
570
571         /* Fill bucket */
572         for (i = 0; i < 4; i++) {
573                 pos[i] = rte_hash_add_key(handle, &keys[i]);
574                 print_key_info("Add", &keys[i], pos[i]);
575                 RETURN_IF_ERROR(pos[i] < 0,
576                         "failed to add key (pos[%u]=%d)", i, pos[i]);
577                 expected_pos[i] = pos[i];
578         }
579         /*
580          * This should work and will push one of the items
581          * in the bucket because it is full
582          */
583         pos[4] = rte_hash_add_key(handle, &keys[4]);
584         print_key_info("Add", &keys[4], pos[4]);
585         RETURN_IF_ERROR(pos[4] < 0,
586                         "failed to add key (pos[4]=%d)", pos[4]);
587         expected_pos[4] = pos[4];
588
589         /* Lookup */
590         for (i = 0; i < 5; i++) {
591                 pos[i] = rte_hash_lookup(handle, &keys[i]);
592                 print_key_info("Lkp", &keys[i], pos[i]);
593                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
594                         "failed to find key (pos[%u]=%d)", i, pos[i]);
595         }
596
597         /* Add - update */
598         for (i = 0; i < 5; i++) {
599                 pos[i] = rte_hash_add_key(handle, &keys[i]);
600                 print_key_info("Add", &keys[i], pos[i]);
601                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
602                         "failed to add key (pos[%u]=%d)", i, pos[i]);
603         }
604
605         /* Lookup */
606         for (i = 0; i < 5; i++) {
607                 pos[i] = rte_hash_lookup(handle, &keys[i]);
608                 print_key_info("Lkp", &keys[i], pos[i]);
609                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
610                         "failed to find key (pos[%u]=%d)", i, pos[i]);
611         }
612
613         /* Delete 1 key, check other keys are still found */
614         pos[1] = rte_hash_del_key(handle, &keys[1]);
615         print_key_info("Del", &keys[1], pos[1]);
616         RETURN_IF_ERROR(pos[1] != expected_pos[1],
617                         "failed to delete key (pos[1]=%d)", pos[1]);
618         pos[3] = rte_hash_lookup(handle, &keys[3]);
619         print_key_info("Lkp", &keys[3], pos[3]);
620         RETURN_IF_ERROR(pos[3] != expected_pos[3],
621                         "failed lookup after deleting key from same bucket "
622                         "(pos[3]=%d)", pos[3]);
623
624         /* Go back to previous state */
625         pos[1] = rte_hash_add_key(handle, &keys[1]);
626         print_key_info("Add", &keys[1], pos[1]);
627         expected_pos[1] = pos[1];
628         RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
629
630         /* Delete */
631         for (i = 0; i < 5; i++) {
632                 pos[i] = rte_hash_del_key(handle, &keys[i]);
633                 print_key_info("Del", &keys[i], pos[i]);
634                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
635                         "failed to delete key (pos[%u]=%d)", i, pos[i]);
636         }
637
638         /* Lookup */
639         for (i = 0; i < 5; i++) {
640                 pos[i] = rte_hash_lookup(handle, &keys[i]);
641                 print_key_info("Lkp", &keys[i], pos[i]);
642                 RETURN_IF_ERROR(pos[i] != -ENOENT,
643                         "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
644         }
645
646         rte_hash_free(handle);
647
648         /* Cover the NULL case. */
649         rte_hash_free(0);
650         return 0;
651 }
652
653 /******************************************************************************/
654 static int
655 fbk_hash_unit_test(void)
656 {
657         struct rte_fbk_hash_params params = {
658                 .name = "fbk_hash_test",
659                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
660                 .entries_per_bucket = 4,
661                 .socket_id = 0,
662         };
663
664         struct rte_fbk_hash_params invalid_params_1 = {
665                 .name = "invalid_1",
666                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
667                 .entries_per_bucket = 4,
668                 .socket_id = 0,
669         };
670
671         struct rte_fbk_hash_params invalid_params_2 = {
672                 .name = "invalid_2",
673                 .entries = 4,
674                 .entries_per_bucket = 3,         /* Not power of 2 */
675                 .socket_id = 0,
676         };
677
678         struct rte_fbk_hash_params invalid_params_3 = {
679                 .name = "invalid_3",
680                 .entries = 0,                    /* Entries is 0 */
681                 .entries_per_bucket = 4,
682                 .socket_id = 0,
683         };
684
685         struct rte_fbk_hash_params invalid_params_4 = {
686                 .name = "invalid_4",
687                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
688                 .entries_per_bucket = 0,         /* Entries per bucket is 0 */
689                 .socket_id = 0,
690         };
691
692         struct rte_fbk_hash_params invalid_params_5 = {
693                 .name = "invalid_5",
694                 .entries = 4,
695                 .entries_per_bucket = 8,         /* Entries per bucket > entries */
696                 .socket_id = 0,
697         };
698
699         struct rte_fbk_hash_params invalid_params_6 = {
700                 .name = "invalid_6",
701                 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2,   /* Entries > max allowed */
702                 .entries_per_bucket = 4,
703                 .socket_id = 0,
704         };
705
706         struct rte_fbk_hash_params invalid_params_7 = {
707                 .name = "invalid_7",
708                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
709                 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2,  /* Entries > max allowed */
710                 .socket_id = 0,
711         };
712
713         struct rte_fbk_hash_params invalid_params_8 = {
714                 .name = "invalid_7",
715                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
716                 .entries_per_bucket = 4,
717                 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
718         };
719
720         /* try to create two hashes with identical names
721          * in this case, trying to create a second one will not
722          * fail but will simply return pointer to the existing
723          * hash with that name. sort of like a "find hash by name" :-)
724          */
725         struct rte_fbk_hash_params invalid_params_same_name_1 = {
726                 .name = "same_name",                            /* hash with identical name */
727                 .entries = 4,
728                 .entries_per_bucket = 2,
729                 .socket_id = 0,
730         };
731
732         /* trying to create this hash should return a pointer to an existing hash */
733         struct rte_fbk_hash_params invalid_params_same_name_2 = {
734                 .name = "same_name",                            /* hash with identical name */
735                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
736                 .entries_per_bucket = 4,
737                 .socket_id = 0,
738         };
739
740         /* this is a sanity check for "same name" test
741          * creating this hash will check if we are actually able to create
742          * multiple hashes with different names (instead of having just one).
743          */
744         struct rte_fbk_hash_params different_name = {
745                 .name = "different_name",                       /* different name */
746                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
747                 .entries_per_bucket = 4,
748                 .socket_id = 0,
749         };
750
751         struct rte_fbk_hash_params params_jhash = {
752                 .name = "valid",
753                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
754                 .entries_per_bucket = 4,
755                 .socket_id = 0,
756                 .hash_func = rte_jhash_1word,              /* Tests for different hash_func */
757                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
758         };
759
760         struct rte_fbk_hash_params params_nohash = {
761                 .name = "valid nohash",
762                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
763                 .entries_per_bucket = 4,
764                 .socket_id = 0,
765                 .hash_func = NULL,                            /* Tests for null hash_func */
766                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
767         };
768
769         struct rte_fbk_hash_table *handle, *tmp;
770         uint32_t keys[5] =
771                 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
772         uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
773         int status;
774         unsigned i;
775         double used_entries;
776
777         /* Try creating hashes with invalid parameters */
778         printf("# Testing hash creation with invalid parameters "
779                         "- expect error msgs\n");
780         handle = rte_fbk_hash_create(&invalid_params_1);
781         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
782
783         handle = rte_fbk_hash_create(&invalid_params_2);
784         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
785
786         handle = rte_fbk_hash_create(&invalid_params_3);
787         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
788
789         handle = rte_fbk_hash_create(&invalid_params_4);
790         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
791
792         handle = rte_fbk_hash_create(&invalid_params_5);
793         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
794
795         handle = rte_fbk_hash_create(&invalid_params_6);
796         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
797
798         handle = rte_fbk_hash_create(&invalid_params_7);
799         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
800
801         handle = rte_fbk_hash_create(&invalid_params_8);
802         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
803
804         handle = rte_fbk_hash_create(&invalid_params_same_name_1);
805         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
806
807         tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
808         if (tmp != NULL)
809                 rte_fbk_hash_free(tmp);
810         RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
811
812         /* we are not freeing  handle here because we need a hash list
813          * to be not empty for the next test */
814
815         /* create a hash in non-empty list - good for coverage */
816         tmp = rte_fbk_hash_create(&different_name);
817         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
818
819         /* free both hashes */
820         rte_fbk_hash_free(handle);
821         rte_fbk_hash_free(tmp);
822
823         /* Create empty jhash hash. */
824         handle = rte_fbk_hash_create(&params_jhash);
825         RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
826
827         /* Cleanup. */
828         rte_fbk_hash_free(handle);
829
830         /* Create empty jhash hash. */
831         handle = rte_fbk_hash_create(&params_nohash);
832         RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
833
834         /* Cleanup. */
835         rte_fbk_hash_free(handle);
836
837         /* Create empty hash. */
838         handle = rte_fbk_hash_create(&params);
839         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
840
841         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
842         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
843                                 "load factor right after creation is not zero but it should be");
844         /* Add keys. */
845         for (i = 0; i < 5; i++) {
846                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
847                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
848         }
849
850         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
851         RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
852                                 "load factor now is not as expected");
853         /* Find value of added keys. */
854         for (i = 0; i < 5; i++) {
855                 status = rte_fbk_hash_lookup(handle, keys[i]);
856                 RETURN_IF_ERROR_FBK(status != vals[i],
857                                 "fbk hash lookup failed");
858         }
859
860         /* Change value of added keys. */
861         for (i = 0; i < 5; i++) {
862                 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
863                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
864         }
865
866         /* Find new values. */
867         for (i = 0; i < 5; i++) {
868                 status = rte_fbk_hash_lookup(handle, keys[i]);
869                 RETURN_IF_ERROR_FBK(status != vals[4-i],
870                                 "fbk hash lookup failed");
871         }
872
873         /* Delete keys individually. */
874         for (i = 0; i < 5; i++) {
875                 status = rte_fbk_hash_delete_key(handle, keys[i]);
876                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
877         }
878
879         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
880         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
881                                 "load factor right after deletion is not zero but it should be");
882         /* Lookup should now fail. */
883         for (i = 0; i < 5; i++) {
884                 status = rte_fbk_hash_lookup(handle, keys[i]);
885                 RETURN_IF_ERROR_FBK(status == 0,
886                                 "fbk hash lookup should have failed");
887         }
888
889         /* Add keys again. */
890         for (i = 0; i < 5; i++) {
891                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
892                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
893         }
894
895         /* Make sure they were added. */
896         for (i = 0; i < 5; i++) {
897                 status = rte_fbk_hash_lookup(handle, keys[i]);
898                 RETURN_IF_ERROR_FBK(status != vals[i],
899                                 "fbk hash lookup failed");
900         }
901
902         /* Clear all entries. */
903         rte_fbk_hash_clear_all(handle);
904
905         /* Lookup should fail. */
906         for (i = 0; i < 5; i++) {
907                 status = rte_fbk_hash_lookup(handle, keys[i]);
908                 RETURN_IF_ERROR_FBK(status == 0,
909                                 "fbk hash lookup should have failed");
910         }
911
912         /* coverage */
913
914         /* fill up the hash_table */
915         for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
916                 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
917
918         /* Find non-existent key in a full hashtable */
919         status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
920         RETURN_IF_ERROR_FBK(status != -ENOENT,
921                         "fbk hash lookup succeeded");
922
923         /* Delete non-existent key in a full hashtable */
924         status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
925         RETURN_IF_ERROR_FBK(status != -ENOENT,
926                         "fbk hash delete succeeded");
927
928         /* Delete one key from a full hashtable */
929         status = rte_fbk_hash_delete_key(handle, 1);
930         RETURN_IF_ERROR_FBK(status != 0,
931                         "fbk hash delete failed");
932
933         /* Clear all entries. */
934         rte_fbk_hash_clear_all(handle);
935
936         /* Cleanup. */
937         rte_fbk_hash_free(handle);
938
939         /* Cover the NULL case. */
940         rte_fbk_hash_free(0);
941
942         return 0;
943 }
944
945 /*
946  * Sequence of operations for find existing fbk hash table
947  *
948  *  - create table
949  *  - find existing table: hit
950  *  - find non-existing table: miss
951  *
952  */
953 static int test_fbk_hash_find_existing(void)
954 {
955         struct rte_fbk_hash_params params = {
956                         .name = "fbk_hash_find_existing",
957                         .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
958                         .entries_per_bucket = 4,
959                         .socket_id = 0,
960         };
961         struct rte_fbk_hash_table *handle = NULL, *result = NULL;
962
963         /* Create hash table. */
964         handle = rte_fbk_hash_create(&params);
965         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
966
967         /* Try to find existing fbk hash table */
968         result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
969         RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
970
971         /* Try to find non-existing fbk hash table */
972         result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
973         RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
974
975         /* Cleanup. */
976         rte_fbk_hash_free(handle);
977
978         return 0;
979 }
980
981 #define BUCKET_ENTRIES 4
982 /*
983  * Do tests for hash creation with bad parameters.
984  */
985 static int test_hash_creation_with_bad_parameters(void)
986 {
987         struct rte_hash *handle, *tmp;
988         struct rte_hash_parameters params;
989
990         handle = rte_hash_create(NULL);
991         if (handle != NULL) {
992                 rte_hash_free(handle);
993                 printf("Impossible creating hash sucessfully without any parameter\n");
994                 return -1;
995         }
996
997         memcpy(&params, &ut_params, sizeof(params));
998         params.name = "creation_with_bad_parameters_0";
999         params.entries = RTE_HASH_ENTRIES_MAX + 1;
1000         handle = rte_hash_create(&params);
1001         if (handle != NULL) {
1002                 rte_hash_free(handle);
1003                 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1004                 return -1;
1005         }
1006
1007         memcpy(&params, &ut_params, sizeof(params));
1008         params.name = "creation_with_bad_parameters_2";
1009         params.entries = BUCKET_ENTRIES - 1;
1010         handle = rte_hash_create(&params);
1011         if (handle != NULL) {
1012                 rte_hash_free(handle);
1013                 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1014                 return -1;
1015         }
1016
1017         memcpy(&params, &ut_params, sizeof(params));
1018         params.name = "creation_with_bad_parameters_3";
1019         params.key_len = 0;
1020         handle = rte_hash_create(&params);
1021         if (handle != NULL) {
1022                 rte_hash_free(handle);
1023                 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1024                 return -1;
1025         }
1026
1027         memcpy(&params, &ut_params, sizeof(params));
1028         params.name = "creation_with_bad_parameters_4";
1029         params.socket_id = RTE_MAX_NUMA_NODES + 1;
1030         handle = rte_hash_create(&params);
1031         if (handle != NULL) {
1032                 rte_hash_free(handle);
1033                 printf("Impossible creating hash sucessfully with invalid socket\n");
1034                 return -1;
1035         }
1036
1037         /* test with same name should fail */
1038         memcpy(&params, &ut_params, sizeof(params));
1039         params.name = "same_name";
1040         handle = rte_hash_create(&params);
1041         if (handle == NULL) {
1042                 printf("Cannot create first hash table with 'same_name'\n");
1043                 return -1;
1044         }
1045         tmp = rte_hash_create(&params);
1046         if (tmp != NULL) {
1047                 printf("Creation of hash table with same name should fail\n");
1048                 rte_hash_free(handle);
1049                 rte_hash_free(tmp);
1050                 return -1;
1051         }
1052         rte_hash_free(handle);
1053
1054         printf("# Test successful. No more errors expected\n");
1055
1056         return 0;
1057 }
1058
1059 /*
1060  * Do tests for hash creation with parameters that look incorrect
1061  * but are actually valid.
1062  */
1063 static int
1064 test_hash_creation_with_good_parameters(void)
1065 {
1066         struct rte_hash *handle;
1067         struct rte_hash_parameters params;
1068
1069         /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1070         memcpy(&params, &ut_params, sizeof(params));
1071         params.name = "name";
1072         params.hash_func = NULL;
1073         handle = rte_hash_create(&params);
1074         if (handle == NULL) {
1075                 printf("Creating hash with null hash_func failed\n");
1076                 return -1;
1077         }
1078
1079         rte_hash_free(handle);
1080
1081         return 0;
1082 }
1083
1084 #define ITERATIONS 3
1085 /*
1086  * Test to see the average table utilization (entries added/max entries)
1087  * before hitting a random entry that cannot be added
1088  */
1089 static int test_average_table_utilization(void)
1090 {
1091         struct rte_hash *handle;
1092         uint8_t simple_key[MAX_KEYSIZE];
1093         unsigned i, j;
1094         unsigned added_keys, average_keys_added = 0;
1095         int ret;
1096
1097         printf("\n# Running test to determine average utilization"
1098                "\n  before adding elements begins to fail\n");
1099         printf("Measuring performance, please wait");
1100         fflush(stdout);
1101         ut_params.entries = 1 << 16;
1102         ut_params.name = "test_average_utilization";
1103         ut_params.hash_func = rte_jhash;
1104         handle = rte_hash_create(&ut_params);
1105         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1106
1107         for (j = 0; j < ITERATIONS; j++) {
1108                 ret = 0;
1109                 /* Add random entries until key cannot be added */
1110                 for (added_keys = 0; ret >= 0; added_keys++) {
1111                         for (i = 0; i < ut_params.key_len; i++)
1112                                 simple_key[i] = rte_rand() % 255;
1113                         ret = rte_hash_add_key(handle, simple_key);
1114                 }
1115                 if (ret != -ENOSPC) {
1116                         printf("Unexpected error when adding keys\n");
1117                         rte_hash_free(handle);
1118                         return -1;
1119                 }
1120
1121                 average_keys_added += added_keys;
1122
1123                 /* Reset the table */
1124                 rte_hash_reset(handle);
1125
1126                 /* Print a dot to show progress on operations */
1127                 printf(".");
1128                 fflush(stdout);
1129         }
1130
1131         average_keys_added /= ITERATIONS;
1132
1133         printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1134                 ((double) average_keys_added / ut_params.entries * 100),
1135                 average_keys_added, ut_params.entries);
1136         rte_hash_free(handle);
1137
1138         return 0;
1139 }
1140
1141 #define NUM_ENTRIES 256
1142 static int test_hash_iteration(void)
1143 {
1144         struct rte_hash *handle;
1145         unsigned i;
1146         uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1147         const void *next_key;
1148         void *next_data;
1149         void *data[NUM_ENTRIES];
1150         unsigned added_keys;
1151         uint32_t iter = 0;
1152         int ret = 0;
1153
1154         ut_params.entries = NUM_ENTRIES;
1155         ut_params.name = "test_hash_iteration";
1156         ut_params.hash_func = rte_jhash;
1157         ut_params.key_len = 16;
1158         handle = rte_hash_create(&ut_params);
1159         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1160
1161         /* Add random entries until key cannot be added */
1162         for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1163                 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1164                 for (i = 0; i < ut_params.key_len; i++)
1165                         keys[added_keys][i] = rte_rand() % 255;
1166                 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1167                 if (ret < 0)
1168                         break;
1169         }
1170
1171         /* Iterate through the hash table */
1172         while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1173                 /* Search for the key in the list of keys added */
1174                 for (i = 0; i < NUM_ENTRIES; i++) {
1175                         if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1176                                 if (next_data != data[i]) {
1177                                         printf("Data found in the hash table is"
1178                                                "not the data added with the key\n");
1179                                         goto err;
1180                                 }
1181                                 added_keys--;
1182                                 break;
1183                         }
1184                 }
1185                 if (i == NUM_ENTRIES) {
1186                         printf("Key found in the hash table was not added\n");
1187                         goto err;
1188                 }
1189         }
1190
1191         /* Check if all keys have been iterated */
1192         if (added_keys != 0) {
1193                 printf("There were still %u keys to iterate\n", added_keys);
1194                 goto err;
1195         }
1196
1197         rte_hash_free(handle);
1198         return 0;
1199
1200 err:
1201         rte_hash_free(handle);
1202         return -1;
1203 }
1204
1205 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1206                         0x04, 0x05, 0x06, 0x07,
1207                         0x08, 0x09, 0x0a, 0x0b,
1208                         0x0c, 0x0d, 0x0e, 0x0f};
1209 static struct rte_hash_parameters hash_params_ex = {
1210         .name = NULL,
1211         .entries = 64,
1212         .key_len = 0,
1213         .hash_func = NULL,
1214         .hash_func_init_val = 0,
1215         .socket_id = 0,
1216 };
1217
1218 /*
1219  * add/delete key with jhash2
1220  */
1221 static int
1222 test_hash_add_delete_jhash2(void)
1223 {
1224         int ret = -1;
1225         struct rte_hash *handle;
1226         int32_t pos1, pos2;
1227
1228         hash_params_ex.name = "hash_test_jhash2";
1229         hash_params_ex.key_len = 4;
1230         hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1231
1232         handle = rte_hash_create(&hash_params_ex);
1233         if (handle == NULL) {
1234                 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1235                 goto fail_jhash2;
1236         }
1237         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1238         if (pos1 < 0) {
1239                 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1240                 goto fail_jhash2;
1241         }
1242
1243         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1244         if (pos2 < 0 || pos1 != pos2) {
1245                 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1246                 goto fail_jhash2;
1247         }
1248         ret = 0;
1249
1250 fail_jhash2:
1251         if (handle != NULL)
1252                 rte_hash_free(handle);
1253
1254         return ret;
1255 }
1256
1257 /*
1258  * add/delete (2) key with jhash2
1259  */
1260 static int
1261 test_hash_add_delete_2_jhash2(void)
1262 {
1263         int ret = -1;
1264         struct rte_hash *handle;
1265         int32_t pos1, pos2;
1266
1267         hash_params_ex.name = "hash_test_2_jhash2";
1268         hash_params_ex.key_len = 8;
1269         hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1270
1271         handle = rte_hash_create(&hash_params_ex);
1272         if (handle == NULL)
1273                 goto fail_2_jhash2;
1274
1275         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1276         if (pos1 < 0)
1277                 goto fail_2_jhash2;
1278
1279         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1280         if (pos2 < 0 || pos1 != pos2)
1281                 goto fail_2_jhash2;
1282
1283         ret = 0;
1284
1285 fail_2_jhash2:
1286         if (handle != NULL)
1287                 rte_hash_free(handle);
1288
1289         return ret;
1290 }
1291
1292 static uint32_t
1293 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1294 {
1295         const uint32_t *k = key;
1296
1297         RTE_SET_USED(length);
1298
1299         return rte_jhash_1word(k[0], initval);
1300 }
1301
1302 static uint32_t
1303 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1304 {
1305         const uint32_t *k = key;
1306
1307         RTE_SET_USED(length);
1308
1309         return rte_jhash_2words(k[0], k[1], initval);
1310 }
1311
1312 static uint32_t
1313 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1314 {
1315         const uint32_t *k = key;
1316
1317         RTE_SET_USED(length);
1318
1319         return rte_jhash_3words(k[0], k[1], k[2], initval);
1320 }
1321
1322 /*
1323  * add/delete key with jhash 1word
1324  */
1325 static int
1326 test_hash_add_delete_jhash_1word(void)
1327 {
1328         int ret = -1;
1329         struct rte_hash *handle;
1330         int32_t pos1, pos2;
1331
1332         hash_params_ex.name = "hash_test_jhash_1word";
1333         hash_params_ex.key_len = 4;
1334         hash_params_ex.hash_func = test_hash_jhash_1word;
1335
1336         handle = rte_hash_create(&hash_params_ex);
1337         if (handle == NULL)
1338                 goto fail_jhash_1word;
1339
1340         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1341         if (pos1 < 0)
1342                 goto fail_jhash_1word;
1343
1344         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1345         if (pos2 < 0 || pos1 != pos2)
1346                 goto fail_jhash_1word;
1347
1348         ret = 0;
1349
1350 fail_jhash_1word:
1351         if (handle != NULL)
1352                 rte_hash_free(handle);
1353
1354         return ret;
1355 }
1356
1357 /*
1358  * add/delete key with jhash 2word
1359  */
1360 static int
1361 test_hash_add_delete_jhash_2word(void)
1362 {
1363         int ret = -1;
1364         struct rte_hash *handle;
1365         int32_t pos1, pos2;
1366
1367         hash_params_ex.name = "hash_test_jhash_2word";
1368         hash_params_ex.key_len = 8;
1369         hash_params_ex.hash_func = test_hash_jhash_2word;
1370
1371         handle = rte_hash_create(&hash_params_ex);
1372         if (handle == NULL)
1373                 goto fail_jhash_2word;
1374
1375         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1376         if (pos1 < 0)
1377                 goto fail_jhash_2word;
1378
1379         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1380         if (pos2 < 0 || pos1 != pos2)
1381                 goto fail_jhash_2word;
1382
1383         ret = 0;
1384
1385 fail_jhash_2word:
1386         if (handle != NULL)
1387                 rte_hash_free(handle);
1388
1389         return ret;
1390 }
1391
1392 /*
1393  * add/delete key with jhash 3word
1394  */
1395 static int
1396 test_hash_add_delete_jhash_3word(void)
1397 {
1398         int ret = -1;
1399         struct rte_hash *handle;
1400         int32_t pos1, pos2;
1401
1402         hash_params_ex.name = "hash_test_jhash_3word";
1403         hash_params_ex.key_len = 12;
1404         hash_params_ex.hash_func = test_hash_jhash_3word;
1405
1406         handle = rte_hash_create(&hash_params_ex);
1407         if (handle == NULL)
1408                 goto fail_jhash_3word;
1409
1410         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1411         if (pos1 < 0)
1412                 goto fail_jhash_3word;
1413
1414         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1415         if (pos2 < 0 || pos1 != pos2)
1416                 goto fail_jhash_3word;
1417
1418         ret = 0;
1419
1420 fail_jhash_3word:
1421         if (handle != NULL)
1422                 rte_hash_free(handle);
1423
1424         return ret;
1425 }
1426
1427 /*
1428  * Do all unit and performance tests.
1429  */
1430 static int
1431 test_hash(void)
1432 {
1433         if (test_add_delete() < 0)
1434                 return -1;
1435         if (test_hash_add_delete_jhash2() < 0)
1436                 return -1;
1437         if (test_hash_add_delete_2_jhash2() < 0)
1438                 return -1;
1439         if (test_hash_add_delete_jhash_1word() < 0)
1440                 return -1;
1441         if (test_hash_add_delete_jhash_2word() < 0)
1442                 return -1;
1443         if (test_hash_add_delete_jhash_3word() < 0)
1444                 return -1;
1445         if (test_hash_find_existing() < 0)
1446                 return -1;
1447         if (test_add_update_delete() < 0)
1448                 return -1;
1449         if (test_five_keys() < 0)
1450                 return -1;
1451         if (test_full_bucket() < 0)
1452                 return -1;
1453
1454         if (test_fbk_hash_find_existing() < 0)
1455                 return -1;
1456         if (fbk_hash_unit_test() < 0)
1457                 return -1;
1458         if (test_hash_creation_with_bad_parameters() < 0)
1459                 return -1;
1460         if (test_hash_creation_with_good_parameters() < 0)
1461                 return -1;
1462         if (test_average_table_utilization() < 0)
1463                 return -1;
1464         if (test_hash_iteration() < 0)
1465                 return -1;
1466
1467         run_hash_func_tests();
1468
1469         if (test_crc32_hash_alg_equiv() < 0)
1470                 return -1;
1471
1472         return 0;
1473 }
1474
1475 static struct test_command hash_cmd = {
1476         .command = "hash_autotest",
1477         .callback = test_hash,
1478 };
1479 REGISTER_TEST_COMMAND(hash_cmd);