cpg.c 68 KB

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