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