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/"
26 struct sockaddr_un _sockaddr_un;
27 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
30 #define MAX_CLIENTS 64
34 struct libvirt_vm_info {
36 unsigned int pcpus[MAX_VCPUS];
40 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
41 /* Communication Channel Status */
42 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
43 CHANNEL_MGR_CHANNEL_CONNECTED,
44 CHANNEL_MGR_CHANNEL_DISABLED,
45 CHANNEL_MGR_CHANNEL_PROCESSING};
47 /* Communication Channel Type */
49 CHANNEL_TYPE_BINARY = 0,
54 /* VM libvirt(qemu/KVM) connection status */
55 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
58 * Represents a single and exclusive VM channel that exists between a guest and
62 char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
63 volatile uint32_t status; /**< Connection status(enum channel_status) */
64 int fd; /**< AF_UNIX socket fd */
65 unsigned channel_num; /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
66 enum channel_type type; /**< Binary, ini, json, etc. */
67 void *priv_info; /**< Pointer to private info, do not modify */
70 /* Represents a single VM instance used to return internal information about
73 char name[CHANNEL_MGR_MAX_NAME_LEN]; /**< VM name */
74 enum vm_status status; /**< libvirt status */
75 uint16_t pcpu_map[RTE_MAX_LCORE]; /**< pCPU map to vCPU */
76 unsigned num_vcpus; /**< number of vCPUS */
77 struct channel_info channels[RTE_MAX_LCORE]; /**< channel_info array */
78 unsigned num_channels; /**< Number of channels */
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 * Add a VM as specified by name to the Channel Manager. The name must
145 * correspond to a valid libvirt domain name.
146 * This is required prior to adding channels.
147 * It is not thread-safe.
150 * Virtual Machine name to lookup.
154 * - Negative on error.
156 int add_vm(const char *name);
159 * Remove a previously added Virtual Machine from the Channel Manager
160 * It is not thread-safe.
163 * Virtual Machine name to lookup.
167 * - Negative on error.
169 int remove_vm(const char *name);
172 * Add all available channels to the VM as specified by name.
173 * Channels in the form of paths
174 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
175 * It is not thread-safe.
178 * Virtual Machine name to lookup.
181 * - N the number of channels added for the VM
183 int add_all_channels(const char *vm_name);
186 * Add the channel numbers in channel_list to the domain specified by name.
187 * Channels in the form of paths
188 * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
189 * It is not thread-safe.
192 * Virtual Machine name to add channels.
194 * @param channel_list
195 * Pointer to list of unsigned integers, representing the channel number to add
196 * It must be allocated outside of this function.
198 * @param num_channels
199 * The amount of channel numbers in channel_list
202 * - N the number of channels added for the VM
205 int add_channels(const char *vm_name, unsigned *channel_list,
206 unsigned num_channels);
209 * Set up a fifo by which host applications can send command an policies
210 * through a fifo to the vm_power_manager
215 int add_host_channel(void);
218 * Remove a channel definition from the channel manager. This must only be
219 * called from the channel monitor thread.
222 * Pointer to a valid struct channel_info.
226 * - Negative on error.
228 int remove_channel(struct channel_info **chan_info_dptr);
231 * For all channels associated with a Virtual Machine name, update the
232 * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
233 * CHANNEL_MGR_CHANNEL_DISABLED only.
237 * Virtual Machine name to modify all channels.
240 * The status to set each channel
242 * @param num_channels
243 * The amount of channel numbers in channel_list
246 * - N the number of channels added for the VM
249 int set_channel_status_all(const char *name, enum channel_status status);
252 * For all channels in channel_list associated with a Virtual Machine name
253 * update the connection status of each.
254 * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
255 * CHANNEL_MGR_CHANNEL_DISABLED only.
256 * It is not thread-safe.
259 * Virtual Machine name to add channels.
261 * @param channel_list
262 * Pointer to list of unsigned integers, representing the channel numbers to
264 * It must be allocated outside of this function.
266 * @param num_channels
267 * The amount of channel numbers in channel_list
270 * - N the number of channels modified for the VM
273 int set_channel_status(const char *vm_name, unsigned *channel_list,
274 unsigned len_channel_list, enum channel_status status);
277 * Populates a pointer to struct vm_info associated with vm_name.
280 * The name of the virtual machine to lookup.
283 * Pointer to a struct vm_info, this must be allocated prior to calling this
288 * - Negative on error.
290 int get_info_vm(const char *vm_name, struct vm_info *info);
293 * Populates a table with all domains running and their physical cpu.
294 * All information is gathered through libvirt api.
297 * modified to store number of active VMs
300 modified to store number of vcpus active
305 void get_all_vm(int *num_vm, int *num_vcpu);
310 #endif /* CHANNEL_MANAGER_H_ */