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