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 #ifndef RTE_LIBRTE_ACL_STANDALONE
40 #include <rte_cycles.h>
41 #include <rte_per_lcore.h>
42 #include <rte_lcore.h>
45 #define PRINT_USAGE_START "%s [EAL options]\n"
49 #define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
50 (((b) & 0xff) << 16) | \
51 (((c) & 0xff) << 8) | \
54 #define RTE_LCORE_FOREACH_SLAVE(x) while (((x) = 0))
56 #define rte_eal_remote_launch(a, b, c) DUMMY_MACRO
57 #define rte_eal_mp_wait_lcore() DUMMY_MACRO
59 #define rte_eal_init(c, v) (0)
61 #define PRINT_USAGE_START "%s\n"
63 #endif /*RTE_LIBRTE_ACL_STANDALONE */
65 #define RTE_LOGTYPE_TESTACL RTE_LOGTYPE_USER1
67 #define APP_NAME "TESTACL"
69 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
73 val = strtoul((in), &end_fld, (base)); \
74 if (errno != 0 || end_fld[0] != (dlm) || val > (lim)) \
76 (fd) = (typeof(fd))val; \
80 #define OPT_RULE_FILE "rulesf"
81 #define OPT_TRACE_FILE "tracef"
82 #define OPT_RULE_NUM "rulenum"
83 #define OPT_TRACE_NUM "tracenum"
84 #define OPT_TRACE_STEP "tracestep"
85 #define OPT_SEARCH_ALG "alg"
86 #define OPT_BLD_CATEGORIES "bldcat"
87 #define OPT_RUN_CATEGORIES "runcat"
88 #define OPT_MAX_SIZE "maxsize"
89 #define OPT_ITER_NUM "iter"
90 #define OPT_VERBOSE "verbose"
91 #define OPT_IPV6 "ipv6"
93 #define TRACE_DEFAULT_NUM 0x10000
94 #define TRACE_STEP_MAX 0x1000
95 #define TRACE_STEP_DEF 0x100
97 #define RULE_NUM 0x10000
108 enum rte_acl_classify_alg alg;
111 static const struct acl_alg acl_alg[] = {
114 .alg = RTE_ACL_CLASSIFY_SCALAR,
118 .alg = RTE_ACL_CLASSIFY_SSE,
122 .alg = RTE_ACL_CLASSIFY_AVX2,
128 const char *rule_file;
129 const char *trace_file;
131 uint32_t bld_categories;
132 uint32_t run_categories;
141 uint32_t used_traces;
143 struct rte_acl_ctx *acx;
147 .nb_rules = RULE_NUM,
148 .nb_traces = TRACE_DEFAULT_NUM,
149 .trace_step = TRACE_STEP_DEF,
154 .alg = RTE_ACL_CLASSIFY_DEFAULT,
159 static struct rte_acl_param prm = {
161 .socket_id = SOCKET_ID_ANY,
165 * Rule and trace formats definitions.
185 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
187 .type = RTE_ACL_FIELD_TYPE_BITMASK,
188 .size = sizeof(uint8_t),
189 .field_index = PROTO_FIELD_IPV4,
190 .input_index = RTE_ACL_IPV4VLAN_PROTO,
191 .offset = offsetof(struct ipv4_5tuple, proto),
194 .type = RTE_ACL_FIELD_TYPE_MASK,
195 .size = sizeof(uint32_t),
196 .field_index = SRC_FIELD_IPV4,
197 .input_index = RTE_ACL_IPV4VLAN_SRC,
198 .offset = offsetof(struct ipv4_5tuple, ip_src),
201 .type = RTE_ACL_FIELD_TYPE_MASK,
202 .size = sizeof(uint32_t),
203 .field_index = DST_FIELD_IPV4,
204 .input_index = RTE_ACL_IPV4VLAN_DST,
205 .offset = offsetof(struct ipv4_5tuple, ip_dst),
208 .type = RTE_ACL_FIELD_TYPE_RANGE,
209 .size = sizeof(uint16_t),
210 .field_index = SRCP_FIELD_IPV4,
211 .input_index = RTE_ACL_IPV4VLAN_PORTS,
212 .offset = offsetof(struct ipv4_5tuple, port_src),
215 .type = RTE_ACL_FIELD_TYPE_RANGE,
216 .size = sizeof(uint16_t),
217 .field_index = DSTP_FIELD_IPV4,
218 .input_index = RTE_ACL_IPV4VLAN_PORTS,
219 .offset = offsetof(struct ipv4_5tuple, port_dst),
223 #define IPV6_ADDR_LEN 16
224 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
225 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
229 uint32_t ip_src[IPV6_ADDR_U32];
230 uint32_t ip_dst[IPV6_ADDR_U32];
250 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
252 .type = RTE_ACL_FIELD_TYPE_BITMASK,
253 .size = sizeof(uint8_t),
254 .field_index = PROTO_FIELD_IPV6,
255 .input_index = PROTO_FIELD_IPV6,
256 .offset = offsetof(struct ipv6_5tuple, proto),
259 .type = RTE_ACL_FIELD_TYPE_MASK,
260 .size = sizeof(uint32_t),
261 .field_index = SRC1_FIELD_IPV6,
262 .input_index = SRC1_FIELD_IPV6,
263 .offset = offsetof(struct ipv6_5tuple, ip_src[0]),
266 .type = RTE_ACL_FIELD_TYPE_MASK,
267 .size = sizeof(uint32_t),
268 .field_index = SRC2_FIELD_IPV6,
269 .input_index = SRC2_FIELD_IPV6,
270 .offset = offsetof(struct ipv6_5tuple, ip_src[1]),
273 .type = RTE_ACL_FIELD_TYPE_MASK,
274 .size = sizeof(uint32_t),
275 .field_index = SRC3_FIELD_IPV6,
276 .input_index = SRC3_FIELD_IPV6,
277 .offset = offsetof(struct ipv6_5tuple, ip_src[2]),
280 .type = RTE_ACL_FIELD_TYPE_MASK,
281 .size = sizeof(uint32_t),
282 .field_index = SRC4_FIELD_IPV6,
283 .input_index = SRC4_FIELD_IPV6,
284 .offset = offsetof(struct ipv6_5tuple, ip_src[3]),
287 .type = RTE_ACL_FIELD_TYPE_MASK,
288 .size = sizeof(uint32_t),
289 .field_index = DST1_FIELD_IPV6,
290 .input_index = DST1_FIELD_IPV6,
291 .offset = offsetof(struct ipv6_5tuple, ip_dst[0]),
294 .type = RTE_ACL_FIELD_TYPE_MASK,
295 .size = sizeof(uint32_t),
296 .field_index = DST2_FIELD_IPV6,
297 .input_index = DST2_FIELD_IPV6,
298 .offset = offsetof(struct ipv6_5tuple, ip_dst[1]),
301 .type = RTE_ACL_FIELD_TYPE_MASK,
302 .size = sizeof(uint32_t),
303 .field_index = DST3_FIELD_IPV6,
304 .input_index = DST3_FIELD_IPV6,
305 .offset = offsetof(struct ipv6_5tuple, ip_dst[2]),
308 .type = RTE_ACL_FIELD_TYPE_MASK,
309 .size = sizeof(uint32_t),
310 .field_index = DST4_FIELD_IPV6,
311 .input_index = DST4_FIELD_IPV6,
312 .offset = offsetof(struct ipv6_5tuple, ip_dst[3]),
315 .type = RTE_ACL_FIELD_TYPE_RANGE,
316 .size = sizeof(uint16_t),
317 .field_index = SRCP_FIELD_IPV6,
318 .input_index = SRCP_FIELD_IPV6,
319 .offset = offsetof(struct ipv6_5tuple, port_src),
322 .type = RTE_ACL_FIELD_TYPE_RANGE,
323 .size = sizeof(uint16_t),
324 .field_index = DSTP_FIELD_IPV6,
325 .input_index = SRCP_FIELD_IPV6,
326 .offset = offsetof(struct ipv6_5tuple, port_dst),
336 CB_FLD_SRC_PORT_HIGH,
339 CB_FLD_DST_PORT_HIGH,
353 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
355 static const char cb_port_delim[] = ":";
357 static char line[LINE_MAX];
359 #define dump_verbose(lvl, fh, fmt, args...) do { \
360 if ((lvl) <= (int32_t)config.verbose) \
361 fprintf(fh, fmt, ##args); \
366 * Parse ClassBench input trace (test vectors and expected results) file.
368 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
369 * <src_port> <space> <dst_port> <space> <proto>
372 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
375 char *s, *sp, *in[CB_TRC_NUM];
376 static const char *dlm = " \t\n";
379 for (i = 0; i != RTE_DIM(in); i++) {
380 in[i] = strtok_r(s, dlm, &sp);
386 GET_CB_FIELD(in[CB_TRC_SRC_ADDR], v->ip_src, 0, UINT32_MAX, 0);
387 GET_CB_FIELD(in[CB_TRC_DST_ADDR], v->ip_dst, 0, UINT32_MAX, 0);
388 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
389 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
390 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
392 /* convert to network byte order. */
393 v->ip_src = rte_cpu_to_be_32(v->ip_src);
394 v->ip_dst = rte_cpu_to_be_32(v->ip_dst);
395 v->port_src = rte_cpu_to_be_16(v->port_src);
396 v->port_dst = rte_cpu_to_be_16(v->port_dst);
402 * Parses IPV6 address, exepcts the following format:
403 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
406 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
409 uint32_t addr[IPV6_ADDR_U16];
411 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
412 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
413 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
414 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
415 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
416 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
417 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
418 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
422 v[0] = (addr[0] << 16) + addr[1];
423 v[1] = (addr[2] << 16) + addr[3];
424 v[2] = (addr[4] << 16) + addr[5];
425 v[3] = (addr[6] << 16) + addr[7];
431 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
436 rc = parse_ipv6_addr(in, &end, v, 0);
440 v[0] = rte_cpu_to_be_32(v[0]);
441 v[1] = rte_cpu_to_be_32(v[1]);
442 v[2] = rte_cpu_to_be_32(v[2]);
443 v[3] = rte_cpu_to_be_32(v[3]);
449 * Parse ClassBench input trace (test vectors and expected results) file.
451 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
452 * <src_port> <space> <dst_port> <space> <proto>
455 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
458 char *s, *sp, *in[CB_TRC_NUM];
459 static const char *dlm = " \t\n";
462 for (i = 0; i != RTE_DIM(in); i++) {
463 in[i] = strtok_r(s, dlm, &sp);
469 /* get ip6 src address. */
470 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
474 /* get ip6 dst address. */
475 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
479 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
480 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
481 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
483 /* convert to network byte order. */
484 v->port_src = rte_cpu_to_be_16(v->port_src);
485 v->port_dst = rte_cpu_to_be_16(v->port_dst);
493 static const char name[] = APP_NAME;
497 struct ipv4_5tuple *v;
498 struct ipv6_5tuple *w;
500 sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
501 config.traces = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE,
503 if (config.traces == NULL)
504 rte_exit(EXIT_FAILURE, "Cannot allocate %zu bytes for "
505 "requested %u number of trace records\n",
506 sz, config.nb_traces);
508 f = fopen(config.trace_file, "r");
510 rte_exit(-EINVAL, "failed to open file: %s\n",
515 for (n = 0; n != config.nb_traces; n++) {
517 if (fgets(line, sizeof(line), f) == NULL)
521 if (parse_cb_ipv6_trace(line, w + n) != 0)
522 rte_exit(EXIT_FAILURE,
523 "%s: failed to parse ipv6 trace "
524 "record at line %u\n",
525 config.trace_file, n + 1);
527 if (parse_cb_ipv4_trace(line, v + n) != 0)
528 rte_exit(EXIT_FAILURE,
529 "%s: failed to parse ipv4 trace "
530 "record at line %u\n",
531 config.trace_file, n + 1);
535 config.used_traces = n;
540 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
545 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
548 rc = parse_ipv6_addr(in, &mp, v, '/');
553 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
555 /* put all together. */
556 for (i = 0; i != RTE_DIM(v); i++) {
557 if (m >= (i + 1) * nbu32)
558 field[i].mask_range.u32 = nbu32;
560 field[i].mask_range.u32 = m > (i * nbu32) ?
563 field[i].value.u32 = v[i];
571 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
574 char *s, *sp, *in[CB_FLD_NUM];
575 static const char *dlm = " \t\n";
580 if (strchr(str, '@') != str)
585 for (i = 0; i != RTE_DIM(in); i++) {
586 in[i] = strtok_r(s, dlm, &sp);
592 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
594 RTE_LOG(ERR, TESTACL,
595 "failed to read source address/mask: %s\n",
596 in[CB_FLD_SRC_ADDR]);
600 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
602 RTE_LOG(ERR, TESTACL,
603 "failed to read destination address/mask: %s\n",
604 in[CB_FLD_DST_ADDR]);
609 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
610 v->field[SRCP_FIELD_IPV6].value.u16,
612 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
613 v->field[SRCP_FIELD_IPV6].mask_range.u16,
616 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
617 sizeof(cb_port_delim)) != 0)
620 /* destination port. */
621 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
622 v->field[DSTP_FIELD_IPV6].value.u16,
624 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
625 v->field[DSTP_FIELD_IPV6].mask_range.u16,
628 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
629 sizeof(cb_port_delim)) != 0)
632 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
634 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
641 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
643 uint8_t a, b, c, d, m;
645 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
646 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
647 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
648 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
649 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
651 addr[0] = IPv4(a, b, c, d);
657 * Parse ClassBench rules file.
659 * '@'<src_ipv4_addr>'/'<masklen> <space> \
660 * <dst_ipv4_addr>'/'<masklen> <space> \
661 * <src_port_low> <space> ":" <src_port_high> <space> \
662 * <dst_port_low> <space> ":" <dst_port_high> <space> \
666 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
669 char *s, *sp, *in[CB_FLD_NUM];
670 static const char *dlm = " \t\n";
675 if (strchr(str, '@') != str)
680 for (i = 0; i != RTE_DIM(in); i++) {
681 in[i] = strtok_r(s, dlm, &sp);
687 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
688 &v->field[SRC_FIELD_IPV4].value.u32,
689 &v->field[SRC_FIELD_IPV4].mask_range.u32);
691 RTE_LOG(ERR, TESTACL,
692 "failed to read source address/mask: %s\n",
693 in[CB_FLD_SRC_ADDR]);
697 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
698 &v->field[DST_FIELD_IPV4].value.u32,
699 &v->field[DST_FIELD_IPV4].mask_range.u32);
701 RTE_LOG(ERR, TESTACL,
702 "failed to read destination address/mask: %s\n",
703 in[CB_FLD_DST_ADDR]);
708 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
709 v->field[SRCP_FIELD_IPV4].value.u16,
711 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
712 v->field[SRCP_FIELD_IPV4].mask_range.u16,
715 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
716 sizeof(cb_port_delim)) != 0)
719 /* destination port. */
720 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
721 v->field[DSTP_FIELD_IPV4].value.u16,
723 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
724 v->field[DSTP_FIELD_IPV4].mask_range.u16,
727 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
728 sizeof(cb_port_delim)) != 0)
731 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
733 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
739 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
742 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
749 memset(&v, 0, sizeof(v));
750 parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
752 for (n = 1; fgets(line, sizeof(line), f) != NULL; n++) {
754 rc = parser(line, &v);
756 RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
757 " failed, error code: %d (%s)\n",
758 n, rc, strerror(-rc));
762 v.data.category_mask = LEN2MASK(RTE_ACL_MAX_CATEGORIES);
763 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
766 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
768 RTE_LOG(ERR, TESTACL, "line %u: failed to add rules "
769 "into ACL context, error code: %d (%s)\n",
770 n, rc, strerror(-rc));
783 struct rte_acl_config cfg;
785 memset(&cfg, 0, sizeof(cfg));
787 /* setup ACL build config. */
789 cfg.num_fields = RTE_DIM(ipv6_defs);
790 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
792 cfg.num_fields = RTE_DIM(ipv4_defs);
793 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
795 cfg.num_categories = config.bld_categories;
796 cfg.max_size = config.max_size;
798 /* setup ACL creation parameters. */
799 prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
800 prm.max_rule_num = config.nb_rules;
802 config.acx = rte_acl_create(&prm);
803 if (config.acx == NULL)
804 rte_exit(rte_errno, "failed to create ACL context\n");
806 /* set default classify method for this context. */
807 if (config.alg.alg != RTE_ACL_CLASSIFY_DEFAULT) {
808 ret = rte_acl_set_ctx_classify(config.acx, config.alg.alg);
810 rte_exit(ret, "failed to setup %s method "
811 "for ACL context\n", config.alg.name);
815 f = fopen(config.rule_file, "r");
817 rte_exit(-EINVAL, "failed to open file %s\n",
820 ret = add_cb_rules(f, config.acx);
822 rte_exit(ret, "failed to add rules into ACL context\n");
827 ret = rte_acl_build(config.acx, &cfg);
829 dump_verbose(DUMP_NONE, stdout,
830 "rte_acl_build(%u) finished with %d\n",
831 config.bld_categories, ret);
833 rte_acl_dump(config.acx);
836 rte_exit(ret, "failed to build search context\n");
840 search_ip5tuples_once(uint32_t categories, uint32_t step, const char *alg)
843 uint32_t i, j, k, n, r;
844 const uint8_t *data[step], *v;
845 uint32_t results[step * categories];
848 for (i = 0; i != config.used_traces; i += n) {
850 n = RTE_MIN(step, config.used_traces - i);
852 for (j = 0; j != n; j++) {
854 v += config.trace_sz;
857 ret = rte_acl_classify(config.acx, data, results,
861 rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
862 config.ipv6 ? '6' : '4', ret);
864 for (r = 0, j = 0; j != n; j++) {
865 for (k = 0; k != categories; k++, r++) {
866 dump_verbose(DUMP_PKT, stdout,
867 "ipv%c_5tuple: %u, category: %u, "
869 config.ipv6 ? '6' : '4',
870 i + j + 1, k, results[r] - 1);
876 dump_verbose(DUMP_SEARCH, stdout,
877 "%s(%u, %u, %s) returns %u\n", __func__,
878 categories, step, alg, i);
883 search_ip5tuples(__attribute__((unused)) void *arg)
885 uint64_t pkt, start, tm;
888 lcore = rte_lcore_id();
892 for (i = 0; i != config.iter_num; i++) {
893 pkt += search_ip5tuples_once(config.run_categories,
894 config.trace_step, config.alg.name);
897 tm = rte_rdtsc() - start;
898 dump_verbose(DUMP_NONE, stdout,
899 "%s @lcore %u: %" PRIu32 " iterations, %" PRIu64 " pkts, %"
900 PRIu32 " categories, %" PRIu64 " cycles, %#Lf cycles/pkt\n",
901 __func__, lcore, i, pkt, config.run_categories,
902 tm, (long double)tm / pkt);
908 get_ulong_opt(const char *opt, const char *name, size_t min, size_t max)
914 val = strtoul(opt, &end, 0);
915 if (errno != 0 || end[0] != 0 || val > max || val < min)
916 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
922 get_alg_opt(const char *opt, const char *name)
926 for (i = 0; i != RTE_DIM(acl_alg); i++) {
927 if (strcmp(opt, acl_alg[i].name) == 0) {
928 config.alg = acl_alg[i];
933 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
938 print_usage(const char *prgname)
946 for (i = 0; i < RTE_DIM(acl_alg) - 1; i++) {
947 rc = snprintf(buf + n, sizeof(buf) - n, "%s|",
949 if (rc > sizeof(buf) - n)
954 snprintf(buf + n, sizeof(buf) - n, "%s", acl_alg[i].name);
958 "--" OPT_RULE_FILE "=<rules set file>\n"
959 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
961 "=<maximum number of rules for ACL context>]\n"
963 "=<number of traces to read binary file in>]\n"
965 "=<number of traces to classify per one call>]\n"
966 "[--" OPT_BLD_CATEGORIES
967 "=<number of categories to build with>]\n"
968 "[--" OPT_RUN_CATEGORIES
969 "=<number of categories to run with> "
970 "should be either 1 or multiple of %zu, "
971 "but not greater then %u]\n"
973 "=<size limit (in bytes) for runtime ACL strucutures> "
974 "leave 0 for default behaviour]\n"
975 "[--" OPT_ITER_NUM "=<number of iterations to perform>]\n"
976 "[--" OPT_VERBOSE "=<verbose level>]\n"
977 "[--" OPT_SEARCH_ALG "=%s]\n"
978 "[--" OPT_IPV6 "=<IPv6 rules and trace files>]\n",
979 prgname, RTE_ACL_RESULTS_MULTIPLIER,
980 (uint32_t)RTE_ACL_MAX_CATEGORIES,
987 fprintf(f, "%s:\n", __func__);
988 fprintf(f, "%s:%s\n", OPT_RULE_FILE, config.rule_file);
989 fprintf(f, "%s:%s\n", OPT_TRACE_FILE, config.trace_file);
990 fprintf(f, "%s:%u\n", OPT_RULE_NUM, config.nb_rules);
991 fprintf(f, "%s:%u\n", OPT_TRACE_NUM, config.nb_traces);
992 fprintf(f, "%s:%u\n", OPT_TRACE_STEP, config.trace_step);
993 fprintf(f, "%s:%u\n", OPT_BLD_CATEGORIES, config.bld_categories);
994 fprintf(f, "%s:%u\n", OPT_RUN_CATEGORIES, config.run_categories);
995 fprintf(f, "%s:%zu\n", OPT_MAX_SIZE, config.max_size);
996 fprintf(f, "%s:%u\n", OPT_ITER_NUM, config.iter_num);
997 fprintf(f, "%s:%u\n", OPT_VERBOSE, config.verbose);
998 fprintf(f, "%s:%u(%s)\n", OPT_SEARCH_ALG, config.alg.alg,
1000 fprintf(f, "%s:%u\n", OPT_IPV6, config.ipv6);
1006 if (config.rule_file == NULL) {
1007 print_usage(config.prgname);
1008 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
1015 get_input_opts(int argc, char **argv)
1017 static struct option lgopts[] = {
1018 {OPT_RULE_FILE, 1, 0, 0},
1019 {OPT_TRACE_FILE, 1, 0, 0},
1020 {OPT_TRACE_NUM, 1, 0, 0},
1021 {OPT_RULE_NUM, 1, 0, 0},
1022 {OPT_MAX_SIZE, 1, 0, 0},
1023 {OPT_TRACE_STEP, 1, 0, 0},
1024 {OPT_BLD_CATEGORIES, 1, 0, 0},
1025 {OPT_RUN_CATEGORIES, 1, 0, 0},
1026 {OPT_ITER_NUM, 1, 0, 0},
1027 {OPT_VERBOSE, 1, 0, 0},
1028 {OPT_SEARCH_ALG, 1, 0, 0},
1029 {OPT_IPV6, 0, 0, 0},
1035 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
1038 print_usage(config.prgname);
1039 rte_exit(-EINVAL, "unknown option: %c", opt);
1042 if (strcmp(lgopts[opt_idx].name, OPT_RULE_FILE) == 0) {
1043 config.rule_file = optarg;
1044 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_FILE) == 0) {
1045 config.trace_file = optarg;
1046 } else if (strcmp(lgopts[opt_idx].name, OPT_RULE_NUM) == 0) {
1047 config.nb_rules = get_ulong_opt(optarg,
1048 lgopts[opt_idx].name, 1, RTE_ACL_MAX_INDEX + 1);
1049 } else if (strcmp(lgopts[opt_idx].name, OPT_MAX_SIZE) == 0) {
1050 config.max_size = get_ulong_opt(optarg,
1051 lgopts[opt_idx].name, 0, SIZE_MAX);
1052 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_NUM) == 0) {
1053 config.nb_traces = get_ulong_opt(optarg,
1054 lgopts[opt_idx].name, 1, UINT32_MAX);
1055 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_STEP) == 0) {
1056 config.trace_step = get_ulong_opt(optarg,
1057 lgopts[opt_idx].name, 1, TRACE_STEP_MAX);
1058 } else if (strcmp(lgopts[opt_idx].name,
1059 OPT_BLD_CATEGORIES) == 0) {
1060 config.bld_categories = get_ulong_opt(optarg,
1061 lgopts[opt_idx].name, 1,
1062 RTE_ACL_MAX_CATEGORIES);
1063 } else if (strcmp(lgopts[opt_idx].name,
1064 OPT_RUN_CATEGORIES) == 0) {
1065 config.run_categories = get_ulong_opt(optarg,
1066 lgopts[opt_idx].name, 1,
1067 RTE_ACL_MAX_CATEGORIES);
1068 } else if (strcmp(lgopts[opt_idx].name, OPT_ITER_NUM) == 0) {
1069 config.iter_num = get_ulong_opt(optarg,
1070 lgopts[opt_idx].name, 1, INT32_MAX);
1071 } else if (strcmp(lgopts[opt_idx].name, OPT_VERBOSE) == 0) {
1072 config.verbose = get_ulong_opt(optarg,
1073 lgopts[opt_idx].name, DUMP_NONE, DUMP_MAX);
1074 } else if (strcmp(lgopts[opt_idx].name,
1075 OPT_SEARCH_ALG) == 0) {
1076 get_alg_opt(optarg, lgopts[opt_idx].name);
1077 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1081 config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1082 sizeof(struct ipv4_5tuple);
1087 main(int argc, char **argv)
1092 ret = rte_eal_init(argc, argv);
1094 rte_panic("Cannot init EAL\n");
1099 config.prgname = argv[0];
1101 get_input_opts(argc, argv);
1102 dump_config(stdout);
1107 if (config.trace_file != NULL)
1110 RTE_LCORE_FOREACH_SLAVE(lcore)
1111 rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1113 search_ip5tuples(NULL);
1115 rte_eal_mp_wait_lcore();
1117 rte_acl_free(config.acx);