xref: /dpdk/examples/l3fwd/main.c (revision c0d02207)
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 #include <signal.h>
16 #include <stdbool.h>
17 
18 #include <rte_common.h>
19 #include <rte_vect.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_eal.h>
25 #include <rte_launch.h>
26 #include <rte_atomic.h>
27 #include <rte_cycles.h>
28 #include <rte_prefetch.h>
29 #include <rte_lcore.h>
30 #include <rte_per_lcore.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_interrupts.h>
33 #include <rte_random.h>
34 #include <rte_debug.h>
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_mempool.h>
38 #include <rte_mbuf.h>
39 #include <rte_ip.h>
40 #include <rte_tcp.h>
41 #include <rte_udp.h>
42 #include <rte_string_fns.h>
43 #include <rte_cpuflags.h>
44 
45 #include <cmdline_parse.h>
46 #include <cmdline_parse_etheraddr.h>
47 
48 #include "l3fwd.h"
49 
50 /*
51  * Configurable number of RX/TX ring descriptors
52  */
53 #define RTE_TEST_RX_DESC_DEFAULT 1024
54 #define RTE_TEST_TX_DESC_DEFAULT 1024
55 
56 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
57 #define MAX_RX_QUEUE_PER_PORT 128
58 
59 #define MAX_LCORE_PARAMS 1024
60 
61 /* Static global variables used within this file. */
62 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
63 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
64 
65 /**< Ports set in promiscuous mode off by default. */
66 static int promiscuous_on;
67 
68 /* Select Longest-Prefix or Exact match. */
69 static int l3fwd_lpm_on;
70 static int l3fwd_em_on;
71 
72 static int numa_on = 1; /**< NUMA is enabled by default. */
73 static int parse_ptype; /**< Parse packet type using rx callback, and */
74 			/**< disabled by default */
75 
76 /* Global variables. */
77 
78 volatile bool force_quit;
79 
80 /* ethernet addresses of ports */
81 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
82 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
83 
84 xmm_t val_eth[RTE_MAX_ETHPORTS];
85 
86 /* mask of enabled ports */
87 uint32_t enabled_port_mask;
88 
89 /* Used only in exact match mode. */
90 int ipv6; /**< ipv6 is false by default. */
91 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
92 
93 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
94 
95 struct lcore_params {
96 	uint16_t port_id;
97 	uint8_t queue_id;
98 	uint8_t lcore_id;
99 } __rte_cache_aligned;
100 
101 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
102 static struct lcore_params lcore_params_array_default[] = {
103 	{0, 0, 2},
104 	{0, 1, 2},
105 	{0, 2, 2},
106 	{1, 0, 2},
107 	{1, 1, 2},
108 	{1, 2, 2},
109 	{2, 0, 2},
110 	{3, 0, 3},
111 	{3, 1, 3},
112 };
113 
114 static struct lcore_params * lcore_params = lcore_params_array_default;
115 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
116 				sizeof(lcore_params_array_default[0]);
117 
118 static struct rte_eth_conf port_conf = {
119 	.rxmode = {
120 		.mq_mode = ETH_MQ_RX_RSS,
121 		.max_rx_pkt_len = ETHER_MAX_LEN,
122 		.split_hdr_size = 0,
123 		.offloads = (DEV_RX_OFFLOAD_CRC_STRIP |
124 			     DEV_RX_OFFLOAD_CHECKSUM),
125 	},
126 	.rx_adv_conf = {
127 		.rss_conf = {
128 			.rss_key = NULL,
129 			.rss_hf = ETH_RSS_IP,
130 		},
131 	},
132 	.txmode = {
133 		.mq_mode = ETH_MQ_TX_NONE,
134 	},
135 };
136 
137 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
138 
139 struct l3fwd_lkp_mode {
140 	void  (*setup)(int);
141 	int   (*check_ptype)(int);
142 	rte_rx_callback_fn cb_parse_ptype;
143 	int   (*main_loop)(void *);
144 	void* (*get_ipv4_lookup_struct)(int);
145 	void* (*get_ipv6_lookup_struct)(int);
146 };
147 
148 static struct l3fwd_lkp_mode l3fwd_lkp;
149 
150 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
151 	.setup                  = setup_hash,
152 	.check_ptype		= em_check_ptype,
153 	.cb_parse_ptype		= em_cb_parse_ptype,
154 	.main_loop              = em_main_loop,
155 	.get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
156 	.get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
157 };
158 
159 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
160 	.setup                  = setup_lpm,
161 	.check_ptype		= lpm_check_ptype,
162 	.cb_parse_ptype		= lpm_cb_parse_ptype,
163 	.main_loop              = lpm_main_loop,
164 	.get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
165 	.get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
166 };
167 
168 /*
169  * Setup lookup methods for forwarding.
170  * Currently exact-match and longest-prefix-match
171  * are supported ones.
172  */
173 static void
174 setup_l3fwd_lookup_tables(void)
175 {
176 	/* Setup HASH lookup functions. */
177 	if (l3fwd_em_on)
178 		l3fwd_lkp = l3fwd_em_lkp;
179 	/* Setup LPM lookup functions. */
180 	else
181 		l3fwd_lkp = l3fwd_lpm_lkp;
182 }
183 
184 static int
185 check_lcore_params(void)
186 {
187 	uint8_t queue, lcore;
188 	uint16_t i;
189 	int socketid;
190 
191 	for (i = 0; i < nb_lcore_params; ++i) {
192 		queue = lcore_params[i].queue_id;
193 		if (queue >= MAX_RX_QUEUE_PER_PORT) {
194 			printf("invalid queue number: %hhu\n", queue);
195 			return -1;
196 		}
197 		lcore = lcore_params[i].lcore_id;
198 		if (!rte_lcore_is_enabled(lcore)) {
199 			printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
200 			return -1;
201 		}
202 		if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
203 			(numa_on == 0)) {
204 			printf("warning: lcore %hhu is on socket %d with numa off \n",
205 				lcore, socketid);
206 		}
207 	}
208 	return 0;
209 }
210 
211 static int
212 check_port_config(void)
213 {
214 	uint16_t portid;
215 	uint16_t i;
216 
217 	for (i = 0; i < nb_lcore_params; ++i) {
218 		portid = lcore_params[i].port_id;
219 		if ((enabled_port_mask & (1 << portid)) == 0) {
220 			printf("port %u is not enabled in port mask\n", portid);
221 			return -1;
222 		}
223 		if (!rte_eth_dev_is_valid_port(portid)) {
224 			printf("port %u is not present on the board\n", portid);
225 			return -1;
226 		}
227 	}
228 	return 0;
229 }
230 
231 static uint8_t
232 get_port_n_rx_queues(const uint16_t port)
233 {
234 	int queue = -1;
235 	uint16_t i;
236 
237 	for (i = 0; i < nb_lcore_params; ++i) {
238 		if (lcore_params[i].port_id == port) {
239 			if (lcore_params[i].queue_id == queue+1)
240 				queue = lcore_params[i].queue_id;
241 			else
242 				rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
243 						" in sequence and must start with 0\n",
244 						lcore_params[i].port_id);
245 		}
246 	}
247 	return (uint8_t)(++queue);
248 }
249 
250 static int
251 init_lcore_rx_queues(void)
252 {
253 	uint16_t i, nb_rx_queue;
254 	uint8_t lcore;
255 
256 	for (i = 0; i < nb_lcore_params; ++i) {
257 		lcore = lcore_params[i].lcore_id;
258 		nb_rx_queue = lcore_conf[lcore].n_rx_queue;
259 		if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
260 			printf("error: too many queues (%u) for lcore: %u\n",
261 				(unsigned)nb_rx_queue + 1, (unsigned)lcore);
262 			return -1;
263 		} else {
264 			lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
265 				lcore_params[i].port_id;
266 			lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
267 				lcore_params[i].queue_id;
268 			lcore_conf[lcore].n_rx_queue++;
269 		}
270 	}
271 	return 0;
272 }
273 
274 /* display usage */
275 static void
276 print_usage(const char *prgname)
277 {
278 	fprintf(stderr, "%s [EAL options] --"
279 		" -p PORTMASK"
280 		" [-P]"
281 		" [-E]"
282 		" [-L]"
283 		" --config (port,queue,lcore)[,(port,queue,lcore)]"
284 		" [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
285 		" [--enable-jumbo [--max-pkt-len PKTLEN]]"
286 		" [--no-numa]"
287 		" [--hash-entry-num]"
288 		" [--ipv6]"
289 		" [--parse-ptype]\n\n"
290 
291 		"  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
292 		"  -P : Enable promiscuous mode\n"
293 		"  -E : Enable exact match\n"
294 		"  -L : Enable longest prefix match (default)\n"
295 		"  --config (port,queue,lcore): Rx queue configuration\n"
296 		"  --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
297 		"  --enable-jumbo: Enable jumbo frames\n"
298 		"  --max-pkt-len: Under the premise of enabling jumbo,\n"
299 		"                 maximum packet length in decimal (64-9600)\n"
300 		"  --no-numa: Disable numa awareness\n"
301 		"  --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
302 		"  --ipv6: Set if running ipv6 packets\n"
303 		"  --parse-ptype: Set to use software to analyze packet type\n\n",
304 		prgname);
305 }
306 
307 static int
308 parse_max_pkt_len(const char *pktlen)
309 {
310 	char *end = NULL;
311 	unsigned long len;
312 
313 	/* parse decimal string */
314 	len = strtoul(pktlen, &end, 10);
315 	if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
316 		return -1;
317 
318 	if (len == 0)
319 		return -1;
320 
321 	return len;
322 }
323 
324 static int
325 parse_portmask(const char *portmask)
326 {
327 	char *end = NULL;
328 	unsigned long pm;
329 
330 	/* parse hexadecimal string */
331 	pm = strtoul(portmask, &end, 16);
332 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
333 		return -1;
334 
335 	if (pm == 0)
336 		return -1;
337 
338 	return pm;
339 }
340 
341 static int
342 parse_hash_entry_number(const char *hash_entry_num)
343 {
344 	char *end = NULL;
345 	unsigned long hash_en;
346 	/* parse hexadecimal string */
347 	hash_en = strtoul(hash_entry_num, &end, 16);
348 	if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
349 		return -1;
350 
351 	if (hash_en == 0)
352 		return -1;
353 
354 	return hash_en;
355 }
356 
357 static int
358 parse_config(const char *q_arg)
359 {
360 	char s[256];
361 	const char *p, *p0 = q_arg;
362 	char *end;
363 	enum fieldnames {
364 		FLD_PORT = 0,
365 		FLD_QUEUE,
366 		FLD_LCORE,
367 		_NUM_FLD
368 	};
369 	unsigned long int_fld[_NUM_FLD];
370 	char *str_fld[_NUM_FLD];
371 	int i;
372 	unsigned size;
373 
374 	nb_lcore_params = 0;
375 
376 	while ((p = strchr(p0,'(')) != NULL) {
377 		++p;
378 		if((p0 = strchr(p,')')) == NULL)
379 			return -1;
380 
381 		size = p0 - p;
382 		if(size >= sizeof(s))
383 			return -1;
384 
385 		snprintf(s, sizeof(s), "%.*s", size, p);
386 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
387 			return -1;
388 		for (i = 0; i < _NUM_FLD; i++){
389 			errno = 0;
390 			int_fld[i] = strtoul(str_fld[i], &end, 0);
391 			if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
392 				return -1;
393 		}
394 		if (nb_lcore_params >= MAX_LCORE_PARAMS) {
395 			printf("exceeded max number of lcore params: %hu\n",
396 				nb_lcore_params);
397 			return -1;
398 		}
399 		lcore_params_array[nb_lcore_params].port_id =
400 			(uint8_t)int_fld[FLD_PORT];
401 		lcore_params_array[nb_lcore_params].queue_id =
402 			(uint8_t)int_fld[FLD_QUEUE];
403 		lcore_params_array[nb_lcore_params].lcore_id =
404 			(uint8_t)int_fld[FLD_LCORE];
405 		++nb_lcore_params;
406 	}
407 	lcore_params = lcore_params_array;
408 	return 0;
409 }
410 
411 static void
412 parse_eth_dest(const char *optarg)
413 {
414 	uint16_t portid;
415 	char *port_end;
416 	uint8_t c, *dest, peer_addr[6];
417 
418 	errno = 0;
419 	portid = strtoul(optarg, &port_end, 10);
420 	if (errno != 0 || port_end == optarg || *port_end++ != ',')
421 		rte_exit(EXIT_FAILURE,
422 		"Invalid eth-dest: %s", optarg);
423 	if (portid >= RTE_MAX_ETHPORTS)
424 		rte_exit(EXIT_FAILURE,
425 		"eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
426 		portid, RTE_MAX_ETHPORTS);
427 
428 	if (cmdline_parse_etheraddr(NULL, port_end,
429 		&peer_addr, sizeof(peer_addr)) < 0)
430 		rte_exit(EXIT_FAILURE,
431 		"Invalid ethernet address: %s\n",
432 		port_end);
433 	dest = (uint8_t *)&dest_eth_addr[portid];
434 	for (c = 0; c < 6; c++)
435 		dest[c] = peer_addr[c];
436 	*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
437 }
438 
439 #define MAX_JUMBO_PKT_LEN  9600
440 #define MEMPOOL_CACHE_SIZE 256
441 
442 static const char short_options[] =
443 	"p:"  /* portmask */
444 	"P"   /* promiscuous */
445 	"L"   /* enable long prefix match */
446 	"E"   /* enable exact match */
447 	;
448 
449 #define CMD_LINE_OPT_CONFIG "config"
450 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
451 #define CMD_LINE_OPT_NO_NUMA "no-numa"
452 #define CMD_LINE_OPT_IPV6 "ipv6"
453 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
454 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
455 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
456 enum {
457 	/* long options mapped to a short option */
458 
459 	/* first long only option value must be >= 256, so that we won't
460 	 * conflict with short options */
461 	CMD_LINE_OPT_MIN_NUM = 256,
462 	CMD_LINE_OPT_CONFIG_NUM,
463 	CMD_LINE_OPT_ETH_DEST_NUM,
464 	CMD_LINE_OPT_NO_NUMA_NUM,
465 	CMD_LINE_OPT_IPV6_NUM,
466 	CMD_LINE_OPT_ENABLE_JUMBO_NUM,
467 	CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
468 	CMD_LINE_OPT_PARSE_PTYPE_NUM,
469 };
470 
471 static const struct option lgopts[] = {
472 	{CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
473 	{CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
474 	{CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
475 	{CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
476 	{CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
477 	{CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
478 	{CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
479 	{NULL, 0, 0, 0}
480 };
481 
482 /*
483  * This expression is used to calculate the number of mbufs needed
484  * depending on user input, taking  into account memory for rx and
485  * tx hardware rings, cache per lcore and mtable per port per lcore.
486  * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
487  * value of 8192
488  */
489 #define NB_MBUF RTE_MAX(	\
490 	(nb_ports*nb_rx_queue*nb_rxd +		\
491 	nb_ports*nb_lcores*MAX_PKT_BURST +	\
492 	nb_ports*n_tx_queue*nb_txd +		\
493 	nb_lcores*MEMPOOL_CACHE_SIZE),		\
494 	(unsigned)8192)
495 
496 /* Parse the argument given in the command line of the application */
497 static int
498 parse_args(int argc, char **argv)
499 {
500 	int opt, ret;
501 	char **argvopt;
502 	int option_index;
503 	char *prgname = argv[0];
504 
505 	argvopt = argv;
506 
507 	/* Error or normal output strings. */
508 	while ((opt = getopt_long(argc, argvopt, short_options,
509 				lgopts, &option_index)) != EOF) {
510 
511 		switch (opt) {
512 		/* portmask */
513 		case 'p':
514 			enabled_port_mask = parse_portmask(optarg);
515 			if (enabled_port_mask == 0) {
516 				fprintf(stderr, "Invalid portmask\n");
517 				print_usage(prgname);
518 				return -1;
519 			}
520 			break;
521 
522 		case 'P':
523 			promiscuous_on = 1;
524 			break;
525 
526 		case 'E':
527 			l3fwd_em_on = 1;
528 			break;
529 
530 		case 'L':
531 			l3fwd_lpm_on = 1;
532 			break;
533 
534 		/* long options */
535 		case CMD_LINE_OPT_CONFIG_NUM:
536 			ret = parse_config(optarg);
537 			if (ret) {
538 				fprintf(stderr, "Invalid config\n");
539 				print_usage(prgname);
540 				return -1;
541 			}
542 			break;
543 
544 		case CMD_LINE_OPT_ETH_DEST_NUM:
545 			parse_eth_dest(optarg);
546 			break;
547 
548 		case CMD_LINE_OPT_NO_NUMA_NUM:
549 			numa_on = 0;
550 			break;
551 
552 		case CMD_LINE_OPT_IPV6_NUM:
553 			ipv6 = 1;
554 			break;
555 
556 		case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
557 			const struct option lenopts = {
558 				"max-pkt-len", required_argument, 0, 0
559 			};
560 
561 			port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
562 			port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
563 
564 			/*
565 			 * if no max-pkt-len set, use the default
566 			 * value ETHER_MAX_LEN.
567 			 */
568 			if (getopt_long(argc, argvopt, "",
569 					&lenopts, &option_index) == 0) {
570 				ret = parse_max_pkt_len(optarg);
571 				if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
572 					fprintf(stderr,
573 						"invalid maximum packet length\n");
574 					print_usage(prgname);
575 					return -1;
576 				}
577 				port_conf.rxmode.max_rx_pkt_len = ret;
578 			}
579 			break;
580 		}
581 
582 		case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
583 			ret = parse_hash_entry_number(optarg);
584 			if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
585 				hash_entry_number = ret;
586 			} else {
587 				fprintf(stderr, "invalid hash entry number\n");
588 				print_usage(prgname);
589 				return -1;
590 			}
591 			break;
592 
593 		case CMD_LINE_OPT_PARSE_PTYPE_NUM:
594 			printf("soft parse-ptype is enabled\n");
595 			parse_ptype = 1;
596 			break;
597 
598 		default:
599 			print_usage(prgname);
600 			return -1;
601 		}
602 	}
603 
604 	/* If both LPM and EM are selected, return error. */
605 	if (l3fwd_lpm_on && l3fwd_em_on) {
606 		fprintf(stderr, "LPM and EM are mutually exclusive, select only one\n");
607 		return -1;
608 	}
609 
610 	/*
611 	 * Nothing is selected, pick longest-prefix match
612 	 * as default match.
613 	 */
614 	if (!l3fwd_lpm_on && !l3fwd_em_on) {
615 		fprintf(stderr, "LPM or EM none selected, default LPM on\n");
616 		l3fwd_lpm_on = 1;
617 	}
618 
619 	/*
620 	 * ipv6 and hash flags are valid only for
621 	 * exact macth, reset them to default for
622 	 * longest-prefix match.
623 	 */
624 	if (l3fwd_lpm_on) {
625 		ipv6 = 0;
626 		hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
627 	}
628 
629 	if (optind >= 0)
630 		argv[optind-1] = prgname;
631 
632 	ret = optind-1;
633 	optind = 1; /* reset getopt lib */
634 	return ret;
635 }
636 
637 static void
638 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
639 {
640 	char buf[ETHER_ADDR_FMT_SIZE];
641 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
642 	printf("%s%s", name, buf);
643 }
644 
645 static int
646 init_mem(unsigned nb_mbuf)
647 {
648 	struct lcore_conf *qconf;
649 	int socketid;
650 	unsigned lcore_id;
651 	char s[64];
652 
653 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
654 		if (rte_lcore_is_enabled(lcore_id) == 0)
655 			continue;
656 
657 		if (numa_on)
658 			socketid = rte_lcore_to_socket_id(lcore_id);
659 		else
660 			socketid = 0;
661 
662 		if (socketid >= NB_SOCKETS) {
663 			rte_exit(EXIT_FAILURE,
664 				"Socket %d of lcore %u is out of range %d\n",
665 				socketid, lcore_id, NB_SOCKETS);
666 		}
667 
668 		if (pktmbuf_pool[socketid] == NULL) {
669 			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
670 			pktmbuf_pool[socketid] =
671 				rte_pktmbuf_pool_create(s, nb_mbuf,
672 					MEMPOOL_CACHE_SIZE, 0,
673 					RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
674 			if (pktmbuf_pool[socketid] == NULL)
675 				rte_exit(EXIT_FAILURE,
676 					"Cannot init mbuf pool on socket %d\n",
677 					socketid);
678 			else
679 				printf("Allocated mbuf pool on socket %d\n",
680 					socketid);
681 
682 			/* Setup either LPM or EM(f.e Hash).  */
683 			l3fwd_lkp.setup(socketid);
684 		}
685 		qconf = &lcore_conf[lcore_id];
686 		qconf->ipv4_lookup_struct =
687 			l3fwd_lkp.get_ipv4_lookup_struct(socketid);
688 		qconf->ipv6_lookup_struct =
689 			l3fwd_lkp.get_ipv6_lookup_struct(socketid);
690 	}
691 	return 0;
692 }
693 
694 /* Check the link status of all ports in up to 9s, and print them finally */
695 static void
696 check_all_ports_link_status(uint32_t port_mask)
697 {
698 #define CHECK_INTERVAL 100 /* 100ms */
699 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
700 	uint16_t portid;
701 	uint8_t count, all_ports_up, print_flag = 0;
702 	struct rte_eth_link link;
703 
704 	printf("\nChecking link status");
705 	fflush(stdout);
706 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
707 		if (force_quit)
708 			return;
709 		all_ports_up = 1;
710 		RTE_ETH_FOREACH_DEV(portid) {
711 			if (force_quit)
712 				return;
713 			if ((port_mask & (1 << portid)) == 0)
714 				continue;
715 			memset(&link, 0, sizeof(link));
716 			rte_eth_link_get_nowait(portid, &link);
717 			/* print link status if flag set */
718 			if (print_flag == 1) {
719 				if (link.link_status)
720 					printf(
721 					"Port%d Link Up. Speed %u Mbps -%s\n",
722 						portid, link.link_speed,
723 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
724 					("full-duplex") : ("half-duplex\n"));
725 				else
726 					printf("Port %d Link Down\n", portid);
727 				continue;
728 			}
729 			/* clear all_ports_up flag if any link down */
730 			if (link.link_status == ETH_LINK_DOWN) {
731 				all_ports_up = 0;
732 				break;
733 			}
734 		}
735 		/* after finally printing all link status, get out */
736 		if (print_flag == 1)
737 			break;
738 
739 		if (all_ports_up == 0) {
740 			printf(".");
741 			fflush(stdout);
742 			rte_delay_ms(CHECK_INTERVAL);
743 		}
744 
745 		/* set the print_flag if all ports up or timeout */
746 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
747 			print_flag = 1;
748 			printf("done\n");
749 		}
750 	}
751 }
752 
753 static void
754 signal_handler(int signum)
755 {
756 	if (signum == SIGINT || signum == SIGTERM) {
757 		printf("\n\nSignal %d received, preparing to exit...\n",
758 				signum);
759 		force_quit = true;
760 	}
761 }
762 
763 static int
764 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
765 {
766 	if (parse_ptype) {
767 		printf("Port %d: softly parse packet type info\n", portid);
768 		if (rte_eth_add_rx_callback(portid, queueid,
769 					    l3fwd_lkp.cb_parse_ptype,
770 					    NULL))
771 			return 1;
772 
773 		printf("Failed to add rx callback: port=%d\n", portid);
774 		return 0;
775 	}
776 
777 	if (l3fwd_lkp.check_ptype(portid))
778 		return 1;
779 
780 	printf("port %d cannot parse packet type, please add --%s\n",
781 	       portid, CMD_LINE_OPT_PARSE_PTYPE);
782 	return 0;
783 }
784 
785 int
786 main(int argc, char **argv)
787 {
788 	struct lcore_conf *qconf;
789 	struct rte_eth_dev_info dev_info;
790 	struct rte_eth_txconf *txconf;
791 	int ret;
792 	unsigned nb_ports;
793 	uint16_t queueid, portid;
794 	unsigned lcore_id;
795 	uint32_t n_tx_queue, nb_lcores;
796 	uint8_t nb_rx_queue, queue, socketid;
797 
798 	/* init EAL */
799 	ret = rte_eal_init(argc, argv);
800 	if (ret < 0)
801 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
802 	argc -= ret;
803 	argv += ret;
804 
805 	force_quit = false;
806 	signal(SIGINT, signal_handler);
807 	signal(SIGTERM, signal_handler);
808 
809 	/* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
810 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
811 		dest_eth_addr[portid] =
812 			ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
813 		*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
814 	}
815 
816 	/* parse application arguments (after the EAL ones) */
817 	ret = parse_args(argc, argv);
818 	if (ret < 0)
819 		rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
820 
821 	if (check_lcore_params() < 0)
822 		rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
823 
824 	ret = init_lcore_rx_queues();
825 	if (ret < 0)
826 		rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
827 
828 	nb_ports = rte_eth_dev_count_avail();
829 
830 	if (check_port_config() < 0)
831 		rte_exit(EXIT_FAILURE, "check_port_config failed\n");
832 
833 	nb_lcores = rte_lcore_count();
834 
835 	/* Setup function pointers for lookup method. */
836 	setup_l3fwd_lookup_tables();
837 
838 	/* initialize all ports */
839 	RTE_ETH_FOREACH_DEV(portid) {
840 		struct rte_eth_conf local_port_conf = port_conf;
841 
842 		/* skip ports that are not enabled */
843 		if ((enabled_port_mask & (1 << portid)) == 0) {
844 			printf("\nSkipping disabled port %d\n", portid);
845 			continue;
846 		}
847 
848 		/* init port */
849 		printf("Initializing port %d ... ", portid );
850 		fflush(stdout);
851 
852 		nb_rx_queue = get_port_n_rx_queues(portid);
853 		n_tx_queue = nb_lcores;
854 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
855 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
856 		printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
857 			nb_rx_queue, (unsigned)n_tx_queue );
858 
859 		rte_eth_dev_info_get(portid, &dev_info);
860 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
861 			local_port_conf.txmode.offloads |=
862 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
863 
864 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
865 			dev_info.flow_type_rss_offloads;
866 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
867 				port_conf.rx_adv_conf.rss_conf.rss_hf) {
868 			printf("Port %u modified RSS hash function based on hardware support,"
869 				"requested:%#"PRIx64" configured:%#"PRIx64"\n",
870 				portid,
871 				port_conf.rx_adv_conf.rss_conf.rss_hf,
872 				local_port_conf.rx_adv_conf.rss_conf.rss_hf);
873 		}
874 
875 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
876 					(uint16_t)n_tx_queue, &local_port_conf);
877 		if (ret < 0)
878 			rte_exit(EXIT_FAILURE,
879 				"Cannot configure device: err=%d, port=%d\n",
880 				ret, portid);
881 
882 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
883 						       &nb_txd);
884 		if (ret < 0)
885 			rte_exit(EXIT_FAILURE,
886 				 "Cannot adjust number of descriptors: err=%d, "
887 				 "port=%d\n", ret, portid);
888 
889 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
890 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
891 		printf(", ");
892 		print_ethaddr("Destination:",
893 			(const struct ether_addr *)&dest_eth_addr[portid]);
894 		printf(", ");
895 
896 		/*
897 		 * prepare src MACs for each port.
898 		 */
899 		ether_addr_copy(&ports_eth_addr[portid],
900 			(struct ether_addr *)(val_eth + portid) + 1);
901 
902 		/* init memory */
903 		ret = init_mem(NB_MBUF);
904 		if (ret < 0)
905 			rte_exit(EXIT_FAILURE, "init_mem failed\n");
906 
907 		/* init one TX queue per couple (lcore,port) */
908 		queueid = 0;
909 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
910 			if (rte_lcore_is_enabled(lcore_id) == 0)
911 				continue;
912 
913 			if (numa_on)
914 				socketid =
915 				(uint8_t)rte_lcore_to_socket_id(lcore_id);
916 			else
917 				socketid = 0;
918 
919 			printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
920 			fflush(stdout);
921 
922 			txconf = &dev_info.default_txconf;
923 			txconf->offloads = local_port_conf.txmode.offloads;
924 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
925 						     socketid, txconf);
926 			if (ret < 0)
927 				rte_exit(EXIT_FAILURE,
928 					"rte_eth_tx_queue_setup: err=%d, "
929 					"port=%d\n", ret, portid);
930 
931 			qconf = &lcore_conf[lcore_id];
932 			qconf->tx_queue_id[portid] = queueid;
933 			queueid++;
934 
935 			qconf->tx_port_id[qconf->n_tx_port] = portid;
936 			qconf->n_tx_port++;
937 		}
938 		printf("\n");
939 	}
940 
941 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
942 		if (rte_lcore_is_enabled(lcore_id) == 0)
943 			continue;
944 		qconf = &lcore_conf[lcore_id];
945 		printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
946 		fflush(stdout);
947 		/* init RX queues */
948 		for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
949 			struct rte_eth_dev *dev;
950 			struct rte_eth_conf *conf;
951 			struct rte_eth_rxconf rxq_conf;
952 
953 			portid = qconf->rx_queue_list[queue].port_id;
954 			queueid = qconf->rx_queue_list[queue].queue_id;
955 			dev = &rte_eth_devices[portid];
956 			conf = &dev->data->dev_conf;
957 
958 			if (numa_on)
959 				socketid =
960 				(uint8_t)rte_lcore_to_socket_id(lcore_id);
961 			else
962 				socketid = 0;
963 
964 			printf("rxq=%d,%d,%d ", portid, queueid, socketid);
965 			fflush(stdout);
966 
967 			rte_eth_dev_info_get(portid, &dev_info);
968 			rxq_conf = dev_info.default_rxconf;
969 			rxq_conf.offloads = conf->rxmode.offloads;
970 			ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
971 					socketid,
972 					&rxq_conf,
973 					pktmbuf_pool[socketid]);
974 			if (ret < 0)
975 				rte_exit(EXIT_FAILURE,
976 				"rte_eth_rx_queue_setup: err=%d, port=%d\n",
977 				ret, portid);
978 		}
979 	}
980 
981 	printf("\n");
982 
983 	/* start ports */
984 	RTE_ETH_FOREACH_DEV(portid) {
985 		if ((enabled_port_mask & (1 << portid)) == 0) {
986 			continue;
987 		}
988 		/* Start device */
989 		ret = rte_eth_dev_start(portid);
990 		if (ret < 0)
991 			rte_exit(EXIT_FAILURE,
992 				"rte_eth_dev_start: err=%d, port=%d\n",
993 				ret, portid);
994 
995 		/*
996 		 * If enabled, put device in promiscuous mode.
997 		 * This allows IO forwarding mode to forward packets
998 		 * to itself through 2 cross-connected  ports of the
999 		 * target machine.
1000 		 */
1001 		if (promiscuous_on)
1002 			rte_eth_promiscuous_enable(portid);
1003 	}
1004 
1005 	printf("\n");
1006 
1007 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1008 		if (rte_lcore_is_enabled(lcore_id) == 0)
1009 			continue;
1010 		qconf = &lcore_conf[lcore_id];
1011 		for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1012 			portid = qconf->rx_queue_list[queue].port_id;
1013 			queueid = qconf->rx_queue_list[queue].queue_id;
1014 			if (prepare_ptype_parser(portid, queueid) == 0)
1015 				rte_exit(EXIT_FAILURE, "ptype check fails\n");
1016 		}
1017 	}
1018 
1019 
1020 	check_all_ports_link_status(enabled_port_mask);
1021 
1022 	ret = 0;
1023 	/* launch per-lcore init on every lcore */
1024 	rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1025 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1026 		if (rte_eal_wait_lcore(lcore_id) < 0) {
1027 			ret = -1;
1028 			break;
1029 		}
1030 	}
1031 
1032 	/* stop ports */
1033 	RTE_ETH_FOREACH_DEV(portid) {
1034 		if ((enabled_port_mask & (1 << portid)) == 0)
1035 			continue;
1036 		printf("Closing port %d...", portid);
1037 		rte_eth_dev_stop(portid);
1038 		rte_eth_dev_close(portid);
1039 		printf(" Done\n");
1040 	}
1041 	printf("Bye...\n");
1042 
1043 	return ret;
1044 }
1045