Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <string.h>
6 : : #include <errno.h>
7 : :
8 : : #include "test.h"
9 : :
10 : : #include <rte_string_fns.h>
11 : : #include <rte_mbuf.h>
12 : : #include <rte_byteorder.h>
13 : : #include <rte_ip.h>
14 : :
15 : : #ifdef RTE_EXEC_ENV_WINDOWS
16 : : static int
17 : : test_acl(void)
18 : : {
19 : : printf("ACL not supported on Windows, skipping test\n");
20 : : return TEST_SKIPPED;
21 : : }
22 : :
23 : : #else
24 : : #include <rte_acl.h>
25 : : #include <rte_common.h>
26 : :
27 : : #include "test_acl.h"
28 : :
29 : : #define BIT_SIZEOF(x) (sizeof(x) * CHAR_BIT)
30 : :
31 : : #define LEN RTE_ACL_MAX_CATEGORIES
32 : :
33 : : RTE_ACL_RULE_DEF(acl_ipv4vlan_rule, RTE_ACL_IPV4VLAN_NUM_FIELDS);
34 : :
35 : : struct rte_acl_param acl_param = {
36 : : .name = "acl_ctx",
37 : : .socket_id = SOCKET_ID_ANY,
38 : : .rule_size = RTE_ACL_IPV4VLAN_RULE_SZ,
39 : : .max_rule_num = 0x30000,
40 : : };
41 : :
42 : : struct rte_acl_ipv4vlan_rule acl_rule = {
43 : : .data = { .priority = 1, .category_mask = 0xff },
44 : : .src_port_low = 0,
45 : : .src_port_high = UINT16_MAX,
46 : : .dst_port_low = 0,
47 : : .dst_port_high = UINT16_MAX,
48 : : };
49 : :
50 : : const uint32_t ipv4_7tuple_layout[RTE_ACL_IPV4VLAN_NUM] = {
51 : : offsetof(struct ipv4_7tuple, proto),
52 : : offsetof(struct ipv4_7tuple, vlan),
53 : : offsetof(struct ipv4_7tuple, ip_src),
54 : : offsetof(struct ipv4_7tuple, ip_dst),
55 : : offsetof(struct ipv4_7tuple, port_src),
56 : : };
57 : :
58 : :
59 : : /* byteswap to cpu or network order */
60 : : static void
61 : 8314 : bswap_test_data(struct ipv4_7tuple *data, int len, int to_be)
62 : : {
63 : : int i;
64 : :
65 [ + + ]: 540144 : for (i = 0; i < len; i++) {
66 : :
67 [ + + ]: 531830 : if (to_be) {
68 : : /* swap all bytes so that they are in network order */
69 [ - + ]: 265915 : data[i].ip_dst = rte_cpu_to_be_32(data[i].ip_dst);
70 [ - + ]: 265915 : data[i].ip_src = rte_cpu_to_be_32(data[i].ip_src);
71 [ - + ]: 265915 : data[i].port_dst = rte_cpu_to_be_16(data[i].port_dst);
72 [ - + ]: 265915 : data[i].port_src = rte_cpu_to_be_16(data[i].port_src);
73 [ - + ]: 265915 : data[i].vlan = rte_cpu_to_be_16(data[i].vlan);
74 [ - + ]: 531830 : data[i].domain = rte_cpu_to_be_16(data[i].domain);
75 : : } else {
76 [ - + ]: 265915 : data[i].ip_dst = rte_be_to_cpu_32(data[i].ip_dst);
77 [ - + ]: 265915 : data[i].ip_src = rte_be_to_cpu_32(data[i].ip_src);
78 [ - + ]: 265915 : data[i].port_dst = rte_be_to_cpu_16(data[i].port_dst);
79 [ - + ]: 265915 : data[i].port_src = rte_be_to_cpu_16(data[i].port_src);
80 [ - + ]: 265915 : data[i].vlan = rte_be_to_cpu_16(data[i].vlan);
81 [ - + ]: 531830 : data[i].domain = rte_be_to_cpu_16(data[i].domain);
82 : : }
83 : : }
84 : 8314 : }
85 : :
86 : : static int
87 : : acl_ipv4vlan_check_rule(const struct rte_acl_ipv4vlan_rule *rule)
88 : : {
89 : 12503 : if (rule->src_port_low > rule->src_port_high ||
90 [ + + ]: 6251 : rule->dst_port_low > rule->dst_port_high ||
91 [ + + ]: 6250 : rule->src_mask_len > BIT_SIZEOF(rule->src_addr) ||
92 [ + + ]: 6249 : rule->dst_mask_len > BIT_SIZEOF(rule->dst_addr))
93 : : return -EINVAL;
94 : : return 0;
95 : : }
96 : :
97 : : static void
98 : : acl_ipv4vlan_convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
99 : : struct acl_ipv4vlan_rule *ro)
100 : : {
101 : 6249 : ro->data = ri->data;
102 : :
103 : 6249 : ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].value.u8 = ri->proto;
104 : 6249 : ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].value.u16 = ri->vlan;
105 : 6249 : ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].value.u16 = ri->domain;
106 : 6249 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = ri->src_addr;
107 : 6249 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = ri->dst_addr;
108 : 6249 : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].value.u16 = ri->src_port_low;
109 : 6249 : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].value.u16 = ri->dst_port_low;
110 : :
111 : 6249 : ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].mask_range.u8 = ri->proto_mask;
112 : 6249 : ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].mask_range.u16 = ri->vlan_mask;
113 : 6249 : ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].mask_range.u16 =
114 : 6249 : ri->domain_mask;
115 : 6249 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
116 : 6249 : ri->src_mask_len;
117 : 6249 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = ri->dst_mask_len;
118 : 6249 : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].mask_range.u16 =
119 : 6249 : ri->src_port_high;
120 : 6249 : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
121 : 6248 : ri->dst_port_high;
122 : : }
123 : :
124 : : /*
125 : : * Add ipv4vlan rules to an existing ACL context.
126 : : * This function is not multi-thread safe.
127 : : *
128 : : * @param ctx
129 : : * ACL context to add patterns to.
130 : : * @param rules
131 : : * Array of rules to add to the ACL context.
132 : : * Note that all fields in rte_acl_ipv4vlan_rule structures are expected
133 : : * to be in host byte order.
134 : : * @param num
135 : : * Number of elements in the input array of rules.
136 : : * @return
137 : : * - -ENOMEM if there is no space in the ACL context for these rules.
138 : : * - -EINVAL if the parameters are invalid.
139 : : * - Zero if operation completed successfully.
140 : : */
141 : : static int
142 : 1022 : rte_acl_ipv4vlan_add_rules(struct rte_acl_ctx *ctx,
143 : : const struct rte_acl_ipv4vlan_rule *rules,
144 : : uint32_t num)
145 : : {
146 : : int32_t rc;
147 : : uint32_t i;
148 : : struct acl_ipv4vlan_rule rv;
149 : :
150 [ + + ]: 1022 : if (ctx == NULL || rules == NULL)
151 : : return -EINVAL;
152 : :
153 : : /* check input rules. */
154 [ + + ]: 7268 : for (i = 0; i != num; i++) {
155 [ + + ]: 6252 : rc = acl_ipv4vlan_check_rule(rules + i);
156 : : if (rc != 0) {
157 : 4 : fprintf(stderr, "%s: rule #%u is invalid\n",
158 : : __func__, i + 1);
159 : 4 : return rc;
160 : : }
161 : : }
162 : :
163 : : /* perform conversion to the internal format and add to the context. */
164 [ + + ]: 7264 : for (i = 0, rc = 0; i != num && rc == 0; i++) {
165 : 6248 : acl_ipv4vlan_convert_rule(rules + i, &rv);
166 : 6248 : rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&rv, 1);
167 : : }
168 : :
169 : : return rc;
170 : : }
171 : :
172 : : static void
173 : 17 : acl_ipv4vlan_config(struct rte_acl_config *cfg,
174 : : const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
175 : : uint32_t num_categories)
176 : : {
177 : : static const struct rte_acl_field_def
178 : : ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
179 : : {
180 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
181 : : .size = sizeof(uint8_t),
182 : : .field_index = RTE_ACL_IPV4VLAN_PROTO_FIELD,
183 : : .input_index = RTE_ACL_IPV4VLAN_PROTO,
184 : : },
185 : : {
186 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
187 : : .size = sizeof(uint16_t),
188 : : .field_index = RTE_ACL_IPV4VLAN_VLAN1_FIELD,
189 : : .input_index = RTE_ACL_IPV4VLAN_VLAN,
190 : : },
191 : : {
192 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
193 : : .size = sizeof(uint16_t),
194 : : .field_index = RTE_ACL_IPV4VLAN_VLAN2_FIELD,
195 : : .input_index = RTE_ACL_IPV4VLAN_VLAN,
196 : : },
197 : : {
198 : : .type = RTE_ACL_FIELD_TYPE_MASK,
199 : : .size = sizeof(uint32_t),
200 : : .field_index = RTE_ACL_IPV4VLAN_SRC_FIELD,
201 : : .input_index = RTE_ACL_IPV4VLAN_SRC,
202 : : },
203 : : {
204 : : .type = RTE_ACL_FIELD_TYPE_MASK,
205 : : .size = sizeof(uint32_t),
206 : : .field_index = RTE_ACL_IPV4VLAN_DST_FIELD,
207 : : .input_index = RTE_ACL_IPV4VLAN_DST,
208 : : },
209 : : {
210 : : .type = RTE_ACL_FIELD_TYPE_RANGE,
211 : : .size = sizeof(uint16_t),
212 : : .field_index = RTE_ACL_IPV4VLAN_SRCP_FIELD,
213 : : .input_index = RTE_ACL_IPV4VLAN_PORTS,
214 : : },
215 : : {
216 : : .type = RTE_ACL_FIELD_TYPE_RANGE,
217 : : .size = sizeof(uint16_t),
218 : : .field_index = RTE_ACL_IPV4VLAN_DSTP_FIELD,
219 : : .input_index = RTE_ACL_IPV4VLAN_PORTS,
220 : : },
221 : : };
222 : :
223 : 17 : memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
224 : 17 : cfg->num_fields = RTE_DIM(ipv4_defs);
225 : :
226 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_PROTO_FIELD].offset =
227 : 17 : layout[RTE_ACL_IPV4VLAN_PROTO];
228 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].offset =
229 : 17 : layout[RTE_ACL_IPV4VLAN_VLAN];
230 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].offset =
231 : 17 : layout[RTE_ACL_IPV4VLAN_VLAN] +
232 : : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].size;
233 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].offset =
234 : 17 : layout[RTE_ACL_IPV4VLAN_SRC];
235 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset =
236 : 17 : layout[RTE_ACL_IPV4VLAN_DST];
237 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset =
238 : 17 : layout[RTE_ACL_IPV4VLAN_PORTS];
239 : 17 : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset =
240 : 17 : layout[RTE_ACL_IPV4VLAN_PORTS] +
241 : : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size;
242 : :
243 : 17 : cfg->num_categories = num_categories;
244 : 17 : }
245 : :
246 : : /*
247 : : * Analyze set of ipv4vlan rules and build required internal
248 : : * run-time structures.
249 : : * This function is not multi-thread safe.
250 : : *
251 : : * @param ctx
252 : : * ACL context to build.
253 : : * @param layout
254 : : * Layout of input data to search through.
255 : : * @param num_categories
256 : : * Maximum number of categories to use in that build.
257 : : * @return
258 : : * - -ENOMEM if couldn't allocate enough memory.
259 : : * - -EINVAL if the parameters are invalid.
260 : : * - Negative error code if operation failed.
261 : : * - Zero if operation completed successfully.
262 : : */
263 : : static int
264 : 19 : rte_acl_ipv4vlan_build(struct rte_acl_ctx *ctx,
265 : : const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
266 : : uint32_t num_categories)
267 : : {
268 : : struct rte_acl_config cfg;
269 : :
270 [ + + ]: 19 : if (ctx == NULL || layout == NULL)
271 : : return -EINVAL;
272 : :
273 : : memset(&cfg, 0, sizeof(cfg));
274 : 17 : acl_ipv4vlan_config(&cfg, layout, num_categories);
275 : 17 : return rte_acl_build(ctx, &cfg);
276 : : }
277 : :
278 : : /*
279 : : * Test ACL lookup (selected alg).
280 : : */
281 : : static int
282 : 33240 : test_classify_alg(struct rte_acl_ctx *acx, struct ipv4_7tuple test_data[],
283 : : const uint8_t *data[], size_t dim, enum rte_acl_classify_alg alg)
284 : 33240 : {
285 : : int32_t ret;
286 : : uint32_t i, result, count;
287 : 33240 : uint32_t results[dim * RTE_ACL_MAX_CATEGORIES];
288 : :
289 : : /* set given classify alg, skip test if alg is not supported */
290 : 33240 : ret = rte_acl_set_ctx_classify(acx, alg);
291 [ + + ]: 33240 : if (ret != 0)
292 [ + - ]: 16620 : return (ret == -ENOTSUP) ? 0 : ret;
293 : :
294 : : /**
295 : : * these will run quite a few times, it's necessary to test code paths
296 : : * from num=0 to num>8
297 : : */
298 [ + + ]: 1096876 : for (count = 0; count <= dim; count++) {
299 : 1080256 : ret = rte_acl_classify(acx, data, results,
300 : : count, RTE_ACL_MAX_CATEGORIES);
301 [ - + ]: 1080256 : if (ret != 0) {
302 : : printf("Line %i: classify(alg=%d) failed!\n",
303 : : __LINE__, alg);
304 : 0 : return ret;
305 : : }
306 : :
307 : : /* check if we allow everything we should allow */
308 [ + + ]: 35655164 : for (i = 0; i < count; i++) {
309 : 34574908 : result =
310 : 34574908 : results[i * RTE_ACL_MAX_CATEGORIES + ACL_ALLOW];
311 [ - + ]: 34574908 : if (result != test_data[i].allow) {
312 : : printf("Line %i: Error in allow results at %i "
313 : : "(expected %"PRIu32" got %"PRIu32")!\n",
314 : : __LINE__, i, test_data[i].allow,
315 : : result);
316 : 0 : return -EINVAL;
317 : : }
318 : : }
319 : :
320 : : /* check if we deny everything we should deny */
321 [ + + ]: 35655164 : for (i = 0; i < count; i++) {
322 : 34574908 : result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_DENY];
323 [ - + ]: 34574908 : if (result != test_data[i].deny) {
324 : : printf("Line %i: Error in deny results at %i "
325 : : "(expected %"PRIu32" got %"PRIu32")!\n",
326 : : __LINE__, i, test_data[i].deny,
327 : : result);
328 : 0 : return -EINVAL;
329 : : }
330 : : }
331 : : }
332 : :
333 : : /* restore default classify alg */
334 : 16620 : return rte_acl_set_ctx_classify(acx, RTE_ACL_CLASSIFY_DEFAULT);
335 : : }
336 : :
337 : : /*
338 : : * Test ACL lookup (all possible methods).
339 : : */
340 : : static int
341 : 4155 : test_classify_run(struct rte_acl_ctx *acx, struct ipv4_7tuple test_data[],
342 : : size_t dim)
343 : 4155 : {
344 : : int32_t ret;
345 : : uint32_t i;
346 : 4155 : const uint8_t *data[dim];
347 : :
348 : : static const enum rte_acl_classify_alg alg[] = {
349 : : RTE_ACL_CLASSIFY_SCALAR,
350 : : RTE_ACL_CLASSIFY_SSE,
351 : : RTE_ACL_CLASSIFY_AVX2,
352 : : RTE_ACL_CLASSIFY_NEON,
353 : : RTE_ACL_CLASSIFY_ALTIVEC,
354 : : RTE_ACL_CLASSIFY_AVX512X16,
355 : : RTE_ACL_CLASSIFY_AVX512X32,
356 : : RTE_ACL_CLASSIFY_RVV,
357 : : };
358 : :
359 : : /* swap all bytes in the data to network order */
360 : 4155 : bswap_test_data(test_data, dim, 1);
361 : :
362 : : /* store pointers to test data */
363 [ + + ]: 270064 : for (i = 0; i < dim; i++)
364 : 265909 : data[i] = (uint8_t *)&test_data[i];
365 : :
366 : : ret = 0;
367 [ + + ]: 37395 : for (i = 0; i != RTE_DIM(alg); i++) {
368 : 33240 : ret = test_classify_alg(acx, test_data, data, dim, alg[i]);
369 [ - + ]: 33240 : if (ret < 0) {
370 : 0 : printf("Line %i: %s() for alg=%d failed, errno=%d\n",
371 : : __LINE__, __func__, alg[i], -ret);
372 : : break;
373 : : }
374 : : }
375 : :
376 : : /* swap data back to cpu order so that next time tests don't fail */
377 : 4155 : bswap_test_data(test_data, dim, 0);
378 : 4155 : return ret;
379 : : }
380 : :
381 : : static int
382 : 13 : test_classify_buid(struct rte_acl_ctx *acx,
383 : : const struct rte_acl_ipv4vlan_rule *rules, uint32_t num)
384 : : {
385 : : int ret;
386 : :
387 : : /* add rules to the context */
388 : 13 : ret = rte_acl_ipv4vlan_add_rules(acx, rules, num);
389 [ - + ]: 13 : if (ret != 0) {
390 : : printf("Line %i: Adding rules to ACL context failed!\n",
391 : : __LINE__);
392 : 0 : return ret;
393 : : }
394 : :
395 : : /* try building the context */
396 : 13 : ret = rte_acl_ipv4vlan_build(acx, ipv4_7tuple_layout,
397 : : RTE_ACL_MAX_CATEGORIES);
398 [ - + ]: 13 : if (ret != 0) {
399 : : printf("Line %i: Building ACL context failed!\n", __LINE__);
400 : 0 : return ret;
401 : : }
402 : :
403 : : return 0;
404 : : }
405 : :
406 : : #define TEST_CLASSIFY_ITER 4
407 : :
408 : : /*
409 : : * Test scalar and SSE ACL lookup.
410 : : */
411 : : static int
412 : 1 : test_classify(void)
413 : : {
414 : : struct rte_acl_ctx *acx;
415 : : int i, ret;
416 : :
417 : 1 : acx = rte_acl_create(&acl_param);
418 [ - + ]: 1 : if (acx == NULL) {
419 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
420 : 0 : return -1;
421 : : }
422 : :
423 : : ret = 0;
424 [ + + ]: 5 : for (i = 0; i != TEST_CLASSIFY_ITER; i++) {
425 : :
426 [ + + ]: 4 : if ((i & 1) == 0)
427 : 2 : rte_acl_reset(acx);
428 : : else
429 : 2 : rte_acl_reset_rules(acx);
430 : :
431 : 4 : ret = test_classify_buid(acx, acl_test_rules,
432 : : RTE_DIM(acl_test_rules));
433 [ - + ]: 4 : if (ret != 0) {
434 : : printf("Line %i, iter: %d: "
435 : : "Adding rules to ACL context failed!\n",
436 : : __LINE__, i);
437 : : break;
438 : : }
439 : :
440 : 4 : ret = test_classify_run(acx, acl_test_data,
441 : : RTE_DIM(acl_test_data));
442 [ - + ]: 4 : if (ret != 0) {
443 : : printf("Line %i, iter: %d: %s failed!\n",
444 : : __LINE__, i, __func__);
445 : : break;
446 : : }
447 : :
448 : : /* reset rules and make sure that classify still works ok. */
449 : 4 : rte_acl_reset_rules(acx);
450 : 4 : ret = test_classify_run(acx, acl_test_data,
451 : : RTE_DIM(acl_test_data));
452 [ - + ]: 4 : if (ret != 0) {
453 : : printf("Line %i, iter: %d: %s failed!\n",
454 : : __LINE__, i, __func__);
455 : : break;
456 : : }
457 : : }
458 : :
459 : 1 : rte_acl_free(acx);
460 : 1 : return ret;
461 : : }
462 : :
463 : : static int
464 : 1 : test_build_ports_range(void)
465 : : {
466 : : static const struct rte_acl_ipv4vlan_rule test_rules[] = {
467 : : {
468 : : /* match all packets. */
469 : : .data = {
470 : : .userdata = 1,
471 : : .category_mask = ACL_ALLOW_MASK,
472 : : .priority = 101,
473 : : },
474 : : .src_port_low = 0,
475 : : .src_port_high = UINT16_MAX,
476 : : .dst_port_low = 0,
477 : : .dst_port_high = UINT16_MAX,
478 : : },
479 : : {
480 : : /* match all packets with dst ports [54-65280]. */
481 : : .data = {
482 : : .userdata = 2,
483 : : .category_mask = ACL_ALLOW_MASK,
484 : : .priority = 102,
485 : : },
486 : : .src_port_low = 0,
487 : : .src_port_high = UINT16_MAX,
488 : : .dst_port_low = 54,
489 : : .dst_port_high = 65280,
490 : : },
491 : : {
492 : : /* match all packets with dst ports [0-52]. */
493 : : .data = {
494 : : .userdata = 3,
495 : : .category_mask = ACL_ALLOW_MASK,
496 : : .priority = 103,
497 : : },
498 : : .src_port_low = 0,
499 : : .src_port_high = UINT16_MAX,
500 : : .dst_port_low = 0,
501 : : .dst_port_high = 52,
502 : : },
503 : : {
504 : : /* match all packets with dst ports [53]. */
505 : : .data = {
506 : : .userdata = 4,
507 : : .category_mask = ACL_ALLOW_MASK,
508 : : .priority = 99,
509 : : },
510 : : .src_port_low = 0,
511 : : .src_port_high = UINT16_MAX,
512 : : .dst_port_low = 53,
513 : : .dst_port_high = 53,
514 : : },
515 : : {
516 : : /* match all packets with dst ports [65279-65535]. */
517 : : .data = {
518 : : .userdata = 5,
519 : : .category_mask = ACL_ALLOW_MASK,
520 : : .priority = 98,
521 : : },
522 : : .src_port_low = 0,
523 : : .src_port_high = UINT16_MAX,
524 : : .dst_port_low = 65279,
525 : : .dst_port_high = UINT16_MAX,
526 : : },
527 : : };
528 : :
529 : : static struct ipv4_7tuple test_data[] = {
530 : : {
531 : : .proto = 6,
532 : : .ip_src = RTE_IPV4(10, 1, 1, 1),
533 : : .ip_dst = RTE_IPV4(192, 168, 0, 33),
534 : : .port_dst = 53,
535 : : .allow = 1,
536 : : },
537 : : {
538 : : .proto = 6,
539 : : .ip_src = RTE_IPV4(127, 84, 33, 1),
540 : : .ip_dst = RTE_IPV4(1, 2, 3, 4),
541 : : .port_dst = 65281,
542 : : .allow = 1,
543 : : },
544 : : };
545 : :
546 : : struct rte_acl_ctx *acx;
547 : : int32_t ret, i, j;
548 : : uint32_t results[RTE_DIM(test_data)];
549 : : const uint8_t *data[RTE_DIM(test_data)];
550 : :
551 : 1 : acx = rte_acl_create(&acl_param);
552 [ - + ]: 1 : if (acx == NULL) {
553 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
554 : 0 : return -1;
555 : : }
556 : :
557 : : /* swap all bytes in the data to network order */
558 : 1 : bswap_test_data(test_data, RTE_DIM(test_data), 1);
559 : :
560 : : /* store pointers to test data */
561 [ + + ]: 3 : for (i = 0; i != RTE_DIM(test_data); i++)
562 : 2 : data[i] = (uint8_t *)&test_data[i];
563 : :
564 [ + + ]: 6 : for (i = 0; i != RTE_DIM(test_rules); i++) {
565 : 5 : rte_acl_reset(acx);
566 : 5 : ret = test_classify_buid(acx, test_rules, i + 1);
567 [ - + ]: 5 : if (ret != 0) {
568 : : printf("Line %i, iter: %d: "
569 : : "Adding rules to ACL context failed!\n",
570 : : __LINE__, i);
571 : : break;
572 : : }
573 : 5 : ret = rte_acl_classify(acx, data, results,
574 : : RTE_DIM(data), 1);
575 [ - + ]: 5 : if (ret != 0) {
576 : : printf("Line %i, iter: %d: classify failed!\n",
577 : : __LINE__, i);
578 : : break;
579 : : }
580 : :
581 : : /* check results */
582 [ + + ]: 15 : for (j = 0; j != RTE_DIM(results); j++) {
583 [ - + ]: 10 : if (results[j] != test_data[j].allow) {
584 : : printf("Line %i: Error in allow results at %i "
585 : : "(expected %"PRIu32" got %"PRIu32")!\n",
586 : : __LINE__, j, test_data[j].allow,
587 : : results[j]);
588 : : ret = -EINVAL;
589 : : }
590 : : }
591 : : }
592 : :
593 : 1 : bswap_test_data(test_data, RTE_DIM(test_data), 0);
594 : :
595 : 1 : rte_acl_free(acx);
596 : 1 : return ret;
597 : : }
598 : :
599 : : static void
600 : 27 : convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
601 : : struct acl_ipv4vlan_rule *ro)
602 : : {
603 : 139 : ro->data = ri->data;
604 : :
605 : 139 : ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].value.u8 = ri->proto;
606 : 139 : ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].value.u16 = ri->vlan;
607 : 139 : ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].value.u16 = ri->domain;
608 : 139 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = ri->src_addr;
609 : 139 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = ri->dst_addr;
610 : 139 : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].value.u16 = ri->src_port_low;
611 : 139 : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].value.u16 = ri->dst_port_low;
612 : :
613 : 139 : ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].mask_range.u8 = ri->proto_mask;
614 : 139 : ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].mask_range.u16 = ri->vlan_mask;
615 : 139 : ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].mask_range.u16 =
616 : 139 : ri->domain_mask;
617 : 139 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
618 : 139 : ri->src_mask_len;
619 : 139 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = ri->dst_mask_len;
620 : 139 : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].mask_range.u16 =
621 : 139 : ri->src_port_high;
622 : 139 : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
623 : 139 : ri->dst_port_high;
624 : 27 : }
625 : :
626 : : /*
627 : : * Convert IPV4 source and destination from RTE_ACL_FIELD_TYPE_MASK to
628 : : * RTE_ACL_FIELD_TYPE_BITMASK.
629 : : */
630 : : static void
631 : 27 : convert_rule_1(const struct rte_acl_ipv4vlan_rule *ri,
632 : : struct acl_ipv4vlan_rule *ro)
633 : : {
634 : : uint32_t v;
635 : :
636 : : convert_rule(ri, ro);
637 : : v = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32;
638 : 27 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
639 [ + + ]: 27 : RTE_ACL_MASKLEN_TO_BITMASK(v, sizeof(v));
640 : : v = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32;
641 : 27 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 =
642 [ + + ]: 27 : RTE_ACL_MASKLEN_TO_BITMASK(v, sizeof(v));
643 : 27 : }
644 : :
645 : : /*
646 : : * Convert IPV4 source and destination from RTE_ACL_FIELD_TYPE_MASK to
647 : : * RTE_ACL_FIELD_TYPE_RANGE.
648 : : */
649 : : static void
650 : 27 : convert_rule_2(const struct rte_acl_ipv4vlan_rule *ri,
651 : : struct acl_ipv4vlan_rule *ro)
652 : : {
653 : : uint32_t hi, lo, mask;
654 : :
655 : : convert_rule(ri, ro);
656 : :
657 : : mask = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32;
658 [ + + ]: 27 : mask = RTE_ACL_MASKLEN_TO_BITMASK(mask, sizeof(mask));
659 : 27 : lo = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 & mask;
660 : 27 : hi = lo + ~mask;
661 : 27 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = lo;
662 : 27 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 = hi;
663 : :
664 : : mask = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32;
665 [ + + ]: 27 : mask = RTE_ACL_MASKLEN_TO_BITMASK(mask, sizeof(mask));
666 : 27 : lo = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 & mask;
667 : 27 : hi = lo + ~mask;
668 : 27 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = lo;
669 : 27 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = hi;
670 : 27 : }
671 : :
672 : : /*
673 : : * Convert rte_acl_ipv4vlan_rule: swap VLAN and PORTS rule fields.
674 : : */
675 : : static void
676 : 27 : convert_rule_3(const struct rte_acl_ipv4vlan_rule *ri,
677 : : struct acl_ipv4vlan_rule *ro)
678 : : {
679 : : struct rte_acl_field t1, t2;
680 : :
681 : : convert_rule(ri, ro);
682 : :
683 : 27 : t1 = ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD];
684 : 27 : t2 = ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD];
685 : :
686 : 27 : ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD] =
687 : : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD];
688 : 27 : ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD] =
689 : : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD];
690 : :
691 : 27 : ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD] = t1;
692 : 27 : ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD] = t2;
693 : 27 : }
694 : :
695 : : /*
696 : : * Convert rte_acl_ipv4vlan_rule: swap SRC and DST IPv4 address rules.
697 : : */
698 : : static void
699 : 27 : convert_rule_4(const struct rte_acl_ipv4vlan_rule *ri,
700 : : struct acl_ipv4vlan_rule *ro)
701 : : {
702 : : struct rte_acl_field t;
703 : :
704 : : convert_rule(ri, ro);
705 : :
706 : 27 : t = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD];
707 : 27 : ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD] =
708 : : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD];
709 : :
710 : 27 : ro->field[RTE_ACL_IPV4VLAN_DST_FIELD] = t;
711 : 27 : }
712 : :
713 : : static void
714 : 11 : ipv4vlan_config(struct rte_acl_config *cfg,
715 : : const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
716 : : uint32_t num_categories)
717 : : {
718 : : static const struct rte_acl_field_def
719 : : ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
720 : : {
721 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
722 : : .size = sizeof(uint8_t),
723 : : .field_index = RTE_ACL_IPV4VLAN_PROTO_FIELD,
724 : : .input_index = RTE_ACL_IPV4VLAN_PROTO,
725 : : },
726 : : {
727 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
728 : : .size = sizeof(uint16_t),
729 : : .field_index = RTE_ACL_IPV4VLAN_VLAN1_FIELD,
730 : : .input_index = RTE_ACL_IPV4VLAN_VLAN,
731 : : },
732 : : {
733 : : .type = RTE_ACL_FIELD_TYPE_BITMASK,
734 : : .size = sizeof(uint16_t),
735 : : .field_index = RTE_ACL_IPV4VLAN_VLAN2_FIELD,
736 : : .input_index = RTE_ACL_IPV4VLAN_VLAN,
737 : : },
738 : : {
739 : : .type = RTE_ACL_FIELD_TYPE_MASK,
740 : : .size = sizeof(uint32_t),
741 : : .field_index = RTE_ACL_IPV4VLAN_SRC_FIELD,
742 : : .input_index = RTE_ACL_IPV4VLAN_SRC,
743 : : },
744 : : {
745 : : .type = RTE_ACL_FIELD_TYPE_MASK,
746 : : .size = sizeof(uint32_t),
747 : : .field_index = RTE_ACL_IPV4VLAN_DST_FIELD,
748 : : .input_index = RTE_ACL_IPV4VLAN_DST,
749 : : },
750 : : {
751 : : .type = RTE_ACL_FIELD_TYPE_RANGE,
752 : : .size = sizeof(uint16_t),
753 : : .field_index = RTE_ACL_IPV4VLAN_SRCP_FIELD,
754 : : .input_index = RTE_ACL_IPV4VLAN_PORTS,
755 : : },
756 : : {
757 : : .type = RTE_ACL_FIELD_TYPE_RANGE,
758 : : .size = sizeof(uint16_t),
759 : : .field_index = RTE_ACL_IPV4VLAN_DSTP_FIELD,
760 : : .input_index = RTE_ACL_IPV4VLAN_PORTS,
761 : : },
762 : : };
763 : :
764 : 11 : memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
765 : 11 : cfg->num_fields = RTE_DIM(ipv4_defs);
766 : :
767 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_PROTO_FIELD].offset =
768 : 11 : layout[RTE_ACL_IPV4VLAN_PROTO];
769 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].offset =
770 : 11 : layout[RTE_ACL_IPV4VLAN_VLAN];
771 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].offset =
772 : 11 : layout[RTE_ACL_IPV4VLAN_VLAN] +
773 : : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].size;
774 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].offset =
775 : 11 : layout[RTE_ACL_IPV4VLAN_SRC];
776 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset =
777 : 11 : layout[RTE_ACL_IPV4VLAN_DST];
778 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset =
779 : 11 : layout[RTE_ACL_IPV4VLAN_PORTS];
780 : 11 : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset =
781 : 11 : layout[RTE_ACL_IPV4VLAN_PORTS] +
782 : : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size;
783 : :
784 : 11 : cfg->num_categories = num_categories;
785 : 11 : }
786 : :
787 : : static int
788 : 5 : convert_rules(struct rte_acl_ctx *acx,
789 : : void (*convert)(const struct rte_acl_ipv4vlan_rule *,
790 : : struct acl_ipv4vlan_rule *),
791 : : const struct rte_acl_ipv4vlan_rule *rules, uint32_t num)
792 : : {
793 : : int32_t rc;
794 : : uint32_t i;
795 : : struct acl_ipv4vlan_rule r;
796 : :
797 [ + + ]: 140 : for (i = 0; i != num; i++) {
798 : 135 : convert(rules + i, &r);
799 : 135 : rc = rte_acl_add_rules(acx, (struct rte_acl_rule *)&r, 1);
800 [ - + ]: 135 : if (rc != 0) {
801 : : printf("Line %i: Adding rule %u to ACL context "
802 : : "failed with error code: %d\n",
803 : : __LINE__, i, rc);
804 : 0 : return rc;
805 : : }
806 : : }
807 : :
808 : : return 0;
809 : : }
810 : :
811 : : static void
812 : 2 : convert_config(struct rte_acl_config *cfg)
813 : : {
814 : 2 : ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
815 : 2 : }
816 : :
817 : : /*
818 : : * Convert rte_acl_ipv4vlan_rule to use RTE_ACL_FIELD_TYPE_BITMASK.
819 : : */
820 : : static void
821 : 2 : convert_config_1(struct rte_acl_config *cfg)
822 : : {
823 : 2 : ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
824 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].type = RTE_ACL_FIELD_TYPE_BITMASK;
825 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = RTE_ACL_FIELD_TYPE_BITMASK;
826 : 2 : }
827 : :
828 : : /*
829 : : * Convert rte_acl_ipv4vlan_rule to use RTE_ACL_FIELD_TYPE_RANGE.
830 : : */
831 : : static void
832 : 3 : convert_config_2(struct rte_acl_config *cfg)
833 : : {
834 : 3 : ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
835 : 3 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].type = RTE_ACL_FIELD_TYPE_RANGE;
836 : 3 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = RTE_ACL_FIELD_TYPE_RANGE;
837 : 3 : }
838 : :
839 : : /*
840 : : * Convert rte_acl_ipv4vlan_rule: swap VLAN and PORTS rule definitions.
841 : : */
842 : : static void
843 : 2 : convert_config_3(struct rte_acl_config *cfg)
844 : : {
845 : : struct rte_acl_field_def t1, t2;
846 : :
847 : 2 : ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
848 : :
849 : 2 : t1 = cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD];
850 : 2 : t2 = cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD];
851 : :
852 : : /* swap VLAN1 and SRCP rule definition. */
853 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD] =
854 : : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD];
855 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].field_index = t1.field_index;
856 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].input_index = t1.input_index;
857 : :
858 : : /* swap VLAN2 and DSTP rule definition. */
859 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD] =
860 : : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD];
861 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].field_index = t2.field_index;
862 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].input_index = t2.input_index;
863 : :
864 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].type = t1.type;
865 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size = t1.size;
866 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset = t1.offset;
867 : :
868 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].type = t2.type;
869 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].size = t2.size;
870 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset = t2.offset;
871 : 2 : }
872 : :
873 : : /*
874 : : * Convert rte_acl_ipv4vlan_rule: swap SRC and DST ip address rule definitions.
875 : : */
876 : : static void
877 : 2 : convert_config_4(struct rte_acl_config *cfg)
878 : : {
879 : : struct rte_acl_field_def t;
880 : :
881 : 2 : ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
882 : :
883 : 2 : t = cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD];
884 : :
885 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD] =
886 : : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD];
887 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].field_index = t.field_index;
888 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].input_index = t.input_index;
889 : :
890 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = t.type;
891 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].size = t.size;
892 : 2 : cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset = t.offset;
893 : 2 : }
894 : :
895 : :
896 : : static int
897 : 11 : build_convert_rules(struct rte_acl_ctx *acx,
898 : : void (*config)(struct rte_acl_config *),
899 : : size_t max_size)
900 : : {
901 : : struct rte_acl_config cfg;
902 : :
903 : : memset(&cfg, 0, sizeof(cfg));
904 : 11 : config(&cfg);
905 : 11 : cfg.max_size = max_size;
906 : 11 : return rte_acl_build(acx, &cfg);
907 : : }
908 : :
909 : : static int
910 : 5 : test_convert_rules(const char *desc,
911 : : void (*config)(struct rte_acl_config *),
912 : : void (*convert)(const struct rte_acl_ipv4vlan_rule *,
913 : : struct acl_ipv4vlan_rule *))
914 : : {
915 : : struct rte_acl_ctx *acx;
916 : : int32_t rc;
917 : : uint32_t i;
918 : : static const size_t mem_sizes[] = {0, -1};
919 : :
920 : : printf("running %s(%s)\n", __func__, desc);
921 : :
922 : 5 : acx = rte_acl_create(&acl_param);
923 [ - + ]: 5 : if (acx == NULL) {
924 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
925 : 0 : return -1;
926 : : }
927 : :
928 : 5 : rc = convert_rules(acx, convert, acl_test_rules,
929 : : RTE_DIM(acl_test_rules));
930 [ - + ]: 5 : if (rc != 0)
931 : : printf("Line %i: Error converting ACL rules!\n", __LINE__);
932 : :
933 [ + + ]: 15 : for (i = 0; rc == 0 && i != RTE_DIM(mem_sizes); i++) {
934 : :
935 : 10 : rc = build_convert_rules(acx, config, mem_sizes[i]);
936 [ - + ]: 10 : if (rc != 0) {
937 : : printf("Line %i: Error @ build_convert_rules(%zu)!\n",
938 : : __LINE__, mem_sizes[i]);
939 : : break;
940 : : }
941 : :
942 : 10 : rc = test_classify_run(acx, acl_test_data,
943 : : RTE_DIM(acl_test_data));
944 [ - + ]: 10 : if (rc != 0)
945 : : printf("%s failed at line %i, max_size=%zu\n",
946 : : __func__, __LINE__, mem_sizes[i]);
947 : : }
948 : :
949 : 5 : rte_acl_free(acx);
950 : 5 : return rc;
951 : : }
952 : :
953 : : static int
954 : 1 : test_convert(void)
955 : : {
956 : : static const struct {
957 : : const char *desc;
958 : : void (*config)(struct rte_acl_config *);
959 : : void (*convert)(const struct rte_acl_ipv4vlan_rule *,
960 : : struct acl_ipv4vlan_rule *);
961 : : } convert_param[] = {
962 : : {
963 : : "acl_ipv4vlan_tuple",
964 : : convert_config,
965 : : convert_rule,
966 : : },
967 : : {
968 : : "acl_ipv4vlan_tuple, RTE_ACL_FIELD_TYPE_BITMASK type "
969 : : "for IPv4",
970 : : convert_config_1,
971 : : convert_rule_1,
972 : : },
973 : : {
974 : : "acl_ipv4vlan_tuple, RTE_ACL_FIELD_TYPE_RANGE type "
975 : : "for IPv4",
976 : : convert_config_2,
977 : : convert_rule_2,
978 : : },
979 : : {
980 : : "acl_ipv4vlan_tuple: swap VLAN and PORTs order",
981 : : convert_config_3,
982 : : convert_rule_3,
983 : : },
984 : : {
985 : : "acl_ipv4vlan_tuple: swap SRC and DST IPv4 order",
986 : : convert_config_4,
987 : : convert_rule_4,
988 : : },
989 : : };
990 : :
991 : : uint32_t i;
992 : : int32_t rc;
993 : :
994 [ + + ]: 6 : for (i = 0; i != RTE_DIM(convert_param); i++) {
995 : 5 : rc = test_convert_rules(convert_param[i].desc,
996 : 5 : convert_param[i].config,
997 : 5 : convert_param[i].convert);
998 [ - + ]: 5 : if (rc != 0) {
999 : : printf("%s for test-case: %s failed, error code: %d;\n",
1000 : : __func__, convert_param[i].desc, rc);
1001 : 0 : return rc;
1002 : : }
1003 : : }
1004 : :
1005 : : return 0;
1006 : : }
1007 : :
1008 : : /*
1009 : : * Test wrong layout behavior
1010 : : * This test supplies the ACL context with invalid layout, which results in
1011 : : * ACL matching the wrong stuff. However, it should match the wrong stuff
1012 : : * the right way. We switch around source and destination addresses,
1013 : : * source and destination ports, and protocol will point to first byte of
1014 : : * destination port.
1015 : : */
1016 : : static int
1017 : 1 : test_invalid_layout(void)
1018 : : {
1019 : : struct rte_acl_ctx *acx;
1020 : : int ret, i;
1021 : :
1022 : : uint32_t results[RTE_DIM(invalid_layout_data)];
1023 : : const uint8_t *data[RTE_DIM(invalid_layout_data)];
1024 : :
1025 : 1 : const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
1026 : : /* proto points to destination port's first byte */
1027 : : offsetof(struct ipv4_7tuple, port_dst),
1028 : :
1029 : : 0, /* VLAN not used */
1030 : :
1031 : : /* src and dst addresses are swapped */
1032 : : offsetof(struct ipv4_7tuple, ip_dst),
1033 : : offsetof(struct ipv4_7tuple, ip_src),
1034 : :
1035 : : /*
1036 : : * we can't swap ports here, so we will swap
1037 : : * them in the data
1038 : : */
1039 : : offsetof(struct ipv4_7tuple, port_src),
1040 : : };
1041 : :
1042 : 1 : acx = rte_acl_create(&acl_param);
1043 [ - + ]: 1 : if (acx == NULL) {
1044 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
1045 : 0 : return -1;
1046 : : }
1047 : :
1048 : : /* putting a lot of rules into the context results in greater
1049 : : * coverage numbers. it doesn't matter if they are identical */
1050 [ + + ]: 1001 : for (i = 0; i < 1000; i++) {
1051 : : /* add rules to the context */
1052 : 1000 : ret = rte_acl_ipv4vlan_add_rules(acx, invalid_layout_rules,
1053 : : RTE_DIM(invalid_layout_rules));
1054 [ - + ]: 1000 : if (ret != 0) {
1055 : : printf("Line %i: Adding rules to ACL context failed!\n",
1056 : : __LINE__);
1057 : 0 : rte_acl_free(acx);
1058 : 0 : return -1;
1059 : : }
1060 : : }
1061 : :
1062 : : /* try building the context */
1063 : 1 : ret = rte_acl_ipv4vlan_build(acx, layout, 1);
1064 [ - + ]: 1 : if (ret != 0) {
1065 : : printf("Line %i: Building ACL context failed!\n", __LINE__);
1066 : 0 : rte_acl_free(acx);
1067 : 0 : return -1;
1068 : : }
1069 : :
1070 : : /* swap all bytes in the data to network order */
1071 : 1 : bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 1);
1072 : :
1073 : : /* prepare data */
1074 [ + + ]: 5 : for (i = 0; i < (int) RTE_DIM(invalid_layout_data); i++) {
1075 : 4 : data[i] = (uint8_t *)&invalid_layout_data[i];
1076 : : }
1077 : :
1078 : : /* classify tuples */
1079 : 1 : ret = rte_acl_classify_alg(acx, data, results,
1080 : : RTE_DIM(results), 1, RTE_ACL_CLASSIFY_SCALAR);
1081 [ - + ]: 1 : if (ret != 0) {
1082 : : printf("Line %i: SSE classify failed!\n", __LINE__);
1083 : 0 : rte_acl_free(acx);
1084 : 0 : return -1;
1085 : : }
1086 : :
1087 [ + + ]: 5 : for (i = 0; i < (int) RTE_DIM(results); i++) {
1088 [ - + ]: 4 : if (results[i] != invalid_layout_data[i].allow) {
1089 : : printf("Line %i: Wrong results at %i "
1090 : : "(result=%u, should be %u)!\n",
1091 : : __LINE__, i, results[i],
1092 : : invalid_layout_data[i].allow);
1093 : 0 : goto err;
1094 : : }
1095 : : }
1096 : :
1097 : : /* classify tuples (scalar) */
1098 : 1 : ret = rte_acl_classify_alg(acx, data, results, RTE_DIM(results), 1,
1099 : : RTE_ACL_CLASSIFY_SCALAR);
1100 : :
1101 [ - + ]: 1 : if (ret != 0) {
1102 : : printf("Line %i: Scalar classify failed!\n", __LINE__);
1103 : 0 : rte_acl_free(acx);
1104 : 0 : return -1;
1105 : : }
1106 : :
1107 [ + + ]: 5 : for (i = 0; i < (int) RTE_DIM(results); i++) {
1108 [ - + ]: 4 : if (results[i] != invalid_layout_data[i].allow) {
1109 : : printf("Line %i: Wrong results at %i "
1110 : : "(result=%u, should be %u)!\n",
1111 : : __LINE__, i, results[i],
1112 : : invalid_layout_data[i].allow);
1113 : 0 : goto err;
1114 : : }
1115 : : }
1116 : :
1117 : 1 : rte_acl_free(acx);
1118 : :
1119 : : /* swap data back to cpu order so that next time tests don't fail */
1120 : 1 : bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
1121 : :
1122 : 1 : return 0;
1123 : 0 : err:
1124 : :
1125 : : /* swap data back to cpu order so that next time tests don't fail */
1126 : 0 : bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
1127 : :
1128 : 0 : rte_acl_free(acx);
1129 : :
1130 : 0 : return -1;
1131 : : }
1132 : :
1133 : : /*
1134 : : * Test creating and finding ACL contexts, and adding rules
1135 : : */
1136 : : static int
1137 : 1 : test_create_find_add(void)
1138 : : {
1139 : : struct rte_acl_param param;
1140 : : struct rte_acl_ctx *acx, *acx2, *tmp;
1141 : : struct rte_acl_ipv4vlan_rule rules[LEN];
1142 : :
1143 : 1 : const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
1144 : :
1145 : : const char *acx_name = "acx";
1146 : : const char *acx2_name = "acx2";
1147 : : int i, ret;
1148 : :
1149 : : /* create two contexts */
1150 : : memcpy(¶m, &acl_param, sizeof(param));
1151 : 1 : param.max_rule_num = 2;
1152 : :
1153 : 1 : param.name = acx_name;
1154 : 1 : acx = rte_acl_create(¶m);
1155 [ - + ]: 1 : if (acx == NULL) {
1156 : : printf("Line %i: Error creating %s!\n", __LINE__, acx_name);
1157 : 0 : return -1;
1158 : : }
1159 : :
1160 : 1 : param.name = acx2_name;
1161 : 1 : acx2 = rte_acl_create(¶m);
1162 [ - + ]: 1 : if (acx2 == NULL || acx2 == acx) {
1163 : : printf("Line %i: Error creating %s!\n", __LINE__, acx2_name);
1164 : 0 : rte_acl_free(acx);
1165 : 0 : return -1;
1166 : : }
1167 : :
1168 : : /* try to create third one, with an existing name */
1169 : 1 : param.name = acx_name;
1170 : 1 : tmp = rte_acl_create(¶m);
1171 [ - + ]: 1 : if (tmp != acx) {
1172 : : printf("Line %i: Creating context with existing name "
1173 : : "test failed!\n",
1174 : : __LINE__);
1175 : 0 : rte_acl_free(tmp);
1176 : 0 : goto err;
1177 : : }
1178 : :
1179 : 1 : param.name = acx2_name;
1180 : 1 : tmp = rte_acl_create(¶m);
1181 [ - + ]: 1 : if (tmp != acx2) {
1182 : : printf("Line %i: Creating context with existing "
1183 : : "name test 2 failed!\n",
1184 : : __LINE__);
1185 : 0 : rte_acl_free(tmp);
1186 : 0 : goto err;
1187 : : }
1188 : :
1189 : : /* try to find existing ACL contexts */
1190 : 1 : tmp = rte_acl_find_existing(acx_name);
1191 [ - + ]: 1 : if (tmp != acx) {
1192 : : printf("Line %i: Finding %s failed!\n", __LINE__, acx_name);
1193 : 0 : rte_acl_free(tmp);
1194 : 0 : goto err;
1195 : : }
1196 : :
1197 : 1 : tmp = rte_acl_find_existing(acx2_name);
1198 [ - + ]: 1 : if (tmp != acx2) {
1199 : : printf("Line %i: Finding %s failed!\n", __LINE__, acx2_name);
1200 : 0 : rte_acl_free(tmp);
1201 : 0 : goto err;
1202 : : }
1203 : :
1204 : : /* try to find non-existing context */
1205 : 1 : tmp = rte_acl_find_existing("invalid");
1206 [ - + ]: 1 : if (tmp != NULL) {
1207 : : printf("Line %i: Non-existent ACL context found!\n", __LINE__);
1208 : 0 : goto err;
1209 : : }
1210 : :
1211 : : /* free context */
1212 : 1 : rte_acl_free(acx);
1213 : :
1214 : :
1215 : : /* create valid (but severely limited) acx */
1216 : : memcpy(¶m, &acl_param, sizeof(param));
1217 : 1 : param.max_rule_num = LEN;
1218 : :
1219 : 1 : acx = rte_acl_create(¶m);
1220 [ - + ]: 1 : if (acx == NULL) {
1221 : 0 : printf("Line %i: Error creating %s!\n", __LINE__, param.name);
1222 : 0 : goto err;
1223 : : }
1224 : :
1225 : : /* create dummy acl */
1226 [ + + ]: 17 : for (i = 0; i < LEN; i++) {
1227 : 16 : rules[i] = acl_rule;
1228 : : /* skip zero */
1229 : 16 : rules[i].data.userdata = i + 1;
1230 : : /* one rule per category */
1231 : 16 : rules[i].data.category_mask = 1 << i;
1232 : : }
1233 : :
1234 : : /* try filling up the context */
1235 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, rules, LEN);
1236 [ - + ]: 1 : if (ret != 0) {
1237 : : printf("Line %i: Adding %i rules to ACL context failed!\n",
1238 : : __LINE__, LEN);
1239 : 0 : goto err;
1240 : : }
1241 : :
1242 : : /* try adding to a (supposedly) full context */
1243 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, rules, 1);
1244 [ - + ]: 1 : if (ret == 0) {
1245 : : printf("Line %i: Adding rules to full ACL context should"
1246 : : "have failed!\n", __LINE__);
1247 : 0 : goto err;
1248 : : }
1249 : :
1250 : : /* try building the context */
1251 : 1 : ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
1252 [ - + ]: 1 : if (ret != 0) {
1253 : : printf("Line %i: Building ACL context failed!\n", __LINE__);
1254 : 0 : goto err;
1255 : : }
1256 : :
1257 : 1 : rte_acl_free(acx);
1258 : 1 : rte_acl_free(acx2);
1259 : :
1260 : 1 : return 0;
1261 : 0 : err:
1262 : 0 : rte_acl_free(acx);
1263 : 0 : rte_acl_free(acx2);
1264 : 0 : return -1;
1265 : : }
1266 : :
1267 : : /*
1268 : : * test various invalid rules
1269 : : */
1270 : : static int
1271 : 1 : test_invalid_rules(void)
1272 : : {
1273 : : struct rte_acl_ctx *acx;
1274 : : int ret;
1275 : :
1276 : : struct rte_acl_ipv4vlan_rule rule;
1277 : : struct acl_ipv4vlan_rule rv;
1278 : :
1279 : 1 : acx = rte_acl_create(&acl_param);
1280 [ - + ]: 1 : if (acx == NULL) {
1281 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
1282 : 0 : return -1;
1283 : : }
1284 : :
1285 : : /* test inverted high/low source and destination ports.
1286 : : * originally, there was a problem with memory consumption when using
1287 : : * such rules.
1288 : : */
1289 : : /* create dummy acl */
1290 : 1 : rule = acl_rule;
1291 : 1 : rule.data.userdata = 1;
1292 : 1 : rule.dst_port_low = 0xfff0;
1293 : 1 : rule.dst_port_high = 0x0010;
1294 : :
1295 : : /* add rules to context and try to build it */
1296 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1297 [ - + ]: 1 : if (ret == 0) {
1298 : : printf("Line %i: Adding rules to ACL context "
1299 : : "should have failed!\n", __LINE__);
1300 : 0 : goto err;
1301 : : }
1302 : :
1303 : 1 : rule.dst_port_low = 0x0;
1304 : 1 : rule.dst_port_high = 0xffff;
1305 : 1 : rule.src_port_low = 0xfff0;
1306 : 1 : rule.src_port_high = 0x0010;
1307 : :
1308 : : /* add rules to context and try to build it */
1309 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1310 [ - + ]: 1 : if (ret == 0) {
1311 : : printf("Line %i: Adding rules to ACL context "
1312 : : "should have failed!\n", __LINE__);
1313 : 0 : goto err;
1314 : : }
1315 : :
1316 : 1 : rule.dst_port_low = 0x0;
1317 : 1 : rule.dst_port_high = 0xffff;
1318 : 1 : rule.src_port_low = 0x0;
1319 : 1 : rule.src_port_high = 0xffff;
1320 : :
1321 : 1 : rule.dst_mask_len = 33;
1322 : :
1323 : : /* add rules to context and try to build it */
1324 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1325 [ - + ]: 1 : if (ret == 0) {
1326 : : printf("Line %i: Adding rules to ACL context "
1327 : : "should have failed!\n", __LINE__);
1328 : 0 : goto err;
1329 : : }
1330 : :
1331 : 1 : rule.dst_mask_len = 0;
1332 : 1 : rule.src_mask_len = 33;
1333 : :
1334 : : /* add rules to context and try to build it */
1335 : 1 : ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1336 [ - + ]: 1 : if (ret == 0) {
1337 : : printf("Line %i: Adding rules to ACL context "
1338 : : "should have failed!\n", __LINE__);
1339 : 0 : goto err;
1340 : : }
1341 : :
1342 : : /* test inverted high/low destination ports added through the
1343 : : * generic API: rte_acl_add_rules() doesn't know the field types,
1344 : : * so the rule has to be rejected by rte_acl_build().
1345 : : */
1346 : 1 : rule = acl_rule;
1347 : 1 : rule.data.userdata = 1;
1348 : 1 : rule.dst_port_low = 0xfff0;
1349 : 1 : rule.dst_port_high = 0x0010;
1350 : :
1351 : : acl_ipv4vlan_convert_rule(&rule, &rv);
1352 : 1 : ret = rte_acl_add_rules(acx, (struct rte_acl_rule *)&rv, 1);
1353 [ - + ]: 1 : if (ret != 0) {
1354 : : printf("Line %i: Adding rules to ACL context failed!\n",
1355 : : __LINE__);
1356 : 0 : goto err;
1357 : : }
1358 : :
1359 : 1 : ret = rte_acl_ipv4vlan_build(acx, ipv4_7tuple_layout,
1360 : : RTE_ACL_MAX_CATEGORIES);
1361 [ - + ]: 1 : if (ret != -EINVAL) {
1362 : : printf("Line %i: Building ACL context "
1363 : : "should have failed!\n", __LINE__);
1364 : 0 : goto err;
1365 : : }
1366 : :
1367 : 1 : rte_acl_free(acx);
1368 : :
1369 : 1 : return 0;
1370 : :
1371 : 0 : err:
1372 : 0 : rte_acl_free(acx);
1373 : :
1374 : 0 : return -1;
1375 : : }
1376 : :
1377 : : /*
1378 : : * test functions by passing invalid or
1379 : : * non-workable parameters.
1380 : : *
1381 : : * we do very limited testing of classify functions here
1382 : : * because those are performance-critical and
1383 : : * thus don't do much parameter checking.
1384 : : */
1385 : : static int
1386 : 1 : test_invalid_parameters(void)
1387 : : {
1388 : : struct rte_acl_param param;
1389 : : struct rte_acl_ctx *acx;
1390 : : struct rte_acl_ipv4vlan_rule rule;
1391 : : int result;
1392 : :
1393 : 1 : uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
1394 : :
1395 : :
1396 : : /**
1397 : : * rte_ac_create()
1398 : : */
1399 : :
1400 : : /* NULL param */
1401 : 1 : acx = rte_acl_create(NULL);
1402 [ - + ]: 1 : if (acx != NULL) {
1403 : : printf("Line %i: ACL context creation with NULL param "
1404 : : "should have failed!\n", __LINE__);
1405 : 0 : rte_acl_free(acx);
1406 : 0 : return -1;
1407 : : }
1408 : :
1409 : : /* zero rule size */
1410 : : memcpy(¶m, &acl_param, sizeof(param));
1411 : 1 : param.rule_size = 0;
1412 : :
1413 : 1 : acx = rte_acl_create(¶m);
1414 [ - + ]: 1 : if (acx == NULL) {
1415 : : printf("Line %i: ACL context creation with zero rule len "
1416 : : "failed!\n", __LINE__);
1417 : 0 : return -1;
1418 : : } else
1419 : 1 : rte_acl_free(acx);
1420 : :
1421 : : /* zero max rule num */
1422 : : memcpy(¶m, &acl_param, sizeof(param));
1423 : 1 : param.max_rule_num = 0;
1424 : :
1425 : 1 : acx = rte_acl_create(¶m);
1426 [ - + ]: 1 : if (acx == NULL) {
1427 : : printf("Line %i: ACL context creation with zero rule num "
1428 : : "failed!\n", __LINE__);
1429 : 0 : return -1;
1430 : : } else
1431 : 1 : rte_acl_free(acx);
1432 : :
1433 [ - + ]: 1 : if (rte_eal_has_hugepages()) {
1434 : : /* invalid NUMA node */
1435 : : memcpy(¶m, &acl_param, sizeof(param));
1436 : 0 : param.socket_id = RTE_MAX_NUMA_NODES + 1;
1437 : :
1438 : 0 : acx = rte_acl_create(¶m);
1439 [ # # ]: 0 : if (acx != NULL) {
1440 : : printf("Line %i: ACL context creation with invalid "
1441 : : "NUMA should have failed!\n", __LINE__);
1442 : 0 : rte_acl_free(acx);
1443 : 0 : return -1;
1444 : : }
1445 : : }
1446 : :
1447 : : /* NULL name */
1448 : : memcpy(¶m, &acl_param, sizeof(param));
1449 : 1 : param.name = NULL;
1450 : :
1451 : 1 : acx = rte_acl_create(¶m);
1452 [ - + ]: 1 : if (acx != NULL) {
1453 : : printf("Line %i: ACL context creation with NULL name "
1454 : : "should have failed!\n", __LINE__);
1455 : 0 : rte_acl_free(acx);
1456 : 0 : return -1;
1457 : : }
1458 : :
1459 : : /**
1460 : : * rte_acl_find_existing
1461 : : */
1462 : :
1463 : 1 : acx = rte_acl_find_existing(NULL);
1464 [ - + ]: 1 : if (acx != NULL) {
1465 : : printf("Line %i: NULL ACL context found!\n", __LINE__);
1466 : 0 : rte_acl_free(acx);
1467 : 0 : return -1;
1468 : : }
1469 : :
1470 : : /**
1471 : : * rte_acl_ipv4vlan_add_rules
1472 : : */
1473 : :
1474 : : /* initialize everything */
1475 : : memcpy(¶m, &acl_param, sizeof(param));
1476 : 1 : acx = rte_acl_create(¶m);
1477 [ - + ]: 1 : if (acx == NULL) {
1478 : : printf("Line %i: ACL context creation failed!\n", __LINE__);
1479 : 0 : return -1;
1480 : : }
1481 : :
1482 : : memcpy(&rule, &acl_rule, sizeof(rule));
1483 : :
1484 : : /* NULL context */
1485 : 1 : result = rte_acl_ipv4vlan_add_rules(NULL, &rule, 1);
1486 [ - + ]: 1 : if (result == 0) {
1487 : : printf("Line %i: Adding rules with NULL ACL context "
1488 : : "should have failed!\n", __LINE__);
1489 : 0 : rte_acl_free(acx);
1490 : 0 : return -1;
1491 : : }
1492 : :
1493 : : /* NULL rule */
1494 : 1 : result = rte_acl_ipv4vlan_add_rules(acx, NULL, 1);
1495 [ - + ]: 1 : if (result == 0) {
1496 : : printf("Line %i: Adding NULL rule to ACL context "
1497 : : "should have failed!\n", __LINE__);
1498 : 0 : rte_acl_free(acx);
1499 : 0 : return -1;
1500 : : }
1501 : :
1502 : : /* zero count (should succeed) */
1503 : 1 : result = rte_acl_ipv4vlan_add_rules(acx, &rule, 0);
1504 [ - + ]: 1 : if (result != 0) {
1505 : : printf("Line %i: Adding 0 rules to ACL context failed!\n",
1506 : : __LINE__);
1507 : 0 : rte_acl_free(acx);
1508 : 0 : return -1;
1509 : : }
1510 : :
1511 : : /* free ACL context */
1512 : 1 : rte_acl_free(acx);
1513 : :
1514 : :
1515 : : /**
1516 : : * rte_acl_ipv4vlan_build
1517 : : */
1518 : :
1519 : : /* reinitialize context */
1520 : : memcpy(¶m, &acl_param, sizeof(param));
1521 : 1 : acx = rte_acl_create(¶m);
1522 [ - + ]: 1 : if (acx == NULL) {
1523 : : printf("Line %i: ACL context creation failed!\n", __LINE__);
1524 : 0 : return -1;
1525 : : }
1526 : :
1527 : : /* NULL context */
1528 : 1 : result = rte_acl_ipv4vlan_build(NULL, layout, 1);
1529 [ - + ]: 1 : if (result == 0) {
1530 : : printf("Line %i: Building with NULL context "
1531 : : "should have failed!\n", __LINE__);
1532 : 0 : rte_acl_free(acx);
1533 : 0 : return -1;
1534 : : }
1535 : :
1536 : : /* NULL layout */
1537 : 1 : result = rte_acl_ipv4vlan_build(acx, NULL, 1);
1538 [ - + ]: 1 : if (result == 0) {
1539 : : printf("Line %i: Building with NULL layout "
1540 : : "should have failed!\n", __LINE__);
1541 : 0 : rte_acl_free(acx);
1542 : 0 : return -1;
1543 : : }
1544 : :
1545 : : /* zero categories (should not fail) */
1546 : 1 : result = rte_acl_ipv4vlan_build(acx, layout, 0);
1547 [ - + ]: 1 : if (result == 0) {
1548 : : printf("Line %i: Building with 0 categories should fail!\n",
1549 : : __LINE__);
1550 : 0 : rte_acl_free(acx);
1551 : 0 : return -1;
1552 : : }
1553 : :
1554 : : /* SSE classify test */
1555 : :
1556 : : /* cover zero categories in classify (should not fail) */
1557 : 1 : result = rte_acl_classify(acx, NULL, NULL, 0, 0);
1558 [ - + ]: 1 : if (result != 0) {
1559 : : printf("Line %i: SSE classify with zero categories "
1560 : : "failed!\n", __LINE__);
1561 : 0 : rte_acl_free(acx);
1562 : 0 : return -1;
1563 : : }
1564 : :
1565 : : /* cover invalid but positive categories in classify */
1566 : 1 : result = rte_acl_classify(acx, NULL, NULL, 0, 3);
1567 [ - + ]: 1 : if (result == 0) {
1568 : : printf("Line %i: SSE classify with 3 categories "
1569 : : "should have failed!\n", __LINE__);
1570 : 0 : rte_acl_free(acx);
1571 : 0 : return -1;
1572 : : }
1573 : :
1574 : : /* scalar classify test */
1575 : :
1576 : : /* cover zero categories in classify (should not fail) */
1577 : 1 : result = rte_acl_classify_alg(acx, NULL, NULL, 0, 0,
1578 : : RTE_ACL_CLASSIFY_SCALAR);
1579 [ - + ]: 1 : if (result != 0) {
1580 : : printf("Line %i: Scalar classify with zero categories "
1581 : : "failed!\n", __LINE__);
1582 : 0 : rte_acl_free(acx);
1583 : 0 : return -1;
1584 : : }
1585 : :
1586 : : /* cover invalid but positive categories in classify */
1587 : 1 : result = rte_acl_classify(acx, NULL, NULL, 0, 3);
1588 [ - + ]: 1 : if (result == 0) {
1589 : : printf("Line %i: Scalar classify with 3 categories "
1590 : : "should have failed!\n", __LINE__);
1591 : 0 : rte_acl_free(acx);
1592 : 0 : return -1;
1593 : : }
1594 : :
1595 : : /* free ACL context */
1596 : 1 : rte_acl_free(acx);
1597 : :
1598 : :
1599 : : /**
1600 : : * make sure void functions don't crash with NULL parameters
1601 : : */
1602 : :
1603 : 1 : rte_acl_free(NULL);
1604 : :
1605 : 1 : rte_acl_dump(NULL);
1606 : :
1607 : 1 : return 0;
1608 : : }
1609 : :
1610 : : /**
1611 : : * Various tests that don't test much but improve coverage
1612 : : */
1613 : : static int
1614 : 1 : test_misc(void)
1615 : : {
1616 : : struct rte_acl_param param;
1617 : : struct rte_acl_ctx *acx;
1618 : :
1619 : : /* create context */
1620 : : memcpy(¶m, &acl_param, sizeof(param));
1621 : :
1622 : 1 : acx = rte_acl_create(¶m);
1623 [ - + ]: 1 : if (acx == NULL) {
1624 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
1625 : 0 : return -1;
1626 : : }
1627 : :
1628 : : /* dump context with rules - useful for coverage */
1629 : 1 : rte_acl_list_dump();
1630 : :
1631 : 1 : rte_acl_dump(acx);
1632 : :
1633 : 1 : rte_acl_free(acx);
1634 : :
1635 : 1 : return 0;
1636 : : }
1637 : :
1638 : : static uint32_t
1639 : : get_u32_range_max(void)
1640 : : {
1641 : : uint32_t i, max;
1642 : :
1643 : : max = 0;
1644 [ + + ]: 5 : for (i = 0; i != RTE_DIM(acl_u32_range_test_rules); i++)
1645 : 4 : max = RTE_MAX(max, acl_u32_range_test_rules[i].src_mask_len);
1646 : : return max;
1647 : : }
1648 : :
1649 : : static uint32_t
1650 : : get_u32_range_min(void)
1651 : : {
1652 : : uint32_t i, min;
1653 : :
1654 : : min = UINT32_MAX;
1655 [ + + ]: 5 : for (i = 0; i != RTE_DIM(acl_u32_range_test_rules); i++)
1656 : 4 : min = RTE_MIN(min, acl_u32_range_test_rules[i].src_addr);
1657 : : return min;
1658 : : }
1659 : :
1660 : : static const struct rte_acl_ipv4vlan_rule *
1661 : : find_u32_range_rule(uint32_t val)
1662 : : {
1663 : : uint32_t i;
1664 : :
1665 [ + + ]: 921692 : for (i = 0; i != RTE_DIM(acl_u32_range_test_rules); i++) {
1666 [ + + ]: 921690 : if (val >= acl_u32_range_test_rules[i].src_addr &&
1667 [ + + ]: 591292 : val <= acl_u32_range_test_rules[i].src_mask_len)
1668 : 264191 : return acl_u32_range_test_rules + i;
1669 : : }
1670 : : return NULL;
1671 : : }
1672 : :
1673 : : static void
1674 : 4129 : fill_u32_range_data(struct ipv4_7tuple tdata[], uint32_t start, uint32_t num)
1675 : : {
1676 : : uint32_t i;
1677 : : const struct rte_acl_ipv4vlan_rule *r;
1678 : :
1679 [ + + ]: 268322 : for (i = 0; i != num; i++) {
1680 : 264193 : tdata[i].ip_src = start + i;
1681 : : r = find_u32_range_rule(start + i);
1682 [ + + ]: 264193 : if (r != NULL)
1683 : 264191 : tdata[i].allow = r->data.userdata;
1684 : : }
1685 : 4129 : }
1686 : :
1687 : : static int
1688 : 1 : test_u32_range(void)
1689 : : {
1690 : : int32_t rc;
1691 : : uint32_t i, k, max, min;
1692 : : struct rte_acl_ctx *acx;
1693 : : struct acl_ipv4vlan_rule r;
1694 : : struct ipv4_7tuple test_data[64];
1695 : :
1696 : 1 : acx = rte_acl_create(&acl_param);
1697 [ - + ]: 1 : if (acx == NULL) {
1698 : : printf("%s#%i: Error creating ACL context!\n",
1699 : : __func__, __LINE__);
1700 : 0 : return -1;
1701 : : }
1702 : :
1703 [ + + ]: 5 : for (i = 0; i != RTE_DIM(acl_u32_range_test_rules); i++) {
1704 : : convert_rule(&acl_u32_range_test_rules[i], &r);
1705 : 4 : rc = rte_acl_add_rules(acx, (struct rte_acl_rule *)&r, 1);
1706 [ - + ]: 4 : if (rc != 0) {
1707 : : printf("%s#%i: Adding rule to ACL context "
1708 : : "failed with error code: %d\n",
1709 : : __func__, __LINE__, rc);
1710 : 0 : rte_acl_free(acx);
1711 : 0 : return rc;
1712 : : }
1713 : : }
1714 : :
1715 : 1 : rc = build_convert_rules(acx, convert_config_2, 0);
1716 [ - + ]: 1 : if (rc != 0) {
1717 : : printf("%s#%i Error @ build_convert_rules!\n",
1718 : : __func__, __LINE__);
1719 : 0 : rte_acl_free(acx);
1720 : 0 : return rc;
1721 : : }
1722 : :
1723 : : max = get_u32_range_max();
1724 : : min = get_u32_range_min();
1725 : :
1726 : 1 : max = RTE_MAX(max, max + 1);
1727 : 1 : min = RTE_MIN(min, min - 1);
1728 : :
1729 : : printf("%s#%d starting range test from %u to %u\n",
1730 : : __func__, __LINE__, min, max);
1731 : :
1732 [ + + ]: 4130 : for (i = min; i <= max; i += k) {
1733 : :
1734 : 4129 : k = RTE_MIN(max - i + 1, RTE_DIM(test_data));
1735 : :
1736 : : memset(test_data, 0, sizeof(test_data));
1737 : 4129 : fill_u32_range_data(test_data, i, k);
1738 : :
1739 : 4129 : rc = test_classify_run(acx, test_data, k);
1740 [ - + ]: 4129 : if (rc != 0) {
1741 : 0 : printf("%s#%d failed at [%u, %u) interval\n",
1742 : : __func__, __LINE__, i, i + k);
1743 : : break;
1744 : : }
1745 : : }
1746 : :
1747 : 1 : rte_acl_free(acx);
1748 : 1 : return rc;
1749 : : }
1750 : :
1751 : : static struct rte_acl_ctx *acl_ctx;
1752 : :
1753 : : static void *
1754 : 4 : running_alloc(char *name, size_t size,
1755 : : size_t align, int32_t socket_id, void *udata)
1756 : : {
1757 : 4 : size_t aligned_size = RTE_ALIGN_CEIL(size, align);
1758 : : RTE_SET_USED(name);
1759 : : RTE_SET_USED(socket_id);
1760 : :
1761 [ - + ]: 4 : if (udata != acl_ctx) {
1762 : : printf("%s#%i udata mismatch!\n",
1763 : : __func__, __LINE__);
1764 : 0 : return NULL;
1765 : : }
1766 : 4 : void *addr = aligned_alloc(align, aligned_size);
1767 [ - + ]: 4 : if (addr == NULL) {
1768 : : printf("%s#%i alloc memory failed!\n",
1769 : : __func__, __LINE__);
1770 : : }
1771 : : return addr;
1772 : : }
1773 : :
1774 : : static void
1775 : 6 : running_free(void *ptr, void *udata)
1776 : : {
1777 [ - + ]: 6 : if (udata != acl_ctx) {
1778 : : printf("%s#%i udata mismatch!\n",
1779 : : __func__, __LINE__);
1780 : 0 : return;
1781 : : }
1782 : 6 : free(ptr);
1783 : : }
1784 : :
1785 : : static int
1786 : 1 : test_mem_hook(void)
1787 : : {
1788 : : int i, ret;
1789 : : struct rte_acl_mem_hook new_hook;
1790 : :
1791 : 1 : acl_ctx = rte_acl_create(&acl_param);
1792 [ - + ]: 1 : if (acl_ctx == NULL) {
1793 : : printf("Line %i: Error creating ACL context!\n", __LINE__);
1794 : 0 : return -1;
1795 : : }
1796 : :
1797 : 1 : struct rte_acl_mem_hook mhook = {
1798 : : .zalloc = running_alloc,
1799 : : .free = running_free,
1800 : : .udata = acl_ctx
1801 : : };
1802 : :
1803 : 1 : ret = rte_acl_set_mem_hook(acl_ctx, &mhook);
1804 [ - + ]: 1 : if (ret != 0) {
1805 : : printf("Line %i: Error set mem hook for acl context!\n", __LINE__);
1806 : 0 : rte_acl_free(acl_ctx);
1807 : 0 : return -1;
1808 : : }
1809 : :
1810 : : memset(&new_hook, 0, sizeof(struct rte_acl_mem_hook));
1811 [ + - ]: 1 : if (rte_acl_get_mem_hook(acl_ctx, &new_hook) != 0
1812 [ - + ]: 1 : || memcmp(&mhook, &new_hook, sizeof(struct rte_acl_mem_hook)) != 0) {
1813 : : printf("Line %i: Error get mem hook for acl context!\n", __LINE__);
1814 : 0 : rte_acl_free(acl_ctx);
1815 : 0 : return -1;
1816 : : }
1817 : :
1818 : : ret = 0;
1819 [ + + ]: 5 : for (i = 0; i < TEST_CLASSIFY_ITER; i++) {
1820 [ + + ]: 4 : if ((i & 1) == 0)
1821 : 2 : rte_acl_reset(acl_ctx);
1822 : : else
1823 : 2 : rte_acl_reset_rules(acl_ctx);
1824 : :
1825 : 4 : ret = test_classify_buid(acl_ctx, acl_test_rules,
1826 : : RTE_DIM(acl_test_rules));
1827 [ - + ]: 4 : if (ret != 0) {
1828 : : printf("Line %i, iter: %d: Adding rules to ACL context failed!\n",
1829 : : __LINE__, i);
1830 : : break;
1831 : : }
1832 : :
1833 : 4 : ret = test_classify_run(acl_ctx, acl_test_data,
1834 : : RTE_DIM(acl_test_data));
1835 [ - + ]: 4 : if (ret != 0) {
1836 : : printf("Line %i, iter: %d: %s failed!\n",
1837 : : __LINE__, i, __func__);
1838 : : break;
1839 : : }
1840 : :
1841 : : /* reset rules and make sure that classify still works ok. */
1842 : 4 : rte_acl_reset_rules(acl_ctx);
1843 : 4 : ret = test_classify_run(acl_ctx, acl_test_data,
1844 : : RTE_DIM(acl_test_data));
1845 [ - + ]: 4 : if (ret != 0) {
1846 : : printf("Line %i, iter: %d: %s failed!\n",
1847 : : __LINE__, i, __func__);
1848 : : break;
1849 : : }
1850 : : }
1851 : :
1852 : 1 : rte_acl_free(acl_ctx);
1853 : 1 : acl_ctx = NULL;
1854 : 1 : return ret;
1855 : : }
1856 : :
1857 : : static int
1858 : 1 : test_acl(void)
1859 : : {
1860 [ + - ]: 1 : if (test_invalid_parameters() < 0)
1861 : : return -1;
1862 [ + - ]: 1 : if (test_invalid_rules() < 0)
1863 : : return -1;
1864 [ + - ]: 1 : if (test_create_find_add() < 0)
1865 : : return -1;
1866 [ + - ]: 1 : if (test_invalid_layout() < 0)
1867 : : return -1;
1868 [ + - ]: 1 : if (test_misc() < 0)
1869 : : return -1;
1870 [ + - ]: 1 : if (test_classify() < 0)
1871 : : return -1;
1872 [ + - ]: 1 : if (test_build_ports_range() < 0)
1873 : : return -1;
1874 [ + - ]: 1 : if (test_convert() < 0)
1875 : : return -1;
1876 [ + - ]: 1 : if (test_u32_range() < 0)
1877 : : return -1;
1878 [ - + ]: 1 : if (test_mem_hook() < 0)
1879 : 0 : return -1;
1880 : :
1881 : : return 0;
1882 : : }
1883 : :
1884 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
1885 : :
1886 : 306 : REGISTER_FAST_TEST(acl_autotest, NOHUGE_OK, ASAN_OK, test_acl);
|