examples: rename ipv4_frag example to ip_fragmentation
[dpdk.git] / examples / ip_fragmentation / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <sys/param.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_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_tailq.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_lpm.h>
72 #include <rte_ip.h>
73
74 #include "rte_ip_frag.h"
75 #include "main.h"
76
77 /*
78  * Default byte size for the IPv4 Maximum Transfer Unit (MTU).
79  * This value includes the size of IPv4 header.
80  */
81 #define IPV4_MTU_DEFAULT        ETHER_MTU
82
83 /*
84  * Default payload in bytes for the IPv4 packet.
85  */
86 #define IPV4_DEFAULT_PAYLOAD    (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
87
88 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
89
90 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
91
92 /* allow max jumbo frame 9.5 KB */
93 #define JUMBO_FRAME_MAX_SIZE    0x2600
94
95 #define ROUNDUP_DIV(a, b)       (((a) + (b) - 1) / (b))
96
97 /*
98  * Max number of fragments per packet expected.
99  */
100 #define MAX_PACKET_FRAG ROUNDUP_DIV(JUMBO_FRAME_MAX_SIZE, IPV4_DEFAULT_PAYLOAD)
101
102 #define NB_MBUF   8192
103
104 /*
105  * RX and TX Prefetch, Host, and Write-back threshold values should be
106  * carefully set for optimal performance. Consult the network
107  * controller's datasheet and supporting DPDK documentation for guidance
108  * on how these parameters should be set.
109  */
110 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
111 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
112 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
113
114 /*
115  * These default values are optimized for use with the Intel(R) 82599 10 GbE
116  * Controller and the DPDK ixgbe PMD. Consider using other values for other
117  * network controllers and/or network drivers.
118  */
119 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
120 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
121 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
122
123 #define MAX_PKT_BURST   32
124 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
125
126 /* Configure how many packets ahead to prefetch, when reading packets */
127 #define PREFETCH_OFFSET 3
128
129 /*
130  * Configurable number of RX/TX ring descriptors
131  */
132 #define RTE_TEST_RX_DESC_DEFAULT 128
133 #define RTE_TEST_TX_DESC_DEFAULT 512
134 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
135 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
136
137 /* ethernet addresses of ports */
138 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
139 static struct ether_addr remote_eth_addr =
140         {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
141
142 /* mask of enabled ports */
143 static int enabled_port_mask = 0;
144
145 static int rx_queue_per_lcore = 1;
146
147 #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
148
149 struct mbuf_table {
150         uint16_t len;
151         struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
152 };
153
154 #define MAX_RX_QUEUE_PER_LCORE 16
155 #define MAX_TX_QUEUE_PER_PORT 16
156 struct lcore_queue_conf {
157         uint16_t n_rx_queue;
158         uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
159         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
160         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
161
162 } __rte_cache_aligned;
163 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 struct rte_mempool *pool_direct = NULL, *pool_indirect = NULL;
199
200 struct l3fwd_route {
201         uint32_t ip;
202         uint8_t  depth;
203         uint8_t  if_out;
204 };
205
206 struct l3fwd_route l3fwd_route_array[] = {
207         {IPv4(100,10,0,0), 16, 2},
208         {IPv4(100,20,0,0), 16, 2},
209         {IPv4(100,30,0,0), 16, 0},
210         {IPv4(100,40,0,0), 16, 0},
211 };
212
213 #define L3FWD_NUM_ROUTES \
214         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
215
216 #define L3FWD_LPM_MAX_RULES     1024
217
218 struct rte_lpm *l3fwd_lpm = NULL;
219
220 /* Send burst of packets on an output interface */
221 static inline int
222 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port)
223 {
224         struct rte_mbuf **m_table;
225         int ret;
226         uint16_t queueid;
227
228         queueid = qconf->tx_queue_id[port];
229         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
230
231         ret = rte_eth_tx_burst(port, queueid, m_table, n);
232         if (unlikely(ret < n)) {
233                 do {
234                         rte_pktmbuf_free(m_table[ret]);
235                 } while (++ret < n);
236         }
237
238         return 0;
239 }
240
241 static inline void
242 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t port_in)
243 {
244         struct lcore_queue_conf *qconf;
245         struct ipv4_hdr *ip_hdr;
246         uint32_t i, len, lcore_id, ip_dst;
247         uint8_t next_hop, port_out;
248         int32_t len2;
249
250         lcore_id = rte_lcore_id();
251         qconf = &lcore_queue_conf[lcore_id];
252
253         /* Remove the Ethernet header and trailer from the input packet */
254         rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
255
256         /* Read the lookup key (i.e. ip_dst) from the input packet */
257         ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
258         ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
259
260         /* Find destination port */
261         if (rte_lpm_lookup(l3fwd_lpm, ip_dst, &next_hop) == 0 &&
262                         (enabled_port_mask & 1 << next_hop) != 0)
263                 port_out = next_hop;
264         else
265                 port_out = port_in;
266
267         /* Build transmission burst */
268         len = qconf->tx_mbufs[port_out].len;
269
270         /* if we don't need to do any fragmentation */
271         if (likely (IPV4_MTU_DEFAULT  >= m->pkt.pkt_len)) {
272                 qconf->tx_mbufs[port_out].m_table[len] = m;
273                 len2 = 1;
274         } else {
275                 len2 = rte_ipv4_fragment_packet(m,
276                         &qconf->tx_mbufs[port_out].m_table[len],
277                         (uint16_t)(MBUF_TABLE_SIZE - len),
278                         IPV4_MTU_DEFAULT,
279                         pool_direct, pool_indirect);
280
281                 /* Free input packet */
282                 rte_pktmbuf_free(m);
283
284                 /* If we fail to fragment the packet */
285                 if (unlikely (len2 < 0))
286                         return;
287         }
288
289         for (i = len; i < len + len2; i ++) {
290                 m = qconf->tx_mbufs[port_out].m_table[i];
291                 struct ether_hdr *eth_hdr = (struct ether_hdr *)
292                         rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
293                 if (eth_hdr == NULL) {
294                         rte_panic("No headroom in mbuf.\n");
295                 }
296
297                 m->pkt.vlan_macip.f.l2_len = sizeof(struct ether_hdr);
298
299                 ether_addr_copy(&remote_eth_addr, &eth_hdr->d_addr);
300                 ether_addr_copy(&ports_eth_addr[port_out], &eth_hdr->s_addr);
301                 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
302         }
303
304         len += len2;
305
306         if (likely(len < MAX_PKT_BURST)) {
307                 qconf->tx_mbufs[port_out].len = (uint16_t)len;
308                 return;
309         }
310
311         /* Transmit packets */
312         send_burst(qconf, (uint16_t)len, port_out);
313         qconf->tx_mbufs[port_out].len = 0;
314 }
315
316 /* main processing loop */
317 static int
318 main_loop(__attribute__((unused)) void *dummy)
319 {
320         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
321         unsigned lcore_id;
322         uint64_t prev_tsc, diff_tsc, cur_tsc;
323         int i, j, nb_rx;
324         uint8_t portid;
325         struct lcore_queue_conf *qconf;
326         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
327
328         prev_tsc = 0;
329
330         lcore_id = rte_lcore_id();
331         qconf = &lcore_queue_conf[lcore_id];
332
333         if (qconf->n_rx_queue == 0) {
334                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
335                 return 0;
336         }
337
338         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
339
340         for (i = 0; i < qconf->n_rx_queue; i++) {
341
342                 portid = qconf->rx_queue_list[i];
343                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%d\n", lcore_id,
344                         (int) portid);
345         }
346
347         while (1) {
348
349                 cur_tsc = rte_rdtsc();
350
351                 /*
352                  * TX burst queue drain
353                  */
354                 diff_tsc = cur_tsc - prev_tsc;
355                 if (unlikely(diff_tsc > drain_tsc)) {
356
357                         /*
358                          * This could be optimized (use queueid instead of
359                          * portid), but it is not called so often
360                          */
361                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
362                                 if (qconf->tx_mbufs[portid].len == 0)
363                                         continue;
364                                 send_burst(&lcore_queue_conf[lcore_id],
365                                            qconf->tx_mbufs[portid].len,
366                                            portid);
367                                 qconf->tx_mbufs[portid].len = 0;
368                         }
369
370                         prev_tsc = cur_tsc;
371                 }
372
373                 /*
374                  * Read packet from RX queues
375                  */
376                 for (i = 0; i < qconf->n_rx_queue; i++) {
377
378                         portid = qconf->rx_queue_list[i];
379                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
380                                                  MAX_PKT_BURST);
381
382                         /* Prefetch first packets */
383                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
384                                 rte_prefetch0(rte_pktmbuf_mtod(
385                                                 pkts_burst[j], void *));
386                         }
387
388                         /* Prefetch and forward already prefetched packets */
389                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
390                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
391                                                 j + PREFETCH_OFFSET], void *));
392                                 l3fwd_simple_forward(pkts_burst[j], portid);
393                         }
394
395                         /* Forward remaining prefetched packets */
396                         for (; j < nb_rx; j++) {
397                                 l3fwd_simple_forward(pkts_burst[j], portid);
398                         }
399                 }
400         }
401 }
402
403 /* display usage */
404 static void
405 print_usage(const char *prgname)
406 {
407         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
408                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
409                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
410                prgname);
411 }
412
413 static int
414 parse_portmask(const char *portmask)
415 {
416         char *end = NULL;
417         unsigned long pm;
418
419         /* parse hexadecimal string */
420         pm = strtoul(portmask, &end, 16);
421         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
422                 return -1;
423
424         if (pm == 0)
425                 return -1;
426
427         return pm;
428 }
429
430 static int
431 parse_nqueue(const char *q_arg)
432 {
433         char *end = NULL;
434         unsigned long n;
435
436         /* parse hexadecimal string */
437         n = strtoul(q_arg, &end, 10);
438         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
439                 return -1;
440         if (n == 0)
441                 return -1;
442         if (n >= MAX_RX_QUEUE_PER_LCORE)
443                 return -1;
444
445         return n;
446 }
447
448 /* Parse the argument given in the command line of the application */
449 static int
450 parse_args(int argc, char **argv)
451 {
452         int opt, ret;
453         char **argvopt;
454         int option_index;
455         char *prgname = argv[0];
456         static struct option lgopts[] = {
457                 {NULL, 0, 0, 0}
458         };
459
460         argvopt = argv;
461
462         while ((opt = getopt_long(argc, argvopt, "p:q:",
463                                   lgopts, &option_index)) != EOF) {
464
465                 switch (opt) {
466                 /* portmask */
467                 case 'p':
468                         enabled_port_mask = parse_portmask(optarg);
469                         if (enabled_port_mask < 0) {
470                                 printf("invalid portmask\n");
471                                 print_usage(prgname);
472                                 return -1;
473                         }
474                         break;
475
476                 /* nqueue */
477                 case 'q':
478                         rx_queue_per_lcore = parse_nqueue(optarg);
479                         if (rx_queue_per_lcore < 0) {
480                                 printf("invalid queue number\n");
481                                 print_usage(prgname);
482                                 return -1;
483                         }
484                         break;
485
486                 /* long options */
487                 case 0:
488                         print_usage(prgname);
489                         return -1;
490
491                 default:
492                         print_usage(prgname);
493                         return -1;
494                 }
495         }
496
497         if (enabled_port_mask == 0) {
498                 printf("portmask not specified\n");
499                 print_usage(prgname);
500                 return -1;
501         }
502
503         if (optind >= 0)
504                 argv[optind-1] = prgname;
505
506         ret = optind-1;
507         optind = 0; /* reset getopt lib */
508         return ret;
509 }
510
511 static void
512 print_ethaddr(const char *name, struct ether_addr *eth_addr)
513 {
514         printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
515                eth_addr->addr_bytes[0],
516                eth_addr->addr_bytes[1],
517                eth_addr->addr_bytes[2],
518                eth_addr->addr_bytes[3],
519                eth_addr->addr_bytes[4],
520                eth_addr->addr_bytes[5]);
521 }
522
523 /* Check the link status of all ports in up to 9s, and print them finally */
524 static void
525 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
526 {
527 #define CHECK_INTERVAL 100 /* 100ms */
528 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
529         uint8_t portid, count, all_ports_up, print_flag = 0;
530         struct rte_eth_link link;
531
532         printf("\nChecking link status");
533         fflush(stdout);
534         for (count = 0; count <= MAX_CHECK_TIME; count++) {
535                 all_ports_up = 1;
536                 for (portid = 0; portid < port_num; portid++) {
537                         if ((port_mask & (1 << portid)) == 0)
538                                 continue;
539                         memset(&link, 0, sizeof(link));
540                         rte_eth_link_get_nowait(portid, &link);
541                         /* print link status if flag set */
542                         if (print_flag == 1) {
543                                 if (link.link_status)
544                                         printf("Port %d Link Up - speed %u "
545                                                 "Mbps - %s\n", (uint8_t)portid,
546                                                 (unsigned)link.link_speed,
547                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
548                                         ("full-duplex") : ("half-duplex\n"));
549                                 else
550                                         printf("Port %d Link Down\n",
551                                                         (uint8_t)portid);
552                                 continue;
553                         }
554                         /* clear all_ports_up flag if any link down */
555                         if (link.link_status == 0) {
556                                 all_ports_up = 0;
557                                 break;
558                         }
559                 }
560                 /* after finally printing all link status, get out */
561                 if (print_flag == 1)
562                         break;
563
564                 if (all_ports_up == 0) {
565                         printf(".");
566                         fflush(stdout);
567                         rte_delay_ms(CHECK_INTERVAL);
568                 }
569
570                 /* set the print_flag if all ports up or timeout */
571                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
572                         print_flag = 1;
573                         printf("done\n");
574                 }
575         }
576 }
577
578 int
579 MAIN(int argc, char **argv)
580 {
581         struct lcore_queue_conf *qconf;
582         int ret;
583         unsigned nb_ports, i;
584         uint16_t queueid = 0;
585         unsigned lcore_id = 0, rx_lcore_id = 0;
586         uint32_t n_tx_queue, nb_lcores;
587         uint8_t portid;
588
589         /* init EAL */
590         ret = rte_eal_init(argc, argv);
591         if (ret < 0)
592                 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
593         argc -= ret;
594         argv += ret;
595
596         /* parse application arguments (after the EAL ones) */
597         ret = parse_args(argc, argv);
598         if (ret < 0)
599                 rte_exit(EXIT_FAILURE, "Invalid arguments");
600
601         /* create the mbuf pools */
602         pool_direct =
603                 rte_mempool_create("pool_direct", NB_MBUF,
604                                    MBUF_SIZE, 32,
605                                    sizeof(struct rte_pktmbuf_pool_private),
606                                    rte_pktmbuf_pool_init, NULL,
607                                    rte_pktmbuf_init, NULL,
608                                    rte_socket_id(), 0);
609         if (pool_direct == NULL)
610                 rte_panic("Cannot init direct mbuf pool\n");
611
612         pool_indirect =
613                 rte_mempool_create("pool_indirect", NB_MBUF,
614                                    sizeof(struct rte_mbuf), 32,
615                                    0,
616                                    NULL, NULL,
617                                    rte_pktmbuf_init, NULL,
618                                    rte_socket_id(), 0);
619         if (pool_indirect == NULL)
620                 rte_panic("Cannot init indirect mbuf pool\n");
621
622         if (rte_eal_pci_probe() < 0)
623                 rte_panic("Cannot probe PCI\n");
624
625         nb_ports = rte_eth_dev_count();
626         if (nb_ports > RTE_MAX_ETHPORTS)
627                 nb_ports = RTE_MAX_ETHPORTS;
628
629         nb_lcores = rte_lcore_count();
630
631         /* initialize all ports */
632         for (portid = 0; portid < nb_ports; portid++) {
633                 /* skip ports that are not enabled */
634                 if ((enabled_port_mask & (1 << portid)) == 0) {
635                         printf("Skipping disabled port %d\n", portid);
636                         continue;
637                 }
638
639                 qconf = &lcore_queue_conf[rx_lcore_id];
640
641                 /* get the lcore_id for this port */
642                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
643                        qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
644
645                         rx_lcore_id ++;
646                         if (rx_lcore_id >= RTE_MAX_LCORE)
647                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
648
649                         qconf = &lcore_queue_conf[rx_lcore_id];
650                 }
651                 qconf->rx_queue_list[qconf->n_rx_queue] = portid;
652                 qconf->n_rx_queue++;
653
654                 /* init port */
655                 printf("Initializing port %d on lcore %u... ", portid,
656                        rx_lcore_id);
657                 fflush(stdout);
658
659                 n_tx_queue = nb_lcores;
660                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
661                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
662                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
663                                             &port_conf);
664                 if (ret < 0)
665                         rte_exit(EXIT_FAILURE, "Cannot configure device: "
666                                 "err=%d, port=%d\n",
667                                 ret, portid);
668
669                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
670                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
671                 printf(", ");
672
673                 /* init one RX queue */
674                 queueid = 0;
675                 printf("rxq=%d ", queueid);
676                 fflush(stdout);
677                 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
678                                              rte_eth_dev_socket_id(portid), &rx_conf,
679                                              pool_direct);
680                 if (ret < 0)
681                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
682                                 "err=%d, port=%d\n",
683                                 ret, portid);
684
685                 /* init one TX queue per couple (lcore,port) */
686                 queueid = 0;
687                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
688                         if (rte_lcore_is_enabled(lcore_id) == 0)
689                                 continue;
690                         printf("txq=%u,%d ", lcore_id, queueid);
691                         fflush(stdout);
692                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
693                                                      rte_eth_dev_socket_id(portid), &tx_conf);
694                         if (ret < 0)
695                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
696                                         "err=%d, port=%d\n", ret, portid);
697
698                         qconf = &lcore_queue_conf[lcore_id];
699                         qconf->tx_queue_id[portid] = queueid;
700                         queueid++;
701                 }
702
703                 /* Start device */
704                 ret = rte_eth_dev_start(portid);
705                 if (ret < 0)
706                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
707                                 "err=%d, port=%d\n",
708                                 ret, portid);
709
710                 printf("done: ");
711
712                 /* Set port in promiscuous mode */
713                 rte_eth_promiscuous_enable(portid);
714         }
715
716         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
717
718         /* create the LPM table */
719         l3fwd_lpm = rte_lpm_create("L3FWD_LPM", rte_socket_id(), L3FWD_LPM_MAX_RULES, 0);
720         if (l3fwd_lpm == NULL)
721                 rte_panic("Unable to create the l3fwd LPM table\n");
722
723         /* populate the LPM table */
724         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
725                 ret = rte_lpm_add(l3fwd_lpm,
726                         l3fwd_route_array[i].ip,
727                         l3fwd_route_array[i].depth,
728                         l3fwd_route_array[i].if_out);
729
730                 if (ret < 0) {
731                         rte_panic("Unable to add entry %u to the l3fwd "
732                                 "LPM table\n", i);
733                 }
734
735                 printf("Adding route 0x%08x / %d (%d)\n",
736                         (unsigned) l3fwd_route_array[i].ip,
737                         l3fwd_route_array[i].depth,
738                         l3fwd_route_array[i].if_out);
739         }
740
741         /* launch per-lcore init on every lcore */
742         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
743         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
744                 if (rte_eal_wait_lcore(lcore_id) < 0)
745                         return -1;
746         }
747
748         return 0;
749 }