4d263f1cfe105f4da564b504f88361b5816b73a0
[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_launch.h>
53 #include <rte_atomic.h>
54 #include <rte_cycles.h>
55 #include <rte_prefetch.h>
56 #include <rte_lcore.h>
57 #include <rte_per_lcore.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_interrupts.h>
60 #include <rte_pci.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_mempool.h>
66 #include <rte_mbuf.h>
67 #include <rte_ip.h>
68 #include <rte_tcp.h>
69 #include <rte_udp.h>
70 #include <rte_string_fns.h>
71 #include <rte_acl.h>
72
73 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
74 #define L3FWDACL_DEBUG
75 #endif
76 #define DO_RFC_1812_CHECKS
77
78 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
79
80 #define MAX_JUMBO_PKT_LEN  9600
81
82 #define MEMPOOL_CACHE_SIZE 256
83
84 /*
85  * This expression is used to calculate the number of mbufs needed
86  * depending on user input, taking into account memory for rx and tx hardware
87  * rings, cache per lcore and mtable per port per lcore.
88  * RTE_MAX is used to ensure that NB_MBUF never goes below a
89  * minimum value of 8192
90  */
91
92 #define NB_MBUF RTE_MAX(\
93         (nb_ports * nb_rx_queue * nb_rxd +      \
94         nb_ports * nb_lcores * MAX_PKT_BURST +  \
95         nb_ports * n_tx_queue * nb_txd +        \
96         nb_lcores * MEMPOOL_CACHE_SIZE),        \
97         (unsigned)8192)
98
99 #define MAX_PKT_BURST 32
100 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
101
102 #define NB_SOCKETS 8
103
104 /* Configure how many packets ahead to prefetch, when reading packets */
105 #define PREFETCH_OFFSET 3
106
107 /*
108  * Configurable number of RX/TX ring descriptors
109  */
110 #define RTE_TEST_RX_DESC_DEFAULT 128
111 #define RTE_TEST_TX_DESC_DEFAULT 512
112 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
113 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
114
115 /* ethernet addresses of ports */
116 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
117
118 /* mask of enabled ports */
119 static uint32_t enabled_port_mask;
120 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
121 static int numa_on = 1; /**< NUMA is enabled by default. */
122
123 struct lcore_rx_queue {
124         uint16_t port_id;
125         uint8_t queue_id;
126 } __rte_cache_aligned;
127
128 #define MAX_RX_QUEUE_PER_LCORE 16
129 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
130 #define MAX_RX_QUEUE_PER_PORT 128
131
132 #define MAX_LCORE_PARAMS 1024
133 struct lcore_params {
134         uint16_t port_id;
135         uint8_t queue_id;
136         uint8_t lcore_id;
137 } __rte_cache_aligned;
138
139 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
140 static struct lcore_params lcore_params_array_default[] = {
141         {0, 0, 2},
142         {0, 1, 2},
143         {0, 2, 2},
144         {1, 0, 2},
145         {1, 1, 2},
146         {1, 2, 2},
147         {2, 0, 2},
148         {3, 0, 3},
149         {3, 1, 3},
150 };
151
152 static struct lcore_params *lcore_params = lcore_params_array_default;
153 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
154                                 sizeof(lcore_params_array_default[0]);
155
156 static struct rte_eth_conf port_conf = {
157         .rxmode = {
158                 .mq_mode        = ETH_MQ_RX_RSS,
159                 .max_rx_pkt_len = ETHER_MAX_LEN,
160                 .split_hdr_size = 0,
161                 .header_split   = 0, /**< Header Split disabled */
162                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
163                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
164                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
165                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
166         },
167         .rx_adv_conf = {
168                 .rss_conf = {
169                         .rss_key = NULL,
170                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
171                                 ETH_RSS_TCP | ETH_RSS_SCTP,
172                 },
173         },
174         .txmode = {
175                 .mq_mode = ETH_MQ_TX_NONE,
176         },
177 };
178
179 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
180
181 /***********************start of ACL part******************************/
182 #ifdef DO_RFC_1812_CHECKS
183 static inline int
184 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len);
185 #endif
186 static inline void
187 send_single_packet(struct rte_mbuf *m, uint16_t port);
188
189 #define MAX_ACL_RULE_NUM        100000
190 #define DEFAULT_MAX_CATEGORIES  1
191 #define L3FWD_ACL_IPV4_NAME     "l3fwd-acl-ipv4"
192 #define L3FWD_ACL_IPV6_NAME     "l3fwd-acl-ipv6"
193 #define ACL_LEAD_CHAR           ('@')
194 #define ROUTE_LEAD_CHAR         ('R')
195 #define COMMENT_LEAD_CHAR       ('#')
196 #define OPTION_CONFIG           "config"
197 #define OPTION_NONUMA           "no-numa"
198 #define OPTION_ENBJMO           "enable-jumbo"
199 #define OPTION_RULE_IPV4        "rule_ipv4"
200 #define OPTION_RULE_IPV6        "rule_ipv6"
201 #define OPTION_SCALAR           "scalar"
202 #define ACL_DENY_SIGNATURE      0xf0000000
203 #define RTE_LOGTYPE_L3FWDACL    RTE_LOGTYPE_USER3
204 #define acl_log(format, ...)    RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
205 #define uint32_t_to_char(ip, a, b, c, d) do {\
206                 *a = (unsigned char)(ip >> 24 & 0xff);\
207                 *b = (unsigned char)(ip >> 16 & 0xff);\
208                 *c = (unsigned char)(ip >> 8 & 0xff);\
209                 *d = (unsigned char)(ip & 0xff);\
210         } while (0)
211 #define OFF_ETHHEAD     (sizeof(struct ether_hdr))
212 #define OFF_IPV42PROTO (offsetof(struct ipv4_hdr, next_proto_id))
213 #define OFF_IPV62PROTO (offsetof(struct ipv6_hdr, proto))
214 #define MBUF_IPV4_2PROTO(m)     \
215         rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
216 #define MBUF_IPV6_2PROTO(m)     \
217         rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
218
219 #define GET_CB_FIELD(in, fd, base, lim, dlm)    do {            \
220         unsigned long val;                                      \
221         char *end;                                              \
222         errno = 0;                                              \
223         val = strtoul((in), &end, (base));                      \
224         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
225                 return -EINVAL;                               \
226         (fd) = (typeof(fd))val;                                 \
227         (in) = end + 1;                                         \
228 } while (0)
229
230 /*
231   * ACL rules should have higher priorities than route ones to ensure ACL rule
232   * always be found when input packets have multi-matches in the database.
233   * A exception case is performance measure, which can define route rules with
234   * higher priority and route rules will always be returned in each lookup.
235   * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
236   * RTE_ACL_MAX_PRIORITY for route entries in performance measure
237   */
238 #define ACL_RULE_PRIORITY_MAX 0x10000000
239
240 /*
241   * Forward port info save in ACL lib starts from 1
242   * since ACL assume 0 is invalid.
243   * So, need add 1 when saving and minus 1 when forwarding packets.
244   */
245 #define FWD_PORT_SHIFT 1
246
247 /*
248  * Rule and trace formats definitions.
249  */
250
251 enum {
252         PROTO_FIELD_IPV4,
253         SRC_FIELD_IPV4,
254         DST_FIELD_IPV4,
255         SRCP_FIELD_IPV4,
256         DSTP_FIELD_IPV4,
257         NUM_FIELDS_IPV4
258 };
259
260 /*
261  * That effectively defines order of IPV4VLAN classifications:
262  *  - PROTO
263  *  - VLAN (TAG and DOMAIN)
264  *  - SRC IP ADDRESS
265  *  - DST IP ADDRESS
266  *  - PORTS (SRC and DST)
267  */
268 enum {
269         RTE_ACL_IPV4VLAN_PROTO,
270         RTE_ACL_IPV4VLAN_VLAN,
271         RTE_ACL_IPV4VLAN_SRC,
272         RTE_ACL_IPV4VLAN_DST,
273         RTE_ACL_IPV4VLAN_PORTS,
274         RTE_ACL_IPV4VLAN_NUM
275 };
276
277 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
278         {
279                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
280                 .size = sizeof(uint8_t),
281                 .field_index = PROTO_FIELD_IPV4,
282                 .input_index = RTE_ACL_IPV4VLAN_PROTO,
283                 .offset = 0,
284         },
285         {
286                 .type = RTE_ACL_FIELD_TYPE_MASK,
287                 .size = sizeof(uint32_t),
288                 .field_index = SRC_FIELD_IPV4,
289                 .input_index = RTE_ACL_IPV4VLAN_SRC,
290                 .offset = offsetof(struct ipv4_hdr, src_addr) -
291                         offsetof(struct ipv4_hdr, next_proto_id),
292         },
293         {
294                 .type = RTE_ACL_FIELD_TYPE_MASK,
295                 .size = sizeof(uint32_t),
296                 .field_index = DST_FIELD_IPV4,
297                 .input_index = RTE_ACL_IPV4VLAN_DST,
298                 .offset = offsetof(struct ipv4_hdr, dst_addr) -
299                         offsetof(struct ipv4_hdr, next_proto_id),
300         },
301         {
302                 .type = RTE_ACL_FIELD_TYPE_RANGE,
303                 .size = sizeof(uint16_t),
304                 .field_index = SRCP_FIELD_IPV4,
305                 .input_index = RTE_ACL_IPV4VLAN_PORTS,
306                 .offset = sizeof(struct ipv4_hdr) -
307                         offsetof(struct ipv4_hdr, next_proto_id),
308         },
309         {
310                 .type = RTE_ACL_FIELD_TYPE_RANGE,
311                 .size = sizeof(uint16_t),
312                 .field_index = DSTP_FIELD_IPV4,
313                 .input_index = RTE_ACL_IPV4VLAN_PORTS,
314                 .offset = sizeof(struct ipv4_hdr) -
315                         offsetof(struct ipv4_hdr, next_proto_id) +
316                         sizeof(uint16_t),
317         },
318 };
319
320 #define IPV6_ADDR_LEN   16
321 #define IPV6_ADDR_U16   (IPV6_ADDR_LEN / sizeof(uint16_t))
322 #define IPV6_ADDR_U32   (IPV6_ADDR_LEN / sizeof(uint32_t))
323
324 enum {
325         PROTO_FIELD_IPV6,
326         SRC1_FIELD_IPV6,
327         SRC2_FIELD_IPV6,
328         SRC3_FIELD_IPV6,
329         SRC4_FIELD_IPV6,
330         DST1_FIELD_IPV6,
331         DST2_FIELD_IPV6,
332         DST3_FIELD_IPV6,
333         DST4_FIELD_IPV6,
334         SRCP_FIELD_IPV6,
335         DSTP_FIELD_IPV6,
336         NUM_FIELDS_IPV6
337 };
338
339 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
340         {
341                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
342                 .size = sizeof(uint8_t),
343                 .field_index = PROTO_FIELD_IPV6,
344                 .input_index = PROTO_FIELD_IPV6,
345                 .offset = 0,
346         },
347         {
348                 .type = RTE_ACL_FIELD_TYPE_MASK,
349                 .size = sizeof(uint32_t),
350                 .field_index = SRC1_FIELD_IPV6,
351                 .input_index = SRC1_FIELD_IPV6,
352                 .offset = offsetof(struct ipv6_hdr, src_addr) -
353                         offsetof(struct ipv6_hdr, proto),
354         },
355         {
356                 .type = RTE_ACL_FIELD_TYPE_MASK,
357                 .size = sizeof(uint32_t),
358                 .field_index = SRC2_FIELD_IPV6,
359                 .input_index = SRC2_FIELD_IPV6,
360                 .offset = offsetof(struct ipv6_hdr, src_addr) -
361                         offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
362         },
363         {
364                 .type = RTE_ACL_FIELD_TYPE_MASK,
365                 .size = sizeof(uint32_t),
366                 .field_index = SRC3_FIELD_IPV6,
367                 .input_index = SRC3_FIELD_IPV6,
368                 .offset = offsetof(struct ipv6_hdr, src_addr) -
369                         offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
370         },
371         {
372                 .type = RTE_ACL_FIELD_TYPE_MASK,
373                 .size = sizeof(uint32_t),
374                 .field_index = SRC4_FIELD_IPV6,
375                 .input_index = SRC4_FIELD_IPV6,
376                 .offset = offsetof(struct ipv6_hdr, src_addr) -
377                         offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
378         },
379         {
380                 .type = RTE_ACL_FIELD_TYPE_MASK,
381                 .size = sizeof(uint32_t),
382                 .field_index = DST1_FIELD_IPV6,
383                 .input_index = DST1_FIELD_IPV6,
384                 .offset = offsetof(struct ipv6_hdr, dst_addr)
385                                 - offsetof(struct ipv6_hdr, proto),
386         },
387         {
388                 .type = RTE_ACL_FIELD_TYPE_MASK,
389                 .size = sizeof(uint32_t),
390                 .field_index = DST2_FIELD_IPV6,
391                 .input_index = DST2_FIELD_IPV6,
392                 .offset = offsetof(struct ipv6_hdr, dst_addr) -
393                         offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
394         },
395         {
396                 .type = RTE_ACL_FIELD_TYPE_MASK,
397                 .size = sizeof(uint32_t),
398                 .field_index = DST3_FIELD_IPV6,
399                 .input_index = DST3_FIELD_IPV6,
400                 .offset = offsetof(struct ipv6_hdr, dst_addr) -
401                         offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
402         },
403         {
404                 .type = RTE_ACL_FIELD_TYPE_MASK,
405                 .size = sizeof(uint32_t),
406                 .field_index = DST4_FIELD_IPV6,
407                 .input_index = DST4_FIELD_IPV6,
408                 .offset = offsetof(struct ipv6_hdr, dst_addr) -
409                         offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
410         },
411         {
412                 .type = RTE_ACL_FIELD_TYPE_RANGE,
413                 .size = sizeof(uint16_t),
414                 .field_index = SRCP_FIELD_IPV6,
415                 .input_index = SRCP_FIELD_IPV6,
416                 .offset = sizeof(struct ipv6_hdr) -
417                         offsetof(struct ipv6_hdr, proto),
418         },
419         {
420                 .type = RTE_ACL_FIELD_TYPE_RANGE,
421                 .size = sizeof(uint16_t),
422                 .field_index = DSTP_FIELD_IPV6,
423                 .input_index = SRCP_FIELD_IPV6,
424                 .offset = sizeof(struct ipv6_hdr) -
425                         offsetof(struct ipv6_hdr, proto) + sizeof(uint16_t),
426         },
427 };
428
429 enum {
430         CB_FLD_SRC_ADDR,
431         CB_FLD_DST_ADDR,
432         CB_FLD_SRC_PORT_LOW,
433         CB_FLD_SRC_PORT_DLM,
434         CB_FLD_SRC_PORT_HIGH,
435         CB_FLD_DST_PORT_LOW,
436         CB_FLD_DST_PORT_DLM,
437         CB_FLD_DST_PORT_HIGH,
438         CB_FLD_PROTO,
439         CB_FLD_USERDATA,
440         CB_FLD_NUM,
441 };
442
443 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
444 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
445
446 struct acl_search_t {
447         const uint8_t *data_ipv4[MAX_PKT_BURST];
448         struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
449         uint32_t res_ipv4[MAX_PKT_BURST];
450         int num_ipv4;
451
452         const uint8_t *data_ipv6[MAX_PKT_BURST];
453         struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
454         uint32_t res_ipv6[MAX_PKT_BURST];
455         int num_ipv6;
456 };
457
458 static struct {
459         char mapped[NB_SOCKETS];
460         struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
461         struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
462 #ifdef L3FWDACL_DEBUG
463         struct acl4_rule *rule_ipv4;
464         struct acl6_rule *rule_ipv6;
465 #endif
466 } acl_config;
467
468 static struct{
469         const char *rule_ipv4_name;
470         const char *rule_ipv6_name;
471         int scalar;
472 } parm_config;
473
474 const char cb_port_delim[] = ":";
475
476 static inline void
477 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
478 {
479         unsigned char a, b, c, d;
480
481         uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
482                         &a, &b, &c, &d);
483         printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
484                         rule->field[SRC_FIELD_IPV4].mask_range.u32);
485         uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
486                         &a, &b, &c, &d);
487         printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
488                         rule->field[DST_FIELD_IPV4].mask_range.u32);
489         printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
490                 rule->field[SRCP_FIELD_IPV4].value.u16,
491                 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
492                 rule->field[DSTP_FIELD_IPV4].value.u16,
493                 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
494                 rule->field[PROTO_FIELD_IPV4].value.u8,
495                 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
496         if (extra)
497                 printf("0x%x-0x%x-0x%x ",
498                         rule->data.category_mask,
499                         rule->data.priority,
500                         rule->data.userdata);
501 }
502
503 static inline void
504 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
505 {
506         unsigned char a, b, c, d;
507
508         uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
509                 &a, &b, &c, &d);
510         printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
511         uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
512                 &a, &b, &c, &d);
513         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
514         uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
515                 &a, &b, &c, &d);
516         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
517         uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
518                 &a, &b, &c, &d);
519         printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
520                         rule->field[SRC1_FIELD_IPV6].mask_range.u32
521                         + rule->field[SRC2_FIELD_IPV6].mask_range.u32
522                         + rule->field[SRC3_FIELD_IPV6].mask_range.u32
523                         + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
524
525         uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
526                 &a, &b, &c, &d);
527         printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
528         uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
529                 &a, &b, &c, &d);
530         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
531         uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
532                 &a, &b, &c, &d);
533         printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
534         uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
535                 &a, &b, &c, &d);
536         printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
537                         rule->field[DST1_FIELD_IPV6].mask_range.u32
538                         + rule->field[DST2_FIELD_IPV6].mask_range.u32
539                         + rule->field[DST3_FIELD_IPV6].mask_range.u32
540                         + rule->field[DST4_FIELD_IPV6].mask_range.u32);
541
542         printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
543                 rule->field[SRCP_FIELD_IPV6].value.u16,
544                 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
545                 rule->field[DSTP_FIELD_IPV6].value.u16,
546                 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
547                 rule->field[PROTO_FIELD_IPV6].value.u8,
548                 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
549         if (extra)
550                 printf("0x%x-0x%x-0x%x ",
551                         rule->data.category_mask,
552                         rule->data.priority,
553                         rule->data.userdata);
554 }
555
556 /* Bypass comment and empty lines */
557 static inline int
558 is_bypass_line(char *buff)
559 {
560         int i = 0;
561
562         /* comment line */
563         if (buff[0] == COMMENT_LEAD_CHAR)
564                 return 1;
565         /* empty line */
566         while (buff[i] != '\0') {
567                 if (!isspace(buff[i]))
568                         return 0;
569                 i++;
570         }
571         return 1;
572 }
573
574 #ifdef L3FWDACL_DEBUG
575 static inline void
576 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
577 {
578         uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
579         unsigned char a, b, c, d;
580         struct ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
581                                                             struct ipv4_hdr *,
582                                                             sizeof(struct ether_hdr));
583
584         uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
585         printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
586         uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
587         printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
588
589         printf("Src port:%hu,Dst port:%hu ",
590                         rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
591                         rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
592         printf("hit ACL %d - ", offset);
593
594         print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
595
596         printf("\n\n");
597 }
598
599 static inline void
600 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
601 {
602         unsigned i;
603         uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
604         struct ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
605                                                             struct ipv6_hdr *,
606                                                             sizeof(struct ether_hdr));
607
608         printf("Packet Src");
609         for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
610                 printf(":%.2x%.2x",
611                         ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
612
613         printf("\nDst");
614         for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
615                 printf(":%.2x%.2x",
616                         ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
617
618         printf("\nSrc port:%hu,Dst port:%hu ",
619                         rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
620                         rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
621         printf("hit ACL %d - ", offset);
622
623         print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
624
625         printf("\n\n");
626 }
627 #endif /* L3FWDACL_DEBUG */
628
629 static inline void
630 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
631 {
632         int i;
633
634         for (i = 0; i < num; i++, rule++) {
635                 printf("\t%d:", i + 1);
636                 print_one_ipv4_rule(rule, extra);
637                 printf("\n");
638         }
639 }
640
641 static inline void
642 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
643 {
644         int i;
645
646         for (i = 0; i < num; i++, rule++) {
647                 printf("\t%d:", i + 1);
648                 print_one_ipv6_rule(rule, extra);
649                 printf("\n");
650         }
651 }
652
653 #ifdef DO_RFC_1812_CHECKS
654 static inline void
655 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
656         int index)
657 {
658         struct ipv4_hdr *ipv4_hdr;
659         struct rte_mbuf *pkt = pkts_in[index];
660
661         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
662                 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
663                                                    sizeof(struct ether_hdr));
664
665                 /* Check to make sure the packet is valid (RFC1812) */
666                 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
667
668                         /* Update time to live and header checksum */
669                         --(ipv4_hdr->time_to_live);
670                         ++(ipv4_hdr->hdr_checksum);
671
672                         /* Fill acl structure */
673                         acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
674                         acl->m_ipv4[(acl->num_ipv4)++] = pkt;
675
676                 } else {
677                         /* Not a valid IPv4 packet */
678                         rte_pktmbuf_free(pkt);
679                 }
680         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
681                 /* Fill acl structure */
682                 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
683                 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
684
685         } else {
686                 /* Unknown type, drop the packet */
687                 rte_pktmbuf_free(pkt);
688         }
689 }
690
691 #else
692 static inline void
693 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
694         int index)
695 {
696         struct rte_mbuf *pkt = pkts_in[index];
697
698         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
699                 /* Fill acl structure */
700                 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
701                 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
702
703         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
704                 /* Fill acl structure */
705                 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
706                 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
707         } else {
708                 /* Unknown type, drop the packet */
709                 rte_pktmbuf_free(pkt);
710         }
711 }
712 #endif /* DO_RFC_1812_CHECKS */
713
714 static inline void
715 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
716         int nb_rx)
717 {
718         int i;
719
720         acl->num_ipv4 = 0;
721         acl->num_ipv6 = 0;
722
723         /* Prefetch first packets */
724         for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
725                 rte_prefetch0(rte_pktmbuf_mtod(
726                                 pkts_in[i], void *));
727         }
728
729         for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
730                 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
731                                 i + PREFETCH_OFFSET], void *));
732                 prepare_one_packet(pkts_in, acl, i);
733         }
734
735         /* Process left packets */
736         for (; i < nb_rx; i++)
737                 prepare_one_packet(pkts_in, acl, i);
738 }
739
740 static inline void
741 send_one_packet(struct rte_mbuf *m, uint32_t res)
742 {
743         if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
744                 /* forward packets */
745                 send_single_packet(m,
746                         (uint8_t)(res - FWD_PORT_SHIFT));
747         } else{
748                 /* in the ACL list, drop it */
749 #ifdef L3FWDACL_DEBUG
750                 if ((res & ACL_DENY_SIGNATURE) != 0) {
751                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
752                                 dump_acl4_rule(m, res);
753                         else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
754                                 dump_acl6_rule(m, res);
755                 }
756 #endif
757                 rte_pktmbuf_free(m);
758         }
759 }
760
761
762
763 static inline void
764 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
765 {
766         int i;
767
768         /* Prefetch first packets */
769         for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
770                 rte_prefetch0(rte_pktmbuf_mtod(
771                                 m[i], void *));
772         }
773
774         for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
775                 rte_prefetch0(rte_pktmbuf_mtod(m[
776                                 i + PREFETCH_OFFSET], void *));
777                 send_one_packet(m[i], res[i]);
778         }
779
780         /* Process left packets */
781         for (; i < num; i++)
782                 send_one_packet(m[i], res[i]);
783 }
784
785 /*
786  * Parses IPV6 address, exepcts the following format:
787  * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
788  */
789 static int
790 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
791         char dlm)
792 {
793         uint32_t addr[IPV6_ADDR_U16];
794
795         GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
796         GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
797         GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
798         GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
799         GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
800         GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
801         GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
802         GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
803
804         *end = in;
805
806         v[0] = (addr[0] << 16) + addr[1];
807         v[1] = (addr[2] << 16) + addr[3];
808         v[2] = (addr[4] << 16) + addr[5];
809         v[3] = (addr[6] << 16) + addr[7];
810
811         return 0;
812 }
813
814 static int
815 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
816 {
817         int32_t rc;
818         const char *mp;
819         uint32_t i, m, v[4];
820         const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
821
822         /* get address. */
823         rc = parse_ipv6_addr(in, &mp, v, '/');
824         if (rc != 0)
825                 return rc;
826
827         /* get mask. */
828         GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
829
830         /* put all together. */
831         for (i = 0; i != RTE_DIM(v); i++) {
832                 if (m >= (i + 1) * nbu32)
833                         field[i].mask_range.u32 = nbu32;
834                 else
835                         field[i].mask_range.u32 = m > (i * nbu32) ?
836                                 m - (i * 32) : 0;
837
838                 field[i].value.u32 = v[i];
839         }
840
841         return 0;
842 }
843
844 static int
845 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
846 {
847         int i, rc;
848         char *s, *sp, *in[CB_FLD_NUM];
849         static const char *dlm = " \t\n";
850         int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
851         s = str;
852
853         for (i = 0; i != dim; i++, s = NULL) {
854                 in[i] = strtok_r(s, dlm, &sp);
855                 if (in[i] == NULL)
856                         return -EINVAL;
857         }
858
859         rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
860         if (rc != 0) {
861                 acl_log("failed to read source address/mask: %s\n",
862                         in[CB_FLD_SRC_ADDR]);
863                 return rc;
864         }
865
866         rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
867         if (rc != 0) {
868                 acl_log("failed to read destination address/mask: %s\n",
869                         in[CB_FLD_DST_ADDR]);
870                 return rc;
871         }
872
873         /* source port. */
874         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
875                 v->field[SRCP_FIELD_IPV6].value.u16,
876                 0, UINT16_MAX, 0);
877         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
878                 v->field[SRCP_FIELD_IPV6].mask_range.u16,
879                 0, UINT16_MAX, 0);
880
881         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
882                         sizeof(cb_port_delim)) != 0)
883                 return -EINVAL;
884
885         /* destination port. */
886         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
887                 v->field[DSTP_FIELD_IPV6].value.u16,
888                 0, UINT16_MAX, 0);
889         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
890                 v->field[DSTP_FIELD_IPV6].mask_range.u16,
891                 0, UINT16_MAX, 0);
892
893         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
894                         sizeof(cb_port_delim)) != 0)
895                 return -EINVAL;
896
897         if (v->field[SRCP_FIELD_IPV6].mask_range.u16
898                         < v->field[SRCP_FIELD_IPV6].value.u16
899                         || v->field[DSTP_FIELD_IPV6].mask_range.u16
900                         < v->field[DSTP_FIELD_IPV6].value.u16)
901                 return -EINVAL;
902
903         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
904                 0, UINT8_MAX, '/');
905         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
906                 0, UINT8_MAX, 0);
907
908         if (has_userdata)
909                 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
910                         0, UINT32_MAX, 0);
911
912         return 0;
913 }
914
915 /*
916  * Parse ClassBench rules file.
917  * Expected format:
918  * '@'<src_ipv4_addr>'/'<masklen> <space> \
919  * <dst_ipv4_addr>'/'<masklen> <space> \
920  * <src_port_low> <space> ":" <src_port_high> <space> \
921  * <dst_port_low> <space> ":" <dst_port_high> <space> \
922  * <proto>'/'<mask>
923  */
924 static int
925 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
926 {
927         uint8_t a, b, c, d, m;
928
929         GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
930         GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
931         GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
932         GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
933         GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
934
935         addr[0] = IPv4(a, b, c, d);
936         mask_len[0] = m;
937
938         return 0;
939 }
940
941 static int
942 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
943 {
944         int i, rc;
945         char *s, *sp, *in[CB_FLD_NUM];
946         static const char *dlm = " \t\n";
947         int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
948         s = str;
949
950         for (i = 0; i != dim; i++, s = NULL) {
951                 in[i] = strtok_r(s, dlm, &sp);
952                 if (in[i] == NULL)
953                         return -EINVAL;
954         }
955
956         rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
957                         &v->field[SRC_FIELD_IPV4].value.u32,
958                         &v->field[SRC_FIELD_IPV4].mask_range.u32);
959         if (rc != 0) {
960                         acl_log("failed to read source address/mask: %s\n",
961                         in[CB_FLD_SRC_ADDR]);
962                 return rc;
963         }
964
965         rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
966                         &v->field[DST_FIELD_IPV4].value.u32,
967                         &v->field[DST_FIELD_IPV4].mask_range.u32);
968         if (rc != 0) {
969                 acl_log("failed to read destination address/mask: %s\n",
970                         in[CB_FLD_DST_ADDR]);
971                 return rc;
972         }
973
974         GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
975                 v->field[SRCP_FIELD_IPV4].value.u16,
976                 0, UINT16_MAX, 0);
977         GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
978                 v->field[SRCP_FIELD_IPV4].mask_range.u16,
979                 0, UINT16_MAX, 0);
980
981         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
982                         sizeof(cb_port_delim)) != 0)
983                 return -EINVAL;
984
985         GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
986                 v->field[DSTP_FIELD_IPV4].value.u16,
987                 0, UINT16_MAX, 0);
988         GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
989                 v->field[DSTP_FIELD_IPV4].mask_range.u16,
990                 0, UINT16_MAX, 0);
991
992         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
993                         sizeof(cb_port_delim)) != 0)
994                 return -EINVAL;
995
996         if (v->field[SRCP_FIELD_IPV4].mask_range.u16
997                         < v->field[SRCP_FIELD_IPV4].value.u16
998                         || v->field[DSTP_FIELD_IPV4].mask_range.u16
999                         < v->field[DSTP_FIELD_IPV4].value.u16)
1000                 return -EINVAL;
1001
1002         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1003                 0, UINT8_MAX, '/');
1004         GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1005                 0, UINT8_MAX, 0);
1006
1007         if (has_userdata)
1008                 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1009                         UINT32_MAX, 0);
1010
1011         return 0;
1012 }
1013
1014 static int
1015 add_rules(const char *rule_path,
1016                 struct rte_acl_rule **proute_base,
1017                 unsigned int *proute_num,
1018                 struct rte_acl_rule **pacl_base,
1019                 unsigned int *pacl_num, uint32_t rule_size,
1020                 int (*parser)(char *, struct rte_acl_rule*, int))
1021 {
1022         uint8_t *acl_rules, *route_rules;
1023         struct rte_acl_rule *next;
1024         unsigned int acl_num = 0, route_num = 0, total_num = 0;
1025         unsigned int acl_cnt = 0, route_cnt = 0;
1026         char buff[LINE_MAX];
1027         FILE *fh = fopen(rule_path, "rb");
1028         unsigned int i = 0;
1029         int val;
1030
1031         if (fh == NULL)
1032                 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1033                         rule_path);
1034
1035         while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1036                 if (buff[0] == ROUTE_LEAD_CHAR)
1037                         route_num++;
1038                 else if (buff[0] == ACL_LEAD_CHAR)
1039                         acl_num++;
1040         }
1041
1042         if (0 == route_num)
1043                 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1044                                 rule_path);
1045
1046         val = fseek(fh, 0, SEEK_SET);
1047         if (val < 0) {
1048                 rte_exit(EXIT_FAILURE, "%s: File seek operation failed\n",
1049                         __func__);
1050         }
1051
1052         acl_rules = calloc(acl_num, rule_size);
1053
1054         if (NULL == acl_rules)
1055                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1056                         __func__);
1057
1058         route_rules = calloc(route_num, rule_size);
1059
1060         if (NULL == route_rules)
1061                 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1062                         __func__);
1063
1064         i = 0;
1065         while (fgets(buff, LINE_MAX, fh) != NULL) {
1066                 i++;
1067
1068                 if (is_bypass_line(buff))
1069                         continue;
1070
1071                 char s = buff[0];
1072
1073                 /* Route entry */
1074                 if (s == ROUTE_LEAD_CHAR)
1075                         next = (struct rte_acl_rule *)(route_rules +
1076                                 route_cnt * rule_size);
1077
1078                 /* ACL entry */
1079                 else if (s == ACL_LEAD_CHAR)
1080                         next = (struct rte_acl_rule *)(acl_rules +
1081                                 acl_cnt * rule_size);
1082
1083                 /* Illegal line */
1084                 else
1085                         rte_exit(EXIT_FAILURE,
1086                                 "%s Line %u: should start with leading "
1087                                 "char %c or %c\n",
1088                                 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1089
1090                 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1091                         rte_exit(EXIT_FAILURE,
1092                                 "%s Line %u: parse rules error\n",
1093                                 rule_path, i);
1094
1095                 if (s == ROUTE_LEAD_CHAR) {
1096                         /* Check the forwarding port number */
1097                         if ((enabled_port_mask & (1 << next->data.userdata)) ==
1098                                         0)
1099                                 rte_exit(EXIT_FAILURE,
1100                                         "%s Line %u: fwd number illegal:%u\n",
1101                                         rule_path, i, next->data.userdata);
1102                         next->data.userdata += FWD_PORT_SHIFT;
1103                         route_cnt++;
1104                 } else {
1105                         next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1106                         acl_cnt++;
1107                 }
1108
1109                 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1110                 next->data.category_mask = -1;
1111                 total_num++;
1112         }
1113
1114         fclose(fh);
1115
1116         *pacl_base = (struct rte_acl_rule *)acl_rules;
1117         *pacl_num = acl_num;
1118         *proute_base = (struct rte_acl_rule *)route_rules;
1119         *proute_num = route_cnt;
1120
1121         return 0;
1122 }
1123
1124 static void
1125 dump_acl_config(void)
1126 {
1127         printf("ACL option are:\n");
1128         printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1129         printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1130         printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1131 }
1132
1133 static int
1134 check_acl_config(void)
1135 {
1136         if (parm_config.rule_ipv4_name == NULL) {
1137                 acl_log("ACL IPv4 rule file not specified\n");
1138                 return -1;
1139         } else if (parm_config.rule_ipv6_name == NULL) {
1140                 acl_log("ACL IPv6 rule file not specified\n");
1141                 return -1;
1142         }
1143
1144         return 0;
1145 }
1146
1147 static struct rte_acl_ctx*
1148 setup_acl(struct rte_acl_rule *route_base,
1149                 struct rte_acl_rule *acl_base, unsigned int route_num,
1150                 unsigned int acl_num, int ipv6, int socketid)
1151 {
1152         char name[PATH_MAX];
1153         struct rte_acl_param acl_param;
1154         struct rte_acl_config acl_build_param;
1155         struct rte_acl_ctx *context;
1156         int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1157
1158         /* Create ACL contexts */
1159         snprintf(name, sizeof(name), "%s%d",
1160                         ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1161                         socketid);
1162
1163         acl_param.name = name;
1164         acl_param.socket_id = socketid;
1165         acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1166         acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1167
1168         if ((context = rte_acl_create(&acl_param)) == NULL)
1169                 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1170
1171         if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1172                         RTE_ACL_CLASSIFY_SCALAR) != 0)
1173                 rte_exit(EXIT_FAILURE,
1174                         "Failed to setup classify method for  ACL context\n");
1175
1176         if (rte_acl_add_rules(context, route_base, route_num) < 0)
1177                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1178
1179         if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1180                         rte_exit(EXIT_FAILURE, "add rules failed\n");
1181
1182         /* Perform builds */
1183         memset(&acl_build_param, 0, sizeof(acl_build_param));
1184
1185         acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1186         acl_build_param.num_fields = dim;
1187         memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1188                 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1189
1190         if (rte_acl_build(context, &acl_build_param) != 0)
1191                 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1192
1193         rte_acl_dump(context);
1194
1195         return context;
1196 }
1197
1198 static int
1199 app_acl_init(void)
1200 {
1201         unsigned lcore_id;
1202         unsigned int i;
1203         int socketid;
1204         struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1205                 *acl_base_ipv6, *route_base_ipv6;
1206         unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1207                 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1208
1209         if (check_acl_config() != 0)
1210                 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1211
1212         dump_acl_config();
1213
1214         /* Load  rules from the input file */
1215         if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1216                         &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1217                         sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1218                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1219
1220         acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1221         dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1222
1223         acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1224         dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1225
1226         if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1227                         &route_num_ipv6,
1228                         &acl_base_ipv6, &acl_num_ipv6,
1229                         sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1230                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1231
1232         acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1233         dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1234
1235         acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1236         dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1237
1238         memset(&acl_config, 0, sizeof(acl_config));
1239
1240         /* Check sockets a context should be created on */
1241         if (!numa_on)
1242                 acl_config.mapped[0] = 1;
1243         else {
1244                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1245                         if (rte_lcore_is_enabled(lcore_id) == 0)
1246                                 continue;
1247
1248                         socketid = rte_lcore_to_socket_id(lcore_id);
1249                         if (socketid >= NB_SOCKETS) {
1250                                 acl_log("Socket %d of lcore %u is out "
1251                                         "of range %d\n",
1252                                         socketid, lcore_id, NB_SOCKETS);
1253                                 free(route_base_ipv4);
1254                                 free(route_base_ipv6);
1255                                 free(acl_base_ipv4);
1256                                 free(acl_base_ipv6);
1257                                 return -1;
1258                         }
1259
1260                         acl_config.mapped[socketid] = 1;
1261                 }
1262         }
1263
1264         for (i = 0; i < NB_SOCKETS; i++) {
1265                 if (acl_config.mapped[i]) {
1266                         acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1267                                 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1268                                 0, i);
1269
1270                         acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1271                                 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1272                                 1, i);
1273                 }
1274         }
1275
1276         free(route_base_ipv4);
1277         free(route_base_ipv6);
1278
1279 #ifdef L3FWDACL_DEBUG
1280         acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1281         acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1282 #else
1283         free(acl_base_ipv4);
1284         free(acl_base_ipv6);
1285 #endif
1286
1287         return 0;
1288 }
1289
1290 /***********************end of ACL part******************************/
1291
1292 struct lcore_conf {
1293         uint16_t n_rx_queue;
1294         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1295         uint16_t n_tx_port;
1296         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1297         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1298         struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1299 } __rte_cache_aligned;
1300
1301 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1302
1303 /* Enqueue a single packet, and send burst if queue is filled */
1304 static inline void
1305 send_single_packet(struct rte_mbuf *m, uint16_t port)
1306 {
1307         uint32_t lcore_id;
1308         struct lcore_conf *qconf;
1309
1310         lcore_id = rte_lcore_id();
1311
1312         qconf = &lcore_conf[lcore_id];
1313         rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1314                         qconf->tx_buffer[port], m);
1315 }
1316
1317 #ifdef DO_RFC_1812_CHECKS
1318 static inline int
1319 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1320 {
1321         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1322         /*
1323          * 1. The packet length reported by the Link Layer must be large
1324          * enough to hold the minimum length legal IP datagram (20 bytes).
1325          */
1326         if (link_len < sizeof(struct ipv4_hdr))
1327                 return -1;
1328
1329         /* 2. The IP checksum must be correct. */
1330         /* this is checked in H/W */
1331
1332         /*
1333          * 3. The IP version number must be 4. If the version number is not 4
1334          * then the packet may be another version of IP, such as IPng or
1335          * ST-II.
1336          */
1337         if (((pkt->version_ihl) >> 4) != 4)
1338                 return -3;
1339         /*
1340          * 4. The IP header length field must be large enough to hold the
1341          * minimum length legal IP datagram (20 bytes = 5 words).
1342          */
1343         if ((pkt->version_ihl & 0xf) < 5)
1344                 return -4;
1345
1346         /*
1347          * 5. The IP total length field must be large enough to hold the IP
1348          * datagram header, whose length is specified in the IP header length
1349          * field.
1350          */
1351         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1352                 return -5;
1353
1354         return 0;
1355 }
1356 #endif
1357
1358 /* main processing loop */
1359 static int
1360 main_loop(__attribute__((unused)) void *dummy)
1361 {
1362         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1363         unsigned lcore_id;
1364         uint64_t prev_tsc, diff_tsc, cur_tsc;
1365         int i, nb_rx;
1366         uint16_t portid;
1367         uint8_t 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=%u 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 uint16_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 = 1; /* 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(uint16_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         uint16_t portid;
1843         uint8_t count, all_ports_up, print_flag = 0;
1844         struct rte_eth_link link;
1845
1846         printf("\nChecking link status");
1847         fflush(stdout);
1848         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1849                 all_ports_up = 1;
1850                 for (portid = 0; portid < port_num; portid++) {
1851                         if ((port_mask & (1 << portid)) == 0)
1852                                 continue;
1853                         memset(&link, 0, sizeof(link));
1854                         rte_eth_link_get_nowait(portid, &link);
1855                         /* print link status if flag set */
1856                         if (print_flag == 1) {
1857                                 if (link.link_status)
1858                                         printf(
1859                                         "Port%d Link Up. Speed %u Mbps %s\n",
1860                                                 portid, link.link_speed,
1861                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1862                                         ("full-duplex") : ("half-duplex\n"));
1863                                 else
1864                                         printf("Port %d Link Down\n", 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         uint16_t portid;
1903         uint8_t nb_rx_queue, queue, socketid;
1904
1905         /* init EAL */
1906         ret = rte_eal_init(argc, argv);
1907         if (ret < 0)
1908                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1909         argc -= ret;
1910         argv += ret;
1911
1912         /* parse application arguments (after the EAL ones) */
1913         ret = parse_args(argc, argv);
1914         if (ret < 0)
1915                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1916
1917         if (check_lcore_params() < 0)
1918                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1919
1920         ret = init_lcore_rx_queues();
1921         if (ret < 0)
1922                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1923
1924         nb_ports = rte_eth_dev_count();
1925
1926         if (check_port_config(nb_ports) < 0)
1927                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1928
1929         /* Add ACL rules and route entries, build trie */
1930         if (app_acl_init() < 0)
1931                 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1932
1933         nb_lcores = rte_lcore_count();
1934
1935         /* initialize all ports */
1936         for (portid = 0; portid < nb_ports; portid++) {
1937                 /* skip ports that are not enabled */
1938                 if ((enabled_port_mask & (1 << portid)) == 0) {
1939                         printf("\nSkipping disabled port %d\n", portid);
1940                         continue;
1941                 }
1942
1943                 /* init port */
1944                 printf("Initializing port %d ... ", portid);
1945                 fflush(stdout);
1946
1947                 nb_rx_queue = get_port_n_rx_queues(portid);
1948                 n_tx_queue = nb_lcores;
1949                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1950                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1951                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1952                         nb_rx_queue, (unsigned)n_tx_queue);
1953                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1954                                         (uint16_t)n_tx_queue, &port_conf);
1955                 if (ret < 0)
1956                         rte_exit(EXIT_FAILURE,
1957                                 "Cannot configure device: err=%d, port=%d\n",
1958                                 ret, portid);
1959
1960                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1961                                                        &nb_txd);
1962                 if (ret < 0)
1963                         rte_exit(EXIT_FAILURE,
1964                                 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
1965                                 ret, portid);
1966
1967                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1968                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1969                 printf(", ");
1970
1971                 /* init memory */
1972                 ret = init_mem(NB_MBUF);
1973                 if (ret < 0)
1974                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1975
1976                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1977                         if (rte_lcore_is_enabled(lcore_id) == 0)
1978                                 continue;
1979
1980                         /* Initialize TX buffers */
1981                         qconf = &lcore_conf[lcore_id];
1982                         qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
1983                                         RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
1984                                         rte_eth_dev_socket_id(portid));
1985                         if (qconf->tx_buffer[portid] == NULL)
1986                                 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
1987                                                 (unsigned) portid);
1988
1989                         rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
1990                 }
1991
1992                 /* init one TX queue per couple (lcore,port) */
1993                 queueid = 0;
1994                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1995                         if (rte_lcore_is_enabled(lcore_id) == 0)
1996                                 continue;
1997
1998                         if (numa_on)
1999                                 socketid = (uint8_t)
2000                                         rte_lcore_to_socket_id(lcore_id);
2001                         else
2002                                 socketid = 0;
2003
2004                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2005                         fflush(stdout);
2006
2007                         rte_eth_dev_info_get(portid, &dev_info);
2008                         txconf = &dev_info.default_txconf;
2009                         if (port_conf.rxmode.jumbo_frame)
2010                                 txconf->txq_flags = 0;
2011                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2012                                                      socketid, txconf);
2013                         if (ret < 0)
2014                                 rte_exit(EXIT_FAILURE,
2015                                         "rte_eth_tx_queue_setup: err=%d, "
2016                                         "port=%d\n", ret, portid);
2017
2018                         qconf = &lcore_conf[lcore_id];
2019                         qconf->tx_queue_id[portid] = queueid;
2020                         queueid++;
2021
2022                         qconf->tx_port_id[qconf->n_tx_port] = portid;
2023                         qconf->n_tx_port++;
2024                 }
2025                 printf("\n");
2026         }
2027
2028         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2029                 if (rte_lcore_is_enabled(lcore_id) == 0)
2030                         continue;
2031                 qconf = &lcore_conf[lcore_id];
2032                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2033                 fflush(stdout);
2034                 /* init RX queues */
2035                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2036                         portid = qconf->rx_queue_list[queue].port_id;
2037                         queueid = qconf->rx_queue_list[queue].queue_id;
2038
2039                         if (numa_on)
2040                                 socketid = (uint8_t)
2041                                         rte_lcore_to_socket_id(lcore_id);
2042                         else
2043                                 socketid = 0;
2044
2045                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2046                         fflush(stdout);
2047
2048                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2049                                         socketid, NULL,
2050                                         pktmbuf_pool[socketid]);
2051                         if (ret < 0)
2052                                 rte_exit(EXIT_FAILURE,
2053                                         "rte_eth_rx_queue_setup: err=%d,"
2054                                         "port=%d\n", ret, portid);
2055                 }
2056         }
2057
2058         printf("\n");
2059
2060         /* start ports */
2061         for (portid = 0; portid < nb_ports; portid++) {
2062                 if ((enabled_port_mask & (1 << portid)) == 0)
2063                         continue;
2064
2065                 /* Start device */
2066                 ret = rte_eth_dev_start(portid);
2067                 if (ret < 0)
2068                         rte_exit(EXIT_FAILURE,
2069                                 "rte_eth_dev_start: err=%d, port=%d\n",
2070                                 ret, portid);
2071
2072                 /*
2073                  * If enabled, put device in promiscuous mode.
2074                  * This allows IO forwarding mode to forward packets
2075                  * to itself through 2 cross-connected  ports of the
2076                  * target machine.
2077                  */
2078                 if (promiscuous_on)
2079                         rte_eth_promiscuous_enable(portid);
2080         }
2081
2082         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2083
2084         /* launch per-lcore init on every lcore */
2085         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2086         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2087                 if (rte_eal_wait_lcore(lcore_id) < 0)
2088                         return -1;
2089         }
2090
2091         return 0;
2092 }