xref: /dpdk/examples/vm_power_manager/main.c (revision 3998e2a07220844d3f3c17f76a781ced3efe0de0)
1*3998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*3998e2a0SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
38db653ffSAlan Carew  */
48db653ffSAlan Carew 
58db653ffSAlan Carew #include <stdio.h>
68db653ffSAlan Carew #include <string.h>
78db653ffSAlan Carew #include <stdint.h>
88db653ffSAlan Carew #include <sys/epoll.h>
98db653ffSAlan Carew #include <fcntl.h>
108db653ffSAlan Carew #include <unistd.h>
118db653ffSAlan Carew #include <stdlib.h>
128db653ffSAlan Carew #include <signal.h>
138db653ffSAlan Carew #include <errno.h>
148db653ffSAlan Carew 
158db653ffSAlan Carew #include <sys/queue.h>
168db653ffSAlan Carew 
178db653ffSAlan Carew #include <rte_common.h>
188db653ffSAlan Carew #include <rte_eal.h>
198db653ffSAlan Carew #include <rte_launch.h>
208db653ffSAlan Carew #include <rte_log.h>
218db653ffSAlan Carew #include <rte_per_lcore.h>
228db653ffSAlan Carew #include <rte_lcore.h>
2320c78ac9SDavid Hunt #include <rte_ethdev.h>
2420c78ac9SDavid Hunt #include <getopt.h>
2520c78ac9SDavid Hunt #include <rte_cycles.h>
268db653ffSAlan Carew #include <rte_debug.h>
278db653ffSAlan Carew 
288db653ffSAlan Carew #include "channel_manager.h"
298db653ffSAlan Carew #include "channel_monitor.h"
308db653ffSAlan Carew #include "power_manager.h"
318db653ffSAlan Carew #include "vm_power_cli.h"
32c9a47791SDavid Hunt #include <rte_pmd_ixgbe.h>
33c9a47791SDavid Hunt #include <rte_pmd_i40e.h>
34c9a47791SDavid Hunt #include <rte_pmd_bnxt.h>
358db653ffSAlan Carew 
3620c78ac9SDavid Hunt #define RX_RING_SIZE 512
3720c78ac9SDavid Hunt #define TX_RING_SIZE 512
3820c78ac9SDavid Hunt 
3920c78ac9SDavid Hunt #define NUM_MBUFS 8191
4020c78ac9SDavid Hunt #define MBUF_CACHE_SIZE 250
4120c78ac9SDavid Hunt #define BURST_SIZE 32
4220c78ac9SDavid Hunt 
4320c78ac9SDavid Hunt static uint32_t enabled_port_mask;
4420c78ac9SDavid Hunt static volatile bool force_quit;
4520c78ac9SDavid Hunt 
4620c78ac9SDavid Hunt /****************/
4720c78ac9SDavid Hunt static const struct rte_eth_conf port_conf_default = {
4820c78ac9SDavid Hunt 	.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
4920c78ac9SDavid Hunt };
5020c78ac9SDavid Hunt 
5120c78ac9SDavid Hunt static inline int
5220c78ac9SDavid Hunt port_init(uint16_t port, struct rte_mempool *mbuf_pool)
5320c78ac9SDavid Hunt {
5420c78ac9SDavid Hunt 	struct rte_eth_conf port_conf = port_conf_default;
5520c78ac9SDavid Hunt 	const uint16_t rx_rings = 1, tx_rings = 1;
5620c78ac9SDavid Hunt 	int retval;
5720c78ac9SDavid Hunt 	uint16_t q;
5820c78ac9SDavid Hunt 
5920c78ac9SDavid Hunt 	if (port >= rte_eth_dev_count())
6020c78ac9SDavid Hunt 		return -1;
6120c78ac9SDavid Hunt 
6220c78ac9SDavid Hunt 	/* Configure the Ethernet device. */
6320c78ac9SDavid Hunt 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
6420c78ac9SDavid Hunt 	if (retval != 0)
6520c78ac9SDavid Hunt 		return retval;
6620c78ac9SDavid Hunt 
6720c78ac9SDavid Hunt 	/* Allocate and set up 1 RX queue per Ethernet port. */
6820c78ac9SDavid Hunt 	for (q = 0; q < rx_rings; q++) {
6920c78ac9SDavid Hunt 		retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
7020c78ac9SDavid Hunt 				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
7120c78ac9SDavid Hunt 		if (retval < 0)
7220c78ac9SDavid Hunt 			return retval;
7320c78ac9SDavid Hunt 	}
7420c78ac9SDavid Hunt 
7520c78ac9SDavid Hunt 	/* Allocate and set up 1 TX queue per Ethernet port. */
7620c78ac9SDavid Hunt 	for (q = 0; q < tx_rings; q++) {
7720c78ac9SDavid Hunt 		retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
7820c78ac9SDavid Hunt 				rte_eth_dev_socket_id(port), NULL);
7920c78ac9SDavid Hunt 		if (retval < 0)
8020c78ac9SDavid Hunt 			return retval;
8120c78ac9SDavid Hunt 	}
8220c78ac9SDavid Hunt 
8320c78ac9SDavid Hunt 	/* Start the Ethernet port. */
8420c78ac9SDavid Hunt 	retval = rte_eth_dev_start(port);
8520c78ac9SDavid Hunt 	if (retval < 0)
8620c78ac9SDavid Hunt 		return retval;
8720c78ac9SDavid Hunt 
8820c78ac9SDavid Hunt 	/* Display the port MAC address. */
8920c78ac9SDavid Hunt 	struct ether_addr addr;
9020c78ac9SDavid Hunt 	rte_eth_macaddr_get(port, &addr);
9120c78ac9SDavid Hunt 	printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
9220c78ac9SDavid Hunt 			   " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
9320c78ac9SDavid Hunt 			(unsigned int)port,
9420c78ac9SDavid Hunt 			addr.addr_bytes[0], addr.addr_bytes[1],
9520c78ac9SDavid Hunt 			addr.addr_bytes[2], addr.addr_bytes[3],
9620c78ac9SDavid Hunt 			addr.addr_bytes[4], addr.addr_bytes[5]);
9720c78ac9SDavid Hunt 
9820c78ac9SDavid Hunt 	/* Enable RX in promiscuous mode for the Ethernet device. */
9920c78ac9SDavid Hunt 	rte_eth_promiscuous_enable(port);
10020c78ac9SDavid Hunt 
10120c78ac9SDavid Hunt 
10220c78ac9SDavid Hunt 	return 0;
10320c78ac9SDavid Hunt }
10420c78ac9SDavid Hunt 
10520c78ac9SDavid Hunt static int
10620c78ac9SDavid Hunt parse_portmask(const char *portmask)
10720c78ac9SDavid Hunt {
10820c78ac9SDavid Hunt 	char *end = NULL;
10920c78ac9SDavid Hunt 	unsigned long pm;
11020c78ac9SDavid Hunt 
11120c78ac9SDavid Hunt 	/* parse hexadecimal string */
11220c78ac9SDavid Hunt 	pm = strtoul(portmask, &end, 16);
11320c78ac9SDavid Hunt 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
11420c78ac9SDavid Hunt 		return -1;
11520c78ac9SDavid Hunt 
11620c78ac9SDavid Hunt 	if (pm == 0)
11720c78ac9SDavid Hunt 		return -1;
11820c78ac9SDavid Hunt 
11920c78ac9SDavid Hunt 	return pm;
12020c78ac9SDavid Hunt }
12120c78ac9SDavid Hunt /* Parse the argument given in the command line of the application */
12220c78ac9SDavid Hunt static int
12320c78ac9SDavid Hunt parse_args(int argc, char **argv)
12420c78ac9SDavid Hunt {
12520c78ac9SDavid Hunt 	int opt, ret;
12620c78ac9SDavid Hunt 	char **argvopt;
12720c78ac9SDavid Hunt 	int option_index;
12820c78ac9SDavid Hunt 	char *prgname = argv[0];
12920c78ac9SDavid Hunt 	static struct option lgopts[] = {
13020c78ac9SDavid Hunt 		{ "mac-updating", no_argument, 0, 1},
13120c78ac9SDavid Hunt 		{ "no-mac-updating", no_argument, 0, 0},
13220c78ac9SDavid Hunt 		{NULL, 0, 0, 0}
13320c78ac9SDavid Hunt 	};
13420c78ac9SDavid Hunt 	argvopt = argv;
13520c78ac9SDavid Hunt 
13620c78ac9SDavid Hunt 	while ((opt = getopt_long(argc, argvopt, "p:q:T:",
13720c78ac9SDavid Hunt 				  lgopts, &option_index)) != EOF) {
13820c78ac9SDavid Hunt 
13920c78ac9SDavid Hunt 		switch (opt) {
14020c78ac9SDavid Hunt 		/* portmask */
14120c78ac9SDavid Hunt 		case 'p':
14220c78ac9SDavid Hunt 			enabled_port_mask = parse_portmask(optarg);
14320c78ac9SDavid Hunt 			if (enabled_port_mask == 0) {
14420c78ac9SDavid Hunt 				printf("invalid portmask\n");
14520c78ac9SDavid Hunt 				return -1;
14620c78ac9SDavid Hunt 			}
14720c78ac9SDavid Hunt 			break;
14820c78ac9SDavid Hunt 		/* long options */
14920c78ac9SDavid Hunt 		case 0:
15020c78ac9SDavid Hunt 			break;
15120c78ac9SDavid Hunt 
15220c78ac9SDavid Hunt 		default:
15320c78ac9SDavid Hunt 			return -1;
15420c78ac9SDavid Hunt 		}
15520c78ac9SDavid Hunt 	}
15620c78ac9SDavid Hunt 
15720c78ac9SDavid Hunt 	if (optind >= 0)
15820c78ac9SDavid Hunt 		argv[optind-1] = prgname;
15920c78ac9SDavid Hunt 
16020c78ac9SDavid Hunt 	ret = optind-1;
16120c78ac9SDavid Hunt 	optind = 0; /* reset getopt lib */
16220c78ac9SDavid Hunt 	return ret;
16320c78ac9SDavid Hunt }
16420c78ac9SDavid Hunt 
16520c78ac9SDavid Hunt static void
16620c78ac9SDavid Hunt check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
16720c78ac9SDavid Hunt {
16820c78ac9SDavid Hunt #define CHECK_INTERVAL 100 /* 100ms */
16920c78ac9SDavid Hunt #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
17020c78ac9SDavid Hunt 	uint16_t portid, count, all_ports_up, print_flag = 0;
17120c78ac9SDavid Hunt 	struct rte_eth_link link;
17220c78ac9SDavid Hunt 
17320c78ac9SDavid Hunt 	printf("\nChecking link status");
17420c78ac9SDavid Hunt 	fflush(stdout);
17520c78ac9SDavid Hunt 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
17620c78ac9SDavid Hunt 		if (force_quit)
17720c78ac9SDavid Hunt 			return;
17820c78ac9SDavid Hunt 		all_ports_up = 1;
17920c78ac9SDavid Hunt 		for (portid = 0; portid < port_num; portid++) {
18020c78ac9SDavid Hunt 			if (force_quit)
18120c78ac9SDavid Hunt 				return;
18220c78ac9SDavid Hunt 			if ((port_mask & (1 << portid)) == 0)
18320c78ac9SDavid Hunt 				continue;
18420c78ac9SDavid Hunt 			memset(&link, 0, sizeof(link));
18520c78ac9SDavid Hunt 			rte_eth_link_get_nowait(portid, &link);
18620c78ac9SDavid Hunt 			/* print link status if flag set */
18720c78ac9SDavid Hunt 			if (print_flag == 1) {
18820c78ac9SDavid Hunt 				if (link.link_status)
18920c78ac9SDavid Hunt 					printf("Port %d Link Up - speed %u "
19020c78ac9SDavid Hunt 						"Mbps - %s\n", (uint16_t)portid,
19120c78ac9SDavid Hunt 						(unsigned int)link.link_speed,
19220c78ac9SDavid Hunt 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
19320c78ac9SDavid Hunt 					("full-duplex") : ("half-duplex\n"));
19420c78ac9SDavid Hunt 				else
19520c78ac9SDavid Hunt 					printf("Port %d Link Down\n",
19620c78ac9SDavid Hunt 						(uint16_t)portid);
19720c78ac9SDavid Hunt 				continue;
19820c78ac9SDavid Hunt 			}
19920c78ac9SDavid Hunt 		       /* clear all_ports_up flag if any link down */
20020c78ac9SDavid Hunt 			if (link.link_status == ETH_LINK_DOWN) {
20120c78ac9SDavid Hunt 				all_ports_up = 0;
20220c78ac9SDavid Hunt 				break;
20320c78ac9SDavid Hunt 			}
20420c78ac9SDavid Hunt 		}
20520c78ac9SDavid Hunt 		/* after finally printing all link status, get out */
20620c78ac9SDavid Hunt 		if (print_flag == 1)
20720c78ac9SDavid Hunt 			break;
20820c78ac9SDavid Hunt 
20920c78ac9SDavid Hunt 		if (all_ports_up == 0) {
21020c78ac9SDavid Hunt 			printf(".");
21120c78ac9SDavid Hunt 			fflush(stdout);
21220c78ac9SDavid Hunt 			rte_delay_ms(CHECK_INTERVAL);
21320c78ac9SDavid Hunt 		}
21420c78ac9SDavid Hunt 
21520c78ac9SDavid Hunt 		/* set the print_flag if all ports up or timeout */
21620c78ac9SDavid Hunt 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
21720c78ac9SDavid Hunt 			print_flag = 1;
21820c78ac9SDavid Hunt 			printf("done\n");
21920c78ac9SDavid Hunt 		}
22020c78ac9SDavid Hunt 	}
22120c78ac9SDavid Hunt }
2228db653ffSAlan Carew static int
2238db653ffSAlan Carew run_monitor(__attribute__((unused)) void *arg)
2248db653ffSAlan Carew {
2258db653ffSAlan Carew 	if (channel_monitor_init() < 0) {
2268db653ffSAlan Carew 		printf("Unable to initialize channel monitor\n");
2278db653ffSAlan Carew 		return -1;
2288db653ffSAlan Carew 	}
2298db653ffSAlan Carew 	run_channel_monitor();
2308db653ffSAlan Carew 	return 0;
2318db653ffSAlan Carew }
2328db653ffSAlan Carew 
2338db653ffSAlan Carew static void
2348db653ffSAlan Carew sig_handler(int signo)
2358db653ffSAlan Carew {
2368db653ffSAlan Carew 	printf("Received signal %d, exiting...\n", signo);
2378db653ffSAlan Carew 	channel_monitor_exit();
2388db653ffSAlan Carew 	channel_manager_exit();
2398db653ffSAlan Carew 	power_manager_exit();
2408db653ffSAlan Carew 
2418db653ffSAlan Carew }
2428db653ffSAlan Carew 
2438db653ffSAlan Carew int
24498a16481SDavid Marchand main(int argc, char **argv)
2458db653ffSAlan Carew {
2468db653ffSAlan Carew 	int ret;
2478db653ffSAlan Carew 	unsigned lcore_id;
24820c78ac9SDavid Hunt 	unsigned int nb_ports;
24920c78ac9SDavid Hunt 	struct rte_mempool *mbuf_pool;
25020c78ac9SDavid Hunt 	uint16_t portid;
25120c78ac9SDavid Hunt 
2528db653ffSAlan Carew 
2538db653ffSAlan Carew 	ret = rte_eal_init(argc, argv);
2548db653ffSAlan Carew 	if (ret < 0)
2558db653ffSAlan Carew 		rte_panic("Cannot init EAL\n");
2568db653ffSAlan Carew 
2578db653ffSAlan Carew 	signal(SIGINT, sig_handler);
2588db653ffSAlan Carew 	signal(SIGTERM, sig_handler);
2598db653ffSAlan Carew 
26020c78ac9SDavid Hunt 	argc -= ret;
26120c78ac9SDavid Hunt 	argv += ret;
26220c78ac9SDavid Hunt 
26320c78ac9SDavid Hunt 	/* parse application arguments (after the EAL ones) */
26420c78ac9SDavid Hunt 	ret = parse_args(argc, argv);
26520c78ac9SDavid Hunt 	if (ret < 0)
26620c78ac9SDavid Hunt 		rte_exit(EXIT_FAILURE, "Invalid arguments\n");
26720c78ac9SDavid Hunt 
26820c78ac9SDavid Hunt 	nb_ports = rte_eth_dev_count();
26920c78ac9SDavid Hunt 
27020c78ac9SDavid Hunt 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
27120c78ac9SDavid Hunt 		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
27220c78ac9SDavid Hunt 
27320c78ac9SDavid Hunt 	if (mbuf_pool == NULL)
27420c78ac9SDavid Hunt 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
27520c78ac9SDavid Hunt 
27620c78ac9SDavid Hunt 	/* Initialize ports. */
27720c78ac9SDavid Hunt 	for (portid = 0; portid < nb_ports; portid++) {
278c9a47791SDavid Hunt 		struct ether_addr eth;
279c9a47791SDavid Hunt 		int w, j;
280c9a47791SDavid Hunt 		int ret = -ENOTSUP;
281c9a47791SDavid Hunt 
28220c78ac9SDavid Hunt 		if ((enabled_port_mask & (1 << portid)) == 0)
28320c78ac9SDavid Hunt 			continue;
284c9a47791SDavid Hunt 
285c9a47791SDavid Hunt 		eth.addr_bytes[0] = 0xe0;
286c9a47791SDavid Hunt 		eth.addr_bytes[1] = 0xe0;
287c9a47791SDavid Hunt 		eth.addr_bytes[2] = 0xe0;
288c9a47791SDavid Hunt 		eth.addr_bytes[3] = 0xe0;
289c9a47791SDavid Hunt 		eth.addr_bytes[4] = portid + 0xf0;
290c9a47791SDavid Hunt 
29120c78ac9SDavid Hunt 		if (port_init(portid, mbuf_pool) != 0)
29220c78ac9SDavid Hunt 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
29320c78ac9SDavid Hunt 					portid);
294c9a47791SDavid Hunt 
295c9a47791SDavid Hunt 		for (w = 0; w < MAX_VFS; w++) {
296c9a47791SDavid Hunt 			eth.addr_bytes[5] = w + 0xf0;
297c9a47791SDavid Hunt 
298c9a47791SDavid Hunt 			if (ret == -ENOTSUP)
299c9a47791SDavid Hunt 				ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
300c9a47791SDavid Hunt 						w, &eth);
301c9a47791SDavid Hunt 			if (ret == -ENOTSUP)
302c9a47791SDavid Hunt 				ret = rte_pmd_i40e_set_vf_mac_addr(portid,
303c9a47791SDavid Hunt 						w, &eth);
304c9a47791SDavid Hunt 			if (ret == -ENOTSUP)
305c9a47791SDavid Hunt 				ret = rte_pmd_bnxt_set_vf_mac_addr(portid,
306c9a47791SDavid Hunt 						w, &eth);
307c9a47791SDavid Hunt 
308c9a47791SDavid Hunt 			switch (ret) {
309c9a47791SDavid Hunt 			case 0:
310c9a47791SDavid Hunt 				printf("Port %d VF %d MAC: ",
311c9a47791SDavid Hunt 						portid, w);
312c9a47791SDavid Hunt 				for (j = 0; j < 6; j++) {
313c9a47791SDavid Hunt 					printf("%02x", eth.addr_bytes[j]);
314c9a47791SDavid Hunt 					if (j < 5)
315c9a47791SDavid Hunt 						printf(":");
316c9a47791SDavid Hunt 				}
317c9a47791SDavid Hunt 				printf("\n");
318c9a47791SDavid Hunt 				break;
319c9a47791SDavid Hunt 			}
320c9a47791SDavid Hunt 		}
32120c78ac9SDavid Hunt 	}
32220c78ac9SDavid Hunt 
3238db653ffSAlan Carew 	lcore_id = rte_get_next_lcore(-1, 1, 0);
3248db653ffSAlan Carew 	if (lcore_id == RTE_MAX_LCORE) {
3258db653ffSAlan Carew 		RTE_LOG(ERR, EAL, "A minimum of two cores are required to run "
3268db653ffSAlan Carew 				"application\n");
3278db653ffSAlan Carew 		return 0;
3288db653ffSAlan Carew 	}
32920c78ac9SDavid Hunt 
33020c78ac9SDavid Hunt 	check_all_ports_link_status(nb_ports, enabled_port_mask);
3318db653ffSAlan Carew 	rte_eal_remote_launch(run_monitor, NULL, lcore_id);
3328db653ffSAlan Carew 
3338db653ffSAlan Carew 	if (power_manager_init() < 0) {
3348db653ffSAlan Carew 		printf("Unable to initialize power manager\n");
3358db653ffSAlan Carew 		return -1;
3368db653ffSAlan Carew 	}
3378db653ffSAlan Carew 	if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
3388db653ffSAlan Carew 		printf("Unable to initialize channel manager\n");
3398db653ffSAlan Carew 		return -1;
3408db653ffSAlan Carew 	}
3418db653ffSAlan Carew 	run_cli(NULL);
3428db653ffSAlan Carew 
3438db653ffSAlan Carew 	rte_eal_mp_wait_lcore();
3448db653ffSAlan Carew 	return 0;
3458db653ffSAlan Carew }
346