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