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