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.
38 #include <rte_cycles.h>
39 #include <rte_per_lcore.h>
40 #include <rte_lcore.h>
43 #define PRINT_USAGE_START "%s [EAL options]\n"
45 #define RTE_LOGTYPE_TESTACL RTE_LOGTYPE_USER1
47 #define APP_NAME "TESTACL"
49 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
53 val = strtoul((in), &end_fld, (base)); \
54 if (errno != 0 || end_fld[0] != (dlm) || val > (lim)) \
56 (fd) = (typeof(fd))val; \
60 #define OPT_RULE_FILE "rulesf"
61 #define OPT_TRACE_FILE "tracef"
62 #define OPT_RULE_NUM "rulenum"
63 #define OPT_TRACE_NUM "tracenum"
64 #define OPT_TRACE_STEP "tracestep"
65 #define OPT_SEARCH_ALG "alg"
66 #define OPT_BLD_CATEGORIES "bldcat"
67 #define OPT_RUN_CATEGORIES "runcat"
68 #define OPT_MAX_SIZE "maxsize"
69 #define OPT_ITER_NUM "iter"
70 #define OPT_VERBOSE "verbose"
71 #define OPT_IPV6 "ipv6"
73 #define TRACE_DEFAULT_NUM 0x10000
74 #define TRACE_STEP_MAX 0x1000
75 #define TRACE_STEP_DEF 0x100
77 #define RULE_NUM 0x10000
88 enum rte_acl_classify_alg alg
;
91 static const struct acl_alg acl_alg
[] = {
94 .alg
= RTE_ACL_CLASSIFY_SCALAR
,
98 .alg
= RTE_ACL_CLASSIFY_SSE
,
102 .alg
= RTE_ACL_CLASSIFY_AVX2
,
106 .alg
= RTE_ACL_CLASSIFY_NEON
,
110 .alg
= RTE_ACL_CLASSIFY_ALTIVEC
,
116 const char *rule_file
;
117 const char *trace_file
;
119 uint32_t bld_categories
;
120 uint32_t run_categories
;
129 uint32_t used_traces
;
131 struct rte_acl_ctx
*acx
;
135 .nb_rules
= RULE_NUM
,
136 .nb_traces
= TRACE_DEFAULT_NUM
,
137 .trace_step
= TRACE_STEP_DEF
,
142 .alg
= RTE_ACL_CLASSIFY_DEFAULT
,
147 static struct rte_acl_param prm
= {
149 .socket_id
= SOCKET_ID_ANY
,
153 * Rule and trace formats definitions.
174 * That effectively defines order of IPV4VLAN classifications:
176 * - VLAN (TAG and DOMAIN)
179 * - PORTS (SRC and DST)
182 RTE_ACL_IPV4VLAN_PROTO
,
183 RTE_ACL_IPV4VLAN_VLAN
,
184 RTE_ACL_IPV4VLAN_SRC
,
185 RTE_ACL_IPV4VLAN_DST
,
186 RTE_ACL_IPV4VLAN_PORTS
,
190 struct rte_acl_field_def ipv4_defs
[NUM_FIELDS_IPV4
] = {
192 .type
= RTE_ACL_FIELD_TYPE_BITMASK
,
193 .size
= sizeof(uint8_t),
194 .field_index
= PROTO_FIELD_IPV4
,
195 .input_index
= RTE_ACL_IPV4VLAN_PROTO
,
196 .offset
= offsetof(struct ipv4_5tuple
, proto
),
199 .type
= RTE_ACL_FIELD_TYPE_MASK
,
200 .size
= sizeof(uint32_t),
201 .field_index
= SRC_FIELD_IPV4
,
202 .input_index
= RTE_ACL_IPV4VLAN_SRC
,
203 .offset
= offsetof(struct ipv4_5tuple
, ip_src
),
206 .type
= RTE_ACL_FIELD_TYPE_MASK
,
207 .size
= sizeof(uint32_t),
208 .field_index
= DST_FIELD_IPV4
,
209 .input_index
= RTE_ACL_IPV4VLAN_DST
,
210 .offset
= offsetof(struct ipv4_5tuple
, ip_dst
),
213 .type
= RTE_ACL_FIELD_TYPE_RANGE
,
214 .size
= sizeof(uint16_t),
215 .field_index
= SRCP_FIELD_IPV4
,
216 .input_index
= RTE_ACL_IPV4VLAN_PORTS
,
217 .offset
= offsetof(struct ipv4_5tuple
, port_src
),
220 .type
= RTE_ACL_FIELD_TYPE_RANGE
,
221 .size
= sizeof(uint16_t),
222 .field_index
= DSTP_FIELD_IPV4
,
223 .input_index
= RTE_ACL_IPV4VLAN_PORTS
,
224 .offset
= offsetof(struct ipv4_5tuple
, port_dst
),
228 #define IPV6_ADDR_LEN 16
229 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
230 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
234 uint32_t ip_src
[IPV6_ADDR_U32
];
235 uint32_t ip_dst
[IPV6_ADDR_U32
];
255 struct rte_acl_field_def ipv6_defs
[NUM_FIELDS_IPV6
] = {
257 .type
= RTE_ACL_FIELD_TYPE_BITMASK
,
258 .size
= sizeof(uint8_t),
259 .field_index
= PROTO_FIELD_IPV6
,
260 .input_index
= PROTO_FIELD_IPV6
,
261 .offset
= offsetof(struct ipv6_5tuple
, proto
),
264 .type
= RTE_ACL_FIELD_TYPE_MASK
,
265 .size
= sizeof(uint32_t),
266 .field_index
= SRC1_FIELD_IPV6
,
267 .input_index
= SRC1_FIELD_IPV6
,
268 .offset
= offsetof(struct ipv6_5tuple
, ip_src
[0]),
271 .type
= RTE_ACL_FIELD_TYPE_MASK
,
272 .size
= sizeof(uint32_t),
273 .field_index
= SRC2_FIELD_IPV6
,
274 .input_index
= SRC2_FIELD_IPV6
,
275 .offset
= offsetof(struct ipv6_5tuple
, ip_src
[1]),
278 .type
= RTE_ACL_FIELD_TYPE_MASK
,
279 .size
= sizeof(uint32_t),
280 .field_index
= SRC3_FIELD_IPV6
,
281 .input_index
= SRC3_FIELD_IPV6
,
282 .offset
= offsetof(struct ipv6_5tuple
, ip_src
[2]),
285 .type
= RTE_ACL_FIELD_TYPE_MASK
,
286 .size
= sizeof(uint32_t),
287 .field_index
= SRC4_FIELD_IPV6
,
288 .input_index
= SRC4_FIELD_IPV6
,
289 .offset
= offsetof(struct ipv6_5tuple
, ip_src
[3]),
292 .type
= RTE_ACL_FIELD_TYPE_MASK
,
293 .size
= sizeof(uint32_t),
294 .field_index
= DST1_FIELD_IPV6
,
295 .input_index
= DST1_FIELD_IPV6
,
296 .offset
= offsetof(struct ipv6_5tuple
, ip_dst
[0]),
299 .type
= RTE_ACL_FIELD_TYPE_MASK
,
300 .size
= sizeof(uint32_t),
301 .field_index
= DST2_FIELD_IPV6
,
302 .input_index
= DST2_FIELD_IPV6
,
303 .offset
= offsetof(struct ipv6_5tuple
, ip_dst
[1]),
306 .type
= RTE_ACL_FIELD_TYPE_MASK
,
307 .size
= sizeof(uint32_t),
308 .field_index
= DST3_FIELD_IPV6
,
309 .input_index
= DST3_FIELD_IPV6
,
310 .offset
= offsetof(struct ipv6_5tuple
, ip_dst
[2]),
313 .type
= RTE_ACL_FIELD_TYPE_MASK
,
314 .size
= sizeof(uint32_t),
315 .field_index
= DST4_FIELD_IPV6
,
316 .input_index
= DST4_FIELD_IPV6
,
317 .offset
= offsetof(struct ipv6_5tuple
, ip_dst
[3]),
320 .type
= RTE_ACL_FIELD_TYPE_RANGE
,
321 .size
= sizeof(uint16_t),
322 .field_index
= SRCP_FIELD_IPV6
,
323 .input_index
= SRCP_FIELD_IPV6
,
324 .offset
= offsetof(struct ipv6_5tuple
, port_src
),
327 .type
= RTE_ACL_FIELD_TYPE_RANGE
,
328 .size
= sizeof(uint16_t),
329 .field_index
= DSTP_FIELD_IPV6
,
330 .input_index
= SRCP_FIELD_IPV6
,
331 .offset
= offsetof(struct ipv6_5tuple
, port_dst
),
341 CB_FLD_SRC_PORT_HIGH
,
344 CB_FLD_DST_PORT_HIGH
,
358 RTE_ACL_RULE_DEF(acl_rule
, RTE_ACL_MAX_FIELDS
);
360 static const char cb_port_delim
[] = ":";
362 static char line
[LINE_MAX
];
364 #define dump_verbose(lvl, fh, fmt, args...) do { \
365 if ((lvl) <= (int32_t)config.verbose) \
366 fprintf(fh, fmt, ##args); \
371 * Parse ClassBench input trace (test vectors and expected results) file.
373 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
374 * <src_port> <space> <dst_port> <space> <proto>
377 parse_cb_ipv4_trace(char *str
, struct ipv4_5tuple
*v
)
380 char *s
, *sp
, *in
[CB_TRC_NUM
];
381 static const char *dlm
= " \t\n";
384 for (i
= 0; i
!= RTE_DIM(in
); i
++) {
385 in
[i
] = strtok_r(s
, dlm
, &sp
);
391 GET_CB_FIELD(in
[CB_TRC_SRC_ADDR
], v
->ip_src
, 0, UINT32_MAX
, 0);
392 GET_CB_FIELD(in
[CB_TRC_DST_ADDR
], v
->ip_dst
, 0, UINT32_MAX
, 0);
393 GET_CB_FIELD(in
[CB_TRC_SRC_PORT
], v
->port_src
, 0, UINT16_MAX
, 0);
394 GET_CB_FIELD(in
[CB_TRC_DST_PORT
], v
->port_dst
, 0, UINT16_MAX
, 0);
395 GET_CB_FIELD(in
[CB_TRC_PROTO
], v
->proto
, 0, UINT8_MAX
, 0);
397 /* convert to network byte order. */
398 v
->ip_src
= rte_cpu_to_be_32(v
->ip_src
);
399 v
->ip_dst
= rte_cpu_to_be_32(v
->ip_dst
);
400 v
->port_src
= rte_cpu_to_be_16(v
->port_src
);
401 v
->port_dst
= rte_cpu_to_be_16(v
->port_dst
);
407 * Parses IPV6 address, exepcts the following format:
408 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
411 parse_ipv6_addr(const char *in
, const char **end
, uint32_t v
[IPV6_ADDR_U32
],
414 uint32_t addr
[IPV6_ADDR_U16
];
416 GET_CB_FIELD(in
, addr
[0], 16, UINT16_MAX
, ':');
417 GET_CB_FIELD(in
, addr
[1], 16, UINT16_MAX
, ':');
418 GET_CB_FIELD(in
, addr
[2], 16, UINT16_MAX
, ':');
419 GET_CB_FIELD(in
, addr
[3], 16, UINT16_MAX
, ':');
420 GET_CB_FIELD(in
, addr
[4], 16, UINT16_MAX
, ':');
421 GET_CB_FIELD(in
, addr
[5], 16, UINT16_MAX
, ':');
422 GET_CB_FIELD(in
, addr
[6], 16, UINT16_MAX
, ':');
423 GET_CB_FIELD(in
, addr
[7], 16, UINT16_MAX
, dlm
);
427 v
[0] = (addr
[0] << 16) + addr
[1];
428 v
[1] = (addr
[2] << 16) + addr
[3];
429 v
[2] = (addr
[4] << 16) + addr
[5];
430 v
[3] = (addr
[6] << 16) + addr
[7];
436 parse_cb_ipv6_addr_trace(const char *in
, uint32_t v
[IPV6_ADDR_U32
])
441 rc
= parse_ipv6_addr(in
, &end
, v
, 0);
445 v
[0] = rte_cpu_to_be_32(v
[0]);
446 v
[1] = rte_cpu_to_be_32(v
[1]);
447 v
[2] = rte_cpu_to_be_32(v
[2]);
448 v
[3] = rte_cpu_to_be_32(v
[3]);
454 * Parse ClassBench input trace (test vectors and expected results) file.
456 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
457 * <src_port> <space> <dst_port> <space> <proto>
460 parse_cb_ipv6_trace(char *str
, struct ipv6_5tuple
*v
)
463 char *s
, *sp
, *in
[CB_TRC_NUM
];
464 static const char *dlm
= " \t\n";
467 for (i
= 0; i
!= RTE_DIM(in
); i
++) {
468 in
[i
] = strtok_r(s
, dlm
, &sp
);
474 /* get ip6 src address. */
475 rc
= parse_cb_ipv6_addr_trace(in
[CB_TRC_SRC_ADDR
], v
->ip_src
);
479 /* get ip6 dst address. */
480 rc
= parse_cb_ipv6_addr_trace(in
[CB_TRC_DST_ADDR
], v
->ip_dst
);
484 GET_CB_FIELD(in
[CB_TRC_SRC_PORT
], v
->port_src
, 0, UINT16_MAX
, 0);
485 GET_CB_FIELD(in
[CB_TRC_DST_PORT
], v
->port_dst
, 0, UINT16_MAX
, 0);
486 GET_CB_FIELD(in
[CB_TRC_PROTO
], v
->proto
, 0, UINT8_MAX
, 0);
488 /* convert to network byte order. */
489 v
->port_src
= rte_cpu_to_be_16(v
->port_src
);
490 v
->port_dst
= rte_cpu_to_be_16(v
->port_dst
);
498 static const char name
[] = APP_NAME
;
502 struct ipv4_5tuple
*v
;
503 struct ipv6_5tuple
*w
;
505 sz
= config
.nb_traces
* (config
.ipv6
? sizeof(*w
) : sizeof(*v
));
506 config
.traces
= rte_zmalloc_socket(name
, sz
, RTE_CACHE_LINE_SIZE
,
508 if (config
.traces
== NULL
)
509 rte_exit(EXIT_FAILURE
, "Cannot allocate %zu bytes for "
510 "requested %u number of trace records\n",
511 sz
, config
.nb_traces
);
513 f
= fopen(config
.trace_file
, "r");
515 rte_exit(-EINVAL
, "failed to open file: %s\n",
520 for (n
= 0; n
!= config
.nb_traces
; n
++) {
522 if (fgets(line
, sizeof(line
), f
) == NULL
)
526 if (parse_cb_ipv6_trace(line
, w
+ n
) != 0)
527 rte_exit(EXIT_FAILURE
,
528 "%s: failed to parse ipv6 trace "
529 "record at line %u\n",
530 config
.trace_file
, n
+ 1);
532 if (parse_cb_ipv4_trace(line
, v
+ n
) != 0)
533 rte_exit(EXIT_FAILURE
,
534 "%s: failed to parse ipv4 trace "
535 "record at line %u\n",
536 config
.trace_file
, n
+ 1);
540 config
.used_traces
= n
;
545 parse_ipv6_net(const char *in
, struct rte_acl_field field
[4])
550 const uint32_t nbu32
= sizeof(uint32_t) * CHAR_BIT
;
553 rc
= parse_ipv6_addr(in
, &mp
, v
, '/');
558 GET_CB_FIELD(mp
, m
, 0, CHAR_BIT
* sizeof(v
), 0);
560 /* put all together. */
561 for (i
= 0; i
!= RTE_DIM(v
); i
++) {
562 if (m
>= (i
+ 1) * nbu32
)
563 field
[i
].mask_range
.u32
= nbu32
;
565 field
[i
].mask_range
.u32
= m
> (i
* nbu32
) ?
568 field
[i
].value
.u32
= v
[i
];
576 parse_cb_ipv6_rule(char *str
, struct acl_rule
*v
)
579 char *s
, *sp
, *in
[CB_FLD_NUM
];
580 static const char *dlm
= " \t\n";
585 if (strchr(str
, '@') != str
)
590 for (i
= 0; i
!= RTE_DIM(in
); i
++) {
591 in
[i
] = strtok_r(s
, dlm
, &sp
);
597 rc
= parse_ipv6_net(in
[CB_FLD_SRC_ADDR
], v
->field
+ SRC1_FIELD_IPV6
);
599 RTE_LOG(ERR
, TESTACL
,
600 "failed to read source address/mask: %s\n",
601 in
[CB_FLD_SRC_ADDR
]);
605 rc
= parse_ipv6_net(in
[CB_FLD_DST_ADDR
], v
->field
+ DST1_FIELD_IPV6
);
607 RTE_LOG(ERR
, TESTACL
,
608 "failed to read destination address/mask: %s\n",
609 in
[CB_FLD_DST_ADDR
]);
614 GET_CB_FIELD(in
[CB_FLD_SRC_PORT_LOW
],
615 v
->field
[SRCP_FIELD_IPV6
].value
.u16
,
617 GET_CB_FIELD(in
[CB_FLD_SRC_PORT_HIGH
],
618 v
->field
[SRCP_FIELD_IPV6
].mask_range
.u16
,
621 if (strncmp(in
[CB_FLD_SRC_PORT_DLM
], cb_port_delim
,
622 sizeof(cb_port_delim
)) != 0)
625 /* destination port. */
626 GET_CB_FIELD(in
[CB_FLD_DST_PORT_LOW
],
627 v
->field
[DSTP_FIELD_IPV6
].value
.u16
,
629 GET_CB_FIELD(in
[CB_FLD_DST_PORT_HIGH
],
630 v
->field
[DSTP_FIELD_IPV6
].mask_range
.u16
,
633 if (strncmp(in
[CB_FLD_DST_PORT_DLM
], cb_port_delim
,
634 sizeof(cb_port_delim
)) != 0)
637 GET_CB_FIELD(in
[CB_FLD_PROTO
], v
->field
[PROTO_FIELD_IPV6
].value
.u8
,
639 GET_CB_FIELD(in
[CB_FLD_PROTO
], v
->field
[PROTO_FIELD_IPV6
].mask_range
.u8
,
646 parse_ipv4_net(const char *in
, uint32_t *addr
, uint32_t *mask_len
)
648 uint8_t a
, b
, c
, d
, m
;
650 GET_CB_FIELD(in
, a
, 0, UINT8_MAX
, '.');
651 GET_CB_FIELD(in
, b
, 0, UINT8_MAX
, '.');
652 GET_CB_FIELD(in
, c
, 0, UINT8_MAX
, '.');
653 GET_CB_FIELD(in
, d
, 0, UINT8_MAX
, '/');
654 GET_CB_FIELD(in
, m
, 0, sizeof(uint32_t) * CHAR_BIT
, 0);
656 addr
[0] = IPv4(a
, b
, c
, d
);
662 * Parse ClassBench rules file.
664 * '@'<src_ipv4_addr>'/'<masklen> <space> \
665 * <dst_ipv4_addr>'/'<masklen> <space> \
666 * <src_port_low> <space> ":" <src_port_high> <space> \
667 * <dst_port_low> <space> ":" <dst_port_high> <space> \
671 parse_cb_ipv4_rule(char *str
, struct acl_rule
*v
)
674 char *s
, *sp
, *in
[CB_FLD_NUM
];
675 static const char *dlm
= " \t\n";
680 if (strchr(str
, '@') != str
)
685 for (i
= 0; i
!= RTE_DIM(in
); i
++) {
686 in
[i
] = strtok_r(s
, dlm
, &sp
);
692 rc
= parse_ipv4_net(in
[CB_FLD_SRC_ADDR
],
693 &v
->field
[SRC_FIELD_IPV4
].value
.u32
,
694 &v
->field
[SRC_FIELD_IPV4
].mask_range
.u32
);
696 RTE_LOG(ERR
, TESTACL
,
697 "failed to read source address/mask: %s\n",
698 in
[CB_FLD_SRC_ADDR
]);
702 rc
= parse_ipv4_net(in
[CB_FLD_DST_ADDR
],
703 &v
->field
[DST_FIELD_IPV4
].value
.u32
,
704 &v
->field
[DST_FIELD_IPV4
].mask_range
.u32
);
706 RTE_LOG(ERR
, TESTACL
,
707 "failed to read destination address/mask: %s\n",
708 in
[CB_FLD_DST_ADDR
]);
713 GET_CB_FIELD(in
[CB_FLD_SRC_PORT_LOW
],
714 v
->field
[SRCP_FIELD_IPV4
].value
.u16
,
716 GET_CB_FIELD(in
[CB_FLD_SRC_PORT_HIGH
],
717 v
->field
[SRCP_FIELD_IPV4
].mask_range
.u16
,
720 if (strncmp(in
[CB_FLD_SRC_PORT_DLM
], cb_port_delim
,
721 sizeof(cb_port_delim
)) != 0)
724 /* destination port. */
725 GET_CB_FIELD(in
[CB_FLD_DST_PORT_LOW
],
726 v
->field
[DSTP_FIELD_IPV4
].value
.u16
,
728 GET_CB_FIELD(in
[CB_FLD_DST_PORT_HIGH
],
729 v
->field
[DSTP_FIELD_IPV4
].mask_range
.u16
,
732 if (strncmp(in
[CB_FLD_DST_PORT_DLM
], cb_port_delim
,
733 sizeof(cb_port_delim
)) != 0)
736 GET_CB_FIELD(in
[CB_FLD_PROTO
], v
->field
[PROTO_FIELD_IPV4
].value
.u8
,
738 GET_CB_FIELD(in
[CB_FLD_PROTO
], v
->field
[PROTO_FIELD_IPV4
].mask_range
.u8
,
744 typedef int (*parse_5tuple
)(char *text
, struct acl_rule
*rule
);
747 add_cb_rules(FILE *f
, struct rte_acl_ctx
*ctx
)
754 memset(&v
, 0, sizeof(v
));
755 parser
= (config
.ipv6
!= 0) ? parse_cb_ipv6_rule
: parse_cb_ipv4_rule
;
757 for (n
= 1; fgets(line
, sizeof(line
), f
) != NULL
; n
++) {
759 rc
= parser(line
, &v
);
761 RTE_LOG(ERR
, TESTACL
, "line %u: parse_cb_ipv4vlan_rule"
762 " failed, error code: %d (%s)\n",
763 n
, rc
, strerror(-rc
));
767 v
.data
.category_mask
= RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES
,
768 typeof(v
.data
.category_mask
));
769 v
.data
.priority
= RTE_ACL_MAX_PRIORITY
- n
;
772 rc
= rte_acl_add_rules(ctx
, (struct rte_acl_rule
*)&v
, 1);
774 RTE_LOG(ERR
, TESTACL
, "line %u: failed to add rules "
775 "into ACL context, error code: %d (%s)\n",
776 n
, rc
, strerror(-rc
));
789 struct rte_acl_config cfg
;
791 memset(&cfg
, 0, sizeof(cfg
));
793 /* setup ACL build config. */
795 cfg
.num_fields
= RTE_DIM(ipv6_defs
);
796 memcpy(&cfg
.defs
, ipv6_defs
, sizeof(ipv6_defs
));
798 cfg
.num_fields
= RTE_DIM(ipv4_defs
);
799 memcpy(&cfg
.defs
, ipv4_defs
, sizeof(ipv4_defs
));
801 cfg
.num_categories
= config
.bld_categories
;
802 cfg
.max_size
= config
.max_size
;
804 /* setup ACL creation parameters. */
805 prm
.rule_size
= RTE_ACL_RULE_SZ(cfg
.num_fields
);
806 prm
.max_rule_num
= config
.nb_rules
;
808 config
.acx
= rte_acl_create(&prm
);
809 if (config
.acx
== NULL
)
810 rte_exit(rte_errno
, "failed to create ACL context\n");
812 /* set default classify method for this context. */
813 if (config
.alg
.alg
!= RTE_ACL_CLASSIFY_DEFAULT
) {
814 ret
= rte_acl_set_ctx_classify(config
.acx
, config
.alg
.alg
);
816 rte_exit(ret
, "failed to setup %s method "
817 "for ACL context\n", config
.alg
.name
);
821 f
= fopen(config
.rule_file
, "r");
823 rte_exit(-EINVAL
, "failed to open file %s\n",
826 ret
= add_cb_rules(f
, config
.acx
);
828 rte_exit(ret
, "failed to add rules into ACL context\n");
833 ret
= rte_acl_build(config
.acx
, &cfg
);
835 dump_verbose(DUMP_NONE
, stdout
,
836 "rte_acl_build(%u) finished with %d\n",
837 config
.bld_categories
, ret
);
839 rte_acl_dump(config
.acx
);
842 rte_exit(ret
, "failed to build search context\n");
846 search_ip5tuples_once(uint32_t categories
, uint32_t step
, const char *alg
)
849 uint32_t i
, j
, k
, n
, r
;
850 const uint8_t *data
[step
], *v
;
851 uint32_t results
[step
* categories
];
854 for (i
= 0; i
!= config
.used_traces
; i
+= n
) {
856 n
= RTE_MIN(step
, config
.used_traces
- i
);
858 for (j
= 0; j
!= n
; j
++) {
860 v
+= config
.trace_sz
;
863 ret
= rte_acl_classify(config
.acx
, data
, results
,
867 rte_exit(ret
, "classify for ipv%c_5tuples returns %d\n",
868 config
.ipv6
? '6' : '4', ret
);
870 for (r
= 0, j
= 0; j
!= n
; j
++) {
871 for (k
= 0; k
!= categories
; k
++, r
++) {
872 dump_verbose(DUMP_PKT
, stdout
,
873 "ipv%c_5tuple: %u, category: %u, "
875 config
.ipv6
? '6' : '4',
876 i
+ j
+ 1, k
, results
[r
] - 1);
882 dump_verbose(DUMP_SEARCH
, stdout
,
883 "%s(%u, %u, %s) returns %u\n", __func__
,
884 categories
, step
, alg
, i
);
889 search_ip5tuples(__attribute__((unused
)) void *arg
)
891 uint64_t pkt
, start
, tm
;
894 lcore
= rte_lcore_id();
898 for (i
= 0; i
!= config
.iter_num
; i
++) {
899 pkt
+= search_ip5tuples_once(config
.run_categories
,
900 config
.trace_step
, config
.alg
.name
);
903 tm
= rte_rdtsc() - start
;
904 dump_verbose(DUMP_NONE
, stdout
,
905 "%s @lcore %u: %" PRIu32
" iterations, %" PRIu64
" pkts, %"
906 PRIu32
" categories, %" PRIu64
" cycles, %#Lf cycles/pkt\n",
907 __func__
, lcore
, i
, pkt
, config
.run_categories
,
908 tm
, (pkt
== 0) ? 0 : (long double)tm
/ pkt
);
914 get_ulong_opt(const char *opt
, const char *name
, size_t min
, size_t max
)
920 val
= strtoul(opt
, &end
, 0);
921 if (errno
!= 0 || end
[0] != 0 || val
> max
|| val
< min
)
922 rte_exit(-EINVAL
, "invalid value: \"%s\" for option: %s\n",
928 get_alg_opt(const char *opt
, const char *name
)
932 for (i
= 0; i
!= RTE_DIM(acl_alg
); i
++) {
933 if (strcmp(opt
, acl_alg
[i
].name
) == 0) {
934 config
.alg
= acl_alg
[i
];
939 rte_exit(-EINVAL
, "invalid value: \"%s\" for option: %s\n",
944 print_usage(const char *prgname
)
952 for (i
= 0; i
< RTE_DIM(acl_alg
) - 1; i
++) {
953 rc
= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s|",
955 if (rc
> sizeof(buf
) - n
)
960 snprintf(buf
+ n
, sizeof(buf
) - n
, "%s", acl_alg
[i
].name
);
964 "--" OPT_RULE_FILE
"=<rules set file>\n"
965 "[--" OPT_TRACE_FILE
"=<input traces file>]\n"
967 "=<maximum number of rules for ACL context>]\n"
969 "=<number of traces to read binary file in>]\n"
971 "=<number of traces to classify per one call>]\n"
972 "[--" OPT_BLD_CATEGORIES
973 "=<number of categories to build with>]\n"
974 "[--" OPT_RUN_CATEGORIES
975 "=<number of categories to run with> "
976 "should be either 1 or multiple of %zu, "
977 "but not greater then %u]\n"
979 "=<size limit (in bytes) for runtime ACL strucutures> "
980 "leave 0 for default behaviour]\n"
981 "[--" OPT_ITER_NUM
"=<number of iterations to perform>]\n"
982 "[--" OPT_VERBOSE
"=<verbose level>]\n"
983 "[--" OPT_SEARCH_ALG
"=%s]\n"
984 "[--" OPT_IPV6
"=<IPv6 rules and trace files>]\n",
985 prgname
, RTE_ACL_RESULTS_MULTIPLIER
,
986 (uint32_t)RTE_ACL_MAX_CATEGORIES
,
993 fprintf(f
, "%s:\n", __func__
);
994 fprintf(f
, "%s:%s\n", OPT_RULE_FILE
, config
.rule_file
);
995 fprintf(f
, "%s:%s\n", OPT_TRACE_FILE
, config
.trace_file
);
996 fprintf(f
, "%s:%u\n", OPT_RULE_NUM
, config
.nb_rules
);
997 fprintf(f
, "%s:%u\n", OPT_TRACE_NUM
, config
.nb_traces
);
998 fprintf(f
, "%s:%u\n", OPT_TRACE_STEP
, config
.trace_step
);
999 fprintf(f
, "%s:%u\n", OPT_BLD_CATEGORIES
, config
.bld_categories
);
1000 fprintf(f
, "%s:%u\n", OPT_RUN_CATEGORIES
, config
.run_categories
);
1001 fprintf(f
, "%s:%zu\n", OPT_MAX_SIZE
, config
.max_size
);
1002 fprintf(f
, "%s:%u\n", OPT_ITER_NUM
, config
.iter_num
);
1003 fprintf(f
, "%s:%u\n", OPT_VERBOSE
, config
.verbose
);
1004 fprintf(f
, "%s:%u(%s)\n", OPT_SEARCH_ALG
, config
.alg
.alg
,
1006 fprintf(f
, "%s:%u\n", OPT_IPV6
, config
.ipv6
);
1012 if (config
.rule_file
== NULL
) {
1013 print_usage(config
.prgname
);
1014 rte_exit(-EINVAL
, "mandatory option %s is not specified\n",
1021 get_input_opts(int argc
, char **argv
)
1023 static struct option lgopts
[] = {
1024 {OPT_RULE_FILE
, 1, 0, 0},
1025 {OPT_TRACE_FILE
, 1, 0, 0},
1026 {OPT_TRACE_NUM
, 1, 0, 0},
1027 {OPT_RULE_NUM
, 1, 0, 0},
1028 {OPT_MAX_SIZE
, 1, 0, 0},
1029 {OPT_TRACE_STEP
, 1, 0, 0},
1030 {OPT_BLD_CATEGORIES
, 1, 0, 0},
1031 {OPT_RUN_CATEGORIES
, 1, 0, 0},
1032 {OPT_ITER_NUM
, 1, 0, 0},
1033 {OPT_VERBOSE
, 1, 0, 0},
1034 {OPT_SEARCH_ALG
, 1, 0, 0},
1035 {OPT_IPV6
, 0, 0, 0},
1041 while ((opt
= getopt_long(argc
, argv
, "", lgopts
, &opt_idx
)) != EOF
) {
1044 print_usage(config
.prgname
);
1045 rte_exit(-EINVAL
, "unknown option: %c", opt
);
1048 if (strcmp(lgopts
[opt_idx
].name
, OPT_RULE_FILE
) == 0) {
1049 config
.rule_file
= optarg
;
1050 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_TRACE_FILE
) == 0) {
1051 config
.trace_file
= optarg
;
1052 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_RULE_NUM
) == 0) {
1053 config
.nb_rules
= get_ulong_opt(optarg
,
1054 lgopts
[opt_idx
].name
, 1, RTE_ACL_MAX_INDEX
+ 1);
1055 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_MAX_SIZE
) == 0) {
1056 config
.max_size
= get_ulong_opt(optarg
,
1057 lgopts
[opt_idx
].name
, 0, SIZE_MAX
);
1058 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_TRACE_NUM
) == 0) {
1059 config
.nb_traces
= get_ulong_opt(optarg
,
1060 lgopts
[opt_idx
].name
, 1, UINT32_MAX
);
1061 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_TRACE_STEP
) == 0) {
1062 config
.trace_step
= get_ulong_opt(optarg
,
1063 lgopts
[opt_idx
].name
, 1, TRACE_STEP_MAX
);
1064 } else if (strcmp(lgopts
[opt_idx
].name
,
1065 OPT_BLD_CATEGORIES
) == 0) {
1066 config
.bld_categories
= get_ulong_opt(optarg
,
1067 lgopts
[opt_idx
].name
, 1,
1068 RTE_ACL_MAX_CATEGORIES
);
1069 } else if (strcmp(lgopts
[opt_idx
].name
,
1070 OPT_RUN_CATEGORIES
) == 0) {
1071 config
.run_categories
= get_ulong_opt(optarg
,
1072 lgopts
[opt_idx
].name
, 1,
1073 RTE_ACL_MAX_CATEGORIES
);
1074 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_ITER_NUM
) == 0) {
1075 config
.iter_num
= get_ulong_opt(optarg
,
1076 lgopts
[opt_idx
].name
, 1, INT32_MAX
);
1077 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_VERBOSE
) == 0) {
1078 config
.verbose
= get_ulong_opt(optarg
,
1079 lgopts
[opt_idx
].name
, DUMP_NONE
, DUMP_MAX
);
1080 } else if (strcmp(lgopts
[opt_idx
].name
,
1081 OPT_SEARCH_ALG
) == 0) {
1082 get_alg_opt(optarg
, lgopts
[opt_idx
].name
);
1083 } else if (strcmp(lgopts
[opt_idx
].name
, OPT_IPV6
) == 0) {
1087 config
.trace_sz
= config
.ipv6
? sizeof(struct ipv6_5tuple
) :
1088 sizeof(struct ipv4_5tuple
);
1093 main(int argc
, char **argv
)
1098 ret
= rte_eal_init(argc
, argv
);
1100 rte_panic("Cannot init EAL\n");
1105 config
.prgname
= argv
[0];
1107 get_input_opts(argc
, argv
);
1108 dump_config(stdout
);
1113 if (config
.trace_file
!= NULL
)
1116 RTE_LCORE_FOREACH_SLAVE(lcore
)
1117 rte_eal_remote_launch(search_ip5tuples
, NULL
, lcore
);
1119 search_ip5tuples(NULL
);
1121 rte_eal_mp_wait_lcore();
1123 rte_acl_free(config
.acx
);