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