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