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