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