xref: /dpdk/examples/vm_power_manager/channel_manager.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef CHANNEL_MANAGER_H_
6 #define CHANNEL_MANAGER_H_
7 
8 #include <linux/limits.h>
9 #include <linux/un.h>
10 #include <stdbool.h>
11 
12 #include <rte_stdatomic.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* Maximum name length including '\0' terminator */
19 #define CHANNEL_MGR_MAX_NAME_LEN    64
20 
21 /* Hypervisor Path for libvirt(qemu/KVM) */
22 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
23 
24 /* File socket directory */
25 #define CHANNEL_MGR_SOCKET_PATH     "/tmp/powermonitor/"
26 
27 /* FIFO file name template */
28 #define CHANNEL_MGR_FIFO_PATTERN_NAME   "fifo"
29 
30 #define MAX_CLIENTS 64
31 #define MAX_VCPUS 20
32 
33 
34 struct libvirt_vm_info {
35 	const char *vm_name;
36 	unsigned int pcpus[MAX_VCPUS];
37 	uint8_t num_cpus;
38 };
39 
40 extern 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};
46 
47 /* Communication Channel Type */
48 enum channel_type {
49 	CHANNEL_TYPE_BINARY = 0,
50 	CHANNEL_TYPE_INI,
51 	CHANNEL_TYPE_JSON
52 };
53 
54 /* VM libvirt(qemu/KVM) connection status */
55 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
56 
57 /*
58  *  Represents a single and exclusive VM channel that exists between a guest and
59  *  the host.
60  */
61 struct channel_info {
62 	char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
63 	volatile RTE_ATOMIC(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 */
68 };
69 
70 /* Represents a single VM instance used to return internal information about
71  * a VM */
72 struct vm_info {
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 */
79 	int allow_query;                              /**< is query allowed */
80 };
81 
82 /**
83  * Initialize the Channel Manager resources and connect to the Hypervisor
84  * specified in path.
85  * This must be successfully called first before calling any other functions.
86  * It must only be call once;
87  *
88  * @param path
89  *  Must be a local path, e.g. qemu:///system.
90  *
91  * @return
92  *  - 0 on success.
93  *  - Negative on error.
94  */
95 int channel_manager_init(const char *path);
96 
97 /**
98  * Free resources associated with the Channel Manager.
99  *
100  * @param path
101  *  Must be a local path, e.g. qemu:///system.
102  *
103  * @return
104  *  None
105  */
106 void channel_manager_exit(void);
107 
108 /**
109  * Get the Physical CPU for VM lcore channel(vcpu).
110  * It is not thread-safe.
111  *
112  * @param chan_info
113  *  Pointer to struct channel_info
114  *
115  * @param vcpu
116  *  The virtual CPU to query.
117  *
118  *
119  * @return
120  *  - 0 on error.
121  *  - >0 on success.
122  */
123 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
124 
125 /**
126  * Set the Physical CPU for the specified vCPU.
127  * It is not thread-safe.
128  *
129  * @param name
130  *  Virtual Machine name to lookup
131  *
132  * @param vcpu
133  *  The virtual CPU to set.
134  *
135  * @param core_num
136  *  The core number of the physical CPU(s) to bind the vCPU
137  *
138  * @return
139  *  - 0 on success.
140  *  - Negative on error.
141  */
142 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
143 
144 /**
145  * Allow or disallow queries for specified VM.
146  * It is thread-safe.
147  *
148  * @param name
149  *  Virtual Machine name to lookup.
150  *
151  * @param allow_query
152  *  Query status to be set.
153  *
154  * @return
155  *  - 0 on success.
156  *  - Negative on error.
157  */
158 int set_query_status(char *vm_name, bool allow_query);
159 
160 /**
161  * Add a VM as specified by name to the Channel Manager. The name must
162  * correspond to a valid libvirt domain name.
163  * This is required prior to adding channels.
164  * It is not thread-safe.
165  *
166  * @param name
167  *  Virtual Machine name to lookup.
168  *
169  * @return
170  *  - 0 on success.
171  *  - Negative on error.
172  */
173 int add_vm(const char *name);
174 
175 /**
176  * Remove a previously added Virtual Machine from the Channel Manager
177  * It is not thread-safe.
178  *
179  * @param name
180  *  Virtual Machine name to lookup.
181  *
182  * @return
183  *  - 0 on success.
184  *  - Negative on error.
185  */
186 int remove_vm(const char *name);
187 
188 /**
189  * Add all available channels to the VM as 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.
193  *
194  * @param name
195  *  Virtual Machine name to lookup.
196  *
197  * @return
198  *  - N the number of channels added for the VM
199  */
200 int add_all_channels(const char *vm_name);
201 
202 /**
203  * Add the channel numbers in channel_list to the domain specified by name.
204  * Channels in the form of paths
205  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
206  * It is not thread-safe.
207  *
208  * @param name
209  *  Virtual Machine name to add channels.
210  *
211  * @param channel_list
212  *  Pointer to list of unsigned integers, representing the channel number to add
213  *  It must be allocated outside of this function.
214  *
215  * @param num_channels
216  *  The amount of channel numbers in channel_list
217  *
218  * @return
219  *  - N the number of channels added for the VM
220  *  - 0 for error
221  */
222 int add_channels(const char *vm_name, unsigned *channel_list,
223 		unsigned num_channels);
224 
225 /**
226  * Set up fifos by which host applications can send command an policies
227  * through a fifo to the vm_power_manager
228  *
229  * @return
230  *  - 0 for success
231  */
232 int add_host_channels(void);
233 
234 /**
235  * Remove a channel definition from the channel manager. This must only be
236  * called from the channel monitor thread.
237  *
238  * @param chan_info
239  *  Pointer to a valid struct channel_info.
240  *
241  * @return
242  *  - 0 on success.
243  *  - Negative on error.
244  */
245 int remove_channel(struct channel_info **chan_info_dptr);
246 
247 /**
248  * For all channels associated with a Virtual Machine name, update the
249  * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
250  * CHANNEL_MGR_CHANNEL_DISABLED only.
251  *
252  *
253  * @param name
254  *  Virtual Machine name to modify all channels.
255  *
256  * @param status
257  *  The status to set each channel
258  *
259  * @param num_channels
260  *  The amount of channel numbers in channel_list
261  *
262  * @return
263  *  - N the number of channels added for the VM
264  *  - 0 for error
265  */
266 int set_channel_status_all(const char *name, enum channel_status status);
267 
268 /**
269  * For all channels in channel_list associated with a Virtual Machine name
270  * update the connection status of each.
271  * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
272  * CHANNEL_MGR_CHANNEL_DISABLED only.
273  * It is not thread-safe.
274  *
275  * @param name
276  *  Virtual Machine name to add channels.
277  *
278  * @param channel_list
279  *  Pointer to list of unsigned integers, representing the channel numbers to
280  *  modify.
281  *  It must be allocated outside of this function.
282  *
283  * @param num_channels
284  *  The amount of channel numbers in channel_list
285  *
286  * @return
287  *  - N the number of channels modified for the VM
288  *  - 0 for error
289  */
290 int set_channel_status(const char *vm_name, unsigned *channel_list,
291 		unsigned len_channel_list, enum channel_status status);
292 
293 /**
294  * Populates a pointer to struct vm_info associated with vm_name.
295  *
296  * @param vm_name
297  *  The name of the virtual machine to lookup.
298  *
299  *  @param vm_info
300  *   Pointer to a struct vm_info, this must be allocated prior to calling this
301  *   function.
302  *
303  * @return
304  *  - 0 on success.
305  *  - Negative on error.
306  */
307 int get_info_vm(const char *vm_name, struct vm_info *info);
308 
309 /**
310  * Populates a table with all domains running and their physical cpu.
311  * All information is gathered through libvirt api.
312  *
313  * @param num_vm
314  *  modified to store number of active VMs
315  *
316  * @param num_vcpu
317     modified to store number of vcpus active
318  *
319  * @return
320  *   void
321  */
322 void get_all_vm(int *num_vm, int *num_vcpu);
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif /* CHANNEL_MANAGER_H_ */
328