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