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