net/softnic: add table action profile
[dpdk.git] / examples / ip_reassembly / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include <signal.h>
16 #include <sys/param.h>
17
18 #include <rte_common.h>
19 #include <rte_byteorder.h>
20 #include <rte_log.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_eal.h>
24 #include <rte_launch.h>
25 #include <rte_atomic.h>
26 #include <rte_cycles.h>
27 #include <rte_prefetch.h>
28 #include <rte_lcore.h>
29 #include <rte_per_lcore.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_interrupts.h>
32 #include <rte_random.h>
33 #include <rte_debug.h>
34 #include <rte_ether.h>
35 #include <rte_ethdev.h>
36 #include <rte_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_malloc.h>
39 #include <rte_ip.h>
40 #include <rte_tcp.h>
41 #include <rte_udp.h>
42 #include <rte_string_fns.h>
43 #include <rte_lpm.h>
44 #include <rte_lpm6.h>
45
46 #include <rte_ip_frag.h>
47
48 #define MAX_PKT_BURST 32
49
50
51 #define RTE_LOGTYPE_IP_RSMBL RTE_LOGTYPE_USER1
52
53 #define MAX_JUMBO_PKT_LEN  9600
54
55 #define BUF_SIZE        RTE_MBUF_DEFAULT_DATAROOM
56 #define MBUF_DATA_SIZE  RTE_MBUF_DEFAULT_BUF_SIZE
57
58 #define NB_MBUF 8192
59 #define MEMPOOL_CACHE_SIZE 256
60
61 /* allow max jumbo frame 9.5 KB */
62 #define JUMBO_FRAME_MAX_SIZE    0x2600
63
64 #define MAX_FLOW_NUM    UINT16_MAX
65 #define MIN_FLOW_NUM    1
66 #define DEF_FLOW_NUM    0x1000
67
68 /* TTL numbers are in ms. */
69 #define MAX_FLOW_TTL    (3600 * MS_PER_S)
70 #define MIN_FLOW_TTL    1
71 #define DEF_FLOW_TTL    MS_PER_S
72
73 #define MAX_FRAG_NUM RTE_LIBRTE_IP_FRAG_MAX_FRAG
74
75 /* Should be power of two. */
76 #define IP_FRAG_TBL_BUCKET_ENTRIES      16
77
78 static uint32_t max_flow_num = DEF_FLOW_NUM;
79 static uint32_t max_flow_ttl = DEF_FLOW_TTL;
80
81 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
82
83 #define NB_SOCKETS 8
84
85 /* Configure how many packets ahead to prefetch, when reading packets */
86 #define PREFETCH_OFFSET 3
87
88 /*
89  * Configurable number of RX/TX ring descriptors
90  */
91 #define RTE_TEST_RX_DESC_DEFAULT 1024
92 #define RTE_TEST_TX_DESC_DEFAULT 1024
93
94 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
95 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
96
97 /* ethernet addresses of ports */
98 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
99
100 #ifndef IPv4_BYTES
101 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
102 #define IPv4_BYTES(addr) \
103                 (uint8_t) (((addr) >> 24) & 0xFF),\
104                 (uint8_t) (((addr) >> 16) & 0xFF),\
105                 (uint8_t) (((addr) >> 8) & 0xFF),\
106                 (uint8_t) ((addr) & 0xFF)
107 #endif
108
109 #ifndef IPv6_BYTES
110 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
111                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
112 #define IPv6_BYTES(addr) \
113         addr[0],  addr[1], addr[2],  addr[3], \
114         addr[4],  addr[5], addr[6],  addr[7], \
115         addr[8],  addr[9], addr[10], addr[11],\
116         addr[12], addr[13],addr[14], addr[15]
117 #endif
118
119 #define IPV6_ADDR_LEN 16
120
121 /* mask of enabled ports */
122 static uint32_t enabled_port_mask = 0;
123
124 static int rx_queue_per_lcore = 1;
125
126 struct mbuf_table {
127         uint32_t len;
128         uint32_t head;
129         uint32_t tail;
130         struct rte_mbuf *m_table[0];
131 };
132
133 struct rx_queue {
134         struct rte_ip_frag_tbl *frag_tbl;
135         struct rte_mempool *pool;
136         struct rte_lpm *lpm;
137         struct rte_lpm6 *lpm6;
138         uint16_t portid;
139 };
140
141 struct tx_lcore_stat {
142         uint64_t call;
143         uint64_t drop;
144         uint64_t queue;
145         uint64_t send;
146 };
147
148 #define MAX_RX_QUEUE_PER_LCORE 16
149 #define MAX_TX_QUEUE_PER_PORT 16
150 #define MAX_RX_QUEUE_PER_PORT 128
151
152 struct lcore_queue_conf {
153         uint16_t n_rx_queue;
154         struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
155         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
156         struct rte_ip_frag_death_row death_row;
157         struct mbuf_table *tx_mbufs[RTE_MAX_ETHPORTS];
158         struct tx_lcore_stat tx_stat;
159 } __rte_cache_aligned;
160 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
161
162 static struct rte_eth_conf port_conf = {
163         .rxmode = {
164                 .mq_mode        = ETH_MQ_RX_RSS,
165                 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
166                 .split_hdr_size = 0,
167                 .offloads = (DEV_RX_OFFLOAD_CHECKSUM |
168                              DEV_RX_OFFLOAD_JUMBO_FRAME |
169                              DEV_RX_OFFLOAD_CRC_STRIP),
170         },
171         .rx_adv_conf = {
172                         .rss_conf = {
173                                 .rss_key = NULL,
174                                 .rss_hf = ETH_RSS_IP,
175                 },
176         },
177         .txmode = {
178                 .mq_mode = ETH_MQ_TX_NONE,
179                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
180                              DEV_TX_OFFLOAD_MULTI_SEGS),
181         },
182 };
183
184 /*
185  * IPv4 forwarding table
186  */
187 struct l3fwd_ipv4_route {
188         uint32_t ip;
189         uint8_t  depth;
190         uint8_t  if_out;
191 };
192
193 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
194                 {IPv4(100,10,0,0), 16, 0},
195                 {IPv4(100,20,0,0), 16, 1},
196                 {IPv4(100,30,0,0), 16, 2},
197                 {IPv4(100,40,0,0), 16, 3},
198                 {IPv4(100,50,0,0), 16, 4},
199                 {IPv4(100,60,0,0), 16, 5},
200                 {IPv4(100,70,0,0), 16, 6},
201                 {IPv4(100,80,0,0), 16, 7},
202 };
203
204 /*
205  * IPv6 forwarding table
206  */
207
208 struct l3fwd_ipv6_route {
209         uint8_t ip[IPV6_ADDR_LEN];
210         uint8_t depth;
211         uint8_t if_out;
212 };
213
214 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
215         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
216         {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
217         {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
218         {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
219         {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
220         {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
221         {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
222         {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
223 };
224
225 #define LPM_MAX_RULES         1024
226 #define LPM6_MAX_RULES         1024
227 #define LPM6_NUMBER_TBL8S (1 << 16)
228
229 struct rte_lpm6_config lpm6_config = {
230                 .max_rules = LPM6_MAX_RULES,
231                 .number_tbl8s = LPM6_NUMBER_TBL8S,
232                 .flags = 0
233 };
234
235 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
236 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
237
238 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT
239 #define TX_LCORE_STAT_UPDATE(s, f, v)   ((s)->f += (v))
240 #else
241 #define TX_LCORE_STAT_UPDATE(s, f, v)   do {} while (0)
242 #endif /* RTE_LIBRTE_IP_FRAG_TBL_STAT */
243
244 /*
245  * If number of queued packets reached given threahold, then
246  * send burst of packets on an output interface.
247  */
248 static inline uint32_t
249 send_burst(struct lcore_queue_conf *qconf, uint32_t thresh, uint16_t port)
250 {
251         uint32_t fill, len, k, n;
252         struct mbuf_table *txmb;
253
254         txmb = qconf->tx_mbufs[port];
255         len = txmb->len;
256
257         if ((int32_t)(fill = txmb->head - txmb->tail) < 0)
258                 fill += len;
259
260         if (fill >= thresh) {
261                 n = RTE_MIN(len - txmb->tail, fill);
262
263                 k = rte_eth_tx_burst(port, qconf->tx_queue_id[port],
264                         txmb->m_table + txmb->tail, (uint16_t)n);
265
266                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, call, 1);
267                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, send, k);
268
269                 fill -= k;
270                 if ((txmb->tail += k) == len)
271                         txmb->tail = 0;
272         }
273
274         return fill;
275 }
276
277 /* Enqueue a single packet, and send burst if queue is filled */
278 static inline int
279 send_single_packet(struct rte_mbuf *m, uint16_t port)
280 {
281         uint32_t fill, lcore_id, len;
282         struct lcore_queue_conf *qconf;
283         struct mbuf_table *txmb;
284
285         lcore_id = rte_lcore_id();
286         qconf = &lcore_queue_conf[lcore_id];
287
288         txmb = qconf->tx_mbufs[port];
289         len = txmb->len;
290
291         fill = send_burst(qconf, MAX_PKT_BURST, port);
292
293         if (fill == len - 1) {
294                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, drop, 1);
295                 rte_pktmbuf_free(txmb->m_table[txmb->tail]);
296                 if (++txmb->tail == len)
297                         txmb->tail = 0;
298         }
299
300         TX_LCORE_STAT_UPDATE(&qconf->tx_stat, queue, 1);
301         txmb->m_table[txmb->head] = m;
302         if(++txmb->head == len)
303                 txmb->head = 0;
304
305         return 0;
306 }
307
308 static inline void
309 reassemble(struct rte_mbuf *m, uint16_t portid, uint32_t queue,
310         struct lcore_queue_conf *qconf, uint64_t tms)
311 {
312         struct ether_hdr *eth_hdr;
313         struct rte_ip_frag_tbl *tbl;
314         struct rte_ip_frag_death_row *dr;
315         struct rx_queue *rxq;
316         void *d_addr_bytes;
317         uint32_t next_hop;
318         uint16_t dst_port;
319
320         rxq = &qconf->rx_queue_list[queue];
321
322         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
323
324         dst_port = portid;
325
326         /* if packet is IPv4 */
327         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
328                 struct ipv4_hdr *ip_hdr;
329                 uint32_t ip_dst;
330
331                 ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
332
333                  /* if it is a fragmented packet, then try to reassemble. */
334                 if (rte_ipv4_frag_pkt_is_fragmented(ip_hdr)) {
335                         struct rte_mbuf *mo;
336
337                         tbl = rxq->frag_tbl;
338                         dr = &qconf->death_row;
339
340                         /* prepare mbuf: setup l2_len/l3_len. */
341                         m->l2_len = sizeof(*eth_hdr);
342                         m->l3_len = sizeof(*ip_hdr);
343
344                         /* process this fragment. */
345                         mo = rte_ipv4_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr);
346                         if (mo == NULL)
347                                 /* no packet to send out. */
348                                 return;
349
350                         /* we have our packet reassembled. */
351                         if (mo != m) {
352                                 m = mo;
353                                 eth_hdr = rte_pktmbuf_mtod(m,
354                                         struct ether_hdr *);
355                                 ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
356                         }
357                 }
358                 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
359
360                 /* Find destination port */
361                 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
362                                 (enabled_port_mask & 1 << next_hop) != 0) {
363                         dst_port = next_hop;
364                 }
365
366                 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
367         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
368                 /* if packet is IPv6 */
369                 struct ipv6_extension_fragment *frag_hdr;
370                 struct ipv6_hdr *ip_hdr;
371
372                 ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
373
374                 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(ip_hdr);
375
376                 if (frag_hdr != NULL) {
377                         struct rte_mbuf *mo;
378
379                         tbl = rxq->frag_tbl;
380                         dr  = &qconf->death_row;
381
382                         /* prepare mbuf: setup l2_len/l3_len. */
383                         m->l2_len = sizeof(*eth_hdr);
384                         m->l3_len = sizeof(*ip_hdr) + sizeof(*frag_hdr);
385
386                         mo = rte_ipv6_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr, frag_hdr);
387                         if (mo == NULL)
388                                 return;
389
390                         if (mo != m) {
391                                 m = mo;
392                                 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
393                                 ip_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
394                         }
395                 }
396
397                 /* Find destination port */
398                 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
399                                                 &next_hop) == 0 &&
400                                 (enabled_port_mask & 1 << next_hop) != 0) {
401                         dst_port = next_hop;
402                 }
403
404                 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
405         }
406         /* if packet wasn't IPv4 or IPv6, it's forwarded to the port it came from */
407
408         /* 02:00:00:00:00:xx */
409         d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
410         *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
411
412         /* src addr */
413         ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
414
415         send_single_packet(m, dst_port);
416 }
417
418 /* main processing loop */
419 static int
420 main_loop(__attribute__((unused)) void *dummy)
421 {
422         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
423         unsigned lcore_id;
424         uint64_t diff_tsc, cur_tsc, prev_tsc;
425         int i, j, nb_rx;
426         uint16_t portid;
427         struct lcore_queue_conf *qconf;
428         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
429
430         prev_tsc = 0;
431
432         lcore_id = rte_lcore_id();
433         qconf = &lcore_queue_conf[lcore_id];
434
435         if (qconf->n_rx_queue == 0) {
436                 RTE_LOG(INFO, IP_RSMBL, "lcore %u has nothing to do\n", lcore_id);
437                 return 0;
438         }
439
440         RTE_LOG(INFO, IP_RSMBL, "entering main loop on lcore %u\n", lcore_id);
441
442         for (i = 0; i < qconf->n_rx_queue; i++) {
443
444                 portid = qconf->rx_queue_list[i].portid;
445                 RTE_LOG(INFO, IP_RSMBL, " -- lcoreid=%u portid=%u\n", lcore_id,
446                         portid);
447         }
448
449         while (1) {
450
451                 cur_tsc = rte_rdtsc();
452
453                 /*
454                  * TX burst queue drain
455                  */
456                 diff_tsc = cur_tsc - prev_tsc;
457                 if (unlikely(diff_tsc > drain_tsc)) {
458
459                         /*
460                          * This could be optimized (use queueid instead of
461                          * portid), but it is not called so often
462                          */
463                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
464                                 if ((enabled_port_mask & (1 << portid)) != 0)
465                                         send_burst(qconf, 1, portid);
466                         }
467
468                         prev_tsc = cur_tsc;
469                 }
470
471                 /*
472                  * Read packet from RX queues
473                  */
474                 for (i = 0; i < qconf->n_rx_queue; ++i) {
475
476                         portid = qconf->rx_queue_list[i].portid;
477
478                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
479                                 MAX_PKT_BURST);
480
481                         /* Prefetch first packets */
482                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
483                                 rte_prefetch0(rte_pktmbuf_mtod(
484                                                 pkts_burst[j], void *));
485                         }
486
487                         /* Prefetch and forward already prefetched packets */
488                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
489                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
490                                         j + PREFETCH_OFFSET], void *));
491                                 reassemble(pkts_burst[j], portid,
492                                         i, qconf, cur_tsc);
493                         }
494
495                         /* Forward remaining prefetched packets */
496                         for (; j < nb_rx; j++) {
497                                 reassemble(pkts_burst[j], portid,
498                                         i, qconf, cur_tsc);
499                         }
500
501                         rte_ip_frag_free_death_row(&qconf->death_row,
502                                 PREFETCH_OFFSET);
503                 }
504         }
505 }
506
507 /* display usage */
508 static void
509 print_usage(const char *prgname)
510 {
511         printf("%s [EAL options] -- -p PORTMASK [-q NQ]"
512                 "  [--max-pkt-len PKTLEN]"
513                 "  [--maxflows=<flows>]  [--flowttl=<ttl>[(s|ms)]]\n"
514                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
515                 "  -q NQ: number of RX queues per lcore\n"
516                 "  --maxflows=<flows>: optional, maximum number of flows "
517                 "supported\n"
518                 "  --flowttl=<ttl>[(s|ms)]: optional, maximum TTL for each "
519                 "flow\n",
520                 prgname);
521 }
522
523 static uint32_t
524 parse_flow_num(const char *str, uint32_t min, uint32_t max, uint32_t *val)
525 {
526         char *end;
527         uint64_t v;
528
529         /* parse decimal string */
530         errno = 0;
531         v = strtoul(str, &end, 10);
532         if (errno != 0 || *end != '\0')
533                 return -EINVAL;
534
535         if (v < min || v > max)
536                 return -EINVAL;
537
538         *val = (uint32_t)v;
539         return 0;
540 }
541
542 static int
543 parse_flow_ttl(const char *str, uint32_t min, uint32_t max, uint32_t *val)
544 {
545         char *end;
546         uint64_t v;
547
548         static const char frmt_sec[] = "s";
549         static const char frmt_msec[] = "ms";
550
551         /* parse decimal string */
552         errno = 0;
553         v = strtoul(str, &end, 10);
554         if (errno != 0)
555                 return -EINVAL;
556
557         if (*end != '\0') {
558                 if (strncmp(frmt_sec, end, sizeof(frmt_sec)) == 0)
559                         v *= MS_PER_S;
560                 else if (strncmp(frmt_msec, end, sizeof (frmt_msec)) != 0)
561                         return -EINVAL;
562         }
563
564         if (v < min || v > max)
565                 return -EINVAL;
566
567         *val = (uint32_t)v;
568         return 0;
569 }
570
571 static int
572 parse_portmask(const char *portmask)
573 {
574         char *end = NULL;
575         unsigned long pm;
576
577         /* parse hexadecimal string */
578         pm = strtoul(portmask, &end, 16);
579         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
580                 return -1;
581
582         if (pm == 0)
583                 return -1;
584
585         return pm;
586 }
587
588 static int
589 parse_nqueue(const char *q_arg)
590 {
591         char *end = NULL;
592         unsigned long n;
593
594         printf("%p\n", q_arg);
595
596         /* parse hexadecimal string */
597         n = strtoul(q_arg, &end, 10);
598         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
599                 return -1;
600         if (n == 0)
601                 return -1;
602         if (n >= MAX_RX_QUEUE_PER_LCORE)
603                 return -1;
604
605         return n;
606 }
607
608 /* Parse the argument given in the command line of the application */
609 static int
610 parse_args(int argc, char **argv)
611 {
612         int opt, ret;
613         char **argvopt;
614         int option_index;
615         char *prgname = argv[0];
616         static struct option lgopts[] = {
617                 {"max-pkt-len", 1, 0, 0},
618                 {"maxflows", 1, 0, 0},
619                 {"flowttl", 1, 0, 0},
620                 {NULL, 0, 0, 0}
621         };
622
623         argvopt = argv;
624
625         while ((opt = getopt_long(argc, argvopt, "p:q:",
626                                 lgopts, &option_index)) != EOF) {
627
628                 switch (opt) {
629                 /* portmask */
630                 case 'p':
631                         enabled_port_mask = parse_portmask(optarg);
632                         if (enabled_port_mask == 0) {
633                                 printf("invalid portmask\n");
634                                 print_usage(prgname);
635                                 return -1;
636                         }
637                         break;
638
639                 /* nqueue */
640                 case 'q':
641                         rx_queue_per_lcore = parse_nqueue(optarg);
642                         if (rx_queue_per_lcore < 0) {
643                                 printf("invalid queue number\n");
644                                 print_usage(prgname);
645                                 return -1;
646                         }
647                         break;
648
649                 /* long options */
650                 case 0:
651                         if (!strncmp(lgopts[option_index].name,
652                                         "maxflows", 8)) {
653                                 if ((ret = parse_flow_num(optarg, MIN_FLOW_NUM,
654                                                 MAX_FLOW_NUM,
655                                                 &max_flow_num)) != 0) {
656                                         printf("invalid value: \"%s\" for "
657                                                 "parameter %s\n",
658                                                 optarg,
659                                                 lgopts[option_index].name);
660                                         print_usage(prgname);
661                                         return ret;
662                                 }
663                         }
664
665                         if (!strncmp(lgopts[option_index].name, "flowttl", 7)) {
666                                 if ((ret = parse_flow_ttl(optarg, MIN_FLOW_TTL,
667                                                 MAX_FLOW_TTL,
668                                                 &max_flow_ttl)) != 0) {
669                                         printf("invalid value: \"%s\" for "
670                                                 "parameter %s\n",
671                                                 optarg,
672                                                 lgopts[option_index].name);
673                                         print_usage(prgname);
674                                         return ret;
675                                 }
676                         }
677
678                         break;
679
680                 default:
681                         print_usage(prgname);
682                         return -1;
683                 }
684         }
685
686         if (optind >= 0)
687                 argv[optind-1] = prgname;
688
689         ret = optind-1;
690         optind = 1; /* reset getopt lib */
691         return ret;
692 }
693
694 static void
695 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
696 {
697         char buf[ETHER_ADDR_FMT_SIZE];
698         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
699         printf("%s%s", name, buf);
700 }
701
702 /* Check the link status of all ports in up to 9s, and print them finally */
703 static void
704 check_all_ports_link_status(uint32_t port_mask)
705 {
706 #define CHECK_INTERVAL 100 /* 100ms */
707 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
708         uint16_t portid;
709         uint8_t count, all_ports_up, print_flag = 0;
710         struct rte_eth_link link;
711
712         printf("\nChecking link status");
713         fflush(stdout);
714         for (count = 0; count <= MAX_CHECK_TIME; count++) {
715                 all_ports_up = 1;
716                 RTE_ETH_FOREACH_DEV(portid) {
717                         if ((port_mask & (1 << portid)) == 0)
718                                 continue;
719                         memset(&link, 0, sizeof(link));
720                         rte_eth_link_get_nowait(portid, &link);
721                         /* print link status if flag set */
722                         if (print_flag == 1) {
723                                 if (link.link_status)
724                                         printf(
725                                         "Port%d Link Up. Speed %u Mbps - %s\n",
726                                                 portid, link.link_speed,
727                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
728                                         ("full-duplex") : ("half-duplex\n"));
729                                 else
730                                         printf("Port %d Link Down\n", portid);
731                                 continue;
732                         }
733                         /* clear all_ports_up flag if any link down */
734                         if (link.link_status == ETH_LINK_DOWN) {
735                                 all_ports_up = 0;
736                                 break;
737                         }
738                 }
739                 /* after finally printing all link status, get out */
740                 if (print_flag == 1)
741                         break;
742
743                 if (all_ports_up == 0) {
744                         printf(".");
745                         fflush(stdout);
746                         rte_delay_ms(CHECK_INTERVAL);
747                 }
748
749                 /* set the print_flag if all ports up or timeout */
750                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
751                         print_flag = 1;
752                         printf("\ndone\n");
753                 }
754         }
755 }
756
757 static int
758 init_routing_table(void)
759 {
760         struct rte_lpm *lpm;
761         struct rte_lpm6 *lpm6;
762         int socket, ret;
763         unsigned i;
764
765         for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
766                 if (socket_lpm[socket]) {
767                         lpm = socket_lpm[socket];
768                         /* populate the LPM table */
769                         for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
770                                 ret = rte_lpm_add(lpm,
771                                         l3fwd_ipv4_route_array[i].ip,
772                                         l3fwd_ipv4_route_array[i].depth,
773                                         l3fwd_ipv4_route_array[i].if_out);
774
775                                 if (ret < 0) {
776                                         RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
777                                                 "LPM table\n", i);
778                                         return -1;
779                                 }
780
781                                 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv4_BYTES_FMT
782                                                 "/%d (port %d)\n",
783                                         socket,
784                                         IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
785                                         l3fwd_ipv4_route_array[i].depth,
786                                         l3fwd_ipv4_route_array[i].if_out);
787                         }
788                 }
789
790                 if (socket_lpm6[socket]) {
791                         lpm6 = socket_lpm6[socket];
792                         /* populate the LPM6 table */
793                         for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
794                                 ret = rte_lpm6_add(lpm6,
795                                         l3fwd_ipv6_route_array[i].ip,
796                                         l3fwd_ipv6_route_array[i].depth,
797                                         l3fwd_ipv6_route_array[i].if_out);
798
799                                 if (ret < 0) {
800                                         RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
801                                                 "LPM6 table\n", i);
802                                         return -1;
803                                 }
804
805                                 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv6_BYTES_FMT
806                                                 "/%d (port %d)\n",
807                                         socket,
808                                         IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
809                                         l3fwd_ipv6_route_array[i].depth,
810                                         l3fwd_ipv6_route_array[i].if_out);
811                         }
812                 }
813         }
814         return 0;
815 }
816
817 static int
818 setup_port_tbl(struct lcore_queue_conf *qconf, uint32_t lcore, int socket,
819         uint32_t port)
820 {
821         struct mbuf_table *mtb;
822         uint32_t n;
823         size_t sz;
824
825         n = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST);
826         sz = sizeof (*mtb) + sizeof (mtb->m_table[0]) *  n;
827
828         if ((mtb = rte_zmalloc_socket(__func__, sz, RTE_CACHE_LINE_SIZE,
829                         socket)) == NULL) {
830                 RTE_LOG(ERR, IP_RSMBL, "%s() for lcore: %u, port: %u "
831                         "failed to allocate %zu bytes\n",
832                         __func__, lcore, port, sz);
833                 return -1;
834         }
835
836         mtb->len = n;
837         qconf->tx_mbufs[port] = mtb;
838
839         return 0;
840 }
841
842 static int
843 setup_queue_tbl(struct rx_queue *rxq, uint32_t lcore, uint32_t queue)
844 {
845         int socket;
846         uint32_t nb_mbuf;
847         uint64_t frag_cycles;
848         char buf[RTE_MEMPOOL_NAMESIZE];
849
850         socket = rte_lcore_to_socket_id(lcore);
851         if (socket == SOCKET_ID_ANY)
852                 socket = 0;
853
854         frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S *
855                 max_flow_ttl;
856
857         if ((rxq->frag_tbl = rte_ip_frag_table_create(max_flow_num,
858                         IP_FRAG_TBL_BUCKET_ENTRIES, max_flow_num, frag_cycles,
859                         socket)) == NULL) {
860                 RTE_LOG(ERR, IP_RSMBL, "ip_frag_tbl_create(%u) on "
861                         "lcore: %u for queue: %u failed\n",
862                         max_flow_num, lcore, queue);
863                 return -1;
864         }
865
866         /*
867          * At any given moment up to <max_flow_num * (MAX_FRAG_NUM)>
868          * mbufs could be stored int the fragment table.
869          * Plus, each TX queue can hold up to <max_flow_num> packets.
870          */
871
872         nb_mbuf = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST) * MAX_FRAG_NUM;
873         nb_mbuf *= (port_conf.rxmode.max_rx_pkt_len + BUF_SIZE - 1) / BUF_SIZE;
874         nb_mbuf *= 2; /* ipv4 and ipv6 */
875         nb_mbuf += nb_rxd + nb_txd;
876
877         nb_mbuf = RTE_MAX(nb_mbuf, (uint32_t)NB_MBUF);
878
879         snprintf(buf, sizeof(buf), "mbuf_pool_%u_%u", lcore, queue);
880
881         rxq->pool = rte_pktmbuf_pool_create(buf, nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
882                                             MBUF_DATA_SIZE, socket);
883         if (rxq->pool == NULL) {
884                 RTE_LOG(ERR, IP_RSMBL,
885                         "rte_pktmbuf_pool_create(%s) failed", buf);
886                 return -1;
887         }
888
889         return 0;
890 }
891
892 static int
893 init_mem(void)
894 {
895         char buf[PATH_MAX];
896         struct rte_lpm *lpm;
897         struct rte_lpm6 *lpm6;
898         struct rte_lpm_config lpm_config;
899         int socket;
900         unsigned lcore_id;
901
902         /* traverse through lcores and initialize structures on each socket */
903
904         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
905
906                 if (rte_lcore_is_enabled(lcore_id) == 0)
907                         continue;
908
909                 socket = rte_lcore_to_socket_id(lcore_id);
910
911                 if (socket == SOCKET_ID_ANY)
912                         socket = 0;
913
914                 if (socket_lpm[socket] == NULL) {
915                         RTE_LOG(INFO, IP_RSMBL, "Creating LPM table on socket %i\n", socket);
916                         snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
917
918                         lpm_config.max_rules = LPM_MAX_RULES;
919                         lpm_config.number_tbl8s = 256;
920                         lpm_config.flags = 0;
921
922                         lpm = rte_lpm_create(buf, socket, &lpm_config);
923                         if (lpm == NULL) {
924                                 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
925                                 return -1;
926                         }
927                         socket_lpm[socket] = lpm;
928                 }
929
930                 if (socket_lpm6[socket] == NULL) {
931                         RTE_LOG(INFO, IP_RSMBL, "Creating LPM6 table on socket %i\n", socket);
932                         snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
933
934                         lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
935                         if (lpm6 == NULL) {
936                                 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
937                                 return -1;
938                         }
939                         socket_lpm6[socket] = lpm6;
940                 }
941         }
942
943         return 0;
944 }
945
946 static void
947 queue_dump_stat(void)
948 {
949         uint32_t i, lcore;
950         const struct lcore_queue_conf *qconf;
951
952         for (lcore = 0; lcore < RTE_MAX_LCORE; lcore++) {
953                 if (rte_lcore_is_enabled(lcore) == 0)
954                         continue;
955
956                 qconf = &lcore_queue_conf[lcore];
957                 for (i = 0; i < qconf->n_rx_queue; i++) {
958
959                         fprintf(stdout, " -- lcoreid=%u portid=%u "
960                                 "frag tbl stat:\n",
961                                 lcore,  qconf->rx_queue_list[i].portid);
962                         rte_ip_frag_table_statistics_dump(stdout,
963                                         qconf->rx_queue_list[i].frag_tbl);
964                         fprintf(stdout, "TX bursts:\t%" PRIu64 "\n"
965                                 "TX packets _queued:\t%" PRIu64 "\n"
966                                 "TX packets dropped:\t%" PRIu64 "\n"
967                                 "TX packets send:\t%" PRIu64 "\n",
968                                 qconf->tx_stat.call,
969                                 qconf->tx_stat.queue,
970                                 qconf->tx_stat.drop,
971                                 qconf->tx_stat.send);
972                 }
973         }
974 }
975
976 static void
977 signal_handler(int signum)
978 {
979         queue_dump_stat();
980         if (signum != SIGUSR1)
981                 rte_exit(0, "received signal: %d, exiting\n", signum);
982 }
983
984 int
985 main(int argc, char **argv)
986 {
987         struct lcore_queue_conf *qconf;
988         struct rte_eth_dev_info dev_info;
989         struct rte_eth_txconf *txconf;
990         struct rx_queue *rxq;
991         int ret, socket;
992         unsigned nb_ports;
993         uint16_t queueid;
994         unsigned lcore_id = 0, rx_lcore_id = 0;
995         uint32_t n_tx_queue, nb_lcores;
996         uint16_t portid;
997
998         /* init EAL */
999         ret = rte_eal_init(argc, argv);
1000         if (ret < 0)
1001                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1002         argc -= ret;
1003         argv += ret;
1004
1005         /* parse application arguments (after the EAL ones) */
1006         ret = parse_args(argc, argv);
1007         if (ret < 0)
1008                 rte_exit(EXIT_FAILURE, "Invalid IP reassembly parameters\n");
1009
1010         nb_ports = rte_eth_dev_count_avail();
1011         if (nb_ports == 0)
1012                 rte_exit(EXIT_FAILURE, "No ports found!\n");
1013
1014         nb_lcores = rte_lcore_count();
1015
1016         /* initialize structures (mempools, lpm etc.) */
1017         if (init_mem() < 0)
1018                 rte_panic("Cannot initialize memory structures!\n");
1019
1020         /* check if portmask has non-existent ports */
1021         if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
1022                 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
1023
1024         /* initialize all ports */
1025         RTE_ETH_FOREACH_DEV(portid) {
1026                 struct rte_eth_rxconf rxq_conf;
1027                 struct rte_eth_conf local_port_conf = port_conf;
1028
1029                 /* skip ports that are not enabled */
1030                 if ((enabled_port_mask & (1 << portid)) == 0) {
1031                         printf("\nSkipping disabled port %d\n", portid);
1032                         continue;
1033                 }
1034
1035                 qconf = &lcore_queue_conf[rx_lcore_id];
1036
1037                 /* limit the frame size to the maximum supported by NIC */
1038                 rte_eth_dev_info_get(portid, &dev_info);
1039                 local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
1040                     dev_info.max_rx_pktlen,
1041                     local_port_conf.rxmode.max_rx_pkt_len);
1042
1043                 /* get the lcore_id for this port */
1044                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1045                            qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
1046
1047                         rx_lcore_id++;
1048                         if (rx_lcore_id >= RTE_MAX_LCORE)
1049                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
1050
1051                         qconf = &lcore_queue_conf[rx_lcore_id];
1052                 }
1053
1054                 socket = rte_lcore_to_socket_id(portid);
1055                 if (socket == SOCKET_ID_ANY)
1056                         socket = 0;
1057
1058                 queueid = qconf->n_rx_queue;
1059                 rxq = &qconf->rx_queue_list[queueid];
1060                 rxq->portid = portid;
1061                 rxq->lpm = socket_lpm[socket];
1062                 rxq->lpm6 = socket_lpm6[socket];
1063
1064                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1065                                                        &nb_txd);
1066                 if (ret < 0)
1067                         rte_exit(EXIT_FAILURE,
1068                                  "Cannot adjust number of descriptors: err=%d, port=%d\n",
1069                                  ret, portid);
1070
1071                 if (setup_queue_tbl(rxq, rx_lcore_id, queueid) < 0)
1072                         rte_exit(EXIT_FAILURE, "Failed to set up queue table\n");
1073                 qconf->n_rx_queue++;
1074
1075                 /* init port */
1076                 printf("Initializing port %d ... ", portid );
1077                 fflush(stdout);
1078
1079                 n_tx_queue = nb_lcores;
1080                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1081                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1082                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1083                         local_port_conf.txmode.offloads |=
1084                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1085
1086                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1087                         dev_info.flow_type_rss_offloads;
1088                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1089                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1090                         printf("Port %u modified RSS hash function based on hardware support,"
1091                                 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1092                                 portid,
1093                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
1094                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1095                 }
1096
1097                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
1098                                             &local_port_conf);
1099                 if (ret < 0) {
1100                         printf("\n");
1101                         rte_exit(EXIT_FAILURE, "Cannot configure device: "
1102                                 "err=%d, port=%d\n",
1103                                 ret, portid);
1104                 }
1105
1106                 /* init one RX queue */
1107                 rxq_conf = dev_info.default_rxconf;
1108                 rxq_conf.offloads = local_port_conf.rxmode.offloads;
1109                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1110                                              socket, &rxq_conf,
1111                                              rxq->pool);
1112                 if (ret < 0) {
1113                         printf("\n");
1114                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
1115                                 "err=%d, port=%d\n",
1116                                 ret, portid);
1117                 }
1118
1119                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1120                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1121                 printf("\n");
1122
1123                 /* init one TX queue per couple (lcore,port) */
1124                 queueid = 0;
1125                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1126                         if (rte_lcore_is_enabled(lcore_id) == 0)
1127                                 continue;
1128
1129                         socket = (int) rte_lcore_to_socket_id(lcore_id);
1130
1131                         printf("txq=%u,%d,%d ", lcore_id, queueid, socket);
1132                         fflush(stdout);
1133
1134                         txconf = &dev_info.default_txconf;
1135                         txconf->offloads = local_port_conf.txmode.offloads;
1136
1137                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1138                                         socket, txconf);
1139                         if (ret < 0)
1140                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1141                                         "port=%d\n", ret, portid);
1142
1143                         qconf = &lcore_queue_conf[lcore_id];
1144                         qconf->tx_queue_id[portid] = queueid;
1145                         setup_port_tbl(qconf, lcore_id, socket, portid);
1146                         queueid++;
1147                 }
1148                 printf("\n");
1149         }
1150
1151         printf("\n");
1152
1153         /* start ports */
1154         RTE_ETH_FOREACH_DEV(portid) {
1155                 if ((enabled_port_mask & (1 << portid)) == 0) {
1156                         continue;
1157                 }
1158                 /* Start device */
1159                 ret = rte_eth_dev_start(portid);
1160                 if (ret < 0)
1161                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1162                                 ret, portid);
1163
1164                 rte_eth_promiscuous_enable(portid);
1165         }
1166
1167         if (init_routing_table() < 0)
1168                 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1169
1170         check_all_ports_link_status(enabled_port_mask);
1171
1172         signal(SIGUSR1, signal_handler);
1173         signal(SIGTERM, signal_handler);
1174         signal(SIGINT, signal_handler);
1175
1176         /* launch per-lcore init on every lcore */
1177         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1178         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1179                 if (rte_eal_wait_lcore(lcore_id) < 0)
1180                         return -1;
1181         }
1182
1183         return 0;
1184 }