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,
112 const char *rule_file;
113 const char *trace_file;
115 uint32_t bld_categories;
116 uint32_t run_categories;
125 uint32_t used_traces;
127 struct rte_acl_ctx *acx;
131 .nb_rules = RULE_NUM,
132 .nb_traces = TRACE_DEFAULT_NUM,
133 .trace_step = TRACE_STEP_DEF,
138 .alg = RTE_ACL_CLASSIFY_DEFAULT,
143 static struct rte_acl_param prm = {
145 .socket_id = SOCKET_ID_ANY,
149 * Rule and trace formats definitions.
170 * That effectively defines order of IPV4VLAN classifications:
172 * - VLAN (TAG and DOMAIN)
175 * - PORTS (SRC and DST)
178 RTE_ACL_IPV4VLAN_PROTO,
179 RTE_ACL_IPV4VLAN_VLAN,
180 RTE_ACL_IPV4VLAN_SRC,
181 RTE_ACL_IPV4VLAN_DST,
182 RTE_ACL_IPV4VLAN_PORTS,
186 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
188 .type = RTE_ACL_FIELD_TYPE_BITMASK,
189 .size = sizeof(uint8_t),
190 .field_index = PROTO_FIELD_IPV4,
191 .input_index = RTE_ACL_IPV4VLAN_PROTO,
192 .offset = offsetof(struct ipv4_5tuple, proto),
195 .type = RTE_ACL_FIELD_TYPE_MASK,
196 .size = sizeof(uint32_t),
197 .field_index = SRC_FIELD_IPV4,
198 .input_index = RTE_ACL_IPV4VLAN_SRC,
199 .offset = offsetof(struct ipv4_5tuple, ip_src),
202 .type = RTE_ACL_FIELD_TYPE_MASK,
203 .size = sizeof(uint32_t),
204 .field_index = DST_FIELD_IPV4,
205 .input_index = RTE_ACL_IPV4VLAN_DST,
206 .offset = offsetof(struct ipv4_5tuple, ip_dst),
209 .type = RTE_ACL_FIELD_TYPE_RANGE,
210 .size = sizeof(uint16_t),
211 .field_index = SRCP_FIELD_IPV4,
212 .input_index = RTE_ACL_IPV4VLAN_PORTS,
213 .offset = offsetof(struct ipv4_5tuple, port_src),
216 .type = RTE_ACL_FIELD_TYPE_RANGE,
217 .size = sizeof(uint16_t),
218 .field_index = DSTP_FIELD_IPV4,
219 .input_index = RTE_ACL_IPV4VLAN_PORTS,
220 .offset = offsetof(struct ipv4_5tuple, port_dst),
224 #define IPV6_ADDR_LEN 16
225 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
226 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
230 uint32_t ip_src[IPV6_ADDR_U32];
231 uint32_t ip_dst[IPV6_ADDR_U32];
251 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
253 .type = RTE_ACL_FIELD_TYPE_BITMASK,
254 .size = sizeof(uint8_t),
255 .field_index = PROTO_FIELD_IPV6,
256 .input_index = PROTO_FIELD_IPV6,
257 .offset = offsetof(struct ipv6_5tuple, proto),
260 .type = RTE_ACL_FIELD_TYPE_MASK,
261 .size = sizeof(uint32_t),
262 .field_index = SRC1_FIELD_IPV6,
263 .input_index = SRC1_FIELD_IPV6,
264 .offset = offsetof(struct ipv6_5tuple, ip_src[0]),
267 .type = RTE_ACL_FIELD_TYPE_MASK,
268 .size = sizeof(uint32_t),
269 .field_index = SRC2_FIELD_IPV6,
270 .input_index = SRC2_FIELD_IPV6,
271 .offset = offsetof(struct ipv6_5tuple, ip_src[1]),
274 .type = RTE_ACL_FIELD_TYPE_MASK,
275 .size = sizeof(uint32_t),
276 .field_index = SRC3_FIELD_IPV6,
277 .input_index = SRC3_FIELD_IPV6,
278 .offset = offsetof(struct ipv6_5tuple, ip_src[2]),
281 .type = RTE_ACL_FIELD_TYPE_MASK,
282 .size = sizeof(uint32_t),
283 .field_index = SRC4_FIELD_IPV6,
284 .input_index = SRC4_FIELD_IPV6,
285 .offset = offsetof(struct ipv6_5tuple, ip_src[3]),
288 .type = RTE_ACL_FIELD_TYPE_MASK,
289 .size = sizeof(uint32_t),
290 .field_index = DST1_FIELD_IPV6,
291 .input_index = DST1_FIELD_IPV6,
292 .offset = offsetof(struct ipv6_5tuple, ip_dst[0]),
295 .type = RTE_ACL_FIELD_TYPE_MASK,
296 .size = sizeof(uint32_t),
297 .field_index = DST2_FIELD_IPV6,
298 .input_index = DST2_FIELD_IPV6,
299 .offset = offsetof(struct ipv6_5tuple, ip_dst[1]),
302 .type = RTE_ACL_FIELD_TYPE_MASK,
303 .size = sizeof(uint32_t),
304 .field_index = DST3_FIELD_IPV6,
305 .input_index = DST3_FIELD_IPV6,
306 .offset = offsetof(struct ipv6_5tuple, ip_dst[2]),
309 .type = RTE_ACL_FIELD_TYPE_MASK,
310 .size = sizeof(uint32_t),
311 .field_index = DST4_FIELD_IPV6,
312 .input_index = DST4_FIELD_IPV6,
313 .offset = offsetof(struct ipv6_5tuple, ip_dst[3]),
316 .type = RTE_ACL_FIELD_TYPE_RANGE,
317 .size = sizeof(uint16_t),
318 .field_index = SRCP_FIELD_IPV6,
319 .input_index = SRCP_FIELD_IPV6,
320 .offset = offsetof(struct ipv6_5tuple, port_src),
323 .type = RTE_ACL_FIELD_TYPE_RANGE,
324 .size = sizeof(uint16_t),
325 .field_index = DSTP_FIELD_IPV6,
326 .input_index = SRCP_FIELD_IPV6,
327 .offset = offsetof(struct ipv6_5tuple, port_dst),
337 CB_FLD_SRC_PORT_HIGH,
340 CB_FLD_DST_PORT_HIGH,
354 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
356 static const char cb_port_delim[] = ":";
358 static char line[LINE_MAX];
360 #define dump_verbose(lvl, fh, fmt, args...) do { \
361 if ((lvl) <= (int32_t)config.verbose) \
362 fprintf(fh, fmt, ##args); \
367 * Parse ClassBench input trace (test vectors and expected results) file.
369 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
370 * <src_port> <space> <dst_port> <space> <proto>
373 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
376 char *s, *sp, *in[CB_TRC_NUM];
377 static const char *dlm = " \t\n";
380 for (i = 0; i != RTE_DIM(in); i++) {
381 in[i] = strtok_r(s, dlm, &sp);
387 GET_CB_FIELD(in[CB_TRC_SRC_ADDR], v->ip_src, 0, UINT32_MAX, 0);
388 GET_CB_FIELD(in[CB_TRC_DST_ADDR], v->ip_dst, 0, UINT32_MAX, 0);
389 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
390 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
391 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
393 /* convert to network byte order. */
394 v->ip_src = rte_cpu_to_be_32(v->ip_src);
395 v->ip_dst = rte_cpu_to_be_32(v->ip_dst);
396 v->port_src = rte_cpu_to_be_16(v->port_src);
397 v->port_dst = rte_cpu_to_be_16(v->port_dst);
403 * Parses IPV6 address, exepcts the following format:
404 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
407 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
410 uint32_t addr[IPV6_ADDR_U16];
412 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
413 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
414 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
415 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
416 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
417 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
418 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
419 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
423 v[0] = (addr[0] << 16) + addr[1];
424 v[1] = (addr[2] << 16) + addr[3];
425 v[2] = (addr[4] << 16) + addr[5];
426 v[3] = (addr[6] << 16) + addr[7];
432 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
437 rc = parse_ipv6_addr(in, &end, v, 0);
441 v[0] = rte_cpu_to_be_32(v[0]);
442 v[1] = rte_cpu_to_be_32(v[1]);
443 v[2] = rte_cpu_to_be_32(v[2]);
444 v[3] = rte_cpu_to_be_32(v[3]);
450 * Parse ClassBench input trace (test vectors and expected results) file.
452 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
453 * <src_port> <space> <dst_port> <space> <proto>
456 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
459 char *s, *sp, *in[CB_TRC_NUM];
460 static const char *dlm = " \t\n";
463 for (i = 0; i != RTE_DIM(in); i++) {
464 in[i] = strtok_r(s, dlm, &sp);
470 /* get ip6 src address. */
471 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
475 /* get ip6 dst address. */
476 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
480 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
481 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
482 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
484 /* convert to network byte order. */
485 v->port_src = rte_cpu_to_be_16(v->port_src);
486 v->port_dst = rte_cpu_to_be_16(v->port_dst);
494 static const char name[] = APP_NAME;
498 struct ipv4_5tuple *v;
499 struct ipv6_5tuple *w;
501 sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
502 config.traces = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE,
504 if (config.traces == NULL)
505 rte_exit(EXIT_FAILURE, "Cannot allocate %zu bytes for "
506 "requested %u number of trace records\n",
507 sz, config.nb_traces);
509 f = fopen(config.trace_file, "r");
511 rte_exit(-EINVAL, "failed to open file: %s\n",
516 for (n = 0; n != config.nb_traces; n++) {
518 if (fgets(line, sizeof(line), f) == NULL)
522 if (parse_cb_ipv6_trace(line, w + n) != 0)
523 rte_exit(EXIT_FAILURE,
524 "%s: failed to parse ipv6 trace "
525 "record at line %u\n",
526 config.trace_file, n + 1);
528 if (parse_cb_ipv4_trace(line, v + n) != 0)
529 rte_exit(EXIT_FAILURE,
530 "%s: failed to parse ipv4 trace "
531 "record at line %u\n",
532 config.trace_file, n + 1);
536 config.used_traces = n;
541 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
546 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
549 rc = parse_ipv6_addr(in, &mp, v, '/');
554 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
556 /* put all together. */
557 for (i = 0; i != RTE_DIM(v); i++) {
558 if (m >= (i + 1) * nbu32)
559 field[i].mask_range.u32 = nbu32;
561 field[i].mask_range.u32 = m > (i * nbu32) ?
564 field[i].value.u32 = v[i];
572 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
575 char *s, *sp, *in[CB_FLD_NUM];
576 static const char *dlm = " \t\n";
581 if (strchr(str, '@') != str)
586 for (i = 0; i != RTE_DIM(in); i++) {
587 in[i] = strtok_r(s, dlm, &sp);
593 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
595 RTE_LOG(ERR, TESTACL,
596 "failed to read source address/mask: %s\n",
597 in[CB_FLD_SRC_ADDR]);
601 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
603 RTE_LOG(ERR, TESTACL,
604 "failed to read destination address/mask: %s\n",
605 in[CB_FLD_DST_ADDR]);
610 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
611 v->field[SRCP_FIELD_IPV6].value.u16,
613 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
614 v->field[SRCP_FIELD_IPV6].mask_range.u16,
617 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
618 sizeof(cb_port_delim)) != 0)
621 /* destination port. */
622 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
623 v->field[DSTP_FIELD_IPV6].value.u16,
625 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
626 v->field[DSTP_FIELD_IPV6].mask_range.u16,
629 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
630 sizeof(cb_port_delim)) != 0)
633 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
635 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
642 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
644 uint8_t a, b, c, d, m;
646 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
647 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
648 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
649 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
650 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
652 addr[0] = IPv4(a, b, c, d);
658 * Parse ClassBench rules file.
660 * '@'<src_ipv4_addr>'/'<masklen> <space> \
661 * <dst_ipv4_addr>'/'<masklen> <space> \
662 * <src_port_low> <space> ":" <src_port_high> <space> \
663 * <dst_port_low> <space> ":" <dst_port_high> <space> \
667 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
670 char *s, *sp, *in[CB_FLD_NUM];
671 static const char *dlm = " \t\n";
676 if (strchr(str, '@') != str)
681 for (i = 0; i != RTE_DIM(in); i++) {
682 in[i] = strtok_r(s, dlm, &sp);
688 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
689 &v->field[SRC_FIELD_IPV4].value.u32,
690 &v->field[SRC_FIELD_IPV4].mask_range.u32);
692 RTE_LOG(ERR, TESTACL,
693 "failed to read source address/mask: %s\n",
694 in[CB_FLD_SRC_ADDR]);
698 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
699 &v->field[DST_FIELD_IPV4].value.u32,
700 &v->field[DST_FIELD_IPV4].mask_range.u32);
702 RTE_LOG(ERR, TESTACL,
703 "failed to read destination address/mask: %s\n",
704 in[CB_FLD_DST_ADDR]);
709 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
710 v->field[SRCP_FIELD_IPV4].value.u16,
712 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
713 v->field[SRCP_FIELD_IPV4].mask_range.u16,
716 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
717 sizeof(cb_port_delim)) != 0)
720 /* destination port. */
721 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
722 v->field[DSTP_FIELD_IPV4].value.u16,
724 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
725 v->field[DSTP_FIELD_IPV4].mask_range.u16,
728 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
729 sizeof(cb_port_delim)) != 0)
732 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
734 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
740 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
743 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
750 memset(&v, 0, sizeof(v));
751 parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
753 for (n = 1; fgets(line, sizeof(line), f) != NULL; n++) {
755 rc = parser(line, &v);
757 RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
758 " failed, error code: %d (%s)\n",
759 n, rc, strerror(-rc));
763 v.data.category_mask = RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES,
764 typeof(v.data.category_mask));
765 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
768 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
770 RTE_LOG(ERR, TESTACL, "line %u: failed to add rules "
771 "into ACL context, error code: %d (%s)\n",
772 n, rc, strerror(-rc));
785 struct rte_acl_config cfg;
787 memset(&cfg, 0, sizeof(cfg));
789 /* setup ACL build config. */
791 cfg.num_fields = RTE_DIM(ipv6_defs);
792 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
794 cfg.num_fields = RTE_DIM(ipv4_defs);
795 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
797 cfg.num_categories = config.bld_categories;
798 cfg.max_size = config.max_size;
800 /* setup ACL creation parameters. */
801 prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
802 prm.max_rule_num = config.nb_rules;
804 config.acx = rte_acl_create(&prm);
805 if (config.acx == NULL)
806 rte_exit(rte_errno, "failed to create ACL context\n");
808 /* set default classify method for this context. */
809 if (config.alg.alg != RTE_ACL_CLASSIFY_DEFAULT) {
810 ret = rte_acl_set_ctx_classify(config.acx, config.alg.alg);
812 rte_exit(ret, "failed to setup %s method "
813 "for ACL context\n", config.alg.name);
817 f = fopen(config.rule_file, "r");
819 rte_exit(-EINVAL, "failed to open file %s\n",
822 ret = add_cb_rules(f, config.acx);
824 rte_exit(ret, "failed to add rules into ACL context\n");
829 ret = rte_acl_build(config.acx, &cfg);
831 dump_verbose(DUMP_NONE, stdout,
832 "rte_acl_build(%u) finished with %d\n",
833 config.bld_categories, ret);
835 rte_acl_dump(config.acx);
838 rte_exit(ret, "failed to build search context\n");
842 search_ip5tuples_once(uint32_t categories, uint32_t step, const char *alg)
845 uint32_t i, j, k, n, r;
846 const uint8_t *data[step], *v;
847 uint32_t results[step * categories];
850 for (i = 0; i != config.used_traces; i += n) {
852 n = RTE_MIN(step, config.used_traces - i);
854 for (j = 0; j != n; j++) {
856 v += config.trace_sz;
859 ret = rte_acl_classify(config.acx, data, results,
863 rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
864 config.ipv6 ? '6' : '4', ret);
866 for (r = 0, j = 0; j != n; j++) {
867 for (k = 0; k != categories; k++, r++) {
868 dump_verbose(DUMP_PKT, stdout,
869 "ipv%c_5tuple: %u, category: %u, "
871 config.ipv6 ? '6' : '4',
872 i + j + 1, k, results[r] - 1);
878 dump_verbose(DUMP_SEARCH, stdout,
879 "%s(%u, %u, %s) returns %u\n", __func__,
880 categories, step, alg, i);
885 search_ip5tuples(__attribute__((unused)) void *arg)
887 uint64_t pkt, start, tm;
890 lcore = rte_lcore_id();
894 for (i = 0; i != config.iter_num; i++) {
895 pkt += search_ip5tuples_once(config.run_categories,
896 config.trace_step, config.alg.name);
899 tm = rte_rdtsc() - start;
900 dump_verbose(DUMP_NONE, stdout,
901 "%s @lcore %u: %" PRIu32 " iterations, %" PRIu64 " pkts, %"
902 PRIu32 " categories, %" PRIu64 " cycles, %#Lf cycles/pkt\n",
903 __func__, lcore, i, pkt, config.run_categories,
904 tm, (pkt == 0) ? 0 : (long double)tm / pkt);
910 get_ulong_opt(const char *opt, const char *name, size_t min, size_t max)
916 val = strtoul(opt, &end, 0);
917 if (errno != 0 || end[0] != 0 || val > max || val < min)
918 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
924 get_alg_opt(const char *opt, const char *name)
928 for (i = 0; i != RTE_DIM(acl_alg); i++) {
929 if (strcmp(opt, acl_alg[i].name) == 0) {
930 config.alg = acl_alg[i];
935 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
940 print_usage(const char *prgname)
948 for (i = 0; i < RTE_DIM(acl_alg) - 1; i++) {
949 rc = snprintf(buf + n, sizeof(buf) - n, "%s|",
951 if (rc > sizeof(buf) - n)
956 snprintf(buf + n, sizeof(buf) - n, "%s", acl_alg[i].name);
960 "--" OPT_RULE_FILE "=<rules set file>\n"
961 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
963 "=<maximum number of rules for ACL context>]\n"
965 "=<number of traces to read binary file in>]\n"
967 "=<number of traces to classify per one call>]\n"
968 "[--" OPT_BLD_CATEGORIES
969 "=<number of categories to build with>]\n"
970 "[--" OPT_RUN_CATEGORIES
971 "=<number of categories to run with> "
972 "should be either 1 or multiple of %zu, "
973 "but not greater then %u]\n"
975 "=<size limit (in bytes) for runtime ACL strucutures> "
976 "leave 0 for default behaviour]\n"
977 "[--" OPT_ITER_NUM "=<number of iterations to perform>]\n"
978 "[--" OPT_VERBOSE "=<verbose level>]\n"
979 "[--" OPT_SEARCH_ALG "=%s]\n"
980 "[--" OPT_IPV6 "=<IPv6 rules and trace files>]\n",
981 prgname, RTE_ACL_RESULTS_MULTIPLIER,
982 (uint32_t)RTE_ACL_MAX_CATEGORIES,
989 fprintf(f, "%s:\n", __func__);
990 fprintf(f, "%s:%s\n", OPT_RULE_FILE, config.rule_file);
991 fprintf(f, "%s:%s\n", OPT_TRACE_FILE, config.trace_file);
992 fprintf(f, "%s:%u\n", OPT_RULE_NUM, config.nb_rules);
993 fprintf(f, "%s:%u\n", OPT_TRACE_NUM, config.nb_traces);
994 fprintf(f, "%s:%u\n", OPT_TRACE_STEP, config.trace_step);
995 fprintf(f, "%s:%u\n", OPT_BLD_CATEGORIES, config.bld_categories);
996 fprintf(f, "%s:%u\n", OPT_RUN_CATEGORIES, config.run_categories);
997 fprintf(f, "%s:%zu\n", OPT_MAX_SIZE, config.max_size);
998 fprintf(f, "%s:%u\n", OPT_ITER_NUM, config.iter_num);
999 fprintf(f, "%s:%u\n", OPT_VERBOSE, config.verbose);
1000 fprintf(f, "%s:%u(%s)\n", OPT_SEARCH_ALG, config.alg.alg,
1002 fprintf(f, "%s:%u\n", OPT_IPV6, config.ipv6);
1008 if (config.rule_file == NULL) {
1009 print_usage(config.prgname);
1010 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
1017 get_input_opts(int argc, char **argv)
1019 static struct option lgopts[] = {
1020 {OPT_RULE_FILE, 1, 0, 0},
1021 {OPT_TRACE_FILE, 1, 0, 0},
1022 {OPT_TRACE_NUM, 1, 0, 0},
1023 {OPT_RULE_NUM, 1, 0, 0},
1024 {OPT_MAX_SIZE, 1, 0, 0},
1025 {OPT_TRACE_STEP, 1, 0, 0},
1026 {OPT_BLD_CATEGORIES, 1, 0, 0},
1027 {OPT_RUN_CATEGORIES, 1, 0, 0},
1028 {OPT_ITER_NUM, 1, 0, 0},
1029 {OPT_VERBOSE, 1, 0, 0},
1030 {OPT_SEARCH_ALG, 1, 0, 0},
1031 {OPT_IPV6, 0, 0, 0},
1037 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
1040 print_usage(config.prgname);
1041 rte_exit(-EINVAL, "unknown option: %c", opt);
1044 if (strcmp(lgopts[opt_idx].name, OPT_RULE_FILE) == 0) {
1045 config.rule_file = optarg;
1046 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_FILE) == 0) {
1047 config.trace_file = optarg;
1048 } else if (strcmp(lgopts[opt_idx].name, OPT_RULE_NUM) == 0) {
1049 config.nb_rules = get_ulong_opt(optarg,
1050 lgopts[opt_idx].name, 1, RTE_ACL_MAX_INDEX + 1);
1051 } else if (strcmp(lgopts[opt_idx].name, OPT_MAX_SIZE) == 0) {
1052 config.max_size = get_ulong_opt(optarg,
1053 lgopts[opt_idx].name, 0, SIZE_MAX);
1054 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_NUM) == 0) {
1055 config.nb_traces = get_ulong_opt(optarg,
1056 lgopts[opt_idx].name, 1, UINT32_MAX);
1057 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_STEP) == 0) {
1058 config.trace_step = get_ulong_opt(optarg,
1059 lgopts[opt_idx].name, 1, TRACE_STEP_MAX);
1060 } else if (strcmp(lgopts[opt_idx].name,
1061 OPT_BLD_CATEGORIES) == 0) {
1062 config.bld_categories = get_ulong_opt(optarg,
1063 lgopts[opt_idx].name, 1,
1064 RTE_ACL_MAX_CATEGORIES);
1065 } else if (strcmp(lgopts[opt_idx].name,
1066 OPT_RUN_CATEGORIES) == 0) {
1067 config.run_categories = get_ulong_opt(optarg,
1068 lgopts[opt_idx].name, 1,
1069 RTE_ACL_MAX_CATEGORIES);
1070 } else if (strcmp(lgopts[opt_idx].name, OPT_ITER_NUM) == 0) {
1071 config.iter_num = get_ulong_opt(optarg,
1072 lgopts[opt_idx].name, 1, INT32_MAX);
1073 } else if (strcmp(lgopts[opt_idx].name, OPT_VERBOSE) == 0) {
1074 config.verbose = get_ulong_opt(optarg,
1075 lgopts[opt_idx].name, DUMP_NONE, DUMP_MAX);
1076 } else if (strcmp(lgopts[opt_idx].name,
1077 OPT_SEARCH_ALG) == 0) {
1078 get_alg_opt(optarg, lgopts[opt_idx].name);
1079 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1083 config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1084 sizeof(struct ipv4_5tuple);
1089 main(int argc, char **argv)
1094 ret = rte_eal_init(argc, argv);
1096 rte_panic("Cannot init EAL\n");
1101 config.prgname = argv[0];
1103 get_input_opts(argc, argv);
1104 dump_config(stdout);
1109 if (config.trace_file != NULL)
1112 RTE_LCORE_FOREACH_SLAVE(lcore)
1113 rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1115 search_ip5tuples(NULL);
1117 rte_eal_mp_wait_lcore();
1119 rte_acl_free(config.acx);