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