1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef CHANNEL_MANAGER_H_
6 #define CHANNEL_MANAGER_H_
12 #include <linux/limits.h>
14 #include <rte_atomic.h>
16 /* Maximum number of CPUs */
17 #define CHANNEL_CMDS_MAX_CPUS 64
18 #if CHANNEL_CMDS_MAX_CPUS > 64
19 #error Maximum number of cores is 64, overflow is guaranteed to \
20 cause problems with VM Power Management
23 /* Maximum name length including '\0' terminator */
24 #define CHANNEL_MGR_MAX_NAME_LEN 64
26 /* Maximum number of channels to each Virtual Machine */
27 #define CHANNEL_MGR_MAX_CHANNELS 64
29 /* Hypervisor Path for libvirt(qemu/KVM) */
30 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
32 /* File socket directory */
33 #define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/"
36 struct sockaddr_un _sockaddr_un;
37 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
40 #define MAX_CLIENTS 64
44 struct libvirt_vm_info {
46 unsigned int pcpus[MAX_VCPUS];
50 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
51 /* Communication Channel Status */
52 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
53 CHANNEL_MGR_CHANNEL_CONNECTED,
54 CHANNEL_MGR_CHANNEL_DISABLED,
55 CHANNEL_MGR_CHANNEL_PROCESSING};
57 /* Communication Channel Type */
59 CHANNEL_TYPE_BINARY = 0,
64 /* VM libvirt(qemu/KVM) connection status */
65 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
68 * Represents a single and exclusive VM channel that exists between a guest and
72 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
73 volatile uint32_t status; /**< Connection status(enum channel_status) */
74 int fd; /**< AF_UNIX socket fd */
75 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
76 enum channel_type type; /**< Binary, ini, json, etc. */
77 void *priv_info; /**< Pointer to private info, do not modify */
80 /* Represents a single VM instance used to return internal information about
83 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
84 enum vm_status status; /**< libvirt status */
85 uint64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS]; /**< pCPU mask for each vCPU */
86 unsigned num_vcpus; /**< number of vCPUS */
87 struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS]; /**< Array of channel_info */
88 unsigned num_channels; /**< Number of channels */
92 * Initialize the Channel Manager resources and connect to the Hypervisor
94 * This must be successfully called first before calling any other functions.
95 * It must only be call once;
98 * Must be a local path, e.g. qemu:///system.
102 * - Negative on error.
104 int channel_manager_init(const char *path);
107 * Free resources associated with the Channel Manager.
110 * Must be a local path, e.g. qemu:///system.
115 void channel_manager_exit(void);
118 * Get the Physical CPU mask for VM lcore channel(vcpu), result is assigned to
120 * It is not thread-safe.
123 * Pointer to struct channel_info
126 * The virtual CPU to query.
133 uint64_t get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu);
136 * Set the Physical CPU mask for the specified vCPU.
137 * It is not thread-safe.
140 * Virtual Machine name to lookup
143 * The virtual CPU to set.
146 * The core mask of the physical CPU(s) to bind the vCPU
150 * - Negative on error.
152 int set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask);
155 * Set the Physical CPU for the specified vCPU.
156 * It is not thread-safe.
159 * Virtual Machine name to lookup
162 * The virtual CPU to set.
165 * The core number of the physical CPU(s) to bind the vCPU
169 * - Negative on error.
171 int set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num);
173 * Add a VM as specified by name to the Channel Manager. The name must
174 * correspond to a valid libvirt domain name.
175 * This is required prior to adding channels.
176 * It is not thread-safe.
179 * Virtual Machine name to lookup.
183 * - Negative on error.
185 int add_vm(const char *name);
188 * Remove a previously added Virtual Machine from the Channel Manager
189 * It is not thread-safe.
192 * Virtual Machine name to lookup.
196 * - Negative on error.
198 int remove_vm(const char *name);
201 * Add all available channels to the VM as 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.
207 * Virtual Machine name to lookup.
210 * - N the number of channels added for the VM
212 int add_all_channels(const char *vm_name);
215 * Add the channel numbers in channel_list to the domain specified by name.
216 * Channels in the form of paths
217 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
218 * It is not thread-safe.
221 * Virtual Machine name to add channels.
223 * @param channel_list
224 * Pointer to list of unsigned integers, representing the channel number to add
225 * It must be allocated outside of this function.
227 * @param num_channels
228 * The amount of channel numbers in channel_list
231 * - N the number of channels added for the VM
234 int add_channels(const char *vm_name, unsigned *channel_list,
235 unsigned num_channels);
238 * Set up a fifo by which host applications can send command an policies
239 * through a fifo to the vm_power_manager
244 int add_host_channel(void);
247 * Remove a channel definition from the channel manager. This must only be
248 * called from the channel monitor thread.
251 * Pointer to a valid struct channel_info.
255 * - Negative on error.
257 int remove_channel(struct channel_info **chan_info_dptr);
260 * For all channels associated with a Virtual Machine name, update the
261 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
262 * CHANNEL_MGR_CHANNEL_DISABLED only.
266 * Virtual Machine name to modify all channels.
269 * The status to set each channel
271 * @param num_channels
272 * The amount of channel numbers in channel_list
275 * - N the number of channels added for the VM
278 int set_channel_status_all(const char *name, enum channel_status status);
281 * For all channels in channel_list associated with a Virtual Machine name
282 * update the connection status of each.
283 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
284 * CHANNEL_MGR_CHANNEL_DISABLED only.
285 * It is not thread-safe.
288 * Virtual Machine name to add channels.
290 * @param channel_list
291 * Pointer to list of unsigned integers, representing the channel numbers to
293 * It must be allocated outside of this function.
295 * @param num_channels
296 * The amount of channel numbers in channel_list
299 * - N the number of channels modified for the VM
302 int set_channel_status(const char *vm_name, unsigned *channel_list,
303 unsigned len_channel_list, enum channel_status status);
306 * Populates a pointer to struct vm_info associated with vm_name.
309 * The name of the virtual machine to lookup.
312 * Pointer to a struct vm_info, this must be allocated prior to calling this
317 * - Negative on error.
319 int get_info_vm(const char *vm_name, struct vm_info *info);
322 * Populates a table with all domains running and their physical cpu.
323 * All information is gathered through libvirt api.
326 * modified to store number of active VMs
329 modified to store number of vcpus active
334 void get_all_vm(int *num_vm, int *num_vcpu);
339 #endif /* CHANNEL_MANAGER_H_ */