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