4 * Copyright(c) 2010-2014 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>
58 #include <rte_per_lcore.h>
59 #include <rte_launch.h>
60 #include <rte_atomic.h>
61 #include <rte_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
69 #include <rte_mempool.h>
71 #include <rte_string_fns.h>
72 #include <rte_cycles.h>
74 /* Macros for printing using RTE_LOG */
75 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
76 #define FATAL_ERROR(fmt, args...) rte_exit(EXIT_FAILURE, fmt "\n", ##args)
77 #define PRINT_INFO(fmt, args...) RTE_LOG(INFO, APP, fmt "\n", ##args)
79 /* Max ports than can be used (each port is associated with two lcores) */
80 #define MAX_PORTS (RTE_MAX_LCORE / 2)
82 /* Max size of a single packet */
83 #define MAX_PACKET_SZ (2048)
85 /* Size of the data buffer in each mbuf */
86 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
88 /* Number of mbufs in mempool that is created */
91 /* How many packets to attempt to read from NIC in one go */
92 #define PKT_BURST_SZ 32
94 /* How many objects (mbufs) to keep in per-lcore mempool cache */
95 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
97 /* Number of RX ring descriptors */
100 /* Number of TX ring descriptors */
104 * RX and TX Prefetch, Host, and Write-back threshold values should be
105 * carefully set for optimal performance. Consult the network
106 * controller's datasheet and supporting DPDK documentation for guidance
107 * on how these parameters should be set.
110 /* Options for configuring ethernet port */
111 static const struct rte_eth_conf port_conf = {
113 .header_split = 0, /* Header Split disabled */
114 .hw_ip_checksum = 0, /* IP checksum offload disabled */
115 .hw_vlan_filter = 0, /* VLAN filtering disabled */
116 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
117 .hw_strip_crc = 0, /* CRC stripped by hardware */
120 .mq_mode = ETH_MQ_TX_NONE,
124 /* Mempool for mbufs */
125 static struct rte_mempool * pktmbuf_pool = NULL;
127 /* Mask of enabled ports */
128 static uint32_t ports_mask = 0;
130 /* Mask of cores that read from NIC and write to tap */
131 static uint64_t input_cores_mask = 0;
133 /* Mask of cores that read from tap and write to NIC */
134 static uint64_t output_cores_mask = 0;
136 /* Array storing port_id that is associated with each lcore */
137 static uint8_t port_ids[RTE_MAX_LCORE];
139 /* Structure type for recording lcore-specific stats */
146 /* Array of lcore-specific stats */
147 static struct stats lcore_stats[RTE_MAX_LCORE];
149 /* Print out statistics on packets handled */
155 printf("\n**Exception-Path example application statistics**\n"
156 "======= ====== ============ ============ ===============\n"
157 " Lcore Port RX TX Dropped on TX\n"
158 "------- ------ ------------ ------------ ---------------\n");
159 RTE_LCORE_FOREACH(i) {
160 printf("%6u %7u %13"PRIu64" %13"PRIu64" %16"PRIu64"\n",
161 i, (unsigned)port_ids[i],
162 lcore_stats[i].rx, lcore_stats[i].tx,
163 lcore_stats[i].dropped);
165 printf("======= ====== ============ ============ ===============\n");
168 /* Custom handling of signals to handle stats */
170 signal_handler(int signum)
172 /* When we receive a USR1 signal, print stats */
173 if (signum == SIGUSR1) {
177 /* When we receive a USR2 signal, reset stats */
178 if (signum == SIGUSR2) {
179 memset(&lcore_stats, 0, sizeof(lcore_stats));
180 printf("\n**Statistics have been reset**\n");
186 * Create a tap network interface, or use existing one with same name.
187 * If name[0]='\0' then a name is automatically assigned and returned in name.
189 static int tap_create(char *name)
194 fd = open("/dev/net/tun", O_RDWR);
198 memset(&ifr, 0, sizeof(ifr));
200 /* TAP device without packet information */
201 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
204 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
206 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
213 snprintf(name, IFNAMSIZ, "%s", ifr.ifr_name);
218 /* Main processing loop */
220 main_loop(__attribute__((unused)) void *arg)
222 const unsigned lcore_id = rte_lcore_id();
223 char tap_name[IFNAMSIZ];
226 if ((1ULL << lcore_id) & input_cores_mask) {
227 /* Create new tap interface */
228 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
229 tap_fd = tap_create(tap_name);
231 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
234 PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
235 lcore_id, (unsigned)port_ids[lcore_id], tap_name);
237 /* Loop forever reading from NIC and writing to tap */
239 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
241 const unsigned nb_rx =
242 rte_eth_rx_burst(port_ids[lcore_id], 0,
243 pkts_burst, PKT_BURST_SZ);
244 lcore_stats[lcore_id].rx += nb_rx;
245 for (i = 0; likely(i < nb_rx); i++) {
246 struct rte_mbuf *m = pkts_burst[i];
247 /* Ignore return val from write() */
248 int ret = write(tap_fd,
249 rte_pktmbuf_mtod(m, void*),
250 rte_pktmbuf_data_len(m));
252 if (unlikely(ret < 0))
253 lcore_stats[lcore_id].dropped++;
255 lcore_stats[lcore_id].tx++;
259 else if ((1ULL << lcore_id) & output_cores_mask) {
260 /* Create new tap interface */
261 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
262 tap_fd = tap_create(tap_name);
264 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
267 PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
268 lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
270 /* Loop forever reading from tap and writing to NIC */
273 struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
277 ret = read(tap_fd, rte_pktmbuf_mtod(m, void *),
279 lcore_stats[lcore_id].rx++;
280 if (unlikely(ret < 0)) {
281 FATAL_ERROR("Reading from %s interface failed",
286 m->pkt_len = (uint16_t)ret;
287 m->data_len = (uint16_t)ret;
288 ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
289 if (unlikely(ret < 1)) {
291 lcore_stats[lcore_id].dropped++;
294 lcore_stats[lcore_id].tx++;
299 PRINT_INFO("Lcore %u has nothing to do", lcore_id);
303 * Tap file is closed automatically when program exits. Putting close()
304 * here will cause the compiler to give an error about unreachable code.
308 /* Display usage instructions */
310 print_usage(const char *prgname)
312 PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
313 " -p PORTMASK: hex bitmask of ports to use\n"
314 " -i IN_CORES: hex bitmask of cores which read from NIC\n"
315 " -o OUT_CORES: hex bitmask of cores which write to NIC",
319 /* Convert string to unsigned number. 0 is returned if error occurs */
321 parse_unsigned(const char *portmask)
326 num = strtoull(portmask, &end, 16);
327 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
330 return (uint64_t)num;
333 /* Record affinities between ports and lcores in global port_ids[] array */
335 setup_port_lcore_affinities(void)
341 /* Setup port_ids[] array, and check masks were ok */
342 RTE_LCORE_FOREACH(i) {
343 if (input_cores_mask & (1ULL << i)) {
344 /* Skip ports that are not enabled */
345 while ((ports_mask & (1 << rx_port)) == 0) {
347 if (rx_port > (sizeof(ports_mask) * 8))
348 goto fail; /* not enough ports */
351 port_ids[i] = rx_port++;
352 } else if (output_cores_mask & (1ULL << (i & 0x3f))) {
353 /* Skip ports that are not enabled */
354 while ((ports_mask & (1 << tx_port)) == 0) {
356 if (tx_port > (sizeof(ports_mask) * 8))
357 goto fail; /* not enough ports */
360 port_ids[i] = tx_port++;
364 if (rx_port != tx_port)
365 goto fail; /* uneven number of cores in masks */
367 if (ports_mask & (~((1 << rx_port) - 1)))
368 goto fail; /* unused ports */
372 FATAL_ERROR("Invalid core/port masks specified on command line");
375 /* Parse the arguments given in the command line of the application */
377 parse_args(int argc, char **argv)
380 const char *prgname = argv[0];
382 /* Disable printing messages within getopt() */
385 /* Parse command line */
386 while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
389 input_cores_mask = parse_unsigned(optarg);
392 output_cores_mask = parse_unsigned(optarg);
395 ports_mask = parse_unsigned(optarg);
398 print_usage(prgname);
399 FATAL_ERROR("Invalid option specified");
403 /* Check that options were parsed ok */
404 if (input_cores_mask == 0) {
405 print_usage(prgname);
406 FATAL_ERROR("IN_CORES not specified correctly");
408 if (output_cores_mask == 0) {
409 print_usage(prgname);
410 FATAL_ERROR("OUT_CORES not specified correctly");
412 if (ports_mask == 0) {
413 print_usage(prgname);
414 FATAL_ERROR("PORTMASK not specified correctly");
417 setup_port_lcore_affinities();
420 /* Initialise a single port on an Ethernet device */
422 init_port(uint8_t port)
426 /* Initialise device and RX/TX queues */
427 PRINT_INFO("Initialising port %u ...", (unsigned)port);
429 ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
431 FATAL_ERROR("Could not configure port%u (%d)",
432 (unsigned)port, ret);
434 ret = rte_eth_rx_queue_setup(port, 0, NB_RXD, rte_eth_dev_socket_id(port),
438 FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
439 (unsigned)port, ret);
441 ret = rte_eth_tx_queue_setup(port, 0, NB_TXD, rte_eth_dev_socket_id(port),
444 FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
445 (unsigned)port, ret);
447 ret = rte_eth_dev_start(port);
449 FATAL_ERROR("Could not start port%u (%d)", (unsigned)port, ret);
451 rte_eth_promiscuous_enable(port);
454 /* Check the link status of all ports in up to 9s, and print them finally */
456 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
458 #define CHECK_INTERVAL 100 /* 100ms */
459 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
460 uint8_t portid, count, all_ports_up, print_flag = 0;
461 struct rte_eth_link link;
463 printf("\nChecking link status");
465 for (count = 0; count <= MAX_CHECK_TIME; count++) {
467 for (portid = 0; portid < port_num; portid++) {
468 if ((port_mask & (1 << portid)) == 0)
470 memset(&link, 0, sizeof(link));
471 rte_eth_link_get_nowait(portid, &link);
472 /* print link status if flag set */
473 if (print_flag == 1) {
474 if (link.link_status)
475 printf("Port %d Link Up - speed %u "
476 "Mbps - %s\n", (uint8_t)portid,
477 (unsigned)link.link_speed,
478 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
479 ("full-duplex") : ("half-duplex\n"));
481 printf("Port %d Link Down\n",
485 /* clear all_ports_up flag if any link down */
486 if (link.link_status == ETH_LINK_DOWN) {
491 /* after finally printing all link status, get out */
495 if (all_ports_up == 0) {
498 rte_delay_ms(CHECK_INTERVAL);
501 /* set the print_flag if all ports up or timeout */
502 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
509 /* Initialise ports/queues etc. and start main loop on each core */
511 main(int argc, char** argv)
514 unsigned i,high_port;
515 uint8_t nb_sys_ports, port;
517 /* Associate signal_hanlder function with USR signals */
518 signal(SIGUSR1, signal_handler);
519 signal(SIGUSR2, signal_handler);
522 ret = rte_eal_init(argc, argv);
524 FATAL_ERROR("Could not initialise EAL (%d)", ret);
528 /* Parse application arguments (after the EAL ones) */
529 parse_args(argc, argv);
531 /* Create the mbuf pool */
532 pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
533 MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, rte_socket_id());
534 if (pktmbuf_pool == NULL) {
535 FATAL_ERROR("Could not initialise mbuf pool");
539 /* Get number of ports found in scan */
540 nb_sys_ports = rte_eth_dev_count();
541 if (nb_sys_ports == 0)
542 FATAL_ERROR("No supported Ethernet device found");
543 /* Find highest port set in portmask */
544 for (high_port = (sizeof(ports_mask) * 8) - 1;
545 (high_port != 0) && !(ports_mask & (1 << high_port));
548 if (high_port > nb_sys_ports)
549 FATAL_ERROR("Port mask requires more ports than available");
551 /* Initialise each port */
552 for (port = 0; port < nb_sys_ports; port++) {
553 /* Skip ports that are not enabled */
554 if ((ports_mask & (1 << port)) == 0) {
559 check_all_ports_link_status(nb_sys_ports, ports_mask);
561 /* Launch per-lcore function on every lcore */
562 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
563 RTE_LCORE_FOREACH_SLAVE(i) {
564 if (rte_eal_wait_lcore(i) < 0)