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