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