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