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