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