app: no more bare metal environment
[dpdk.git] / app / test-acl / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <rte_acl.h>
35 #include <getopt.h>
36 #include <string.h>
37
38 #ifndef RTE_LIBRTE_ACL_STANDALONE
39
40 #include <rte_cycles.h>
41 #include <rte_per_lcore.h>
42 #include <rte_lcore.h>
43 #include <rte_ip.h>
44
45 #define PRINT_USAGE_START       "%s [EAL options]\n"
46
47 #else
48
49 #define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
50                                 (((b) & 0xff) << 16) |     \
51                                 (((c) & 0xff) << 8)  |     \
52                                 ((d) & 0xff))
53
54 #define RTE_LCORE_FOREACH_SLAVE(x)      while (((x) = 0))
55
56 #define rte_eal_remote_launch(a, b, c)  DUMMY_MACRO
57 #define rte_eal_mp_wait_lcore()         DUMMY_MACRO
58
59 #define rte_eal_init(c, v)      (0)
60
61 #define PRINT_USAGE_START       "%s\n"
62
63 #endif /*RTE_LIBRTE_ACL_STANDALONE */
64
65 #define RTE_LOGTYPE_TESTACL     RTE_LOGTYPE_USER1
66
67 #define APP_NAME        "TESTACL"
68
69 #define GET_CB_FIELD(in, fd, base, lim, dlm)    do {            \
70         unsigned long val;                                      \
71         char *end_fld;                                          \
72         errno = 0;                                              \
73         val = strtoul((in), &end_fld, (base));                  \
74         if (errno != 0 || end_fld[0] != (dlm) || val > (lim))   \
75                 return -EINVAL;                               \
76         (fd) = (typeof(fd))val;                                 \
77         (in) = end_fld + 1;                                     \
78 } while (0)
79
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"
91
92 #define TRACE_DEFAULT_NUM       0x10000
93 #define TRACE_STEP_MAX          0x1000
94 #define TRACE_STEP_DEF          0x100
95
96 #define RULE_NUM                0x10000
97
98 enum {
99         DUMP_NONE,
100         DUMP_SEARCH,
101         DUMP_PKT,
102         DUMP_MAX
103 };
104
105 static struct {
106         const char         *prgname;
107         const char         *rule_file;
108         const char         *trace_file;
109         uint32_t            bld_categories;
110         uint32_t            run_categories;
111         uint32_t            nb_rules;
112         uint32_t            nb_traces;
113         uint32_t            trace_step;
114         uint32_t            trace_sz;
115         uint32_t            iter_num;
116         uint32_t            verbose;
117         uint32_t            scalar;
118         uint32_t            used_traces;
119         void               *traces;
120         struct rte_acl_ctx *acx;
121         uint32_t                        ipv6;
122 } config = {
123         .bld_categories = 3,
124         .run_categories = 1,
125         .nb_rules = RULE_NUM,
126         .nb_traces = TRACE_DEFAULT_NUM,
127         .trace_step = TRACE_STEP_DEF,
128         .iter_num = 1,
129         .verbose = DUMP_MAX,
130         .ipv6 = 0
131 };
132
133 static struct rte_acl_param prm = {
134         .name = APP_NAME,
135         .socket_id = SOCKET_ID_ANY,
136 };
137
138 /*
139  * Rule and trace formats definitions.
140  */
141
142 struct ipv4_5tuple {
143         uint8_t  proto;
144         uint32_t ip_src;
145         uint32_t ip_dst;
146         uint16_t port_src;
147         uint16_t port_dst;
148 };
149
150 enum {
151         PROTO_FIELD_IPV4,
152         SRC_FIELD_IPV4,
153         DST_FIELD_IPV4,
154         SRCP_FIELD_IPV4,
155         DSTP_FIELD_IPV4,
156         NUM_FIELDS_IPV4
157 };
158
159 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
160         {
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),
166         },
167         {
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),
173         },
174         {
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),
180         },
181         {
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),
187         },
188         {
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),
194         },
195 };
196
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))
200
201 struct ipv6_5tuple {
202         uint8_t  proto;
203         uint32_t ip_src[IPV6_ADDR_U32];
204         uint32_t ip_dst[IPV6_ADDR_U32];
205         uint16_t port_src;
206         uint16_t port_dst;
207 };
208
209 enum {
210         PROTO_FIELD_IPV6,
211         SRC1_FIELD_IPV6,
212         SRC2_FIELD_IPV6,
213         SRC3_FIELD_IPV6,
214         SRC4_FIELD_IPV6,
215         DST1_FIELD_IPV6,
216         DST2_FIELD_IPV6,
217         DST3_FIELD_IPV6,
218         DST4_FIELD_IPV6,
219         SRCP_FIELD_IPV6,
220         DSTP_FIELD_IPV6,
221         NUM_FIELDS_IPV6
222 };
223
224 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
225         {
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),
231         },
232         {
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]),
238         },
239         {
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]),
245         },
246         {
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]),
252         },
253         {
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]),
259         },
260         {
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]),
266         },
267         {
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]),
273         },
274         {
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]),
280         },
281         {
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]),
287         },
288         {
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),
294         },
295         {
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),
301         },
302 };
303
304
305 enum {
306         CB_FLD_SRC_ADDR,
307         CB_FLD_DST_ADDR,
308         CB_FLD_SRC_PORT_LOW,
309         CB_FLD_SRC_PORT_DLM,
310         CB_FLD_SRC_PORT_HIGH,
311         CB_FLD_DST_PORT_LOW,
312         CB_FLD_DST_PORT_DLM,
313         CB_FLD_DST_PORT_HIGH,
314         CB_FLD_PROTO,
315         CB_FLD_NUM,
316 };
317
318 enum {
319         CB_TRC_SRC_ADDR,
320         CB_TRC_DST_ADDR,
321         CB_TRC_SRC_PORT,
322         CB_TRC_DST_PORT,
323         CB_TRC_PROTO,
324         CB_TRC_NUM,
325 };
326
327 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
328
329 static const char cb_port_delim[] = ":";
330
331 static char line[LINE_MAX];
332
333 #define dump_verbose(lvl, fh, fmt, args...)     do { \
334         if ((lvl) <= (int32_t)config.verbose)        \
335                 fprintf(fh, fmt, ##args);            \
336 } while (0)
337
338
339 /*
340  * Parse ClassBench input trace (test vectors and expected results) file.
341  * Expected format:
342  * <src_ipv4_addr> <space> <dst_ipv4_addr> <space> \
343  * <src_port> <space> <dst_port> <space> <proto>
344  */
345 static int
346 parse_cb_ipv4_trace(char *str, struct ipv4_5tuple *v)
347 {
348         int i;
349         char *s, *sp, *in[CB_TRC_NUM];
350         static const char *dlm = " \t\n";
351
352         s = str;
353         for (i = 0; i != RTE_DIM(in); i++) {
354                 in[i] = strtok_r(s, dlm, &sp);
355                 if (in[i] == NULL)
356                         return -EINVAL;
357                 s = NULL;
358         }
359
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);
365
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);
371
372         return 0;
373 }
374
375 /*
376  * Parses IPV6 address, exepcts the following format:
377  * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
378  */
379 static int
380 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
381         char dlm)
382 {
383         uint32_t addr[IPV6_ADDR_U16];
384
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);
393
394         *end = in;
395
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];
400
401         return 0;
402 }
403
404 static int
405 parse_cb_ipv6_addr_trace(const char *in, uint32_t v[IPV6_ADDR_U32])
406 {
407         int32_t rc;
408         const char *end;
409
410         rc = parse_ipv6_addr(in, &end, v, 0);
411         if (rc != 0)
412                 return rc;
413
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]);
418
419         return 0;
420 }
421
422 /*
423  * Parse ClassBench input trace (test vectors and expected results) file.
424  * Expected format:
425  * <src_ipv6_addr> <space> <dst_ipv6_addr> <space> \
426  * <src_port> <space> <dst_port> <space> <proto>
427  */
428 static int
429 parse_cb_ipv6_trace(char *str, struct ipv6_5tuple *v)
430 {
431         int32_t i, rc;
432         char *s, *sp, *in[CB_TRC_NUM];
433         static const char *dlm = " \t\n";
434
435         s = str;
436         for (i = 0; i != RTE_DIM(in); i++) {
437                 in[i] = strtok_r(s, dlm, &sp);
438                 if (in[i] == NULL)
439                         return -EINVAL;
440                 s = NULL;
441         }
442
443         /* get ip6 src address. */
444         rc = parse_cb_ipv6_addr_trace(in[CB_TRC_SRC_ADDR], v->ip_src);
445         if (rc != 0)
446                 return rc;
447
448         /* get ip6 dst address. */
449         rc = parse_cb_ipv6_addr_trace(in[CB_TRC_DST_ADDR], v->ip_dst);
450         if (rc != 0)
451                 return rc;
452
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);
456
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);
460
461         return 0;
462 }
463
464 static void
465 tracef_init(void)
466 {
467         static const char name[] = APP_NAME;
468         FILE *f;
469         size_t sz;
470         uint32_t n;
471         struct ipv4_5tuple *v;
472         struct ipv6_5tuple *w;
473
474         sz = config.nb_traces * (config.ipv6 ? sizeof(*w) : sizeof(*v));
475         config.traces = rte_zmalloc_socket(name, sz, CACHE_LINE_SIZE,
476                         SOCKET_ID_ANY);
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);
481
482         f = fopen(config.trace_file, "r");
483         if (f == NULL)
484                 rte_exit(-EINVAL, "failed to open file: %s\n",
485                         config.trace_file);
486
487         v = config.traces;
488         w = config.traces;
489         for (n = 0; n != config.nb_traces; n++) {
490
491                 if (fgets(line, sizeof(line), f) == NULL)
492                         break;
493
494                 if (config.ipv6) {
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);
500                 } else {
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);
506                 }
507         }
508
509         config.used_traces = n;
510         fclose(f);
511 }
512
513 static int
514 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
515 {
516         int32_t rc;
517         const char *mp;
518         uint32_t i, m, v[4];
519         const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
520
521         /* get address. */
522         rc = parse_ipv6_addr(in, &mp, v, '/');
523         if (rc != 0)
524                 return rc;
525
526         /* get mask. */
527         GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
528
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;
533                 else
534                         field[i].mask_range.u32 = m > (i * nbu32) ?
535                                 m - (i * 32) : 0;
536
537                 field[i].value.u32 = v[i];
538         }
539
540         return 0;
541 }
542
543
544 static int
545 parse_cb_ipv6_rule(char *str, struct acl_rule *v)
546 {
547         int i, rc;
548         char *s, *sp, *in[CB_FLD_NUM];
549         static const char *dlm = " \t\n";
550
551         /*
552          * Skip leading '@'
553          */
554         if (strchr(str, '@') != str)
555                 return -EINVAL;
556
557         s = str + 1;
558
559         for (i = 0; i != RTE_DIM(in); i++) {
560                 in[i] = strtok_r(s, dlm, &sp);
561                 if (in[i] == NULL)
562                         return -EINVAL;
563                 s = NULL;
564         }
565
566         rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
567         if (rc != 0) {
568                 RTE_LOG(ERR, TESTACL,
569                         "failed to read source address/mask: %s\n",
570                         in[CB_FLD_SRC_ADDR]);
571                 return rc;
572         }
573
574         rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
575         if (rc != 0) {
576                 RTE_LOG(ERR, TESTACL,
577                         "failed to read destination address/mask: %s\n",
578                         in[CB_FLD_DST_ADDR]);
579                 return rc;
580         }
581
582         /* source port. */
583         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
584                 v->field[SRCP_FIELD_IPV6].value.u16,
585                 0, UINT16_MAX, 0);
586         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
587                 v->field[SRCP_FIELD_IPV6].mask_range.u16,
588                 0, UINT16_MAX, 0);
589
590         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
591                         sizeof(cb_port_delim)) != 0)
592                 return -EINVAL;
593
594         /* destination port. */
595         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
596                 v->field[DSTP_FIELD_IPV6].value.u16,
597                 0, UINT16_MAX, 0);
598         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
599                 v->field[DSTP_FIELD_IPV6].mask_range.u16,
600                 0, UINT16_MAX, 0);
601
602         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
603                         sizeof(cb_port_delim)) != 0)
604                 return -EINVAL;
605
606         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
607                 0, UINT8_MAX, '/');
608         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
609                 0, UINT8_MAX, 0);
610
611         return 0;
612 }
613
614 static int
615 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
616 {
617         uint8_t a, b, c, d, m;
618
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);
624
625         addr[0] = IPv4(a, b, c, d);
626         mask_len[0] = m;
627
628         return 0;
629 }
630 /*
631  * Parse ClassBench rules file.
632  * Expected format:
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> \
637  * <proto>'/'<mask>
638  */
639 static int
640 parse_cb_ipv4_rule(char *str, struct acl_rule *v)
641 {
642         int i, rc;
643         char *s, *sp, *in[CB_FLD_NUM];
644         static const char *dlm = " \t\n";
645
646         /*
647          * Skip leading '@'
648          */
649         if (strchr(str, '@') != str)
650                 return -EINVAL;
651
652         s = str + 1;
653
654         for (i = 0; i != RTE_DIM(in); i++) {
655                 in[i] = strtok_r(s, dlm, &sp);
656                 if (in[i] == NULL)
657                         return -EINVAL;
658                 s = NULL;
659         }
660
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);
664         if (rc != 0) {
665                 RTE_LOG(ERR, TESTACL,
666                         "failed to read source address/mask: %s\n",
667                         in[CB_FLD_SRC_ADDR]);
668                 return rc;
669         }
670
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);
674         if (rc != 0) {
675                 RTE_LOG(ERR, TESTACL,
676                         "failed to read destination address/mask: %s\n",
677                         in[CB_FLD_DST_ADDR]);
678                 return rc;
679         }
680
681         /* source port. */
682         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
683                 v->field[SRCP_FIELD_IPV4].value.u16,
684                 0, UINT16_MAX, 0);
685         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
686                 v->field[SRCP_FIELD_IPV4].mask_range.u16,
687                 0, UINT16_MAX, 0);
688
689         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
690                         sizeof(cb_port_delim)) != 0)
691                 return -EINVAL;
692
693         /* destination port. */
694         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
695                 v->field[DSTP_FIELD_IPV4].value.u16,
696                 0, UINT16_MAX, 0);
697         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
698                 v->field[DSTP_FIELD_IPV4].mask_range.u16,
699                 0, UINT16_MAX, 0);
700
701         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
702                         sizeof(cb_port_delim)) != 0)
703                 return -EINVAL;
704
705         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
706                 0, UINT8_MAX, '/');
707         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
708                 0, UINT8_MAX, 0);
709
710         return 0;
711 }
712
713 typedef int (*parse_5tuple)(char *text, struct acl_rule *rule);
714
715 static int
716 add_cb_rules(FILE *f, struct rte_acl_ctx *ctx)
717 {
718         int rc;
719         uint32_t n;
720         struct acl_rule v;
721         parse_5tuple parser;
722
723         memset(&v, 0, sizeof(v));
724         parser = (config.ipv6 != 0) ? parse_cb_ipv6_rule : parse_cb_ipv4_rule;
725
726         for (n = 1; fgets(line, sizeof(line), f) != NULL; n++) {
727
728                 rc = parser(line, &v);
729                 if (rc != 0) {
730                         RTE_LOG(ERR, TESTACL, "line %u: parse_cb_ipv4vlan_rule"
731                                 " failed, error code: %d (%s)\n",
732                                 n, rc, strerror(-rc));
733                         return rc;
734                 }
735
736                 v.data.category_mask = LEN2MASK(RTE_ACL_MAX_CATEGORIES);
737                 v.data.priority = RTE_ACL_MAX_PRIORITY - n;
738                 v.data.userdata = n;
739
740                 rc = rte_acl_add_rules(ctx, (struct rte_acl_rule *)&v, 1);
741                 if (rc != 0) {
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));
745                         return rc;
746                 }
747         }
748
749         return 0;
750 }
751
752 static void
753 acx_init(void)
754 {
755         int ret;
756         FILE *f;
757         struct rte_acl_config cfg;
758
759         /* setup ACL build config. */
760         if (config.ipv6) {
761                 cfg.num_fields = RTE_DIM(ipv6_defs);
762                 memcpy(&cfg.defs, ipv6_defs, sizeof(ipv6_defs));
763         } else {
764                 cfg.num_fields = RTE_DIM(ipv4_defs);
765                 memcpy(&cfg.defs, ipv4_defs, sizeof(ipv4_defs));
766         }
767         cfg.num_categories = config.bld_categories;
768
769         /* setup ACL creation parameters. */
770         prm.rule_size = RTE_ACL_RULE_SZ(cfg.num_fields);
771         prm.max_rule_num = config.nb_rules;
772
773         config.acx = rte_acl_create(&prm);
774         if (config.acx == NULL)
775                 rte_exit(rte_errno, "failed to create ACL context\n");
776
777         /* set default classify method to scalar for this context. */
778         if (config.scalar) {
779                 ret = rte_acl_set_ctx_classify(config.acx,
780                         RTE_ACL_CLASSIFY_SCALAR);
781                 if (ret != 0)
782                         rte_exit(ret, "failed to setup classify method "
783                                 "for ACL context\n");
784         }
785
786         /* add ACL rules. */
787         f = fopen(config.rule_file, "r");
788         if (f == NULL)
789                 rte_exit(-EINVAL, "failed to open file %s\n",
790                         config.rule_file);
791
792         ret = add_cb_rules(f, config.acx);
793         if (ret != 0)
794                 rte_exit(ret, "failed to add rules into ACL context\n");
795
796         fclose(f);
797
798         /* perform build. */
799         ret = rte_acl_build(config.acx, &cfg);
800
801         dump_verbose(DUMP_NONE, stdout,
802                 "rte_acl_build(%u) finished with %d\n",
803                 config.bld_categories, ret);
804
805         rte_acl_dump(config.acx);
806
807         if (ret != 0)
808                 rte_exit(ret, "failed to build search context\n");
809 }
810
811 static uint32_t
812 search_ip5tuples_once(uint32_t categories, uint32_t step, int scalar)
813 {
814         int ret;
815         uint32_t i, j, k, n, r;
816         const uint8_t *data[step], *v;
817         uint32_t results[step * categories];
818
819         v = config.traces;
820         for (i = 0; i != config.used_traces; i += n) {
821
822                 n = RTE_MIN(step, config.used_traces - i);
823
824                 for (j = 0; j != n; j++) {
825                         data[j] = v;
826                         v += config.trace_sz;
827                 }
828
829                 ret = rte_acl_classify(config.acx, data, results,
830                         n, categories);
831
832                 if (ret != 0)
833                         rte_exit(ret, "classify for ipv%c_5tuples returns %d\n",
834                                 config.ipv6 ? '6' : '4', ret);
835
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, "
840                                         "result: %u\n",
841                                         config.ipv6 ? '6' : '4',
842                                         i + j + 1, k, results[r] - 1);
843                         }
844
845                 }
846         }
847
848         dump_verbose(DUMP_SEARCH, stdout,
849                 "%s(%u, %u, %s) returns %u\n", __func__,
850                 categories, step, scalar != 0 ? "scalar" : "sse", i);
851         return i;
852 }
853
854 static int
855 search_ip5tuples(__attribute__((unused)) void *arg)
856 {
857         uint64_t pkt, start, tm;
858         uint32_t i, lcore;
859
860         lcore = rte_lcore_id();
861         start = rte_rdtsc();
862         pkt = 0;
863
864         for (i = 0; i != config.iter_num; i++) {
865                 pkt += search_ip5tuples_once(config.run_categories,
866                         config.trace_step, config.scalar);
867         }
868
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);
875
876         return 0;
877 }
878
879 static uint32_t
880 get_uint32_opt(const char *opt, const char *name, uint32_t min, uint32_t max)
881 {
882         unsigned long val;
883         char *end;
884
885         errno = 0;
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",
889                         opt, name);
890         return val;
891 }
892
893 static void
894 print_usage(const char *prgname)
895 {
896         fprintf(stdout,
897                 PRINT_USAGE_START
898                 "--" OPT_RULE_FILE "=<rules set file>\n"
899                 "[--" OPT_TRACE_FILE "=<input traces file>]\n"
900                 "[--" OPT_RULE_NUM
901                         "=<maximum number of rules for ACL context>]\n"
902                 "[--" OPT_TRACE_NUM
903                         "=<number of traces to read binary file in>]\n"
904                 "[--" OPT_TRACE_STEP
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);
918 }
919
920 static void
921 dump_config(FILE *f)
922 {
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);
935 }
936
937 static void
938 check_config(void)
939 {
940         if (config.rule_file == NULL) {
941                 print_usage(config.prgname);
942                 rte_exit(-EINVAL, "mandatory option %s is not specified\n",
943                         OPT_RULE_FILE);
944         }
945 }
946
947
948 static void
949 get_input_opts(int argc, char **argv)
950 {
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},
962                 {OPT_IPV6, 0, 0, 0},
963                 {NULL, 0, 0, 0}
964         };
965
966         int opt, opt_idx;
967
968         while ((opt = getopt_long(argc, argv, "", lgopts,  &opt_idx)) != EOF) {
969
970                 if (opt != 0) {
971                         print_usage(config.prgname);
972                         rte_exit(-EINVAL, "unknown option: %c", opt);
973                 }
974
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) {
1006                         config.scalar = 1;
1007                 } else if (strcmp(lgopts[opt_idx].name, OPT_IPV6) == 0) {
1008                         config.ipv6 = 1;
1009                 }
1010         }
1011         config.trace_sz = config.ipv6 ? sizeof(struct ipv6_5tuple) :
1012                                                 sizeof(struct ipv4_5tuple);
1013
1014 }
1015
1016 int
1017 main(int argc, char **argv)
1018 {
1019         int ret;
1020         uint32_t lcore;
1021
1022         ret = rte_eal_init(argc, argv);
1023         if (ret < 0)
1024                 rte_panic("Cannot init EAL\n");
1025
1026         argc -= ret;
1027         argv += ret;
1028
1029         config.prgname = argv[0];
1030
1031         get_input_opts(argc, argv);
1032         dump_config(stdout);
1033         check_config();
1034
1035         acx_init();
1036
1037         if (config.trace_file != NULL)
1038                 tracef_init();
1039
1040         RTE_LCORE_FOREACH_SLAVE(lcore)
1041                  rte_eal_remote_launch(search_ip5tuples, NULL, lcore);
1042
1043         search_ip5tuples(NULL);
1044
1045         rte_eal_mp_wait_lcore();
1046
1047         rte_acl_free(config.acx);
1048         return 0;
1049 }