examples: numa updates
[dpdk.git] / examples / l3fwd / main.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.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_ip.h>
72 #include <rte_tcp.h>
73 #include <rte_udp.h>
74 #include <rte_string_fns.h>
75
76 #include "main.h"
77
78 #define APP_LOOKUP_EXACT_MATCH          0
79 #define APP_LOOKUP_LPM                  1
80 #define DO_RFC_1812_CHECKS
81
82 //#define APP_LOOKUP_METHOD             APP_LOOKUP_EXACT_MATCH
83 #ifndef APP_LOOKUP_METHOD
84 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
85 #endif
86
87 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
88 #include <rte_hash.h>
89 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
90 #include <rte_lpm.h>
91 #else
92 #error "APP_LOOKUP_METHOD set to incorrect value"
93 #endif
94
95 #ifndef IPv6_BYTES
96 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
97                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
98 #define IPv6_BYTES(addr) \
99         addr[0],  addr[1], addr[2],  addr[3], \
100         addr[4],  addr[5], addr[6],  addr[7], \
101         addr[8],  addr[9], addr[10], addr[11],\
102         addr[12], addr[13],addr[14], addr[15]
103 #endif
104
105
106 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
107
108 #define MAX_PORTS 32
109
110 #define MAX_JUMBO_PKT_LEN  9600
111
112 #define IPV6_ADDR_LEN 16
113
114 #define MEMPOOL_CACHE_SIZE 256
115
116 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
117
118 /*
119  * This expression is used to calculate the number of mbufs needed depending on user input, taking
120  *  into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
121  *  RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
122  */
123
124 #define NB_MBUF RTE_MAX (                                                                                                                                       \
125                                 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT +                                                        \
126                                 nb_ports*nb_lcores*MAX_PKT_BURST +                                                                                      \
127                                 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT +                                                          \
128                                 nb_lcores*MEMPOOL_CACHE_SIZE),                                                                                          \
129                                 (unsigned)8192)
130
131 /*
132  * RX and TX Prefetch, Host, and Write-back threshold values should be
133  * carefully set for optimal performance. Consult the network
134  * controller's datasheet and supporting DPDK documentation for guidance
135  * on how these parameters should be set.
136  */
137 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
138 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
139 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
140
141 /*
142  * These default values are optimized for use with the Intel(R) 82599 10 GbE
143  * Controller and the DPDK ixgbe PMD. Consider using other values for other
144  * network controllers and/or network drivers.
145  */
146 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
147 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
148 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
149
150 #define MAX_PKT_BURST 32
151 #define BURST_TX_DRAIN 200000ULL /* around 100us at 2 Ghz */
152
153 #define NB_SOCKETS 8
154
155 /* Configure how many packets ahead to prefetch, when reading packets */
156 #define PREFETCH_OFFSET 3
157
158 /*
159  * Configurable number of RX/TX ring descriptors
160  */
161 #define RTE_TEST_RX_DESC_DEFAULT 128
162 #define RTE_TEST_TX_DESC_DEFAULT 512
163 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
164 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
165
166 /* ethernet addresses of ports */
167 static struct ether_addr ports_eth_addr[MAX_PORTS];
168
169 /* mask of enabled ports */
170 static uint32_t enabled_port_mask = 0;
171 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
172 static int numa_on = 1; /**< NUMA is enabled by default. */
173
174 struct mbuf_table {
175         uint16_t len;
176         struct rte_mbuf *m_table[MAX_PKT_BURST];
177 };
178
179 struct lcore_rx_queue {
180         uint8_t port_id;
181         uint8_t queue_id;
182 } __rte_cache_aligned;
183
184 #define MAX_RX_QUEUE_PER_LCORE 16
185 #define MAX_TX_QUEUE_PER_PORT MAX_PORTS
186 #define MAX_RX_QUEUE_PER_PORT 128
187
188 #define MAX_LCORE_PARAMS 1024
189 struct lcore_params {
190         uint8_t port_id;
191         uint8_t queue_id;
192         uint8_t lcore_id;
193 } __rte_cache_aligned;
194
195 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
196 static struct lcore_params lcore_params_array_default[] = {
197         {0, 0, 2},
198         {0, 1, 2},
199         {0, 2, 2},
200         {1, 0, 2},
201         {1, 1, 2},
202         {1, 2, 2},
203         {2, 0, 2},
204         {3, 0, 3},
205         {3, 1, 3},
206 };
207
208 static struct lcore_params * lcore_params = lcore_params_array_default;
209 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
210                                 sizeof(lcore_params_array_default[0]);
211
212 static struct rte_eth_conf port_conf = {
213         .rxmode = {
214                 .max_rx_pkt_len = ETHER_MAX_LEN,
215                 .split_hdr_size = 0,
216                 .header_split   = 0, /**< Header Split disabled */
217                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
218                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
219                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
220                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
221         },
222         .rx_adv_conf = {
223                 .rss_conf = {
224                         .rss_key = NULL,
225                         .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
226                 },
227         },
228         .txmode = {
229                 .mq_mode = ETH_MQ_TX_NONE,
230         },
231 };
232
233 static const struct rte_eth_rxconf rx_conf = {
234         .rx_thresh = {
235                 .pthresh = RX_PTHRESH,
236                 .hthresh = RX_HTHRESH,
237                 .wthresh = RX_WTHRESH,
238         },
239         .rx_free_thresh = 32,
240 };
241
242 static const struct rte_eth_txconf tx_conf = {
243         .tx_thresh = {
244                 .pthresh = TX_PTHRESH,
245                 .hthresh = TX_HTHRESH,
246                 .wthresh = TX_WTHRESH,
247         },
248         .tx_free_thresh = 0, /* Use PMD default values */
249         .tx_rs_thresh = 0, /* Use PMD default values */
250         .txq_flags = 0x0,
251 };
252
253 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
254
255
256 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
257
258 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
259 #include <rte_hash_crc.h>
260 #define DEFAULT_HASH_FUNC       rte_hash_crc
261 #else
262 #include <rte_jhash.h>
263 #define DEFAULT_HASH_FUNC       rte_jhash
264 #endif
265
266 struct ipv4_5tuple {
267         uint32_t ip_dst;
268         uint32_t ip_src;
269         uint16_t port_dst;
270         uint16_t port_src;
271         uint8_t  proto;
272 } __attribute__((__packed__));
273
274 struct ipv6_5tuple {
275         uint8_t  ip_dst[IPV6_ADDR_LEN];
276         uint8_t  ip_src[IPV6_ADDR_LEN];
277         uint16_t port_dst;
278         uint16_t port_src;
279         uint8_t  proto;
280 } __attribute__((__packed__));
281
282 struct ipv4_l3fwd_route {
283         struct ipv4_5tuple key;
284         uint8_t if_out;
285 };
286
287 struct ipv6_l3fwd_route {
288         struct ipv6_5tuple key;
289         uint8_t if_out;
290 };
291
292 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
293         {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
294         {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
295         {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
296         {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
297 };
298
299 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
300         {
301                 {
302                         {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303                          0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
304                         {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305                          0x02, 0x1e, 0x67, 0xff, 0xfe, 0x0d, 0xb6, 0x0a},
306                          1, 10, IPPROTO_UDP
307                 }, 4
308         },
309 };
310
311 typedef struct rte_hash lookup_struct_t;
312 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
313 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
314
315 #define L3FWD_HASH_ENTRIES      1024
316
317 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
318         .name = "ipv4_l3fwd_hash_0",
319         .entries = L3FWD_HASH_ENTRIES,
320         .bucket_entries = 4,
321         .key_len = sizeof(struct ipv4_5tuple),
322         .hash_func = DEFAULT_HASH_FUNC,
323         .hash_func_init_val = 0,
324         .socket_id = 0,
325 };
326
327 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
328         .name = "ipv6_l3fwd_hash_0",
329         .entries = L3FWD_HASH_ENTRIES,
330         .bucket_entries = 4,
331         .key_len = sizeof(struct ipv6_5tuple),
332         .hash_func = DEFAULT_HASH_FUNC,
333         .hash_func_init_val = 0,
334         .socket_id = 0,
335 };
336
337 #define IPV4_L3FWD_NUM_ROUTES \
338         (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
339
340 #define IPV6_L3FWD_NUM_ROUTES \
341         (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
342
343 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
344 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
345 #endif
346
347 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
348 struct ipv4_l3fwd_route {
349         uint32_t ip;
350         uint8_t  depth;
351         uint8_t  if_out;
352 };
353
354 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
355         {IPv4(1,1,1,0), 24, 0},
356         {IPv4(2,1,1,0), 24, 1},
357         {IPv4(3,1,1,0), 24, 2},
358         {IPv4(4,1,1,0), 24, 3},
359         {IPv4(5,1,1,0), 24, 4},
360         {IPv4(6,1,1,0), 24, 5},
361         {IPv4(7,1,1,0), 24, 6},
362         {IPv4(8,1,1,0), 24, 7},
363 };
364
365 #define IPV4_L3FWD_NUM_ROUTES \
366         (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
367
368 #define IPV4_L3FWD_LPM_MAX_RULES     1024
369
370 typedef struct rte_lpm lookup_struct_t;
371 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
372 #endif
373
374 struct lcore_conf {
375         uint16_t n_rx_queue;
376         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
377         uint16_t tx_queue_id[MAX_PORTS];
378         struct mbuf_table tx_mbufs[MAX_PORTS];
379         lookup_struct_t * ipv4_lookup_struct;
380         lookup_struct_t * ipv6_lookup_struct;
381 } __rte_cache_aligned;
382
383 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
384
385 /* Send burst of packets on an output interface */
386 static inline int
387 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
388 {
389         struct rte_mbuf **m_table;
390         int ret;
391         uint16_t queueid;
392
393         queueid = qconf->tx_queue_id[port];
394         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
395
396         ret = rte_eth_tx_burst(port, queueid, m_table, n);
397         if (unlikely(ret < n)) {
398                 do {
399                         rte_pktmbuf_free(m_table[ret]);
400                 } while (++ret < n);
401         }
402
403         return 0;
404 }
405
406 /* Enqueue a single packet, and send burst if queue is filled */
407 static inline int
408 send_single_packet(struct rte_mbuf *m, uint8_t port)
409 {
410         uint32_t lcore_id;
411         uint16_t len;
412         struct lcore_conf *qconf;
413
414         lcore_id = rte_lcore_id();
415
416         qconf = &lcore_conf[lcore_id];
417         len = qconf->tx_mbufs[port].len;
418         qconf->tx_mbufs[port].m_table[len] = m;
419         len++;
420
421         /* enough pkts to be sent */
422         if (unlikely(len == MAX_PKT_BURST)) {
423                 send_burst(qconf, MAX_PKT_BURST, port);
424                 len = 0;
425         }
426
427         qconf->tx_mbufs[port].len = len;
428         return 0;
429 }
430
431 #ifdef DO_RFC_1812_CHECKS
432 static inline int
433 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
434 {
435         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
436         /*
437          * 1. The packet length reported by the Link Layer must be large
438          * enough to hold the minimum length legal IP datagram (20 bytes).
439          */
440         if (link_len < sizeof(struct ipv4_hdr))
441                 return -1;
442
443         /* 2. The IP checksum must be correct. */
444         /* this is checked in H/W */
445
446         /*
447          * 3. The IP version number must be 4. If the version number is not 4
448          * then the packet may be another version of IP, such as IPng or
449          * ST-II.
450          */
451         if (((pkt->version_ihl) >> 4) != 4)
452                 return -3;
453         /*
454          * 4. The IP header length field must be large enough to hold the
455          * minimum length legal IP datagram (20 bytes = 5 words).
456          */
457         if ((pkt->version_ihl & 0xf) < 5)
458                 return -4;
459
460         /*
461          * 5. The IP total length field must be large enough to hold the IP
462          * datagram header, whose length is specified in the IP header length
463          * field.
464          */
465         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
466                 return -5;
467
468         return 0;
469 }
470 #endif
471
472 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
473 static void
474 print_ipv4_key(struct ipv4_5tuple key)
475 {
476         printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
477                         (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
478 }
479 static void
480 print_ipv6_key(struct ipv6_5tuple key)
481 {
482         printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
483                 "port dst = %d, port src = %d, proto = %d\n",
484                 IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
485                 key.port_dst, key.port_src, key.proto);
486 }
487
488 static inline uint8_t
489 get_ipv4_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
490 {
491         struct ipv4_5tuple key;
492         struct tcp_hdr *tcp;
493         struct udp_hdr *udp;
494         int ret = 0;
495
496         key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
497         key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
498         key.proto = ipv4_hdr->next_proto_id;
499
500         switch (ipv4_hdr->next_proto_id) {
501         case IPPROTO_TCP:
502                 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
503                                         sizeof(struct ipv4_hdr));
504                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
505                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
506                 break;
507
508         case IPPROTO_UDP:
509                 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
510                                         sizeof(struct ipv4_hdr));
511                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
512                 key.port_src = rte_be_to_cpu_16(udp->src_port);
513                 break;
514
515         default:
516                 key.port_dst = 0;
517                 key.port_src = 0;
518                 break;
519         }
520
521         /* Find destination port */
522         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
523         return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
524 }
525
526 static inline uint8_t
527 get_ipv6_dst_port(struct ipv6_hdr *ipv6_hdr,  uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
528 {
529         struct ipv6_5tuple key;
530         struct tcp_hdr *tcp;
531         struct udp_hdr *udp;
532         int ret = 0;
533
534         memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
535         memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
536
537         key.proto = ipv6_hdr->proto;
538
539         switch (ipv6_hdr->proto) {
540         case IPPROTO_TCP:
541                 tcp = (struct tcp_hdr *)((unsigned char *) ipv6_hdr +
542                                         sizeof(struct ipv6_hdr));
543                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
544                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
545                 break;
546
547         case IPPROTO_UDP:
548                 udp = (struct udp_hdr *)((unsigned char *) ipv6_hdr +
549                                         sizeof(struct ipv6_hdr));
550                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
551                 key.port_src = rte_be_to_cpu_16(udp->src_port);
552                 break;
553
554         default:
555                 key.port_dst = 0;
556                 key.port_src = 0;
557                 break;
558         }
559
560         /* Find destination port */
561         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
562         return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
563 }
564 #endif
565
566 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
567 static inline uint8_t
568 get_ipv4_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
569 {
570         uint8_t next_hop;
571
572         return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
573                         rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
574                         next_hop : portid);
575 }
576 #endif
577
578 static inline void
579 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
580 {
581         struct ether_hdr *eth_hdr;
582         struct ipv4_hdr *ipv4_hdr;
583         void *d_addr_bytes;
584         uint8_t dst_port;
585
586         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
587
588         if (m->ol_flags & PKT_RX_IPV4_HDR) {
589                 /* Handle IPv4 headers.*/
590                 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
591                                 sizeof(struct ether_hdr));
592
593 #ifdef DO_RFC_1812_CHECKS
594                 /* Check to make sure the packet is valid (RFC1812) */
595                 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt.pkt_len) < 0) {
596                         rte_pktmbuf_free(m);
597                         return;
598                 }
599 #endif
600
601                 dst_port = get_ipv4_dst_port(ipv4_hdr, portid, qconf->ipv4_lookup_struct);
602                 if (dst_port >= MAX_PORTS || (enabled_port_mask & 1 << dst_port) == 0)
603                         dst_port = portid;
604
605                 /* 02:00:00:00:00:xx */
606                 d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
607                 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
608
609 #ifdef DO_RFC_1812_CHECKS
610                 /* Update time to live and header checksum */
611                 --(ipv4_hdr->time_to_live);
612                 ++(ipv4_hdr->hdr_checksum);
613 #endif
614
615                 /* src addr */
616                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
617
618                 send_single_packet(m, dst_port);
619         }
620         else {
621                 /* Handle IPv6 headers.*/
622 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
623                 struct ipv6_hdr *ipv6_hdr;
624
625                 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
626                                 sizeof(struct ether_hdr));
627
628                 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
629
630                 if (dst_port >= MAX_PORTS || (enabled_port_mask & 1 << dst_port) == 0)
631                         dst_port = portid;
632
633                 /* 02:00:00:00:00:xx */
634                 d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
635                 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
636
637                 /* src addr */
638                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
639
640                 send_single_packet(m, dst_port);
641 #else
642                 /* We don't currently handle IPv6 packets in LPM mode. */
643                 rte_pktmbuf_free(m);
644 #endif
645         }
646
647 }
648
649 /* main processing loop */
650 static __attribute__((noreturn)) int
651 main_loop(__attribute__((unused)) void *dummy)
652 {
653         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
654         unsigned lcore_id;
655         uint64_t prev_tsc = 0;
656         uint64_t diff_tsc, cur_tsc;
657         int i, j, nb_rx;
658         uint8_t portid, queueid;
659         struct lcore_conf *qconf;
660
661         lcore_id = rte_lcore_id();
662         qconf = &lcore_conf[lcore_id];
663
664         if (qconf->n_rx_queue == 0) {
665                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
666                 while(1);
667         }
668
669         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
670
671         for (i = 0; i < qconf->n_rx_queue; i++) {
672
673                 portid = qconf->rx_queue_list[i].port_id;
674                 queueid = qconf->rx_queue_list[i].queue_id;
675                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
676                         portid, queueid);
677         }
678
679         while (1) {
680
681                 cur_tsc = rte_rdtsc();
682
683                 /*
684                  * TX burst queue drain
685                  */
686                 diff_tsc = cur_tsc - prev_tsc;
687                 if (unlikely(diff_tsc > BURST_TX_DRAIN)) {
688
689                         /*
690                          * This could be optimized (use queueid instead of
691                          * portid), but it is not called so often
692                          */
693                         for (portid = 0; portid < MAX_PORTS; portid++) {
694                                 if (qconf->tx_mbufs[portid].len == 0)
695                                         continue;
696                                 send_burst(&lcore_conf[lcore_id],
697                                         qconf->tx_mbufs[portid].len,
698                                         portid);
699                                 qconf->tx_mbufs[portid].len = 0;
700                         }
701
702                         prev_tsc = cur_tsc;
703                 }
704
705                 /*
706                  * Read packet from RX queues
707                  */
708                 for (i = 0; i < qconf->n_rx_queue; ++i) {
709
710                         portid = qconf->rx_queue_list[i].port_id;
711                         queueid = qconf->rx_queue_list[i].queue_id;
712                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
713
714                         /* Prefetch first packets */
715                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
716                                 rte_prefetch0(rte_pktmbuf_mtod(
717                                                 pkts_burst[j], void *));
718                         }
719
720                         /* Prefetch and forward already prefetched packets */
721                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
722                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
723                                                 j + PREFETCH_OFFSET], void *));
724                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
725                         }
726
727                         /* Forward remaining prefetched packets */
728                         for (; j < nb_rx; j++) {
729                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
730                         }
731                 }
732         }
733 }
734
735 static int
736 check_lcore_params(void)
737 {
738         uint8_t queue, lcore;
739         uint16_t i;
740         int socketid;
741
742         for (i = 0; i < nb_lcore_params; ++i) {
743                 queue = lcore_params[i].queue_id;
744                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
745                         printf("invalid queue number: %hhu\n", queue);
746                         return -1;
747                 }
748                 lcore = lcore_params[i].lcore_id;
749                 if (!rte_lcore_is_enabled(lcore)) {
750                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
751                         return -1;
752                 }
753                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
754                         (numa_on == 0)) {
755                         printf("warning: lcore %hhu is on socket %d with numa off \n",
756                                 lcore, socketid);
757                 }
758         }
759         return 0;
760 }
761
762 static int
763 check_port_config(const unsigned nb_ports)
764 {
765         unsigned portid;
766         uint16_t i;
767
768         for (i = 0; i < nb_lcore_params; ++i) {
769                 portid = lcore_params[i].port_id;
770                 if ((enabled_port_mask & (1 << portid)) == 0) {
771                         printf("port %u is not enabled in port mask\n", portid);
772                         return -1;
773                 }
774                 if (portid >= nb_ports) {
775                         printf("port %u is not present on the board\n", portid);
776                         return -1;
777                 }
778         }
779         return 0;
780 }
781
782 static uint8_t
783 get_port_n_rx_queues(const uint8_t port)
784 {
785         int queue = -1;
786         uint16_t i;
787
788         for (i = 0; i < nb_lcore_params; ++i) {
789                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
790                         queue = lcore_params[i].queue_id;
791         }
792         return (uint8_t)(++queue);
793 }
794
795 static int
796 init_lcore_rx_queues(void)
797 {
798         uint16_t i, nb_rx_queue;
799         uint8_t lcore;
800
801         for (i = 0; i < nb_lcore_params; ++i) {
802                 lcore = lcore_params[i].lcore_id;
803                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
804                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
805                         printf("error: too many queues (%u) for lcore: %u\n",
806                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
807                         return -1;
808                 } else {
809                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
810                                 lcore_params[i].port_id;
811                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
812                                 lcore_params[i].queue_id;
813                         lcore_conf[lcore].n_rx_queue++;
814                 }
815         }
816         return 0;
817 }
818
819 /* display usage */
820 static void
821 print_usage(const char *prgname)
822 {
823         printf ("%s [EAL options] -- -p PORTMASK -P"
824                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]"
825                 "  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
826                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
827                 "  -P : enable promiscuous mode\n"
828                 "  --config (port,queue,lcore): rx queues configuration\n"
829                 "  --no-numa: optional, disable numa awareness\n"
830                 "  --enable-jumbo: enable jumbo frame"
831                 " which max packet len is PKTLEN in decimal (64-9600)\n",
832                 prgname);
833 }
834
835 static int parse_max_pkt_len(const char *pktlen)
836 {
837         char *end = NULL;
838         unsigned long len;
839
840         /* parse decimal string */
841         len = strtoul(pktlen, &end, 10);
842         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
843                 return -1;
844
845         if (len == 0)
846                 return -1;
847
848         return len;
849 }
850
851 static int
852 parse_portmask(const char *portmask)
853 {
854         char *end = NULL;
855         unsigned long pm;
856
857         /* parse hexadecimal string */
858         pm = strtoul(portmask, &end, 16);
859         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
860                 return -1;
861
862         if (pm == 0)
863                 return -1;
864
865         return pm;
866 }
867
868 static int
869 parse_config(const char *q_arg)
870 {
871         char s[256];
872         const char *p, *p0 = q_arg;
873         char *end;
874         enum fieldnames {
875                 FLD_PORT = 0,
876                 FLD_QUEUE,
877                 FLD_LCORE,
878                 _NUM_FLD
879         };
880         unsigned long int_fld[_NUM_FLD];
881         char *str_fld[_NUM_FLD];
882         int i;
883         unsigned size;
884
885         nb_lcore_params = 0;
886
887         while ((p = strchr(p0,'(')) != NULL) {
888                 ++p;
889                 if((p0 = strchr(p,')')) == NULL)
890                         return -1;
891
892                 size = p0 - p;
893                 if(size >= sizeof(s))
894                         return -1;
895
896                 rte_snprintf(s, sizeof(s), "%.*s", size, p);
897                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
898                         return -1;
899                 for (i = 0; i < _NUM_FLD; i++){
900                         errno = 0;
901                         int_fld[i] = strtoul(str_fld[i], &end, 0);
902                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
903                                 return -1;
904                 }
905                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
906                         printf("exceeded max number of lcore params: %hu\n",
907                                 nb_lcore_params);
908                         return -1;
909                 }
910                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
911                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
912                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
913                 ++nb_lcore_params;
914         }
915         lcore_params = lcore_params_array;
916         return 0;
917 }
918
919 /* Parse the argument given in the command line of the application */
920 static int
921 parse_args(int argc, char **argv)
922 {
923         int opt, ret;
924         char **argvopt;
925         int option_index;
926         char *prgname = argv[0];
927         static struct option lgopts[] = {
928                 {"config", 1, 0, 0},
929                 {"no-numa", 0, 0, 0},
930                 {"enable-jumbo", 0, 0, 0},
931                 {NULL, 0, 0, 0}
932         };
933
934         argvopt = argv;
935
936         while ((opt = getopt_long(argc, argvopt, "p:P",
937                                 lgopts, &option_index)) != EOF) {
938
939                 switch (opt) {
940                 /* portmask */
941                 case 'p':
942                         enabled_port_mask = parse_portmask(optarg);
943                         if (enabled_port_mask == 0) {
944                                 printf("invalid portmask\n");
945                                 print_usage(prgname);
946                                 return -1;
947                         }
948                         break;
949                 case 'P':
950                         printf("Promiscuous mode selected\n");
951                         promiscuous_on = 1;
952                         break;
953
954                 /* long options */
955                 case 0:
956                         if (!strncmp(lgopts[option_index].name, "config", 6)) {
957                                 ret = parse_config(optarg);
958                                 if (ret) {
959                                         printf("invalid config\n");
960                                         print_usage(prgname);
961                                         return -1;
962                                 }
963                         }
964
965                         if (!strncmp(lgopts[option_index].name, "no-numa", 7)) {
966                                 printf("numa is disabled \n");
967                                 numa_on = 0;
968                         }
969                         
970                         if (!strncmp(lgopts[option_index].name, "enable-jumbo", 12)) {
971                                 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
972
973                                 printf("jumbo frame is enabled \n");
974                                 port_conf.rxmode.jumbo_frame = 1;
975         
976                                 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */        
977                                 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
978                                         ret = parse_max_pkt_len(optarg);
979                                         if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
980                                                 printf("invalid packet length\n");
981                                                 print_usage(prgname);
982                                                 return -1;
983                                         }
984                                         port_conf.rxmode.max_rx_pkt_len = ret;
985                                 }
986                                 printf("set jumbo frame max packet length to %u\n", 
987                                                 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
988                         }
989                         
990                         break;
991
992                 default:
993                         print_usage(prgname);
994                         return -1;
995                 }
996         }
997
998         if (optind >= 0)
999                 argv[optind-1] = prgname;
1000
1001         ret = optind-1;
1002         optind = 0; /* reset getopt lib */
1003         return ret;
1004 }
1005
1006 static void
1007 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1008 {
1009         printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
1010                 eth_addr->addr_bytes[0],
1011                 eth_addr->addr_bytes[1],
1012                 eth_addr->addr_bytes[2],
1013                 eth_addr->addr_bytes[3],
1014                 eth_addr->addr_bytes[4],
1015                 eth_addr->addr_bytes[5]);
1016 }
1017
1018 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1019 static void
1020 setup_hash(int socketid)
1021 {
1022         unsigned i;
1023         int ret;
1024         char s[64];
1025
1026         /* create ipv4 hash */
1027         rte_snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1028         ipv4_l3fwd_hash_params.name = s;
1029         ipv4_l3fwd_hash_params.socket_id = socketid;
1030         ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
1031         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1032                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1033                                 "socket %d\n", socketid);
1034
1035         /* create ipv6 hash */
1036         rte_snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1037         ipv6_l3fwd_hash_params.name = s;
1038         ipv6_l3fwd_hash_params.socket_id = socketid;
1039         ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
1040         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1041                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1042                                 "socket %d\n", socketid);
1043
1044
1045         /* populate the ipv4 hash */
1046         for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
1047                 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
1048                                 (void *) &ipv4_l3fwd_route_array[i].key);
1049                 if (ret < 0) {
1050                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1051                                 "l3fwd hash on socket %d\n", i, socketid);
1052                 }
1053                 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
1054                 printf("Hash: Adding key\n");
1055                 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
1056         }
1057
1058         /* populate the ipv6 hash */
1059         for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
1060                 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
1061                                 (void *) &ipv6_l3fwd_route_array[i].key);
1062                 if (ret < 0) {
1063                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1064                                 "l3fwd hash on socket %d\n", i, socketid);
1065                 }
1066                 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
1067                 printf("Hash: Adding key\n");
1068                 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
1069         }
1070 }
1071 #endif
1072
1073 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1074 static void
1075 setup_lpm(int socketid)
1076 {
1077         unsigned i;
1078         int ret;
1079         char s[64];
1080
1081         /* create the LPM table */
1082         rte_snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1083         ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
1084                                 IPV4_L3FWD_LPM_MAX_RULES, 0);
1085         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1086                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1087                                 " on socket %d\n", socketid);
1088
1089         /* populate the LPM table */
1090         for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
1091                 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1092                         ipv4_l3fwd_route_array[i].ip,
1093                         ipv4_l3fwd_route_array[i].depth,
1094                         ipv4_l3fwd_route_array[i].if_out);
1095
1096                 if (ret < 0) {
1097                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1098                                 "l3fwd LPM table on socket %d\n",
1099                                 i, socketid);
1100                 }
1101
1102                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
1103                         (unsigned)ipv4_l3fwd_route_array[i].ip,
1104                         ipv4_l3fwd_route_array[i].depth,
1105                         ipv4_l3fwd_route_array[i].if_out);
1106         }
1107 }
1108 #endif
1109
1110 static int
1111 init_mem(unsigned nb_mbuf)
1112 {
1113         struct lcore_conf *qconf;
1114         int socketid;
1115         unsigned lcore_id;
1116         char s[64];
1117
1118         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1119                 if (rte_lcore_is_enabled(lcore_id) == 0)
1120                         continue;
1121
1122                 if (numa_on)
1123                         socketid = rte_lcore_to_socket_id(lcore_id);
1124                 else
1125                         socketid = 0;
1126
1127                 if (socketid >= NB_SOCKETS) {
1128                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
1129                                 socketid, lcore_id, NB_SOCKETS);
1130                 }
1131                 if (pktmbuf_pool[socketid] == NULL) {
1132                         rte_snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1133                         pktmbuf_pool[socketid] =
1134                                 rte_mempool_create(s, nb_mbuf, MBUF_SIZE, MEMPOOL_CACHE_SIZE,
1135                                         sizeof(struct rte_pktmbuf_pool_private),
1136                                         rte_pktmbuf_pool_init, NULL,
1137                                         rte_pktmbuf_init, NULL,
1138                                         socketid, 0);
1139                         if (pktmbuf_pool[socketid] == NULL)
1140                                 rte_exit(EXIT_FAILURE,
1141                                                 "Cannot init mbuf pool on socket %d\n", socketid);
1142                         else
1143                                 printf("Allocated mbuf pool on socket %d\n", socketid);
1144
1145 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1146                         setup_lpm(socketid);
1147 #else
1148                         setup_hash(socketid);
1149 #endif
1150                 }
1151                 qconf = &lcore_conf[lcore_id];
1152                 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1153 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1154                 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1155 #endif
1156         }
1157         return 0;
1158 }
1159
1160 /* Check the link status of all ports in up to 9s, and print them finally */
1161 static void
1162 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1163 {
1164 #define CHECK_INTERVAL 100 /* 100ms */
1165 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1166         uint8_t portid, count, all_ports_up, print_flag = 0;
1167         struct rte_eth_link link;
1168
1169         printf("\nChecking link status");
1170         fflush(stdout);
1171         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1172                 all_ports_up = 1;
1173                 for (portid = 0; portid < port_num; portid++) {
1174                         if ((port_mask & (1 << portid)) == 0)
1175                                 continue;
1176                         memset(&link, 0, sizeof(link));
1177                         rte_eth_link_get_nowait(portid, &link);
1178                         /* print link status if flag set */
1179                         if (print_flag == 1) {
1180                                 if (link.link_status)
1181                                         printf("Port %d Link Up - speed %u "
1182                                                 "Mbps - %s\n", (uint8_t)portid,
1183                                                 (unsigned)link.link_speed,
1184                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1185                                         ("full-duplex") : ("half-duplex\n"));
1186                                 else
1187                                         printf("Port %d Link Down\n",
1188                                                 (uint8_t)portid);
1189                                 continue;
1190                         }
1191                         /* clear all_ports_up flag if any link down */
1192                         if (link.link_status == 0) {
1193                                 all_ports_up = 0;
1194                                 break;
1195                         }
1196                 }
1197                 /* after finally printing all link status, get out */
1198                 if (print_flag == 1)
1199                         break;
1200
1201                 if (all_ports_up == 0) {
1202                         printf(".");
1203                         fflush(stdout);
1204                         rte_delay_ms(CHECK_INTERVAL);
1205                 }
1206
1207                 /* set the print_flag if all ports up or timeout */
1208                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1209                         print_flag = 1;
1210                         printf("done\n");
1211                 }
1212         }
1213 }
1214
1215 int
1216 MAIN(int argc, char **argv)
1217 {
1218         struct lcore_conf *qconf;
1219         int ret;
1220         unsigned nb_ports;
1221         uint16_t queueid;
1222         unsigned lcore_id;
1223         uint32_t n_tx_queue, nb_lcores;
1224         uint8_t portid, nb_rx_queue, queue, socketid;
1225
1226         /* init EAL */
1227         ret = rte_eal_init(argc, argv);
1228         if (ret < 0)
1229                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1230         argc -= ret;
1231         argv += ret;
1232
1233         /* parse application arguments (after the EAL ones) */
1234         ret = parse_args(argc, argv);
1235         if (ret < 0)
1236                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1237
1238         if (check_lcore_params() < 0)
1239                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1240
1241         ret = init_lcore_rx_queues();
1242         if (ret < 0)
1243                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1244
1245
1246         /* init driver(s) */
1247         if (rte_pmd_init_all() < 0)
1248                 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
1249
1250         if (rte_eal_pci_probe() < 0)
1251                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
1252
1253         nb_ports = rte_eth_dev_count();
1254         if (nb_ports > MAX_PORTS)
1255                 nb_ports = MAX_PORTS;
1256
1257         if (check_port_config(nb_ports) < 0)
1258                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1259
1260         nb_lcores = rte_lcore_count();
1261
1262         /* initialize all ports */
1263         for (portid = 0; portid < nb_ports; portid++) {
1264                 /* skip ports that are not enabled */
1265                 if ((enabled_port_mask & (1 << portid)) == 0) {
1266                         printf("\nSkipping disabled port %d\n", portid);
1267                         continue;
1268                 }
1269
1270                 /* init port */
1271                 printf("Initializing port %d ... ", portid );
1272                 fflush(stdout);
1273
1274                 nb_rx_queue = get_port_n_rx_queues(portid);
1275                 n_tx_queue = nb_lcores;
1276                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1277                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1278                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1279                         nb_rx_queue, (unsigned)n_tx_queue );
1280                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1281                                         (uint16_t)n_tx_queue, &port_conf);
1282                 if (ret < 0)
1283                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1284                                 ret, portid);
1285
1286                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1287                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1288                 printf(", ");
1289
1290                 /* init memory */
1291                 ret = init_mem(NB_MBUF);
1292                 if (ret < 0)
1293                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1294
1295                 /* init one TX queue per couple (lcore,port) */
1296                 queueid = 0;
1297                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1298                         if (rte_lcore_is_enabled(lcore_id) == 0)
1299                                 continue;
1300
1301                         if (numa_on)
1302                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1303                         else
1304                                 socketid = 0;
1305
1306                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1307                         fflush(stdout);
1308                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1309                                                      socketid, &tx_conf);
1310                         if (ret < 0)
1311                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1312                                         "port=%d\n", ret, portid);
1313
1314                         qconf = &lcore_conf[lcore_id];
1315                         qconf->tx_queue_id[portid] = queueid;
1316                         queueid++;
1317                 }
1318                 printf("\n");
1319         }
1320
1321         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1322                 if (rte_lcore_is_enabled(lcore_id) == 0)
1323                         continue;
1324                 qconf = &lcore_conf[lcore_id];
1325                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1326                 fflush(stdout);
1327                 /* init RX queues */
1328                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1329                         portid = qconf->rx_queue_list[queue].port_id;
1330                         queueid = qconf->rx_queue_list[queue].queue_id;
1331
1332                         if (numa_on)
1333                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1334                         else
1335                                 socketid = 0;
1336
1337                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1338                         fflush(stdout);
1339
1340                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1341                                         socketid, &rx_conf, pktmbuf_pool[socketid]);
1342                         if (ret < 0)
1343                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1344                                                 "port=%d\n", ret, portid);
1345                 }
1346         }
1347
1348         printf("\n");
1349
1350         /* start ports */
1351         for (portid = 0; portid < nb_ports; portid++) {
1352                 if ((enabled_port_mask & (1 << portid)) == 0) {
1353                         continue;
1354                 }
1355                 /* Start device */
1356                 ret = rte_eth_dev_start(portid);
1357                 if (ret < 0)
1358                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1359                                 ret, portid);
1360
1361                 /*
1362                  * If enabled, put device in promiscuous mode.
1363                  * This allows IO forwarding mode to forward packets
1364                  * to itself through 2 cross-connected  ports of the
1365                  * target machine.
1366                  */
1367                 if (promiscuous_on)
1368                         rte_eth_promiscuous_enable(portid);
1369         }
1370
1371         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
1372
1373         /* launch per-lcore init on every lcore */
1374         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1375         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1376                 if (rte_eal_wait_lcore(lcore_id) < 0)
1377                         return -1;
1378         }
1379
1380         return 0;
1381 }