acl: add new test case for ranges build
[dpdk.git] / app / test / test_acl.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <errno.h>
36
37 #include "test.h"
38
39 #include <rte_string_fns.h>
40 #include <rte_mbuf.h>
41 #include <rte_byteorder.h>
42 #include <rte_ip.h>
43 #include <rte_acl.h>
44 #include <rte_common.h>
45
46 #include "test_acl.h"
47
48 #define LEN RTE_ACL_MAX_CATEGORIES
49
50 struct rte_acl_param acl_param = {
51         .name = "acl_ctx",
52         .socket_id = SOCKET_ID_ANY,
53         .rule_size = RTE_ACL_IPV4VLAN_RULE_SZ,
54         .max_rule_num = 0x30000,
55 };
56
57 struct rte_acl_ipv4vlan_rule acl_rule = {
58                 .data = { .priority = 1, .category_mask = 0xff },
59                 .src_port_low = 0,
60                 .src_port_high = UINT16_MAX,
61                 .dst_port_low = 0,
62                 .dst_port_high = UINT16_MAX,
63 };
64
65 /* byteswap to cpu or network order */
66 static void
67 bswap_test_data(struct ipv4_7tuple *data, int len, int to_be)
68 {
69         int i;
70
71         for (i = 0; i < len; i++) {
72
73                 if (to_be) {
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);
81                 } else {
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);
88                 }
89         }
90 }
91
92 /*
93  * Test scalar and SSE ACL lookup.
94  */
95 static int
96 test_classify_run(struct rte_acl_ctx *acx)
97 {
98         int ret, i;
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)];
102
103         /* swap all bytes in the data to network order */
104         bswap_test_data(acl_test_data, RTE_DIM(acl_test_data), 1);
105
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];
109
110         /**
111          * these will run quite a few times, it's necessary to test code paths
112          * from num=0 to num>8
113          */
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);
117                 if (ret != 0) {
118                         printf("Line %i: SSE classify failed!\n", __LINE__);
119                         goto err;
120                 }
121
122                 /* check if we allow everything we should allow */
123                 for (i = 0; i < (int) count; i++) {
124                         result =
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,
130                                         result);
131                                 ret = -EINVAL;
132                                 goto err;
133                         }
134                 }
135
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,
143                                         result);
144                                 ret = -EINVAL;
145                                 goto err;
146                         }
147                 }
148         }
149
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);
154         if (ret != 0) {
155                 printf("Line %i: scalar classify failed!\n", __LINE__);
156                 goto err;
157         }
158
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,
166                                         result);
167                         ret = -EINVAL;
168                         goto err;
169                 }
170         }
171
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,
179                                         result);
180                         ret = -EINVAL;
181                         goto err;
182                 }
183         }
184
185         ret = 0;
186
187 err:
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);
190         return ret;
191 }
192
193 static int
194 test_classify_buid(struct rte_acl_ctx *acx,
195         const struct rte_acl_ipv4vlan_rule *rules, uint32_t num)
196 {
197         int ret;
198         const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
199                         offsetof(struct ipv4_7tuple, proto),
200                         offsetof(struct ipv4_7tuple, vlan),
201                         offsetof(struct ipv4_7tuple, ip_src),
202                         offsetof(struct ipv4_7tuple, ip_dst),
203                         offsetof(struct ipv4_7tuple, port_src),
204         };
205
206         /* add rules to the context */
207         ret = rte_acl_ipv4vlan_add_rules(acx, rules, num);
208         if (ret != 0) {
209                 printf("Line %i: Adding rules to ACL context failed!\n",
210                         __LINE__);
211                 return ret;
212         }
213
214         /* try building the context */
215         ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
216         if (ret != 0) {
217                 printf("Line %i: Building ACL context failed!\n", __LINE__);
218                 return ret;
219         }
220
221         return 0;
222 }
223
224 #define TEST_CLASSIFY_ITER      4
225
226 /*
227  * Test scalar and SSE ACL lookup.
228  */
229 static int
230 test_classify(void)
231 {
232         struct rte_acl_ctx *acx;
233         int i, ret;
234
235         acx = rte_acl_create(&acl_param);
236         if (acx == NULL) {
237                 printf("Line %i: Error creating ACL context!\n", __LINE__);
238                 return -1;
239         }
240
241         ret = 0;
242         for (i = 0; i != TEST_CLASSIFY_ITER; i++) {
243
244                 if ((i & 1) == 0)
245                         rte_acl_reset(acx);
246                 else
247                         rte_acl_reset_rules(acx);
248
249                 ret = test_classify_buid(acx, acl_test_rules,
250                         RTE_DIM(acl_test_rules));
251                 if (ret != 0) {
252                         printf("Line %i, iter: %d: "
253                                 "Adding rules to ACL context failed!\n",
254                                 __LINE__, i);
255                         break;
256                 }
257
258                 ret = test_classify_run(acx);
259                 if (ret != 0) {
260                         printf("Line %i, iter: %d: %s failed!\n",
261                                 __LINE__, i, __func__);
262                         break;
263                 }
264
265                 /* reset rules and make sure that classify still works ok. */
266                 rte_acl_reset_rules(acx);
267                 ret = test_classify_run(acx);
268                 if (ret != 0) {
269                         printf("Line %i, iter: %d: %s failed!\n",
270                                 __LINE__, i, __func__);
271                         break;
272                 }
273         }
274
275         rte_acl_free(acx);
276         return ret;
277 }
278
279 static int
280 test_build_ports_range(void)
281 {
282         static const struct rte_acl_ipv4vlan_rule test_rules[] = {
283                 {
284                         /* match all packets. */
285                         .data = {
286                                 .userdata = 1,
287                                 .category_mask = ACL_ALLOW_MASK,
288                                 .priority = 101,
289                         },
290                         .src_port_low = 0,
291                         .src_port_high = UINT16_MAX,
292                         .dst_port_low = 0,
293                         .dst_port_high = UINT16_MAX,
294                 },
295                 {
296                         /* match all packets with dst ports [54-65280]. */
297                         .data = {
298                                 .userdata = 2,
299                                 .category_mask = ACL_ALLOW_MASK,
300                                 .priority = 102,
301                         },
302                         .src_port_low = 0,
303                         .src_port_high = UINT16_MAX,
304                         .dst_port_low = 54,
305                         .dst_port_high = 65280,
306                 },
307                 {
308                         /* match all packets with dst ports [0-52]. */
309                         .data = {
310                                 .userdata = 3,
311                                 .category_mask = ACL_ALLOW_MASK,
312                                 .priority = 103,
313                         },
314                         .src_port_low = 0,
315                         .src_port_high = UINT16_MAX,
316                         .dst_port_low = 0,
317                         .dst_port_high = 52,
318                 },
319                 {
320                         /* match all packets with dst ports [53]. */
321                         .data = {
322                                 .userdata = 4,
323                                 .category_mask = ACL_ALLOW_MASK,
324                                 .priority = 99,
325                         },
326                         .src_port_low = 0,
327                         .src_port_high = UINT16_MAX,
328                         .dst_port_low = 53,
329                         .dst_port_high = 53,
330                 },
331                 {
332                         /* match all packets with dst ports [65279-65535]. */
333                         .data = {
334                                 .userdata = 5,
335                                 .category_mask = ACL_ALLOW_MASK,
336                                 .priority = 98,
337                         },
338                         .src_port_low = 0,
339                         .src_port_high = UINT16_MAX,
340                         .dst_port_low = 65279,
341                         .dst_port_high = UINT16_MAX,
342                 },
343         };
344
345         static struct ipv4_7tuple test_data[] = {
346                 {
347                         .proto = 6,
348                         .ip_src = IPv4(10, 1, 1, 1),
349                         .ip_dst = IPv4(192, 168, 0, 33),
350                         .port_dst = 53,
351                         .allow = 1,
352                 },
353                 {
354                         .proto = 6,
355                         .ip_src = IPv4(127, 84, 33, 1),
356                         .ip_dst = IPv4(1, 2, 3, 4),
357                         .port_dst = 65281,
358                         .allow = 1,
359                 },
360         };
361
362         struct rte_acl_ctx *acx;
363         int32_t ret, i, j;
364         uint32_t results[RTE_DIM(test_data)];
365         const uint8_t *data[RTE_DIM(test_data)];
366
367         acx = rte_acl_create(&acl_param);
368         if (acx == NULL) {
369                 printf("Line %i: Error creating ACL context!\n", __LINE__);
370                 return -1;
371         }
372
373         /* swap all bytes in the data to network order */
374         bswap_test_data(test_data, RTE_DIM(test_data), 1);
375
376         /* store pointers to test data */
377         for (i = 0; i != RTE_DIM(test_data); i++)
378                 data[i] = (uint8_t *)&test_data[i];
379
380         for (i = 0; i != RTE_DIM(test_rules); i++) {
381                 rte_acl_reset(acx);
382                 ret = test_classify_buid(acx, test_rules, i + 1);
383                 if (ret != 0) {
384                         printf("Line %i, iter: %d: "
385                                 "Adding rules to ACL context failed!\n",
386                                 __LINE__, i);
387                         break;
388                 }
389                 ret = rte_acl_classify(acx, data, results,
390                         RTE_DIM(data), 1);
391                 if (ret != 0) {
392                         printf("Line %i, iter: %d: classify failed!\n",
393                                 __LINE__, i);
394                         break;
395                 }
396
397                 /* check results */
398                 for (j = 0; j != RTE_DIM(results); j++) {
399                         if (results[j] != test_data[j].allow) {
400                                 printf("Line %i: Error in allow results at %i "
401                                         "(expected %"PRIu32" got %"PRIu32")!\n",
402                                         __LINE__, j, test_data[j].allow,
403                                         results[j]);
404                                 ret = -EINVAL;
405                         }
406                 }
407         }
408
409         bswap_test_data(test_data, RTE_DIM(test_data), 0);
410
411         rte_acl_free(acx);
412         return ret;
413 }
414
415 /*
416  * Test wrong layout behavior
417  * This test supplies the ACL context with invalid layout, which results in
418  * ACL matching the wrong stuff. However, it should match the wrong stuff
419  * the right way. We switch around source and destination addresses,
420  * source and destination ports, and protocol will point to first byte of
421  * destination port.
422  */
423 static int
424 test_invalid_layout(void)
425 {
426         struct rte_acl_ctx *acx;
427         int ret, i;
428
429         uint32_t results[RTE_DIM(invalid_layout_data)];
430         const uint8_t *data[RTE_DIM(invalid_layout_data)];
431
432         const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {
433                         /* proto points to destination port's first byte */
434                         offsetof(struct ipv4_7tuple, port_dst),
435
436                         0, /* VLAN not used */
437
438                         /* src and dst addresses are swapped */
439                         offsetof(struct ipv4_7tuple, ip_dst),
440                         offsetof(struct ipv4_7tuple, ip_src),
441
442                         /*
443                          * we can't swap ports here, so we will swap
444                          * them in the data
445                          */
446                         offsetof(struct ipv4_7tuple, port_src),
447         };
448
449         acx = rte_acl_create(&acl_param);
450         if (acx == NULL) {
451                 printf("Line %i: Error creating ACL context!\n", __LINE__);
452                 return -1;
453         }
454
455         /* putting a lot of rules into the context results in greater
456          * coverage numbers. it doesn't matter if they are identical */
457         for (i = 0; i < 1000; i++) {
458                 /* add rules to the context */
459                 ret = rte_acl_ipv4vlan_add_rules(acx, invalid_layout_rules,
460                                 RTE_DIM(invalid_layout_rules));
461                 if (ret != 0) {
462                         printf("Line %i: Adding rules to ACL context failed!\n",
463                                 __LINE__);
464                         rte_acl_free(acx);
465                         return -1;
466                 }
467         }
468
469         /* try building the context */
470         ret = rte_acl_ipv4vlan_build(acx, layout, 1);
471         if (ret != 0) {
472                 printf("Line %i: Building ACL context failed!\n", __LINE__);
473                 rte_acl_free(acx);
474                 return -1;
475         }
476
477         /* swap all bytes in the data to network order */
478         bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 1);
479
480         /* prepare data */
481         for (i = 0; i < (int) RTE_DIM(invalid_layout_data); i++) {
482                 data[i] = (uint8_t *)&invalid_layout_data[i];
483         }
484
485         /* classify tuples */
486         ret = rte_acl_classify_alg(acx, data, results,
487                         RTE_DIM(results), 1, RTE_ACL_CLASSIFY_SCALAR);
488         if (ret != 0) {
489                 printf("Line %i: SSE classify failed!\n", __LINE__);
490                 rte_acl_free(acx);
491                 return -1;
492         }
493
494         for (i = 0; i < (int) RTE_DIM(results); i++) {
495                 if (results[i] != invalid_layout_data[i].allow) {
496                         printf("Line %i: Wrong results at %i "
497                                 "(result=%u, should be %u)!\n",
498                                 __LINE__, i, results[i],
499                                 invalid_layout_data[i].allow);
500                         goto err;
501                 }
502         }
503
504         /* classify tuples (scalar) */
505         ret = rte_acl_classify_alg(acx, data, results, RTE_DIM(results), 1,
506                 RTE_ACL_CLASSIFY_SCALAR);
507
508         if (ret != 0) {
509                 printf("Line %i: Scalar classify failed!\n", __LINE__);
510                 rte_acl_free(acx);
511                 return -1;
512         }
513
514         for (i = 0; i < (int) RTE_DIM(results); i++) {
515                 if (results[i] != invalid_layout_data[i].allow) {
516                         printf("Line %i: Wrong results at %i "
517                                 "(result=%u, should be %u)!\n",
518                                 __LINE__, i, results[i],
519                                 invalid_layout_data[i].allow);
520                         goto err;
521                 }
522         }
523
524         rte_acl_free(acx);
525
526         /* swap data back to cpu order so that next time tests don't fail */
527         bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
528
529         return 0;
530 err:
531
532         /* swap data back to cpu order so that next time tests don't fail */
533         bswap_test_data(invalid_layout_data, RTE_DIM(invalid_layout_data), 0);
534
535         rte_acl_free(acx);
536
537         return -1;
538 }
539
540 /*
541  * Test creating and finding ACL contexts, and adding rules
542  */
543 static int
544 test_create_find_add(void)
545 {
546         struct rte_acl_param param;
547         struct rte_acl_ctx *acx, *acx2, *tmp;
548         struct rte_acl_ipv4vlan_rule rules[LEN];
549
550         const uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
551
552         const char *acx_name = "acx";
553         const char *acx2_name = "acx2";
554         int i, ret;
555
556         /* create two contexts */
557         memcpy(&param, &acl_param, sizeof(param));
558         param.max_rule_num = 2;
559
560         param.name = acx_name;
561         acx = rte_acl_create(&param);
562         if (acx == NULL) {
563                 printf("Line %i: Error creating %s!\n", __LINE__, acx_name);
564                 return -1;
565         }
566
567         param.name = acx2_name;
568         acx2 = rte_acl_create(&param);
569         if (acx2 == NULL || acx2 == acx) {
570                 printf("Line %i: Error creating %s!\n", __LINE__, acx2_name);
571                 rte_acl_free(acx);
572                 return -1;
573         }
574
575         /* try to create third one, with an existing name */
576         param.name = acx_name;
577         tmp = rte_acl_create(&param);
578         if (tmp != acx) {
579                 printf("Line %i: Creating context with existing name "
580                         "test failed!\n",
581                         __LINE__);
582                 if (tmp)
583                         rte_acl_free(tmp);
584                 goto err;
585         }
586
587         param.name = acx2_name;
588         tmp = rte_acl_create(&param);
589         if (tmp != acx2) {
590                 printf("Line %i: Creating context with existing "
591                         "name test 2 failed!\n",
592                         __LINE__);
593                 if (tmp)
594                         rte_acl_free(tmp);
595                 goto err;
596         }
597
598         /* try to find existing ACL contexts */
599         tmp = rte_acl_find_existing(acx_name);
600         if (tmp != acx) {
601                 printf("Line %i: Finding %s failed!\n", __LINE__, acx_name);
602                 if (tmp)
603                         rte_acl_free(tmp);
604                 goto err;
605         }
606
607         tmp = rte_acl_find_existing(acx2_name);
608         if (tmp != acx2) {
609                 printf("Line %i: Finding %s failed!\n", __LINE__, acx2_name);
610                 if (tmp)
611                         rte_acl_free(tmp);
612                 goto err;
613         }
614
615         /* try to find non-existing context */
616         tmp = rte_acl_find_existing("invalid");
617         if (tmp != NULL) {
618                 printf("Line %i: Non-existent ACL context found!\n", __LINE__);
619                 goto err;
620         }
621
622         /* free context */
623         rte_acl_free(acx);
624
625
626         /* create valid (but severely limited) acx */
627         memcpy(&param, &acl_param, sizeof(param));
628         param.max_rule_num = LEN;
629
630         acx = rte_acl_create(&param);
631         if (acx == NULL) {
632                 printf("Line %i: Error creating %s!\n", __LINE__, param.name);
633                 goto err;
634         }
635
636         /* create dummy acl */
637         for (i = 0; i < LEN; i++) {
638                 memcpy(&rules[i], &acl_rule,
639                         sizeof(struct rte_acl_ipv4vlan_rule));
640                 /* skip zero */
641                 rules[i].data.userdata = i + 1;
642                 /* one rule per category */
643                 rules[i].data.category_mask = 1 << i;
644         }
645
646         /* try filling up the context */
647         ret = rte_acl_ipv4vlan_add_rules(acx, rules, LEN);
648         if (ret != 0) {
649                 printf("Line %i: Adding %i rules to ACL context failed!\n",
650                                 __LINE__, LEN);
651                 goto err;
652         }
653
654         /* try adding to a (supposedly) full context */
655         ret = rte_acl_ipv4vlan_add_rules(acx, rules, 1);
656         if (ret == 0) {
657                 printf("Line %i: Adding rules to full ACL context should"
658                                 "have failed!\n", __LINE__);
659                 goto err;
660         }
661
662         /* try building the context */
663         ret = rte_acl_ipv4vlan_build(acx, layout, RTE_ACL_MAX_CATEGORIES);
664         if (ret != 0) {
665                 printf("Line %i: Building ACL context failed!\n", __LINE__);
666                 goto err;
667         }
668
669         rte_acl_free(acx);
670         rte_acl_free(acx2);
671
672         return 0;
673 err:
674         rte_acl_free(acx);
675         rte_acl_free(acx2);
676         return -1;
677 }
678
679 /*
680  * test various invalid rules
681  */
682 static int
683 test_invalid_rules(void)
684 {
685         struct rte_acl_ctx *acx;
686         int ret;
687
688         struct rte_acl_ipv4vlan_rule rule;
689
690         acx = rte_acl_create(&acl_param);
691         if (acx == NULL) {
692                 printf("Line %i: Error creating ACL context!\n", __LINE__);
693                 return -1;
694         }
695
696         /* test inverted high/low source and destination ports.
697          * originally, there was a problem with memory consumption when using
698          * such rules.
699          */
700         /* create dummy acl */
701         memcpy(&rule, &acl_rule, sizeof(struct rte_acl_ipv4vlan_rule));
702         rule.data.userdata = 1;
703         rule.dst_port_low = 0xfff0;
704         rule.dst_port_high = 0x0010;
705
706         /* add rules to context and try to build it */
707         ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
708         if (ret == 0) {
709                 printf("Line %i: Adding rules to ACL context "
710                                 "should have failed!\n", __LINE__);
711                 goto err;
712         }
713
714         rule.dst_port_low = 0x0;
715         rule.dst_port_high = 0xffff;
716         rule.src_port_low = 0xfff0;
717         rule.src_port_high = 0x0010;
718
719         /* add rules to context and try to build it */
720         ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
721         if (ret == 0) {
722                 printf("Line %i: Adding rules to ACL context "
723                                 "should have failed!\n", __LINE__);
724                 goto err;
725         }
726
727         rule.dst_port_low = 0x0;
728         rule.dst_port_high = 0xffff;
729         rule.src_port_low = 0x0;
730         rule.src_port_high = 0xffff;
731
732         rule.dst_mask_len = 33;
733
734         /* add rules to context and try to build it */
735         ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
736         if (ret == 0) {
737                 printf("Line %i: Adding rules to ACL context "
738                                 "should have failed!\n", __LINE__);
739                 goto err;
740         }
741
742         rule.dst_mask_len = 0;
743         rule.src_mask_len = 33;
744
745         /* add rules to context and try to build it */
746         ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
747         if (ret == 0) {
748                 printf("Line %i: Adding rules to ACL context "
749                                 "should have failed!\n", __LINE__);
750                 goto err;
751         }
752
753         rule.dst_mask_len = 0;
754         rule.src_mask_len = 0;
755         rule.data.userdata = 0;
756
757         /* try adding this rule (it should fail because userdata is invalid) */
758         ret = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
759         if (ret == 0) {
760                 printf("Line %i: Adding a rule with invalid user data "
761                                 "should have failed!\n", __LINE__);
762                 rte_acl_free(acx);
763                 return -1;
764         }
765
766         rte_acl_free(acx);
767
768         return 0;
769
770 err:
771         rte_acl_free(acx);
772
773         return -1;
774 }
775
776 /*
777  * test functions by passing invalid or
778  * non-workable parameters.
779  *
780  * we do very limited testing of classify functions here
781  * because those are performance-critical and
782  * thus don't do much parameter checking.
783  */
784 static int
785 test_invalid_parameters(void)
786 {
787         struct rte_acl_param param;
788         struct rte_acl_ctx *acx;
789         struct rte_acl_ipv4vlan_rule rule;
790         int result;
791
792         uint32_t layout[RTE_ACL_IPV4VLAN_NUM] = {0};
793
794
795         /**
796          * rte_ac_create()
797          */
798
799         /* NULL param */
800         acx = rte_acl_create(NULL);
801         if (acx != NULL) {
802                 printf("Line %i: ACL context creation with NULL param "
803                                 "should have failed!\n", __LINE__);
804                 rte_acl_free(acx);
805                 return -1;
806         }
807
808         /* zero rule size */
809         memcpy(&param, &acl_param, sizeof(param));
810         param.rule_size = 0;
811
812         acx = rte_acl_create(&param);
813         if (acx == NULL) {
814                 printf("Line %i: ACL context creation with zero rule len "
815                                 "failed!\n", __LINE__);
816                 return -1;
817         } else
818                 rte_acl_free(acx);
819
820         /* zero max rule num */
821         memcpy(&param, &acl_param, sizeof(param));
822         param.max_rule_num = 0;
823
824         acx = rte_acl_create(&param);
825         if (acx == NULL) {
826                 printf("Line %i: ACL context creation with zero rule num "
827                                 "failed!\n", __LINE__);
828                 return -1;
829         } else
830                 rte_acl_free(acx);
831
832         /* invalid NUMA node */
833         memcpy(&param, &acl_param, sizeof(param));
834         param.socket_id = RTE_MAX_NUMA_NODES + 1;
835
836         acx = rte_acl_create(&param);
837         if (acx != NULL) {
838                 printf("Line %i: ACL context creation with invalid NUMA "
839                                 "should have failed!\n", __LINE__);
840                 rte_acl_free(acx);
841                 return -1;
842         }
843
844         /* NULL name */
845         memcpy(&param, &acl_param, sizeof(param));
846         param.name = NULL;
847
848         acx = rte_acl_create(&param);
849         if (acx != NULL) {
850                 printf("Line %i: ACL context creation with NULL name "
851                                 "should have failed!\n", __LINE__);
852                 rte_acl_free(acx);
853                 return -1;
854         }
855
856         /**
857          * rte_acl_find_existing
858          */
859
860         acx = rte_acl_find_existing(NULL);
861         if (acx != NULL) {
862                 printf("Line %i: NULL ACL context found!\n", __LINE__);
863                 rte_acl_free(acx);
864                 return -1;
865         }
866
867         /**
868          * rte_acl_ipv4vlan_add_rules
869          */
870
871         /* initialize everything */
872         memcpy(&param, &acl_param, sizeof(param));
873         acx = rte_acl_create(&param);
874         if (acx == NULL) {
875                 printf("Line %i: ACL context creation failed!\n", __LINE__);
876                 return -1;
877         }
878
879         memcpy(&rule, &acl_rule, sizeof(rule));
880
881         /* NULL context */
882         result = rte_acl_ipv4vlan_add_rules(NULL, &rule, 1);
883         if (result == 0) {
884                 printf("Line %i: Adding rules with NULL ACL context "
885                                 "should have failed!\n", __LINE__);
886                 rte_acl_free(acx);
887                 return -1;
888         }
889
890         /* NULL rule */
891         result = rte_acl_ipv4vlan_add_rules(acx, NULL, 1);
892         if (result == 0) {
893                 printf("Line %i: Adding NULL rule to ACL context "
894                                 "should have failed!\n", __LINE__);
895                 rte_acl_free(acx);
896                 return -1;
897         }
898
899         /* zero count (should succeed) */
900         result = rte_acl_ipv4vlan_add_rules(acx, &rule, 0);
901         if (result != 0) {
902                 printf("Line %i: Adding 0 rules to ACL context failed!\n",
903                         __LINE__);
904                 rte_acl_free(acx);
905                 return -1;
906         }
907
908         /* free ACL context */
909         rte_acl_free(acx);
910
911         /* set wrong rule_size so that adding any rules would fail */
912         param.rule_size = RTE_ACL_IPV4VLAN_RULE_SZ + 4;
913         acx = rte_acl_create(&param);
914         if (acx == NULL) {
915                 printf("Line %i: ACL context creation failed!\n", __LINE__);
916                 return -1;
917         }
918
919         /* try adding a rule with size different from context rule_size */
920         result = rte_acl_ipv4vlan_add_rules(acx, &rule, 1);
921         if (result == 0) {
922                 printf("Line %i: Adding an invalid sized rule "
923                                 "should have failed!\n", __LINE__);
924                 rte_acl_free(acx);
925                 return -1;
926         }
927
928         /* free ACL context */
929         rte_acl_free(acx);
930
931
932         /**
933          * rte_acl_ipv4vlan_build
934          */
935
936         /* reinitialize context */
937         memcpy(&param, &acl_param, sizeof(param));
938         acx = rte_acl_create(&param);
939         if (acx == NULL) {
940                 printf("Line %i: ACL context creation failed!\n", __LINE__);
941                 return -1;
942         }
943
944         /* NULL context */
945         result = rte_acl_ipv4vlan_build(NULL, layout, 1);
946         if (result == 0) {
947                 printf("Line %i: Building with NULL context "
948                                 "should have failed!\n", __LINE__);
949                 rte_acl_free(acx);
950                 return -1;
951         }
952
953         /* NULL layout */
954         result = rte_acl_ipv4vlan_build(acx, NULL, 1);
955         if (result == 0) {
956                 printf("Line %i: Building with NULL layout "
957                                 "should have failed!\n", __LINE__);
958                 rte_acl_free(acx);
959                 return -1;
960         }
961
962         /* zero categories (should not fail) */
963         result = rte_acl_ipv4vlan_build(acx, layout, 0);
964         if (result == 0) {
965                 printf("Line %i: Building with 0 categories should fail!\n",
966                         __LINE__);
967                 rte_acl_free(acx);
968                 return -1;
969         }
970
971         /* SSE classify test */
972
973         /* cover zero categories in classify (should not fail) */
974         result = rte_acl_classify(acx, NULL, NULL, 0, 0);
975         if (result != 0) {
976                 printf("Line %i: SSE classify with zero categories "
977                                 "failed!\n", __LINE__);
978                 rte_acl_free(acx);
979                 return -1;
980         }
981
982         /* cover invalid but positive categories in classify */
983         result = rte_acl_classify(acx, NULL, NULL, 0, 3);
984         if (result == 0) {
985                 printf("Line %i: SSE classify with 3 categories "
986                                 "should have failed!\n", __LINE__);
987                 rte_acl_free(acx);
988                 return -1;
989         }
990
991         /* scalar classify test */
992
993         /* cover zero categories in classify (should not fail) */
994         result = rte_acl_classify_alg(acx, NULL, NULL, 0, 0,
995                 RTE_ACL_CLASSIFY_SCALAR);
996         if (result != 0) {
997                 printf("Line %i: Scalar classify with zero categories "
998                                 "failed!\n", __LINE__);
999                 rte_acl_free(acx);
1000                 return -1;
1001         }
1002
1003         /* cover invalid but positive categories in classify */
1004         result = rte_acl_classify(acx, NULL, NULL, 0, 3);
1005         if (result == 0) {
1006                 printf("Line %i: Scalar classify with 3 categories "
1007                                 "should have failed!\n", __LINE__);
1008                 rte_acl_free(acx);
1009                 return -1;
1010         }
1011
1012         /* free ACL context */
1013         rte_acl_free(acx);
1014
1015
1016         /**
1017          * make sure void functions don't crash with NULL parameters
1018          */
1019
1020         rte_acl_free(NULL);
1021
1022         rte_acl_dump(NULL);
1023
1024         return 0;
1025 }
1026
1027 /**
1028  * Various tests that don't test much but improve coverage
1029  */
1030 static int
1031 test_misc(void)
1032 {
1033         struct rte_acl_param param;
1034         struct rte_acl_ctx *acx;
1035
1036         /* create context */
1037         memcpy(&param, &acl_param, sizeof(param));
1038
1039         acx = rte_acl_create(&param);
1040         if (acx == NULL) {
1041                 printf("Line %i: Error creating ACL context!\n", __LINE__);
1042                 return -1;
1043         }
1044
1045         /* dump context with rules - useful for coverage */
1046         rte_acl_list_dump();
1047
1048         rte_acl_dump(acx);
1049
1050         rte_acl_free(acx);
1051
1052         return 0;
1053 }
1054
1055 static int
1056 test_acl(void)
1057 {
1058         if (test_invalid_parameters() < 0)
1059                 return -1;
1060         if (test_invalid_rules() < 0)
1061                 return -1;
1062         if (test_create_find_add() < 0)
1063                 return -1;
1064         if (test_invalid_layout() < 0)
1065                 return -1;
1066         if (test_misc() < 0)
1067                 return -1;
1068         if (test_classify() < 0)
1069                 return -1;
1070         if (test_build_ports_range() < 0)
1071                 return -1;
1072
1073         return 0;
1074 }
1075
1076 static struct test_command acl_cmd = {
1077         .command = "acl_autotest",
1078         .callback = test_acl,
1079 };
1080 REGISTER_TEST_COMMAND(acl_cmd);