Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : : #include "test.h"
5 : :
6 : : #ifdef RTE_EXEC_ENV_WINDOWS
7 : : static int
8 : : test_efd(void)
9 : : {
10 : : printf("EFD not supported on Windows, skipping test\n");
11 : : return TEST_SKIPPED;
12 : : }
13 : :
14 : : #else
15 : :
16 : : #include <rte_memcpy.h>
17 : : #include <rte_malloc.h>
18 : : #include <rte_efd.h>
19 : : #include <rte_byteorder.h>
20 : : #include <rte_random.h>
21 : : #include <rte_debug.h>
22 : : #include <rte_ip.h>
23 : :
24 : : #define EFD_TEST_KEY_LEN 8
25 : : #define TABLE_SIZE (1 << 21)
26 : : #define ITERATIONS 3
27 : :
28 : : #if RTE_EFD_VALUE_NUM_BITS == 32
29 : : #define VALUE_BITMASK 0xffffffff
30 : : #else
31 : : #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
32 : : #endif
33 : : static unsigned int test_socket_id;
34 : :
35 : : /* 5-tuple key type */
36 : : struct flow_key {
37 : : uint32_t ip_src;
38 : : uint32_t ip_dst;
39 : : uint16_t port_src;
40 : : uint16_t port_dst;
41 : : uint8_t proto;
42 : : } __rte_packed;
43 : :
44 [ - + ]: 251 : RTE_LOG_REGISTER(efd_logtype_test, test.efd, INFO);
45 : :
46 : : /*
47 : : * Print out result of unit test efd operation.
48 : : */
49 : 0 : static void print_key_info(const char *msg, const struct flow_key *key,
50 : : efd_value_t val)
51 : : {
52 : : const uint8_t *p = (const uint8_t *) key;
53 : : unsigned int i;
54 : :
55 : 0 : rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%s key:0x", msg);
56 [ # # ]: 0 : for (i = 0; i < sizeof(struct flow_key); i++)
57 : 0 : rte_log(RTE_LOG_DEBUG, efd_logtype_test, "%02X", p[i]);
58 : :
59 : 0 : rte_log(RTE_LOG_DEBUG, efd_logtype_test, " @ val %d\n", val);
60 : 0 : }
61 : :
62 : : /* Keys used by unit test functions */
63 : : static struct flow_key keys[5] = {
64 : : {
65 : : .ip_src = RTE_IPV4(0x03, 0x02, 0x01, 0x00),
66 : : .ip_dst = RTE_IPV4(0x07, 0x06, 0x05, 0x04),
67 : : .port_src = 0x0908,
68 : : .port_dst = 0x0b0a,
69 : : .proto = 0x0c,
70 : : },
71 : : {
72 : : .ip_src = RTE_IPV4(0x13, 0x12, 0x11, 0x10),
73 : : .ip_dst = RTE_IPV4(0x17, 0x16, 0x15, 0x14),
74 : : .port_src = 0x1918,
75 : : .port_dst = 0x1b1a,
76 : : .proto = 0x1c,
77 : : },
78 : : {
79 : : .ip_src = RTE_IPV4(0x23, 0x22, 0x21, 0x20),
80 : : .ip_dst = RTE_IPV4(0x27, 0x26, 0x25, 0x24),
81 : : .port_src = 0x2928,
82 : : .port_dst = 0x2b2a,
83 : : .proto = 0x2c,
84 : : },
85 : : {
86 : : .ip_src = RTE_IPV4(0x33, 0x32, 0x31, 0x30),
87 : : .ip_dst = RTE_IPV4(0x37, 0x36, 0x35, 0x34),
88 : : .port_src = 0x3938,
89 : : .port_dst = 0x3b3a,
90 : : .proto = 0x3c,
91 : : },
92 : : {
93 : : .ip_src = RTE_IPV4(0x43, 0x42, 0x41, 0x40),
94 : : .ip_dst = RTE_IPV4(0x47, 0x46, 0x45, 0x44),
95 : : .port_src = 0x4948,
96 : : .port_dst = 0x4b4a,
97 : : .proto = 0x4c,
98 : : }
99 : : };
100 : : /* Array to store the data */
101 : : static efd_value_t data[5];
102 : :
103 : 0 : static inline uint64_t efd_get_all_sockets_bitmask(void)
104 : : {
105 : : uint64_t all_cpu_sockets_bitmask = 0;
106 : : unsigned int i;
107 : 0 : unsigned int next_lcore = rte_get_main_lcore();
108 : : const int val_true = 1, val_false = 0;
109 [ # # ]: 0 : for (i = 0; i < rte_lcore_count(); i++) {
110 : 0 : all_cpu_sockets_bitmask |= 1ULL << rte_lcore_to_socket_id(next_lcore);
111 : 0 : next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
112 : : }
113 : :
114 : 0 : return all_cpu_sockets_bitmask;
115 : : }
116 : :
117 : : /*
118 : : * Basic sequence of operations for a single key:
119 : : * - add
120 : : * - lookup (hit)
121 : : * - delete
122 : : * Note: lookup (miss) is not applicable since this is a filter
123 : : */
124 : 0 : static int test_add_delete(void)
125 : : {
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__);
130 : :
131 : 0 : handle = rte_efd_create("test_add_delete",
132 : : TABLE_SIZE, sizeof(struct flow_key),
133 : : efd_get_all_sockets_bitmask(), test_socket_id);
134 [ # # ]: 0 : TEST_ASSERT_NOT_NULL(handle, "Error creating the EFD table\n");
135 : :
136 : 0 : data[0] = mrand48() & VALUE_BITMASK;
137 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[0],
138 : : data[0]),
139 : : "Error inserting the key");
140 : 0 : print_key_info("Add", &keys[0], data[0]);
141 : :
142 [ # # ]: 0 : TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[0]),
143 : : data[0],
144 : : "failed to find key");
145 : :
146 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[0],
147 : : &prev_value),
148 : : "failed to delete key");
149 [ # # ]: 0 : TEST_ASSERT_EQUAL(prev_value, data[0],
150 : : "failed to delete the expected value, got %d, "
151 : : "expected %d", prev_value, data[0]);
152 : 0 : print_key_info("Del", &keys[0], data[0]);
153 : :
154 : 0 : rte_efd_free(handle);
155 : :
156 : 0 : return 0;
157 : : }
158 : :
159 : : /*
160 : : * Sequence of operations for a single key:
161 : : * - add
162 : : * - lookup: hit
163 : : * - add: update
164 : : * - lookup: hit (updated data)
165 : : * - delete: hit
166 : : */
167 : 0 : static int test_add_update_delete(void)
168 : : {
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 : 0 : data[1] = mrand48() & VALUE_BITMASK;
174 : :
175 : 0 : 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 [ # # ]: 0 : TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
179 : :
180 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
181 : : data[1]), "Error inserting the key");
182 : 0 : print_key_info("Add", &keys[1], data[1]);
183 : :
184 [ # # ]: 0 : TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
185 : : data[1], "failed to find key");
186 : 0 : print_key_info("Lkp", &keys[1], data[1]);
187 : :
188 : 0 : data[1] = data[1] + 1;
189 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id, &keys[1],
190 : : data[1]), "Error re-inserting the key");
191 : 0 : print_key_info("Add", &keys[1], data[1]);
192 : :
193 [ # # ]: 0 : TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id, &keys[1]),
194 : : data[1], "failed to find key");
195 : 0 : print_key_info("Lkp", &keys[1], data[1]);
196 : :
197 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id, &keys[1],
198 : : &prev_value), "failed to delete key");
199 [ # # ]: 0 : TEST_ASSERT_EQUAL(prev_value, data[1],
200 : : "failed to delete the expected value, got %d, "
201 : : "expected %d", prev_value, data[1]);
202 : 0 : print_key_info("Del", &keys[1], data[1]);
203 : :
204 : :
205 : 0 : rte_efd_free(handle);
206 : 0 : return 0;
207 : : }
208 : :
209 : : /*
210 : : * Sequence of operations for find existing EFD table
211 : : *
212 : : * - create table
213 : : * - find existing table: hit
214 : : * - find non-existing table: miss
215 : : *
216 : : */
217 : 0 : static int test_efd_find_existing(void)
218 : : {
219 : : struct rte_efd_table *handle = NULL, *result = NULL;
220 : :
221 : : printf("Entering %s\n", __func__);
222 : :
223 : : /* Create EFD table. */
224 : 0 : handle = rte_efd_create("efd_find_existing", TABLE_SIZE,
225 : : sizeof(struct flow_key),
226 : : efd_get_all_sockets_bitmask(), test_socket_id);
227 [ # # ]: 0 : TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
228 : :
229 : : /* Try to find existing EFD table */
230 : 0 : result = rte_efd_find_existing("efd_find_existing");
231 [ # # ]: 0 : TEST_ASSERT_EQUAL(result, handle, "could not find existing efd table");
232 : :
233 : : /* Try to find non-existing EFD table */
234 : 0 : result = rte_efd_find_existing("efd_find_non_existing");
235 [ # # ]: 0 : TEST_ASSERT_NULL(result, "found table that shouldn't exist");
236 : :
237 : : /* Cleanup. */
238 : 0 : rte_efd_free(handle);
239 : :
240 : 0 : return 0;
241 : : }
242 : :
243 : : /*
244 : : * Sequence of operations for 5 keys
245 : : * - add keys
246 : : * - lookup keys: hit (bulk)
247 : : * - add keys (update)
248 : : * - lookup keys: hit (updated data)
249 : : * - delete keys : hit
250 : : */
251 : 0 : static int test_five_keys(void)
252 : : {
253 : : struct rte_efd_table *handle;
254 : 0 : const void *key_array[5] = {0};
255 : 0 : efd_value_t result[5] = {0};
256 : : efd_value_t prev_value;
257 : : unsigned int i;
258 : : printf("Entering %s\n", __func__);
259 : :
260 : 0 : handle = rte_efd_create("test_five_keys", TABLE_SIZE,
261 : : sizeof(struct flow_key),
262 : : efd_get_all_sockets_bitmask(), test_socket_id);
263 [ # # ]: 0 : TEST_ASSERT_NOT_NULL(handle, "Error creating the efd table\n");
264 : :
265 : : /* Setup data */
266 [ # # ]: 0 : for (i = 0; i < 5; i++)
267 : 0 : data[i] = mrand48() & VALUE_BITMASK;
268 : :
269 : : /* Add */
270 [ # # ]: 0 : for (i = 0; i < 5; i++) {
271 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
272 : : &keys[i], data[i]),
273 : : "Error inserting the key");
274 : 0 : print_key_info("Add", &keys[i], data[i]);
275 : : }
276 : :
277 : : /* Lookup */
278 [ # # ]: 0 : for (i = 0; i < 5; i++)
279 : 0 : key_array[i] = &keys[i];
280 : :
281 : 0 : rte_efd_lookup_bulk(handle, test_socket_id, 5,
282 : : (void *) &key_array, result);
283 : :
284 [ # # ]: 0 : for (i = 0; i < 5; i++) {
285 [ # # ]: 0 : TEST_ASSERT_EQUAL(result[i], data[i],
286 : : "bulk: failed to find key. Expected %d, got %d",
287 : : data[i], result[i]);
288 : 0 : print_key_info("Lkp", &keys[i], data[i]);
289 : : }
290 : :
291 : : /* Modify data (bulk) */
292 [ # # ]: 0 : for (i = 0; i < 5; i++)
293 : 0 : data[i] = data[i] + 1;
294 : :
295 : : /* Add - update */
296 [ # # ]: 0 : for (i = 0; i < 5; i++) {
297 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_update(handle, test_socket_id,
298 : : &keys[i], data[i]),
299 : : "Error inserting the key");
300 : 0 : print_key_info("Add", &keys[i], data[i]);
301 : : }
302 : :
303 : : /* Lookup */
304 [ # # ]: 0 : for (i = 0; i < 5; i++) {
305 [ # # ]: 0 : TEST_ASSERT_EQUAL(rte_efd_lookup(handle, test_socket_id,
306 : : &keys[i]), data[i],
307 : : "failed to find key");
308 : 0 : print_key_info("Lkp", &keys[i], data[i]);
309 : : }
310 : :
311 : : /* Delete */
312 [ # # ]: 0 : for (i = 0; i < 5; i++) {
313 [ # # ]: 0 : TEST_ASSERT_SUCCESS(rte_efd_delete(handle, test_socket_id,
314 : : &keys[i], &prev_value),
315 : : "failed to delete key");
316 [ # # ]: 0 : TEST_ASSERT_EQUAL(prev_value, data[i],
317 : : "failed to delete the expected value, got %d, "
318 : : "expected %d", prev_value, data[i]);
319 : 0 : print_key_info("Del", &keys[i], data[i]);
320 : : }
321 : :
322 : :
323 : 0 : rte_efd_free(handle);
324 : :
325 : 0 : return 0;
326 : : }
327 : :
328 : : /*
329 : : * Test to see the average table utilization (entries added/max entries)
330 : : * before hitting a random entry that cannot be added
331 : : */
332 : 0 : static int test_average_table_utilization(void)
333 : : {
334 : : struct rte_efd_table *handle = NULL;
335 : : uint32_t num_rules_in = TABLE_SIZE;
336 : : uint8_t simple_key[EFD_TEST_KEY_LEN];
337 : : unsigned int i, j;
338 : : unsigned int added_keys, average_keys_added = 0;
339 : :
340 : : printf("Evaluating table utilization and correctness, please wait\n");
341 : 0 : fflush(stdout);
342 : :
343 [ # # ]: 0 : for (j = 0; j < ITERATIONS; j++) {
344 : 0 : handle = rte_efd_create("test_efd", num_rules_in,
345 : : EFD_TEST_KEY_LEN, efd_get_all_sockets_bitmask(),
346 : : test_socket_id);
347 [ # # ]: 0 : if (handle == NULL) {
348 : : printf("efd table creation failed\n");
349 : 0 : return -1;
350 : : }
351 : :
352 : : unsigned int succeeded = 0;
353 : : unsigned int lost_keys = 0;
354 : :
355 : : /* Add random entries until key cannot be added */
356 [ # # ]: 0 : for (added_keys = 0; added_keys < num_rules_in; added_keys++) {
357 : :
358 [ # # ]: 0 : for (i = 0; i < EFD_TEST_KEY_LEN; i++)
359 : 0 : simple_key[i] = rte_rand() & 0xFF;
360 : :
361 : 0 : efd_value_t val = simple_key[0];
362 : :
363 [ # # ]: 0 : if (rte_efd_update(handle, test_socket_id, simple_key,
364 : : val))
365 : : break; /* continue;*/
366 [ # # ]: 0 : if (rte_efd_lookup(handle, test_socket_id, simple_key)
367 : : != val)
368 : 0 : lost_keys++;
369 : : else
370 : 0 : succeeded++;
371 : : }
372 : :
373 : 0 : average_keys_added += succeeded;
374 : :
375 : : /* Reset the table */
376 : 0 : rte_efd_free(handle);
377 : :
378 : : /* Print progress on operations */
379 : : printf("Added %10u Succeeded %10u Lost %10u\n",
380 : : added_keys, succeeded, lost_keys);
381 : 0 : fflush(stdout);
382 : : }
383 : :
384 : 0 : average_keys_added /= ITERATIONS;
385 : :
386 : 0 : printf("\nAverage table utilization = %.2f%% (%u/%u)\n",
387 : 0 : ((double) average_keys_added / num_rules_in * 100),
388 : : average_keys_added, num_rules_in);
389 : :
390 : 0 : return 0;
391 : : }
392 : :
393 : : /*
394 : : * Do tests for EFD creation with bad parameters.
395 : : */
396 : 0 : static int test_efd_creation_with_bad_parameters(void)
397 : : {
398 : : struct rte_efd_table *handle, *tmp;
399 : : printf("Entering %s, **Errors are expected **\n", __func__);
400 : :
401 : 0 : handle = rte_efd_create("creation_with_bad_parameters_0", TABLE_SIZE, 0,
402 : : efd_get_all_sockets_bitmask(), test_socket_id);
403 [ # # ]: 0 : if (handle != NULL) {
404 : 0 : rte_efd_free(handle);
405 : : printf("Impossible creating EFD table successfully "
406 : : "if key_len in parameter is zero\n");
407 : 0 : return -1;
408 : : }
409 : :
410 : 0 : handle = rte_efd_create("creation_with_bad_parameters_1", TABLE_SIZE,
411 : : sizeof(struct flow_key), 0, test_socket_id);
412 [ # # ]: 0 : if (handle != NULL) {
413 : 0 : rte_efd_free(handle);
414 : : printf("Impossible creating EFD table successfully "
415 : : "with invalid socket bitmask\n");
416 : 0 : return -1;
417 : : }
418 : :
419 : 0 : handle = rte_efd_create("creation_with_bad_parameters_2", TABLE_SIZE,
420 : : sizeof(struct flow_key), efd_get_all_sockets_bitmask(),
421 : : 255);
422 [ # # ]: 0 : if (handle != NULL) {
423 : 0 : rte_efd_free(handle);
424 : : printf("Impossible creating EFD table successfully "
425 : : "with invalid socket\n");
426 : 0 : return -1;
427 : : }
428 : :
429 : : /* test with same name should fail */
430 : 0 : handle = rte_efd_create("same_name", TABLE_SIZE,
431 : : sizeof(struct flow_key),
432 : : efd_get_all_sockets_bitmask(), 0);
433 [ # # ]: 0 : if (handle == NULL) {
434 : : printf("Cannot create first EFD table with 'same_name'\n");
435 : 0 : return -1;
436 : : }
437 : 0 : tmp = rte_efd_create("same_name", TABLE_SIZE, sizeof(struct flow_key),
438 : : efd_get_all_sockets_bitmask(), 0);
439 [ # # ]: 0 : if (tmp != NULL) {
440 : : printf("Creation of EFD table with same name should fail\n");
441 : 0 : rte_efd_free(handle);
442 : 0 : rte_efd_free(tmp);
443 : 0 : return -1;
444 : : }
445 : 0 : rte_efd_free(handle);
446 : :
447 : : printf("# Test successful. No more errors expected\n");
448 : :
449 : 0 : return 0;
450 : : }
451 : :
452 : : static int
453 : 0 : test_efd(void)
454 : : {
455 : 0 : test_socket_id = rte_socket_id();
456 : :
457 : : /* Unit tests */
458 [ # # ]: 0 : if (test_add_delete() < 0)
459 : : return -1;
460 [ # # ]: 0 : if (test_efd_find_existing() < 0)
461 : : return -1;
462 [ # # ]: 0 : if (test_add_update_delete() < 0)
463 : : return -1;
464 [ # # ]: 0 : if (test_five_keys() < 0)
465 : : return -1;
466 [ # # ]: 0 : if (test_efd_creation_with_bad_parameters() < 0)
467 : : return -1;
468 [ # # ]: 0 : if (test_average_table_utilization() < 0)
469 : 0 : return -1;
470 : :
471 : : return 0;
472 : : }
473 : :
474 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
475 : :
476 : 251 : REGISTER_PERF_TEST(efd_autotest, test_efd);
|