xref: /dpdk/examples/vm_power_manager/guest_cli/main.c (revision 59287933a0bb7101cdf9df8ba5dba0ae944e1ee3)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3f5e5c334SAlan Carew  */
4f5e5c334SAlan Carew 
5f5e5c334SAlan Carew #include <stdio.h>
6f5e5c334SAlan Carew #include <stdlib.h>
7f5e5c334SAlan Carew #include <signal.h>
8*59287933SDavid Hunt #include <getopt.h>
9*59287933SDavid Hunt #include <string.h>
10f5e5c334SAlan Carew 
11f5e5c334SAlan Carew #include <rte_lcore.h>
12f5e5c334SAlan Carew #include <rte_power.h>
13f5e5c334SAlan Carew #include <rte_debug.h>
14*59287933SDavid Hunt #include <rte_eal.h>
15*59287933SDavid Hunt #include <rte_log.h>
16f5e5c334SAlan Carew 
17f5e5c334SAlan Carew #include "vm_power_cli_guest.h"
18*59287933SDavid Hunt #include "parse.h"
19f5e5c334SAlan Carew 
20f5e5c334SAlan Carew static void
21f5e5c334SAlan Carew sig_handler(int signo)
22f5e5c334SAlan Carew {
23f5e5c334SAlan Carew 	printf("Received signal %d, exiting...\n", signo);
24f5e5c334SAlan Carew 	unsigned lcore_id;
25f5e5c334SAlan Carew 
26f5e5c334SAlan Carew 	RTE_LCORE_FOREACH(lcore_id) {
27f5e5c334SAlan Carew 		rte_power_exit(lcore_id);
28f5e5c334SAlan Carew 	}
29f5e5c334SAlan Carew 
30f5e5c334SAlan Carew }
31f5e5c334SAlan Carew 
32*59287933SDavid Hunt #define MAX_HOURS 24
33*59287933SDavid Hunt 
34*59287933SDavid Hunt /* Parse the argument given in the command line of the application */
35*59287933SDavid Hunt static int
36*59287933SDavid Hunt parse_args(int argc, char **argv)
37*59287933SDavid Hunt {
38*59287933SDavid Hunt 	int opt, ret;
39*59287933SDavid Hunt 	char **argvopt;
40*59287933SDavid Hunt 	int option_index;
41*59287933SDavid Hunt 	char *prgname = argv[0];
42*59287933SDavid Hunt 	const struct option lgopts[] = {
43*59287933SDavid Hunt 		{ "vm-name", required_argument, 0, 'n'},
44*59287933SDavid Hunt 		{ "busy-hours", required_argument, 0, 'b'},
45*59287933SDavid Hunt 		{ "quiet-hours", required_argument, 0, 'q'},
46*59287933SDavid Hunt 		{ "port-list", required_argument, 0, 'p'},
47*59287933SDavid Hunt 		{ "vcpu-list", required_argument, 0, 'l'},
48*59287933SDavid Hunt 		{ "policy", required_argument, 0, 'o'},
49*59287933SDavid Hunt 		{NULL, 0, 0, 0}
50*59287933SDavid Hunt 	};
51*59287933SDavid Hunt 	struct channel_packet *policy;
52*59287933SDavid Hunt 	unsigned short int hours[MAX_HOURS];
53*59287933SDavid Hunt 	unsigned short int cores[MAX_VCPU_PER_VM];
54*59287933SDavid Hunt 	unsigned short int ports[MAX_VCPU_PER_VM];
55*59287933SDavid Hunt 	int i, cnt, idx;
56*59287933SDavid Hunt 
57*59287933SDavid Hunt 	policy = get_policy();
58*59287933SDavid Hunt 	set_policy_defaults(policy);
59*59287933SDavid Hunt 
60*59287933SDavid Hunt 	argvopt = argv;
61*59287933SDavid Hunt 
62*59287933SDavid Hunt 	while ((opt = getopt_long(argc, argvopt, "n:b:q:p:",
63*59287933SDavid Hunt 				  lgopts, &option_index)) != EOF) {
64*59287933SDavid Hunt 
65*59287933SDavid Hunt 		switch (opt) {
66*59287933SDavid Hunt 		/* portmask */
67*59287933SDavid Hunt 		case 'n':
68*59287933SDavid Hunt 			strcpy(policy->vm_name, optarg);
69*59287933SDavid Hunt 			printf("Setting VM Name to [%s]\n", policy->vm_name);
70*59287933SDavid Hunt 			break;
71*59287933SDavid Hunt 		case 'b':
72*59287933SDavid Hunt 		case 'q':
73*59287933SDavid Hunt 			//printf("***Processing set using [%s]\n", optarg);
74*59287933SDavid Hunt 			cnt = parse_set(optarg, hours, MAX_HOURS);
75*59287933SDavid Hunt 			if (cnt < 0) {
76*59287933SDavid Hunt 				printf("Invalid value passed to quiet/busy hours - [%s]\n",
77*59287933SDavid Hunt 						optarg);
78*59287933SDavid Hunt 				break;
79*59287933SDavid Hunt 			}
80*59287933SDavid Hunt 			idx = 0;
81*59287933SDavid Hunt 			for (i = 0; i < MAX_HOURS; i++) {
82*59287933SDavid Hunt 				if (hours[i]) {
83*59287933SDavid Hunt 					if (opt == 'b') {
84*59287933SDavid Hunt 						printf("***Busy Hour %d\n", i);
85*59287933SDavid Hunt 						policy->timer_policy.busy_hours
86*59287933SDavid Hunt 							[idx++] = i;
87*59287933SDavid Hunt 					} else {
88*59287933SDavid Hunt 						printf("***Quiet Hour %d\n", i);
89*59287933SDavid Hunt 						policy->timer_policy.quiet_hours
90*59287933SDavid Hunt 							[idx++] = i;
91*59287933SDavid Hunt 					}
92*59287933SDavid Hunt 				}
93*59287933SDavid Hunt 			}
94*59287933SDavid Hunt 			break;
95*59287933SDavid Hunt 		case 'l':
96*59287933SDavid Hunt 			cnt = parse_set(optarg, cores, MAX_VCPU_PER_VM);
97*59287933SDavid Hunt 			if (cnt < 0) {
98*59287933SDavid Hunt 				printf("Invalid value passed to vcpu-list - [%s]\n",
99*59287933SDavid Hunt 						optarg);
100*59287933SDavid Hunt 				break;
101*59287933SDavid Hunt 			}
102*59287933SDavid Hunt 			idx = 0;
103*59287933SDavid Hunt 			for (i = 0; i < MAX_VCPU_PER_VM; i++) {
104*59287933SDavid Hunt 				if (cores[i]) {
105*59287933SDavid Hunt 					printf("***Using core %d\n", i);
106*59287933SDavid Hunt 					policy->vcpu_to_control[idx++] = i;
107*59287933SDavid Hunt 				}
108*59287933SDavid Hunt 			}
109*59287933SDavid Hunt 			policy->num_vcpu = idx;
110*59287933SDavid Hunt 			printf("Total cores: %d\n", idx);
111*59287933SDavid Hunt 			break;
112*59287933SDavid Hunt 		case 'p':
113*59287933SDavid Hunt 			cnt = parse_set(optarg, ports, MAX_VCPU_PER_VM);
114*59287933SDavid Hunt 			if (cnt < 0) {
115*59287933SDavid Hunt 				printf("Invalid value passed to port-list - [%s]\n",
116*59287933SDavid Hunt 						optarg);
117*59287933SDavid Hunt 				break;
118*59287933SDavid Hunt 			}
119*59287933SDavid Hunt 			idx = 0;
120*59287933SDavid Hunt 			for (i = 0; i < MAX_VCPU_PER_VM; i++) {
121*59287933SDavid Hunt 				if (ports[i]) {
122*59287933SDavid Hunt 					printf("***Using port %d\n", i);
123*59287933SDavid Hunt 					set_policy_mac(i, idx++);
124*59287933SDavid Hunt 				}
125*59287933SDavid Hunt 			}
126*59287933SDavid Hunt 			policy->nb_mac_to_monitor = idx;
127*59287933SDavid Hunt 			printf("Total Ports: %d\n", idx);
128*59287933SDavid Hunt 			break;
129*59287933SDavid Hunt 		case 'o':
130*59287933SDavid Hunt 			if (!strcmp(optarg, "TRAFFIC"))
131*59287933SDavid Hunt 				policy->policy_to_use = TRAFFIC;
132*59287933SDavid Hunt 			else if (!strcmp(optarg, "TIME"))
133*59287933SDavid Hunt 				policy->policy_to_use = TIME;
134*59287933SDavid Hunt 			else if (!strcmp(optarg, "WORKLOAD"))
135*59287933SDavid Hunt 				policy->policy_to_use = WORKLOAD;
136*59287933SDavid Hunt 			else if (!strcmp(optarg, "BRANCH_RATIO"))
137*59287933SDavid Hunt 				policy->policy_to_use = BRANCH_RATIO;
138*59287933SDavid Hunt 			else {
139*59287933SDavid Hunt 				printf("Invalid policy specified: %s\n",
140*59287933SDavid Hunt 						optarg);
141*59287933SDavid Hunt 				return -1;
142*59287933SDavid Hunt 			}
143*59287933SDavid Hunt 			break;
144*59287933SDavid Hunt 		/* long options */
145*59287933SDavid Hunt 
146*59287933SDavid Hunt 		case 0:
147*59287933SDavid Hunt 			break;
148*59287933SDavid Hunt 
149*59287933SDavid Hunt 		default:
150*59287933SDavid Hunt 			return -1;
151*59287933SDavid Hunt 		}
152*59287933SDavid Hunt 	}
153*59287933SDavid Hunt 
154*59287933SDavid Hunt 	if (optind >= 0)
155*59287933SDavid Hunt 		argv[optind-1] = prgname;
156*59287933SDavid Hunt 
157*59287933SDavid Hunt 	ret = optind-1;
158*59287933SDavid Hunt 	optind = 0; /* reset getopt lib */
159*59287933SDavid Hunt 	return ret;
160*59287933SDavid Hunt }
161*59287933SDavid Hunt 
162f5e5c334SAlan Carew int
16398a16481SDavid Marchand main(int argc, char **argv)
164f5e5c334SAlan Carew {
165f5e5c334SAlan Carew 	int ret;
166f5e5c334SAlan Carew 	unsigned lcore_id;
167f5e5c334SAlan Carew 
168f5e5c334SAlan Carew 	ret = rte_eal_init(argc, argv);
169f5e5c334SAlan Carew 	if (ret < 0)
170f5e5c334SAlan Carew 		rte_panic("Cannot init EAL\n");
171f5e5c334SAlan Carew 
172f5e5c334SAlan Carew 	signal(SIGINT, sig_handler);
173f5e5c334SAlan Carew 	signal(SIGTERM, sig_handler);
174f5e5c334SAlan Carew 
175*59287933SDavid Hunt 	argc -= ret;
176*59287933SDavid Hunt 	argv += ret;
177*59287933SDavid Hunt 
178*59287933SDavid Hunt 	/* parse application arguments (after the EAL ones) */
179*59287933SDavid Hunt 	ret = parse_args(argc, argv);
180*59287933SDavid Hunt 	if (ret < 0)
181*59287933SDavid Hunt 		rte_exit(EXIT_FAILURE, "Invalid arguments\n");
182*59287933SDavid Hunt 
183f5e5c334SAlan Carew 	rte_power_set_env(PM_ENV_KVM_VM);
184f5e5c334SAlan Carew 	RTE_LCORE_FOREACH(lcore_id) {
185f5e5c334SAlan Carew 		rte_power_init(lcore_id);
186f5e5c334SAlan Carew 	}
187f5e5c334SAlan Carew 	run_cli(NULL);
188f5e5c334SAlan Carew 
189f5e5c334SAlan Carew 	return 0;
190f5e5c334SAlan Carew }
191