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)
44 struct libvirt_vm_info {
46 unsigned int pcpus[MAX_VCPUS];
50 struct libvirt_vm_info lvm_info[MAX_VMS];
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 /* VM libvirt(qemu/KVM) connection status */
58 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
61 * Represents a single and exclusive VM channel that exists between a guest and
65 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
66 volatile uint32_t status; /**< Connection status(enum channel_status) */
67 int fd; /**< AF_UNIX socket fd */
68 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
69 void *priv_info; /**< Pointer to private info, do not modify */
72 /* Represents a single VM instance used to return internal information about
75 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
76 enum vm_status status; /**< libvirt status */
77 uint64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS]; /**< pCPU mask for each vCPU */
78 unsigned num_vcpus; /**< number of vCPUS */
79 struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS]; /**< Array of channel_info */
80 unsigned num_channels; /**< Number of channels */
84 * Initialize the Channel Manager resources and connect to the Hypervisor
86 * This must be successfully called first before calling any other functions.
87 * It must only be call once;
90 * Must be a local path, e.g. qemu:///system.
94 * - Negative on error.
96 int channel_manager_init(const char *path);
99 * Free resources associated with the Channel Manager.
102 * Must be a local path, e.g. qemu:///system.
107 void channel_manager_exit(void);
110 * Get the Physical CPU mask for VM lcore channel(vcpu), result is assigned to
112 * It is not thread-safe.
115 * Pointer to struct channel_info
118 * The virtual CPU to query.
125 uint64_t get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu);
128 * Set the Physical CPU mask for the specified vCPU.
129 * It is not thread-safe.
132 * Virtual Machine name to lookup
135 * The virtual CPU to set.
138 * The core mask of the physical CPU(s) to bind the vCPU
142 * - Negative on error.
144 int set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask);
147 * Set the Physical CPU for the specified vCPU.
148 * It is not thread-safe.
151 * Virtual Machine name to lookup
154 * The virtual CPU to set.
157 * The core number of the physical CPU(s) to bind the vCPU
161 * - Negative on error.
163 int set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num);
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.
171 * Virtual Machine name to lookup.
175 * - Negative on error.
177 int add_vm(const char *name);
180 * Remove a previously added Virtual Machine from the Channel Manager
181 * It is not thread-safe.
184 * Virtual Machine name to lookup.
188 * - Negative on error.
190 int remove_vm(const char *name);
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.
199 * Virtual Machine name to lookup.
202 * - N the number of channels added for the VM
204 int add_all_channels(const char *vm_name);
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.
213 * Virtual Machine name to add channels.
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.
219 * @param num_channels
220 * The amount of channel numbers in channel_list
223 * - N the number of channels added for the VM
226 int add_channels(const char *vm_name, unsigned *channel_list,
227 unsigned num_channels);
230 * Remove a channel definition from the channel manager. This must only be
231 * called from the channel monitor thread.
234 * Pointer to a valid struct channel_info.
238 * - Negative on error.
240 int remove_channel(struct channel_info **chan_info_dptr);
243 * For all channels associated with a Virtual Machine name, update the
244 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
245 * CHANNEL_MGR_CHANNEL_DISABLED only.
249 * Virtual Machine name to modify all channels.
252 * The status to set each channel
254 * @param num_channels
255 * The amount of channel numbers in channel_list
258 * - N the number of channels added for the VM
261 int set_channel_status_all(const char *name, enum channel_status status);
264 * For all channels in channel_list associated with a Virtual Machine name
265 * update the connection status of each.
266 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
267 * CHANNEL_MGR_CHANNEL_DISABLED only.
268 * It is not thread-safe.
271 * Virtual Machine name to add channels.
273 * @param channel_list
274 * Pointer to list of unsigned integers, representing the channel numbers to
276 * It must be allocated outside of this function.
278 * @param num_channels
279 * The amount of channel numbers in channel_list
282 * - N the number of channels modified for the VM
285 int set_channel_status(const char *vm_name, unsigned *channel_list,
286 unsigned len_channel_list, enum channel_status status);
289 * Populates a pointer to struct vm_info associated with vm_name.
292 * The name of the virtual machine to lookup.
295 * Pointer to a struct vm_info, this must be allocated prior to calling this
300 * - Negative on error.
302 int get_info_vm(const char *vm_name, struct vm_info *info);
305 * Populates a table with all domains running and their physical cpu.
306 * All information is gathered through libvirt api.
309 * modified to store number of active VMs
312 modified to store number of vcpus active
317 void get_all_vm(int *num_vm, int *num_vcpu);
322 #endif /* CHANNEL_MANAGER_H_ */