4
0

cpg.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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/qblist.h>
  55. #include <qb/qbmap.h>
  56. #include <corosync/corotypes.h>
  57. #include <qb/qbipc_common.h>
  58. #include <corosync/corodefs.h>
  59. #include <corosync/logsys.h>
  60. #include <corosync/coroapi.h>
  61. #include <corosync/cpg.h>
  62. #include <corosync/ipc_cpg.h>
  63. #ifndef MAP_ANONYMOUS
  64. #define MAP_ANONYMOUS MAP_ANON
  65. #endif
  66. #include "service.h"
  67. LOGSYS_DECLARE_SUBSYS ("CPG");
  68. #define GROUP_HASH_SIZE 32
  69. enum cpg_message_req_types {
  70. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  71. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  72. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  73. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  74. MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD = 4,
  75. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 5,
  76. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST = 6,
  77. };
  78. struct zcb_mapped {
  79. struct qb_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 qb_list_head downlist_messages_head;
  137. static struct qb_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 qb_list_head list;
  148. struct qb_list_head iteration_instance_list_head;
  149. struct qb_list_head zcb_mapped_list_head;
  150. };
  151. struct cpg_iteration_instance {
  152. hdb_handle_t handle;
  153. struct qb_list_head list;
  154. struct qb_list_head items_list_head; /* List of process_info */
  155. struct qb_list_head *current_pointer;
  156. };
  157. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  158. QB_LIST_DECLARE (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 qb_list_head list; /* on the group_info members list */
  171. };
  172. QB_LIST_DECLARE (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 qb_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 qb_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 qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  555. struct cpg_pd *cpg_pd = qb_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 qb_list_head *iter;
  575. int count;
  576. struct res_lib_cpg_confchg_callback *res;
  577. mar_cpg_address_t *retgi;
  578. count = 0;
  579. qb_list_for_each(iter, &process_info_list_head) {
  580. struct process_info *pi = qb_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. qb_list_for_each(iter, &process_info_list_head) {
  608. struct process_info *pi=qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  636. struct cpg_pd *cpd = qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  666. struct cpg_pd *cpd = qb_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 qb_list_head *iter;
  688. uint32_t cmp_members;
  689. uint32_t best_members;
  690. uint32_t i;
  691. int ignore_msg;
  692. qb_list_for_each(iter, &downlist_messages_head) {
  693. cmp = qb_list_entry(iter, struct downlist_msg, list);
  694. downlist_log("comparing", cmp);
  695. ignore_msg = 0;
  696. for (i = 0; i < cmp->left_nodes; i++) {
  697. if (cmp->nodeids[i] == api->totem_nodeid_get()) {
  698. log_printf (LOG_DEBUG, "Ignoring this entry because I'm in the left list\n");
  699. ignore_msg = 1;
  700. break;
  701. }
  702. }
  703. if (ignore_msg) {
  704. continue ;
  705. }
  706. if (best == NULL) {
  707. best = cmp;
  708. continue;
  709. }
  710. best_members = best->old_members - best->left_nodes;
  711. cmp_members = cmp->old_members - cmp->left_nodes;
  712. if (cmp_members > best_members) {
  713. best = cmp;
  714. } else if (cmp_members == best_members) {
  715. if (cmp->old_members > best->old_members) {
  716. best = cmp;
  717. } else if (cmp->old_members == best->old_members) {
  718. if (cmp->sender_nodeid < best->sender_nodeid) {
  719. best = cmp;
  720. }
  721. }
  722. }
  723. }
  724. assert (best != NULL);
  725. return best;
  726. }
  727. static void downlist_master_choose_and_send (void)
  728. {
  729. struct downlist_msg *stored_msg;
  730. struct qb_list_head *iter, *tmp_iter;
  731. struct process_info *left_pi;
  732. qb_map_t *group_map;
  733. struct cpg_name cpg_group;
  734. mar_cpg_name_t group;
  735. struct confchg_data{
  736. struct cpg_name cpg_group;
  737. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  738. int left_list_entries;
  739. struct qb_list_head list;
  740. } *pcd;
  741. qb_map_iter_t *miter;
  742. int i, size;
  743. downlist_state = CPG_DOWNLIST_APPLYING;
  744. stored_msg = downlist_master_choose ();
  745. if (!stored_msg) {
  746. log_printf (LOGSYS_LEVEL_DEBUG, "NO chosen downlist");
  747. return;
  748. }
  749. downlist_log("chosen downlist", stored_msg);
  750. group_map = qb_skiplist_create();
  751. /*
  752. * only the cpg groups included in left nodes should receive
  753. * confchg event, so we will collect these cpg groups and
  754. * relative left_lists here.
  755. */
  756. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  757. struct process_info *pi = qb_list_entry(iter, struct process_info, list);
  758. left_pi = NULL;
  759. for (i = 0; i < stored_msg->left_nodes; i++) {
  760. if (pi->nodeid == stored_msg->nodeids[i]) {
  761. left_pi = pi;
  762. break;
  763. }
  764. }
  765. if (left_pi) {
  766. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  767. cpg_group.value[cpg_group.length] = 0;
  768. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  769. if (pcd == NULL) {
  770. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  771. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  772. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  773. }
  774. size = pcd->left_list_entries;
  775. pcd->left_list[size].nodeid = left_pi->nodeid;
  776. pcd->left_list[size].pid = left_pi->pid;
  777. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  778. pcd->left_list_entries++;
  779. qb_list_del (&left_pi->list);
  780. free (left_pi);
  781. }
  782. }
  783. /* send only one confchg event per cpg group */
  784. miter = qb_map_iter_create(group_map);
  785. while (qb_map_iter_next(miter, (void **)&pcd)) {
  786. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  787. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  788. for (i=0; i<pcd->left_list_entries; i++) {
  789. log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
  790. i, cpg_print_group_name(&group),
  791. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  792. pcd->left_list[i].pid);
  793. }
  794. /* send confchg event */
  795. notify_lib_joinlist(&group, NULL,
  796. 0, NULL,
  797. pcd->left_list_entries,
  798. pcd->left_list,
  799. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  800. free(pcd);
  801. }
  802. qb_map_iter_free(miter);
  803. qb_map_destroy(group_map);
  804. }
  805. /*
  806. * Remove processes that might have left the group while we were suspended.
  807. */
  808. static void joinlist_remove_zombie_pi_entries (void)
  809. {
  810. struct qb_list_head *pi_iter, *tmp_iter;
  811. struct qb_list_head *jl_iter;
  812. struct process_info *pi;
  813. struct joinlist_msg *stored_msg;
  814. int found;
  815. qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
  816. pi = qb_list_entry (pi_iter, struct process_info, list);
  817. /*
  818. * Ignore local node
  819. */
  820. if (pi->nodeid == api->totem_nodeid_get()) {
  821. continue ;
  822. }
  823. /*
  824. * Try to find message in joinlist messages
  825. */
  826. found = 0;
  827. qb_list_for_each(jl_iter, &joinlist_messages_head) {
  828. stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
  829. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  830. continue ;
  831. }
  832. if (pi->nodeid == stored_msg->sender_nodeid &&
  833. pi->pid == stored_msg->pid &&
  834. mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
  835. found = 1;
  836. break ;
  837. }
  838. }
  839. if (!found) {
  840. do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
  841. }
  842. }
  843. }
  844. static void joinlist_inform_clients (void)
  845. {
  846. struct joinlist_msg *stored_msg;
  847. struct qb_list_head *iter;
  848. unsigned int i;
  849. i = 0;
  850. qb_list_for_each(iter, &joinlist_messages_head) {
  851. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  852. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  853. i++, cpg_print_group_name(&stored_msg->group_name),
  854. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  855. stored_msg->pid);
  856. /* Ignore our own messages */
  857. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  858. continue ;
  859. }
  860. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  861. CONFCHG_CPG_REASON_NODEUP);
  862. }
  863. joinlist_remove_zombie_pi_entries ();
  864. }
  865. static void downlist_messages_delete (void)
  866. {
  867. struct downlist_msg *stored_msg;
  868. struct qb_list_head *iter, *tmp_iter;
  869. qb_list_for_each_safe(iter, tmp_iter, &downlist_messages_head) {
  870. stored_msg = qb_list_entry(iter, struct downlist_msg, list);
  871. qb_list_del (&stored_msg->list);
  872. free (stored_msg);
  873. }
  874. }
  875. static void joinlist_messages_delete (void)
  876. {
  877. struct joinlist_msg *stored_msg;
  878. struct qb_list_head *iter, *tmp_iter;
  879. qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
  880. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  881. qb_list_del (&stored_msg->list);
  882. free (stored_msg);
  883. }
  884. qb_list_init (&joinlist_messages_head);
  885. }
  886. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  887. {
  888. qb_list_init (&downlist_messages_head);
  889. qb_list_init (&joinlist_messages_head);
  890. api = corosync_api;
  891. return (NULL);
  892. }
  893. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  894. {
  895. struct qb_list_head *iter, *tmp_iter;
  896. struct process_info *pi;
  897. qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
  898. pi = qb_list_entry (iter, struct process_info, list);
  899. qb_list_del (&pi->list);
  900. free (pi);
  901. }
  902. qb_list_del (&cpg_iteration_instance->list);
  903. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  904. }
  905. static void cpg_pd_finalize (struct cpg_pd *cpd)
  906. {
  907. struct qb_list_head *iter, *tmp_iter;
  908. struct cpg_iteration_instance *cpii;
  909. zcb_all_free(cpd);
  910. qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
  911. cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
  912. cpg_iteration_instance_finalize (cpii);
  913. }
  914. qb_list_del (&cpd->list);
  915. }
  916. static int cpg_lib_exit_fn (void *conn)
  917. {
  918. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  919. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  920. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  921. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  922. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  923. }
  924. cpg_pd_finalize (cpd);
  925. api->ipc_refcnt_dec (conn);
  926. return (0);
  927. }
  928. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  929. {
  930. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  931. struct iovec req_exec_cpg_iovec;
  932. int result;
  933. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  934. req_exec_cpg_procjoin.pid = pid;
  935. req_exec_cpg_procjoin.reason = reason;
  936. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  937. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  938. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  939. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  940. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  941. return (result);
  942. }
  943. /* Can byteswap join & leave messages */
  944. static void exec_cpg_procjoin_endian_convert (void *msg)
  945. {
  946. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  947. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  948. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  949. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  950. }
  951. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  952. {
  953. char *msg = msg_v;
  954. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  955. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  956. swab_mar_int32_t (&res->size);
  957. while ((const char*)jle < msg + res->size) {
  958. jle->pid = swab32(jle->pid);
  959. swab_mar_cpg_name_t (&jle->group_name);
  960. jle++;
  961. }
  962. }
  963. static void exec_cpg_downlist_endian_convert_old (void *msg)
  964. {
  965. }
  966. static void exec_cpg_downlist_endian_convert (void *msg)
  967. {
  968. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  969. unsigned int i;
  970. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  971. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  972. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  973. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  974. }
  975. }
  976. static void exec_cpg_mcast_endian_convert (void *msg)
  977. {
  978. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  979. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  980. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  981. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  982. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  983. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  984. }
  985. static void exec_cpg_partial_mcast_endian_convert (void *msg)
  986. {
  987. struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
  988. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  989. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  990. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  991. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  992. req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
  993. req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
  994. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  995. }
  996. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  997. struct qb_list_head *iter;
  998. qb_list_for_each(iter, &process_info_list_head) {
  999. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1000. if (pi->pid == pid && pi->nodeid == nodeid &&
  1001. mar_name_compare (&pi->group, group_name) == 0) {
  1002. return pi;
  1003. }
  1004. }
  1005. return NULL;
  1006. }
  1007. static void do_proc_join(
  1008. const mar_cpg_name_t *name,
  1009. uint32_t pid,
  1010. unsigned int nodeid,
  1011. int reason)
  1012. {
  1013. struct process_info *pi;
  1014. struct process_info *pi_entry;
  1015. mar_cpg_address_t notify_info;
  1016. struct qb_list_head *list;
  1017. struct qb_list_head *list_to_add = NULL;
  1018. if (process_info_find (name, pid, nodeid) != NULL) {
  1019. return ;
  1020. }
  1021. pi = malloc (sizeof (struct process_info));
  1022. if (!pi) {
  1023. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1024. return;
  1025. }
  1026. pi->nodeid = nodeid;
  1027. pi->pid = pid;
  1028. memcpy(&pi->group, name, sizeof(*name));
  1029. qb_list_init(&pi->list);
  1030. /*
  1031. * Insert new process in sorted order so synchronization works properly
  1032. */
  1033. list_to_add = &process_info_list_head;
  1034. qb_list_for_each(list, &process_info_list_head) {
  1035. pi_entry = qb_list_entry(list, struct process_info, list);
  1036. if (pi_entry->nodeid > pi->nodeid ||
  1037. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  1038. break;
  1039. }
  1040. list_to_add = list;
  1041. }
  1042. qb_list_add (&pi->list, list_to_add);
  1043. notify_info.pid = pi->pid;
  1044. notify_info.nodeid = nodeid;
  1045. notify_info.reason = reason;
  1046. notify_lib_joinlist(&pi->group, NULL,
  1047. 1, &notify_info,
  1048. 0, NULL,
  1049. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1050. }
  1051. static void do_proc_leave(
  1052. const mar_cpg_name_t *name,
  1053. uint32_t pid,
  1054. unsigned int nodeid,
  1055. int reason)
  1056. {
  1057. struct process_info *pi;
  1058. struct qb_list_head *iter, *tmp_iter;
  1059. mar_cpg_address_t notify_info;
  1060. notify_info.pid = pid;
  1061. notify_info.nodeid = nodeid;
  1062. notify_info.reason = reason;
  1063. notify_lib_joinlist(name, NULL,
  1064. 0, NULL,
  1065. 1, &notify_info,
  1066. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1067. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  1068. pi = qb_list_entry(iter, struct process_info, list);
  1069. if (pi->pid == pid && pi->nodeid == nodeid &&
  1070. mar_name_compare (&pi->group, name)==0) {
  1071. qb_list_del (&pi->list);
  1072. free (pi);
  1073. }
  1074. }
  1075. }
  1076. static void message_handler_req_exec_cpg_downlist_old (
  1077. const void *message,
  1078. unsigned int nodeid)
  1079. {
  1080. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node 0x%x",
  1081. nodeid);
  1082. }
  1083. static void message_handler_req_exec_cpg_downlist(
  1084. const void *message,
  1085. unsigned int nodeid)
  1086. {
  1087. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1088. int i;
  1089. struct qb_list_head *iter;
  1090. struct downlist_msg *stored_msg;
  1091. int found;
  1092. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  1093. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  1094. req_exec_cpg_downlist->left_nodes, downlist_state);
  1095. return;
  1096. }
  1097. stored_msg = malloc (sizeof (struct downlist_msg));
  1098. stored_msg->sender_nodeid = nodeid;
  1099. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  1100. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  1101. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  1102. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  1103. qb_list_init (&stored_msg->list);
  1104. qb_list_add (&stored_msg->list, &downlist_messages_head);
  1105. for (i = 0; i < my_member_list_entries; i++) {
  1106. found = 0;
  1107. qb_list_for_each(iter, &downlist_messages_head) {
  1108. stored_msg = qb_list_entry(iter, struct downlist_msg, list);
  1109. if (my_member_list[i] == stored_msg->sender_nodeid) {
  1110. found = 1;
  1111. }
  1112. }
  1113. if (!found) {
  1114. return;
  1115. }
  1116. }
  1117. downlist_master_choose_and_send ();
  1118. }
  1119. static void message_handler_req_exec_cpg_procjoin (
  1120. const void *message,
  1121. unsigned int nodeid)
  1122. {
  1123. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1124. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node 0x%x (%s) for pid %u",
  1125. nodeid,
  1126. api->totem_ifaces_print(nodeid),
  1127. (unsigned int)req_exec_cpg_procjoin->pid);
  1128. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1129. req_exec_cpg_procjoin->pid, nodeid,
  1130. CONFCHG_CPG_REASON_JOIN);
  1131. }
  1132. static void message_handler_req_exec_cpg_procleave (
  1133. const void *message,
  1134. unsigned int nodeid)
  1135. {
  1136. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1137. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node 0x%x (%s) for pid %u",
  1138. nodeid,
  1139. api->totem_ifaces_print(nodeid),
  1140. (unsigned int)req_exec_cpg_procjoin->pid);
  1141. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1142. req_exec_cpg_procjoin->pid, nodeid,
  1143. req_exec_cpg_procjoin->reason);
  1144. }
  1145. /* Got a proclist from another node */
  1146. static void message_handler_req_exec_cpg_joinlist (
  1147. const void *message_v,
  1148. unsigned int nodeid)
  1149. {
  1150. const char *message = message_v;
  1151. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1152. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1153. struct joinlist_msg *stored_msg;
  1154. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node 0x%x",
  1155. nodeid);
  1156. while ((const char*)jle < message + res->size) {
  1157. stored_msg = malloc (sizeof (struct joinlist_msg));
  1158. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1159. stored_msg->sender_nodeid = nodeid;
  1160. stored_msg->pid = jle->pid;
  1161. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1162. qb_list_init (&stored_msg->list);
  1163. qb_list_add (&stored_msg->list, &joinlist_messages_head);
  1164. jle++;
  1165. }
  1166. }
  1167. static void message_handler_req_exec_cpg_mcast (
  1168. const void *message,
  1169. unsigned int nodeid)
  1170. {
  1171. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1172. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1173. int msglen = req_exec_cpg_mcast->msglen;
  1174. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1175. struct cpg_pd *cpd;
  1176. struct iovec iovec[2];
  1177. int known_node = 0;
  1178. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1179. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1180. res_lib_cpg_mcast.msglen = msglen;
  1181. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1182. res_lib_cpg_mcast.nodeid = nodeid;
  1183. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1184. sizeof(mar_cpg_name_t));
  1185. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1186. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1187. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1188. iovec[1].iov_len = msglen;
  1189. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1190. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1191. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1192. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1193. if (!known_node) {
  1194. /* Try to find, if we know the node */
  1195. qb_list_for_each(pi_iter, &process_info_list_head) {
  1196. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1197. if (pi->nodeid == nodeid &&
  1198. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1199. known_node = 1;
  1200. break;
  1201. }
  1202. }
  1203. }
  1204. if (!known_node) {
  1205. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1206. return ;
  1207. }
  1208. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1209. }
  1210. }
  1211. }
  1212. static void message_handler_req_exec_cpg_partial_mcast (
  1213. const void *message,
  1214. unsigned int nodeid)
  1215. {
  1216. const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
  1217. struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
  1218. int msglen = req_exec_cpg_mcast->fraglen;
  1219. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1220. struct cpg_pd *cpd;
  1221. struct iovec iovec[2];
  1222. int known_node = 0;
  1223. log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node %d, size = %d bytes\n", nodeid, msglen);
  1224. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
  1225. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1226. res_lib_cpg_mcast.fraglen = msglen;
  1227. res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
  1228. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1229. res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
  1230. res_lib_cpg_mcast.nodeid = nodeid;
  1231. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1232. sizeof(mar_cpg_name_t));
  1233. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1234. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1235. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1236. iovec[1].iov_len = msglen;
  1237. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1238. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1239. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1240. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1241. if (!known_node) {
  1242. /* Try to find, if we know the node */
  1243. qb_list_for_each(pi_iter, &process_info_list_head) {
  1244. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1245. if (pi->nodeid == nodeid &&
  1246. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1247. known_node = 1;
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. if (!known_node) {
  1253. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1254. return ;
  1255. }
  1256. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1257. }
  1258. }
  1259. }
  1260. static int cpg_exec_send_downlist(void)
  1261. {
  1262. struct iovec iov;
  1263. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1264. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1265. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1266. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1267. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1268. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1269. }
  1270. static int cpg_exec_send_joinlist(void)
  1271. {
  1272. int count = 0;
  1273. struct qb_list_head *iter;
  1274. struct qb_ipc_response_header *res;
  1275. char *buf;
  1276. struct join_list_entry *jle;
  1277. struct iovec req_exec_cpg_iovec;
  1278. qb_list_for_each(iter, &process_info_list_head) {
  1279. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1280. if (pi->nodeid == api->totem_nodeid_get ()) {
  1281. count++;
  1282. }
  1283. }
  1284. /* Nothing to send */
  1285. if (!count)
  1286. return 0;
  1287. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1288. if (!buf) {
  1289. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1290. return -1;
  1291. }
  1292. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1293. res = (struct qb_ipc_response_header *)buf;
  1294. qb_list_for_each(iter, &process_info_list_head) {
  1295. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1296. if (pi->nodeid == api->totem_nodeid_get ()) {
  1297. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1298. jle->pid = pi->pid;
  1299. jle++;
  1300. }
  1301. }
  1302. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1303. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1304. req_exec_cpg_iovec.iov_base = buf;
  1305. req_exec_cpg_iovec.iov_len = res->size;
  1306. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1307. }
  1308. static int cpg_lib_init_fn (void *conn)
  1309. {
  1310. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1311. memset (cpd, 0, sizeof(struct cpg_pd));
  1312. cpd->conn = conn;
  1313. qb_list_add (&cpd->list, &cpg_pd_list_head);
  1314. qb_list_init (&cpd->iteration_instance_list_head);
  1315. qb_list_init (&cpd->zcb_mapped_list_head);
  1316. api->ipc_refcnt_inc (conn);
  1317. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1318. return (0);
  1319. }
  1320. /* Join message from the library */
  1321. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1322. {
  1323. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1324. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1325. struct res_lib_cpg_join res_lib_cpg_join;
  1326. cs_error_t error = CS_OK;
  1327. struct qb_list_head *iter;
  1328. /* Test, if we don't have same pid and group name joined */
  1329. qb_list_for_each(iter, &cpg_pd_list_head) {
  1330. struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
  1331. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1332. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1333. /* We have same pid and group name joined -> return error */
  1334. error = CS_ERR_EXIST;
  1335. goto response_send;
  1336. }
  1337. }
  1338. /*
  1339. * Same check must be done in process info list, because there may be not yet delivered
  1340. * leave of client.
  1341. */
  1342. qb_list_for_each(iter, &process_info_list_head) {
  1343. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1344. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1345. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1346. /* We have same pid and group name joined -> return error */
  1347. error = CS_ERR_TRY_AGAIN;
  1348. goto response_send;
  1349. }
  1350. }
  1351. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1352. error = CS_ERR_NAME_TOO_LONG;
  1353. goto response_send;
  1354. }
  1355. switch (cpd->cpd_state) {
  1356. case CPD_STATE_UNJOINED:
  1357. error = CS_OK;
  1358. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1359. cpd->pid = req_lib_cpg_join->pid;
  1360. cpd->flags = req_lib_cpg_join->flags;
  1361. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1362. sizeof (cpd->group_name));
  1363. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1364. &req_lib_cpg_join->group_name,
  1365. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1366. break;
  1367. case CPD_STATE_LEAVE_STARTED:
  1368. error = CS_ERR_BUSY;
  1369. break;
  1370. case CPD_STATE_JOIN_STARTED:
  1371. error = CS_ERR_EXIST;
  1372. break;
  1373. case CPD_STATE_JOIN_COMPLETED:
  1374. error = CS_ERR_EXIST;
  1375. break;
  1376. }
  1377. response_send:
  1378. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1379. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1380. res_lib_cpg_join.header.error = error;
  1381. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1382. }
  1383. /* Leave message from the library */
  1384. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1385. {
  1386. struct res_lib_cpg_leave res_lib_cpg_leave;
  1387. cs_error_t error = CS_OK;
  1388. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1389. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1390. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1391. switch (cpd->cpd_state) {
  1392. case CPD_STATE_UNJOINED:
  1393. error = CS_ERR_NOT_EXIST;
  1394. break;
  1395. case CPD_STATE_LEAVE_STARTED:
  1396. error = CS_ERR_NOT_EXIST;
  1397. break;
  1398. case CPD_STATE_JOIN_STARTED:
  1399. error = CS_ERR_BUSY;
  1400. break;
  1401. case CPD_STATE_JOIN_COMPLETED:
  1402. error = CS_OK;
  1403. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1404. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1405. &req_lib_cpg_leave->group_name,
  1406. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1407. CONFCHG_CPG_REASON_LEAVE);
  1408. break;
  1409. }
  1410. /* send return */
  1411. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1412. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1413. res_lib_cpg_leave.header.error = error;
  1414. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1415. }
  1416. /* Finalize message from library */
  1417. static void message_handler_req_lib_cpg_finalize (
  1418. void *conn,
  1419. const void *message)
  1420. {
  1421. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1422. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1423. cs_error_t error = CS_OK;
  1424. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1425. /*
  1426. * We will just remove cpd from list. After this call, connection will be
  1427. * closed on lib side, and cpg_lib_exit_fn will be called
  1428. */
  1429. qb_list_del (&cpd->list);
  1430. qb_list_init (&cpd->list);
  1431. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1432. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1433. res_lib_cpg_finalize.header.error = error;
  1434. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1435. sizeof (res_lib_cpg_finalize));
  1436. }
  1437. static int
  1438. memory_map (
  1439. const char *path,
  1440. size_t bytes,
  1441. void **buf)
  1442. {
  1443. int32_t fd;
  1444. void *addr;
  1445. int32_t res;
  1446. fd = open (path, O_RDWR, 0600);
  1447. unlink (path);
  1448. if (fd == -1) {
  1449. return (-1);
  1450. }
  1451. res = ftruncate (fd, bytes);
  1452. if (res == -1) {
  1453. goto error_close_unlink;
  1454. }
  1455. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  1456. MAP_SHARED, fd, 0);
  1457. if (addr == MAP_FAILED) {
  1458. goto error_close_unlink;
  1459. }
  1460. #ifdef MADV_NOSYNC
  1461. madvise(addr, bytes, MADV_NOSYNC);
  1462. #endif
  1463. res = close (fd);
  1464. if (res) {
  1465. munmap (addr, bytes);
  1466. return (-1);
  1467. }
  1468. *buf = addr;
  1469. return (0);
  1470. error_close_unlink:
  1471. close (fd);
  1472. unlink(path);
  1473. return -1;
  1474. }
  1475. static inline int zcb_alloc (
  1476. struct cpg_pd *cpd,
  1477. const char *path_to_file,
  1478. size_t size,
  1479. void **addr)
  1480. {
  1481. struct zcb_mapped *zcb_mapped;
  1482. unsigned int res;
  1483. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1484. if (zcb_mapped == NULL) {
  1485. return (-1);
  1486. }
  1487. res = memory_map (
  1488. path_to_file,
  1489. size,
  1490. addr);
  1491. if (res == -1) {
  1492. free (zcb_mapped);
  1493. return (-1);
  1494. }
  1495. qb_list_init (&zcb_mapped->list);
  1496. zcb_mapped->addr = *addr;
  1497. zcb_mapped->size = size;
  1498. qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1499. return (0);
  1500. }
  1501. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1502. {
  1503. unsigned int res;
  1504. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1505. qb_list_del (&zcb_mapped->list);
  1506. free (zcb_mapped);
  1507. return (res);
  1508. }
  1509. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1510. {
  1511. struct qb_list_head *list, *tmp_iter;
  1512. struct zcb_mapped *zcb_mapped;
  1513. unsigned int res = 0;
  1514. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1515. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1516. if (zcb_mapped->addr == addr) {
  1517. res = zcb_free (zcb_mapped);
  1518. break;
  1519. }
  1520. }
  1521. return (res);
  1522. }
  1523. static inline int zcb_all_free (
  1524. struct cpg_pd *cpd)
  1525. {
  1526. struct qb_list_head *list, *tmp_iter;
  1527. struct zcb_mapped *zcb_mapped;
  1528. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1529. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1530. zcb_free (zcb_mapped);
  1531. }
  1532. return (0);
  1533. }
  1534. union u {
  1535. uint64_t server_addr;
  1536. void *server_ptr;
  1537. };
  1538. static uint64_t void2serveraddr (void *server_ptr)
  1539. {
  1540. union u u;
  1541. u.server_ptr = server_ptr;
  1542. return (u.server_addr);
  1543. }
  1544. static void *serveraddr2void (uint64_t server_addr)
  1545. {
  1546. union u u;
  1547. u.server_addr = server_addr;
  1548. return (u.server_ptr);
  1549. };
  1550. static void message_handler_req_lib_cpg_zc_alloc (
  1551. void *conn,
  1552. const void *message)
  1553. {
  1554. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1555. struct qb_ipc_response_header res_header;
  1556. void *addr = NULL;
  1557. struct coroipcs_zc_header *zc_header;
  1558. unsigned int res;
  1559. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1560. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1561. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1562. &addr);
  1563. assert(res == 0);
  1564. zc_header = (struct coroipcs_zc_header *)addr;
  1565. zc_header->server_address = void2serveraddr(addr);
  1566. res_header.size = sizeof (struct qb_ipc_response_header);
  1567. res_header.id = 0;
  1568. api->ipc_response_send (conn,
  1569. &res_header,
  1570. res_header.size);
  1571. }
  1572. static void message_handler_req_lib_cpg_zc_free (
  1573. void *conn,
  1574. const void *message)
  1575. {
  1576. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1577. struct qb_ipc_response_header res_header;
  1578. void *addr = NULL;
  1579. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1580. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1581. addr = serveraddr2void (hdr->server_address);
  1582. zcb_by_addr_free (cpd, addr);
  1583. res_header.size = sizeof (struct qb_ipc_response_header);
  1584. res_header.id = 0;
  1585. api->ipc_response_send (
  1586. conn, &res_header,
  1587. res_header.size);
  1588. }
  1589. /* Fragmented mcast message from the library */
  1590. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1591. {
  1592. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1593. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1594. mar_cpg_name_t group_name = cpd->group_name;
  1595. struct iovec req_exec_cpg_iovec[2];
  1596. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1597. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1598. int msglen = req_lib_cpg_mcast->fraglen;
  1599. int result;
  1600. cs_error_t error = CS_ERR_NOT_EXIST;
  1601. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1602. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1603. switch (cpd->cpd_state) {
  1604. case CPD_STATE_UNJOINED:
  1605. error = CS_ERR_NOT_EXIST;
  1606. break;
  1607. case CPD_STATE_LEAVE_STARTED:
  1608. error = CS_ERR_NOT_EXIST;
  1609. break;
  1610. case CPD_STATE_JOIN_STARTED:
  1611. error = CS_OK;
  1612. break;
  1613. case CPD_STATE_JOIN_COMPLETED:
  1614. error = CS_OK;
  1615. break;
  1616. }
  1617. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1618. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1619. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1620. cpd->initial_transition_counter = cpd->transition_counter;
  1621. }
  1622. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1623. error = CS_ERR_INTERRUPT;
  1624. }
  1625. if (error == CS_OK) {
  1626. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1627. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1628. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1629. req_exec_cpg_mcast.pid = cpd->pid;
  1630. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1631. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1632. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1633. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1634. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1635. sizeof(mar_cpg_name_t));
  1636. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1637. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1638. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1639. req_exec_cpg_iovec[1].iov_len = msglen;
  1640. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1641. assert(result == 0);
  1642. } else {
  1643. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1644. conn, group_name.value, cpd->cpd_state, error);
  1645. }
  1646. res_lib_cpg_partial_send.header.error = error;
  1647. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1648. sizeof (res_lib_cpg_partial_send));
  1649. }
  1650. /* Mcast message from the library */
  1651. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1652. {
  1653. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1654. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1655. mar_cpg_name_t group_name = cpd->group_name;
  1656. struct iovec req_exec_cpg_iovec[2];
  1657. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1658. int msglen = req_lib_cpg_mcast->msglen;
  1659. int result;
  1660. cs_error_t error = CS_ERR_NOT_EXIST;
  1661. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1662. switch (cpd->cpd_state) {
  1663. case CPD_STATE_UNJOINED:
  1664. error = CS_ERR_NOT_EXIST;
  1665. break;
  1666. case CPD_STATE_LEAVE_STARTED:
  1667. error = CS_ERR_NOT_EXIST;
  1668. break;
  1669. case CPD_STATE_JOIN_STARTED:
  1670. error = CS_OK;
  1671. break;
  1672. case CPD_STATE_JOIN_COMPLETED:
  1673. error = CS_OK;
  1674. break;
  1675. }
  1676. if (error == CS_OK) {
  1677. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1678. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1679. MESSAGE_REQ_EXEC_CPG_MCAST);
  1680. req_exec_cpg_mcast.pid = cpd->pid;
  1681. req_exec_cpg_mcast.msglen = msglen;
  1682. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1683. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1684. sizeof(mar_cpg_name_t));
  1685. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1686. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1687. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1688. req_exec_cpg_iovec[1].iov_len = msglen;
  1689. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1690. assert(result == 0);
  1691. } else {
  1692. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1693. conn, group_name.value, cpd->cpd_state, error);
  1694. }
  1695. }
  1696. static void message_handler_req_lib_cpg_zc_execute (
  1697. void *conn,
  1698. const void *message)
  1699. {
  1700. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1701. struct qb_ipc_request_header *header;
  1702. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1703. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1704. struct iovec req_exec_cpg_iovec[2];
  1705. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1706. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1707. int result;
  1708. cs_error_t error = CS_ERR_NOT_EXIST;
  1709. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1710. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1711. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1712. switch (cpd->cpd_state) {
  1713. case CPD_STATE_UNJOINED:
  1714. error = CS_ERR_NOT_EXIST;
  1715. break;
  1716. case CPD_STATE_LEAVE_STARTED:
  1717. error = CS_ERR_NOT_EXIST;
  1718. break;
  1719. case CPD_STATE_JOIN_STARTED:
  1720. error = CS_OK;
  1721. break;
  1722. case CPD_STATE_JOIN_COMPLETED:
  1723. error = CS_OK;
  1724. break;
  1725. }
  1726. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1727. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1728. if (error == CS_OK) {
  1729. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1730. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1731. MESSAGE_REQ_EXEC_CPG_MCAST);
  1732. req_exec_cpg_mcast.pid = cpd->pid;
  1733. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1734. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1735. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1736. sizeof(mar_cpg_name_t));
  1737. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1738. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1739. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1740. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1741. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1742. if (result == 0) {
  1743. res_lib_cpg_mcast.header.error = CS_OK;
  1744. } else {
  1745. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1746. }
  1747. } else {
  1748. res_lib_cpg_mcast.header.error = error;
  1749. }
  1750. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1751. sizeof (res_lib_cpg_mcast));
  1752. }
  1753. static void message_handler_req_lib_cpg_membership (void *conn,
  1754. const void *message)
  1755. {
  1756. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1757. (struct req_lib_cpg_membership_get *)message;
  1758. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1759. struct qb_list_head *iter;
  1760. int member_count = 0;
  1761. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1762. res_lib_cpg_membership_get.header.error = CS_OK;
  1763. res_lib_cpg_membership_get.header.size =
  1764. sizeof (struct res_lib_cpg_membership_get);
  1765. qb_list_for_each(iter, &process_info_list_head) {
  1766. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1767. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1768. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1769. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1770. member_count += 1;
  1771. }
  1772. }
  1773. res_lib_cpg_membership_get.member_count = member_count;
  1774. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1775. sizeof (res_lib_cpg_membership_get));
  1776. }
  1777. static void message_handler_req_lib_cpg_local_get (void *conn,
  1778. const void *message)
  1779. {
  1780. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1781. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1782. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1783. res_lib_cpg_local_get.header.error = CS_OK;
  1784. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1785. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1786. sizeof (res_lib_cpg_local_get));
  1787. }
  1788. static void message_handler_req_lib_cpg_iteration_initialize (
  1789. void *conn,
  1790. const void *message)
  1791. {
  1792. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1793. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1794. hdb_handle_t cpg_iteration_handle = 0;
  1795. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1796. struct qb_list_head *iter, *iter2;
  1797. struct cpg_iteration_instance *cpg_iteration_instance;
  1798. cs_error_t error = CS_OK;
  1799. int res;
  1800. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1801. /* Because between calling this function and *next can be some operations which will
  1802. * change list, we must do full copy.
  1803. */
  1804. /*
  1805. * Create new iteration instance
  1806. */
  1807. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1808. &cpg_iteration_handle);
  1809. if (res != 0) {
  1810. error = CS_ERR_NO_MEMORY;
  1811. goto response_send;
  1812. }
  1813. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1814. if (res != 0) {
  1815. error = CS_ERR_BAD_HANDLE;
  1816. goto error_destroy;
  1817. }
  1818. qb_list_init (&cpg_iteration_instance->items_list_head);
  1819. cpg_iteration_instance->handle = cpg_iteration_handle;
  1820. /*
  1821. * Create copy of process_info list "grouped by" group name
  1822. */
  1823. qb_list_for_each(iter, &process_info_list_head) {
  1824. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1825. struct process_info *new_pi;
  1826. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1827. /*
  1828. * Try to find processed group name in our list new list
  1829. */
  1830. int found = 0;
  1831. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1832. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1833. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1834. found = 1;
  1835. break;
  1836. }
  1837. }
  1838. if (found) {
  1839. /*
  1840. * We have this name in list -> don't add
  1841. */
  1842. continue ;
  1843. }
  1844. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1845. /*
  1846. * Test pi group name with request
  1847. */
  1848. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1849. /*
  1850. * Not same -> don't add
  1851. */
  1852. continue ;
  1853. }
  1854. new_pi = malloc (sizeof (struct process_info));
  1855. if (!new_pi) {
  1856. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1857. error = CS_ERR_NO_MEMORY;
  1858. goto error_put_destroy;
  1859. }
  1860. memcpy (new_pi, pi, sizeof (struct process_info));
  1861. qb_list_init (&new_pi->list);
  1862. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1863. /*
  1864. * pid and nodeid -> undefined
  1865. */
  1866. new_pi->pid = new_pi->nodeid = 0;
  1867. }
  1868. /*
  1869. * We will return list "grouped" by "group name", so try to find right place to add
  1870. */
  1871. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1872. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1873. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1874. break;
  1875. }
  1876. }
  1877. qb_list_add (&new_pi->list, iter2);
  1878. }
  1879. /*
  1880. * Now we have a full "grouped by" copy of process_info list
  1881. */
  1882. /*
  1883. * Add instance to current cpd list
  1884. */
  1885. qb_list_init (&cpg_iteration_instance->list);
  1886. qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1887. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1888. error_put_destroy:
  1889. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1890. error_destroy:
  1891. if (error != CS_OK) {
  1892. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1893. }
  1894. response_send:
  1895. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1896. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1897. res_lib_cpg_iterationinitialize.header.error = error;
  1898. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1899. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1900. sizeof (res_lib_cpg_iterationinitialize));
  1901. }
  1902. static void message_handler_req_lib_cpg_iteration_next (
  1903. void *conn,
  1904. const void *message)
  1905. {
  1906. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1907. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1908. struct cpg_iteration_instance *cpg_iteration_instance;
  1909. cs_error_t error = CS_OK;
  1910. int res;
  1911. struct process_info *pi;
  1912. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1913. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1914. req_lib_cpg_iterationnext->iteration_handle,
  1915. (void *)&cpg_iteration_instance);
  1916. if (res != 0) {
  1917. error = CS_ERR_LIBRARY;
  1918. goto error_exit;
  1919. }
  1920. assert (cpg_iteration_instance);
  1921. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1922. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1923. error = CS_ERR_NO_SECTIONS;
  1924. goto error_put;
  1925. }
  1926. pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1927. /*
  1928. * Copy iteration data
  1929. */
  1930. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1931. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1932. memcpy (&res_lib_cpg_iterationnext.description.group,
  1933. &pi->group,
  1934. sizeof (mar_cpg_name_t));
  1935. error_put:
  1936. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1937. error_exit:
  1938. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1939. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1940. res_lib_cpg_iterationnext.header.error = error;
  1941. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1942. sizeof (res_lib_cpg_iterationnext));
  1943. }
  1944. static void message_handler_req_lib_cpg_iteration_finalize (
  1945. void *conn,
  1946. const void *message)
  1947. {
  1948. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1949. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1950. struct cpg_iteration_instance *cpg_iteration_instance;
  1951. cs_error_t error = CS_OK;
  1952. int res;
  1953. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1954. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1955. req_lib_cpg_iterationfinalize->iteration_handle,
  1956. (void *)&cpg_iteration_instance);
  1957. if (res != 0) {
  1958. error = CS_ERR_LIBRARY;
  1959. goto error_exit;
  1960. }
  1961. assert (cpg_iteration_instance);
  1962. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1963. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1964. error_exit:
  1965. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1966. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1967. res_lib_cpg_iterationfinalize.header.error = error;
  1968. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1969. sizeof (res_lib_cpg_iterationfinalize));
  1970. }