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