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