4
0

cpg.c 61 KB

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