xref: /dpdk/examples/vm_power_manager/guest_cli/vm_power_cli_guest.c (revision ade02f0f3e7fbb5acd98b2fdf5fce0945ad9554a)
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> <up|down|min|max>, Set the current "
135 			"frequency for the specified core by scaling up/down/min/max",
136 	.tokens = {
137 		(void *)&cmd_set_cpu_freq,
138 		(void *)&cmd_set_cpu_freq_core_num,
139 		(void *)&cmd_set_cpu_freq_cmd_cmd,
140 		NULL,
141 	},
142 };
143 
144 struct cmd_send_policy_result {
145 	cmdline_fixed_string_t send_policy;
146 	cmdline_fixed_string_t cmd;
147 };
148 
149 union PFID {
150 	struct ether_addr addr;
151 	uint64_t pfid;
152 };
153 
154 static inline int
155 send_policy(void)
156 {
157 	struct channel_packet pkt;
158 	int ret;
159 
160 	union PFID pfid;
161 	/* Use port MAC address as the vfid */
162 	rte_eth_macaddr_get(0, &pfid.addr);
163 	printf("Port %u MAC: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
164 			"%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
165 			1,
166 			pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
167 			pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
168 			pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);
169 	pkt.vfid[0] = pfid.pfid;
170 
171 	pkt.nb_mac_to_monitor = 1;
172 	pkt.t_boost_status.tbEnabled = false;
173 
174 	pkt.vcpu_to_control[0] = 0;
175 	pkt.vcpu_to_control[1] = 1;
176 	pkt.num_vcpu = 2;
177 	/* Dummy Population. */
178 	pkt.traffic_policy.min_packet_thresh = 96000;
179 	pkt.traffic_policy.avg_max_packet_thresh = 1800000;
180 	pkt.traffic_policy.max_max_packet_thresh = 2000000;
181 
182 	pkt.timer_policy.busy_hours[0] = 3;
183 	pkt.timer_policy.busy_hours[1] = 4;
184 	pkt.timer_policy.busy_hours[2] = 5;
185 	pkt.timer_policy.quiet_hours[0] = 11;
186 	pkt.timer_policy.quiet_hours[1] = 12;
187 	pkt.timer_policy.quiet_hours[2] = 13;
188 
189 	pkt.timer_policy.hours_to_use_traffic_profile[0] = 8;
190 	pkt.timer_policy.hours_to_use_traffic_profile[1] = 10;
191 
192 	pkt.workload = LOW;
193 	pkt.policy_to_use = TIME;
194 	pkt.command = PKT_POLICY;
195 	strcpy(pkt.vm_name, "ubuntu2");
196 	ret = rte_power_guest_channel_send_msg(&pkt, 1);
197 	if (ret == 0)
198 		return 1;
199 	RTE_LOG(DEBUG, POWER, "Error sending message: %s\n",
200 			ret > 0 ? strerror(ret) : "channel not connected");
201 	return -1;
202 }
203 
204 static void
205 cmd_send_policy_parsed(void *parsed_result, struct cmdline *cl,
206 		       __attribute__((unused)) void *data)
207 {
208 	int ret = -1;
209 	struct cmd_send_policy_result *res = parsed_result;
210 
211 	if (!strcmp(res->cmd, "now")) {
212 		printf("Sending Policy down now!\n");
213 		ret = send_policy();
214 	}
215 	if (ret != 1)
216 		cmdline_printf(cl, "Error sending message: %s\n",
217 				strerror(ret));
218 }
219 
220 cmdline_parse_token_string_t cmd_send_policy =
221 	TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
222 			send_policy, "send_policy");
223 cmdline_parse_token_string_t cmd_send_policy_cmd_cmd =
224 	TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
225 			cmd, "now");
226 
227 cmdline_parse_inst_t cmd_send_policy_set = {
228 	.f = cmd_send_policy_parsed,
229 	.data = NULL,
230 	.help_str = "send_policy now",
231 	.tokens = {
232 		(void *)&cmd_send_policy,
233 		(void *)&cmd_send_policy_cmd_cmd,
234 		NULL,
235 	},
236 };
237 
238 cmdline_parse_ctx_t main_ctx[] = {
239 		(cmdline_parse_inst_t *)&cmd_quit,
240 		(cmdline_parse_inst_t *)&cmd_send_policy_set,
241 		(cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
242 		NULL,
243 };
244 
245 void
246 run_cli(__attribute__((unused)) void *arg)
247 {
248 	struct cmdline *cl;
249 
250 	cl = cmdline_stdin_new(main_ctx, "vmpower(guest)> ");
251 	if (cl == NULL)
252 		return;
253 
254 	cmdline_interact(cl);
255 	cmdline_stdin_exit(cl);
256 }
257