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