xref: /dpdk/examples/vm_power_manager/guest_cli/vm_power_cli_guest.c (revision 9e06e39b3c6fbcf03d233cbe1fb9604d45dc866f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <termios.h>
10 
11 #include <cmdline_rdline.h>
12 #include <cmdline_parse.h>
13 #include <cmdline_parse_string.h>
14 #include <cmdline_parse_num.h>
15 #include <cmdline_socket.h>
16 #include <cmdline.h>
17 #include <rte_log.h>
18 #include <rte_lcore.h>
19 #include <rte_ethdev.h>
20 
21 #include <rte_power.h>
22 #include <guest_channel.h>
23 
24 #include "vm_power_cli_guest.h"
25 
26 
27 #define CHANNEL_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
28 
29 
30 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
31 
32 struct cmd_quit_result {
33 	cmdline_fixed_string_t quit;
34 };
35 
36 union PFID {
37 	struct rte_ether_addr addr;
38 	uint64_t pfid;
39 };
40 
41 static struct channel_packet policy;
42 
43 struct channel_packet *
44 get_policy(void)
45 {
46 	return &policy;
47 }
48 
49 int
50 set_policy_mac(int port, int idx)
51 {
52 	struct channel_packet *policy;
53 	union PFID pfid;
54 	int ret;
55 
56 	/* Use port MAC address as the vfid */
57 	ret = rte_eth_macaddr_get(port, &pfid.addr);
58 	if (ret != 0) {
59 		printf("Failed to get device (port %u) MAC address: %s\n",
60 				port, rte_strerror(-ret));
61 		return ret;
62 	}
63 
64 	printf("Port %u MAC: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
65 			"%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
66 			port,
67 			pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
68 			pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
69 			pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);
70 	policy = get_policy();
71 	policy->vfid[idx] = pfid.pfid;
72 	return 0;
73 }
74 
75 int
76 set_policy_defaults(struct channel_packet *pkt)
77 {
78 	int ret;
79 
80 	ret = set_policy_mac(0, 0);
81 	if (ret != 0)
82 		return ret;
83 
84 	pkt->nb_mac_to_monitor = 1;
85 
86 	pkt->t_boost_status.tbEnabled = false;
87 
88 	pkt->vcpu_to_control[0] = 0;
89 	pkt->vcpu_to_control[1] = 1;
90 	pkt->num_vcpu = 2;
91 	/* Dummy Population. */
92 	pkt->traffic_policy.min_packet_thresh = 96000;
93 	pkt->traffic_policy.avg_max_packet_thresh = 1800000;
94 	pkt->traffic_policy.max_max_packet_thresh = 2000000;
95 
96 	pkt->timer_policy.busy_hours[0] = 3;
97 	pkt->timer_policy.busy_hours[1] = 4;
98 	pkt->timer_policy.busy_hours[2] = 5;
99 	pkt->timer_policy.quiet_hours[0] = 11;
100 	pkt->timer_policy.quiet_hours[1] = 12;
101 	pkt->timer_policy.quiet_hours[2] = 13;
102 
103 	pkt->timer_policy.hours_to_use_traffic_profile[0] = 8;
104 	pkt->timer_policy.hours_to_use_traffic_profile[1] = 10;
105 
106 	pkt->core_type = CORE_TYPE_VIRTUAL;
107 	pkt->workload = LOW;
108 	pkt->policy_to_use = TIME;
109 	pkt->command = PKT_POLICY;
110 	strcpy(pkt->vm_name, "ubuntu2");
111 
112 	return 0;
113 }
114 
115 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
116 				__attribute__((unused)) struct cmdline *cl,
117 			    __attribute__((unused)) void *data)
118 {
119 	unsigned lcore_id;
120 
121 	RTE_LCORE_FOREACH(lcore_id) {
122 		rte_power_exit(lcore_id);
123 	}
124 	cmdline_quit(cl);
125 }
126 
127 cmdline_parse_token_string_t cmd_quit_quit =
128 	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
129 
130 cmdline_parse_inst_t cmd_quit = {
131 	.f = cmd_quit_parsed,  /* function to call */
132 	.data = NULL,      /* 2nd arg of func */
133 	.help_str = "close the application",
134 	.tokens = {        /* token list, NULL terminated */
135 		(void *)&cmd_quit_quit,
136 		NULL,
137 	},
138 };
139 
140 /* *** VM operations *** */
141 
142 struct cmd_set_cpu_freq_result {
143 	cmdline_fixed_string_t set_cpu_freq;
144 	uint8_t lcore_id;
145 	cmdline_fixed_string_t cmd;
146 };
147 
148 static void
149 cmd_set_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
150 		       __attribute__((unused)) void *data)
151 {
152 	int ret = -1;
153 	struct cmd_set_cpu_freq_result *res = parsed_result;
154 
155 	if (!strcmp(res->cmd , "up"))
156 		ret = rte_power_freq_up(res->lcore_id);
157 	else if (!strcmp(res->cmd , "down"))
158 		ret = rte_power_freq_down(res->lcore_id);
159 	else if (!strcmp(res->cmd , "min"))
160 		ret = rte_power_freq_min(res->lcore_id);
161 	else if (!strcmp(res->cmd , "max"))
162 		ret = rte_power_freq_max(res->lcore_id);
163 	else if (!strcmp(res->cmd, "enable_turbo"))
164 		ret = rte_power_freq_enable_turbo(res->lcore_id);
165 	else if (!strcmp(res->cmd, "disable_turbo"))
166 		ret = rte_power_freq_disable_turbo(res->lcore_id);
167 	if (ret != 1)
168 		cmdline_printf(cl, "Error sending message: %s\n", strerror(ret));
169 }
170 
171 cmdline_parse_token_string_t cmd_set_cpu_freq =
172 	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
173 			set_cpu_freq, "set_cpu_freq");
174 cmdline_parse_token_string_t cmd_set_cpu_freq_core_num =
175 	TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result,
176 			lcore_id, UINT8);
177 cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd =
178 	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
179 			cmd, "up#down#min#max#enable_turbo#disable_turbo");
180 
181 cmdline_parse_inst_t cmd_set_cpu_freq_set = {
182 	.f = cmd_set_cpu_freq_parsed,
183 	.data = NULL,
184 	.help_str = "set_cpu_freq <core_num> "
185 			"<up|down|min|max|enable_turbo|disable_turbo>, "
186 			"adjust the frequency for the specified core.",
187 	.tokens = {
188 		(void *)&cmd_set_cpu_freq,
189 		(void *)&cmd_set_cpu_freq_core_num,
190 		(void *)&cmd_set_cpu_freq_cmd_cmd,
191 		NULL,
192 	},
193 };
194 
195 struct cmd_send_policy_result {
196 	cmdline_fixed_string_t send_policy;
197 	cmdline_fixed_string_t cmd;
198 };
199 
200 static inline int
201 send_policy(struct channel_packet *pkt)
202 {
203 	int ret;
204 
205 	ret = rte_power_guest_channel_send_msg(pkt, 1);
206 	if (ret == 0)
207 		return 1;
208 	RTE_LOG(DEBUG, POWER, "Error sending message: %s\n",
209 			ret > 0 ? strerror(ret) : "channel not connected");
210 	return -1;
211 }
212 
213 static void
214 cmd_send_policy_parsed(void *parsed_result, struct cmdline *cl,
215 		       __attribute__((unused)) void *data)
216 {
217 	int ret = -1;
218 	struct cmd_send_policy_result *res = parsed_result;
219 
220 	if (!strcmp(res->cmd, "now")) {
221 		printf("Sending Policy down now!\n");
222 		ret = send_policy(&policy);
223 	}
224 	if (ret != 1)
225 		cmdline_printf(cl, "Error sending message: %s\n",
226 				strerror(ret));
227 }
228 
229 cmdline_parse_token_string_t cmd_send_policy =
230 	TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
231 			send_policy, "send_policy");
232 cmdline_parse_token_string_t cmd_send_policy_cmd_cmd =
233 	TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
234 			cmd, "now");
235 
236 cmdline_parse_inst_t cmd_send_policy_set = {
237 	.f = cmd_send_policy_parsed,
238 	.data = NULL,
239 	.help_str = "send_policy now",
240 	.tokens = {
241 		(void *)&cmd_send_policy,
242 		(void *)&cmd_send_policy_cmd_cmd,
243 		NULL,
244 	},
245 };
246 
247 cmdline_parse_ctx_t main_ctx[] = {
248 		(cmdline_parse_inst_t *)&cmd_quit,
249 		(cmdline_parse_inst_t *)&cmd_send_policy_set,
250 		(cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
251 		NULL,
252 };
253 
254 void
255 run_cli(__attribute__((unused)) void *arg)
256 {
257 	struct cmdline *cl;
258 
259 	cl = cmdline_stdin_new(main_ctx, "vmpower(guest)> ");
260 	if (cl == NULL)
261 		return;
262 
263 	cmdline_interact(cl);
264 	cmdline_stdin_exit(cl);
265 }
266