xref: /dpdk/examples/ip_reassembly/main.c (revision 89f0711f9ddfb5822da9d34f384b92f72a61c4dc)
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 <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 <sys/param.h>
17 
18 #include <rte_common.h>
19 #include <rte_byteorder.h>
20 #include <rte_log.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_eal.h>
24 #include <rte_launch.h>
25 #include <rte_atomic.h>
26 #include <rte_cycles.h>
27 #include <rte_prefetch.h>
28 #include <rte_lcore.h>
29 #include <rte_per_lcore.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_interrupts.h>
32 #include <rte_random.h>
33 #include <rte_debug.h>
34 #include <rte_ether.h>
35 #include <rte_ethdev.h>
36 #include <rte_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_malloc.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_lpm.h>
44 #include <rte_lpm6.h>
45 
46 #include <rte_ip_frag.h>
47 
48 #define MAX_PKT_BURST 32
49 
50 
51 #define RTE_LOGTYPE_IP_RSMBL RTE_LOGTYPE_USER1
52 
53 #define MAX_JUMBO_PKT_LEN  9600
54 
55 #define	BUF_SIZE	RTE_MBUF_DEFAULT_DATAROOM
56 #define	MBUF_DATA_SIZE	RTE_MBUF_DEFAULT_BUF_SIZE
57 
58 #define NB_MBUF 8192
59 #define MEMPOOL_CACHE_SIZE 256
60 
61 /* allow max jumbo frame 9.5 KB */
62 #define JUMBO_FRAME_MAX_SIZE	0x2600
63 
64 #define	MAX_FLOW_NUM	UINT16_MAX
65 #define	MIN_FLOW_NUM	1
66 #define	DEF_FLOW_NUM	0x1000
67 
68 /* TTL numbers are in ms. */
69 #define	MAX_FLOW_TTL	(3600 * MS_PER_S)
70 #define	MIN_FLOW_TTL	1
71 #define	DEF_FLOW_TTL	MS_PER_S
72 
73 #define MAX_FRAG_NUM RTE_LIBRTE_IP_FRAG_MAX_FRAG
74 
75 /* Should be power of two. */
76 #define	IP_FRAG_TBL_BUCKET_ENTRIES	16
77 
78 static uint32_t max_flow_num = DEF_FLOW_NUM;
79 static uint32_t max_flow_ttl = DEF_FLOW_TTL;
80 
81 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
82 
83 #define NB_SOCKETS 8
84 
85 /* Configure how many packets ahead to prefetch, when reading packets */
86 #define PREFETCH_OFFSET	3
87 
88 /*
89  * Configurable number of RX/TX ring descriptors
90  */
91 #define RTE_TEST_RX_DESC_DEFAULT 128
92 #define RTE_TEST_TX_DESC_DEFAULT 512
93 
94 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
95 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
96 
97 /* ethernet addresses of ports */
98 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
99 
100 #ifndef IPv4_BYTES
101 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
102 #define IPv4_BYTES(addr) \
103 		(uint8_t) (((addr) >> 24) & 0xFF),\
104 		(uint8_t) (((addr) >> 16) & 0xFF),\
105 		(uint8_t) (((addr) >> 8) & 0xFF),\
106 		(uint8_t) ((addr) & 0xFF)
107 #endif
108 
109 #ifndef IPv6_BYTES
110 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
111                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
112 #define IPv6_BYTES(addr) \
113 	addr[0],  addr[1], addr[2],  addr[3], \
114 	addr[4],  addr[5], addr[6],  addr[7], \
115 	addr[8],  addr[9], addr[10], addr[11],\
116 	addr[12], addr[13],addr[14], addr[15]
117 #endif
118 
119 #define IPV6_ADDR_LEN 16
120 
121 /* mask of enabled ports */
122 static uint32_t enabled_port_mask = 0;
123 
124 static int rx_queue_per_lcore = 1;
125 
126 struct mbuf_table {
127 	uint32_t len;
128 	uint32_t head;
129 	uint32_t tail;
130 	struct rte_mbuf *m_table[0];
131 };
132 
133 struct rx_queue {
134 	struct rte_ip_frag_tbl *frag_tbl;
135 	struct rte_mempool *pool;
136 	struct rte_lpm *lpm;
137 	struct rte_lpm6 *lpm6;
138 	uint16_t portid;
139 };
140 
141 struct tx_lcore_stat {
142 	uint64_t call;
143 	uint64_t drop;
144 	uint64_t queue;
145 	uint64_t send;
146 };
147 
148 #define MAX_RX_QUEUE_PER_LCORE 16
149 #define MAX_TX_QUEUE_PER_PORT 16
150 #define MAX_RX_QUEUE_PER_PORT 128
151 
152 struct lcore_queue_conf {
153 	uint16_t n_rx_queue;
154 	struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
155 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
156 	struct rte_ip_frag_death_row death_row;
157 	struct mbuf_table *tx_mbufs[RTE_MAX_ETHPORTS];
158 	struct tx_lcore_stat tx_stat;
159 } __rte_cache_aligned;
160 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
161 
162 static struct rte_eth_conf port_conf = {
163 	.rxmode = {
164 		.mq_mode        = ETH_MQ_RX_RSS,
165 		.max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
166 		.split_hdr_size = 0,
167 		.ignore_offload_bitfield = 1,
168 		.offloads = (DEV_RX_OFFLOAD_CHECKSUM |
169 			     DEV_RX_OFFLOAD_JUMBO_FRAME |
170 			     DEV_RX_OFFLOAD_CRC_STRIP),
171 	},
172 	.rx_adv_conf = {
173 			.rss_conf = {
174 				.rss_key = NULL,
175 				.rss_hf = ETH_RSS_IP,
176 		},
177 	},
178 	.txmode = {
179 		.mq_mode = ETH_MQ_TX_NONE,
180 		.offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
181 			     DEV_TX_OFFLOAD_MULTI_SEGS),
182 	},
183 };
184 
185 /*
186  * IPv4 forwarding table
187  */
188 struct l3fwd_ipv4_route {
189 	uint32_t ip;
190 	uint8_t  depth;
191 	uint8_t  if_out;
192 };
193 
194 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
195 		{IPv4(100,10,0,0), 16, 0},
196 		{IPv4(100,20,0,0), 16, 1},
197 		{IPv4(100,30,0,0), 16, 2},
198 		{IPv4(100,40,0,0), 16, 3},
199 		{IPv4(100,50,0,0), 16, 4},
200 		{IPv4(100,60,0,0), 16, 5},
201 		{IPv4(100,70,0,0), 16, 6},
202 		{IPv4(100,80,0,0), 16, 7},
203 };
204 
205 /*
206  * IPv6 forwarding table
207  */
208 
209 struct l3fwd_ipv6_route {
210 	uint8_t ip[IPV6_ADDR_LEN];
211 	uint8_t depth;
212 	uint8_t if_out;
213 };
214 
215 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
216 	{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
217 	{{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
218 	{{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
219 	{{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
220 	{{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
221 	{{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
222 	{{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
223 	{{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
224 };
225 
226 #define LPM_MAX_RULES         1024
227 #define LPM6_MAX_RULES         1024
228 #define LPM6_NUMBER_TBL8S (1 << 16)
229 
230 struct rte_lpm6_config lpm6_config = {
231 		.max_rules = LPM6_MAX_RULES,
232 		.number_tbl8s = LPM6_NUMBER_TBL8S,
233 		.flags = 0
234 };
235 
236 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
237 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
238 
239 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT
240 #define TX_LCORE_STAT_UPDATE(s, f, v)   ((s)->f += (v))
241 #else
242 #define TX_LCORE_STAT_UPDATE(s, f, v)   do {} while (0)
243 #endif /* RTE_LIBRTE_IP_FRAG_TBL_STAT */
244 
245 /*
246  * If number of queued packets reached given threahold, then
247  * send burst of packets on an output interface.
248  */
249 static inline uint32_t
250 send_burst(struct lcore_queue_conf *qconf, uint32_t thresh, uint16_t port)
251 {
252 	uint32_t fill, len, k, n;
253 	struct mbuf_table *txmb;
254 
255 	txmb = qconf->tx_mbufs[port];
256 	len = txmb->len;
257 
258 	if ((int32_t)(fill = txmb->head - txmb->tail) < 0)
259 		fill += len;
260 
261 	if (fill >= thresh) {
262 		n = RTE_MIN(len - txmb->tail, fill);
263 
264 		k = rte_eth_tx_burst(port, qconf->tx_queue_id[port],
265 			txmb->m_table + txmb->tail, (uint16_t)n);
266 
267 		TX_LCORE_STAT_UPDATE(&qconf->tx_stat, call, 1);
268 		TX_LCORE_STAT_UPDATE(&qconf->tx_stat, send, k);
269 
270 		fill -= k;
271 		if ((txmb->tail += k) == len)
272 			txmb->tail = 0;
273 	}
274 
275 	return fill;
276 }
277 
278 /* Enqueue a single packet, and send burst if queue is filled */
279 static inline int
280 send_single_packet(struct rte_mbuf *m, uint16_t port)
281 {
282 	uint32_t fill, lcore_id, len;
283 	struct lcore_queue_conf *qconf;
284 	struct mbuf_table *txmb;
285 
286 	lcore_id = rte_lcore_id();
287 	qconf = &lcore_queue_conf[lcore_id];
288 
289 	txmb = qconf->tx_mbufs[port];
290 	len = txmb->len;
291 
292 	fill = send_burst(qconf, MAX_PKT_BURST, port);
293 
294 	if (fill == len - 1) {
295 		TX_LCORE_STAT_UPDATE(&qconf->tx_stat, drop, 1);
296 		rte_pktmbuf_free(txmb->m_table[txmb->tail]);
297 		if (++txmb->tail == len)
298 			txmb->tail = 0;
299 	}
300 
301 	TX_LCORE_STAT_UPDATE(&qconf->tx_stat, queue, 1);
302 	txmb->m_table[txmb->head] = m;
303 	if(++txmb->head == len)
304 		txmb->head = 0;
305 
306 	return 0;
307 }
308 
309 static inline void
310 reassemble(struct rte_mbuf *m, uint16_t portid, uint32_t queue,
311 	struct lcore_queue_conf *qconf, uint64_t tms)
312 {
313 	struct ether_hdr *eth_hdr;
314 	struct rte_ip_frag_tbl *tbl;
315 	struct rte_ip_frag_death_row *dr;
316 	struct rx_queue *rxq;
317 	void *d_addr_bytes;
318 	uint32_t next_hop;
319 	uint16_t dst_port;
320 
321 	rxq = &qconf->rx_queue_list[queue];
322 
323 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
324 
325 	dst_port = portid;
326 
327 	/* if packet is IPv4 */
328 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
329 		struct ipv4_hdr *ip_hdr;
330 		uint32_t ip_dst;
331 
332 		ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
333 
334 		 /* if it is a fragmented packet, then try to reassemble. */
335 		if (rte_ipv4_frag_pkt_is_fragmented(ip_hdr)) {
336 			struct rte_mbuf *mo;
337 
338 			tbl = rxq->frag_tbl;
339 			dr = &qconf->death_row;
340 
341 			/* prepare mbuf: setup l2_len/l3_len. */
342 			m->l2_len = sizeof(*eth_hdr);
343 			m->l3_len = sizeof(*ip_hdr);
344 
345 			/* process this fragment. */
346 			mo = rte_ipv4_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr);
347 			if (mo == NULL)
348 				/* no packet to send out. */
349 				return;
350 
351 			/* we have our packet reassembled. */
352 			if (mo != m) {
353 				m = mo;
354 				eth_hdr = rte_pktmbuf_mtod(m,
355 					struct ether_hdr *);
356 				ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
357 			}
358 		}
359 		ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
360 
361 		/* Find destination port */
362 		if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
363 				(enabled_port_mask & 1 << next_hop) != 0) {
364 			dst_port = next_hop;
365 		}
366 
367 		eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
368 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
369 		/* if packet is IPv6 */
370 		struct ipv6_extension_fragment *frag_hdr;
371 		struct ipv6_hdr *ip_hdr;
372 
373 		ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
374 
375 		frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(ip_hdr);
376 
377 		if (frag_hdr != NULL) {
378 			struct rte_mbuf *mo;
379 
380 			tbl = rxq->frag_tbl;
381 			dr  = &qconf->death_row;
382 
383 			/* prepare mbuf: setup l2_len/l3_len. */
384 			m->l2_len = sizeof(*eth_hdr);
385 			m->l3_len = sizeof(*ip_hdr) + sizeof(*frag_hdr);
386 
387 			mo = rte_ipv6_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr, frag_hdr);
388 			if (mo == NULL)
389 				return;
390 
391 			if (mo != m) {
392 				m = mo;
393 				eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
394 				ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
395 			}
396 		}
397 
398 		/* Find destination port */
399 		if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
400 						&next_hop) == 0 &&
401 				(enabled_port_mask & 1 << next_hop) != 0) {
402 			dst_port = next_hop;
403 		}
404 
405 		eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
406 	}
407 	/* if packet wasn't IPv4 or IPv6, it's forwarded to the port it came from */
408 
409 	/* 02:00:00:00:00:xx */
410 	d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
411 	*((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
412 
413 	/* src addr */
414 	ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
415 
416 	send_single_packet(m, dst_port);
417 }
418 
419 /* main processing loop */
420 static int
421 main_loop(__attribute__((unused)) void *dummy)
422 {
423 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
424 	unsigned lcore_id;
425 	uint64_t diff_tsc, cur_tsc, prev_tsc;
426 	int i, j, nb_rx;
427 	uint16_t portid;
428 	struct lcore_queue_conf *qconf;
429 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
430 
431 	prev_tsc = 0;
432 
433 	lcore_id = rte_lcore_id();
434 	qconf = &lcore_queue_conf[lcore_id];
435 
436 	if (qconf->n_rx_queue == 0) {
437 		RTE_LOG(INFO, IP_RSMBL, "lcore %u has nothing to do\n", lcore_id);
438 		return 0;
439 	}
440 
441 	RTE_LOG(INFO, IP_RSMBL, "entering main loop on lcore %u\n", lcore_id);
442 
443 	for (i = 0; i < qconf->n_rx_queue; i++) {
444 
445 		portid = qconf->rx_queue_list[i].portid;
446 		RTE_LOG(INFO, IP_RSMBL, " -- lcoreid=%u portid=%u\n", lcore_id,
447 			portid);
448 	}
449 
450 	while (1) {
451 
452 		cur_tsc = rte_rdtsc();
453 
454 		/*
455 		 * TX burst queue drain
456 		 */
457 		diff_tsc = cur_tsc - prev_tsc;
458 		if (unlikely(diff_tsc > drain_tsc)) {
459 
460 			/*
461 			 * This could be optimized (use queueid instead of
462 			 * portid), but it is not called so often
463 			 */
464 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
465 				if ((enabled_port_mask & (1 << portid)) != 0)
466 					send_burst(qconf, 1, portid);
467 			}
468 
469 			prev_tsc = cur_tsc;
470 		}
471 
472 		/*
473 		 * Read packet from RX queues
474 		 */
475 		for (i = 0; i < qconf->n_rx_queue; ++i) {
476 
477 			portid = qconf->rx_queue_list[i].portid;
478 
479 			nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
480 				MAX_PKT_BURST);
481 
482 			/* Prefetch first packets */
483 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
484 				rte_prefetch0(rte_pktmbuf_mtod(
485 						pkts_burst[j], void *));
486 			}
487 
488 			/* Prefetch and forward already prefetched packets */
489 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
490 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
491 					j + PREFETCH_OFFSET], void *));
492 				reassemble(pkts_burst[j], portid,
493 					i, qconf, cur_tsc);
494 			}
495 
496 			/* Forward remaining prefetched packets */
497 			for (; j < nb_rx; j++) {
498 				reassemble(pkts_burst[j], portid,
499 					i, qconf, cur_tsc);
500 			}
501 
502 			rte_ip_frag_free_death_row(&qconf->death_row,
503 				PREFETCH_OFFSET);
504 		}
505 	}
506 }
507 
508 /* display usage */
509 static void
510 print_usage(const char *prgname)
511 {
512 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]"
513 		"  [--max-pkt-len PKTLEN]"
514 		"  [--maxflows=<flows>]  [--flowttl=<ttl>[(s|ms)]]\n"
515 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
516 		"  -q NQ: number of RX queues per lcore\n"
517 		"  --maxflows=<flows>: optional, maximum number of flows "
518 		"supported\n"
519 		"  --flowttl=<ttl>[(s|ms)]: optional, maximum TTL for each "
520 		"flow\n",
521 		prgname);
522 }
523 
524 static uint32_t
525 parse_flow_num(const char *str, uint32_t min, uint32_t max, uint32_t *val)
526 {
527 	char *end;
528 	uint64_t v;
529 
530 	/* parse decimal string */
531 	errno = 0;
532 	v = strtoul(str, &end, 10);
533 	if (errno != 0 || *end != '\0')
534 		return -EINVAL;
535 
536 	if (v < min || v > max)
537 		return -EINVAL;
538 
539 	*val = (uint32_t)v;
540 	return 0;
541 }
542 
543 static int
544 parse_flow_ttl(const char *str, uint32_t min, uint32_t max, uint32_t *val)
545 {
546 	char *end;
547 	uint64_t v;
548 
549 	static const char frmt_sec[] = "s";
550 	static const char frmt_msec[] = "ms";
551 
552 	/* parse decimal string */
553 	errno = 0;
554 	v = strtoul(str, &end, 10);
555 	if (errno != 0)
556 		return -EINVAL;
557 
558 	if (*end != '\0') {
559 		if (strncmp(frmt_sec, end, sizeof(frmt_sec)) == 0)
560 			v *= MS_PER_S;
561 		else if (strncmp(frmt_msec, end, sizeof (frmt_msec)) != 0)
562 			return -EINVAL;
563 	}
564 
565 	if (v < min || v > max)
566 		return -EINVAL;
567 
568 	*val = (uint32_t)v;
569 	return 0;
570 }
571 
572 static int
573 parse_portmask(const char *portmask)
574 {
575 	char *end = NULL;
576 	unsigned long pm;
577 
578 	/* parse hexadecimal string */
579 	pm = strtoul(portmask, &end, 16);
580 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
581 		return -1;
582 
583 	if (pm == 0)
584 		return -1;
585 
586 	return pm;
587 }
588 
589 static int
590 parse_nqueue(const char *q_arg)
591 {
592 	char *end = NULL;
593 	unsigned long n;
594 
595 	printf("%p\n", q_arg);
596 
597 	/* parse hexadecimal string */
598 	n = strtoul(q_arg, &end, 10);
599 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
600 		return -1;
601 	if (n == 0)
602 		return -1;
603 	if (n >= MAX_RX_QUEUE_PER_LCORE)
604 		return -1;
605 
606 	return n;
607 }
608 
609 /* Parse the argument given in the command line of the application */
610 static int
611 parse_args(int argc, char **argv)
612 {
613 	int opt, ret;
614 	char **argvopt;
615 	int option_index;
616 	char *prgname = argv[0];
617 	static struct option lgopts[] = {
618 		{"max-pkt-len", 1, 0, 0},
619 		{"maxflows", 1, 0, 0},
620 		{"flowttl", 1, 0, 0},
621 		{NULL, 0, 0, 0}
622 	};
623 
624 	argvopt = argv;
625 
626 	while ((opt = getopt_long(argc, argvopt, "p:q:",
627 				lgopts, &option_index)) != EOF) {
628 
629 		switch (opt) {
630 		/* portmask */
631 		case 'p':
632 			enabled_port_mask = parse_portmask(optarg);
633 			if (enabled_port_mask == 0) {
634 				printf("invalid portmask\n");
635 				print_usage(prgname);
636 				return -1;
637 			}
638 			break;
639 
640 		/* nqueue */
641 		case 'q':
642 			rx_queue_per_lcore = parse_nqueue(optarg);
643 			if (rx_queue_per_lcore < 0) {
644 				printf("invalid queue number\n");
645 				print_usage(prgname);
646 				return -1;
647 			}
648 			break;
649 
650 		/* long options */
651 		case 0:
652 			if (!strncmp(lgopts[option_index].name,
653 					"maxflows", 8)) {
654 				if ((ret = parse_flow_num(optarg, MIN_FLOW_NUM,
655 						MAX_FLOW_NUM,
656 						&max_flow_num)) != 0) {
657 					printf("invalid value: \"%s\" for "
658 						"parameter %s\n",
659 						optarg,
660 						lgopts[option_index].name);
661 					print_usage(prgname);
662 					return ret;
663 				}
664 			}
665 
666 			if (!strncmp(lgopts[option_index].name, "flowttl", 7)) {
667 				if ((ret = parse_flow_ttl(optarg, MIN_FLOW_TTL,
668 						MAX_FLOW_TTL,
669 						&max_flow_ttl)) != 0) {
670 					printf("invalid value: \"%s\" for "
671 						"parameter %s\n",
672 						optarg,
673 						lgopts[option_index].name);
674 					print_usage(prgname);
675 					return ret;
676 				}
677 			}
678 
679 			break;
680 
681 		default:
682 			print_usage(prgname);
683 			return -1;
684 		}
685 	}
686 
687 	if (optind >= 0)
688 		argv[optind-1] = prgname;
689 
690 	ret = optind-1;
691 	optind = 1; /* reset getopt lib */
692 	return ret;
693 }
694 
695 static void
696 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
697 {
698 	char buf[ETHER_ADDR_FMT_SIZE];
699 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
700 	printf("%s%s", name, buf);
701 }
702 
703 /* Check the link status of all ports in up to 9s, and print them finally */
704 static void
705 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
706 {
707 #define CHECK_INTERVAL 100 /* 100ms */
708 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
709 	uint16_t portid;
710 	uint8_t count, all_ports_up, print_flag = 0;
711 	struct rte_eth_link link;
712 
713 	printf("\nChecking link status");
714 	fflush(stdout);
715 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
716 		all_ports_up = 1;
717 		for (portid = 0; portid < port_num; portid++) {
718 			if ((port_mask & (1 << portid)) == 0)
719 				continue;
720 			memset(&link, 0, sizeof(link));
721 			rte_eth_link_get_nowait(portid, &link);
722 			/* print link status if flag set */
723 			if (print_flag == 1) {
724 				if (link.link_status)
725 					printf(
726 					"Port%d Link Up. Speed %u Mbps - %s\n",
727 						portid, link.link_speed,
728 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
729 					("full-duplex") : ("half-duplex\n"));
730 				else
731 					printf("Port %d Link Down\n", portid);
732 				continue;
733 			}
734 			/* clear all_ports_up flag if any link down */
735 			if (link.link_status == ETH_LINK_DOWN) {
736 				all_ports_up = 0;
737 				break;
738 			}
739 		}
740 		/* after finally printing all link status, get out */
741 		if (print_flag == 1)
742 			break;
743 
744 		if (all_ports_up == 0) {
745 			printf(".");
746 			fflush(stdout);
747 			rte_delay_ms(CHECK_INTERVAL);
748 		}
749 
750 		/* set the print_flag if all ports up or timeout */
751 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
752 			print_flag = 1;
753 			printf("\ndone\n");
754 		}
755 	}
756 }
757 
758 static int
759 init_routing_table(void)
760 {
761 	struct rte_lpm *lpm;
762 	struct rte_lpm6 *lpm6;
763 	int socket, ret;
764 	unsigned i;
765 
766 	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
767 		if (socket_lpm[socket]) {
768 			lpm = socket_lpm[socket];
769 			/* populate the LPM table */
770 			for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
771 				ret = rte_lpm_add(lpm,
772 					l3fwd_ipv4_route_array[i].ip,
773 					l3fwd_ipv4_route_array[i].depth,
774 					l3fwd_ipv4_route_array[i].if_out);
775 
776 				if (ret < 0) {
777 					RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
778 						"LPM table\n", i);
779 					return -1;
780 				}
781 
782 				RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv4_BYTES_FMT
783 						"/%d (port %d)\n",
784 					socket,
785 					IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
786 					l3fwd_ipv4_route_array[i].depth,
787 					l3fwd_ipv4_route_array[i].if_out);
788 			}
789 		}
790 
791 		if (socket_lpm6[socket]) {
792 			lpm6 = socket_lpm6[socket];
793 			/* populate the LPM6 table */
794 			for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
795 				ret = rte_lpm6_add(lpm6,
796 					l3fwd_ipv6_route_array[i].ip,
797 					l3fwd_ipv6_route_array[i].depth,
798 					l3fwd_ipv6_route_array[i].if_out);
799 
800 				if (ret < 0) {
801 					RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
802 						"LPM6 table\n", i);
803 					return -1;
804 				}
805 
806 				RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv6_BYTES_FMT
807 						"/%d (port %d)\n",
808 					socket,
809 					IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
810 					l3fwd_ipv6_route_array[i].depth,
811 					l3fwd_ipv6_route_array[i].if_out);
812 			}
813 		}
814 	}
815 	return 0;
816 }
817 
818 static int
819 setup_port_tbl(struct lcore_queue_conf *qconf, uint32_t lcore, int socket,
820 	uint32_t port)
821 {
822 	struct mbuf_table *mtb;
823 	uint32_t n;
824 	size_t sz;
825 
826 	n = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST);
827 	sz = sizeof (*mtb) + sizeof (mtb->m_table[0]) *  n;
828 
829 	if ((mtb = rte_zmalloc_socket(__func__, sz, RTE_CACHE_LINE_SIZE,
830 			socket)) == NULL) {
831 		RTE_LOG(ERR, IP_RSMBL, "%s() for lcore: %u, port: %u "
832 			"failed to allocate %zu bytes\n",
833 			__func__, lcore, port, sz);
834 		return -1;
835 	}
836 
837 	mtb->len = n;
838 	qconf->tx_mbufs[port] = mtb;
839 
840 	return 0;
841 }
842 
843 static int
844 setup_queue_tbl(struct rx_queue *rxq, uint32_t lcore, uint32_t queue)
845 {
846 	int socket;
847 	uint32_t nb_mbuf;
848 	uint64_t frag_cycles;
849 	char buf[RTE_MEMPOOL_NAMESIZE];
850 
851 	socket = rte_lcore_to_socket_id(lcore);
852 	if (socket == SOCKET_ID_ANY)
853 		socket = 0;
854 
855 	frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S *
856 		max_flow_ttl;
857 
858 	if ((rxq->frag_tbl = rte_ip_frag_table_create(max_flow_num,
859 			IP_FRAG_TBL_BUCKET_ENTRIES, max_flow_num, frag_cycles,
860 			socket)) == NULL) {
861 		RTE_LOG(ERR, IP_RSMBL, "ip_frag_tbl_create(%u) on "
862 			"lcore: %u for queue: %u failed\n",
863 			max_flow_num, lcore, queue);
864 		return -1;
865 	}
866 
867 	/*
868 	 * At any given moment up to <max_flow_num * (MAX_FRAG_NUM)>
869 	 * mbufs could be stored int the fragment table.
870 	 * Plus, each TX queue can hold up to <max_flow_num> packets.
871 	 */
872 
873 	nb_mbuf = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST) * MAX_FRAG_NUM;
874 	nb_mbuf *= (port_conf.rxmode.max_rx_pkt_len + BUF_SIZE - 1) / BUF_SIZE;
875 	nb_mbuf *= 2; /* ipv4 and ipv6 */
876 	nb_mbuf += nb_rxd + nb_txd;
877 
878 	nb_mbuf = RTE_MAX(nb_mbuf, (uint32_t)NB_MBUF);
879 
880 	snprintf(buf, sizeof(buf), "mbuf_pool_%u_%u", lcore, queue);
881 
882 	rxq->pool = rte_pktmbuf_pool_create(buf, nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
883 					    MBUF_DATA_SIZE, socket);
884 	if (rxq->pool == NULL) {
885 		RTE_LOG(ERR, IP_RSMBL,
886 			"rte_pktmbuf_pool_create(%s) failed", buf);
887 		return -1;
888 	}
889 
890 	return 0;
891 }
892 
893 static int
894 init_mem(void)
895 {
896 	char buf[PATH_MAX];
897 	struct rte_lpm *lpm;
898 	struct rte_lpm6 *lpm6;
899 	struct rte_lpm_config lpm_config;
900 	int socket;
901 	unsigned lcore_id;
902 
903 	/* traverse through lcores and initialize structures on each socket */
904 
905 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
906 
907 		if (rte_lcore_is_enabled(lcore_id) == 0)
908 			continue;
909 
910 		socket = rte_lcore_to_socket_id(lcore_id);
911 
912 		if (socket == SOCKET_ID_ANY)
913 			socket = 0;
914 
915 		if (socket_lpm[socket] == NULL) {
916 			RTE_LOG(INFO, IP_RSMBL, "Creating LPM table on socket %i\n", socket);
917 			snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
918 
919 			lpm_config.max_rules = LPM_MAX_RULES;
920 			lpm_config.number_tbl8s = 256;
921 			lpm_config.flags = 0;
922 
923 			lpm = rte_lpm_create(buf, socket, &lpm_config);
924 			if (lpm == NULL) {
925 				RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
926 				return -1;
927 			}
928 			socket_lpm[socket] = lpm;
929 		}
930 
931 		if (socket_lpm6[socket] == NULL) {
932 			RTE_LOG(INFO, IP_RSMBL, "Creating LPM6 table on socket %i\n", socket);
933 			snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
934 
935 			lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
936 			if (lpm6 == NULL) {
937 				RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
938 				return -1;
939 			}
940 			socket_lpm6[socket] = lpm6;
941 		}
942 	}
943 
944 	return 0;
945 }
946 
947 static void
948 queue_dump_stat(void)
949 {
950 	uint32_t i, lcore;
951 	const struct lcore_queue_conf *qconf;
952 
953 	for (lcore = 0; lcore < RTE_MAX_LCORE; lcore++) {
954 		if (rte_lcore_is_enabled(lcore) == 0)
955 			continue;
956 
957 		qconf = &lcore_queue_conf[lcore];
958 		for (i = 0; i < qconf->n_rx_queue; i++) {
959 
960 			fprintf(stdout, " -- lcoreid=%u portid=%u "
961 				"frag tbl stat:\n",
962 				lcore,  qconf->rx_queue_list[i].portid);
963 			rte_ip_frag_table_statistics_dump(stdout,
964 					qconf->rx_queue_list[i].frag_tbl);
965 			fprintf(stdout, "TX bursts:\t%" PRIu64 "\n"
966 				"TX packets _queued:\t%" PRIu64 "\n"
967 				"TX packets dropped:\t%" PRIu64 "\n"
968 				"TX packets send:\t%" PRIu64 "\n",
969 				qconf->tx_stat.call,
970 				qconf->tx_stat.queue,
971 				qconf->tx_stat.drop,
972 				qconf->tx_stat.send);
973 		}
974 	}
975 }
976 
977 static void
978 signal_handler(int signum)
979 {
980 	queue_dump_stat();
981 	if (signum != SIGUSR1)
982 		rte_exit(0, "received signal: %d, exiting\n", signum);
983 }
984 
985 int
986 main(int argc, char **argv)
987 {
988 	struct lcore_queue_conf *qconf;
989 	struct rte_eth_dev_info dev_info;
990 	struct rte_eth_txconf *txconf;
991 	struct rx_queue *rxq;
992 	int ret, socket;
993 	unsigned nb_ports;
994 	uint16_t queueid;
995 	unsigned lcore_id = 0, rx_lcore_id = 0;
996 	uint32_t n_tx_queue, nb_lcores;
997 	uint16_t portid;
998 
999 	/* init EAL */
1000 	ret = rte_eal_init(argc, argv);
1001 	if (ret < 0)
1002 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1003 	argc -= ret;
1004 	argv += ret;
1005 
1006 	/* parse application arguments (after the EAL ones) */
1007 	ret = parse_args(argc, argv);
1008 	if (ret < 0)
1009 		rte_exit(EXIT_FAILURE, "Invalid IP reassembly parameters\n");
1010 
1011 	nb_ports = rte_eth_dev_count();
1012 	if (nb_ports == 0)
1013 		rte_exit(EXIT_FAILURE, "No ports found!\n");
1014 
1015 	nb_lcores = rte_lcore_count();
1016 
1017 	/* initialize structures (mempools, lpm etc.) */
1018 	if (init_mem() < 0)
1019 		rte_panic("Cannot initialize memory structures!\n");
1020 
1021 	/* check if portmask has non-existent ports */
1022 	if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
1023 		rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
1024 
1025 	/* initialize all ports */
1026 	for (portid = 0; portid < nb_ports; portid++) {
1027 		struct rte_eth_rxconf rxq_conf;
1028 		struct rte_eth_conf local_port_conf = port_conf;
1029 
1030 		/* skip ports that are not enabled */
1031 		if ((enabled_port_mask & (1 << portid)) == 0) {
1032 			printf("\nSkipping disabled port %d\n", portid);
1033 			continue;
1034 		}
1035 
1036 		qconf = &lcore_queue_conf[rx_lcore_id];
1037 
1038 		/* limit the frame size to the maximum supported by NIC */
1039 		rte_eth_dev_info_get(portid, &dev_info);
1040 		local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
1041 		    dev_info.max_rx_pktlen,
1042 		    local_port_conf.rxmode.max_rx_pkt_len);
1043 
1044 		/* get the lcore_id for this port */
1045 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1046 			   qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
1047 
1048 			rx_lcore_id++;
1049 			if (rx_lcore_id >= RTE_MAX_LCORE)
1050 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
1051 
1052 			qconf = &lcore_queue_conf[rx_lcore_id];
1053 		}
1054 
1055 		socket = rte_lcore_to_socket_id(portid);
1056 		if (socket == SOCKET_ID_ANY)
1057 			socket = 0;
1058 
1059 		queueid = qconf->n_rx_queue;
1060 		rxq = &qconf->rx_queue_list[queueid];
1061 		rxq->portid = portid;
1062 		rxq->lpm = socket_lpm[socket];
1063 		rxq->lpm6 = socket_lpm6[socket];
1064 
1065 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1066 						       &nb_txd);
1067 		if (ret < 0)
1068 			rte_exit(EXIT_FAILURE,
1069 				 "Cannot adjust number of descriptors: err=%d, port=%d\n",
1070 				 ret, portid);
1071 
1072 		if (setup_queue_tbl(rxq, rx_lcore_id, queueid) < 0)
1073 			rte_exit(EXIT_FAILURE, "Failed to set up queue table\n");
1074 		qconf->n_rx_queue++;
1075 
1076 		/* init port */
1077 		printf("Initializing port %d ... ", portid );
1078 		fflush(stdout);
1079 
1080 		n_tx_queue = nb_lcores;
1081 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1082 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1083 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1084 			local_port_conf.txmode.offloads |=
1085 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1086 		ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
1087 					    &local_port_conf);
1088 		if (ret < 0) {
1089 			printf("\n");
1090 			rte_exit(EXIT_FAILURE, "Cannot configure device: "
1091 				"err=%d, port=%d\n",
1092 				ret, portid);
1093 		}
1094 
1095 		/* init one RX queue */
1096 		rxq_conf = dev_info.default_rxconf;
1097 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
1098 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1099 					     socket, &rxq_conf,
1100 					     rxq->pool);
1101 		if (ret < 0) {
1102 			printf("\n");
1103 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
1104 				"err=%d, port=%d\n",
1105 				ret, portid);
1106 		}
1107 
1108 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1109 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
1110 		printf("\n");
1111 
1112 		/* init one TX queue per couple (lcore,port) */
1113 		queueid = 0;
1114 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1115 			if (rte_lcore_is_enabled(lcore_id) == 0)
1116 				continue;
1117 
1118 			socket = (int) rte_lcore_to_socket_id(lcore_id);
1119 
1120 			printf("txq=%u,%d,%d ", lcore_id, queueid, socket);
1121 			fflush(stdout);
1122 
1123 			txconf = &dev_info.default_txconf;
1124 			txconf->txq_flags = ETH_TXQ_FLAGS_IGNORE;
1125 			txconf->offloads = local_port_conf.txmode.offloads;
1126 
1127 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1128 					socket, txconf);
1129 			if (ret < 0)
1130 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1131 					"port=%d\n", ret, portid);
1132 
1133 			qconf = &lcore_queue_conf[lcore_id];
1134 			qconf->tx_queue_id[portid] = queueid;
1135 			setup_port_tbl(qconf, lcore_id, socket, portid);
1136 			queueid++;
1137 		}
1138 		printf("\n");
1139 	}
1140 
1141 	printf("\n");
1142 
1143 	/* start ports */
1144 	for (portid = 0; portid < nb_ports; portid++) {
1145 		if ((enabled_port_mask & (1 << portid)) == 0) {
1146 			continue;
1147 		}
1148 		/* Start device */
1149 		ret = rte_eth_dev_start(portid);
1150 		if (ret < 0)
1151 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1152 				ret, portid);
1153 
1154 		rte_eth_promiscuous_enable(portid);
1155 	}
1156 
1157 	if (init_routing_table() < 0)
1158 		rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1159 
1160 	check_all_ports_link_status(nb_ports, enabled_port_mask);
1161 
1162 	signal(SIGUSR1, signal_handler);
1163 	signal(SIGTERM, signal_handler);
1164 	signal(SIGINT, signal_handler);
1165 
1166 	/* launch per-lcore init on every lcore */
1167 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1168 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1169 		if (rte_eal_wait_lcore(lcore_id) < 0)
1170 			return -1;
1171 	}
1172 
1173 	return 0;
1174 }
1175