examples/power: add FIFO per core for JSON interface
[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 name length including '\0' terminator */
17 #define CHANNEL_MGR_MAX_NAME_LEN    64
18
19 /* Hypervisor Path for libvirt(qemu/KVM) */
20 #define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system"
21
22 /* File socket directory */
23 #define CHANNEL_MGR_SOCKET_PATH     "/tmp/powermonitor/"
24
25 /* FIFO file name template */
26 #define CHANNEL_MGR_FIFO_PATTERN_NAME   "fifo"
27
28 #ifndef UNIX_PATH_MAX
29 struct sockaddr_un _sockaddr_un;
30 #define UNIX_PATH_MAX sizeof(_sockaddr_un.sun_path)
31 #endif
32
33 #define MAX_CLIENTS 64
34 #define MAX_VCPUS 20
35
36
37 struct libvirt_vm_info {
38         const char *vm_name;
39         unsigned int pcpus[MAX_VCPUS];
40         uint8_t num_cpus;
41 };
42
43 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
44 /* Communication Channel Status */
45 enum channel_status { CHANNEL_MGR_CHANNEL_DISCONNECTED = 0,
46         CHANNEL_MGR_CHANNEL_CONNECTED,
47         CHANNEL_MGR_CHANNEL_DISABLED,
48         CHANNEL_MGR_CHANNEL_PROCESSING};
49
50 /* Communication Channel Type */
51 enum channel_type {
52         CHANNEL_TYPE_BINARY = 0,
53         CHANNEL_TYPE_INI,
54         CHANNEL_TYPE_JSON
55 };
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         enum channel_type type;      /**< Binary, ini, json, etc. */
70         void *priv_info;             /**< Pointer to private info, do not modify */
71 };
72
73 /* Represents a single VM instance used to return internal information about
74  * a VM */
75 struct vm_info {
76         char name[CHANNEL_MGR_MAX_NAME_LEN];          /**< VM name */
77         enum vm_status status;                        /**< libvirt status */
78         uint16_t pcpu_map[RTE_MAX_LCORE];             /**< pCPU map to vCPU */
79         unsigned num_vcpus;                           /**< number of vCPUS */
80         struct channel_info channels[RTE_MAX_LCORE];  /**< channel_info array */
81         unsigned num_channels;                        /**< Number of channels */
82 };
83
84 /**
85  * Initialize the Channel Manager resources and connect to the Hypervisor
86  * specified in path.
87  * This must be successfully called first before calling any other functions.
88  * It must only be call once;
89  *
90  * @param path
91  *  Must be a local path, e.g. qemu:///system.
92  *
93  * @return
94  *  - 0 on success.
95  *  - Negative on error.
96  */
97 int channel_manager_init(const char *path);
98
99 /**
100  * Free resources associated with the Channel Manager.
101  *
102  * @param path
103  *  Must be a local path, e.g. qemu:///system.
104  *
105  * @return
106  *  None
107  */
108 void channel_manager_exit(void);
109
110 /**
111  * Get the Physical CPU for VM lcore channel(vcpu).
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 uint16_t get_pcpu(struct channel_info *chan_info, unsigned int vcpu);
126
127 /**
128  * Set the Physical CPU 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_num
138  *  The core number of the physical CPU(s) to bind the vCPU
139  *
140  * @return
141  *  - 0 on success.
142  *  - Negative on error.
143  */
144 int set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu);
145
146 /**
147  * Add a VM as specified by name to the Channel Manager. The name must
148  * correspond to a valid libvirt domain name.
149  * This is required prior to adding channels.
150  * It is not thread-safe.
151  *
152  * @param name
153  *  Virtual Machine name to lookup.
154  *
155  * @return
156  *  - 0 on success.
157  *  - Negative on error.
158  */
159 int add_vm(const char *name);
160
161 /**
162  * Remove a previously added Virtual Machine from the Channel Manager
163  * It is not thread-safe.
164  *
165  * @param name
166  *  Virtual Machine name to lookup.
167  *
168  * @return
169  *  - 0 on success.
170  *  - Negative on error.
171  */
172 int remove_vm(const char *name);
173
174 /**
175  * Add all available channels to the VM as specified by name.
176  * Channels in the form of paths
177  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
178  * It is not thread-safe.
179  *
180  * @param name
181  *  Virtual Machine name to lookup.
182  *
183  * @return
184  *  - N the number of channels added for the VM
185  */
186 int add_all_channels(const char *vm_name);
187
188 /**
189  * Add the channel numbers in channel_list to the domain specified by name.
190  * Channels in the form of paths
191  * (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed.
192  * It is not thread-safe.
193  *
194  * @param name
195  *  Virtual Machine name to add channels.
196  *
197  * @param channel_list
198  *  Pointer to list of unsigned integers, representing the channel number to add
199  *  It must be allocated outside of this function.
200  *
201  * @param num_channels
202  *  The amount of channel numbers in channel_list
203  *
204  * @return
205  *  - N the number of channels added for the VM
206  *  - 0 for error
207  */
208 int add_channels(const char *vm_name, unsigned *channel_list,
209                 unsigned num_channels);
210
211 /**
212  * Set up fifos by which host applications can send command an policies
213  * through a fifo to the vm_power_manager
214  *
215  * @return
216  *  - 0 for success
217  */
218 int add_host_channels(void);
219
220 /**
221  * Remove a channel definition from the channel manager. This must only be
222  * called from the channel monitor thread.
223  *
224  * @param chan_info
225  *  Pointer to a valid struct channel_info.
226  *
227  * @return
228  *  - 0 on success.
229  *  - Negative on error.
230  */
231 int remove_channel(struct channel_info **chan_info_dptr);
232
233 /**
234  * For all channels associated with a Virtual Machine name, update the
235  * connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
236  * CHANNEL_MGR_CHANNEL_DISABLED only.
237  *
238  *
239  * @param name
240  *  Virtual Machine name to modify all channels.
241  *
242  * @param status
243  *  The status to set each channel
244  *
245  * @param num_channels
246  *  The amount of channel numbers in channel_list
247  *
248  * @return
249  *  - N the number of channels added for the VM
250  *  - 0 for error
251  */
252 int set_channel_status_all(const char *name, enum channel_status status);
253
254 /**
255  * For all channels in channel_list associated with a Virtual Machine name
256  * update the connection status of each.
257  * Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or
258  * CHANNEL_MGR_CHANNEL_DISABLED only.
259  * It is not thread-safe.
260  *
261  * @param name
262  *  Virtual Machine name to add channels.
263  *
264  * @param channel_list
265  *  Pointer to list of unsigned integers, representing the channel numbers to
266  *  modify.
267  *  It must be allocated outside of this function.
268  *
269  * @param num_channels
270  *  The amount of channel numbers in channel_list
271  *
272  * @return
273  *  - N the number of channels modified for the VM
274  *  - 0 for error
275  */
276 int set_channel_status(const char *vm_name, unsigned *channel_list,
277                 unsigned len_channel_list, enum channel_status status);
278
279 /**
280  * Populates a pointer to struct vm_info associated with vm_name.
281  *
282  * @param vm_name
283  *  The name of the virtual machine to lookup.
284  *
285  *  @param vm_info
286  *   Pointer to a struct vm_info, this must be allocated prior to calling this
287  *   function.
288  *
289  * @return
290  *  - 0 on success.
291  *  - Negative on error.
292  */
293 int get_info_vm(const char *vm_name, struct vm_info *info);
294
295 /**
296  * Populates a table with all domains running and their physical cpu.
297  * All information is gathered through libvirt api.
298  *
299  * @param num_vm
300  *  modified to store number of active VMs
301  *
302  * @param num_vcpu
303     modified to store number of vcpus active
304  *
305  * @return
306  *   void
307  */
308 void get_all_vm(int *num_vm, int *num_vcpu);
309 #ifdef __cplusplus
310 }
311 #endif
312
313 #endif /* CHANNEL_MANAGER_H_ */