4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <rte_string_fns.h>
41 #include <rte_byteorder.h>
44 #include <rte_common.h>
48 #define LEN RTE_ACL_MAX_CATEGORIES
50 RTE_ACL_RULE_DEF(acl_ipv4vlan_rule, RTE_ACL_IPV4VLAN_NUM_FIELDS);
52 struct rte_acl_param acl_param = {
54 .socket_id = SOCKET_ID_ANY,
55 .rule_size = RTE_ACL_IPV4VLAN_RULE_SZ,
56 .max_rule_num = 0x30000,
59 struct rte_acl_ipv4vlan_rule acl_rule = {
60 .data = { .priority = 1, .category_mask = 0xff },
62 .src_port_high = UINT16_MAX,
64 .dst_port_high = UINT16_MAX,
67 const uint32_t ipv4_7tuple_layout[RTE_ACL_IPV4VLAN_NUM] = {
68 offsetof(struct ipv4_7tuple, proto),
69 offsetof(struct ipv4_7tuple, vlan),
70 offsetof(struct ipv4_7tuple, ip_src),
71 offsetof(struct ipv4_7tuple, ip_dst),
72 offsetof(struct ipv4_7tuple, port_src),
76 /* byteswap to cpu or network order */
78 bswap_test_data(struct ipv4_7tuple *data, int len, int to_be)
82 for (i = 0; i < len; i++) {
85 /* swap all bytes so that they are in network order */
86 data[i].ip_dst = rte_cpu_to_be_32(data[i].ip_dst);
87 data[i].ip_src = rte_cpu_to_be_32(data[i].ip_src);
88 data[i].port_dst = rte_cpu_to_be_16(data[i].port_dst);
89 data[i].port_src = rte_cpu_to_be_16(data[i].port_src);
90 data[i].vlan = rte_cpu_to_be_16(data[i].vlan);
91 data[i].domain = rte_cpu_to_be_16(data[i].domain);
93 data[i].ip_dst = rte_be_to_cpu_32(data[i].ip_dst);
94 data[i].ip_src = rte_be_to_cpu_32(data[i].ip_src);
95 data[i].port_dst = rte_be_to_cpu_16(data[i].port_dst);
96 data[i].port_src = rte_be_to_cpu_16(data[i].port_src);
97 data[i].vlan = rte_be_to_cpu_16(data[i].vlan);
98 data[i].domain = rte_be_to_cpu_16(data[i].domain);
104 * Test scalar and SSE ACL lookup.
107 test_classify_run(struct rte_acl_ctx *acx)
110 uint32_t result, count;
111 uint32_t results[RTE_DIM(acl_test_data) * RTE_ACL_MAX_CATEGORIES];
112 const uint8_t *data[RTE_DIM(acl_test_data)];
114 /* swap all bytes in the data to network order */
115 bswap_test_data(acl_test_data, RTE_DIM(acl_test_data), 1);
117 /* store pointers to test data */
118 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++)
119 data[i] = (uint8_t *)&acl_test_data[i];
122 * these will run quite a few times, it's necessary to test code paths
123 * from num=0 to num>8
125 for (count = 0; count <= RTE_DIM(acl_test_data); count++) {
126 ret = rte_acl_classify(acx, data, results,
127 count, RTE_ACL_MAX_CATEGORIES);
129 printf("Line %i: SSE classify failed!\n", __LINE__);
133 /* check if we allow everything we should allow */
134 for (i = 0; i < (int) count; i++) {
136 results[i * RTE_ACL_MAX_CATEGORIES + ACL_ALLOW];
137 if (result != acl_test_data[i].allow) {
138 printf("Line %i: Error in allow results at %i "
139 "(expected %"PRIu32" got %"PRIu32")!\n",
140 __LINE__, i, acl_test_data[i].allow,
147 /* check if we deny everything we should deny */
148 for (i = 0; i < (int) count; i++) {
149 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_DENY];
150 if (result != acl_test_data[i].deny) {
151 printf("Line %i: Error in deny results at %i "
152 "(expected %"PRIu32" got %"PRIu32")!\n",
153 __LINE__, i, acl_test_data[i].deny,
161 /* make a quick check for scalar */
162 ret = rte_acl_classify_alg(acx, data, results,
163 RTE_DIM(acl_test_data), RTE_ACL_MAX_CATEGORIES,
164 RTE_ACL_CLASSIFY_SCALAR);
166 printf("Line %i: scalar classify failed!\n", __LINE__);
170 /* check if we allow everything we should allow */
171 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++) {
172 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_ALLOW];
173 if (result != acl_test_data[i].allow) {
174 printf("Line %i: Error in allow results at %i "
175 "(expected %"PRIu32" got %"PRIu32")!\n",
176 __LINE__, i, acl_test_data[i].allow,
183 /* check if we deny everything we should deny */
184 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++) {
185 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_DENY];
186 if (result != acl_test_data[i].deny) {
187 printf("Line %i: Error in deny results at %i "
188 "(expected %"PRIu32" got %"PRIu32")!\n",
189 __LINE__, i, acl_test_data[i].deny,
199 /* swap data back to cpu order so that next time tests don't fail */
200 bswap_test_data(acl_test_data, RTE_DIM(acl_test_data), 0);
205 test_classify_buid(struct rte_acl_ctx *acx,
206 const struct rte_acl_ipv4vlan_rule *rules, uint32_t num)
210 /* add rules to the context */
211 ret = rte_acl_ipv4vlan_add_rules(acx, rules, num);
213 printf("Line %i: Adding rules to ACL context failed!\n",
218 /* try building the context */
219 ret = rte_acl_ipv4vlan_build(acx, ipv4_7tuple_layout,
220 RTE_ACL_MAX_CATEGORIES);
222 printf("Line %i: Building ACL context failed!\n", __LINE__);
229 #define TEST_CLASSIFY_ITER 4
232 * Test scalar and SSE ACL lookup.
237 struct rte_acl_ctx *acx;
240 acx = rte_acl_create(&acl_param);
242 printf("Line %i: Error creating ACL context!\n", __LINE__);
247 for (i = 0; i != TEST_CLASSIFY_ITER; i++) {
252 rte_acl_reset_rules(acx);
254 ret = test_classify_buid(acx, acl_test_rules,
255 RTE_DIM(acl_test_rules));
257 printf("Line %i, iter: %d: "
258 "Adding rules to ACL context failed!\n",
263 ret = test_classify_run(acx);
265 printf("Line %i, iter: %d: %s failed!\n",
266 __LINE__, i, __func__);
270 /* reset rules and make sure that classify still works ok. */
271 rte_acl_reset_rules(acx);
272 ret = test_classify_run(acx);
274 printf("Line %i, iter: %d: %s failed!\n",
275 __LINE__, i, __func__);
285 test_build_ports_range(void)
287 static const struct rte_acl_ipv4vlan_rule test_rules[] = {
289 /* match all packets. */
292 .category_mask = ACL_ALLOW_MASK,
296 .src_port_high = UINT16_MAX,
298 .dst_port_high = UINT16_MAX,
301 /* match all packets with dst ports [54-65280]. */
304 .category_mask = ACL_ALLOW_MASK,
308 .src_port_high = UINT16_MAX,
310 .dst_port_high = 65280,
313 /* match all packets with dst ports [0-52]. */
316 .category_mask = ACL_ALLOW_MASK,
320 .src_port_high = UINT16_MAX,
325 /* match all packets with dst ports [53]. */
328 .category_mask = ACL_ALLOW_MASK,
332 .src_port_high = UINT16_MAX,
337 /* match all packets with dst ports [65279-65535]. */
340 .category_mask = ACL_ALLOW_MASK,
344 .src_port_high = UINT16_MAX,
345 .dst_port_low = 65279,
346 .dst_port_high = UINT16_MAX,
350 static struct ipv4_7tuple test_data[] = {
353 .ip_src = IPv4(10, 1, 1, 1),
354 .ip_dst = IPv4(192, 168, 0, 33),
360 .ip_src = IPv4(127, 84, 33, 1),
361 .ip_dst = IPv4(1, 2, 3, 4),
367 struct rte_acl_ctx *acx;
369 uint32_t results[RTE_DIM(test_data)];
370 const uint8_t *data[RTE_DIM(test_data)];
372 acx = rte_acl_create(&acl_param);
374 printf("Line %i: Error creating ACL context!\n", __LINE__);
378 /* swap all bytes in the data to network order */
379 bswap_test_data(test_data, RTE_DIM(test_data), 1);
381 /* store pointers to test data */
382 for (i = 0; i != RTE_DIM(test_data); i++)
383 data[i] = (uint8_t *)&test_data[i];
385 for (i = 0; i != RTE_DIM(test_rules); i++) {
387 ret = test_classify_buid(acx, test_rules, i + 1);
389 printf("Line %i, iter: %d: "
390 "Adding rules to ACL context failed!\n",
394 ret = rte_acl_classify(acx, data, results,
397 printf("Line %i, iter: %d: classify failed!\n",
403 for (j = 0; j != RTE_DIM(results); j++) {
404 if (results[j] != test_data[j].allow) {
405 printf("Line %i: Error in allow results at %i "
406 "(expected %"PRIu32" got %"PRIu32")!\n",
407 __LINE__, j, test_data[j].allow,
414 bswap_test_data(test_data, RTE_DIM(test_data), 0);
421 convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
422 struct acl_ipv4vlan_rule *ro)
426 ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].value.u8 = ri->proto;
427 ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].value.u16 = ri->vlan;
428 ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].value.u16 = ri->domain;
429 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = ri->src_addr;
430 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = ri->dst_addr;
431 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].value.u16 = ri->src_port_low;
432 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].value.u16 = ri->dst_port_low;
434 ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].mask_range.u8 = ri->proto_mask;
435 ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].mask_range.u16 = ri->vlan_mask;
436 ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].mask_range.u16 =
438 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
440 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = ri->dst_mask_len;
441 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].mask_range.u16 =
443 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
448 * Convert IPV4 source and destination from RTE_ACL_FIELD_TYPE_MASK to
449 * RTE_ACL_FIELD_TYPE_BITMASK.
452 convert_rule_1(const struct rte_acl_ipv4vlan_rule *ri,
453 struct acl_ipv4vlan_rule *ro)
457 convert_rule(ri, ro);
458 v = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32;
459 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
460 RTE_ACL_MASKLEN_TO_BITMASK(v, sizeof(v));
461 v = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32;
462 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 =
463 RTE_ACL_MASKLEN_TO_BITMASK(v, sizeof(v));
467 * Convert IPV4 source and destination from RTE_ACL_FIELD_TYPE_MASK to
468 * RTE_ACL_FIELD_TYPE_RANGE.
471 convert_rule_2(const struct rte_acl_ipv4vlan_rule *ri,
472 struct acl_ipv4vlan_rule *ro)
474 uint32_t hi, lo, mask;
476 convert_rule(ri, ro);
478 mask = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32;
479 mask = RTE_ACL_MASKLEN_TO_BITMASK(mask, sizeof(mask));
480 lo = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 & mask;
482 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = lo;
483 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 = hi;
485 mask = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32;
486 mask = RTE_ACL_MASKLEN_TO_BITMASK(mask, sizeof(mask));
487 lo = ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 & mask;
489 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = lo;
490 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = hi;
494 * Convert rte_acl_ipv4vlan_rule: swap VLAN and PORTS rule fields.
497 convert_rule_3(const struct rte_acl_ipv4vlan_rule *ri,
498 struct acl_ipv4vlan_rule *ro)
500 struct rte_acl_field t1, t2;
502 convert_rule(ri, ro);
504 t1 = ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD];
505 t2 = ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD];
507 ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD] =
508 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD];
509 ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD] =
510 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD];
512 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD] = t1;
513 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD] = t2;
517 * Convert rte_acl_ipv4vlan_rule: swap SRC and DST IPv4 address rules.
520 convert_rule_4(const struct rte_acl_ipv4vlan_rule *ri,
521 struct acl_ipv4vlan_rule *ro)
523 struct rte_acl_field t;
525 convert_rule(ri, ro);
527 t = ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD];
528 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD] =
529 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD];
531 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD] = t;
535 ipv4vlan_config(struct rte_acl_config *cfg,
536 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
537 uint32_t num_categories)
539 static const struct rte_acl_field_def
540 ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
542 .type = RTE_ACL_FIELD_TYPE_BITMASK,
543 .size = sizeof(uint8_t),
544 .field_index = RTE_ACL_IPV4VLAN_PROTO_FIELD,
545 .input_index = RTE_ACL_IPV4VLAN_PROTO,
548 .type = RTE_ACL_FIELD_TYPE_BITMASK,
549 .size = sizeof(uint16_t),
550 .field_index = RTE_ACL_IPV4VLAN_VLAN1_FIELD,
551 .input_index = RTE_ACL_IPV4VLAN_VLAN,
554 .type = RTE_ACL_FIELD_TYPE_BITMASK,
555 .size = sizeof(uint16_t),
556 .field_index = RTE_ACL_IPV4VLAN_VLAN2_FIELD,
557 .input_index = RTE_ACL_IPV4VLAN_VLAN,
560 .type = RTE_ACL_FIELD_TYPE_MASK,
561 .size = sizeof(uint32_t),
562 .field_index = RTE_ACL_IPV4VLAN_SRC_FIELD,
563 .input_index = RTE_ACL_IPV4VLAN_SRC,
566 .type = RTE_ACL_FIELD_TYPE_MASK,
567 .size = sizeof(uint32_t),
568 .field_index = RTE_ACL_IPV4VLAN_DST_FIELD,
569 .input_index = RTE_ACL_IPV4VLAN_DST,
572 .type = RTE_ACL_FIELD_TYPE_RANGE,
573 .size = sizeof(uint16_t),
574 .field_index = RTE_ACL_IPV4VLAN_SRCP_FIELD,
575 .input_index = RTE_ACL_IPV4VLAN_PORTS,
578 .type = RTE_ACL_FIELD_TYPE_RANGE,
579 .size = sizeof(uint16_t),
580 .field_index = RTE_ACL_IPV4VLAN_DSTP_FIELD,
581 .input_index = RTE_ACL_IPV4VLAN_PORTS,
585 memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
586 cfg->num_fields = RTE_DIM(ipv4_defs);
588 cfg->defs[RTE_ACL_IPV4VLAN_PROTO_FIELD].offset =
589 layout[RTE_ACL_IPV4VLAN_PROTO];
590 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].offset =
591 layout[RTE_ACL_IPV4VLAN_VLAN];
592 cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].offset =
593 layout[RTE_ACL_IPV4VLAN_VLAN] +
594 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].size;
595 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].offset =
596 layout[RTE_ACL_IPV4VLAN_SRC];
597 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset =
598 layout[RTE_ACL_IPV4VLAN_DST];
599 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset =
600 layout[RTE_ACL_IPV4VLAN_PORTS];
601 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset =
602 layout[RTE_ACL_IPV4VLAN_PORTS] +
603 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size;
605 cfg->num_categories = num_categories;
609 convert_rules(struct rte_acl_ctx *acx,
610 void (*convert)(const struct rte_acl_ipv4vlan_rule *,
611 struct acl_ipv4vlan_rule *),
612 const struct rte_acl_ipv4vlan_rule *rules, uint32_t num)
616 struct acl_ipv4vlan_rule r;
618 for (i = 0; i != num; i++) {
619 convert(rules + i, &r);
620 rc = rte_acl_add_rules(acx, (struct rte_acl_rule *)&r, 1);
622 printf("Line %i: Adding rule %u to ACL context "
623 "failed with error code: %d\n",
633 convert_config(struct rte_acl_config *cfg)
635 ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
639 * Convert rte_acl_ipv4vlan_rule to use RTE_ACL_FIELD_TYPE_BITMASK.
642 convert_config_1(struct rte_acl_config *cfg)
644 ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
645 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].type = RTE_ACL_FIELD_TYPE_BITMASK;
646 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = RTE_ACL_FIELD_TYPE_BITMASK;
650 * Convert rte_acl_ipv4vlan_rule to use RTE_ACL_FIELD_TYPE_RANGE.
653 convert_config_2(struct rte_acl_config *cfg)
655 ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
656 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].type = RTE_ACL_FIELD_TYPE_RANGE;
657 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = RTE_ACL_FIELD_TYPE_RANGE;
661 * Convert rte_acl_ipv4vlan_rule: swap VLAN and PORTS rule definitions.
664 convert_config_3(struct rte_acl_config *cfg)
666 struct rte_acl_field_def t1, t2;
668 ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
670 t1 = cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD];
671 t2 = cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD];
673 /* swap VLAN1 and SRCP rule definition. */
674 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD] =
675 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD];
676 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].field_index = t1.field_index;
677 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].input_index = t1.input_index;
679 /* swap VLAN2 and DSTP rule definition. */
680 cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD] =
681 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD];
682 cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].field_index = t2.field_index;
683 cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].input_index = t2.input_index;
685 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].type = t1.type;
686 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size = t1.size;
687 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset = t1.offset;
689 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].type = t2.type;
690 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].size = t2.size;
691 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset = t2.offset;
695 * Convert rte_acl_ipv4vlan_rule: swap SRC and DST ip address rule definitions.
698 convert_config_4(struct rte_acl_config *cfg)
700 struct rte_acl_field_def t;
702 ipv4vlan_config(cfg, ipv4_7tuple_layout, RTE_ACL_MAX_CATEGORIES);
704 t = cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD];
706 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD] =
707 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD];
708 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].field_index = t.field_index;
709 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].input_index = t.input_index;
711 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].type = t.type;
712 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].size = t.size;
713 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset = t.offset;
718 build_convert_rules(struct rte_acl_ctx *acx,
719 void (*config)(struct rte_acl_config *),
722 struct rte_acl_config cfg;
724 memset(&cfg, 0, sizeof(cfg));
726 cfg.max_size = max_size;
727 return rte_acl_build(acx, &cfg);
731 test_convert_rules(const char *desc,
732 void (*config)(struct rte_acl_config *),
733 void (*convert)(const struct rte_acl_ipv4vlan_rule *,
734 struct acl_ipv4vlan_rule *))
736 struct rte_acl_ctx *acx;
739 static const size_t mem_sizes[] = {0, -1};
741 printf("running %s(%s)\n", __func__, desc);
743 acx = rte_acl_create(&acl_param);
745 printf("Line %i: Error creating ACL context!\n", __LINE__);
749 rc = convert_rules(acx, convert, acl_test_rules,
750 RTE_DIM(acl_test_rules));
752 printf("Line %i: Error converting ACL rules!\n", __LINE__);
754 for (i = 0; rc == 0 && i != RTE_DIM(mem_sizes); i++) {
756 rc = build_convert_rules(acx, config, mem_sizes[i]);
758 printf("Line %i: Error @ build_convert_rules(%zu)!\n",
759 __LINE__, mem_sizes[i]);
763 rc = test_classify_run(acx);
765 printf("%s failed at line %i, max_size=%zu\n",
766 __func__, __LINE__, mem_sizes[i]);
776 static const struct {
778 void (*config)(struct rte_acl_config *);
779 void (*convert)(const struct rte_acl_ipv4vlan_rule *,
780 struct acl_ipv4vlan_rule *);
781 } convert_param[] = {
783 "acl_ipv4vlan_tuple",
788 "acl_ipv4vlan_tuple, RTE_ACL_FIELD_TYPE_BITMASK type "
794 "acl_ipv4vlan_tuple, RTE_ACL_FIELD_TYPE_RANGE type "
800 "acl_ipv4vlan_tuple: swap VLAN and PORTs order",
805 "acl_ipv4vlan_tuple: swap SRC and DST IPv4 order",
814 for (i = 0; i != RTE_DIM(convert_param); i++) {
815 rc = test_convert_rules(convert_param[i].desc,
816 convert_param[i].config,
817 convert_param[i].convert);
819 printf("%s for test-case: %s failed, error code: %d;\n",
820 __func__, convert_param[i].desc, rc);
829 * Test wrong layout behavior
830 * This test supplies the ACL context with invalid layout, which results in
831 * ACL matching the wrong stuff. However, it should match the wrong stuff
832 * the right way. We switch around source and destination addresses,
833 * source and destination ports, and protocol will point to first byte of
837 test_invalid_layout(void)
839 struct rte_acl_ctx *acx;
842 uint32_t results[RTE_DIM(invalid_layout_data)];
843 const uint8_t *data[RTE_DIM(invalid_layout_data)];
845 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
846 /* proto points to destination port's first byte */
847 offsetof(struct ipv4_7tuple, port_dst),
849 0, /* VLAN not used */
851 /* src and dst addresses are swapped */
852 offsetof(struct ipv4_7tuple, ip_dst),
853 offsetof(struct ipv4_7tuple, ip_src),
856 * we can't swap ports here, so we will swap
859 offsetof(struct ipv4_7tuple, port_src),
862 acx = rte_acl_create(&acl_param);
864 printf("Line %i: Error creating ACL context!\n", __LINE__);
868 /* putting a lot of rules into the context results in greater
869 * coverage numbers. it doesn't matter if they are identical */
870 for (i = 0; i < 1000; i++) {
871 /* add rules to the context */
872 ret = rte_acl_ipv4vlan_add_rules(acx, invalid_layout_rules,
873 RTE_DIM(invalid_layout_rules));
875 printf("Line %i: Adding rules to ACL context failed!\n",
882 /* try building the context */
883 ret = rte_acl_ipv4vlan_build(acx, layout, 1);
885 printf("Line %i: Building ACL context failed!\n", __LINE__);
890 /* swap all bytes in the data to network order */
891 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 1);
894 for (i = 0; i < (int) RTE_DIM(invalid_layout_data); i++) {
895 data[i] = (uint8_t *)&invalid_layout_data[i];
898 /* classify tuples */
899 ret = rte_acl_classify_alg(acx, data, results,
900 RTE_DIM(results), 1, RTE_ACL_CLASSIFY_SCALAR);
902 printf("Line %i: SSE classify failed!\n", __LINE__);
907 for (i = 0; i < (int) RTE_DIM(results); i++) {
908 if (results[i] != invalid_layout_data[i].allow) {
909 printf("Line %i: Wrong results at %i "
910 "(result=%u, should be %u)!\n",
911 __LINE__, i, results[i],
912 invalid_layout_data[i].allow);
917 /* classify tuples (scalar) */
918 ret = rte_acl_classify_alg(acx, data, results, RTE_DIM(results), 1,
919 RTE_ACL_CLASSIFY_SCALAR);
922 printf("Line %i: Scalar classify failed!\n", __LINE__);
927 for (i = 0; i < (int) RTE_DIM(results); i++) {
928 if (results[i] != invalid_layout_data[i].allow) {
929 printf("Line %i: Wrong results at %i "
930 "(result=%u, should be %u)!\n",
931 __LINE__, i, results[i],
932 invalid_layout_data[i].allow);
939 /* swap data back to cpu order so that next time tests don't fail */
940 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
945 /* swap data back to cpu order so that next time tests don't fail */
946 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
954 * Test creating and finding ACL contexts, and adding rules
957 test_create_find_add(void)
959 struct rte_acl_param param;
960 struct rte_acl_ctx *acx, *acx2, *tmp;
961 struct rte_acl_ipv4vlan_rule rules[LEN];
963 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
965 const char *acx_name = "acx";
966 const char *acx2_name = "acx2";
969 /* create two contexts */
970 memcpy(¶m, &acl_param, sizeof(param));
971 param.max_rule_num = 2;
973 param.name = acx_name;
974 acx = rte_acl_create(¶m);
976 printf("Line %i: Error creating %s!\n", __LINE__, acx_name);
980 param.name = acx2_name;
981 acx2 = rte_acl_create(¶m);
982 if (acx2 == NULL || acx2 == acx) {
983 printf("Line %i: Error creating %s!\n", __LINE__, acx2_name);
988 /* try to create third one, with an existing name */
989 param.name = acx_name;
990 tmp = rte_acl_create(¶m);
992 printf("Line %i: Creating context with existing name "
1000 param.name = acx2_name;
1001 tmp = rte_acl_create(¶m);
1003 printf("Line %i: Creating context with existing "
1004 "name test 2 failed!\n",
1011 /* try to find existing ACL contexts */
1012 tmp = rte_acl_find_existing(acx_name);
1014 printf("Line %i: Finding %s failed!\n", __LINE__, acx_name);
1020 tmp = rte_acl_find_existing(acx2_name);
1022 printf("Line %i: Finding %s failed!\n", __LINE__, acx2_name);
1028 /* try to find non-existing context */
1029 tmp = rte_acl_find_existing("invalid");
1031 printf("Line %i: Non-existent ACL context found!\n", __LINE__);
1039 /* create valid (but severely limited) acx */
1040 memcpy(¶m, &acl_param, sizeof(param));
1041 param.max_rule_num = LEN;
1043 acx = rte_acl_create(¶m);
1045 printf("Line %i: Error creating %s!\n", __LINE__, param.name);
1049 /* create dummy acl */
1050 for (i = 0; i < LEN; i++) {
1051 memcpy(&rules[i], &acl_rule,
1052 sizeof(struct rte_acl_ipv4vlan_rule));
1054 rules[i].data.userdata = i + 1;
1055 /* one rule per category */
1056 rules[i].data.category_mask = 1 << i;
1059 /* try filling up the context */
1060 ret = rte_acl_ipv4vlan_add_rules(acx, rules, LEN);
1062 printf("Line %i: Adding %i rules to ACL context failed!\n",
1067 /* try adding to a (supposedly) full context */
1068 ret = rte_acl_ipv4vlan_add_rules(acx, rules, 1);
1070 printf("Line %i: Adding rules to full ACL context should"
1071 "have failed!\n", __LINE__);
1075 /* try building the context */
1076 ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
1078 printf("Line %i: Building ACL context failed!\n", __LINE__);
1093 * test various invalid rules
1096 test_invalid_rules(void)
1098 struct rte_acl_ctx *acx;
1101 struct rte_acl_ipv4vlan_rule rule;
1103 acx = rte_acl_create(&acl_param);
1105 printf("Line %i: Error creating ACL context!\n", __LINE__);
1109 /* test inverted high/low source and destination ports.
1110 * originally, there was a problem with memory consumption when using
1113 /* create dummy acl */
1114 memcpy(&rule, &acl_rule, sizeof(struct rte_acl_ipv4vlan_rule));
1115 rule.data.userdata = 1;
1116 rule.dst_port_low = 0xfff0;
1117 rule.dst_port_high = 0x0010;
1119 /* add rules to context and try to build it */
1120 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1122 printf("Line %i: Adding rules to ACL context "
1123 "should have failed!\n", __LINE__);
1127 rule.dst_port_low = 0x0;
1128 rule.dst_port_high = 0xffff;
1129 rule.src_port_low = 0xfff0;
1130 rule.src_port_high = 0x0010;
1132 /* add rules to context and try to build it */
1133 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1135 printf("Line %i: Adding rules to ACL context "
1136 "should have failed!\n", __LINE__);
1140 rule.dst_port_low = 0x0;
1141 rule.dst_port_high = 0xffff;
1142 rule.src_port_low = 0x0;
1143 rule.src_port_high = 0xffff;
1145 rule.dst_mask_len = 33;
1147 /* add rules to context and try to build it */
1148 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1150 printf("Line %i: Adding rules to ACL context "
1151 "should have failed!\n", __LINE__);
1155 rule.dst_mask_len = 0;
1156 rule.src_mask_len = 33;
1158 /* add rules to context and try to build it */
1159 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1161 printf("Line %i: Adding rules to ACL context "
1162 "should have failed!\n", __LINE__);
1166 rule.dst_mask_len = 0;
1167 rule.src_mask_len = 0;
1168 rule.data.userdata = 0;
1170 /* try adding this rule (it should fail because userdata is invalid) */
1171 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1173 printf("Line %i: Adding a rule with invalid user data "
1174 "should have failed!\n", __LINE__);
1190 * test functions by passing invalid or
1191 * non-workable parameters.
1193 * we do very limited testing of classify functions here
1194 * because those are performance-critical and
1195 * thus don't do much parameter checking.
1198 test_invalid_parameters(void)
1200 struct rte_acl_param param;
1201 struct rte_acl_ctx *acx;
1202 struct rte_acl_ipv4vlan_rule rule;
1205 uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
1213 acx = rte_acl_create(NULL);
1215 printf("Line %i: ACL context creation with NULL param "
1216 "should have failed!\n", __LINE__);
1221 /* zero rule size */
1222 memcpy(¶m, &acl_param, sizeof(param));
1223 param.rule_size = 0;
1225 acx = rte_acl_create(¶m);
1227 printf("Line %i: ACL context creation with zero rule len "
1228 "failed!\n", __LINE__);
1233 /* zero max rule num */
1234 memcpy(¶m, &acl_param, sizeof(param));
1235 param.max_rule_num = 0;
1237 acx = rte_acl_create(¶m);
1239 printf("Line %i: ACL context creation with zero rule num "
1240 "failed!\n", __LINE__);
1245 /* invalid NUMA node */
1246 memcpy(¶m, &acl_param, sizeof(param));
1247 param.socket_id = RTE_MAX_NUMA_NODES + 1;
1249 acx = rte_acl_create(¶m);
1251 printf("Line %i: ACL context creation with invalid NUMA "
1252 "should have failed!\n", __LINE__);
1258 memcpy(¶m, &acl_param, sizeof(param));
1261 acx = rte_acl_create(¶m);
1263 printf("Line %i: ACL context creation with NULL name "
1264 "should have failed!\n", __LINE__);
1270 * rte_acl_find_existing
1273 acx = rte_acl_find_existing(NULL);
1275 printf("Line %i: NULL ACL context found!\n", __LINE__);
1281 * rte_acl_ipv4vlan_add_rules
1284 /* initialize everything */
1285 memcpy(¶m, &acl_param, sizeof(param));
1286 acx = rte_acl_create(¶m);
1288 printf("Line %i: ACL context creation failed!\n", __LINE__);
1292 memcpy(&rule, &acl_rule, sizeof(rule));
1295 result = rte_acl_ipv4vlan_add_rules(NULL, &rule, 1);
1297 printf("Line %i: Adding rules with NULL ACL context "
1298 "should have failed!\n", __LINE__);
1304 result = rte_acl_ipv4vlan_add_rules(acx, NULL, 1);
1306 printf("Line %i: Adding NULL rule to ACL context "
1307 "should have failed!\n", __LINE__);
1312 /* zero count (should succeed) */
1313 result = rte_acl_ipv4vlan_add_rules(acx, &rule, 0);
1315 printf("Line %i: Adding 0 rules to ACL context failed!\n",
1321 /* free ACL context */
1324 /* set wrong rule_size so that adding any rules would fail */
1325 param.rule_size = RTE_ACL_IPV4VLAN_RULE_SZ + 4;
1326 acx = rte_acl_create(¶m);
1328 printf("Line %i: ACL context creation failed!\n", __LINE__);
1332 /* try adding a rule with size different from context rule_size */
1333 result = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
1335 printf("Line %i: Adding an invalid sized rule "
1336 "should have failed!\n", __LINE__);
1341 /* free ACL context */
1346 * rte_acl_ipv4vlan_build
1349 /* reinitialize context */
1350 memcpy(¶m, &acl_param, sizeof(param));
1351 acx = rte_acl_create(¶m);
1353 printf("Line %i: ACL context creation failed!\n", __LINE__);
1358 result = rte_acl_ipv4vlan_build(NULL, layout, 1);
1360 printf("Line %i: Building with NULL context "
1361 "should have failed!\n", __LINE__);
1367 result = rte_acl_ipv4vlan_build(acx, NULL, 1);
1369 printf("Line %i: Building with NULL layout "
1370 "should have failed!\n", __LINE__);
1375 /* zero categories (should not fail) */
1376 result = rte_acl_ipv4vlan_build(acx, layout, 0);
1378 printf("Line %i: Building with 0 categories should fail!\n",
1384 /* SSE classify test */
1386 /* cover zero categories in classify (should not fail) */
1387 result = rte_acl_classify(acx, NULL, NULL, 0, 0);
1389 printf("Line %i: SSE classify with zero categories "
1390 "failed!\n", __LINE__);
1395 /* cover invalid but positive categories in classify */
1396 result = rte_acl_classify(acx, NULL, NULL, 0, 3);
1398 printf("Line %i: SSE classify with 3 categories "
1399 "should have failed!\n", __LINE__);
1404 /* scalar classify test */
1406 /* cover zero categories in classify (should not fail) */
1407 result = rte_acl_classify_alg(acx, NULL, NULL, 0, 0,
1408 RTE_ACL_CLASSIFY_SCALAR);
1410 printf("Line %i: Scalar classify with zero categories "
1411 "failed!\n", __LINE__);
1416 /* cover invalid but positive categories in classify */
1417 result = rte_acl_classify(acx, NULL, NULL, 0, 3);
1419 printf("Line %i: Scalar classify with 3 categories "
1420 "should have failed!\n", __LINE__);
1425 /* free ACL context */
1430 * make sure void functions don't crash with NULL parameters
1441 * Various tests that don't test much but improve coverage
1446 struct rte_acl_param param;
1447 struct rte_acl_ctx *acx;
1449 /* create context */
1450 memcpy(¶m, &acl_param, sizeof(param));
1452 acx = rte_acl_create(¶m);
1454 printf("Line %i: Error creating ACL context!\n", __LINE__);
1458 /* dump context with rules - useful for coverage */
1459 rte_acl_list_dump();
1471 if (test_invalid_parameters() < 0)
1473 if (test_invalid_rules() < 0)
1475 if (test_create_find_add() < 0)
1477 if (test_invalid_layout() < 0)
1479 if (test_misc() < 0)
1481 if (test_classify() < 0)
1483 if (test_build_ports_range() < 0)
1485 if (test_convert() < 0)
1491 static struct test_command acl_cmd = {
1492 .command = "acl_autotest",
1493 .callback = test_acl,
1495 REGISTER_TEST_COMMAND(acl_cmd);