1ba6cc8b0cc7af2f0e5a00a0fe68744130c56527
[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
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 #include <signal.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 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
91 #include <rte_lpm.h>
92 #else
93 #error "APP_LOOKUP_METHOD set to incorrect value"
94 #endif
95
96 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
97
98 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
99 #define NB_MBUF   8192
100
101 /*
102  * RX and TX Prefetch, Host, and Write-back threshold values should be
103  * carefully set for optimal performance. Consult the network
104  * controller's datasheet and supporting DPDK documentation for guidance
105  * on how these parameters should be set.
106  */
107 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
108 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
109 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
110
111 /*
112  * These default values are optimized for use with the Intel(R) 82599 10 GbE
113  * Controller and the DPDK ixgbe PMD. Consider using other values for other
114  * network controllers and/or network drivers.
115  */
116 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
117 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
118 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
119
120 #define MAX_PKT_BURST 32
121 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
122
123 #define NB_SOCKETS 8
124
125 #define SOCKET0 0
126
127 /* Configure how many packets ahead to prefetch, when reading packets */
128 #define PREFETCH_OFFSET 3
129
130 /*
131  * Configurable number of RX/TX ring descriptors
132  */
133 #define RTE_TEST_RX_DESC_DEFAULT 128
134 #define RTE_TEST_TX_DESC_DEFAULT 512
135 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
136 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
137
138 /* ethernet addresses of ports */
139 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
140
141 /* mask of enabled ports */
142 static uint32_t enabled_port_mask = 0;
143 static int numa_on = 1; /**< NUMA is enabled by default. */
144
145 struct mbuf_table {
146         uint16_t len;
147         struct rte_mbuf *m_table[MAX_PKT_BURST];
148 };
149
150 struct lcore_rx_queue {
151         uint8_t port_id;
152         uint8_t queue_id;
153 } __rte_cache_aligned;
154
155 #define MAX_RX_QUEUE_PER_LCORE 16
156 #define MAX_TX_QUEUE_PER_PORT 1
157 #define MAX_RX_QUEUE_PER_PORT 1
158
159 #define MAX_LCORE_PARAMS 1024
160 struct lcore_params {
161         uint8_t port_id;
162         uint8_t queue_id;
163         uint8_t lcore_id;
164 } __rte_cache_aligned;
165
166 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
167 static struct lcore_params lcore_params_array_default[] = {
168         {0, 0, 2},
169         {0, 1, 2},
170         {0, 2, 2},
171         {1, 0, 2},
172         {1, 1, 2},
173         {1, 2, 2},
174         {2, 0, 2},
175         {3, 0, 3},
176         {3, 1, 3},
177 };
178
179 static struct lcore_params * lcore_params = lcore_params_array_default;
180 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
181                                 sizeof(lcore_params_array_default[0]);
182
183 static struct rte_eth_conf port_conf = {
184         .rxmode = {
185                 .split_hdr_size = 0,
186                 .header_split   = 0, /**< Header Split disabled */
187                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
188                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
189                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
190                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
191         },
192         .rx_adv_conf = {
193                 .rss_conf = {
194                         .rss_key = NULL,
195                         .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
196                 },
197         },
198         .txmode = {
199                 .mq_mode = ETH_MQ_TX_NONE,
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[RTE_MAX_ETHPORTS];
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 >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
479                 dst_port = portid;
480
481         /* 02:00:00:00:00:xx */
482         tmp = &eth_hdr->d_addr.addr_bytes[0];
483         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
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 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, diff_tsc, cur_tsc;
505         int i, j, nb_rx;
506         uint8_t portid, queueid;
507         struct lcore_conf *qconf;
508         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
509
510         prev_tsc = 0;
511
512         lcore_id = rte_lcore_id();
513         qconf = &lcore_conf[lcore_id];
514
515         if (qconf->n_rx_queue == 0) {
516                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
517                 return 0;
518         }
519
520         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
521
522         for (i = 0; i < qconf->n_rx_queue; i++) {
523
524                 portid = qconf->rx_queue_list[i].port_id;
525                 queueid = qconf->rx_queue_list[i].queue_id;
526                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
527                         portid, queueid);
528         }
529
530         while (1) {
531
532                 cur_tsc = rte_rdtsc();
533
534                 /*
535                  * TX burst queue drain
536                  */
537                 diff_tsc = cur_tsc - prev_tsc;
538                 if (unlikely(diff_tsc > drain_tsc)) {
539
540                         /*
541                          * This could be optimized (use queueid instead of
542                          * portid), but it is not called so often
543                          */
544                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
545                                 if (qconf->tx_mbufs[portid].len == 0)
546                                         continue;
547                                 send_burst(&lcore_conf[lcore_id],
548                                         qconf->tx_mbufs[portid].len,
549                                         portid);
550                                 qconf->tx_mbufs[portid].len = 0;
551                         }
552
553                         prev_tsc = cur_tsc;
554                 }
555
556                 /*
557                  * Read packet from RX queues
558                  */
559                 for (i = 0; i < qconf->n_rx_queue; ++i) {
560
561                         portid = qconf->rx_queue_list[i].port_id;
562                         queueid = qconf->rx_queue_list[i].queue_id;
563                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
564
565                         /* Prefetch first packets */
566                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
567                                 rte_prefetch0(rte_pktmbuf_mtod(
568                                                 pkts_burst[j], void *));
569                         }
570
571                         /* Prefetch and forward already prefetched packets */
572                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
573                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
574                                                 j + PREFETCH_OFFSET], void *));
575                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
576                         }
577
578                         /* Forward remaining prefetched packets */
579                         for (; j < nb_rx; j++) {
580                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
581                         }
582                 }
583         }
584 }
585
586 static int
587 check_lcore_params(void)
588 {
589         uint8_t queue, lcore;
590         uint16_t i;
591         int socketid;
592
593         for (i = 0; i < nb_lcore_params; ++i) {
594                 queue = lcore_params[i].queue_id;
595                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
596                         printf("invalid queue number: %hhu\n", queue);
597                         return -1;
598                 }
599                 lcore = lcore_params[i].lcore_id;
600                 if (!rte_lcore_is_enabled(lcore)) {
601                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
602                         return -1;
603                 }
604                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
605                         (numa_on == 0)) {
606                         printf("warning: lcore %hhu is on socket %d with numa off \n",
607                                 lcore, socketid);
608                 }
609         }
610         return 0;
611 }
612
613 static int
614 check_port_config(const unsigned nb_ports)
615 {
616         unsigned portid;
617         uint16_t i;
618
619         for (i = 0; i < nb_lcore_params; ++i) {
620                 portid = lcore_params[i].port_id;
621                 if ((enabled_port_mask & (1 << portid)) == 0) {
622                         printf("port %u is not enabled in port mask\n", portid);
623                         return -1;
624                 }
625                 if (portid >= nb_ports) {
626                         printf("port %u is not present on the board\n", portid);
627                         return -1;
628                 }
629         }
630         return 0;
631 }
632
633 static uint8_t
634 get_port_n_rx_queues(const uint8_t port)
635 {
636         int queue = -1;
637         uint16_t i;
638
639         for (i = 0; i < nb_lcore_params; ++i) {
640                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
641                         queue = lcore_params[i].queue_id;
642         }
643         return (uint8_t)(++queue);
644 }
645
646 static int
647 init_lcore_rx_queues(void)
648 {
649         uint16_t i, nb_rx_queue;
650         uint8_t lcore;
651
652         for (i = 0; i < nb_lcore_params; ++i) {
653                 lcore = lcore_params[i].lcore_id;
654                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
655                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
656                         printf("error: too many queues (%u) for lcore: %u\n",
657                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
658                         return -1;
659                 } else {
660                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
661                                 lcore_params[i].port_id;
662                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
663                                 lcore_params[i].queue_id;
664                         lcore_conf[lcore].n_rx_queue++;
665                 }
666         }
667         return 0;
668 }
669
670 /* display usage */
671 static void
672 print_usage(const char *prgname)
673 {
674         printf ("%s [EAL options] -- -p PORTMASK"
675                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]\n"
676                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
677                 "  --config (port,queue,lcore): rx queues configuration\n"
678                 "  --no-numa: optional, disable numa awareness\n",
679                 prgname);
680 }
681
682 /* Custom handling of signals to handle process terminal */
683 static void
684 signal_handler(int signum)
685 {
686         uint8_t portid;
687         uint8_t nb_ports = rte_eth_dev_count();
688
689         /* When we receive a SIGINT signal */
690         if (signum == SIGINT) {
691                 for (portid = 0; portid < nb_ports; portid++) {
692                         /* skip ports that are not enabled */
693                         if ((enabled_port_mask & (1 << portid)) == 0) 
694                                 continue;
695                         rte_eth_dev_close(portid); 
696                 }
697         }
698         rte_exit(EXIT_SUCCESS, "\n User forced exit\n");
699 }
700 static int
701 parse_portmask(const char *portmask)
702 {
703         char *end = NULL;
704         unsigned long pm;
705
706         /* parse hexadecimal string */
707         pm = strtoul(portmask, &end, 16);
708         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
709                 return -1;
710
711         if (pm == 0)
712                 return -1;
713
714         return pm;
715 }
716
717 static int
718 parse_config(const char *q_arg)
719 {
720         char s[256];
721         const char *p, *p0 = q_arg;
722         char *end;
723         enum fieldnames {
724                 FLD_PORT = 0,
725                 FLD_QUEUE,
726                 FLD_LCORE,
727                 _NUM_FLD
728         };
729         unsigned long int_fld[_NUM_FLD];
730         char *str_fld[_NUM_FLD];
731         int i;
732         unsigned size;
733
734         nb_lcore_params = 0;
735
736         while ((p = strchr(p0,'(')) != NULL) {
737                 ++p;
738                 if((p0 = strchr(p,')')) == NULL)
739                         return -1;
740
741                 size = p0 - p;
742                 if(size >= sizeof(s))
743                         return -1;
744
745                 rte_snprintf(s, sizeof(s), "%.*s", size, p);
746                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
747                         return -1;
748                 for (i = 0; i < _NUM_FLD; i++){
749                         errno = 0;
750                         int_fld[i] = strtoul(str_fld[i], &end, 0);
751                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
752                                 return -1;
753                 }
754                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
755                         printf("exceeded max number of lcore params: %hu\n",
756                                 nb_lcore_params);
757                         return -1;
758                 }
759                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
760                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
761                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
762                 ++nb_lcore_params;
763         }
764         lcore_params = lcore_params_array;
765         return 0;
766 }
767
768 /* Parse the argument given in the command line of the application */
769 static int
770 parse_args(int argc, char **argv)
771 {
772         int opt, ret;
773         char **argvopt;
774         int option_index;
775         char *prgname = argv[0];
776         static struct option lgopts[] = {
777                 {"config", 1, 0, 0},
778                 {"no-numa", 0, 0, 0},
779                 {NULL, 0, 0, 0}
780         };
781
782         argvopt = argv;
783
784         while ((opt = getopt_long(argc, argvopt, "p:",
785                                 lgopts, &option_index)) != EOF) {
786
787                 switch (opt) {
788                 /* portmask */
789                 case 'p':
790                         enabled_port_mask = parse_portmask(optarg);
791                         if (enabled_port_mask == 0) {
792                                 printf("invalid portmask\n");
793                                 print_usage(prgname);
794                                 return -1;
795                         }
796                         break;
797
798                 /* long options */
799                 case 0:
800                         if (!strcmp(lgopts[option_index].name, "config")) {
801                                 ret = parse_config(optarg);
802                                 if (ret) {
803                                         printf("invalid config\n");
804                                         print_usage(prgname);
805                                         return -1;
806                                 }
807                         }
808
809                         if (!strcmp(lgopts[option_index].name, "no-numa")) {
810                                 printf("numa is disabled \n");
811                                 numa_on = 0;
812                         }
813                         break;
814
815                 default:
816                         print_usage(prgname);
817                         return -1;
818                 }
819         }
820
821         if (optind >= 0)
822                 argv[optind-1] = prgname;
823
824         ret = optind-1;
825         optind = 0; /* reset getopt lib */
826         return ret;
827 }
828
829 static void
830 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
831 {
832         printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
833                 eth_addr->addr_bytes[0],
834                 eth_addr->addr_bytes[1],
835                 eth_addr->addr_bytes[2],
836                 eth_addr->addr_bytes[3],
837                 eth_addr->addr_bytes[4],
838                 eth_addr->addr_bytes[5]);
839 }
840
841 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
842 static void
843 setup_hash(int socketid)
844 {
845         unsigned i;
846         int ret;
847         char s[64];
848
849         /* create  hashes */
850         rte_snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
851         l3fwd_hash_params.name = s;
852         l3fwd_hash_params.socket_id = socketid;
853         l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
854         if (l3fwd_lookup_struct[socketid] == NULL)
855                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
856                                 "socket %d\n", socketid);
857
858         /* populate the hash */
859         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
860                 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
861                                 (void *) &l3fwd_route_array[i].key);
862                 if (ret < 0) {
863                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
864                                 "l3fwd hash on socket %d\n", i, socketid);
865                 }
866                 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
867                 printf("Hash: Adding key\n");
868                 print_key(l3fwd_route_array[i].key);
869         }
870 }
871 #endif
872
873 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
874 static void
875 setup_lpm(int socketid)
876 {
877         unsigned i;
878         int ret;
879         char s[64];
880
881         /* create the LPM table */
882         rte_snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
883         l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
884                                 L3FWD_LPM_MAX_RULES, 0);
885         if (l3fwd_lookup_struct[socketid] == NULL)
886                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
887                                 " on socket %d\n", socketid);
888
889         /* populate the LPM table */
890         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
891                 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
892                         l3fwd_route_array[i].ip,
893                         l3fwd_route_array[i].depth,
894                         l3fwd_route_array[i].if_out);
895
896                 if (ret < 0) {
897                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
898                                 "l3fwd LPM table on socket %d\n",
899                                 i, socketid);
900                 }
901
902                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
903                         (unsigned)l3fwd_route_array[i].ip,
904                         l3fwd_route_array[i].depth,
905                         l3fwd_route_array[i].if_out);
906         }
907 }
908 #endif
909
910 static int
911 init_mem(void)
912 {
913         struct lcore_conf *qconf;
914         int socketid;
915         unsigned lcore_id;
916         char s[64];
917
918         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
919                 if (rte_lcore_is_enabled(lcore_id) == 0)
920                         continue;
921
922                 if (numa_on)
923                         socketid = rte_lcore_to_socket_id(lcore_id);
924                 else
925                         socketid = 0;
926
927                 if (socketid >= NB_SOCKETS) {
928                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
929                                 socketid, lcore_id, NB_SOCKETS);
930                 }
931                 if (pktmbuf_pool[socketid] == NULL) {
932                         rte_snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
933                         pktmbuf_pool[socketid] =
934                                 rte_mempool_create(s, NB_MBUF, MBUF_SIZE, 32,
935                                         sizeof(struct rte_pktmbuf_pool_private),
936                                         rte_pktmbuf_pool_init, NULL,
937                                         rte_pktmbuf_init, NULL,
938                                         socketid, 0);
939                         if (pktmbuf_pool[socketid] == NULL)
940                                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", socketid);
941                         else
942                                 printf("Allocated mbuf pool on socket %d\n", socketid);
943
944 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
945                         setup_lpm(socketid);
946 #else
947                         setup_hash(socketid);
948 #endif
949                 }
950                 qconf = &lcore_conf[lcore_id];
951                 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
952         }
953         return 0;
954 }
955
956 int
957 MAIN(int argc, char **argv)
958 {
959         struct lcore_conf *qconf;
960         int ret;
961         unsigned nb_ports;
962         uint16_t queueid;
963         unsigned lcore_id;
964         uint8_t portid, nb_rx_queue, queue, socketid;
965
966         signal(SIGINT, signal_handler);
967         /* init EAL */
968         ret = rte_eal_init(argc, argv);
969         if (ret < 0)
970                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
971         argc -= ret;
972         argv += ret;
973
974         /* parse application arguments (after the EAL ones) */
975         ret = parse_args(argc, argv);
976         if (ret < 0)
977                 rte_exit(EXIT_FAILURE, "Invalid L3FWD-VF parameters\n");
978
979         if (check_lcore_params() < 0)
980                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
981
982         ret = init_lcore_rx_queues();
983         if (ret < 0)
984                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
985
986         ret = init_mem();
987         if (ret < 0)
988                 rte_exit(EXIT_FAILURE, "init_mem failed\n");
989
990         /* init driver */
991         if (rte_pmd_init_all() < 0)
992                 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
993
994         if (rte_eal_pci_probe() < 0)
995                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
996
997         nb_ports = rte_eth_dev_count();
998         if (nb_ports > RTE_MAX_ETHPORTS)
999                 nb_ports = RTE_MAX_ETHPORTS;
1000
1001         if (check_port_config(nb_ports) < 0)
1002                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1003
1004         /* initialize all ports */
1005         for (portid = 0; portid < nb_ports; portid++) {
1006                 /* skip ports that are not enabled */
1007                 if ((enabled_port_mask & (1 << portid)) == 0) {
1008                         printf("\nSkipping disabled port %d\n", portid);
1009                         continue;
1010                 }
1011
1012                 /* init port */
1013                 printf("Initializing port %d ... ", portid );
1014                 fflush(stdout);
1015
1016                 /* must always equal(=1) */
1017                 nb_rx_queue = get_port_n_rx_queues(portid);
1018
1019                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1020                         nb_rx_queue, (unsigned)1 );
1021                 ret = rte_eth_dev_configure(portid, nb_rx_queue, 1, &port_conf);
1022                 if (ret < 0)
1023                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1024                                 ret, portid);
1025
1026                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1027                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1028                 printf(", ");
1029
1030                 /* init one TX queue */
1031                 socketid = 0;
1032
1033                 printf("txq=%d,%d ", 0, socketid);
1034                 fflush(stdout);
1035                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1036                                                  socketid, &tx_conf);
1037                 if (ret < 0)
1038                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1039                                 "port=%d\n", ret, portid);
1040
1041                 printf("\n");
1042         }
1043
1044         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1045                 if (rte_lcore_is_enabled(lcore_id) == 0)
1046                         continue;
1047                 qconf = &lcore_conf[lcore_id];
1048                 qconf->tx_queue_id = 0;
1049
1050                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1051                 fflush(stdout);
1052                 /* init RX queues */
1053                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1054                         portid = qconf->rx_queue_list[queue].port_id;
1055                         queueid = qconf->rx_queue_list[queue].queue_id;
1056
1057                         if (numa_on)
1058                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1059                         else
1060                                 socketid = 0;
1061
1062                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1063                         fflush(stdout);
1064
1065                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1066                                                 socketid, &rx_conf, pktmbuf_pool[socketid]);
1067                         if (ret < 0)
1068                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1069                                                 "port=%d\n", ret, portid);
1070                 }
1071         }
1072         printf("\n");
1073
1074         /* start ports */
1075         for (portid = 0; portid < nb_ports; portid++) {
1076                 if ((enabled_port_mask & (1 << portid)) == 0) {
1077                         continue;
1078                 }
1079                 /* Start device */
1080                 ret = rte_eth_dev_start(portid);
1081                 if (ret < 0)
1082                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1083                                 ret, portid);
1084
1085                 printf("done: Port %d\n", portid);
1086
1087         }
1088
1089         /* launch per-lcore init on every lcore */
1090         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1091         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1092                 if (rte_eal_wait_lcore(lcore_id) < 0)
1093                         return -1;
1094         }
1095
1096         return 0;
1097 }