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