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