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 uint16_t pcpu_map[CHANNEL_CMDS_MAX_CPUS]; /**< pCPU map to 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 for VM lcore channel(vcpu).
119 * It is not thread-safe.
122 * Pointer to struct channel_info
125 * The virtual CPU to query.
132 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
135 * Set the Physical CPU for the specified vCPU.
136 * It is not thread-safe.
139 * Virtual Machine name to lookup
142 * The virtual CPU to set.
145 * The core number of the physical CPU(s) to bind the vCPU
149 * - Negative on error.
151 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
154 * Add a VM as specified by name to the Channel Manager. The name must
155 * correspond to a valid libvirt domain name.
156 * This is required prior to adding channels.
157 * It is not thread-safe.
160 * Virtual Machine name to lookup.
164 * - Negative on error.
166 int add_vm(const char *name);
169 * Remove a previously added Virtual Machine from the Channel Manager
170 * It is not thread-safe.
173 * Virtual Machine name to lookup.
177 * - Negative on error.
179 int remove_vm(const char *name);
182 * Add all available channels to the VM as specified by name.
183 * Channels in the form of paths
184 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
185 * It is not thread-safe.
188 * Virtual Machine name to lookup.
191 * - N the number of channels added for the VM
193 int add_all_channels(const char *vm_name);
196 * Add the channel numbers in channel_list to the domain specified by name.
197 * Channels in the form of paths
198 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
199 * It is not thread-safe.
202 * Virtual Machine name to add channels.
204 * @param channel_list
205 * Pointer to list of unsigned integers, representing the channel number to add
206 * It must be allocated outside of this function.
208 * @param num_channels
209 * The amount of channel numbers in channel_list
212 * - N the number of channels added for the VM
215 int add_channels(const char *vm_name, unsigned *channel_list,
216 unsigned num_channels);
219 * Set up a fifo by which host applications can send command an policies
220 * through a fifo to the vm_power_manager
225 int add_host_channel(void);
228 * Remove a channel definition from the channel manager. This must only be
229 * called from the channel monitor thread.
232 * Pointer to a valid struct channel_info.
236 * - Negative on error.
238 int remove_channel(struct channel_info **chan_info_dptr);
241 * For all channels associated with a Virtual Machine name, update the
242 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
243 * CHANNEL_MGR_CHANNEL_DISABLED only.
247 * Virtual Machine name to modify all channels.
250 * The status to set each channel
252 * @param num_channels
253 * The amount of channel numbers in channel_list
256 * - N the number of channels added for the VM
259 int set_channel_status_all(const char *name, enum channel_status status);
262 * For all channels in channel_list associated with a Virtual Machine name
263 * update the connection status of each.
264 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
265 * CHANNEL_MGR_CHANNEL_DISABLED only.
266 * It is not thread-safe.
269 * Virtual Machine name to add channels.
271 * @param channel_list
272 * Pointer to list of unsigned integers, representing the channel numbers to
274 * It must be allocated outside of this function.
276 * @param num_channels
277 * The amount of channel numbers in channel_list
280 * - N the number of channels modified for the VM
283 int set_channel_status(const char *vm_name, unsigned *channel_list,
284 unsigned len_channel_list, enum channel_status status);
287 * Populates a pointer to struct vm_info associated with vm_name.
290 * The name of the virtual machine to lookup.
293 * Pointer to a struct vm_info, this must be allocated prior to calling this
298 * - Negative on error.
300 int get_info_vm(const char *vm_name, struct vm_info *info);
303 * Populates a table with all domains running and their physical cpu.
304 * All information is gathered through libvirt api.
307 * modified to store number of active VMs
310 modified to store number of vcpus active
315 void get_all_vm(int *num_vm, int *num_vcpu);
320 #endif /* CHANNEL_MANAGER_H_ */