e551e6d118e763bd9d7b64cb537fe6cfffef3bcd
[deb_dpdk.git] / examples / exception_path / 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 <string.h>
39 #include <sys/queue.h>
40 #include <stdarg.h>
41 #include <errno.h>
42 #include <getopt.h>
43
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #ifdef RTE_EXEC_ENV_LINUXAPP
47 #include <linux/if_tun.h>
48 #endif
49 #include <fcntl.h>
50 #include <sys/ioctl.h>
51 #include <unistd.h>
52 #include <signal.h>
53
54 #include <rte_common.h>
55 #include <rte_log.h>
56 #include <rte_memory.h>
57 #include <rte_memcpy.h>
58 #include <rte_memzone.h>
59 #include <rte_eal.h>
60 #include <rte_per_lcore.h>
61 #include <rte_launch.h>
62 #include <rte_atomic.h>
63 #include <rte_lcore.h>
64 #include <rte_branch_prediction.h>
65 #include <rte_interrupts.h>
66 #include <rte_pci.h>
67 #include <rte_debug.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
70 #include <rte_mempool.h>
71 #include <rte_mbuf.h>
72 #include <rte_string_fns.h>
73 #include <rte_cycles.h>
74
75 /* Macros for printing using RTE_LOG */
76 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
77 #define FATAL_ERROR(fmt, args...)       rte_exit(EXIT_FAILURE, fmt "\n", ##args)
78 #define PRINT_INFO(fmt, args...)        RTE_LOG(INFO, APP, fmt "\n", ##args)
79
80 /* Max ports than can be used (each port is associated with two lcores) */
81 #define MAX_PORTS               (RTE_MAX_LCORE / 2)
82
83 /* Max size of a single packet */
84 #define MAX_PACKET_SZ (2048)
85
86 /* Size of the data buffer in each mbuf */
87 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
88
89 /* Number of mbufs in mempool that is created */
90 #define NB_MBUF                 8192
91
92 /* How many packets to attempt to read from NIC in one go */
93 #define PKT_BURST_SZ            32
94
95 /* How many objects (mbufs) to keep in per-lcore mempool cache */
96 #define MEMPOOL_CACHE_SZ        PKT_BURST_SZ
97
98 /* Number of RX ring descriptors */
99 #define NB_RXD                  128
100
101 /* Number of TX ring descriptors */
102 #define NB_TXD                  512
103
104 /*
105  * RX and TX Prefetch, Host, and Write-back threshold values should be
106  * carefully set for optimal performance. Consult the network
107  * controller's datasheet and supporting DPDK documentation for guidance
108  * on how these parameters should be set.
109  */
110
111 /* Options for configuring ethernet port */
112 static const struct rte_eth_conf port_conf = {
113         .rxmode = {
114                 .header_split = 0,      /* Header Split disabled */
115                 .hw_ip_checksum = 0,    /* IP checksum offload disabled */
116                 .hw_vlan_filter = 0,    /* VLAN filtering disabled */
117                 .jumbo_frame = 0,       /* Jumbo Frame Support disabled */
118                 .hw_strip_crc = 1,      /* CRC stripped by hardware */
119         },
120         .txmode = {
121                 .mq_mode = ETH_MQ_TX_NONE,
122         },
123 };
124
125 /* Mempool for mbufs */
126 static struct rte_mempool * pktmbuf_pool = NULL;
127
128 /* Mask of enabled ports */
129 static uint32_t ports_mask = 0;
130
131 /* Mask of cores that read from NIC and write to tap */
132 static uint64_t input_cores_mask = 0;
133
134 /* Mask of cores that read from tap and write to NIC */
135 static uint64_t output_cores_mask = 0;
136
137 /* Array storing port_id that is associated with each lcore */
138 static uint8_t port_ids[RTE_MAX_LCORE];
139
140 /* Structure type for recording lcore-specific stats */
141 struct stats {
142         uint64_t rx;
143         uint64_t tx;
144         uint64_t dropped;
145 };
146
147 /* Array of lcore-specific stats */
148 static struct stats lcore_stats[RTE_MAX_LCORE];
149
150 /* Print out statistics on packets handled */
151 static void
152 print_stats(void)
153 {
154         unsigned i;
155
156         printf("\n**Exception-Path example application statistics**\n"
157                "=======  ======  ============  ============  ===============\n"
158                " Lcore    Port            RX            TX    Dropped on TX\n"
159                "-------  ------  ------------  ------------  ---------------\n");
160         RTE_LCORE_FOREACH(i) {
161                 printf("%6u %7u %13"PRIu64" %13"PRIu64" %16"PRIu64"\n",
162                        i, (unsigned)port_ids[i],
163                        lcore_stats[i].rx, lcore_stats[i].tx,
164                        lcore_stats[i].dropped);
165         }
166         printf("=======  ======  ============  ============  ===============\n");
167 }
168
169 /* Custom handling of signals to handle stats */
170 static void
171 signal_handler(int signum)
172 {
173         /* When we receive a USR1 signal, print stats */
174         if (signum == SIGUSR1) {
175                 print_stats();
176         }
177
178         /* When we receive a USR2 signal, reset stats */
179         if (signum == SIGUSR2) {
180                 memset(&lcore_stats, 0, sizeof(lcore_stats));
181                 printf("\n**Statistics have been reset**\n");
182                 return;
183         }
184 }
185
186 #ifdef RTE_EXEC_ENV_LINUXAPP
187 /*
188  * Create a tap network interface, or use existing one with same name.
189  * If name[0]='\0' then a name is automatically assigned and returned in name.
190  */
191 static int tap_create(char *name)
192 {
193         struct ifreq ifr;
194         int fd, ret;
195
196         fd = open("/dev/net/tun", O_RDWR);
197         if (fd < 0)
198                 return fd;
199
200         memset(&ifr, 0, sizeof(ifr));
201
202         /* TAP device without packet information */
203         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
204
205         if (name && *name)
206                 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
207
208         ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
209         if (ret < 0) {
210                 close(fd);
211                 return ret;
212         }
213
214         if (name)
215                 snprintf(name, IFNAMSIZ, "%s", ifr.ifr_name);
216
217         return fd;
218 }
219 #else
220 /*
221  * Find a free tap network interface, or create a new one.
222  * The name is automatically assigned and returned in name.
223  */
224 static int tap_create(char *name)
225 {
226         int i, fd = -1;
227         char devname[PATH_MAX];
228
229         for (i = 0; i < 255; i++) {
230                 snprintf(devname, sizeof(devname), "/dev/tap%d", i);
231                 fd = open(devname, O_RDWR);
232                 if (fd >= 0 || errno != EBUSY)
233                         break;
234         }
235
236         if (name)
237                 snprintf(name, IFNAMSIZ, "tap%d", i);
238
239         return fd;
240 }
241 #endif
242
243 /* Main processing loop */
244 static int
245 main_loop(__attribute__((unused)) void *arg)
246 {
247         const unsigned lcore_id = rte_lcore_id();
248         char tap_name[IFNAMSIZ];
249         int tap_fd;
250
251         if ((1ULL << lcore_id) & input_cores_mask) {
252                 /* Create new tap interface */
253                 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
254                 tap_fd = tap_create(tap_name);
255                 if (tap_fd < 0)
256                         FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
257                                         tap_name, tap_fd);
258
259                 PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
260                            lcore_id, (unsigned)port_ids[lcore_id], tap_name);
261                 fflush(stdout);
262                 /* Loop forever reading from NIC and writing to tap */
263                 for (;;) {
264                         struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
265                         unsigned i;
266                         const unsigned nb_rx =
267                                         rte_eth_rx_burst(port_ids[lcore_id], 0,
268                                             pkts_burst, PKT_BURST_SZ);
269                         lcore_stats[lcore_id].rx += nb_rx;
270                         for (i = 0; likely(i < nb_rx); i++) {
271                                 struct rte_mbuf *m = pkts_burst[i];
272                                 /* Ignore return val from write() */
273                                 int ret = write(tap_fd,
274                                                 rte_pktmbuf_mtod(m, void*),
275                                                 rte_pktmbuf_data_len(m));
276                                 rte_pktmbuf_free(m);
277                                 if (unlikely(ret < 0))
278                                         lcore_stats[lcore_id].dropped++;
279                                 else
280                                         lcore_stats[lcore_id].tx++;
281                         }
282                 }
283         }
284         else if ((1ULL << lcore_id) & output_cores_mask) {
285                 /* Create new tap interface */
286                 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
287                 tap_fd = tap_create(tap_name);
288                 if (tap_fd < 0)
289                         FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
290                                         tap_name, tap_fd);
291
292                 PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
293                            lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
294                 fflush(stdout);
295                 /* Loop forever reading from tap and writing to NIC */
296                 for (;;) {
297                         int ret;
298                         struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
299                         if (m == NULL)
300                                 continue;
301
302                         ret = read(tap_fd, rte_pktmbuf_mtod(m, void *),
303                                 MAX_PACKET_SZ);
304                         lcore_stats[lcore_id].rx++;
305                         if (unlikely(ret < 0)) {
306                                 FATAL_ERROR("Reading from %s interface failed",
307                                             tap_name);
308                         }
309                         m->nb_segs = 1;
310                         m->next = NULL;
311                         m->pkt_len = (uint16_t)ret;
312                         m->data_len = (uint16_t)ret;
313                         ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
314                         if (unlikely(ret < 1)) {
315                                 rte_pktmbuf_free(m);
316                                 lcore_stats[lcore_id].dropped++;
317                         }
318                         else {
319                                 lcore_stats[lcore_id].tx++;
320                         }
321                 }
322         }
323         else {
324                 PRINT_INFO("Lcore %u has nothing to do", lcore_id);
325                 return 0;
326         }
327         /*
328          * Tap file is closed automatically when program exits. Putting close()
329          * here will cause the compiler to give an error about unreachable code.
330          */
331 }
332
333 /* Display usage instructions */
334 static void
335 print_usage(const char *prgname)
336 {
337         PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
338                    "    -p PORTMASK: hex bitmask of ports to use\n"
339                    "    -i IN_CORES: hex bitmask of cores which read from NIC\n"
340                    "    -o OUT_CORES: hex bitmask of cores which write to NIC",
341                    prgname);
342 }
343
344 /* Convert string to unsigned number. 0 is returned if error occurs */
345 static uint64_t
346 parse_unsigned(const char *portmask)
347 {
348         char *end = NULL;
349         uint64_t num;
350
351         num = strtoull(portmask, &end, 16);
352         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
353                 return 0;
354
355         return (uint64_t)num;
356 }
357
358 /* Record affinities between ports and lcores in global port_ids[] array */
359 static void
360 setup_port_lcore_affinities(void)
361 {
362         unsigned long i;
363         uint8_t tx_port = 0;
364         uint8_t rx_port = 0;
365
366         /* Setup port_ids[] array, and check masks were ok */
367         RTE_LCORE_FOREACH(i) {
368                 if (input_cores_mask & (1ULL << i)) {
369                         /* Skip ports that are not enabled */
370                         while ((ports_mask & (1 << rx_port)) == 0) {
371                                 rx_port++;
372                                 if (rx_port > (sizeof(ports_mask) * 8))
373                                         goto fail; /* not enough ports */
374                         }
375
376                         port_ids[i] = rx_port++;
377                 } else if (output_cores_mask & (1ULL << (i & 0x3f))) {
378                         /* Skip ports that are not enabled */
379                         while ((ports_mask & (1 << tx_port)) == 0) {
380                                 tx_port++;
381                                 if (tx_port > (sizeof(ports_mask) * 8))
382                                         goto fail; /* not enough ports */
383                         }
384
385                         port_ids[i] = tx_port++;
386                 }
387         }
388
389         if (rx_port != tx_port)
390                 goto fail; /* uneven number of cores in masks */
391
392         if (ports_mask & (~((1 << rx_port) - 1)))
393                 goto fail; /* unused ports */
394
395         return;
396 fail:
397         FATAL_ERROR("Invalid core/port masks specified on command line");
398 }
399
400 /* Parse the arguments given in the command line of the application */
401 static void
402 parse_args(int argc, char **argv)
403 {
404         int opt;
405         const char *prgname = argv[0];
406
407         /* Disable printing messages within getopt() */
408         opterr = 0;
409
410         /* Parse command line */
411         while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
412                 switch (opt) {
413                 case 'i':
414                         input_cores_mask = parse_unsigned(optarg);
415                         break;
416                 case 'o':
417                         output_cores_mask = parse_unsigned(optarg);
418                         break;
419                 case 'p':
420                         ports_mask = parse_unsigned(optarg);
421                         break;
422                 default:
423                         print_usage(prgname);
424                         FATAL_ERROR("Invalid option specified");
425                 }
426         }
427
428         /* Check that options were parsed ok */
429         if (input_cores_mask == 0) {
430                 print_usage(prgname);
431                 FATAL_ERROR("IN_CORES not specified correctly");
432         }
433         if (output_cores_mask == 0) {
434                 print_usage(prgname);
435                 FATAL_ERROR("OUT_CORES not specified correctly");
436         }
437         if (ports_mask == 0) {
438                 print_usage(prgname);
439                 FATAL_ERROR("PORTMASK not specified correctly");
440         }
441
442         setup_port_lcore_affinities();
443 }
444
445 /* Initialise a single port on an Ethernet device */
446 static void
447 init_port(uint8_t port)
448 {
449         int ret;
450         uint16_t nb_rxd = NB_RXD;
451         uint16_t nb_txd = NB_TXD;
452
453         /* Initialise device and RX/TX queues */
454         PRINT_INFO("Initialising port %u ...", (unsigned)port);
455         fflush(stdout);
456         ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
457         if (ret < 0)
458                 FATAL_ERROR("Could not configure port%u (%d)",
459                             (unsigned)port, ret);
460
461         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
462         if (ret < 0)
463                 FATAL_ERROR("Could not adjust number of descriptors for port%u (%d)",
464                             (unsigned)port, ret);
465
466         ret = rte_eth_rx_queue_setup(port, 0, nb_rxd,
467                                 rte_eth_dev_socket_id(port),
468                                 NULL,
469                                 pktmbuf_pool);
470         if (ret < 0)
471                 FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
472                             (unsigned)port, ret);
473
474         ret = rte_eth_tx_queue_setup(port, 0, nb_txd,
475                                 rte_eth_dev_socket_id(port),
476                                 NULL);
477         if (ret < 0)
478                 FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
479                             (unsigned)port, ret);
480
481         ret = rte_eth_dev_start(port);
482         if (ret < 0)
483                 FATAL_ERROR("Could not start port%u (%d)", (unsigned)port, ret);
484
485         rte_eth_promiscuous_enable(port);
486 }
487
488 /* Check the link status of all ports in up to 9s, and print them finally */
489 static void
490 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
491 {
492 #define CHECK_INTERVAL 100 /* 100ms */
493 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
494         uint8_t portid, count, all_ports_up, print_flag = 0;
495         struct rte_eth_link link;
496
497         printf("\nChecking link status");
498         fflush(stdout);
499         for (count = 0; count <= MAX_CHECK_TIME; count++) {
500                 all_ports_up = 1;
501                 for (portid = 0; portid < port_num; portid++) {
502                         if ((port_mask & (1 << portid)) == 0)
503                                 continue;
504                         memset(&link, 0, sizeof(link));
505                         rte_eth_link_get_nowait(portid, &link);
506                         /* print link status if flag set */
507                         if (print_flag == 1) {
508                                 if (link.link_status)
509                                         printf("Port %d Link Up - speed %u "
510                                                 "Mbps - %s\n", (uint8_t)portid,
511                                                 (unsigned)link.link_speed,
512                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
513                                         ("full-duplex") : ("half-duplex\n"));
514                                 else
515                                         printf("Port %d Link Down\n",
516                                                 (uint8_t)portid);
517                                 continue;
518                         }
519                         /* clear all_ports_up flag if any link down */
520                         if (link.link_status == ETH_LINK_DOWN) {
521                                 all_ports_up = 0;
522                                 break;
523                         }
524                 }
525                 /* after finally printing all link status, get out */
526                 if (print_flag == 1)
527                         break;
528
529                 if (all_ports_up == 0) {
530                         printf(".");
531                         fflush(stdout);
532                         rte_delay_ms(CHECK_INTERVAL);
533                 }
534
535                 /* set the print_flag if all ports up or timeout */
536                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
537                         print_flag = 1;
538                         printf("done\n");
539                 }
540         }
541 }
542
543 /* Initialise ports/queues etc. and start main loop on each core */
544 int
545 main(int argc, char** argv)
546 {
547         int ret;
548         unsigned i,high_port;
549         uint8_t nb_sys_ports, port;
550
551         /* Associate signal_hanlder function with USR signals */
552         signal(SIGUSR1, signal_handler);
553         signal(SIGUSR2, signal_handler);
554
555         /* Initialise EAL */
556         ret = rte_eal_init(argc, argv);
557         if (ret < 0)
558                 FATAL_ERROR("Could not initialise EAL (%d)", ret);
559         argc -= ret;
560         argv += ret;
561
562         /* Parse application arguments (after the EAL ones) */
563         parse_args(argc, argv);
564
565         /* Create the mbuf pool */
566         pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
567                         MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, rte_socket_id());
568         if (pktmbuf_pool == NULL) {
569                 FATAL_ERROR("Could not initialise mbuf pool");
570                 return -1;
571         }
572
573         /* Get number of ports found in scan */
574         nb_sys_ports = rte_eth_dev_count();
575         if (nb_sys_ports == 0)
576                 FATAL_ERROR("No supported Ethernet device found");
577         /* Find highest port set in portmask */
578         for (high_port = (sizeof(ports_mask) * 8) - 1;
579                         (high_port != 0) && !(ports_mask & (1 << high_port));
580                         high_port--)
581                 ; /* empty body */
582         if (high_port > nb_sys_ports)
583                 FATAL_ERROR("Port mask requires more ports than available");
584
585         /* Initialise each port */
586         for (port = 0; port < nb_sys_ports; port++) {
587                 /* Skip ports that are not enabled */
588                 if ((ports_mask & (1 << port)) == 0) {
589                         continue;
590                 }
591                 init_port(port);
592         }
593         check_all_ports_link_status(nb_sys_ports, ports_mask);
594
595         /* Launch per-lcore function on every lcore */
596         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
597         RTE_LCORE_FOREACH_SLAVE(i) {
598                 if (rte_eal_wait_lcore(i) < 0)
599                         return -1;
600         }
601
602         return 0;
603 }