1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <cmdline_rdline.h>
14 #include <cmdline_parse.h>
15 #include <cmdline_parse_string.h>
16 #include <cmdline_parse_num.h>
17 #include <cmdline_socket.h>
20 #include "vm_power_cli.h"
21 #include "channel_manager.h"
22 #include "channel_monitor.h"
23 #include "power_manager.h"
25 struct cmd_quit_result {
26 cmdline_fixed_string_t quit;
29 static void cmd_quit_parsed(__rte_unused void *parsed_result,
31 __rte_unused void *data)
33 channel_monitor_exit();
34 channel_manager_exit();
39 cmdline_parse_token_string_t cmd_quit_quit =
40 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
42 cmdline_parse_inst_t cmd_quit = {
43 .f = cmd_quit_parsed, /* function to call */
44 .data = NULL, /* 2nd arg of func */
45 .help_str = "close the application",
46 .tokens = { /* token list, NULL terminated */
47 (void *)&cmd_quit_quit,
52 /* *** VM operations *** */
53 struct cmd_show_vm_result {
54 cmdline_fixed_string_t show_vm;
55 cmdline_fixed_string_t vm_name;
59 cmd_show_vm_parsed(void *parsed_result, struct cmdline *cl,
60 __rte_unused void *data)
62 struct cmd_show_vm_result *res = parsed_result;
66 if (get_info_vm(res->vm_name, &info) != 0)
68 cmdline_printf(cl, "VM: '%s', status = ", info.name);
69 if (info.status == CHANNEL_MGR_VM_ACTIVE)
70 cmdline_printf(cl, "ACTIVE\n");
72 cmdline_printf(cl, "INACTIVE\n");
73 cmdline_printf(cl, "Channels %u\n", info.num_channels);
74 for (i = 0; i < info.num_channels; i++) {
75 cmdline_printf(cl, " [%u]: %s, status = ", i,
76 info.channels[i].channel_path);
77 switch (info.channels[i].status) {
78 case CHANNEL_MGR_CHANNEL_CONNECTED:
79 cmdline_printf(cl, "CONNECTED\n");
81 case CHANNEL_MGR_CHANNEL_DISCONNECTED:
82 cmdline_printf(cl, "DISCONNECTED\n");
84 case CHANNEL_MGR_CHANNEL_DISABLED:
85 cmdline_printf(cl, "DISABLED\n");
87 case CHANNEL_MGR_CHANNEL_PROCESSING:
88 cmdline_printf(cl, "PROCESSING\n");
91 cmdline_printf(cl, "UNKNOWN\n");
95 cmdline_printf(cl, "Virtual CPU(s): %u\n", info.num_vcpus);
96 for (i = 0; i < info.num_vcpus; i++) {
97 cmdline_printf(cl, " [%u]: Physical CPU %d\n", i,
104 cmdline_parse_token_string_t cmd_vm_show =
105 TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result,
107 cmdline_parse_token_string_t cmd_show_vm_name =
108 TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result,
111 cmdline_parse_inst_t cmd_show_vm_set = {
112 .f = cmd_show_vm_parsed,
114 .help_str = "show_vm <vm_name>, prints the information on the "
115 "specified VM(s), the information lists the number of vCPUS, the "
116 "pinning to pCPU(s) as a bit mask, along with any communication "
117 "channels associated with each VM",
119 (void *)&cmd_vm_show,
120 (void *)&cmd_show_vm_name,
125 /* *** vCPU to pCPU mapping operations *** */
128 struct cmd_set_pcpu_result {
129 cmdline_fixed_string_t set_pcpu;
130 cmdline_fixed_string_t vm_name;
136 cmd_set_pcpu_parsed(void *parsed_result, struct cmdline *cl,
137 __rte_unused void *data)
139 struct cmd_set_pcpu_result *res = parsed_result;
141 if (set_pcpu(res->vm_name, res->vcpu, res->core) == 0)
142 cmdline_printf(cl, "Pinned vCPU(%"PRId8") to pCPU core "
143 "%"PRId8")\n", res->vcpu, res->core);
145 cmdline_printf(cl, "Unable to pin vCPU(%"PRId8") to pCPU core "
146 "%"PRId8")\n", res->vcpu, res->core);
149 cmdline_parse_token_string_t cmd_set_pcpu =
150 TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result,
151 set_pcpu, "set_pcpu");
152 cmdline_parse_token_string_t cmd_set_pcpu_vm_name =
153 TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result,
155 cmdline_parse_token_num_t set_pcpu_vcpu =
156 TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result,
158 cmdline_parse_token_num_t set_pcpu_core =
159 TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result,
163 cmdline_parse_inst_t cmd_set_pcpu_set = {
164 .f = cmd_set_pcpu_parsed,
166 .help_str = "set_pcpu <vm_name> <vcpu> <pcpu>, Set the binding "
167 "of Virtual CPU on VM to the Physical CPU.",
169 (void *)&cmd_set_pcpu,
170 (void *)&cmd_set_pcpu_vm_name,
171 (void *)&set_pcpu_vcpu,
172 (void *)&set_pcpu_core,
177 struct cmd_vm_op_result {
178 cmdline_fixed_string_t op_vm;
179 cmdline_fixed_string_t vm_name;
183 cmd_vm_op_parsed(void *parsed_result, struct cmdline *cl,
184 __rte_unused void *data)
186 struct cmd_vm_op_result *res = parsed_result;
188 if (!strcmp(res->op_vm, "add_vm")) {
189 if (add_vm(res->vm_name) < 0)
190 cmdline_printf(cl, "Unable to add VM '%s'\n", res->vm_name);
191 } else if (remove_vm(res->vm_name) < 0)
192 cmdline_printf(cl, "Unable to remove VM '%s'\n", res->vm_name);
195 cmdline_parse_token_string_t cmd_vm_op =
196 TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result,
197 op_vm, "add_vm#rm_vm");
198 cmdline_parse_token_string_t cmd_vm_name =
199 TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result,
202 cmdline_parse_inst_t cmd_vm_op_set = {
203 .f = cmd_vm_op_parsed,
205 .help_str = "add_vm|rm_vm <name>, add a VM for "
206 "subsequent operations with the CLI or remove a previously added "
207 "VM from the VM Power Manager",
210 (void *)&cmd_vm_name,
215 /* *** VM channel operations *** */
216 struct cmd_channels_op_result {
217 cmdline_fixed_string_t op;
218 cmdline_fixed_string_t vm_name;
219 cmdline_fixed_string_t channel_list;
222 cmd_channels_op_parsed(void *parsed_result, struct cmdline *cl,
223 __rte_unused void *data)
225 unsigned num_channels = 0, channel_num, i;
227 unsigned int channel_list[RTE_MAX_LCORE];
228 char *token, *remaining, *tail_ptr;
229 struct cmd_channels_op_result *res = parsed_result;
231 if (!strcmp(res->channel_list, "all")) {
232 channels_added = add_all_channels(res->vm_name);
233 cmdline_printf(cl, "Added %d channels for VM '%s'\n",
234 channels_added, res->vm_name);
238 remaining = res->channel_list;
240 if (remaining == NULL || remaining[0] == '\0')
243 token = strsep(&remaining, ",");
247 channel_num = (unsigned)strtol(token, &tail_ptr, 10);
248 if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
251 if (channel_num == RTE_MAX_LCORE) {
252 cmdline_printf(cl, "Channel number '%u' exceeds the maximum number "
253 "of allowable channels(%u) for VM '%s'\n", channel_num,
254 RTE_MAX_LCORE, res->vm_name);
257 channel_list[num_channels++] = channel_num;
259 for (i = 0; i < num_channels; i++)
260 cmdline_printf(cl, "[%u]: Adding channel %u\n", i, channel_list[i]);
262 channels_added = add_channels(res->vm_name, channel_list,
264 cmdline_printf(cl, "Enabled %d channels for '%s'\n", channels_added,
268 cmdline_parse_token_string_t cmd_channels_op =
269 TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
271 cmdline_parse_token_string_t cmd_channels_vm_name =
272 TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
274 cmdline_parse_token_string_t cmd_channels_list =
275 TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
278 cmdline_parse_inst_t cmd_channels_op_set = {
279 .f = cmd_channels_op_parsed,
281 .help_str = "add_channels <vm_name> <list>|all, add "
282 "communication channels for the specified VM, the "
283 "virtio channels must be enabled in the VM "
284 "configuration(qemu/libvirt) and the associated VM must be active. "
285 "<list> is a comma-separated list of channel numbers to add, using "
286 "the keyword 'all' will attempt to add all channels for the VM",
288 (void *)&cmd_channels_op,
289 (void *)&cmd_channels_vm_name,
290 (void *)&cmd_channels_list,
295 struct cmd_set_query_result {
296 cmdline_fixed_string_t set_query;
297 cmdline_fixed_string_t vm_name;
298 cmdline_fixed_string_t query_status;
302 cmd_set_query_parsed(void *parsed_result,
303 __rte_unused struct cmdline *cl,
304 __rte_unused void *data)
306 struct cmd_set_query_result *res = parsed_result;
308 if (!strcmp(res->query_status, "enable")) {
309 if (set_query_status(res->vm_name, true) < 0)
310 cmdline_printf(cl, "Unable to allow query for VM '%s'\n",
312 } else if (!strcmp(res->query_status, "disable")) {
313 if (set_query_status(res->vm_name, false) < 0)
314 cmdline_printf(cl, "Unable to disallow query for VM '%s'\n",
319 cmdline_parse_token_string_t cmd_set_query =
320 TOKEN_STRING_INITIALIZER(struct cmd_set_query_result,
321 set_query, "set_query");
322 cmdline_parse_token_string_t cmd_set_query_vm_name =
323 TOKEN_STRING_INITIALIZER(struct cmd_set_query_result,
325 cmdline_parse_token_string_t cmd_set_query_status =
326 TOKEN_STRING_INITIALIZER(struct cmd_set_query_result,
327 query_status, "enable#disable");
329 cmdline_parse_inst_t cmd_set_query_set = {
330 .f = cmd_set_query_parsed,
332 .help_str = "set_query <vm_name> <enable|disable>, allow or disallow queries"
333 " for the specified VM",
335 (void *)&cmd_set_query,
336 (void *)&cmd_set_query_vm_name,
337 (void *)&cmd_set_query_status,
342 struct cmd_channels_status_op_result {
343 cmdline_fixed_string_t op;
344 cmdline_fixed_string_t vm_name;
345 cmdline_fixed_string_t channel_list;
346 cmdline_fixed_string_t status;
350 cmd_channels_status_op_parsed(void *parsed_result, struct cmdline *cl,
351 __rte_unused void *data)
353 unsigned num_channels = 0, channel_num;
355 unsigned int channel_list[RTE_MAX_LCORE];
356 char *token, *remaining, *tail_ptr;
357 struct cmd_channels_status_op_result *res = parsed_result;
358 enum channel_status status;
360 if (!strcmp(res->status, "enabled"))
361 status = CHANNEL_MGR_CHANNEL_CONNECTED;
363 status = CHANNEL_MGR_CHANNEL_DISABLED;
365 if (!strcmp(res->channel_list, "all")) {
366 changed = set_channel_status_all(res->vm_name, status);
367 cmdline_printf(cl, "Updated status of %d channels "
368 "for VM '%s'\n", changed, res->vm_name);
371 remaining = res->channel_list;
373 if (remaining == NULL || remaining[0] == '\0')
375 token = strsep(&remaining, ",");
379 channel_num = (unsigned)strtol(token, &tail_ptr, 10);
380 if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
383 if (channel_num == RTE_MAX_LCORE) {
384 cmdline_printf(cl, "%u exceeds the maximum number of allowable "
385 "channels(%u) for VM '%s'\n", channel_num,
386 RTE_MAX_LCORE, res->vm_name);
389 channel_list[num_channels++] = channel_num;
391 changed = set_channel_status(res->vm_name, channel_list, num_channels,
393 cmdline_printf(cl, "Updated status of %d channels "
394 "for VM '%s'\n", changed, res->vm_name);
397 cmdline_parse_token_string_t cmd_channels_status_op =
398 TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
399 op, "set_channel_status");
400 cmdline_parse_token_string_t cmd_channels_status_vm_name =
401 TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
403 cmdline_parse_token_string_t cmd_channels_status_list =
404 TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
406 cmdline_parse_token_string_t cmd_channels_status =
407 TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
408 status, "enabled#disabled");
410 cmdline_parse_inst_t cmd_channels_status_op_set = {
411 .f = cmd_channels_status_op_parsed,
413 .help_str = "set_channel_status <vm_name> <list>|all enabled|disabled, "
414 " enable or disable the communication channels in "
415 "list(comma-separated) for the specified VM, alternatively "
416 "list can be replaced with keyword 'all'. "
417 "Disabled channels will still receive packets on the host, "
418 "however the commands they specify will be ignored. "
419 "Set status to 'enabled' to begin processing requests again.",
421 (void *)&cmd_channels_status_op,
422 (void *)&cmd_channels_status_vm_name,
423 (void *)&cmd_channels_status_list,
424 (void *)&cmd_channels_status,
429 /* *** CPU Frequency operations *** */
430 struct cmd_show_cpu_freq_result {
431 cmdline_fixed_string_t show_cpu_freq;
436 cmd_show_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
437 __rte_unused void *data)
439 struct cmd_show_cpu_freq_result *res = parsed_result;
440 uint32_t curr_freq = power_manager_get_current_frequency(res->core_num);
442 if (curr_freq == 0) {
443 cmdline_printf(cl, "Unable to get frequency for core %u\n",
447 cmdline_printf(cl, "Core %u frequency: %"PRId32"\n", res->core_num,
451 cmdline_parse_token_string_t cmd_show_cpu_freq =
452 TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_result,
453 show_cpu_freq, "show_cpu_freq");
455 cmdline_parse_token_num_t cmd_show_cpu_freq_core_num =
456 TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_result,
457 core_num, RTE_UINT8);
459 cmdline_parse_inst_t cmd_show_cpu_freq_set = {
460 .f = cmd_show_cpu_freq_parsed,
462 .help_str = "Get the current frequency for the specified core",
464 (void *)&cmd_show_cpu_freq,
465 (void *)&cmd_show_cpu_freq_core_num,
470 struct cmd_set_cpu_freq_result {
471 cmdline_fixed_string_t set_cpu_freq;
473 cmdline_fixed_string_t cmd;
477 cmd_set_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
478 __rte_unused void *data)
481 struct cmd_set_cpu_freq_result *res = parsed_result;
483 if (!strcmp(res->cmd , "up"))
484 ret = power_manager_scale_core_up(res->core_num);
485 else if (!strcmp(res->cmd , "down"))
486 ret = power_manager_scale_core_down(res->core_num);
487 else if (!strcmp(res->cmd , "min"))
488 ret = power_manager_scale_core_min(res->core_num);
489 else if (!strcmp(res->cmd , "max"))
490 ret = power_manager_scale_core_max(res->core_num);
491 else if (!strcmp(res->cmd, "enable_turbo"))
492 ret = power_manager_enable_turbo_core(res->core_num);
493 else if (!strcmp(res->cmd, "disable_turbo"))
494 ret = power_manager_disable_turbo_core(res->core_num);
496 cmdline_printf(cl, "Error scaling core(%u) '%s'\n", res->core_num,
501 cmdline_parse_token_string_t cmd_set_cpu_freq =
502 TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
503 set_cpu_freq, "set_cpu_freq");
504 cmdline_parse_token_num_t cmd_set_cpu_freq_core_num =
505 TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result,
506 core_num, RTE_UINT8);
507 cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd =
508 TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
509 cmd, "up#down#min#max#enable_turbo#disable_turbo");
511 cmdline_parse_inst_t cmd_set_cpu_freq_set = {
512 .f = cmd_set_cpu_freq_parsed,
514 .help_str = "set_cpu_freq <core_num> <up|down|min|max|enable_turbo|disable_turbo>, adjust the current "
515 "frequency for the specified core",
517 (void *)&cmd_set_cpu_freq,
518 (void *)&cmd_set_cpu_freq_core_num,
519 (void *)&cmd_set_cpu_freq_cmd_cmd,
524 cmdline_parse_ctx_t main_ctx[] = {
525 (cmdline_parse_inst_t *)&cmd_quit,
526 (cmdline_parse_inst_t *)&cmd_vm_op_set,
527 (cmdline_parse_inst_t *)&cmd_channels_op_set,
528 (cmdline_parse_inst_t *)&cmd_channels_status_op_set,
529 (cmdline_parse_inst_t *)&cmd_show_vm_set,
530 (cmdline_parse_inst_t *)&cmd_show_cpu_freq_set,
531 (cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
532 (cmdline_parse_inst_t *)&cmd_set_pcpu_set,
533 (cmdline_parse_inst_t *)&cmd_set_query_set,
538 run_cli(__rte_unused void *arg)
542 cl = cmdline_stdin_new(main_ctx, "vmpower> ");
546 cmdline_interact(cl);
547 cmdline_stdin_exit(cl);