cpg.c 59 KB

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