doc: whitespace changes in licenses
[dpdk.git] / examples / l3fwd-vf / main.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <signal.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 MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
98 #define NB_MBUF   8192
99
100 /*
101  * RX and TX Prefetch, Host, and Write-back threshold values should be
102  * carefully set for optimal performance. Consult the network
103  * controller's datasheet and supporting DPDK documentation for guidance
104  * on how these parameters should be set.
105  */
106 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
107 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
108 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
109
110 /*
111  * These default values are optimized for use with the Intel(R) 82599 10 GbE
112  * Controller and the DPDK ixgbe PMD. Consider using other values for other
113  * network controllers and/or network drivers.
114  */
115 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
116 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
117 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
118
119 #define MAX_PKT_BURST 32
120 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
121
122 #define NB_SOCKETS 8
123
124 #define SOCKET0 0
125
126 /* Configure how many packets ahead to prefetch, when reading packets */
127 #define PREFETCH_OFFSET 3
128
129 /*
130  * Configurable number of RX/TX ring descriptors
131  */
132 #define RTE_TEST_RX_DESC_DEFAULT 128
133 #define RTE_TEST_TX_DESC_DEFAULT 512
134 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
135 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
136
137 /* ethernet addresses of ports */
138 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
139
140 /* mask of enabled ports */
141 static uint32_t enabled_port_mask = 0;
142 static int numa_on = 1; /**< NUMA is enabled by default. */
143
144 struct mbuf_table {
145         uint16_t len;
146         struct rte_mbuf *m_table[MAX_PKT_BURST];
147 };
148
149 struct lcore_rx_queue {
150         uint8_t port_id;
151         uint8_t queue_id;
152 } __rte_cache_aligned;
153
154 #define MAX_RX_QUEUE_PER_LCORE 16
155 #define MAX_TX_QUEUE_PER_PORT 1
156 #define MAX_RX_QUEUE_PER_PORT 1
157
158 #define MAX_LCORE_PARAMS 1024
159 struct lcore_params {
160         uint8_t port_id;
161         uint8_t queue_id;
162         uint8_t lcore_id;
163 } __rte_cache_aligned;
164
165 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
166 static struct lcore_params lcore_params_array_default[] = {
167         {0, 0, 2},
168         {0, 1, 2},
169         {0, 2, 2},
170         {1, 0, 2},
171         {1, 1, 2},
172         {1, 2, 2},
173         {2, 0, 2},
174         {3, 0, 3},
175         {3, 1, 3},
176 };
177
178 static struct lcore_params * lcore_params = lcore_params_array_default;
179 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
180                                 sizeof(lcore_params_array_default[0]);
181
182 static struct rte_eth_conf port_conf = {
183         .rxmode = {
184                 .split_hdr_size = 0,
185                 .header_split   = 0, /**< Header Split disabled */
186                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
187                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
188                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
189                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
190         },
191         .rx_adv_conf = {
192                 .rss_conf = {
193                         .rss_key = NULL,
194                         .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
195                 },
196         },
197         .txmode = {
198                 .mq_mode = ETH_MQ_TX_NONE,
199         },
200 };
201
202 static const struct rte_eth_rxconf rx_conf = {
203         .rx_thresh = {
204                 .pthresh = RX_PTHRESH,
205                 .hthresh = RX_HTHRESH,
206                 .wthresh = RX_WTHRESH,
207         },
208 };
209
210 static const struct rte_eth_txconf tx_conf = {
211         .tx_thresh = {
212                 .pthresh = TX_PTHRESH,
213                 .hthresh = TX_HTHRESH,
214                 .wthresh = TX_WTHRESH,
215         },
216         .tx_free_thresh = 0, /* Use PMD default values */
217         .tx_rs_thresh = 0, /* Use PMD default values */
218 };
219
220 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
221
222
223 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
224
225 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
226 #include <rte_hash_crc.h>
227 #define DEFAULT_HASH_FUNC       rte_hash_crc
228 #else
229 #include <rte_jhash.h>
230 #define DEFAULT_HASH_FUNC       rte_jhash
231 #endif
232
233 struct ipv4_5tuple {
234         uint32_t ip_dst;
235         uint32_t ip_src;
236         uint16_t port_dst;
237         uint16_t port_src;
238         uint8_t proto;
239 } __attribute__((__packed__));
240
241 struct l3fwd_route {
242         struct ipv4_5tuple key;
243         uint8_t if_out;
244 };
245
246 static struct l3fwd_route l3fwd_route_array[] = {
247         {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
248         {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
249         {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
250         {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
251 };
252
253 typedef struct rte_hash lookup_struct_t;
254 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
255
256 #define L3FWD_HASH_ENTRIES      1024
257 struct rte_hash_parameters l3fwd_hash_params = {
258         .name = "l3fwd_hash_0",
259         .entries = L3FWD_HASH_ENTRIES,
260         .bucket_entries = 4,
261         .key_len = sizeof(struct ipv4_5tuple),
262         .hash_func = DEFAULT_HASH_FUNC,
263         .hash_func_init_val = 0,
264         .socket_id = SOCKET0,
265 };
266
267 #define L3FWD_NUM_ROUTES \
268         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
269
270 static uint8_t l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
271 #endif
272
273 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
274 struct l3fwd_route {
275         uint32_t ip;
276         uint8_t  depth;
277         uint8_t  if_out;
278 };
279
280 static struct l3fwd_route l3fwd_route_array[] = {
281         {IPv4(1,1,1,0), 24, 0},
282         {IPv4(2,1,1,0), 24, 1},
283         {IPv4(3,1,1,0), 24, 2},
284         {IPv4(4,1,1,0), 24, 3},
285         {IPv4(5,1,1,0), 24, 4},
286         {IPv4(6,1,1,0), 24, 5},
287         {IPv4(7,1,1,0), 24, 6},
288         {IPv4(8,1,1,0), 24, 7},
289 };
290
291 #define L3FWD_NUM_ROUTES \
292         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
293
294 #define L3FWD_LPM_MAX_RULES     1024
295
296 typedef struct rte_lpm lookup_struct_t;
297 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
298 #endif
299
300 struct lcore_conf {
301         uint16_t n_rx_queue;
302         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
303         uint16_t tx_queue_id;
304         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
305         lookup_struct_t * lookup_struct;
306 } __rte_cache_aligned;
307
308 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
309
310 /* Send burst of packets on an output interface */
311 static inline int
312 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
313 {
314         struct rte_mbuf **m_table;
315         int ret;
316         uint16_t queueid;
317
318         queueid = qconf->tx_queue_id;
319         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
320
321         ret = rte_eth_tx_burst(port, queueid, m_table, n);
322         if (unlikely(ret < n)) {
323                 do {
324                         rte_pktmbuf_free(m_table[ret]);
325                 } while (++ret < n);
326         }
327
328         return 0;
329 }
330
331 /* Enqueue a single packet, and send burst if queue is filled */
332 static inline int
333 send_single_packet(struct rte_mbuf *m, uint8_t port)
334 {
335         uint32_t lcore_id;
336         uint16_t len;
337         struct lcore_conf *qconf;
338
339         lcore_id = rte_lcore_id();
340
341         qconf = &lcore_conf[lcore_id];
342         len = qconf->tx_mbufs[port].len;
343         qconf->tx_mbufs[port].m_table[len] = m;
344         len++;
345
346         /* enough pkts to be sent */
347         if (unlikely(len == MAX_PKT_BURST)) {
348                 send_burst(qconf, MAX_PKT_BURST, port);
349                 len = 0;
350         }
351
352         qconf->tx_mbufs[port].len = len;
353         return 0;
354 }
355
356 #ifdef DO_RFC_1812_CHECKS
357 static inline int
358 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
359 {
360         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
361         /*
362          * 1. The packet length reported by the Link Layer must be large
363          * enough to hold the minimum length legal IP datagram (20 bytes).
364          */
365         if (link_len < sizeof(struct ipv4_hdr))
366                 return -1;
367
368         /* 2. The IP checksum must be correct. */
369         /* this is checked in H/W */
370
371         /*
372          * 3. The IP version number must be 4. If the version number is not 4
373          * then the packet may be another version of IP, such as IPng or
374          * ST-II.
375          */
376         if (((pkt->version_ihl) >> 4) != 4)
377                 return -3;
378         /*
379          * 4. The IP header length field must be large enough to hold the
380          * minimum length legal IP datagram (20 bytes = 5 words).
381          */
382         if ((pkt->version_ihl & 0xf) < 5)
383                 return -4;
384
385         /*
386          * 5. The IP total length field must be large enough to hold the IP
387          * datagram header, whose length is specified in the IP header length
388          * field.
389          */
390         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
391                 return -5;
392
393         return 0;
394 }
395 #endif
396
397 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
398 static void
399 print_key(struct ipv4_5tuple key)
400 {
401         printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
402                (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
403 }
404
405 static inline uint8_t
406 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
407 {
408         struct ipv4_5tuple key;
409         struct tcp_hdr *tcp;
410         struct udp_hdr *udp;
411         int ret = 0;
412
413         key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
414         key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
415         key.proto = ipv4_hdr->next_proto_id;
416
417         switch (ipv4_hdr->next_proto_id) {
418         case IPPROTO_TCP:
419                 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
420                                         sizeof(struct ipv4_hdr));
421                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
422                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
423                 break;
424
425         case IPPROTO_UDP:
426                 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
427                                         sizeof(struct ipv4_hdr));
428                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
429                 key.port_src = rte_be_to_cpu_16(udp->src_port);
430                 break;
431
432         default:
433                 key.port_dst = 0;
434                 key.port_src = 0;
435         }
436
437         /* Find destination port */
438         ret = rte_hash_lookup(l3fwd_lookup_struct, (const void *)&key);
439         return (uint8_t)((ret < 0)? portid : l3fwd_out_if[ret]);
440 }
441 #endif
442
443 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
444 static inline uint8_t
445 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
446 {
447         uint8_t next_hop;
448
449         return (uint8_t) ((rte_lpm_lookup(l3fwd_lookup_struct,
450                         rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
451                         next_hop : portid);
452 }
453 #endif
454
455 static inline void
456 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
457 {
458         struct ether_hdr *eth_hdr;
459         struct ipv4_hdr *ipv4_hdr;
460         void *tmp;
461         uint8_t dst_port;
462
463         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
464
465         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
466                                 sizeof(struct ether_hdr));
467
468 #ifdef DO_RFC_1812_CHECKS
469         /* Check to make sure the packet is valid (RFC1812) */
470         if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt.pkt_len) < 0) {
471                 rte_pktmbuf_free(m);
472                 return;
473         }
474 #endif
475
476         dst_port = get_dst_port(ipv4_hdr, portid, l3fwd_lookup_struct);
477         if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
478                 dst_port = portid;
479
480         /* 02:00:00:00:00:xx */
481         tmp = &eth_hdr->d_addr.addr_bytes[0];
482         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
483
484 #ifdef DO_RFC_1812_CHECKS
485         /* Update time to live and header checksum */
486         --(ipv4_hdr->time_to_live);
487         ++(ipv4_hdr->hdr_checksum);
488 #endif
489
490         /* src addr */
491         ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
492
493         send_single_packet(m, dst_port);
494
495 }
496
497 /* main processing loop */
498 static int
499 main_loop(__attribute__((unused)) void *dummy)
500 {
501         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
502         unsigned lcore_id;
503         uint64_t prev_tsc, diff_tsc, cur_tsc;
504         int i, j, nb_rx;
505         uint8_t portid, queueid;
506         struct lcore_conf *qconf;
507         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
508
509         prev_tsc = 0;
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                 return 0;
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 > drain_tsc)) {
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 < RTE_MAX_ETHPORTS; 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"
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 /* Custom handling of signals to handle process terminal */
682 static void
683 signal_handler(int signum)
684 {
685         uint8_t portid;
686         uint8_t nb_ports = rte_eth_dev_count();
687
688         /* When we receive a SIGINT signal */
689         if (signum == SIGINT) {
690                 for (portid = 0; portid < nb_ports; portid++) {
691                         /* skip ports that are not enabled */
692                         if ((enabled_port_mask & (1 << portid)) == 0) 
693                                 continue;
694                         rte_eth_dev_close(portid); 
695                 }
696         }
697         rte_exit(EXIT_SUCCESS, "\n User forced exit\n");
698 }
699 static int
700 parse_portmask(const char *portmask)
701 {
702         char *end = NULL;
703         unsigned long pm;
704
705         /* parse hexadecimal string */
706         pm = strtoul(portmask, &end, 16);
707         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
708                 return -1;
709
710         if (pm == 0)
711                 return -1;
712
713         return pm;
714 }
715
716 static int
717 parse_config(const char *q_arg)
718 {
719         char s[256];
720         const char *p, *p0 = q_arg;
721         char *end;
722         enum fieldnames {
723                 FLD_PORT = 0,
724                 FLD_QUEUE,
725                 FLD_LCORE,
726                 _NUM_FLD
727         };
728         unsigned long int_fld[_NUM_FLD];
729         char *str_fld[_NUM_FLD];
730         int i;
731         unsigned size;
732
733         nb_lcore_params = 0;
734
735         while ((p = strchr(p0,'(')) != NULL) {
736                 ++p;
737                 if((p0 = strchr(p,')')) == NULL)
738                         return -1;
739
740                 size = p0 - p;
741                 if(size >= sizeof(s))
742                         return -1;
743
744                 rte_snprintf(s, sizeof(s), "%.*s", size, p);
745                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
746                         return -1;
747                 for (i = 0; i < _NUM_FLD; i++){
748                         errno = 0;
749                         int_fld[i] = strtoul(str_fld[i], &end, 0);
750                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
751                                 return -1;
752                 }
753                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
754                         printf("exceeded max number of lcore params: %hu\n",
755                                 nb_lcore_params);
756                         return -1;
757                 }
758                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
759                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
760                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
761                 ++nb_lcore_params;
762         }
763         lcore_params = lcore_params_array;
764         return 0;
765 }
766
767 /* Parse the argument given in the command line of the application */
768 static int
769 parse_args(int argc, char **argv)
770 {
771         int opt, ret;
772         char **argvopt;
773         int option_index;
774         char *prgname = argv[0];
775         static struct option lgopts[] = {
776                 {"config", 1, 0, 0},
777                 {"no-numa", 0, 0, 0},
778                 {NULL, 0, 0, 0}
779         };
780
781         argvopt = argv;
782
783         while ((opt = getopt_long(argc, argvopt, "p:",
784                                 lgopts, &option_index)) != EOF) {
785
786                 switch (opt) {
787                 /* portmask */
788                 case 'p':
789                         enabled_port_mask = parse_portmask(optarg);
790                         if (enabled_port_mask == 0) {
791                                 printf("invalid portmask\n");
792                                 print_usage(prgname);
793                                 return -1;
794                         }
795                         break;
796
797                 /* long options */
798                 case 0:
799                         if (!strcmp(lgopts[option_index].name, "config")) {
800                                 ret = parse_config(optarg);
801                                 if (ret) {
802                                         printf("invalid config\n");
803                                         print_usage(prgname);
804                                         return -1;
805                                 }
806                         }
807
808                         if (!strcmp(lgopts[option_index].name, "no-numa")) {
809                                 printf("numa is disabled \n");
810                                 numa_on = 0;
811                         }
812                         break;
813
814                 default:
815                         print_usage(prgname);
816                         return -1;
817                 }
818         }
819
820         if (optind >= 0)
821                 argv[optind-1] = prgname;
822
823         ret = optind-1;
824         optind = 0; /* reset getopt lib */
825         return ret;
826 }
827
828 static void
829 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
830 {
831         printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
832                 eth_addr->addr_bytes[0],
833                 eth_addr->addr_bytes[1],
834                 eth_addr->addr_bytes[2],
835                 eth_addr->addr_bytes[3],
836                 eth_addr->addr_bytes[4],
837                 eth_addr->addr_bytes[5]);
838 }
839
840 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
841 static void
842 setup_hash(int socketid)
843 {
844         unsigned i;
845         int ret;
846         char s[64];
847
848         /* create  hashes */
849         rte_snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
850         l3fwd_hash_params.name = s;
851         l3fwd_hash_params.socket_id = socketid;
852         l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
853         if (l3fwd_lookup_struct[socketid] == NULL)
854                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
855                                 "socket %d\n", socketid);
856
857         /* populate the hash */
858         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
859                 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
860                                 (void *) &l3fwd_route_array[i].key);
861                 if (ret < 0) {
862                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
863                                 "l3fwd hash on socket %d\n", i, socketid);
864                 }
865                 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
866                 printf("Hash: Adding key\n");
867                 print_key(l3fwd_route_array[i].key);
868         }
869 }
870 #endif
871
872 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
873 static void
874 setup_lpm(int socketid)
875 {
876         unsigned i;
877         int ret;
878         char s[64];
879
880         /* create the LPM table */
881         rte_snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
882         l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
883                                 L3FWD_LPM_MAX_RULES, 0);
884         if (l3fwd_lookup_struct[socketid] == NULL)
885                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
886                                 " on socket %d\n", socketid);
887
888         /* populate the LPM table */
889         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
890                 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
891                         l3fwd_route_array[i].ip,
892                         l3fwd_route_array[i].depth,
893                         l3fwd_route_array[i].if_out);
894
895                 if (ret < 0) {
896                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
897                                 "l3fwd LPM table on socket %d\n",
898                                 i, socketid);
899                 }
900
901                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
902                         (unsigned)l3fwd_route_array[i].ip,
903                         l3fwd_route_array[i].depth,
904                         l3fwd_route_array[i].if_out);
905         }
906 }
907 #endif
908
909 static int
910 init_mem(void)
911 {
912         struct lcore_conf *qconf;
913         int socketid;
914         unsigned lcore_id;
915         char s[64];
916
917         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
918                 if (rte_lcore_is_enabled(lcore_id) == 0)
919                         continue;
920
921                 if (numa_on)
922                         socketid = rte_lcore_to_socket_id(lcore_id);
923                 else
924                         socketid = 0;
925
926                 if (socketid >= NB_SOCKETS) {
927                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
928                                 socketid, lcore_id, NB_SOCKETS);
929                 }
930                 if (pktmbuf_pool[socketid] == NULL) {
931                         rte_snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
932                         pktmbuf_pool[socketid] =
933                                 rte_mempool_create(s, NB_MBUF, MBUF_SIZE, 32,
934                                         sizeof(struct rte_pktmbuf_pool_private),
935                                         rte_pktmbuf_pool_init, NULL,
936                                         rte_pktmbuf_init, NULL,
937                                         socketid, 0);
938                         if (pktmbuf_pool[socketid] == NULL)
939                                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", socketid);
940                         else
941                                 printf("Allocated mbuf pool on socket %d\n", socketid);
942
943 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
944                         setup_lpm(socketid);
945 #else
946                         setup_hash(socketid);
947 #endif
948                 }
949                 qconf = &lcore_conf[lcore_id];
950                 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
951         }
952         return 0;
953 }
954
955 int
956 MAIN(int argc, char **argv)
957 {
958         struct lcore_conf *qconf;
959         int ret;
960         unsigned nb_ports;
961         uint16_t queueid;
962         unsigned lcore_id;
963         uint8_t portid, nb_rx_queue, queue, socketid;
964
965         signal(SIGINT, signal_handler);
966         /* init EAL */
967         ret = rte_eal_init(argc, argv);
968         if (ret < 0)
969                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
970         argc -= ret;
971         argv += ret;
972
973         /* parse application arguments (after the EAL ones) */
974         ret = parse_args(argc, argv);
975         if (ret < 0)
976                 rte_exit(EXIT_FAILURE, "Invalid L3FWD-VF parameters\n");
977
978         if (check_lcore_params() < 0)
979                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
980
981         ret = init_lcore_rx_queues();
982         if (ret < 0)
983                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
984
985         ret = init_mem();
986         if (ret < 0)
987                 rte_exit(EXIT_FAILURE, "init_mem failed\n");
988
989         /* init driver */
990         if (rte_pmd_init_all() < 0)
991                 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
992
993         if (rte_eal_pci_probe() < 0)
994                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
995
996         nb_ports = rte_eth_dev_count();
997         if (nb_ports > RTE_MAX_ETHPORTS)
998                 nb_ports = RTE_MAX_ETHPORTS;
999
1000         if (check_port_config(nb_ports) < 0)
1001                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1002
1003         /* initialize all ports */
1004         for (portid = 0; portid < nb_ports; portid++) {
1005                 /* skip ports that are not enabled */
1006                 if ((enabled_port_mask & (1 << portid)) == 0) {
1007                         printf("\nSkipping disabled port %d\n", portid);
1008                         continue;
1009                 }
1010
1011                 /* init port */
1012                 printf("Initializing port %d ... ", portid );
1013                 fflush(stdout);
1014
1015                 /* must always equal(=1) */
1016                 nb_rx_queue = get_port_n_rx_queues(portid);
1017
1018                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1019                         nb_rx_queue, (unsigned)1 );
1020                 ret = rte_eth_dev_configure(portid, nb_rx_queue, 1, &port_conf);
1021                 if (ret < 0)
1022                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1023                                 ret, portid);
1024
1025                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1026                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1027                 printf(", ");
1028
1029                 /* init one TX queue */
1030                 socketid = 0;
1031
1032                 printf("txq=%d,%d ", 0, socketid);
1033                 fflush(stdout);
1034                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1035                                                  socketid, &tx_conf);
1036                 if (ret < 0)
1037                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1038                                 "port=%d\n", ret, portid);
1039
1040                 printf("\n");
1041         }
1042
1043         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1044                 if (rte_lcore_is_enabled(lcore_id) == 0)
1045                         continue;
1046                 qconf = &lcore_conf[lcore_id];
1047                 qconf->tx_queue_id = 0;
1048
1049                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1050                 fflush(stdout);
1051                 /* init RX queues */
1052                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1053                         portid = qconf->rx_queue_list[queue].port_id;
1054                         queueid = qconf->rx_queue_list[queue].queue_id;
1055
1056                         if (numa_on)
1057                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1058                         else
1059                                 socketid = 0;
1060
1061                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1062                         fflush(stdout);
1063
1064                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1065                                                 socketid, &rx_conf, pktmbuf_pool[socketid]);
1066                         if (ret < 0)
1067                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1068                                                 "port=%d\n", ret, portid);
1069                 }
1070         }
1071         printf("\n");
1072
1073         /* start ports */
1074         for (portid = 0; portid < nb_ports; portid++) {
1075                 if ((enabled_port_mask & (1 << portid)) == 0) {
1076                         continue;
1077                 }
1078                 /* Start device */
1079                 ret = rte_eth_dev_start(portid);
1080                 if (ret < 0)
1081                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1082                                 ret, portid);
1083
1084                 printf("done: Port %d\n", portid);
1085
1086         }
1087
1088         /* launch per-lcore init on every lcore */
1089         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1090         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1091                 if (rte_eal_wait_lcore(lcore_id) < 0)
1092                         return -1;
1093         }
1094
1095         return 0;
1096 }