fe080c8ef41be69447224284648fc5d22e4728f0
[dpdk.git] / app / test / test_hash.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
54 #include <rte_hash.h>
55 #include <rte_fbk_hash.h>
56 #include <rte_jhash.h>
57
58 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
59 #include <rte_hash_crc.h>
60 #endif
61 #include <cmdline_parse.h>
62
63 #include "test.h"
64
65 #ifdef RTE_LIBRTE_HASH
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         int pos0, expectedPos0;
246
247         ut_params.name = "test1";
248         handle = rte_hash_create(&ut_params);
249         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
250
251         pos0 = rte_hash_add_key(handle, &keys[0]);
252         print_key_info("Add", &keys[0], pos0);
253         RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
254         expectedPos0 = pos0;
255
256         pos0 = rte_hash_lookup(handle, &keys[0]);
257         print_key_info("Lkp", &keys[0], pos0);
258         RETURN_IF_ERROR(pos0 != expectedPos0,
259                         "failed to find key (pos0=%d)", pos0);
260
261         pos0 = rte_hash_del_key(handle, &keys[0]);
262         print_key_info("Del", &keys[0], pos0);
263         RETURN_IF_ERROR(pos0 != expectedPos0,
264                         "failed to delete key (pos0=%d)", pos0);
265
266         pos0 = rte_hash_lookup(handle, &keys[0]);
267         print_key_info("Lkp", &keys[0], pos0);
268         RETURN_IF_ERROR(pos0 != -ENOENT,
269                         "fail: found key after deleting! (pos0=%d)", pos0);
270
271         rte_hash_free(handle);
272         return 0;
273 }
274
275 /*
276  * Sequence of operations for a single key:
277  *      - delete: miss
278  *      - add
279  *      - lookup: hit
280  *      - add: update
281  *      - lookup: hit (updated data)
282  *      - delete: hit
283  *      - delete: miss
284  *      - lookup: miss
285  */
286 static int test_add_update_delete(void)
287 {
288         struct rte_hash *handle;
289         int pos0, expectedPos0;
290
291         ut_params.name = "test2";
292         handle = rte_hash_create(&ut_params);
293         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
294
295         pos0 = rte_hash_del_key(handle, &keys[0]);
296         print_key_info("Del", &keys[0], pos0);
297         RETURN_IF_ERROR(pos0 != -ENOENT,
298                         "fail: found non-existent key (pos0=%d)", pos0);
299
300         pos0 = rte_hash_add_key(handle, &keys[0]);
301         print_key_info("Add", &keys[0], pos0);
302         RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0);
303         expectedPos0 = pos0;
304
305         pos0 = rte_hash_lookup(handle, &keys[0]);
306         print_key_info("Lkp", &keys[0], pos0);
307         RETURN_IF_ERROR(pos0 != expectedPos0,
308                         "failed to find key (pos0=%d)", pos0);
309
310         pos0 = rte_hash_add_key(handle, &keys[0]);
311         print_key_info("Add", &keys[0], pos0);
312         RETURN_IF_ERROR(pos0 != expectedPos0,
313                         "failed to re-add key (pos0=%d)", pos0);
314
315         pos0 = rte_hash_lookup(handle, &keys[0]);
316         print_key_info("Lkp", &keys[0], pos0);
317         RETURN_IF_ERROR(pos0 != expectedPos0,
318                         "failed to find key (pos0=%d)", pos0);
319
320         pos0 = rte_hash_del_key(handle, &keys[0]);
321         print_key_info("Del", &keys[0], pos0);
322         RETURN_IF_ERROR(pos0 != expectedPos0,
323                         "failed to delete key (pos0=%d)", pos0);
324
325         pos0 = rte_hash_del_key(handle, &keys[0]);
326         print_key_info("Del", &keys[0], pos0);
327         RETURN_IF_ERROR(pos0 != -ENOENT,
328                         "fail: deleted already deleted key (pos0=%d)", pos0);
329
330         pos0 = rte_hash_lookup(handle, &keys[0]);
331         print_key_info("Lkp", &keys[0], pos0);
332         RETURN_IF_ERROR(pos0 != -ENOENT,
333                         "fail: found key after deleting! (pos0=%d)", pos0);
334
335         rte_hash_free(handle);
336         return 0;
337 }
338
339 /*
340  * Sequence of operations for find existing hash table
341  *
342  *  - create table
343  *  - find existing table: hit
344  *  - find non-existing table: miss
345  *
346  */
347 static int test_hash_find_existing(void)
348 {
349         struct rte_hash *handle = NULL, *result = NULL;
350
351         /* Create hash table. */
352         ut_params.name = "hash_find_existing";
353         handle = rte_hash_create(&ut_params);
354         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
355
356         /* Try to find existing hash table */
357         result = rte_hash_find_existing("hash_find_existing");
358         RETURN_IF_ERROR(result != handle, "could not find existing hash table");
359
360         /* Try to find non-existing hash table */
361         result = rte_hash_find_existing("hash_find_non_existing");
362         RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist");
363
364         /* Cleanup. */
365         rte_hash_free(handle);
366
367         return 0;
368 }
369
370 /*
371  * Sequence of operations for 5 keys
372  *      - add keys
373  *      - lookup keys: hit
374  *      - add keys (update)
375  *      - lookup keys: hit (updated data)
376  *      - delete keys : hit
377  *      - lookup keys: miss
378  */
379 static int test_five_keys(void)
380 {
381         struct rte_hash *handle;
382         const void *key_array[5] = {0};
383         int pos[5];
384         int expected_pos[5];
385         unsigned i;
386         int ret;
387
388         ut_params.name = "test3";
389         handle = rte_hash_create(&ut_params);
390         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
391
392         /* Add */
393         for (i = 0; i < 5; i++) {
394                 pos[i] = rte_hash_add_key(handle, &keys[i]);
395                 print_key_info("Add", &keys[i], pos[i]);
396                 RETURN_IF_ERROR(pos[i] < 0,
397                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
398                 expected_pos[i] = pos[i];
399         }
400
401         /* Lookup */
402         for(i = 0; i < 5; i++)
403                 key_array[i] = &keys[i];
404
405         ret = rte_hash_lookup_multi(handle, &key_array[0], 5, (int32_t *)pos);
406         if(ret == 0)
407                 for(i = 0; i < 5; i++) {
408                         print_key_info("Lkp", key_array[i], pos[i]);
409                         RETURN_IF_ERROR(pos[i] != expected_pos[i],
410                                         "failed to find key (pos[%u]=%d)", i, pos[i]);
411                 }
412
413         /* Add - update */
414         for (i = 0; i < 5; i++) {
415                 pos[i] = rte_hash_add_key(handle, &keys[i]);
416                 print_key_info("Add", &keys[i], pos[i]);
417                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
418                                 "failed to add key (pos[%u]=%d)", i, pos[i]);
419         }
420
421         /* Lookup */
422         for (i = 0; i < 5; i++) {
423                 pos[i] = rte_hash_lookup(handle, &keys[i]);
424                 print_key_info("Lkp", &keys[i], pos[i]);
425                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
426                                 "failed to find key (pos[%u]=%d)", i, pos[i]);
427         }
428
429         /* Delete */
430         for (i = 0; i < 5; i++) {
431                 pos[i] = rte_hash_del_key(handle, &keys[i]);
432                 print_key_info("Del", &keys[i], pos[i]);
433                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
434                                 "failed to delete key (pos[%u]=%d)", i, pos[i]);
435         }
436
437         /* Lookup */
438         for (i = 0; i < 5; i++) {
439                 pos[i] = rte_hash_lookup(handle, &keys[i]);
440                 print_key_info("Lkp", &keys[i], pos[i]);
441                 RETURN_IF_ERROR(pos[i] != -ENOENT,
442                                 "failed to find key (pos[%u]=%d)", i, pos[i]);
443         }
444
445         rte_hash_free(handle);
446
447         return 0;
448 }
449
450 /*
451  * Add keys to the same bucket until bucket full.
452  *      - add 5 keys to the same bucket (hash created with 4 keys per bucket):
453  *        first 4 successful, 5th unsuccessful
454  *      - lookup the 5 keys: 4 hits, 1 miss
455  *      - add the 5 keys again: 4 OK, one error as bucket is full
456  *      - lookup the 5 keys: 4 hits (updated data), 1 miss
457  *      - delete the 5 keys: 5 OK (even if the 5th is not in the table)
458  *      - lookup the 5 keys: 5 misses
459  *      - add the 5th key: OK
460  *      - lookup the 5th key: hit
461  */
462 static int test_full_bucket(void)
463 {
464         struct rte_hash_parameters params_pseudo_hash = {
465                 .name = "test4",
466                 .entries = 64,
467                 .bucket_entries = 4,
468                 .key_len = sizeof(struct flow_key), /* 13 */
469                 .hash_func = pseudo_hash,
470                 .hash_func_init_val = 0,
471                 .socket_id = 0,
472         };
473         struct rte_hash *handle;
474         int pos[5];
475         int expected_pos[5];
476         unsigned i;
477
478         handle = rte_hash_create(&params_pseudo_hash);
479         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
480
481         /* Fill bucket*/
482         for (i = 0; i < 4; i++) {
483                 pos[i] = rte_hash_add_key(handle, &keys[i]);
484                 print_key_info("Add", &keys[i], pos[i]);
485                 RETURN_IF_ERROR(pos[i] < 0,
486                         "failed to add key (pos[%u]=%d)", i, pos[i]);
487                 expected_pos[i] = pos[i];
488         }
489         /* This shouldn't work because the bucket is full */
490         pos[4] = rte_hash_add_key(handle, &keys[4]);
491         print_key_info("Add", &keys[4], pos[4]);
492         RETURN_IF_ERROR(pos[4] != -ENOSPC,
493                         "fail: added key to full bucket (pos[4]=%d)", pos[4]);
494
495         /* Lookup */
496         for (i = 0; i < 4; i++) {
497                 pos[i] = rte_hash_lookup(handle, &keys[i]);
498                 print_key_info("Lkp", &keys[i], pos[i]);
499                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
500                         "failed to find key (pos[%u]=%d)", i, pos[i]);
501         }
502         pos[4] = rte_hash_lookup(handle, &keys[4]);
503         print_key_info("Lkp", &keys[4], pos[4]);
504         RETURN_IF_ERROR(pos[4] != -ENOENT,
505                         "fail: found non-existent key (pos[4]=%d)", pos[4]);
506
507         /* Add - update */
508         for (i = 0; i < 4; i++) {
509                 pos[i] = rte_hash_add_key(handle, &keys[i]);
510                 print_key_info("Add", &keys[i], pos[i]);
511                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
512                         "failed to add key (pos[%u]=%d)", i, pos[i]);
513         }
514         pos[4] = rte_hash_add_key(handle, &keys[4]);
515         print_key_info("Add", &keys[4], pos[4]);
516         RETURN_IF_ERROR(pos[4] != -ENOSPC,
517                         "fail: added key to full bucket (pos[4]=%d)", pos[4]);
518
519         /* Lookup */
520         for (i = 0; i < 4; i++) {
521                 pos[i] = rte_hash_lookup(handle, &keys[i]);
522                 print_key_info("Lkp", &keys[i], pos[i]);
523                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
524                         "failed to find key (pos[%u]=%d)", i, pos[i]);
525         }
526         pos[4] = rte_hash_lookup(handle, &keys[4]);
527         print_key_info("Lkp", &keys[4], pos[4]);
528         RETURN_IF_ERROR(pos[4] != -ENOENT,
529                         "fail: found non-existent key (pos[4]=%d)", pos[4]);
530
531         /* Delete 1 key, check other keys are still found */
532         pos[1] = rte_hash_del_key(handle, &keys[1]);
533         print_key_info("Del", &keys[1], pos[1]);
534         RETURN_IF_ERROR(pos[1] != expected_pos[1],
535                         "failed to delete key (pos[1]=%d)", pos[1]);
536         pos[3] = rte_hash_lookup(handle, &keys[3]);
537         print_key_info("Lkp", &keys[3], pos[3]);
538         RETURN_IF_ERROR(pos[3] != expected_pos[3],
539                         "failed lookup after deleting key from same bucket "
540                         "(pos[3]=%d)", pos[3]);
541
542         /* Go back to previous state */
543         pos[1] = rte_hash_add_key(handle, &keys[1]);
544         print_key_info("Add", &keys[1], pos[1]);
545         expected_pos[1] = pos[1];
546         RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]);
547
548         /* Delete */
549         for (i = 0; i < 4; i++) {
550                 pos[i] = rte_hash_del_key(handle, &keys[i]);
551                 print_key_info("Del", &keys[i], pos[i]);
552                 RETURN_IF_ERROR(pos[i] != expected_pos[i],
553                         "failed to delete key (pos[%u]=%d)", i, pos[i]);
554         }
555         pos[4] = rte_hash_del_key(handle, &keys[4]);
556         print_key_info("Del", &keys[4], pos[4]);
557         RETURN_IF_ERROR(pos[4] != -ENOENT,
558                         "fail: deleted non-existent key (pos[4]=%d)", pos[4]);
559
560         /* Lookup */
561         for (i = 0; i < 4; i++) {
562                 pos[i] = rte_hash_lookup(handle, &keys[i]);
563                 print_key_info("Lkp", &keys[i], pos[i]);
564                 RETURN_IF_ERROR(pos[i] != -ENOENT,
565                         "fail: found non-existent key (pos[%u]=%d)", i, pos[i]);
566         }
567
568         /* Add and lookup the 5th key */
569         pos[4] = rte_hash_add_key(handle, &keys[4]);
570         print_key_info("Add", &keys[4], pos[4]);
571         RETURN_IF_ERROR(pos[4] < 0, "failed to add key (pos[4]=%d)", pos[4]);
572         expected_pos[4] = pos[4];
573         pos[4] = rte_hash_lookup(handle, &keys[4]);
574         print_key_info("Lkp", &keys[4], pos[4]);
575         RETURN_IF_ERROR(pos[4] != expected_pos[4],
576                         "failed to find key (pos[4]=%d)", pos[4]);
577
578         rte_hash_free(handle);
579
580         /* Cover the NULL case. */
581         rte_hash_free(0);
582         return 0;
583 }
584
585 /******************************************************************************/
586 static int
587 fbk_hash_unit_test(void)
588 {
589         struct rte_fbk_hash_params params = {
590                 .name = "fbk_hash_test",
591                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
592                 .entries_per_bucket = 4,
593                 .socket_id = 0,
594         };
595
596         struct rte_fbk_hash_params invalid_params_1 = {
597                 .name = "invalid_1",
598                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
599                 .entries_per_bucket = 4,
600                 .socket_id = 0,
601         };
602
603         struct rte_fbk_hash_params invalid_params_2 = {
604                 .name = "invalid_2",
605                 .entries = 4,
606                 .entries_per_bucket = 3,         /* Not power of 2 */
607                 .socket_id = 0,
608         };
609
610         struct rte_fbk_hash_params invalid_params_3 = {
611                 .name = "invalid_3",
612                 .entries = 0,                    /* Entries is 0 */
613                 .entries_per_bucket = 4,
614                 .socket_id = 0,
615         };
616
617         struct rte_fbk_hash_params invalid_params_4 = {
618                 .name = "invalid_4",
619                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
620                 .entries_per_bucket = 0,         /* Entries per bucket is 0 */
621                 .socket_id = 0,
622         };
623
624         struct rte_fbk_hash_params invalid_params_5 = {
625                 .name = "invalid_5",
626                 .entries = 4,
627                 .entries_per_bucket = 8,         /* Entries per bucket > entries */
628                 .socket_id = 0,
629         };
630
631         struct rte_fbk_hash_params invalid_params_6 = {
632                 .name = "invalid_6",
633                 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2,   /* Entries > max allowed */
634                 .entries_per_bucket = 4,
635                 .socket_id = 0,
636         };
637
638         struct rte_fbk_hash_params invalid_params_7 = {
639                 .name = "invalid_7",
640                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
641                 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2,  /* Entries > max allowed */
642                 .socket_id = 0,
643         };
644
645         struct rte_fbk_hash_params invalid_params_8 = {
646                 .name = "invalid_7",
647                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
648                 .entries_per_bucket = 4,
649                 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
650         };
651
652         /* try to create two hashes with identical names
653          * in this case, trying to create a second one will not
654          * fail but will simply return pointer to the existing
655          * hash with that name. sort of like a "find hash by name" :-)
656          */
657         struct rte_fbk_hash_params invalid_params_same_name_1 = {
658                 .name = "same_name",                            /* hash with identical name */
659                 .entries = 4,
660                 .entries_per_bucket = 2,
661                 .socket_id = 0,
662         };
663
664         /* trying to create this hash should return a pointer to an existing hash */
665         struct rte_fbk_hash_params invalid_params_same_name_2 = {
666                 .name = "same_name",                            /* hash with identical name */
667                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
668                 .entries_per_bucket = 4,
669                 .socket_id = 0,
670         };
671
672         /* this is a sanity check for "same name" test
673          * creating this hash will check if we are actually able to create
674          * multiple hashes with different names (instead of having just one).
675          */
676         struct rte_fbk_hash_params different_name = {
677                 .name = "different_name",                       /* different name */
678                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
679                 .entries_per_bucket = 4,
680                 .socket_id = 0,
681         };
682
683         struct rte_fbk_hash_params params_jhash = {
684                 .name = "valid",
685                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
686                 .entries_per_bucket = 4,
687                 .socket_id = 0,
688                 .hash_func = rte_jhash_1word,              /* Tests for different hash_func */
689                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
690         };
691
692         struct rte_fbk_hash_params params_nohash = {
693                 .name = "valid nohash",
694                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
695                 .entries_per_bucket = 4,
696                 .socket_id = 0,
697                 .hash_func = NULL,                            /* Tests for null hash_func */
698                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
699         };
700
701         struct rte_fbk_hash_table *handle, *tmp;
702         uint32_t keys[5] =
703                 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
704         uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
705         int status;
706         unsigned i;
707         double used_entries;
708
709         /* Try creating hashes with invalid parameters */
710         handle = rte_fbk_hash_create(&invalid_params_1);
711         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
712
713         handle = rte_fbk_hash_create(&invalid_params_2);
714         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
715
716         handle = rte_fbk_hash_create(&invalid_params_3);
717         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
718
719         handle = rte_fbk_hash_create(&invalid_params_4);
720         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
721
722         handle = rte_fbk_hash_create(&invalid_params_5);
723         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
724
725         handle = rte_fbk_hash_create(&invalid_params_6);
726         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
727
728         handle = rte_fbk_hash_create(&invalid_params_7);
729         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
730
731         handle = rte_fbk_hash_create(&invalid_params_8);
732         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
733
734         handle = rte_fbk_hash_create(&invalid_params_same_name_1);
735         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
736
737         tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
738         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
739         if (tmp != handle) {
740                         printf("ERROR line %d: hashes should have been the same\n", __LINE__);
741                         rte_fbk_hash_free(handle);
742                         rte_fbk_hash_free(tmp);
743                         return -1;
744         }
745
746         /* we are not freeing tmp or handle here because we need a hash list
747          * to be not empty for the next test */
748
749         /* create a hash in non-empty list - good for coverage */
750         tmp = rte_fbk_hash_create(&different_name);
751         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
752
753         /* free both hashes */
754         rte_fbk_hash_free(handle);
755         rte_fbk_hash_free(tmp);
756
757         /* Create empty jhash hash. */
758         handle = rte_fbk_hash_create(&params_jhash);
759         RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
760
761         /* Cleanup. */
762         rte_fbk_hash_free(handle);
763
764         /* Create empty jhash hash. */
765         handle = rte_fbk_hash_create(&params_nohash);
766         RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
767
768         /* Cleanup. */
769         rte_fbk_hash_free(handle);
770
771         /* Create empty hash. */
772         handle = rte_fbk_hash_create(&params);
773         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
774
775         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
776         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
777                                 "load factor right after creation is not zero but it should be");
778         /* Add keys. */
779         for (i = 0; i < 5; i++) {
780                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
781                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
782         }
783
784         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
785         RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
786                                 "load factor now is not as expected");
787         /* Find value of added keys. */
788         for (i = 0; i < 5; i++) {
789                 status = rte_fbk_hash_lookup(handle, keys[i]);
790                 RETURN_IF_ERROR_FBK(status != vals[i],
791                                 "fbk hash lookup failed");
792         }
793
794         /* Change value of added keys. */
795         for (i = 0; i < 5; i++) {
796                 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
797                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
798         }
799
800         /* Find new values. */
801         for (i = 0; i < 5; i++) {
802                 status = rte_fbk_hash_lookup(handle, keys[i]);
803                 RETURN_IF_ERROR_FBK(status != vals[4-i],
804                                 "fbk hash lookup failed");
805         }
806
807         /* Delete keys individually. */
808         for (i = 0; i < 5; i++) {
809                 status = rte_fbk_hash_delete_key(handle, keys[i]);
810                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
811         }
812
813         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
814         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
815                                 "load factor right after deletion is not zero but it should be");
816         /* Lookup should now fail. */
817         for (i = 0; i < 5; i++) {
818                 status = rte_fbk_hash_lookup(handle, keys[i]);
819                 RETURN_IF_ERROR_FBK(status == 0,
820                                 "fbk hash lookup should have failed");
821         }
822
823         /* Add keys again. */
824         for (i = 0; i < 5; i++) {
825                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
826                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
827         }
828
829         /* Make sure they were added. */
830         for (i = 0; i < 5; i++) {
831                 status = rte_fbk_hash_lookup(handle, keys[i]);
832                 RETURN_IF_ERROR_FBK(status != vals[i],
833                                 "fbk hash lookup failed");
834         }
835
836         /* Clear all entries. */
837         rte_fbk_hash_clear_all(handle);
838
839         /* Lookup should fail. */
840         for (i = 0; i < 5; i++) {
841                 status = rte_fbk_hash_lookup(handle, keys[i]);
842                 RETURN_IF_ERROR_FBK(status == 0,
843                                 "fbk hash lookup should have failed");
844         }
845
846         /* coverage */
847
848         /* fill up the hash_table */
849         for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
850                 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
851
852         /* Find non-existent key in a full hashtable */
853         status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
854         RETURN_IF_ERROR_FBK(status != -ENOENT,
855                         "fbk hash lookup succeeded");
856
857         /* Delete non-existent key in a full hashtable */
858         status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
859         RETURN_IF_ERROR_FBK(status != -ENOENT,
860                         "fbk hash delete succeeded");
861
862         /* Delete one key from a full hashtable */
863         status = rte_fbk_hash_delete_key(handle, 1);
864         RETURN_IF_ERROR_FBK(status != 0,
865                         "fbk hash delete failed");
866
867         /* Clear all entries. */
868         rte_fbk_hash_clear_all(handle);
869
870         /* Cleanup. */
871         rte_fbk_hash_free(handle);
872
873         /* Cover the NULL case. */
874         rte_fbk_hash_free(0);
875
876         return 0;
877 }
878
879 /*
880  * Sequence of operations for find existing fbk hash table
881  *
882  *  - create table
883  *  - find existing table: hit
884  *  - find non-existing table: miss
885  *
886  */
887 static int test_fbk_hash_find_existing(void)
888 {
889         struct rte_fbk_hash_params params = {
890                         .name = "fbk_hash_find_existing",
891                         .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
892                         .entries_per_bucket = 4,
893                         .socket_id = 0,
894         };
895         struct rte_fbk_hash_table *handle = NULL, *result = NULL;
896
897         /* Create hash table. */
898         handle = rte_fbk_hash_create(&params);
899         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
900
901         /* Try to find existing fbk hash table */
902         result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
903         RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
904
905         /* Try to find non-existing fbk hash table */
906         result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
907         RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
908
909         /* Cleanup. */
910         rte_fbk_hash_free(handle);
911
912         return 0;
913 }
914
915 /*
916  * Do tests for hash creation with bad parameters.
917  */
918 static int test_hash_creation_with_bad_parameters(void)
919 {
920         struct rte_hash *handle;
921         struct rte_hash_parameters params;
922
923         handle = rte_hash_create(NULL);
924         if (handle != NULL) {
925                 rte_hash_free(handle);
926                 printf("Impossible creating hash sucessfully without any parameter\n");
927                 return -1;
928         }
929
930         memcpy(&params, &ut_params, sizeof(params));
931         params.name = "creation_with_bad_parameters_0";
932         params.entries = RTE_HASH_ENTRIES_MAX + 1;
933         handle = rte_hash_create(&params);
934         if (handle != NULL) {
935                 rte_hash_free(handle);
936                 printf("Impossible creating hash sucessfully with entries in parameter exceeded\n");
937                 return -1;
938         }
939
940         memcpy(&params, &ut_params, sizeof(params));
941         params.name = "creation_with_bad_parameters_1";
942         params.bucket_entries = RTE_HASH_BUCKET_ENTRIES_MAX + 1;
943         handle = rte_hash_create(&params);
944         if (handle != NULL) {
945                 rte_hash_free(handle);
946                 printf("Impossible creating hash sucessfully with bucket_entries in parameter exceeded\n");
947                 return -1;
948         }
949
950         memcpy(&params, &ut_params, sizeof(params));
951         params.name = "creation_with_bad_parameters_2";
952         params.entries = params.bucket_entries - 1;
953         handle = rte_hash_create(&params);
954         if (handle != NULL) {
955                 rte_hash_free(handle);
956                 printf("Impossible creating hash sucessfully if entries less than bucket_entries in parameter\n");
957                 return -1;
958         }
959
960         memcpy(&params, &ut_params, sizeof(params));
961         params.name = "creation_with_bad_parameters_3";
962         params.entries = params.entries - 1;
963         handle = rte_hash_create(&params);
964         if (handle != NULL) {
965                 rte_hash_free(handle);
966                 printf("Impossible creating hash sucessfully if entries in parameter is not power of 2\n");
967                 return -1;
968         }
969
970         memcpy(&params, &ut_params, sizeof(params));
971         params.name = "creation_with_bad_parameters_4";
972         params.bucket_entries = params.bucket_entries - 1;
973         handle = rte_hash_create(&params);
974         if (handle != NULL) {
975                 rte_hash_free(handle);
976                 printf("Impossible creating hash sucessfully if bucket_entries in parameter is not power of 2\n");
977                 return -1;
978         }
979
980         memcpy(&params, &ut_params, sizeof(params));
981         params.name = "creation_with_bad_parameters_5";
982         params.key_len = 0;
983         handle = rte_hash_create(&params);
984         if (handle != NULL) {
985                 rte_hash_free(handle);
986                 printf("Impossible creating hash sucessfully if key_len in parameter is zero\n");
987                 return -1;
988         }
989
990         memcpy(&params, &ut_params, sizeof(params));
991         params.name = "creation_with_bad_parameters_6";
992         params.key_len = RTE_HASH_KEY_LENGTH_MAX + 1;
993         handle = rte_hash_create(&params);
994         if (handle != NULL) {
995                 rte_hash_free(handle);
996                 printf("Impossible creating hash sucessfully if key_len is greater than the maximum\n");
997                 return -1;
998         }
999
1000         memcpy(&params, &ut_params, sizeof(params));
1001         params.name = "creation_with_bad_parameters_7";
1002         params.socket_id = RTE_MAX_NUMA_NODES + 1;
1003         handle = rte_hash_create(&params);
1004         if (handle != NULL) {
1005                 rte_hash_free(handle);
1006                 printf("Impossible creating hash sucessfully with invalid socket\n");
1007                 return -1;
1008         }
1009
1010         rte_hash_free(handle);
1011
1012         return 0;
1013 }
1014
1015 /*
1016  * Do tests for hash creation with parameters that look incorrect
1017  * but are actually valid.
1018  */
1019 static int
1020 test_hash_creation_with_good_parameters(void)
1021 {
1022         struct rte_hash *handle, *tmp;
1023         struct rte_hash_parameters params;
1024
1025         /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1026         memcpy(&params, &ut_params, sizeof(params));
1027         params.name = "same_name";
1028         params.hash_func = NULL;
1029         handle = rte_hash_create(&params);
1030         if (handle == NULL) {
1031                 printf("Creating hash with null hash_func failed\n");
1032                 return -1;
1033         }
1034         if (handle->hash_func == NULL) {
1035                 printf("Hash function should have been DEFAULT_HASH_FUNC\n");
1036                 return -1;
1037         }
1038
1039         /* this test is trying to create a hash with the same name as previous one.
1040          * this should return a pointer to the hash we previously created.
1041          * the previous hash isn't freed exactly for the purpose of it being in
1042          * the hash list.
1043          */
1044         memcpy(&params, &ut_params, sizeof(params));
1045         params.name = "same_name";
1046         tmp = rte_hash_create(&params);
1047
1048         /* check if the returned handle is actually equal to the previous hash */
1049         if (handle != tmp) {
1050                 rte_hash_free(handle);
1051                 rte_hash_free(tmp);
1052                 printf("Creating hash with existing name was successful\n");
1053                 return -1;
1054         }
1055
1056         /* try creating hash when there already are hashes in the list.
1057          * the previous hash is not freed to have a non-empty hash list.
1058          * the other hash that's in the list is still pointed to by "handle" var.
1059          */
1060         memcpy(&params, &ut_params, sizeof(params));
1061         params.name = "different_name";
1062         tmp = rte_hash_create(&params);
1063         if (tmp == NULL) {
1064                 rte_hash_free(handle);
1065                 printf("Creating hash with valid parameters failed\n");
1066                 return -1;
1067         }
1068
1069         rte_hash_free(tmp);
1070         rte_hash_free(handle);
1071
1072         return 0;
1073 }
1074
1075 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1076                         0x04, 0x05, 0x06, 0x07,
1077                         0x08, 0x09, 0x0a, 0x0b,
1078                         0x0c, 0x0d, 0x0e, 0x0f};
1079 static struct rte_hash_parameters hash_params_ex = {
1080         .name = NULL,
1081         .entries = 64,
1082         .bucket_entries = 4,
1083         .key_len = 0,
1084         .hash_func = NULL,
1085         .hash_func_init_val = 0,
1086         .socket_id = 0,
1087 };
1088
1089 /*
1090  * add/delete key with jhash2
1091  */
1092 static int
1093 test_hash_add_delete_jhash2(void)
1094 {
1095         int ret = -1;
1096         struct rte_hash *handle;
1097         int32_t pos1, pos2;
1098
1099         hash_params_ex.name = "hash_test_jhash2";
1100         hash_params_ex.key_len = 4;
1101         hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1102
1103         handle = rte_hash_create(&hash_params_ex);
1104         if (handle == NULL) {
1105                 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1106                 goto fail_jhash2;
1107         }
1108         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1109         if (pos1 < 0) {
1110                 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1111                 goto fail_jhash2;
1112         }
1113
1114         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1115         if (pos2 < 0 || pos1 != pos2) {
1116                 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1117                 goto fail_jhash2;
1118         }
1119         ret = 0;
1120
1121 fail_jhash2:
1122         if (handle != NULL)
1123                 rte_hash_free(handle);
1124
1125         return ret;
1126 }
1127
1128 /*
1129  * add/delete (2) key with jhash2
1130  */
1131 static int
1132 test_hash_add_delete_2_jhash2(void)
1133 {
1134         int ret = -1;
1135         struct rte_hash *handle;
1136         int32_t pos1, pos2;
1137
1138         hash_params_ex.name = "hash_test_2_jhash2";
1139         hash_params_ex.key_len = 8;
1140         hash_params_ex.hash_func = (rte_hash_function)rte_jhash2;
1141
1142         handle = rte_hash_create(&hash_params_ex);
1143         if (handle == NULL)
1144                 goto fail_2_jhash2;
1145
1146         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1147         if (pos1 < 0)
1148                 goto fail_2_jhash2;
1149
1150         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1151         if (pos2 < 0 || pos1 != pos2)
1152                 goto fail_2_jhash2;
1153
1154         ret = 0;
1155
1156 fail_2_jhash2:
1157         if (handle != NULL)
1158                 rte_hash_free(handle);
1159
1160         return ret;
1161 }
1162
1163 static uint32_t
1164 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1165 {
1166         const uint32_t *k = key;
1167
1168         RTE_SET_USED(length);
1169
1170         return rte_jhash_1word(k[0], initval);
1171 }
1172
1173 static uint32_t
1174 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1175 {
1176         const uint32_t *k = key;
1177
1178         RTE_SET_USED(length);
1179
1180         return rte_jhash_2words(k[0], k[1], initval);
1181 }
1182
1183 static uint32_t
1184 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1185 {
1186         const uint32_t *k = key;
1187
1188         RTE_SET_USED(length);
1189
1190         return rte_jhash_3words(k[0], k[1], k[2], initval);
1191 }
1192
1193 /*
1194  * add/delete key with jhash 1word
1195  */
1196 static int
1197 test_hash_add_delete_jhash_1word(void)
1198 {
1199         int ret = -1;
1200         struct rte_hash *handle;
1201         int32_t pos1, pos2;
1202
1203         hash_params_ex.name = "hash_test_jhash_1word";
1204         hash_params_ex.key_len = 4;
1205         hash_params_ex.hash_func = test_hash_jhash_1word;
1206
1207         handle = rte_hash_create(&hash_params_ex);
1208         if (handle == NULL)
1209                 goto fail_jhash_1word;
1210
1211         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1212         if (pos1 < 0)
1213                 goto fail_jhash_1word;
1214
1215         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1216         if (pos2 < 0 || pos1 != pos2)
1217                 goto fail_jhash_1word;
1218
1219         ret = 0;
1220
1221 fail_jhash_1word:
1222         if (handle != NULL)
1223                 rte_hash_free(handle);
1224
1225         return ret;
1226 }
1227
1228 /*
1229  * add/delete key with jhash 2word
1230  */
1231 static int
1232 test_hash_add_delete_jhash_2word(void)
1233 {
1234         int ret = -1;
1235         struct rte_hash *handle;
1236         int32_t pos1, pos2;
1237
1238         hash_params_ex.name = "hash_test_jhash_2word";
1239         hash_params_ex.key_len = 8;
1240         hash_params_ex.hash_func = test_hash_jhash_2word;
1241
1242         handle = rte_hash_create(&hash_params_ex);
1243         if (handle == NULL)
1244                 goto fail_jhash_2word;
1245
1246         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1247         if (pos1 < 0)
1248                 goto fail_jhash_2word;
1249
1250         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1251         if (pos2 < 0 || pos1 != pos2)
1252                 goto fail_jhash_2word;
1253
1254         ret = 0;
1255
1256 fail_jhash_2word:
1257         if (handle != NULL)
1258                 rte_hash_free(handle);
1259
1260         return ret;
1261 }
1262
1263 /*
1264  * add/delete key with jhash 3word
1265  */
1266 static int
1267 test_hash_add_delete_jhash_3word(void)
1268 {
1269         int ret = -1;
1270         struct rte_hash *handle;
1271         int32_t pos1, pos2;
1272
1273         hash_params_ex.name = "hash_test_jhash_3word";
1274         hash_params_ex.key_len = 12;
1275         hash_params_ex.hash_func = test_hash_jhash_3word;
1276
1277         handle = rte_hash_create(&hash_params_ex);
1278         if (handle == NULL)
1279                 goto fail_jhash_3word;
1280
1281         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1282         if (pos1 < 0)
1283                 goto fail_jhash_3word;
1284
1285         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1286         if (pos2 < 0 || pos1 != pos2)
1287                 goto fail_jhash_3word;
1288
1289         ret = 0;
1290
1291 fail_jhash_3word:
1292         if (handle != NULL)
1293                 rte_hash_free(handle);
1294
1295         return ret;
1296 }
1297
1298 /*
1299  * Do all unit and performance tests.
1300  */
1301 int test_hash(void)
1302 {
1303         if (test_add_delete() < 0)
1304                 return -1;
1305         if (test_hash_add_delete_jhash2() < 0)
1306                 return -1;
1307         if (test_hash_add_delete_2_jhash2() < 0)
1308                 return -1;
1309         if (test_hash_add_delete_jhash_1word() < 0)
1310                 return -1;
1311         if (test_hash_add_delete_jhash_2word() < 0)
1312                 return -1;
1313         if (test_hash_add_delete_jhash_3word() < 0)
1314                 return -1;
1315         if (test_hash_find_existing() < 0)
1316                 return -1;
1317         if (test_add_update_delete() < 0)
1318                 return -1;
1319         if (test_five_keys() < 0)
1320                 return -1;
1321         if (test_full_bucket() < 0)
1322                 return -1;
1323
1324         if (test_fbk_hash_find_existing() < 0)
1325                 return -1;
1326         if (fbk_hash_unit_test() < 0)
1327                 return -1;
1328         if (test_hash_creation_with_bad_parameters() < 0)
1329                 return -1;
1330         if (test_hash_creation_with_good_parameters() < 0)
1331                 return -1;
1332
1333         run_hash_func_tests();
1334
1335         return 0;
1336 }
1337 #else
1338
1339 int
1340 test_hash(void)
1341 {
1342         printf("The Hash library is not included in this build\n");
1343         return 0;
1344 }
1345
1346 #endif