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