1 /*
2  * Copyright (C)2018-2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 /*
7 *****************************************************
8 NOTE: Any changes to this file may require rebuilding
9 IOC daemon (cbc_lifecycle) as they share this common
10 header file.
11 *****************************************************
12 */
13 
14 #ifndef ACRN_MANAGER_H
15 #define ACRN_MANAGER_H
16 
17 #include <stdlib.h>
18 #include "dm.h"
19 
20 #define MNGR_MSG_MAGIC	0x67736d206d6d76	/* that is char[8] "mngr msg", on X86 */
21 #define PATH_LEN		128
22 
23 #define ACRN_CONF_PATH			"/usr/share/acrn/conf"
24 #define ACRN_CONF_PATH_ADD		ACRN_CONF_PATH "/add"
25 #define ACRN_CONF_TIMER_LIST	ACRN_CONF_PATH "/timer_list"
26 
27 #define ACRN_DM_BASE_PATH	"/run/acrn"
28 #define ACRN_DM_SOCK_PATH	"/run/acrn/mngr"
29 
30 /* TODO: Revisit PARAM_LEN and see if size can be reduced */
31 #define PARAM_LEN	256
32 
33 struct mngr_msg {
34 	unsigned long long magic;	/* Make sure you get a mngr_msg */
35 	unsigned int msgid;
36 	unsigned long timestamp;
37 	union {
38 
39 		/* Arguments to rescan virtio-blk device */
40 		char devargs[PARAM_LEN];
41 
42 		/* ack of DM_STOP, DM_SUSPEND, DM_RESUME,
43 		   ACRND_TIMER, ACRND_STOP, ACRND_RESUME, RTC_TIMER */
44 		int err;
45 
46 		/* ack of WAKEUP_REASON */
47 		unsigned reason;
48 
49 		/* ack of DM_QUERY */
50 		int state;
51 
52 		/* req of ACRND_TIMER */
53 		struct req_acrnd_timer {
54 			char name[MAX_VM_NAME_LEN];
55 			time_t t;
56 		} acrnd_timer;
57 
58 		/* req of ACRND_STOP */
59 		struct req_acrnd_stop {
60 			int force;
61 			unsigned timeout;
62 		} acrnd_stop;
63 
64 		/* req of ACRND_SUSPEND */
65 		struct req_acrnd_suspend {
66 			int force;
67 			unsigned timeout;
68 		} acrnd_suspend;
69 
70 		/* req of ACRND_RESUME */
71 		struct req_acrnd_resume {
72 			int force;
73 			unsigned timeout;
74 		} acrnd_resume;
75 
76 		/* req of RTC_TIMER */
77 		struct req_rtc_timer {
78 			char vmname[MAX_VM_NAME_LEN];
79 			time_t t;
80 		} rtc_timer;
81 
82 	} data;
83 };
84 
85 /* mngr_msg event types */
86 enum msgid {
87 	MSG_MIN = 0,
88 	MSG_STR,		/* The message payload is a string, terminated with '\0' */
89 	MSG_MAX,
90 };
91 
92 /* DM handled message event types */
93 enum dm_msgid {
94 	DM_STOP = MSG_MAX + 1,	/* Stop this UOS */
95 	DM_SUSPEND,		/* Suspend this UOS from running state */
96 	DM_RESUME,		/* Resume this UOS from suspend state */
97 	DM_QUERY,		/* Ask power state of this UOS */
98 	DM_BLKRESCAN,		/* Rescan virtio-blk device for any changes in UOS */
99 	DM_MAX,
100 };
101 
102 /* DM handled message req/ack pairs */
103 
104 /* Acrnd handled message event types */
105 enum acrnd_msgid {
106 	/* DM -> Acrnd */
107 	ACRND_TIMER = DM_MAX + 1,	/* DM request to setup a launch timer */
108 	ACRND_REASON,		/* DM ask for updating wakeup reason */
109 	DM_NOTIFY,		/* DM notify Acrnd that state is changed */
110 
111 	/* Service-VM-LCS ->Acrnd */
112 	ACRND_STOP,		/* Service-VM-LCS request to Stop all User VM */
113 	ACRND_RESUME,		/* Service-VM-LCS request to Resume User VM */
114 	ACRND_SUSPEND,		/* Service-VM-LCS request to Suspend all User VM */
115 
116 	ACRND_MAX,
117 };
118 
119 /* Acrnd handled message req/ack pairs */
120 
121 /* SOS-LCS handled message event types */
122 enum sos_lcs_msgid {
123 	WAKEUP_REASON = ACRND_MAX + 1,	/* Acrnd/Acrnctl request wakeup reason */
124 	RTC_TIMER,		/* Acrnd request to setup RTC timer */
125 	SUSPEND,
126 	SHUTDOWN,
127 	REBOOT,
128 };
129 
130 /* helper functions */
131 #define MNGR_SERVER	1	/* create a server fd, which you can add handlers onto it */
132 #define MNGR_CLIENT	0	/* create a client, just send req and read ack */
133 
134 #define CHK_CREAT 1		/* create a directory, if not exist */
135 #define CHK_ONLY  0		/* check if the directory exist only */
136 
137 /**
138  * @brief create a descripter for vm management IPC
139  *
140  * @param name refer to a sock file under /run/acrn/mngr/[name].[pid].socket
141  * @param flags MNGR_SERVER to create a server, MNGR_CLIENT to create a client
142  *
143  * @return descripter ID (> 1024) on success, errno (< 0) on error.
144  */
145 int mngr_open_un(const char *name, int flags);
146 
147 /**
148  * @brief close descripter and release the resouces
149  *
150  * @param desc descripter to be closed
151  */
152 void mngr_close(int desc);
153 
154 /**
155  * @brief add a handler for message specified by msg
156  *
157  * @param desc descripter to register handler to
158  * @param id id of message to handle
159  * @param cb handler callback
160  * @param param param for the callback
161  * @return 0 on success, errno on error
162  */
163 int mngr_add_handler(int desc, unsigned id,
164 		     void (*cb) (struct mngr_msg * msg, int client_fd,
165 				 void *param), void *param);
166 
167 /**
168  * @brief send a message and wait for ack
169  *
170  * @param desc descripter created using mngr_open_un
171  * @param req pointer to message to send
172  * @param ack pointer to ack struct, NULL if no ack required
173  * @param timeout time to wait for ack, zero to blocking waiting
174  * @return len of ack messsage (0 if ack is NULL) on succes, errno on error
175  */
176 int mngr_send_msg(int desc, struct mngr_msg *req, struct mngr_msg *ack,
177 		  unsigned timeout);
178 
179 /**
180  * @brief check @path existence and create or report error accoding to the flag
181  *
182  * @param path path to check
183  * @param flags CHK_CREAT to create directory, CHK_ONLY check directory only
184  * @return 0 on success, -1 on error
185  */
186 int check_dir(const char *path, int flags);
187 
188 #endif				/* ACRN_MANAGER_H */
189