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