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,
108 const char *rule_file;
109 const char *trace_file;
111 uint32_t bld_categories;
112 uint32_t run_categories;
121 uint32_t used_traces;
123 struct rte_acl_ctx *acx;
127 .nb_rules = RULE_NUM,
128 .nb_traces = TRACE_DEFAULT_NUM,
129 .trace_step = TRACE_STEP_DEF,
134 .alg = RTE_ACL_CLASSIFY_DEFAULT,
139 static struct rte_acl_param prm = {
141 .socket_id = SOCKET_ID_ANY,
145 * Rule and trace formats definitions.
165 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
167 .type = RTE_ACL_FIELD_TYPE_BITMASK,
168 .size = sizeof(uint8_t),
169 .field_index = PROTO_FIELD_IPV4,
170 .input_index = RTE_ACL_IPV4VLAN_PROTO,
171 .offset = offsetof(struct ipv4_5tuple, proto),
174 .type = RTE_ACL_FIELD_TYPE_MASK,
175 .size = sizeof(uint32_t),
176 .field_index = SRC_FIELD_IPV4,
177 .input_index = RTE_ACL_IPV4VLAN_SRC,
178 .offset = offsetof(struct ipv4_5tuple, ip_src),
181 .type = RTE_ACL_FIELD_TYPE_MASK,
182 .size = sizeof(uint32_t),
183 .field_index = DST_FIELD_IPV4,
184 .input_index = RTE_ACL_IPV4VLAN_DST,
185 .offset = offsetof(struct ipv4_5tuple, ip_dst),
188 .type = RTE_ACL_FIELD_TYPE_RANGE,
189 .size = sizeof(uint16_t),
190 .field_index = SRCP_FIELD_IPV4,
191 .input_index = RTE_ACL_IPV4VLAN_PORTS,
192 .offset = offsetof(struct ipv4_5tuple, port_src),
195 .type = RTE_ACL_FIELD_TYPE_RANGE,
196 .size = sizeof(uint16_t),
197 .field_index = DSTP_FIELD_IPV4,
198 .input_index = RTE_ACL_IPV4VLAN_PORTS,
199 .offset = offsetof(struct ipv4_5tuple, port_dst),
203 #define IPV6_ADDR_LEN 16
204 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
205 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
209 uint32_t ip_src[IPV6_ADDR_U32];
210 uint32_t ip_dst[IPV6_ADDR_U32];
230 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
232 .type = RTE_ACL_FIELD_TYPE_BITMASK,
233 .size = sizeof(uint8_t),
234 .field_index = PROTO_FIELD_IPV6,
235 .input_index = PROTO_FIELD_IPV6,
236 .offset = offsetof(struct ipv6_5tuple, proto),
239 .type = RTE_ACL_FIELD_TYPE_MASK,
240 .size = sizeof(uint32_t),
241 .field_index = SRC1_FIELD_IPV6,
242 .input_index = SRC1_FIELD_IPV6,
243 .offset = offsetof(struct ipv6_5tuple, ip_src[0]),
246 .type = RTE_ACL_FIELD_TYPE_MASK,
247 .size = sizeof(uint32_t),
248 .field_index = SRC2_FIELD_IPV6,
249 .input_index = SRC2_FIELD_IPV6,
250 .offset = offsetof(struct ipv6_5tuple, ip_src[1]),
253 .type = RTE_ACL_FIELD_TYPE_MASK,
254 .size = sizeof(uint32_t),
255 .field_index = SRC3_FIELD_IPV6,
256 .input_index = SRC3_FIELD_IPV6,
257 .offset = offsetof(struct ipv6_5tuple, ip_src[2]),
260 .type = RTE_ACL_FIELD_TYPE_MASK,
261 .size = sizeof(uint32_t),
262 .field_index = SRC4_FIELD_IPV6,
263 .input_index = SRC4_FIELD_IPV6,
264 .offset = offsetof(struct ipv6_5tuple, ip_src[3]),
267 .type = RTE_ACL_FIELD_TYPE_MASK,
268 .size = sizeof(uint32_t),
269 .field_index = DST1_FIELD_IPV6,
270 .input_index = DST1_FIELD_IPV6,
271 .offset = offsetof(struct ipv6_5tuple, ip_dst[0]),
274 .type = RTE_ACL_FIELD_TYPE_MASK,
275 .size = sizeof(uint32_t),
276 .field_index = DST2_FIELD_IPV6,
277 .input_index = DST2_FIELD_IPV6,
278 .offset = offsetof(struct ipv6_5tuple, ip_dst[1]),
281 .type = RTE_ACL_FIELD_TYPE_MASK,
282 .size = sizeof(uint32_t),
283 .field_index = DST3_FIELD_IPV6,
284 .input_index = DST3_FIELD_IPV6,
285 .offset = offsetof(struct ipv6_5tuple, ip_dst[2]),
288 .type = RTE_ACL_FIELD_TYPE_MASK,
289 .size = sizeof(uint32_t),
290 .field_index = DST4_FIELD_IPV6,
291 .input_index = DST4_FIELD_IPV6,
292 .offset = offsetof(struct ipv6_5tuple, ip_dst[3]),
295 .type = RTE_ACL_FIELD_TYPE_RANGE,
296 .size = sizeof(uint16_t),
297 .field_index = SRCP_FIELD_IPV6,
298 .input_index = SRCP_FIELD_IPV6,
299 .offset = offsetof(struct ipv6_5tuple, port_src),
302 .type = RTE_ACL_FIELD_TYPE_RANGE,
303 .size = sizeof(uint16_t),
304 .field_index = DSTP_FIELD_IPV6,
305 .input_index = SRCP_FIELD_IPV6,
306 .offset = offsetof(struct ipv6_5tuple, port_dst),
316 CB_FLD_SRC_PORT_HIGH,
319 CB_FLD_DST_PORT_HIGH,
333 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
335 static const char cb_port_delim[] = ":";
337 static char line[LINE_MAX];
339 #define dump_verbose(lvl, fh, fmt, args...) do { \
340 if ((lvl) <= (int32_t)config.verbose) \
341 fprintf(fh, fmt, ##args); \
346 * Parse ClassBench input trace (test vectors and expected results) file.
348 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
349 * <src_port> <space> <dst_port> <space> <proto>
352 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
355 char *s, *sp, *in[CB_TRC_NUM];
356 static const char *dlm = " \t\n";
359 for (i = 0; i != RTE_DIM(in); i++) {
360 in[i] = strtok_r(s, dlm, &sp);
366 GET_CB_FIELD(in[CB_TRC_SRC_ADDR], v->ip_src, 0, UINT32_MAX, 0);
367 GET_CB_FIELD(in[CB_TRC_DST_ADDR], v->ip_dst, 0, UINT32_MAX, 0);
368 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
369 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
370 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
372 /* convert to network byte order. */
373 v->ip_src = rte_cpu_to_be_32(v->ip_src);
374 v->ip_dst = rte_cpu_to_be_32(v->ip_dst);
375 v->port_src = rte_cpu_to_be_16(v->port_src);
376 v->port_dst = rte_cpu_to_be_16(v->port_dst);
382 * Parses IPV6 address, exepcts the following format:
383 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
386 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
389 uint32_t addr[IPV6_ADDR_U16];
391 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
392 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
393 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
394 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
395 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
396 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
397 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
398 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
402 v[0] = (addr[0] << 16) + addr[1];
403 v[1] = (addr[2] << 16) + addr[3];
404 v[2] = (addr[4] << 16) + addr[5];
405 v[3] = (addr[6] << 16) + addr[7];
411 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
416 rc = parse_ipv6_addr(in, &end, v, 0);
420 v[0] = rte_cpu_to_be_32(v[0]);
421 v[1] = rte_cpu_to_be_32(v[1]);
422 v[2] = rte_cpu_to_be_32(v[2]);
423 v[3] = rte_cpu_to_be_32(v[3]);
429 * Parse ClassBench input trace (test vectors and expected results) file.
431 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
432 * <src_port> <space> <dst_port> <space> <proto>
435 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
438 char *s, *sp, *in[CB_TRC_NUM];
439 static const char *dlm = " \t\n";
442 for (i = 0; i != RTE_DIM(in); i++) {
443 in[i] = strtok_r(s, dlm, &sp);
449 /* get ip6 src address. */
450 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
454 /* get ip6 dst address. */
455 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
459 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
460 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
461 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
463 /* convert to network byte order. */
464 v->port_src = rte_cpu_to_be_16(v->port_src);
465 v->port_dst = rte_cpu_to_be_16(v->port_dst);
473 static const char name[] = APP_NAME;
477 struct ipv4_5tuple *v;
478 struct ipv6_5tuple *w;
480 sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
481 config.traces = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE,
483 if (config.traces == NULL)
484 rte_exit(EXIT_FAILURE, "Cannot allocate %zu bytes for "
485 "requested %u number of trace records\n",
486 sz, config.nb_traces);
488 f = fopen(config.trace_file, "r");
490 rte_exit(-EINVAL, "failed to open file: %s\n",
495 for (n = 0; n != config.nb_traces; n++) {
497 if (fgets(line, sizeof(line), f) == NULL)
501 if (parse_cb_ipv6_trace(line, w + n) != 0)
502 rte_exit(EXIT_FAILURE,
503 "%s: failed to parse ipv6 trace "
504 "record at line %u\n",
505 config.trace_file, n + 1);
507 if (parse_cb_ipv4_trace(line, v + n) != 0)
508 rte_exit(EXIT_FAILURE,
509 "%s: failed to parse ipv4 trace "
510 "record at line %u\n",
511 config.trace_file, n + 1);
515 config.used_traces = n;
520 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
525 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
528 rc = parse_ipv6_addr(in, &mp, v, '/');
533 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
535 /* put all together. */
536 for (i = 0; i != RTE_DIM(v); i++) {
537 if (m >= (i + 1) * nbu32)
538 field[i].mask_range.u32 = nbu32;
540 field[i].mask_range.u32 = m > (i * nbu32) ?
543 field[i].value.u32 = v[i];
551 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
554 char *s, *sp, *in[CB_FLD_NUM];
555 static const char *dlm = " \t\n";
560 if (strchr(str, '@') != str)
565 for (i = 0; i != RTE_DIM(in); i++) {
566 in[i] = strtok_r(s, dlm, &sp);
572 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
574 RTE_LOG(ERR, TESTACL,
575 "failed to read source address/mask: %s\n",
576 in[CB_FLD_SRC_ADDR]);
580 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
582 RTE_LOG(ERR, TESTACL,
583 "failed to read destination address/mask: %s\n",
584 in[CB_FLD_DST_ADDR]);
589 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
590 v->field[SRCP_FIELD_IPV6].value.u16,
592 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
593 v->field[SRCP_FIELD_IPV6].mask_range.u16,
596 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
597 sizeof(cb_port_delim)) != 0)
600 /* destination port. */
601 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
602 v->field[DSTP_FIELD_IPV6].value.u16,
604 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
605 v->field[DSTP_FIELD_IPV6].mask_range.u16,
608 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
609 sizeof(cb_port_delim)) != 0)
612 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
614 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
621 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
623 uint8_t a, b, c, d, m;
625 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
626 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
627 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
628 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
629 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
631 addr[0] = IPv4(a, b, c, d);
637 * Parse ClassBench rules file.
639 * '@'<src_ipv4_addr>'/'<masklen> <space> \
640 * <dst_ipv4_addr>'/'<masklen> <space> \
641 * <src_port_low> <space> ":" <src_port_high> <space> \
642 * <dst_port_low> <space> ":" <dst_port_high> <space> \
646 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
649 char *s, *sp, *in[CB_FLD_NUM];
650 static const char *dlm = " \t\n";
655 if (strchr(str, '@') != str)
660 for (i = 0; i != RTE_DIM(in); i++) {
661 in[i] = strtok_r(s, dlm, &sp);
667 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
668 &v->field[SRC_FIELD_IPV4].value.u32,
669 &v->field[SRC_FIELD_IPV4].mask_range.u32);
671 RTE_LOG(ERR, TESTACL,
672 "failed to read source address/mask: %s\n",
673 in[CB_FLD_SRC_ADDR]);
677 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
678 &v->field[DST_FIELD_IPV4].value.u32,
679 &v->field[DST_FIELD_IPV4].mask_range.u32);
681 RTE_LOG(ERR, TESTACL,
682 "failed to read destination address/mask: %s\n",
683 in[CB_FLD_DST_ADDR]);
688 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
689 v->field[SRCP_FIELD_IPV4].value.u16,
691 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
692 v->field[SRCP_FIELD_IPV4].mask_range.u16,
695 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
696 sizeof(cb_port_delim)) != 0)
699 /* destination port. */
700 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
701 v->field[DSTP_FIELD_IPV4].value.u16,
703 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
704 v->field[DSTP_FIELD_IPV4].mask_range.u16,
707 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
708 sizeof(cb_port_delim)) != 0)
711 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
713 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
719 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
722 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
729 memset(&v, 0, sizeof(v));
730 parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
732 for (n = 1; fgets(line, sizeof(line), f) != NULL; n++) {
734 rc = parser(line, &v);
736 RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
737 " failed, error code: %d (%s)\n",
738 n, rc, strerror(-rc));
742 v.data.category_mask = LEN2MASK(RTE_ACL_MAX_CATEGORIES);
743 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
746 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
748 RTE_LOG(ERR, TESTACL, "line %u: failed to add rules "
749 "into ACL context, error code: %d (%s)\n",
750 n, rc, strerror(-rc));
763 struct rte_acl_config cfg;
765 memset(&cfg, 0, sizeof(cfg));
767 /* setup ACL build config. */
769 cfg.num_fields = RTE_DIM(ipv6_defs);
770 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
772 cfg.num_fields = RTE_DIM(ipv4_defs);
773 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
775 cfg.num_categories = config.bld_categories;
776 cfg.max_size = config.max_size;
778 /* setup ACL creation parameters. */
779 prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
780 prm.max_rule_num = config.nb_rules;
782 config.acx = rte_acl_create(&prm);
783 if (config.acx == NULL)
784 rte_exit(rte_errno, "failed to create ACL context\n");
786 /* set default classify method for this context. */
787 if (config.alg.alg != RTE_ACL_CLASSIFY_DEFAULT) {
788 ret = rte_acl_set_ctx_classify(config.acx, config.alg.alg);
790 rte_exit(ret, "failed to setup %s method "
791 "for ACL context\n", config.alg.name);
795 f = fopen(config.rule_file, "r");
797 rte_exit(-EINVAL, "failed to open file %s\n",
800 ret = add_cb_rules(f, config.acx);
802 rte_exit(ret, "failed to add rules into ACL context\n");
807 ret = rte_acl_build(config.acx, &cfg);
809 dump_verbose(DUMP_NONE, stdout,
810 "rte_acl_build(%u) finished with %d\n",
811 config.bld_categories, ret);
813 rte_acl_dump(config.acx);
816 rte_exit(ret, "failed to build search context\n");
820 search_ip5tuples_once(uint32_t categories, uint32_t step, const char *alg)
823 uint32_t i, j, k, n, r;
824 const uint8_t *data[step], *v;
825 uint32_t results[step * categories];
828 for (i = 0; i != config.used_traces; i += n) {
830 n = RTE_MIN(step, config.used_traces - i);
832 for (j = 0; j != n; j++) {
834 v += config.trace_sz;
837 ret = rte_acl_classify(config.acx, data, results,
841 rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
842 config.ipv6 ? '6' : '4', ret);
844 for (r = 0, j = 0; j != n; j++) {
845 for (k = 0; k != categories; k++, r++) {
846 dump_verbose(DUMP_PKT, stdout,
847 "ipv%c_5tuple: %u, category: %u, "
849 config.ipv6 ? '6' : '4',
850 i + j + 1, k, results[r] - 1);
856 dump_verbose(DUMP_SEARCH, stdout,
857 "%s(%u, %u, %s) returns %u\n", __func__,
858 categories, step, alg, i);
863 search_ip5tuples(__attribute__((unused)) void *arg)
865 uint64_t pkt, start, tm;
868 lcore = rte_lcore_id();
872 for (i = 0; i != config.iter_num; i++) {
873 pkt += search_ip5tuples_once(config.run_categories,
874 config.trace_step, config.alg.name);
877 tm = rte_rdtsc() - start;
878 dump_verbose(DUMP_NONE, stdout,
879 "%s @lcore %u: %" PRIu32 " iterations, %" PRIu64 " pkts, %"
880 PRIu32 " categories, %" PRIu64 " cycles, %#Lf cycles/pkt\n",
881 __func__, lcore, i, pkt, config.run_categories,
882 tm, (long double)tm / pkt);
888 get_ulong_opt(const char *opt, const char *name, size_t min, size_t max)
894 val = strtoul(opt, &end, 0);
895 if (errno != 0 || end[0] != 0 || val > max || val < min)
896 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
902 get_alg_opt(const char *opt, const char *name)
906 for (i = 0; i != RTE_DIM(acl_alg); i++) {
907 if (strcmp(opt, acl_alg[i].name) == 0) {
908 config.alg = acl_alg[i];
913 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
918 print_usage(const char *prgname)
926 for (i = 0; i < RTE_DIM(acl_alg) - 1; i++) {
927 rc = snprintf(buf + n, sizeof(buf) - n, "%s|",
929 if (rc > sizeof(buf) - n)
934 snprintf(buf + n, sizeof(buf) - n, "%s", acl_alg[i].name);
938 "--" OPT_RULE_FILE "=<rules set file>\n"
939 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
941 "=<maximum number of rules for ACL context>]\n"
943 "=<number of traces to read binary file in>]\n"
945 "=<number of traces to classify per one call>]\n"
946 "[--" OPT_BLD_CATEGORIES
947 "=<number of categories to build with>]\n"
948 "[--" OPT_RUN_CATEGORIES
949 "=<number of categories to run with> "
950 "should be either 1 or multiple of %zu, "
951 "but not greater then %u]\n"
953 "=<size limit (in bytes) for runtime ACL strucutures> "
954 "leave 0 for default behaviour]\n"
955 "[--" OPT_ITER_NUM "=<number of iterations to perform>]\n"
956 "[--" OPT_VERBOSE "=<verbose level>]\n"
957 "[--" OPT_SEARCH_ALG "=%s]\n"
958 "[--" OPT_IPV6 "=<IPv6 rules and trace files>]\n",
959 prgname, RTE_ACL_RESULTS_MULTIPLIER,
960 (uint32_t)RTE_ACL_MAX_CATEGORIES,
967 fprintf(f, "%s:\n", __func__);
968 fprintf(f, "%s:%s\n", OPT_RULE_FILE, config.rule_file);
969 fprintf(f, "%s:%s\n", OPT_TRACE_FILE, config.trace_file);
970 fprintf(f, "%s:%u\n", OPT_RULE_NUM, config.nb_rules);
971 fprintf(f, "%s:%u\n", OPT_TRACE_NUM, config.nb_traces);
972 fprintf(f, "%s:%u\n", OPT_TRACE_STEP, config.trace_step);
973 fprintf(f, "%s:%u\n", OPT_BLD_CATEGORIES, config.bld_categories);
974 fprintf(f, "%s:%u\n", OPT_RUN_CATEGORIES, config.run_categories);
975 fprintf(f, "%s:%zu\n", OPT_MAX_SIZE, config.max_size);
976 fprintf(f, "%s:%u\n", OPT_ITER_NUM, config.iter_num);
977 fprintf(f, "%s:%u\n", OPT_VERBOSE, config.verbose);
978 fprintf(f, "%s:%u(%s)\n", OPT_SEARCH_ALG, config.alg.alg,
980 fprintf(f, "%s:%u\n", OPT_IPV6, config.ipv6);
986 if (config.rule_file == NULL) {
987 print_usage(config.prgname);
988 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
995 get_input_opts(int argc, char **argv)
997 static struct option lgopts[] = {
998 {OPT_RULE_FILE, 1, 0, 0},
999 {OPT_TRACE_FILE, 1, 0, 0},
1000 {OPT_TRACE_NUM, 1, 0, 0},
1001 {OPT_RULE_NUM, 1, 0, 0},
1002 {OPT_MAX_SIZE, 1, 0, 0},
1003 {OPT_TRACE_STEP, 1, 0, 0},
1004 {OPT_BLD_CATEGORIES, 1, 0, 0},
1005 {OPT_RUN_CATEGORIES, 1, 0, 0},
1006 {OPT_ITER_NUM, 1, 0, 0},
1007 {OPT_VERBOSE, 1, 0, 0},
1008 {OPT_SEARCH_ALG, 1, 0, 0},
1009 {OPT_IPV6, 0, 0, 0},
1015 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
1018 print_usage(config.prgname);
1019 rte_exit(-EINVAL, "unknown option: %c", opt);
1022 if (strcmp(lgopts[opt_idx].name, OPT_RULE_FILE) == 0) {
1023 config.rule_file = optarg;
1024 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_FILE) == 0) {
1025 config.trace_file = optarg;
1026 } else if (strcmp(lgopts[opt_idx].name, OPT_RULE_NUM) == 0) {
1027 config.nb_rules = get_ulong_opt(optarg,
1028 lgopts[opt_idx].name, 1, RTE_ACL_MAX_INDEX + 1);
1029 } else if (strcmp(lgopts[opt_idx].name, OPT_MAX_SIZE) == 0) {
1030 config.max_size = get_ulong_opt(optarg,
1031 lgopts[opt_idx].name, 0, SIZE_MAX);
1032 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_NUM) == 0) {
1033 config.nb_traces = get_ulong_opt(optarg,
1034 lgopts[opt_idx].name, 1, UINT32_MAX);
1035 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_STEP) == 0) {
1036 config.trace_step = get_ulong_opt(optarg,
1037 lgopts[opt_idx].name, 1, TRACE_STEP_MAX);
1038 } else if (strcmp(lgopts[opt_idx].name,
1039 OPT_BLD_CATEGORIES) == 0) {
1040 config.bld_categories = get_ulong_opt(optarg,
1041 lgopts[opt_idx].name, 1,
1042 RTE_ACL_MAX_CATEGORIES);
1043 } else if (strcmp(lgopts[opt_idx].name,
1044 OPT_RUN_CATEGORIES) == 0) {
1045 config.run_categories = get_ulong_opt(optarg,
1046 lgopts[opt_idx].name, 1,
1047 RTE_ACL_MAX_CATEGORIES);
1048 } else if (strcmp(lgopts[opt_idx].name, OPT_ITER_NUM) == 0) {
1049 config.iter_num = get_ulong_opt(optarg,
1050 lgopts[opt_idx].name, 1, INT32_MAX);
1051 } else if (strcmp(lgopts[opt_idx].name, OPT_VERBOSE) == 0) {
1052 config.verbose = get_ulong_opt(optarg,
1053 lgopts[opt_idx].name, DUMP_NONE, DUMP_MAX);
1054 } else if (strcmp(lgopts[opt_idx].name,
1055 OPT_SEARCH_ALG) == 0) {
1056 get_alg_opt(optarg, lgopts[opt_idx].name);
1057 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1061 config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1062 sizeof(struct ipv4_5tuple);
1067 main(int argc, char **argv)
1072 ret = rte_eal_init(argc, argv);
1074 rte_panic("Cannot init EAL\n");
1079 config.prgname = argv[0];
1081 get_input_opts(argc, argv);
1082 dump_config(stdout);
1087 if (config.trace_file != NULL)
1090 RTE_LCORE_FOREACH_SLAVE(lcore)
1091 rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1093 search_ip5tuples(NULL);
1095 rte_eal_mp_wait_lcore();
1097 rte_acl_free(config.acx);