4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <sys/queue.h>
44 #include <netinet/in.h>
46 #include <linux/if_tun.h>
48 #include <sys/ioctl.h>
52 #include <rte_common.h>
54 #include <rte_memory.h>
55 #include <rte_memcpy.h>
56 #include <rte_memzone.h>
57 #include <rte_tailq.h>
59 #include <rte_per_lcore.h>
60 #include <rte_launch.h>
61 #include <rte_atomic.h>
62 #include <rte_lcore.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_interrupts.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
71 #include <rte_mempool.h>
73 #include <rte_string_fns.h>
74 #include <rte_cycles.h>
76 /* Macros for printing using RTE_LOG */
77 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
78 #define FATAL_ERROR(fmt, args...) rte_exit(EXIT_FAILURE, fmt "\n", ##args)
79 #define PRINT_INFO(fmt, args...) RTE_LOG(INFO, APP, fmt "\n", ##args)
81 /* Max ports than can be used (each port is associated with two lcores) */
82 #define MAX_PORTS (RTE_MAX_LCORE / 2)
84 /* Max size of a single packet */
85 #define MAX_PACKET_SZ 2048
87 /* Number of bytes needed for each mbuf */
89 (MAX_PACKET_SZ + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
91 /* Number of mbufs in mempool that is created */
94 /* How many packets to attempt to read from NIC in one go */
95 #define PKT_BURST_SZ 32
97 /* How many objects (mbufs) to keep in per-lcore mempool cache */
98 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
100 /* Number of RX ring descriptors */
103 /* Number of TX ring descriptors */
107 * RX and TX Prefetch, Host, and Write-back threshold values should be
108 * carefully set for optimal performance. Consult the network
109 * controller's datasheet and supporting DPDK documentation for guidance
110 * on how these parameters should be set.
112 /* RX ring configuration */
113 static const struct rte_eth_rxconf rx_conf = {
115 .pthresh = 8, /* Ring prefetch threshold */
116 .hthresh = 8, /* Ring host threshold */
117 .wthresh = 4, /* Ring writeback threshold */
119 .rx_free_thresh = 0, /* Immediately free RX descriptors */
123 * These default values are optimized for use with the Intel(R) 82599 10 GbE
124 * Controller and the DPDK ixgbe PMD. Consider using other values for other
125 * network controllers and/or network drivers.
127 /* TX ring configuration */
128 static const struct rte_eth_txconf tx_conf = {
130 .pthresh = 36, /* Ring prefetch threshold */
131 .hthresh = 0, /* Ring host threshold */
132 .wthresh = 0, /* Ring writeback threshold */
134 .tx_free_thresh = 0, /* Use PMD default values */
135 .tx_rs_thresh = 0, /* Use PMD default values */
138 /* Options for configuring ethernet port */
139 static const struct rte_eth_conf port_conf = {
141 .header_split = 0, /* Header Split disabled */
142 .hw_ip_checksum = 0, /* IP checksum offload disabled */
143 .hw_vlan_filter = 0, /* VLAN filtering disabled */
144 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
145 .hw_strip_crc = 0, /* CRC stripped by hardware */
148 .mq_mode = ETH_MQ_TX_NONE,
152 /* Mempool for mbufs */
153 static struct rte_mempool * pktmbuf_pool = NULL;
155 /* Mask of enabled ports */
156 static uint32_t ports_mask = 0;
158 /* Mask of cores that read from NIC and write to tap */
159 static uint32_t input_cores_mask = 0;
161 /* Mask of cores that read from tap and write to NIC */
162 static uint32_t output_cores_mask = 0;
164 /* Array storing port_id that is associated with each lcore */
165 static uint8_t port_ids[RTE_MAX_LCORE];
167 /* Structure type for recording lcore-specific stats */
174 /* Array of lcore-specific stats */
175 static struct stats lcore_stats[RTE_MAX_LCORE];
177 /* Print out statistics on packets handled */
183 printf("\n**Exception-Path example application statistics**\n"
184 "======= ====== ============ ============ ===============\n"
185 " Lcore Port RX TX Dropped on TX\n"
186 "------- ------ ------------ ------------ ---------------\n");
187 RTE_LCORE_FOREACH(i) {
188 printf("%6u %7u %13"PRIu64" %13"PRIu64" %16"PRIu64"\n",
189 i, (unsigned)port_ids[i],
190 lcore_stats[i].rx, lcore_stats[i].tx,
191 lcore_stats[i].dropped);
193 printf("======= ====== ============ ============ ===============\n");
196 /* Custom handling of signals to handle stats */
198 signal_handler(int signum)
200 /* When we receive a USR1 signal, print stats */
201 if (signum == SIGUSR1) {
205 /* When we receive a USR2 signal, reset stats */
206 if (signum == SIGUSR2) {
207 memset(&lcore_stats, 0, sizeof(lcore_stats));
208 printf("\n**Statistics have been reset**\n");
214 * Create a tap network interface, or use existing one with same name.
215 * If name[0]='\0' then a name is automatically assigned and returned in name.
217 static int tap_create(char *name)
222 fd = open("/dev/net/tun", O_RDWR);
226 memset(&ifr, 0, sizeof(ifr));
228 /* TAP device without packet information */
229 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
232 rte_snprintf(ifr.ifr_name, IFNAMSIZ, name);
234 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
241 rte_snprintf(name, IFNAMSIZ, ifr.ifr_name);
246 /* Main processing loop */
248 main_loop(__attribute__((unused)) void *arg)
250 const unsigned lcore_id = rte_lcore_id();
251 char tap_name[IFNAMSIZ];
254 if ((1 << lcore_id) & input_cores_mask) {
255 /* Create new tap interface */
256 rte_snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
257 tap_fd = tap_create(tap_name);
259 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
262 PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
263 lcore_id, (unsigned)port_ids[lcore_id], tap_name);
265 /* Loop forever reading from NIC and writing to tap */
267 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
269 const unsigned nb_rx =
270 rte_eth_rx_burst(port_ids[lcore_id], 0,
271 pkts_burst, PKT_BURST_SZ);
272 lcore_stats[lcore_id].rx += nb_rx;
273 for (i = 0; likely(i < nb_rx); i++) {
274 struct rte_mbuf *m = pkts_burst[i];
275 /* Ignore return val from write() */
276 int ret = write(tap_fd,
277 rte_pktmbuf_mtod(m, void*),
278 rte_pktmbuf_data_len(m));
280 if (unlikely(ret < 0))
281 lcore_stats[lcore_id].dropped++;
283 lcore_stats[lcore_id].tx++;
287 else if ((1 << lcore_id) & output_cores_mask) {
288 /* Create new tap interface */
289 rte_snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
290 tap_fd = tap_create(tap_name);
292 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
295 PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
296 lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
298 /* Loop forever reading from tap and writing to NIC */
301 struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
305 ret = read(tap_fd, m->pkt.data, MAX_PACKET_SZ);
306 lcore_stats[lcore_id].rx++;
307 if (unlikely(ret < 0)) {
308 FATAL_ERROR("Reading from %s interface failed",
313 m->pkt.pkt_len = (uint16_t)ret;
314 m->pkt.data_len = (uint16_t)ret;
315 ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
316 if (unlikely(ret < 1)) {
318 lcore_stats[lcore_id].dropped++;
321 lcore_stats[lcore_id].tx++;
326 PRINT_INFO("Lcore %u has nothing to do", lcore_id);
330 * Tap file is closed automatically when program exits. Putting close()
331 * here will cause the compiler to give an error about unreachable code.
335 /* Display usage instructions */
337 print_usage(const char *prgname)
339 PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
340 " -p PORTMASK: hex bitmask of ports to use\n"
341 " -i IN_CORES: hex bitmask of cores which read from NIC\n"
342 " -o OUT_CORES: hex bitmask of cores which write to NIC",
346 /* Convert string to unsigned number. 0 is returned if error occurs */
348 parse_unsigned(const char *portmask)
353 num = strtoul(portmask, &end, 16);
354 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
357 return (uint32_t)num;
360 /* Record affinities between ports and lcores in global port_ids[] array */
362 setup_port_lcore_affinities(void)
368 /* Setup port_ids[] array, and check masks were ok */
369 RTE_LCORE_FOREACH(i) {
370 if (input_cores_mask & (1 << i)) {
371 /* Skip ports that are not enabled */
372 while ((ports_mask & (1 << rx_port)) == 0) {
374 if (rx_port > (sizeof(ports_mask) * 8))
375 goto fail; /* not enough ports */
378 port_ids[i] = rx_port++;
380 else if (output_cores_mask & (1 << i)) {
381 /* Skip ports that are not enabled */
382 while ((ports_mask & (1 << tx_port)) == 0) {
384 if (tx_port > (sizeof(ports_mask) * 8))
385 goto fail; /* not enough ports */
388 port_ids[i] = tx_port++;
392 if (rx_port != tx_port)
393 goto fail; /* uneven number of cores in masks */
395 if (ports_mask & (~((1 << rx_port) - 1)))
396 goto fail; /* unused ports */
400 FATAL_ERROR("Invalid core/port masks specified on command line");
403 /* Parse the arguments given in the command line of the application */
405 parse_args(int argc, char **argv)
408 const char *prgname = argv[0];
410 /* Disable printing messages within getopt() */
413 /* Parse command line */
414 while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
417 input_cores_mask = parse_unsigned(optarg);
420 output_cores_mask = parse_unsigned(optarg);
423 ports_mask = parse_unsigned(optarg);
426 print_usage(prgname);
427 FATAL_ERROR("Invalid option specified");
431 /* Check that options were parsed ok */
432 if (input_cores_mask == 0) {
433 print_usage(prgname);
434 FATAL_ERROR("IN_CORES not specified correctly");
436 if (output_cores_mask == 0) {
437 print_usage(prgname);
438 FATAL_ERROR("OUT_CORES not specified correctly");
440 if (ports_mask == 0) {
441 print_usage(prgname);
442 FATAL_ERROR("PORTMASK not specified correctly");
445 setup_port_lcore_affinities();
448 /* Initialise a single port on an Ethernet device */
450 init_port(uint8_t port)
454 /* Initialise device and RX/TX queues */
455 PRINT_INFO("Initialising port %u ...", (unsigned)port);
457 ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
459 FATAL_ERROR("Could not configure port%u (%d)",
460 (unsigned)port, ret);
462 ret = rte_eth_rx_queue_setup(port, 0, NB_RXD, rte_eth_dev_socket_id(port),
463 &rx_conf, pktmbuf_pool);
465 FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
466 (unsigned)port, ret);
468 ret = rte_eth_tx_queue_setup(port, 0, NB_TXD, rte_eth_dev_socket_id(port),
471 FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
472 (unsigned)port, ret);
474 ret = rte_eth_dev_start(port);
476 FATAL_ERROR("Could not start port%u (%d)", (unsigned)port, ret);
478 rte_eth_promiscuous_enable(port);
481 /* Check the link status of all ports in up to 9s, and print them finally */
483 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
485 #define CHECK_INTERVAL 100 /* 100ms */
486 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
487 uint8_t portid, count, all_ports_up, print_flag = 0;
488 struct rte_eth_link link;
490 printf("\nChecking link status");
492 for (count = 0; count <= MAX_CHECK_TIME; count++) {
494 for (portid = 0; portid < port_num; portid++) {
495 if ((port_mask & (1 << portid)) == 0)
497 memset(&link, 0, sizeof(link));
498 rte_eth_link_get_nowait(portid, &link);
499 /* print link status if flag set */
500 if (print_flag == 1) {
501 if (link.link_status)
502 printf("Port %d Link Up - speed %u "
503 "Mbps - %s\n", (uint8_t)portid,
504 (unsigned)link.link_speed,
505 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
506 ("full-duplex") : ("half-duplex\n"));
508 printf("Port %d Link Down\n",
512 /* clear all_ports_up flag if any link down */
513 if (link.link_status == 0) {
518 /* after finally printing all link status, get out */
522 if (all_ports_up == 0) {
525 rte_delay_ms(CHECK_INTERVAL);
528 /* set the print_flag if all ports up or timeout */
529 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
536 /* Initialise ports/queues etc. and start main loop on each core */
538 main(int argc, char** argv)
541 unsigned i,high_port;
542 uint8_t nb_sys_ports, port;
544 /* Associate signal_hanlder function with USR signals */
545 signal(SIGUSR1, signal_handler);
546 signal(SIGUSR2, signal_handler);
549 ret = rte_eal_init(argc, argv);
551 FATAL_ERROR("Could not initialise EAL (%d)", ret);
555 /* Parse application arguments (after the EAL ones) */
556 parse_args(argc, argv);
558 /* Create the mbuf pool */
559 pktmbuf_pool = rte_mempool_create("mbuf_pool", NB_MBUF, MBUF_SZ,
561 sizeof(struct rte_pktmbuf_pool_private),
562 rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL,
564 if (pktmbuf_pool == NULL) {
565 FATAL_ERROR("Could not initialise mbuf pool");
569 /* Initialise PMD driver(s) */
570 ret = rte_pmd_init_all();
572 FATAL_ERROR("Could not probe PMD (%d)", ret);
574 /* Scan PCI bus for recognised devices */
575 ret = rte_eal_pci_probe();
577 FATAL_ERROR("Could not probe PCI (%d)", ret);
579 /* Get number of ports found in scan */
580 nb_sys_ports = rte_eth_dev_count();
581 if (nb_sys_ports == 0)
582 FATAL_ERROR("No supported Ethernet devices found - check that "
583 "CONFIG_RTE_LIBRTE_IGB_PMD=y and/or "
584 "CONFIG_RTE_LIBRTE_IXGBE_PMD=y in the config file");
585 /* Find highest port set in portmask */
586 for (high_port = (sizeof(ports_mask) * 8) - 1;
587 (high_port != 0) && !(ports_mask & (1 << high_port));
590 if (high_port > nb_sys_ports)
591 FATAL_ERROR("Port mask requires more ports than available");
593 /* Initialise each port */
594 for (port = 0; port < nb_sys_ports; port++) {
595 /* Skip ports that are not enabled */
596 if ((ports_mask & (1 << port)) == 0) {
601 check_all_ports_link_status(nb_sys_ports, ports_mask);
603 /* Launch per-lcore function on every lcore */
604 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
605 RTE_LCORE_FOREACH_SLAVE(i) {
606 if (rte_eal_wait_lcore(i) < 0)