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>
17 /* Maximum name length including '\0' terminator */
18 #define CHANNEL_MGR_MAX_NAME_LEN 64
20 /* Hypervisor Path for libvirt(qemu/KVM) */
21 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
23 /* File socket directory */
24 #define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/"
26 /* FIFO file name template */
27 #define CHANNEL_MGR_FIFO_PATTERN_NAME "fifo"
30 struct sockaddr_un _sockaddr_un;
31 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
34 #define MAX_CLIENTS 64
38 struct libvirt_vm_info {
40 unsigned int pcpus[MAX_VCPUS];
44 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
45 /* Communication Channel Status */
46 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
47 CHANNEL_MGR_CHANNEL_CONNECTED,
48 CHANNEL_MGR_CHANNEL_DISABLED,
49 CHANNEL_MGR_CHANNEL_PROCESSING};
51 /* Communication Channel Type */
53 CHANNEL_TYPE_BINARY = 0,
58 /* VM libvirt(qemu/KVM) connection status */
59 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
62 * Represents a single and exclusive VM channel that exists between a guest and
66 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
67 volatile uint32_t status; /**< Connection status(enum channel_status) */
68 int fd; /**< AF_UNIX socket fd */
69 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
70 enum channel_type type; /**< Binary, ini, json, etc. */
71 void *priv_info; /**< Pointer to private info, do not modify */
74 /* Represents a single VM instance used to return internal information about
77 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
78 enum vm_status status; /**< libvirt status */
79 uint16_t pcpu_map[RTE_MAX_LCORE]; /**< pCPU map to vCPU */
80 unsigned num_vcpus; /**< number of vCPUS */
81 struct channel_info channels[RTE_MAX_LCORE]; /**< channel_info array */
82 unsigned num_channels; /**< Number of channels */
83 int allow_query; /**< is query allowed */
87 * Initialize the Channel Manager resources and connect to the Hypervisor
89 * This must be successfully called first before calling any other functions.
90 * It must only be call once;
93 * Must be a local path, e.g. qemu:///system.
97 * - Negative on error.
99 int channel_manager_init(const char *path);
102 * Free resources associated with the Channel Manager.
105 * Must be a local path, e.g. qemu:///system.
110 void channel_manager_exit(void);
113 * Get the Physical CPU for VM lcore channel(vcpu).
114 * It is not thread-safe.
117 * Pointer to struct channel_info
120 * The virtual CPU to query.
127 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
130 * Set the Physical CPU for the specified vCPU.
131 * It is not thread-safe.
134 * Virtual Machine name to lookup
137 * The virtual CPU to set.
140 * The core number of the physical CPU(s) to bind the vCPU
144 * - Negative on error.
146 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
149 * Allow or disallow queries for specified VM.
153 * Virtual Machine name to lookup.
156 * Query status to be set.
160 * - Negative on error.
162 int set_query_status(char *vm_name, bool allow_query);
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 * Set up fifos by which host applications can send command an policies
231 * through a fifo to the vm_power_manager
236 int add_host_channels(void);
239 * Remove a channel definition from the channel manager. This must only be
240 * called from the channel monitor thread.
243 * Pointer to a valid struct channel_info.
247 * - Negative on error.
249 int remove_channel(struct channel_info **chan_info_dptr);
252 * For all channels associated with a Virtual Machine name, update the
253 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
254 * CHANNEL_MGR_CHANNEL_DISABLED only.
258 * Virtual Machine name to modify all channels.
261 * The status to set each channel
263 * @param num_channels
264 * The amount of channel numbers in channel_list
267 * - N the number of channels added for the VM
270 int set_channel_status_all(const char *name, enum channel_status status);
273 * For all channels in channel_list associated with a Virtual Machine name
274 * update the connection status of each.
275 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
276 * CHANNEL_MGR_CHANNEL_DISABLED only.
277 * It is not thread-safe.
280 * Virtual Machine name to add channels.
282 * @param channel_list
283 * Pointer to list of unsigned integers, representing the channel numbers to
285 * It must be allocated outside of this function.
287 * @param num_channels
288 * The amount of channel numbers in channel_list
291 * - N the number of channels modified for the VM
294 int set_channel_status(const char *vm_name, unsigned *channel_list,
295 unsigned len_channel_list, enum channel_status status);
298 * Populates a pointer to struct vm_info associated with vm_name.
301 * The name of the virtual machine to lookup.
304 * Pointer to a struct vm_info, this must be allocated prior to calling this
309 * - Negative on error.
311 int get_info_vm(const char *vm_name, struct vm_info *info);
314 * Populates a table with all domains running and their physical cpu.
315 * All information is gathered through libvirt api.
318 * modified to store number of active VMs
321 modified to store number of vcpus active
326 void get_all_vm(int *num_vm, int *num_vcpu);
331 #endif /* CHANNEL_MANAGER_H_ */