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