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