New upstream version 17.11-rc3
[deb_dpdk.git] / examples / vm_power_manager / channel_manager.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef CHANNEL_MANAGER_H_
35 #define CHANNEL_MANAGER_H_
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #include <linux/limits.h>
42 #include <sys/un.h>
43 #include <rte_atomic.h>
44
45 /* Maximum number of CPUs */
46 #define CHANNEL_CMDS_MAX_CPUS        64
47 #if CHANNEL_CMDS_MAX_CPUS > 64
48 #error Maximum number of cores is 64, overflow is guaranteed to \
49     cause problems with VM Power Management
50 #endif
51
52 /* Maximum name length including '\0' terminator */
53 #define CHANNEL_MGR_MAX_NAME_LEN    64
54
55 /* Maximum number of channels to each Virtual Machine */
56 #define CHANNEL_MGR_MAX_CHANNELS    64
57
58 /* Hypervisor Path for libvirt(qemu/KVM) */
59 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
60
61 /* File socket directory */
62 #define CHANNEL_MGR_SOCKET_PATH     "/tmp/powermonitor/"
63
64 #ifndef UNIX_PATH_MAX
65 struct sockaddr_un _sockaddr_un;
66 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
67 #endif
68
69 #define MAX_VMS 4
70 #define MAX_VCPUS 20
71
72
73 struct libvirt_vm_info {
74         const char *vm_name;
75         unsigned int pcpus[MAX_VCPUS];
76         uint8_t num_cpus;
77 };
78
79 struct libvirt_vm_info lvm_info[MAX_VMS];
80 /* Communication Channel Status */
81 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
82         CHANNEL_MGR_CHANNEL_CONNECTED,
83         CHANNEL_MGR_CHANNEL_DISABLED,
84         CHANNEL_MGR_CHANNEL_PROCESSING};
85
86 /* VM libvirt(qemu/KVM) connection status */
87 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
88
89 /*
90  *  Represents a single and exclusive VM channel that exists between a guest and
91  *  the host.
92  */
93 struct channel_info {
94         char channel_path[UNIX_PATH_MAX]; /**< Path to host socket */
95         volatile uint32_t status;    /**< Connection status(enum channel_status) */
96         int fd;                      /**< AF_UNIX socket fd */
97         unsigned channel_num;        /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
98         void *priv_info;             /**< Pointer to private info, do not modify */
99 };
100
101 /* Represents a single VM instance used to return internal information about
102  * a VM */
103 struct vm_info {
104         char name[CHANNEL_MGR_MAX_NAME_LEN];          /**< VM name */
105         enum vm_status status;                        /**< libvirt status */
106         uint64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS];    /**< pCPU mask for each vCPU */
107         unsigned num_vcpus;                           /**< number of vCPUS */
108         struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS]; /**< Array of channel_info */
109         unsigned num_channels;                        /**< Number of channels */
110 };
111
112 /**
113  * Initialize the Channel Manager resources and connect to the Hypervisor
114  * specified in path.
115  * This must be successfully called first before calling any other functions.
116  * It must only be call once;
117  *
118  * @param path
119  *  Must be a local path, e.g. qemu:///system.
120  *
121  * @return
122  *  - 0 on success.
123  *  - Negative on error.
124  */
125 int channel_manager_init(const char *path);
126
127 /**
128  * Free resources associated with the Channel Manager.
129  *
130  * @param path
131  *  Must be a local path, e.g. qemu:///system.
132  *
133  * @return
134  *  None
135  */
136 void channel_manager_exit(void);
137
138 /**
139  * Get the Physical CPU mask for VM lcore channel(vcpu), result is assigned to
140  * core_mask.
141  * It is not thread-safe.
142  *
143  * @param chan_info
144  *  Pointer to struct channel_info
145  *
146  * @param vcpu
147  *  The virtual CPU to query.
148  *
149  *
150  * @return
151  *  - 0 on error.
152  *  - >0 on success.
153  */
154 uint64_t get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu);
155
156 /**
157  * Set the Physical CPU mask for the specified vCPU.
158  * It is not thread-safe.
159  *
160  * @param name
161  *  Virtual Machine name to lookup
162  *
163  * @param vcpu
164  *  The virtual CPU to set.
165  *
166  * @param core_mask
167  *  The core mask of the physical CPU(s) to bind the vCPU
168  *
169  * @return
170  *  - 0 on success.
171  *  - Negative on error.
172  */
173 int set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask);
174
175 /**
176  * Set the Physical CPU for the specified vCPU.
177  * It is not thread-safe.
178  *
179  * @param name
180  *  Virtual Machine name to lookup
181  *
182  * @param vcpu
183  *  The virtual CPU to set.
184  *
185  * @param core_num
186  *  The core number of the physical CPU(s) to bind the vCPU
187  *
188  * @return
189  *  - 0 on success.
190  *  - Negative on error.
191  */
192 int set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num);
193 /**
194  * Add a VM as specified by name to the Channel Manager. The name must
195  * correspond to a valid libvirt domain name.
196  * This is required prior to adding channels.
197  * It is not thread-safe.
198  *
199  * @param name
200  *  Virtual Machine name to lookup.
201  *
202  * @return
203  *  - 0 on success.
204  *  - Negative on error.
205  */
206 int add_vm(const char *name);
207
208 /**
209  * Remove a previously added Virtual Machine from the Channel Manager
210  * It is not thread-safe.
211  *
212  * @param name
213  *  Virtual Machine name to lookup.
214  *
215  * @return
216  *  - 0 on success.
217  *  - Negative on error.
218  */
219 int remove_vm(const char *name);
220
221 /**
222  * Add all available channels to the VM as specified by name.
223  * Channels in the form of paths
224  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
225  * It is not thread-safe.
226  *
227  * @param name
228  *  Virtual Machine name to lookup.
229  *
230  * @return
231  *  - N the number of channels added for the VM
232  */
233 int add_all_channels(const char *vm_name);
234
235 /**
236  * Add the channel numbers in channel_list to the domain specified by name.
237  * Channels in the form of paths
238  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
239  * It is not thread-safe.
240  *
241  * @param name
242  *  Virtual Machine name to add channels.
243  *
244  * @param channel_list
245  *  Pointer to list of unsigned integers, representing the channel number to add
246  *  It must be allocated outside of this function.
247  *
248  * @param num_channels
249  *  The amount of channel numbers in channel_list
250  *
251  * @return
252  *  - N the number of channels added for the VM
253  *  - 0 for error
254  */
255 int add_channels(const char *vm_name, unsigned *channel_list,
256                 unsigned num_channels);
257
258 /**
259  * Remove a channel definition from the channel manager. This must only be
260  * called from the channel monitor thread.
261  *
262  * @param chan_info
263  *  Pointer to a valid struct channel_info.
264  *
265  * @return
266  *  - 0 on success.
267  *  - Negative on error.
268  */
269 int remove_channel(struct channel_info **chan_info_dptr);
270
271 /**
272  * For all channels associated with a Virtual Machine name, update the
273  * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
274  * CHANNEL_MGR_CHANNEL_DISABLED only.
275  *
276  *
277  * @param name
278  *  Virtual Machine name to modify all channels.
279  *
280  * @param status
281  *  The status to set each channel
282  *
283  * @param num_channels
284  *  The amount of channel numbers in channel_list
285  *
286  * @return
287  *  - N the number of channels added for the VM
288  *  - 0 for error
289  */
290 int set_channel_status_all(const char *name, enum channel_status status);
291
292 /**
293  * For all channels in channel_list associated with a Virtual Machine name
294  * update the connection status of each.
295  * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
296  * CHANNEL_MGR_CHANNEL_DISABLED only.
297  * It is not thread-safe.
298  *
299  * @param name
300  *  Virtual Machine name to add channels.
301  *
302  * @param channel_list
303  *  Pointer to list of unsigned integers, representing the channel numbers to
304  *  modify.
305  *  It must be allocated outside of this function.
306  *
307  * @param num_channels
308  *  The amount of channel numbers in channel_list
309  *
310  * @return
311  *  - N the number of channels modified for the VM
312  *  - 0 for error
313  */
314 int set_channel_status(const char *vm_name, unsigned *channel_list,
315                 unsigned len_channel_list, enum channel_status status);
316
317 /**
318  * Populates a pointer to struct vm_info associated with vm_name.
319  *
320  * @param vm_name
321  *  The name of the virtual machine to lookup.
322  *
323  *  @param vm_info
324  *   Pointer to a struct vm_info, this must be allocated prior to calling this
325  *   function.
326  *
327  * @return
328  *  - 0 on success.
329  *  - Negative on error.
330  */
331 int get_info_vm(const char *vm_name, struct vm_info *info);
332
333 /**
334  * Populates a table with all domains running and their physical cpu.
335  * All information is gathered through libvirt api.
336  *
337  * @param num_vm
338  *  modified to store number of active VMs
339  *
340  * @param num_vcpu
341     modified to store number of vcpus active
342  *
343  * @return
344  *   void
345  */
346 void get_all_vm(int *num_vm, int *num_vcpu);
347 #ifdef __cplusplus
348 }
349 #endif
350
351 #endif /* CHANNEL_MANAGER_H_ */