examples: add eal cleanup to examples
[dpdk.git] / examples / l3fwd-acl / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_memory.h>
20 #include <rte_memcpy.h>
21 #include <rte_eal.h>
22 #include <rte_launch.h>
23 #include <rte_atomic.h>
24 #include <rte_cycles.h>
25 #include <rte_prefetch.h>
26 #include <rte_lcore.h>
27 #include <rte_per_lcore.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_interrupts.h>
30 #include <rte_random.h>
31 #include <rte_debug.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
34 #include <rte_mempool.h>
35 #include <rte_mbuf.h>
36 #include <rte_ip.h>
37 #include <rte_tcp.h>
38 #include <rte_udp.h>
39 #include <rte_string_fns.h>
40 #include <rte_acl.h>
41
42 #include <cmdline_parse.h>
43 #include <cmdline_parse_etheraddr.h>
44
45 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
46 #define L3FWDACL_DEBUG
47 #endif
48 #define DO_RFC_1812_CHECKS
49
50 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
51
52 #define MAX_JUMBO_PKT_LEN  9600
53
54 #define MEMPOOL_CACHE_SIZE 256
55
56 /*
57  * This expression is used to calculate the number of mbufs needed
58  * depending on user input, taking into account memory for rx and tx hardware
59  * rings, cache per lcore and mtable per port per lcore.
60  * RTE_MAX is used to ensure that NB_MBUF never goes below a
61  * minimum value of 8192
62  */
63
64 #define NB_MBUF RTE_MAX(\
65         (nb_ports * nb_rx_queue * nb_rxd +      \
66         nb_ports * nb_lcores * MAX_PKT_BURST +  \
67         nb_ports * n_tx_queue * nb_txd +        \
68         nb_lcores * MEMPOOL_CACHE_SIZE),        \
69         (unsigned)8192)
70
71 #define MAX_PKT_BURST 32
72 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
73
74 #define NB_SOCKETS 8
75
76 /* Configure how many packets ahead to prefetch, when reading packets */
77 #define PREFETCH_OFFSET 3
78
79 /*
80  * Configurable number of RX/TX ring descriptors
81  */
82 #define RTE_TEST_RX_DESC_DEFAULT 1024
83 #define RTE_TEST_TX_DESC_DEFAULT 1024
84 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
85 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
86
87 /* mask of enabled ports */
88 static uint32_t enabled_port_mask;
89 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
90 static int numa_on = 1; /**< NUMA is enabled by default. */
91
92 struct lcore_rx_queue {
93         uint16_t port_id;
94         uint8_t queue_id;
95 } __rte_cache_aligned;
96
97 #define MAX_RX_QUEUE_PER_LCORE 16
98 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
99 #define MAX_RX_QUEUE_PER_PORT 128
100
101 #define MAX_LCORE_PARAMS 1024
102 struct lcore_params {
103         uint16_t port_id;
104         uint8_t queue_id;
105         uint8_t lcore_id;
106 } __rte_cache_aligned;
107
108 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
109 static struct lcore_params lcore_params_array_default[] = {
110         {0, 0, 2},
111         {0, 1, 2},
112         {0, 2, 2},
113         {1, 0, 2},
114         {1, 1, 2},
115         {1, 2, 2},
116         {2, 0, 2},
117         {3, 0, 3},
118         {3, 1, 3},
119 };
120
121 static struct lcore_params *lcore_params = lcore_params_array_default;
122 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
123                                 sizeof(lcore_params_array_default[0]);
124
125 static struct rte_eth_conf port_conf = {
126         .rxmode = {
127                 .mq_mode        = ETH_MQ_RX_RSS,
128                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
129                 .split_hdr_size = 0,
130                 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
131         },
132         .rx_adv_conf = {
133                 .rss_conf = {
134                         .rss_key = NULL,
135                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
136                                 ETH_RSS_TCP | ETH_RSS_SCTP,
137                 },
138         },
139         .txmode = {
140                 .mq_mode = ETH_MQ_TX_NONE,
141         },
142 };
143
144 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
145
146 /* ethernet addresses of ports */
147 static struct rte_ether_hdr port_l2hdr[RTE_MAX_ETHPORTS];
148
149 static const struct {
150         const char *name;
151         enum rte_acl_classify_alg alg;
152 } acl_alg[] = {
153         {
154                 .name = "scalar",
155                 .alg = RTE_ACL_CLASSIFY_SCALAR,
156         },
157         {
158                 .name = "sse",
159                 .alg = RTE_ACL_CLASSIFY_SSE,
160         },
161         {
162                 .name = "avx2",
163                 .alg = RTE_ACL_CLASSIFY_AVX2,
164         },
165         {
166                 .name = "neon",
167                 .alg = RTE_ACL_CLASSIFY_NEON,
168         },
169         {
170                 .name = "altivec",
171                 .alg = RTE_ACL_CLASSIFY_ALTIVEC,
172         },
173         {
174                 .name = "avx512x16",
175                 .alg = RTE_ACL_CLASSIFY_AVX512X16,
176         },
177         {
178                 .name = "avx512x32",
179                 .alg = RTE_ACL_CLASSIFY_AVX512X32,
180         },
181 };
182
183 /***********************start of ACL part******************************/
184 #ifdef DO_RFC_1812_CHECKS
185 static inline int
186 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len);
187 #endif
188 static inline void
189 send_single_packet(struct rte_mbuf *m, uint16_t port);
190
191 #define MAX_ACL_RULE_NUM        100000
192 #define DEFAULT_MAX_CATEGORIES  1
193 #define L3FWD_ACL_IPV4_NAME     "l3fwd-acl-ipv4"
194 #define L3FWD_ACL_IPV6_NAME     "l3fwd-acl-ipv6"
195 #define ACL_LEAD_CHAR           ('@')
196 #define ROUTE_LEAD_CHAR         ('R')
197 #define COMMENT_LEAD_CHAR       ('#')
198
199 enum {
200 #define OPT_CONFIG      "config"
201         OPT_CONFIG_NUM = 256,
202 #define OPT_NONUMA      "no-numa"
203         OPT_NONUMA_NUM,
204 #define OPT_ENBJMO      "enable-jumbo"
205         OPT_ENBJMO_NUM,
206 #define OPT_RULE_IPV4   "rule_ipv4"
207         OPT_RULE_IPV4_NUM,
208 #define OPT_RULE_IPV6   "rule_ipv6"
209         OPT_RULE_IPV6_NUM,
210 #define OPT_ALG         "alg"
211         OPT_ALG_NUM,
212 #define OPT_ETH_DEST    "eth-dest"
213         OPT_ETH_DEST_NUM,
214 };
215
216 #define ACL_DENY_SIGNATURE      0xf0000000
217 #define RTE_LOGTYPE_L3FWDACL    RTE_LOGTYPE_USER3
218 #define acl_log(format, ...)    RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
219 #define uint32_t_to_char(ip, a, b, c, d) do {\
220                 *a = (unsigned char)(ip >> 24 & 0xff);\
221                 *b = (unsigned char)(ip >> 16 & 0xff);\
222                 *c = (unsigned char)(ip >> 8 & 0xff);\
223                 *d = (unsigned char)(ip & 0xff);\
224         } while (0)
225 #define OFF_ETHHEAD     (sizeof(struct rte_ether_hdr))
226 #define OFF_IPV42PROTO (offsetof(struct rte_ipv4_hdr, next_proto_id))
227 #define OFF_IPV62PROTO (offsetof(struct rte_ipv6_hdr, proto))
228 #define MBUF_IPV4_2PROTO(m)     \
229         rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
230 #define MBUF_IPV6_2PROTO(m)     \
231         rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
232
233 #define GET_CB_FIELD(in, fd, base, lim, dlm)    do {            \
234         unsigned long val;                                      \
235         char *end;                                              \
236         errno = 0;                                              \
237         val = strtoul((in), &end, (base));                      \
238         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
239                 return -EINVAL;                               \
240         (fd) = (typeof(fd))val;                                 \
241         (in) = end + 1;                                         \
242 } while (0)
243
244 /*
245   * ACL rules should have higher priorities than route ones to ensure ACL rule
246   * always be found when input packets have multi-matches in the database.
247   * A exception case is performance measure, which can define route rules with
248   * higher priority and route rules will always be returned in each lookup.
249   * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
250   * RTE_ACL_MAX_PRIORITY for route entries in performance measure
251   */
252 #define ACL_RULE_PRIORITY_MAX 0x10000000
253
254 /*
255   * Forward port info save in ACL lib starts from 1
256   * since ACL assume 0 is invalid.
257   * So, need add 1 when saving and minus 1 when forwarding packets.
258   */
259 #define FWD_PORT_SHIFT 1
260
261 /*
262  * Rule and trace formats definitions.
263  */
264
265 enum {
266         PROTO_FIELD_IPV4,
267         SRC_FIELD_IPV4,
268         DST_FIELD_IPV4,
269         SRCP_FIELD_IPV4,
270         DSTP_FIELD_IPV4,
271         NUM_FIELDS_IPV4
272 };
273
274 /*
275  * That effectively defines order of IPV4VLAN classifications:
276  *  - PROTO
277  *  - VLAN (TAG and DOMAIN)
278  *  - SRC IP ADDRESS
279  *  - DST IP ADDRESS
280  *  - PORTS (SRC and DST)
281  */
282 enum {
283         RTE_ACL_IPV4VLAN_PROTO,
284         RTE_ACL_IPV4VLAN_VLAN,
285         RTE_ACL_IPV4VLAN_SRC,
286         RTE_ACL_IPV4VLAN_DST,
287         RTE_ACL_IPV4VLAN_PORTS,
288         RTE_ACL_IPV4VLAN_NUM
289 };
290
291 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
292         {
293                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
294                 .size = sizeof(uint8_t),
295                 .field_index = PROTO_FIELD_IPV4,
296                 .input_index = RTE_ACL_IPV4VLAN_PROTO,
297                 .offset = 0,
298         },
299         {
300                 .type = RTE_ACL_FIELD_TYPE_MASK,
301                 .size = sizeof(uint32_t),
302                 .field_index = SRC_FIELD_IPV4,
303                 .input_index = RTE_ACL_IPV4VLAN_SRC,
304                 .offset = offsetof(struct rte_ipv4_hdr, src_addr) -
305                         offsetof(struct rte_ipv4_hdr, next_proto_id),
306         },
307         {
308                 .type = RTE_ACL_FIELD_TYPE_MASK,
309                 .size = sizeof(uint32_t),
310                 .field_index = DST_FIELD_IPV4,
311                 .input_index = RTE_ACL_IPV4VLAN_DST,
312                 .offset = offsetof(struct rte_ipv4_hdr, dst_addr) -
313                         offsetof(struct rte_ipv4_hdr, next_proto_id),
314         },
315         {
316                 .type = RTE_ACL_FIELD_TYPE_RANGE,
317                 .size = sizeof(uint16_t),
318                 .field_index = SRCP_FIELD_IPV4,
319                 .input_index = RTE_ACL_IPV4VLAN_PORTS,
320                 .offset = sizeof(struct rte_ipv4_hdr) -
321                         offsetof(struct rte_ipv4_hdr, next_proto_id),
322         },
323         {
324                 .type = RTE_ACL_FIELD_TYPE_RANGE,
325                 .size = sizeof(uint16_t),
326                 .field_index = DSTP_FIELD_IPV4,
327                 .input_index = RTE_ACL_IPV4VLAN_PORTS,
328                 .offset = sizeof(struct rte_ipv4_hdr) -
329                         offsetof(struct rte_ipv4_hdr, next_proto_id) +
330                         sizeof(uint16_t),
331         },
332 };
333
334 #define IPV6_ADDR_LEN   16
335 #define IPV6_ADDR_U16   (IPV6_ADDR_LEN / sizeof(uint16_t))
336 #define IPV6_ADDR_U32   (IPV6_ADDR_LEN / sizeof(uint32_t))
337
338 enum {
339         PROTO_FIELD_IPV6,
340         SRC1_FIELD_IPV6,
341         SRC2_FIELD_IPV6,
342         SRC3_FIELD_IPV6,
343         SRC4_FIELD_IPV6,
344         DST1_FIELD_IPV6,
345         DST2_FIELD_IPV6,
346         DST3_FIELD_IPV6,
347         DST4_FIELD_IPV6,
348         SRCP_FIELD_IPV6,
349         DSTP_FIELD_IPV6,
350         NUM_FIELDS_IPV6
351 };
352
353 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
354         {
355                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
356                 .size = sizeof(uint8_t),
357                 .field_index = PROTO_FIELD_IPV6,
358                 .input_index = PROTO_FIELD_IPV6,
359                 .offset = 0,
360         },
361         {
362                 .type = RTE_ACL_FIELD_TYPE_MASK,
363                 .size = sizeof(uint32_t),
364                 .field_index = SRC1_FIELD_IPV6,
365                 .input_index = SRC1_FIELD_IPV6,
366                 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
367                         offsetof(struct rte_ipv6_hdr, proto),
368         },
369         {
370                 .type = RTE_ACL_FIELD_TYPE_MASK,
371                 .size = sizeof(uint32_t),
372                 .field_index = SRC2_FIELD_IPV6,
373                 .input_index = SRC2_FIELD_IPV6,
374                 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
375                         offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
376         },
377         {
378                 .type = RTE_ACL_FIELD_TYPE_MASK,
379                 .size = sizeof(uint32_t),
380                 .field_index = SRC3_FIELD_IPV6,
381                 .input_index = SRC3_FIELD_IPV6,
382                 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
383                         offsetof(struct rte_ipv6_hdr, proto) +
384                         2 * sizeof(uint32_t),
385         },
386         {
387                 .type = RTE_ACL_FIELD_TYPE_MASK,
388                 .size = sizeof(uint32_t),
389                 .field_index = SRC4_FIELD_IPV6,
390                 .input_index = SRC4_FIELD_IPV6,
391                 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
392                         offsetof(struct rte_ipv6_hdr, proto) +
393                         3 * sizeof(uint32_t),
394         },
395         {
396                 .type = RTE_ACL_FIELD_TYPE_MASK,
397                 .size = sizeof(uint32_t),
398                 .field_index = DST1_FIELD_IPV6,
399                 .input_index = DST1_FIELD_IPV6,
400                 .offset = offsetof(struct rte_ipv6_hdr, dst_addr)
401                                 - offsetof(struct rte_ipv6_hdr, proto),
402         },
403         {
404                 .type = RTE_ACL_FIELD_TYPE_MASK,
405                 .size = sizeof(uint32_t),
406                 .field_index = DST2_FIELD_IPV6,
407                 .input_index = DST2_FIELD_IPV6,
408                 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
409                         offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
410         },
411         {
412                 .type = RTE_ACL_FIELD_TYPE_MASK,
413                 .size = sizeof(uint32_t),
414                 .field_index = DST3_FIELD_IPV6,
415                 .input_index = DST3_FIELD_IPV6,
416                 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
417                         offsetof(struct rte_ipv6_hdr, proto) +
418                         2 * sizeof(uint32_t),
419         },
420         {
421                 .type = RTE_ACL_FIELD_TYPE_MASK,
422                 .size = sizeof(uint32_t),
423                 .field_index = DST4_FIELD_IPV6,
424                 .input_index = DST4_FIELD_IPV6,
425                 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
426                         offsetof(struct rte_ipv6_hdr, proto) +
427                         3 * sizeof(uint32_t),
428         },
429         {
430                 .type = RTE_ACL_FIELD_TYPE_RANGE,
431                 .size = sizeof(uint16_t),
432                 .field_index = SRCP_FIELD_IPV6,
433                 .input_index = SRCP_FIELD_IPV6,
434                 .offset = sizeof(struct rte_ipv6_hdr) -
435                         offsetof(struct rte_ipv6_hdr, proto),
436         },
437         {
438                 .type = RTE_ACL_FIELD_TYPE_RANGE,
439                 .size = sizeof(uint16_t),
440                 .field_index = DSTP_FIELD_IPV6,
441                 .input_index = SRCP_FIELD_IPV6,
442                 .offset = sizeof(struct rte_ipv6_hdr) -
443                         offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint16_t),
444         },
445 };
446
447 enum {
448         CB_FLD_SRC_ADDR,
449         CB_FLD_DST_ADDR,
450         CB_FLD_SRC_PORT_LOW,
451         CB_FLD_SRC_PORT_DLM,
452         CB_FLD_SRC_PORT_HIGH,
453         CB_FLD_DST_PORT_LOW,
454         CB_FLD_DST_PORT_DLM,
455         CB_FLD_DST_PORT_HIGH,
456         CB_FLD_PROTO,
457         CB_FLD_USERDATA,
458         CB_FLD_NUM,
459 };
460
461 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
462 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
463
464 struct acl_search_t {
465         const uint8_t *data_ipv4[MAX_PKT_BURST];
466         struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
467         uint32_t res_ipv4[MAX_PKT_BURST];
468         int num_ipv4;
469
470         const uint8_t *data_ipv6[MAX_PKT_BURST];
471         struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
472         uint32_t res_ipv6[MAX_PKT_BURST];
473         int num_ipv6;
474 };
475
476 static struct {
477         char mapped[NB_SOCKETS];
478         struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
479         struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
480 #ifdef L3FWDACL_DEBUG
481         struct acl4_rule *rule_ipv4;
482         struct acl6_rule *rule_ipv6;
483 #endif
484 } acl_config;
485
486 static struct{
487         const char *rule_ipv4_name;
488         const char *rule_ipv6_name;
489         enum rte_acl_classify_alg alg;
490 } parm_config;
491
492 const char cb_port_delim[] = ":";
493
494 static inline void
495 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
496 {
497         unsigned char a, b, c, d;
498
499         uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
500                         &a, &b, &c, &d);
501         printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
502                         rule->field[SRC_FIELD_IPV4].mask_range.u32);
503         uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
504                         &a, &b, &c, &d);
505         printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
506                         rule->field[DST_FIELD_IPV4].mask_range.u32);
507         printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
508                 rule->field[SRCP_FIELD_IPV4].value.u16,
509                 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
510                 rule->field[DSTP_FIELD_IPV4].value.u16,
511                 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
512                 rule->field[PROTO_FIELD_IPV4].value.u8,
513                 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
514         if (extra)
515                 printf("0x%x-0x%x-0x%x ",
516                         rule->data.category_mask,
517                         rule->data.priority,
518                         rule->data.userdata);
519 }
520
521 static inline void
522 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
523 {
524         unsigned char a, b, c, d;
525
526         uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
527                 &a, &b, &c, &d);
528         printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
529         uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
530                 &a, &b, &c, &d);
531         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
532         uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
533                 &a, &b, &c, &d);
534         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
535         uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
536                 &a, &b, &c, &d);
537         printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
538                         rule->field[SRC1_FIELD_IPV6].mask_range.u32
539                         + rule->field[SRC2_FIELD_IPV6].mask_range.u32
540                         + rule->field[SRC3_FIELD_IPV6].mask_range.u32
541                         + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
542
543         uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
544                 &a, &b, &c, &d);
545         printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
546         uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
547                 &a, &b, &c, &d);
548         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
549         uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
550                 &a, &b, &c, &d);
551         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
552         uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
553                 &a, &b, &c, &d);
554         printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
555                         rule->field[DST1_FIELD_IPV6].mask_range.u32
556                         + rule->field[DST2_FIELD_IPV6].mask_range.u32
557                         + rule->field[DST3_FIELD_IPV6].mask_range.u32
558                         + rule->field[DST4_FIELD_IPV6].mask_range.u32);
559
560         printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
561                 rule->field[SRCP_FIELD_IPV6].value.u16,
562                 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
563                 rule->field[DSTP_FIELD_IPV6].value.u16,
564                 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
565                 rule->field[PROTO_FIELD_IPV6].value.u8,
566                 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
567         if (extra)
568                 printf("0x%x-0x%x-0x%x ",
569                         rule->data.category_mask,
570                         rule->data.priority,
571                         rule->data.userdata);
572 }
573
574 /* Bypass comment and empty lines */
575 static inline int
576 is_bypass_line(char *buff)
577 {
578         int i = 0;
579
580         /* comment line */
581         if (buff[0] == COMMENT_LEAD_CHAR)
582                 return 1;
583         /* empty line */
584         while (buff[i] != '\0') {
585                 if (!isspace(buff[i]))
586                         return 0;
587                 i++;
588         }
589         return 1;
590 }
591
592 #ifdef L3FWDACL_DEBUG
593 static inline void
594 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
595 {
596         uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
597         unsigned char a, b, c, d;
598         struct rte_ipv4_hdr *ipv4_hdr =
599                 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
600                                         sizeof(struct rte_ether_hdr));
601
602         uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
603         printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
604         uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
605         printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
606
607         printf("Src port:%hu,Dst port:%hu ",
608                         rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
609                         rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
610         printf("hit ACL %d - ", offset);
611
612         print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
613
614         printf("\n\n");
615 }
616
617 static inline void
618 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
619 {
620         unsigned i;
621         uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
622         struct rte_ipv6_hdr *ipv6_hdr =
623                 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
624                                         sizeof(struct rte_ether_hdr));
625
626         printf("Packet Src");
627         for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
628                 printf(":%.2x%.2x",
629                         ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
630
631         printf("\nDst");
632         for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
633                 printf(":%.2x%.2x",
634                         ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
635
636         printf("\nSrc port:%hu,Dst port:%hu ",
637                         rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
638                         rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
639         printf("hit ACL %d - ", offset);
640
641         print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
642
643         printf("\n\n");
644 }
645 #endif /* L3FWDACL_DEBUG */
646
647 static inline void
648 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
649 {
650         int i;
651
652         for (i = 0; i < num; i++, rule++) {
653                 printf("\t%d:", i + 1);
654                 print_one_ipv4_rule(rule, extra);
655                 printf("\n");
656         }
657 }
658
659 static inline void
660 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
661 {
662         int i;
663
664         for (i = 0; i < num; i++, rule++) {
665                 printf("\t%d:", i + 1);
666                 print_one_ipv6_rule(rule, extra);
667                 printf("\n");
668         }
669 }
670
671 #ifdef DO_RFC_1812_CHECKS
672 static inline void
673 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
674         int index)
675 {
676         struct rte_ipv4_hdr *ipv4_hdr;
677         struct rte_mbuf *pkt = pkts_in[index];
678
679         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
680                 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
681                                                 sizeof(struct rte_ether_hdr));
682
683                 /* Check to make sure the packet is valid (RFC1812) */
684                 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
685
686                         /* Update time to live and header checksum */
687                         --(ipv4_hdr->time_to_live);
688                         ++(ipv4_hdr->hdr_checksum);
689
690                         /* Fill acl structure */
691                         acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
692                         acl->m_ipv4[(acl->num_ipv4)++] = pkt;
693
694                 } else {
695                         /* Not a valid IPv4 packet */
696                         rte_pktmbuf_free(pkt);
697                 }
698         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
699                 /* Fill acl structure */
700                 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
701                 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
702
703         } else {
704                 /* Unknown type, drop the packet */
705                 rte_pktmbuf_free(pkt);
706         }
707 }
708
709 #else
710 static inline void
711 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
712         int index)
713 {
714         struct rte_mbuf *pkt = pkts_in[index];
715
716         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
717                 /* Fill acl structure */
718                 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
719                 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
720
721         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
722                 /* Fill acl structure */
723                 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
724                 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
725         } else {
726                 /* Unknown type, drop the packet */
727                 rte_pktmbuf_free(pkt);
728         }
729 }
730 #endif /* DO_RFC_1812_CHECKS */
731
732 static inline void
733 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
734         int nb_rx)
735 {
736         int i;
737
738         acl->num_ipv4 = 0;
739         acl->num_ipv6 = 0;
740
741         /* Prefetch first packets */
742         for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
743                 rte_prefetch0(rte_pktmbuf_mtod(
744                                 pkts_in[i], void *));
745         }
746
747         for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
748                 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
749                                 i + PREFETCH_OFFSET], void *));
750                 prepare_one_packet(pkts_in, acl, i);
751         }
752
753         /* Process left packets */
754         for (; i < nb_rx; i++)
755                 prepare_one_packet(pkts_in, acl, i);
756 }
757
758 static inline void
759 send_one_packet(struct rte_mbuf *m, uint32_t res)
760 {
761         if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
762                 /* forward packets */
763                 send_single_packet(m,
764                         (uint8_t)(res - FWD_PORT_SHIFT));
765         } else{
766                 /* in the ACL list, drop it */
767 #ifdef L3FWDACL_DEBUG
768                 if ((res & ACL_DENY_SIGNATURE) != 0) {
769                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
770                                 dump_acl4_rule(m, res);
771                         else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
772                                 dump_acl6_rule(m, res);
773                 }
774 #endif
775                 rte_pktmbuf_free(m);
776         }
777 }
778
779
780
781 static inline void
782 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
783 {
784         int i;
785
786         /* Prefetch first packets */
787         for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
788                 rte_prefetch0(rte_pktmbuf_mtod(
789                                 m[i], void *));
790         }
791
792         for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
793                 rte_prefetch0(rte_pktmbuf_mtod(m[
794                                 i + PREFETCH_OFFSET], void *));
795                 send_one_packet(m[i], res[i]);
796         }
797
798         /* Process left packets */
799         for (; i < num; i++)
800                 send_one_packet(m[i], res[i]);
801 }
802
803 /*
804  * Parses IPV6 address, exepcts the following format:
805  * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
806  */
807 static int
808 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
809         char dlm)
810 {
811         uint32_t addr[IPV6_ADDR_U16];
812
813         GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
814         GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
815         GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
816         GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
817         GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
818         GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
819         GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
820         GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
821
822         *end = in;
823
824         v[0] = (addr[0] << 16) + addr[1];
825         v[1] = (addr[2] << 16) + addr[3];
826         v[2] = (addr[4] << 16) + addr[5];
827         v[3] = (addr[6] << 16) + addr[7];
828
829         return 0;
830 }
831
832 static int
833 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
834 {
835         int32_t rc;
836         const char *mp;
837         uint32_t i, m, v[4];
838         const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
839
840         /* get address. */
841         rc = parse_ipv6_addr(in, &mp, v, '/');
842         if (rc != 0)
843                 return rc;
844
845         /* get mask. */
846         GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
847
848         /* put all together. */
849         for (i = 0; i != RTE_DIM(v); i++) {
850                 if (m >= (i + 1) * nbu32)
851                         field[i].mask_range.u32 = nbu32;
852                 else
853                         field[i].mask_range.u32 = m > (i * nbu32) ?
854                                 m - (i * 32) : 0;
855
856                 field[i].value.u32 = v[i];
857         }
858
859         return 0;
860 }
861
862 static int
863 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
864 {
865         int i, rc;
866         char *s, *sp, *in[CB_FLD_NUM];
867         static const char *dlm = " \t\n";
868         int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
869         s = str;
870
871         for (i = 0; i != dim; i++, s = NULL) {
872                 in[i] = strtok_r(s, dlm, &sp);
873                 if (in[i] == NULL)
874                         return -EINVAL;
875         }
876
877         rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
878         if (rc != 0) {
879                 acl_log("failed to read source address/mask: %s\n",
880                         in[CB_FLD_SRC_ADDR]);
881                 return rc;
882         }
883
884         rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
885         if (rc != 0) {
886                 acl_log("failed to read destination address/mask: %s\n",
887                         in[CB_FLD_DST_ADDR]);
888                 return rc;
889         }
890
891         /* source port. */
892         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
893                 v->field[SRCP_FIELD_IPV6].value.u16,
894                 0, UINT16_MAX, 0);
895         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
896                 v->field[SRCP_FIELD_IPV6].mask_range.u16,
897                 0, UINT16_MAX, 0);
898
899         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
900                         sizeof(cb_port_delim)) != 0)
901                 return -EINVAL;
902
903         /* destination port. */
904         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
905                 v->field[DSTP_FIELD_IPV6].value.u16,
906                 0, UINT16_MAX, 0);
907         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
908                 v->field[DSTP_FIELD_IPV6].mask_range.u16,
909                 0, UINT16_MAX, 0);
910
911         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
912                         sizeof(cb_port_delim)) != 0)
913                 return -EINVAL;
914
915         if (v->field[SRCP_FIELD_IPV6].mask_range.u16
916                         < v->field[SRCP_FIELD_IPV6].value.u16
917                         || v->field[DSTP_FIELD_IPV6].mask_range.u16
918                         < v->field[DSTP_FIELD_IPV6].value.u16)
919                 return -EINVAL;
920
921         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
922                 0, UINT8_MAX, '/');
923         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
924                 0, UINT8_MAX, 0);
925
926         if (has_userdata)
927                 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
928                         0, UINT32_MAX, 0);
929
930         return 0;
931 }
932
933 /*
934  * Parse ClassBench rules file.
935  * Expected format:
936  * '@'<src_ipv4_addr>'/'<masklen> <space> \
937  * <dst_ipv4_addr>'/'<masklen> <space> \
938  * <src_port_low> <space> ":" <src_port_high> <space> \
939  * <dst_port_low> <space> ":" <dst_port_high> <space> \
940  * <proto>'/'<mask>
941  */
942 static int
943 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
944 {
945         uint8_t a, b, c, d, m;
946
947         GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
948         GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
949         GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
950         GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
951         GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
952
953         addr[0] = RTE_IPV4(a, b, c, d);
954         mask_len[0] = m;
955
956         return 0;
957 }
958
959 static int
960 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
961 {
962         int i, rc;
963         char *s, *sp, *in[CB_FLD_NUM];
964         static const char *dlm = " \t\n";
965         int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
966         s = str;
967
968         for (i = 0; i != dim; i++, s = NULL) {
969                 in[i] = strtok_r(s, dlm, &sp);
970                 if (in[i] == NULL)
971                         return -EINVAL;
972         }
973
974         rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
975                         &v->field[SRC_FIELD_IPV4].value.u32,
976                         &v->field[SRC_FIELD_IPV4].mask_range.u32);
977         if (rc != 0) {
978                         acl_log("failed to read source address/mask: %s\n",
979                         in[CB_FLD_SRC_ADDR]);
980                 return rc;
981         }
982
983         rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
984                         &v->field[DST_FIELD_IPV4].value.u32,
985                         &v->field[DST_FIELD_IPV4].mask_range.u32);
986         if (rc != 0) {
987                 acl_log("failed to read destination address/mask: %s\n",
988                         in[CB_FLD_DST_ADDR]);
989                 return rc;
990         }
991
992         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
993                 v->field[SRCP_FIELD_IPV4].value.u16,
994                 0, UINT16_MAX, 0);
995         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
996                 v->field[SRCP_FIELD_IPV4].mask_range.u16,
997                 0, UINT16_MAX, 0);
998
999         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
1000                         sizeof(cb_port_delim)) != 0)
1001                 return -EINVAL;
1002
1003         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
1004                 v->field[DSTP_FIELD_IPV4].value.u16,
1005                 0, UINT16_MAX, 0);
1006         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
1007                 v->field[DSTP_FIELD_IPV4].mask_range.u16,
1008                 0, UINT16_MAX, 0);
1009
1010         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
1011                         sizeof(cb_port_delim)) != 0)
1012                 return -EINVAL;
1013
1014         if (v->field[SRCP_FIELD_IPV4].mask_range.u16
1015                         < v->field[SRCP_FIELD_IPV4].value.u16
1016                         || v->field[DSTP_FIELD_IPV4].mask_range.u16
1017                         < v->field[DSTP_FIELD_IPV4].value.u16)
1018                 return -EINVAL;
1019
1020         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1021                 0, UINT8_MAX, '/');
1022         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1023                 0, UINT8_MAX, 0);
1024
1025         if (has_userdata)
1026                 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1027                         UINT32_MAX, 0);
1028
1029         return 0;
1030 }
1031
1032 static int
1033 add_rules(const char *rule_path,
1034                 struct rte_acl_rule **proute_base,
1035                 unsigned int *proute_num,
1036                 struct rte_acl_rule **pacl_base,
1037                 unsigned int *pacl_num, uint32_t rule_size,
1038                 int (*parser)(char *, struct rte_acl_rule*, int))
1039 {
1040         uint8_t *acl_rules, *route_rules;
1041         struct rte_acl_rule *next;
1042         unsigned int acl_num = 0, route_num = 0, total_num = 0;
1043         unsigned int acl_cnt = 0, route_cnt = 0;
1044         char buff[LINE_MAX];
1045         FILE *fh = fopen(rule_path, "rb");
1046         unsigned int i = 0;
1047         int val;
1048
1049         if (fh == NULL)
1050                 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1051                         rule_path);
1052
1053         while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1054                 if (buff[0] == ROUTE_LEAD_CHAR)
1055                         route_num++;
1056                 else if (buff[0] == ACL_LEAD_CHAR)
1057                         acl_num++;
1058         }
1059
1060         if (0 == route_num)
1061                 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1062                                 rule_path);
1063
1064         val = fseek(fh, 0, SEEK_SET);
1065         if (val < 0) {
1066                 rte_exit(EXIT_FAILURE, "%s: File seek operation failed\n",
1067                         __func__);
1068         }
1069
1070         acl_rules = calloc(acl_num, rule_size);
1071
1072         if (NULL == acl_rules)
1073                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1074                         __func__);
1075
1076         route_rules = calloc(route_num, rule_size);
1077
1078         if (NULL == route_rules)
1079                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1080                         __func__);
1081
1082         i = 0;
1083         while (fgets(buff, LINE_MAX, fh) != NULL) {
1084                 i++;
1085
1086                 if (is_bypass_line(buff))
1087                         continue;
1088
1089                 char s = buff[0];
1090
1091                 /* Route entry */
1092                 if (s == ROUTE_LEAD_CHAR)
1093                         next = (struct rte_acl_rule *)(route_rules +
1094                                 route_cnt * rule_size);
1095
1096                 /* ACL entry */
1097                 else if (s == ACL_LEAD_CHAR)
1098                         next = (struct rte_acl_rule *)(acl_rules +
1099                                 acl_cnt * rule_size);
1100
1101                 /* Illegal line */
1102                 else
1103                         rte_exit(EXIT_FAILURE,
1104                                 "%s Line %u: should start with leading "
1105                                 "char %c or %c\n",
1106                                 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1107
1108                 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1109                         rte_exit(EXIT_FAILURE,
1110                                 "%s Line %u: parse rules error\n",
1111                                 rule_path, i);
1112
1113                 if (s == ROUTE_LEAD_CHAR) {
1114                         /* Check the forwarding port number */
1115                         if ((enabled_port_mask & (1 << next->data.userdata)) ==
1116                                         0)
1117                                 rte_exit(EXIT_FAILURE,
1118                                         "%s Line %u: fwd number illegal:%u\n",
1119                                         rule_path, i, next->data.userdata);
1120                         next->data.userdata += FWD_PORT_SHIFT;
1121                         route_cnt++;
1122                 } else {
1123                         next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1124                         acl_cnt++;
1125                 }
1126
1127                 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1128                 next->data.category_mask = -1;
1129                 total_num++;
1130         }
1131
1132         fclose(fh);
1133
1134         *pacl_base = (struct rte_acl_rule *)acl_rules;
1135         *pacl_num = acl_num;
1136         *proute_base = (struct rte_acl_rule *)route_rules;
1137         *proute_num = route_cnt;
1138
1139         return 0;
1140 }
1141
1142 static int
1143 usage_acl_alg(char *buf, size_t sz)
1144 {
1145         uint32_t i, n, rc, tn;
1146
1147         n = 0;
1148         tn = 0;
1149         for (i = 0; i < RTE_DIM(acl_alg); i++) {
1150                 rc = snprintf(buf + n, sz - n,
1151                         i == RTE_DIM(acl_alg) - 1 ? "%s" : "%s|",
1152                         acl_alg[i].name);
1153                 tn += rc;
1154                 if (rc < sz - n)
1155                         n += rc;
1156         }
1157
1158         return tn;
1159 }
1160
1161 static const char *
1162 str_acl_alg(enum rte_acl_classify_alg alg)
1163 {
1164         uint32_t i;
1165
1166         for (i = 0; i != RTE_DIM(acl_alg); i++) {
1167                 if (alg == acl_alg[i].alg)
1168                         return acl_alg[i].name;
1169         }
1170
1171         return "default";
1172 }
1173
1174 static enum rte_acl_classify_alg
1175 parse_acl_alg(const char *alg)
1176 {
1177         uint32_t i;
1178
1179         for (i = 0; i != RTE_DIM(acl_alg); i++) {
1180                 if (strcmp(alg, acl_alg[i].name) == 0)
1181                         return acl_alg[i].alg;
1182         }
1183
1184         return RTE_ACL_CLASSIFY_DEFAULT;
1185 }
1186
1187 static void
1188 dump_acl_config(void)
1189 {
1190         printf("ACL option are:\n");
1191         printf(OPT_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1192         printf(OPT_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1193         printf(OPT_ALG": %s\n", str_acl_alg(parm_config.alg));
1194 }
1195
1196 static int
1197 check_acl_config(void)
1198 {
1199         if (parm_config.rule_ipv4_name == NULL) {
1200                 acl_log("ACL IPv4 rule file not specified\n");
1201                 return -1;
1202         } else if (parm_config.rule_ipv6_name == NULL) {
1203                 acl_log("ACL IPv6 rule file not specified\n");
1204                 return -1;
1205         }
1206
1207         return 0;
1208 }
1209
1210 static struct rte_acl_ctx*
1211 setup_acl(struct rte_acl_rule *route_base,
1212                 struct rte_acl_rule *acl_base, unsigned int route_num,
1213                 unsigned int acl_num, int ipv6, int socketid)
1214 {
1215         char name[PATH_MAX];
1216         struct rte_acl_param acl_param;
1217         struct rte_acl_config acl_build_param;
1218         struct rte_acl_ctx *context;
1219         int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1220
1221         /* Create ACL contexts */
1222         snprintf(name, sizeof(name), "%s%d",
1223                         ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1224                         socketid);
1225
1226         acl_param.name = name;
1227         acl_param.socket_id = socketid;
1228         acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1229         acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1230
1231         if ((context = rte_acl_create(&acl_param)) == NULL)
1232                 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1233
1234         if (parm_config.alg != RTE_ACL_CLASSIFY_DEFAULT &&
1235                         rte_acl_set_ctx_classify(context, parm_config.alg) != 0)
1236                 rte_exit(EXIT_FAILURE,
1237                         "Failed to setup classify method for  ACL context\n");
1238
1239         if (rte_acl_add_rules(context, route_base, route_num) < 0)
1240                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1241
1242         if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1243                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1244
1245         /* Perform builds */
1246         memset(&acl_build_param, 0, sizeof(acl_build_param));
1247
1248         acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1249         acl_build_param.num_fields = dim;
1250         memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1251                 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1252
1253         if (rte_acl_build(context, &acl_build_param) != 0)
1254                 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1255
1256         rte_acl_dump(context);
1257
1258         return context;
1259 }
1260
1261 static int
1262 app_acl_init(void)
1263 {
1264         unsigned lcore_id;
1265         unsigned int i;
1266         int socketid;
1267         struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1268                 *acl_base_ipv6, *route_base_ipv6;
1269         unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1270                 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1271
1272         if (check_acl_config() != 0)
1273                 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1274
1275         dump_acl_config();
1276
1277         /* Load  rules from the input file */
1278         if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1279                         &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1280                         sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1281                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1282
1283         acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1284         dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1285
1286         acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1287         dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1288
1289         if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1290                         &route_num_ipv6,
1291                         &acl_base_ipv6, &acl_num_ipv6,
1292                         sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1293                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1294
1295         acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1296         dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1297
1298         acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1299         dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1300
1301         memset(&acl_config, 0, sizeof(acl_config));
1302
1303         /* Check sockets a context should be created on */
1304         if (!numa_on)
1305                 acl_config.mapped[0] = 1;
1306         else {
1307                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1308                         if (rte_lcore_is_enabled(lcore_id) == 0)
1309                                 continue;
1310
1311                         socketid = rte_lcore_to_socket_id(lcore_id);
1312                         if (socketid >= NB_SOCKETS) {
1313                                 acl_log("Socket %d of lcore %u is out "
1314                                         "of range %d\n",
1315                                         socketid, lcore_id, NB_SOCKETS);
1316                                 free(route_base_ipv4);
1317                                 free(route_base_ipv6);
1318                                 free(acl_base_ipv4);
1319                                 free(acl_base_ipv6);
1320                                 return -1;
1321                         }
1322
1323                         acl_config.mapped[socketid] = 1;
1324                 }
1325         }
1326
1327         for (i = 0; i < NB_SOCKETS; i++) {
1328                 if (acl_config.mapped[i]) {
1329                         acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1330                                 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1331                                 0, i);
1332
1333                         acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1334                                 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1335                                 1, i);
1336                 }
1337         }
1338
1339         free(route_base_ipv4);
1340         free(route_base_ipv6);
1341
1342 #ifdef L3FWDACL_DEBUG
1343         acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1344         acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1345 #else
1346         free(acl_base_ipv4);
1347         free(acl_base_ipv6);
1348 #endif
1349
1350         return 0;
1351 }
1352
1353 /***********************end of ACL part******************************/
1354
1355 struct lcore_conf {
1356         uint16_t n_rx_queue;
1357         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1358         uint16_t n_tx_port;
1359         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1360         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1361         struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1362 } __rte_cache_aligned;
1363
1364 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1365
1366 /* Enqueue a single packet, and send burst if queue is filled */
1367 static inline void
1368 send_single_packet(struct rte_mbuf *m, uint16_t port)
1369 {
1370         uint32_t lcore_id;
1371         struct lcore_conf *qconf;
1372         struct rte_ether_hdr *eh;
1373
1374         lcore_id = rte_lcore_id();
1375
1376         /* update src and dst mac*/
1377         eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1378         memcpy(eh, &port_l2hdr[port], sizeof(eh->d_addr) + sizeof(eh->s_addr));
1379
1380         qconf = &lcore_conf[lcore_id];
1381         rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1382                         qconf->tx_buffer[port], m);
1383 }
1384
1385 #ifdef DO_RFC_1812_CHECKS
1386 static inline int
1387 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
1388 {
1389         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1390         /*
1391          * 1. The packet length reported by the Link Layer must be large
1392          * enough to hold the minimum length legal IP datagram (20 bytes).
1393          */
1394         if (link_len < sizeof(struct rte_ipv4_hdr))
1395                 return -1;
1396
1397         /* 2. The IP checksum must be correct. */
1398         /* this is checked in H/W */
1399
1400         /*
1401          * 3. The IP version number must be 4. If the version number is not 4
1402          * then the packet may be another version of IP, such as IPng or
1403          * ST-II.
1404          */
1405         if (((pkt->version_ihl) >> 4) != 4)
1406                 return -3;
1407         /*
1408          * 4. The IP header length field must be large enough to hold the
1409          * minimum length legal IP datagram (20 bytes = 5 words).
1410          */
1411         if ((pkt->version_ihl & 0xf) < 5)
1412                 return -4;
1413
1414         /*
1415          * 5. The IP total length field must be large enough to hold the IP
1416          * datagram header, whose length is specified in the IP header length
1417          * field.
1418          */
1419         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
1420                 return -5;
1421
1422         return 0;
1423 }
1424 #endif
1425
1426 /* main processing loop */
1427 static int
1428 main_loop(__rte_unused void *dummy)
1429 {
1430         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1431         unsigned lcore_id;
1432         uint64_t prev_tsc, diff_tsc, cur_tsc;
1433         int i, nb_rx;
1434         uint16_t portid;
1435         uint8_t queueid;
1436         struct lcore_conf *qconf;
1437         int socketid;
1438         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1439                         / US_PER_S * BURST_TX_DRAIN_US;
1440
1441         prev_tsc = 0;
1442         lcore_id = rte_lcore_id();
1443         qconf = &lcore_conf[lcore_id];
1444         socketid = rte_lcore_to_socket_id(lcore_id);
1445
1446         if (qconf->n_rx_queue == 0) {
1447                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1448                 return 0;
1449         }
1450
1451         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1452
1453         for (i = 0; i < qconf->n_rx_queue; i++) {
1454
1455                 portid = qconf->rx_queue_list[i].port_id;
1456                 queueid = qconf->rx_queue_list[i].queue_id;
1457                 RTE_LOG(INFO, L3FWD,
1458                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1459                         lcore_id, portid, queueid);
1460         }
1461
1462         while (1) {
1463
1464                 cur_tsc = rte_rdtsc();
1465
1466                 /*
1467                  * TX burst queue drain
1468                  */
1469                 diff_tsc = cur_tsc - prev_tsc;
1470                 if (unlikely(diff_tsc > drain_tsc)) {
1471                         for (i = 0; i < qconf->n_tx_port; ++i) {
1472                                 portid = qconf->tx_port_id[i];
1473                                 rte_eth_tx_buffer_flush(portid,
1474                                                 qconf->tx_queue_id[portid],
1475                                                 qconf->tx_buffer[portid]);
1476                         }
1477                         prev_tsc = cur_tsc;
1478                 }
1479
1480                 /*
1481                  * Read packet from RX queues
1482                  */
1483                 for (i = 0; i < qconf->n_rx_queue; ++i) {
1484
1485                         portid = qconf->rx_queue_list[i].port_id;
1486                         queueid = qconf->rx_queue_list[i].queue_id;
1487                         nb_rx = rte_eth_rx_burst(portid, queueid,
1488                                 pkts_burst, MAX_PKT_BURST);
1489
1490                         if (nb_rx > 0) {
1491                                 struct acl_search_t acl_search;
1492
1493                                 prepare_acl_parameter(pkts_burst, &acl_search,
1494                                         nb_rx);
1495
1496                                 if (acl_search.num_ipv4) {
1497                                         rte_acl_classify(
1498                                                 acl_config.acx_ipv4[socketid],
1499                                                 acl_search.data_ipv4,
1500                                                 acl_search.res_ipv4,
1501                                                 acl_search.num_ipv4,
1502                                                 DEFAULT_MAX_CATEGORIES);
1503
1504                                         send_packets(acl_search.m_ipv4,
1505                                                 acl_search.res_ipv4,
1506                                                 acl_search.num_ipv4);
1507                                 }
1508
1509                                 if (acl_search.num_ipv6) {
1510                                         rte_acl_classify(
1511                                                 acl_config.acx_ipv6[socketid],
1512                                                 acl_search.data_ipv6,
1513                                                 acl_search.res_ipv6,
1514                                                 acl_search.num_ipv6,
1515                                                 DEFAULT_MAX_CATEGORIES);
1516
1517                                         send_packets(acl_search.m_ipv6,
1518                                                 acl_search.res_ipv6,
1519                                                 acl_search.num_ipv6);
1520                                 }
1521                         }
1522                 }
1523         }
1524 }
1525
1526 static int
1527 check_lcore_params(void)
1528 {
1529         uint8_t queue, lcore;
1530         uint16_t i;
1531         int socketid;
1532
1533         for (i = 0; i < nb_lcore_params; ++i) {
1534                 queue = lcore_params[i].queue_id;
1535                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1536                         printf("invalid queue number: %hhu\n", queue);
1537                         return -1;
1538                 }
1539                 lcore = lcore_params[i].lcore_id;
1540                 if (!rte_lcore_is_enabled(lcore)) {
1541                         printf("error: lcore %hhu is not enabled in "
1542                                 "lcore mask\n", lcore);
1543                         return -1;
1544                 }
1545                 socketid = rte_lcore_to_socket_id(lcore);
1546                 if (socketid != 0 && numa_on == 0) {
1547                         printf("warning: lcore %hhu is on socket %d "
1548                                 "with numa off\n",
1549                                 lcore, socketid);
1550                 }
1551         }
1552         return 0;
1553 }
1554
1555 static int
1556 check_port_config(void)
1557 {
1558         unsigned portid;
1559         uint16_t i;
1560
1561         for (i = 0; i < nb_lcore_params; ++i) {
1562                 portid = lcore_params[i].port_id;
1563
1564                 if ((enabled_port_mask & (1 << portid)) == 0) {
1565                         printf("port %u is not enabled in port mask\n", portid);
1566                         return -1;
1567                 }
1568                 if (!rte_eth_dev_is_valid_port(portid)) {
1569                         printf("port %u is not present on the board\n", portid);
1570                         return -1;
1571                 }
1572         }
1573         return 0;
1574 }
1575
1576 static uint8_t
1577 get_port_n_rx_queues(const uint16_t port)
1578 {
1579         int queue = -1;
1580         uint16_t i;
1581
1582         for (i = 0; i < nb_lcore_params; ++i) {
1583                 if (lcore_params[i].port_id == port &&
1584                                 lcore_params[i].queue_id > queue)
1585                         queue = lcore_params[i].queue_id;
1586         }
1587         return (uint8_t)(++queue);
1588 }
1589
1590 static int
1591 init_lcore_rx_queues(void)
1592 {
1593         uint16_t i, nb_rx_queue;
1594         uint8_t lcore;
1595
1596         for (i = 0; i < nb_lcore_params; ++i) {
1597                 lcore = lcore_params[i].lcore_id;
1598                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1599                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1600                         printf("error: too many queues (%u) for lcore: %u\n",
1601                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1602                         return -1;
1603                 } else {
1604                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1605                                 lcore_params[i].port_id;
1606                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1607                                 lcore_params[i].queue_id;
1608                         lcore_conf[lcore].n_rx_queue++;
1609                 }
1610         }
1611         return 0;
1612 }
1613
1614 /* display usage */
1615 static void
1616 print_usage(const char *prgname)
1617 {
1618         char alg[PATH_MAX];
1619
1620         usage_acl_alg(alg, sizeof(alg));
1621         printf("%s [EAL options] -- -p PORTMASK -P"
1622                 "--"OPT_RULE_IPV4"=FILE"
1623                 "--"OPT_RULE_IPV6"=FILE"
1624                 "  [--"OPT_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1625                 "  [--"OPT_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1626                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1627                 "  -P : enable promiscuous mode\n"
1628                 "  --"OPT_CONFIG": (port,queue,lcore): "
1629                 "rx queues configuration\n"
1630                 "  --"OPT_NONUMA": optional, disable numa awareness\n"
1631                 "  --"OPT_ENBJMO": enable jumbo frame"
1632                 " which max packet len is PKTLEN in decimal (64-9600)\n"
1633                 "  --"OPT_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1634                 "file. "
1635                 "Each rule occupy one line. "
1636                 "2 kinds of rules are supported. "
1637                 "One is ACL entry at while line leads with character '%c', "
1638                 "another is route entry at while line leads with "
1639                 "character '%c'.\n"
1640                 "  --"OPT_RULE_IPV6"=FILE: specify the ipv6 rules "
1641                 "entries file.\n"
1642                 "  --"OPT_ALG": ACL classify method to use, one of: %s\n",
1643                 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR, alg);
1644 }
1645
1646 static int
1647 parse_max_pkt_len(const char *pktlen)
1648 {
1649         char *end = NULL;
1650         unsigned long len;
1651
1652         /* parse decimal string */
1653         len = strtoul(pktlen, &end, 10);
1654         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1655                 return -1;
1656
1657         if (len == 0)
1658                 return -1;
1659
1660         return len;
1661 }
1662
1663 static int
1664 parse_portmask(const char *portmask)
1665 {
1666         char *end = NULL;
1667         unsigned long pm;
1668
1669         /* parse hexadecimal string */
1670         pm = strtoul(portmask, &end, 16);
1671         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1672                 return 0;
1673
1674         return pm;
1675 }
1676
1677 static int
1678 parse_config(const char *q_arg)
1679 {
1680         char s[256];
1681         const char *p, *p0 = q_arg;
1682         char *end;
1683         enum fieldnames {
1684                 FLD_PORT = 0,
1685                 FLD_QUEUE,
1686                 FLD_LCORE,
1687                 _NUM_FLD
1688         };
1689         unsigned long int_fld[_NUM_FLD];
1690         char *str_fld[_NUM_FLD];
1691         int i;
1692         unsigned size;
1693
1694         nb_lcore_params = 0;
1695
1696         while ((p = strchr(p0, '(')) != NULL) {
1697                 ++p;
1698                 if ((p0 = strchr(p, ')')) == NULL)
1699                         return -1;
1700
1701                 size = p0 - p;
1702                 if (size >= sizeof(s))
1703                         return -1;
1704
1705                 snprintf(s, sizeof(s), "%.*s", size, p);
1706                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1707                                 _NUM_FLD)
1708                         return -1;
1709                 for (i = 0; i < _NUM_FLD; i++) {
1710                         errno = 0;
1711                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1712                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1713                                 return -1;
1714                 }
1715                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1716                         printf("exceeded max number of lcore params: %hu\n",
1717                                 nb_lcore_params);
1718                         return -1;
1719                 }
1720                 lcore_params_array[nb_lcore_params].port_id =
1721                         (uint8_t)int_fld[FLD_PORT];
1722                 lcore_params_array[nb_lcore_params].queue_id =
1723                         (uint8_t)int_fld[FLD_QUEUE];
1724                 lcore_params_array[nb_lcore_params].lcore_id =
1725                         (uint8_t)int_fld[FLD_LCORE];
1726                 ++nb_lcore_params;
1727         }
1728         lcore_params = lcore_params_array;
1729         return 0;
1730 }
1731
1732 static const char *
1733 parse_eth_dest(const char *optarg)
1734 {
1735         unsigned long portid;
1736         char *port_end;
1737
1738         errno = 0;
1739         portid = strtoul(optarg, &port_end, 0);
1740         if (errno != 0 || port_end == optarg || *port_end++ != ',')
1741                 return "Invalid format";
1742         else if (portid >= RTE_MAX_ETHPORTS)
1743                 return "port value exceeds RTE_MAX_ETHPORTS("
1744                         RTE_STR(RTE_MAX_ETHPORTS) ")";
1745
1746         if (cmdline_parse_etheraddr(NULL, port_end, &port_l2hdr[portid].d_addr,
1747                         sizeof(port_l2hdr[portid].d_addr)) < 0)
1748                 return "Invalid ethernet address";
1749         return NULL;
1750 }
1751
1752 /* Parse the argument given in the command line of the application */
1753 static int
1754 parse_args(int argc, char **argv)
1755 {
1756         int opt, ret;
1757         char **argvopt;
1758         int option_index;
1759         char *prgname = argv[0];
1760         static struct option lgopts[] = {
1761                 {OPT_CONFIG,    1, NULL, OPT_CONFIG_NUM    },
1762                 {OPT_NONUMA,    0, NULL, OPT_NONUMA_NUM    },
1763                 {OPT_ENBJMO,    0, NULL, OPT_ENBJMO_NUM    },
1764                 {OPT_RULE_IPV4, 1, NULL, OPT_RULE_IPV4_NUM },
1765                 {OPT_RULE_IPV6, 1, NULL, OPT_RULE_IPV6_NUM },
1766                 {OPT_ALG,       1, NULL, OPT_ALG_NUM       },
1767                 {OPT_ETH_DEST,  1, NULL, OPT_ETH_DEST_NUM  },
1768                 {NULL,          0, 0,    0                 }
1769         };
1770
1771         argvopt = argv;
1772
1773         while ((opt = getopt_long(argc, argvopt, "p:P",
1774                                 lgopts, &option_index)) != EOF) {
1775
1776                 switch (opt) {
1777                 /* portmask */
1778                 case 'p':
1779                         enabled_port_mask = parse_portmask(optarg);
1780                         if (enabled_port_mask == 0) {
1781                                 printf("invalid portmask\n");
1782                                 print_usage(prgname);
1783                                 return -1;
1784                         }
1785                         break;
1786
1787                 case 'P':
1788                         printf("Promiscuous mode selected\n");
1789                         promiscuous_on = 1;
1790                         break;
1791
1792                 /* long options */
1793                 case OPT_CONFIG_NUM:
1794                         ret = parse_config(optarg);
1795                         if (ret) {
1796                                 printf("invalid config\n");
1797                                 print_usage(prgname);
1798                                 return -1;
1799                         }
1800                         break;
1801
1802                 case OPT_NONUMA_NUM:
1803                         printf("numa is disabled\n");
1804                         numa_on = 0;
1805                         break;
1806
1807                 case OPT_ENBJMO_NUM:
1808                 {
1809                         struct option lenopts = {
1810                                 "max-pkt-len",
1811                                 required_argument,
1812                                 0,
1813                                 0
1814                         };
1815
1816                         printf("jumbo frame is enabled\n");
1817                         port_conf.rxmode.offloads |=
1818                                         DEV_RX_OFFLOAD_JUMBO_FRAME;
1819                         port_conf.txmode.offloads |=
1820                                         DEV_TX_OFFLOAD_MULTI_SEGS;
1821
1822                         /*
1823                          * if no max-pkt-len set, then use the
1824                          * default value RTE_ETHER_MAX_LEN
1825                          */
1826                         if (getopt_long(argc, argvopt, "",
1827                                         &lenopts, &option_index) == 0) {
1828                                 ret = parse_max_pkt_len(optarg);
1829                                 if ((ret < 64) ||
1830                                         (ret > MAX_JUMBO_PKT_LEN)) {
1831                                         printf("invalid packet "
1832                                                 "length\n");
1833                                         print_usage(prgname);
1834                                         return -1;
1835                                 }
1836                                 port_conf.rxmode.max_rx_pkt_len = ret;
1837                         }
1838                         printf("set jumbo frame max packet length "
1839                                 "to %u\n",
1840                                 (unsigned int)
1841                                 port_conf.rxmode.max_rx_pkt_len);
1842                         break;
1843                 }
1844                 case OPT_RULE_IPV4_NUM:
1845                         parm_config.rule_ipv4_name = optarg;
1846                         break;
1847
1848                 case OPT_RULE_IPV6_NUM:
1849                         parm_config.rule_ipv6_name = optarg;
1850                         break;
1851
1852                 case OPT_ALG_NUM:
1853                         parm_config.alg = parse_acl_alg(optarg);
1854                         if (parm_config.alg ==
1855                                         RTE_ACL_CLASSIFY_DEFAULT) {
1856                                 printf("unknown %s value:\"%s\"\n",
1857                                         OPT_ALG, optarg);
1858                                 print_usage(prgname);
1859                                 return -1;
1860                         }
1861                         break;
1862
1863                 case OPT_ETH_DEST_NUM:
1864                 {
1865                         const char *serr = parse_eth_dest(optarg);
1866                         if (serr != NULL) {
1867                                 printf("invalid %s value:\"%s\": %s\n",
1868                                         OPT_ETH_DEST, optarg, serr);
1869                                 print_usage(prgname);
1870                                 return -1;
1871                         }
1872                         break;
1873                 }
1874                 default:
1875                         print_usage(prgname);
1876                         return -1;
1877                 }
1878         }
1879
1880         if (optind >= 0)
1881                 argv[optind-1] = prgname;
1882
1883         ret = optind-1;
1884         optind = 1; /* reset getopt lib */
1885         return ret;
1886 }
1887
1888 static void
1889 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1890 {
1891         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1892         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1893         printf("%s%s", name, buf);
1894 }
1895
1896 static int
1897 init_mem(unsigned nb_mbuf)
1898 {
1899         int socketid;
1900         unsigned lcore_id;
1901         char s[64];
1902
1903         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1904                 if (rte_lcore_is_enabled(lcore_id) == 0)
1905                         continue;
1906
1907                 if (numa_on)
1908                         socketid = rte_lcore_to_socket_id(lcore_id);
1909                 else
1910                         socketid = 0;
1911
1912                 if (socketid >= NB_SOCKETS) {
1913                         rte_exit(EXIT_FAILURE,
1914                                 "Socket %d of lcore %u is out of range %d\n",
1915                                 socketid, lcore_id, NB_SOCKETS);
1916                 }
1917                 if (pktmbuf_pool[socketid] == NULL) {
1918                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1919                         pktmbuf_pool[socketid] =
1920                                 rte_pktmbuf_pool_create(s, nb_mbuf,
1921                                         MEMPOOL_CACHE_SIZE, 0,
1922                                         RTE_MBUF_DEFAULT_BUF_SIZE,
1923                                         socketid);
1924                         if (pktmbuf_pool[socketid] == NULL)
1925                                 rte_exit(EXIT_FAILURE,
1926                                         "Cannot init mbuf pool on socket %d\n",
1927                                         socketid);
1928                         else
1929                                 printf("Allocated mbuf pool on socket %d\n",
1930                                         socketid);
1931                 }
1932         }
1933         return 0;
1934 }
1935
1936 /* Check the link status of all ports in up to 9s, and print them finally */
1937 static void
1938 check_all_ports_link_status(uint32_t port_mask)
1939 {
1940 #define CHECK_INTERVAL 100 /* 100ms */
1941 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1942         uint16_t portid;
1943         uint8_t count, all_ports_up, print_flag = 0;
1944         struct rte_eth_link link;
1945         int ret;
1946         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1947
1948         printf("\nChecking link status");
1949         fflush(stdout);
1950         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1951                 all_ports_up = 1;
1952                 RTE_ETH_FOREACH_DEV(portid) {
1953                         if ((port_mask & (1 << portid)) == 0)
1954                                 continue;
1955                         memset(&link, 0, sizeof(link));
1956                         ret = rte_eth_link_get_nowait(portid, &link);
1957                         if (ret < 0) {
1958                                 all_ports_up = 0;
1959                                 if (print_flag == 1)
1960                                         printf("Port %u link get failed: %s\n",
1961                                                 portid, rte_strerror(-ret));
1962                                 continue;
1963                         }
1964                         /* print link status if flag set */
1965                         if (print_flag == 1) {
1966                                 rte_eth_link_to_str(link_status_text,
1967                                         sizeof(link_status_text), &link);
1968                                 printf("Port %d %s\n", portid,
1969                                        link_status_text);
1970                                 continue;
1971                         }
1972                         /* clear all_ports_up flag if any link down */
1973                         if (link.link_status == ETH_LINK_DOWN) {
1974                                 all_ports_up = 0;
1975                                 break;
1976                         }
1977                 }
1978                 /* after finally printing all link status, get out */
1979                 if (print_flag == 1)
1980                         break;
1981
1982                 if (all_ports_up == 0) {
1983                         printf(".");
1984                         fflush(stdout);
1985                         rte_delay_ms(CHECK_INTERVAL);
1986                 }
1987
1988                 /* set the print_flag if all ports up or timeout */
1989                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1990                         print_flag = 1;
1991                         printf("done\n");
1992                 }
1993         }
1994 }
1995
1996 /*
1997  * build-up default vaues for dest MACs.
1998  */
1999 static void
2000 set_default_dest_mac(void)
2001 {
2002         uint32_t i;
2003
2004         for (i = 0; i != RTE_DIM(port_l2hdr); i++) {
2005                 port_l2hdr[i].d_addr.addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
2006                 port_l2hdr[i].d_addr.addr_bytes[5] = i;
2007         }
2008 }
2009
2010 int
2011 main(int argc, char **argv)
2012 {
2013         struct lcore_conf *qconf;
2014         struct rte_eth_dev_info dev_info;
2015         struct rte_eth_txconf *txconf;
2016         int ret;
2017         unsigned nb_ports;
2018         uint16_t queueid;
2019         unsigned lcore_id;
2020         uint32_t n_tx_queue, nb_lcores;
2021         uint16_t portid;
2022         uint8_t nb_rx_queue, queue, socketid;
2023
2024         /* init EAL */
2025         ret = rte_eal_init(argc, argv);
2026         if (ret < 0)
2027                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2028         argc -= ret;
2029         argv += ret;
2030
2031         set_default_dest_mac();
2032
2033         /* parse application arguments (after the EAL ones) */
2034         ret = parse_args(argc, argv);
2035         if (ret < 0)
2036                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2037
2038         if (check_lcore_params() < 0)
2039                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2040
2041         ret = init_lcore_rx_queues();
2042         if (ret < 0)
2043                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2044
2045         nb_ports = rte_eth_dev_count_avail();
2046
2047         if (check_port_config() < 0)
2048                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2049
2050         /* Add ACL rules and route entries, build trie */
2051         if (app_acl_init() < 0)
2052                 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
2053
2054         nb_lcores = rte_lcore_count();
2055
2056         /* initialize all ports */
2057         RTE_ETH_FOREACH_DEV(portid) {
2058                 struct rte_eth_conf local_port_conf = port_conf;
2059
2060                 /* skip ports that are not enabled */
2061                 if ((enabled_port_mask & (1 << portid)) == 0) {
2062                         printf("\nSkipping disabled port %d\n", portid);
2063                         continue;
2064                 }
2065
2066                 /* init port */
2067                 printf("Initializing port %d ... ", portid);
2068                 fflush(stdout);
2069
2070                 nb_rx_queue = get_port_n_rx_queues(portid);
2071                 n_tx_queue = nb_lcores;
2072                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2073                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2074                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2075                         nb_rx_queue, (unsigned)n_tx_queue);
2076
2077                 ret = rte_eth_dev_info_get(portid, &dev_info);
2078                 if (ret != 0)
2079                         rte_exit(EXIT_FAILURE,
2080                                 "Error during getting device (port %u) info: %s\n",
2081                                 portid, strerror(-ret));
2082
2083                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2084                         local_port_conf.txmode.offloads |=
2085                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2086
2087                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2088                         dev_info.flow_type_rss_offloads;
2089                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2090                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2091                         printf("Port %u modified RSS hash function based on hardware support,"
2092                                 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2093                                 portid,
2094                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
2095                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2096                 }
2097
2098                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2099                                         (uint16_t)n_tx_queue, &local_port_conf);
2100                 if (ret < 0)
2101                         rte_exit(EXIT_FAILURE,
2102                                 "Cannot configure device: err=%d, port=%d\n",
2103                                 ret, portid);
2104
2105                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2106                                                        &nb_txd);
2107                 if (ret < 0)
2108                         rte_exit(EXIT_FAILURE,
2109                                 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
2110                                 ret, portid);
2111
2112                 ret = rte_eth_macaddr_get(portid, &port_l2hdr[portid].s_addr);
2113                 if (ret < 0)
2114                         rte_exit(EXIT_FAILURE,
2115                                 "rte_eth_macaddr_get: err=%d, port=%d\n",
2116                                 ret, portid);
2117
2118                 print_ethaddr("Dst MAC:", &port_l2hdr[portid].d_addr);
2119                 print_ethaddr(", Src MAC:", &port_l2hdr[portid].s_addr);
2120                 printf(", ");
2121
2122                 /* init memory */
2123                 ret = init_mem(NB_MBUF);
2124                 if (ret < 0)
2125                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
2126
2127                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2128                         if (rte_lcore_is_enabled(lcore_id) == 0)
2129                                 continue;
2130
2131                         /* Initialize TX buffers */
2132                         qconf = &lcore_conf[lcore_id];
2133                         qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2134                                         RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2135                                         rte_eth_dev_socket_id(portid));
2136                         if (qconf->tx_buffer[portid] == NULL)
2137                                 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2138                                                 (unsigned) portid);
2139
2140                         rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2141                 }
2142
2143                 /* init one TX queue per couple (lcore,port) */
2144                 queueid = 0;
2145                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2146                         if (rte_lcore_is_enabled(lcore_id) == 0)
2147                                 continue;
2148
2149                         if (numa_on)
2150                                 socketid = (uint8_t)
2151                                         rte_lcore_to_socket_id(lcore_id);
2152                         else
2153                                 socketid = 0;
2154
2155                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2156                         fflush(stdout);
2157
2158                         ret = rte_eth_dev_info_get(portid, &dev_info);
2159                         if (ret != 0)
2160                                 rte_exit(EXIT_FAILURE,
2161                                         "Error during getting device (port %u) info: %s\n",
2162                                         portid, strerror(-ret));
2163
2164                         txconf = &dev_info.default_txconf;
2165                         txconf->offloads = local_port_conf.txmode.offloads;
2166                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2167                                                      socketid, txconf);
2168                         if (ret < 0)
2169                                 rte_exit(EXIT_FAILURE,
2170                                         "rte_eth_tx_queue_setup: err=%d, "
2171                                         "port=%d\n", ret, portid);
2172
2173                         qconf = &lcore_conf[lcore_id];
2174                         qconf->tx_queue_id[portid] = queueid;
2175                         queueid++;
2176
2177                         qconf->tx_port_id[qconf->n_tx_port] = portid;
2178                         qconf->n_tx_port++;
2179                 }
2180                 printf("\n");
2181         }
2182
2183         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2184                 if (rte_lcore_is_enabled(lcore_id) == 0)
2185                         continue;
2186                 qconf = &lcore_conf[lcore_id];
2187                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2188                 fflush(stdout);
2189                 /* init RX queues */
2190                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2191                         struct rte_eth_rxconf rxq_conf;
2192
2193                         portid = qconf->rx_queue_list[queue].port_id;
2194                         queueid = qconf->rx_queue_list[queue].queue_id;
2195
2196                         if (numa_on)
2197                                 socketid = (uint8_t)
2198                                         rte_lcore_to_socket_id(lcore_id);
2199                         else
2200                                 socketid = 0;
2201
2202                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2203                         fflush(stdout);
2204
2205                         ret = rte_eth_dev_info_get(portid, &dev_info);
2206                         if (ret != 0)
2207                                 rte_exit(EXIT_FAILURE,
2208                                         "Error during getting device (port %u) info: %s\n",
2209                                         portid, strerror(-ret));
2210
2211                         rxq_conf = dev_info.default_rxconf;
2212                         rxq_conf.offloads = port_conf.rxmode.offloads;
2213                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2214                                         socketid, &rxq_conf,
2215                                         pktmbuf_pool[socketid]);
2216                         if (ret < 0)
2217                                 rte_exit(EXIT_FAILURE,
2218                                         "rte_eth_rx_queue_setup: err=%d,"
2219                                         "port=%d\n", ret, portid);
2220                 }
2221         }
2222
2223         printf("\n");
2224
2225         /* start ports */
2226         RTE_ETH_FOREACH_DEV(portid) {
2227                 if ((enabled_port_mask & (1 << portid)) == 0)
2228                         continue;
2229
2230                 /* Start device */
2231                 ret = rte_eth_dev_start(portid);
2232                 if (ret < 0)
2233                         rte_exit(EXIT_FAILURE,
2234                                 "rte_eth_dev_start: err=%d, port=%d\n",
2235                                 ret, portid);
2236
2237                 /*
2238                  * If enabled, put device in promiscuous mode.
2239                  * This allows IO forwarding mode to forward packets
2240                  * to itself through 2 cross-connected  ports of the
2241                  * target machine.
2242                  */
2243                 if (promiscuous_on) {
2244                         ret = rte_eth_promiscuous_enable(portid);
2245                         if (ret != 0)
2246                                 rte_exit(EXIT_FAILURE,
2247                                         "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2248                                         rte_strerror(-ret), portid);
2249                 }
2250         }
2251
2252         check_all_ports_link_status(enabled_port_mask);
2253
2254         /* launch per-lcore init on every lcore */
2255         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
2256         RTE_LCORE_FOREACH_WORKER(lcore_id) {
2257                 if (rte_eal_wait_lcore(lcore_id) < 0)
2258                         return -1;
2259         }
2260
2261         /* clean up the EAL */
2262         rte_eal_cleanup();
2263
2264         return 0;
2265 }