New upstream version 18.02
[deb_dpdk.git] / examples / ip_fragmentation / 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 <sys/param.h>
11 #include <string.h>
12 #include <sys/queue.h>
13 #include <stdarg.h>
14 #include <errno.h>
15 #include <getopt.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_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
36 #include <rte_mbuf.h>
37 #include <rte_lpm.h>
38 #include <rte_lpm6.h>
39 #include <rte_ip.h>
40 #include <rte_string_fns.h>
41
42 #include <rte_ip_frag.h>
43
44 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
45
46 /* allow max jumbo frame 9.5 KB */
47 #define JUMBO_FRAME_MAX_SIZE    0x2600
48
49 #define ROUNDUP_DIV(a, b)       (((a) + (b) - 1) / (b))
50
51 /*
52  * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
53  * This value includes the size of IPv6 header.
54  */
55 #define IPV4_MTU_DEFAULT        ETHER_MTU
56 #define IPV6_MTU_DEFAULT        ETHER_MTU
57
58 /*
59  * Default payload in bytes for the IPv6 packet.
60  */
61 #define IPV4_DEFAULT_PAYLOAD    (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
62 #define IPV6_DEFAULT_PAYLOAD    (IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr))
63
64 /*
65  * Max number of fragments per packet expected - defined by config file.
66  */
67 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
68
69 #define NB_MBUF   8192
70
71 #define MAX_PKT_BURST   32
72 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
73
74 /* Configure how many packets ahead to prefetch, when reading packets */
75 #define PREFETCH_OFFSET 3
76
77 /*
78  * Configurable number of RX/TX ring descriptors
79  */
80 #define RTE_TEST_RX_DESC_DEFAULT 1024
81 #define RTE_TEST_TX_DESC_DEFAULT 1024
82 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
83 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
84
85 /* ethernet addresses of ports */
86 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
87
88 #ifndef IPv4_BYTES
89 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
90 #define IPv4_BYTES(addr) \
91                 (uint8_t) (((addr) >> 24) & 0xFF),\
92                 (uint8_t) (((addr) >> 16) & 0xFF),\
93                 (uint8_t) (((addr) >> 8) & 0xFF),\
94                 (uint8_t) ((addr) & 0xFF)
95 #endif
96
97 #ifndef IPv6_BYTES
98 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
99                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
100 #define IPv6_BYTES(addr) \
101         addr[0],  addr[1], addr[2],  addr[3], \
102         addr[4],  addr[5], addr[6],  addr[7], \
103         addr[8],  addr[9], addr[10], addr[11],\
104         addr[12], addr[13],addr[14], addr[15]
105 #endif
106
107 #define IPV6_ADDR_LEN 16
108
109 /* mask of enabled ports */
110 static int enabled_port_mask = 0;
111
112 static int rx_queue_per_lcore = 1;
113
114 #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
115
116 struct mbuf_table {
117         uint16_t len;
118         struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
119 };
120
121 struct rx_queue {
122         struct rte_mempool *direct_pool;
123         struct rte_mempool *indirect_pool;
124         struct rte_lpm *lpm;
125         struct rte_lpm6 *lpm6;
126         uint16_t portid;
127 };
128
129 #define MAX_RX_QUEUE_PER_LCORE 16
130 #define MAX_TX_QUEUE_PER_PORT 16
131 struct lcore_queue_conf {
132         uint16_t n_rx_queue;
133         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
134         struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
135         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
136 } __rte_cache_aligned;
137 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
138
139 static struct rte_eth_conf port_conf = {
140         .rxmode = {
141                 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
142                 .split_hdr_size = 0,
143                 .ignore_offload_bitfield = 1,
144                 .offloads = (DEV_RX_OFFLOAD_CHECKSUM |
145                              DEV_RX_OFFLOAD_JUMBO_FRAME |
146                              DEV_RX_OFFLOAD_CRC_STRIP),
147         },
148         .txmode = {
149                 .mq_mode = ETH_MQ_TX_NONE,
150                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
151                              DEV_TX_OFFLOAD_MULTI_SEGS),
152         },
153 };
154
155 /*
156  * IPv4 forwarding table
157  */
158 struct l3fwd_ipv4_route {
159         uint32_t ip;
160         uint8_t  depth;
161         uint8_t  if_out;
162 };
163
164 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
165                 {IPv4(100,10,0,0), 16, 0},
166                 {IPv4(100,20,0,0), 16, 1},
167                 {IPv4(100,30,0,0), 16, 2},
168                 {IPv4(100,40,0,0), 16, 3},
169                 {IPv4(100,50,0,0), 16, 4},
170                 {IPv4(100,60,0,0), 16, 5},
171                 {IPv4(100,70,0,0), 16, 6},
172                 {IPv4(100,80,0,0), 16, 7},
173 };
174
175 /*
176  * IPv6 forwarding table
177  */
178
179 struct l3fwd_ipv6_route {
180         uint8_t ip[IPV6_ADDR_LEN];
181         uint8_t depth;
182         uint8_t if_out;
183 };
184
185 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
186         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
187         {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
188         {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
189         {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
190         {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
191         {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
192         {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
193         {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
194 };
195
196 #define LPM_MAX_RULES         1024
197 #define LPM6_MAX_RULES         1024
198 #define LPM6_NUMBER_TBL8S (1 << 16)
199
200 struct rte_lpm6_config lpm6_config = {
201                 .max_rules = LPM6_MAX_RULES,
202                 .number_tbl8s = LPM6_NUMBER_TBL8S,
203                 .flags = 0
204 };
205
206 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
207 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
208 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
209 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
210
211 /* Send burst of packets on an output interface */
212 static inline int
213 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint16_t port)
214 {
215         struct rte_mbuf **m_table;
216         int ret;
217         uint16_t queueid;
218
219         queueid = qconf->tx_queue_id[port];
220         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
221
222         ret = rte_eth_tx_burst(port, queueid, m_table, n);
223         if (unlikely(ret < n)) {
224                 do {
225                         rte_pktmbuf_free(m_table[ret]);
226                 } while (++ret < n);
227         }
228
229         return 0;
230 }
231
232 static inline void
233 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
234                 uint8_t queueid, uint16_t port_in)
235 {
236         struct rx_queue *rxq;
237         uint32_t i, len, next_hop;
238         uint8_t ipv6;
239         uint16_t port_out;
240         int32_t len2;
241
242         ipv6 = 0;
243         rxq = &qconf->rx_queue_list[queueid];
244
245         /* by default, send everything back to the source port */
246         port_out = port_in;
247
248         /* Remove the Ethernet header and trailer from the input packet */
249         rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
250
251         /* Build transmission burst */
252         len = qconf->tx_mbufs[port_out].len;
253
254         /* if this is an IPv4 packet */
255         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
256                 struct ipv4_hdr *ip_hdr;
257                 uint32_t ip_dst;
258                 /* Read the lookup key (i.e. ip_dst) from the input packet */
259                 ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
260                 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
261
262                 /* Find destination port */
263                 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
264                                 (enabled_port_mask & 1 << next_hop) != 0) {
265                         port_out = next_hop;
266
267                         /* Build transmission burst for new port */
268                         len = qconf->tx_mbufs[port_out].len;
269                 }
270
271                 /* if we don't need to do any fragmentation */
272                 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
273                         qconf->tx_mbufs[port_out].m_table[len] = m;
274                         len2 = 1;
275                 } else {
276                         len2 = rte_ipv4_fragment_packet(m,
277                                 &qconf->tx_mbufs[port_out].m_table[len],
278                                 (uint16_t)(MBUF_TABLE_SIZE - len),
279                                 IPV4_MTU_DEFAULT,
280                                 rxq->direct_pool, rxq->indirect_pool);
281
282                         /* Free input packet */
283                         rte_pktmbuf_free(m);
284
285                         /* If we fail to fragment the packet */
286                         if (unlikely (len2 < 0))
287                                 return;
288                 }
289         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
290                 /* if this is an IPv6 packet */
291                 struct ipv6_hdr *ip_hdr;
292
293                 ipv6 = 1;
294
295                 /* Read the lookup key (i.e. ip_dst) from the input packet */
296                 ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
297
298                 /* Find destination port */
299                 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
300                                                 &next_hop) == 0 &&
301                                 (enabled_port_mask & 1 << next_hop) != 0) {
302                         port_out = next_hop;
303
304                         /* Build transmission burst for new port */
305                         len = qconf->tx_mbufs[port_out].len;
306                 }
307
308                 /* if we don't need to do any fragmentation */
309                 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
310                         qconf->tx_mbufs[port_out].m_table[len] = m;
311                         len2 = 1;
312                 } else {
313                         len2 = rte_ipv6_fragment_packet(m,
314                                 &qconf->tx_mbufs[port_out].m_table[len],
315                                 (uint16_t)(MBUF_TABLE_SIZE - len),
316                                 IPV6_MTU_DEFAULT,
317                                 rxq->direct_pool, rxq->indirect_pool);
318
319                         /* Free input packet */
320                         rte_pktmbuf_free(m);
321
322                         /* If we fail to fragment the packet */
323                         if (unlikely (len2 < 0))
324                                 return;
325                 }
326         }
327         /* else, just forward the packet */
328         else {
329                 qconf->tx_mbufs[port_out].m_table[len] = m;
330                 len2 = 1;
331         }
332
333         for (i = len; i < len + len2; i ++) {
334                 void *d_addr_bytes;
335
336                 m = qconf->tx_mbufs[port_out].m_table[i];
337                 struct ether_hdr *eth_hdr = (struct ether_hdr *)
338                         rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
339                 if (eth_hdr == NULL) {
340                         rte_panic("No headroom in mbuf.\n");
341                 }
342
343                 m->l2_len = sizeof(struct ether_hdr);
344
345                 /* 02:00:00:00:00:xx */
346                 d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
347                 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40);
348
349                 /* src addr */
350                 ether_addr_copy(&ports_eth_addr[port_out], &eth_hdr->s_addr);
351                 if (ipv6)
352                         eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
353                 else
354                         eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
355         }
356
357         len += len2;
358
359         if (likely(len < MAX_PKT_BURST)) {
360                 qconf->tx_mbufs[port_out].len = (uint16_t)len;
361                 return;
362         }
363
364         /* Transmit packets */
365         send_burst(qconf, (uint16_t)len, port_out);
366         qconf->tx_mbufs[port_out].len = 0;
367 }
368
369 /* main processing loop */
370 static int
371 main_loop(__attribute__((unused)) void *dummy)
372 {
373         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
374         unsigned lcore_id;
375         uint64_t prev_tsc, diff_tsc, cur_tsc;
376         int i, j, nb_rx;
377         uint16_t portid;
378         struct lcore_queue_conf *qconf;
379         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
380
381         prev_tsc = 0;
382
383         lcore_id = rte_lcore_id();
384         qconf = &lcore_queue_conf[lcore_id];
385
386         if (qconf->n_rx_queue == 0) {
387                 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
388                 return 0;
389         }
390
391         RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
392
393         for (i = 0; i < qconf->n_rx_queue; i++) {
394
395                 portid = qconf->rx_queue_list[i].portid;
396                 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
397                                 portid);
398         }
399
400         while (1) {
401
402                 cur_tsc = rte_rdtsc();
403
404                 /*
405                  * TX burst queue drain
406                  */
407                 diff_tsc = cur_tsc - prev_tsc;
408                 if (unlikely(diff_tsc > drain_tsc)) {
409
410                         /*
411                          * This could be optimized (use queueid instead of
412                          * portid), but it is not called so often
413                          */
414                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
415                                 if (qconf->tx_mbufs[portid].len == 0)
416                                         continue;
417                                 send_burst(&lcore_queue_conf[lcore_id],
418                                            qconf->tx_mbufs[portid].len,
419                                            portid);
420                                 qconf->tx_mbufs[portid].len = 0;
421                         }
422
423                         prev_tsc = cur_tsc;
424                 }
425
426                 /*
427                  * Read packet from RX queues
428                  */
429                 for (i = 0; i < qconf->n_rx_queue; i++) {
430
431                         portid = qconf->rx_queue_list[i].portid;
432                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
433                                                  MAX_PKT_BURST);
434
435                         /* Prefetch first packets */
436                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
437                                 rte_prefetch0(rte_pktmbuf_mtod(
438                                                 pkts_burst[j], void *));
439                         }
440
441                         /* Prefetch and forward already prefetched packets */
442                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
443                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
444                                                 j + PREFETCH_OFFSET], void *));
445                                 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
446                         }
447
448                         /* Forward remaining prefetched packets */
449                         for (; j < nb_rx; j++) {
450                                 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
451                         }
452                 }
453         }
454 }
455
456 /* display usage */
457 static void
458 print_usage(const char *prgname)
459 {
460         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
461                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
462                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
463                prgname);
464 }
465
466 static int
467 parse_portmask(const char *portmask)
468 {
469         char *end = NULL;
470         unsigned long pm;
471
472         /* parse hexadecimal string */
473         pm = strtoul(portmask, &end, 16);
474         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
475                 return -1;
476
477         if (pm == 0)
478                 return -1;
479
480         return pm;
481 }
482
483 static int
484 parse_nqueue(const char *q_arg)
485 {
486         char *end = NULL;
487         unsigned long n;
488
489         /* parse hexadecimal string */
490         n = strtoul(q_arg, &end, 10);
491         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
492                 return -1;
493         if (n == 0)
494                 return -1;
495         if (n >= MAX_RX_QUEUE_PER_LCORE)
496                 return -1;
497
498         return n;
499 }
500
501 /* Parse the argument given in the command line of the application */
502 static int
503 parse_args(int argc, char **argv)
504 {
505         int opt, ret;
506         char **argvopt;
507         int option_index;
508         char *prgname = argv[0];
509         static struct option lgopts[] = {
510                 {NULL, 0, 0, 0}
511         };
512
513         argvopt = argv;
514
515         while ((opt = getopt_long(argc, argvopt, "p:q:",
516                                   lgopts, &option_index)) != EOF) {
517
518                 switch (opt) {
519                 /* portmask */
520                 case 'p':
521                         enabled_port_mask = parse_portmask(optarg);
522                         if (enabled_port_mask < 0) {
523                                 printf("invalid portmask\n");
524                                 print_usage(prgname);
525                                 return -1;
526                         }
527                         break;
528
529                 /* nqueue */
530                 case 'q':
531                         rx_queue_per_lcore = parse_nqueue(optarg);
532                         if (rx_queue_per_lcore < 0) {
533                                 printf("invalid queue number\n");
534                                 print_usage(prgname);
535                                 return -1;
536                         }
537                         break;
538
539                 /* long options */
540                 case 0:
541                         print_usage(prgname);
542                         return -1;
543
544                 default:
545                         print_usage(prgname);
546                         return -1;
547                 }
548         }
549
550         if (enabled_port_mask == 0) {
551                 printf("portmask not specified\n");
552                 print_usage(prgname);
553                 return -1;
554         }
555
556         if (optind >= 0)
557                 argv[optind-1] = prgname;
558
559         ret = optind-1;
560         optind = 1; /* reset getopt lib */
561         return ret;
562 }
563
564 static void
565 print_ethaddr(const char *name, struct ether_addr *eth_addr)
566 {
567         char buf[ETHER_ADDR_FMT_SIZE];
568         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
569         printf("%s%s", name, buf);
570 }
571
572 /* Check the link status of all ports in up to 9s, and print them finally */
573 static void
574 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
575 {
576 #define CHECK_INTERVAL 100 /* 100ms */
577 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
578         uint16_t portid;
579         uint8_t count, all_ports_up, print_flag = 0;
580         struct rte_eth_link link;
581
582         printf("\nChecking link status");
583         fflush(stdout);
584         for (count = 0; count <= MAX_CHECK_TIME; count++) {
585                 all_ports_up = 1;
586                 for (portid = 0; portid < port_num; portid++) {
587                         if ((port_mask & (1 << portid)) == 0)
588                                 continue;
589                         memset(&link, 0, sizeof(link));
590                         rte_eth_link_get_nowait(portid, &link);
591                         /* print link status if flag set */
592                         if (print_flag == 1) {
593                                 if (link.link_status)
594                                         printf(
595                                         "Port%d Link Up .Speed %u Mbps - %s\n",
596                                                 portid, link.link_speed,
597                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
598                                         ("full-duplex") : ("half-duplex\n"));
599                                 else
600                                         printf("Port %d Link Down\n", portid);
601                                 continue;
602                         }
603                         /* clear all_ports_up flag if any link down */
604                         if (link.link_status == ETH_LINK_DOWN) {
605                                 all_ports_up = 0;
606                                 break;
607                         }
608                 }
609                 /* after finally printing all link status, get out */
610                 if (print_flag == 1)
611                         break;
612
613                 if (all_ports_up == 0) {
614                         printf(".");
615                         fflush(stdout);
616                         rte_delay_ms(CHECK_INTERVAL);
617                 }
618
619                 /* set the print_flag if all ports up or timeout */
620                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
621                         print_flag = 1;
622                         printf("\ndone\n");
623                 }
624         }
625 }
626
627 /* Check L3 packet type detection capablity of the NIC port */
628 static int
629 check_ptype(int portid)
630 {
631         int i, ret;
632         int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
633         uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
634
635         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
636         if (ret <= 0)
637                 return 0;
638
639         uint32_t ptypes[ret];
640
641         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
642         for (i = 0; i < ret; ++i) {
643                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
644                         ptype_l3_ipv4 = 1;
645                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
646                         ptype_l3_ipv6 = 1;
647         }
648
649         if (ptype_l3_ipv4 == 0)
650                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
651
652         if (ptype_l3_ipv6 == 0)
653                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
654
655         if (ptype_l3_ipv4 && ptype_l3_ipv6)
656                 return 1;
657
658         return 0;
659
660 }
661
662 /* Parse packet type of a packet by SW */
663 static inline void
664 parse_ptype(struct rte_mbuf *m)
665 {
666         struct ether_hdr *eth_hdr;
667         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
668         uint16_t ether_type;
669
670         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
671         ether_type = eth_hdr->ether_type;
672         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
673                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
674         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
675                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
676
677         m->packet_type = packet_type;
678 }
679
680 /* callback function to detect packet type for a queue of a port */
681 static uint16_t
682 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
683                    struct rte_mbuf *pkts[], uint16_t nb_pkts,
684                    uint16_t max_pkts __rte_unused,
685                    void *user_param __rte_unused)
686 {
687         uint16_t i;
688
689         for (i = 0; i < nb_pkts; ++i)
690                 parse_ptype(pkts[i]);
691
692         return nb_pkts;
693 }
694
695 static int
696 init_routing_table(void)
697 {
698         struct rte_lpm *lpm;
699         struct rte_lpm6 *lpm6;
700         int socket, ret;
701         unsigned i;
702
703         for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
704                 if (socket_lpm[socket]) {
705                         lpm = socket_lpm[socket];
706                         /* populate the LPM table */
707                         for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
708                                 ret = rte_lpm_add(lpm,
709                                         l3fwd_ipv4_route_array[i].ip,
710                                         l3fwd_ipv4_route_array[i].depth,
711                                         l3fwd_ipv4_route_array[i].if_out);
712
713                                 if (ret < 0) {
714                                         RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
715                                                 "LPM table\n", i);
716                                         return -1;
717                                 }
718
719                                 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
720                                                 "/%d (port %d)\n",
721                                         socket,
722                                         IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
723                                         l3fwd_ipv4_route_array[i].depth,
724                                         l3fwd_ipv4_route_array[i].if_out);
725                         }
726                 }
727
728                 if (socket_lpm6[socket]) {
729                         lpm6 = socket_lpm6[socket];
730                         /* populate the LPM6 table */
731                         for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
732                                 ret = rte_lpm6_add(lpm6,
733                                         l3fwd_ipv6_route_array[i].ip,
734                                         l3fwd_ipv6_route_array[i].depth,
735                                         l3fwd_ipv6_route_array[i].if_out);
736
737                                 if (ret < 0) {
738                                         RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
739                                                 "LPM6 table\n", i);
740                                         return -1;
741                                 }
742
743                                 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
744                                                 "/%d (port %d)\n",
745                                         socket,
746                                         IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
747                                         l3fwd_ipv6_route_array[i].depth,
748                                         l3fwd_ipv6_route_array[i].if_out);
749                         }
750                 }
751         }
752         return 0;
753 }
754
755 static int
756 init_mem(void)
757 {
758         char buf[PATH_MAX];
759         struct rte_mempool *mp;
760         struct rte_lpm *lpm;
761         struct rte_lpm6 *lpm6;
762         struct rte_lpm_config lpm_config;
763         int socket;
764         unsigned lcore_id;
765
766         /* traverse through lcores and initialize structures on each socket */
767
768         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
769
770                 if (rte_lcore_is_enabled(lcore_id) == 0)
771                         continue;
772
773                 socket = rte_lcore_to_socket_id(lcore_id);
774
775                 if (socket == SOCKET_ID_ANY)
776                         socket = 0;
777
778                 if (socket_direct_pool[socket] == NULL) {
779                         RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
780                                         socket);
781                         snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
782
783                         mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
784                                 0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
785                         if (mp == NULL) {
786                                 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
787                                 return -1;
788                         }
789                         socket_direct_pool[socket] = mp;
790                 }
791
792                 if (socket_indirect_pool[socket] == NULL) {
793                         RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
794                                         socket);
795                         snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
796
797                         mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
798                                 socket);
799                         if (mp == NULL) {
800                                 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
801                                 return -1;
802                         }
803                         socket_indirect_pool[socket] = mp;
804                 }
805
806                 if (socket_lpm[socket] == NULL) {
807                         RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
808                         snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
809
810                         lpm_config.max_rules = LPM_MAX_RULES;
811                         lpm_config.number_tbl8s = 256;
812                         lpm_config.flags = 0;
813
814                         lpm = rte_lpm_create(buf, socket, &lpm_config);
815                         if (lpm == NULL) {
816                                 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
817                                 return -1;
818                         }
819                         socket_lpm[socket] = lpm;
820                 }
821
822                 if (socket_lpm6[socket] == NULL) {
823                         RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
824                         snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
825
826                         lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
827                         if (lpm6 == NULL) {
828                                 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
829                                 return -1;
830                         }
831                         socket_lpm6[socket] = lpm6;
832                 }
833         }
834
835         return 0;
836 }
837
838 int
839 main(int argc, char **argv)
840 {
841         struct lcore_queue_conf *qconf;
842         struct rte_eth_dev_info dev_info;
843         struct rte_eth_txconf *txconf;
844         struct rx_queue *rxq;
845         int socket, ret;
846         unsigned nb_ports;
847         uint16_t queueid = 0;
848         unsigned lcore_id = 0, rx_lcore_id = 0;
849         uint32_t n_tx_queue, nb_lcores;
850         uint16_t portid;
851
852         /* init EAL */
853         ret = rte_eal_init(argc, argv);
854         if (ret < 0)
855                 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
856         argc -= ret;
857         argv += ret;
858
859         /* parse application arguments (after the EAL ones) */
860         ret = parse_args(argc, argv);
861         if (ret < 0)
862                 rte_exit(EXIT_FAILURE, "Invalid arguments");
863
864         nb_ports = rte_eth_dev_count();
865         if (nb_ports == 0)
866                 rte_exit(EXIT_FAILURE, "No ports found!\n");
867
868         nb_lcores = rte_lcore_count();
869
870         /* initialize structures (mempools, lpm etc.) */
871         if (init_mem() < 0)
872                 rte_panic("Cannot initialize memory structures!\n");
873
874         /* check if portmask has non-existent ports */
875         if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
876                 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
877
878         /* initialize all ports */
879         for (portid = 0; portid < nb_ports; portid++) {
880                 struct rte_eth_conf local_port_conf = port_conf;
881                 struct rte_eth_rxconf rxq_conf;
882
883                 /* skip ports that are not enabled */
884                 if ((enabled_port_mask & (1 << portid)) == 0) {
885                         printf("Skipping disabled port %d\n", portid);
886                         continue;
887                 }
888
889                 qconf = &lcore_queue_conf[rx_lcore_id];
890
891                 /* limit the frame size to the maximum supported by NIC */
892                 rte_eth_dev_info_get(portid, &dev_info);
893                 local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
894                     dev_info.max_rx_pktlen,
895                     local_port_conf.rxmode.max_rx_pkt_len);
896
897                 /* get the lcore_id for this port */
898                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
899                        qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
900
901                         rx_lcore_id ++;
902                         if (rx_lcore_id >= RTE_MAX_LCORE)
903                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
904
905                         qconf = &lcore_queue_conf[rx_lcore_id];
906                 }
907
908                 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
909                 if (socket == SOCKET_ID_ANY)
910                         socket = 0;
911
912                 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
913                 rxq->portid = portid;
914                 rxq->direct_pool = socket_direct_pool[socket];
915                 rxq->indirect_pool = socket_indirect_pool[socket];
916                 rxq->lpm = socket_lpm[socket];
917                 rxq->lpm6 = socket_lpm6[socket];
918                 qconf->n_rx_queue++;
919
920                 /* init port */
921                 printf("Initializing port %d on lcore %u...", portid,
922                        rx_lcore_id);
923                 fflush(stdout);
924
925                 n_tx_queue = nb_lcores;
926                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
927                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
928                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
929                         local_port_conf.txmode.offloads |=
930                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
931                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
932                                             &local_port_conf);
933                 if (ret < 0) {
934                         printf("\n");
935                         rte_exit(EXIT_FAILURE, "Cannot configure device: "
936                                 "err=%d, port=%d\n",
937                                 ret, portid);
938                 }
939
940                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
941                                             &nb_txd);
942                 if (ret < 0) {
943                         printf("\n");
944                         rte_exit(EXIT_FAILURE, "Cannot adjust number of "
945                                 "descriptors: err=%d, port=%d\n", ret, portid);
946                 }
947
948                 /* init one RX queue */
949                 rxq_conf = dev_info.default_rxconf;
950                 rxq_conf.offloads = local_port_conf.rxmode.offloads;
951                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
952                                              socket, &rxq_conf,
953                                              socket_direct_pool[socket]);
954                 if (ret < 0) {
955                         printf("\n");
956                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
957                                 "err=%d, port=%d\n",
958                                 ret, portid);
959                 }
960
961                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
962                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
963                 printf("\n");
964
965                 /* init one TX queue per couple (lcore,port) */
966                 queueid = 0;
967                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
968                         if (rte_lcore_is_enabled(lcore_id) == 0)
969                                 continue;
970
971                         socket = (int) rte_lcore_to_socket_id(lcore_id);
972                         printf("txq=%u,%d ", lcore_id, queueid);
973                         fflush(stdout);
974
975                         txconf = &dev_info.default_txconf;
976                         txconf->txq_flags = ETH_TXQ_FLAGS_IGNORE;
977                         txconf->offloads = local_port_conf.txmode.offloads;
978                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
979                                                      socket, txconf);
980                         if (ret < 0) {
981                                 printf("\n");
982                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
983                                         "err=%d, port=%d\n", ret, portid);
984                         }
985
986                         qconf = &lcore_queue_conf[lcore_id];
987                         qconf->tx_queue_id[portid] = queueid;
988                         queueid++;
989                 }
990
991                 printf("\n");
992         }
993
994         printf("\n");
995
996         /* start ports */
997         for (portid = 0; portid < nb_ports; portid++) {
998                 if ((enabled_port_mask & (1 << portid)) == 0) {
999                         continue;
1000                 }
1001                 /* Start device */
1002                 ret = rte_eth_dev_start(portid);
1003                 if (ret < 0)
1004                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1005                                 ret, portid);
1006
1007                 rte_eth_promiscuous_enable(portid);
1008
1009                 if (check_ptype(portid) == 0) {
1010                         rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
1011                         printf("Add Rx callback function to detect L3 packet type by SW :"
1012                                 " port = %d\n", portid);
1013                 }
1014         }
1015
1016         if (init_routing_table() < 0)
1017                 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1018
1019         check_all_ports_link_status(nb_ports, enabled_port_mask);
1020
1021         /* launch per-lcore init on every lcore */
1022         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1023         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1024                 if (rte_eal_wait_lcore(lcore_id) < 0)
1025                         return -1;
1026         }
1027
1028         return 0;
1029 }