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 256
19 /* Maximum name length including '\0' terminator */
20 #define CHANNEL_MGR_MAX_NAME_LEN 64
22 /* Maximum number of channels to each Virtual Machine */
23 #define CHANNEL_MGR_MAX_CHANNELS 256
25 /* Hypervisor Path for libvirt(qemu/KVM) */
26 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
28 /* File socket directory */
29 #define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/"
32 struct sockaddr_un _sockaddr_un;
33 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
36 #define MAX_CLIENTS 64
40 struct libvirt_vm_info {
42 unsigned int pcpus[MAX_VCPUS];
46 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
47 /* Communication Channel Status */
48 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
49 CHANNEL_MGR_CHANNEL_CONNECTED,
50 CHANNEL_MGR_CHANNEL_DISABLED,
51 CHANNEL_MGR_CHANNEL_PROCESSING};
53 /* Communication Channel Type */
55 CHANNEL_TYPE_BINARY = 0,
60 /* VM libvirt(qemu/KVM) connection status */
61 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
64 * Represents a single and exclusive VM channel that exists between a guest and
68 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
69 volatile uint32_t status; /**< Connection status(enum channel_status) */
70 int fd; /**< AF_UNIX socket fd */
71 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
72 enum channel_type type; /**< Binary, ini, json, etc. */
73 void *priv_info; /**< Pointer to private info, do not modify */
76 /* Represents a single VM instance used to return internal information about
79 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
80 enum vm_status status; /**< libvirt status */
81 uint16_t pcpu_map[CHANNEL_CMDS_MAX_CPUS]; /**< pCPU map to vCPU */
82 unsigned num_vcpus; /**< number of vCPUS */
83 struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS]; /**< Array of channel_info */
84 unsigned num_channels; /**< Number of channels */
88 * Initialize the Channel Manager resources and connect to the Hypervisor
90 * This must be successfully called first before calling any other functions.
91 * It must only be call once;
94 * Must be a local path, e.g. qemu:///system.
98 * - Negative on error.
100 int channel_manager_init(const char *path);
103 * Free resources associated with the Channel Manager.
106 * Must be a local path, e.g. qemu:///system.
111 void channel_manager_exit(void);
114 * Get the Physical CPU for VM lcore channel(vcpu).
115 * It is not thread-safe.
118 * Pointer to struct channel_info
121 * The virtual CPU to query.
128 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
131 * Set the Physical CPU for the specified vCPU.
132 * It is not thread-safe.
135 * Virtual Machine name to lookup
138 * The virtual CPU to set.
141 * The core number of the physical CPU(s) to bind the vCPU
145 * - Negative on error.
147 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
150 * Add a VM as specified by name to the Channel Manager. The name must
151 * correspond to a valid libvirt domain name.
152 * This is required prior to adding channels.
153 * It is not thread-safe.
156 * Virtual Machine name to lookup.
160 * - Negative on error.
162 int add_vm(const char *name);
165 * Remove a previously added Virtual Machine from the Channel Manager
166 * It is not thread-safe.
169 * Virtual Machine name to lookup.
173 * - Negative on error.
175 int remove_vm(const char *name);
178 * Add all available channels to the VM as specified by name.
179 * Channels in the form of paths
180 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
181 * It is not thread-safe.
184 * Virtual Machine name to lookup.
187 * - N the number of channels added for the VM
189 int add_all_channels(const char *vm_name);
192 * Add the channel numbers in channel_list to the domain specified by name.
193 * Channels in the form of paths
194 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
195 * It is not thread-safe.
198 * Virtual Machine name to add channels.
200 * @param channel_list
201 * Pointer to list of unsigned integers, representing the channel number to add
202 * It must be allocated outside of this function.
204 * @param num_channels
205 * The amount of channel numbers in channel_list
208 * - N the number of channels added for the VM
211 int add_channels(const char *vm_name, unsigned *channel_list,
212 unsigned num_channels);
215 * Set up a fifo by which host applications can send command an policies
216 * through a fifo to the vm_power_manager
221 int add_host_channel(void);
224 * Remove a channel definition from the channel manager. This must only be
225 * called from the channel monitor thread.
228 * Pointer to a valid struct channel_info.
232 * - Negative on error.
234 int remove_channel(struct channel_info **chan_info_dptr);
237 * For all channels associated with a Virtual Machine name, update the
238 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
239 * CHANNEL_MGR_CHANNEL_DISABLED only.
243 * Virtual Machine name to modify all channels.
246 * The status to set each channel
248 * @param num_channels
249 * The amount of channel numbers in channel_list
252 * - N the number of channels added for the VM
255 int set_channel_status_all(const char *name, enum channel_status status);
258 * For all channels in channel_list associated with a Virtual Machine name
259 * update the connection status of each.
260 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
261 * CHANNEL_MGR_CHANNEL_DISABLED only.
262 * It is not thread-safe.
265 * Virtual Machine name to add channels.
267 * @param channel_list
268 * Pointer to list of unsigned integers, representing the channel numbers to
270 * It must be allocated outside of this function.
272 * @param num_channels
273 * The amount of channel numbers in channel_list
276 * - N the number of channels modified for the VM
279 int set_channel_status(const char *vm_name, unsigned *channel_list,
280 unsigned len_channel_list, enum channel_status status);
283 * Populates a pointer to struct vm_info associated with vm_name.
286 * The name of the virtual machine to lookup.
289 * Pointer to a struct vm_info, this must be allocated prior to calling this
294 * - Negative on error.
296 int get_info_vm(const char *vm_name, struct vm_info *info);
299 * Populates a table with all domains running and their physical cpu.
300 * All information is gathered through libvirt api.
303 * modified to store number of active VMs
306 modified to store number of vcpus active
311 void get_all_vm(int *num_vm, int *num_vcpu);
316 #endif /* CHANNEL_MANAGER_H_ */