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