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