954a11512a8ff740119e8bc28d79fc725297f41b
[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 rte_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         },
170         .rx_adv_conf = {
171                         .rss_conf = {
172                                 .rss_key = NULL,
173                                 .rss_hf = ETH_RSS_IP,
174                 },
175         },
176         .txmode = {
177                 .mq_mode = ETH_MQ_TX_NONE,
178                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
179                              DEV_TX_OFFLOAD_MULTI_SEGS),
180         },
181 };
182
183 /*
184  * IPv4 forwarding table
185  */
186 struct l3fwd_ipv4_route {
187         uint32_t ip;
188         uint8_t  depth;
189         uint8_t  if_out;
190 };
191
192 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
193                 {RTE_IPV4(100,10,0,0), 16, 0},
194                 {RTE_IPV4(100,20,0,0), 16, 1},
195                 {RTE_IPV4(100,30,0,0), 16, 2},
196                 {RTE_IPV4(100,40,0,0), 16, 3},
197                 {RTE_IPV4(100,50,0,0), 16, 4},
198                 {RTE_IPV4(100,60,0,0), 16, 5},
199                 {RTE_IPV4(100,70,0,0), 16, 6},
200                 {RTE_IPV4(100,80,0,0), 16, 7},
201 };
202
203 /*
204  * IPv6 forwarding table
205  */
206
207 struct l3fwd_ipv6_route {
208         uint8_t ip[IPV6_ADDR_LEN];
209         uint8_t depth;
210         uint8_t if_out;
211 };
212
213 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
214         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
215         {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
216         {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
217         {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
218         {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
219         {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
220         {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
221         {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
222 };
223
224 #define LPM_MAX_RULES         1024
225 #define LPM6_MAX_RULES         1024
226 #define LPM6_NUMBER_TBL8S (1 << 16)
227
228 struct rte_lpm6_config lpm6_config = {
229                 .max_rules = LPM6_MAX_RULES,
230                 .number_tbl8s = LPM6_NUMBER_TBL8S,
231                 .flags = 0
232 };
233
234 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
235 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
236
237 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT
238 #define TX_LCORE_STAT_UPDATE(s, f, v)   ((s)->f += (v))
239 #else
240 #define TX_LCORE_STAT_UPDATE(s, f, v)   do {} while (0)
241 #endif /* RTE_LIBRTE_IP_FRAG_TBL_STAT */
242
243 /*
244  * If number of queued packets reached given threahold, then
245  * send burst of packets on an output interface.
246  */
247 static inline uint32_t
248 send_burst(struct lcore_queue_conf *qconf, uint32_t thresh, uint16_t port)
249 {
250         uint32_t fill, len, k, n;
251         struct mbuf_table *txmb;
252
253         txmb = qconf->tx_mbufs[port];
254         len = txmb->len;
255
256         if ((int32_t)(fill = txmb->head - txmb->tail) < 0)
257                 fill += len;
258
259         if (fill >= thresh) {
260                 n = RTE_MIN(len - txmb->tail, fill);
261
262                 k = rte_eth_tx_burst(port, qconf->tx_queue_id[port],
263                         txmb->m_table + txmb->tail, (uint16_t)n);
264
265                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, call, 1);
266                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, send, k);
267
268                 fill -= k;
269                 if ((txmb->tail += k) == len)
270                         txmb->tail = 0;
271         }
272
273         return fill;
274 }
275
276 /* Enqueue a single packet, and send burst if queue is filled */
277 static inline int
278 send_single_packet(struct rte_mbuf *m, uint16_t port)
279 {
280         uint32_t fill, lcore_id, len;
281         struct lcore_queue_conf *qconf;
282         struct mbuf_table *txmb;
283
284         lcore_id = rte_lcore_id();
285         qconf = &lcore_queue_conf[lcore_id];
286
287         txmb = qconf->tx_mbufs[port];
288         len = txmb->len;
289
290         fill = send_burst(qconf, MAX_PKT_BURST, port);
291
292         if (fill == len - 1) {
293                 TX_LCORE_STAT_UPDATE(&qconf->tx_stat, drop, 1);
294                 rte_pktmbuf_free(txmb->m_table[txmb->tail]);
295                 if (++txmb->tail == len)
296                         txmb->tail = 0;
297         }
298
299         TX_LCORE_STAT_UPDATE(&qconf->tx_stat, queue, 1);
300         txmb->m_table[txmb->head] = m;
301         if(++txmb->head == len)
302                 txmb->head = 0;
303
304         return 0;
305 }
306
307 static inline void
308 reassemble(struct rte_mbuf *m, uint16_t portid, uint32_t queue,
309         struct lcore_queue_conf *qconf, uint64_t tms)
310 {
311         struct rte_ether_hdr *eth_hdr;
312         struct rte_ip_frag_tbl *tbl;
313         struct rte_ip_frag_death_row *dr;
314         struct rx_queue *rxq;
315         void *d_addr_bytes;
316         uint32_t next_hop;
317         uint16_t dst_port;
318
319         rxq = &qconf->rx_queue_list[queue];
320
321         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
322
323         dst_port = portid;
324
325         /* if packet is IPv4 */
326         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
327                 struct rte_ipv4_hdr *ip_hdr;
328                 uint32_t ip_dst;
329
330                 ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
331
332                  /* if it is a fragmented packet, then try to reassemble. */
333                 if (rte_ipv4_frag_pkt_is_fragmented(ip_hdr)) {
334                         struct rte_mbuf *mo;
335
336                         tbl = rxq->frag_tbl;
337                         dr = &qconf->death_row;
338
339                         /* prepare mbuf: setup l2_len/l3_len. */
340                         m->l2_len = sizeof(*eth_hdr);
341                         m->l3_len = sizeof(*ip_hdr);
342
343                         /* process this fragment. */
344                         mo = rte_ipv4_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr);
345                         if (mo == NULL)
346                                 /* no packet to send out. */
347                                 return;
348
349                         /* we have our packet reassembled. */
350                         if (mo != m) {
351                                 m = mo;
352                                 eth_hdr = rte_pktmbuf_mtod(m,
353                                         struct rte_ether_hdr *);
354                                 ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
355                         }
356
357                         /* update offloading flags */
358                         m->ol_flags |= (PKT_TX_IPV4 | PKT_TX_IP_CKSUM);
359                 }
360                 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
361
362                 /* Find destination port */
363                 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
364                                 (enabled_port_mask & 1 << next_hop) != 0) {
365                         dst_port = next_hop;
366                 }
367
368                 eth_hdr->ether_type = rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV4);
369         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
370                 /* if packet is IPv6 */
371                 struct ipv6_extension_fragment *frag_hdr;
372                 struct rte_ipv6_hdr *ip_hdr;
373
374                 ip_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
375
376                 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(ip_hdr);
377
378                 if (frag_hdr != NULL) {
379                         struct rte_mbuf *mo;
380
381                         tbl = rxq->frag_tbl;
382                         dr  = &qconf->death_row;
383
384                         /* prepare mbuf: setup l2_len/l3_len. */
385                         m->l2_len = sizeof(*eth_hdr);
386                         m->l3_len = sizeof(*ip_hdr) + sizeof(*frag_hdr);
387
388                         mo = rte_ipv6_frag_reassemble_packet(tbl, dr, m, tms, ip_hdr, frag_hdr);
389                         if (mo == NULL)
390                                 return;
391
392                         if (mo != m) {
393                                 m = mo;
394                                 eth_hdr = rte_pktmbuf_mtod(m,
395                                                         struct rte_ether_hdr *);
396                                 ip_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
397                         }
398                 }
399
400                 /* Find destination port */
401                 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
402                                                 &next_hop) == 0 &&
403                                 (enabled_port_mask & 1 << next_hop) != 0) {
404                         dst_port = next_hop;
405                 }
406
407                 eth_hdr->ether_type = rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV6);
408         }
409         /* if packet wasn't IPv4 or IPv6, it's forwarded to the port it came from */
410
411         /* 02:00:00:00:00:xx */
412         d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
413         *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
414
415         /* src addr */
416         rte_ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
417
418         send_single_packet(m, dst_port);
419 }
420
421 /* main processing loop */
422 static int
423 main_loop(__rte_unused void *dummy)
424 {
425         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
426         unsigned lcore_id;
427         uint64_t diff_tsc, cur_tsc, prev_tsc;
428         int i, j, nb_rx;
429         uint16_t portid;
430         struct lcore_queue_conf *qconf;
431         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
432
433         prev_tsc = 0;
434
435         lcore_id = rte_lcore_id();
436         qconf = &lcore_queue_conf[lcore_id];
437
438         if (qconf->n_rx_queue == 0) {
439                 RTE_LOG(INFO, IP_RSMBL, "lcore %u has nothing to do\n", lcore_id);
440                 return 0;
441         }
442
443         RTE_LOG(INFO, IP_RSMBL, "entering main loop on lcore %u\n", lcore_id);
444
445         for (i = 0; i < qconf->n_rx_queue; i++) {
446
447                 portid = qconf->rx_queue_list[i].portid;
448                 RTE_LOG(INFO, IP_RSMBL, " -- lcoreid=%u portid=%u\n", lcore_id,
449                         portid);
450         }
451
452         while (1) {
453
454                 cur_tsc = rte_rdtsc();
455
456                 /*
457                  * TX burst queue drain
458                  */
459                 diff_tsc = cur_tsc - prev_tsc;
460                 if (unlikely(diff_tsc > drain_tsc)) {
461
462                         /*
463                          * This could be optimized (use queueid instead of
464                          * portid), but it is not called so often
465                          */
466                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
467                                 if ((enabled_port_mask & (1 << portid)) != 0)
468                                         send_burst(qconf, 1, portid);
469                         }
470
471                         prev_tsc = cur_tsc;
472                 }
473
474                 /*
475                  * Read packet from RX queues
476                  */
477                 for (i = 0; i < qconf->n_rx_queue; ++i) {
478
479                         portid = qconf->rx_queue_list[i].portid;
480
481                         nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
482                                 MAX_PKT_BURST);
483
484                         /* Prefetch first packets */
485                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
486                                 rte_prefetch0(rte_pktmbuf_mtod(
487                                                 pkts_burst[j], void *));
488                         }
489
490                         /* Prefetch and forward already prefetched packets */
491                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
492                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
493                                         j + PREFETCH_OFFSET], void *));
494                                 reassemble(pkts_burst[j], portid,
495                                         i, qconf, cur_tsc);
496                         }
497
498                         /* Forward remaining prefetched packets */
499                         for (; j < nb_rx; j++) {
500                                 reassemble(pkts_burst[j], portid,
501                                         i, qconf, cur_tsc);
502                         }
503
504                         rte_ip_frag_free_death_row(&qconf->death_row,
505                                 PREFETCH_OFFSET);
506                 }
507         }
508 }
509
510 /* display usage */
511 static void
512 print_usage(const char *prgname)
513 {
514         printf("%s [EAL options] -- -p PORTMASK [-q NQ]"
515                 "  [--max-pkt-len PKTLEN]"
516                 "  [--maxflows=<flows>]  [--flowttl=<ttl>[(s|ms)]]\n"
517                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
518                 "  -q NQ: number of RX queues per lcore\n"
519                 "  --maxflows=<flows>: optional, maximum number of flows "
520                 "supported\n"
521                 "  --flowttl=<ttl>[(s|ms)]: optional, maximum TTL for each "
522                 "flow\n",
523                 prgname);
524 }
525
526 static uint32_t
527 parse_flow_num(const char *str, uint32_t min, uint32_t max, uint32_t *val)
528 {
529         char *end;
530         uint64_t v;
531
532         /* parse decimal string */
533         errno = 0;
534         v = strtoul(str, &end, 10);
535         if (errno != 0 || *end != '\0')
536                 return -EINVAL;
537
538         if (v < min || v > max)
539                 return -EINVAL;
540
541         *val = (uint32_t)v;
542         return 0;
543 }
544
545 static int
546 parse_flow_ttl(const char *str, uint32_t min, uint32_t max, uint32_t *val)
547 {
548         char *end;
549         uint64_t v;
550
551         static const char frmt_sec[] = "s";
552         static const char frmt_msec[] = "ms";
553
554         /* parse decimal string */
555         errno = 0;
556         v = strtoul(str, &end, 10);
557         if (errno != 0)
558                 return -EINVAL;
559
560         if (*end != '\0') {
561                 if (strncmp(frmt_sec, end, sizeof(frmt_sec)) == 0)
562                         v *= MS_PER_S;
563                 else if (strncmp(frmt_msec, end, sizeof (frmt_msec)) != 0)
564                         return -EINVAL;
565         }
566
567         if (v < min || v > max)
568                 return -EINVAL;
569
570         *val = (uint32_t)v;
571         return 0;
572 }
573
574 static int
575 parse_portmask(const char *portmask)
576 {
577         char *end = NULL;
578         unsigned long pm;
579
580         /* parse hexadecimal string */
581         pm = strtoul(portmask, &end, 16);
582         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
583                 return 0;
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 rte_ether_addr *eth_addr)
696 {
697         char buf[RTE_ETHER_ADDR_FMT_SIZE];
698         rte_ether_format_addr(buf, RTE_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         int ret;
712         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
713
714         printf("\nChecking link status");
715         fflush(stdout);
716         for (count = 0; count <= MAX_CHECK_TIME; count++) {
717                 all_ports_up = 1;
718                 RTE_ETH_FOREACH_DEV(portid) {
719                         if ((port_mask & (1 << portid)) == 0)
720                                 continue;
721                         memset(&link, 0, sizeof(link));
722                         ret = rte_eth_link_get_nowait(portid, &link);
723                         if (ret < 0) {
724                                 all_ports_up = 0;
725                                 if (print_flag == 1)
726                                         printf("Port %u link get failed: %s\n",
727                                                 portid, rte_strerror(-ret));
728                                 continue;
729                         }
730                         /* print link status if flag set */
731                         if (print_flag == 1) {
732                                 rte_eth_link_to_str(link_status_text,
733                                         sizeof(link_status_text), &link);
734                                 printf("Port %d %s\n", portid,
735                                        link_status_text);
736                                 continue;
737                         }
738                         /* clear all_ports_up flag if any link down */
739                         if (link.link_status == ETH_LINK_DOWN) {
740                                 all_ports_up = 0;
741                                 break;
742                         }
743                 }
744                 /* after finally printing all link status, get out */
745                 if (print_flag == 1)
746                         break;
747
748                 if (all_ports_up == 0) {
749                         printf(".");
750                         fflush(stdout);
751                         rte_delay_ms(CHECK_INTERVAL);
752                 }
753
754                 /* set the print_flag if all ports up or timeout */
755                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
756                         print_flag = 1;
757                         printf("\ndone\n");
758                 }
759         }
760 }
761
762 static int
763 init_routing_table(void)
764 {
765         struct rte_lpm *lpm;
766         struct rte_lpm6 *lpm6;
767         int socket, ret;
768         unsigned i;
769
770         for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
771                 if (socket_lpm[socket]) {
772                         lpm = socket_lpm[socket];
773                         /* populate the LPM table */
774                         for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
775                                 ret = rte_lpm_add(lpm,
776                                         l3fwd_ipv4_route_array[i].ip,
777                                         l3fwd_ipv4_route_array[i].depth,
778                                         l3fwd_ipv4_route_array[i].if_out);
779
780                                 if (ret < 0) {
781                                         RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
782                                                 "LPM table\n", i);
783                                         return -1;
784                                 }
785
786                                 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv4_BYTES_FMT
787                                                 "/%d (port %d)\n",
788                                         socket,
789                                         IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
790                                         l3fwd_ipv4_route_array[i].depth,
791                                         l3fwd_ipv4_route_array[i].if_out);
792                         }
793                 }
794
795                 if (socket_lpm6[socket]) {
796                         lpm6 = socket_lpm6[socket];
797                         /* populate the LPM6 table */
798                         for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
799                                 ret = rte_lpm6_add(lpm6,
800                                         l3fwd_ipv6_route_array[i].ip,
801                                         l3fwd_ipv6_route_array[i].depth,
802                                         l3fwd_ipv6_route_array[i].if_out);
803
804                                 if (ret < 0) {
805                                         RTE_LOG(ERR, IP_RSMBL, "Unable to add entry %i to the l3fwd "
806                                                 "LPM6 table\n", i);
807                                         return -1;
808                                 }
809
810                                 RTE_LOG(INFO, IP_RSMBL, "Socket %i: adding route " IPv6_BYTES_FMT
811                                                 "/%d (port %d)\n",
812                                         socket,
813                                         IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
814                                         l3fwd_ipv6_route_array[i].depth,
815                                         l3fwd_ipv6_route_array[i].if_out);
816                         }
817                 }
818         }
819         return 0;
820 }
821
822 static int
823 setup_port_tbl(struct lcore_queue_conf *qconf, uint32_t lcore, int socket,
824         uint32_t port)
825 {
826         struct mbuf_table *mtb;
827         uint32_t n;
828         size_t sz;
829
830         n = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST);
831         sz = sizeof (*mtb) + sizeof (mtb->m_table[0]) *  n;
832
833         if ((mtb = rte_zmalloc_socket(__func__, sz, RTE_CACHE_LINE_SIZE,
834                         socket)) == NULL) {
835                 RTE_LOG(ERR, IP_RSMBL, "%s() for lcore: %u, port: %u "
836                         "failed to allocate %zu bytes\n",
837                         __func__, lcore, port, sz);
838                 return -1;
839         }
840
841         mtb->len = n;
842         qconf->tx_mbufs[port] = mtb;
843
844         return 0;
845 }
846
847 static int
848 setup_queue_tbl(struct rx_queue *rxq, uint32_t lcore, uint32_t queue)
849 {
850         int socket;
851         uint32_t nb_mbuf;
852         uint64_t frag_cycles;
853         char buf[RTE_MEMPOOL_NAMESIZE];
854
855         socket = rte_lcore_to_socket_id(lcore);
856         if (socket == SOCKET_ID_ANY)
857                 socket = 0;
858
859         frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S *
860                 max_flow_ttl;
861
862         if ((rxq->frag_tbl = rte_ip_frag_table_create(max_flow_num,
863                         IP_FRAG_TBL_BUCKET_ENTRIES, max_flow_num, frag_cycles,
864                         socket)) == NULL) {
865                 RTE_LOG(ERR, IP_RSMBL, "ip_frag_tbl_create(%u) on "
866                         "lcore: %u for queue: %u failed\n",
867                         max_flow_num, lcore, queue);
868                 return -1;
869         }
870
871         /*
872          * At any given moment up to <max_flow_num * (MAX_FRAG_NUM)>
873          * mbufs could be stored int the fragment table.
874          * Plus, each TX queue can hold up to <max_flow_num> packets.
875          */
876
877         nb_mbuf = RTE_MAX(max_flow_num, 2UL * MAX_PKT_BURST) * MAX_FRAG_NUM;
878         nb_mbuf *= (port_conf.rxmode.max_rx_pkt_len + BUF_SIZE - 1) / BUF_SIZE;
879         nb_mbuf *= 2; /* ipv4 and ipv6 */
880         nb_mbuf += nb_rxd + nb_txd;
881
882         nb_mbuf = RTE_MAX(nb_mbuf, (uint32_t)NB_MBUF);
883
884         snprintf(buf, sizeof(buf), "mbuf_pool_%u_%u", lcore, queue);
885
886         rxq->pool = rte_pktmbuf_pool_create(buf, nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
887                                             MBUF_DATA_SIZE, socket);
888         if (rxq->pool == NULL) {
889                 RTE_LOG(ERR, IP_RSMBL,
890                         "rte_pktmbuf_pool_create(%s) failed", buf);
891                 return -1;
892         }
893
894         return 0;
895 }
896
897 static int
898 init_mem(void)
899 {
900         char buf[PATH_MAX];
901         struct rte_lpm *lpm;
902         struct rte_lpm6 *lpm6;
903         struct rte_lpm_config lpm_config;
904         int socket;
905         unsigned lcore_id;
906
907         /* traverse through lcores and initialize structures on each socket */
908
909         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
910
911                 if (rte_lcore_is_enabled(lcore_id) == 0)
912                         continue;
913
914                 socket = rte_lcore_to_socket_id(lcore_id);
915
916                 if (socket == SOCKET_ID_ANY)
917                         socket = 0;
918
919                 if (socket_lpm[socket] == NULL) {
920                         RTE_LOG(INFO, IP_RSMBL, "Creating LPM table on socket %i\n", socket);
921                         snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
922
923                         lpm_config.max_rules = LPM_MAX_RULES;
924                         lpm_config.number_tbl8s = 256;
925                         lpm_config.flags = 0;
926
927                         lpm = rte_lpm_create(buf, socket, &lpm_config);
928                         if (lpm == NULL) {
929                                 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
930                                 return -1;
931                         }
932                         socket_lpm[socket] = lpm;
933                 }
934
935                 if (socket_lpm6[socket] == NULL) {
936                         RTE_LOG(INFO, IP_RSMBL, "Creating LPM6 table on socket %i\n", socket);
937                         snprintf(buf, sizeof(buf), "IP_RSMBL_LPM_%i", socket);
938
939                         lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
940                         if (lpm6 == NULL) {
941                                 RTE_LOG(ERR, IP_RSMBL, "Cannot create LPM table\n");
942                                 return -1;
943                         }
944                         socket_lpm6[socket] = lpm6;
945                 }
946         }
947
948         return 0;
949 }
950
951 static void
952 queue_dump_stat(void)
953 {
954         uint32_t i, lcore;
955         const struct lcore_queue_conf *qconf;
956
957         for (lcore = 0; lcore < RTE_MAX_LCORE; lcore++) {
958                 if (rte_lcore_is_enabled(lcore) == 0)
959                         continue;
960
961                 qconf = &lcore_queue_conf[lcore];
962                 for (i = 0; i < qconf->n_rx_queue; i++) {
963
964                         fprintf(stdout, " -- lcoreid=%u portid=%u "
965                                 "frag tbl stat:\n",
966                                 lcore,  qconf->rx_queue_list[i].portid);
967                         rte_ip_frag_table_statistics_dump(stdout,
968                                         qconf->rx_queue_list[i].frag_tbl);
969                         fprintf(stdout, "TX bursts:\t%" PRIu64 "\n"
970                                 "TX packets _queued:\t%" PRIu64 "\n"
971                                 "TX packets dropped:\t%" PRIu64 "\n"
972                                 "TX packets send:\t%" PRIu64 "\n",
973                                 qconf->tx_stat.call,
974                                 qconf->tx_stat.queue,
975                                 qconf->tx_stat.drop,
976                                 qconf->tx_stat.send);
977                 }
978         }
979 }
980
981 static void
982 signal_handler(int signum)
983 {
984         queue_dump_stat();
985         if (signum != SIGUSR1)
986                 rte_exit(0, "received signal: %d, exiting\n", signum);
987 }
988
989 int
990 main(int argc, char **argv)
991 {
992         struct lcore_queue_conf *qconf;
993         struct rte_eth_dev_info dev_info;
994         struct rte_eth_txconf *txconf;
995         struct rx_queue *rxq;
996         int ret, socket;
997         unsigned nb_ports;
998         uint16_t queueid;
999         unsigned lcore_id = 0, rx_lcore_id = 0;
1000         uint32_t n_tx_queue, nb_lcores;
1001         uint16_t portid;
1002
1003         /* init EAL */
1004         ret = rte_eal_init(argc, argv);
1005         if (ret < 0)
1006                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1007         argc -= ret;
1008         argv += ret;
1009
1010         /* parse application arguments (after the EAL ones) */
1011         ret = parse_args(argc, argv);
1012         if (ret < 0)
1013                 rte_exit(EXIT_FAILURE, "Invalid IP reassembly parameters\n");
1014
1015         nb_ports = rte_eth_dev_count_avail();
1016         if (nb_ports == 0)
1017                 rte_exit(EXIT_FAILURE, "No ports found!\n");
1018
1019         nb_lcores = rte_lcore_count();
1020
1021         /* initialize structures (mempools, lpm etc.) */
1022         if (init_mem() < 0)
1023                 rte_panic("Cannot initialize memory structures!\n");
1024
1025         /* check if portmask has non-existent ports */
1026         if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
1027                 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
1028
1029         /* initialize all ports */
1030         RTE_ETH_FOREACH_DEV(portid) {
1031                 struct rte_eth_rxconf rxq_conf;
1032                 struct rte_eth_conf local_port_conf = port_conf;
1033
1034                 /* skip ports that are not enabled */
1035                 if ((enabled_port_mask & (1 << portid)) == 0) {
1036                         printf("\nSkipping disabled port %d\n", portid);
1037                         continue;
1038                 }
1039
1040                 qconf = &lcore_queue_conf[rx_lcore_id];
1041
1042                 /* limit the frame size to the maximum supported by NIC */
1043                 ret = rte_eth_dev_info_get(portid, &dev_info);
1044                 if (ret != 0)
1045                         rte_exit(EXIT_FAILURE,
1046                                 "Error during getting device (port %u) info: %s\n",
1047                                 portid, strerror(-ret));
1048
1049                 local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
1050                     dev_info.max_rx_pktlen,
1051                     local_port_conf.rxmode.max_rx_pkt_len);
1052
1053                 /* get the lcore_id for this port */
1054                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1055                            qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
1056
1057                         rx_lcore_id++;
1058                         if (rx_lcore_id >= RTE_MAX_LCORE)
1059                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
1060
1061                         qconf = &lcore_queue_conf[rx_lcore_id];
1062                 }
1063
1064                 socket = rte_lcore_to_socket_id(portid);
1065                 if (socket == SOCKET_ID_ANY)
1066                         socket = 0;
1067
1068                 queueid = qconf->n_rx_queue;
1069                 rxq = &qconf->rx_queue_list[queueid];
1070                 rxq->portid = portid;
1071                 rxq->lpm = socket_lpm[socket];
1072                 rxq->lpm6 = socket_lpm6[socket];
1073
1074                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1075                                                        &nb_txd);
1076                 if (ret < 0)
1077                         rte_exit(EXIT_FAILURE,
1078                                  "Cannot adjust number of descriptors: err=%d, port=%d\n",
1079                                  ret, portid);
1080
1081                 if (setup_queue_tbl(rxq, rx_lcore_id, queueid) < 0)
1082                         rte_exit(EXIT_FAILURE, "Failed to set up queue table\n");
1083                 qconf->n_rx_queue++;
1084
1085                 /* init port */
1086                 printf("Initializing port %d ... ", portid );
1087                 fflush(stdout);
1088
1089                 n_tx_queue = nb_lcores;
1090                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1091                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1092                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1093                         local_port_conf.txmode.offloads |=
1094                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1095
1096                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1097                         dev_info.flow_type_rss_offloads;
1098                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1099                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1100                         printf("Port %u modified RSS hash function based on hardware support,"
1101                                 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1102                                 portid,
1103                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
1104                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1105                 }
1106
1107                 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
1108                                             &local_port_conf);
1109                 if (ret < 0) {
1110                         printf("\n");
1111                         rte_exit(EXIT_FAILURE, "Cannot configure device: "
1112                                 "err=%d, port=%d\n",
1113                                 ret, portid);
1114                 }
1115
1116                 /* init one RX queue */
1117                 rxq_conf = dev_info.default_rxconf;
1118                 rxq_conf.offloads = local_port_conf.rxmode.offloads;
1119                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1120                                              socket, &rxq_conf,
1121                                              rxq->pool);
1122                 if (ret < 0) {
1123                         printf("\n");
1124                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
1125                                 "err=%d, port=%d\n",
1126                                 ret, portid);
1127                 }
1128
1129                 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1130                 if (ret < 0) {
1131                         printf("\n");
1132                         rte_exit(EXIT_FAILURE,
1133                                 "rte_eth_macaddr_get: err=%d, port=%d\n",
1134                                 ret, portid);
1135                 }
1136
1137                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1138                 printf("\n");
1139
1140                 /* init one TX queue per couple (lcore,port) */
1141                 queueid = 0;
1142                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1143                         if (rte_lcore_is_enabled(lcore_id) == 0)
1144                                 continue;
1145
1146                         socket = (int) rte_lcore_to_socket_id(lcore_id);
1147
1148                         printf("txq=%u,%d,%d ", lcore_id, queueid, socket);
1149                         fflush(stdout);
1150
1151                         txconf = &dev_info.default_txconf;
1152                         txconf->offloads = local_port_conf.txmode.offloads;
1153
1154                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1155                                         socket, txconf);
1156                         if (ret < 0)
1157                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1158                                         "port=%d\n", ret, portid);
1159
1160                         qconf = &lcore_queue_conf[lcore_id];
1161                         qconf->tx_queue_id[portid] = queueid;
1162                         setup_port_tbl(qconf, lcore_id, socket, portid);
1163                         queueid++;
1164                 }
1165                 printf("\n");
1166         }
1167
1168         printf("\n");
1169
1170         /* start ports */
1171         RTE_ETH_FOREACH_DEV(portid) {
1172                 if ((enabled_port_mask & (1 << portid)) == 0) {
1173                         continue;
1174                 }
1175                 /* Start device */
1176                 ret = rte_eth_dev_start(portid);
1177                 if (ret < 0)
1178                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1179                                 ret, portid);
1180
1181                 ret = rte_eth_promiscuous_enable(portid);
1182                 if (ret != 0)
1183                         rte_exit(EXIT_FAILURE,
1184                                 "rte_eth_promiscuous_enable: err=%s, port=%d\n",
1185                                 rte_strerror(-ret), portid);
1186         }
1187
1188         if (init_routing_table() < 0)
1189                 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1190
1191         check_all_ports_link_status(enabled_port_mask);
1192
1193         signal(SIGUSR1, signal_handler);
1194         signal(SIGTERM, signal_handler);
1195         signal(SIGINT, signal_handler);
1196
1197         /* launch per-lcore init on every lcore */
1198         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
1199         RTE_LCORE_FOREACH_WORKER(lcore_id) {
1200                 if (rte_eal_wait_lcore(lcore_id) < 0)
1201                         return -1;
1202         }
1203
1204         return 0;
1205 }