cpg.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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. for (iter = downlist_messages_head.next;
  613. iter != &downlist_messages_head;
  614. iter = iter->next) {
  615. cmp = list_entry(iter, struct downlist_msg, list);
  616. downlist_log("comparing", cmp);
  617. if (best == NULL) {
  618. best = cmp;
  619. continue;
  620. }
  621. best_members = best->old_members - best->left_nodes;
  622. cmp_members = cmp->old_members - cmp->left_nodes;
  623. if (cmp_members < best_members) {
  624. continue;
  625. }
  626. else if (cmp_members > best_members) {
  627. best = cmp;
  628. }
  629. else if (cmp->sender_nodeid < best->sender_nodeid) {
  630. best = cmp;
  631. }
  632. }
  633. return best;
  634. }
  635. static void downlist_master_choose_and_send (void)
  636. {
  637. struct downlist_msg *stored_msg;
  638. struct list_head *iter;
  639. struct process_info *left_pi;
  640. qb_map_t *group_map;
  641. struct cpg_name cpg_group;
  642. mar_cpg_name_t group;
  643. struct confchg_data{
  644. struct cpg_name cpg_group;
  645. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  646. int left_list_entries;
  647. struct list_head list;
  648. } *pcd;
  649. qb_map_iter_t *miter;
  650. int i, size;
  651. downlist_state = CPG_DOWNLIST_APPLYING;
  652. stored_msg = downlist_master_choose ();
  653. if (!stored_msg) {
  654. log_printf (LOGSYS_LEVEL_DEBUG, "NO chosen downlist");
  655. return;
  656. }
  657. downlist_log("chosen downlist", stored_msg);
  658. group_map = qb_skiplist_create();
  659. /*
  660. * only the cpg groups included in left nodes should receive
  661. * confchg event, so we will collect these cpg groups and
  662. * relative left_lists here.
  663. */
  664. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  665. struct process_info *pi = list_entry(iter, struct process_info, list);
  666. iter = iter->next;
  667. left_pi = NULL;
  668. for (i = 0; i < stored_msg->left_nodes; i++) {
  669. if (pi->nodeid == stored_msg->nodeids[i]) {
  670. left_pi = pi;
  671. break;
  672. }
  673. }
  674. if (left_pi) {
  675. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  676. cpg_group.value[cpg_group.length] = 0;
  677. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  678. if (pcd == NULL) {
  679. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  680. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  681. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  682. }
  683. size = pcd->left_list_entries;
  684. pcd->left_list[size].nodeid = left_pi->nodeid;
  685. pcd->left_list[size].pid = left_pi->pid;
  686. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  687. pcd->left_list_entries++;
  688. list_del (&left_pi->list);
  689. free (left_pi);
  690. }
  691. }
  692. /* send only one confchg event per cpg group */
  693. miter = qb_map_iter_create(group_map);
  694. while (qb_map_iter_next(miter, (void **)&pcd)) {
  695. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  696. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  697. for (i=0; i<pcd->left_list_entries; i++) {
  698. log_printf (LOG_DEBUG, "left_list[%d] group:%d, ip:%s, pid:%d",
  699. i, pcd->cpg_group.value,
  700. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  701. pcd->left_list[i].pid);
  702. }
  703. /* send confchg event */
  704. notify_lib_joinlist(&group, NULL,
  705. 0, NULL,
  706. pcd->left_list_entries,
  707. pcd->left_list,
  708. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  709. free(pcd);
  710. }
  711. qb_map_iter_free(miter);
  712. qb_map_destroy(group_map);
  713. }
  714. static void downlist_messages_delete (void)
  715. {
  716. struct downlist_msg *stored_msg;
  717. struct list_head *iter, *iter_next;
  718. for (iter = downlist_messages_head.next;
  719. iter != &downlist_messages_head;
  720. iter = iter_next) {
  721. iter_next = iter->next;
  722. stored_msg = list_entry(iter, struct downlist_msg, list);
  723. list_del (&stored_msg->list);
  724. free (stored_msg);
  725. }
  726. }
  727. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  728. {
  729. #ifdef COROSYNC_SOLARIS
  730. logsys_subsys_init();
  731. #endif
  732. list_init (&downlist_messages_head);
  733. api = corosync_api;
  734. return (NULL);
  735. }
  736. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  737. {
  738. struct list_head *iter, *iter_next;
  739. struct process_info *pi;
  740. for (iter = cpg_iteration_instance->items_list_head.next;
  741. iter != &cpg_iteration_instance->items_list_head;
  742. iter = iter_next) {
  743. iter_next = iter->next;
  744. pi = list_entry (iter, struct process_info, list);
  745. list_del (&pi->list);
  746. free (pi);
  747. }
  748. list_del (&cpg_iteration_instance->list);
  749. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  750. }
  751. static void cpg_pd_finalize (struct cpg_pd *cpd)
  752. {
  753. struct list_head *iter, *iter_next;
  754. struct cpg_iteration_instance *cpii;
  755. zcb_all_free(cpd);
  756. for (iter = cpd->iteration_instance_list_head.next;
  757. iter != &cpd->iteration_instance_list_head;
  758. iter = iter_next) {
  759. iter_next = iter->next;
  760. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  761. cpg_iteration_instance_finalize (cpii);
  762. }
  763. list_del (&cpd->list);
  764. }
  765. static int cpg_lib_exit_fn (void *conn)
  766. {
  767. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  768. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  769. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  770. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  771. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  772. }
  773. cpg_pd_finalize (cpd);
  774. api->ipc_refcnt_dec (conn);
  775. return (0);
  776. }
  777. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  778. {
  779. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  780. struct iovec req_exec_cpg_iovec;
  781. int result;
  782. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  783. req_exec_cpg_procjoin.pid = pid;
  784. req_exec_cpg_procjoin.reason = reason;
  785. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  786. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  787. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  788. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  789. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  790. return (result);
  791. }
  792. /* Can byteswap join & leave messages */
  793. static void exec_cpg_procjoin_endian_convert (void *msg)
  794. {
  795. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  796. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  797. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  798. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  799. }
  800. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  801. {
  802. char *msg = msg_v;
  803. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  804. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  805. swab_mar_int32_t (&res->size);
  806. while ((const char*)jle < msg + res->size) {
  807. jle->pid = swab32(jle->pid);
  808. swab_mar_cpg_name_t (&jle->group_name);
  809. jle++;
  810. }
  811. }
  812. static void exec_cpg_downlist_endian_convert_old (void *msg)
  813. {
  814. }
  815. static void exec_cpg_downlist_endian_convert (void *msg)
  816. {
  817. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  818. unsigned int i;
  819. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  820. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  821. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  822. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  823. }
  824. }
  825. static void exec_cpg_mcast_endian_convert (void *msg)
  826. {
  827. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  828. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  829. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  830. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  831. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  832. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  833. }
  834. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  835. struct list_head *iter;
  836. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  837. struct process_info *pi = list_entry (iter, struct process_info, list);
  838. iter = iter->next;
  839. if (pi->pid == pid && pi->nodeid == nodeid &&
  840. mar_name_compare (&pi->group, group_name) == 0) {
  841. return pi;
  842. }
  843. }
  844. return NULL;
  845. }
  846. static void do_proc_join(
  847. const mar_cpg_name_t *name,
  848. uint32_t pid,
  849. unsigned int nodeid,
  850. int reason)
  851. {
  852. struct process_info *pi;
  853. struct process_info *pi_entry;
  854. mar_cpg_address_t notify_info;
  855. struct list_head *list;
  856. struct list_head *list_to_add = NULL;
  857. if (process_info_find (name, pid, nodeid) != NULL) {
  858. return ;
  859. }
  860. pi = malloc (sizeof (struct process_info));
  861. if (!pi) {
  862. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  863. return;
  864. }
  865. pi->nodeid = nodeid;
  866. pi->pid = pid;
  867. memcpy(&pi->group, name, sizeof(*name));
  868. list_init(&pi->list);
  869. /*
  870. * Insert new process in sorted order so synchronization works properly
  871. */
  872. list_to_add = &process_info_list_head;
  873. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  874. pi_entry = list_entry(list, struct process_info, list);
  875. if (pi_entry->nodeid > pi->nodeid ||
  876. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  877. break;
  878. }
  879. list_to_add = list;
  880. }
  881. list_add (&pi->list, list_to_add);
  882. notify_info.pid = pi->pid;
  883. notify_info.nodeid = nodeid;
  884. notify_info.reason = reason;
  885. notify_lib_joinlist(&pi->group, NULL,
  886. 1, &notify_info,
  887. 0, NULL,
  888. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  889. }
  890. static void message_handler_req_exec_cpg_downlist_old (
  891. const void *message,
  892. unsigned int nodeid)
  893. {
  894. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node %d",
  895. nodeid);
  896. }
  897. static void message_handler_req_exec_cpg_downlist(
  898. const void *message,
  899. unsigned int nodeid)
  900. {
  901. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  902. int i;
  903. struct list_head *iter;
  904. struct downlist_msg *stored_msg;
  905. int found;
  906. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  907. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  908. req_exec_cpg_downlist->left_nodes, downlist_state);
  909. return;
  910. }
  911. stored_msg = malloc (sizeof (struct downlist_msg));
  912. stored_msg->sender_nodeid = nodeid;
  913. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  914. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  915. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  916. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  917. list_init (&stored_msg->list);
  918. list_add (&stored_msg->list, &downlist_messages_head);
  919. for (i = 0; i < my_member_list_entries; i++) {
  920. found = 0;
  921. for (iter = downlist_messages_head.next;
  922. iter != &downlist_messages_head;
  923. iter = iter->next) {
  924. stored_msg = list_entry(iter, struct downlist_msg, list);
  925. if (my_member_list[i] == stored_msg->sender_nodeid) {
  926. found = 1;
  927. }
  928. }
  929. if (!found) {
  930. return;
  931. }
  932. }
  933. downlist_master_choose_and_send ();
  934. }
  935. static void message_handler_req_exec_cpg_procjoin (
  936. const void *message,
  937. unsigned int nodeid)
  938. {
  939. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  940. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d", nodeid);
  941. do_proc_join (&req_exec_cpg_procjoin->group_name,
  942. req_exec_cpg_procjoin->pid, nodeid,
  943. CONFCHG_CPG_REASON_JOIN);
  944. }
  945. static void message_handler_req_exec_cpg_procleave (
  946. const void *message,
  947. unsigned int nodeid)
  948. {
  949. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  950. struct process_info *pi;
  951. struct list_head *iter;
  952. mar_cpg_address_t notify_info;
  953. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d", nodeid);
  954. notify_info.pid = req_exec_cpg_procjoin->pid;
  955. notify_info.nodeid = nodeid;
  956. notify_info.reason = req_exec_cpg_procjoin->reason;
  957. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  958. 0, NULL,
  959. 1, &notify_info,
  960. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  961. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  962. pi = list_entry(iter, struct process_info, list);
  963. iter = iter->next;
  964. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  965. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  966. list_del (&pi->list);
  967. free (pi);
  968. }
  969. }
  970. }
  971. /* Got a proclist from another node */
  972. static void message_handler_req_exec_cpg_joinlist (
  973. const void *message_v,
  974. unsigned int nodeid)
  975. {
  976. const char *message = message_v;
  977. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  978. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  979. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x",
  980. nodeid);
  981. /* Ignore our own messages */
  982. if (nodeid == api->totem_nodeid_get()) {
  983. return;
  984. }
  985. while ((const char*)jle < message + res->size) {
  986. do_proc_join (&jle->group_name, jle->pid, nodeid,
  987. CONFCHG_CPG_REASON_NODEUP);
  988. jle++;
  989. }
  990. }
  991. static void message_handler_req_exec_cpg_mcast (
  992. const void *message,
  993. unsigned int nodeid)
  994. {
  995. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  996. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  997. int msglen = req_exec_cpg_mcast->msglen;
  998. struct list_head *iter, *pi_iter;
  999. struct cpg_pd *cpd;
  1000. struct iovec iovec[2];
  1001. int known_node = 0;
  1002. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1003. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1004. res_lib_cpg_mcast.msglen = msglen;
  1005. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1006. res_lib_cpg_mcast.nodeid = nodeid;
  1007. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1008. sizeof(mar_cpg_name_t));
  1009. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1010. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1011. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1012. iovec[1].iov_len = msglen;
  1013. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  1014. cpd = list_entry(iter, struct cpg_pd, list);
  1015. iter = iter->next;
  1016. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1017. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1018. if (!known_node) {
  1019. /* Try to find, if we know the node */
  1020. for (pi_iter = process_info_list_head.next;
  1021. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  1022. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  1023. if (pi->nodeid == nodeid &&
  1024. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1025. known_node = 1;
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. if (!known_node) {
  1031. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1032. return ;
  1033. }
  1034. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1035. }
  1036. }
  1037. }
  1038. static int cpg_exec_send_downlist(void)
  1039. {
  1040. struct iovec iov;
  1041. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1042. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1043. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1044. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1045. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1046. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1047. }
  1048. static int cpg_exec_send_joinlist(void)
  1049. {
  1050. int count = 0;
  1051. struct list_head *iter;
  1052. struct qb_ipc_response_header *res;
  1053. char *buf;
  1054. struct join_list_entry *jle;
  1055. struct iovec req_exec_cpg_iovec;
  1056. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1057. struct process_info *pi = list_entry (iter, struct process_info, list);
  1058. if (pi->nodeid == api->totem_nodeid_get ()) {
  1059. count++;
  1060. }
  1061. }
  1062. /* Nothing to send */
  1063. if (!count)
  1064. return 0;
  1065. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1066. if (!buf) {
  1067. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1068. return -1;
  1069. }
  1070. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1071. res = (struct qb_ipc_response_header *)buf;
  1072. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1073. struct process_info *pi = list_entry (iter, struct process_info, list);
  1074. if (pi->nodeid == api->totem_nodeid_get ()) {
  1075. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1076. jle->pid = pi->pid;
  1077. jle++;
  1078. }
  1079. }
  1080. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1081. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1082. req_exec_cpg_iovec.iov_base = buf;
  1083. req_exec_cpg_iovec.iov_len = res->size;
  1084. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1085. }
  1086. static int cpg_lib_init_fn (void *conn)
  1087. {
  1088. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1089. memset (cpd, 0, sizeof(struct cpg_pd));
  1090. cpd->conn = conn;
  1091. list_add (&cpd->list, &cpg_pd_list_head);
  1092. list_init (&cpd->iteration_instance_list_head);
  1093. list_init (&cpd->zcb_mapped_list_head);
  1094. api->ipc_refcnt_inc (conn);
  1095. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1096. return (0);
  1097. }
  1098. /* Join message from the library */
  1099. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1100. {
  1101. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1102. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1103. struct res_lib_cpg_join res_lib_cpg_join;
  1104. cs_error_t error = CS_OK;
  1105. struct list_head *iter;
  1106. /* Test, if we don't have same pid and group name joined */
  1107. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  1108. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  1109. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1110. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1111. /* We have same pid and group name joined -> return error */
  1112. error = CS_ERR_EXIST;
  1113. goto response_send;
  1114. }
  1115. }
  1116. /*
  1117. * Same check must be done in process info list, because there may be not yet delivered
  1118. * leave of client.
  1119. */
  1120. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1121. struct process_info *pi = list_entry (iter, struct process_info, list);
  1122. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1123. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1124. /* We have same pid and group name joined -> return error */
  1125. error = CS_ERR_TRY_AGAIN;
  1126. goto response_send;
  1127. }
  1128. }
  1129. switch (cpd->cpd_state) {
  1130. case CPD_STATE_UNJOINED:
  1131. error = CS_OK;
  1132. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1133. cpd->pid = req_lib_cpg_join->pid;
  1134. cpd->flags = req_lib_cpg_join->flags;
  1135. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1136. sizeof (cpd->group_name));
  1137. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1138. &req_lib_cpg_join->group_name,
  1139. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1140. break;
  1141. case CPD_STATE_LEAVE_STARTED:
  1142. error = CS_ERR_BUSY;
  1143. break;
  1144. case CPD_STATE_JOIN_STARTED:
  1145. error = CS_ERR_EXIST;
  1146. break;
  1147. case CPD_STATE_JOIN_COMPLETED:
  1148. error = CS_ERR_EXIST;
  1149. break;
  1150. }
  1151. response_send:
  1152. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1153. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1154. res_lib_cpg_join.header.error = error;
  1155. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1156. }
  1157. /* Leave message from the library */
  1158. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1159. {
  1160. struct res_lib_cpg_leave res_lib_cpg_leave;
  1161. cs_error_t error = CS_OK;
  1162. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1163. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1164. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1165. switch (cpd->cpd_state) {
  1166. case CPD_STATE_UNJOINED:
  1167. error = CS_ERR_NOT_EXIST;
  1168. break;
  1169. case CPD_STATE_LEAVE_STARTED:
  1170. error = CS_ERR_NOT_EXIST;
  1171. break;
  1172. case CPD_STATE_JOIN_STARTED:
  1173. error = CS_ERR_BUSY;
  1174. break;
  1175. case CPD_STATE_JOIN_COMPLETED:
  1176. error = CS_OK;
  1177. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1178. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1179. &req_lib_cpg_leave->group_name,
  1180. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1181. CONFCHG_CPG_REASON_LEAVE);
  1182. break;
  1183. }
  1184. /* send return */
  1185. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1186. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1187. res_lib_cpg_leave.header.error = error;
  1188. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1189. }
  1190. /* Finalize message from library */
  1191. static void message_handler_req_lib_cpg_finalize (
  1192. void *conn,
  1193. const void *message)
  1194. {
  1195. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1196. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1197. cs_error_t error = CS_OK;
  1198. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1199. /*
  1200. * We will just remove cpd from list. After this call, connection will be
  1201. * closed on lib side, and cpg_lib_exit_fn will be called
  1202. */
  1203. list_del (&cpd->list);
  1204. list_init (&cpd->list);
  1205. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1206. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1207. res_lib_cpg_finalize.header.error = error;
  1208. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1209. sizeof (res_lib_cpg_finalize));
  1210. }
  1211. static int
  1212. memory_map (
  1213. const char *path,
  1214. size_t bytes,
  1215. void **buf)
  1216. {
  1217. int32_t fd;
  1218. void *addr_orig;
  1219. void *addr;
  1220. int32_t res;
  1221. fd = open (path, O_RDWR, 0600);
  1222. unlink (path);
  1223. if (fd == -1) {
  1224. return (-1);
  1225. }
  1226. res = ftruncate (fd, bytes);
  1227. if (res == -1) {
  1228. goto error_close_unlink;
  1229. }
  1230. addr_orig = mmap (NULL, bytes, PROT_NONE,
  1231. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  1232. if (addr_orig == MAP_FAILED) {
  1233. goto error_close_unlink;
  1234. }
  1235. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  1236. MAP_FIXED | MAP_SHARED, fd, 0);
  1237. if (addr != addr_orig) {
  1238. munmap(addr_orig, bytes);
  1239. goto error_close_unlink;
  1240. }
  1241. #ifdef COROSYNC_BSD
  1242. madvise(addr, bytes, MADV_NOSYNC);
  1243. #endif
  1244. res = close (fd);
  1245. if (res) {
  1246. return (-1);
  1247. }
  1248. *buf = addr_orig;
  1249. return (0);
  1250. error_close_unlink:
  1251. close (fd);
  1252. unlink(path);
  1253. return -1;
  1254. }
  1255. static inline int zcb_alloc (
  1256. struct cpg_pd *cpd,
  1257. const char *path_to_file,
  1258. size_t size,
  1259. void **addr)
  1260. {
  1261. struct zcb_mapped *zcb_mapped;
  1262. unsigned int res;
  1263. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1264. if (zcb_mapped == NULL) {
  1265. return (-1);
  1266. }
  1267. res = memory_map (
  1268. path_to_file,
  1269. size,
  1270. addr);
  1271. if (res == -1) {
  1272. free (zcb_mapped);
  1273. return (-1);
  1274. }
  1275. list_init (&zcb_mapped->list);
  1276. zcb_mapped->addr = *addr;
  1277. zcb_mapped->size = size;
  1278. list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1279. return (0);
  1280. }
  1281. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1282. {
  1283. unsigned int res;
  1284. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1285. list_del (&zcb_mapped->list);
  1286. free (zcb_mapped);
  1287. return (res);
  1288. }
  1289. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1290. {
  1291. struct list_head *list;
  1292. struct zcb_mapped *zcb_mapped;
  1293. unsigned int res = 0;
  1294. for (list = cpd->zcb_mapped_list_head.next;
  1295. list != &cpd->zcb_mapped_list_head; list = list->next) {
  1296. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1297. if (zcb_mapped->addr == addr) {
  1298. res = zcb_free (zcb_mapped);
  1299. break;
  1300. }
  1301. }
  1302. return (res);
  1303. }
  1304. static inline int zcb_all_free (
  1305. struct cpg_pd *cpd)
  1306. {
  1307. struct list_head *list;
  1308. struct zcb_mapped *zcb_mapped;
  1309. for (list = cpd->zcb_mapped_list_head.next;
  1310. list != &cpd->zcb_mapped_list_head;) {
  1311. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1312. list = list->next;
  1313. zcb_free (zcb_mapped);
  1314. }
  1315. return (0);
  1316. }
  1317. union u {
  1318. uint64_t server_addr;
  1319. void *server_ptr;
  1320. };
  1321. static uint64_t void2serveraddr (void *server_ptr)
  1322. {
  1323. union u u;
  1324. u.server_ptr = server_ptr;
  1325. return (u.server_addr);
  1326. }
  1327. static void *serveraddr2void (uint64_t server_addr)
  1328. {
  1329. union u u;
  1330. u.server_addr = server_addr;
  1331. return (u.server_ptr);
  1332. };
  1333. static void message_handler_req_lib_cpg_zc_alloc (
  1334. void *conn,
  1335. const void *message)
  1336. {
  1337. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1338. struct qb_ipc_response_header res_header;
  1339. void *addr = NULL;
  1340. struct coroipcs_zc_header *zc_header;
  1341. unsigned int res;
  1342. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1343. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1344. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1345. &addr);
  1346. assert(res == 0);
  1347. zc_header = (struct coroipcs_zc_header *)addr;
  1348. zc_header->server_address = void2serveraddr(addr);
  1349. res_header.size = sizeof (struct qb_ipc_response_header);
  1350. res_header.id = 0;
  1351. api->ipc_response_send (conn,
  1352. &res_header,
  1353. res_header.size);
  1354. }
  1355. static void message_handler_req_lib_cpg_zc_free (
  1356. void *conn,
  1357. const void *message)
  1358. {
  1359. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1360. struct qb_ipc_response_header res_header;
  1361. void *addr = NULL;
  1362. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1363. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1364. addr = serveraddr2void (hdr->server_address);
  1365. zcb_by_addr_free (cpd, addr);
  1366. res_header.size = sizeof (struct qb_ipc_response_header);
  1367. res_header.id = 0;
  1368. api->ipc_response_send (
  1369. conn, &res_header,
  1370. res_header.size);
  1371. }
  1372. /* Mcast message from the library */
  1373. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1374. {
  1375. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1376. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1377. mar_cpg_name_t group_name = cpd->group_name;
  1378. struct iovec req_exec_cpg_iovec[2];
  1379. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1380. int msglen = req_lib_cpg_mcast->msglen;
  1381. int result;
  1382. cs_error_t error = CS_ERR_NOT_EXIST;
  1383. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p", conn);
  1384. switch (cpd->cpd_state) {
  1385. case CPD_STATE_UNJOINED:
  1386. error = CS_ERR_NOT_EXIST;
  1387. break;
  1388. case CPD_STATE_LEAVE_STARTED:
  1389. error = CS_ERR_NOT_EXIST;
  1390. break;
  1391. case CPD_STATE_JOIN_STARTED:
  1392. error = CS_OK;
  1393. break;
  1394. case CPD_STATE_JOIN_COMPLETED:
  1395. error = CS_OK;
  1396. break;
  1397. }
  1398. if (error == CS_OK) {
  1399. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1400. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1401. MESSAGE_REQ_EXEC_CPG_MCAST);
  1402. req_exec_cpg_mcast.pid = cpd->pid;
  1403. req_exec_cpg_mcast.msglen = msglen;
  1404. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1405. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1406. sizeof(mar_cpg_name_t));
  1407. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1408. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1409. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1410. req_exec_cpg_iovec[1].iov_len = msglen;
  1411. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1412. assert(result == 0);
  1413. } else {
  1414. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1415. conn, group_name.value, cpd->cpd_state, error);
  1416. }
  1417. }
  1418. static void message_handler_req_lib_cpg_zc_execute (
  1419. void *conn,
  1420. const void *message)
  1421. {
  1422. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1423. struct qb_ipc_request_header *header;
  1424. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1425. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1426. struct iovec req_exec_cpg_iovec[2];
  1427. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1428. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1429. int result;
  1430. cs_error_t error = CS_ERR_NOT_EXIST;
  1431. log_printf(LOGSYS_LEVEL_DEBUG, "got ZC mcast request on %p", conn);
  1432. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1433. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1434. switch (cpd->cpd_state) {
  1435. case CPD_STATE_UNJOINED:
  1436. error = CS_ERR_NOT_EXIST;
  1437. break;
  1438. case CPD_STATE_LEAVE_STARTED:
  1439. error = CS_ERR_NOT_EXIST;
  1440. break;
  1441. case CPD_STATE_JOIN_STARTED:
  1442. error = CS_OK;
  1443. break;
  1444. case CPD_STATE_JOIN_COMPLETED:
  1445. error = CS_OK;
  1446. break;
  1447. }
  1448. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1449. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1450. if (error == CS_OK) {
  1451. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1452. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1453. MESSAGE_REQ_EXEC_CPG_MCAST);
  1454. req_exec_cpg_mcast.pid = cpd->pid;
  1455. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1456. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1457. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1458. sizeof(mar_cpg_name_t));
  1459. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1460. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1461. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1462. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1463. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1464. if (result == 0) {
  1465. res_lib_cpg_mcast.header.error = CS_OK;
  1466. } else {
  1467. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1468. }
  1469. } else {
  1470. res_lib_cpg_mcast.header.error = error;
  1471. }
  1472. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1473. sizeof (res_lib_cpg_mcast));
  1474. }
  1475. static void message_handler_req_lib_cpg_membership (void *conn,
  1476. const void *message)
  1477. {
  1478. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1479. (struct req_lib_cpg_membership_get *)message;
  1480. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1481. struct list_head *iter;
  1482. int member_count = 0;
  1483. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1484. res_lib_cpg_membership_get.header.error = CS_OK;
  1485. res_lib_cpg_membership_get.header.size =
  1486. sizeof (struct req_lib_cpg_membership_get);
  1487. for (iter = process_info_list_head.next;
  1488. iter != &process_info_list_head; iter = iter->next) {
  1489. struct process_info *pi = list_entry (iter, struct process_info, list);
  1490. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1491. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1492. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1493. member_count += 1;
  1494. }
  1495. }
  1496. res_lib_cpg_membership_get.member_count = member_count;
  1497. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1498. sizeof (res_lib_cpg_membership_get));
  1499. }
  1500. static void message_handler_req_lib_cpg_local_get (void *conn,
  1501. const void *message)
  1502. {
  1503. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1504. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1505. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1506. res_lib_cpg_local_get.header.error = CS_OK;
  1507. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1508. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1509. sizeof (res_lib_cpg_local_get));
  1510. }
  1511. static void message_handler_req_lib_cpg_iteration_initialize (
  1512. void *conn,
  1513. const void *message)
  1514. {
  1515. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1516. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1517. hdb_handle_t cpg_iteration_handle = 0;
  1518. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1519. struct list_head *iter, *iter2;
  1520. struct cpg_iteration_instance *cpg_iteration_instance;
  1521. cs_error_t error = CS_OK;
  1522. int res;
  1523. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1524. /* Because between calling this function and *next can be some operations which will
  1525. * change list, we must do full copy.
  1526. */
  1527. /*
  1528. * Create new iteration instance
  1529. */
  1530. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1531. &cpg_iteration_handle);
  1532. if (res != 0) {
  1533. error = CS_ERR_NO_MEMORY;
  1534. goto response_send;
  1535. }
  1536. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1537. if (res != 0) {
  1538. error = CS_ERR_BAD_HANDLE;
  1539. goto error_destroy;
  1540. }
  1541. list_init (&cpg_iteration_instance->items_list_head);
  1542. cpg_iteration_instance->handle = cpg_iteration_handle;
  1543. /*
  1544. * Create copy of process_info list "grouped by" group name
  1545. */
  1546. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1547. struct process_info *pi = list_entry (iter, struct process_info, list);
  1548. struct process_info *new_pi;
  1549. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1550. /*
  1551. * Try to find processed group name in our list new list
  1552. */
  1553. int found = 0;
  1554. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1555. iter2 != &cpg_iteration_instance->items_list_head;
  1556. iter2 = iter2->next) {
  1557. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1558. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1559. found = 1;
  1560. break;
  1561. }
  1562. }
  1563. if (found) {
  1564. /*
  1565. * We have this name in list -> don't add
  1566. */
  1567. continue ;
  1568. }
  1569. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1570. /*
  1571. * Test pi group name with request
  1572. */
  1573. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1574. /*
  1575. * Not same -> don't add
  1576. */
  1577. continue ;
  1578. }
  1579. new_pi = malloc (sizeof (struct process_info));
  1580. if (!new_pi) {
  1581. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1582. error = CS_ERR_NO_MEMORY;
  1583. goto error_put_destroy;
  1584. }
  1585. memcpy (new_pi, pi, sizeof (struct process_info));
  1586. list_init (&new_pi->list);
  1587. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1588. /*
  1589. * pid and nodeid -> undefined
  1590. */
  1591. new_pi->pid = new_pi->nodeid = 0;
  1592. }
  1593. /*
  1594. * We will return list "grouped" by "group name", so try to find right place to add
  1595. */
  1596. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1597. iter2 != &cpg_iteration_instance->items_list_head;
  1598. iter2 = iter2->next) {
  1599. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1600. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1601. break;
  1602. }
  1603. }
  1604. list_add (&new_pi->list, iter2);
  1605. }
  1606. /*
  1607. * Now we have a full "grouped by" copy of process_info list
  1608. */
  1609. /*
  1610. * Add instance to current cpd list
  1611. */
  1612. list_init (&cpg_iteration_instance->list);
  1613. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1614. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1615. error_put_destroy:
  1616. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1617. error_destroy:
  1618. if (error != CS_OK) {
  1619. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1620. }
  1621. response_send:
  1622. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1623. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1624. res_lib_cpg_iterationinitialize.header.error = error;
  1625. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1626. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1627. sizeof (res_lib_cpg_iterationinitialize));
  1628. }
  1629. static void message_handler_req_lib_cpg_iteration_next (
  1630. void *conn,
  1631. const void *message)
  1632. {
  1633. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1634. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1635. struct cpg_iteration_instance *cpg_iteration_instance;
  1636. cs_error_t error = CS_OK;
  1637. int res;
  1638. struct process_info *pi;
  1639. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1640. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1641. req_lib_cpg_iterationnext->iteration_handle,
  1642. (void *)&cpg_iteration_instance);
  1643. if (res != 0) {
  1644. error = CS_ERR_LIBRARY;
  1645. goto error_exit;
  1646. }
  1647. assert (cpg_iteration_instance);
  1648. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1649. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1650. error = CS_ERR_NO_SECTIONS;
  1651. goto error_put;
  1652. }
  1653. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1654. /*
  1655. * Copy iteration data
  1656. */
  1657. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1658. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1659. memcpy (&res_lib_cpg_iterationnext.description.group,
  1660. &pi->group,
  1661. sizeof (mar_cpg_name_t));
  1662. error_put:
  1663. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1664. error_exit:
  1665. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1666. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1667. res_lib_cpg_iterationnext.header.error = error;
  1668. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1669. sizeof (res_lib_cpg_iterationnext));
  1670. }
  1671. static void message_handler_req_lib_cpg_iteration_finalize (
  1672. void *conn,
  1673. const void *message)
  1674. {
  1675. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1676. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1677. struct cpg_iteration_instance *cpg_iteration_instance;
  1678. cs_error_t error = CS_OK;
  1679. int res;
  1680. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1681. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1682. req_lib_cpg_iterationfinalize->iteration_handle,
  1683. (void *)&cpg_iteration_instance);
  1684. if (res != 0) {
  1685. error = CS_ERR_LIBRARY;
  1686. goto error_exit;
  1687. }
  1688. assert (cpg_iteration_instance);
  1689. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1690. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1691. error_exit:
  1692. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1693. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1694. res_lib_cpg_iterationfinalize.header.error = error;
  1695. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1696. sizeof (res_lib_cpg_iterationfinalize));
  1697. }