ethdev: add DCB support
[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  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <sys/queue.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <getopt.h>
45
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_tailq.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_ring.h>
69 #include <rte_mempool.h>
70 #include <rte_mbuf.h>
71 #include <rte_malloc.h>
72 #include <rte_hash_crc.h>
73 #include <rte_fbk_hash.h>
74 #include <rte_ip.h>
75
76 #include "main.h"
77
78 #define RTE_LOGTYPE_IPv4_MULTICAST RTE_LOGTYPE_USER1
79
80 #define MAX_PORTS 16
81
82 #define MCAST_CLONE_PORTS       2
83 #define MCAST_CLONE_SEGS        2
84
85 #define PKT_MBUF_SIZE   (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
86 #define NB_PKT_MBUF     8192
87
88 #define HDR_MBUF_SIZE   (sizeof(struct rte_mbuf) + 2 * RTE_PKTMBUF_HEADROOM)
89 #define NB_HDR_MBUF     (NB_PKT_MBUF * MAX_PORTS)
90
91 #define CLONE_MBUF_SIZE (sizeof(struct rte_mbuf))
92 #define NB_CLONE_MBUF   (NB_PKT_MBUF * MCAST_CLONE_PORTS * MCAST_CLONE_SEGS * 2)
93
94 /* allow max jumbo frame 9.5 KB */
95 #define JUMBO_FRAME_MAX_SIZE    0x2600
96
97 /*
98  * RX and TX Prefetch, Host, and Write-back threshold values should be
99  * carefully set for optimal performance. Consult the network
100  * controller's datasheet and supporting DPDK documentation for guidance
101  * on how these parameters should be set.
102  */
103 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
104 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
105 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
106
107 /*
108  * These default values are optimized for use with the Intel(R) 82599 10 GbE
109  * Controller and the DPDK ixgbe PMD. Consider using other values for other
110  * network controllers and/or network drivers.
111  */
112 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
113 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
114 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
115
116 #define MAX_PKT_BURST 32
117 #define BURST_TX_DRAIN 200000ULL /* around 100us at 2 Ghz */
118
119 #define SOCKET0 0
120
121 /* Configure how many packets ahead to prefetch, when reading packets */
122 #define PREFETCH_OFFSET 3
123
124 /*
125  * Construct Ethernet multicast address from IPv4 multicast address.
126  * Citing RFC 1112, section 6.4:
127  * "An IP host group address is mapped to an Ethernet multicast address
128  * by placing the low-order 23-bits of the IP address into the low-order
129  * 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex)."
130  */
131 #define ETHER_ADDR_FOR_IPV4_MCAST(x)    \
132         (rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16)
133
134 /*
135  * Configurable number of RX/TX ring descriptors
136  */
137 #define RTE_TEST_RX_DESC_DEFAULT 128
138 #define RTE_TEST_TX_DESC_DEFAULT 512
139 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
140 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
141
142 /* ethernet addresses of ports */
143 static struct ether_addr ports_eth_addr[MAX_PORTS];
144
145 /* mask of enabled ports */
146 static uint32_t enabled_port_mask = 0;
147
148 static uint8_t nb_ports = 0;
149
150 static int rx_queue_per_lcore = 1;
151
152 struct mbuf_table {
153         uint16_t len;
154         struct rte_mbuf *m_table[MAX_PKT_BURST];
155 };
156
157 #define MAX_RX_QUEUE_PER_LCORE 16
158 #define MAX_TX_QUEUE_PER_PORT 16
159 struct lcore_queue_conf {
160         uint64_t tx_tsc;
161         uint16_t n_rx_queue;
162         uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
163         uint16_t tx_queue_id[MAX_PORTS];
164         struct mbuf_table tx_mbufs[MAX_PORTS];
165 } __rte_cache_aligned;
166 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
167
168 static const struct rte_eth_conf port_conf = {
169         .rxmode = {
170                 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
171                 .split_hdr_size = 0,
172                 .header_split   = 0, /**< Header Split disabled */
173                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
174                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
175                 .jumbo_frame    = 1, /**< Jumbo Frame Support enabled */
176                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
177         },
178         .txmode = {
179                 .mq_mode = ETH_DCB_NONE,
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_macip = pkt->pkt.vlan_macip;
346         hdr->pkt.hash = pkt->pkt.hash;
347
348         hdr->ol_flags = pkt->ol_flags;
349
350         __rte_mbuf_sanity_check(hdr, RTE_MBUF_PKT, 1);
351         return (hdr);
352 }
353
354 /*
355  * Write new Ethernet header to the outgoing packet,
356  * and put it into the outgoing queue for the given port.
357  */
358 static inline void
359 mcast_send_pkt(struct rte_mbuf *pkt, struct ether_addr *dest_addr,
360                 struct lcore_queue_conf *qconf, uint8_t port)
361 {
362         struct ether_hdr *ethdr;
363         uint16_t len;
364
365         /* Construct Ethernet header. */
366         ethdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(*ethdr));
367         RTE_MBUF_ASSERT(ethdr != NULL);
368
369         ether_addr_copy(dest_addr, &ethdr->d_addr);
370         ether_addr_copy(&ports_eth_addr[port], &ethdr->s_addr);
371         ethdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
372
373         /* Put new packet into the output queue */
374         len = qconf->tx_mbufs[port].len;
375         qconf->tx_mbufs[port].m_table[len] = pkt;
376         qconf->tx_mbufs[port].len = ++len;
377
378         /* Transmit packets */
379         if (unlikely(MAX_PKT_BURST == len))
380                 send_burst(qconf, port);
381 }
382
383 /* Multicast forward of the input packet */
384 static inline void
385 mcast_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf)
386 {
387         struct rte_mbuf *mc;
388         struct ipv4_hdr *iphdr;
389         uint32_t dest_addr, port_mask, port_num, use_clone;
390         int32_t hash;
391         uint8_t port;
392         union {
393                 uint64_t as_int;
394                 struct ether_addr as_addr;
395         } dst_eth_addr;
396
397         /* Remove the Ethernet header from the input packet */
398         iphdr = (struct ipv4_hdr *)rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
399         RTE_MBUF_ASSERT(iphdr != NULL);
400
401         dest_addr = rte_be_to_cpu_32(iphdr->dst_addr);
402
403         /*
404          * Check that it is a valid multicast address and
405          * we have some active ports assigned to it.
406          */
407         if(!IS_IPV4_MCAST(dest_addr) ||
408             (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 ||
409             (port_mask = hash & enabled_port_mask) == 0) {
410                 rte_pktmbuf_free(m);
411                 return;
412         }
413
414         /* Calculate number of destination ports. */
415         port_num = bitcnt(port_mask);
416
417         /* Should we use rte_pktmbuf_clone() or not. */
418         use_clone = (port_num <= MCAST_CLONE_PORTS &&
419             m->pkt.nb_segs <= MCAST_CLONE_SEGS);
420
421         /* Mark all packet's segments as referenced port_num times */
422         if (use_clone == 0)
423                 rte_pktmbuf_refcnt_update(m, (uint16_t)port_num);
424
425         /* construct destination ethernet address */
426         dst_eth_addr.as_int = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr);
427
428         for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) {
429
430                 /* Prepare output packet and send it out. */
431                 if ((port_mask & 1) != 0) {
432                         if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL))
433                                 mcast_send_pkt(mc, &dst_eth_addr.as_addr,
434                                                 qconf, port);
435                         else if (use_clone == 0)
436                                 rte_pktmbuf_free(m);
437                 }
438         }
439
440         /*
441          * If we making clone packets, then, for the last destination port,
442          * we can overwrite input packet's metadata.
443          */
444         if (use_clone != 0)
445                 mcast_send_pkt(m, &dst_eth_addr.as_addr, qconf, port);
446         else
447                 rte_pktmbuf_free(m);
448 }
449
450 /* Send burst of outgoing packet, if timeout expires. */
451 static inline void
452 send_timeout_burst(struct lcore_queue_conf *qconf)
453 {
454         uint64_t cur_tsc;
455         uint8_t portid;
456
457         cur_tsc = rte_rdtsc();
458         if (likely (cur_tsc < qconf->tx_tsc + BURST_TX_DRAIN))
459                 return;
460
461         for (portid = 0; portid < MAX_PORTS; portid++) {
462                 if (qconf->tx_mbufs[portid].len != 0)
463                         send_burst(qconf, portid);
464         }
465         qconf->tx_tsc = cur_tsc;
466 }
467
468 /* main processing loop */
469 static __attribute__((noreturn)) int
470 main_loop(__rte_unused void *dummy)
471 {
472         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
473         uint32_t lcore_id;
474         int i, j, nb_rx;
475         uint8_t portid;
476         struct lcore_queue_conf *qconf;
477
478         lcore_id = rte_lcore_id();
479         qconf = &lcore_queue_conf[lcore_id];
480
481
482         if (qconf->n_rx_queue == 0) {
483                 RTE_LOG(INFO, IPv4_MULTICAST, "lcore %u has nothing to do\n",
484                     lcore_id);
485                 while(1);
486         }
487
488         RTE_LOG(INFO, IPv4_MULTICAST, "entering main loop on lcore %u\n",
489             lcore_id);
490
491         for (i = 0; i < qconf->n_rx_queue; i++) {
492
493                 portid = qconf->rx_queue_list[i];
494                 RTE_LOG(INFO, IPv4_MULTICAST, " -- lcoreid=%u portid=%d\n",
495                     lcore_id, (int) portid);
496         }
497
498         while (1) {
499
500                 /*
501                  * Read packet from RX queues
502                  */
503                 for (i = 0; i < qconf->n_rx_queue; i++) {
504
505                         portid = qconf->rx_queue_list[i];
506                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
507                                                  MAX_PKT_BURST);
508
509                         /* Prefetch first packets */
510                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
511                                 rte_prefetch0(rte_pktmbuf_mtod(
512                                                 pkts_burst[j], void *));
513                         }
514
515                         /* Prefetch and forward already prefetched packets */
516                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
517                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
518                                                 j + PREFETCH_OFFSET], void *));
519                                 mcast_forward(pkts_burst[j], qconf);
520                         }
521
522                         /* Forward remaining prefetched packets */
523                         for (; j < nb_rx; j++) {
524                                 mcast_forward(pkts_burst[j], qconf);
525                         }
526                 }
527
528                 /* Send out packets from TX queues */
529                 send_timeout_burst(qconf);
530         }
531 }
532
533 /* display usage */
534 static void
535 print_usage(const char *prgname)
536 {
537         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
538             "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
539             "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
540             prgname);
541 }
542
543 static uint32_t
544 parse_portmask(const char *portmask)
545 {
546         char *end = NULL;
547         unsigned long pm;
548
549         /* parse hexadecimal string */
550         pm = strtoul(portmask, &end, 16);
551         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
552                 return 0;
553
554         return ((uint32_t)pm);
555 }
556
557 static int
558 parse_nqueue(const char *q_arg)
559 {
560         char *end = NULL;
561         unsigned long n;
562
563         /* parse numerical string */
564         errno = 0;
565         n = strtoul(q_arg, &end, 0);
566         if (errno != 0 || end == NULL || *end != '\0' ||
567                         n == 0 || n >= MAX_RX_QUEUE_PER_LCORE)
568                 return (-1);
569
570         return (n);
571 }
572
573 /* Parse the argument given in the command line of the application */
574 static int
575 parse_args(int argc, char **argv)
576 {
577         int opt, ret;
578         char **argvopt;
579         int option_index;
580         char *prgname = argv[0];
581         static struct option lgopts[] = {
582                 {NULL, 0, 0, 0}
583         };
584
585         argvopt = argv;
586
587         while ((opt = getopt_long(argc, argvopt, "p:q:",
588                                   lgopts, &option_index)) != EOF) {
589
590                 switch (opt) {
591                 /* portmask */
592                 case 'p':
593                         enabled_port_mask = parse_portmask(optarg);
594                         if (enabled_port_mask == 0) {
595                                 printf("invalid portmask\n");
596                                 print_usage(prgname);
597                                 return -1;
598                         }
599                         break;
600
601                 /* nqueue */
602                 case 'q':
603                         rx_queue_per_lcore = parse_nqueue(optarg);
604                         if (rx_queue_per_lcore < 0) {
605                                 printf("invalid queue number\n");
606                                 print_usage(prgname);
607                                 return -1;
608                         }
609                         break;
610
611                 default:
612                         print_usage(prgname);
613                         return -1;
614                 }
615         }
616
617         if (optind >= 0)
618                 argv[optind-1] = prgname;
619
620         ret = optind-1;
621         optind = 0; /* reset getopt lib */
622         return ret;
623 }
624
625 static void
626 print_ethaddr(const char *name, struct ether_addr *eth_addr)
627 {
628         printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
629                eth_addr->addr_bytes[0],
630                eth_addr->addr_bytes[1],
631                eth_addr->addr_bytes[2],
632                eth_addr->addr_bytes[3],
633                eth_addr->addr_bytes[4],
634                eth_addr->addr_bytes[5]);
635 }
636
637 static int
638 init_mcast_hash(void)
639 {
640         uint32_t i;
641
642         mcast_hash = rte_fbk_hash_create(&mcast_hash_params);
643         if (mcast_hash == NULL){
644                 return -1;
645         }
646
647         for (i = 0; i < N_MCAST_GROUPS; i ++){
648                 if (rte_fbk_hash_add_key(mcast_hash,
649                         mcast_group_table[i].ip,
650                         mcast_group_table[i].port_mask) < 0) {
651                         return -1;
652                 }
653         }
654
655         return 0;
656 }
657
658 int
659 MAIN(int argc, char **argv)
660 {
661         struct lcore_queue_conf *qconf;
662         struct rte_eth_link link;
663         int ret;
664         uint16_t queueid;
665         unsigned lcore_id = 0, rx_lcore_id = 0;;
666         uint32_t n_tx_queue, nb_lcores;
667         uint8_t portid;
668
669         /* init EAL */
670         ret = rte_eal_init(argc, argv);
671         if (ret < 0)
672                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
673         argc -= ret;
674         argv += ret;
675
676         /* parse application arguments (after the EAL ones) */
677         ret = parse_args(argc, argv);
678         if (ret < 0)
679                 rte_exit(EXIT_FAILURE, "Invalid IPV4_MULTICAST parameters\n");
680
681         /* create the mbuf pools */
682         packet_pool = rte_mempool_create("packet_pool", NB_PKT_MBUF,
683             PKT_MBUF_SIZE, 32, sizeof(struct rte_pktmbuf_pool_private),
684             rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL,
685             SOCKET0, 0);
686
687         if (packet_pool == NULL)
688                 rte_exit(EXIT_FAILURE, "Cannot init packet mbuf pool\n");
689
690         header_pool = rte_mempool_create("header_pool", NB_HDR_MBUF,
691             HDR_MBUF_SIZE, 32, 0, NULL, NULL, rte_pktmbuf_init, NULL,
692             SOCKET0, 0);
693
694         if (header_pool == NULL)
695                 rte_exit(EXIT_FAILURE, "Cannot init header mbuf pool\n");
696
697         clone_pool = rte_mempool_create("clone_pool", NB_CLONE_MBUF,
698             CLONE_MBUF_SIZE, 32, 0, NULL, NULL, rte_pktmbuf_init, NULL,
699             SOCKET0, 0);
700
701         if (clone_pool == NULL)
702                 rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n");
703
704         /* init driver */
705         if (rte_pmd_init_all() < 0)
706                 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
707
708         if (rte_eal_pci_probe() < 0)
709                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
710
711         nb_ports = rte_eth_dev_count();
712         if (nb_ports == 0)
713                 rte_exit(EXIT_FAILURE, "No physical ports!\n");
714         if (nb_ports > MAX_PORTS)
715                 nb_ports = MAX_PORTS;
716
717         nb_lcores = rte_lcore_count();
718
719         /* initialize all ports */
720         for (portid = 0; portid < nb_ports; portid++) {
721                 /* skip ports that are not enabled */
722                 if ((enabled_port_mask & (1 << portid)) == 0) {
723                         printf("Skipping disabled port %d\n", portid);
724                         continue;
725                 }
726
727                 qconf = &lcore_queue_conf[rx_lcore_id];
728
729                 /* get the lcore_id for this port */
730                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
731                        qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
732
733                         rx_lcore_id ++;
734                         qconf = &lcore_queue_conf[rx_lcore_id];
735
736                         if (rx_lcore_id >= RTE_MAX_LCORE)
737                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
738                 }
739                 qconf->rx_queue_list[qconf->n_rx_queue] = portid;
740                 qconf->n_rx_queue++;
741
742                 /* init port */
743                 printf("Initializing port %d on lcore %u... ", portid,
744                        rx_lcore_id);
745                 fflush(stdout);
746
747                 n_tx_queue = nb_lcores;
748                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
749                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
750                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
751                                             &port_conf);
752                 if (ret < 0)
753                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
754                                   ret, portid);
755
756                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
757                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
758                 printf(", ");
759
760                 /* init one RX queue */
761                 queueid = 0;
762                 printf("rxq=%hu ", queueid);
763                 fflush(stdout);
764                 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
765                                              SOCKET0, &rx_conf,
766                                              packet_pool);
767                 if (ret < 0)
768                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, port=%d\n",
769                                   ret, portid);
770
771                 /* init one TX queue per couple (lcore,port) */
772                 queueid = 0;
773
774                 RTE_LCORE_FOREACH(lcore_id) {
775                         if (rte_lcore_is_enabled(lcore_id) == 0)
776                                 continue;
777                         printf("txq=%u,%hu ", lcore_id, queueid);
778                         fflush(stdout);
779                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
780                                                      SOCKET0, &tx_conf);
781                         if (ret < 0)
782                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
783                                           "port=%d\n", ret, portid);
784
785                         qconf = &lcore_queue_conf[lcore_id];
786                         qconf->tx_queue_id[portid] = queueid;
787                         queueid++;
788                 }
789
790                 /* Start device */
791                 ret = rte_eth_dev_start(portid);
792                 if (ret < 0)
793                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
794                                   ret, portid);
795
796                 printf("done: ");
797
798                 /* get link status */
799                 rte_eth_link_get(portid, &link);
800                 if (link.link_status) {
801                         printf(" Link Up - speed %u Mbps - %s\n",
802                                (uint32_t) link.link_speed,
803                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
804                                ("full-duplex") : ("half-duplex\n"));
805                         rte_eth_promiscuous_enable(portid);
806                         rte_eth_allmulticast_enable(portid);
807                 } else {
808                         printf(" Link Down\n");
809                 }
810         }
811
812
813         /* initialize the multicast hash */
814         int retval = init_mcast_hash();
815         if (retval != 0)
816                 rte_exit(EXIT_FAILURE, "Cannot build the multicast hash\n");
817
818         /* launch per-lcore init on every lcore */
819         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
820         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
821                 if (rte_eal_wait_lcore(lcore_id) < 0)
822                         return -1;
823         }
824
825         return 0;
826 }