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