cpg.c 58 KB

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