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