net/virtio: fix incorrect cast of void *
[dpdk.git] / test / test / test_member.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /* This test is for membership library's simple feature test */
35
36 #include <rte_memcpy.h>
37 #include <rte_malloc.h>
38 #include <rte_member.h>
39 #include <rte_byteorder.h>
40 #include <rte_random.h>
41 #include <rte_debug.h>
42 #include <rte_ip.h>
43
44 #include "test.h"
45
46 struct rte_member_setsum *setsum_ht;
47 struct rte_member_setsum *setsum_cache;
48 struct rte_member_setsum *setsum_vbf;
49
50 /* 5-tuple key type */
51 struct flow_key {
52         uint32_t ip_src;
53         uint32_t ip_dst;
54         uint16_t port_src;
55         uint16_t port_dst;
56         uint8_t proto;
57 } __attribute__((packed));
58
59 /* Set ID Macros for multimatch test usage */
60 #define M_MATCH_S 1     /* Not start with 0 since by default 0 means no match */
61 #define M_MATCH_E 15
62 #define M_MATCH_STEP 2
63 #define M_MATCH_CNT \
64         (1 + (M_MATCH_E - M_MATCH_S) / M_MATCH_STEP)
65
66
67 #define NUM_SAMPLES 5
68 #define MAX_MATCH 32
69
70 /* Keys used by unit test functions */
71 static struct flow_key keys[NUM_SAMPLES] = {
72         {
73                 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
74                 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
75                 .port_src = 0x0908,
76                 .port_dst = 0x0b0a,
77                 .proto = 0x0c,
78         },
79         {
80                 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
81                 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
82                 .port_src = 0x1918,
83                 .port_dst = 0x1b1a,
84                 .proto = 0x1c,
85         },
86         {
87                 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
88                 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
89                 .port_src = 0x2928,
90                 .port_dst = 0x2b2a,
91                 .proto = 0x2c,
92         },
93         {
94                 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
95                 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
96                 .port_src = 0x3938,
97                 .port_dst = 0x3b3a,
98                 .proto = 0x3c,
99         },
100         {
101                 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
102                 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
103                 .port_src = 0x4948,
104                 .port_dst = 0x4b4a,
105                 .proto = 0x4c,
106         }
107 };
108
109 uint32_t test_set[NUM_SAMPLES] = {1, 2, 3, 4, 5};
110
111 #define ITERATIONS  3
112 #define KEY_SIZE  4
113
114 #define MAX_ENTRIES (1 << 16)
115 uint8_t generated_keys[MAX_ENTRIES][KEY_SIZE];
116
117 static struct rte_member_parameters params = {
118                 .num_keys = MAX_ENTRIES,        /* Total hash table entries. */
119                 .key_len = KEY_SIZE,            /* Length of hash key. */
120
121                 /* num_set and false_positive_rate only relevant to vBF */
122                 .num_set = 16,
123                 .false_positive_rate = 0.03,
124                 .prim_hash_seed = 1,
125                 .sec_hash_seed = 11,
126                 .socket_id = 0                  /* NUMA Socket ID for memory. */
127 };
128
129 /*
130  * Sequence of operations for find existing setsummary
131  *
132  *  - create setsum
133  *  - find existing setsum: hit
134  *  - find non-existing setsum: miss
135  *
136  */
137 static int
138 test_member_find_existing(void)
139 {
140         struct rte_member_setsum *tmp_setsum = NULL, *result = NULL;
141         struct rte_member_parameters tmp_params = {
142                 .name = "member_find_existing",
143                 .num_keys = MAX_ENTRIES,        /* Total hash table entries. */
144                 .key_len = KEY_SIZE,            /* Length of hash key. */
145                 .type = RTE_MEMBER_TYPE_HT,
146                 .num_set = 32,
147                 .false_positive_rate = 0.03,
148                 .prim_hash_seed = 1,
149                 .sec_hash_seed = 11,
150                 .socket_id = 0                  /* NUMA Socket ID for memory. */
151         };
152
153         /* Create */
154         tmp_setsum = rte_member_create(&tmp_params);
155         TEST_ASSERT(tmp_setsum != NULL, "setsum creation failed");
156
157         /* Try to find existing hash table */
158         result = rte_member_find_existing("member_find_existing");
159         TEST_ASSERT(result == tmp_setsum, "could not find existing setsum");
160
161         /* Try to find non-existing hash table */
162         result = rte_member_find_existing("member_find_non_existing");
163         TEST_ASSERT(result == NULL, "found setsum that shouldn't exist");
164
165         /* Cleanup. */
166         rte_member_free(tmp_setsum);
167
168         return 0;
169 }
170
171 /*
172  * Test for bad creating parameters
173  */
174 static int
175 test_member_create_bad_param(void)
176 {
177         struct rte_member_setsum *bad_setsum = NULL;
178         struct rte_member_parameters bad_params = {
179                 .num_keys = MAX_ENTRIES,        /* Total hash table entries. */
180                 .key_len = KEY_SIZE,            /* Length of hash key. */
181                 .type = RTE_MEMBER_TYPE_HT,
182                 .num_set = 32,
183                 .false_positive_rate = 0.03,
184                 .prim_hash_seed = 1,
185                 .sec_hash_seed = 11,
186                 .socket_id = 0                  /* NUMA Socket ID for memory. */
187         };
188
189         printf("Expected error section begin...\n");
190         bad_params.name = "bad_param1";
191         bad_params.num_set = 0;
192         bad_params.type = RTE_MEMBER_TYPE_VBF;
193         /* Test with 0 set for vBF should fail */
194         bad_setsum = rte_member_create(&bad_params);
195         if (bad_setsum != NULL) {
196                 rte_member_free(bad_setsum);
197                 printf("Impossible creating setsum successfully with invalid "
198                         "number of set for vBF\n");
199                 return -1;
200         }
201
202         bad_params.name = "bad_param2";
203         bad_params.false_positive_rate = 0;
204         bad_params.num_set = 32;
205         /* Test with 0 false positive for vBF should fail */
206         bad_setsum = rte_member_create(&bad_params);
207         if (bad_setsum != NULL) {
208                 rte_member_free(bad_setsum);
209                 printf("Impossible creating setsum successfully with invalid "
210                         "false positive rate for vBF\n");
211                 return -1;
212         }
213
214         bad_params.name = "bad_param3";
215         bad_params.false_positive_rate = 0.03;
216         bad_params.num_keys = 0;
217         /* Test with 0 key per BF for vBF should fail */
218         bad_setsum = rte_member_create(&bad_params);
219         if (bad_setsum != NULL) {
220                 rte_member_free(bad_setsum);
221                 printf("Impossible creating setsum successfully with invalid "
222                         "num_keys for vBF\n");
223                 return -1;
224         }
225
226         bad_params.name = "bad_param4";
227         bad_params.type = RTE_MEMBER_TYPE_HT;
228         bad_params.num_keys = RTE_MEMBER_BUCKET_ENTRIES / 2;
229         /* Test with less than 1 bucket for HTSS should fail */
230         bad_setsum = rte_member_create(&bad_params);
231         if (bad_setsum != NULL) {
232                 rte_member_free(bad_setsum);
233                 printf("Impossible creating setsum successfully with too few "
234                         "number of keys(entries) for HT\n");
235                 return -1;
236         }
237
238         bad_params.name = "bad_param5";
239         bad_params.num_keys = RTE_MEMBER_ENTRIES_MAX + 1;
240         /* Test with more than maximum entries for HTSS should fail */
241         bad_setsum = rte_member_create(&bad_params);
242         if (bad_setsum != NULL) {
243                 rte_member_free(bad_setsum);
244                 printf("Impossible creating setsum successfully with to many "
245                         "number of keys(entries) for HT\n");
246                 return -1;
247         }
248
249         bad_params.name = "bad_param5";
250         /* Test with same name should fail */
251         bad_setsum = rte_member_create(&bad_params);
252         if (bad_setsum != NULL) {
253                 rte_member_free(bad_setsum);
254                 printf("Impossible creating setsum successfully with existed "
255                         "name\n");
256                 return -1;
257         }
258         printf("Expected error section end...\n");
259         rte_member_free(bad_setsum);
260         return 0;
261 }
262
263 /* Create test setsummaries. */
264 static int test_member_create(void)
265 {
266         params.key_len = sizeof(struct flow_key);
267
268         params.name = "test_member_ht";
269         params.is_cache = 0;
270         params.type = RTE_MEMBER_TYPE_HT;
271         setsum_ht = rte_member_create(&params);
272
273         params.name = "test_member_cache";
274         params.is_cache = 1;
275         setsum_cache = rte_member_create(&params);
276
277         params.name = "test_member_vbf";
278         params.type = RTE_MEMBER_TYPE_VBF;
279         setsum_vbf = rte_member_create(&params);
280
281         if (setsum_ht == NULL || setsum_cache == NULL || setsum_vbf == NULL) {
282                 printf("Creation of setsums fail\n");
283                 return -1;
284         }
285         printf("Creation of setsums success\n");
286         return 0;
287 }
288
289 static int test_member_insert(void)
290 {
291         int ret_ht, ret_cache, ret_vbf, i;
292
293         for (i = 0; i < NUM_SAMPLES; i++) {
294                 ret_ht = rte_member_add(setsum_ht, &keys[i], test_set[i]);
295                 ret_cache = rte_member_add(setsum_cache, &keys[i],
296                                                 test_set[i]);
297                 ret_vbf = rte_member_add(setsum_vbf, &keys[i], test_set[i]);
298                 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
299                                 "insert error");
300         }
301         printf("insert key success\n");
302         return 0;
303 }
304
305 static int test_member_lookup(void)
306 {
307         int ret_ht, ret_cache, ret_vbf, i;
308         uint16_t set_ht, set_cache, set_vbf;
309         member_set_t set_ids_ht[NUM_SAMPLES] = {0};
310         member_set_t set_ids_cache[NUM_SAMPLES] = {0};
311         member_set_t set_ids_vbf[NUM_SAMPLES] = {0};
312
313         uint32_t num_key_ht = NUM_SAMPLES;
314         uint32_t num_key_cache = NUM_SAMPLES;
315         uint32_t num_key_vbf = NUM_SAMPLES;
316
317         const void *key_array[NUM_SAMPLES];
318
319         /* Single lookup test */
320         for (i = 0; i < NUM_SAMPLES; i++) {
321                 ret_ht = rte_member_lookup(setsum_ht, &keys[i], &set_ht);
322                 ret_cache = rte_member_lookup(setsum_cache, &keys[i],
323                                                         &set_cache);
324                 ret_vbf = rte_member_lookup(setsum_vbf, &keys[i], &set_vbf);
325                 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
326                                 "single lookup function error");
327
328                 TEST_ASSERT(set_ht == test_set[i] &&
329                                 set_cache == test_set[i] &&
330                                 set_vbf == test_set[i],
331                                 "single lookup set value error");
332         }
333         printf("lookup single key success\n");
334
335         /* Bulk lookup test */
336         for (i = 0; i < NUM_SAMPLES; i++)
337                 key_array[i] = &keys[i];
338
339         ret_ht = rte_member_lookup_bulk(setsum_ht, key_array,
340                         num_key_ht, set_ids_ht);
341
342         ret_cache = rte_member_lookup_bulk(setsum_cache, key_array,
343                         num_key_cache, set_ids_cache);
344
345         ret_vbf = rte_member_lookup_bulk(setsum_vbf, key_array,
346                         num_key_vbf, set_ids_vbf);
347
348         TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
349                         "bulk lookup function error");
350
351         for (i = 0; i < NUM_SAMPLES; i++) {
352                 TEST_ASSERT((set_ids_ht[i] == test_set[i]) &&
353                                 (set_ids_cache[i] == test_set[i]) &&
354                                 (set_ids_vbf[i] == test_set[i]),
355                                 "bulk lookup result error");
356         }
357
358         return 0;
359 }
360
361 static int test_member_delete(void)
362 {
363         int ret_ht, ret_cache, ret_vbf, i;
364         uint16_t set_ht, set_cache, set_vbf;
365         const void *key_array[NUM_SAMPLES];
366         member_set_t set_ids_ht[NUM_SAMPLES] = {0};
367         member_set_t set_ids_cache[NUM_SAMPLES] = {0};
368         member_set_t set_ids_vbf[NUM_SAMPLES] = {0};
369         uint32_t num_key_ht = NUM_SAMPLES;
370         uint32_t num_key_cache = NUM_SAMPLES;
371         uint32_t num_key_vbf = NUM_SAMPLES;
372
373         /* Delete part of all inserted keys */
374         for (i = 0; i < NUM_SAMPLES / 2; i++) {
375                 ret_ht = rte_member_delete(setsum_ht, &keys[i], test_set[i]);
376                 ret_cache = rte_member_delete(setsum_cache, &keys[i],
377                                                 test_set[i]);
378                 ret_vbf = rte_member_delete(setsum_vbf, &keys[i], test_set[i]);
379                 /* VBF does not support delete yet, so return error code */
380                 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0,
381                                 "key deletion function error");
382                 TEST_ASSERT(ret_vbf < 0,
383                                 "vbf does not support deletion, error");
384         }
385
386         for (i = 0; i < NUM_SAMPLES; i++)
387                 key_array[i] = &keys[i];
388
389         ret_ht = rte_member_lookup_bulk(setsum_ht, key_array,
390                         num_key_ht, set_ids_ht);
391
392         ret_cache = rte_member_lookup_bulk(setsum_cache, key_array,
393                         num_key_cache, set_ids_cache);
394
395         ret_vbf = rte_member_lookup_bulk(setsum_vbf, key_array,
396                         num_key_vbf, set_ids_vbf);
397
398         TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
399                         "bulk lookup function error");
400
401         for (i = 0; i < NUM_SAMPLES / 2; i++) {
402                 TEST_ASSERT((set_ids_ht[i] == RTE_MEMBER_NO_MATCH) &&
403                                 (set_ids_cache[i] == RTE_MEMBER_NO_MATCH),
404                                 "bulk lookup result error");
405         }
406
407         for (i = NUM_SAMPLES / 2; i < NUM_SAMPLES; i++) {
408                 TEST_ASSERT((set_ids_ht[i] == test_set[i]) &&
409                                 (set_ids_cache[i] == test_set[i]) &&
410                                 (set_ids_vbf[i] == test_set[i]),
411                                 "bulk lookup result error");
412         }
413
414         /* Delete the left of inserted keys */
415         for (i = NUM_SAMPLES / 2; i < NUM_SAMPLES; i++) {
416                 ret_ht = rte_member_delete(setsum_ht, &keys[i], test_set[i]);
417                 ret_cache = rte_member_delete(setsum_cache, &keys[i],
418                                                 test_set[i]);
419                 ret_vbf = rte_member_delete(setsum_vbf, &keys[i], test_set[i]);
420                 /* VBF does not support delete yet, so return error code */
421                 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0,
422                                 "key deletion function error");
423                 TEST_ASSERT(ret_vbf < 0,
424                                 "vbf does not support deletion, error");
425         }
426
427         for (i = 0; i < NUM_SAMPLES; i++) {
428                 ret_ht = rte_member_lookup(setsum_ht, &keys[i], &set_ht);
429                 ret_cache = rte_member_lookup(setsum_cache, &keys[i],
430                                                 &set_cache);
431                 ret_vbf = rte_member_lookup(setsum_vbf, &keys[i], &set_vbf);
432                 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0,
433                                 "key lookup function error");
434                 TEST_ASSERT(set_ht == RTE_MEMBER_NO_MATCH &&
435                                 ret_cache == RTE_MEMBER_NO_MATCH,
436                                 "key deletion failed");
437         }
438         /* Reset vbf for other following tests */
439         rte_member_reset(setsum_vbf);
440
441         printf("delete success\n");
442         return 0;
443 }
444
445 static int test_member_multimatch(void)
446 {
447         int ret_ht, ret_vbf, ret_cache;
448         member_set_t set_ids_ht[MAX_MATCH] = {0};
449         member_set_t set_ids_vbf[MAX_MATCH] = {0};
450         member_set_t set_ids_cache[MAX_MATCH] = {0};
451
452         member_set_t set_ids_ht_m[NUM_SAMPLES][MAX_MATCH] = {{0} };
453         member_set_t set_ids_vbf_m[NUM_SAMPLES][MAX_MATCH] = {{0} };
454         member_set_t set_ids_cache_m[NUM_SAMPLES][MAX_MATCH] = {{0} };
455
456         uint32_t match_count_ht[NUM_SAMPLES];
457         uint32_t match_count_vbf[NUM_SAMPLES];
458         uint32_t match_count_cache[NUM_SAMPLES];
459
460         uint32_t num_key_ht = NUM_SAMPLES;
461         uint32_t num_key_vbf = NUM_SAMPLES;
462         uint32_t num_key_cache = NUM_SAMPLES;
463
464         const void *key_array[NUM_SAMPLES];
465
466         uint32_t i, j;
467
468         /* Same key at most inserted 2*entry_per_bucket times for HT mode */
469         for (i = M_MATCH_S; i <= M_MATCH_E; i += M_MATCH_STEP) {
470                 for (j = 0; j < NUM_SAMPLES; j++) {
471                         ret_ht = rte_member_add(setsum_ht, &keys[j], i);
472                         ret_vbf = rte_member_add(setsum_vbf, &keys[j], i);
473                         ret_cache = rte_member_add(setsum_cache, &keys[j], i);
474
475                         TEST_ASSERT(ret_ht >= 0 && ret_vbf >= 0 &&
476                                         ret_cache >= 0,
477                                         "insert function error");
478                 }
479         }
480
481         /* Single multimatch test */
482         for (i = 0; i < NUM_SAMPLES; i++) {
483                 ret_vbf = rte_member_lookup_multi(setsum_vbf, &keys[i],
484                                                         MAX_MATCH, set_ids_vbf);
485                 ret_ht = rte_member_lookup_multi(setsum_ht, &keys[i],
486                                                         MAX_MATCH, set_ids_ht);
487                 ret_cache = rte_member_lookup_multi(setsum_cache, &keys[i],
488                                                 MAX_MATCH, set_ids_cache);
489                 /*
490                  * For cache mode, keys overwrite when signature same.
491                  * the mutimatch should work like single match.
492                  */
493                 TEST_ASSERT(ret_ht == M_MATCH_CNT && ret_vbf == M_MATCH_CNT &&
494                                 ret_cache == 1,
495                                 "single lookup_multi error");
496                 TEST_ASSERT(set_ids_cache[0] == M_MATCH_E,
497                                 "single lookup_multi cache error");
498
499                 for (j = 1; j <= M_MATCH_CNT; j++) {
500                         TEST_ASSERT(set_ids_ht[j-1] == j * M_MATCH_STEP - 1 &&
501                                         set_ids_vbf[j-1] ==
502                                                         j * M_MATCH_STEP - 1,
503                                         "single multimatch lookup error");
504                 }
505         }
506         printf("lookup single key for multimatch success\n");
507
508         /* Bulk multimatch test */
509         for (i = 0; i < NUM_SAMPLES; i++)
510                 key_array[i] = &keys[i];
511         ret_vbf = rte_member_lookup_multi_bulk(setsum_vbf,
512                         &key_array[0], num_key_ht, MAX_MATCH, match_count_vbf,
513                         (member_set_t *)set_ids_vbf_m);
514
515         ret_ht = rte_member_lookup_multi_bulk(setsum_ht,
516                         &key_array[0], num_key_vbf, MAX_MATCH, match_count_ht,
517                         (member_set_t *)set_ids_ht_m);
518
519         ret_cache = rte_member_lookup_multi_bulk(setsum_cache,
520                         &key_array[0], num_key_cache, MAX_MATCH,
521                         match_count_cache, (member_set_t *)set_ids_cache_m);
522
523
524         for (j = 0; j < NUM_SAMPLES; j++) {
525                 TEST_ASSERT(match_count_ht[j] == M_MATCH_CNT,
526                         "bulk multimatch lookup HT match count error");
527                 TEST_ASSERT(match_count_vbf[j] == M_MATCH_CNT,
528                         "bulk multimatch lookup vBF match count error");
529                 TEST_ASSERT(match_count_cache[j] == 1,
530                         "bulk multimatch lookup CACHE match count error");
531                 TEST_ASSERT(set_ids_cache_m[j][0] == M_MATCH_E,
532                         "bulk multimatch lookup CACHE set value error");
533
534                 for (i = 1; i <= M_MATCH_CNT; i++) {
535                         TEST_ASSERT(set_ids_ht_m[j][i-1] ==
536                                                         i * M_MATCH_STEP - 1,
537                                 "bulk multimatch lookup HT set value error");
538                         TEST_ASSERT(set_ids_vbf_m[j][i-1] ==
539                                                         i * M_MATCH_STEP - 1,
540                                 "bulk multimatch lookup vBF set value error");
541                 }
542         }
543
544         printf("lookup for bulk multimatch success\n");
545
546         return 0;
547 }
548
549 static int key_compare(const void *key1, const void *key2)
550 {
551         return memcmp(key1, key2, KEY_SIZE);
552 }
553
554 static void
555 setup_keys_and_data(void)
556 {
557         unsigned int i, j;
558         int num_duplicates;
559
560         /* Reset all arrays */
561         for (i = 0; i < KEY_SIZE; i++)
562                 generated_keys[0][i] = 0;
563
564         /* Generate a list of keys, some of which may be duplicates */
565         for (i = 0; i < MAX_ENTRIES; i++) {
566                 for (j = 0; j < KEY_SIZE; j++)
567                         generated_keys[i][j] = rte_rand() & 0xFF;
568         }
569
570         /* Remove duplicates from the keys array */
571         do {
572                 num_duplicates = 0;
573                 /* Sort the list of keys to make it easier to find duplicates */
574                 qsort(generated_keys, MAX_ENTRIES, KEY_SIZE, key_compare);
575
576                 /* Sift through the list of keys and look for duplicates */
577                 int num_duplicates = 0;
578                 for (i = 0; i < MAX_ENTRIES - 1; i++) {
579                         if (memcmp(generated_keys[i], generated_keys[i + 1],
580                                         KEY_SIZE) == 0) {
581                                 /* This key already exists, try again */
582                                 num_duplicates++;
583                                 for (j = 0; j < KEY_SIZE; j++)
584                                         generated_keys[i][j] =
585                                                         rte_rand() & 0xFF;
586                         }
587                 }
588         } while (num_duplicates != 0);
589 }
590
591 static inline int
592 add_generated_keys(struct rte_member_setsum *setsum, unsigned int *added_keys)
593 {
594         int ret = 0;
595
596         for (*added_keys = 0; ret >= 0 && *added_keys < MAX_ENTRIES;
597                         (*added_keys)++) {
598                 uint16_t set = (rte_rand() & 0xf) + 1;
599                 ret = rte_member_add(setsum, &generated_keys[*added_keys], set);
600         }
601         return ret;
602 }
603
604 static inline int
605 add_generated_keys_cache(struct rte_member_setsum *setsum,
606                                 unsigned int *added_keys)
607 {
608         int ret = 0;
609
610         for (*added_keys = 0; ret == 0 && *added_keys < MAX_ENTRIES;
611                         (*added_keys)++) {
612                 uint16_t set = (rte_rand() & 0xf) + 1;
613                 ret = rte_member_add(setsum, &generated_keys[*added_keys], set);
614         }
615         return ret;
616 }
617
618 static int
619 test_member_loadfactor(void)
620 {
621         unsigned  int j;
622         unsigned int added_keys, average_keys_added = 0;
623         int ret;
624
625         setup_keys_and_data();
626
627         rte_member_free(setsum_ht);
628         rte_member_free(setsum_cache);
629         rte_member_free(setsum_vbf);
630
631         params.key_len = KEY_SIZE;
632         params.name = "test_member_ht";
633         params.is_cache = 0;
634         params.type = RTE_MEMBER_TYPE_HT;
635         setsum_ht = rte_member_create(&params);
636
637         params.name = "test_member_cache";
638         params.is_cache = 1;
639         setsum_cache = rte_member_create(&params);
640
641
642         if (setsum_ht == NULL || setsum_cache == NULL) {
643                 printf("Creation of setsums fail\n");
644                 return -1;
645         }
646         /* Test HT non-cache mode */
647         for (j = 0; j < ITERATIONS; j++) {
648                 /* Add random entries until key cannot be added */
649                 ret = add_generated_keys(setsum_ht, &added_keys);
650                 if (ret != -ENOSPC) {
651                         printf("Unexpected error when adding keys\n");
652                         return -1;
653                 }
654                 average_keys_added += added_keys;
655
656                 /* Reset the table */
657                 rte_member_reset(setsum_ht);
658
659                 /* Print a dot to show progress on operations */
660                 printf(".");
661                 fflush(stdout);
662         }
663
664         average_keys_added /= ITERATIONS;
665
666         printf("\nKeys inserted when no space(non-cache) = %.2f%% (%u/%u)\n",
667                 ((double) average_keys_added / params.num_keys * 100),
668                 average_keys_added, params.num_keys);
669
670         /* Test cache mode */
671         added_keys = average_keys_added = 0;
672         for (j = 0; j < ITERATIONS; j++) {
673                 /* Add random entries until key cannot be added */
674                 ret = add_generated_keys_cache(setsum_cache, &added_keys);
675                 if (ret != 1) {
676                         printf("Unexpected error when adding keys\n");
677                         return -1;
678                 }
679                 average_keys_added += added_keys;
680
681                 /* Reset the table */
682                 rte_member_reset(setsum_cache);
683
684                 /* Print a dot to show progress on operations */
685                 printf(".");
686                 fflush(stdout);
687         }
688
689         average_keys_added /= ITERATIONS;
690
691         printf("\nKeys inserted when eviction happens(cache)= %.2f%% (%u/%u)\n",
692                 ((double) average_keys_added / params.num_keys * 100),
693                 average_keys_added, params.num_keys);
694         return 0;
695 }
696
697 static void
698 perform_free(void)
699 {
700         rte_member_free(setsum_ht);
701         rte_member_free(setsum_cache);
702         rte_member_free(setsum_vbf);
703 }
704
705 static int
706 test_member(void)
707 {
708         if (test_member_create_bad_param() < 0)
709                 return -1;
710
711         if (test_member_find_existing() < 0)
712                 return -1;
713
714         if (test_member_create() < 0) {
715                 perform_free();
716                 return -1;
717         }
718         if (test_member_insert() < 0) {
719                 perform_free();
720                 return -1;
721         }
722         if (test_member_lookup() < 0) {
723                 perform_free();
724                 return -1;
725         }
726         if (test_member_delete() < 0) {
727                 perform_free();
728                 return -1;
729         }
730         if (test_member_multimatch() < 0) {
731                 perform_free();
732                 return -1;
733         }
734         if (test_member_loadfactor() < 0) {
735                 rte_member_free(setsum_ht);
736                 rte_member_free(setsum_cache);
737                 return -1;
738         }
739
740         perform_free();
741         return 0;
742 }
743
744 REGISTER_TEST_COMMAND(member_autotest, test_member);