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