examples/vm_power: cpu frequency in host
[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 <rte_atomic.h>
43 #include "channel_commands.h"
44
45 /* Maximum name length including '\0' terminator */
46 #define CHANNEL_MGR_MAX_NAME_LEN    64
47
48 /* Maximum number of channels to each Virtual Machine */
49 #define CHANNEL_MGR_MAX_CHANNELS    64
50
51 /* Hypervisor Path for libvirt(qemu/KVM) */
52 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
53
54 /* File socket directory */
55 #define CHANNEL_MGR_SOCKET_PATH     "/tmp/powermonitor/"
56
57 /* Communication Channel Status */
58 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
59         CHANNEL_MGR_CHANNEL_CONNECTED,
60         CHANNEL_MGR_CHANNEL_DISABLED,
61         CHANNEL_MGR_CHANNEL_PROCESSING};
62
63 /* VM libvirt(qemu/KVM) connection status */
64 enum vm_status { CHANNEL_MGR_VM_INACTIVE = 0, CHANNEL_MGR_VM_ACTIVE};
65
66 /*
67  *  Represents a single and exclusive VM channel that exists between a guest and
68  *  the host.
69  */
70 struct channel_info {
71         char channel_path[PATH_MAX]; /**< Path to host socket */
72         volatile uint32_t status;    /**< Connection status(enum channel_status) */
73         int fd;                      /**< AF_UNIX socket fd */
74         unsigned channel_num;        /**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */
75         void *priv_info;             /**< Pointer to private info, do not modify */
76 };
77
78 /* Represents a single VM instance used to return internal information about
79  * a VM */
80 struct vm_info {
81         char name[CHANNEL_MGR_MAX_NAME_LEN];          /**< VM name */
82         enum vm_status status;                        /**< libvirt status */
83         uint64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS];    /**< pCPU mask for each vCPU */
84         unsigned num_vcpus;                           /**< number of vCPUS */
85         struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS]; /**< Array of channel_info */
86         unsigned num_channels;                        /**< Number of channels */
87 };
88
89 /**
90  * Initialize the Channel Manager resources and connect to the Hypervisor
91  * specified in path.
92  * This must be successfully called first before calling any other functions.
93  * It must only be call once;
94  *
95  * @param path
96  *  Must be a local path, e.g. qemu:///system.
97  *
98  * @return
99  *  - 0 on success.
100  *  - Negative on error.
101  */
102 int channel_manager_init(const char *path);
103
104 /**
105  * Free resources associated with the Channel Manager.
106  *
107  * @param path
108  *  Must be a local path, e.g. qemu:///system.
109  *
110  * @return
111  *  None
112  */
113 void channel_manager_exit(void);
114
115 /**
116  * Get the Physical CPU mask for VM lcore channel(vcpu), result is assigned to
117  * core_mask.
118  * It is not thread-safe.
119  *
120  * @param chan_info
121  *  Pointer to struct channel_info
122  *
123  * @param vcpu
124  *  The virtual CPU to query.
125  *
126  *
127  * @return
128  *  - 0 on error.
129  *  - >0 on success.
130  */
131 uint64_t get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu);
132
133 /**
134  * Set the Physical CPU mask for the specified vCPU.
135  * It is not thread-safe.
136  *
137  * @param name
138  *  Virtual Machine name to lookup
139  *
140  * @param vcpu
141  *  The virtual CPU to set.
142  *
143  * @param core_mask
144  *  The core mask of the physical CPU(s) to bind the vCPU
145  *
146  * @return
147  *  - 0 on success.
148  *  - Negative on error.
149  */
150 int set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask);
151
152 /**
153  * Set the Physical CPU for the specified vCPU.
154  * It is not thread-safe.
155  *
156  * @param name
157  *  Virtual Machine name to lookup
158  *
159  * @param vcpu
160  *  The virtual CPU to set.
161  *
162  * @param core_num
163  *  The core number of the physical CPU(s) to bind the vCPU
164  *
165  * @return
166  *  - 0 on success.
167  *  - Negative on error.
168  */
169 int set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num);
170 /**
171  * Add a VM as specified by name to the Channel Manager. The name must
172  * correspond to a valid libvirt domain name.
173  * This is required prior to adding channels.
174  * It is not thread-safe.
175  *
176  * @param name
177  *  Virtual Machine name to lookup.
178  *
179  * @return
180  *  - 0 on success.
181  *  - Negative on error.
182  */
183 int add_vm(const char *name);
184
185 /**
186  * Remove a previously added Virtual Machine from the Channel Manager
187  * It is not thread-safe.
188  *
189  * @param name
190  *  Virtual Machine name to lookup.
191  *
192  * @return
193  *  - 0 on success.
194  *  - Negative on error.
195  */
196 int remove_vm(const char *name);
197
198 /**
199  * Add all available channels to the VM as specified by name.
200  * Channels in the form of paths
201  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
202  * It is not thread-safe.
203  *
204  * @param name
205  *  Virtual Machine name to lookup.
206  *
207  * @return
208  *  - N the number of channels added for the VM
209  */
210 int add_all_channels(const char *vm_name);
211
212 /**
213  * Add the channel numbers in channel_list to the domain specified by name.
214  * Channels in the form of paths
215  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
216  * It is not thread-safe.
217  *
218  * @param name
219  *  Virtual Machine name to add channels.
220  *
221  * @param channel_list
222  *  Pointer to list of unsigned integers, representing the channel number to add
223  *  It must be allocated outside of this function.
224  *
225  * @param num_channels
226  *  The amount of channel numbers in channel_list
227  *
228  * @return
229  *  - N the number of channels added for the VM
230  *  - 0 for error
231  */
232 int add_channels(const char *vm_name, unsigned *channel_list,
233                 unsigned num_channels);
234
235 /**
236  * Remove a channel definition from the channel manager. This must only be
237  * called from the channel monitor thread.
238  *
239  * @param chan_info
240  *  Pointer to a valid struct channel_info.
241  *
242  * @return
243  *  - 0 on success.
244  *  - Negative on error.
245  */
246 int remove_channel(struct channel_info **chan_info_dptr);
247
248 /**
249  * For all channels associated with a Virtual Machine name, update the
250  * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
251  * CHANNEL_MGR_CHANNEL_DISABLED only.
252  *
253  *
254  * @param name
255  *  Virtual Machine name to modify all channels.
256  *
257  * @param status
258  *  The status to set each channel
259  *
260  * @param num_channels
261  *  The amount of channel numbers in channel_list
262  *
263  * @return
264  *  - N the number of channels added for the VM
265  *  - 0 for error
266  */
267 int set_channel_status_all(const char *name, enum channel_status status);
268
269 /**
270  * For all channels in channel_list associated with a Virtual Machine name
271  * update the connection status of each.
272  * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
273  * CHANNEL_MGR_CHANNEL_DISABLED only.
274  * It is not thread-safe.
275  *
276  * @param name
277  *  Virtual Machine name to add channels.
278  *
279  * @param channel_list
280  *  Pointer to list of unsigned integers, representing the channel numbers to
281  *  modify.
282  *  It must be allocated outside of this function.
283  *
284  * @param num_channels
285  *  The amount of channel numbers in channel_list
286  *
287  * @return
288  *  - N the number of channels modified for the VM
289  *  - 0 for error
290  */
291 int set_channel_status(const char *vm_name, unsigned *channel_list,
292                 unsigned len_channel_list, enum channel_status status);
293
294 /**
295  * Populates a pointer to struct vm_info associated with vm_name.
296  *
297  * @param vm_name
298  *  The name of the virtual machine to lookup.
299  *
300  *  @param vm_info
301  *   Pointer to a struct vm_info, this must be allocated prior to calling this
302  *   function.
303  *
304  * @return
305  *  - 0 on success.
306  *  - Negative on error.
307  */
308 int get_info_vm(const char *vm_name, struct vm_info *info);
309
310 #ifdef __cplusplus
311 }
312 #endif
313
314 #endif /* CHANNEL_MANAGER_H_ */