shim_ipc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_ipc.h
  15. *
  16. * Definitions of types and functions for IPC bookkeeping.
  17. */
  18. #ifndef _SHIM_IPC_H_
  19. #define _SHIM_IPC_H_
  20. #include <list.h>
  21. #include <pal.h>
  22. #include <shim_defs.h>
  23. #include <shim_handle.h>
  24. #include <shim_sysv.h>
  25. #include <shim_thread.h>
  26. #include <shim_types.h>
  27. DEFINE_LIST(shim_ipc_info);
  28. struct shim_ipc_info {
  29. IDTYPE vmid;
  30. struct shim_ipc_port* port;
  31. PAL_HANDLE pal_handle;
  32. struct shim_qstr uri;
  33. LIST_TYPE(shim_ipc_info) hlist;
  34. REFTYPE ref_count;
  35. };
  36. enum { PID_NS, SYSV_NS, TOTAL_NS };
  37. struct shim_process {
  38. IDTYPE vmid;
  39. struct shim_lock lock;
  40. int exit_code;
  41. struct shim_ipc_info* self;
  42. struct shim_ipc_info* parent;
  43. struct shim_ipc_info* ns[TOTAL_NS];
  44. };
  45. extern struct shim_process cur_process;
  46. #define IPC_MSG_MINIMAL_SIZE 48
  47. struct shim_ipc_msg {
  48. unsigned char code;
  49. size_t size;
  50. IDTYPE src, dst;
  51. unsigned long seq;
  52. #ifdef PROFILE
  53. unsigned long time;
  54. #endif
  55. char msg[];
  56. } __attribute__((packed));
  57. struct shim_ipc_port;
  58. struct shim_thread;
  59. DEFINE_LIST(shim_ipc_msg_duplex);
  60. struct shim_ipc_msg_duplex {
  61. struct shim_thread* thread;
  62. LIST_TYPE(shim_ipc_msg_duplex) list;
  63. int retval;
  64. void* private;
  65. struct shim_ipc_msg msg;
  66. };
  67. typedef void (*port_fini)(struct shim_ipc_port*, IDTYPE vmid, unsigned int exitcode);
  68. #define MAX_IPC_PORT_FINI_CB 3
  69. DEFINE_LIST(shim_ipc_port);
  70. DEFINE_LISTP(shim_ipc_msg_duplex);
  71. struct shim_ipc_port {
  72. PAL_HANDLE pal_handle;
  73. REFTYPE ref_count;
  74. LIST_TYPE(shim_ipc_port) list;
  75. LISTP_TYPE(shim_ipc_msg_duplex) msgs;
  76. struct shim_lock msgs_lock;
  77. port_fini fini[MAX_IPC_PORT_FINI_CB];
  78. IDTYPE type;
  79. IDTYPE vmid;
  80. };
  81. #define IPC_CALLBACK_ARGS struct shim_ipc_msg* msg, struct shim_ipc_port* port
  82. /* if callback return RESPONSE_CALLBACK, send a response even if the callback
  83. succeed. */
  84. #define RESPONSE_CALLBACK 1
  85. typedef int (*ipc_callback)(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  86. /* Basic message codes */
  87. enum {
  88. IPC_RESP = 0,
  89. IPC_CHECKPOINT,
  90. IPC_BASE_BOUND,
  91. };
  92. /* IPC_RESP: response for incoming messages */
  93. struct shim_ipc_resp {
  94. int retval;
  95. } __attribute__((packed));
  96. /* PID_CHECKPOINT: broadcast checkpointing */
  97. struct shim_ipc_checkpoint {
  98. IDTYPE cpsession;
  99. char cpdir[1];
  100. } __attribute__((packed));
  101. int ipc_checkpoint_send(const char* cpdir, IDTYPE cpsession);
  102. int ipc_checkpoint_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  103. /* Message code from child to parent */
  104. #define IPC_CLD_BASE IPC_BASE_BOUND
  105. enum {
  106. IPC_CLD_EXIT = IPC_CLD_BASE,
  107. #ifdef PROFILE
  108. IPC_CLD_PROFILE,
  109. #endif
  110. IPC_CLD_BOUND,
  111. };
  112. /* CLD_EXIT: thread exit */
  113. struct shim_ipc_cld_exit {
  114. IDTYPE ppid, tid;
  115. unsigned int exitcode;
  116. unsigned int term_signal;
  117. #ifdef PROFILE
  118. unsigned long time;
  119. #endif
  120. } __attribute__((packed));
  121. int ipc_cld_exit_send(IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned int term_signal);
  122. int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  123. #ifdef PROFILE
  124. #include <shim_profile.h>
  125. struct shim_ipc_cld_profile {
  126. unsigned long time;
  127. int nprofile;
  128. struct profile_val profile[];
  129. } __attribute__((packed));
  130. int ipc_cld_profile_send(void);
  131. int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  132. #endif
  133. /* Message code to namespace manager */
  134. #define IPC_PID_BASE IPC_CLD_BOUND
  135. #define NS pid
  136. #define NS_CAP PID
  137. #include "shim_ipc_ns.h"
  138. enum {
  139. IPC_PID_KILL = IPC_PID_TEMPLATE_BOUND,
  140. IPC_PID_GETSTATUS,
  141. IPC_PID_RETSTATUS,
  142. IPC_PID_GETMETA,
  143. IPC_PID_RETMETA,
  144. IPC_PID_NOP,
  145. IPC_PID_SENDRPC,
  146. IPC_PID_BOUND,
  147. };
  148. enum kill_type { KILL_THREAD, KILL_PROCESS, KILL_PGROUP, KILL_ALL };
  149. /* PID_KILL: send signal to certain pid */
  150. struct shim_ipc_pid_kill {
  151. IDTYPE sender;
  152. enum kill_type type;
  153. IDTYPE id;
  154. int signum;
  155. } __attribute__((packed));
  156. int ipc_pid_kill_send(IDTYPE sender, IDTYPE target, enum kill_type type, int signum);
  157. int ipc_pid_kill_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  158. struct pid_status {
  159. IDTYPE pid, tgid, pgid;
  160. } __attribute__((packed));
  161. /* PID_GETSTATUS: check if certain pid(s) exists */
  162. struct shim_ipc_pid_getstatus {
  163. int npids;
  164. IDTYPE pids[];
  165. } __attribute__((packed));
  166. int ipc_pid_getstatus_send(struct shim_ipc_port* port, IDTYPE dest, int npids, IDTYPE* pids,
  167. struct pid_status** status);
  168. int ipc_pid_getstatus_callback(IPC_CALLBACK_ARGS);
  169. /* PID_RETSTATUS: return status of pid(s) */
  170. struct shim_ipc_pid_retstatus {
  171. int nstatus;
  172. struct pid_status status[];
  173. } __attribute__((packed));
  174. int ipc_pid_retstatus_send(struct shim_ipc_port* port, IDTYPE dest, int nstatus,
  175. struct pid_status* status, unsigned long seq);
  176. int ipc_pid_retstatus_callback(IPC_CALLBACK_ARGS);
  177. /* PID_GETMETA: get metadata of certain pid */
  178. enum pid_meta_code {
  179. PID_META_CRED,
  180. PID_META_EXEC,
  181. PID_META_CWD,
  182. PID_META_ROOT,
  183. };
  184. struct shim_ipc_pid_getmeta {
  185. IDTYPE pid;
  186. enum pid_meta_code code;
  187. } __attribute__((packed));
  188. int ipc_pid_getmeta_send(IDTYPE pid, enum pid_meta_code code, void** data);
  189. int ipc_pid_getmeta_callback(IPC_CALLBACK_ARGS);
  190. /* PID_RETMETA: return metadata of certain pid */
  191. struct shim_ipc_pid_retmeta {
  192. IDTYPE pid;
  193. enum pid_meta_code code;
  194. int datasize;
  195. char data[];
  196. } __attribute__((packed));
  197. int ipc_pid_retmeta_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE pid,
  198. enum pid_meta_code code, const void* data, int datasize,
  199. unsigned long seq);
  200. int ipc_pid_retmeta_callback(IPC_CALLBACK_ARGS);
  201. /* PID_NOP: send junk message (for benchmarking) */
  202. struct shim_ipc_pid_nop {
  203. int count;
  204. char payload[];
  205. } __attribute__((packed));
  206. int ipc_pid_nop_send(struct shim_ipc_port* port, IDTYPE dest, int count, const void* buf, int len);
  207. int ipc_pid_nop_callback(IPC_CALLBACK_ARGS);
  208. /* PID_SENDRPC: send arbitary message (for benchmarking) */
  209. struct shim_ipc_pid_sendrpc {
  210. IDTYPE sender;
  211. int len;
  212. char payload[];
  213. } __attribute__((packed));
  214. int ipc_pid_sendrpc_send(IDTYPE pid, IDTYPE sender, const void* buf, int len);
  215. int ipc_pid_sendrpc_callback(IPC_CALLBACK_ARGS);
  216. #define IPC_SYSV_BASE IPC_PID_BOUND
  217. struct sysv_key {
  218. unsigned long key;
  219. enum sysv_type type;
  220. };
  221. #define NS sysv
  222. #define NS_CAP SYSV
  223. #define NS_KEY struct sysv_key
  224. #include "shim_ipc_ns.h"
  225. enum {
  226. IPC_SYSV_DELRES = IPC_SYSV_TEMPLATE_BOUND,
  227. IPC_SYSV_MOVRES,
  228. IPC_SYSV_MSGSND,
  229. IPC_SYSV_MSGRCV,
  230. IPC_SYSV_MSGMOV,
  231. IPC_SYSV_SEMOP,
  232. IPC_SYSV_SEMCTL,
  233. IPC_SYSV_SEMRET,
  234. IPC_SYSV_SEMMOV,
  235. #ifdef USE_SHARED_SEMAPHORE
  236. IPC_SYSV_SEMQUERY,
  237. IPC_SYSV_SEMREPLY,
  238. #endif
  239. IPC_SYSV_BOUND,
  240. };
  241. /* SYSV_DELRES */
  242. struct shim_ipc_sysv_delres {
  243. IDTYPE resid;
  244. enum sysv_type type;
  245. } __attribute__((packed));
  246. int ipc_sysv_delres_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE resid,
  247. enum sysv_type type);
  248. int ipc_sysv_delres_callback(IPC_CALLBACK_ARGS);
  249. /* SYSV_MOVRES */
  250. struct shim_ipc_sysv_movres {
  251. IDTYPE resid;
  252. enum sysv_type type;
  253. IDTYPE owner;
  254. LEASETYPE lease;
  255. char uri[1];
  256. } __attribute__((packed));
  257. int ipc_sysv_movres_send(struct sysv_client* client, IDTYPE owner, const char* uri, LEASETYPE lease,
  258. IDTYPE resid, enum sysv_type type);
  259. int ipc_sysv_movres_callback(IPC_CALLBACK_ARGS);
  260. /* SYSV_MSGSND */
  261. struct shim_ipc_sysv_msgsnd {
  262. IDTYPE msgid;
  263. long msgtype;
  264. char msg[];
  265. } __attribute__((packed));
  266. int ipc_sysv_msgsnd_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE msgid, long msgtype,
  267. const void* buf, size_t size, unsigned long seq);
  268. int ipc_sysv_msgsnd_callback(IPC_CALLBACK_ARGS);
  269. /* SYSV_MSGRCV */
  270. struct shim_ipc_sysv_msgrcv {
  271. IDTYPE msgid;
  272. long msgtype;
  273. size_t size;
  274. int flags;
  275. } __attribute__((packed));
  276. int ipc_sysv_msgrcv_send(IDTYPE msgid, long msgtype, int flags, void* buf, size_t size);
  277. int ipc_sysv_msgrcv_callback(IPC_CALLBACK_ARGS);
  278. /* SYSV_MSGMOV */
  279. struct shim_ipc_sysv_msgmov {
  280. IDTYPE msgid;
  281. LEASETYPE lease;
  282. unsigned short nscores;
  283. struct sysv_score scores[];
  284. } __attribute__((packed));
  285. int ipc_sysv_msgmov_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE msgid, LEASETYPE lease,
  286. struct sysv_score* scores, int nscores);
  287. int ipc_sysv_msgmov_callback(IPC_CALLBACK_ARGS);
  288. /* SYSV_SEMOP */
  289. struct shim_ipc_sysv_semop {
  290. IDTYPE semid;
  291. unsigned long timeout;
  292. int nsops;
  293. struct sembuf sops[];
  294. } __attribute__((packed));
  295. #define IPC_SEM_NOTIMEOUT ((unsigned long)-1)
  296. int ipc_sysv_semop_send(IDTYPE semid, struct sembuf* sops, int nsops, unsigned long timeout,
  297. unsigned long* seq);
  298. int ipc_sysv_semop_callback(IPC_CALLBACK_ARGS);
  299. /* SYSV_SEMCTL */
  300. struct shim_ipc_sysv_semctl {
  301. IDTYPE semid;
  302. int semnum;
  303. int cmd;
  304. size_t valsize;
  305. unsigned char vals[];
  306. } __attribute__((packed));
  307. int ipc_sysv_semctl_send(IDTYPE semid, int semnum, int cmd, void* vals, size_t valsize);
  308. int ipc_sysv_semctl_callback(IPC_CALLBACK_ARGS);
  309. /* SYSV_SEMRET */
  310. struct shim_ipc_sysv_semret {
  311. size_t valsize;
  312. unsigned char vals[];
  313. } __attribute__((packed));
  314. int ipc_sysv_semret_send(struct shim_ipc_port* port, IDTYPE dest, void* vals, size_t valsize,
  315. unsigned long seq);
  316. int ipc_sysv_semret_callback(IPC_CALLBACK_ARGS);
  317. /* SYSV_SEMMOV */
  318. struct shim_ipc_sysv_semmov {
  319. IDTYPE semid;
  320. LEASETYPE lease;
  321. unsigned short nsems, nsrcs, nscores;
  322. struct sem_backup sems[];
  323. } __attribute__((packed));
  324. int ipc_sysv_semmov_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE semid, LEASETYPE lease,
  325. struct sem_backup* sems, int nsems, struct sem_client_backup* srcs,
  326. int nsrcs, struct sysv_score* scores, int nscores);
  327. int ipc_sysv_semmov_callback(IPC_CALLBACK_ARGS);
  328. #ifdef USE_SHARED_SEMAPHORE
  329. /* SYSV_SEMQUERY */
  330. struct shim_ipc_sysv_semquery {
  331. IDTYPE semid;
  332. } __attribute__((packed));
  333. int ipc_sysv_semquery_send(IDTYPE semid, int* nsems, PAL_NUM** host_sem_ids);
  334. int ipc_sysv_semquery_callback(IPC_CALLBACK_ARGS);
  335. /* SYSV_SEMREPLY */
  336. struct shim_ipc_sysv_semreply {
  337. IDTYPE semid;
  338. int nsems;
  339. PAL_NUM host_sem_ids[];
  340. } __attribute__((packed));
  341. int ipc_sysv_semreply_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE semid, int nsems,
  342. PAL_NUM* host_sem_ids, unsigned long seq);
  343. int ipc_sysv_semreply_callback(IPC_CALLBACK_ARGS);
  344. #endif
  345. #define IPC_CODE_NUM IPC_SYSV_BOUND
  346. /* functions and routines */
  347. int init_ipc(void);
  348. int init_ipc_helper(void);
  349. struct shim_process* create_process(bool dup_cur_process);
  350. void free_process(struct shim_process* process);
  351. struct shim_ipc_info* create_ipc_info_cur_process(bool is_self_ipc_info);
  352. int get_ipc_info_cur_process(struct shim_ipc_info** pinfo);
  353. enum {
  354. LISTEN, /* listening */
  355. SERVER, /* connect as a server */
  356. KEEPALIVE, /* keep the connetion alive */
  357. DIRCLD, /* direct child */
  358. DIRPRT, /* direct parent */
  359. NS_PORT_CONSTS(PID) NS_PORT_CONSTS(SYSV)
  360. };
  361. enum {
  362. IPC_PORT_LISTEN = 1 << LISTEN,
  363. IPC_PORT_SERVER = 1 << SERVER,
  364. IPC_PORT_KEEPALIVE = 1 << KEEPALIVE,
  365. IPC_PORT_DIRCLD = 1 << DIRCLD,
  366. IPC_PORT_DIRPRT = 1 << DIRPRT,
  367. NS_PORT_TYPES(PID) NS_PORT_TYPES(SYSV)
  368. };
  369. /* general-purpose routines */
  370. void add_ipc_port_by_id(IDTYPE vmid, PAL_HANDLE hdl, IDTYPE type, port_fini fini,
  371. struct shim_ipc_port** portptr);
  372. void add_ipc_port(struct shim_ipc_port* port, IDTYPE vmid, IDTYPE type, port_fini fini);
  373. void del_ipc_port_fini(struct shim_ipc_port* port, unsigned int exitcode);
  374. struct shim_ipc_port* lookup_ipc_port(IDTYPE vmid, IDTYPE type);
  375. void get_ipc_port(struct shim_ipc_port* port);
  376. void put_ipc_port(struct shim_ipc_port* port);
  377. void del_all_ipc_ports(void);
  378. struct shim_ipc_info* create_ipc_info(IDTYPE vmid, const char* uri, size_t len);
  379. void get_ipc_info(struct shim_ipc_info* port);
  380. void put_ipc_info(struct shim_ipc_info* port);
  381. struct shim_ipc_info* create_ipc_info_in_list(IDTYPE vmid, const char* uri, size_t len);
  382. void put_ipc_info_in_list(struct shim_ipc_info* info);
  383. struct shim_ipc_info* lookup_ipc_info(IDTYPE vmid);
  384. static_always_inline size_t get_ipc_msg_size(size_t payload) {
  385. size_t size = sizeof(struct shim_ipc_msg) + payload;
  386. return (size > IPC_MSG_MINIMAL_SIZE) ? size : IPC_MSG_MINIMAL_SIZE;
  387. }
  388. static_always_inline size_t get_ipc_msg_duplex_size(size_t payload) {
  389. assert(sizeof(struct shim_ipc_msg_duplex) >= sizeof(struct shim_ipc_msg));
  390. return get_ipc_msg_size(payload) +
  391. (sizeof(struct shim_ipc_msg_duplex) - sizeof(struct shim_ipc_msg));
  392. }
  393. void init_ipc_msg(struct shim_ipc_msg* msg, int code, size_t size, IDTYPE dest);
  394. void init_ipc_msg_duplex(struct shim_ipc_msg_duplex* msg, int code, size_t size, IDTYPE dest);
  395. struct shim_ipc_msg_duplex* pop_ipc_msg_duplex(struct shim_ipc_port* port, unsigned long seq);
  396. int broadcast_ipc(struct shim_ipc_msg* msg, int target_type, struct shim_ipc_port* exclude_port);
  397. int send_ipc_message(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
  398. int send_ipc_message_duplex(struct shim_ipc_msg_duplex* msg, struct shim_ipc_port* port,
  399. unsigned long* seq, void* private_data);
  400. int send_response_ipc_message(struct shim_ipc_port* port, IDTYPE dest, int ret, unsigned long seq);
  401. void ipc_port_with_child_fini(struct shim_ipc_port* port, IDTYPE vmid, unsigned int exitcode);
  402. struct shim_thread* terminate_ipc_helper(void);
  403. int prepare_ns_leaders(void);
  404. #endif /* _SHIM_IPC_H_ */