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