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