examples/power: allow VM to use lcores over 63
[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         uint16_t pcpu_map[CHANNEL_CMDS_MAX_CPUS];     /**< pCPU map to 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 for VM lcore channel(vcpu).
119  * It is not thread-safe.
120  *
121  * @param chan_info
122  *  Pointer to struct channel_info
123  *
124  * @param vcpu
125  *  The virtual CPU to query.
126  *
127  *
128  * @return
129  *  - 0 on error.
130  *  - >0 on success.
131  */
132 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
133
134 /**
135  * Set the Physical CPU for the specified vCPU.
136  * It is not thread-safe.
137  *
138  * @param name
139  *  Virtual Machine name to lookup
140  *
141  * @param vcpu
142  *  The virtual CPU to set.
143  *
144  * @param core_num
145  *  The core number of the physical CPU(s) to bind the vCPU
146  *
147  * @return
148  *  - 0 on success.
149  *  - Negative on error.
150  */
151 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
152
153 /**
154  * Add a VM as specified by name to the Channel Manager. The name must
155  * correspond to a valid libvirt domain name.
156  * This is required prior to adding channels.
157  * It is not thread-safe.
158  *
159  * @param name
160  *  Virtual Machine name to lookup.
161  *
162  * @return
163  *  - 0 on success.
164  *  - Negative on error.
165  */
166 int add_vm(const char *name);
167
168 /**
169  * Remove a previously added Virtual Machine from the Channel Manager
170  * It is not thread-safe.
171  *
172  * @param name
173  *  Virtual Machine name to lookup.
174  *
175  * @return
176  *  - 0 on success.
177  *  - Negative on error.
178  */
179 int remove_vm(const char *name);
180
181 /**
182  * Add all available channels to the VM as specified by name.
183  * Channels in the form of paths
184  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
185  * It is not thread-safe.
186  *
187  * @param name
188  *  Virtual Machine name to lookup.
189  *
190  * @return
191  *  - N the number of channels added for the VM
192  */
193 int add_all_channels(const char *vm_name);
194
195 /**
196  * Add the channel numbers in channel_list to the domain specified by name.
197  * Channels in the form of paths
198  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
199  * It is not thread-safe.
200  *
201  * @param name
202  *  Virtual Machine name to add channels.
203  *
204  * @param channel_list
205  *  Pointer to list of unsigned integers, representing the channel number to add
206  *  It must be allocated outside of this function.
207  *
208  * @param num_channels
209  *  The amount of channel numbers in channel_list
210  *
211  * @return
212  *  - N the number of channels added for the VM
213  *  - 0 for error
214  */
215 int add_channels(const char *vm_name, unsigned *channel_list,
216                 unsigned num_channels);
217
218 /**
219  * Set up a fifo by which host applications can send command an policies
220  * through a fifo to the vm_power_manager
221  *
222  * @return
223  *  - 0 for success
224  */
225 int add_host_channel(void);
226
227 /**
228  * Remove a channel definition from the channel manager. This must only be
229  * called from the channel monitor thread.
230  *
231  * @param chan_info
232  *  Pointer to a valid struct channel_info.
233  *
234  * @return
235  *  - 0 on success.
236  *  - Negative on error.
237  */
238 int remove_channel(struct channel_info **chan_info_dptr);
239
240 /**
241  * For all channels associated with a Virtual Machine name, update the
242  * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
243  * CHANNEL_MGR_CHANNEL_DISABLED only.
244  *
245  *
246  * @param name
247  *  Virtual Machine name to modify all channels.
248  *
249  * @param status
250  *  The status to set each channel
251  *
252  * @param num_channels
253  *  The amount of channel numbers in channel_list
254  *
255  * @return
256  *  - N the number of channels added for the VM
257  *  - 0 for error
258  */
259 int set_channel_status_all(const char *name, enum channel_status status);
260
261 /**
262  * For all channels in channel_list associated with a Virtual Machine name
263  * update the connection status of each.
264  * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
265  * CHANNEL_MGR_CHANNEL_DISABLED only.
266  * It is not thread-safe.
267  *
268  * @param name
269  *  Virtual Machine name to add channels.
270  *
271  * @param channel_list
272  *  Pointer to list of unsigned integers, representing the channel numbers to
273  *  modify.
274  *  It must be allocated outside of this function.
275  *
276  * @param num_channels
277  *  The amount of channel numbers in channel_list
278  *
279  * @return
280  *  - N the number of channels modified for the VM
281  *  - 0 for error
282  */
283 int set_channel_status(const char *vm_name, unsigned *channel_list,
284                 unsigned len_channel_list, enum channel_status status);
285
286 /**
287  * Populates a pointer to struct vm_info associated with vm_name.
288  *
289  * @param vm_name
290  *  The name of the virtual machine to lookup.
291  *
292  *  @param vm_info
293  *   Pointer to a struct vm_info, this must be allocated prior to calling this
294  *   function.
295  *
296  * @return
297  *  - 0 on success.
298  *  - Negative on error.
299  */
300 int get_info_vm(const char *vm_name, struct vm_info *info);
301
302 /**
303  * Populates a table with all domains running and their physical cpu.
304  * All information is gathered through libvirt api.
305  *
306  * @param num_vm
307  *  modified to store number of active VMs
308  *
309  * @param num_vcpu
310     modified to store number of vcpus active
311  *
312  * @return
313  *   void
314  */
315 void get_all_vm(int *num_vm, int *num_vcpu);
316 #ifdef __cplusplus
317 }
318 #endif
319
320 #endif /* CHANNEL_MANAGER_H_ */