1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
6 #ifdef RTE_EXEC_ENV_WINDOWS
10 printf("EFD not supported on Windows, skipping test\n");
16 #include <rte_memcpy.h>
17 #include <rte_malloc.h>
19 #include <rte_byteorder.h>
20 #include <rte_random.h>
21 #include <rte_debug.h>
24 #define EFD_TEST_KEY_LEN 8
25 #define TABLE_SIZE (1 << 21)
28 #if RTE_EFD_VALUE_NUM_BITS == 32
29 #define VALUE_BITMASK 0xffffffff
31 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
33 static unsigned int test_socket_id;
35 /* 5-tuple key type */
44 RTE_LOG_REGISTER(efd_logtype_test, test.efd, INFO);
47 * Print out result of unit test efd operation.
49 static void print_key_info(const char *msg, const struct flow_key *key,
52 const uint8_t *p = (const uint8_t *) key;
55 rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%s key:0x", msg);
56 for (i = 0; i < sizeof(struct flow_key); i++)
57 rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%02X", p[i]);
59 rte_log(RTE_LOG_DEBUG, efd_logtype_test, " @ val %d\n", val);
62 /* Keys used by unit test functions */
63 static struct flow_key keys[5] = {
65 .ip_src = RTE_IPV4(0x03, 0x02, 0x01, 0x00),
66 .ip_dst = RTE_IPV4(0x07, 0x06, 0x05, 0x04),
72 .ip_src = RTE_IPV4(0x13, 0x12, 0x11, 0x10),
73 .ip_dst = RTE_IPV4(0x17, 0x16, 0x15, 0x14),
79 .ip_src = RTE_IPV4(0x23, 0x22, 0x21, 0x20),
80 .ip_dst = RTE_IPV4(0x27, 0x26, 0x25, 0x24),
86 .ip_src = RTE_IPV4(0x33, 0x32, 0x31, 0x30),
87 .ip_dst = RTE_IPV4(0x37, 0x36, 0x35, 0x34),
93 .ip_src = RTE_IPV4(0x43, 0x42, 0x41, 0x40),
94 .ip_dst = RTE_IPV4(0x47, 0x46, 0x45, 0x44),
100 /* Array to store the data */
101 static efd_value_t data[5];
103 static inline uint64_t efd_get_all_sockets_bitmask(void)
105 uint64_t all_cpu_sockets_bitmask = 0;
107 unsigned int next_lcore = rte_get_main_lcore();
108 const int val_true = 1, val_false = 0;
109 for (i = 0; i < rte_lcore_count(); i++) {
110 all_cpu_sockets_bitmask |= 1ULL << rte_lcore_to_socket_id(next_lcore);
111 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
114 return all_cpu_sockets_bitmask;
118 * Basic sequence of operations for a single key:
122 * Note: lookup (miss) is not applicable since this is a filter
124 static int test_add_delete(void)
126 struct rte_efd_table *handle;
127 /* test with standard add/lookup/delete functions */
128 efd_value_t prev_value;
129 printf("Entering %s\n", __func__);
131 handle = rte_efd_create("test_add_delete",
132 TABLE_SIZE, sizeof(struct flow_key),
133 efd_get_all_sockets_bitmask(), test_socket_id);
134 TEST_ASSERT_NOT_NULL(handle, "Error creating the EFD table\n");
136 data[0] = mrand48() & VALUE_BITMASK;
137 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[0],
139 "Error inserting the key");
140 print_key_info("Add", &keys[0], data[0]);
142 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[0]),
144 "failed to find key");
146 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[0],
148 "failed to delete key");
149 TEST_ASSERT_EQUAL(prev_value, data[0],
150 "failed to delete the expected value, got %d, "
151 "expected %d", prev_value, data[0]);
152 print_key_info("Del", &keys[0], data[0]);
154 rte_efd_free(handle);
160 * Sequence of operations for a single key:
164 * - lookup: hit (updated data)
167 static int test_add_update_delete(void)
169 struct rte_efd_table *handle;
170 printf("Entering %s\n", __func__);
171 /* test with standard add/lookup/delete functions */
172 efd_value_t prev_value;
173 data[1] = mrand48() & VALUE_BITMASK;
175 handle = rte_efd_create("test_add_update_delete", TABLE_SIZE,
176 sizeof(struct flow_key),
177 efd_get_all_sockets_bitmask(), test_socket_id);
178 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
180 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
181 data[1]), "Error inserting the key");
182 print_key_info("Add", &keys[1], data[1]);
184 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
185 data[1], "failed to find key");
186 print_key_info("Lkp", &keys[1], data[1]);
188 data[1] = data[1] + 1;
189 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
190 data[1]), "Error re-inserting the key");
191 print_key_info("Add", &keys[1], data[1]);
193 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
194 data[1], "failed to find key");
195 print_key_info("Lkp", &keys[1], data[1]);
197 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[1],
198 &prev_value), "failed to delete key");
199 TEST_ASSERT_EQUAL(prev_value, data[1],
200 "failed to delete the expected value, got %d, "
201 "expected %d", prev_value, data[1]);
202 print_key_info("Del", &keys[1], data[1]);
205 rte_efd_free(handle);
210 * Sequence of operations for find existing EFD table
213 * - find existing table: hit
214 * - find non-existing table: miss
217 static int test_efd_find_existing(void)
219 struct rte_efd_table *handle = NULL, *result = NULL;
221 printf("Entering %s\n", __func__);
223 /* Create EFD table. */
224 handle = rte_efd_create("efd_find_existing", TABLE_SIZE,
225 sizeof(struct flow_key),
226 efd_get_all_sockets_bitmask(), test_socket_id);
227 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
229 /* Try to find existing EFD table */
230 result = rte_efd_find_existing("efd_find_existing");
231 TEST_ASSERT_EQUAL(result, handle, "could not find existing efd table");
233 /* Try to find non-existing EFD table */
234 result = rte_efd_find_existing("efd_find_non_existing");
235 TEST_ASSERT_NULL(result, "found table that shouldn't exist");
238 rte_efd_free(handle);
244 * Sequence of operations for 5 keys
246 * - lookup keys: hit (bulk)
247 * - add keys (update)
248 * - lookup keys: hit (updated data)
249 * - delete keys : hit
251 static int test_five_keys(void)
253 struct rte_efd_table *handle;
254 const void *key_array[5] = {0};
255 efd_value_t result[5] = {0};
256 efd_value_t prev_value;
258 printf("Entering %s\n", __func__);
260 handle = rte_efd_create("test_five_keys", TABLE_SIZE,
261 sizeof(struct flow_key),
262 efd_get_all_sockets_bitmask(), test_socket_id);
263 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
266 for (i = 0; i < 5; i++)
267 data[i] = mrand48() & VALUE_BITMASK;
270 for (i = 0; i < 5; i++) {
271 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
273 "Error inserting the key");
274 print_key_info("Add", &keys[i], data[i]);
278 for (i = 0; i < 5; i++)
279 key_array[i] = &keys[i];
281 rte_efd_lookup_bulk(handle, test_socket_id, 5,
282 (void *) &key_array, result);
284 for (i = 0; i < 5; i++) {
285 TEST_ASSERT_EQUAL(result[i], data[i],
286 "bulk: failed to find key. Expected %d, got %d",
288 print_key_info("Lkp", &keys[i], data[i]);
291 /* Modify data (bulk) */
292 for (i = 0; i < 5; i++)
293 data[i] = data[i] + 1;
296 for (i = 0; i < 5; i++) {
297 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
299 "Error inserting the key");
300 print_key_info("Add", &keys[i], data[i]);
304 for (i = 0; i < 5; i++) {
305 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id,
307 "failed to find key");
308 print_key_info("Lkp", &keys[i], data[i]);
312 for (i = 0; i < 5; i++) {
313 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id,
314 &keys[i], &prev_value),
315 "failed to delete key");
316 TEST_ASSERT_EQUAL(prev_value, data[i],
317 "failed to delete the expected value, got %d, "
318 "expected %d", prev_value, data[i]);
319 print_key_info("Del", &keys[i], data[i]);
323 rte_efd_free(handle);
329 * Test to see the average table utilization (entries added/max entries)
330 * before hitting a random entry that cannot be added
332 static int test_average_table_utilization(void)
334 struct rte_efd_table *handle = NULL;
335 uint32_t num_rules_in = TABLE_SIZE;
336 uint8_t simple_key[EFD_TEST_KEY_LEN];
338 unsigned int added_keys, average_keys_added = 0;
340 printf("Evaluating table utilization and correctness, please wait\n");
343 for (j = 0; j < ITERATIONS; j++) {
344 handle = rte_efd_create("test_efd", num_rules_in,
345 EFD_TEST_KEY_LEN, efd_get_all_sockets_bitmask(),
347 if (handle == NULL) {
348 printf("efd table creation failed\n");
352 unsigned int succeeded = 0;
353 unsigned int lost_keys = 0;
355 /* Add random entries until key cannot be added */
356 for (added_keys = 0; added_keys < num_rules_in; added_keys++) {
358 for (i = 0; i < EFD_TEST_KEY_LEN; i++)
359 simple_key[i] = rte_rand() & 0xFF;
361 efd_value_t val = simple_key[0];
363 if (rte_efd_update(handle, test_socket_id, simple_key,
365 break; /* continue;*/
366 if (rte_efd_lookup(handle, test_socket_id, simple_key)
373 average_keys_added += succeeded;
375 /* Reset the table */
376 rte_efd_free(handle);
378 /* Print progress on operations */
379 printf("Added %10u Succeeded %10u Lost %10u\n",
380 added_keys, succeeded, lost_keys);
384 average_keys_added /= ITERATIONS;
386 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
387 ((double) average_keys_added / num_rules_in * 100),
388 average_keys_added, num_rules_in);
394 * Do tests for EFD creation with bad parameters.
396 static int test_efd_creation_with_bad_parameters(void)
398 struct rte_efd_table *handle, *tmp;
399 printf("Entering %s, **Errors are expected **\n", __func__);
401 handle = rte_efd_create("creation_with_bad_parameters_0", TABLE_SIZE, 0,
402 efd_get_all_sockets_bitmask(), test_socket_id);
403 if (handle != NULL) {
404 rte_efd_free(handle);
405 printf("Impossible creating EFD table successfully "
406 "if key_len in parameter is zero\n");
410 handle = rte_efd_create("creation_with_bad_parameters_1", TABLE_SIZE,
411 sizeof(struct flow_key), 0, test_socket_id);
412 if (handle != NULL) {
413 rte_efd_free(handle);
414 printf("Impossible creating EFD table successfully "
415 "with invalid socket bitmask\n");
419 handle = rte_efd_create("creation_with_bad_parameters_2", TABLE_SIZE,
420 sizeof(struct flow_key), efd_get_all_sockets_bitmask(),
422 if (handle != NULL) {
423 rte_efd_free(handle);
424 printf("Impossible creating EFD table successfully "
425 "with invalid socket\n");
429 /* test with same name should fail */
430 handle = rte_efd_create("same_name", TABLE_SIZE,
431 sizeof(struct flow_key),
432 efd_get_all_sockets_bitmask(), 0);
433 if (handle == NULL) {
434 printf("Cannot create first EFD table with 'same_name'\n");
437 tmp = rte_efd_create("same_name", TABLE_SIZE, sizeof(struct flow_key),
438 efd_get_all_sockets_bitmask(), 0);
440 printf("Creation of EFD table with same name should fail\n");
441 rte_efd_free(handle);
445 rte_efd_free(handle);
447 printf("# Test successful. No more errors expected\n");
455 test_socket_id = rte_socket_id();
458 if (test_add_delete() < 0)
460 if (test_efd_find_existing() < 0)
462 if (test_add_update_delete() < 0)
464 if (test_five_keys() < 0)
466 if (test_efd_creation_with_bad_parameters() < 0)
468 if (test_average_table_utilization() < 0)
474 #endif /* !RTE_EXEC_ENV_WINDOWS */
476 REGISTER_TEST_COMMAND(efd_autotest, test_efd);