doc: remove release version from known issues
[dpdk.git] / app / test / test_hash.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_common.h>
43 #include <rte_malloc.h>
44 #include <rte_cycles.h>
45 #include <rte_random.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_eal.h>
49 #include <rte_ip.h>
50 #include <rte_string_fns.h>
51
52 #include "test.h"
53
54 #include <rte_hash.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57 #include <rte_hash_crc.h>
58
59 /*******************************************************************************
60  * Hash function performance test configuration section. Each performance test
61  * will be performed HASHTEST_ITERATIONS times.
62  *
63  * The five arrays below control what tests are performed. Every combination
64  * from the array entries is tested.
65  */
66 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
67 static uint32_t hashtest_initvals[] = {0};
68 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
69 /******************************************************************************/
70 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
71
72 /*
73  * Check condition and return an error if true. Assumes that "handle" is the
74  * name of the hash structure pointer to be freed.
75  */
76 #define RETURN_IF_ERROR(cond, str, ...) do {                            \
77         if (cond) {                                                     \
78                 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
79                 if (handle) rte_hash_free(handle);                      \
80                 return -1;                                              \
81         }                                                               \
82 } while(0)
83
84 #define RETURN_IF_ERROR_FBK(cond, str, ...) do {                                \
85         if (cond) {                                                     \
86                 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
87                 if (handle) rte_fbk_hash_free(handle);                  \
88                 return -1;                                              \
89         }                                                               \
90 } while(0)
91
92 /* 5-tuple key type */
93 struct flow_key {
94         uint32_t ip_src;
95         uint32_t ip_dst;
96         uint16_t port_src;
97         uint16_t port_dst;
98         uint8_t proto;
99 } __attribute__((packed));
100
101 /*
102  * Hash function that always returns the same value, to easily test what
103  * happens when a bucket is full.
104  */
105 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys,
106                             __attribute__((unused)) uint32_t key_len,
107                             __attribute__((unused)) uint32_t init_val)
108 {
109         return 3;
110 }
111
112 /*
113  * Print out result of unit test hash operation.
114  */
115 #if defined(UNIT_TEST_HASH_VERBOSE)
116 static void print_key_info(const char *msg, const struct flow_key *key,
117                                                                 int32_t pos)
118 {
119         uint8_t *p = (uint8_t *)key;
120         unsigned i;
121
122         printf("%s key:0x", msg);
123         for (i = 0; i < sizeof(struct flow_key); i++) {
124                 printf("%02X", p[i]);
125         }
126         printf(" @ pos %d\n", pos);
127 }
128 #else
129 static void print_key_info(__attribute__((unused)) const char *msg,
130                 __attribute__((unused)) const struct flow_key *key,
131                 __attribute__((unused)) int32_t pos)
132 {
133 }
134 #endif
135
136 /* Keys used by unit test functions */
137 static struct flow_key keys[5] = { {
138         .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
139         .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
140         .port_src = 0x0908,
141         .port_dst = 0x0b0a,
142         .proto = 0x0c,
143 }, {
144         .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
145         .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
146         .port_src = 0x1918,
147         .port_dst = 0x1b1a,
148         .proto = 0x1c,
149 }, {
150         .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
151         .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
152         .port_src = 0x2928,
153         .port_dst = 0x2b2a,
154         .proto = 0x2c,
155 }, {
156         .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
157         .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
158         .port_src = 0x3938,
159         .port_dst = 0x3b3a,
160         .proto = 0x3c,
161 }, {
162         .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
163         .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
164         .port_src = 0x4948,
165         .port_dst = 0x4b4a,
166         .proto = 0x4c,
167 } };
168
169 /* Parameters used for hash table in unit test functions. Name set later. */
170 static struct rte_hash_parameters ut_params = {
171         .entries = 64,
172         .bucket_entries = 4,
173         .key_len = sizeof(struct flow_key), /* 13 */
174         .hash_func = rte_jhash,
175         .hash_func_init_val = 0,
176         .socket_id = 0,
177 };
178
179 #define CRC32_ITERATIONS (1U << 20)
180 #define CRC32_DWORDS (1U << 6)
181 /*
182  * Test if all CRC32 implementations yield the same hash value
183  */
184 static int
185 test_crc32_hash_alg_equiv(void)
186 {
187         uint32_t hash_val;
188         uint32_t init_val;
189         uint64_t data64[CRC32_DWORDS];
190         unsigned i, j;
191         size_t data_len;
192
193         printf("# CRC32 implementations equivalence test\n");
194         for (i = 0; i < CRC32_ITERATIONS; i++) {
195                 /* Randomizing data_len of data set */
196                 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1);
197                 init_val = (uint32_t) rte_rand();
198
199                 /* Fill the data set */
200                 for (j = 0; j < CRC32_DWORDS; j++)
201                         data64[j] = rte_rand();
202
203                 /* Calculate software CRC32 */
204                 rte_hash_crc_set_alg(CRC32_SW);
205                 hash_val = rte_hash_crc(data64, data_len, init_val);
206
207                 /* Check against 4-byte-operand sse4.2 CRC32 if available */
208                 rte_hash_crc_set_alg(CRC32_SSE42);
209                 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
210                         printf("Failed checking CRC32_SW against CRC32_SSE42\n");
211                         break;
212                 }
213
214                 /* Check against 8-byte-operand sse4.2 CRC32 if available */
215                 rte_hash_crc_set_alg(CRC32_SSE42_x64);
216                 if (hash_val != rte_hash_crc(data64, data_len, init_val)) {
217                         printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n");
218                         break;
219                 }
220         }
221
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[RTE_HASH_KEY_LENGTH_MAX];
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 find existing hash table
418  *
419  *  - create table
420  *  - find existing table: hit
421  *  - find non-existing table: miss
422  *
423  */
424 static int test_hash_find_existing(void)
425 {
426         struct rte_hash *handle = NULL, *result = NULL;
427
428         /* Create hash table. */
429         ut_params.name = "hash_find_existing";
430         handle = rte_hash_create(&ut_params);
431         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
432
433         /* Try to find existing hash table */
434         result = rte_hash_find_existing("hash_find_existing");
435         RETURN_IF_ERROR(result != handle, "could not find existing hash table");
436
437         /* Try to find non-existing hash table */
438         result = rte_hash_find_existing("hash_find_non_existing");
439         RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
440
441         /* Cleanup. */
442         rte_hash_free(handle);
443
444         return 0;
445 }
446
447 /*
448  * Sequence of operations for 5 keys
449  *      - add keys
450  *      - lookup keys: hit
451  *      - add keys (update)
452  *      - lookup keys: hit (updated data)
453  *      - delete keys : hit
454  *      - lookup keys: miss
455  */
456 static int test_five_keys(void)
457 {
458         struct rte_hash *handle;
459         const void *key_array[5] = {0};
460         int pos[5];
461         int expected_pos[5];
462         unsigned i;
463         int ret;
464
465         ut_params.name = "test3";
466         handle = rte_hash_create(&ut_params);
467         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
468
469         /* Add */
470         for (i = 0; i < 5; i++) {
471                 pos[i] = rte_hash_add_key(handle, &keys[i]);
472                 print_key_info("Add", &keys[i], pos[i]);
473                 RETURN_IF_ERROR(pos[i] < 0,
474                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
475                 expected_pos[i] = pos[i];
476         }
477
478         /* Lookup */
479         for(i = 0; i < 5; i++)
480                 key_array[i] = &keys[i];
481
482         ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
483         if(ret == 0)
484                 for(i = 0; i < 5; i++) {
485                         print_key_info("Lkp", key_array[i], pos[i]);
486                         RETURN_IF_ERROR(pos[i] != expected_pos[i],
487                                         "failed to find key (pos[%u]=%d)", i, pos[i]);
488                 }
489
490         /* Add - update */
491         for (i = 0; i < 5; i++) {
492                 pos[i] = rte_hash_add_key(handle, &keys[i]);
493                 print_key_info("Add", &keys[i], pos[i]);
494                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
495                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
496         }
497
498         /* Lookup */
499         for (i = 0; i < 5; i++) {
500                 pos[i] = rte_hash_lookup(handle, &keys[i]);
501                 print_key_info("Lkp", &keys[i], pos[i]);
502                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
503                                 "failed to find key (pos[%u]=%d)", i, pos[i]);
504         }
505
506         /* Delete */
507         for (i = 0; i < 5; i++) {
508                 pos[i] = rte_hash_del_key(handle, &keys[i]);
509                 print_key_info("Del", &keys[i], pos[i]);
510                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
511                                 "failed to delete key (pos[%u]=%d)", i, pos[i]);
512         }
513
514         /* Lookup */
515         for (i = 0; i < 5; i++) {
516                 pos[i] = rte_hash_lookup(handle, &keys[i]);
517                 print_key_info("Lkp", &keys[i], pos[i]);
518                 RETURN_IF_ERROR(pos[i] != -ENOENT,
519                                 "failed to find key (pos[%u]=%d)", i, pos[i]);
520         }
521
522         rte_hash_free(handle);
523
524         return 0;
525 }
526
527 /*
528  * Add keys to the same bucket until bucket full.
529  *      - add 5 keys to the same bucket (hash created with 4 keys per bucket):
530  *        first 4 successful, 5th unsuccessful
531  *      - lookup the 5 keys: 4 hits, 1 miss
532  *      - add the 5 keys again: 4 OK, one error as bucket is full
533  *      - lookup the 5 keys: 4 hits (updated data), 1 miss
534  *      - delete the 5 keys: 5 OK (even if the 5th is not in the table)
535  *      - lookup the 5 keys: 5 misses
536  *      - add the 5th key: OK
537  *      - lookup the 5th key: hit
538  */
539 static int test_full_bucket(void)
540 {
541         struct rte_hash_parameters params_pseudo_hash = {
542                 .name = "test4",
543                 .entries = 64,
544                 .bucket_entries = 4,
545                 .key_len = sizeof(struct flow_key), /* 13 */
546                 .hash_func = pseudo_hash,
547                 .hash_func_init_val = 0,
548                 .socket_id = 0,
549         };
550         struct rte_hash *handle;
551         int pos[5];
552         int expected_pos[5];
553         unsigned i;
554
555         handle = rte_hash_create(&params_pseudo_hash);
556         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
557
558         /* Fill bucket*/
559         for (i = 0; i < 4; i++) {
560                 pos[i] = rte_hash_add_key(handle, &keys[i]);
561                 print_key_info("Add", &keys[i], pos[i]);
562                 RETURN_IF_ERROR(pos[i] < 0,
563                         "failed to add key (pos[%u]=%d)", i, pos[i]);
564                 expected_pos[i] = pos[i];
565         }
566         /* This shouldn't work because the bucket is full */
567         pos[4] = rte_hash_add_key(handle, &keys[4]);
568         print_key_info("Add", &keys[4], pos[4]);
569         RETURN_IF_ERROR(pos[4] != -ENOSPC,
570                         "fail: added key to full bucket (pos[4]=%d)", pos[4]);
571
572         /* Lookup */
573         for (i = 0; i < 4; i++) {
574                 pos[i] = rte_hash_lookup(handle, &keys[i]);
575                 print_key_info("Lkp", &keys[i], pos[i]);
576                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
577                         "failed to find key (pos[%u]=%d)", i, pos[i]);
578         }
579         pos[4] = rte_hash_lookup(handle, &keys[4]);
580         print_key_info("Lkp", &keys[4], pos[4]);
581         RETURN_IF_ERROR(pos[4] != -ENOENT,
582                         "fail: found non-existent key (pos[4]=%d)", pos[4]);
583
584         /* Add - update */
585         for (i = 0; i < 4; i++) {
586                 pos[i] = rte_hash_add_key(handle, &keys[i]);
587                 print_key_info("Add", &keys[i], pos[i]);
588                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
589                         "failed to add key (pos[%u]=%d)", i, pos[i]);
590         }
591         pos[4] = rte_hash_add_key(handle, &keys[4]);
592         print_key_info("Add", &keys[4], pos[4]);
593         RETURN_IF_ERROR(pos[4] != -ENOSPC,
594                         "fail: added key to full bucket (pos[4]=%d)", pos[4]);
595
596         /* Lookup */
597         for (i = 0; i < 4; i++) {
598                 pos[i] = rte_hash_lookup(handle, &keys[i]);
599                 print_key_info("Lkp", &keys[i], pos[i]);
600                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
601                         "failed to find key (pos[%u]=%d)", i, pos[i]);
602         }
603         pos[4] = rte_hash_lookup(handle, &keys[4]);
604         print_key_info("Lkp", &keys[4], pos[4]);
605         RETURN_IF_ERROR(pos[4] != -ENOENT,
606                         "fail: found non-existent key (pos[4]=%d)", pos[4]);
607
608         /* Delete 1 key, check other keys are still found */
609         pos[1] = rte_hash_del_key(handle, &keys[1]);
610         print_key_info("Del", &keys[1], pos[1]);
611         RETURN_IF_ERROR(pos[1] != expected_pos[1],
612                         "failed to delete key (pos[1]=%d)", pos[1]);
613         pos[3] = rte_hash_lookup(handle, &keys[3]);
614         print_key_info("Lkp", &keys[3], pos[3]);
615         RETURN_IF_ERROR(pos[3] != expected_pos[3],
616                         "failed lookup after deleting key from same bucket "
617                         "(pos[3]=%d)", pos[3]);
618
619         /* Go back to previous state */
620         pos[1] = rte_hash_add_key(handle, &keys[1]);
621         print_key_info("Add", &keys[1], pos[1]);
622         expected_pos[1] = pos[1];
623         RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
624
625         /* Delete */
626         for (i = 0; i < 4; i++) {
627                 pos[i] = rte_hash_del_key(handle, &keys[i]);
628                 print_key_info("Del", &keys[i], pos[i]);
629                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
630                         "failed to delete key (pos[%u]=%d)", i, pos[i]);
631         }
632         pos[4] = rte_hash_del_key(handle, &keys[4]);
633         print_key_info("Del", &keys[4], pos[4]);
634         RETURN_IF_ERROR(pos[4] != -ENOENT,
635                         "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
636
637         /* Lookup */
638         for (i = 0; i < 4; i++) {
639                 pos[i] = rte_hash_lookup(handle, &keys[i]);
640                 print_key_info("Lkp", &keys[i], pos[i]);
641                 RETURN_IF_ERROR(pos[i] != -ENOENT,
642                         "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
643         }
644
645         /* Add and lookup the 5th key */
646         pos[4] = rte_hash_add_key(handle, &keys[4]);
647         print_key_info("Add", &keys[4], pos[4]);
648         RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
649         expected_pos[4] = pos[4];
650         pos[4] = rte_hash_lookup(handle, &keys[4]);
651         print_key_info("Lkp", &keys[4], pos[4]);
652         RETURN_IF_ERROR(pos[4] != expected_pos[4],
653                         "failed to find key (pos[4]=%d)", pos[4]);
654
655         rte_hash_free(handle);
656
657         /* Cover the NULL case. */
658         rte_hash_free(0);
659         return 0;
660 }
661
662 /******************************************************************************/
663 static int
664 fbk_hash_unit_test(void)
665 {
666         struct rte_fbk_hash_params params = {
667                 .name = "fbk_hash_test",
668                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
669                 .entries_per_bucket = 4,
670                 .socket_id = 0,
671         };
672
673         struct rte_fbk_hash_params invalid_params_1 = {
674                 .name = "invalid_1",
675                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
676                 .entries_per_bucket = 4,
677                 .socket_id = 0,
678         };
679
680         struct rte_fbk_hash_params invalid_params_2 = {
681                 .name = "invalid_2",
682                 .entries = 4,
683                 .entries_per_bucket = 3,         /* Not power of 2 */
684                 .socket_id = 0,
685         };
686
687         struct rte_fbk_hash_params invalid_params_3 = {
688                 .name = "invalid_3",
689                 .entries = 0,                    /* Entries is 0 */
690                 .entries_per_bucket = 4,
691                 .socket_id = 0,
692         };
693
694         struct rte_fbk_hash_params invalid_params_4 = {
695                 .name = "invalid_4",
696                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
697                 .entries_per_bucket = 0,         /* Entries per bucket is 0 */
698                 .socket_id = 0,
699         };
700
701         struct rte_fbk_hash_params invalid_params_5 = {
702                 .name = "invalid_5",
703                 .entries = 4,
704                 .entries_per_bucket = 8,         /* Entries per bucket > entries */
705                 .socket_id = 0,
706         };
707
708         struct rte_fbk_hash_params invalid_params_6 = {
709                 .name = "invalid_6",
710                 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2,   /* Entries > max allowed */
711                 .entries_per_bucket = 4,
712                 .socket_id = 0,
713         };
714
715         struct rte_fbk_hash_params invalid_params_7 = {
716                 .name = "invalid_7",
717                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
718                 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2,  /* Entries > max allowed */
719                 .socket_id = 0,
720         };
721
722         struct rte_fbk_hash_params invalid_params_8 = {
723                 .name = "invalid_7",
724                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
725                 .entries_per_bucket = 4,
726                 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
727         };
728
729         /* try to create two hashes with identical names
730          * in this case, trying to create a second one will not
731          * fail but will simply return pointer to the existing
732          * hash with that name. sort of like a "find hash by name" :-)
733          */
734         struct rte_fbk_hash_params invalid_params_same_name_1 = {
735                 .name = "same_name",                            /* hash with identical name */
736                 .entries = 4,
737                 .entries_per_bucket = 2,
738                 .socket_id = 0,
739         };
740
741         /* trying to create this hash should return a pointer to an existing hash */
742         struct rte_fbk_hash_params invalid_params_same_name_2 = {
743                 .name = "same_name",                            /* hash with identical name */
744                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
745                 .entries_per_bucket = 4,
746                 .socket_id = 0,
747         };
748
749         /* this is a sanity check for "same name" test
750          * creating this hash will check if we are actually able to create
751          * multiple hashes with different names (instead of having just one).
752          */
753         struct rte_fbk_hash_params different_name = {
754                 .name = "different_name",                       /* different name */
755                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
756                 .entries_per_bucket = 4,
757                 .socket_id = 0,
758         };
759
760         struct rte_fbk_hash_params params_jhash = {
761                 .name = "valid",
762                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
763                 .entries_per_bucket = 4,
764                 .socket_id = 0,
765                 .hash_func = rte_jhash_1word,              /* Tests for different hash_func */
766                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
767         };
768
769         struct rte_fbk_hash_params params_nohash = {
770                 .name = "valid nohash",
771                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
772                 .entries_per_bucket = 4,
773                 .socket_id = 0,
774                 .hash_func = NULL,                            /* Tests for null hash_func */
775                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
776         };
777
778         struct rte_fbk_hash_table *handle, *tmp;
779         uint32_t keys[5] =
780                 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
781         uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
782         int status;
783         unsigned i;
784         double used_entries;
785
786         /* Try creating hashes with invalid parameters */
787         printf("# Testing hash creation with invalid parameters "
788                         "- expert error msgs\n");
789         handle = rte_fbk_hash_create(&invalid_params_1);
790         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
791
792         handle = rte_fbk_hash_create(&invalid_params_2);
793         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
794
795         handle = rte_fbk_hash_create(&invalid_params_3);
796         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
797
798         handle = rte_fbk_hash_create(&invalid_params_4);
799         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
800
801         handle = rte_fbk_hash_create(&invalid_params_5);
802         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
803
804         handle = rte_fbk_hash_create(&invalid_params_6);
805         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
806
807         handle = rte_fbk_hash_create(&invalid_params_7);
808         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
809
810         handle = rte_fbk_hash_create(&invalid_params_8);
811         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
812
813         handle = rte_fbk_hash_create(&invalid_params_same_name_1);
814         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
815
816         tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
817         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
818         if (tmp != handle) {
819                         printf("ERROR line %d: hashes should have been the same\n", __LINE__);
820                         rte_fbk_hash_free(handle);
821                         rte_fbk_hash_free(tmp);
822                         return -1;
823         }
824
825         /* we are not freeing tmp or handle here because we need a hash list
826          * to be not empty for the next test */
827
828         /* create a hash in non-empty list - good for coverage */
829         tmp = rte_fbk_hash_create(&different_name);
830         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
831
832         /* free both hashes */
833         rte_fbk_hash_free(handle);
834         rte_fbk_hash_free(tmp);
835
836         /* Create empty jhash hash. */
837         handle = rte_fbk_hash_create(&params_jhash);
838         RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
839
840         /* Cleanup. */
841         rte_fbk_hash_free(handle);
842
843         /* Create empty jhash hash. */
844         handle = rte_fbk_hash_create(&params_nohash);
845         RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
846
847         /* Cleanup. */
848         rte_fbk_hash_free(handle);
849
850         /* Create empty hash. */
851         handle = rte_fbk_hash_create(&params);
852         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
853
854         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
855         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
856                                 "load factor right after creation is not zero but it should be");
857         /* Add keys. */
858         for (i = 0; i < 5; i++) {
859                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
860                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
861         }
862
863         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
864         RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
865                                 "load factor now is not as expected");
866         /* Find value of added keys. */
867         for (i = 0; i < 5; i++) {
868                 status = rte_fbk_hash_lookup(handle, keys[i]);
869                 RETURN_IF_ERROR_FBK(status != vals[i],
870                                 "fbk hash lookup failed");
871         }
872
873         /* Change value of added keys. */
874         for (i = 0; i < 5; i++) {
875                 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
876                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
877         }
878
879         /* Find new values. */
880         for (i = 0; i < 5; i++) {
881                 status = rte_fbk_hash_lookup(handle, keys[i]);
882                 RETURN_IF_ERROR_FBK(status != vals[4-i],
883                                 "fbk hash lookup failed");
884         }
885
886         /* Delete keys individually. */
887         for (i = 0; i < 5; i++) {
888                 status = rte_fbk_hash_delete_key(handle, keys[i]);
889                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
890         }
891
892         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
893         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
894                                 "load factor right after deletion is not zero but it should be");
895         /* Lookup should now fail. */
896         for (i = 0; i < 5; i++) {
897                 status = rte_fbk_hash_lookup(handle, keys[i]);
898                 RETURN_IF_ERROR_FBK(status == 0,
899                                 "fbk hash lookup should have failed");
900         }
901
902         /* Add keys again. */
903         for (i = 0; i < 5; i++) {
904                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
905                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
906         }
907
908         /* Make sure they were added. */
909         for (i = 0; i < 5; i++) {
910                 status = rte_fbk_hash_lookup(handle, keys[i]);
911                 RETURN_IF_ERROR_FBK(status != vals[i],
912                                 "fbk hash lookup failed");
913         }
914
915         /* Clear all entries. */
916         rte_fbk_hash_clear_all(handle);
917
918         /* Lookup should fail. */
919         for (i = 0; i < 5; i++) {
920                 status = rte_fbk_hash_lookup(handle, keys[i]);
921                 RETURN_IF_ERROR_FBK(status == 0,
922                                 "fbk hash lookup should have failed");
923         }
924
925         /* coverage */
926
927         /* fill up the hash_table */
928         for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
929                 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
930
931         /* Find non-existent key in a full hashtable */
932         status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
933         RETURN_IF_ERROR_FBK(status != -ENOENT,
934                         "fbk hash lookup succeeded");
935
936         /* Delete non-existent key in a full hashtable */
937         status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
938         RETURN_IF_ERROR_FBK(status != -ENOENT,
939                         "fbk hash delete succeeded");
940
941         /* Delete one key from a full hashtable */
942         status = rte_fbk_hash_delete_key(handle, 1);
943         RETURN_IF_ERROR_FBK(status != 0,
944                         "fbk hash delete failed");
945
946         /* Clear all entries. */
947         rte_fbk_hash_clear_all(handle);
948
949         /* Cleanup. */
950         rte_fbk_hash_free(handle);
951
952         /* Cover the NULL case. */
953         rte_fbk_hash_free(0);
954
955         return 0;
956 }
957
958 /*
959  * Sequence of operations for find existing fbk hash table
960  *
961  *  - create table
962  *  - find existing table: hit
963  *  - find non-existing table: miss
964  *
965  */
966 static int test_fbk_hash_find_existing(void)
967 {
968         struct rte_fbk_hash_params params = {
969                         .name = "fbk_hash_find_existing",
970                         .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
971                         .entries_per_bucket = 4,
972                         .socket_id = 0,
973         };
974         struct rte_fbk_hash_table *handle = NULL, *result = NULL;
975
976         /* Create hash table. */
977         handle = rte_fbk_hash_create(&params);
978         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
979
980         /* Try to find existing fbk hash table */
981         result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
982         RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
983
984         /* Try to find non-existing fbk hash table */
985         result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
986         RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
987
988         /* Cleanup. */
989         rte_fbk_hash_free(handle);
990
991         return 0;
992 }
993
994 /*
995  * Do tests for hash creation with bad parameters.
996  */
997 static int test_hash_creation_with_bad_parameters(void)
998 {
999         struct rte_hash *handle;
1000         struct rte_hash_parameters params;
1001
1002         handle = rte_hash_create(NULL);
1003         if (handle != NULL) {
1004                 rte_hash_free(handle);
1005                 printf("Impossible creating hash sucessfully without any parameter\n");
1006                 return -1;
1007         }
1008
1009         memcpy(&params, &ut_params, sizeof(params));
1010         params.name = "creation_with_bad_parameters_0";
1011         params.entries = RTE_HASH_ENTRIES_MAX + 1;
1012         handle = rte_hash_create(&params);
1013         if (handle != NULL) {
1014                 rte_hash_free(handle);
1015                 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
1016                 return -1;
1017         }
1018
1019         memcpy(&params, &ut_params, sizeof(params));
1020         params.name = "creation_with_bad_parameters_1";
1021         params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
1022         handle = rte_hash_create(&params);
1023         if (handle != NULL) {
1024                 rte_hash_free(handle);
1025                 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
1026                 return -1;
1027         }
1028
1029         memcpy(&params, &ut_params, sizeof(params));
1030         params.name = "creation_with_bad_parameters_2";
1031         params.entries = params.bucket_entries - 1;
1032         handle = rte_hash_create(&params);
1033         if (handle != NULL) {
1034                 rte_hash_free(handle);
1035                 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
1036                 return -1;
1037         }
1038
1039         memcpy(&params, &ut_params, sizeof(params));
1040         params.name = "creation_with_bad_parameters_3";
1041         params.entries = params.entries - 1;
1042         handle = rte_hash_create(&params);
1043         if (handle != NULL) {
1044                 rte_hash_free(handle);
1045                 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
1046                 return -1;
1047         }
1048
1049         memcpy(&params, &ut_params, sizeof(params));
1050         params.name = "creation_with_bad_parameters_4";
1051         params.bucket_entries = params.bucket_entries - 1;
1052         handle = rte_hash_create(&params);
1053         if (handle != NULL) {
1054                 rte_hash_free(handle);
1055                 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
1056                 return -1;
1057         }
1058
1059         memcpy(&params, &ut_params, sizeof(params));
1060         params.name = "creation_with_bad_parameters_5";
1061         params.key_len = 0;
1062         handle = rte_hash_create(&params);
1063         if (handle != NULL) {
1064                 rte_hash_free(handle);
1065                 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
1066                 return -1;
1067         }
1068
1069         memcpy(&params, &ut_params, sizeof(params));
1070         params.name = "creation_with_bad_parameters_6";
1071         params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
1072         handle = rte_hash_create(&params);
1073         if (handle != NULL) {
1074                 rte_hash_free(handle);
1075                 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
1076                 return -1;
1077         }
1078
1079         memcpy(&params, &ut_params, sizeof(params));
1080         params.name = "creation_with_bad_parameters_7";
1081         params.socket_id = RTE_MAX_NUMA_NODES + 1;
1082         handle = rte_hash_create(&params);
1083         if (handle != NULL) {
1084                 rte_hash_free(handle);
1085                 printf("Impossible creating hash sucessfully with invalid socket\n");
1086                 return -1;
1087         }
1088
1089         rte_hash_free(handle);
1090
1091         return 0;
1092 }
1093
1094 /*
1095  * Do tests for hash creation with parameters that look incorrect
1096  * but are actually valid.
1097  */
1098 static int
1099 test_hash_creation_with_good_parameters(void)
1100 {
1101         struct rte_hash *handle, *tmp;
1102         struct rte_hash_parameters params;
1103
1104         /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1105         memcpy(&params, &ut_params, sizeof(params));
1106         params.name = "same_name";
1107         params.hash_func = NULL;
1108         handle = rte_hash_create(&params);
1109         if (handle == NULL) {
1110                 printf("Creating hash with null hash_func failed\n");
1111                 return -1;
1112         }
1113         if (handle->hash_func == NULL) {
1114                 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1115                 return -1;
1116         }
1117
1118         /* this test is trying to create a hash with the same name as previous one.
1119          * this should return a pointer to the hash we previously created.
1120          * the previous hash isn't freed exactly for the purpose of it being in
1121          * the hash list.
1122          */
1123         memcpy(&params, &ut_params, sizeof(params));
1124         params.name = "same_name";
1125         tmp = rte_hash_create(&params);
1126
1127         /* check if the returned handle is actually equal to the previous hash */
1128         if (handle != tmp) {
1129                 rte_hash_free(handle);
1130                 rte_hash_free(tmp);
1131                 printf("Creating hash with existing name was successful\n");
1132                 return -1;
1133         }
1134
1135         /* try creating hash when there already are hashes in the list.
1136          * the previous hash is not freed to have a non-empty hash list.
1137          * the other hash that's in the list is still pointed to by "handle" var.
1138          */
1139         memcpy(&params, &ut_params, sizeof(params));
1140         params.name = "different_name";
1141         tmp = rte_hash_create(&params);
1142         if (tmp == NULL) {
1143                 rte_hash_free(handle);
1144                 printf("Creating hash with valid parameters failed\n");
1145                 return -1;
1146         }
1147
1148         rte_hash_free(tmp);
1149         rte_hash_free(handle);
1150
1151         return 0;
1152 }
1153
1154 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1155                         0x04, 0x05, 0x06, 0x07,
1156                         0x08, 0x09, 0x0a, 0x0b,
1157                         0x0c, 0x0d, 0x0e, 0x0f};
1158 static struct rte_hash_parameters hash_params_ex = {
1159         .name = NULL,
1160         .entries = 64,
1161         .bucket_entries = 4,
1162         .key_len = 0,
1163         .hash_func = NULL,
1164         .hash_func_init_val = 0,
1165         .socket_id = 0,
1166 };
1167
1168 /*
1169  * add/delete key with jhash2
1170  */
1171 static int
1172 test_hash_add_delete_jhash2(void)
1173 {
1174         int ret = -1;
1175         struct rte_hash *handle;
1176         int32_t pos1, pos2;
1177
1178         hash_params_ex.name = "hash_test_jhash2";
1179         hash_params_ex.key_len = 4;
1180         hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1181
1182         handle = rte_hash_create(&hash_params_ex);
1183         if (handle == NULL) {
1184                 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1185                 goto fail_jhash2;
1186         }
1187         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1188         if (pos1 < 0) {
1189                 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1190                 goto fail_jhash2;
1191         }
1192
1193         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1194         if (pos2 < 0 || pos1 != pos2) {
1195                 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1196                 goto fail_jhash2;
1197         }
1198         ret = 0;
1199
1200 fail_jhash2:
1201         if (handle != NULL)
1202                 rte_hash_free(handle);
1203
1204         return ret;
1205 }
1206
1207 /*
1208  * add/delete (2) key with jhash2
1209  */
1210 static int
1211 test_hash_add_delete_2_jhash2(void)
1212 {
1213         int ret = -1;
1214         struct rte_hash *handle;
1215         int32_t pos1, pos2;
1216
1217         hash_params_ex.name = "hash_test_2_jhash2";
1218         hash_params_ex.key_len = 8;
1219         hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1220
1221         handle = rte_hash_create(&hash_params_ex);
1222         if (handle == NULL)
1223                 goto fail_2_jhash2;
1224
1225         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1226         if (pos1 < 0)
1227                 goto fail_2_jhash2;
1228
1229         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1230         if (pos2 < 0 || pos1 != pos2)
1231                 goto fail_2_jhash2;
1232
1233         ret = 0;
1234
1235 fail_2_jhash2:
1236         if (handle != NULL)
1237                 rte_hash_free(handle);
1238
1239         return ret;
1240 }
1241
1242 static uint32_t
1243 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1244 {
1245         const uint32_t *k = key;
1246
1247         RTE_SET_USED(length);
1248
1249         return rte_jhash_1word(k[0], initval);
1250 }
1251
1252 static uint32_t
1253 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1254 {
1255         const uint32_t *k = key;
1256
1257         RTE_SET_USED(length);
1258
1259         return rte_jhash_2words(k[0], k[1], initval);
1260 }
1261
1262 static uint32_t
1263 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1264 {
1265         const uint32_t *k = key;
1266
1267         RTE_SET_USED(length);
1268
1269         return rte_jhash_3words(k[0], k[1], k[2], initval);
1270 }
1271
1272 /*
1273  * add/delete key with jhash 1word
1274  */
1275 static int
1276 test_hash_add_delete_jhash_1word(void)
1277 {
1278         int ret = -1;
1279         struct rte_hash *handle;
1280         int32_t pos1, pos2;
1281
1282         hash_params_ex.name = "hash_test_jhash_1word";
1283         hash_params_ex.key_len = 4;
1284         hash_params_ex.hash_func = test_hash_jhash_1word;
1285
1286         handle = rte_hash_create(&hash_params_ex);
1287         if (handle == NULL)
1288                 goto fail_jhash_1word;
1289
1290         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1291         if (pos1 < 0)
1292                 goto fail_jhash_1word;
1293
1294         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1295         if (pos2 < 0 || pos1 != pos2)
1296                 goto fail_jhash_1word;
1297
1298         ret = 0;
1299
1300 fail_jhash_1word:
1301         if (handle != NULL)
1302                 rte_hash_free(handle);
1303
1304         return ret;
1305 }
1306
1307 /*
1308  * add/delete key with jhash 2word
1309  */
1310 static int
1311 test_hash_add_delete_jhash_2word(void)
1312 {
1313         int ret = -1;
1314         struct rte_hash *handle;
1315         int32_t pos1, pos2;
1316
1317         hash_params_ex.name = "hash_test_jhash_2word";
1318         hash_params_ex.key_len = 8;
1319         hash_params_ex.hash_func = test_hash_jhash_2word;
1320
1321         handle = rte_hash_create(&hash_params_ex);
1322         if (handle == NULL)
1323                 goto fail_jhash_2word;
1324
1325         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1326         if (pos1 < 0)
1327                 goto fail_jhash_2word;
1328
1329         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1330         if (pos2 < 0 || pos1 != pos2)
1331                 goto fail_jhash_2word;
1332
1333         ret = 0;
1334
1335 fail_jhash_2word:
1336         if (handle != NULL)
1337                 rte_hash_free(handle);
1338
1339         return ret;
1340 }
1341
1342 /*
1343  * add/delete key with jhash 3word
1344  */
1345 static int
1346 test_hash_add_delete_jhash_3word(void)
1347 {
1348         int ret = -1;
1349         struct rte_hash *handle;
1350         int32_t pos1, pos2;
1351
1352         hash_params_ex.name = "hash_test_jhash_3word";
1353         hash_params_ex.key_len = 12;
1354         hash_params_ex.hash_func = test_hash_jhash_3word;
1355
1356         handle = rte_hash_create(&hash_params_ex);
1357         if (handle == NULL)
1358                 goto fail_jhash_3word;
1359
1360         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1361         if (pos1 < 0)
1362                 goto fail_jhash_3word;
1363
1364         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1365         if (pos2 < 0 || pos1 != pos2)
1366                 goto fail_jhash_3word;
1367
1368         ret = 0;
1369
1370 fail_jhash_3word:
1371         if (handle != NULL)
1372                 rte_hash_free(handle);
1373
1374         return ret;
1375 }
1376
1377 /*
1378  * Do all unit and performance tests.
1379  */
1380 static int
1381 test_hash(void)
1382 {
1383         if (test_add_delete() < 0)
1384                 return -1;
1385         if (test_hash_add_delete_jhash2() < 0)
1386                 return -1;
1387         if (test_hash_add_delete_2_jhash2() < 0)
1388                 return -1;
1389         if (test_hash_add_delete_jhash_1word() < 0)
1390                 return -1;
1391         if (test_hash_add_delete_jhash_2word() < 0)
1392                 return -1;
1393         if (test_hash_add_delete_jhash_3word() < 0)
1394                 return -1;
1395         if (test_hash_find_existing() < 0)
1396                 return -1;
1397         if (test_add_update_delete() < 0)
1398                 return -1;
1399         if (test_five_keys() < 0)
1400                 return -1;
1401         if (test_full_bucket() < 0)
1402                 return -1;
1403
1404         if (test_fbk_hash_find_existing() < 0)
1405                 return -1;
1406         if (fbk_hash_unit_test() < 0)
1407                 return -1;
1408         if (test_hash_creation_with_bad_parameters() < 0)
1409                 return -1;
1410         if (test_hash_creation_with_good_parameters() < 0)
1411                 return -1;
1412
1413         run_hash_func_tests();
1414
1415         if (test_crc32_hash_alg_equiv() < 0)
1416                 return -1;
1417
1418         return 0;
1419 }
1420
1421 static struct test_command hash_cmd = {
1422         .command = "hash_autotest",
1423         .callback = test_hash,
1424 };
1425 REGISTER_TEST_COMMAND(hash_cmd);