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