edf41f5a7f540286150975c12c5ef17144631cdf
[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 static int
665 fbk_hash_unit_test(void)
666 {
667         struct rte_fbk_hash_params params = {
668                 .name = "fbk_hash_test",
669                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
670                 .entries_per_bucket = 4,
671                 .socket_id = 0,
672         };
673
674         struct rte_fbk_hash_params invalid_params_1 = {
675                 .name = "invalid_1",
676                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */
677                 .entries_per_bucket = 4,
678                 .socket_id = 0,
679         };
680
681         struct rte_fbk_hash_params invalid_params_2 = {
682                 .name = "invalid_2",
683                 .entries = 4,
684                 .entries_per_bucket = 3,         /* Not power of 2 */
685                 .socket_id = 0,
686         };
687
688         struct rte_fbk_hash_params invalid_params_3 = {
689                 .name = "invalid_3",
690                 .entries = 0,                    /* Entries is 0 */
691                 .entries_per_bucket = 4,
692                 .socket_id = 0,
693         };
694
695         struct rte_fbk_hash_params invalid_params_4 = {
696                 .name = "invalid_4",
697                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
698                 .entries_per_bucket = 0,         /* Entries per bucket is 0 */
699                 .socket_id = 0,
700         };
701
702         struct rte_fbk_hash_params invalid_params_5 = {
703                 .name = "invalid_5",
704                 .entries = 4,
705                 .entries_per_bucket = 8,         /* Entries per bucket > entries */
706                 .socket_id = 0,
707         };
708
709         struct rte_fbk_hash_params invalid_params_6 = {
710                 .name = "invalid_6",
711                 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2,   /* Entries > max allowed */
712                 .entries_per_bucket = 4,
713                 .socket_id = 0,
714         };
715
716         struct rte_fbk_hash_params invalid_params_7 = {
717                 .name = "invalid_7",
718                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
719                 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2,  /* Entries > max allowed */
720                 .socket_id = 0,
721         };
722
723         struct rte_fbk_hash_params invalid_params_8 = {
724                 .name = "invalid_7",
725                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
726                 .entries_per_bucket = 4,
727                 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */
728         };
729
730         /* try to create two hashes with identical names
731          * in this case, trying to create a second one will not
732          * fail but will simply return pointer to the existing
733          * hash with that name. sort of like a "find hash by name" :-)
734          */
735         struct rte_fbk_hash_params invalid_params_same_name_1 = {
736                 .name = "same_name",                            /* hash with identical name */
737                 .entries = 4,
738                 .entries_per_bucket = 2,
739                 .socket_id = 0,
740         };
741
742         /* trying to create this hash should return a pointer to an existing hash */
743         struct rte_fbk_hash_params invalid_params_same_name_2 = {
744                 .name = "same_name",                            /* hash with identical name */
745                 .entries = RTE_FBK_HASH_ENTRIES_MAX,
746                 .entries_per_bucket = 4,
747                 .socket_id = 0,
748         };
749
750         /* this is a sanity check for "same name" test
751          * creating this hash will check if we are actually able to create
752          * multiple hashes with different names (instead of having just one).
753          */
754         struct rte_fbk_hash_params different_name = {
755                 .name = "different_name",                       /* different name */
756                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
757                 .entries_per_bucket = 4,
758                 .socket_id = 0,
759         };
760
761         struct rte_fbk_hash_params params_jhash = {
762                 .name = "valid",
763                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
764                 .entries_per_bucket = 4,
765                 .socket_id = 0,
766                 .hash_func = rte_jhash_1word,              /* Tests for different hash_func */
767                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
768         };
769
770         struct rte_fbk_hash_params params_nohash = {
771                 .name = "valid nohash",
772                 .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
773                 .entries_per_bucket = 4,
774                 .socket_id = 0,
775                 .hash_func = NULL,                            /* Tests for null hash_func */
776                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
777         };
778
779         struct rte_fbk_hash_table *handle, *tmp;
780         uint32_t keys[5] =
781                 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9};
782         uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571};
783         int status;
784         unsigned i;
785         double used_entries;
786
787         /* Try creating hashes with invalid parameters */
788         printf("# Testing hash creation with invalid parameters "
789                         "- expect error msgs\n");
790         handle = rte_fbk_hash_create(&invalid_params_1);
791         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
792
793         handle = rte_fbk_hash_create(&invalid_params_2);
794         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
795
796         handle = rte_fbk_hash_create(&invalid_params_3);
797         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
798
799         handle = rte_fbk_hash_create(&invalid_params_4);
800         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
801
802         handle = rte_fbk_hash_create(&invalid_params_5);
803         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
804
805         handle = rte_fbk_hash_create(&invalid_params_6);
806         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
807
808         handle = rte_fbk_hash_create(&invalid_params_7);
809         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
810
811         handle = rte_fbk_hash_create(&invalid_params_8);
812         RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed");
813
814         handle = rte_fbk_hash_create(&invalid_params_same_name_1);
815         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded");
816
817         tmp = rte_fbk_hash_create(&invalid_params_same_name_2);
818         if (tmp != NULL)
819                 rte_fbk_hash_free(tmp);
820         RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed");
821
822         /* we are not freeing  handle here because we need a hash list
823          * to be not empty for the next test */
824
825         /* create a hash in non-empty list - good for coverage */
826         tmp = rte_fbk_hash_create(&different_name);
827         RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded");
828
829         /* free both hashes */
830         rte_fbk_hash_free(handle);
831         rte_fbk_hash_free(tmp);
832
833         /* Create empty jhash hash. */
834         handle = rte_fbk_hash_create(&params_jhash);
835         RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed");
836
837         /* Cleanup. */
838         rte_fbk_hash_free(handle);
839
840         /* Create empty jhash hash. */
841         handle = rte_fbk_hash_create(&params_nohash);
842         RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed");
843
844         /* Cleanup. */
845         rte_fbk_hash_free(handle);
846
847         /* Create empty hash. */
848         handle = rte_fbk_hash_create(&params);
849         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
850
851         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
852         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
853                                 "load factor right after creation is not zero but it should be");
854         /* Add keys. */
855         for (i = 0; i < 5; i++) {
856                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
857                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
858         }
859
860         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
861         RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \
862                                 "load factor now is not as expected");
863         /* Find value of added keys. */
864         for (i = 0; i < 5; i++) {
865                 status = rte_fbk_hash_lookup(handle, keys[i]);
866                 RETURN_IF_ERROR_FBK(status != vals[i],
867                                 "fbk hash lookup failed");
868         }
869
870         /* Change value of added keys. */
871         for (i = 0; i < 5; i++) {
872                 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]);
873                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed");
874         }
875
876         /* Find new values. */
877         for (i = 0; i < 5; i++) {
878                 status = rte_fbk_hash_lookup(handle, keys[i]);
879                 RETURN_IF_ERROR_FBK(status != vals[4-i],
880                                 "fbk hash lookup failed");
881         }
882
883         /* Delete keys individually. */
884         for (i = 0; i < 5; i++) {
885                 status = rte_fbk_hash_delete_key(handle, keys[i]);
886                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed");
887         }
888
889         used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX;
890         RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \
891                                 "load factor right after deletion is not zero but it should be");
892         /* Lookup should now fail. */
893         for (i = 0; i < 5; i++) {
894                 status = rte_fbk_hash_lookup(handle, keys[i]);
895                 RETURN_IF_ERROR_FBK(status == 0,
896                                 "fbk hash lookup should have failed");
897         }
898
899         /* Add keys again. */
900         for (i = 0; i < 5; i++) {
901                 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]);
902                 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed");
903         }
904
905         /* Make sure they were added. */
906         for (i = 0; i < 5; i++) {
907                 status = rte_fbk_hash_lookup(handle, keys[i]);
908                 RETURN_IF_ERROR_FBK(status != vals[i],
909                                 "fbk hash lookup failed");
910         }
911
912         /* Clear all entries. */
913         rte_fbk_hash_clear_all(handle);
914
915         /* Lookup should fail. */
916         for (i = 0; i < 5; i++) {
917                 status = rte_fbk_hash_lookup(handle, keys[i]);
918                 RETURN_IF_ERROR_FBK(status == 0,
919                                 "fbk hash lookup should have failed");
920         }
921
922         /* coverage */
923
924         /* fill up the hash_table */
925         for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++)
926                 rte_fbk_hash_add_key(handle, i, (uint16_t) i);
927
928         /* Find non-existent key in a full hashtable */
929         status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
930         RETURN_IF_ERROR_FBK(status != -ENOENT,
931                         "fbk hash lookup succeeded");
932
933         /* Delete non-existent key in a full hashtable */
934         status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1);
935         RETURN_IF_ERROR_FBK(status != -ENOENT,
936                         "fbk hash delete succeeded");
937
938         /* Delete one key from a full hashtable */
939         status = rte_fbk_hash_delete_key(handle, 1);
940         RETURN_IF_ERROR_FBK(status != 0,
941                         "fbk hash delete failed");
942
943         /* Clear all entries. */
944         rte_fbk_hash_clear_all(handle);
945
946         /* Cleanup. */
947         rte_fbk_hash_free(handle);
948
949         /* Cover the NULL case. */
950         rte_fbk_hash_free(0);
951
952         return 0;
953 }
954
955 /*
956  * Sequence of operations for find existing fbk hash table
957  *
958  *  - create table
959  *  - find existing table: hit
960  *  - find non-existing table: miss
961  *
962  */
963 static int test_fbk_hash_find_existing(void)
964 {
965         struct rte_fbk_hash_params params = {
966                         .name = "fbk_hash_find_existing",
967                         .entries = LOCAL_FBK_HASH_ENTRIES_MAX,
968                         .entries_per_bucket = 4,
969                         .socket_id = 0,
970         };
971         struct rte_fbk_hash_table *handle = NULL, *result = NULL;
972
973         /* Create hash table. */
974         handle = rte_fbk_hash_create(&params);
975         RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed");
976
977         /* Try to find existing fbk hash table */
978         result = rte_fbk_hash_find_existing("fbk_hash_find_existing");
979         RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table");
980
981         /* Try to find non-existing fbk hash table */
982         result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing");
983         RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist");
984
985         /* Cleanup. */
986         rte_fbk_hash_free(handle);
987
988         return 0;
989 }
990
991 #define BUCKET_ENTRIES 4
992 /*
993  * Do tests for hash creation with bad parameters.
994  */
995 static int test_hash_creation_with_bad_parameters(void)
996 {
997         struct rte_hash *handle, *tmp;
998         struct rte_hash_parameters params;
999
1000         handle = rte_hash_create(NULL);
1001         if (handle != NULL) {
1002                 rte_hash_free(handle);
1003                 printf("Impossible creating hash successfully without any parameter\n");
1004                 return -1;
1005         }
1006
1007         memcpy(&params, &ut_params, sizeof(params));
1008         params.name = "creation_with_bad_parameters_0";
1009         params.entries = RTE_HASH_ENTRIES_MAX + 1;
1010         handle = rte_hash_create(&params);
1011         if (handle != NULL) {
1012                 rte_hash_free(handle);
1013                 printf("Impossible creating hash successfully with entries in parameter exceeded\n");
1014                 return -1;
1015         }
1016
1017         memcpy(&params, &ut_params, sizeof(params));
1018         params.name = "creation_with_bad_parameters_2";
1019         params.entries = BUCKET_ENTRIES - 1;
1020         handle = rte_hash_create(&params);
1021         if (handle != NULL) {
1022                 rte_hash_free(handle);
1023                 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
1024                 return -1;
1025         }
1026
1027         memcpy(&params, &ut_params, sizeof(params));
1028         params.name = "creation_with_bad_parameters_3";
1029         params.key_len = 0;
1030         handle = rte_hash_create(&params);
1031         if (handle != NULL) {
1032                 rte_hash_free(handle);
1033                 printf("Impossible creating hash successfully if key_len in parameter is zero\n");
1034                 return -1;
1035         }
1036
1037         memcpy(&params, &ut_params, sizeof(params));
1038         params.name = "creation_with_bad_parameters_4";
1039         params.socket_id = RTE_MAX_NUMA_NODES + 1;
1040         handle = rte_hash_create(&params);
1041         if (handle != NULL) {
1042                 rte_hash_free(handle);
1043                 printf("Impossible creating hash successfully with invalid socket\n");
1044                 return -1;
1045         }
1046
1047         /* test with same name should fail */
1048         memcpy(&params, &ut_params, sizeof(params));
1049         params.name = "same_name";
1050         handle = rte_hash_create(&params);
1051         if (handle == NULL) {
1052                 printf("Cannot create first hash table with 'same_name'\n");
1053                 return -1;
1054         }
1055         tmp = rte_hash_create(&params);
1056         if (tmp != NULL) {
1057                 printf("Creation of hash table with same name should fail\n");
1058                 rte_hash_free(handle);
1059                 rte_hash_free(tmp);
1060                 return -1;
1061         }
1062         rte_hash_free(handle);
1063
1064         printf("# Test successful. No more errors expected\n");
1065
1066         return 0;
1067 }
1068
1069 /*
1070  * Do tests for hash creation with parameters that look incorrect
1071  * but are actually valid.
1072  */
1073 static int
1074 test_hash_creation_with_good_parameters(void)
1075 {
1076         struct rte_hash *handle;
1077         struct rte_hash_parameters params;
1078
1079         /* create with null hash function - should choose DEFAULT_HASH_FUNC */
1080         memcpy(&params, &ut_params, sizeof(params));
1081         params.name = "name";
1082         params.hash_func = NULL;
1083         handle = rte_hash_create(&params);
1084         if (handle == NULL) {
1085                 printf("Creating hash with null hash_func failed\n");
1086                 return -1;
1087         }
1088
1089         rte_hash_free(handle);
1090
1091         return 0;
1092 }
1093
1094 #define ITERATIONS 3
1095 /*
1096  * Test to see the average table utilization (entries added/max entries)
1097  * before hitting a random entry that cannot be added
1098  */
1099 static int test_average_table_utilization(void)
1100 {
1101         struct rte_hash *handle;
1102         uint8_t simple_key[MAX_KEYSIZE];
1103         unsigned i, j;
1104         unsigned added_keys, average_keys_added = 0;
1105         int ret;
1106
1107         printf("\n# Running test to determine average utilization"
1108                "\n  before adding elements begins to fail\n");
1109         printf("Measuring performance, please wait");
1110         fflush(stdout);
1111         ut_params.entries = 1 << 16;
1112         ut_params.name = "test_average_utilization";
1113         ut_params.hash_func = rte_jhash;
1114         handle = rte_hash_create(&ut_params);
1115         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1116
1117         for (j = 0; j < ITERATIONS; j++) {
1118                 ret = 0;
1119                 /* Add random entries until key cannot be added */
1120                 for (added_keys = 0; ret >= 0; added_keys++) {
1121                         for (i = 0; i < ut_params.key_len; i++)
1122                                 simple_key[i] = rte_rand() % 255;
1123                         ret = rte_hash_add_key(handle, simple_key);
1124                 }
1125                 if (ret != -ENOSPC) {
1126                         printf("Unexpected error when adding keys\n");
1127                         rte_hash_free(handle);
1128                         return -1;
1129                 }
1130
1131                 average_keys_added += added_keys;
1132
1133                 /* Reset the table */
1134                 rte_hash_reset(handle);
1135
1136                 /* Print a dot to show progress on operations */
1137                 printf(".");
1138                 fflush(stdout);
1139         }
1140
1141         average_keys_added /= ITERATIONS;
1142
1143         printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
1144                 ((double) average_keys_added / ut_params.entries * 100),
1145                 average_keys_added, ut_params.entries);
1146         rte_hash_free(handle);
1147
1148         return 0;
1149 }
1150
1151 #define NUM_ENTRIES 256
1152 static int test_hash_iteration(void)
1153 {
1154         struct rte_hash *handle;
1155         unsigned i;
1156         uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE];
1157         const void *next_key;
1158         void *next_data;
1159         void *data[NUM_ENTRIES];
1160         unsigned added_keys;
1161         uint32_t iter = 0;
1162         int ret = 0;
1163
1164         ut_params.entries = NUM_ENTRIES;
1165         ut_params.name = "test_hash_iteration";
1166         ut_params.hash_func = rte_jhash;
1167         ut_params.key_len = 16;
1168         handle = rte_hash_create(&ut_params);
1169         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
1170
1171         /* Add random entries until key cannot be added */
1172         for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) {
1173                 data[added_keys] = (void *) ((uintptr_t) rte_rand());
1174                 for (i = 0; i < ut_params.key_len; i++)
1175                         keys[added_keys][i] = rte_rand() % 255;
1176                 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]);
1177                 if (ret < 0)
1178                         break;
1179         }
1180
1181         /* Iterate through the hash table */
1182         while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
1183                 /* Search for the key in the list of keys added */
1184                 for (i = 0; i < NUM_ENTRIES; i++) {
1185                         if (memcmp(next_key, keys[i], ut_params.key_len) == 0) {
1186                                 if (next_data != data[i]) {
1187                                         printf("Data found in the hash table is"
1188                                                "not the data added with the key\n");
1189                                         goto err;
1190                                 }
1191                                 added_keys--;
1192                                 break;
1193                         }
1194                 }
1195                 if (i == NUM_ENTRIES) {
1196                         printf("Key found in the hash table was not added\n");
1197                         goto err;
1198                 }
1199         }
1200
1201         /* Check if all keys have been iterated */
1202         if (added_keys != 0) {
1203                 printf("There were still %u keys to iterate\n", added_keys);
1204                 goto err;
1205         }
1206
1207         rte_hash_free(handle);
1208         return 0;
1209
1210 err:
1211         rte_hash_free(handle);
1212         return -1;
1213 }
1214
1215 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03,
1216                         0x04, 0x05, 0x06, 0x07,
1217                         0x08, 0x09, 0x0a, 0x0b,
1218                         0x0c, 0x0d, 0x0e, 0x0f};
1219 static struct rte_hash_parameters hash_params_ex = {
1220         .name = NULL,
1221         .entries = 64,
1222         .key_len = 0,
1223         .hash_func = NULL,
1224         .hash_func_init_val = 0,
1225         .socket_id = 0,
1226 };
1227
1228 /*
1229  * add/delete key with jhash2
1230  */
1231 static int
1232 test_hash_add_delete_jhash2(void)
1233 {
1234         int ret = -1;
1235         struct rte_hash *handle;
1236         int32_t pos1, pos2;
1237
1238         hash_params_ex.name = "hash_test_jhash2";
1239         hash_params_ex.key_len = 4;
1240         hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1241
1242         handle = rte_hash_create(&hash_params_ex);
1243         if (handle == NULL) {
1244                 printf("test_hash_add_delete_jhash2 fail to create hash\n");
1245                 goto fail_jhash2;
1246         }
1247         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1248         if (pos1 < 0) {
1249                 printf("test_hash_add_delete_jhash2 fail to add hash key\n");
1250                 goto fail_jhash2;
1251         }
1252
1253         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1254         if (pos2 < 0 || pos1 != pos2) {
1255                 printf("test_hash_add_delete_jhash2 delete different key from being added\n");
1256                 goto fail_jhash2;
1257         }
1258         ret = 0;
1259
1260 fail_jhash2:
1261         if (handle != NULL)
1262                 rte_hash_free(handle);
1263
1264         return ret;
1265 }
1266
1267 /*
1268  * add/delete (2) key with jhash2
1269  */
1270 static int
1271 test_hash_add_delete_2_jhash2(void)
1272 {
1273         int ret = -1;
1274         struct rte_hash *handle;
1275         int32_t pos1, pos2;
1276
1277         hash_params_ex.name = "hash_test_2_jhash2";
1278         hash_params_ex.key_len = 8;
1279         hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b;
1280
1281         handle = rte_hash_create(&hash_params_ex);
1282         if (handle == NULL)
1283                 goto fail_2_jhash2;
1284
1285         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1286         if (pos1 < 0)
1287                 goto fail_2_jhash2;
1288
1289         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1290         if (pos2 < 0 || pos1 != pos2)
1291                 goto fail_2_jhash2;
1292
1293         ret = 0;
1294
1295 fail_2_jhash2:
1296         if (handle != NULL)
1297                 rte_hash_free(handle);
1298
1299         return ret;
1300 }
1301
1302 static uint32_t
1303 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval)
1304 {
1305         const uint32_t *k = key;
1306
1307         RTE_SET_USED(length);
1308
1309         return rte_jhash_1word(k[0], initval);
1310 }
1311
1312 static uint32_t
1313 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval)
1314 {
1315         const uint32_t *k = key;
1316
1317         RTE_SET_USED(length);
1318
1319         return rte_jhash_2words(k[0], k[1], initval);
1320 }
1321
1322 static uint32_t
1323 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval)
1324 {
1325         const uint32_t *k = key;
1326
1327         RTE_SET_USED(length);
1328
1329         return rte_jhash_3words(k[0], k[1], k[2], initval);
1330 }
1331
1332 /*
1333  * add/delete key with jhash 1word
1334  */
1335 static int
1336 test_hash_add_delete_jhash_1word(void)
1337 {
1338         int ret = -1;
1339         struct rte_hash *handle;
1340         int32_t pos1, pos2;
1341
1342         hash_params_ex.name = "hash_test_jhash_1word";
1343         hash_params_ex.key_len = 4;
1344         hash_params_ex.hash_func = test_hash_jhash_1word;
1345
1346         handle = rte_hash_create(&hash_params_ex);
1347         if (handle == NULL)
1348                 goto fail_jhash_1word;
1349
1350         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1351         if (pos1 < 0)
1352                 goto fail_jhash_1word;
1353
1354         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1355         if (pos2 < 0 || pos1 != pos2)
1356                 goto fail_jhash_1word;
1357
1358         ret = 0;
1359
1360 fail_jhash_1word:
1361         if (handle != NULL)
1362                 rte_hash_free(handle);
1363
1364         return ret;
1365 }
1366
1367 /*
1368  * add/delete key with jhash 2word
1369  */
1370 static int
1371 test_hash_add_delete_jhash_2word(void)
1372 {
1373         int ret = -1;
1374         struct rte_hash *handle;
1375         int32_t pos1, pos2;
1376
1377         hash_params_ex.name = "hash_test_jhash_2word";
1378         hash_params_ex.key_len = 8;
1379         hash_params_ex.hash_func = test_hash_jhash_2word;
1380
1381         handle = rte_hash_create(&hash_params_ex);
1382         if (handle == NULL)
1383                 goto fail_jhash_2word;
1384
1385         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1386         if (pos1 < 0)
1387                 goto fail_jhash_2word;
1388
1389         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1390         if (pos2 < 0 || pos1 != pos2)
1391                 goto fail_jhash_2word;
1392
1393         ret = 0;
1394
1395 fail_jhash_2word:
1396         if (handle != NULL)
1397                 rte_hash_free(handle);
1398
1399         return ret;
1400 }
1401
1402 /*
1403  * add/delete key with jhash 3word
1404  */
1405 static int
1406 test_hash_add_delete_jhash_3word(void)
1407 {
1408         int ret = -1;
1409         struct rte_hash *handle;
1410         int32_t pos1, pos2;
1411
1412         hash_params_ex.name = "hash_test_jhash_3word";
1413         hash_params_ex.key_len = 12;
1414         hash_params_ex.hash_func = test_hash_jhash_3word;
1415
1416         handle = rte_hash_create(&hash_params_ex);
1417         if (handle == NULL)
1418                 goto fail_jhash_3word;
1419
1420         pos1 = rte_hash_add_key(handle, (void *)&key[0]);
1421         if (pos1 < 0)
1422                 goto fail_jhash_3word;
1423
1424         pos2 = rte_hash_del_key(handle, (void *)&key[0]);
1425         if (pos2 < 0 || pos1 != pos2)
1426                 goto fail_jhash_3word;
1427
1428         ret = 0;
1429
1430 fail_jhash_3word:
1431         if (handle != NULL)
1432                 rte_hash_free(handle);
1433
1434         return ret;
1435 }
1436
1437 /*
1438  * Do all unit and performance tests.
1439  */
1440 static int
1441 test_hash(void)
1442 {
1443         if (test_add_delete() < 0)
1444                 return -1;
1445         if (test_hash_add_delete_jhash2() < 0)
1446                 return -1;
1447         if (test_hash_add_delete_2_jhash2() < 0)
1448                 return -1;
1449         if (test_hash_add_delete_jhash_1word() < 0)
1450                 return -1;
1451         if (test_hash_add_delete_jhash_2word() < 0)
1452                 return -1;
1453         if (test_hash_add_delete_jhash_3word() < 0)
1454                 return -1;
1455         if (test_hash_get_key_with_position() < 0)
1456                 return -1;
1457         if (test_hash_find_existing() < 0)
1458                 return -1;
1459         if (test_add_update_delete() < 0)
1460                 return -1;
1461         if (test_five_keys() < 0)
1462                 return -1;
1463         if (test_full_bucket() < 0)
1464                 return -1;
1465
1466         if (test_fbk_hash_find_existing() < 0)
1467                 return -1;
1468         if (fbk_hash_unit_test() < 0)
1469                 return -1;
1470         if (test_hash_creation_with_bad_parameters() < 0)
1471                 return -1;
1472         if (test_hash_creation_with_good_parameters() < 0)
1473                 return -1;
1474         if (test_average_table_utilization() < 0)
1475                 return -1;
1476         if (test_hash_iteration() < 0)
1477                 return -1;
1478
1479         run_hash_func_tests();
1480
1481         if (test_crc32_hash_alg_equiv() < 0)
1482                 return -1;
1483
1484         return 0;
1485 }
1486
1487 REGISTER_TEST_COMMAND(hash_autotest, test_hash);