1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_string_fns.h>
10 #include <rte_cycles.h>
11 #include <rte_per_lcore.h>
12 #include <rte_lcore.h>
15 #define PRINT_USAGE_START "%s [EAL options] --\n"
17 #define RTE_LOGTYPE_TESTACL RTE_LOGTYPE_USER1
19 #define APP_NAME "TESTACL"
21 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
25 val = strtoul((in), &end_fld, (base)); \
26 if (errno != 0 || end_fld[0] != (dlm) || val > (lim)) \
28 (fd) = (typeof(fd))val; \
32 #define OPT_RULE_FILE "rulesf"
33 #define OPT_TRACE_FILE "tracef"
34 #define OPT_RULE_NUM "rulenum"
35 #define OPT_TRACE_NUM "tracenum"
36 #define OPT_TRACE_STEP "tracestep"
37 #define OPT_SEARCH_ALG "alg"
38 #define OPT_BLD_CATEGORIES "bldcat"
39 #define OPT_RUN_CATEGORIES "runcat"
40 #define OPT_MAX_SIZE "maxsize"
41 #define OPT_ITER_NUM "iter"
42 #define OPT_VERBOSE "verbose"
43 #define OPT_IPV6 "ipv6"
45 #define TRACE_DEFAULT_NUM 0x10000
46 #define TRACE_STEP_MAX 0x1000
47 #define TRACE_STEP_DEF 0x100
49 #define RULE_NUM 0x10000
51 #define COMMENT_LEAD_CHAR '#'
62 enum rte_acl_classify_alg alg;
65 static const struct acl_alg acl_alg[] = {
68 .alg = RTE_ACL_CLASSIFY_SCALAR,
72 .alg = RTE_ACL_CLASSIFY_SSE,
76 .alg = RTE_ACL_CLASSIFY_AVX2,
80 .alg = RTE_ACL_CLASSIFY_NEON,
84 .alg = RTE_ACL_CLASSIFY_ALTIVEC,
88 .alg = RTE_ACL_CLASSIFY_AVX512X16,
92 .alg = RTE_ACL_CLASSIFY_AVX512X32,
98 const char *rule_file;
99 const char *trace_file;
101 uint32_t bld_categories;
102 uint32_t run_categories;
111 uint32_t used_traces;
113 struct rte_acl_ctx *acx;
117 .nb_rules = RULE_NUM,
118 .nb_traces = TRACE_DEFAULT_NUM,
119 .trace_step = TRACE_STEP_DEF,
124 .alg = RTE_ACL_CLASSIFY_DEFAULT,
129 static struct rte_acl_param prm = {
131 .socket_id = SOCKET_ID_ANY,
135 * Rule and trace formats definitions.
156 * That effectively defines order of IPV4VLAN classifications:
158 * - VLAN (TAG and DOMAIN)
161 * - PORTS (SRC and DST)
164 RTE_ACL_IPV4VLAN_PROTO,
165 RTE_ACL_IPV4VLAN_VLAN,
166 RTE_ACL_IPV4VLAN_SRC,
167 RTE_ACL_IPV4VLAN_DST,
168 RTE_ACL_IPV4VLAN_PORTS,
172 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
174 .type = RTE_ACL_FIELD_TYPE_BITMASK,
175 .size = sizeof(uint8_t),
176 .field_index = PROTO_FIELD_IPV4,
177 .input_index = RTE_ACL_IPV4VLAN_PROTO,
178 .offset = offsetof(struct ipv4_5tuple, proto),
181 .type = RTE_ACL_FIELD_TYPE_MASK,
182 .size = sizeof(uint32_t),
183 .field_index = SRC_FIELD_IPV4,
184 .input_index = RTE_ACL_IPV4VLAN_SRC,
185 .offset = offsetof(struct ipv4_5tuple, ip_src),
188 .type = RTE_ACL_FIELD_TYPE_MASK,
189 .size = sizeof(uint32_t),
190 .field_index = DST_FIELD_IPV4,
191 .input_index = RTE_ACL_IPV4VLAN_DST,
192 .offset = offsetof(struct ipv4_5tuple, ip_dst),
195 .type = RTE_ACL_FIELD_TYPE_RANGE,
196 .size = sizeof(uint16_t),
197 .field_index = SRCP_FIELD_IPV4,
198 .input_index = RTE_ACL_IPV4VLAN_PORTS,
199 .offset = offsetof(struct ipv4_5tuple, port_src),
202 .type = RTE_ACL_FIELD_TYPE_RANGE,
203 .size = sizeof(uint16_t),
204 .field_index = DSTP_FIELD_IPV4,
205 .input_index = RTE_ACL_IPV4VLAN_PORTS,
206 .offset = offsetof(struct ipv4_5tuple, port_dst),
210 #define IPV6_ADDR_LEN 16
211 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
212 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
216 uint32_t ip_src[IPV6_ADDR_U32];
217 uint32_t ip_dst[IPV6_ADDR_U32];
237 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
239 .type = RTE_ACL_FIELD_TYPE_BITMASK,
240 .size = sizeof(uint8_t),
241 .field_index = PROTO_FIELD_IPV6,
242 .input_index = PROTO_FIELD_IPV6,
243 .offset = offsetof(struct ipv6_5tuple, proto),
246 .type = RTE_ACL_FIELD_TYPE_MASK,
247 .size = sizeof(uint32_t),
248 .field_index = SRC1_FIELD_IPV6,
249 .input_index = SRC1_FIELD_IPV6,
250 .offset = offsetof(struct ipv6_5tuple, ip_src[0]),
253 .type = RTE_ACL_FIELD_TYPE_MASK,
254 .size = sizeof(uint32_t),
255 .field_index = SRC2_FIELD_IPV6,
256 .input_index = SRC2_FIELD_IPV6,
257 .offset = offsetof(struct ipv6_5tuple, ip_src[1]),
260 .type = RTE_ACL_FIELD_TYPE_MASK,
261 .size = sizeof(uint32_t),
262 .field_index = SRC3_FIELD_IPV6,
263 .input_index = SRC3_FIELD_IPV6,
264 .offset = offsetof(struct ipv6_5tuple, ip_src[2]),
267 .type = RTE_ACL_FIELD_TYPE_MASK,
268 .size = sizeof(uint32_t),
269 .field_index = SRC4_FIELD_IPV6,
270 .input_index = SRC4_FIELD_IPV6,
271 .offset = offsetof(struct ipv6_5tuple, ip_src[3]),
274 .type = RTE_ACL_FIELD_TYPE_MASK,
275 .size = sizeof(uint32_t),
276 .field_index = DST1_FIELD_IPV6,
277 .input_index = DST1_FIELD_IPV6,
278 .offset = offsetof(struct ipv6_5tuple, ip_dst[0]),
281 .type = RTE_ACL_FIELD_TYPE_MASK,
282 .size = sizeof(uint32_t),
283 .field_index = DST2_FIELD_IPV6,
284 .input_index = DST2_FIELD_IPV6,
285 .offset = offsetof(struct ipv6_5tuple, ip_dst[1]),
288 .type = RTE_ACL_FIELD_TYPE_MASK,
289 .size = sizeof(uint32_t),
290 .field_index = DST3_FIELD_IPV6,
291 .input_index = DST3_FIELD_IPV6,
292 .offset = offsetof(struct ipv6_5tuple, ip_dst[2]),
295 .type = RTE_ACL_FIELD_TYPE_MASK,
296 .size = sizeof(uint32_t),
297 .field_index = DST4_FIELD_IPV6,
298 .input_index = DST4_FIELD_IPV6,
299 .offset = offsetof(struct ipv6_5tuple, ip_dst[3]),
302 .type = RTE_ACL_FIELD_TYPE_RANGE,
303 .size = sizeof(uint16_t),
304 .field_index = SRCP_FIELD_IPV6,
305 .input_index = SRCP_FIELD_IPV6,
306 .offset = offsetof(struct ipv6_5tuple, port_src),
309 .type = RTE_ACL_FIELD_TYPE_RANGE,
310 .size = sizeof(uint16_t),
311 .field_index = DSTP_FIELD_IPV6,
312 .input_index = SRCP_FIELD_IPV6,
313 .offset = offsetof(struct ipv6_5tuple, port_dst),
323 CB_FLD_SRC_PORT_HIGH,
326 CB_FLD_DST_PORT_HIGH,
340 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
342 static const char cb_port_delim[] = ":";
344 static char line[LINE_MAX];
346 #define dump_verbose(lvl, fh, fmt, args...) do { \
347 if ((lvl) <= (int32_t)config.verbose) \
348 fprintf(fh, fmt, ##args); \
353 * Parse ClassBench input trace (test vectors and expected results) file.
355 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
356 * <src_port> <space> <dst_port> <space> <proto>
359 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
362 char *s, *sp, *in[CB_TRC_NUM];
363 static const char *dlm = " \t\n";
366 for (i = 0; i != RTE_DIM(in); i++) {
367 in[i] = strtok_r(s, dlm, &sp);
373 GET_CB_FIELD(in[CB_TRC_SRC_ADDR], v->ip_src, 0, UINT32_MAX, 0);
374 GET_CB_FIELD(in[CB_TRC_DST_ADDR], v->ip_dst, 0, UINT32_MAX, 0);
375 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
376 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
377 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
379 /* convert to network byte order. */
380 v->ip_src = rte_cpu_to_be_32(v->ip_src);
381 v->ip_dst = rte_cpu_to_be_32(v->ip_dst);
382 v->port_src = rte_cpu_to_be_16(v->port_src);
383 v->port_dst = rte_cpu_to_be_16(v->port_dst);
389 * Parses IPV6 address, exepcts the following format:
390 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
393 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
396 uint32_t addr[IPV6_ADDR_U16];
398 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
399 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
400 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
401 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
402 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
403 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
404 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
405 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
409 v[0] = (addr[0] << 16) + addr[1];
410 v[1] = (addr[2] << 16) + addr[3];
411 v[2] = (addr[4] << 16) + addr[5];
412 v[3] = (addr[6] << 16) + addr[7];
418 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
423 rc = parse_ipv6_addr(in, &end, v, 0);
427 v[0] = rte_cpu_to_be_32(v[0]);
428 v[1] = rte_cpu_to_be_32(v[1]);
429 v[2] = rte_cpu_to_be_32(v[2]);
430 v[3] = rte_cpu_to_be_32(v[3]);
436 * Parse ClassBench input trace (test vectors and expected results) file.
438 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
439 * <src_port> <space> <dst_port> <space> <proto>
442 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
445 char *s, *sp, *in[CB_TRC_NUM];
446 static const char *dlm = " \t\n";
449 for (i = 0; i != RTE_DIM(in); i++) {
450 in[i] = strtok_r(s, dlm, &sp);
456 /* get ip6 src address. */
457 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
461 /* get ip6 dst address. */
462 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
466 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
467 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
468 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
470 /* convert to network byte order. */
471 v->port_src = rte_cpu_to_be_16(v->port_src);
472 v->port_dst = rte_cpu_to_be_16(v->port_dst);
477 /* Bypass comment and empty lines */
479 skip_line(const char *buf)
483 for (i = 0; isspace(buf[i]) != 0; i++)
486 if (buf[i] == 0 || buf[i] == COMMENT_LEAD_CHAR)
495 static const char name[] = APP_NAME;
499 struct ipv4_5tuple *v;
500 struct ipv6_5tuple *w;
502 sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
503 config.traces = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE,
505 if (config.traces == NULL)
506 rte_exit(EXIT_FAILURE, "Cannot allocate %zu bytes for "
507 "requested %u number of trace records\n",
508 sz, config.nb_traces);
510 f = fopen(config.trace_file, "r");
512 rte_exit(-EINVAL, "failed to open file: %s\n",
519 for (i = 0; n != config.nb_traces; i++) {
521 if (fgets(line, sizeof(line), f) == NULL)
524 if (skip_line(line) != 0) {
532 if (parse_cb_ipv6_trace(line, w + n) != 0)
533 rte_exit(EXIT_FAILURE,
534 "%s: failed to parse ipv6 trace "
535 "record at line %u\n",
536 config.trace_file, i + 1);
538 if (parse_cb_ipv4_trace(line, v + n) != 0)
539 rte_exit(EXIT_FAILURE,
540 "%s: failed to parse ipv4 trace "
541 "record at line %u\n",
542 config.trace_file, i + 1);
546 config.used_traces = i - k;
551 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
556 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
559 rc = parse_ipv6_addr(in, &mp, v, '/');
564 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
566 /* put all together. */
567 for (i = 0; i != RTE_DIM(v); i++) {
568 if (m >= (i + 1) * nbu32)
569 field[i].mask_range.u32 = nbu32;
571 field[i].mask_range.u32 = m > (i * nbu32) ?
574 field[i].value.u32 = v[i];
582 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
585 char *s, *sp, *in[CB_FLD_NUM];
586 static const char *dlm = " \t\n";
591 if (strchr(str, '@') != str)
596 for (i = 0; i != RTE_DIM(in); i++) {
597 in[i] = strtok_r(s, dlm, &sp);
603 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
605 RTE_LOG(ERR, TESTACL,
606 "failed to read source address/mask: %s\n",
607 in[CB_FLD_SRC_ADDR]);
611 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
613 RTE_LOG(ERR, TESTACL,
614 "failed to read destination address/mask: %s\n",
615 in[CB_FLD_DST_ADDR]);
620 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
621 v->field[SRCP_FIELD_IPV6].value.u16,
623 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
624 v->field[SRCP_FIELD_IPV6].mask_range.u16,
627 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
628 sizeof(cb_port_delim)) != 0)
631 /* destination port. */
632 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
633 v->field[DSTP_FIELD_IPV6].value.u16,
635 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
636 v->field[DSTP_FIELD_IPV6].mask_range.u16,
639 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
640 sizeof(cb_port_delim)) != 0)
643 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
645 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
652 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
654 uint8_t a, b, c, d, m;
656 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
657 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
658 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
659 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
660 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
662 addr[0] = RTE_IPV4(a, b, c, d);
668 * Parse ClassBench rules file.
670 * '@'<src_ipv4_addr>'/'<masklen> <space> \
671 * <dst_ipv4_addr>'/'<masklen> <space> \
672 * <src_port_low> <space> ":" <src_port_high> <space> \
673 * <dst_port_low> <space> ":" <dst_port_high> <space> \
677 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
680 char *s, *sp, *in[CB_FLD_NUM];
681 static const char *dlm = " \t\n";
686 if (strchr(str, '@') != str)
691 for (i = 0; i != RTE_DIM(in); i++) {
692 in[i] = strtok_r(s, dlm, &sp);
698 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
699 &v->field[SRC_FIELD_IPV4].value.u32,
700 &v->field[SRC_FIELD_IPV4].mask_range.u32);
702 RTE_LOG(ERR, TESTACL,
703 "failed to read source address/mask: %s\n",
704 in[CB_FLD_SRC_ADDR]);
708 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
709 &v->field[DST_FIELD_IPV4].value.u32,
710 &v->field[DST_FIELD_IPV4].mask_range.u32);
712 RTE_LOG(ERR, TESTACL,
713 "failed to read destination address/mask: %s\n",
714 in[CB_FLD_DST_ADDR]);
719 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
720 v->field[SRCP_FIELD_IPV4].value.u16,
722 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
723 v->field[SRCP_FIELD_IPV4].mask_range.u16,
726 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
727 sizeof(cb_port_delim)) != 0)
730 /* destination port. */
731 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
732 v->field[DSTP_FIELD_IPV4].value.u16,
734 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
735 v->field[DSTP_FIELD_IPV4].mask_range.u16,
738 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
739 sizeof(cb_port_delim)) != 0)
742 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
744 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
750 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
753 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
760 memset(&v, 0, sizeof(v));
761 parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
764 for (i = 1; fgets(line, sizeof(line), f) != NULL; i++) {
766 if (skip_line(line) != 0) {
772 rc = parser(line, &v);
774 RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
775 " failed, error code: %d (%s)\n",
776 i, rc, strerror(-rc));
780 v.data.category_mask = RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES,
781 typeof(v.data.category_mask));
782 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
785 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
787 RTE_LOG(ERR, TESTACL, "line %u: failed to add rules "
788 "into ACL context, error code: %d (%s)\n",
789 i, rc, strerror(-rc));
802 struct rte_acl_config cfg;
804 memset(&cfg, 0, sizeof(cfg));
806 /* setup ACL build config. */
808 cfg.num_fields = RTE_DIM(ipv6_defs);
809 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
811 cfg.num_fields = RTE_DIM(ipv4_defs);
812 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
814 cfg.num_categories = config.bld_categories;
815 cfg.max_size = config.max_size;
817 /* setup ACL creation parameters. */
818 prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
819 prm.max_rule_num = config.nb_rules;
821 config.acx = rte_acl_create(&prm);
822 if (config.acx == NULL)
823 rte_exit(rte_errno, "failed to create ACL context\n");
825 /* set default classify method for this context. */
826 if (config.alg.alg != RTE_ACL_CLASSIFY_DEFAULT) {
827 ret = rte_acl_set_ctx_classify(config.acx, config.alg.alg);
829 rte_exit(ret, "failed to setup %s method "
830 "for ACL context\n", config.alg.name);
834 f = fopen(config.rule_file, "r");
836 rte_exit(-EINVAL, "failed to open file %s\n",
839 ret = add_cb_rules(f, config.acx);
841 rte_exit(ret, "failed to add rules into ACL context\n");
846 ret = rte_acl_build(config.acx, &cfg);
848 dump_verbose(DUMP_NONE, stdout,
849 "rte_acl_build(%u) finished with %d\n",
850 config.bld_categories, ret);
852 rte_acl_dump(config.acx);
855 rte_exit(ret, "failed to build search context\n");
859 search_ip5tuples_once(uint32_t categories, uint32_t step, const char *alg)
862 uint32_t i, j, k, n, r;
863 const uint8_t *data[step], *v;
864 uint32_t results[step * categories];
867 for (i = 0; i != config.used_traces; i += n) {
869 n = RTE_MIN(step, config.used_traces - i);
871 for (j = 0; j != n; j++) {
873 v += config.trace_sz;
876 ret = rte_acl_classify(config.acx, data, results,
880 rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
881 config.ipv6 ? '6' : '4', ret);
883 for (r = 0, j = 0; j != n; j++) {
884 for (k = 0; k != categories; k++, r++) {
885 dump_verbose(DUMP_PKT, stdout,
886 "ipv%c_5tuple: %u, category: %u, "
888 config.ipv6 ? '6' : '4',
889 i + j + 1, k, results[r] - 1);
895 dump_verbose(DUMP_SEARCH, stdout,
896 "%s(%u, %u, %s) returns %u\n", __func__,
897 categories, step, alg, i);
902 search_ip5tuples(__rte_unused void *arg)
904 uint64_t pkt, start, tm;
908 lcore = rte_lcore_id();
909 start = rte_rdtsc_precise();
912 for (i = 0; i != config.iter_num; i++) {
913 pkt += search_ip5tuples_once(config.run_categories,
914 config.trace_step, config.alg.name);
917 tm = rte_rdtsc_precise() - start;
919 st = (long double)tm / rte_get_timer_hz();
920 dump_verbose(DUMP_NONE, stdout,
921 "%s @lcore %u: %" PRIu32 " iterations, %" PRIu64 " pkts, %"
922 PRIu32 " categories, %" PRIu64 " cycles (%.2Lf sec), "
923 "%.2Lf cycles/pkt, %.2Lf pkt/sec\n",
924 __func__, lcore, i, pkt,
925 config.run_categories, tm, st,
926 (pkt == 0) ? 0 : (long double)tm / pkt, pkt / st);
932 get_ulong_opt(const char *opt, const char *name, size_t min, size_t max)
938 val = strtoul(opt, &end, 0);
939 if (errno != 0 || end[0] != 0 || val > max || val < min)
940 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
946 get_alg_opt(const char *opt, const char *name)
950 for (i = 0; i != RTE_DIM(acl_alg); i++) {
951 if (strcmp(opt, acl_alg[i].name) == 0) {
952 config.alg = acl_alg[i];
957 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
962 print_usage(const char *prgname)
970 for (i = 0; i < RTE_DIM(acl_alg) - 1; i++) {
971 rc = snprintf(buf + n, sizeof(buf) - n, "%s|",
973 if (rc > sizeof(buf) - n)
978 strlcpy(buf + n, acl_alg[i].name, sizeof(buf) - n);
982 "--" OPT_RULE_FILE "=<rules set file>\n"
983 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
985 "=<maximum number of rules for ACL context>]\n"
987 "=<number of traces to read binary file in>]\n"
989 "=<number of traces to classify per one call>]\n"
990 "[--" OPT_BLD_CATEGORIES
991 "=<number of categories to build with>]\n"
992 "[--" OPT_RUN_CATEGORIES
993 "=<number of categories to run with> "
994 "should be either 1 or multiple of %zu, "
995 "but not greater then %u]\n"
997 "=<size limit (in bytes) for runtime ACL strucutures> "
998 "leave 0 for default behaviour]\n"
999 "[--" OPT_ITER_NUM "=<number of iterations to perform>]\n"
1000 "[--" OPT_VERBOSE "=<verbose level>]\n"
1001 "[--" OPT_SEARCH_ALG "=%s]\n"
1002 "[--" OPT_IPV6 "=<IPv6 rules and trace files>]\n",
1003 prgname, RTE_ACL_RESULTS_MULTIPLIER,
1004 (uint32_t)RTE_ACL_MAX_CATEGORIES,
1009 dump_config(FILE *f)
1011 fprintf(f, "%s:\n", __func__);
1012 fprintf(f, "%s:%s\n", OPT_RULE_FILE, config.rule_file);
1013 fprintf(f, "%s:%s\n", OPT_TRACE_FILE, config.trace_file);
1014 fprintf(f, "%s:%u\n", OPT_RULE_NUM, config.nb_rules);
1015 fprintf(f, "%s:%u\n", OPT_TRACE_NUM, config.nb_traces);
1016 fprintf(f, "%s:%u\n", OPT_TRACE_STEP, config.trace_step);
1017 fprintf(f, "%s:%u\n", OPT_BLD_CATEGORIES, config.bld_categories);
1018 fprintf(f, "%s:%u\n", OPT_RUN_CATEGORIES, config.run_categories);
1019 fprintf(f, "%s:%zu\n", OPT_MAX_SIZE, config.max_size);
1020 fprintf(f, "%s:%u\n", OPT_ITER_NUM, config.iter_num);
1021 fprintf(f, "%s:%u\n", OPT_VERBOSE, config.verbose);
1022 fprintf(f, "%s:%u(%s)\n", OPT_SEARCH_ALG, config.alg.alg,
1024 fprintf(f, "%s:%u\n", OPT_IPV6, config.ipv6);
1030 if (config.rule_file == NULL) {
1031 print_usage(config.prgname);
1032 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
1039 get_input_opts(int argc, char **argv)
1041 static struct option lgopts[] = {
1042 {OPT_RULE_FILE, 1, 0, 0},
1043 {OPT_TRACE_FILE, 1, 0, 0},
1044 {OPT_TRACE_NUM, 1, 0, 0},
1045 {OPT_RULE_NUM, 1, 0, 0},
1046 {OPT_MAX_SIZE, 1, 0, 0},
1047 {OPT_TRACE_STEP, 1, 0, 0},
1048 {OPT_BLD_CATEGORIES, 1, 0, 0},
1049 {OPT_RUN_CATEGORIES, 1, 0, 0},
1050 {OPT_ITER_NUM, 1, 0, 0},
1051 {OPT_VERBOSE, 1, 0, 0},
1052 {OPT_SEARCH_ALG, 1, 0, 0},
1053 {OPT_IPV6, 0, 0, 0},
1059 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
1062 print_usage(config.prgname);
1063 rte_exit(-EINVAL, "unknown option: %c", opt);
1066 if (strcmp(lgopts[opt_idx].name, OPT_RULE_FILE) == 0) {
1067 config.rule_file = optarg;
1068 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_FILE) == 0) {
1069 config.trace_file = optarg;
1070 } else if (strcmp(lgopts[opt_idx].name, OPT_RULE_NUM) == 0) {
1071 config.nb_rules = get_ulong_opt(optarg,
1072 lgopts[opt_idx].name, 1, RTE_ACL_MAX_INDEX + 1);
1073 } else if (strcmp(lgopts[opt_idx].name, OPT_MAX_SIZE) == 0) {
1074 config.max_size = get_ulong_opt(optarg,
1075 lgopts[opt_idx].name, 0, SIZE_MAX);
1076 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_NUM) == 0) {
1077 config.nb_traces = get_ulong_opt(optarg,
1078 lgopts[opt_idx].name, 1, UINT32_MAX);
1079 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_STEP) == 0) {
1080 config.trace_step = get_ulong_opt(optarg,
1081 lgopts[opt_idx].name, 1, TRACE_STEP_MAX);
1082 } else if (strcmp(lgopts[opt_idx].name,
1083 OPT_BLD_CATEGORIES) == 0) {
1084 config.bld_categories = get_ulong_opt(optarg,
1085 lgopts[opt_idx].name, 1,
1086 RTE_ACL_MAX_CATEGORIES);
1087 } else if (strcmp(lgopts[opt_idx].name,
1088 OPT_RUN_CATEGORIES) == 0) {
1089 config.run_categories = get_ulong_opt(optarg,
1090 lgopts[opt_idx].name, 1,
1091 RTE_ACL_MAX_CATEGORIES);
1092 } else if (strcmp(lgopts[opt_idx].name, OPT_ITER_NUM) == 0) {
1093 config.iter_num = get_ulong_opt(optarg,
1094 lgopts[opt_idx].name, 1, INT32_MAX);
1095 } else if (strcmp(lgopts[opt_idx].name, OPT_VERBOSE) == 0) {
1096 config.verbose = get_ulong_opt(optarg,
1097 lgopts[opt_idx].name, DUMP_NONE, DUMP_MAX);
1098 } else if (strcmp(lgopts[opt_idx].name,
1099 OPT_SEARCH_ALG) == 0) {
1100 get_alg_opt(optarg, lgopts[opt_idx].name);
1101 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1105 config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1106 sizeof(struct ipv4_5tuple);
1111 main(int argc, char **argv)
1116 ret = rte_eal_init(argc, argv);
1118 rte_panic("Cannot init EAL\n");
1123 config.prgname = argv[0];
1125 get_input_opts(argc, argv);
1126 dump_config(stdout);
1131 if (config.trace_file != NULL)
1134 RTE_LCORE_FOREACH_WORKER(lcore)
1135 rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1137 search_ip5tuples(NULL);
1139 rte_eal_mp_wait_lcore();
1141 rte_acl_free(config.acx);