xref: /dpdk/examples/ipv4_multicast/main.c (revision 1ad89e0d0883b95ba1f52fef4eb98deebfbaaff5)
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 
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.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_pci.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_ring.h>
67 #include <rte_mempool.h>
68 #include <rte_mbuf.h>
69 #include <rte_malloc.h>
70 #include <rte_fbk_hash.h>
71 #include <rte_ip.h>
72 
73 #define RTE_LOGTYPE_IPv4_MULTICAST RTE_LOGTYPE_USER1
74 
75 #define MAX_PORTS 16
76 
77 #define	MCAST_CLONE_PORTS	2
78 #define	MCAST_CLONE_SEGS	2
79 
80 #define	PKT_MBUF_SIZE	(2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
81 #define	NB_PKT_MBUF	8192
82 
83 #define	HDR_MBUF_SIZE	(sizeof(struct rte_mbuf) + 2 * RTE_PKTMBUF_HEADROOM)
84 #define	NB_HDR_MBUF	(NB_PKT_MBUF * MAX_PORTS)
85 
86 #define	CLONE_MBUF_SIZE	(sizeof(struct rte_mbuf))
87 #define	NB_CLONE_MBUF	(NB_PKT_MBUF * MCAST_CLONE_PORTS * MCAST_CLONE_SEGS * 2)
88 
89 /* allow max jumbo frame 9.5 KB */
90 #define	JUMBO_FRAME_MAX_SIZE	0x2600
91 
92 #define MAX_PKT_BURST 32
93 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
94 
95 /* Configure how many packets ahead to prefetch, when reading packets */
96 #define PREFETCH_OFFSET	3
97 
98 /*
99  * Construct Ethernet multicast address from IPv4 multicast address.
100  * Citing RFC 1112, section 6.4:
101  * "An IP host group address is mapped to an Ethernet multicast address
102  * by placing the low-order 23-bits of the IP address into the low-order
103  * 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex)."
104  */
105 #define	ETHER_ADDR_FOR_IPV4_MCAST(x)	\
106 	(rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16)
107 
108 /*
109  * Configurable number of RX/TX ring descriptors
110  */
111 #define RTE_TEST_RX_DESC_DEFAULT 128
112 #define RTE_TEST_TX_DESC_DEFAULT 512
113 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
114 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
115 
116 /* ethernet addresses of ports */
117 static struct ether_addr ports_eth_addr[MAX_PORTS];
118 
119 /* mask of enabled ports */
120 static uint32_t enabled_port_mask = 0;
121 
122 static uint8_t nb_ports = 0;
123 
124 static int rx_queue_per_lcore = 1;
125 
126 struct mbuf_table {
127 	uint16_t len;
128 	struct rte_mbuf *m_table[MAX_PKT_BURST];
129 };
130 
131 #define MAX_RX_QUEUE_PER_LCORE 16
132 #define MAX_TX_QUEUE_PER_PORT 16
133 struct lcore_queue_conf {
134 	uint64_t tx_tsc;
135 	uint16_t n_rx_queue;
136 	uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
137 	uint16_t tx_queue_id[MAX_PORTS];
138 	struct mbuf_table tx_mbufs[MAX_PORTS];
139 } __rte_cache_aligned;
140 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
141 
142 static const struct rte_eth_conf port_conf = {
143 	.rxmode = {
144 		.max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
145 		.split_hdr_size = 0,
146 		.header_split   = 0, /**< Header Split disabled */
147 		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
148 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
149 		.jumbo_frame    = 1, /**< Jumbo Frame Support enabled */
150 		.hw_strip_crc   = 0, /**< CRC stripped by hardware */
151 	},
152 	.txmode = {
153 		.mq_mode = ETH_MQ_TX_NONE,
154 	},
155 };
156 
157 static struct rte_mempool *packet_pool, *header_pool, *clone_pool;
158 
159 
160 /* Multicast */
161 static struct rte_fbk_hash_params mcast_hash_params = {
162 	.name = "MCAST_HASH",
163 	.entries = 1024,
164 	.entries_per_bucket = 4,
165 	.socket_id = 0,
166 	.hash_func = NULL,
167 	.init_val = 0,
168 };
169 
170 struct rte_fbk_hash_table *mcast_hash = NULL;
171 
172 struct mcast_group_params {
173 	uint32_t ip;
174 	uint16_t port_mask;
175 };
176 
177 static struct mcast_group_params mcast_group_table[] = {
178 		{IPv4(224,0,0,101), 0x1},
179 		{IPv4(224,0,0,102), 0x2},
180 		{IPv4(224,0,0,103), 0x3},
181 		{IPv4(224,0,0,104), 0x4},
182 		{IPv4(224,0,0,105), 0x5},
183 		{IPv4(224,0,0,106), 0x6},
184 		{IPv4(224,0,0,107), 0x7},
185 		{IPv4(224,0,0,108), 0x8},
186 		{IPv4(224,0,0,109), 0x9},
187 		{IPv4(224,0,0,110), 0xA},
188 		{IPv4(224,0,0,111), 0xB},
189 		{IPv4(224,0,0,112), 0xC},
190 		{IPv4(224,0,0,113), 0xD},
191 		{IPv4(224,0,0,114), 0xE},
192 		{IPv4(224,0,0,115), 0xF},
193 };
194 
195 #define N_MCAST_GROUPS \
196 	(sizeof (mcast_group_table) / sizeof (mcast_group_table[0]))
197 
198 
199 /* Send burst of packets on an output interface */
200 static void
201 send_burst(struct lcore_queue_conf *qconf, uint8_t port)
202 {
203 	struct rte_mbuf **m_table;
204 	uint16_t n, queueid;
205 	int ret;
206 
207 	queueid = qconf->tx_queue_id[port];
208 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
209 	n = qconf->tx_mbufs[port].len;
210 
211 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
212 	while (unlikely (ret < n)) {
213 		rte_pktmbuf_free(m_table[ret]);
214 		ret++;
215 	}
216 
217 	qconf->tx_mbufs[port].len = 0;
218 }
219 
220 /* Get number of bits set. */
221 static inline uint32_t
222 bitcnt(uint32_t v)
223 {
224 	uint32_t n;
225 
226 	for (n = 0; v != 0; v &= v - 1, n++)
227 		;
228 
229 	return (n);
230 }
231 
232 /**
233  * Create the output multicast packet based on the given input packet.
234  * There are two approaches for creating outgoing packet, though both
235  * are based on data zero-copy idea, they differ in few details:
236  * First one creates a clone of the input packet, e.g - walk though all
237  * segments of the input packet, and for each of them create a new packet
238  * mbuf and attach that new mbuf to the segment (refer to rte_pktmbuf_clone()
239  * for more details). Then new mbuf is allocated for the packet header
240  * and is prepended to the 'clone' mbuf.
241  * Second approach doesn't make a clone, it just increment refcnt for all
242  * input packet segments. Then it allocates new mbuf for the packet header
243  * and prepends it to the input packet.
244  * Basically first approach reuses only input packet's data, but creates
245  * it's own copy of packet's metadata. Second approach reuses both input's
246  * packet data and metadata.
247  * The advantage of first approach - is that each outgoing packet has it's
248  * own copy of metadata, so we can safely modify data pointer of the
249  * input packet. That allows us to skip creation if the output packet for
250  * the last destination port, but instead modify input packet's header inplace,
251  * e.g: for N destination ports we need to invoke mcast_out_pkt (N-1) times.
252  * The advantage of second approach - less work for each outgoing packet,
253  * e.g: we skip "clone" operation completely. Though it comes with a price -
254  * input packet's metadata has to be intact. So for N destination ports we
255  * need to invoke mcast_out_pkt N times.
256  * So for small number of outgoing ports (and segments in the input packet)
257  * first approach will be faster.
258  * As number of outgoing ports (and/or input segments) will grow,
259  * second way will become more preferable.
260  *
261  *  @param pkt
262  *  Input packet mbuf.
263  *  @param use_clone
264  *  Control which of the two approaches described above should be used:
265  *  - 0 - use second approach:
266  *    Don't "clone" input packet.
267  *    Prepend new header directly to the input packet
268  *  - 1 - use first approach:
269  *    Make a "clone" of input packet first.
270  *    Prepend new header to the clone of the input packet
271  *  @return
272  *  - The pointer to the new outgoing packet.
273  *  - NULL if operation failed.
274  */
275 static inline struct rte_mbuf *
276 mcast_out_pkt(struct rte_mbuf *pkt, int use_clone)
277 {
278 	struct rte_mbuf *hdr;
279 
280 	/* Create new mbuf for the header. */
281 	if (unlikely ((hdr = rte_pktmbuf_alloc(header_pool)) == NULL))
282 		return (NULL);
283 
284 	/* If requested, then make a new clone packet. */
285 	if (use_clone != 0 &&
286 	    unlikely ((pkt = rte_pktmbuf_clone(pkt, clone_pool)) == NULL)) {
287 		rte_pktmbuf_free(hdr);
288 		return (NULL);
289 	}
290 
291 	/* prepend new header */
292 	hdr->next = pkt;
293 
294 
295 	/* update header's fields */
296 	hdr->pkt_len = (uint16_t)(hdr->data_len + pkt->pkt_len);
297 	hdr->nb_segs = (uint8_t)(pkt->nb_segs + 1);
298 
299 	/* copy metadata from source packet*/
300 	hdr->port = pkt->port;
301 	hdr->vlan_tci = pkt->vlan_tci;
302 	hdr->tx_offload = pkt->tx_offload;
303 	hdr->hash = pkt->hash;
304 
305 	hdr->ol_flags = pkt->ol_flags;
306 
307 	__rte_mbuf_sanity_check(hdr, 1);
308 	return (hdr);
309 }
310 
311 /*
312  * Write new Ethernet header to the outgoing packet,
313  * and put it into the outgoing queue for the given port.
314  */
315 static inline void
316 mcast_send_pkt(struct rte_mbuf *pkt, struct ether_addr *dest_addr,
317 		struct lcore_queue_conf *qconf, uint8_t port)
318 {
319 	struct ether_hdr *ethdr;
320 	uint16_t len;
321 
322 	/* Construct Ethernet header. */
323 	ethdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(*ethdr));
324 	RTE_MBUF_ASSERT(ethdr != NULL);
325 
326 	ether_addr_copy(dest_addr, &ethdr->d_addr);
327 	ether_addr_copy(&ports_eth_addr[port], &ethdr->s_addr);
328 	ethdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
329 
330 	/* Put new packet into the output queue */
331 	len = qconf->tx_mbufs[port].len;
332 	qconf->tx_mbufs[port].m_table[len] = pkt;
333 	qconf->tx_mbufs[port].len = ++len;
334 
335 	/* Transmit packets */
336 	if (unlikely(MAX_PKT_BURST == len))
337 		send_burst(qconf, port);
338 }
339 
340 /* Multicast forward of the input packet */
341 static inline void
342 mcast_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf)
343 {
344 	struct rte_mbuf *mc;
345 	struct ipv4_hdr *iphdr;
346 	uint32_t dest_addr, port_mask, port_num, use_clone;
347 	int32_t hash;
348 	uint8_t port;
349 	union {
350 		uint64_t as_int;
351 		struct ether_addr as_addr;
352 	} dst_eth_addr;
353 
354 	/* Remove the Ethernet header from the input packet */
355 	iphdr = (struct ipv4_hdr *)rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
356 	RTE_MBUF_ASSERT(iphdr != NULL);
357 
358 	dest_addr = rte_be_to_cpu_32(iphdr->dst_addr);
359 
360 	/*
361 	 * Check that it is a valid multicast address and
362 	 * we have some active ports assigned to it.
363 	 */
364 	if(!IS_IPV4_MCAST(dest_addr) ||
365 	    (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 ||
366 	    (port_mask = hash & enabled_port_mask) == 0) {
367 		rte_pktmbuf_free(m);
368 		return;
369 	}
370 
371 	/* Calculate number of destination ports. */
372 	port_num = bitcnt(port_mask);
373 
374 	/* Should we use rte_pktmbuf_clone() or not. */
375 	use_clone = (port_num <= MCAST_CLONE_PORTS &&
376 	    m->nb_segs <= MCAST_CLONE_SEGS);
377 
378 	/* Mark all packet's segments as referenced port_num times */
379 	if (use_clone == 0)
380 		rte_pktmbuf_refcnt_update(m, (uint16_t)port_num);
381 
382 	/* construct destination ethernet address */
383 	dst_eth_addr.as_int = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr);
384 
385 	for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) {
386 
387 		/* Prepare output packet and send it out. */
388 		if ((port_mask & 1) != 0) {
389 			if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL))
390 				mcast_send_pkt(mc, &dst_eth_addr.as_addr,
391 						qconf, port);
392 			else if (use_clone == 0)
393 				rte_pktmbuf_free(m);
394 		}
395 	}
396 
397 	/*
398 	 * If we making clone packets, then, for the last destination port,
399 	 * we can overwrite input packet's metadata.
400 	 */
401 	if (use_clone != 0)
402 		mcast_send_pkt(m, &dst_eth_addr.as_addr, qconf, port);
403 	else
404 		rte_pktmbuf_free(m);
405 }
406 
407 /* Send burst of outgoing packet, if timeout expires. */
408 static inline void
409 send_timeout_burst(struct lcore_queue_conf *qconf)
410 {
411 	uint64_t cur_tsc;
412 	uint8_t portid;
413 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
414 
415 	cur_tsc = rte_rdtsc();
416 	if (likely (cur_tsc < qconf->tx_tsc + drain_tsc))
417 		return;
418 
419 	for (portid = 0; portid < MAX_PORTS; portid++) {
420 		if (qconf->tx_mbufs[portid].len != 0)
421 			send_burst(qconf, portid);
422 	}
423 	qconf->tx_tsc = cur_tsc;
424 }
425 
426 /* main processing loop */
427 static int
428 main_loop(__rte_unused void *dummy)
429 {
430 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
431 	unsigned lcore_id;
432 	int i, j, nb_rx;
433 	uint8_t portid;
434 	struct lcore_queue_conf *qconf;
435 
436 	lcore_id = rte_lcore_id();
437 	qconf = &lcore_queue_conf[lcore_id];
438 
439 
440 	if (qconf->n_rx_queue == 0) {
441 		RTE_LOG(INFO, IPv4_MULTICAST, "lcore %u has nothing to do\n",
442 		    lcore_id);
443 		return 0;
444 	}
445 
446 	RTE_LOG(INFO, IPv4_MULTICAST, "entering main loop on lcore %u\n",
447 	    lcore_id);
448 
449 	for (i = 0; i < qconf->n_rx_queue; i++) {
450 
451 		portid = qconf->rx_queue_list[i];
452 		RTE_LOG(INFO, IPv4_MULTICAST, " -- lcoreid=%u portid=%d\n",
453 		    lcore_id, (int) portid);
454 	}
455 
456 	while (1) {
457 
458 		/*
459 		 * Read packet from RX queues
460 		 */
461 		for (i = 0; i < qconf->n_rx_queue; i++) {
462 
463 			portid = qconf->rx_queue_list[i];
464 			nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
465 						 MAX_PKT_BURST);
466 
467 			/* Prefetch first packets */
468 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
469 				rte_prefetch0(rte_pktmbuf_mtod(
470 						pkts_burst[j], void *));
471 			}
472 
473 			/* Prefetch and forward already prefetched packets */
474 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
475 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
476 						j + PREFETCH_OFFSET], void *));
477 				mcast_forward(pkts_burst[j], qconf);
478 			}
479 
480 			/* Forward remaining prefetched packets */
481 			for (; j < nb_rx; j++) {
482 				mcast_forward(pkts_burst[j], qconf);
483 			}
484 		}
485 
486 		/* Send out packets from TX queues */
487 		send_timeout_burst(qconf);
488 	}
489 }
490 
491 /* display usage */
492 static void
493 print_usage(const char *prgname)
494 {
495 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
496 	    "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
497 	    "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
498 	    prgname);
499 }
500 
501 static uint32_t
502 parse_portmask(const char *portmask)
503 {
504 	char *end = NULL;
505 	unsigned long pm;
506 
507 	/* parse hexadecimal string */
508 	pm = strtoul(portmask, &end, 16);
509 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
510 		return 0;
511 
512 	return ((uint32_t)pm);
513 }
514 
515 static int
516 parse_nqueue(const char *q_arg)
517 {
518 	char *end = NULL;
519 	unsigned long n;
520 
521 	/* parse numerical string */
522 	errno = 0;
523 	n = strtoul(q_arg, &end, 0);
524 	if (errno != 0 || end == NULL || *end != '\0' ||
525 			n == 0 || n >= MAX_RX_QUEUE_PER_LCORE)
526 		return (-1);
527 
528 	return (n);
529 }
530 
531 /* Parse the argument given in the command line of the application */
532 static int
533 parse_args(int argc, char **argv)
534 {
535 	int opt, ret;
536 	char **argvopt;
537 	int option_index;
538 	char *prgname = argv[0];
539 	static struct option lgopts[] = {
540 		{NULL, 0, 0, 0}
541 	};
542 
543 	argvopt = argv;
544 
545 	while ((opt = getopt_long(argc, argvopt, "p:q:",
546 				  lgopts, &option_index)) != EOF) {
547 
548 		switch (opt) {
549 		/* portmask */
550 		case 'p':
551 			enabled_port_mask = parse_portmask(optarg);
552 			if (enabled_port_mask == 0) {
553 				printf("invalid portmask\n");
554 				print_usage(prgname);
555 				return -1;
556 			}
557 			break;
558 
559 		/* nqueue */
560 		case 'q':
561 			rx_queue_per_lcore = parse_nqueue(optarg);
562 			if (rx_queue_per_lcore < 0) {
563 				printf("invalid queue number\n");
564 				print_usage(prgname);
565 				return -1;
566 			}
567 			break;
568 
569 		default:
570 			print_usage(prgname);
571 			return -1;
572 		}
573 	}
574 
575 	if (optind >= 0)
576 		argv[optind-1] = prgname;
577 
578 	ret = optind-1;
579 	optind = 0; /* reset getopt lib */
580 	return ret;
581 }
582 
583 static void
584 print_ethaddr(const char *name, struct ether_addr *eth_addr)
585 {
586 	char buf[ETHER_ADDR_FMT_SIZE];
587 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
588 	printf("%s%s", name, buf);
589 }
590 
591 static int
592 init_mcast_hash(void)
593 {
594 	uint32_t i;
595 
596 	mcast_hash_params.socket_id = rte_socket_id();
597 	mcast_hash = rte_fbk_hash_create(&mcast_hash_params);
598 	if (mcast_hash == NULL){
599 		return -1;
600 	}
601 
602 	for (i = 0; i < N_MCAST_GROUPS; i ++){
603 		if (rte_fbk_hash_add_key(mcast_hash,
604 			mcast_group_table[i].ip,
605 			mcast_group_table[i].port_mask) < 0) {
606 			return -1;
607 		}
608 	}
609 
610 	return 0;
611 }
612 
613 /* Check the link status of all ports in up to 9s, and print them finally */
614 static void
615 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
616 {
617 #define CHECK_INTERVAL 100 /* 100ms */
618 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
619 	uint8_t portid, count, all_ports_up, print_flag = 0;
620 	struct rte_eth_link link;
621 
622 	printf("\nChecking link status");
623 	fflush(stdout);
624 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
625 		all_ports_up = 1;
626 		for (portid = 0; portid < port_num; portid++) {
627 			if ((port_mask & (1 << portid)) == 0)
628 				continue;
629 			memset(&link, 0, sizeof(link));
630 			rte_eth_link_get_nowait(portid, &link);
631 			/* print link status if flag set */
632 			if (print_flag == 1) {
633 				if (link.link_status)
634 					printf("Port %d Link Up - speed %u "
635 						"Mbps - %s\n", (uint8_t)portid,
636 						(unsigned)link.link_speed,
637 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
638 					("full-duplex") : ("half-duplex\n"));
639 				else
640 					printf("Port %d Link Down\n",
641 							(uint8_t)portid);
642 				continue;
643 			}
644 			/* clear all_ports_up flag if any link down */
645 			if (link.link_status == 0) {
646 				all_ports_up = 0;
647 				break;
648 			}
649 		}
650 		/* after finally printing all link status, get out */
651 		if (print_flag == 1)
652 			break;
653 
654 		if (all_ports_up == 0) {
655 			printf(".");
656 			fflush(stdout);
657 			rte_delay_ms(CHECK_INTERVAL);
658 		}
659 
660 		/* set the print_flag if all ports up or timeout */
661 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
662 			print_flag = 1;
663 			printf("done\n");
664 		}
665 	}
666 }
667 
668 int
669 main(int argc, char **argv)
670 {
671 	struct lcore_queue_conf *qconf;
672 	struct rte_eth_dev_info dev_info;
673 	struct rte_eth_txconf *txconf;
674 	int ret;
675 	uint16_t queueid;
676 	unsigned lcore_id = 0, rx_lcore_id = 0;
677 	uint32_t n_tx_queue, nb_lcores;
678 	uint8_t portid;
679 
680 	/* init EAL */
681 	ret = rte_eal_init(argc, argv);
682 	if (ret < 0)
683 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
684 	argc -= ret;
685 	argv += ret;
686 
687 	/* parse application arguments (after the EAL ones) */
688 	ret = parse_args(argc, argv);
689 	if (ret < 0)
690 		rte_exit(EXIT_FAILURE, "Invalid IPV4_MULTICAST parameters\n");
691 
692 	/* create the mbuf pools */
693 	packet_pool = rte_mempool_create("packet_pool", NB_PKT_MBUF,
694 	    PKT_MBUF_SIZE, 32, sizeof(struct rte_pktmbuf_pool_private),
695 	    rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL,
696 	    rte_socket_id(), 0);
697 
698 	if (packet_pool == NULL)
699 		rte_exit(EXIT_FAILURE, "Cannot init packet mbuf pool\n");
700 
701 	header_pool = rte_mempool_create("header_pool", NB_HDR_MBUF,
702 	    HDR_MBUF_SIZE, 32, 0, NULL, NULL, rte_pktmbuf_init, NULL,
703 	    rte_socket_id(), 0);
704 
705 	if (header_pool == NULL)
706 		rte_exit(EXIT_FAILURE, "Cannot init header mbuf pool\n");
707 
708 	clone_pool = rte_mempool_create("clone_pool", NB_CLONE_MBUF,
709 	    CLONE_MBUF_SIZE, 32, 0, NULL, NULL, rte_pktmbuf_init, NULL,
710 	    rte_socket_id(), 0);
711 
712 	if (clone_pool == NULL)
713 		rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n");
714 
715 	nb_ports = rte_eth_dev_count();
716 	if (nb_ports == 0)
717 		rte_exit(EXIT_FAILURE, "No physical ports!\n");
718 	if (nb_ports > MAX_PORTS)
719 		nb_ports = MAX_PORTS;
720 
721 	nb_lcores = rte_lcore_count();
722 
723 	/* initialize all ports */
724 	for (portid = 0; portid < nb_ports; portid++) {
725 		/* skip ports that are not enabled */
726 		if ((enabled_port_mask & (1 << portid)) == 0) {
727 			printf("Skipping disabled port %d\n", portid);
728 			continue;
729 		}
730 
731 		qconf = &lcore_queue_conf[rx_lcore_id];
732 
733 		/* get the lcore_id for this port */
734 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
735 		       qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
736 
737 			rx_lcore_id ++;
738 			qconf = &lcore_queue_conf[rx_lcore_id];
739 
740 			if (rx_lcore_id >= RTE_MAX_LCORE)
741 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
742 		}
743 		qconf->rx_queue_list[qconf->n_rx_queue] = portid;
744 		qconf->n_rx_queue++;
745 
746 		/* init port */
747 		printf("Initializing port %d on lcore %u... ", portid,
748 		       rx_lcore_id);
749 		fflush(stdout);
750 
751 		n_tx_queue = nb_lcores;
752 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
753 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
754 		ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
755 					    &port_conf);
756 		if (ret < 0)
757 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
758 				  ret, portid);
759 
760 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
761 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
762 		printf(", ");
763 
764 		/* init one RX queue */
765 		queueid = 0;
766 		printf("rxq=%hu ", queueid);
767 		fflush(stdout);
768 		ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
769 					     rte_eth_dev_socket_id(portid),
770 					     NULL,
771 					     packet_pool);
772 		if (ret < 0)
773 			rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, port=%d\n",
774 				  ret, portid);
775 
776 		/* init one TX queue per couple (lcore,port) */
777 		queueid = 0;
778 
779 		RTE_LCORE_FOREACH(lcore_id) {
780 			if (rte_lcore_is_enabled(lcore_id) == 0)
781 				continue;
782 			printf("txq=%u,%hu ", lcore_id, queueid);
783 			fflush(stdout);
784 
785 			rte_eth_dev_info_get(portid, &dev_info);
786 			txconf = &dev_info.default_txconf;
787 			txconf->txq_flags = 0;
788 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
789 						     rte_lcore_to_socket_id(lcore_id), txconf);
790 			if (ret < 0)
791 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
792 					  "port=%d\n", ret, portid);
793 
794 			qconf = &lcore_queue_conf[lcore_id];
795 			qconf->tx_queue_id[portid] = queueid;
796 			queueid++;
797 		}
798 
799 		/* Start device */
800 		ret = rte_eth_dev_start(portid);
801 		if (ret < 0)
802 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
803 				  ret, portid);
804 
805 		printf("done:\n");
806 	}
807 
808 	check_all_ports_link_status(nb_ports, enabled_port_mask);
809 
810 	/* initialize the multicast hash */
811 	int retval = init_mcast_hash();
812 	if (retval != 0)
813 		rte_exit(EXIT_FAILURE, "Cannot build the multicast hash\n");
814 
815 	/* launch per-lcore init on every lcore */
816 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
817 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
818 		if (rte_eal_wait_lcore(lcore_id) < 0)
819 			return -1;
820 	}
821 
822 	return 0;
823 }
824