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