xref: /dpdk/examples/ip_fragmentation/main.c (revision e1a06e391ba74f9c4d46a6ecef6d8ee084f4229e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 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 <sys/param.h>
11 #include <string.h>
12 #include <sys/queue.h>
13 #include <stdarg.h>
14 #include <errno.h>
15 #include <getopt.h>
16 
17 #include <rte_common.h>
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_eal.h>
23 #include <rte_launch.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_lpm.h>
37 #include <rte_lpm6.h>
38 #include <rte_ip.h>
39 #include <rte_string_fns.h>
40 
41 #include <rte_ip_frag.h>
42 
43 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
44 
45 /* allow max jumbo frame 9.5 KB */
46 #define JUMBO_FRAME_MAX_SIZE	0x2600
47 
48 #define	ROUNDUP_DIV(a, b)	(((a) + (b) - 1) / (b))
49 
50 /*
51  * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
52  * This value includes the size of IPv6 header.
53  */
54 #define	IPV4_MTU_DEFAULT	RTE_ETHER_MTU
55 #define	IPV6_MTU_DEFAULT	RTE_ETHER_MTU
56 
57 /*
58  * The overhead from max frame size to MTU.
59  * We have to consider the max possible overhead.
60  */
61 #define MTU_OVERHEAD	\
62 	(RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + \
63 		2 * sizeof(struct rte_vlan_hdr))
64 
65 /*
66  * Default payload in bytes for the IPv6 packet.
67  */
68 #define	IPV4_DEFAULT_PAYLOAD	(IPV4_MTU_DEFAULT - sizeof(struct rte_ipv4_hdr))
69 #define	IPV6_DEFAULT_PAYLOAD	(IPV6_MTU_DEFAULT - sizeof(struct rte_ipv6_hdr))
70 
71 /*
72  * Max number of fragments per packet expected - defined by config file.
73  */
74 #define	MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
75 
76 #define NB_MBUF   8192
77 
78 #define MAX_PKT_BURST	32
79 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
80 
81 /* Configure how many packets ahead to prefetch, when reading packets */
82 #define PREFETCH_OFFSET	3
83 
84 /*
85  * Configurable number of RX/TX ring descriptors
86  */
87 #define RX_DESC_DEFAULT 1024
88 #define TX_DESC_DEFAULT 1024
89 static uint16_t nb_rxd = RX_DESC_DEFAULT;
90 static uint16_t nb_txd = TX_DESC_DEFAULT;
91 
92 /* ethernet addresses of ports */
93 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
94 
95 #ifndef IPv4_BYTES
96 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
97 #define IPv4_BYTES(addr) \
98 		(uint8_t) (((addr) >> 24) & 0xFF),\
99 		(uint8_t) (((addr) >> 16) & 0xFF),\
100 		(uint8_t) (((addr) >> 8) & 0xFF),\
101 		(uint8_t) ((addr) & 0xFF)
102 #endif
103 
104 #ifndef IPv6_BYTES
105 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
106                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
107 #define IPv6_BYTES(addr) \
108 	addr[0],  addr[1], addr[2],  addr[3], \
109 	addr[4],  addr[5], addr[6],  addr[7], \
110 	addr[8],  addr[9], addr[10], addr[11],\
111 	addr[12], addr[13],addr[14], addr[15]
112 #endif
113 
114 #define IPV6_ADDR_LEN 16
115 
116 /* mask of enabled ports */
117 static int enabled_port_mask = 0;
118 
119 static int rx_queue_per_lcore = 1;
120 
121 #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
122 
123 struct mbuf_table {
124 	uint16_t len;
125 	struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
126 };
127 
128 struct rx_queue {
129 	struct rte_mempool *direct_pool;
130 	struct rte_mempool *indirect_pool;
131 	struct rte_lpm *lpm;
132 	struct rte_lpm6 *lpm6;
133 	uint16_t portid;
134 };
135 
136 #define MAX_RX_QUEUE_PER_LCORE 16
137 #define MAX_TX_QUEUE_PER_PORT 16
138 struct __rte_cache_aligned lcore_queue_conf {
139 	uint16_t n_rx_queue;
140 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
141 	struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
142 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
143 };
144 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
145 
146 static struct rte_eth_conf port_conf = {
147 	.rxmode = {
148 		.mtu = JUMBO_FRAME_MAX_SIZE - RTE_ETHER_HDR_LEN -
149 			RTE_ETHER_CRC_LEN,
150 		.offloads = (RTE_ETH_RX_OFFLOAD_CHECKSUM |
151 			     RTE_ETH_RX_OFFLOAD_SCATTER),
152 	},
153 	.txmode = {
154 		.mq_mode = RTE_ETH_MQ_TX_NONE,
155 		.offloads = (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
156 			     RTE_ETH_TX_OFFLOAD_MULTI_SEGS),
157 	},
158 };
159 
160 /*
161  * IPv4 forwarding table
162  */
163 struct l3fwd_ipv4_route {
164 	uint32_t ip;
165 	uint8_t  depth;
166 	uint8_t  if_out;
167 };
168 
169 /* Default l3fwd_ipv4_route_array table. 8< */
170 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
171 		{RTE_IPV4(100,10,0,0), 16, 0},
172 		{RTE_IPV4(100,20,0,0), 16, 1},
173 		{RTE_IPV4(100,30,0,0), 16, 2},
174 		{RTE_IPV4(100,40,0,0), 16, 3},
175 		{RTE_IPV4(100,50,0,0), 16, 4},
176 		{RTE_IPV4(100,60,0,0), 16, 5},
177 		{RTE_IPV4(100,70,0,0), 16, 6},
178 		{RTE_IPV4(100,80,0,0), 16, 7},
179 };
180 /* >8 End of default l3fwd_ipv4_route_array table */
181 
182 /*
183  * IPv6 forwarding table
184  */
185 
186 struct l3fwd_ipv6_route {
187 	struct rte_ipv6_addr ip;
188 	uint8_t depth;
189 	uint8_t if_out;
190 };
191 
192 /* Default l3fwd_ipv6_route_array table. 8< */
193 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
194 	{RTE_IPV6(0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 0},
195 	{RTE_IPV6(0x0201, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 1},
196 	{RTE_IPV6(0x0301, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 2},
197 	{RTE_IPV6(0x0401, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 3},
198 	{RTE_IPV6(0x0501, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 4},
199 	{RTE_IPV6(0x0601, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 5},
200 	{RTE_IPV6(0x0701, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101), 48, 6}
201 };
202 /* >8 End of default l3fwd_ipv6_route_array table. */
203 
204 #define LPM_MAX_RULES         1024
205 #define LPM6_MAX_RULES         1024
206 #define LPM6_NUMBER_TBL8S (1 << 16)
207 
208 struct rte_lpm6_config lpm6_config = {
209 		.max_rules = LPM6_MAX_RULES,
210 		.number_tbl8s = LPM6_NUMBER_TBL8S,
211 		.flags = 0
212 };
213 
214 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
215 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
216 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
217 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
218 
219 /* Send burst of packets on an output interface */
220 static inline int
221 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint16_t port)
222 {
223 	struct rte_mbuf **m_table;
224 	int ret;
225 	uint16_t queueid;
226 
227 	queueid = qconf->tx_queue_id[port];
228 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
229 
230 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
231 	if (unlikely(ret < n)) {
232 		do {
233 			rte_pktmbuf_free(m_table[ret]);
234 		} while (++ret < n);
235 	}
236 
237 	return 0;
238 }
239 
240 static inline void
241 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
242 		uint8_t queueid, uint16_t port_in)
243 {
244 	struct rx_queue *rxq;
245 	uint32_t i, len, next_hop;
246 	uint16_t port_out, ether_type;
247 	int32_t len2;
248 	uint64_t ol_flags;
249 	const struct rte_ether_hdr *eth;
250 
251 	ol_flags = 0;
252 	rxq = &qconf->rx_queue_list[queueid];
253 
254 	/* by default, send everything back to the source port */
255 	port_out = port_in;
256 
257 	/* save ether type of the incoming packet */
258 	eth = rte_pktmbuf_mtod(m, const struct rte_ether_hdr *);
259 	ether_type = eth->ether_type;
260 
261 	/* Remove the Ethernet header and trailer from the input packet */
262 	rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr));
263 
264 	/* Build transmission burst */
265 	len = qconf->tx_mbufs[port_out].len;
266 
267 	/* if this is an IPv4 packet */
268 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
269 		struct rte_ipv4_hdr *ip_hdr;
270 		uint32_t ip_dst;
271 		/* Read the lookup key (i.e. ip_dst) from the input packet */
272 		ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv4_hdr *);
273 		ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
274 
275 		/* Find destination port */
276 		if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
277 				(enabled_port_mask & 1 << next_hop) != 0) {
278 			port_out = next_hop;
279 
280 			/* Build transmission burst for new port */
281 			len = qconf->tx_mbufs[port_out].len;
282 		}
283 
284 		/* if we don't need to do any fragmentation */
285 		if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
286 			qconf->tx_mbufs[port_out].m_table[len] = m;
287 			len2 = 1;
288 		} else {
289 			len2 = rte_ipv4_fragment_packet(m,
290 				&qconf->tx_mbufs[port_out].m_table[len],
291 				(uint16_t)(MBUF_TABLE_SIZE - len),
292 				IPV4_MTU_DEFAULT,
293 				rxq->direct_pool, rxq->indirect_pool);
294 
295 			/* Free input packet */
296 			rte_pktmbuf_free(m);
297 
298 			/* request HW to regenerate IPv4 cksum */
299 			ol_flags |= (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM);
300 
301 			/* If we fail to fragment the packet */
302 			if (unlikely (len2 < 0))
303 				return;
304 		}
305 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
306 		/* if this is an IPv6 packet */
307 		struct rte_ipv6_hdr *ip_hdr;
308 
309 		/* Read the lookup key (i.e. ip_dst) from the input packet */
310 		ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv6_hdr *);
311 
312 		/* Find destination port */
313 		if (rte_lpm6_lookup(rxq->lpm6, &ip_hdr->dst_addr,
314 						&next_hop) == 0 &&
315 				(enabled_port_mask & 1 << next_hop) != 0) {
316 			port_out = next_hop;
317 
318 			/* Build transmission burst for new port */
319 			len = qconf->tx_mbufs[port_out].len;
320 		}
321 
322 		/* if we don't need to do any fragmentation */
323 		if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
324 			qconf->tx_mbufs[port_out].m_table[len] = m;
325 			len2 = 1;
326 		} else {
327 			len2 = rte_ipv6_fragment_packet(m,
328 				&qconf->tx_mbufs[port_out].m_table[len],
329 				(uint16_t)(MBUF_TABLE_SIZE - len),
330 				IPV6_MTU_DEFAULT,
331 				rxq->direct_pool, rxq->indirect_pool);
332 
333 			/* Free input packet */
334 			rte_pktmbuf_free(m);
335 
336 			/* If we fail to fragment the packet */
337 			if (unlikely (len2 < 0))
338 				return;
339 		}
340 	}
341 	/* else, just forward the packet */
342 	else {
343 		qconf->tx_mbufs[port_out].m_table[len] = m;
344 		len2 = 1;
345 	}
346 
347 	for (i = len; i < len + len2; i ++) {
348 		void *d_addr_bytes;
349 
350 		m = qconf->tx_mbufs[port_out].m_table[i];
351 		struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)
352 			rte_pktmbuf_prepend(m,
353 				(uint16_t)sizeof(struct rte_ether_hdr));
354 		if (eth_hdr == NULL) {
355 			rte_panic("No headroom in mbuf.\n");
356 		}
357 
358 		m->ol_flags |= ol_flags;
359 		m->l2_len = sizeof(struct rte_ether_hdr);
360 
361 		/* 02:00:00:00:00:xx */
362 		d_addr_bytes = &eth_hdr->dst_addr.addr_bytes[0];
363 		*((uint64_t *)d_addr_bytes) = 0x000000000002 +
364 			((uint64_t)port_out << 40);
365 
366 		/* src addr */
367 		rte_ether_addr_copy(&ports_eth_addr[port_out],
368 				&eth_hdr->src_addr);
369 		eth_hdr->ether_type = ether_type;
370 	}
371 
372 	len += len2;
373 
374 	if (likely(len < MAX_PKT_BURST)) {
375 		qconf->tx_mbufs[port_out].len = (uint16_t)len;
376 		return;
377 	}
378 
379 	/* Transmit packets */
380 	send_burst(qconf, (uint16_t)len, port_out);
381 	qconf->tx_mbufs[port_out].len = 0;
382 }
383 
384 /* main processing loop */
385 static int
386 main_loop(__rte_unused void *dummy)
387 {
388 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
389 	unsigned lcore_id;
390 	uint64_t prev_tsc, diff_tsc, cur_tsc;
391 	int i, j, nb_rx;
392 	uint16_t portid;
393 	struct lcore_queue_conf *qconf;
394 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
395 
396 	prev_tsc = 0;
397 
398 	lcore_id = rte_lcore_id();
399 	qconf = &lcore_queue_conf[lcore_id];
400 
401 	if (qconf->n_rx_queue == 0) {
402 		RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
403 		return 0;
404 	}
405 
406 	RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
407 
408 	for (i = 0; i < qconf->n_rx_queue; i++) {
409 
410 		portid = qconf->rx_queue_list[i].portid;
411 		RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
412 				portid);
413 	}
414 
415 	while (1) {
416 
417 		cur_tsc = rte_rdtsc();
418 
419 		/*
420 		 * TX burst queue drain
421 		 */
422 		diff_tsc = cur_tsc - prev_tsc;
423 		if (unlikely(diff_tsc > drain_tsc)) {
424 
425 			/*
426 			 * This could be optimized (use queueid instead of
427 			 * portid), but it is not called so often
428 			 */
429 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
430 				if (qconf->tx_mbufs[portid].len == 0)
431 					continue;
432 				send_burst(&lcore_queue_conf[lcore_id],
433 					   qconf->tx_mbufs[portid].len,
434 					   portid);
435 				qconf->tx_mbufs[portid].len = 0;
436 			}
437 
438 			prev_tsc = cur_tsc;
439 		}
440 
441 		/*
442 		 * Read packet from RX queues
443 		 */
444 		for (i = 0; i < qconf->n_rx_queue; i++) {
445 
446 			portid = qconf->rx_queue_list[i].portid;
447 			nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
448 						 MAX_PKT_BURST);
449 
450 			/* Prefetch first packets */
451 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
452 				rte_prefetch0(rte_pktmbuf_mtod(
453 						pkts_burst[j], void *));
454 			}
455 
456 			/* Prefetch and forward already prefetched packets */
457 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
458 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
459 						j + PREFETCH_OFFSET], void *));
460 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
461 			}
462 
463 			/* Forward remaining prefetched packets */
464 			for (; j < nb_rx; j++) {
465 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
466 			}
467 		}
468 	}
469 }
470 
471 /* display usage */
472 static void
473 print_usage(const char *prgname)
474 {
475 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
476 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
477 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
478 	       prgname);
479 }
480 
481 static int
482 parse_portmask(const char *portmask)
483 {
484 	char *end = NULL;
485 	unsigned long pm;
486 
487 	/* parse hexadecimal string */
488 	pm = strtoul(portmask, &end, 16);
489 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
490 		return -1;
491 
492 	if (pm == 0)
493 		return -1;
494 
495 	return pm;
496 }
497 
498 static int
499 parse_nqueue(const char *q_arg)
500 {
501 	char *end = NULL;
502 	unsigned long n;
503 
504 	/* parse hexadecimal string */
505 	n = strtoul(q_arg, &end, 10);
506 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
507 		return -1;
508 	if (n == 0)
509 		return -1;
510 	if (n >= MAX_RX_QUEUE_PER_LCORE)
511 		return -1;
512 
513 	return n;
514 }
515 
516 /* Parse the argument given in the command line of the application */
517 static int
518 parse_args(int argc, char **argv)
519 {
520 	int opt, ret;
521 	char **argvopt;
522 	int option_index;
523 	char *prgname = argv[0];
524 	static struct option lgopts[] = {
525 		{NULL, 0, 0, 0}
526 	};
527 
528 	argvopt = argv;
529 
530 	while ((opt = getopt_long(argc, argvopt, "p:q:",
531 				  lgopts, &option_index)) != EOF) {
532 
533 		switch (opt) {
534 		/* portmask */
535 		case 'p':
536 			enabled_port_mask = parse_portmask(optarg);
537 			if (enabled_port_mask < 0) {
538 				printf("invalid portmask\n");
539 				print_usage(prgname);
540 				return -1;
541 			}
542 			break;
543 
544 		/* nqueue */
545 		case 'q':
546 			rx_queue_per_lcore = parse_nqueue(optarg);
547 			if (rx_queue_per_lcore < 0) {
548 				printf("invalid queue number\n");
549 				print_usage(prgname);
550 				return -1;
551 			}
552 			break;
553 
554 		/* long options */
555 		case 0:
556 			print_usage(prgname);
557 			return -1;
558 
559 		default:
560 			print_usage(prgname);
561 			return -1;
562 		}
563 	}
564 
565 	if (enabled_port_mask == 0) {
566 		printf("portmask not specified\n");
567 		print_usage(prgname);
568 		return -1;
569 	}
570 
571 	if (optind >= 0)
572 		argv[optind-1] = prgname;
573 
574 	ret = optind-1;
575 	optind = 1; /* reset getopt lib */
576 	return ret;
577 }
578 
579 static void
580 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
581 {
582 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
583 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
584 	printf("%s%s", name, buf);
585 }
586 
587 /* Check the link status of all ports in up to 9s, and print them finally */
588 static void
589 check_all_ports_link_status(uint32_t port_mask)
590 {
591 #define CHECK_INTERVAL 100 /* 100ms */
592 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
593 	uint16_t portid;
594 	uint8_t count, all_ports_up, print_flag = 0;
595 	struct rte_eth_link link;
596 	int ret;
597 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
598 
599 	printf("\nChecking link status");
600 	fflush(stdout);
601 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
602 		all_ports_up = 1;
603 		RTE_ETH_FOREACH_DEV(portid) {
604 			if ((port_mask & (1 << portid)) == 0)
605 				continue;
606 			memset(&link, 0, sizeof(link));
607 			ret = rte_eth_link_get_nowait(portid, &link);
608 			if (ret < 0) {
609 				all_ports_up = 0;
610 				if (print_flag == 1)
611 					printf("Port %u link get failed: %s\n",
612 						portid, rte_strerror(-ret));
613 				continue;
614 			}
615 			/* print link status if flag set */
616 			if (print_flag == 1) {
617 				rte_eth_link_to_str(link_status_text,
618 					sizeof(link_status_text), &link);
619 				printf("Port %d %s\n", portid,
620 				       link_status_text);
621 				continue;
622 			}
623 			/* clear all_ports_up flag if any link down */
624 			if (link.link_status == RTE_ETH_LINK_DOWN) {
625 				all_ports_up = 0;
626 				break;
627 			}
628 		}
629 		/* after finally printing all link status, get out */
630 		if (print_flag == 1)
631 			break;
632 
633 		if (all_ports_up == 0) {
634 			printf(".");
635 			fflush(stdout);
636 			rte_delay_ms(CHECK_INTERVAL);
637 		}
638 
639 		/* set the print_flag if all ports up or timeout */
640 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
641 			print_flag = 1;
642 			printf("\ndone\n");
643 		}
644 	}
645 }
646 
647 /* Check L3 packet type detection capability of the NIC port */
648 static int
649 check_ptype(int portid)
650 {
651 	int i, ret;
652 	int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
653 	uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
654 
655 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
656 	if (ret <= 0)
657 		return 0;
658 
659 	uint32_t ptypes[ret];
660 
661 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
662 	for (i = 0; i < ret; ++i) {
663 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
664 			ptype_l3_ipv4 = 1;
665 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
666 			ptype_l3_ipv6 = 1;
667 	}
668 
669 	if (ptype_l3_ipv4 == 0)
670 		printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
671 
672 	if (ptype_l3_ipv6 == 0)
673 		printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
674 
675 	if (ptype_l3_ipv4 && ptype_l3_ipv6)
676 		return 1;
677 
678 	return 0;
679 
680 }
681 
682 /* Parse packet type of a packet by SW */
683 static inline void
684 parse_ptype(struct rte_mbuf *m)
685 {
686 	struct rte_ether_hdr *eth_hdr;
687 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
688 	uint16_t ether_type;
689 
690 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
691 	ether_type = eth_hdr->ether_type;
692 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
693 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
694 	else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
695 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
696 
697 	m->packet_type = packet_type;
698 }
699 
700 /* callback function to detect packet type for a queue of a port */
701 static uint16_t
702 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
703 		   struct rte_mbuf *pkts[], uint16_t nb_pkts,
704 		   uint16_t max_pkts __rte_unused,
705 		   void *user_param __rte_unused)
706 {
707 	uint16_t i;
708 
709 	for (i = 0; i < nb_pkts; ++i)
710 		parse_ptype(pkts[i]);
711 
712 	return nb_pkts;
713 }
714 
715 static int
716 init_routing_table(void)
717 {
718 	struct rte_lpm *lpm;
719 	struct rte_lpm6 *lpm6;
720 	int socket, ret;
721 	unsigned i;
722 
723 	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
724 		if (socket_lpm[socket]) {
725 			lpm = socket_lpm[socket];
726 			/* populate the LPM table */
727 			for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
728 				ret = rte_lpm_add(lpm,
729 					l3fwd_ipv4_route_array[i].ip,
730 					l3fwd_ipv4_route_array[i].depth,
731 					l3fwd_ipv4_route_array[i].if_out);
732 
733 				if (ret < 0) {
734 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
735 						"LPM table\n", i);
736 					return -1;
737 				}
738 
739 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
740 						"/%d (port %d)\n",
741 					socket,
742 					IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
743 					l3fwd_ipv4_route_array[i].depth,
744 					l3fwd_ipv4_route_array[i].if_out);
745 			}
746 		}
747 
748 		if (socket_lpm6[socket]) {
749 			lpm6 = socket_lpm6[socket];
750 			/* populate the LPM6 table */
751 			for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
752 				ret = rte_lpm6_add(lpm6,
753 					&l3fwd_ipv6_route_array[i].ip,
754 					l3fwd_ipv6_route_array[i].depth,
755 					l3fwd_ipv6_route_array[i].if_out);
756 
757 				if (ret < 0) {
758 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
759 						"LPM6 table\n", i);
760 					return -1;
761 				}
762 
763 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
764 						"/%d (port %d)\n",
765 					socket,
766 					IPv6_BYTES(l3fwd_ipv6_route_array[i].ip.a),
767 					l3fwd_ipv6_route_array[i].depth,
768 					l3fwd_ipv6_route_array[i].if_out);
769 			}
770 		}
771 	}
772 	return 0;
773 }
774 
775 static int
776 init_mem(void)
777 {
778 	char buf[PATH_MAX];
779 	struct rte_mempool *mp;
780 	struct rte_lpm *lpm;
781 	struct rte_lpm6 *lpm6;
782 	struct rte_lpm_config lpm_config;
783 	int socket;
784 	unsigned lcore_id;
785 
786 	/* traverse through lcores and initialize structures on each socket */
787 
788 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
789 
790 		if (rte_lcore_is_enabled(lcore_id) == 0)
791 			continue;
792 
793 		socket = rte_lcore_to_socket_id(lcore_id);
794 
795 		if (socket == SOCKET_ID_ANY)
796 			socket = 0;
797 
798 		if (socket_direct_pool[socket] == NULL) {
799 			RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
800 					socket);
801 			snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
802 
803 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
804 				0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
805 			if (mp == NULL) {
806 				RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
807 				return -1;
808 			}
809 			socket_direct_pool[socket] = mp;
810 		}
811 
812 		if (socket_indirect_pool[socket] == NULL) {
813 			RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
814 					socket);
815 			snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
816 
817 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
818 				socket);
819 			if (mp == NULL) {
820 				RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
821 				return -1;
822 			}
823 			socket_indirect_pool[socket] = mp;
824 		}
825 
826 		if (socket_lpm[socket] == NULL) {
827 			RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
828 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
829 
830 			lpm_config.max_rules = LPM_MAX_RULES;
831 			lpm_config.number_tbl8s = 256;
832 			lpm_config.flags = 0;
833 
834 			lpm = rte_lpm_create(buf, socket, &lpm_config);
835 			if (lpm == NULL) {
836 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
837 				return -1;
838 			}
839 			socket_lpm[socket] = lpm;
840 		}
841 
842 		if (socket_lpm6[socket] == NULL) {
843 			RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
844 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
845 
846 			lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
847 			if (lpm6 == NULL) {
848 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
849 				return -1;
850 			}
851 			socket_lpm6[socket] = lpm6;
852 		}
853 	}
854 
855 	return 0;
856 }
857 
858 int
859 main(int argc, char **argv)
860 {
861 	struct lcore_queue_conf *qconf;
862 	struct rte_eth_dev_info dev_info;
863 	struct rte_eth_txconf *txconf;
864 	struct rx_queue *rxq;
865 	int socket, ret;
866 	uint16_t nb_ports;
867 	uint16_t queueid = 0;
868 	unsigned lcore_id = 0, rx_lcore_id = 0;
869 	uint32_t n_tx_queue, nb_lcores;
870 	uint16_t portid;
871 
872 	/* init EAL */
873 	ret = rte_eal_init(argc, argv);
874 	if (ret < 0)
875 		rte_exit(EXIT_FAILURE, "rte_eal_init failed");
876 	argc -= ret;
877 	argv += ret;
878 
879 	/* parse application arguments (after the EAL ones) */
880 	ret = parse_args(argc, argv);
881 	if (ret < 0)
882 		rte_exit(EXIT_FAILURE, "Invalid arguments");
883 
884 	nb_ports = rte_eth_dev_count_avail();
885 	if (nb_ports == 0)
886 		rte_exit(EXIT_FAILURE, "No ports found!\n");
887 
888 	nb_lcores = rte_lcore_count();
889 
890 	/* initialize structures (mempools, lpm etc.) */
891 	if (init_mem() < 0)
892 		rte_panic("Cannot initialize memory structures!\n");
893 
894 	/* check if portmask has non-existent ports */
895 	if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
896 		rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
897 
898 	/* initialize all ports */
899 	RTE_ETH_FOREACH_DEV(portid) {
900 		struct rte_eth_conf local_port_conf = port_conf;
901 		struct rte_eth_rxconf rxq_conf;
902 
903 		/* skip ports that are not enabled */
904 		if ((enabled_port_mask & (1 << portid)) == 0) {
905 			printf("Skipping disabled port %d\n", portid);
906 			continue;
907 		}
908 
909 		qconf = &lcore_queue_conf[rx_lcore_id];
910 
911 		/* limit the frame size to the maximum supported by NIC */
912 		ret = rte_eth_dev_info_get(portid, &dev_info);
913 		if (ret != 0)
914 			rte_exit(EXIT_FAILURE,
915 				"Error during getting device (port %u) info: %s\n",
916 				portid, strerror(-ret));
917 
918 		local_port_conf.rxmode.mtu = RTE_MIN(
919 		    dev_info.max_mtu,
920 		    local_port_conf.rxmode.mtu);
921 
922 		/* get the lcore_id for this port */
923 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
924 		       qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
925 
926 			rx_lcore_id ++;
927 			if (rx_lcore_id >= RTE_MAX_LCORE)
928 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
929 
930 			qconf = &lcore_queue_conf[rx_lcore_id];
931 		}
932 
933 		socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
934 		if (socket == SOCKET_ID_ANY)
935 			socket = 0;
936 
937 		rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
938 		rxq->portid = portid;
939 		rxq->direct_pool = socket_direct_pool[socket];
940 		rxq->indirect_pool = socket_indirect_pool[socket];
941 		rxq->lpm = socket_lpm[socket];
942 		rxq->lpm6 = socket_lpm6[socket];
943 		qconf->n_rx_queue++;
944 
945 		/* init port */
946 		printf("Initializing port %d on lcore %u...", portid,
947 		       rx_lcore_id);
948 		fflush(stdout);
949 
950 		n_tx_queue = nb_lcores;
951 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
952 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
953 		ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
954 					    &local_port_conf);
955 		if (ret < 0) {
956 			printf("\n");
957 			rte_exit(EXIT_FAILURE, "Cannot configure device: "
958 				"err=%d, port=%d\n",
959 				ret, portid);
960 		}
961 
962 		/* set the mtu to the maximum received packet size */
963 		ret = rte_eth_dev_set_mtu(portid, local_port_conf.rxmode.mtu);
964 		if (ret < 0) {
965 			printf("\n");
966 			rte_exit(EXIT_FAILURE, "Set MTU failed: "
967 				"err=%d, port=%d\n",
968 			ret, portid);
969 		}
970 
971 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
972 					    &nb_txd);
973 		if (ret < 0) {
974 			printf("\n");
975 			rte_exit(EXIT_FAILURE, "Cannot adjust number of "
976 				"descriptors: err=%d, port=%d\n", ret, portid);
977 		}
978 
979 		/* init one RX queue */
980 		rxq_conf = dev_info.default_rxconf;
981 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
982 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
983 					     socket, &rxq_conf,
984 					     socket_direct_pool[socket]);
985 		if (ret < 0) {
986 			printf("\n");
987 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
988 				"err=%d, port=%d\n",
989 				ret, portid);
990 		}
991 
992 		ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
993 		if (ret < 0) {
994 			printf("\n");
995 			rte_exit(EXIT_FAILURE,
996 				"rte_eth_macaddr_get: err=%d, port=%d\n",
997 				ret, portid);
998 		}
999 
1000 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
1001 		printf("\n");
1002 
1003 		/* init one TX queue per couple (lcore,port) */
1004 		ret = rte_eth_dev_info_get(portid, &dev_info);
1005 		if (ret != 0)
1006 			rte_exit(EXIT_FAILURE,
1007 				"Error during getting device (port %u) info: %s\n",
1008 				portid, strerror(-ret));
1009 
1010 		queueid = 0;
1011 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1012 			if (rte_lcore_is_enabled(lcore_id) == 0)
1013 				continue;
1014 
1015 			if (queueid >= dev_info.nb_tx_queues)
1016 				break;
1017 
1018 			socket = (int) rte_lcore_to_socket_id(lcore_id);
1019 			printf("txq=%u,%d ", lcore_id, queueid);
1020 			fflush(stdout);
1021 
1022 			txconf = &dev_info.default_txconf;
1023 			txconf->offloads = local_port_conf.txmode.offloads;
1024 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1025 						     socket, txconf);
1026 			if (ret < 0) {
1027 				printf("\n");
1028 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1029 					"err=%d, port=%d\n", ret, portid);
1030 			}
1031 
1032 			qconf = &lcore_queue_conf[lcore_id];
1033 			qconf->tx_queue_id[portid] = queueid;
1034 			queueid++;
1035 		}
1036 
1037 		printf("\n");
1038 	}
1039 
1040 	printf("\n");
1041 
1042 	/* start ports */
1043 	RTE_ETH_FOREACH_DEV(portid) {
1044 		if ((enabled_port_mask & (1 << portid)) == 0) {
1045 			continue;
1046 		}
1047 		/* Start device */
1048 		ret = rte_eth_dev_start(portid);
1049 		if (ret < 0)
1050 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1051 				ret, portid);
1052 
1053 		ret = rte_eth_promiscuous_enable(portid);
1054 		if (ret != 0)
1055 			rte_exit(EXIT_FAILURE,
1056 				"rte_eth_promiscuous_enable: err=%s, port=%d\n",
1057 				rte_strerror(-ret), portid);
1058 
1059 		if (check_ptype(portid) == 0) {
1060 			rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
1061 			printf("Add Rx callback function to detect L3 packet type by SW :"
1062 				" port = %d\n", portid);
1063 		}
1064 	}
1065 
1066 	if (init_routing_table() < 0)
1067 		rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1068 
1069 	check_all_ports_link_status(enabled_port_mask);
1070 
1071 	/* launch per-lcore init on every lcore */
1072 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
1073 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
1074 		if (rte_eal_wait_lcore(lcore_id) < 0)
1075 			return -1;
1076 	}
1077 
1078 	/* clean up the EAL */
1079 	rte_eal_cleanup();
1080 
1081 	return 0;
1082 }
1083