New upstream version 16.11.4
[deb_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   = 1, /**< 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         int val;
1031
1032         if (fh == NULL)
1033                 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1034                         rule_path);
1035
1036         while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1037                 if (buff[0] == ROUTE_LEAD_CHAR)
1038                         route_num++;
1039                 else if (buff[0] == ACL_LEAD_CHAR)
1040                         acl_num++;
1041         }
1042
1043         if (0 == route_num)
1044                 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1045                                 rule_path);
1046
1047         val = fseek(fh, 0, SEEK_SET);
1048         if (val < 0) {
1049                 rte_exit(EXIT_FAILURE, "%s: File seek operation failed\n",
1050                         __func__);
1051         }
1052
1053         acl_rules = calloc(acl_num, rule_size);
1054
1055         if (NULL == acl_rules)
1056                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1057                         __func__);
1058
1059         route_rules = calloc(route_num, rule_size);
1060
1061         if (NULL == route_rules)
1062                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1063                         __func__);
1064
1065         i = 0;
1066         while (fgets(buff, LINE_MAX, fh) != NULL) {
1067                 i++;
1068
1069                 if (is_bypass_line(buff))
1070                         continue;
1071
1072                 char s = buff[0];
1073
1074                 /* Route entry */
1075                 if (s == ROUTE_LEAD_CHAR)
1076                         next = (struct rte_acl_rule *)(route_rules +
1077                                 route_cnt * rule_size);
1078
1079                 /* ACL entry */
1080                 else if (s == ACL_LEAD_CHAR)
1081                         next = (struct rte_acl_rule *)(acl_rules +
1082                                 acl_cnt * rule_size);
1083
1084                 /* Illegal line */
1085                 else
1086                         rte_exit(EXIT_FAILURE,
1087                                 "%s Line %u: should start with leading "
1088                                 "char %c or %c\n",
1089                                 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1090
1091                 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1092                         rte_exit(EXIT_FAILURE,
1093                                 "%s Line %u: parse rules error\n",
1094                                 rule_path, i);
1095
1096                 if (s == ROUTE_LEAD_CHAR) {
1097                         /* Check the forwarding port number */
1098                         if ((enabled_port_mask & (1 << next->data.userdata)) ==
1099                                         0)
1100                                 rte_exit(EXIT_FAILURE,
1101                                         "%s Line %u: fwd number illegal:%u\n",
1102                                         rule_path, i, next->data.userdata);
1103                         next->data.userdata += FWD_PORT_SHIFT;
1104                         route_cnt++;
1105                 } else {
1106                         next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1107                         acl_cnt++;
1108                 }
1109
1110                 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1111                 next->data.category_mask = -1;
1112                 total_num++;
1113         }
1114
1115         fclose(fh);
1116
1117         *pacl_base = (struct rte_acl_rule *)acl_rules;
1118         *pacl_num = acl_num;
1119         *proute_base = (struct rte_acl_rule *)route_rules;
1120         *proute_num = route_cnt;
1121
1122         return 0;
1123 }
1124
1125 static void
1126 dump_acl_config(void)
1127 {
1128         printf("ACL option are:\n");
1129         printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1130         printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1131         printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1132 }
1133
1134 static int
1135 check_acl_config(void)
1136 {
1137         if (parm_config.rule_ipv4_name == NULL) {
1138                 acl_log("ACL IPv4 rule file not specified\n");
1139                 return -1;
1140         } else if (parm_config.rule_ipv6_name == NULL) {
1141                 acl_log("ACL IPv6 rule file not specified\n");
1142                 return -1;
1143         }
1144
1145         return 0;
1146 }
1147
1148 static struct rte_acl_ctx*
1149 setup_acl(struct rte_acl_rule *route_base,
1150                 struct rte_acl_rule *acl_base, unsigned int route_num,
1151                 unsigned int acl_num, int ipv6, int socketid)
1152 {
1153         char name[PATH_MAX];
1154         struct rte_acl_param acl_param;
1155         struct rte_acl_config acl_build_param;
1156         struct rte_acl_ctx *context;
1157         int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1158
1159         /* Create ACL contexts */
1160         snprintf(name, sizeof(name), "%s%d",
1161                         ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1162                         socketid);
1163
1164         acl_param.name = name;
1165         acl_param.socket_id = socketid;
1166         acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1167         acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1168
1169         if ((context = rte_acl_create(&acl_param)) == NULL)
1170                 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1171
1172         if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1173                         RTE_ACL_CLASSIFY_SCALAR) != 0)
1174                 rte_exit(EXIT_FAILURE,
1175                         "Failed to setup classify method for  ACL context\n");
1176
1177         if (rte_acl_add_rules(context, route_base, route_num) < 0)
1178                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1179
1180         if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1181                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1182
1183         /* Perform builds */
1184         memset(&acl_build_param, 0, sizeof(acl_build_param));
1185
1186         acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1187         acl_build_param.num_fields = dim;
1188         memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1189                 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1190
1191         if (rte_acl_build(context, &acl_build_param) != 0)
1192                 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1193
1194         rte_acl_dump(context);
1195
1196         return context;
1197 }
1198
1199 static int
1200 app_acl_init(void)
1201 {
1202         unsigned lcore_id;
1203         unsigned int i;
1204         int socketid;
1205         struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1206                 *acl_base_ipv6, *route_base_ipv6;
1207         unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1208                 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1209
1210         if (check_acl_config() != 0)
1211                 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1212
1213         dump_acl_config();
1214
1215         /* Load  rules from the input file */
1216         if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1217                         &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1218                         sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1219                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1220
1221         acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1222         dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1223
1224         acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1225         dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1226
1227         if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1228                         &route_num_ipv6,
1229                         &acl_base_ipv6, &acl_num_ipv6,
1230                         sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1231                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1232
1233         acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1234         dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1235
1236         acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1237         dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1238
1239         memset(&acl_config, 0, sizeof(acl_config));
1240
1241         /* Check sockets a context should be created on */
1242         if (!numa_on)
1243                 acl_config.mapped[0] = 1;
1244         else {
1245                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1246                         if (rte_lcore_is_enabled(lcore_id) == 0)
1247                                 continue;
1248
1249                         socketid = rte_lcore_to_socket_id(lcore_id);
1250                         if (socketid >= NB_SOCKETS) {
1251                                 acl_log("Socket %d of lcore %u is out "
1252                                         "of range %d\n",
1253                                         socketid, lcore_id, NB_SOCKETS);
1254                                 free(route_base_ipv4);
1255                                 free(route_base_ipv6);
1256                                 free(acl_base_ipv4);
1257                                 free(acl_base_ipv6);
1258                                 return -1;
1259                         }
1260
1261                         acl_config.mapped[socketid] = 1;
1262                 }
1263         }
1264
1265         for (i = 0; i < NB_SOCKETS; i++) {
1266                 if (acl_config.mapped[i]) {
1267                         acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1268                                 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1269                                 0, i);
1270
1271                         acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1272                                 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1273                                 1, i);
1274                 }
1275         }
1276
1277         free(route_base_ipv4);
1278         free(route_base_ipv6);
1279
1280 #ifdef L3FWDACL_DEBUG
1281         acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1282         acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1283 #else
1284         free(acl_base_ipv4);
1285         free(acl_base_ipv6);
1286 #endif
1287
1288         return 0;
1289 }
1290
1291 /***********************end of ACL part******************************/
1292
1293 struct lcore_conf {
1294         uint16_t n_rx_queue;
1295         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1296         uint16_t n_tx_port;
1297         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1298         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1299         struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1300 } __rte_cache_aligned;
1301
1302 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1303
1304 /* Enqueue a single packet, and send burst if queue is filled */
1305 static inline void
1306 send_single_packet(struct rte_mbuf *m, uint8_t port)
1307 {
1308         uint32_t lcore_id;
1309         struct lcore_conf *qconf;
1310
1311         lcore_id = rte_lcore_id();
1312
1313         qconf = &lcore_conf[lcore_id];
1314         rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1315                         qconf->tx_buffer[port], m);
1316 }
1317
1318 #ifdef DO_RFC_1812_CHECKS
1319 static inline int
1320 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1321 {
1322         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1323         /*
1324          * 1. The packet length reported by the Link Layer must be large
1325          * enough to hold the minimum length legal IP datagram (20 bytes).
1326          */
1327         if (link_len < sizeof(struct ipv4_hdr))
1328                 return -1;
1329
1330         /* 2. The IP checksum must be correct. */
1331         /* this is checked in H/W */
1332
1333         /*
1334          * 3. The IP version number must be 4. If the version number is not 4
1335          * then the packet may be another version of IP, such as IPng or
1336          * ST-II.
1337          */
1338         if (((pkt->version_ihl) >> 4) != 4)
1339                 return -3;
1340         /*
1341          * 4. The IP header length field must be large enough to hold the
1342          * minimum length legal IP datagram (20 bytes = 5 words).
1343          */
1344         if ((pkt->version_ihl & 0xf) < 5)
1345                 return -4;
1346
1347         /*
1348          * 5. The IP total length field must be large enough to hold the IP
1349          * datagram header, whose length is specified in the IP header length
1350          * field.
1351          */
1352         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1353                 return -5;
1354
1355         return 0;
1356 }
1357 #endif
1358
1359 /* main processing loop */
1360 static int
1361 main_loop(__attribute__((unused)) void *dummy)
1362 {
1363         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1364         unsigned lcore_id;
1365         uint64_t prev_tsc, diff_tsc, cur_tsc;
1366         int i, nb_rx;
1367         uint8_t portid, queueid;
1368         struct lcore_conf *qconf;
1369         int socketid;
1370         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1371                         / US_PER_S * BURST_TX_DRAIN_US;
1372
1373         prev_tsc = 0;
1374         lcore_id = rte_lcore_id();
1375         qconf = &lcore_conf[lcore_id];
1376         socketid = rte_lcore_to_socket_id(lcore_id);
1377
1378         if (qconf->n_rx_queue == 0) {
1379                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1380                 return 0;
1381         }
1382
1383         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1384
1385         for (i = 0; i < qconf->n_rx_queue; i++) {
1386
1387                 portid = qconf->rx_queue_list[i].port_id;
1388                 queueid = qconf->rx_queue_list[i].queue_id;
1389                 RTE_LOG(INFO, L3FWD,
1390                         " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
1391                         lcore_id, portid, queueid);
1392         }
1393
1394         while (1) {
1395
1396                 cur_tsc = rte_rdtsc();
1397
1398                 /*
1399                  * TX burst queue drain
1400                  */
1401                 diff_tsc = cur_tsc - prev_tsc;
1402                 if (unlikely(diff_tsc > drain_tsc)) {
1403                         for (i = 0; i < qconf->n_tx_port; ++i) {
1404                                 portid = qconf->tx_port_id[i];
1405                                 rte_eth_tx_buffer_flush(portid,
1406                                                 qconf->tx_queue_id[portid],
1407                                                 qconf->tx_buffer[portid]);
1408                         }
1409                         prev_tsc = cur_tsc;
1410                 }
1411
1412                 /*
1413                  * Read packet from RX queues
1414                  */
1415                 for (i = 0; i < qconf->n_rx_queue; ++i) {
1416
1417                         portid = qconf->rx_queue_list[i].port_id;
1418                         queueid = qconf->rx_queue_list[i].queue_id;
1419                         nb_rx = rte_eth_rx_burst(portid, queueid,
1420                                 pkts_burst, MAX_PKT_BURST);
1421
1422                         if (nb_rx > 0) {
1423                                 struct acl_search_t acl_search;
1424
1425                                 prepare_acl_parameter(pkts_burst, &acl_search,
1426                                         nb_rx);
1427
1428                                 if (acl_search.num_ipv4) {
1429                                         rte_acl_classify(
1430                                                 acl_config.acx_ipv4[socketid],
1431                                                 acl_search.data_ipv4,
1432                                                 acl_search.res_ipv4,
1433                                                 acl_search.num_ipv4,
1434                                                 DEFAULT_MAX_CATEGORIES);
1435
1436                                         send_packets(acl_search.m_ipv4,
1437                                                 acl_search.res_ipv4,
1438                                                 acl_search.num_ipv4);
1439                                 }
1440
1441                                 if (acl_search.num_ipv6) {
1442                                         rte_acl_classify(
1443                                                 acl_config.acx_ipv6[socketid],
1444                                                 acl_search.data_ipv6,
1445                                                 acl_search.res_ipv6,
1446                                                 acl_search.num_ipv6,
1447                                                 DEFAULT_MAX_CATEGORIES);
1448
1449                                         send_packets(acl_search.m_ipv6,
1450                                                 acl_search.res_ipv6,
1451                                                 acl_search.num_ipv6);
1452                                 }
1453                         }
1454                 }
1455         }
1456 }
1457
1458 static int
1459 check_lcore_params(void)
1460 {
1461         uint8_t queue, lcore;
1462         uint16_t i;
1463         int socketid;
1464
1465         for (i = 0; i < nb_lcore_params; ++i) {
1466                 queue = lcore_params[i].queue_id;
1467                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1468                         printf("invalid queue number: %hhu\n", queue);
1469                         return -1;
1470                 }
1471                 lcore = lcore_params[i].lcore_id;
1472                 if (!rte_lcore_is_enabled(lcore)) {
1473                         printf("error: lcore %hhu is not enabled in "
1474                                 "lcore mask\n", lcore);
1475                         return -1;
1476                 }
1477                 socketid = rte_lcore_to_socket_id(lcore);
1478                 if (socketid != 0 && numa_on == 0) {
1479                         printf("warning: lcore %hhu is on socket %d "
1480                                 "with numa off\n",
1481                                 lcore, socketid);
1482                 }
1483         }
1484         return 0;
1485 }
1486
1487 static int
1488 check_port_config(const unsigned nb_ports)
1489 {
1490         unsigned portid;
1491         uint16_t i;
1492
1493         for (i = 0; i < nb_lcore_params; ++i) {
1494                 portid = lcore_params[i].port_id;
1495
1496                 if ((enabled_port_mask & (1 << portid)) == 0) {
1497                         printf("port %u is not enabled in port mask\n", portid);
1498                         return -1;
1499                 }
1500                 if (portid >= nb_ports) {
1501                         printf("port %u is not present on the board\n", portid);
1502                         return -1;
1503                 }
1504         }
1505         return 0;
1506 }
1507
1508 static uint8_t
1509 get_port_n_rx_queues(const uint8_t port)
1510 {
1511         int queue = -1;
1512         uint16_t i;
1513
1514         for (i = 0; i < nb_lcore_params; ++i) {
1515                 if (lcore_params[i].port_id == port &&
1516                                 lcore_params[i].queue_id > queue)
1517                         queue = lcore_params[i].queue_id;
1518         }
1519         return (uint8_t)(++queue);
1520 }
1521
1522 static int
1523 init_lcore_rx_queues(void)
1524 {
1525         uint16_t i, nb_rx_queue;
1526         uint8_t lcore;
1527
1528         for (i = 0; i < nb_lcore_params; ++i) {
1529                 lcore = lcore_params[i].lcore_id;
1530                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1531                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1532                         printf("error: too many queues (%u) for lcore: %u\n",
1533                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1534                         return -1;
1535                 } else {
1536                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1537                                 lcore_params[i].port_id;
1538                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1539                                 lcore_params[i].queue_id;
1540                         lcore_conf[lcore].n_rx_queue++;
1541                 }
1542         }
1543         return 0;
1544 }
1545
1546 /* display usage */
1547 static void
1548 print_usage(const char *prgname)
1549 {
1550         printf("%s [EAL options] -- -p PORTMASK -P"
1551                 "--"OPTION_RULE_IPV4"=FILE"
1552                 "--"OPTION_RULE_IPV6"=FILE"
1553                 "  [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1554                 "  [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1555                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1556                 "  -P : enable promiscuous mode\n"
1557                 "  --"OPTION_CONFIG": (port,queue,lcore): "
1558                 "rx queues configuration\n"
1559                 "  --"OPTION_NONUMA": optional, disable numa awareness\n"
1560                 "  --"OPTION_ENBJMO": enable jumbo frame"
1561                 " which max packet len is PKTLEN in decimal (64-9600)\n"
1562                 "  --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1563                 "file. "
1564                 "Each rule occupy one line. "
1565                 "2 kinds of rules are supported. "
1566                 "One is ACL entry at while line leads with character '%c', "
1567                 "another is route entry at while line leads with "
1568                 "character '%c'.\n"
1569                 "  --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
1570                 "entries file.\n"
1571                 "  --"OPTION_SCALAR": Use scalar function to do lookup\n",
1572                 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
1573 }
1574
1575 static int
1576 parse_max_pkt_len(const char *pktlen)
1577 {
1578         char *end = NULL;
1579         unsigned long len;
1580
1581         /* parse decimal string */
1582         len = strtoul(pktlen, &end, 10);
1583         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1584                 return -1;
1585
1586         if (len == 0)
1587                 return -1;
1588
1589         return len;
1590 }
1591
1592 static int
1593 parse_portmask(const char *portmask)
1594 {
1595         char *end = NULL;
1596         unsigned long pm;
1597
1598         /* parse hexadecimal string */
1599         pm = strtoul(portmask, &end, 16);
1600         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1601                 return -1;
1602
1603         if (pm == 0)
1604                 return -1;
1605
1606         return pm;
1607 }
1608
1609 static int
1610 parse_config(const char *q_arg)
1611 {
1612         char s[256];
1613         const char *p, *p0 = q_arg;
1614         char *end;
1615         enum fieldnames {
1616                 FLD_PORT = 0,
1617                 FLD_QUEUE,
1618                 FLD_LCORE,
1619                 _NUM_FLD
1620         };
1621         unsigned long int_fld[_NUM_FLD];
1622         char *str_fld[_NUM_FLD];
1623         int i;
1624         unsigned size;
1625
1626         nb_lcore_params = 0;
1627
1628         while ((p = strchr(p0, '(')) != NULL) {
1629                 ++p;
1630                 if ((p0 = strchr(p, ')')) == NULL)
1631                         return -1;
1632
1633                 size = p0 - p;
1634                 if (size >= sizeof(s))
1635                         return -1;
1636
1637                 snprintf(s, sizeof(s), "%.*s", size, p);
1638                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1639                                 _NUM_FLD)
1640                         return -1;
1641                 for (i = 0; i < _NUM_FLD; i++) {
1642                         errno = 0;
1643                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1644                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1645                                 return -1;
1646                 }
1647                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1648                         printf("exceeded max number of lcore params: %hu\n",
1649                                 nb_lcore_params);
1650                         return -1;
1651                 }
1652                 lcore_params_array[nb_lcore_params].port_id =
1653                         (uint8_t)int_fld[FLD_PORT];
1654                 lcore_params_array[nb_lcore_params].queue_id =
1655                         (uint8_t)int_fld[FLD_QUEUE];
1656                 lcore_params_array[nb_lcore_params].lcore_id =
1657                         (uint8_t)int_fld[FLD_LCORE];
1658                 ++nb_lcore_params;
1659         }
1660         lcore_params = lcore_params_array;
1661         return 0;
1662 }
1663
1664 /* Parse the argument given in the command line of the application */
1665 static int
1666 parse_args(int argc, char **argv)
1667 {
1668         int opt, ret;
1669         char **argvopt;
1670         int option_index;
1671         char *prgname = argv[0];
1672         static struct option lgopts[] = {
1673                 {OPTION_CONFIG, 1, 0, 0},
1674                 {OPTION_NONUMA, 0, 0, 0},
1675                 {OPTION_ENBJMO, 0, 0, 0},
1676                 {OPTION_RULE_IPV4, 1, 0, 0},
1677                 {OPTION_RULE_IPV6, 1, 0, 0},
1678                 {OPTION_SCALAR, 0, 0, 0},
1679                 {NULL, 0, 0, 0}
1680         };
1681
1682         argvopt = argv;
1683
1684         while ((opt = getopt_long(argc, argvopt, "p:P",
1685                                 lgopts, &option_index)) != EOF) {
1686
1687                 switch (opt) {
1688                 /* portmask */
1689                 case 'p':
1690                         enabled_port_mask = parse_portmask(optarg);
1691                         if (enabled_port_mask == 0) {
1692                                 printf("invalid portmask\n");
1693                                 print_usage(prgname);
1694                                 return -1;
1695                         }
1696                         break;
1697                 case 'P':
1698                         printf("Promiscuous mode selected\n");
1699                         promiscuous_on = 1;
1700                         break;
1701
1702                 /* long options */
1703                 case 0:
1704                         if (!strncmp(lgopts[option_index].name,
1705                                         OPTION_CONFIG,
1706                                         sizeof(OPTION_CONFIG))) {
1707                                 ret = parse_config(optarg);
1708                                 if (ret) {
1709                                         printf("invalid config\n");
1710                                         print_usage(prgname);
1711                                         return -1;
1712                                 }
1713                         }
1714
1715                         if (!strncmp(lgopts[option_index].name,
1716                                         OPTION_NONUMA,
1717                                         sizeof(OPTION_NONUMA))) {
1718                                 printf("numa is disabled\n");
1719                                 numa_on = 0;
1720                         }
1721
1722                         if (!strncmp(lgopts[option_index].name,
1723                                         OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
1724                                 struct option lenopts = {
1725                                         "max-pkt-len",
1726                                         required_argument,
1727                                         0,
1728                                         0
1729                                 };
1730
1731                                 printf("jumbo frame is enabled\n");
1732                                 port_conf.rxmode.jumbo_frame = 1;
1733
1734                                 /*
1735                                  * if no max-pkt-len set, then use the
1736                                  * default value ETHER_MAX_LEN
1737                                  */
1738                                 if (0 == getopt_long(argc, argvopt, "",
1739                                                 &lenopts, &option_index)) {
1740                                         ret = parse_max_pkt_len(optarg);
1741                                         if ((ret < 64) ||
1742                                                 (ret > MAX_JUMBO_PKT_LEN)) {
1743                                                 printf("invalid packet "
1744                                                         "length\n");
1745                                                 print_usage(prgname);
1746                                                 return -1;
1747                                         }
1748                                         port_conf.rxmode.max_rx_pkt_len = ret;
1749                                 }
1750                                 printf("set jumbo frame max packet length "
1751                                         "to %u\n",
1752                                         (unsigned int)
1753                                         port_conf.rxmode.max_rx_pkt_len);
1754                         }
1755
1756                         if (!strncmp(lgopts[option_index].name,
1757                                         OPTION_RULE_IPV4,
1758                                         sizeof(OPTION_RULE_IPV4)))
1759                                 parm_config.rule_ipv4_name = optarg;
1760
1761                         if (!strncmp(lgopts[option_index].name,
1762                                         OPTION_RULE_IPV6,
1763                                         sizeof(OPTION_RULE_IPV6))) {
1764                                 parm_config.rule_ipv6_name = optarg;
1765                         }
1766
1767                         if (!strncmp(lgopts[option_index].name,
1768                                         OPTION_SCALAR, sizeof(OPTION_SCALAR)))
1769                                 parm_config.scalar = 1;
1770
1771
1772                         break;
1773
1774                 default:
1775                         print_usage(prgname);
1776                         return -1;
1777                 }
1778         }
1779
1780         if (optind >= 0)
1781                 argv[optind-1] = prgname;
1782
1783         ret = optind-1;
1784         optind = 0; /* reset getopt lib */
1785         return ret;
1786 }
1787
1788 static void
1789 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1790 {
1791         char buf[ETHER_ADDR_FMT_SIZE];
1792         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1793         printf("%s%s", name, buf);
1794 }
1795
1796 static int
1797 init_mem(unsigned nb_mbuf)
1798 {
1799         int socketid;
1800         unsigned lcore_id;
1801         char s[64];
1802
1803         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1804                 if (rte_lcore_is_enabled(lcore_id) == 0)
1805                         continue;
1806
1807                 if (numa_on)
1808                         socketid = rte_lcore_to_socket_id(lcore_id);
1809                 else
1810                         socketid = 0;
1811
1812                 if (socketid >= NB_SOCKETS) {
1813                         rte_exit(EXIT_FAILURE,
1814                                 "Socket %d of lcore %u is out of range %d\n",
1815                                 socketid, lcore_id, NB_SOCKETS);
1816                 }
1817                 if (pktmbuf_pool[socketid] == NULL) {
1818                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1819                         pktmbuf_pool[socketid] =
1820                                 rte_pktmbuf_pool_create(s, nb_mbuf,
1821                                         MEMPOOL_CACHE_SIZE, 0,
1822                                         RTE_MBUF_DEFAULT_BUF_SIZE,
1823                                         socketid);
1824                         if (pktmbuf_pool[socketid] == NULL)
1825                                 rte_exit(EXIT_FAILURE,
1826                                         "Cannot init mbuf pool on socket %d\n",
1827                                         socketid);
1828                         else
1829                                 printf("Allocated mbuf pool on socket %d\n",
1830                                         socketid);
1831                 }
1832         }
1833         return 0;
1834 }
1835
1836 /* Check the link status of all ports in up to 9s, and print them finally */
1837 static void
1838 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1839 {
1840 #define CHECK_INTERVAL 100 /* 100ms */
1841 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1842         uint8_t portid, count, all_ports_up, print_flag = 0;
1843         struct rte_eth_link link;
1844
1845         printf("\nChecking link status");
1846         fflush(stdout);
1847         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1848                 all_ports_up = 1;
1849                 for (portid = 0; portid < port_num; portid++) {
1850                         if ((port_mask & (1 << portid)) == 0)
1851                                 continue;
1852                         memset(&link, 0, sizeof(link));
1853                         rte_eth_link_get_nowait(portid, &link);
1854                         /* print link status if flag set */
1855                         if (print_flag == 1) {
1856                                 if (link.link_status)
1857                                         printf("Port %d Link Up - speed %u "
1858                                                 "Mbps - %s\n", (uint8_t)portid,
1859                                                 (unsigned)link.link_speed,
1860                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1861                                         ("full-duplex") : ("half-duplex\n"));
1862                                 else
1863                                         printf("Port %d Link Down\n",
1864                                                 (uint8_t)portid);
1865                                 continue;
1866                         }
1867                         /* clear all_ports_up flag if any link down */
1868                         if (link.link_status == ETH_LINK_DOWN) {
1869                                 all_ports_up = 0;
1870                                 break;
1871                         }
1872                 }
1873                 /* after finally printing all link status, get out */
1874                 if (print_flag == 1)
1875                         break;
1876
1877                 if (all_ports_up == 0) {
1878                         printf(".");
1879                         fflush(stdout);
1880                         rte_delay_ms(CHECK_INTERVAL);
1881                 }
1882
1883                 /* set the print_flag if all ports up or timeout */
1884                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1885                         print_flag = 1;
1886                         printf("done\n");
1887                 }
1888         }
1889 }
1890
1891 int
1892 main(int argc, char **argv)
1893 {
1894         struct lcore_conf *qconf;
1895         struct rte_eth_dev_info dev_info;
1896         struct rte_eth_txconf *txconf;
1897         int ret;
1898         unsigned nb_ports;
1899         uint16_t queueid;
1900         unsigned lcore_id;
1901         uint32_t n_tx_queue, nb_lcores;
1902         uint8_t portid, nb_rx_queue, queue, socketid;
1903
1904         /* init EAL */
1905         ret = rte_eal_init(argc, argv);
1906         if (ret < 0)
1907                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1908         argc -= ret;
1909         argv += ret;
1910
1911         /* parse application arguments (after the EAL ones) */
1912         ret = parse_args(argc, argv);
1913         if (ret < 0)
1914                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1915
1916         if (check_lcore_params() < 0)
1917                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1918
1919         ret = init_lcore_rx_queues();
1920         if (ret < 0)
1921                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1922
1923         nb_ports = rte_eth_dev_count();
1924
1925         if (check_port_config(nb_ports) < 0)
1926                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1927
1928         /* Add ACL rules and route entries, build trie */
1929         if (app_acl_init() < 0)
1930                 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1931
1932         nb_lcores = rte_lcore_count();
1933
1934         /* initialize all ports */
1935         for (portid = 0; portid < nb_ports; portid++) {
1936                 /* skip ports that are not enabled */
1937                 if ((enabled_port_mask & (1 << portid)) == 0) {
1938                         printf("\nSkipping disabled port %d\n", portid);
1939                         continue;
1940                 }
1941
1942                 /* init port */
1943                 printf("Initializing port %d ... ", portid);
1944                 fflush(stdout);
1945
1946                 nb_rx_queue = get_port_n_rx_queues(portid);
1947                 n_tx_queue = nb_lcores;
1948                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1949                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1950                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1951                         nb_rx_queue, (unsigned)n_tx_queue);
1952                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1953                                         (uint16_t)n_tx_queue, &port_conf);
1954                 if (ret < 0)
1955                         rte_exit(EXIT_FAILURE,
1956                                 "Cannot configure device: err=%d, port=%d\n",
1957                                 ret, portid);
1958
1959                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1960                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1961                 printf(", ");
1962
1963                 /* init memory */
1964                 ret = init_mem(NB_MBUF);
1965                 if (ret < 0)
1966                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1967
1968                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1969                         if (rte_lcore_is_enabled(lcore_id) == 0)
1970                                 continue;
1971
1972                         /* Initialize TX buffers */
1973                         qconf = &lcore_conf[lcore_id];
1974                         qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
1975                                         RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
1976                                         rte_eth_dev_socket_id(portid));
1977                         if (qconf->tx_buffer[portid] == NULL)
1978                                 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
1979                                                 (unsigned) portid);
1980
1981                         rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
1982                 }
1983
1984                 /* init one TX queue per couple (lcore,port) */
1985                 queueid = 0;
1986                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1987                         if (rte_lcore_is_enabled(lcore_id) == 0)
1988                                 continue;
1989
1990                         if (numa_on)
1991                                 socketid = (uint8_t)
1992                                         rte_lcore_to_socket_id(lcore_id);
1993                         else
1994                                 socketid = 0;
1995
1996                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1997                         fflush(stdout);
1998
1999                         rte_eth_dev_info_get(portid, &dev_info);
2000                         txconf = &dev_info.default_txconf;
2001                         if (port_conf.rxmode.jumbo_frame)
2002                                 txconf->txq_flags = 0;
2003                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2004                                                      socketid, txconf);
2005                         if (ret < 0)
2006                                 rte_exit(EXIT_FAILURE,
2007                                         "rte_eth_tx_queue_setup: err=%d, "
2008                                         "port=%d\n", ret, portid);
2009
2010                         qconf = &lcore_conf[lcore_id];
2011                         qconf->tx_queue_id[portid] = queueid;
2012                         queueid++;
2013
2014                         qconf->tx_port_id[qconf->n_tx_port] = portid;
2015                         qconf->n_tx_port++;
2016                 }
2017                 printf("\n");
2018         }
2019
2020         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2021                 if (rte_lcore_is_enabled(lcore_id) == 0)
2022                         continue;
2023                 qconf = &lcore_conf[lcore_id];
2024                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2025                 fflush(stdout);
2026                 /* init RX queues */
2027                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2028                         portid = qconf->rx_queue_list[queue].port_id;
2029                         queueid = qconf->rx_queue_list[queue].queue_id;
2030
2031                         if (numa_on)
2032                                 socketid = (uint8_t)
2033                                         rte_lcore_to_socket_id(lcore_id);
2034                         else
2035                                 socketid = 0;
2036
2037                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2038                         fflush(stdout);
2039
2040                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2041                                         socketid, NULL,
2042                                         pktmbuf_pool[socketid]);
2043                         if (ret < 0)
2044                                 rte_exit(EXIT_FAILURE,
2045                                         "rte_eth_rx_queue_setup: err=%d,"
2046                                         "port=%d\n", ret, portid);
2047                 }
2048         }
2049
2050         printf("\n");
2051
2052         /* start ports */
2053         for (portid = 0; portid < nb_ports; portid++) {
2054                 if ((enabled_port_mask & (1 << portid)) == 0)
2055                         continue;
2056
2057                 /* Start device */
2058                 ret = rte_eth_dev_start(portid);
2059                 if (ret < 0)
2060                         rte_exit(EXIT_FAILURE,
2061                                 "rte_eth_dev_start: err=%d, port=%d\n",
2062                                 ret, portid);
2063
2064                 /*
2065                  * If enabled, put device in promiscuous mode.
2066                  * This allows IO forwarding mode to forward packets
2067                  * to itself through 2 cross-connected  ports of the
2068                  * target machine.
2069                  */
2070                 if (promiscuous_on)
2071                         rte_eth_promiscuous_enable(portid);
2072         }
2073
2074         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2075
2076         /* launch per-lcore init on every lcore */
2077         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2078         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2079                 if (rte_eal_wait_lcore(lcore_id) < 0)
2080                         return -1;
2081         }
2082
2083         return 0;
2084 }