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