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