1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
15 #include <netinet/in.h>
17 #ifdef RTE_EXEC_ENV_LINUXAPP
18 #include <linux/if_tun.h>
21 #include <sys/ioctl.h>
25 #include <rte_common.h>
27 #include <rte_memory.h>
28 #include <rte_memcpy.h>
30 #include <rte_per_lcore.h>
31 #include <rte_launch.h>
32 #include <rte_atomic.h>
33 #include <rte_lcore.h>
34 #include <rte_branch_prediction.h>
35 #include <rte_interrupts.h>
36 #include <rte_debug.h>
37 #include <rte_ether.h>
38 #include <rte_ethdev.h>
39 #include <rte_mempool.h>
41 #include <rte_string_fns.h>
42 #include <rte_cycles.h>
44 /* Macros for printing using RTE_LOG */
45 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
46 #define FATAL_ERROR(fmt, args...) rte_exit(EXIT_FAILURE, fmt "\n", ##args)
47 #define PRINT_INFO(fmt, args...) RTE_LOG(INFO, APP, fmt "\n", ##args)
49 /* Max ports than can be used (each port is associated with two lcores) */
50 #define MAX_PORTS (RTE_MAX_LCORE / 2)
52 /* Max size of a single packet */
53 #define MAX_PACKET_SZ (2048)
55 /* Size of the data buffer in each mbuf */
56 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
58 /* Number of mbufs in mempool that is created */
61 /* How many packets to attempt to read from NIC in one go */
62 #define PKT_BURST_SZ 32
64 /* How many objects (mbufs) to keep in per-lcore mempool cache */
65 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
67 /* Number of RX ring descriptors */
70 /* Number of TX ring descriptors */
74 * RX and TX Prefetch, Host, and Write-back threshold values should be
75 * carefully set for optimal performance. Consult the network
76 * controller's datasheet and supporting DPDK documentation for guidance
77 * on how these parameters should be set.
80 /* Options for configuring ethernet port */
81 static const struct rte_eth_conf port_conf = {
83 .header_split = 0, /* Header Split disabled */
84 .hw_ip_checksum = 0, /* IP checksum offload disabled */
85 .hw_vlan_filter = 0, /* VLAN filtering disabled */
86 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
87 .hw_strip_crc = 1, /* CRC stripped by hardware */
90 .mq_mode = ETH_MQ_TX_NONE,
94 /* Mempool for mbufs */
95 static struct rte_mempool * pktmbuf_pool = NULL;
97 /* Mask of enabled ports */
98 static uint32_t ports_mask = 0;
100 /* Mask of cores that read from NIC and write to tap */
101 static uint64_t input_cores_mask = 0;
103 /* Mask of cores that read from tap and write to NIC */
104 static uint64_t output_cores_mask = 0;
106 /* Array storing port_id that is associated with each lcore */
107 static uint16_t port_ids[RTE_MAX_LCORE];
109 /* Structure type for recording lcore-specific stats */
116 /* Array of lcore-specific stats */
117 static struct stats lcore_stats[RTE_MAX_LCORE];
119 /* Print out statistics on packets handled */
125 printf("\n**Exception-Path example application statistics**\n"
126 "======= ====== ============ ============ ===============\n"
127 " Lcore Port RX TX Dropped on TX\n"
128 "------- ------ ------------ ------------ ---------------\n");
129 RTE_LCORE_FOREACH(i) {
130 printf("%6u %7u %13"PRIu64" %13"PRIu64" %16"PRIu64"\n",
131 i, (unsigned)port_ids[i],
132 lcore_stats[i].rx, lcore_stats[i].tx,
133 lcore_stats[i].dropped);
135 printf("======= ====== ============ ============ ===============\n");
138 /* Custom handling of signals to handle stats */
140 signal_handler(int signum)
142 /* When we receive a USR1 signal, print stats */
143 if (signum == SIGUSR1) {
147 /* When we receive a USR2 signal, reset stats */
148 if (signum == SIGUSR2) {
149 memset(&lcore_stats, 0, sizeof(lcore_stats));
150 printf("\n**Statistics have been reset**\n");
155 #ifdef RTE_EXEC_ENV_LINUXAPP
157 * Create a tap network interface, or use existing one with same name.
158 * If name[0]='\0' then a name is automatically assigned and returned in name.
160 static int tap_create(char *name)
165 fd = open("/dev/net/tun", O_RDWR);
169 memset(&ifr, 0, sizeof(ifr));
171 /* TAP device without packet information */
172 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
175 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
177 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
184 snprintf(name, IFNAMSIZ, "%s", ifr.ifr_name);
190 * Find a free tap network interface, or create a new one.
191 * The name is automatically assigned and returned in name.
193 static int tap_create(char *name)
196 char devname[PATH_MAX];
198 for (i = 0; i < 255; i++) {
199 snprintf(devname, sizeof(devname), "/dev/tap%d", i);
200 fd = open(devname, O_RDWR);
201 if (fd >= 0 || errno != EBUSY)
206 snprintf(name, IFNAMSIZ, "tap%d", i);
212 /* Main processing loop */
214 main_loop(__attribute__((unused)) void *arg)
216 const unsigned lcore_id = rte_lcore_id();
217 char tap_name[IFNAMSIZ];
220 if ((1ULL << lcore_id) & input_cores_mask) {
221 /* Create new tap interface */
222 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
223 tap_fd = tap_create(tap_name);
225 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
228 PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
229 lcore_id, (unsigned)port_ids[lcore_id], tap_name);
231 /* Loop forever reading from NIC and writing to tap */
233 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
235 const unsigned nb_rx =
236 rte_eth_rx_burst(port_ids[lcore_id], 0,
237 pkts_burst, PKT_BURST_SZ);
238 lcore_stats[lcore_id].rx += nb_rx;
239 for (i = 0; likely(i < nb_rx); i++) {
240 struct rte_mbuf *m = pkts_burst[i];
241 /* Ignore return val from write() */
242 int ret = write(tap_fd,
243 rte_pktmbuf_mtod(m, void*),
244 rte_pktmbuf_data_len(m));
246 if (unlikely(ret < 0))
247 lcore_stats[lcore_id].dropped++;
249 lcore_stats[lcore_id].tx++;
253 else if ((1ULL << lcore_id) & output_cores_mask) {
254 /* Create new tap interface */
255 snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
256 tap_fd = tap_create(tap_name);
258 FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
261 PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
262 lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
264 /* Loop forever reading from tap and writing to NIC */
267 struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
271 ret = read(tap_fd, rte_pktmbuf_mtod(m, void *),
273 lcore_stats[lcore_id].rx++;
274 if (unlikely(ret < 0)) {
275 FATAL_ERROR("Reading from %s interface failed",
280 m->pkt_len = (uint16_t)ret;
281 m->data_len = (uint16_t)ret;
282 ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
283 if (unlikely(ret < 1)) {
285 lcore_stats[lcore_id].dropped++;
288 lcore_stats[lcore_id].tx++;
293 PRINT_INFO("Lcore %u has nothing to do", lcore_id);
297 * Tap file is closed automatically when program exits. Putting close()
298 * here will cause the compiler to give an error about unreachable code.
302 /* Display usage instructions */
304 print_usage(const char *prgname)
306 PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
307 " -p PORTMASK: hex bitmask of ports to use\n"
308 " -i IN_CORES: hex bitmask of cores which read from NIC\n"
309 " -o OUT_CORES: hex bitmask of cores which write to NIC",
313 /* Convert string to unsigned number. 0 is returned if error occurs */
315 parse_unsigned(const char *portmask)
320 num = strtoull(portmask, &end, 16);
321 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
324 return (uint64_t)num;
327 /* Record affinities between ports and lcores in global port_ids[] array */
329 setup_port_lcore_affinities(void)
332 uint16_t tx_port = 0;
333 uint16_t rx_port = 0;
335 /* Setup port_ids[] array, and check masks were ok */
336 RTE_LCORE_FOREACH(i) {
337 if (input_cores_mask & (1ULL << i)) {
338 /* Skip ports that are not enabled */
339 while ((ports_mask & (1 << rx_port)) == 0) {
341 if (rx_port > (sizeof(ports_mask) * 8))
342 goto fail; /* not enough ports */
345 port_ids[i] = rx_port++;
346 } else if (output_cores_mask & (1ULL << (i & 0x3f))) {
347 /* Skip ports that are not enabled */
348 while ((ports_mask & (1 << tx_port)) == 0) {
350 if (tx_port > (sizeof(ports_mask) * 8))
351 goto fail; /* not enough ports */
354 port_ids[i] = tx_port++;
358 if (rx_port != tx_port)
359 goto fail; /* uneven number of cores in masks */
361 if (ports_mask & (~((1 << rx_port) - 1)))
362 goto fail; /* unused ports */
366 FATAL_ERROR("Invalid core/port masks specified on command line");
369 /* Parse the arguments given in the command line of the application */
371 parse_args(int argc, char **argv)
374 const char *prgname = argv[0];
376 /* Disable printing messages within getopt() */
379 /* Parse command line */
380 while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
383 input_cores_mask = parse_unsigned(optarg);
386 output_cores_mask = parse_unsigned(optarg);
389 ports_mask = parse_unsigned(optarg);
392 print_usage(prgname);
393 FATAL_ERROR("Invalid option specified");
397 /* Check that options were parsed ok */
398 if (input_cores_mask == 0) {
399 print_usage(prgname);
400 FATAL_ERROR("IN_CORES not specified correctly");
402 if (output_cores_mask == 0) {
403 print_usage(prgname);
404 FATAL_ERROR("OUT_CORES not specified correctly");
406 if (ports_mask == 0) {
407 print_usage(prgname);
408 FATAL_ERROR("PORTMASK not specified correctly");
411 setup_port_lcore_affinities();
414 /* Initialise a single port on an Ethernet device */
416 init_port(uint16_t port)
419 uint16_t nb_rxd = NB_RXD;
420 uint16_t nb_txd = NB_TXD;
422 /* Initialise device and RX/TX queues */
423 PRINT_INFO("Initialising port %u ...", port);
425 ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
427 FATAL_ERROR("Could not configure port%u (%d)", port, ret);
429 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
431 FATAL_ERROR("Could not adjust number of descriptors for port%u (%d)",
434 ret = rte_eth_rx_queue_setup(port, 0, nb_rxd,
435 rte_eth_dev_socket_id(port),
439 FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
442 ret = rte_eth_tx_queue_setup(port, 0, nb_txd,
443 rte_eth_dev_socket_id(port),
446 FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
449 ret = rte_eth_dev_start(port);
451 FATAL_ERROR("Could not start port%u (%d)", 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(uint16_t port_num, uint32_t port_mask)
460 #define CHECK_INTERVAL 100 /* 100ms */
461 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
463 uint8_t count, all_ports_up, print_flag = 0;
464 struct rte_eth_link link;
466 printf("\nChecking link status");
468 for (count = 0; count <= MAX_CHECK_TIME; count++) {
470 for (portid = 0; portid < port_num; portid++) {
471 if ((port_mask & (1 << portid)) == 0)
473 memset(&link, 0, sizeof(link));
474 rte_eth_link_get_nowait(portid, &link);
475 /* print link status if flag set */
476 if (print_flag == 1) {
477 if (link.link_status)
479 "Port%d Link Up. Speed %u Mbps - %s\n",
480 portid, link.link_speed,
481 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
482 ("full-duplex") : ("half-duplex\n"));
484 printf("Port %d Link Down\n", portid);
487 /* clear all_ports_up flag if any link down */
488 if (link.link_status == ETH_LINK_DOWN) {
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 uint16_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)