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