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