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