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