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