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