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