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>
70 #include <rte_mempool.h>
72 #include <rte_string_fns.h>
73 #include <rte_cycles.h>
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)
80 /* Max ports than can be used (each port is associated with two lcores) */
81 #define MAX_PORTS (RTE_MAX_LCORE / 2)
83 /* Max size of a single packet */
84 #define MAX_PACKET_SZ (2048)
86 /* Size of the data buffer in each mbuf */
87 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
89 /* Number of mbufs in mempool that is created */
92 /* How many packets to attempt to read from NIC in one go */
93 #define PKT_BURST_SZ 32
95 /* How many objects (mbufs) to keep in per-lcore mempool cache */
96 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
98 /* Number of RX ring descriptors */
101 /* Number of TX ring descriptors */
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.
111 /* Options for configuring ethernet port */
112 static const struct rte_eth_conf port_conf = {
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 = 0, /* CRC stripped by hardware */
121 .mq_mode = ETH_MQ_TX_NONE,
125 /* Mempool for mbufs */
126 static struct rte_mempool * pktmbuf_pool = NULL;
128 /* Mask of enabled ports */
129 static uint32_t ports_mask = 0;
131 /* Mask of cores that read from NIC and write to tap */
132 static uint64_t input_cores_mask = 0;
134 /* Mask of cores that read from tap and write to NIC */
135 static uint64_t output_cores_mask = 0;
137 /* Array storing port_id that is associated with each lcore */
138 static uint8_t port_ids[RTE_MAX_LCORE];
140 /* Structure type for recording lcore-specific stats */
147 /* Array of lcore-specific stats */
148 static struct stats lcore_stats[RTE_MAX_LCORE];
150 /* Print out statistics on packets handled */
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);
166 printf("======= ====== ============ ============ ===============\n");
169 /* Custom handling of signals to handle stats */
171 signal_handler(int signum)
173 /* When we receive a USR1 signal, print stats */
174 if (signum == SIGUSR1) {
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");
187 * Create a tap network interface, or use existing one with same name.
188 * If name[0]='\0' then a name is automatically assigned and returned in name.
190 static int tap_create(char *name)
195 fd = open("/dev/net/tun", O_RDWR);
199 memset(&ifr, 0, sizeof(ifr));
201 /* TAP device without packet information */
202 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
205 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
207 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
214 snprintf(name, IFNAMSIZ, "%s", ifr.ifr_name);
219 /* Main processing loop */
221 main_loop(__attribute__((unused)) void *arg)
223 const unsigned lcore_id = rte_lcore_id();
224 char tap_name[IFNAMSIZ];
227 if ((1ULL << lcore_id) & input_cores_mask) {
228 /* Create new tap interface */
229 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
230 tap_fd = tap_create(tap_name);
232 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
235 PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
236 lcore_id, (unsigned)port_ids[lcore_id], tap_name);
238 /* Loop forever reading from NIC and writing to tap */
240 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
242 const unsigned nb_rx =
243 rte_eth_rx_burst(port_ids[lcore_id], 0,
244 pkts_burst, PKT_BURST_SZ);
245 lcore_stats[lcore_id].rx += nb_rx;
246 for (i = 0; likely(i < nb_rx); i++) {
247 struct rte_mbuf *m = pkts_burst[i];
248 /* Ignore return val from write() */
249 int ret = write(tap_fd,
250 rte_pktmbuf_mtod(m, void*),
251 rte_pktmbuf_data_len(m));
253 if (unlikely(ret < 0))
254 lcore_stats[lcore_id].dropped++;
256 lcore_stats[lcore_id].tx++;
260 else if ((1ULL << lcore_id) & output_cores_mask) {
261 /* Create new tap interface */
262 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
263 tap_fd = tap_create(tap_name);
265 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
268 PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
269 lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
271 /* Loop forever reading from tap and writing to NIC */
274 struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
278 ret = read(tap_fd, rte_pktmbuf_mtod(m, void *),
280 lcore_stats[lcore_id].rx++;
281 if (unlikely(ret < 0)) {
282 FATAL_ERROR("Reading from %s interface failed",
287 m->pkt_len = (uint16_t)ret;
288 m->data_len = (uint16_t)ret;
289 ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
290 if (unlikely(ret < 1)) {
292 lcore_stats[lcore_id].dropped++;
295 lcore_stats[lcore_id].tx++;
300 PRINT_INFO("Lcore %u has nothing to do", lcore_id);
304 * Tap file is closed automatically when program exits. Putting close()
305 * here will cause the compiler to give an error about unreachable code.
309 /* Display usage instructions */
311 print_usage(const char *prgname)
313 PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
314 " -p PORTMASK: hex bitmask of ports to use\n"
315 " -i IN_CORES: hex bitmask of cores which read from NIC\n"
316 " -o OUT_CORES: hex bitmask of cores which write to NIC",
320 /* Convert string to unsigned number. 0 is returned if error occurs */
322 parse_unsigned(const char *portmask)
327 num = strtoull(portmask, &end, 16);
328 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
331 return (uint64_t)num;
334 /* Record affinities between ports and lcores in global port_ids[] array */
336 setup_port_lcore_affinities(void)
342 /* Setup port_ids[] array, and check masks were ok */
343 RTE_LCORE_FOREACH(i) {
344 if (input_cores_mask & (1ULL << i)) {
345 /* Skip ports that are not enabled */
346 while ((ports_mask & (1 << rx_port)) == 0) {
348 if (rx_port > (sizeof(ports_mask) * 8))
349 goto fail; /* not enough ports */
352 port_ids[i] = rx_port++;
354 else if (output_cores_mask & (1ULL << i)) {
355 /* Skip ports that are not enabled */
356 while ((ports_mask & (1 << tx_port)) == 0) {
358 if (tx_port > (sizeof(ports_mask) * 8))
359 goto fail; /* not enough ports */
362 port_ids[i] = tx_port++;
366 if (rx_port != tx_port)
367 goto fail; /* uneven number of cores in masks */
369 if (ports_mask & (~((1 << rx_port) - 1)))
370 goto fail; /* unused ports */
374 FATAL_ERROR("Invalid core/port masks specified on command line");
377 /* Parse the arguments given in the command line of the application */
379 parse_args(int argc, char **argv)
382 const char *prgname = argv[0];
384 /* Disable printing messages within getopt() */
387 /* Parse command line */
388 while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
391 input_cores_mask = parse_unsigned(optarg);
394 output_cores_mask = parse_unsigned(optarg);
397 ports_mask = parse_unsigned(optarg);
400 print_usage(prgname);
401 FATAL_ERROR("Invalid option specified");
405 /* Check that options were parsed ok */
406 if (input_cores_mask == 0) {
407 print_usage(prgname);
408 FATAL_ERROR("IN_CORES not specified correctly");
410 if (output_cores_mask == 0) {
411 print_usage(prgname);
412 FATAL_ERROR("OUT_CORES not specified correctly");
414 if (ports_mask == 0) {
415 print_usage(prgname);
416 FATAL_ERROR("PORTMASK not specified correctly");
419 setup_port_lcore_affinities();
422 /* Initialise a single port on an Ethernet device */
424 init_port(uint8_t port)
428 /* Initialise device and RX/TX queues */
429 PRINT_INFO("Initialising port %u ...", (unsigned)port);
431 ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
433 FATAL_ERROR("Could not configure port%u (%d)",
434 (unsigned)port, ret);
436 ret = rte_eth_rx_queue_setup(port, 0, NB_RXD, rte_eth_dev_socket_id(port),
440 FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
441 (unsigned)port, ret);
443 ret = rte_eth_tx_queue_setup(port, 0, NB_TXD, rte_eth_dev_socket_id(port),
446 FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
447 (unsigned)port, ret);
449 ret = rte_eth_dev_start(port);
451 FATAL_ERROR("Could not start port%u (%d)", (unsigned)port, ret);
453 rte_eth_promiscuous_enable(port);
456 /* Check the link status of all ports in up to 9s, and print them finally */
458 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
460 #define CHECK_INTERVAL 100 /* 100ms */
461 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
462 uint8_t portid, count, all_ports_up, print_flag = 0;
463 struct rte_eth_link link;
465 printf("\nChecking link status");
467 for (count = 0; count <= MAX_CHECK_TIME; count++) {
469 for (portid = 0; portid < port_num; portid++) {
470 if ((port_mask & (1 << portid)) == 0)
472 memset(&link, 0, sizeof(link));
473 rte_eth_link_get_nowait(portid, &link);
474 /* print link status if flag set */
475 if (print_flag == 1) {
476 if (link.link_status)
477 printf("Port %d Link Up - speed %u "
478 "Mbps - %s\n", (uint8_t)portid,
479 (unsigned)link.link_speed,
480 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
481 ("full-duplex") : ("half-duplex\n"));
483 printf("Port %d Link Down\n",
487 /* clear all_ports_up flag if any link down */
488 if (link.link_status == 0) {
493 /* after finally printing all link status, get out */
497 if (all_ports_up == 0) {
500 rte_delay_ms(CHECK_INTERVAL);
503 /* set the print_flag if all ports up or timeout */
504 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
511 /* Initialise ports/queues etc. and start main loop on each core */
513 main(int argc, char** argv)
516 unsigned i,high_port;
517 uint8_t nb_sys_ports, port;
519 /* Associate signal_hanlder function with USR signals */
520 signal(SIGUSR1, signal_handler);
521 signal(SIGUSR2, signal_handler);
524 ret = rte_eal_init(argc, argv);
526 FATAL_ERROR("Could not initialise EAL (%d)", ret);
530 /* Parse application arguments (after the EAL ones) */
531 parse_args(argc, argv);
533 /* Create the mbuf pool */
534 pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
535 MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, rte_socket_id());
536 if (pktmbuf_pool == NULL) {
537 FATAL_ERROR("Could not initialise mbuf pool");
541 /* Get number of ports found in scan */
542 nb_sys_ports = rte_eth_dev_count();
543 if (nb_sys_ports == 0)
544 FATAL_ERROR("No supported Ethernet device found");
545 /* Find highest port set in portmask */
546 for (high_port = (sizeof(ports_mask) * 8) - 1;
547 (high_port != 0) && !(ports_mask & (1 << high_port));
550 if (high_port > nb_sys_ports)
551 FATAL_ERROR("Port mask requires more ports than available");
553 /* Initialise each port */
554 for (port = 0; port < nb_sys_ports; port++) {
555 /* Skip ports that are not enabled */
556 if ((ports_mask & (1 << port)) == 0) {
561 check_all_ports_link_status(nb_sys_ports, ports_mask);
563 /* Launch per-lcore function on every lcore */
564 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
565 RTE_LCORE_FOREACH_SLAVE(i) {
566 if (rte_eal_wait_lcore(i) < 0)