1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
5 #include <rte_memcpy.h>
6 #include <rte_malloc.h>
8 #include <rte_byteorder.h>
9 #include <rte_random.h>
10 #include <rte_debug.h>
15 #define EFD_TEST_KEY_LEN 8
16 #define TABLE_SIZE (1 << 21)
19 #if RTE_EFD_VALUE_NUM_BITS == 32
20 #define VALUE_BITMASK 0xffffffff
22 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
24 static unsigned int test_socket_id;
26 /* 5-tuple key type */
33 } __attribute__((packed));
37 RTE_INIT(test_efd_init_log)
39 efd_logtype_test = rte_log_register("test.efd");
43 * Print out result of unit test efd operation.
45 static void print_key_info(const char *msg, const struct flow_key *key,
48 const uint8_t *p = (const uint8_t *) key;
51 rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%s key:0x", msg);
52 for (i = 0; i < sizeof(struct flow_key); i++)
53 rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%02X", p[i]);
55 rte_log(RTE_LOG_DEBUG, efd_logtype_test, " @ val %d\n", val);
58 /* Keys used by unit test functions */
59 static struct flow_key keys[5] = {
61 .ip_src = RTE_IPV4(0x03, 0x02, 0x01, 0x00),
62 .ip_dst = RTE_IPV4(0x07, 0x06, 0x05, 0x04),
68 .ip_src = RTE_IPV4(0x13, 0x12, 0x11, 0x10),
69 .ip_dst = RTE_IPV4(0x17, 0x16, 0x15, 0x14),
75 .ip_src = RTE_IPV4(0x23, 0x22, 0x21, 0x20),
76 .ip_dst = RTE_IPV4(0x27, 0x26, 0x25, 0x24),
82 .ip_src = RTE_IPV4(0x33, 0x32, 0x31, 0x30),
83 .ip_dst = RTE_IPV4(0x37, 0x36, 0x35, 0x34),
89 .ip_src = RTE_IPV4(0x43, 0x42, 0x41, 0x40),
90 .ip_dst = RTE_IPV4(0x47, 0x46, 0x45, 0x44),
96 /* Array to store the data */
99 static inline uint8_t efd_get_all_sockets_bitmask(void)
101 uint8_t all_cpu_sockets_bitmask = 0;
103 unsigned int next_lcore = rte_get_master_lcore();
104 const int val_true = 1, val_false = 0;
105 for (i = 0; i < rte_lcore_count(); i++) {
106 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
107 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
110 return all_cpu_sockets_bitmask;
114 * Basic sequence of operations for a single key:
118 * Note: lookup (miss) is not applicable since this is a filter
120 static int test_add_delete(void)
122 struct rte_efd_table *handle;
123 /* test with standard add/lookup/delete functions */
124 efd_value_t prev_value;
125 printf("Entering %s\n", __func__);
127 handle = rte_efd_create("test_add_delete",
128 TABLE_SIZE, sizeof(struct flow_key),
129 efd_get_all_sockets_bitmask(), test_socket_id);
130 TEST_ASSERT_NOT_NULL(handle, "Error creating the EFD table\n");
132 data[0] = mrand48() & VALUE_BITMASK;
133 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[0],
135 "Error inserting the key");
136 print_key_info("Add", &keys[0], data[0]);
138 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[0]),
140 "failed to find key");
142 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[0],
144 "failed to delete key");
145 TEST_ASSERT_EQUAL(prev_value, data[0],
146 "failed to delete the expected value, got %d, "
147 "expected %d", prev_value, data[0]);
148 print_key_info("Del", &keys[0], data[0]);
150 rte_efd_free(handle);
156 * Sequence of operations for a single key:
160 * - lookup: hit (updated data)
163 static int test_add_update_delete(void)
165 struct rte_efd_table *handle;
166 printf("Entering %s\n", __func__);
167 /* test with standard add/lookup/delete functions */
168 efd_value_t prev_value;
169 data[1] = mrand48() & VALUE_BITMASK;
171 handle = rte_efd_create("test_add_update_delete", TABLE_SIZE,
172 sizeof(struct flow_key),
173 efd_get_all_sockets_bitmask(), test_socket_id);
174 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
176 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
177 data[1]), "Error inserting the key");
178 print_key_info("Add", &keys[1], data[1]);
180 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
181 data[1], "failed to find key");
182 print_key_info("Lkp", &keys[1], data[1]);
184 data[1] = data[1] + 1;
185 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
186 data[1]), "Error re-inserting the key");
187 print_key_info("Add", &keys[1], data[1]);
189 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
190 data[1], "failed to find key");
191 print_key_info("Lkp", &keys[1], data[1]);
193 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[1],
194 &prev_value), "failed to delete key");
195 TEST_ASSERT_EQUAL(prev_value, data[1],
196 "failed to delete the expected value, got %d, "
197 "expected %d", prev_value, data[1]);
198 print_key_info("Del", &keys[1], data[1]);
201 rte_efd_free(handle);
206 * Sequence of operations for find existing EFD table
209 * - find existing table: hit
210 * - find non-existing table: miss
213 static int test_efd_find_existing(void)
215 struct rte_efd_table *handle = NULL, *result = NULL;
217 printf("Entering %s\n", __func__);
219 /* Create EFD table. */
220 handle = rte_efd_create("efd_find_existing", TABLE_SIZE,
221 sizeof(struct flow_key),
222 efd_get_all_sockets_bitmask(), test_socket_id);
223 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
225 /* Try to find existing EFD table */
226 result = rte_efd_find_existing("efd_find_existing");
227 TEST_ASSERT_EQUAL(result, handle, "could not find existing efd table");
229 /* Try to find non-existing EFD table */
230 result = rte_efd_find_existing("efd_find_non_existing");
231 TEST_ASSERT_NULL(result, "found table that shouldn't exist");
234 rte_efd_free(handle);
240 * Sequence of operations for 5 keys
242 * - lookup keys: hit (bulk)
243 * - add keys (update)
244 * - lookup keys: hit (updated data)
245 * - delete keys : hit
247 static int test_five_keys(void)
249 struct rte_efd_table *handle;
250 const void *key_array[5] = {0};
251 efd_value_t result[5] = {0};
252 efd_value_t prev_value;
254 printf("Entering %s\n", __func__);
256 handle = rte_efd_create("test_five_keys", TABLE_SIZE,
257 sizeof(struct flow_key),
258 efd_get_all_sockets_bitmask(), test_socket_id);
259 TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
262 for (i = 0; i < 5; i++)
263 data[i] = mrand48() & VALUE_BITMASK;
266 for (i = 0; i < 5; i++) {
267 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
269 "Error inserting the key");
270 print_key_info("Add", &keys[i], data[i]);
274 for (i = 0; i < 5; i++)
275 key_array[i] = &keys[i];
277 rte_efd_lookup_bulk(handle, test_socket_id, 5,
278 (void *) &key_array, result);
280 for (i = 0; i < 5; i++) {
281 TEST_ASSERT_EQUAL(result[i], data[i],
282 "bulk: failed to find key. Expected %d, got %d",
284 print_key_info("Lkp", &keys[i], data[i]);
287 /* Modify data (bulk) */
288 for (i = 0; i < 5; i++)
289 data[i] = data[i] + 1;
292 for (i = 0; i < 5; i++) {
293 TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
295 "Error inserting the key");
296 print_key_info("Add", &keys[i], data[i]);
300 for (i = 0; i < 5; i++) {
301 TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id,
303 "failed to find key");
304 print_key_info("Lkp", &keys[i], data[i]);
308 for (i = 0; i < 5; i++) {
309 TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id,
310 &keys[i], &prev_value),
311 "failed to delete key");
312 TEST_ASSERT_EQUAL(prev_value, data[i],
313 "failed to delete the expected value, got %d, "
314 "expected %d", prev_value, data[i]);
315 print_key_info("Del", &keys[i], data[i]);
319 rte_efd_free(handle);
325 * Test to see the average table utilization (entries added/max entries)
326 * before hitting a random entry that cannot be added
328 static int test_average_table_utilization(void)
330 struct rte_efd_table *handle = NULL;
331 uint32_t num_rules_in = TABLE_SIZE;
332 uint8_t simple_key[EFD_TEST_KEY_LEN];
334 unsigned int added_keys, average_keys_added = 0;
336 printf("Evaluating table utilization and correctness, please wait\n");
339 for (j = 0; j < ITERATIONS; j++) {
340 handle = rte_efd_create("test_efd", num_rules_in,
341 EFD_TEST_KEY_LEN, efd_get_all_sockets_bitmask(),
343 if (handle == NULL) {
344 printf("efd table creation failed\n");
348 unsigned int succeeded = 0;
349 unsigned int lost_keys = 0;
351 /* Add random entries until key cannot be added */
352 for (added_keys = 0; added_keys < num_rules_in; added_keys++) {
354 for (i = 0; i < EFD_TEST_KEY_LEN; i++)
355 simple_key[i] = rte_rand() & 0xFF;
357 efd_value_t val = simple_key[0];
359 if (rte_efd_update(handle, test_socket_id, simple_key,
361 break; /* continue;*/
362 if (rte_efd_lookup(handle, test_socket_id, simple_key)
369 average_keys_added += succeeded;
371 /* Reset the table */
372 rte_efd_free(handle);
374 /* Print progress on operations */
375 printf("Added %10u Succeeded %10u Lost %10u\n",
376 added_keys, succeeded, lost_keys);
380 average_keys_added /= ITERATIONS;
382 printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
383 ((double) average_keys_added / num_rules_in * 100),
384 average_keys_added, num_rules_in);
390 * Do tests for EFD creation with bad parameters.
392 static int test_efd_creation_with_bad_parameters(void)
394 struct rte_efd_table *handle, *tmp;
395 printf("Entering %s, **Errors are expected **\n", __func__);
397 handle = rte_efd_create("creation_with_bad_parameters_0", TABLE_SIZE, 0,
398 efd_get_all_sockets_bitmask(), test_socket_id);
399 if (handle != NULL) {
400 rte_efd_free(handle);
401 printf("Impossible creating EFD table successfully "
402 "if key_len in parameter is zero\n");
406 handle = rte_efd_create("creation_with_bad_parameters_1", TABLE_SIZE,
407 sizeof(struct flow_key), 0, test_socket_id);
408 if (handle != NULL) {
409 rte_efd_free(handle);
410 printf("Impossible creating EFD table successfully "
411 "with invalid socket bitmask\n");
415 handle = rte_efd_create("creation_with_bad_parameters_2", TABLE_SIZE,
416 sizeof(struct flow_key), efd_get_all_sockets_bitmask(),
418 if (handle != NULL) {
419 rte_efd_free(handle);
420 printf("Impossible creating EFD table successfully "
421 "with invalid socket\n");
425 /* test with same name should fail */
426 handle = rte_efd_create("same_name", TABLE_SIZE,
427 sizeof(struct flow_key),
428 efd_get_all_sockets_bitmask(), 0);
429 if (handle == NULL) {
430 printf("Cannot create first EFD table with 'same_name'\n");
433 tmp = rte_efd_create("same_name", TABLE_SIZE, sizeof(struct flow_key),
434 efd_get_all_sockets_bitmask(), 0);
436 printf("Creation of EFD table with same name should fail\n");
437 rte_efd_free(handle);
441 rte_efd_free(handle);
443 printf("# Test successful. No more errors expected\n");
453 if (test_add_delete() < 0)
455 if (test_efd_find_existing() < 0)
457 if (test_add_update_delete() < 0)
459 if (test_five_keys() < 0)
461 if (test_efd_creation_with_bad_parameters() < 0)
463 if (test_average_table_utilization() < 0)
469 REGISTER_TEST_COMMAND(efd_autotest, test_efd);