1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 /* This test is for membership library's simple feature test */
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>
17 struct rte_member_setsum *setsum_ht;
18 struct rte_member_setsum *setsum_cache;
19 struct rte_member_setsum *setsum_vbf;
21 /* 5-tuple key type */
28 } __attribute__((packed));
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 */
33 #define M_MATCH_STEP 2
35 (1 + (M_MATCH_E - M_MATCH_S) / M_MATCH_STEP)
41 /* Keys used by unit test functions */
42 static struct flow_key keys[NUM_SAMPLES] = {
44 .ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
45 .ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
51 .ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
52 .ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
58 .ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
59 .ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
65 .ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
66 .ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
72 .ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
73 .ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
80 uint32_t test_set[NUM_SAMPLES] = {1, 2, 3, 4, 5};
85 #define MAX_ENTRIES (1 << 16)
86 uint8_t generated_keys[MAX_ENTRIES][KEY_SIZE];
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. */
92 /* num_set and false_positive_rate only relevant to vBF */
94 .false_positive_rate = 0.03,
97 .socket_id = 0 /* NUMA Socket ID for memory. */
101 * Sequence of operations for find existing setsummary
104 * - find existing setsum: hit
105 * - find non-existing setsum: miss
109 test_member_find_existing(void)
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,
118 .false_positive_rate = 0.03,
121 .socket_id = 0 /* NUMA Socket ID for memory. */
125 tmp_setsum = rte_member_create(&tmp_params);
126 TEST_ASSERT(tmp_setsum != NULL, "setsum creation failed");
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");
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");
137 rte_member_free(tmp_setsum);
143 * Test for bad creating parameters
146 test_member_create_bad_param(void)
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,
154 .false_positive_rate = 0.03,
157 .socket_id = 0 /* NUMA Socket ID for memory. */
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");
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");
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");
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");
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");
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 "
229 printf("Expected error section end...\n");
230 rte_member_free(bad_setsum);
234 /* Create test setsummaries. */
235 static int test_member_create(void)
237 params.key_len = sizeof(struct flow_key);
239 params.name = "test_member_ht";
241 params.type = RTE_MEMBER_TYPE_HT;
242 setsum_ht = rte_member_create(¶ms);
244 params.name = "test_member_cache";
246 setsum_cache = rte_member_create(¶ms);
248 params.name = "test_member_vbf";
249 params.type = RTE_MEMBER_TYPE_VBF;
250 setsum_vbf = rte_member_create(¶ms);
252 if (setsum_ht == NULL || setsum_cache == NULL || setsum_vbf == NULL) {
253 printf("Creation of setsums fail\n");
256 printf("Creation of setsums success\n");
260 static int test_member_insert(void)
262 int ret_ht, ret_cache, ret_vbf, i;
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],
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,
272 printf("insert key success\n");
276 static int test_member_lookup(void)
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};
284 uint32_t num_key_ht = NUM_SAMPLES;
285 uint32_t num_key_cache = NUM_SAMPLES;
286 uint32_t num_key_vbf = NUM_SAMPLES;
288 const void *key_array[NUM_SAMPLES];
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],
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");
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");
304 printf("lookup single key success\n");
306 /* Bulk lookup test */
307 for (i = 0; i < NUM_SAMPLES; i++)
308 key_array[i] = &keys[i];
310 ret_ht = rte_member_lookup_bulk(setsum_ht, key_array,
311 num_key_ht, set_ids_ht);
313 ret_cache = rte_member_lookup_bulk(setsum_cache, key_array,
314 num_key_cache, set_ids_cache);
316 ret_vbf = rte_member_lookup_bulk(setsum_vbf, key_array,
317 num_key_vbf, set_ids_vbf);
319 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
320 "bulk lookup function error");
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");
332 static int test_member_delete(void)
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;
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],
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");
357 for (i = 0; i < NUM_SAMPLES; i++)
358 key_array[i] = &keys[i];
360 ret_ht = rte_member_lookup_bulk(setsum_ht, key_array,
361 num_key_ht, set_ids_ht);
363 ret_cache = rte_member_lookup_bulk(setsum_cache, key_array,
364 num_key_cache, set_ids_cache);
366 ret_vbf = rte_member_lookup_bulk(setsum_vbf, key_array,
367 num_key_vbf, set_ids_vbf);
369 TEST_ASSERT(ret_ht >= 0 && ret_cache >= 0 && ret_vbf >= 0,
370 "bulk lookup function error");
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");
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");
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],
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");
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],
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");
409 /* Reset vbf for other following tests */
410 rte_member_reset(setsum_vbf);
412 printf("delete success\n");
416 static int test_member_multimatch(void)
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};
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} };
427 uint32_t match_count_ht[NUM_SAMPLES];
428 uint32_t match_count_vbf[NUM_SAMPLES];
429 uint32_t match_count_cache[NUM_SAMPLES];
431 uint32_t num_key_ht = NUM_SAMPLES;
432 uint32_t num_key_vbf = NUM_SAMPLES;
433 uint32_t num_key_cache = NUM_SAMPLES;
435 const void *key_array[NUM_SAMPLES];
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);
446 TEST_ASSERT(ret_ht >= 0 && ret_vbf >= 0 &&
448 "insert function error");
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);
461 * For cache mode, keys overwrite when signature same.
462 * the mutimatch should work like single match.
464 TEST_ASSERT(ret_ht == M_MATCH_CNT && ret_vbf == M_MATCH_CNT &&
466 "single lookup_multi error");
467 TEST_ASSERT(set_ids_cache[0] == M_MATCH_E,
468 "single lookup_multi cache error");
470 for (j = 1; j <= M_MATCH_CNT; j++) {
471 TEST_ASSERT(set_ids_ht[j-1] == j * M_MATCH_STEP - 1 &&
473 j * M_MATCH_STEP - 1,
474 "single multimatch lookup error");
477 printf("lookup single key for multimatch success\n");
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);
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);
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);
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");
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");
515 printf("lookup for bulk multimatch success\n");
520 static int key_compare(const void *key1, const void *key2)
522 return memcmp(key1, key2, KEY_SIZE);
526 setup_keys_and_data(void)
531 /* Reset all arrays */
532 for (i = 0; i < KEY_SIZE; i++)
533 generated_keys[0][i] = 0;
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;
541 /* Remove duplicates from the keys array */
544 /* Sort the list of keys to make it easier to find duplicates */
545 qsort(generated_keys, MAX_ENTRIES, KEY_SIZE, key_compare);
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],
552 /* This key already exists, try again */
554 for (j = 0; j < KEY_SIZE; j++)
555 generated_keys[i][j] =
559 } while (num_duplicates != 0);
563 add_generated_keys(struct rte_member_setsum *setsum, unsigned int *added_keys)
567 for (*added_keys = 0; ret >= 0 && *added_keys < MAX_ENTRIES;
569 uint16_t set = (rte_rand() & 0xf) + 1;
570 ret = rte_member_add(setsum, &generated_keys[*added_keys], set);
576 add_generated_keys_cache(struct rte_member_setsum *setsum,
577 unsigned int *added_keys)
581 for (*added_keys = 0; ret == 0 && *added_keys < MAX_ENTRIES;
583 uint16_t set = (rte_rand() & 0xf) + 1;
584 ret = rte_member_add(setsum, &generated_keys[*added_keys], set);
590 test_member_loadfactor(void)
593 unsigned int added_keys, average_keys_added = 0;
596 setup_keys_and_data();
598 rte_member_free(setsum_ht);
599 rte_member_free(setsum_cache);
600 rte_member_free(setsum_vbf);
602 params.key_len = KEY_SIZE;
603 params.name = "test_member_ht";
605 params.type = RTE_MEMBER_TYPE_HT;
606 setsum_ht = rte_member_create(¶ms);
608 params.name = "test_member_cache";
610 setsum_cache = rte_member_create(¶ms);
613 if (setsum_ht == NULL || setsum_cache == NULL) {
614 printf("Creation of setsums fail\n");
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");
625 average_keys_added += added_keys;
627 /* Reset the table */
628 rte_member_reset(setsum_ht);
630 /* Print a dot to show progress on operations */
635 average_keys_added /= ITERATIONS;
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);
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);
647 printf("Unexpected error when adding keys\n");
650 average_keys_added += added_keys;
652 /* Reset the table */
653 rte_member_reset(setsum_cache);
655 /* Print a dot to show progress on operations */
660 average_keys_added /= ITERATIONS;
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);
671 rte_member_free(setsum_ht);
672 rte_member_free(setsum_cache);
673 rte_member_free(setsum_vbf);
679 if (test_member_create_bad_param() < 0)
682 if (test_member_find_existing() < 0)
685 if (test_member_create() < 0) {
689 if (test_member_insert() < 0) {
693 if (test_member_lookup() < 0) {
697 if (test_member_delete() < 0) {
701 if (test_member_multimatch() < 0) {
705 if (test_member_loadfactor() < 0) {
706 rte_member_free(setsum_ht);
707 rte_member_free(setsum_cache);
715 REGISTER_TEST_COMMAND(member_autotest, test_member);