xref: /dpdk/examples/vm_power_manager/guest_cli/vm_power_cli_guest.c (revision 5c451eb543fab68bb40ea6d0ed4090b14f0973e4)
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 
49 #include <rte_power.h>
50 
51 #include "vm_power_cli_guest.h"
52 
53 
54 #define CHANNEL_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
55 
56 
57 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
58 
59 struct cmd_quit_result {
60 	cmdline_fixed_string_t quit;
61 };
62 
63 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
64 				__attribute__((unused)) struct cmdline *cl,
65 			    __attribute__((unused)) void *data)
66 {
67 	unsigned lcore_id;
68 
69 	RTE_LCORE_FOREACH(lcore_id) {
70 		rte_power_exit(lcore_id);
71 	}
72 	cmdline_quit(cl);
73 }
74 
75 cmdline_parse_token_string_t cmd_quit_quit =
76 	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
77 
78 cmdline_parse_inst_t cmd_quit = {
79 	.f = cmd_quit_parsed,  /* function to call */
80 	.data = NULL,      /* 2nd arg of func */
81 	.help_str = "close the application",
82 	.tokens = {        /* token list, NULL terminated */
83 		(void *)&cmd_quit_quit,
84 		NULL,
85 	},
86 };
87 
88 /* *** VM operations *** */
89 
90 struct cmd_set_cpu_freq_result {
91 	cmdline_fixed_string_t set_cpu_freq;
92 	uint8_t lcore_id;
93 	cmdline_fixed_string_t cmd;
94 };
95 
96 static void
97 cmd_set_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
98 		       __attribute__((unused)) void *data)
99 {
100 	int ret = -1;
101 	struct cmd_set_cpu_freq_result *res = parsed_result;
102 
103 	if (!strcmp(res->cmd , "up"))
104 		ret = rte_power_freq_up(res->lcore_id);
105 	else if (!strcmp(res->cmd , "down"))
106 		ret = rte_power_freq_down(res->lcore_id);
107 	else if (!strcmp(res->cmd , "min"))
108 		ret = rte_power_freq_min(res->lcore_id);
109 	else if (!strcmp(res->cmd , "max"))
110 		ret = rte_power_freq_max(res->lcore_id);
111 	else if (!strcmp(res->cmd, "enable_turbo"))
112 		ret = rte_power_freq_enable_turbo(res->lcore_id);
113 	else if (!strcmp(res->cmd, "disable_turbo"))
114 		ret = rte_power_freq_disable_turbo(res->lcore_id);
115 	if (ret != 1)
116 		cmdline_printf(cl, "Error sending message: %s\n", strerror(ret));
117 }
118 
119 cmdline_parse_token_string_t cmd_set_cpu_freq =
120 	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
121 			set_cpu_freq, "set_cpu_freq");
122 cmdline_parse_token_string_t cmd_set_cpu_freq_core_num =
123 	TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result,
124 			lcore_id, UINT8);
125 cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd =
126 	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
127 			cmd, "up#down#min#max#enable_turbo#disable_turbo");
128 
129 cmdline_parse_inst_t cmd_set_cpu_freq_set = {
130 	.f = cmd_set_cpu_freq_parsed,
131 	.data = NULL,
132 	.help_str = "set_cpu_freq <core_num> <up|down|min|max>, Set the current "
133 			"frequency for the specified core by scaling up/down/min/max",
134 	.tokens = {
135 		(void *)&cmd_set_cpu_freq,
136 		(void *)&cmd_set_cpu_freq_core_num,
137 		(void *)&cmd_set_cpu_freq_cmd_cmd,
138 		NULL,
139 	},
140 };
141 
142 cmdline_parse_ctx_t main_ctx[] = {
143 		(cmdline_parse_inst_t *)&cmd_quit,
144 		(cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
145 		NULL,
146 };
147 
148 void
149 run_cli(__attribute__((unused)) void *arg)
150 {
151 	struct cmdline *cl;
152 
153 	cl = cmdline_stdin_new(main_ctx, "vmpower(guest)> ");
154 	if (cl == NULL)
155 		return;
156 
157 	cmdline_interact(cl);
158 	cmdline_stdin_exit(cl);
159 }
160