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