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_SCALAR "scalar"
86 #define OPT_BLD_CATEGORIES "bldcat"
87 #define OPT_RUN_CATEGORIES "runcat"
88 #define OPT_ITER_NUM "iter"
89 #define OPT_VERBOSE "verbose"
90 #define OPT_IPV6 "ipv6"
92 #define TRACE_DEFAULT_NUM 0x10000
93 #define TRACE_STEP_MAX 0x1000
94 #define TRACE_STEP_DEF 0x100
96 #define RULE_NUM 0x10000
107 const char *rule_file;
108 const char *trace_file;
109 uint32_t bld_categories;
110 uint32_t run_categories;
118 uint32_t used_traces;
120 struct rte_acl_ctx *acx;
125 .nb_rules = RULE_NUM,
126 .nb_traces = TRACE_DEFAULT_NUM,
127 .trace_step = TRACE_STEP_DEF,
133 static struct rte_acl_param prm = {
135 .socket_id = SOCKET_ID_ANY,
139 * Rule and trace formats definitions.
159 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
161 .type = RTE_ACL_FIELD_TYPE_BITMASK,
162 .size = sizeof(uint8_t),
163 .field_index = PROTO_FIELD_IPV4,
164 .input_index = RTE_ACL_IPV4VLAN_PROTO,
165 .offset = offsetof(struct ipv4_5tuple, proto),
168 .type = RTE_ACL_FIELD_TYPE_MASK,
169 .size = sizeof(uint32_t),
170 .field_index = SRC_FIELD_IPV4,
171 .input_index = RTE_ACL_IPV4VLAN_SRC,
172 .offset = offsetof(struct ipv4_5tuple, ip_src),
175 .type = RTE_ACL_FIELD_TYPE_MASK,
176 .size = sizeof(uint32_t),
177 .field_index = DST_FIELD_IPV4,
178 .input_index = RTE_ACL_IPV4VLAN_DST,
179 .offset = offsetof(struct ipv4_5tuple, ip_dst),
182 .type = RTE_ACL_FIELD_TYPE_RANGE,
183 .size = sizeof(uint16_t),
184 .field_index = SRCP_FIELD_IPV4,
185 .input_index = RTE_ACL_IPV4VLAN_PORTS,
186 .offset = offsetof(struct ipv4_5tuple, port_src),
189 .type = RTE_ACL_FIELD_TYPE_RANGE,
190 .size = sizeof(uint16_t),
191 .field_index = DSTP_FIELD_IPV4,
192 .input_index = RTE_ACL_IPV4VLAN_PORTS,
193 .offset = offsetof(struct ipv4_5tuple, port_dst),
197 #define IPV6_ADDR_LEN 16
198 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
199 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
203 uint32_t ip_src[IPV6_ADDR_U32];
204 uint32_t ip_dst[IPV6_ADDR_U32];
224 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
226 .type = RTE_ACL_FIELD_TYPE_BITMASK,
227 .size = sizeof(uint8_t),
228 .field_index = PROTO_FIELD_IPV6,
229 .input_index = PROTO_FIELD_IPV6,
230 .offset = offsetof(struct ipv6_5tuple, proto),
233 .type = RTE_ACL_FIELD_TYPE_MASK,
234 .size = sizeof(uint32_t),
235 .field_index = SRC1_FIELD_IPV6,
236 .input_index = SRC1_FIELD_IPV6,
237 .offset = offsetof(struct ipv6_5tuple, ip_src[0]),
240 .type = RTE_ACL_FIELD_TYPE_MASK,
241 .size = sizeof(uint32_t),
242 .field_index = SRC2_FIELD_IPV6,
243 .input_index = SRC2_FIELD_IPV6,
244 .offset = offsetof(struct ipv6_5tuple, ip_src[1]),
247 .type = RTE_ACL_FIELD_TYPE_MASK,
248 .size = sizeof(uint32_t),
249 .field_index = SRC3_FIELD_IPV6,
250 .input_index = SRC3_FIELD_IPV6,
251 .offset = offsetof(struct ipv6_5tuple, ip_src[2]),
254 .type = RTE_ACL_FIELD_TYPE_MASK,
255 .size = sizeof(uint32_t),
256 .field_index = SRC4_FIELD_IPV6,
257 .input_index = SRC4_FIELD_IPV6,
258 .offset = offsetof(struct ipv6_5tuple, ip_src[3]),
261 .type = RTE_ACL_FIELD_TYPE_MASK,
262 .size = sizeof(uint32_t),
263 .field_index = DST1_FIELD_IPV6,
264 .input_index = DST1_FIELD_IPV6,
265 .offset = offsetof(struct ipv6_5tuple, ip_dst[0]),
268 .type = RTE_ACL_FIELD_TYPE_MASK,
269 .size = sizeof(uint32_t),
270 .field_index = DST2_FIELD_IPV6,
271 .input_index = DST2_FIELD_IPV6,
272 .offset = offsetof(struct ipv6_5tuple, ip_dst[1]),
275 .type = RTE_ACL_FIELD_TYPE_MASK,
276 .size = sizeof(uint32_t),
277 .field_index = DST3_FIELD_IPV6,
278 .input_index = DST3_FIELD_IPV6,
279 .offset = offsetof(struct ipv6_5tuple, ip_dst[2]),
282 .type = RTE_ACL_FIELD_TYPE_MASK,
283 .size = sizeof(uint32_t),
284 .field_index = DST4_FIELD_IPV6,
285 .input_index = DST4_FIELD_IPV6,
286 .offset = offsetof(struct ipv6_5tuple, ip_dst[3]),
289 .type = RTE_ACL_FIELD_TYPE_RANGE,
290 .size = sizeof(uint16_t),
291 .field_index = SRCP_FIELD_IPV6,
292 .input_index = SRCP_FIELD_IPV6,
293 .offset = offsetof(struct ipv6_5tuple, port_src),
296 .type = RTE_ACL_FIELD_TYPE_RANGE,
297 .size = sizeof(uint16_t),
298 .field_index = DSTP_FIELD_IPV6,
299 .input_index = SRCP_FIELD_IPV6,
300 .offset = offsetof(struct ipv6_5tuple, port_dst),
310 CB_FLD_SRC_PORT_HIGH,
313 CB_FLD_DST_PORT_HIGH,
327 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
329 static const char cb_port_delim[] = ":";
331 static char line[LINE_MAX];
333 #define dump_verbose(lvl, fh, fmt, args...) do { \
334 if ((lvl) <= (int32_t)config.verbose) \
335 fprintf(fh, fmt, ##args); \
340 * Parse ClassBench input trace (test vectors and expected results) file.
342 * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
343 * <src_port> <space> <dst_port> <space> <proto>
346 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
349 char *s, *sp, *in[CB_TRC_NUM];
350 static const char *dlm = " \t\n";
353 for (i = 0; i != RTE_DIM(in); i++) {
354 in[i] = strtok_r(s, dlm, &sp);
360 GET_CB_FIELD(in[CB_TRC_SRC_ADDR], v->ip_src, 0, UINT32_MAX, 0);
361 GET_CB_FIELD(in[CB_TRC_DST_ADDR], v->ip_dst, 0, UINT32_MAX, 0);
362 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
363 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
364 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
366 /* convert to network byte order. */
367 v->ip_src = rte_cpu_to_be_32(v->ip_src);
368 v->ip_dst = rte_cpu_to_be_32(v->ip_dst);
369 v->port_src = rte_cpu_to_be_16(v->port_src);
370 v->port_dst = rte_cpu_to_be_16(v->port_dst);
376 * Parses IPV6 address, exepcts the following format:
377 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
380 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
383 uint32_t addr[IPV6_ADDR_U16];
385 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
386 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
387 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
388 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
389 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
390 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
391 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
392 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
396 v[0] = (addr[0] << 16) + addr[1];
397 v[1] = (addr[2] << 16) + addr[3];
398 v[2] = (addr[4] << 16) + addr[5];
399 v[3] = (addr[6] << 16) + addr[7];
405 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
410 rc = parse_ipv6_addr(in, &end, v, 0);
414 v[0] = rte_cpu_to_be_32(v[0]);
415 v[1] = rte_cpu_to_be_32(v[1]);
416 v[2] = rte_cpu_to_be_32(v[2]);
417 v[3] = rte_cpu_to_be_32(v[3]);
423 * Parse ClassBench input trace (test vectors and expected results) file.
425 * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
426 * <src_port> <space> <dst_port> <space> <proto>
429 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
432 char *s, *sp, *in[CB_TRC_NUM];
433 static const char *dlm = " \t\n";
436 for (i = 0; i != RTE_DIM(in); i++) {
437 in[i] = strtok_r(s, dlm, &sp);
443 /* get ip6 src address. */
444 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
448 /* get ip6 dst address. */
449 rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
453 GET_CB_FIELD(in[CB_TRC_SRC_PORT], v->port_src, 0, UINT16_MAX, 0);
454 GET_CB_FIELD(in[CB_TRC_DST_PORT], v->port_dst, 0, UINT16_MAX, 0);
455 GET_CB_FIELD(in[CB_TRC_PROTO], v->proto, 0, UINT8_MAX, 0);
457 /* convert to network byte order. */
458 v->port_src = rte_cpu_to_be_16(v->port_src);
459 v->port_dst = rte_cpu_to_be_16(v->port_dst);
467 static const char name[] = APP_NAME;
471 struct ipv4_5tuple *v;
472 struct ipv6_5tuple *w;
474 sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
475 config.traces = rte_zmalloc_socket(name, sz, CACHE_LINE_SIZE,
477 if (config.traces == NULL)
478 rte_exit(EXIT_FAILURE, "Cannot allocate %zu bytes for "
479 "requested %u number of trace records\n",
480 sz, config.nb_traces);
482 f = fopen(config.trace_file, "r");
484 rte_exit(-EINVAL, "failed to open file: %s\n",
489 for (n = 0; n != config.nb_traces; n++) {
491 if (fgets(line, sizeof(line), f) == NULL)
495 if (parse_cb_ipv6_trace(line, w + n) != 0)
496 rte_exit(EXIT_FAILURE,
497 "%s: failed to parse ipv6 trace "
498 "record at line %u\n",
499 config.trace_file, n + 1);
501 if (parse_cb_ipv4_trace(line, v + n) != 0)
502 rte_exit(EXIT_FAILURE,
503 "%s: failed to parse ipv4 trace "
504 "record at line %u\n",
505 config.trace_file, n + 1);
509 config.used_traces = n;
514 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
519 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
522 rc = parse_ipv6_addr(in, &mp, v, '/');
527 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
529 /* put all together. */
530 for (i = 0; i != RTE_DIM(v); i++) {
531 if (m >= (i + 1) * nbu32)
532 field[i].mask_range.u32 = nbu32;
534 field[i].mask_range.u32 = m > (i * nbu32) ?
537 field[i].value.u32 = v[i];
545 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
548 char *s, *sp, *in[CB_FLD_NUM];
549 static const char *dlm = " \t\n";
554 if (strchr(str, '@') != str)
559 for (i = 0; i != RTE_DIM(in); i++) {
560 in[i] = strtok_r(s, dlm, &sp);
566 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
568 RTE_LOG(ERR, TESTACL,
569 "failed to read source address/mask: %s\n",
570 in[CB_FLD_SRC_ADDR]);
574 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
576 RTE_LOG(ERR, TESTACL,
577 "failed to read destination address/mask: %s\n",
578 in[CB_FLD_DST_ADDR]);
583 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
584 v->field[SRCP_FIELD_IPV6].value.u16,
586 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
587 v->field[SRCP_FIELD_IPV6].mask_range.u16,
590 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
591 sizeof(cb_port_delim)) != 0)
594 /* destination port. */
595 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
596 v->field[DSTP_FIELD_IPV6].value.u16,
598 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
599 v->field[DSTP_FIELD_IPV6].mask_range.u16,
602 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
603 sizeof(cb_port_delim)) != 0)
606 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
608 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
615 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
617 uint8_t a, b, c, d, m;
619 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
620 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
621 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
622 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
623 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
625 addr[0] = IPv4(a, b, c, d);
631 * Parse ClassBench rules file.
633 * '@'<src_ipv4_addr>'/'<masklen> <space> \
634 * <dst_ipv4_addr>'/'<masklen> <space> \
635 * <src_port_low> <space> ":" <src_port_high> <space> \
636 * <dst_port_low> <space> ":" <dst_port_high> <space> \
640 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
643 char *s, *sp, *in[CB_FLD_NUM];
644 static const char *dlm = " \t\n";
649 if (strchr(str, '@') != str)
654 for (i = 0; i != RTE_DIM(in); i++) {
655 in[i] = strtok_r(s, dlm, &sp);
661 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
662 &v->field[SRC_FIELD_IPV4].value.u32,
663 &v->field[SRC_FIELD_IPV4].mask_range.u32);
665 RTE_LOG(ERR, TESTACL,
666 "failed to read source address/mask: %s\n",
667 in[CB_FLD_SRC_ADDR]);
671 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
672 &v->field[DST_FIELD_IPV4].value.u32,
673 &v->field[DST_FIELD_IPV4].mask_range.u32);
675 RTE_LOG(ERR, TESTACL,
676 "failed to read destination address/mask: %s\n",
677 in[CB_FLD_DST_ADDR]);
682 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
683 v->field[SRCP_FIELD_IPV4].value.u16,
685 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
686 v->field[SRCP_FIELD_IPV4].mask_range.u16,
689 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
690 sizeof(cb_port_delim)) != 0)
693 /* destination port. */
694 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
695 v->field[DSTP_FIELD_IPV4].value.u16,
697 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
698 v->field[DSTP_FIELD_IPV4].mask_range.u16,
701 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
702 sizeof(cb_port_delim)) != 0)
705 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
707 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
713 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
716 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
723 memset(&v, 0, sizeof(v));
724 parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
726 for (n = 1; fgets(line, sizeof(line), f) != NULL; n++) {
728 rc = parser(line, &v);
730 RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
731 " failed, error code: %d (%s)\n",
732 n, rc, strerror(-rc));
736 v.data.category_mask = LEN2MASK(RTE_ACL_MAX_CATEGORIES);
737 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
740 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
742 RTE_LOG(ERR, TESTACL, "line %u: failed to add rules "
743 "into ACL context, error code: %d (%s)\n",
744 n, rc, strerror(-rc));
757 struct rte_acl_config cfg;
759 /* setup ACL build config. */
761 cfg.num_fields = RTE_DIM(ipv6_defs);
762 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
764 cfg.num_fields = RTE_DIM(ipv4_defs);
765 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
767 cfg.num_categories = config.bld_categories;
769 /* setup ACL creation parameters. */
770 prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
771 prm.max_rule_num = config.nb_rules;
773 config.acx = rte_acl_create(&prm);
774 if (config.acx == NULL)
775 rte_exit(rte_errno, "failed to create ACL context\n");
777 /* set default classify method to scalar for this context. */
779 ret = rte_acl_set_ctx_classify(config.acx,
780 RTE_ACL_CLASSIFY_SCALAR);
782 rte_exit(ret, "failed to setup classify method "
783 "for ACL context\n");
787 f = fopen(config.rule_file, "r");
789 rte_exit(-EINVAL, "failed to open file %s\n",
792 ret = add_cb_rules(f, config.acx);
794 rte_exit(ret, "failed to add rules into ACL context\n");
799 ret = rte_acl_build(config.acx, &cfg);
801 dump_verbose(DUMP_NONE, stdout,
802 "rte_acl_build(%u) finished with %d\n",
803 config.bld_categories, ret);
805 rte_acl_dump(config.acx);
808 rte_exit(ret, "failed to build search context\n");
812 search_ip5tuples_once(uint32_t categories, uint32_t step, int scalar)
815 uint32_t i, j, k, n, r;
816 const uint8_t *data[step], *v;
817 uint32_t results[step * categories];
820 for (i = 0; i != config.used_traces; i += n) {
822 n = RTE_MIN(step, config.used_traces - i);
824 for (j = 0; j != n; j++) {
826 v += config.trace_sz;
829 ret = rte_acl_classify(config.acx, data, results,
833 rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
834 config.ipv6 ? '6' : '4', ret);
836 for (r = 0, j = 0; j != n; j++) {
837 for (k = 0; k != categories; k++, r++) {
838 dump_verbose(DUMP_PKT, stdout,
839 "ipv%c_5tuple: %u, category: %u, "
841 config.ipv6 ? '6' : '4',
842 i + j + 1, k, results[r] - 1);
848 dump_verbose(DUMP_SEARCH, stdout,
849 "%s(%u, %u, %s) returns %u\n", __func__,
850 categories, step, scalar != 0 ? "scalar" : "sse", i);
855 search_ip5tuples(__attribute__((unused)) void *arg)
857 uint64_t pkt, start, tm;
860 lcore = rte_lcore_id();
864 for (i = 0; i != config.iter_num; i++) {
865 pkt += search_ip5tuples_once(config.run_categories,
866 config.trace_step, config.scalar);
869 tm = rte_rdtsc() - start;
870 dump_verbose(DUMP_NONE, stdout,
871 "%s @lcore %u: %" PRIu32 " iterations, %" PRIu64 " pkts, %"
872 PRIu32 " categories, %" PRIu64 " cycles, %#Lf cycles/pkt\n",
873 __func__, lcore, i, pkt, config.run_categories,
874 tm, (long double)tm / pkt);
880 get_uint32_opt(const char *opt, const char *name, uint32_t min, uint32_t max)
886 val = strtoul(opt, &end, 0);
887 if (errno != 0 || end[0] != 0 || val > max || val < min)
888 rte_exit(-EINVAL, "invalid value: \"%s\" for option: %s\n",
894 print_usage(const char *prgname)
898 "--" OPT_RULE_FILE "=<rules set file>\n"
899 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
901 "=<maximum number of rules for ACL context>]\n"
903 "=<number of traces to read binary file in>]\n"
905 "=<number of traces to classify per one call>]\n"
906 "[--" OPT_BLD_CATEGORIES
907 "=<number of categories to build with>]\n"
908 "[--" OPT_RUN_CATEGORIES
909 "=<number of categories to run with> "
910 "should be either 1 or multiple of %zu, "
911 "but not greater then %u]\n"
912 "[--" OPT_ITER_NUM "=<number of iterations to perform>]\n"
913 "[--" OPT_VERBOSE "=<verbose level>]\n"
914 "[--" OPT_SEARCH_SCALAR "=<use scalar version>]\n"
915 "[--" OPT_IPV6 "=<IPv6 rules and trace files>]\n",
916 prgname, RTE_ACL_RESULTS_MULTIPLIER,
917 (uint32_t)RTE_ACL_MAX_CATEGORIES);
923 fprintf(f, "%s:\n", __func__);
924 fprintf(f, "%s:%s\n", OPT_RULE_FILE, config.rule_file);
925 fprintf(f, "%s:%s\n", OPT_TRACE_FILE, config.trace_file);
926 fprintf(f, "%s:%u\n", OPT_RULE_NUM, config.nb_rules);
927 fprintf(f, "%s:%u\n", OPT_TRACE_NUM, config.nb_traces);
928 fprintf(f, "%s:%u\n", OPT_TRACE_STEP, config.trace_step);
929 fprintf(f, "%s:%u\n", OPT_BLD_CATEGORIES, config.bld_categories);
930 fprintf(f, "%s:%u\n", OPT_RUN_CATEGORIES, config.run_categories);
931 fprintf(f, "%s:%u\n", OPT_ITER_NUM, config.iter_num);
932 fprintf(f, "%s:%u\n", OPT_VERBOSE, config.verbose);
933 fprintf(f, "%s:%u\n", OPT_SEARCH_SCALAR, config.scalar);
934 fprintf(f, "%s:%u\n", OPT_IPV6, config.ipv6);
940 if (config.rule_file == NULL) {
941 print_usage(config.prgname);
942 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
949 get_input_opts(int argc, char **argv)
951 static struct option lgopts[] = {
952 {OPT_RULE_FILE, 1, 0, 0},
953 {OPT_TRACE_FILE, 1, 0, 0},
954 {OPT_TRACE_NUM, 1, 0, 0},
955 {OPT_RULE_NUM, 1, 0, 0},
956 {OPT_TRACE_STEP, 1, 0, 0},
957 {OPT_BLD_CATEGORIES, 1, 0, 0},
958 {OPT_RUN_CATEGORIES, 1, 0, 0},
959 {OPT_ITER_NUM, 1, 0, 0},
960 {OPT_VERBOSE, 1, 0, 0},
961 {OPT_SEARCH_SCALAR, 0, 0, 0},
968 while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
971 print_usage(config.prgname);
972 rte_exit(-EINVAL, "unknown option: %c", opt);
975 if (strcmp(lgopts[opt_idx].name, OPT_RULE_FILE) == 0) {
976 config.rule_file = optarg;
977 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_FILE) == 0) {
978 config.trace_file = optarg;
979 } else if (strcmp(lgopts[opt_idx].name, OPT_RULE_NUM) == 0) {
980 config.nb_rules = get_uint32_opt(optarg,
981 lgopts[opt_idx].name, 1, RTE_ACL_MAX_INDEX + 1);
982 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_NUM) == 0) {
983 config.nb_traces = get_uint32_opt(optarg,
984 lgopts[opt_idx].name, 1, UINT32_MAX);
985 } else if (strcmp(lgopts[opt_idx].name, OPT_TRACE_STEP) == 0) {
986 config.trace_step = get_uint32_opt(optarg,
987 lgopts[opt_idx].name, 1, TRACE_STEP_MAX);
988 } else if (strcmp(lgopts[opt_idx].name,
989 OPT_BLD_CATEGORIES) == 0) {
990 config.bld_categories = get_uint32_opt(optarg,
991 lgopts[opt_idx].name, 1,
992 RTE_ACL_MAX_CATEGORIES);
993 } else if (strcmp(lgopts[opt_idx].name,
994 OPT_RUN_CATEGORIES) == 0) {
995 config.run_categories = get_uint32_opt(optarg,
996 lgopts[opt_idx].name, 1,
997 RTE_ACL_MAX_CATEGORIES);
998 } else if (strcmp(lgopts[opt_idx].name, OPT_ITER_NUM) == 0) {
999 config.iter_num = get_uint32_opt(optarg,
1000 lgopts[opt_idx].name, 1, UINT16_MAX);
1001 } else if (strcmp(lgopts[opt_idx].name, OPT_VERBOSE) == 0) {
1002 config.verbose = get_uint32_opt(optarg,
1003 lgopts[opt_idx].name, DUMP_NONE, DUMP_MAX);
1004 } else if (strcmp(lgopts[opt_idx].name,
1005 OPT_SEARCH_SCALAR) == 0) {
1007 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1011 config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1012 sizeof(struct ipv4_5tuple);
1017 main(int argc, char **argv)
1022 ret = rte_eal_init(argc, argv);
1024 rte_panic("Cannot init EAL\n");
1029 config.prgname = argv[0];
1031 get_input_opts(argc, argv);
1032 dump_config(stdout);
1037 if (config.trace_file != NULL)
1040 RTE_LCORE_FOREACH_SLAVE(lcore)
1041 rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1043 search_ip5tuples(NULL);
1045 rte_eal_mp_wait_lcore();
1047 rte_acl_free(config.acx);