cpg.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. /*
  2. * Copyright (c) 2006-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * Author: Jan Friesse (jfriesse@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #ifdef HAVE_ALLOCA_H
  37. #include <alloca.h>
  38. #endif
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <netinet/in.h>
  44. #include <sys/uio.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <time.h>
  51. #include <assert.h>
  52. #include <unistd.h>
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55. #include <sys/mman.h>
  56. #include <qb/qbmap.h>
  57. #include <corosync/corotypes.h>
  58. #include <qb/qbipc_common.h>
  59. #include <corosync/corodefs.h>
  60. #include <corosync/list.h>
  61. #include <corosync/logsys.h>
  62. #include <corosync/coroapi.h>
  63. #include <corosync/cpg.h>
  64. #include <corosync/ipc_cpg.h>
  65. #include "service.h"
  66. LOGSYS_DECLARE_SUBSYS ("CPG");
  67. #define GROUP_HASH_SIZE 32
  68. enum cpg_message_req_types {
  69. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  70. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  71. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  72. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  73. MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD = 4,
  74. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 5
  75. };
  76. struct zcb_mapped {
  77. struct list_head list;
  78. void *addr;
  79. size_t size;
  80. };
  81. /*
  82. * state` exec deliver
  83. * match group name, pid -> if matched deliver for YES:
  84. * XXX indicates impossible state
  85. *
  86. * join leave mcast
  87. * UNJOINED XXX XXX NO
  88. * LEAVE_STARTED XXX YES(unjoined_enter) YES
  89. * JOIN_STARTED YES(join_started_enter) XXX NO
  90. * JOIN_COMPLETED XXX NO YES
  91. *
  92. * join_started_enter
  93. * set JOIN_COMPLETED
  94. * add entry to process_info list
  95. * unjoined_enter
  96. * set UNJOINED
  97. * delete entry from process_info list
  98. *
  99. *
  100. * library accept join error codes
  101. * UNJOINED YES(CS_OK) set JOIN_STARTED
  102. * LEAVE_STARTED NO(CS_ERR_BUSY)
  103. * JOIN_STARTED NO(CS_ERR_EXIST)
  104. * JOIN_COMPlETED NO(CS_ERR_EXIST)
  105. *
  106. * library accept leave error codes
  107. * UNJOINED NO(CS_ERR_NOT_EXIST)
  108. * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
  109. * JOIN_STARTED NO(CS_ERR_BUSY)
  110. * JOIN_COMPLETED YES(CS_OK) set LEAVE_STARTED
  111. *
  112. * library accept mcast
  113. * UNJOINED NO(CS_ERR_NOT_EXIST)
  114. * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
  115. * JOIN_STARTED YES(CS_OK)
  116. * JOIN_COMPLETED YES(CS_OK)
  117. */
  118. enum cpd_state {
  119. CPD_STATE_UNJOINED,
  120. CPD_STATE_LEAVE_STARTED,
  121. CPD_STATE_JOIN_STARTED,
  122. CPD_STATE_JOIN_COMPLETED
  123. };
  124. enum cpg_sync_state {
  125. CPGSYNC_DOWNLIST,
  126. CPGSYNC_JOINLIST
  127. };
  128. enum cpg_downlist_state_e {
  129. CPG_DOWNLIST_NONE,
  130. CPG_DOWNLIST_WAITING_FOR_MESSAGES,
  131. CPG_DOWNLIST_APPLYING,
  132. };
  133. static enum cpg_downlist_state_e downlist_state;
  134. static struct list_head downlist_messages_head;
  135. static struct list_head joinlist_messages_head;
  136. struct cpg_pd {
  137. void *conn;
  138. mar_cpg_name_t group_name;
  139. uint32_t pid;
  140. enum cpd_state cpd_state;
  141. unsigned int flags;
  142. int initial_totem_conf_sent;
  143. struct list_head list;
  144. struct list_head iteration_instance_list_head;
  145. struct list_head zcb_mapped_list_head;
  146. };
  147. struct cpg_iteration_instance {
  148. hdb_handle_t handle;
  149. struct list_head list;
  150. struct list_head items_list_head; /* List of process_info */
  151. struct list_head *current_pointer;
  152. };
  153. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  154. DECLARE_LIST_INIT(cpg_pd_list_head);
  155. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  156. static unsigned int my_member_list_entries;
  157. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  158. static unsigned int my_old_member_list_entries = 0;
  159. static struct corosync_api_v1 *api = NULL;
  160. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  161. static mar_cpg_ring_id_t last_sync_ring_id;
  162. struct process_info {
  163. unsigned int nodeid;
  164. uint32_t pid;
  165. mar_cpg_name_t group;
  166. struct list_head list; /* on the group_info members list */
  167. };
  168. DECLARE_LIST_INIT(process_info_list_head);
  169. struct join_list_entry {
  170. uint32_t pid;
  171. mar_cpg_name_t group_name;
  172. };
  173. /*
  174. * Service Interfaces required by service_message_handler struct
  175. */
  176. static char *cpg_exec_init_fn (struct corosync_api_v1 *);
  177. static int cpg_lib_init_fn (void *conn);
  178. static int cpg_lib_exit_fn (void *conn);
  179. static void message_handler_req_exec_cpg_procjoin (
  180. const void *message,
  181. unsigned int nodeid);
  182. static void message_handler_req_exec_cpg_procleave (
  183. const void *message,
  184. unsigned int nodeid);
  185. static void message_handler_req_exec_cpg_joinlist (
  186. const void *message,
  187. unsigned int nodeid);
  188. static void message_handler_req_exec_cpg_mcast (
  189. const void *message,
  190. unsigned int nodeid);
  191. static void message_handler_req_exec_cpg_downlist_old (
  192. const void *message,
  193. unsigned int nodeid);
  194. static void message_handler_req_exec_cpg_downlist (
  195. const void *message,
  196. unsigned int nodeid);
  197. static void exec_cpg_procjoin_endian_convert (void *msg);
  198. static void exec_cpg_joinlist_endian_convert (void *msg);
  199. static void exec_cpg_mcast_endian_convert (void *msg);
  200. static void exec_cpg_downlist_endian_convert_old (void *msg);
  201. static void exec_cpg_downlist_endian_convert (void *msg);
  202. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  203. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  204. static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
  205. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  206. static void message_handler_req_lib_cpg_membership (void *conn,
  207. const void *message);
  208. static void message_handler_req_lib_cpg_local_get (void *conn,
  209. const void *message);
  210. static void message_handler_req_lib_cpg_iteration_initialize (
  211. void *conn,
  212. const void *message);
  213. static void message_handler_req_lib_cpg_iteration_next (
  214. void *conn,
  215. const void *message);
  216. static void message_handler_req_lib_cpg_iteration_finalize (
  217. void *conn,
  218. const void *message);
  219. static void message_handler_req_lib_cpg_zc_alloc (
  220. void *conn,
  221. const void *message);
  222. static void message_handler_req_lib_cpg_zc_free (
  223. void *conn,
  224. const void *message);
  225. static void message_handler_req_lib_cpg_zc_execute (
  226. void *conn,
  227. const void *message);
  228. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  229. static int cpg_exec_send_downlist(void);
  230. static int cpg_exec_send_joinlist(void);
  231. static void downlist_messages_delete (void);
  232. static void downlist_master_choose_and_send (void);
  233. static void joinlist_inform_clients (void);
  234. static void joinlist_messages_delete (void);
  235. static void cpg_sync_init (
  236. const unsigned int *trans_list,
  237. size_t trans_list_entries,
  238. const unsigned int *member_list,
  239. size_t member_list_entries,
  240. const struct memb_ring_id *ring_id);
  241. static int cpg_sync_process (void);
  242. static void cpg_sync_activate (void);
  243. static void cpg_sync_abort (void);
  244. static void do_proc_join(
  245. const mar_cpg_name_t *name,
  246. uint32_t pid,
  247. unsigned int nodeid,
  248. int reason);
  249. static int notify_lib_totem_membership (
  250. void *conn,
  251. int member_list_entries,
  252. const unsigned int *member_list);
  253. static inline int zcb_all_free (
  254. struct cpg_pd *cpd);
  255. static char *cpg_print_group_name (
  256. const mar_cpg_name_t *group);
  257. /*
  258. * Library Handler Definition
  259. */
  260. static struct corosync_lib_handler cpg_lib_engine[] =
  261. {
  262. { /* 0 - MESSAGE_REQ_CPG_JOIN */
  263. .lib_handler_fn = message_handler_req_lib_cpg_join,
  264. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  265. },
  266. { /* 1 - MESSAGE_REQ_CPG_LEAVE */
  267. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  268. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  269. },
  270. { /* 2 - MESSAGE_REQ_CPG_MCAST */
  271. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  272. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  273. },
  274. { /* 3 - MESSAGE_REQ_CPG_MEMBERSHIP */
  275. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  276. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  277. },
  278. { /* 4 - MESSAGE_REQ_CPG_LOCAL_GET */
  279. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  280. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  281. },
  282. { /* 5 - MESSAGE_REQ_CPG_ITERATIONINITIALIZE */
  283. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  284. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  285. },
  286. { /* 6 - MESSAGE_REQ_CPG_ITERATIONNEXT */
  287. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  288. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  289. },
  290. { /* 7 - MESSAGE_REQ_CPG_ITERATIONFINALIZE */
  291. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  292. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  293. },
  294. { /* 8 - MESSAGE_REQ_CPG_FINALIZE */
  295. .lib_handler_fn = message_handler_req_lib_cpg_finalize,
  296. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  297. },
  298. { /* 9 */
  299. .lib_handler_fn = message_handler_req_lib_cpg_zc_alloc,
  300. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  301. },
  302. { /* 10 */
  303. .lib_handler_fn = message_handler_req_lib_cpg_zc_free,
  304. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  305. },
  306. { /* 11 */
  307. .lib_handler_fn = message_handler_req_lib_cpg_zc_execute,
  308. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  309. },
  310. };
  311. static struct corosync_exec_handler cpg_exec_engine[] =
  312. {
  313. { /* 0 - MESSAGE_REQ_EXEC_CPG_PROCJOIN */
  314. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  315. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  316. },
  317. { /* 1 - MESSAGE_REQ_EXEC_CPG_PROCLEAVE */
  318. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  319. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  320. },
  321. { /* 2 - MESSAGE_REQ_EXEC_CPG_JOINLIST */
  322. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  323. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  324. },
  325. { /* 3 - MESSAGE_REQ_EXEC_CPG_MCAST */
  326. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  327. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  328. },
  329. { /* 4 - MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD */
  330. .exec_handler_fn = message_handler_req_exec_cpg_downlist_old,
  331. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert_old
  332. },
  333. { /* 5 - MESSAGE_REQ_EXEC_CPG_DOWNLIST */
  334. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  335. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  336. },
  337. };
  338. struct corosync_service_engine cpg_service_engine = {
  339. .name = "corosync cluster closed process group service v1.01",
  340. .id = CPG_SERVICE,
  341. .priority = 1,
  342. .private_data_size = sizeof (struct cpg_pd),
  343. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  344. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  345. .lib_init_fn = cpg_lib_init_fn,
  346. .lib_exit_fn = cpg_lib_exit_fn,
  347. .lib_engine = cpg_lib_engine,
  348. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  349. .exec_init_fn = cpg_exec_init_fn,
  350. .exec_dump_fn = NULL,
  351. .exec_engine = cpg_exec_engine,
  352. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  353. .sync_init = cpg_sync_init,
  354. .sync_process = cpg_sync_process,
  355. .sync_activate = cpg_sync_activate,
  356. .sync_abort = cpg_sync_abort
  357. };
  358. struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  359. {
  360. return (&cpg_service_engine);
  361. }
  362. struct req_exec_cpg_procjoin {
  363. struct qb_ipc_request_header header __attribute__((aligned(8)));
  364. mar_cpg_name_t group_name __attribute__((aligned(8)));
  365. mar_uint32_t pid __attribute__((aligned(8)));
  366. mar_uint32_t reason __attribute__((aligned(8)));
  367. };
  368. struct req_exec_cpg_mcast {
  369. struct qb_ipc_request_header header __attribute__((aligned(8)));
  370. mar_cpg_name_t group_name __attribute__((aligned(8)));
  371. mar_uint32_t msglen __attribute__((aligned(8)));
  372. mar_uint32_t pid __attribute__((aligned(8)));
  373. mar_message_source_t source __attribute__((aligned(8)));
  374. mar_uint8_t message[] __attribute__((aligned(8)));
  375. };
  376. struct req_exec_cpg_downlist_old {
  377. struct qb_ipc_request_header header __attribute__((aligned(8)));
  378. mar_uint32_t left_nodes __attribute__((aligned(8)));
  379. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  380. };
  381. struct req_exec_cpg_downlist {
  382. struct qb_ipc_request_header header __attribute__((aligned(8)));
  383. /* merge decisions */
  384. mar_uint32_t old_members __attribute__((aligned(8)));
  385. /* downlist below */
  386. mar_uint32_t left_nodes __attribute__((aligned(8)));
  387. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  388. };
  389. struct downlist_msg {
  390. mar_uint32_t sender_nodeid;
  391. mar_uint32_t old_members __attribute__((aligned(8)));
  392. mar_uint32_t left_nodes __attribute__((aligned(8)));
  393. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  394. struct list_head list;
  395. };
  396. struct joinlist_msg {
  397. mar_uint32_t sender_nodeid;
  398. uint32_t pid;
  399. mar_cpg_name_t group_name;
  400. struct list_head list;
  401. };
  402. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  403. /*
  404. * Function print group name. It's not reentrant
  405. */
  406. static char *cpg_print_group_name(const mar_cpg_name_t *group)
  407. {
  408. static char res[CPG_MAX_NAME_LENGTH * 4 + 1];
  409. int dest_pos = 0;
  410. char c;
  411. int i;
  412. for (i = 0; i < group->length; i++) {
  413. c = group->value[i];
  414. if (c >= ' ' && c < 0x7f && c != '\\') {
  415. res[dest_pos++] = c;
  416. } else {
  417. if (c == '\\') {
  418. res[dest_pos++] = '\\';
  419. res[dest_pos++] = '\\';
  420. } else {
  421. snprintf(res + dest_pos, sizeof(res) - dest_pos, "\\x%02X", c);
  422. dest_pos += 4;
  423. }
  424. }
  425. }
  426. res[dest_pos] = 0;
  427. return (res);
  428. }
  429. static void cpg_sync_init (
  430. const unsigned int *trans_list,
  431. size_t trans_list_entries,
  432. const unsigned int *member_list,
  433. size_t member_list_entries,
  434. const struct memb_ring_id *ring_id)
  435. {
  436. int entries;
  437. int i, j;
  438. int found;
  439. my_sync_state = CPGSYNC_DOWNLIST;
  440. memcpy (my_member_list, member_list, member_list_entries *
  441. sizeof (unsigned int));
  442. my_member_list_entries = member_list_entries;
  443. last_sync_ring_id.nodeid = ring_id->rep.nodeid;
  444. last_sync_ring_id.seq = ring_id->seq;
  445. downlist_state = CPG_DOWNLIST_WAITING_FOR_MESSAGES;
  446. entries = 0;
  447. /*
  448. * Determine list of nodeids for downlist message
  449. */
  450. for (i = 0; i < my_old_member_list_entries; i++) {
  451. found = 0;
  452. for (j = 0; j < trans_list_entries; j++) {
  453. if (my_old_member_list[i] == trans_list[j]) {
  454. found = 1;
  455. break;
  456. }
  457. }
  458. if (found == 0) {
  459. g_req_exec_cpg_downlist.nodeids[entries++] =
  460. my_old_member_list[i];
  461. }
  462. }
  463. g_req_exec_cpg_downlist.left_nodes = entries;
  464. }
  465. static int cpg_sync_process (void)
  466. {
  467. int res = -1;
  468. if (my_sync_state == CPGSYNC_DOWNLIST) {
  469. res = cpg_exec_send_downlist();
  470. if (res == -1) {
  471. return (-1);
  472. }
  473. my_sync_state = CPGSYNC_JOINLIST;
  474. }
  475. if (my_sync_state == CPGSYNC_JOINLIST) {
  476. res = cpg_exec_send_joinlist();
  477. }
  478. return (res);
  479. }
  480. static void cpg_sync_activate (void)
  481. {
  482. memcpy (my_old_member_list, my_member_list,
  483. my_member_list_entries * sizeof (unsigned int));
  484. my_old_member_list_entries = my_member_list_entries;
  485. if (downlist_state == CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  486. downlist_master_choose_and_send ();
  487. }
  488. joinlist_inform_clients ();
  489. downlist_messages_delete ();
  490. downlist_state = CPG_DOWNLIST_NONE;
  491. joinlist_messages_delete ();
  492. notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
  493. }
  494. static void cpg_sync_abort (void)
  495. {
  496. downlist_state = CPG_DOWNLIST_NONE;
  497. downlist_messages_delete ();
  498. joinlist_messages_delete ();
  499. }
  500. static int notify_lib_totem_membership (
  501. void *conn,
  502. int member_list_entries,
  503. const unsigned int *member_list)
  504. {
  505. struct list_head *iter;
  506. char *buf;
  507. int size;
  508. struct res_lib_cpg_totem_confchg_callback *res;
  509. size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
  510. sizeof(mar_uint32_t) * (member_list_entries);
  511. buf = alloca(size);
  512. if (!buf)
  513. return CS_ERR_LIBRARY;
  514. res = (struct res_lib_cpg_totem_confchg_callback *)buf;
  515. res->member_list_entries = member_list_entries;
  516. res->header.size = size;
  517. res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
  518. res->header.error = CS_OK;
  519. memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
  520. memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
  521. if (conn == NULL) {
  522. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  523. struct cpg_pd *cpg_pd = list_entry (iter, struct cpg_pd, list);
  524. api->ipc_dispatch_send (cpg_pd->conn, buf, size);
  525. }
  526. } else {
  527. api->ipc_dispatch_send (conn, buf, size);
  528. }
  529. return CS_OK;
  530. }
  531. static int notify_lib_joinlist(
  532. const mar_cpg_name_t *group_name,
  533. void *conn,
  534. int joined_list_entries,
  535. mar_cpg_address_t *joined_list,
  536. int left_list_entries,
  537. mar_cpg_address_t *left_list,
  538. int id)
  539. {
  540. int size;
  541. char *buf;
  542. struct list_head *iter;
  543. int count;
  544. struct res_lib_cpg_confchg_callback *res;
  545. mar_cpg_address_t *retgi;
  546. count = 0;
  547. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  548. struct process_info *pi = list_entry (iter, struct process_info, list);
  549. if (mar_name_compare (&pi->group, group_name) == 0) {
  550. int i;
  551. int founded = 0;
  552. for (i = 0; i < left_list_entries; i++) {
  553. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  554. founded++;
  555. }
  556. }
  557. if (!founded)
  558. count++;
  559. }
  560. }
  561. size = sizeof(struct res_lib_cpg_confchg_callback) +
  562. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  563. buf = alloca(size);
  564. if (!buf)
  565. return CS_ERR_LIBRARY;
  566. res = (struct res_lib_cpg_confchg_callback *)buf;
  567. res->joined_list_entries = joined_list_entries;
  568. res->left_list_entries = left_list_entries;
  569. res->member_list_entries = count;
  570. retgi = res->member_list;
  571. res->header.size = size;
  572. res->header.id = id;
  573. res->header.error = CS_OK;
  574. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  575. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  576. struct process_info *pi=list_entry (iter, struct process_info, list);
  577. if (mar_name_compare (&pi->group, group_name) == 0) {
  578. int i;
  579. int founded = 0;
  580. for (i = 0;i < left_list_entries; i++) {
  581. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  582. founded++;
  583. }
  584. }
  585. if (!founded) {
  586. retgi->nodeid = pi->nodeid;
  587. retgi->pid = pi->pid;
  588. retgi++;
  589. }
  590. }
  591. }
  592. if (left_list_entries) {
  593. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  594. retgi += left_list_entries;
  595. }
  596. if (joined_list_entries) {
  597. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  598. retgi += joined_list_entries;
  599. }
  600. if (conn) {
  601. api->ipc_dispatch_send (conn, buf, size);
  602. } else {
  603. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  604. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  605. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  606. assert (joined_list_entries <= 1);
  607. if (joined_list_entries) {
  608. if (joined_list[0].pid == cpd->pid &&
  609. joined_list[0].nodeid == api->totem_nodeid_get()) {
  610. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  611. }
  612. }
  613. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  614. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  615. api->ipc_dispatch_send (cpd->conn, buf, size);
  616. }
  617. if (left_list_entries) {
  618. if (left_list[0].pid == cpd->pid &&
  619. left_list[0].nodeid == api->totem_nodeid_get() &&
  620. left_list[0].reason == CONFCHG_CPG_REASON_LEAVE) {
  621. cpd->pid = 0;
  622. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  623. cpd->cpd_state = CPD_STATE_UNJOINED;
  624. }
  625. }
  626. }
  627. }
  628. }
  629. /*
  630. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  631. */
  632. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  633. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  634. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  635. cpd->initial_totem_conf_sent = 1;
  636. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  637. }
  638. }
  639. return CS_OK;
  640. }
  641. static void downlist_log(const char *msg, struct downlist_msg* dl)
  642. {
  643. log_printf (LOG_DEBUG,
  644. "%s: sender %s; members(old:%d left:%d)",
  645. msg,
  646. api->totem_ifaces_print(dl->sender_nodeid),
  647. dl->old_members,
  648. dl->left_nodes);
  649. }
  650. static struct downlist_msg* downlist_master_choose (void)
  651. {
  652. struct downlist_msg *cmp;
  653. struct downlist_msg *best = NULL;
  654. struct list_head *iter;
  655. uint32_t cmp_members;
  656. uint32_t best_members;
  657. uint32_t i;
  658. int ignore_msg;
  659. for (iter = downlist_messages_head.next;
  660. iter != &downlist_messages_head;
  661. iter = iter->next) {
  662. cmp = list_entry(iter, struct downlist_msg, list);
  663. downlist_log("comparing", cmp);
  664. ignore_msg = 0;
  665. for (i = 0; i < cmp->left_nodes; i++) {
  666. if (cmp->nodeids[i] == api->totem_nodeid_get()) {
  667. log_printf (LOG_DEBUG, "Ignoring this entry because I'm in the left list\n");
  668. ignore_msg = 1;
  669. break;
  670. }
  671. }
  672. if (ignore_msg) {
  673. continue ;
  674. }
  675. if (best == NULL) {
  676. best = cmp;
  677. continue;
  678. }
  679. best_members = best->old_members - best->left_nodes;
  680. cmp_members = cmp->old_members - cmp->left_nodes;
  681. if (cmp_members > best_members) {
  682. best = cmp;
  683. } else if (cmp_members == best_members) {
  684. if (cmp->old_members > best->old_members) {
  685. best = cmp;
  686. } else if (cmp->old_members == best->old_members) {
  687. if (cmp->sender_nodeid < best->sender_nodeid) {
  688. best = cmp;
  689. }
  690. }
  691. }
  692. }
  693. assert (best != NULL);
  694. return best;
  695. }
  696. static void downlist_master_choose_and_send (void)
  697. {
  698. struct downlist_msg *stored_msg;
  699. struct list_head *iter;
  700. struct process_info *left_pi;
  701. qb_map_t *group_map;
  702. struct cpg_name cpg_group;
  703. mar_cpg_name_t group;
  704. struct confchg_data{
  705. struct cpg_name cpg_group;
  706. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  707. int left_list_entries;
  708. struct list_head list;
  709. } *pcd;
  710. qb_map_iter_t *miter;
  711. int i, size;
  712. downlist_state = CPG_DOWNLIST_APPLYING;
  713. stored_msg = downlist_master_choose ();
  714. if (!stored_msg) {
  715. log_printf (LOGSYS_LEVEL_DEBUG, "NO chosen downlist");
  716. return;
  717. }
  718. downlist_log("chosen downlist", stored_msg);
  719. group_map = qb_skiplist_create();
  720. /*
  721. * only the cpg groups included in left nodes should receive
  722. * confchg event, so we will collect these cpg groups and
  723. * relative left_lists here.
  724. */
  725. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  726. struct process_info *pi = list_entry(iter, struct process_info, list);
  727. iter = iter->next;
  728. left_pi = NULL;
  729. for (i = 0; i < stored_msg->left_nodes; i++) {
  730. if (pi->nodeid == stored_msg->nodeids[i]) {
  731. left_pi = pi;
  732. break;
  733. }
  734. }
  735. if (left_pi) {
  736. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  737. cpg_group.value[cpg_group.length] = 0;
  738. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  739. if (pcd == NULL) {
  740. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  741. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  742. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  743. }
  744. size = pcd->left_list_entries;
  745. pcd->left_list[size].nodeid = left_pi->nodeid;
  746. pcd->left_list[size].pid = left_pi->pid;
  747. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  748. pcd->left_list_entries++;
  749. list_del (&left_pi->list);
  750. free (left_pi);
  751. }
  752. }
  753. /* send only one confchg event per cpg group */
  754. miter = qb_map_iter_create(group_map);
  755. while (qb_map_iter_next(miter, (void **)&pcd)) {
  756. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  757. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  758. for (i=0; i<pcd->left_list_entries; i++) {
  759. log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
  760. i, cpg_print_group_name(&group),
  761. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  762. pcd->left_list[i].pid);
  763. }
  764. /* send confchg event */
  765. notify_lib_joinlist(&group, NULL,
  766. 0, NULL,
  767. pcd->left_list_entries,
  768. pcd->left_list,
  769. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  770. free(pcd);
  771. }
  772. qb_map_iter_free(miter);
  773. qb_map_destroy(group_map);
  774. }
  775. static void joinlist_inform_clients (void)
  776. {
  777. struct joinlist_msg *stored_msg;
  778. struct list_head *iter;
  779. unsigned int i;
  780. i = 0;
  781. for (iter = joinlist_messages_head.next;
  782. iter != &joinlist_messages_head;
  783. iter = iter->next) {
  784. stored_msg = list_entry(iter, struct joinlist_msg, list);
  785. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  786. i++, cpg_print_group_name(&stored_msg->group_name),
  787. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  788. stored_msg->pid);
  789. /* Ignore our own messages */
  790. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  791. continue ;
  792. }
  793. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  794. CONFCHG_CPG_REASON_NODEUP);
  795. }
  796. }
  797. static void downlist_messages_delete (void)
  798. {
  799. struct downlist_msg *stored_msg;
  800. struct list_head *iter, *iter_next;
  801. for (iter = downlist_messages_head.next;
  802. iter != &downlist_messages_head;
  803. iter = iter_next) {
  804. iter_next = iter->next;
  805. stored_msg = list_entry(iter, struct downlist_msg, list);
  806. list_del (&stored_msg->list);
  807. free (stored_msg);
  808. }
  809. }
  810. static void joinlist_messages_delete (void)
  811. {
  812. struct joinlist_msg *stored_msg;
  813. struct list_head *iter, *iter_next;
  814. for (iter = joinlist_messages_head.next;
  815. iter != &joinlist_messages_head;
  816. iter = iter_next) {
  817. iter_next = iter->next;
  818. stored_msg = list_entry(iter, struct joinlist_msg, list);
  819. list_del (&stored_msg->list);
  820. free (stored_msg);
  821. }
  822. list_init (&joinlist_messages_head);
  823. }
  824. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  825. {
  826. #ifdef COROSYNC_SOLARIS
  827. logsys_subsys_init();
  828. #endif
  829. list_init (&downlist_messages_head);
  830. list_init (&joinlist_messages_head);
  831. api = corosync_api;
  832. return (NULL);
  833. }
  834. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  835. {
  836. struct list_head *iter, *iter_next;
  837. struct process_info *pi;
  838. for (iter = cpg_iteration_instance->items_list_head.next;
  839. iter != &cpg_iteration_instance->items_list_head;
  840. iter = iter_next) {
  841. iter_next = iter->next;
  842. pi = list_entry (iter, struct process_info, list);
  843. list_del (&pi->list);
  844. free (pi);
  845. }
  846. list_del (&cpg_iteration_instance->list);
  847. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  848. }
  849. static void cpg_pd_finalize (struct cpg_pd *cpd)
  850. {
  851. struct list_head *iter, *iter_next;
  852. struct cpg_iteration_instance *cpii;
  853. zcb_all_free(cpd);
  854. for (iter = cpd->iteration_instance_list_head.next;
  855. iter != &cpd->iteration_instance_list_head;
  856. iter = iter_next) {
  857. iter_next = iter->next;
  858. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  859. cpg_iteration_instance_finalize (cpii);
  860. }
  861. list_del (&cpd->list);
  862. }
  863. static int cpg_lib_exit_fn (void *conn)
  864. {
  865. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  866. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  867. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  868. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  869. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  870. }
  871. cpg_pd_finalize (cpd);
  872. api->ipc_refcnt_dec (conn);
  873. return (0);
  874. }
  875. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  876. {
  877. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  878. struct iovec req_exec_cpg_iovec;
  879. int result;
  880. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  881. req_exec_cpg_procjoin.pid = pid;
  882. req_exec_cpg_procjoin.reason = reason;
  883. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  884. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  885. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  886. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  887. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  888. return (result);
  889. }
  890. /* Can byteswap join & leave messages */
  891. static void exec_cpg_procjoin_endian_convert (void *msg)
  892. {
  893. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  894. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  895. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  896. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  897. }
  898. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  899. {
  900. char *msg = msg_v;
  901. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  902. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  903. swab_mar_int32_t (&res->size);
  904. while ((const char*)jle < msg + res->size) {
  905. jle->pid = swab32(jle->pid);
  906. swab_mar_cpg_name_t (&jle->group_name);
  907. jle++;
  908. }
  909. }
  910. static void exec_cpg_downlist_endian_convert_old (void *msg)
  911. {
  912. }
  913. static void exec_cpg_downlist_endian_convert (void *msg)
  914. {
  915. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  916. unsigned int i;
  917. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  918. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  919. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  920. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  921. }
  922. }
  923. static void exec_cpg_mcast_endian_convert (void *msg)
  924. {
  925. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  926. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  927. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  928. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  929. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  930. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  931. }
  932. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  933. struct list_head *iter;
  934. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  935. struct process_info *pi = list_entry (iter, struct process_info, list);
  936. iter = iter->next;
  937. if (pi->pid == pid && pi->nodeid == nodeid &&
  938. mar_name_compare (&pi->group, group_name) == 0) {
  939. return pi;
  940. }
  941. }
  942. return NULL;
  943. }
  944. static void do_proc_join(
  945. const mar_cpg_name_t *name,
  946. uint32_t pid,
  947. unsigned int nodeid,
  948. int reason)
  949. {
  950. struct process_info *pi;
  951. struct process_info *pi_entry;
  952. mar_cpg_address_t notify_info;
  953. struct list_head *list;
  954. struct list_head *list_to_add = NULL;
  955. if (process_info_find (name, pid, nodeid) != NULL) {
  956. return ;
  957. }
  958. pi = malloc (sizeof (struct process_info));
  959. if (!pi) {
  960. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  961. return;
  962. }
  963. pi->nodeid = nodeid;
  964. pi->pid = pid;
  965. memcpy(&pi->group, name, sizeof(*name));
  966. list_init(&pi->list);
  967. /*
  968. * Insert new process in sorted order so synchronization works properly
  969. */
  970. list_to_add = &process_info_list_head;
  971. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  972. pi_entry = list_entry(list, struct process_info, list);
  973. if (pi_entry->nodeid > pi->nodeid ||
  974. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  975. break;
  976. }
  977. list_to_add = list;
  978. }
  979. list_add (&pi->list, list_to_add);
  980. notify_info.pid = pi->pid;
  981. notify_info.nodeid = nodeid;
  982. notify_info.reason = reason;
  983. notify_lib_joinlist(&pi->group, NULL,
  984. 1, &notify_info,
  985. 0, NULL,
  986. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  987. }
  988. static void message_handler_req_exec_cpg_downlist_old (
  989. const void *message,
  990. unsigned int nodeid)
  991. {
  992. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node %d",
  993. nodeid);
  994. }
  995. static void message_handler_req_exec_cpg_downlist(
  996. const void *message,
  997. unsigned int nodeid)
  998. {
  999. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1000. int i;
  1001. struct list_head *iter;
  1002. struct downlist_msg *stored_msg;
  1003. int found;
  1004. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  1005. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  1006. req_exec_cpg_downlist->left_nodes, downlist_state);
  1007. return;
  1008. }
  1009. stored_msg = malloc (sizeof (struct downlist_msg));
  1010. stored_msg->sender_nodeid = nodeid;
  1011. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  1012. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  1013. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  1014. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  1015. list_init (&stored_msg->list);
  1016. list_add (&stored_msg->list, &downlist_messages_head);
  1017. for (i = 0; i < my_member_list_entries; i++) {
  1018. found = 0;
  1019. for (iter = downlist_messages_head.next;
  1020. iter != &downlist_messages_head;
  1021. iter = iter->next) {
  1022. stored_msg = list_entry(iter, struct downlist_msg, list);
  1023. if (my_member_list[i] == stored_msg->sender_nodeid) {
  1024. found = 1;
  1025. }
  1026. }
  1027. if (!found) {
  1028. return;
  1029. }
  1030. }
  1031. downlist_master_choose_and_send ();
  1032. }
  1033. static void message_handler_req_exec_cpg_procjoin (
  1034. const void *message,
  1035. unsigned int nodeid)
  1036. {
  1037. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1038. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d (%s) for pid %u",
  1039. nodeid,
  1040. api->totem_ifaces_print(nodeid),
  1041. (unsigned int)req_exec_cpg_procjoin->pid);
  1042. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1043. req_exec_cpg_procjoin->pid, nodeid,
  1044. CONFCHG_CPG_REASON_JOIN);
  1045. }
  1046. static void message_handler_req_exec_cpg_procleave (
  1047. const void *message,
  1048. unsigned int nodeid)
  1049. {
  1050. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1051. struct process_info *pi;
  1052. struct list_head *iter;
  1053. mar_cpg_address_t notify_info;
  1054. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d", nodeid);
  1055. notify_info.pid = req_exec_cpg_procjoin->pid;
  1056. notify_info.nodeid = nodeid;
  1057. notify_info.reason = req_exec_cpg_procjoin->reason;
  1058. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  1059. 0, NULL,
  1060. 1, &notify_info,
  1061. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1062. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  1063. pi = list_entry(iter, struct process_info, list);
  1064. iter = iter->next;
  1065. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  1066. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  1067. list_del (&pi->list);
  1068. free (pi);
  1069. }
  1070. }
  1071. }
  1072. /* Got a proclist from another node */
  1073. static void message_handler_req_exec_cpg_joinlist (
  1074. const void *message_v,
  1075. unsigned int nodeid)
  1076. {
  1077. const char *message = message_v;
  1078. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1079. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1080. struct joinlist_msg *stored_msg;
  1081. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x",
  1082. nodeid);
  1083. while ((const char*)jle < message + res->size) {
  1084. stored_msg = malloc (sizeof (struct joinlist_msg));
  1085. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1086. stored_msg->sender_nodeid = nodeid;
  1087. stored_msg->pid = jle->pid;
  1088. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1089. list_init (&stored_msg->list);
  1090. list_add (&stored_msg->list, &joinlist_messages_head);
  1091. jle++;
  1092. }
  1093. }
  1094. static void message_handler_req_exec_cpg_mcast (
  1095. const void *message,
  1096. unsigned int nodeid)
  1097. {
  1098. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1099. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1100. int msglen = req_exec_cpg_mcast->msglen;
  1101. struct list_head *iter, *pi_iter;
  1102. struct cpg_pd *cpd;
  1103. struct iovec iovec[2];
  1104. int known_node = 0;
  1105. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1106. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1107. res_lib_cpg_mcast.msglen = msglen;
  1108. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1109. res_lib_cpg_mcast.nodeid = nodeid;
  1110. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1111. sizeof(mar_cpg_name_t));
  1112. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1113. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1114. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1115. iovec[1].iov_len = msglen;
  1116. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  1117. cpd = list_entry(iter, struct cpg_pd, list);
  1118. iter = iter->next;
  1119. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1120. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1121. if (!known_node) {
  1122. /* Try to find, if we know the node */
  1123. for (pi_iter = process_info_list_head.next;
  1124. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  1125. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  1126. if (pi->nodeid == nodeid &&
  1127. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1128. known_node = 1;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. if (!known_node) {
  1134. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1135. return ;
  1136. }
  1137. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1138. }
  1139. }
  1140. }
  1141. static int cpg_exec_send_downlist(void)
  1142. {
  1143. struct iovec iov;
  1144. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1145. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1146. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1147. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1148. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1149. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1150. }
  1151. static int cpg_exec_send_joinlist(void)
  1152. {
  1153. int count = 0;
  1154. struct list_head *iter;
  1155. struct qb_ipc_response_header *res;
  1156. char *buf;
  1157. struct join_list_entry *jle;
  1158. struct iovec req_exec_cpg_iovec;
  1159. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1160. struct process_info *pi = list_entry (iter, struct process_info, list);
  1161. if (pi->nodeid == api->totem_nodeid_get ()) {
  1162. count++;
  1163. }
  1164. }
  1165. /* Nothing to send */
  1166. if (!count)
  1167. return 0;
  1168. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1169. if (!buf) {
  1170. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1171. return -1;
  1172. }
  1173. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1174. res = (struct qb_ipc_response_header *)buf;
  1175. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1176. struct process_info *pi = list_entry (iter, struct process_info, list);
  1177. if (pi->nodeid == api->totem_nodeid_get ()) {
  1178. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1179. jle->pid = pi->pid;
  1180. jle++;
  1181. }
  1182. }
  1183. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1184. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1185. req_exec_cpg_iovec.iov_base = buf;
  1186. req_exec_cpg_iovec.iov_len = res->size;
  1187. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1188. }
  1189. static int cpg_lib_init_fn (void *conn)
  1190. {
  1191. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1192. memset (cpd, 0, sizeof(struct cpg_pd));
  1193. cpd->conn = conn;
  1194. list_add (&cpd->list, &cpg_pd_list_head);
  1195. list_init (&cpd->iteration_instance_list_head);
  1196. list_init (&cpd->zcb_mapped_list_head);
  1197. api->ipc_refcnt_inc (conn);
  1198. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1199. return (0);
  1200. }
  1201. /* Join message from the library */
  1202. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1203. {
  1204. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1205. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1206. struct res_lib_cpg_join res_lib_cpg_join;
  1207. cs_error_t error = CS_OK;
  1208. struct list_head *iter;
  1209. /* Test, if we don't have same pid and group name joined */
  1210. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  1211. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  1212. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1213. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1214. /* We have same pid and group name joined -> return error */
  1215. error = CS_ERR_EXIST;
  1216. goto response_send;
  1217. }
  1218. }
  1219. /*
  1220. * Same check must be done in process info list, because there may be not yet delivered
  1221. * leave of client.
  1222. */
  1223. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1224. struct process_info *pi = list_entry (iter, struct process_info, list);
  1225. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1226. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1227. /* We have same pid and group name joined -> return error */
  1228. error = CS_ERR_TRY_AGAIN;
  1229. goto response_send;
  1230. }
  1231. }
  1232. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1233. error = CS_ERR_NAME_TOO_LONG;
  1234. goto response_send;
  1235. }
  1236. switch (cpd->cpd_state) {
  1237. case CPD_STATE_UNJOINED:
  1238. error = CS_OK;
  1239. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1240. cpd->pid = req_lib_cpg_join->pid;
  1241. cpd->flags = req_lib_cpg_join->flags;
  1242. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1243. sizeof (cpd->group_name));
  1244. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1245. &req_lib_cpg_join->group_name,
  1246. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1247. break;
  1248. case CPD_STATE_LEAVE_STARTED:
  1249. error = CS_ERR_BUSY;
  1250. break;
  1251. case CPD_STATE_JOIN_STARTED:
  1252. error = CS_ERR_EXIST;
  1253. break;
  1254. case CPD_STATE_JOIN_COMPLETED:
  1255. error = CS_ERR_EXIST;
  1256. break;
  1257. }
  1258. response_send:
  1259. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1260. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1261. res_lib_cpg_join.header.error = error;
  1262. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1263. }
  1264. /* Leave message from the library */
  1265. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1266. {
  1267. struct res_lib_cpg_leave res_lib_cpg_leave;
  1268. cs_error_t error = CS_OK;
  1269. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1270. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1271. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1272. switch (cpd->cpd_state) {
  1273. case CPD_STATE_UNJOINED:
  1274. error = CS_ERR_NOT_EXIST;
  1275. break;
  1276. case CPD_STATE_LEAVE_STARTED:
  1277. error = CS_ERR_NOT_EXIST;
  1278. break;
  1279. case CPD_STATE_JOIN_STARTED:
  1280. error = CS_ERR_BUSY;
  1281. break;
  1282. case CPD_STATE_JOIN_COMPLETED:
  1283. error = CS_OK;
  1284. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1285. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1286. &req_lib_cpg_leave->group_name,
  1287. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1288. CONFCHG_CPG_REASON_LEAVE);
  1289. break;
  1290. }
  1291. /* send return */
  1292. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1293. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1294. res_lib_cpg_leave.header.error = error;
  1295. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1296. }
  1297. /* Finalize message from library */
  1298. static void message_handler_req_lib_cpg_finalize (
  1299. void *conn,
  1300. const void *message)
  1301. {
  1302. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1303. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1304. cs_error_t error = CS_OK;
  1305. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1306. /*
  1307. * We will just remove cpd from list. After this call, connection will be
  1308. * closed on lib side, and cpg_lib_exit_fn will be called
  1309. */
  1310. list_del (&cpd->list);
  1311. list_init (&cpd->list);
  1312. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1313. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1314. res_lib_cpg_finalize.header.error = error;
  1315. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1316. sizeof (res_lib_cpg_finalize));
  1317. }
  1318. static int
  1319. memory_map (
  1320. const char *path,
  1321. size_t bytes,
  1322. void **buf)
  1323. {
  1324. int32_t fd;
  1325. void *addr_orig;
  1326. void *addr;
  1327. int32_t res;
  1328. fd = open (path, O_RDWR, 0600);
  1329. unlink (path);
  1330. if (fd == -1) {
  1331. return (-1);
  1332. }
  1333. res = ftruncate (fd, bytes);
  1334. if (res == -1) {
  1335. goto error_close_unlink;
  1336. }
  1337. addr_orig = mmap (NULL, bytes, PROT_NONE,
  1338. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  1339. if (addr_orig == MAP_FAILED) {
  1340. goto error_close_unlink;
  1341. }
  1342. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  1343. MAP_FIXED | MAP_SHARED, fd, 0);
  1344. if (addr != addr_orig) {
  1345. munmap(addr_orig, bytes);
  1346. goto error_close_unlink;
  1347. }
  1348. #ifdef COROSYNC_BSD
  1349. madvise(addr, bytes, MADV_NOSYNC);
  1350. #endif
  1351. res = close (fd);
  1352. if (res) {
  1353. return (-1);
  1354. }
  1355. *buf = addr_orig;
  1356. return (0);
  1357. error_close_unlink:
  1358. close (fd);
  1359. unlink(path);
  1360. return -1;
  1361. }
  1362. static inline int zcb_alloc (
  1363. struct cpg_pd *cpd,
  1364. const char *path_to_file,
  1365. size_t size,
  1366. void **addr)
  1367. {
  1368. struct zcb_mapped *zcb_mapped;
  1369. unsigned int res;
  1370. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1371. if (zcb_mapped == NULL) {
  1372. return (-1);
  1373. }
  1374. res = memory_map (
  1375. path_to_file,
  1376. size,
  1377. addr);
  1378. if (res == -1) {
  1379. free (zcb_mapped);
  1380. return (-1);
  1381. }
  1382. list_init (&zcb_mapped->list);
  1383. zcb_mapped->addr = *addr;
  1384. zcb_mapped->size = size;
  1385. list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1386. return (0);
  1387. }
  1388. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1389. {
  1390. unsigned int res;
  1391. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1392. list_del (&zcb_mapped->list);
  1393. free (zcb_mapped);
  1394. return (res);
  1395. }
  1396. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1397. {
  1398. struct list_head *list;
  1399. struct zcb_mapped *zcb_mapped;
  1400. unsigned int res = 0;
  1401. for (list = cpd->zcb_mapped_list_head.next;
  1402. list != &cpd->zcb_mapped_list_head; list = list->next) {
  1403. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1404. if (zcb_mapped->addr == addr) {
  1405. res = zcb_free (zcb_mapped);
  1406. break;
  1407. }
  1408. }
  1409. return (res);
  1410. }
  1411. static inline int zcb_all_free (
  1412. struct cpg_pd *cpd)
  1413. {
  1414. struct list_head *list;
  1415. struct zcb_mapped *zcb_mapped;
  1416. for (list = cpd->zcb_mapped_list_head.next;
  1417. list != &cpd->zcb_mapped_list_head;) {
  1418. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1419. list = list->next;
  1420. zcb_free (zcb_mapped);
  1421. }
  1422. return (0);
  1423. }
  1424. union u {
  1425. uint64_t server_addr;
  1426. void *server_ptr;
  1427. };
  1428. static uint64_t void2serveraddr (void *server_ptr)
  1429. {
  1430. union u u;
  1431. u.server_ptr = server_ptr;
  1432. return (u.server_addr);
  1433. }
  1434. static void *serveraddr2void (uint64_t server_addr)
  1435. {
  1436. union u u;
  1437. u.server_addr = server_addr;
  1438. return (u.server_ptr);
  1439. };
  1440. static void message_handler_req_lib_cpg_zc_alloc (
  1441. void *conn,
  1442. const void *message)
  1443. {
  1444. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1445. struct qb_ipc_response_header res_header;
  1446. void *addr = NULL;
  1447. struct coroipcs_zc_header *zc_header;
  1448. unsigned int res;
  1449. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1450. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1451. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1452. &addr);
  1453. assert(res == 0);
  1454. zc_header = (struct coroipcs_zc_header *)addr;
  1455. zc_header->server_address = void2serveraddr(addr);
  1456. res_header.size = sizeof (struct qb_ipc_response_header);
  1457. res_header.id = 0;
  1458. api->ipc_response_send (conn,
  1459. &res_header,
  1460. res_header.size);
  1461. }
  1462. static void message_handler_req_lib_cpg_zc_free (
  1463. void *conn,
  1464. const void *message)
  1465. {
  1466. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1467. struct qb_ipc_response_header res_header;
  1468. void *addr = NULL;
  1469. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1470. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1471. addr = serveraddr2void (hdr->server_address);
  1472. zcb_by_addr_free (cpd, addr);
  1473. res_header.size = sizeof (struct qb_ipc_response_header);
  1474. res_header.id = 0;
  1475. api->ipc_response_send (
  1476. conn, &res_header,
  1477. res_header.size);
  1478. }
  1479. /* Mcast message from the library */
  1480. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1481. {
  1482. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1483. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1484. mar_cpg_name_t group_name = cpd->group_name;
  1485. struct iovec req_exec_cpg_iovec[2];
  1486. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1487. int msglen = req_lib_cpg_mcast->msglen;
  1488. int result;
  1489. cs_error_t error = CS_ERR_NOT_EXIST;
  1490. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p", conn);
  1491. switch (cpd->cpd_state) {
  1492. case CPD_STATE_UNJOINED:
  1493. error = CS_ERR_NOT_EXIST;
  1494. break;
  1495. case CPD_STATE_LEAVE_STARTED:
  1496. error = CS_ERR_NOT_EXIST;
  1497. break;
  1498. case CPD_STATE_JOIN_STARTED:
  1499. error = CS_OK;
  1500. break;
  1501. case CPD_STATE_JOIN_COMPLETED:
  1502. error = CS_OK;
  1503. break;
  1504. }
  1505. if (error == CS_OK) {
  1506. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1507. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1508. MESSAGE_REQ_EXEC_CPG_MCAST);
  1509. req_exec_cpg_mcast.pid = cpd->pid;
  1510. req_exec_cpg_mcast.msglen = msglen;
  1511. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1512. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1513. sizeof(mar_cpg_name_t));
  1514. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1515. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1516. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1517. req_exec_cpg_iovec[1].iov_len = msglen;
  1518. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1519. assert(result == 0);
  1520. } else {
  1521. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1522. conn, group_name.value, cpd->cpd_state, error);
  1523. }
  1524. }
  1525. static void message_handler_req_lib_cpg_zc_execute (
  1526. void *conn,
  1527. const void *message)
  1528. {
  1529. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1530. struct qb_ipc_request_header *header;
  1531. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1532. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1533. struct iovec req_exec_cpg_iovec[2];
  1534. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1535. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1536. int result;
  1537. cs_error_t error = CS_ERR_NOT_EXIST;
  1538. log_printf(LOGSYS_LEVEL_DEBUG, "got ZC mcast request on %p", conn);
  1539. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1540. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1541. switch (cpd->cpd_state) {
  1542. case CPD_STATE_UNJOINED:
  1543. error = CS_ERR_NOT_EXIST;
  1544. break;
  1545. case CPD_STATE_LEAVE_STARTED:
  1546. error = CS_ERR_NOT_EXIST;
  1547. break;
  1548. case CPD_STATE_JOIN_STARTED:
  1549. error = CS_OK;
  1550. break;
  1551. case CPD_STATE_JOIN_COMPLETED:
  1552. error = CS_OK;
  1553. break;
  1554. }
  1555. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1556. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1557. if (error == CS_OK) {
  1558. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1559. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1560. MESSAGE_REQ_EXEC_CPG_MCAST);
  1561. req_exec_cpg_mcast.pid = cpd->pid;
  1562. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1563. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1564. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1565. sizeof(mar_cpg_name_t));
  1566. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1567. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1568. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1569. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1570. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1571. if (result == 0) {
  1572. res_lib_cpg_mcast.header.error = CS_OK;
  1573. } else {
  1574. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1575. }
  1576. } else {
  1577. res_lib_cpg_mcast.header.error = error;
  1578. }
  1579. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1580. sizeof (res_lib_cpg_mcast));
  1581. }
  1582. static void message_handler_req_lib_cpg_membership (void *conn,
  1583. const void *message)
  1584. {
  1585. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1586. (struct req_lib_cpg_membership_get *)message;
  1587. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1588. struct list_head *iter;
  1589. int member_count = 0;
  1590. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1591. res_lib_cpg_membership_get.header.error = CS_OK;
  1592. res_lib_cpg_membership_get.header.size =
  1593. sizeof (struct req_lib_cpg_membership_get);
  1594. for (iter = process_info_list_head.next;
  1595. iter != &process_info_list_head; iter = iter->next) {
  1596. struct process_info *pi = list_entry (iter, struct process_info, list);
  1597. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1598. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1599. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1600. member_count += 1;
  1601. }
  1602. }
  1603. res_lib_cpg_membership_get.member_count = member_count;
  1604. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1605. sizeof (res_lib_cpg_membership_get));
  1606. }
  1607. static void message_handler_req_lib_cpg_local_get (void *conn,
  1608. const void *message)
  1609. {
  1610. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1611. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1612. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1613. res_lib_cpg_local_get.header.error = CS_OK;
  1614. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1615. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1616. sizeof (res_lib_cpg_local_get));
  1617. }
  1618. static void message_handler_req_lib_cpg_iteration_initialize (
  1619. void *conn,
  1620. const void *message)
  1621. {
  1622. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1623. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1624. hdb_handle_t cpg_iteration_handle = 0;
  1625. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1626. struct list_head *iter, *iter2;
  1627. struct cpg_iteration_instance *cpg_iteration_instance;
  1628. cs_error_t error = CS_OK;
  1629. int res;
  1630. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1631. /* Because between calling this function and *next can be some operations which will
  1632. * change list, we must do full copy.
  1633. */
  1634. /*
  1635. * Create new iteration instance
  1636. */
  1637. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1638. &cpg_iteration_handle);
  1639. if (res != 0) {
  1640. error = CS_ERR_NO_MEMORY;
  1641. goto response_send;
  1642. }
  1643. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1644. if (res != 0) {
  1645. error = CS_ERR_BAD_HANDLE;
  1646. goto error_destroy;
  1647. }
  1648. list_init (&cpg_iteration_instance->items_list_head);
  1649. cpg_iteration_instance->handle = cpg_iteration_handle;
  1650. /*
  1651. * Create copy of process_info list "grouped by" group name
  1652. */
  1653. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1654. struct process_info *pi = list_entry (iter, struct process_info, list);
  1655. struct process_info *new_pi;
  1656. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1657. /*
  1658. * Try to find processed group name in our list new list
  1659. */
  1660. int found = 0;
  1661. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1662. iter2 != &cpg_iteration_instance->items_list_head;
  1663. iter2 = iter2->next) {
  1664. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1665. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1666. found = 1;
  1667. break;
  1668. }
  1669. }
  1670. if (found) {
  1671. /*
  1672. * We have this name in list -> don't add
  1673. */
  1674. continue ;
  1675. }
  1676. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1677. /*
  1678. * Test pi group name with request
  1679. */
  1680. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1681. /*
  1682. * Not same -> don't add
  1683. */
  1684. continue ;
  1685. }
  1686. new_pi = malloc (sizeof (struct process_info));
  1687. if (!new_pi) {
  1688. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1689. error = CS_ERR_NO_MEMORY;
  1690. goto error_put_destroy;
  1691. }
  1692. memcpy (new_pi, pi, sizeof (struct process_info));
  1693. list_init (&new_pi->list);
  1694. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1695. /*
  1696. * pid and nodeid -> undefined
  1697. */
  1698. new_pi->pid = new_pi->nodeid = 0;
  1699. }
  1700. /*
  1701. * We will return list "grouped" by "group name", so try to find right place to add
  1702. */
  1703. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1704. iter2 != &cpg_iteration_instance->items_list_head;
  1705. iter2 = iter2->next) {
  1706. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1707. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1708. break;
  1709. }
  1710. }
  1711. list_add (&new_pi->list, iter2);
  1712. }
  1713. /*
  1714. * Now we have a full "grouped by" copy of process_info list
  1715. */
  1716. /*
  1717. * Add instance to current cpd list
  1718. */
  1719. list_init (&cpg_iteration_instance->list);
  1720. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1721. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1722. error_put_destroy:
  1723. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1724. error_destroy:
  1725. if (error != CS_OK) {
  1726. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1727. }
  1728. response_send:
  1729. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1730. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1731. res_lib_cpg_iterationinitialize.header.error = error;
  1732. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1733. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1734. sizeof (res_lib_cpg_iterationinitialize));
  1735. }
  1736. static void message_handler_req_lib_cpg_iteration_next (
  1737. void *conn,
  1738. const void *message)
  1739. {
  1740. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1741. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1742. struct cpg_iteration_instance *cpg_iteration_instance;
  1743. cs_error_t error = CS_OK;
  1744. int res;
  1745. struct process_info *pi;
  1746. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1747. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1748. req_lib_cpg_iterationnext->iteration_handle,
  1749. (void *)&cpg_iteration_instance);
  1750. if (res != 0) {
  1751. error = CS_ERR_LIBRARY;
  1752. goto error_exit;
  1753. }
  1754. assert (cpg_iteration_instance);
  1755. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1756. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1757. error = CS_ERR_NO_SECTIONS;
  1758. goto error_put;
  1759. }
  1760. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1761. /*
  1762. * Copy iteration data
  1763. */
  1764. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1765. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1766. memcpy (&res_lib_cpg_iterationnext.description.group,
  1767. &pi->group,
  1768. sizeof (mar_cpg_name_t));
  1769. error_put:
  1770. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1771. error_exit:
  1772. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1773. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1774. res_lib_cpg_iterationnext.header.error = error;
  1775. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1776. sizeof (res_lib_cpg_iterationnext));
  1777. }
  1778. static void message_handler_req_lib_cpg_iteration_finalize (
  1779. void *conn,
  1780. const void *message)
  1781. {
  1782. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1783. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1784. struct cpg_iteration_instance *cpg_iteration_instance;
  1785. cs_error_t error = CS_OK;
  1786. int res;
  1787. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1788. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1789. req_lib_cpg_iterationfinalize->iteration_handle,
  1790. (void *)&cpg_iteration_instance);
  1791. if (res != 0) {
  1792. error = CS_ERR_LIBRARY;
  1793. goto error_exit;
  1794. }
  1795. assert (cpg_iteration_instance);
  1796. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1797. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1798. error_exit:
  1799. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1800. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1801. res_lib_cpg_iterationfinalize.header.error = error;
  1802. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1803. sizeof (res_lib_cpg_iterationfinalize));
  1804. }