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