examples: use factorized default Rx/Tx configuration
[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_lpm6.h>
73 #include <rte_ip.h>
74 #include <rte_string_fns.h>
75
76 #include <rte_ip_frag.h>
77
78 #include "main.h"
79
80 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
81
82 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
83
84 /* allow max jumbo frame 9.5 KB */
85 #define JUMBO_FRAME_MAX_SIZE    0x2600
86
87 #define ROUNDUP_DIV(a, b)       (((a) + (b) - 1) / (b))
88
89 /*
90  * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
91  * This value includes the size of IPv6 header.
92  */
93 #define IPV4_MTU_DEFAULT        ETHER_MTU
94 #define IPV6_MTU_DEFAULT        ETHER_MTU
95
96 /*
97  * Default payload in bytes for the IPv6 packet.
98  */
99 #define IPV4_DEFAULT_PAYLOAD    (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
100 #define IPV6_DEFAULT_PAYLOAD    (IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr))
101
102 /*
103  * Max number of fragments per packet expected - defined by config file.
104  */
105 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
106
107 #define NB_MBUF   8192
108
109 #define MAX_PKT_BURST   32
110 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
111
112 /* Configure how many packets ahead to prefetch, when reading packets */
113 #define PREFETCH_OFFSET 3
114
115 /*
116  * Configurable number of RX/TX ring descriptors
117  */
118 #define RTE_TEST_RX_DESC_DEFAULT 128
119 #define RTE_TEST_TX_DESC_DEFAULT 512
120 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
121 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
122
123 /* ethernet addresses of ports */
124 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
125
126 #ifndef IPv4_BYTES
127 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
128 #define IPv4_BYTES(addr) \
129                 (uint8_t) (((addr) >> 24) & 0xFF),\
130                 (uint8_t) (((addr) >> 16) & 0xFF),\
131                 (uint8_t) (((addr) >> 8) & 0xFF),\
132                 (uint8_t) ((addr) & 0xFF)
133 #endif
134
135 #ifndef IPv6_BYTES
136 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
137                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
138 #define IPv6_BYTES(addr) \
139         addr[0],  addr[1], addr[2],  addr[3], \
140         addr[4],  addr[5], addr[6],  addr[7], \
141         addr[8],  addr[9], addr[10], addr[11],\
142         addr[12], addr[13],addr[14], addr[15]
143 #endif
144
145 #define IPV6_ADDR_LEN 16
146
147 /* mask of enabled ports */
148 static int enabled_port_mask = 0;
149
150 static int rx_queue_per_lcore = 1;
151
152 #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
153
154 struct mbuf_table {
155         uint16_t len;
156         struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
157 };
158
159 struct rx_queue {
160         struct rte_mempool *direct_pool;
161         struct rte_mempool *indirect_pool;
162         struct rte_lpm *lpm;
163         struct rte_lpm6 *lpm6;
164         uint8_t portid;
165 };
166
167 #define MAX_RX_QUEUE_PER_LCORE 16
168 #define MAX_TX_QUEUE_PER_PORT 16
169 struct lcore_queue_conf {
170         uint16_t n_rx_queue;
171         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
172         struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
173         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
174 } __rte_cache_aligned;
175 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
176
177 static const struct rte_eth_conf port_conf = {
178         .rxmode = {
179                 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
180                 .split_hdr_size = 0,
181                 .header_split   = 0, /**< Header Split disabled */
182                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
183                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
184                 .jumbo_frame    = 1, /**< Jumbo Frame Support enabled */
185                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
186         },
187         .txmode = {
188                 .mq_mode = ETH_MQ_TX_NONE,
189         },
190 };
191
192 /*
193  * IPv4 forwarding table
194  */
195 struct l3fwd_ipv4_route {
196         uint32_t ip;
197         uint8_t  depth;
198         uint8_t  if_out;
199 };
200
201 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
202                 {IPv4(100,10,0,0), 16, 0},
203                 {IPv4(100,20,0,0), 16, 1},
204                 {IPv4(100,30,0,0), 16, 2},
205                 {IPv4(100,40,0,0), 16, 3},
206                 {IPv4(100,50,0,0), 16, 4},
207                 {IPv4(100,60,0,0), 16, 5},
208                 {IPv4(100,70,0,0), 16, 6},
209                 {IPv4(100,80,0,0), 16, 7},
210 };
211
212 /*
213  * IPv6 forwarding table
214  */
215
216 struct l3fwd_ipv6_route {
217         uint8_t ip[IPV6_ADDR_LEN];
218         uint8_t depth;
219         uint8_t if_out;
220 };
221
222 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
223         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
224         {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
225         {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
226         {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
227         {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
228         {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
229         {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
230         {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
231 };
232
233 #define LPM_MAX_RULES         1024
234 #define LPM6_MAX_RULES         1024
235 #define LPM6_NUMBER_TBL8S (1 << 16)
236
237 struct rte_lpm6_config lpm6_config = {
238                 .max_rules = LPM6_MAX_RULES,
239                 .number_tbl8s = LPM6_NUMBER_TBL8S,
240                 .flags = 0
241 };
242
243 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
244 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
245 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
246 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
247
248 /* Send burst of packets on an output interface */
249 static inline int
250 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port)
251 {
252         struct rte_mbuf **m_table;
253         int ret;
254         uint16_t queueid;
255
256         queueid = qconf->tx_queue_id[port];
257         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
258
259         ret = rte_eth_tx_burst(port, queueid, m_table, n);
260         if (unlikely(ret < n)) {
261                 do {
262                         rte_pktmbuf_free(m_table[ret]);
263                 } while (++ret < n);
264         }
265
266         return 0;
267 }
268
269 static inline void
270 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
271                 uint8_t queueid, uint8_t port_in)
272 {
273         struct rx_queue *rxq;
274         uint32_t i, len;
275         uint8_t next_hop, port_out, ipv6;
276         int32_t len2;
277
278         ipv6 = 0;
279         rxq = &qconf->rx_queue_list[queueid];
280
281         /* by default, send everything back to the source port */
282         port_out = port_in;
283
284         /* Remove the Ethernet header and trailer from the input packet */
285         rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
286
287         /* Build transmission burst */
288         len = qconf->tx_mbufs[port_out].len;
289
290         /* if this is an IPv4 packet */
291         if (m->ol_flags & PKT_RX_IPV4_HDR) {
292                 struct ipv4_hdr *ip_hdr;
293                 uint32_t ip_dst;
294                 /* Read the lookup key (i.e. ip_dst) from the input packet */
295                 ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
296                 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
297
298                 /* Find destination port */
299                 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
300                                 (enabled_port_mask & 1 << next_hop) != 0) {
301                         port_out = next_hop;
302
303                         /* Build transmission burst for new port */
304                         len = qconf->tx_mbufs[port_out].len;
305                 }
306
307                 /* if we don't need to do any fragmentation */
308                 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
309                         qconf->tx_mbufs[port_out].m_table[len] = m;
310                         len2 = 1;
311                 } else {
312                         len2 = rte_ipv4_fragment_packet(m,
313                                 &qconf->tx_mbufs[port_out].m_table[len],
314                                 (uint16_t)(MBUF_TABLE_SIZE - len),
315                                 IPV4_MTU_DEFAULT,
316                                 rxq->direct_pool, rxq->indirect_pool);
317
318                         /* Free input packet */
319                         rte_pktmbuf_free(m);
320
321                         /* If we fail to fragment the packet */
322                         if (unlikely (len2 < 0))
323                                 return;
324                 }
325         }
326         /* if this is an IPv6 packet */
327         else if (m->ol_flags & PKT_RX_IPV6_HDR) {
328                 struct ipv6_hdr *ip_hdr;
329
330                 ipv6 = 1;
331
332                 /* Read the lookup key (i.e. ip_dst) from the input packet */
333                 ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
334
335                 /* Find destination port */
336                 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr, &next_hop) == 0 &&
337                                 (enabled_port_mask & 1 << next_hop) != 0) {
338                         port_out = next_hop;
339
340                         /* Build transmission burst for new port */
341                         len = qconf->tx_mbufs[port_out].len;
342                 }
343
344                 /* if we don't need to do any fragmentation */
345                 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
346                         qconf->tx_mbufs[port_out].m_table[len] = m;
347                         len2 = 1;
348                 } else {
349                         len2 = rte_ipv6_fragment_packet(m,
350                                 &qconf->tx_mbufs[port_out].m_table[len],
351                                 (uint16_t)(MBUF_TABLE_SIZE - len),
352                                 IPV6_MTU_DEFAULT,
353                                 rxq->direct_pool, rxq->indirect_pool);
354
355                         /* Free input packet */
356                         rte_pktmbuf_free(m);
357
358                         /* If we fail to fragment the packet */
359                         if (unlikely (len2 < 0))
360                                 return;
361                 }
362         }
363         /* else, just forward the packet */
364         else {
365                 qconf->tx_mbufs[port_out].m_table[len] = m;
366                 len2 = 1;
367         }
368
369         for (i = len; i < len + len2; i ++) {
370                 void *d_addr_bytes;
371
372                 m = qconf->tx_mbufs[port_out].m_table[i];
373                 struct ether_hdr *eth_hdr = (struct ether_hdr *)
374                         rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
375                 if (eth_hdr == NULL) {
376                         rte_panic("No headroom in mbuf.\n");
377                 }
378
379                 m->l2_len = sizeof(struct ether_hdr);
380
381                 /* 02:00:00:00:00:xx */
382                 d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
383                 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40);
384
385                 /* src addr */
386                 ether_addr_copy(&ports_eth_addr[port_out], &eth_hdr->s_addr);
387                 if (ipv6)
388                         eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
389                 else
390                         eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
391         }
392
393         len += len2;
394
395         if (likely(len < MAX_PKT_BURST)) {
396                 qconf->tx_mbufs[port_out].len = (uint16_t)len;
397                 return;
398         }
399
400         /* Transmit packets */
401         send_burst(qconf, (uint16_t)len, port_out);
402         qconf->tx_mbufs[port_out].len = 0;
403 }
404
405 /* main processing loop */
406 static int
407 main_loop(__attribute__((unused)) void *dummy)
408 {
409         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
410         unsigned lcore_id;
411         uint64_t prev_tsc, diff_tsc, cur_tsc;
412         int i, j, nb_rx;
413         uint8_t portid;
414         struct lcore_queue_conf *qconf;
415         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
416
417         prev_tsc = 0;
418
419         lcore_id = rte_lcore_id();
420         qconf = &lcore_queue_conf[lcore_id];
421
422         if (qconf->n_rx_queue == 0) {
423                 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
424                 return 0;
425         }
426
427         RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
428
429         for (i = 0; i < qconf->n_rx_queue; i++) {
430
431                 portid = qconf->rx_queue_list[i].portid;
432                 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
433                                 (int) portid);
434         }
435
436         while (1) {
437
438                 cur_tsc = rte_rdtsc();
439
440                 /*
441                  * TX burst queue drain
442                  */
443                 diff_tsc = cur_tsc - prev_tsc;
444                 if (unlikely(diff_tsc > drain_tsc)) {
445
446                         /*
447                          * This could be optimized (use queueid instead of
448                          * portid), but it is not called so often
449                          */
450                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
451                                 if (qconf->tx_mbufs[portid].len == 0)
452                                         continue;
453                                 send_burst(&lcore_queue_conf[lcore_id],
454                                            qconf->tx_mbufs[portid].len,
455                                            portid);
456                                 qconf->tx_mbufs[portid].len = 0;
457                         }
458
459                         prev_tsc = cur_tsc;
460                 }
461
462                 /*
463                  * Read packet from RX queues
464                  */
465                 for (i = 0; i < qconf->n_rx_queue; i++) {
466
467                         portid = qconf->rx_queue_list[i].portid;
468                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
469                                                  MAX_PKT_BURST);
470
471                         /* Prefetch first packets */
472                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
473                                 rte_prefetch0(rte_pktmbuf_mtod(
474                                                 pkts_burst[j], void *));
475                         }
476
477                         /* Prefetch and forward already prefetched packets */
478                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
479                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
480                                                 j + PREFETCH_OFFSET], void *));
481                                 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
482                         }
483
484                         /* Forward remaining prefetched packets */
485                         for (; j < nb_rx; j++) {
486                                 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
487                         }
488                 }
489         }
490 }
491
492 /* display usage */
493 static void
494 print_usage(const char *prgname)
495 {
496         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
497                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
498                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
499                prgname);
500 }
501
502 static int
503 parse_portmask(const char *portmask)
504 {
505         char *end = NULL;
506         unsigned long pm;
507
508         /* parse hexadecimal string */
509         pm = strtoul(portmask, &end, 16);
510         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
511                 return -1;
512
513         if (pm == 0)
514                 return -1;
515
516         return pm;
517 }
518
519 static int
520 parse_nqueue(const char *q_arg)
521 {
522         char *end = NULL;
523         unsigned long n;
524
525         /* parse hexadecimal string */
526         n = strtoul(q_arg, &end, 10);
527         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
528                 return -1;
529         if (n == 0)
530                 return -1;
531         if (n >= MAX_RX_QUEUE_PER_LCORE)
532                 return -1;
533
534         return n;
535 }
536
537 /* Parse the argument given in the command line of the application */
538 static int
539 parse_args(int argc, char **argv)
540 {
541         int opt, ret;
542         char **argvopt;
543         int option_index;
544         char *prgname = argv[0];
545         static struct option lgopts[] = {
546                 {NULL, 0, 0, 0}
547         };
548
549         argvopt = argv;
550
551         while ((opt = getopt_long(argc, argvopt, "p:q:",
552                                   lgopts, &option_index)) != EOF) {
553
554                 switch (opt) {
555                 /* portmask */
556                 case 'p':
557                         enabled_port_mask = parse_portmask(optarg);
558                         if (enabled_port_mask < 0) {
559                                 printf("invalid portmask\n");
560                                 print_usage(prgname);
561                                 return -1;
562                         }
563                         break;
564
565                 /* nqueue */
566                 case 'q':
567                         rx_queue_per_lcore = parse_nqueue(optarg);
568                         if (rx_queue_per_lcore < 0) {
569                                 printf("invalid queue number\n");
570                                 print_usage(prgname);
571                                 return -1;
572                         }
573                         break;
574
575                 /* long options */
576                 case 0:
577                         print_usage(prgname);
578                         return -1;
579
580                 default:
581                         print_usage(prgname);
582                         return -1;
583                 }
584         }
585
586         if (enabled_port_mask == 0) {
587                 printf("portmask not specified\n");
588                 print_usage(prgname);
589                 return -1;
590         }
591
592         if (optind >= 0)
593                 argv[optind-1] = prgname;
594
595         ret = optind-1;
596         optind = 0; /* reset getopt lib */
597         return ret;
598 }
599
600 static void
601 print_ethaddr(const char *name, struct ether_addr *eth_addr)
602 {
603         printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
604                eth_addr->addr_bytes[0],
605                eth_addr->addr_bytes[1],
606                eth_addr->addr_bytes[2],
607                eth_addr->addr_bytes[3],
608                eth_addr->addr_bytes[4],
609                eth_addr->addr_bytes[5]);
610 }
611
612 /* Check the link status of all ports in up to 9s, and print them finally */
613 static void
614 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
615 {
616 #define CHECK_INTERVAL 100 /* 100ms */
617 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
618         uint8_t portid, count, all_ports_up, print_flag = 0;
619         struct rte_eth_link link;
620
621         printf("\nChecking link status");
622         fflush(stdout);
623         for (count = 0; count <= MAX_CHECK_TIME; count++) {
624                 all_ports_up = 1;
625                 for (portid = 0; portid < port_num; portid++) {
626                         if ((port_mask & (1 << portid)) == 0)
627                                 continue;
628                         memset(&link, 0, sizeof(link));
629                         rte_eth_link_get_nowait(portid, &link);
630                         /* print link status if flag set */
631                         if (print_flag == 1) {
632                                 if (link.link_status)
633                                         printf("Port %d Link Up - speed %u "
634                                                 "Mbps - %s\n", (uint8_t)portid,
635                                                 (unsigned)link.link_speed,
636                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
637                                         ("full-duplex") : ("half-duplex\n"));
638                                 else
639                                         printf("Port %d Link Down\n",
640                                                         (uint8_t)portid);
641                                 continue;
642                         }
643                         /* clear all_ports_up flag if any link down */
644                         if (link.link_status == 0) {
645                                 all_ports_up = 0;
646                                 break;
647                         }
648                 }
649                 /* after finally printing all link status, get out */
650                 if (print_flag == 1)
651                         break;
652
653                 if (all_ports_up == 0) {
654                         printf(".");
655                         fflush(stdout);
656                         rte_delay_ms(CHECK_INTERVAL);
657                 }
658
659                 /* set the print_flag if all ports up or timeout */
660                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
661                         print_flag = 1;
662                         printf("\ndone\n");
663                 }
664         }
665 }
666
667 static int
668 init_routing_table(void)
669 {
670         struct rte_lpm *lpm;
671         struct rte_lpm6 *lpm6;
672         int socket, ret;
673         unsigned i;
674
675         for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
676                 if (socket_lpm[socket]) {
677                         lpm = socket_lpm[socket];
678                         /* populate the LPM table */
679                         for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
680                                 ret = rte_lpm_add(lpm,
681                                         l3fwd_ipv4_route_array[i].ip,
682                                         l3fwd_ipv4_route_array[i].depth,
683                                         l3fwd_ipv4_route_array[i].if_out);
684
685                                 if (ret < 0) {
686                                         RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
687                                                 "LPM table\n", i);
688                                         return -1;
689                                 }
690
691                                 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
692                                                 "/%d (port %d)\n",
693                                         socket,
694                                         IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
695                                         l3fwd_ipv4_route_array[i].depth,
696                                         l3fwd_ipv4_route_array[i].if_out);
697                         }
698                 }
699
700                 if (socket_lpm6[socket]) {
701                         lpm6 = socket_lpm6[socket];
702                         /* populate the LPM6 table */
703                         for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
704                                 ret = rte_lpm6_add(lpm6,
705                                         l3fwd_ipv6_route_array[i].ip,
706                                         l3fwd_ipv6_route_array[i].depth,
707                                         l3fwd_ipv6_route_array[i].if_out);
708
709                                 if (ret < 0) {
710                                         RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
711                                                 "LPM6 table\n", i);
712                                         return -1;
713                                 }
714
715                                 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
716                                                 "/%d (port %d)\n",
717                                         socket,
718                                         IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
719                                         l3fwd_ipv6_route_array[i].depth,
720                                         l3fwd_ipv6_route_array[i].if_out);
721                         }
722                 }
723         }
724         return 0;
725 }
726
727 static int
728 init_mem(void)
729 {
730         char buf[PATH_MAX];
731         struct rte_mempool *mp;
732         struct rte_lpm *lpm;
733         struct rte_lpm6 *lpm6;
734         int socket;
735         unsigned lcore_id;
736
737         /* traverse through lcores and initialize structures on each socket */
738
739         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
740
741                 if (rte_lcore_is_enabled(lcore_id) == 0)
742                         continue;
743
744                 socket = rte_lcore_to_socket_id(lcore_id);
745
746                 if (socket == SOCKET_ID_ANY)
747                         socket = 0;
748
749                 if (socket_direct_pool[socket] == NULL) {
750                         RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
751                                         socket);
752                         snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
753
754                         mp = rte_mempool_create(buf, NB_MBUF,
755                                                    MBUF_SIZE, 32,
756                                                    sizeof(struct rte_pktmbuf_pool_private),
757                                                    rte_pktmbuf_pool_init, NULL,
758                                                    rte_pktmbuf_init, NULL,
759                                                    socket, 0);
760                         if (mp == NULL) {
761                                 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
762                                 return -1;
763                         }
764                         socket_direct_pool[socket] = mp;
765                 }
766
767                 if (socket_indirect_pool[socket] == NULL) {
768                         RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
769                                         socket);
770                         snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
771
772                         mp = rte_mempool_create(buf, NB_MBUF,
773                                                            sizeof(struct rte_mbuf), 32,
774                                                            0,
775                                                            NULL, NULL,
776                                                            rte_pktmbuf_init, NULL,
777                                                            socket, 0);
778                         if (mp == NULL) {
779                                 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
780                                 return -1;
781                         }
782                         socket_indirect_pool[socket] = mp;
783                 }
784
785                 if (socket_lpm[socket] == NULL) {
786                         RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
787                         snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
788
789                         lpm = rte_lpm_create(buf, socket, LPM_MAX_RULES, 0);
790                         if (lpm == NULL) {
791                                 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
792                                 return -1;
793                         }
794                         socket_lpm[socket] = lpm;
795                 }
796
797                 if (socket_lpm6[socket] == NULL) {
798                         RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
799                         snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
800
801                         lpm6 = rte_lpm6_create("IP_FRAG_LPM6", socket, &lpm6_config);
802                         if (lpm6 == NULL) {
803                                 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
804                                 return -1;
805                         }
806                         socket_lpm6[socket] = lpm6;
807                 }
808         }
809
810         return 0;
811 }
812
813 int
814 MAIN(int argc, char **argv)
815 {
816         struct lcore_queue_conf *qconf;
817         struct rte_eth_dev_info dev_info;
818         struct rte_eth_txconf *txconf;
819         struct rx_queue *rxq;
820         int socket, ret;
821         unsigned nb_ports;
822         uint16_t queueid = 0;
823         unsigned lcore_id = 0, rx_lcore_id = 0;
824         uint32_t n_tx_queue, nb_lcores;
825         uint8_t portid;
826
827         /* init EAL */
828         ret = rte_eal_init(argc, argv);
829         if (ret < 0)
830                 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
831         argc -= ret;
832         argv += ret;
833
834         /* parse application arguments (after the EAL ones) */
835         ret = parse_args(argc, argv);
836         if (ret < 0)
837                 rte_exit(EXIT_FAILURE, "Invalid arguments");
838
839         nb_ports = rte_eth_dev_count();
840         if (nb_ports > RTE_MAX_ETHPORTS)
841                 nb_ports = RTE_MAX_ETHPORTS;
842         else if (nb_ports == 0)
843                 rte_exit(EXIT_FAILURE, "No ports found!\n");
844
845         nb_lcores = rte_lcore_count();
846
847         /* initialize structures (mempools, lpm etc.) */
848         if (init_mem() < 0)
849                 rte_panic("Cannot initialize memory structures!\n");
850
851         /* check if portmask has non-existent ports */
852         if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
853                 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
854
855         /* initialize all ports */
856         for (portid = 0; portid < nb_ports; portid++) {
857                 /* skip ports that are not enabled */
858                 if ((enabled_port_mask & (1 << portid)) == 0) {
859                         printf("Skipping disabled port %d\n", portid);
860                         continue;
861                 }
862
863                 qconf = &lcore_queue_conf[rx_lcore_id];
864
865                 /* get the lcore_id for this port */
866                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
867                        qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
868
869                         rx_lcore_id ++;
870                         if (rx_lcore_id >= RTE_MAX_LCORE)
871                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
872
873                         qconf = &lcore_queue_conf[rx_lcore_id];
874                 }
875
876                 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
877                 if (socket == SOCKET_ID_ANY)
878                         socket = 0;
879
880                 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
881                 rxq->portid = portid;
882                 rxq->direct_pool = socket_direct_pool[socket];
883                 rxq->indirect_pool = socket_indirect_pool[socket];
884                 rxq->lpm = socket_lpm[socket];
885                 rxq->lpm6 = socket_lpm6[socket];
886                 qconf->n_rx_queue++;
887
888                 /* init port */
889                 printf("Initializing port %d on lcore %u...", portid,
890                        rx_lcore_id);
891                 fflush(stdout);
892
893                 n_tx_queue = nb_lcores;
894                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
895                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
896                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
897                                             &port_conf);
898                 if (ret < 0) {
899                         printf("\n");
900                         rte_exit(EXIT_FAILURE, "Cannot configure device: "
901                                 "err=%d, port=%d\n",
902                                 ret, portid);
903                 }
904
905                 /* init one RX queue */
906                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
907                                              socket, NULL,
908                                              socket_direct_pool[socket]);
909                 if (ret < 0) {
910                         printf("\n");
911                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
912                                 "err=%d, port=%d\n",
913                                 ret, portid);
914                 }
915
916                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
917                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
918                 printf("\n");
919
920                 /* init one TX queue per couple (lcore,port) */
921                 queueid = 0;
922                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
923                         if (rte_lcore_is_enabled(lcore_id) == 0)
924                                 continue;
925
926                         socket = (int) rte_lcore_to_socket_id(lcore_id);
927                         printf("txq=%u,%d ", lcore_id, queueid);
928                         fflush(stdout);
929
930                         rte_eth_dev_info_get(portid, &dev_info);
931                         txconf = &dev_info.default_txconf;
932                         txconf->txq_flags = 0;
933                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
934                                                      socket, txconf);
935                         if (ret < 0) {
936                                 printf("\n");
937                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
938                                         "err=%d, port=%d\n", ret, portid);
939                         }
940
941                         qconf = &lcore_queue_conf[lcore_id];
942                         qconf->tx_queue_id[portid] = queueid;
943                         queueid++;
944                 }
945
946                 printf("\n");
947         }
948
949         printf("\n");
950
951         /* start ports */
952         for (portid = 0; portid < nb_ports; portid++) {
953                 if ((enabled_port_mask & (1 << portid)) == 0) {
954                         continue;
955                 }
956                 /* Start device */
957                 ret = rte_eth_dev_start(portid);
958                 if (ret < 0)
959                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
960                                 ret, portid);
961
962                 rte_eth_promiscuous_enable(portid);
963         }
964
965         if (init_routing_table() < 0)
966                 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
967
968         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
969
970         /* launch per-lcore init on every lcore */
971         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
972         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
973                 if (rte_eal_wait_lcore(lcore_id) < 0)
974                         return -1;
975         }
976
977         return 0;
978 }