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