first public release
[dpdk.git] / examples / l3fwd / main.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <sys/queue.h>
43 #include <stdarg.h>
44 #include <errno.h>
45 #include <getopt.h>
46
47 #include <rte_common.h>
48 #include <rte_byteorder.h>
49 #include <rte_log.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_tailq.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
64 #include <rte_pci.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_ring.h>
70 #include <rte_mempool.h>
71 #include <rte_mbuf.h>
72 #include <rte_ip.h>
73 #include <rte_tcp.h>
74 #include <rte_udp.h>
75 #include <rte_string_fns.h>
76
77 #include "main.h"
78
79 #define APP_LOOKUP_EXACT_MATCH          0
80 #define APP_LOOKUP_LPM                  1
81 #define DO_RFC_1812_CHECKS
82
83 //#define APP_LOOKUP_METHOD             APP_LOOKUP_EXACT_MATCH
84 #ifndef APP_LOOKUP_METHOD
85 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
86 #endif
87
88 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
89 #include <rte_hash.h>
90 #include <rte_hash_crc.h>
91 #include <rte_jhash.h>
92 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
93 #include <rte_lpm.h>
94 #else
95 #error "APP_LOOKUP_METHOD set to incorrect value"
96 #endif
97
98 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
99
100 #define MAX_PORTS 32
101
102 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
103 #define NB_MBUF   8192
104
105 /*
106  * RX and TX Prefetch, Host, and Write-back threshold values should be
107  * carefully set for optimal performance. Consult the network
108  * controller's datasheet and supporting DPDK documentation for guidance
109  * on how these parameters should be set.
110  */
111 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
112 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
113 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
114
115 /*
116  * These default values are optimized for use with the Intel(R) 82599 10 GbE
117  * Controller and the DPDK ixgbe PMD. Consider using other values for other
118  * network controllers and/or network drivers.
119  */
120 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
121 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
122 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
123
124 #define MAX_PKT_BURST 32
125 #define BURST_TX_DRAIN 200000ULL /* around 100us at 2 Ghz */
126
127 #define NB_SOCKETS 8
128
129 #define SOCKET0 0
130
131 /* Configure how many packets ahead to prefetch, when reading packets */
132 #define PREFETCH_OFFSET 3
133
134 /*
135  * Configurable number of RX/TX ring descriptors
136  */
137 #define RTE_TEST_RX_DESC_DEFAULT 128
138 #define RTE_TEST_TX_DESC_DEFAULT 512
139 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
140 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
141
142 /* ethernet addresses of ports */
143 static struct ether_addr ports_eth_addr[MAX_PORTS];
144
145 /* mask of enabled ports */
146 static uint32_t enabled_port_mask = 0;
147 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
148 static int numa_on = 1; /**< NUMA is enabled by default. */
149
150 struct mbuf_table {
151         uint16_t len;
152         struct rte_mbuf *m_table[MAX_PKT_BURST];
153 };
154
155 struct lcore_rx_queue {
156         uint8_t port_id;
157         uint8_t queue_id;
158 } __rte_cache_aligned;
159
160 #define MAX_RX_QUEUE_PER_LCORE 16
161 #define MAX_TX_QUEUE_PER_PORT MAX_PORTS
162 #define MAX_RX_QUEUE_PER_PORT 128
163
164 #define MAX_LCORE_PARAMS 1024
165 struct lcore_params {
166         uint8_t port_id;
167         uint8_t queue_id;
168         uint8_t lcore_id;
169 } __rte_cache_aligned;
170
171 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
172 static struct lcore_params lcore_params_array_default[] = {
173         {0, 0, 2},
174         {0, 1, 2},
175         {0, 2, 2},
176         {1, 0, 2},
177         {1, 1, 2},
178         {1, 2, 2},
179         {2, 0, 2},
180         {3, 0, 3},
181         {3, 1, 3},
182 };
183
184 static struct lcore_params * lcore_params = lcore_params_array_default;
185 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
186                                 sizeof(lcore_params_array_default[0]);
187
188 static struct rte_eth_conf port_conf = {
189         .rxmode = {
190                 .split_hdr_size = 0,
191                 .header_split   = 0, /**< Header Split disabled */
192                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
193                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
194                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
195                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
196         },
197         .rx_adv_conf = {
198                 .rss_conf = {
199                         .rss_key = NULL,
200                         .rss_hf = ETH_RSS_IPV4,
201                 },
202         },
203         .txmode = {
204         },
205 };
206
207 static const struct rte_eth_rxconf rx_conf = {
208         .rx_thresh = {
209                 .pthresh = RX_PTHRESH,
210                 .hthresh = RX_HTHRESH,
211                 .wthresh = RX_WTHRESH,
212         },
213 };
214
215 static const struct rte_eth_txconf tx_conf = {
216         .tx_thresh = {
217                 .pthresh = TX_PTHRESH,
218                 .hthresh = TX_HTHRESH,
219                 .wthresh = TX_WTHRESH,
220         },
221         .tx_free_thresh = 0, /* Use PMD default values */
222         .tx_rs_thresh = 0, /* Use PMD default values */
223 };
224
225 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
226
227
228 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
229 struct ipv4_5tuple {
230         uint32_t ip_dst;
231         uint32_t ip_src;
232         uint16_t port_dst;
233         uint16_t port_src;
234         uint8_t proto;
235 } __attribute__((__packed__));
236
237 struct l3fwd_route {
238         struct ipv4_5tuple key;
239         uint8_t if_out;
240 };
241
242 static struct l3fwd_route l3fwd_route_array[] = {
243         {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
244         {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
245         {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
246         {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
247 };
248
249 typedef struct rte_hash lookup_struct_t;
250 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
251
252 #define L3FWD_HASH_ENTRIES      1024
253 struct rte_hash_parameters l3fwd_hash_params = {
254         .name = "l3fwd_hash_0",
255         .entries = L3FWD_HASH_ENTRIES,
256         .bucket_entries = 4,
257         .key_len = sizeof(struct ipv4_5tuple),
258         .hash_func = rte_hash_crc,
259         .hash_func_init_val = 0,
260         .socket_id = SOCKET0,
261 };
262
263 #define L3FWD_NUM_ROUTES \
264         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
265
266 static uint8_t l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
267 #endif
268
269 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
270 struct l3fwd_route {
271         uint32_t ip;
272         uint8_t  depth;
273         uint8_t  if_out;
274 };
275
276 static struct l3fwd_route l3fwd_route_array[] = {
277         {IPv4(1,1,1,0), 24, 0},
278         {IPv4(2,1,1,0), 24, 1},
279         {IPv4(3,1,1,0), 24, 2},
280         {IPv4(4,1,1,0), 24, 3},
281         {IPv4(5,1,1,0), 24, 4},
282         {IPv4(6,1,1,0), 24, 5},
283         {IPv4(7,1,1,0), 24, 6},
284         {IPv4(8,1,1,0), 24, 7},
285 };
286
287 #define L3FWD_NUM_ROUTES \
288         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
289
290 #define L3FWD_LPM_MAX_RULES     1024
291
292 typedef struct rte_lpm lookup_struct_t;
293 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
294 #endif
295
296 struct lcore_conf {
297         uint16_t n_rx_queue;
298         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
299         uint16_t tx_queue_id[MAX_PORTS];
300         struct mbuf_table tx_mbufs[MAX_PORTS];
301         lookup_struct_t * lookup_struct;
302 } __rte_cache_aligned;
303
304 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
305
306 /* Send burst of packets on an output interface */
307 static inline int
308 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
309 {
310         struct rte_mbuf **m_table;
311         int ret;
312         uint16_t queueid;
313
314         queueid = qconf->tx_queue_id[port];
315         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
316
317         ret = rte_eth_tx_burst(port, queueid, m_table, n);
318         if (unlikely(ret < n)) {
319                 do {
320                         rte_pktmbuf_free(m_table[ret]);
321                 } while (++ret < n);
322         }
323
324         return 0;
325 }
326
327 /* Enqueue a single packet, and send burst if queue is filled */
328 static inline int
329 send_single_packet(struct rte_mbuf *m, uint8_t port)
330 {
331         uint32_t lcore_id;
332         uint16_t len;
333         struct lcore_conf *qconf;
334
335         lcore_id = rte_lcore_id();
336
337         qconf = &lcore_conf[lcore_id];
338         len = qconf->tx_mbufs[port].len;
339         qconf->tx_mbufs[port].m_table[len] = m;
340         len++;
341
342         /* enough pkts to be sent */
343         if (unlikely(len == MAX_PKT_BURST)) {
344                 send_burst(qconf, MAX_PKT_BURST, port);
345                 len = 0;
346         }
347
348         qconf->tx_mbufs[port].len = len;
349         return 0;
350 }
351
352 #ifdef DO_RFC_1812_CHECKS
353 static inline int
354 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
355 {
356         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
357         /*
358          * 1. The packet length reported by the Link Layer must be large
359          * enough to hold the minimum length legal IP datagram (20 bytes).
360          */
361         if (link_len < sizeof(struct ipv4_hdr))
362                 return -1;
363
364         /* 2. The IP checksum must be correct. */
365         /* this is checked in H/W */
366
367         /*
368          * 3. The IP version number must be 4. If the version number is not 4
369          * then the packet may be another version of IP, such as IPng or
370          * ST-II.
371          */
372         if (((pkt->version_ihl) >> 4) != 4)
373                 return -3;
374         /*
375          * 4. The IP header length field must be large enough to hold the
376          * minimum length legal IP datagram (20 bytes = 5 words).
377          */
378         if ((pkt->version_ihl & 0xf) < 5)
379                 return -4;
380
381         /*
382          * 5. The IP total length field must be large enough to hold the IP
383          * datagram header, whose length is specified in the IP header length
384          * field.
385          */
386         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
387                 return -5;
388
389         return 0;
390 }
391 #endif
392
393 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
394 static void
395 print_key(struct ipv4_5tuple key)
396 {
397         printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
398                (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
399 }
400
401 static inline uint8_t
402 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
403 {
404         struct ipv4_5tuple key;
405         struct tcp_hdr *tcp;
406         struct udp_hdr *udp;
407         int ret = 0;
408
409         key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
410         key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
411         key.proto = ipv4_hdr->next_proto_id;
412
413         switch (ipv4_hdr->next_proto_id) {
414         case IPPROTO_TCP:
415                 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
416                                         sizeof(struct ipv4_hdr));
417                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
418                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
419                 break;
420
421         case IPPROTO_UDP:
422                 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
423                                         sizeof(struct ipv4_hdr));
424                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
425                 key.port_src = rte_be_to_cpu_16(udp->src_port);
426                 break;
427
428         default:
429                 key.port_dst = 0;
430                 key.port_src = 0;
431         }
432
433         /* Find destination port */
434         ret = rte_hash_lookup(l3fwd_lookup_struct, (const void *)&key);
435         return (uint8_t)((ret < 0)? portid : l3fwd_out_if[ret]);
436 }
437 #endif
438
439 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
440 static inline uint8_t
441 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
442 {
443         uint8_t next_hop;
444
445         return (uint8_t) ((rte_lpm_lookup(l3fwd_lookup_struct,
446                         rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
447                         next_hop : portid);
448 }
449 #endif
450
451 static inline void
452 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
453 {
454         struct ether_hdr *eth_hdr;
455         struct ipv4_hdr *ipv4_hdr;
456         void *tmp;
457         uint8_t dst_port;
458
459         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
460
461         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
462                                 sizeof(struct ether_hdr));
463
464 #ifdef DO_RFC_1812_CHECKS
465         /* Check to make sure the packet is valid (RFC1812) */
466         if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt.pkt_len) < 0) {
467                 rte_pktmbuf_free(m);
468                 return;
469         }
470 #endif
471
472         dst_port = get_dst_port(ipv4_hdr, portid, l3fwd_lookup_struct);
473         if (dst_port >= MAX_PORTS || (enabled_port_mask & 1 << dst_port) == 0)
474                 dst_port = portid;
475
476         /* 00:09:c0:00:00:xx */
477         tmp = &eth_hdr->d_addr.addr_bytes[0];
478         *((uint64_t *)tmp) = 0x000000c00900 + (dst_port << 24);
479
480 #ifdef DO_RFC_1812_CHECKS
481         /* Update time to live and header checksum */
482         --(ipv4_hdr->time_to_live);
483         ++(ipv4_hdr->hdr_checksum);
484 #endif
485
486         /* src addr */
487         ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
488
489         send_single_packet(m, dst_port);
490
491 }
492
493 /* main processing loop */
494 static __attribute__((noreturn)) int
495 main_loop(__attribute__((unused)) void *dummy)
496 {
497         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
498         unsigned lcore_id;
499         uint64_t prev_tsc = 0;
500         uint64_t diff_tsc, cur_tsc;
501         int i, j, nb_rx;
502         uint8_t portid, queueid;
503         struct lcore_conf *qconf;
504
505         lcore_id = rte_lcore_id();
506         qconf = &lcore_conf[lcore_id];
507
508         if (qconf->n_rx_queue == 0) {
509                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
510                 while(1);
511         }
512
513         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
514
515         for (i = 0; i < qconf->n_rx_queue; i++) {
516
517                 portid = qconf->rx_queue_list[i].port_id;
518                 queueid = qconf->rx_queue_list[i].queue_id;
519                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
520                         portid, queueid);
521         }
522
523         while (1) {
524
525                 cur_tsc = rte_rdtsc();
526
527                 /*
528                  * TX burst queue drain
529                  */
530                 diff_tsc = cur_tsc - prev_tsc;
531                 if (unlikely(diff_tsc > BURST_TX_DRAIN)) {
532
533                         /*
534                          * This could be optimized (use queueid instead of
535                          * portid), but it is not called so often
536                          */
537                         for (portid = 0; portid < MAX_PORTS; portid++) {
538                                 if (qconf->tx_mbufs[portid].len == 0)
539                                         continue;
540                                 send_burst(&lcore_conf[lcore_id],
541                                         qconf->tx_mbufs[portid].len,
542                                         portid);
543                                 qconf->tx_mbufs[portid].len = 0;
544                         }
545
546                         prev_tsc = cur_tsc;
547                 }
548
549                 /*
550                  * Read packet from RX queues
551                  */
552                 for (i = 0; i < qconf->n_rx_queue; ++i) {
553
554                         portid = qconf->rx_queue_list[i].port_id;
555                         queueid = qconf->rx_queue_list[i].queue_id;
556                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
557
558                         /* Prefetch first packets */
559                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
560                                 rte_prefetch0(rte_pktmbuf_mtod(
561                                                 pkts_burst[j], void *));
562                         }
563
564                         /* Prefetch and forward already prefetched packets */
565                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
566                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
567                                                 j + PREFETCH_OFFSET], void *));
568                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
569                         }
570
571                         /* Forward remaining prefetched packets */
572                         for (; j < nb_rx; j++) {
573                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
574                         }
575                 }
576         }
577 }
578
579 static int
580 check_lcore_params(void)
581 {
582         uint8_t queue, lcore;
583         uint16_t i;
584         int socketid;
585
586         for (i = 0; i < nb_lcore_params; ++i) {
587                 queue = lcore_params[i].queue_id;
588                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
589                         printf("invalid queue number: %hhu\n", queue);
590                         return -1;
591                 }
592                 lcore = lcore_params[i].lcore_id;
593                 if (!rte_lcore_is_enabled(lcore)) {
594                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
595                         return -1;
596                 }
597                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
598                         (numa_on == 0)) {
599                         printf("warning: lcore %hhu is on socket %d with numa off \n",
600                                 lcore, socketid);
601                 }
602         }
603         return 0;
604 }
605
606 static int
607 check_port_config(const unsigned nb_ports)
608 {
609         unsigned portid;
610         uint16_t i;
611
612         for (i = 0; i < nb_lcore_params; ++i) {
613                 portid = lcore_params[i].port_id;
614                 if ((enabled_port_mask & (1 << portid)) == 0) {
615                         printf("port %u is not enabled in port mask\n", portid);
616                         return -1;
617                 }
618                 if (portid >= nb_ports) {
619                         printf("port %u is not present on the board\n", portid);
620                         return -1;
621                 }
622         }
623         return 0;
624 }
625
626 static uint8_t
627 get_port_n_rx_queues(const uint8_t port)
628 {
629         int queue = -1;
630         uint16_t i;
631
632         for (i = 0; i < nb_lcore_params; ++i) {
633                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
634                         queue = lcore_params[i].queue_id;
635         }
636         return (uint8_t)(++queue);
637 }
638
639 static int
640 init_lcore_rx_queues(void)
641 {
642         uint16_t i, nb_rx_queue;
643         uint8_t lcore;
644
645         for (i = 0; i < nb_lcore_params; ++i) {
646                 lcore = lcore_params[i].lcore_id;
647                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
648                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
649                         printf("error: too many queues (%u) for lcore: %u\n",
650                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
651                         return -1;
652                 } else {
653                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
654                                 lcore_params[i].port_id;
655                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
656                                 lcore_params[i].queue_id;
657                         lcore_conf[lcore].n_rx_queue++;
658                 }
659         }
660         return 0;
661 }
662
663 /* display usage */
664 static void
665 print_usage(const char *prgname)
666 {
667         printf ("%s [EAL options] -- -p PORTMASK -P"
668                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]\n"
669                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
670                 "  --config (port,queue,lcore): rx queues configuration\n"
671                 "  --no-numa: optional, disable numa awareness\n",
672                 prgname);
673 }
674
675 static int
676 parse_portmask(const char *portmask)
677 {
678         char *end = NULL;
679         unsigned long pm;
680
681         /* parse hexadecimal string */
682         pm = strtoul(portmask, &end, 16);
683         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
684                 return -1;
685
686         if (pm == 0)
687                 return -1;
688
689         return pm;
690 }
691
692 static int
693 parse_config(const char *q_arg)
694 {
695         char s[256];
696         const char *p, *p0 = q_arg;
697         char *end;
698         enum fieldnames {
699                 FLD_PORT = 0,
700                 FLD_QUEUE,
701                 FLD_LCORE,
702                 _NUM_FLD
703         };
704         unsigned long int_fld[_NUM_FLD];
705         char *str_fld[_NUM_FLD];
706         int i;
707         unsigned size;
708
709         nb_lcore_params = 0;
710
711         while ((p = strchr(p0,'(')) != NULL) {
712                 ++p;
713                 if((p0 = strchr(p,')')) == NULL)
714                         return -1;
715
716                 size = p0 - p;
717                 if(size >= sizeof(s))
718                         return -1;
719
720                 rte_snprintf(s, sizeof(s), "%.*s", size, p);
721                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
722                         return -1;
723                 for (i = 0; i < _NUM_FLD; i++){
724                         errno = 0;
725                         int_fld[i] = strtoul(str_fld[i], &end, 0);
726                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
727                                 return -1;
728                 }
729                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
730                         printf("exceeded max number of lcore params: %hu\n",
731                                 nb_lcore_params);
732                         return -1;
733                 }
734                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
735                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
736                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
737                 ++nb_lcore_params;
738         }
739         lcore_params = lcore_params_array;
740         return 0;
741 }
742
743 /* Parse the argument given in the command line of the application */
744 static int
745 parse_args(int argc, char **argv)
746 {
747         int opt, ret;
748         char **argvopt;
749         int option_index;
750         char *prgname = argv[0];
751         static struct option lgopts[] = {
752                 {"config", 1, 0, 0},
753                 {"no-numa", 0, 0, 0},
754                 {NULL, 0, 0, 0}
755         };
756
757         argvopt = argv;
758
759         while ((opt = getopt_long(argc, argvopt, "p:P",
760                                 lgopts, &option_index)) != EOF) {
761
762                 switch (opt) {
763                 /* portmask */
764                 case 'p':
765                         enabled_port_mask = parse_portmask(optarg);
766                         if (enabled_port_mask == 0) {
767                                 printf("invalid portmask\n");
768                                 print_usage(prgname);
769                                 return -1;
770                         }
771                         break;
772                 case 'P':
773                         printf("Promiscuous mode selected\n");
774                         promiscuous_on = 1;
775                         break;
776
777                 /* long options */
778                 case 0:
779                         if (!strcmp(lgopts[option_index].name, "config")) {
780                                 ret = parse_config(optarg);
781                                 if (ret) {
782                                         printf("invalid config\n");
783                                         print_usage(prgname);
784                                         return -1;
785                                 }
786                         }
787
788                         if (!strcmp(lgopts[option_index].name, "no-numa")) {
789                                 printf("numa is disabled \n");
790                                 numa_on = 0;
791                         }
792                         break;
793
794                 default:
795                         print_usage(prgname);
796                         return -1;
797                 }
798         }
799
800         if (optind >= 0)
801                 argv[optind-1] = prgname;
802
803         ret = optind-1;
804         optind = 0; /* reset getopt lib */
805         return ret;
806 }
807
808 static void
809 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
810 {
811         printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
812                 eth_addr->addr_bytes[0],
813                 eth_addr->addr_bytes[1],
814                 eth_addr->addr_bytes[2],
815                 eth_addr->addr_bytes[3],
816                 eth_addr->addr_bytes[4],
817                 eth_addr->addr_bytes[5]);
818 }
819
820 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
821 static void
822 setup_hash(int socketid)
823 {
824         unsigned i;
825         int ret;
826         char s[64];
827
828         /* create  hashes */
829         rte_snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
830         l3fwd_hash_params.name = s;
831         l3fwd_hash_params.socket_id = socketid;
832         l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
833         if (l3fwd_lookup_struct[socketid] == NULL)
834                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
835                                 "socket %d\n", socketid);
836
837         /* populate the hash */
838         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
839                 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
840                                 (void *) &l3fwd_route_array[i].key);
841                 if (ret < 0) {
842                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
843                                 "l3fwd hash on socket %d\n", i, socketid);
844                 }
845                 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
846                 printf("Hash: Adding key\n");
847                 print_key(l3fwd_route_array[i].key);
848         }
849 }
850 #endif
851
852 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
853 static void
854 setup_lpm(int socketid)
855 {
856         unsigned i;
857         int ret;
858         char s[64];
859
860         /* create the LPM table */
861         rte_snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
862         l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
863                                 L3FWD_LPM_MAX_RULES, RTE_LPM_MEMZONE);
864         if (l3fwd_lookup_struct[socketid] == NULL)
865                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
866                                 " on socket %d\n", socketid);
867
868         /* populate the LPM table */
869         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
870                 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
871                         l3fwd_route_array[i].ip,
872                         l3fwd_route_array[i].depth,
873                         l3fwd_route_array[i].if_out);
874
875                 if (ret < 0) {
876                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
877                                 "l3fwd LPM table on socket %d\n",
878                                 i, socketid);
879                 }
880
881                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
882                         (unsigned)l3fwd_route_array[i].ip,
883                         l3fwd_route_array[i].depth,
884                         l3fwd_route_array[i].if_out);
885         }
886 }
887 #endif
888
889 static int
890 init_mem(void)
891 {
892         struct lcore_conf *qconf;
893         int socketid;
894         unsigned lcore_id;
895         char s[64];
896
897         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
898                 if (rte_lcore_is_enabled(lcore_id) == 0)
899                         continue;
900
901                 if (numa_on)
902                         socketid = rte_lcore_to_socket_id(lcore_id);
903                 else
904                         socketid = 0;
905
906                 if (socketid >= NB_SOCKETS) {
907                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
908                                 socketid, lcore_id, NB_SOCKETS);
909                 }
910                 if (pktmbuf_pool[socketid] == NULL) {
911                         rte_snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
912                         pktmbuf_pool[socketid] =
913                                 rte_mempool_create(s, NB_MBUF, MBUF_SIZE, 32,
914                                         sizeof(struct rte_pktmbuf_pool_private),
915                                         rte_pktmbuf_pool_init, NULL,
916                                         rte_pktmbuf_init, NULL,
917                                         socketid, 0);
918                         if (pktmbuf_pool[socketid] == NULL)
919                                 rte_exit(EXIT_FAILURE,
920                                                 "Cannot init mbuf pool on socket %d\n", socketid);
921                         else
922                                 printf("Allocated mbuf pool on socket %d\n", socketid);
923
924 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
925                         setup_lpm(socketid);
926 #else
927                         setup_hash(socketid);
928 #endif
929                 }
930                 qconf = &lcore_conf[lcore_id];
931                 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
932         }
933         return 0;
934 }
935
936 int
937 MAIN(int argc, char **argv)
938 {
939         struct lcore_conf *qconf;
940         struct rte_eth_link link;
941         int ret;
942         unsigned nb_ports;
943         uint16_t queueid;
944         unsigned lcore_id;
945         uint32_t n_tx_queue, nb_lcores;
946         uint8_t portid, nb_rx_queue, queue, socketid;
947
948         /* init EAL */
949         ret = rte_eal_init(argc, argv);
950         if (ret < 0)
951                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
952         argc -= ret;
953         argv += ret;
954
955         /* parse application arguments (after the EAL ones) */
956         ret = parse_args(argc, argv);
957         if (ret < 0)
958                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
959
960         if (check_lcore_params() < 0)
961                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
962
963         ret = init_lcore_rx_queues();
964         if (ret < 0)
965                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
966
967         ret = init_mem();
968         if (ret < 0)
969                 rte_exit(EXIT_FAILURE, "init_mem failed\n");
970
971         /* init driver */
972 #ifdef RTE_LIBRTE_IGB_PMD
973         if (rte_igb_pmd_init() < 0)
974                 rte_exit(EXIT_FAILURE, "Cannot init igb pmd\n");
975 #endif
976 #ifdef RTE_LIBRTE_IXGBE_PMD
977         if (rte_ixgbe_pmd_init() < 0)
978                 rte_exit(EXIT_FAILURE, "Cannot init ixgbe pmd\n");
979 #endif
980
981         if (rte_eal_pci_probe() < 0)
982                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
983
984         nb_ports = rte_eth_dev_count();
985         if (nb_ports > MAX_PORTS)
986                 nb_ports = MAX_PORTS;
987
988         if (check_port_config(nb_ports) < 0)
989                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
990
991         nb_lcores = rte_lcore_count();
992
993         /* initialize all ports */
994         for (portid = 0; portid < nb_ports; portid++) {
995                 /* skip ports that are not enabled */
996                 if ((enabled_port_mask & (1 << portid)) == 0) {
997                         printf("\nSkipping disabled port %d\n", portid);
998                         continue;
999                 }
1000
1001                 /* init port */
1002                 printf("Initializing port %d ... ", portid );
1003                 fflush(stdout);
1004
1005                 nb_rx_queue = get_port_n_rx_queues(portid);
1006                 n_tx_queue = nb_lcores;
1007                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1008                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1009                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1010                         nb_rx_queue, (unsigned)n_tx_queue );
1011                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1012                                         (uint16_t)n_tx_queue, &port_conf);
1013                 if (ret < 0)
1014                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1015                                 ret, portid);
1016
1017                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1018                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1019                 printf(", ");
1020
1021
1022                 /* init one TX queue per couple (lcore,port) */
1023                 queueid = 0;
1024                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1025                         if (rte_lcore_is_enabled(lcore_id) == 0)
1026                                 continue;
1027
1028                         if (numa_on)
1029                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1030                         else
1031                                 socketid = 0;
1032
1033                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1034                         fflush(stdout);
1035                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1036                                                      socketid, &tx_conf);
1037                         if (ret < 0)
1038                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1039                                         "port=%d\n", ret, portid);
1040
1041                         qconf = &lcore_conf[lcore_id];
1042                         qconf->tx_queue_id[portid] = queueid;
1043                         queueid++;
1044                 }
1045                 printf("\n");
1046         }
1047
1048         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1049                 if (rte_lcore_is_enabled(lcore_id) == 0)
1050                         continue;
1051                 qconf = &lcore_conf[lcore_id];
1052                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1053                 fflush(stdout);
1054                 /* init RX queues */
1055                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1056                         portid = qconf->rx_queue_list[queue].port_id;
1057                         queueid = qconf->rx_queue_list[queue].queue_id;
1058
1059                         if (numa_on)
1060                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1061                         else
1062                                 socketid = 0;
1063
1064                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1065                         fflush(stdout);
1066
1067                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1068                                         socketid, &rx_conf, pktmbuf_pool[socketid]);
1069                         if (ret < 0)
1070                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1071                                                 "port=%d\n", ret, portid);
1072                 }
1073         }
1074
1075         printf("\n");
1076
1077         /* start ports */
1078         for (portid = 0; portid < nb_ports; portid++) {
1079                 if ((enabled_port_mask & (1 << portid)) == 0) {
1080                         continue;
1081                 }
1082                 /* Start device */
1083                 ret = rte_eth_dev_start(portid);
1084                 if (ret < 0)
1085                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1086                                 ret, portid);
1087
1088                 printf("done: Port %d ", portid);
1089
1090                 /* get link status */
1091                 rte_eth_link_get(portid, &link);
1092                 if (link.link_status) {
1093                         printf(" Link Up - speed %u Mbps - %s\n",
1094                                (unsigned) link.link_speed,
1095                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1096                                ("full-duplex") : ("half-duplex\n"));
1097                 } else {
1098                         printf(" Link Down\n");
1099                 }
1100                 /*
1101                  * If enabled, put device in promiscuous mode.
1102                  * This allows IO forwarding mode to forward packets
1103                  * to itself through 2 cross-connected  ports of the
1104                  * target machine.
1105                  */
1106                 if (promiscuous_on)
1107                         rte_eth_promiscuous_enable(portid);
1108         }
1109
1110         /* launch per-lcore init on every lcore */
1111         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1112         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1113                 if (rte_eal_wait_lcore(lcore_id) < 0)
1114                         return -1;
1115         }
1116
1117         return 0;
1118 }