shim_ipc.h 15 KB

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