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