1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
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>
18 #include <rte_lcore.h>
19 #include <rte_ethdev.h>
21 #include <rte_power.h>
22 #include <guest_channel.h>
24 #include "vm_power_cli_guest.h"
27 #define CHANNEL_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
30 #define RTE_LOGTYPE_GUEST_CLI RTE_LOGTYPE_USER1
32 struct cmd_quit_result {
33 cmdline_fixed_string_t quit;
37 struct rte_ether_addr addr;
41 static struct channel_packet policy;
43 struct channel_packet *
50 set_policy_mac(int port, int idx)
52 struct channel_packet *policy;
56 /* Use port MAC address as the vfid */
57 ret = rte_eth_macaddr_get(port, &pfid.addr);
59 printf("Failed to get device (port %u) MAC address: %s\n",
60 port, rte_strerror(-ret));
64 printf("Port %u MAC: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
65 "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
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;
76 set_policy_defaults(struct channel_packet *pkt)
80 ret = set_policy_mac(0, 0);
84 pkt->nb_mac_to_monitor = 1;
86 pkt->t_boost_status.tbEnabled = false;
88 pkt->vcpu_to_control[0] = 0;
89 pkt->vcpu_to_control[1] = 1;
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;
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;
103 pkt->timer_policy.hours_to_use_traffic_profile[0] = 8;
104 pkt->timer_policy.hours_to_use_traffic_profile[1] = 10;
106 pkt->core_type = CORE_TYPE_VIRTUAL;
108 pkt->policy_to_use = TIME;
109 pkt->command = PKT_POLICY;
110 strlcpy(pkt->vm_name, "ubuntu2", sizeof(pkt->vm_name));
115 static void cmd_quit_parsed(__rte_unused void *parsed_result,
116 __rte_unused struct cmdline *cl,
117 __rte_unused void *data)
121 RTE_LCORE_FOREACH(lcore_id) {
122 rte_power_exit(lcore_id);
127 cmdline_parse_token_string_t cmd_quit_quit =
128 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
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,
140 /* *** VM operations *** */
142 struct cmd_freq_list_result {
143 cmdline_fixed_string_t query_freq;
144 cmdline_fixed_string_t cpu_num;
148 query_freq_list(struct channel_packet *pkt, unsigned int lcore_id)
151 ret = rte_power_guest_channel_send_msg(pkt, lcore_id);
153 RTE_LOG(ERR, GUEST_CLI, "Error sending message.\n");
160 receive_freq_list(struct channel_packet_freq_list *pkt_freq_list,
161 unsigned int lcore_id)
165 ret = rte_power_guest_channel_receive_msg(pkt_freq_list,
166 sizeof(struct channel_packet_freq_list),
169 RTE_LOG(ERR, GUEST_CLI, "Error receiving message.\n");
172 if (pkt_freq_list->command != CPU_POWER_FREQ_LIST) {
173 RTE_LOG(ERR, GUEST_CLI, "Unexpected message received.\n");
180 cmd_query_freq_list_parsed(void *parsed_result,
181 __rte_unused struct cmdline *cl,
182 __rte_unused void *data)
184 struct cmd_freq_list_result *res = parsed_result;
185 unsigned int lcore_id;
186 struct channel_packet_freq_list pkt_freq_list;
187 struct channel_packet pkt;
188 bool query_list = false;
192 memset(&pkt, 0, sizeof(struct channel_packet));
193 memset(&pkt_freq_list, 0, sizeof(struct channel_packet_freq_list));
195 if (!strcmp(res->cpu_num, "all")) {
197 /* Get first enabled lcore. */
198 lcore_id = rte_get_next_lcore(-1,
201 if (lcore_id == RTE_MAX_LCORE) {
202 cmdline_printf(cl, "Enabled core not found.\n");
206 pkt.command = CPU_POWER_QUERY_FREQ_LIST;
207 strlcpy(pkt.vm_name, policy.vm_name, sizeof(pkt.vm_name));
211 lcore_id = (unsigned int)strtol(res->cpu_num, &ep, 10);
212 if (errno != 0 || lcore_id >= MAX_VCPU_PER_VM ||
213 ep == res->cpu_num) {
214 cmdline_printf(cl, "Invalid parameter provided.\n");
217 pkt.command = CPU_POWER_QUERY_FREQ;
218 strlcpy(pkt.vm_name, policy.vm_name, sizeof(pkt.vm_name));
219 pkt.resource_id = lcore_id;
222 ret = query_freq_list(&pkt, lcore_id);
224 cmdline_printf(cl, "Error during sending frequency list query.\n");
228 ret = receive_freq_list(&pkt_freq_list, lcore_id);
230 cmdline_printf(cl, "Error during frequency list reception.\n");
235 for (i = 0; i < pkt_freq_list.num_vcpu; ++i)
236 cmdline_printf(cl, "Frequency of [%d] vcore is %d.\n",
238 pkt_freq_list.freq_list[i]);
240 cmdline_printf(cl, "Frequency of [%d] vcore is %d.\n",
242 pkt_freq_list.freq_list[lcore_id]);
246 cmdline_parse_token_string_t cmd_query_freq_token =
247 TOKEN_STRING_INITIALIZER(struct cmd_freq_list_result, query_freq, "query_cpu_freq");
248 cmdline_parse_token_string_t cmd_query_freq_cpu_num_token =
249 TOKEN_STRING_INITIALIZER(struct cmd_freq_list_result, cpu_num, NULL);
251 cmdline_parse_inst_t cmd_query_freq_list = {
252 .f = cmd_query_freq_list_parsed, /* function to call */
253 .data = NULL, /* 2nd arg of func */
254 .help_str = "query_cpu_freq <core_num>|all, request"
255 " information regarding virtual core frequencies."
256 " The keyword 'all' will query list of all vcores for the VM",
257 .tokens = { /* token list, NULL terminated */
258 (void *)&cmd_query_freq_token,
259 (void *)&cmd_query_freq_cpu_num_token,
265 check_response_cmd(unsigned int lcore_id, int *result)
267 struct channel_packet pkt;
270 ret = rte_power_guest_channel_receive_msg(&pkt, sizeof pkt, lcore_id);
274 switch (pkt.command) {
275 case(CPU_POWER_CMD_ACK):
278 case(CPU_POWER_CMD_NACK):
282 RTE_LOG(ERR, GUEST_CLI,
283 "Received invalid response from host, expecting ACK/NACK.\n");
290 struct cmd_set_cpu_freq_result {
291 cmdline_fixed_string_t set_cpu_freq;
293 cmdline_fixed_string_t cmd;
297 cmd_set_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
298 __rte_unused void *data)
301 struct cmd_set_cpu_freq_result *res = parsed_result;
303 if (!strcmp(res->cmd, "up"))
304 ret = rte_power_freq_up(res->lcore_id);
305 else if (!strcmp(res->cmd, "down"))
306 ret = rte_power_freq_down(res->lcore_id);
307 else if (!strcmp(res->cmd, "min"))
308 ret = rte_power_freq_min(res->lcore_id);
309 else if (!strcmp(res->cmd, "max"))
310 ret = rte_power_freq_max(res->lcore_id);
311 else if (!strcmp(res->cmd, "enable_turbo"))
312 ret = rte_power_freq_enable_turbo(res->lcore_id);
313 else if (!strcmp(res->cmd, "disable_turbo"))
314 ret = rte_power_freq_disable_turbo(res->lcore_id);
317 cmdline_printf(cl, "Error sending message: %s\n", strerror(ret));
321 ret = check_response_cmd(res->lcore_id, &result);
323 RTE_LOG(ERR, GUEST_CLI, "No confirmation for sent message received\n");
325 cmdline_printf(cl, "%s received for message sent to host.\n",
326 result == 1 ? "ACK" : "NACK");
330 cmdline_parse_token_string_t cmd_set_cpu_freq =
331 TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
332 set_cpu_freq, "set_cpu_freq");
333 cmdline_parse_token_num_t cmd_set_cpu_freq_core_num =
334 TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result,
336 cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd =
337 TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
338 cmd, "up#down#min#max#enable_turbo#disable_turbo");
340 cmdline_parse_inst_t cmd_set_cpu_freq_set = {
341 .f = cmd_set_cpu_freq_parsed,
343 .help_str = "set_cpu_freq <core_num> "
344 "<up|down|min|max|enable_turbo|disable_turbo>, "
345 "adjust the frequency for the specified core.",
347 (void *)&cmd_set_cpu_freq,
348 (void *)&cmd_set_cpu_freq_core_num,
349 (void *)&cmd_set_cpu_freq_cmd_cmd,
354 struct cmd_send_policy_result {
355 cmdline_fixed_string_t send_policy;
356 cmdline_fixed_string_t cmd;
360 send_policy(struct channel_packet *pkt, struct cmdline *cl)
364 ret = rte_power_guest_channel_send_msg(pkt, 1);
366 RTE_LOG(ERR, GUEST_CLI, "Error sending message: %s\n",
367 ret > 0 ? strerror(ret) : "channel not connected");
372 ret = check_response_cmd(1, &result);
374 RTE_LOG(ERR, GUEST_CLI, "No confirmation for sent policy received\n");
376 cmdline_printf(cl, "%s for sent policy received.\n",
377 result == 1 ? "ACK" : "NACK");
383 cmd_send_policy_parsed(void *parsed_result, struct cmdline *cl,
384 __rte_unused void *data)
387 struct cmd_send_policy_result *res = parsed_result;
389 if (!strcmp(res->cmd, "now")) {
390 printf("Sending Policy down now!\n");
391 ret = send_policy(&policy, cl);
394 cmdline_printf(cl, "Error sending message: %s\n",
398 cmdline_parse_token_string_t cmd_send_policy =
399 TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
400 send_policy, "send_policy");
401 cmdline_parse_token_string_t cmd_send_policy_cmd_cmd =
402 TOKEN_STRING_INITIALIZER(struct cmd_send_policy_result,
405 cmdline_parse_inst_t cmd_send_policy_set = {
406 .f = cmd_send_policy_parsed,
408 .help_str = "send_policy now",
410 (void *)&cmd_send_policy,
411 (void *)&cmd_send_policy_cmd_cmd,
416 cmdline_parse_ctx_t main_ctx[] = {
417 (cmdline_parse_inst_t *)&cmd_quit,
418 (cmdline_parse_inst_t *)&cmd_send_policy_set,
419 (cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
420 (cmdline_parse_inst_t *)&cmd_query_freq_list,
425 run_cli(__rte_unused void *arg)
429 cl = cmdline_stdin_new(main_ctx, "vmpower(guest)> ");
433 cmdline_interact(cl);
434 cmdline_stdin_exit(cl);