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