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