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 struct rte_acl_param acl_param = {
52 .socket_id = SOCKET_ID_ANY,
53 .rule_size = RTE_ACL_IPV4VLAN_RULE_SZ,
54 .max_rule_num = 0x30000,
57 struct rte_acl_ipv4vlan_rule acl_rule = {
58 .data = { .priority = 1, .category_mask = 0xff },
60 .src_port_high = UINT16_MAX,
62 .dst_port_high = UINT16_MAX,
65 /* byteswap to cpu or network order */
67 bswap_test_data(struct ipv4_7tuple *data, int len, int to_be)
71 for (i = 0; i < len; i++) {
74 /* swap all bytes so that they are in network order */
75 data[i].ip_dst = rte_cpu_to_be_32(data[i].ip_dst);
76 data[i].ip_src = rte_cpu_to_be_32(data[i].ip_src);
77 data[i].port_dst = rte_cpu_to_be_16(data[i].port_dst);
78 data[i].port_src = rte_cpu_to_be_16(data[i].port_src);
79 data[i].vlan = rte_cpu_to_be_16(data[i].vlan);
80 data[i].domain = rte_cpu_to_be_16(data[i].domain);
82 data[i].ip_dst = rte_be_to_cpu_32(data[i].ip_dst);
83 data[i].ip_src = rte_be_to_cpu_32(data[i].ip_src);
84 data[i].port_dst = rte_be_to_cpu_16(data[i].port_dst);
85 data[i].port_src = rte_be_to_cpu_16(data[i].port_src);
86 data[i].vlan = rte_be_to_cpu_16(data[i].vlan);
87 data[i].domain = rte_be_to_cpu_16(data[i].domain);
93 * Test scalar and SSE ACL lookup.
96 test_classify_run(struct rte_acl_ctx *acx)
99 uint32_t result, count;
100 uint32_t results[RTE_DIM(acl_test_data) * RTE_ACL_MAX_CATEGORIES];
101 const uint8_t *data[RTE_DIM(acl_test_data)];
103 /* swap all bytes in the data to network order */
104 bswap_test_data(acl_test_data, RTE_DIM(acl_test_data), 1);
106 /* store pointers to test data */
107 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++)
108 data[i] = (uint8_t *)&acl_test_data[i];
111 * these will run quite a few times, it's necessary to test code paths
112 * from num=0 to num>8
114 for (count = 0; count <= RTE_DIM(acl_test_data); count++) {
115 ret = rte_acl_classify(acx, data, results,
116 count, RTE_ACL_MAX_CATEGORIES);
118 printf("Line %i: SSE classify failed!\n", __LINE__);
122 /* check if we allow everything we should allow */
123 for (i = 0; i < (int) count; i++) {
125 results[i * RTE_ACL_MAX_CATEGORIES + ACL_ALLOW];
126 if (result != acl_test_data[i].allow) {
127 printf("Line %i: Error in allow results at %i "
128 "(expected %"PRIu32" got %"PRIu32")!\n",
129 __LINE__, i, acl_test_data[i].allow,
136 /* check if we deny everything we should deny */
137 for (i = 0; i < (int) count; i++) {
138 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_DENY];
139 if (result != acl_test_data[i].deny) {
140 printf("Line %i: Error in deny results at %i "
141 "(expected %"PRIu32" got %"PRIu32")!\n",
142 __LINE__, i, acl_test_data[i].deny,
150 /* make a quick check for scalar */
151 ret = rte_acl_classify_alg(acx, data, results,
152 RTE_DIM(acl_test_data), RTE_ACL_MAX_CATEGORIES,
153 RTE_ACL_CLASSIFY_SCALAR);
155 printf("Line %i: scalar classify failed!\n", __LINE__);
159 /* check if we allow everything we should allow */
160 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++) {
161 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_ALLOW];
162 if (result != acl_test_data[i].allow) {
163 printf("Line %i: Error in allow results at %i "
164 "(expected %"PRIu32" got %"PRIu32")!\n",
165 __LINE__, i, acl_test_data[i].allow,
172 /* check if we deny everything we should deny */
173 for (i = 0; i < (int) RTE_DIM(acl_test_data); i++) {
174 result = results[i * RTE_ACL_MAX_CATEGORIES + ACL_DENY];
175 if (result != acl_test_data[i].deny) {
176 printf("Line %i: Error in deny results at %i "
177 "(expected %"PRIu32" got %"PRIu32")!\n",
178 __LINE__, i, acl_test_data[i].deny,
188 /* swap data back to cpu order so that next time tests don't fail */
189 bswap_test_data(acl_test_data, RTE_DIM(acl_test_data), 0);
194 test_classify_buid(struct rte_acl_ctx *acx)
197 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
198 offsetof(struct ipv4_7tuple, proto),
199 offsetof(struct ipv4_7tuple, vlan),
200 offsetof(struct ipv4_7tuple, ip_src),
201 offsetof(struct ipv4_7tuple, ip_dst),
202 offsetof(struct ipv4_7tuple, port_src),
205 /* add rules to the context */
206 ret = rte_acl_ipv4vlan_add_rules(acx, acl_test_rules,
207 RTE_DIM(acl_test_rules));
209 printf("Line %i: Adding rules to ACL context failed!\n",
214 /* try building the context */
215 ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
217 printf("Line %i: Building ACL context failed!\n", __LINE__);
224 #define TEST_CLASSIFY_ITER 4
227 * Test scalar and SSE ACL lookup.
232 struct rte_acl_ctx *acx;
235 acx = rte_acl_create(&acl_param);
237 printf("Line %i: Error creating ACL context!\n", __LINE__);
242 for (i = 0; i != TEST_CLASSIFY_ITER; i++) {
247 rte_acl_reset_rules(acx);
249 ret = test_classify_buid(acx);
251 printf("Line %i, iter: %d: "
252 "Adding rules to ACL context failed!\n",
257 ret = test_classify_run(acx);
259 printf("Line %i, iter: %d: %s failed!\n",
260 __LINE__, i, __func__);
264 /* reset rules and make sure that classify still works ok. */
265 rte_acl_reset_rules(acx);
266 ret = test_classify_run(acx);
268 printf("Line %i, iter: %d: %s failed!\n",
269 __LINE__, i, __func__);
279 * Test wrong layout behavior
280 * This test supplies the ACL context with invalid layout, which results in
281 * ACL matching the wrong stuff. However, it should match the wrong stuff
282 * the right way. We switch around source and destination addresses,
283 * source and destination ports, and protocol will point to first byte of
287 test_invalid_layout(void)
289 struct rte_acl_ctx *acx;
292 uint32_t results[RTE_DIM(invalid_layout_data)];
293 const uint8_t *data[RTE_DIM(invalid_layout_data)];
295 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
296 /* proto points to destination port's first byte */
297 offsetof(struct ipv4_7tuple, port_dst),
299 0, /* VLAN not used */
301 /* src and dst addresses are swapped */
302 offsetof(struct ipv4_7tuple, ip_dst),
303 offsetof(struct ipv4_7tuple, ip_src),
306 * we can't swap ports here, so we will swap
309 offsetof(struct ipv4_7tuple, port_src),
312 acx = rte_acl_create(&acl_param);
314 printf("Line %i: Error creating ACL context!\n", __LINE__);
318 /* putting a lot of rules into the context results in greater
319 * coverage numbers. it doesn't matter if they are identical */
320 for (i = 0; i < 1000; i++) {
321 /* add rules to the context */
322 ret = rte_acl_ipv4vlan_add_rules(acx, invalid_layout_rules,
323 RTE_DIM(invalid_layout_rules));
325 printf("Line %i: Adding rules to ACL context failed!\n",
332 /* try building the context */
333 ret = rte_acl_ipv4vlan_build(acx, layout, 1);
335 printf("Line %i: Building ACL context failed!\n", __LINE__);
340 /* swap all bytes in the data to network order */
341 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 1);
344 for (i = 0; i < (int) RTE_DIM(invalid_layout_data); i++) {
345 data[i] = (uint8_t *)&invalid_layout_data[i];
348 /* classify tuples */
349 ret = rte_acl_classify_alg(acx, data, results,
350 RTE_DIM(results), 1, RTE_ACL_CLASSIFY_SCALAR);
352 printf("Line %i: SSE classify failed!\n", __LINE__);
357 for (i = 0; i < (int) RTE_DIM(results); i++) {
358 if (results[i] != invalid_layout_data[i].allow) {
359 printf("Line %i: Wrong results at %i "
360 "(result=%u, should be %u)!\n",
361 __LINE__, i, results[i],
362 invalid_layout_data[i].allow);
367 /* classify tuples (scalar) */
368 ret = rte_acl_classify_alg(acx, data, results, RTE_DIM(results), 1,
369 RTE_ACL_CLASSIFY_SCALAR);
372 printf("Line %i: Scalar classify failed!\n", __LINE__);
377 for (i = 0; i < (int) RTE_DIM(results); i++) {
378 if (results[i] != invalid_layout_data[i].allow) {
379 printf("Line %i: Wrong results at %i "
380 "(result=%u, should be %u)!\n",
381 __LINE__, i, results[i],
382 invalid_layout_data[i].allow);
389 /* swap data back to cpu order so that next time tests don't fail */
390 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
395 /* swap data back to cpu order so that next time tests don't fail */
396 bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
404 * Test creating and finding ACL contexts, and adding rules
407 test_create_find_add(void)
409 struct rte_acl_param param;
410 struct rte_acl_ctx *acx, *acx2, *tmp;
411 struct rte_acl_ipv4vlan_rule rules[LEN];
413 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
415 const char *acx_name = "acx";
416 const char *acx2_name = "acx2";
419 /* create two contexts */
420 memcpy(¶m, &acl_param, sizeof(param));
421 param.max_rule_num = 2;
423 param.name = acx_name;
424 acx = rte_acl_create(¶m);
426 printf("Line %i: Error creating %s!\n", __LINE__, acx_name);
430 param.name = acx2_name;
431 acx2 = rte_acl_create(¶m);
432 if (acx2 == NULL || acx2 == acx) {
433 printf("Line %i: Error creating %s!\n", __LINE__, acx2_name);
438 /* try to create third one, with an existing name */
439 param.name = acx_name;
440 tmp = rte_acl_create(¶m);
442 printf("Line %i: Creating context with existing name "
450 param.name = acx2_name;
451 tmp = rte_acl_create(¶m);
453 printf("Line %i: Creating context with existing "
454 "name test 2 failed!\n",
461 /* try to find existing ACL contexts */
462 tmp = rte_acl_find_existing(acx_name);
464 printf("Line %i: Finding %s failed!\n", __LINE__, acx_name);
470 tmp = rte_acl_find_existing(acx2_name);
472 printf("Line %i: Finding %s failed!\n", __LINE__, acx2_name);
478 /* try to find non-existing context */
479 tmp = rte_acl_find_existing("invalid");
481 printf("Line %i: Non-existent ACL context found!\n", __LINE__);
489 /* create valid (but severely limited) acx */
490 memcpy(¶m, &acl_param, sizeof(param));
491 param.max_rule_num = LEN;
493 acx = rte_acl_create(¶m);
495 printf("Line %i: Error creating %s!\n", __LINE__, param.name);
499 /* create dummy acl */
500 for (i = 0; i < LEN; i++) {
501 memcpy(&rules[i], &acl_rule,
502 sizeof(struct rte_acl_ipv4vlan_rule));
504 rules[i].data.userdata = i + 1;
505 /* one rule per category */
506 rules[i].data.category_mask = 1 << i;
509 /* try filling up the context */
510 ret = rte_acl_ipv4vlan_add_rules(acx, rules, LEN);
512 printf("Line %i: Adding %i rules to ACL context failed!\n",
517 /* try adding to a (supposedly) full context */
518 ret = rte_acl_ipv4vlan_add_rules(acx, rules, 1);
520 printf("Line %i: Adding rules to full ACL context should"
521 "have failed!\n", __LINE__);
525 /* try building the context */
526 ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
528 printf("Line %i: Building ACL context failed!\n", __LINE__);
543 * test various invalid rules
546 test_invalid_rules(void)
548 struct rte_acl_ctx *acx;
551 struct rte_acl_ipv4vlan_rule rule;
553 acx = rte_acl_create(&acl_param);
555 printf("Line %i: Error creating ACL context!\n", __LINE__);
559 /* test inverted high/low source and destination ports.
560 * originally, there was a problem with memory consumption when using
563 /* create dummy acl */
564 memcpy(&rule, &acl_rule, sizeof(struct rte_acl_ipv4vlan_rule));
565 rule.data.userdata = 1;
566 rule.dst_port_low = 0xfff0;
567 rule.dst_port_high = 0x0010;
569 /* add rules to context and try to build it */
570 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
572 printf("Line %i: Adding rules to ACL context "
573 "should have failed!\n", __LINE__);
577 rule.dst_port_low = 0x0;
578 rule.dst_port_high = 0xffff;
579 rule.src_port_low = 0xfff0;
580 rule.src_port_high = 0x0010;
582 /* add rules to context and try to build it */
583 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
585 printf("Line %i: Adding rules to ACL context "
586 "should have failed!\n", __LINE__);
590 rule.dst_port_low = 0x0;
591 rule.dst_port_high = 0xffff;
592 rule.src_port_low = 0x0;
593 rule.src_port_high = 0xffff;
595 rule.dst_mask_len = 33;
597 /* add rules to context and try to build it */
598 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
600 printf("Line %i: Adding rules to ACL context "
601 "should have failed!\n", __LINE__);
605 rule.dst_mask_len = 0;
606 rule.src_mask_len = 33;
608 /* add rules to context and try to build it */
609 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
611 printf("Line %i: Adding rules to ACL context "
612 "should have failed!\n", __LINE__);
616 rule.dst_mask_len = 0;
617 rule.src_mask_len = 0;
618 rule.data.userdata = 0;
620 /* try adding this rule (it should fail because userdata is invalid) */
621 ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
623 printf("Line %i: Adding a rule with invalid user data "
624 "should have failed!\n", __LINE__);
640 * test functions by passing invalid or
641 * non-workable parameters.
643 * we do very limited testing of classify functions here
644 * because those are performance-critical and
645 * thus don't do much parameter checking.
648 test_invalid_parameters(void)
650 struct rte_acl_param param;
651 struct rte_acl_ctx *acx;
652 struct rte_acl_ipv4vlan_rule rule;
655 uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
663 acx = rte_acl_create(NULL);
665 printf("Line %i: ACL context creation with NULL param "
666 "should have failed!\n", __LINE__);
672 memcpy(¶m, &acl_param, sizeof(param));
675 acx = rte_acl_create(¶m);
677 printf("Line %i: ACL context creation with zero rule len "
678 "failed!\n", __LINE__);
683 /* zero max rule num */
684 memcpy(¶m, &acl_param, sizeof(param));
685 param.max_rule_num = 0;
687 acx = rte_acl_create(¶m);
689 printf("Line %i: ACL context creation with zero rule num "
690 "failed!\n", __LINE__);
695 /* invalid NUMA node */
696 memcpy(¶m, &acl_param, sizeof(param));
697 param.socket_id = RTE_MAX_NUMA_NODES + 1;
699 acx = rte_acl_create(¶m);
701 printf("Line %i: ACL context creation with invalid NUMA "
702 "should have failed!\n", __LINE__);
708 memcpy(¶m, &acl_param, sizeof(param));
711 acx = rte_acl_create(¶m);
713 printf("Line %i: ACL context creation with NULL name "
714 "should have failed!\n", __LINE__);
720 * rte_acl_find_existing
723 acx = rte_acl_find_existing(NULL);
725 printf("Line %i: NULL ACL context found!\n", __LINE__);
731 * rte_acl_ipv4vlan_add_rules
734 /* initialize everything */
735 memcpy(¶m, &acl_param, sizeof(param));
736 acx = rte_acl_create(¶m);
738 printf("Line %i: ACL context creation failed!\n", __LINE__);
742 memcpy(&rule, &acl_rule, sizeof(rule));
745 result = rte_acl_ipv4vlan_add_rules(NULL, &rule, 1);
747 printf("Line %i: Adding rules with NULL ACL context "
748 "should have failed!\n", __LINE__);
754 result = rte_acl_ipv4vlan_add_rules(acx, NULL, 1);
756 printf("Line %i: Adding NULL rule to ACL context "
757 "should have failed!\n", __LINE__);
762 /* zero count (should succeed) */
763 result = rte_acl_ipv4vlan_add_rules(acx, &rule, 0);
765 printf("Line %i: Adding 0 rules to ACL context failed!\n",
771 /* free ACL context */
774 /* set wrong rule_size so that adding any rules would fail */
775 param.rule_size = RTE_ACL_IPV4VLAN_RULE_SZ + 4;
776 acx = rte_acl_create(¶m);
778 printf("Line %i: ACL context creation failed!\n", __LINE__);
782 /* try adding a rule with size different from context rule_size */
783 result = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
785 printf("Line %i: Adding an invalid sized rule "
786 "should have failed!\n", __LINE__);
791 /* free ACL context */
796 * rte_acl_ipv4vlan_build
799 /* reinitialize context */
800 memcpy(¶m, &acl_param, sizeof(param));
801 acx = rte_acl_create(¶m);
803 printf("Line %i: ACL context creation failed!\n", __LINE__);
808 result = rte_acl_ipv4vlan_build(NULL, layout, 1);
810 printf("Line %i: Building with NULL context "
811 "should have failed!\n", __LINE__);
817 result = rte_acl_ipv4vlan_build(acx, NULL, 1);
819 printf("Line %i: Building with NULL layout "
820 "should have failed!\n", __LINE__);
825 /* zero categories (should not fail) */
826 result = rte_acl_ipv4vlan_build(acx, layout, 0);
828 printf("Line %i: Building with 0 categories should fail!\n",
834 /* SSE classify test */
836 /* cover zero categories in classify (should not fail) */
837 result = rte_acl_classify(acx, NULL, NULL, 0, 0);
839 printf("Line %i: SSE classify with zero categories "
840 "failed!\n", __LINE__);
845 /* cover invalid but positive categories in classify */
846 result = rte_acl_classify(acx, NULL, NULL, 0, 3);
848 printf("Line %i: SSE classify with 3 categories "
849 "should have failed!\n", __LINE__);
854 /* scalar classify test */
856 /* cover zero categories in classify (should not fail) */
857 result = rte_acl_classify_alg(acx, NULL, NULL, 0, 0,
858 RTE_ACL_CLASSIFY_SCALAR);
860 printf("Line %i: Scalar classify with zero categories "
861 "failed!\n", __LINE__);
866 /* cover invalid but positive categories in classify */
867 result = rte_acl_classify(acx, NULL, NULL, 0, 3);
869 printf("Line %i: Scalar classify with 3 categories "
870 "should have failed!\n", __LINE__);
875 /* free ACL context */
880 * make sure void functions don't crash with NULL parameters
891 * Various tests that don't test much but improve coverage
896 struct rte_acl_param param;
897 struct rte_acl_ctx *acx;
900 memcpy(¶m, &acl_param, sizeof(param));
902 acx = rte_acl_create(¶m);
904 printf("Line %i: Error creating ACL context!\n", __LINE__);
908 /* dump context with rules - useful for coverage */
921 if (test_invalid_parameters() < 0)
923 if (test_invalid_rules() < 0)
925 if (test_create_find_add() < 0)
927 if (test_invalid_layout() < 0)
931 if (test_classify() < 0)
937 static struct test_command acl_cmd = {
938 .command = "acl_autotest",
939 .callback = test_acl,
941 REGISTER_TEST_COMMAND(acl_cmd);