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