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