1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #ifndef CHANNEL_MANAGER_H_ 6 #define CHANNEL_MANAGER_H_ 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #include <linux/limits.h> 13 #include <linux/un.h> 14 #include <stdbool.h> 15 16 /* Maximum name length including '\0' terminator */ 17 #define CHANNEL_MGR_MAX_NAME_LEN 64 18 19 /* Hypervisor Path for libvirt(qemu/KVM) */ 20 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system" 21 22 /* File socket directory */ 23 #define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/" 24 25 /* FIFO file name template */ 26 #define CHANNEL_MGR_FIFO_PATTERN_NAME "fifo" 27 28 #define MAX_CLIENTS 64 29 #define MAX_VCPUS 20 30 31 32 struct libvirt_vm_info { 33 const char *vm_name; 34 unsigned int pcpus[MAX_VCPUS]; 35 uint8_t num_cpus; 36 }; 37 38 extern struct libvirt_vm_info lvm_info[MAX_CLIENTS]; 39 /* Communication Channel Status */ 40 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0, 41 CHANNEL_MGR_CHANNEL_CONNECTED, 42 CHANNEL_MGR_CHANNEL_DISABLED, 43 CHANNEL_MGR_CHANNEL_PROCESSING}; 44 45 /* Communication Channel Type */ 46 enum channel_type { 47 CHANNEL_TYPE_BINARY = 0, 48 CHANNEL_TYPE_INI, 49 CHANNEL_TYPE_JSON 50 }; 51 52 /* VM libvirt(qemu/KVM) connection status */ 53 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE}; 54 55 /* 56 * Represents a single and exclusive VM channel that exists between a guest and 57 * the host. 58 */ 59 struct channel_info { 60 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */ 61 volatile uint32_t status; /**< Connection status(enum channel_status) */ 62 int fd; /**< AF_UNIX socket fd */ 63 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */ 64 enum channel_type type; /**< Binary, ini, json, etc. */ 65 void *priv_info; /**< Pointer to private info, do not modify */ 66 }; 67 68 /* Represents a single VM instance used to return internal information about 69 * a VM */ 70 struct vm_info { 71 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */ 72 enum vm_status status; /**< libvirt status */ 73 uint16_t pcpu_map[RTE_MAX_LCORE]; /**< pCPU map to vCPU */ 74 unsigned num_vcpus; /**< number of vCPUS */ 75 struct channel_info channels[RTE_MAX_LCORE]; /**< channel_info array */ 76 unsigned num_channels; /**< Number of channels */ 77 int allow_query; /**< is query allowed */ 78 }; 79 80 /** 81 * Initialize the Channel Manager resources and connect to the Hypervisor 82 * specified in path. 83 * This must be successfully called first before calling any other functions. 84 * It must only be call once; 85 * 86 * @param path 87 * Must be a local path, e.g. qemu:///system. 88 * 89 * @return 90 * - 0 on success. 91 * - Negative on error. 92 */ 93 int channel_manager_init(const char *path); 94 95 /** 96 * Free resources associated with the Channel Manager. 97 * 98 * @param path 99 * Must be a local path, e.g. qemu:///system. 100 * 101 * @return 102 * None 103 */ 104 void channel_manager_exit(void); 105 106 /** 107 * Get the Physical CPU for VM lcore channel(vcpu). 108 * It is not thread-safe. 109 * 110 * @param chan_info 111 * Pointer to struct channel_info 112 * 113 * @param vcpu 114 * The virtual CPU to query. 115 * 116 * 117 * @return 118 * - 0 on error. 119 * - >0 on success. 120 */ 121 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu); 122 123 /** 124 * Set the Physical CPU for the specified vCPU. 125 * It is not thread-safe. 126 * 127 * @param name 128 * Virtual Machine name to lookup 129 * 130 * @param vcpu 131 * The virtual CPU to set. 132 * 133 * @param core_num 134 * The core number of the physical CPU(s) to bind the vCPU 135 * 136 * @return 137 * - 0 on success. 138 * - Negative on error. 139 */ 140 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu); 141 142 /** 143 * Allow or disallow queries for specified VM. 144 * It is thread-safe. 145 * 146 * @param name 147 * Virtual Machine name to lookup. 148 * 149 * @param allow_query 150 * Query status to be set. 151 * 152 * @return 153 * - 0 on success. 154 * - Negative on error. 155 */ 156 int set_query_status(char *vm_name, bool allow_query); 157 158 /** 159 * Add a VM as specified by name to the Channel Manager. The name must 160 * correspond to a valid libvirt domain name. 161 * This is required prior to adding channels. 162 * It is not thread-safe. 163 * 164 * @param name 165 * Virtual Machine name to lookup. 166 * 167 * @return 168 * - 0 on success. 169 * - Negative on error. 170 */ 171 int add_vm(const char *name); 172 173 /** 174 * Remove a previously added Virtual Machine from the Channel Manager 175 * It is not thread-safe. 176 * 177 * @param name 178 * Virtual Machine name to lookup. 179 * 180 * @return 181 * - 0 on success. 182 * - Negative on error. 183 */ 184 int remove_vm(const char *name); 185 186 /** 187 * Add all available channels to the VM as specified by name. 188 * Channels in the form of paths 189 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. 190 * It is not thread-safe. 191 * 192 * @param name 193 * Virtual Machine name to lookup. 194 * 195 * @return 196 * - N the number of channels added for the VM 197 */ 198 int add_all_channels(const char *vm_name); 199 200 /** 201 * Add the channel numbers in channel_list to the domain specified by name. 202 * Channels in the form of paths 203 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. 204 * It is not thread-safe. 205 * 206 * @param name 207 * Virtual Machine name to add channels. 208 * 209 * @param channel_list 210 * Pointer to list of unsigned integers, representing the channel number to add 211 * It must be allocated outside of this function. 212 * 213 * @param num_channels 214 * The amount of channel numbers in channel_list 215 * 216 * @return 217 * - N the number of channels added for the VM 218 * - 0 for error 219 */ 220 int add_channels(const char *vm_name, unsigned *channel_list, 221 unsigned num_channels); 222 223 /** 224 * Set up fifos by which host applications can send command an policies 225 * through a fifo to the vm_power_manager 226 * 227 * @return 228 * - 0 for success 229 */ 230 int add_host_channels(void); 231 232 /** 233 * Remove a channel definition from the channel manager. This must only be 234 * called from the channel monitor thread. 235 * 236 * @param chan_info 237 * Pointer to a valid struct channel_info. 238 * 239 * @return 240 * - 0 on success. 241 * - Negative on error. 242 */ 243 int remove_channel(struct channel_info **chan_info_dptr); 244 245 /** 246 * For all channels associated with a Virtual Machine name, update the 247 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or 248 * CHANNEL_MGR_CHANNEL_DISABLED only. 249 * 250 * 251 * @param name 252 * Virtual Machine name to modify all channels. 253 * 254 * @param status 255 * The status to set each channel 256 * 257 * @param num_channels 258 * The amount of channel numbers in channel_list 259 * 260 * @return 261 * - N the number of channels added for the VM 262 * - 0 for error 263 */ 264 int set_channel_status_all(const char *name, enum channel_status status); 265 266 /** 267 * For all channels in channel_list associated with a Virtual Machine name 268 * update the connection status of each. 269 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or 270 * CHANNEL_MGR_CHANNEL_DISABLED only. 271 * It is not thread-safe. 272 * 273 * @param name 274 * Virtual Machine name to add channels. 275 * 276 * @param channel_list 277 * Pointer to list of unsigned integers, representing the channel numbers to 278 * modify. 279 * It must be allocated outside of this function. 280 * 281 * @param num_channels 282 * The amount of channel numbers in channel_list 283 * 284 * @return 285 * - N the number of channels modified for the VM 286 * - 0 for error 287 */ 288 int set_channel_status(const char *vm_name, unsigned *channel_list, 289 unsigned len_channel_list, enum channel_status status); 290 291 /** 292 * Populates a pointer to struct vm_info associated with vm_name. 293 * 294 * @param vm_name 295 * The name of the virtual machine to lookup. 296 * 297 * @param vm_info 298 * Pointer to a struct vm_info, this must be allocated prior to calling this 299 * function. 300 * 301 * @return 302 * - 0 on success. 303 * - Negative on error. 304 */ 305 int get_info_vm(const char *vm_name, struct vm_info *info); 306 307 /** 308 * Populates a table with all domains running and their physical cpu. 309 * All information is gathered through libvirt api. 310 * 311 * @param num_vm 312 * modified to store number of active VMs 313 * 314 * @param num_vcpu 315 modified to store number of vcpus active 316 * 317 * @return 318 * void 319 */ 320 void get_all_vm(int *num_vm, int *num_vcpu); 321 #ifdef __cplusplus 322 } 323 #endif 324 325 #endif /* CHANNEL_MANAGER_H_ */ 326