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