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