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