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