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