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