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 name length including '\0' terminator */
17 #define CHANNEL_MGR_MAX_NAME_LEN 64
19 /* Hypervisor Path for libvirt(qemu/KVM) */
20 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
22 /* File socket directory */
23 #define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/"
25 /* FIFO file name template */
26 #define CHANNEL_MGR_FIFO_PATTERN_NAME "fifo"
29 struct sockaddr_un _sockaddr_un;
30 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
33 #define MAX_CLIENTS 64
37 struct libvirt_vm_info {
39 unsigned int pcpus[MAX_VCPUS];
43 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
44 /* Communication Channel Status */
45 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
46 CHANNEL_MGR_CHANNEL_CONNECTED,
47 CHANNEL_MGR_CHANNEL_DISABLED,
48 CHANNEL_MGR_CHANNEL_PROCESSING};
50 /* Communication Channel Type */
52 CHANNEL_TYPE_BINARY = 0,
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 enum channel_type type; /**< Binary, ini, json, etc. */
70 void *priv_info; /**< Pointer to private info, do not modify */
73 /* Represents a single VM instance used to return internal information about
76 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
77 enum vm_status status; /**< libvirt status */
78 uint16_t pcpu_map[RTE_MAX_LCORE]; /**< pCPU map to vCPU */
79 unsigned num_vcpus; /**< number of vCPUS */
80 struct channel_info channels[RTE_MAX_LCORE]; /**< channel_info array */
81 unsigned num_channels; /**< Number of channels */
85 * Initialize the Channel Manager resources and connect to the Hypervisor
87 * This must be successfully called first before calling any other functions.
88 * It must only be call once;
91 * Must be a local path, e.g. qemu:///system.
95 * - Negative on error.
97 int channel_manager_init(const char *path);
100 * Free resources associated with the Channel Manager.
103 * Must be a local path, e.g. qemu:///system.
108 void channel_manager_exit(void);
111 * Get the Physical CPU for VM lcore channel(vcpu).
112 * It is not thread-safe.
115 * Pointer to struct channel_info
118 * The virtual CPU to query.
125 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
128 * Set the Physical CPU 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 number of the physical CPU(s) to bind the vCPU
142 * - Negative on error.
144 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
147 * Add a VM as specified by name to the Channel Manager. The name must
148 * correspond to a valid libvirt domain name.
149 * This is required prior to adding channels.
150 * It is not thread-safe.
153 * Virtual Machine name to lookup.
157 * - Negative on error.
159 int add_vm(const char *name);
162 * Remove a previously added Virtual Machine from the Channel Manager
163 * It is not thread-safe.
166 * Virtual Machine name to lookup.
170 * - Negative on error.
172 int remove_vm(const char *name);
175 * Add all available channels to the VM as specified by name.
176 * Channels in the form of paths
177 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
178 * It is not thread-safe.
181 * Virtual Machine name to lookup.
184 * - N the number of channels added for the VM
186 int add_all_channels(const char *vm_name);
189 * Add the channel numbers in channel_list to the domain specified by name.
190 * Channels in the form of paths
191 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
192 * It is not thread-safe.
195 * Virtual Machine name to add channels.
197 * @param channel_list
198 * Pointer to list of unsigned integers, representing the channel number to add
199 * It must be allocated outside of this function.
201 * @param num_channels
202 * The amount of channel numbers in channel_list
205 * - N the number of channels added for the VM
208 int add_channels(const char *vm_name, unsigned *channel_list,
209 unsigned num_channels);
212 * Set up fifos by which host applications can send command an policies
213 * through a fifo to the vm_power_manager
218 int add_host_channels(void);
221 * Remove a channel definition from the channel manager. This must only be
222 * called from the channel monitor thread.
225 * Pointer to a valid struct channel_info.
229 * - Negative on error.
231 int remove_channel(struct channel_info **chan_info_dptr);
234 * For all channels associated with a Virtual Machine name, update the
235 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
236 * CHANNEL_MGR_CHANNEL_DISABLED only.
240 * Virtual Machine name to modify all channels.
243 * The status to set each channel
245 * @param num_channels
246 * The amount of channel numbers in channel_list
249 * - N the number of channels added for the VM
252 int set_channel_status_all(const char *name, enum channel_status status);
255 * For all channels in channel_list associated with a Virtual Machine name
256 * update the connection status of each.
257 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
258 * CHANNEL_MGR_CHANNEL_DISABLED only.
259 * It is not thread-safe.
262 * Virtual Machine name to add channels.
264 * @param channel_list
265 * Pointer to list of unsigned integers, representing the channel numbers to
267 * It must be allocated outside of this function.
269 * @param num_channels
270 * The amount of channel numbers in channel_list
273 * - N the number of channels modified for the VM
276 int set_channel_status(const char *vm_name, unsigned *channel_list,
277 unsigned len_channel_list, enum channel_status status);
280 * Populates a pointer to struct vm_info associated with vm_name.
283 * The name of the virtual machine to lookup.
286 * Pointer to a struct vm_info, this must be allocated prior to calling this
291 * - Negative on error.
293 int get_info_vm(const char *vm_name, struct vm_info *info);
296 * Populates a table with all domains running and their physical cpu.
297 * All information is gathered through libvirt api.
300 * modified to store number of active VMs
303 modified to store number of vcpus active
308 void get_all_vm(int *num_vm, int *num_vcpu);
313 #endif /* CHANNEL_MANAGER_H_ */