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