xref: /dpdk/examples/ip_fragmentation/main.c (revision ef97d30812a4c0c12e9cf0ef8739b21a666bd482)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <string.h>
41 #include <sys/queue.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <getopt.h>
45 
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_ring.h>
68 #include <rte_mempool.h>
69 #include <rte_mbuf.h>
70 #include <rte_lpm.h>
71 #include <rte_lpm6.h>
72 #include <rte_ip.h>
73 #include <rte_string_fns.h>
74 
75 #include <rte_ip_frag.h>
76 
77 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
78 
79 /* allow max jumbo frame 9.5 KB */
80 #define JUMBO_FRAME_MAX_SIZE	0x2600
81 
82 #define	ROUNDUP_DIV(a, b)	(((a) + (b) - 1) / (b))
83 
84 /*
85  * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
86  * This value includes the size of IPv6 header.
87  */
88 #define	IPV4_MTU_DEFAULT	ETHER_MTU
89 #define	IPV6_MTU_DEFAULT	ETHER_MTU
90 
91 /*
92  * Default payload in bytes for the IPv6 packet.
93  */
94 #define	IPV4_DEFAULT_PAYLOAD	(IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
95 #define	IPV6_DEFAULT_PAYLOAD	(IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr))
96 
97 /*
98  * Max number of fragments per packet expected - defined by config file.
99  */
100 #define	MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
101 
102 #define NB_MBUF   8192
103 
104 #define MAX_PKT_BURST	32
105 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
106 
107 /* Configure how many packets ahead to prefetch, when reading packets */
108 #define PREFETCH_OFFSET	3
109 
110 /*
111  * Configurable number of RX/TX ring descriptors
112  */
113 #define RTE_TEST_RX_DESC_DEFAULT 128
114 #define RTE_TEST_TX_DESC_DEFAULT 512
115 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
116 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
117 
118 /* ethernet addresses of ports */
119 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
120 
121 #ifndef IPv4_BYTES
122 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
123 #define IPv4_BYTES(addr) \
124 		(uint8_t) (((addr) >> 24) & 0xFF),\
125 		(uint8_t) (((addr) >> 16) & 0xFF),\
126 		(uint8_t) (((addr) >> 8) & 0xFF),\
127 		(uint8_t) ((addr) & 0xFF)
128 #endif
129 
130 #ifndef IPv6_BYTES
131 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
132                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
133 #define IPv6_BYTES(addr) \
134 	addr[0],  addr[1], addr[2],  addr[3], \
135 	addr[4],  addr[5], addr[6],  addr[7], \
136 	addr[8],  addr[9], addr[10], addr[11],\
137 	addr[12], addr[13],addr[14], addr[15]
138 #endif
139 
140 #define IPV6_ADDR_LEN 16
141 
142 /* mask of enabled ports */
143 static int enabled_port_mask = 0;
144 
145 static int rx_queue_per_lcore = 1;
146 
147 #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
148 
149 struct mbuf_table {
150 	uint16_t len;
151 	struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
152 };
153 
154 struct rx_queue {
155 	struct rte_mempool *direct_pool;
156 	struct rte_mempool *indirect_pool;
157 	struct rte_lpm *lpm;
158 	struct rte_lpm6 *lpm6;
159 	uint8_t portid;
160 };
161 
162 #define MAX_RX_QUEUE_PER_LCORE 16
163 #define MAX_TX_QUEUE_PER_PORT 16
164 struct lcore_queue_conf {
165 	uint16_t n_rx_queue;
166 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
167 	struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
168 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
169 } __rte_cache_aligned;
170 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
171 
172 static const struct rte_eth_conf port_conf = {
173 	.rxmode = {
174 		.max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
175 		.split_hdr_size = 0,
176 		.header_split   = 0, /**< Header Split disabled */
177 		.hw_ip_checksum = 1, /**< IP checksum offload enabled */
178 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
179 		.jumbo_frame    = 1, /**< Jumbo Frame Support enabled */
180 		.hw_strip_crc   = 0, /**< CRC stripped by hardware */
181 	},
182 	.txmode = {
183 		.mq_mode = ETH_MQ_TX_NONE,
184 	},
185 };
186 
187 /*
188  * IPv4 forwarding table
189  */
190 struct l3fwd_ipv4_route {
191 	uint32_t ip;
192 	uint8_t  depth;
193 	uint8_t  if_out;
194 };
195 
196 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
197 		{IPv4(100,10,0,0), 16, 0},
198 		{IPv4(100,20,0,0), 16, 1},
199 		{IPv4(100,30,0,0), 16, 2},
200 		{IPv4(100,40,0,0), 16, 3},
201 		{IPv4(100,50,0,0), 16, 4},
202 		{IPv4(100,60,0,0), 16, 5},
203 		{IPv4(100,70,0,0), 16, 6},
204 		{IPv4(100,80,0,0), 16, 7},
205 };
206 
207 /*
208  * IPv6 forwarding table
209  */
210 
211 struct l3fwd_ipv6_route {
212 	uint8_t ip[IPV6_ADDR_LEN];
213 	uint8_t depth;
214 	uint8_t if_out;
215 };
216 
217 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
218 	{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
219 	{{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
220 	{{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
221 	{{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
222 	{{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
223 	{{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
224 	{{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
225 	{{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
226 };
227 
228 #define LPM_MAX_RULES         1024
229 #define LPM6_MAX_RULES         1024
230 #define LPM6_NUMBER_TBL8S (1 << 16)
231 
232 struct rte_lpm6_config lpm6_config = {
233 		.max_rules = LPM6_MAX_RULES,
234 		.number_tbl8s = LPM6_NUMBER_TBL8S,
235 		.flags = 0
236 };
237 
238 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
239 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
240 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
241 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
242 
243 /* Send burst of packets on an output interface */
244 static inline int
245 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port)
246 {
247 	struct rte_mbuf **m_table;
248 	int ret;
249 	uint16_t queueid;
250 
251 	queueid = qconf->tx_queue_id[port];
252 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
253 
254 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
255 	if (unlikely(ret < n)) {
256 		do {
257 			rte_pktmbuf_free(m_table[ret]);
258 		} while (++ret < n);
259 	}
260 
261 	return 0;
262 }
263 
264 static inline void
265 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
266 		uint8_t queueid, uint8_t port_in)
267 {
268 	struct rx_queue *rxq;
269 	uint32_t i, len;
270 	uint8_t next_hop, port_out, ipv6;
271 	int32_t len2;
272 
273 	ipv6 = 0;
274 	rxq = &qconf->rx_queue_list[queueid];
275 
276 	/* by default, send everything back to the source port */
277 	port_out = port_in;
278 
279 	/* Remove the Ethernet header and trailer from the input packet */
280 	rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
281 
282 	/* Build transmission burst */
283 	len = qconf->tx_mbufs[port_out].len;
284 
285 	/* if this is an IPv4 packet */
286 #ifdef RTE_NEXT_ABI
287 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
288 #else
289 	if (m->ol_flags & PKT_RX_IPV4_HDR) {
290 #endif
291 		struct ipv4_hdr *ip_hdr;
292 		uint32_t ip_dst;
293 		/* Read the lookup key (i.e. ip_dst) from the input packet */
294 		ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
295 		ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
296 
297 		/* Find destination port */
298 		if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
299 				(enabled_port_mask & 1 << next_hop) != 0) {
300 			port_out = next_hop;
301 
302 			/* Build transmission burst for new port */
303 			len = qconf->tx_mbufs[port_out].len;
304 		}
305 
306 		/* if we don't need to do any fragmentation */
307 		if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
308 			qconf->tx_mbufs[port_out].m_table[len] = m;
309 			len2 = 1;
310 		} else {
311 			len2 = rte_ipv4_fragment_packet(m,
312 				&qconf->tx_mbufs[port_out].m_table[len],
313 				(uint16_t)(MBUF_TABLE_SIZE - len),
314 				IPV4_MTU_DEFAULT,
315 				rxq->direct_pool, rxq->indirect_pool);
316 
317 			/* Free input packet */
318 			rte_pktmbuf_free(m);
319 
320 			/* If we fail to fragment the packet */
321 			if (unlikely (len2 < 0))
322 				return;
323 		}
324 #ifdef RTE_NEXT_ABI
325 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
326 		/* if this is an IPv6 packet */
327 #else
328 	}
329 	/* if this is an IPv6 packet */
330 	else if (m->ol_flags & PKT_RX_IPV6_HDR) {
331 #endif
332 		struct ipv6_hdr *ip_hdr;
333 
334 		ipv6 = 1;
335 
336 		/* Read the lookup key (i.e. ip_dst) from the input packet */
337 		ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
338 
339 		/* Find destination port */
340 		if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr, &next_hop) == 0 &&
341 				(enabled_port_mask & 1 << next_hop) != 0) {
342 			port_out = next_hop;
343 
344 			/* Build transmission burst for new port */
345 			len = qconf->tx_mbufs[port_out].len;
346 		}
347 
348 		/* if we don't need to do any fragmentation */
349 		if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
350 			qconf->tx_mbufs[port_out].m_table[len] = m;
351 			len2 = 1;
352 		} else {
353 			len2 = rte_ipv6_fragment_packet(m,
354 				&qconf->tx_mbufs[port_out].m_table[len],
355 				(uint16_t)(MBUF_TABLE_SIZE - len),
356 				IPV6_MTU_DEFAULT,
357 				rxq->direct_pool, rxq->indirect_pool);
358 
359 			/* Free input packet */
360 			rte_pktmbuf_free(m);
361 
362 			/* If we fail to fragment the packet */
363 			if (unlikely (len2 < 0))
364 				return;
365 		}
366 	}
367 	/* else, just forward the packet */
368 	else {
369 		qconf->tx_mbufs[port_out].m_table[len] = m;
370 		len2 = 1;
371 	}
372 
373 	for (i = len; i < len + len2; i ++) {
374 		void *d_addr_bytes;
375 
376 		m = qconf->tx_mbufs[port_out].m_table[i];
377 		struct ether_hdr *eth_hdr = (struct ether_hdr *)
378 			rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
379 		if (eth_hdr == NULL) {
380 			rte_panic("No headroom in mbuf.\n");
381 		}
382 
383 		m->l2_len = sizeof(struct ether_hdr);
384 
385 		/* 02:00:00:00:00:xx */
386 		d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
387 		*((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40);
388 
389 		/* src addr */
390 		ether_addr_copy(&ports_eth_addr[port_out], &eth_hdr->s_addr);
391 		if (ipv6)
392 			eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
393 		else
394 			eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
395 	}
396 
397 	len += len2;
398 
399 	if (likely(len < MAX_PKT_BURST)) {
400 		qconf->tx_mbufs[port_out].len = (uint16_t)len;
401 		return;
402 	}
403 
404 	/* Transmit packets */
405 	send_burst(qconf, (uint16_t)len, port_out);
406 	qconf->tx_mbufs[port_out].len = 0;
407 }
408 
409 /* main processing loop */
410 static int
411 main_loop(__attribute__((unused)) void *dummy)
412 {
413 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
414 	unsigned lcore_id;
415 	uint64_t prev_tsc, diff_tsc, cur_tsc;
416 	int i, j, nb_rx;
417 	uint8_t portid;
418 	struct lcore_queue_conf *qconf;
419 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
420 
421 	prev_tsc = 0;
422 
423 	lcore_id = rte_lcore_id();
424 	qconf = &lcore_queue_conf[lcore_id];
425 
426 	if (qconf->n_rx_queue == 0) {
427 		RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
428 		return 0;
429 	}
430 
431 	RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
432 
433 	for (i = 0; i < qconf->n_rx_queue; i++) {
434 
435 		portid = qconf->rx_queue_list[i].portid;
436 		RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
437 				(int) portid);
438 	}
439 
440 	while (1) {
441 
442 		cur_tsc = rte_rdtsc();
443 
444 		/*
445 		 * TX burst queue drain
446 		 */
447 		diff_tsc = cur_tsc - prev_tsc;
448 		if (unlikely(diff_tsc > drain_tsc)) {
449 
450 			/*
451 			 * This could be optimized (use queueid instead of
452 			 * portid), but it is not called so often
453 			 */
454 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
455 				if (qconf->tx_mbufs[portid].len == 0)
456 					continue;
457 				send_burst(&lcore_queue_conf[lcore_id],
458 					   qconf->tx_mbufs[portid].len,
459 					   portid);
460 				qconf->tx_mbufs[portid].len = 0;
461 			}
462 
463 			prev_tsc = cur_tsc;
464 		}
465 
466 		/*
467 		 * Read packet from RX queues
468 		 */
469 		for (i = 0; i < qconf->n_rx_queue; i++) {
470 
471 			portid = qconf->rx_queue_list[i].portid;
472 			nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
473 						 MAX_PKT_BURST);
474 
475 			/* Prefetch first packets */
476 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
477 				rte_prefetch0(rte_pktmbuf_mtod(
478 						pkts_burst[j], void *));
479 			}
480 
481 			/* Prefetch and forward already prefetched packets */
482 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
483 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
484 						j + PREFETCH_OFFSET], void *));
485 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
486 			}
487 
488 			/* Forward remaining prefetched packets */
489 			for (; j < nb_rx; j++) {
490 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
491 			}
492 		}
493 	}
494 }
495 
496 /* display usage */
497 static void
498 print_usage(const char *prgname)
499 {
500 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
501 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
502 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
503 	       prgname);
504 }
505 
506 static int
507 parse_portmask(const char *portmask)
508 {
509 	char *end = NULL;
510 	unsigned long pm;
511 
512 	/* parse hexadecimal string */
513 	pm = strtoul(portmask, &end, 16);
514 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
515 		return -1;
516 
517 	if (pm == 0)
518 		return -1;
519 
520 	return pm;
521 }
522 
523 static int
524 parse_nqueue(const char *q_arg)
525 {
526 	char *end = NULL;
527 	unsigned long n;
528 
529 	/* parse hexadecimal string */
530 	n = strtoul(q_arg, &end, 10);
531 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
532 		return -1;
533 	if (n == 0)
534 		return -1;
535 	if (n >= MAX_RX_QUEUE_PER_LCORE)
536 		return -1;
537 
538 	return n;
539 }
540 
541 /* Parse the argument given in the command line of the application */
542 static int
543 parse_args(int argc, char **argv)
544 {
545 	int opt, ret;
546 	char **argvopt;
547 	int option_index;
548 	char *prgname = argv[0];
549 	static struct option lgopts[] = {
550 		{NULL, 0, 0, 0}
551 	};
552 
553 	argvopt = argv;
554 
555 	while ((opt = getopt_long(argc, argvopt, "p:q:",
556 				  lgopts, &option_index)) != EOF) {
557 
558 		switch (opt) {
559 		/* portmask */
560 		case 'p':
561 			enabled_port_mask = parse_portmask(optarg);
562 			if (enabled_port_mask < 0) {
563 				printf("invalid portmask\n");
564 				print_usage(prgname);
565 				return -1;
566 			}
567 			break;
568 
569 		/* nqueue */
570 		case 'q':
571 			rx_queue_per_lcore = parse_nqueue(optarg);
572 			if (rx_queue_per_lcore < 0) {
573 				printf("invalid queue number\n");
574 				print_usage(prgname);
575 				return -1;
576 			}
577 			break;
578 
579 		/* long options */
580 		case 0:
581 			print_usage(prgname);
582 			return -1;
583 
584 		default:
585 			print_usage(prgname);
586 			return -1;
587 		}
588 	}
589 
590 	if (enabled_port_mask == 0) {
591 		printf("portmask not specified\n");
592 		print_usage(prgname);
593 		return -1;
594 	}
595 
596 	if (optind >= 0)
597 		argv[optind-1] = prgname;
598 
599 	ret = optind-1;
600 	optind = 0; /* reset getopt lib */
601 	return ret;
602 }
603 
604 static void
605 print_ethaddr(const char *name, struct ether_addr *eth_addr)
606 {
607 	char buf[ETHER_ADDR_FMT_SIZE];
608 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
609 	printf("%s%s", name, buf);
610 }
611 
612 /* Check the link status of all ports in up to 9s, and print them finally */
613 static void
614 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
615 {
616 #define CHECK_INTERVAL 100 /* 100ms */
617 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
618 	uint8_t portid, count, all_ports_up, print_flag = 0;
619 	struct rte_eth_link link;
620 
621 	printf("\nChecking link status");
622 	fflush(stdout);
623 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
624 		all_ports_up = 1;
625 		for (portid = 0; portid < port_num; portid++) {
626 			if ((port_mask & (1 << portid)) == 0)
627 				continue;
628 			memset(&link, 0, sizeof(link));
629 			rte_eth_link_get_nowait(portid, &link);
630 			/* print link status if flag set */
631 			if (print_flag == 1) {
632 				if (link.link_status)
633 					printf("Port %d Link Up - speed %u "
634 						"Mbps - %s\n", (uint8_t)portid,
635 						(unsigned)link.link_speed,
636 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
637 					("full-duplex") : ("half-duplex\n"));
638 				else
639 					printf("Port %d Link Down\n",
640 							(uint8_t)portid);
641 				continue;
642 			}
643 			/* clear all_ports_up flag if any link down */
644 			if (link.link_status == 0) {
645 				all_ports_up = 0;
646 				break;
647 			}
648 		}
649 		/* after finally printing all link status, get out */
650 		if (print_flag == 1)
651 			break;
652 
653 		if (all_ports_up == 0) {
654 			printf(".");
655 			fflush(stdout);
656 			rte_delay_ms(CHECK_INTERVAL);
657 		}
658 
659 		/* set the print_flag if all ports up or timeout */
660 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
661 			print_flag = 1;
662 			printf("\ndone\n");
663 		}
664 	}
665 }
666 
667 static int
668 init_routing_table(void)
669 {
670 	struct rte_lpm *lpm;
671 	struct rte_lpm6 *lpm6;
672 	int socket, ret;
673 	unsigned i;
674 
675 	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
676 		if (socket_lpm[socket]) {
677 			lpm = socket_lpm[socket];
678 			/* populate the LPM table */
679 			for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
680 				ret = rte_lpm_add(lpm,
681 					l3fwd_ipv4_route_array[i].ip,
682 					l3fwd_ipv4_route_array[i].depth,
683 					l3fwd_ipv4_route_array[i].if_out);
684 
685 				if (ret < 0) {
686 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
687 						"LPM table\n", i);
688 					return -1;
689 				}
690 
691 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
692 						"/%d (port %d)\n",
693 					socket,
694 					IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
695 					l3fwd_ipv4_route_array[i].depth,
696 					l3fwd_ipv4_route_array[i].if_out);
697 			}
698 		}
699 
700 		if (socket_lpm6[socket]) {
701 			lpm6 = socket_lpm6[socket];
702 			/* populate the LPM6 table */
703 			for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
704 				ret = rte_lpm6_add(lpm6,
705 					l3fwd_ipv6_route_array[i].ip,
706 					l3fwd_ipv6_route_array[i].depth,
707 					l3fwd_ipv6_route_array[i].if_out);
708 
709 				if (ret < 0) {
710 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
711 						"LPM6 table\n", i);
712 					return -1;
713 				}
714 
715 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
716 						"/%d (port %d)\n",
717 					socket,
718 					IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
719 					l3fwd_ipv6_route_array[i].depth,
720 					l3fwd_ipv6_route_array[i].if_out);
721 			}
722 		}
723 	}
724 	return 0;
725 }
726 
727 static int
728 init_mem(void)
729 {
730 	char buf[PATH_MAX];
731 	struct rte_mempool *mp;
732 	struct rte_lpm *lpm;
733 	struct rte_lpm6 *lpm6;
734 	int socket;
735 	unsigned lcore_id;
736 
737 	/* traverse through lcores and initialize structures on each socket */
738 
739 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
740 
741 		if (rte_lcore_is_enabled(lcore_id) == 0)
742 			continue;
743 
744 		socket = rte_lcore_to_socket_id(lcore_id);
745 
746 		if (socket == SOCKET_ID_ANY)
747 			socket = 0;
748 
749 		if (socket_direct_pool[socket] == NULL) {
750 			RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
751 					socket);
752 			snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
753 
754 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
755 				0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
756 			if (mp == NULL) {
757 				RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
758 				return -1;
759 			}
760 			socket_direct_pool[socket] = mp;
761 		}
762 
763 		if (socket_indirect_pool[socket] == NULL) {
764 			RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
765 					socket);
766 			snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
767 
768 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
769 				socket);
770 			if (mp == NULL) {
771 				RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
772 				return -1;
773 			}
774 			socket_indirect_pool[socket] = mp;
775 		}
776 
777 		if (socket_lpm[socket] == NULL) {
778 			RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
779 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
780 
781 			lpm = rte_lpm_create(buf, socket, LPM_MAX_RULES, 0);
782 			if (lpm == NULL) {
783 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
784 				return -1;
785 			}
786 			socket_lpm[socket] = lpm;
787 		}
788 
789 		if (socket_lpm6[socket] == NULL) {
790 			RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
791 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
792 
793 			lpm6 = rte_lpm6_create("IP_FRAG_LPM6", socket, &lpm6_config);
794 			if (lpm6 == NULL) {
795 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
796 				return -1;
797 			}
798 			socket_lpm6[socket] = lpm6;
799 		}
800 	}
801 
802 	return 0;
803 }
804 
805 int
806 main(int argc, char **argv)
807 {
808 	struct lcore_queue_conf *qconf;
809 	struct rte_eth_dev_info dev_info;
810 	struct rte_eth_txconf *txconf;
811 	struct rx_queue *rxq;
812 	int socket, ret;
813 	unsigned nb_ports;
814 	uint16_t queueid = 0;
815 	unsigned lcore_id = 0, rx_lcore_id = 0;
816 	uint32_t n_tx_queue, nb_lcores;
817 	uint8_t portid;
818 
819 	/* init EAL */
820 	ret = rte_eal_init(argc, argv);
821 	if (ret < 0)
822 		rte_exit(EXIT_FAILURE, "rte_eal_init failed");
823 	argc -= ret;
824 	argv += ret;
825 
826 	/* parse application arguments (after the EAL ones) */
827 	ret = parse_args(argc, argv);
828 	if (ret < 0)
829 		rte_exit(EXIT_FAILURE, "Invalid arguments");
830 
831 	nb_ports = rte_eth_dev_count();
832 	if (nb_ports > RTE_MAX_ETHPORTS)
833 		nb_ports = RTE_MAX_ETHPORTS;
834 	else if (nb_ports == 0)
835 		rte_exit(EXIT_FAILURE, "No ports found!\n");
836 
837 	nb_lcores = rte_lcore_count();
838 
839 	/* initialize structures (mempools, lpm etc.) */
840 	if (init_mem() < 0)
841 		rte_panic("Cannot initialize memory structures!\n");
842 
843 	/* check if portmask has non-existent ports */
844 	if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
845 		rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
846 
847 	/* initialize all ports */
848 	for (portid = 0; portid < nb_ports; portid++) {
849 		/* skip ports that are not enabled */
850 		if ((enabled_port_mask & (1 << portid)) == 0) {
851 			printf("Skipping disabled port %d\n", portid);
852 			continue;
853 		}
854 
855 		qconf = &lcore_queue_conf[rx_lcore_id];
856 
857 		/* get the lcore_id for this port */
858 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
859 		       qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
860 
861 			rx_lcore_id ++;
862 			if (rx_lcore_id >= RTE_MAX_LCORE)
863 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
864 
865 			qconf = &lcore_queue_conf[rx_lcore_id];
866 		}
867 
868 		socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
869 		if (socket == SOCKET_ID_ANY)
870 			socket = 0;
871 
872 		rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
873 		rxq->portid = portid;
874 		rxq->direct_pool = socket_direct_pool[socket];
875 		rxq->indirect_pool = socket_indirect_pool[socket];
876 		rxq->lpm = socket_lpm[socket];
877 		rxq->lpm6 = socket_lpm6[socket];
878 		qconf->n_rx_queue++;
879 
880 		/* init port */
881 		printf("Initializing port %d on lcore %u...", portid,
882 		       rx_lcore_id);
883 		fflush(stdout);
884 
885 		n_tx_queue = nb_lcores;
886 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
887 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
888 		ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
889 					    &port_conf);
890 		if (ret < 0) {
891 			printf("\n");
892 			rte_exit(EXIT_FAILURE, "Cannot configure device: "
893 				"err=%d, port=%d\n",
894 				ret, portid);
895 		}
896 
897 		/* init one RX queue */
898 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
899 					     socket, NULL,
900 					     socket_direct_pool[socket]);
901 		if (ret < 0) {
902 			printf("\n");
903 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
904 				"err=%d, port=%d\n",
905 				ret, portid);
906 		}
907 
908 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
909 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
910 		printf("\n");
911 
912 		/* init one TX queue per couple (lcore,port) */
913 		queueid = 0;
914 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
915 			if (rte_lcore_is_enabled(lcore_id) == 0)
916 				continue;
917 
918 			socket = (int) rte_lcore_to_socket_id(lcore_id);
919 			printf("txq=%u,%d ", lcore_id, queueid);
920 			fflush(stdout);
921 
922 			rte_eth_dev_info_get(portid, &dev_info);
923 			txconf = &dev_info.default_txconf;
924 			txconf->txq_flags = 0;
925 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
926 						     socket, txconf);
927 			if (ret < 0) {
928 				printf("\n");
929 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
930 					"err=%d, port=%d\n", ret, portid);
931 			}
932 
933 			qconf = &lcore_queue_conf[lcore_id];
934 			qconf->tx_queue_id[portid] = queueid;
935 			queueid++;
936 		}
937 
938 		printf("\n");
939 	}
940 
941 	printf("\n");
942 
943 	/* start ports */
944 	for (portid = 0; portid < nb_ports; portid++) {
945 		if ((enabled_port_mask & (1 << portid)) == 0) {
946 			continue;
947 		}
948 		/* Start device */
949 		ret = rte_eth_dev_start(portid);
950 		if (ret < 0)
951 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
952 				ret, portid);
953 
954 		rte_eth_promiscuous_enable(portid);
955 	}
956 
957 	if (init_routing_table() < 0)
958 		rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
959 
960 	check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
961 
962 	/* launch per-lcore init on every lcore */
963 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
964 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
965 		if (rte_eal_wait_lcore(lcore_id) < 0)
966 			return -1;
967 	}
968 
969 	return 0;
970 }
971