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"
29 #define MAX_CLIENTS 64
33 struct libvirt_vm_info {
35 unsigned int pcpus[MAX_VCPUS];
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};
46 /* Communication Channel Type */
48 CHANNEL_TYPE_BINARY = 0,
53 /* VM libvirt(qemu/KVM) connection status */
54 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
57 * Represents a single and exclusive VM channel that exists between a guest and
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 */
69 /* Represents a single VM instance used to return internal information about
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 */
82 * Initialize the Channel Manager resources and connect to the Hypervisor
84 * This must be successfully called first before calling any other functions.
85 * It must only be call once;
88 * Must be a local path, e.g. qemu:///system.
92 * - Negative on error.
94 int channel_manager_init(const char *path);
97 * Free resources associated with the Channel Manager.
100 * Must be a local path, e.g. qemu:///system.
105 void channel_manager_exit(void);
108 * Get the Physical CPU for VM lcore channel(vcpu).
109 * It is not thread-safe.
112 * Pointer to struct channel_info
115 * The virtual CPU to query.
122 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
125 * Set the Physical CPU for the specified vCPU.
126 * It is not thread-safe.
129 * Virtual Machine name to lookup
132 * The virtual CPU to set.
135 * The core number of the physical CPU(s) to bind the vCPU
139 * - Negative on error.
141 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
144 * Allow or disallow queries for specified VM.
148 * Virtual Machine name to lookup.
151 * Query status to be set.
155 * - Negative on error.
157 int set_query_status(char *vm_name, bool allow_query);
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.
166 * Virtual Machine name to lookup.
170 * - Negative on error.
172 int add_vm(const char *name);
175 * Remove a previously added Virtual Machine from the Channel Manager
176 * It is not thread-safe.
179 * Virtual Machine name to lookup.
183 * - Negative on error.
185 int remove_vm(const char *name);
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.
194 * Virtual Machine name to lookup.
197 * - N the number of channels added for the VM
199 int add_all_channels(const char *vm_name);
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.
208 * Virtual Machine name to add channels.
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.
214 * @param num_channels
215 * The amount of channel numbers in channel_list
218 * - N the number of channels added for the VM
221 int add_channels(const char *vm_name, unsigned *channel_list,
222 unsigned num_channels);
225 * Set up fifos by which host applications can send command an policies
226 * through a fifo to the vm_power_manager
231 int add_host_channels(void);
234 * Remove a channel definition from the channel manager. This must only be
235 * called from the channel monitor thread.
238 * Pointer to a valid struct channel_info.
242 * - Negative on error.
244 int remove_channel(struct channel_info **chan_info_dptr);
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.
253 * Virtual Machine name to modify all channels.
256 * The status to set each channel
258 * @param num_channels
259 * The amount of channel numbers in channel_list
262 * - N the number of channels added for the VM
265 int set_channel_status_all(const char *name, enum channel_status status);
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.
275 * Virtual Machine name to add channels.
277 * @param channel_list
278 * Pointer to list of unsigned integers, representing the channel numbers to
280 * It must be allocated outside of this function.
282 * @param num_channels
283 * The amount of channel numbers in channel_list
286 * - N the number of channels modified for the VM
289 int set_channel_status(const char *vm_name, unsigned *channel_list,
290 unsigned len_channel_list, enum channel_status status);
293 * Populates a pointer to struct vm_info associated with vm_name.
296 * The name of the virtual machine to lookup.
299 * Pointer to a struct vm_info, this must be allocated prior to calling this
304 * - Negative on error.
306 int get_info_vm(const char *vm_name, struct vm_info *info);
309 * Populates a table with all domains running and their physical cpu.
310 * All information is gathered through libvirt api.
313 * modified to store number of active VMs
316 modified to store number of vcpus active
321 void get_all_vm(int *num_vm, int *num_vcpu);
326 #endif /* CHANNEL_MANAGER_H_ */