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 <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 #define MAX_CLIENTS 64 30 #define MAX_VCPUS 20 31 32 33 struct libvirt_vm_info { 34 const char *vm_name; 35 unsigned int pcpus[MAX_VCPUS]; 36 uint8_t num_cpus; 37 }; 38 39 extern struct libvirt_vm_info lvm_info[MAX_CLIENTS]; 40 /* Communication Channel Status */ 41 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0, 42 CHANNEL_MGR_CHANNEL_CONNECTED, 43 CHANNEL_MGR_CHANNEL_DISABLED, 44 CHANNEL_MGR_CHANNEL_PROCESSING}; 45 46 /* Communication Channel Type */ 47 enum channel_type { 48 CHANNEL_TYPE_BINARY = 0, 49 CHANNEL_TYPE_INI, 50 CHANNEL_TYPE_JSON 51 }; 52 53 /* VM libvirt(qemu/KVM) connection status */ 54 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE}; 55 56 /* 57 * Represents a single and exclusive VM channel that exists between a guest and 58 * the host. 59 */ 60 struct channel_info { 61 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */ 62 volatile uint32_t status; /**< Connection status(enum channel_status) */ 63 int fd; /**< AF_UNIX socket fd */ 64 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */ 65 enum channel_type type; /**< Binary, ini, json, etc. */ 66 void *priv_info; /**< Pointer to private info, do not modify */ 67 }; 68 69 /* Represents a single VM instance used to return internal information about 70 * a VM */ 71 struct vm_info { 72 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */ 73 enum vm_status status; /**< libvirt status */ 74 uint16_t pcpu_map[RTE_MAX_LCORE]; /**< pCPU map to vCPU */ 75 unsigned num_vcpus; /**< number of vCPUS */ 76 struct channel_info channels[RTE_MAX_LCORE]; /**< channel_info array */ 77 unsigned num_channels; /**< Number of channels */ 78 int allow_query; /**< is query allowed */ 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 * Allow or disallow queries for specified VM. 145 * It is thread-safe. 146 * 147 * @param name 148 * Virtual Machine name to lookup. 149 * 150 * @param allow_query 151 * Query status to be set. 152 * 153 * @return 154 * - 0 on success. 155 * - Negative on error. 156 */ 157 int set_query_status(char *vm_name, bool allow_query); 158 159 /** 160 * Add a VM as specified by name to the Channel Manager. The name must 161 * correspond to a valid libvirt domain name. 162 * This is required prior to adding channels. 163 * It is not thread-safe. 164 * 165 * @param name 166 * Virtual Machine name to lookup. 167 * 168 * @return 169 * - 0 on success. 170 * - Negative on error. 171 */ 172 int add_vm(const char *name); 173 174 /** 175 * Remove a previously added Virtual Machine from the Channel Manager 176 * It is not thread-safe. 177 * 178 * @param name 179 * Virtual Machine name to lookup. 180 * 181 * @return 182 * - 0 on success. 183 * - Negative on error. 184 */ 185 int remove_vm(const char *name); 186 187 /** 188 * Add all available channels to the VM as specified by name. 189 * Channels in the form of paths 190 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. 191 * It is not thread-safe. 192 * 193 * @param name 194 * Virtual Machine name to lookup. 195 * 196 * @return 197 * - N the number of channels added for the VM 198 */ 199 int add_all_channels(const char *vm_name); 200 201 /** 202 * Add the channel numbers in channel_list to the domain specified by name. 203 * Channels in the form of paths 204 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. 205 * It is not thread-safe. 206 * 207 * @param name 208 * Virtual Machine name to add channels. 209 * 210 * @param channel_list 211 * Pointer to list of unsigned integers, representing the channel number to add 212 * It must be allocated outside of this function. 213 * 214 * @param num_channels 215 * The amount of channel numbers in channel_list 216 * 217 * @return 218 * - N the number of channels added for the VM 219 * - 0 for error 220 */ 221 int add_channels(const char *vm_name, unsigned *channel_list, 222 unsigned num_channels); 223 224 /** 225 * Set up fifos by which host applications can send command an policies 226 * through a fifo to the vm_power_manager 227 * 228 * @return 229 * - 0 for success 230 */ 231 int add_host_channels(void); 232 233 /** 234 * Remove a channel definition from the channel manager. This must only be 235 * called from the channel monitor thread. 236 * 237 * @param chan_info 238 * Pointer to a valid struct channel_info. 239 * 240 * @return 241 * - 0 on success. 242 * - Negative on error. 243 */ 244 int remove_channel(struct channel_info **chan_info_dptr); 245 246 /** 247 * For all channels associated with a Virtual Machine name, update the 248 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or 249 * CHANNEL_MGR_CHANNEL_DISABLED only. 250 * 251 * 252 * @param name 253 * Virtual Machine name to modify all channels. 254 * 255 * @param status 256 * The status to set each channel 257 * 258 * @param num_channels 259 * The amount of channel numbers in channel_list 260 * 261 * @return 262 * - N the number of channels added for the VM 263 * - 0 for error 264 */ 265 int set_channel_status_all(const char *name, enum channel_status status); 266 267 /** 268 * For all channels in channel_list associated with a Virtual Machine name 269 * update the connection status of each. 270 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or 271 * CHANNEL_MGR_CHANNEL_DISABLED only. 272 * It is not thread-safe. 273 * 274 * @param name 275 * Virtual Machine name to add channels. 276 * 277 * @param channel_list 278 * Pointer to list of unsigned integers, representing the channel numbers to 279 * modify. 280 * It must be allocated outside of this function. 281 * 282 * @param num_channels 283 * The amount of channel numbers in channel_list 284 * 285 * @return 286 * - N the number of channels modified for the VM 287 * - 0 for error 288 */ 289 int set_channel_status(const char *vm_name, unsigned *channel_list, 290 unsigned len_channel_list, enum channel_status status); 291 292 /** 293 * Populates a pointer to struct vm_info associated with vm_name. 294 * 295 * @param vm_name 296 * The name of the virtual machine to lookup. 297 * 298 * @param vm_info 299 * Pointer to a struct vm_info, this must be allocated prior to calling this 300 * function. 301 * 302 * @return 303 * - 0 on success. 304 * - Negative on error. 305 */ 306 int get_info_vm(const char *vm_name, struct vm_info *info); 307 308 /** 309 * Populates a table with all domains running and their physical cpu. 310 * All information is gathered through libvirt api. 311 * 312 * @param num_vm 313 * modified to store number of active VMs 314 * 315 * @param num_vcpu 316 modified to store number of vcpus active 317 * 318 * @return 319 * void 320 */ 321 void get_all_vm(int *num_vm, int *num_vcpu); 322 #ifdef __cplusplus 323 } 324 #endif 325 326 #endif /* CHANNEL_MANAGER_H_ */ 327