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