cpg.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /*
  2. * Copyright (c) 2006-2009 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 <unistd.h>
  52. #include <netinet/in.h>
  53. #include <arpa/inet.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/coroipc_types.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/list.h>
  58. #include <corosync/jhash.h>
  59. #include <corosync/lcr/lcr_comp.h>
  60. #include <corosync/engine/logsys.h>
  61. #include <corosync/engine/coroapi.h>
  62. #include <corosync/cpg.h>
  63. #include <corosync/ipc_cpg.h>
  64. LOGSYS_DECLARE_SUBSYS ("CPG");
  65. #define GROUP_HASH_SIZE 32
  66. enum cpg_message_req_types {
  67. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  68. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  69. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  70. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  71. MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD = 4,
  72. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 5
  73. };
  74. /*
  75. * state` exec deliver
  76. * match group name, pid -> if matched deliver for YES:
  77. * XXX indicates impossible state
  78. *
  79. * join leave mcast
  80. * UNJOINED XXX XXX NO
  81. * LEAVE_STARTED XXX YES(unjoined_enter) YES
  82. * JOIN_STARTED YES(join_started_enter) XXX NO
  83. * JOIN_COMPLETED XXX NO YES
  84. *
  85. * join_started_enter
  86. * set JOIN_COMPLETED
  87. * add entry to process_info list
  88. * unjoined_enter
  89. * set UNJOINED
  90. * delete entry from process_info list
  91. *
  92. *
  93. * library accept join error codes
  94. * UNJOINED YES(CPG_OK) set JOIN_STARTED
  95. * LEAVE_STARTED NO(CPG_ERR_BUSY)
  96. * JOIN_STARTED NO(CPG_ERR_EXIST)
  97. * JOIN_COMPlETED NO(CPG_ERR_EXIST)
  98. *
  99. * library accept leave error codes
  100. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  101. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  102. * JOIN_STARTED NO(CPG_ERR_BUSY)
  103. * JOIN_COMPLETED YES(CPG_OK) set LEAVE_STARTED
  104. *
  105. * library accept mcast
  106. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  107. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  108. * JOIN_STARTED YES(CPG_OK)
  109. * JOIN_COMPLETED YES(CPG_OK)
  110. */
  111. enum cpd_state {
  112. CPD_STATE_UNJOINED,
  113. CPD_STATE_LEAVE_STARTED,
  114. CPD_STATE_JOIN_STARTED,
  115. CPD_STATE_JOIN_COMPLETED
  116. };
  117. enum cpg_sync_state {
  118. CPGSYNC_DOWNLIST,
  119. CPGSYNC_JOINLIST
  120. };
  121. enum cpg_downlist_state_e {
  122. CPG_DOWNLIST_NONE,
  123. CPG_DOWNLIST_WAITING_FOR_MESSAGES,
  124. CPG_DOWNLIST_APPLYING,
  125. };
  126. static enum cpg_downlist_state_e downlist_state;
  127. static struct list_head downlist_messages_head;
  128. static struct list_head joinlist_messages_head;
  129. struct cpg_pd {
  130. void *conn;
  131. mar_cpg_name_t group_name;
  132. uint32_t pid;
  133. enum cpd_state cpd_state;
  134. unsigned int flags;
  135. int initial_totem_conf_sent;
  136. struct list_head list;
  137. struct list_head iteration_instance_list_head;
  138. };
  139. struct cpg_iteration_instance {
  140. hdb_handle_t handle;
  141. struct list_head list;
  142. struct list_head items_list_head; /* List of process_info */
  143. struct list_head *current_pointer;
  144. };
  145. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  146. DECLARE_LIST_INIT(cpg_pd_list_head);
  147. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  148. static unsigned int my_member_list_entries;
  149. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  150. static unsigned int my_old_member_list_entries = 0;
  151. static struct corosync_api_v1 *api = NULL;
  152. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  153. static mar_cpg_ring_id_t last_sync_ring_id;
  154. struct process_info {
  155. unsigned int nodeid;
  156. uint32_t pid;
  157. mar_cpg_name_t group;
  158. struct list_head list; /* on the group_info members list */
  159. };
  160. DECLARE_LIST_INIT(process_info_list_head);
  161. struct join_list_entry {
  162. uint32_t pid;
  163. mar_cpg_name_t group_name;
  164. };
  165. /*
  166. * Service Interfaces required by service_message_handler struct
  167. */
  168. static int cpg_exec_init_fn (struct corosync_api_v1 *);
  169. static int cpg_lib_init_fn (void *conn);
  170. static int cpg_lib_exit_fn (void *conn);
  171. static void message_handler_req_exec_cpg_procjoin (
  172. const void *message,
  173. unsigned int nodeid);
  174. static void message_handler_req_exec_cpg_procleave (
  175. const void *message,
  176. unsigned int nodeid);
  177. static void message_handler_req_exec_cpg_joinlist (
  178. const void *message,
  179. unsigned int nodeid);
  180. static void message_handler_req_exec_cpg_mcast (
  181. const void *message,
  182. unsigned int nodeid);
  183. static void message_handler_req_exec_cpg_downlist_old (
  184. const void *message,
  185. unsigned int nodeid);
  186. static void message_handler_req_exec_cpg_downlist (
  187. const void *message,
  188. unsigned int nodeid);
  189. static void exec_cpg_procjoin_endian_convert (void *msg);
  190. static void exec_cpg_joinlist_endian_convert (void *msg);
  191. static void exec_cpg_mcast_endian_convert (void *msg);
  192. static void exec_cpg_downlist_endian_convert_old (void *msg);
  193. static void exec_cpg_downlist_endian_convert (void *msg);
  194. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  195. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  196. static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
  197. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  198. static void message_handler_req_lib_cpg_membership (void *conn,
  199. const void *message);
  200. static void message_handler_req_lib_cpg_local_get (void *conn,
  201. const void *message);
  202. static void message_handler_req_lib_cpg_iteration_initialize (
  203. void *conn,
  204. const void *message);
  205. static void message_handler_req_lib_cpg_iteration_next (
  206. void *conn,
  207. const void *message);
  208. static void message_handler_req_lib_cpg_iteration_finalize (
  209. void *conn,
  210. const void *message);
  211. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  212. static int cpg_exec_send_downlist(void);
  213. static int cpg_exec_send_joinlist(void);
  214. static void downlist_messages_delete (void);
  215. static void downlist_master_choose_and_send (void);
  216. static void joinlist_inform_clients (void);
  217. static void joinlist_messages_delete (void);
  218. static void cpg_sync_init_v2 (
  219. const unsigned int *trans_list,
  220. size_t trans_list_entries,
  221. const unsigned int *member_list,
  222. size_t member_list_entries,
  223. const struct memb_ring_id *ring_id);
  224. static int cpg_sync_process (void);
  225. static void cpg_sync_activate (void);
  226. static void cpg_sync_abort (void);
  227. static void do_proc_join(
  228. const mar_cpg_name_t *name,
  229. uint32_t pid,
  230. unsigned int nodeid,
  231. int reason);
  232. static int notify_lib_totem_membership (
  233. void *conn,
  234. int member_list_entries,
  235. const unsigned int *member_list);
  236. static char *cpg_print_group_name (
  237. const mar_cpg_name_t *group);
  238. /*
  239. * Library Handler Definition
  240. */
  241. static struct corosync_lib_handler cpg_lib_engine[] =
  242. {
  243. { /* 0 - MESSAGE_REQ_CPG_JOIN */
  244. .lib_handler_fn = message_handler_req_lib_cpg_join,
  245. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  246. },
  247. { /* 1 - MESSAGE_REQ_CPG_LEAVE */
  248. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  249. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  250. },
  251. { /* 2 - MESSAGE_REQ_CPG_MCAST */
  252. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  253. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  254. },
  255. { /* 3 - MESSAGE_REQ_CPG_MEMBERSHIP */
  256. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  257. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  258. },
  259. { /* 4 - MESSAGE_REQ_CPG_LOCAL_GET */
  260. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  261. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  262. },
  263. { /* 5 - MESSAGE_REQ_CPG_ITERATIONINITIALIZE */
  264. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  265. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  266. },
  267. { /* 6 - MESSAGE_REQ_CPG_ITERATIONNEXT */
  268. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  269. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  270. },
  271. { /* 7 - MESSAGE_REQ_CPG_ITERATIONFINALIZE */
  272. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  273. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  274. },
  275. { /* 8 - MESSAGE_REQ_CPG_FINALIZE */
  276. .lib_handler_fn = message_handler_req_lib_cpg_finalize,
  277. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  278. },
  279. };
  280. static struct corosync_exec_handler cpg_exec_engine[] =
  281. {
  282. { /* 0 - MESSAGE_REQ_EXEC_CPG_PROCJOIN */
  283. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  284. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  285. },
  286. { /* 1 - MESSAGE_REQ_EXEC_CPG_PROCLEAVE */
  287. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  288. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  289. },
  290. { /* 2 - MESSAGE_REQ_EXEC_CPG_JOINLIST */
  291. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  292. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  293. },
  294. { /* 3 - MESSAGE_REQ_EXEC_CPG_MCAST */
  295. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  296. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  297. },
  298. { /* 4 - MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD */
  299. .exec_handler_fn = message_handler_req_exec_cpg_downlist_old,
  300. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert_old
  301. },
  302. { /* 5 - MESSAGE_REQ_EXEC_CPG_DOWNLIST */
  303. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  304. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  305. },
  306. };
  307. struct corosync_service_engine cpg_service_engine = {
  308. .name = "corosync cluster closed process group service v1.01",
  309. .id = CPG_SERVICE,
  310. .priority = 1,
  311. .private_data_size = sizeof (struct cpg_pd),
  312. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  313. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  314. .lib_init_fn = cpg_lib_init_fn,
  315. .lib_exit_fn = cpg_lib_exit_fn,
  316. .lib_engine = cpg_lib_engine,
  317. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  318. .exec_init_fn = cpg_exec_init_fn,
  319. .exec_dump_fn = NULL,
  320. .exec_engine = cpg_exec_engine,
  321. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  322. .sync_mode = CS_SYNC_V1_APIV2,
  323. .sync_init = (sync_init_v1_fn_t)cpg_sync_init_v2,
  324. .sync_process = cpg_sync_process,
  325. .sync_activate = cpg_sync_activate,
  326. .sync_abort = cpg_sync_abort
  327. };
  328. /*
  329. * Dynamic loader definition
  330. */
  331. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void);
  332. static struct corosync_service_engine_iface_ver0 cpg_service_engine_iface = {
  333. .corosync_get_service_engine_ver0 = cpg_get_service_engine_ver0
  334. };
  335. static struct lcr_iface corosync_cpg_ver0[1] = {
  336. {
  337. .name = "corosync_cpg",
  338. .version = 0,
  339. .versions_replace = 0,
  340. .versions_replace_count = 0,
  341. .dependencies = 0,
  342. .dependency_count = 0,
  343. .constructor = NULL,
  344. .destructor = NULL,
  345. .interfaces = NULL
  346. }
  347. };
  348. static struct lcr_comp cpg_comp_ver0 = {
  349. .iface_count = 1,
  350. .ifaces = corosync_cpg_ver0
  351. };
  352. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  353. {
  354. return (&cpg_service_engine);
  355. }
  356. #ifdef COROSYNC_SOLARIS
  357. void corosync_lcr_component_register (void);
  358. void corosync_lcr_component_register (void) {
  359. #else
  360. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  361. #endif
  362. lcr_interfaces_set (&corosync_cpg_ver0[0], &cpg_service_engine_iface);
  363. lcr_component_register (&cpg_comp_ver0);
  364. }
  365. struct req_exec_cpg_procjoin {
  366. coroipc_request_header_t header __attribute__((aligned(8)));
  367. mar_cpg_name_t group_name __attribute__((aligned(8)));
  368. mar_uint32_t pid __attribute__((aligned(8)));
  369. mar_uint32_t reason __attribute__((aligned(8)));
  370. };
  371. struct req_exec_cpg_mcast {
  372. coroipc_request_header_t header __attribute__((aligned(8)));
  373. mar_cpg_name_t group_name __attribute__((aligned(8)));
  374. mar_uint32_t msglen __attribute__((aligned(8)));
  375. mar_uint32_t pid __attribute__((aligned(8)));
  376. mar_message_source_t source __attribute__((aligned(8)));
  377. mar_uint8_t message[] __attribute__((aligned(8)));
  378. };
  379. struct req_exec_cpg_downlist_old {
  380. coroipc_request_header_t header __attribute__((aligned(8)));
  381. mar_uint32_t left_nodes __attribute__((aligned(8)));
  382. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  383. };
  384. struct req_exec_cpg_downlist {
  385. coroipc_request_header_t header __attribute__((aligned(8)));
  386. /* merge decisions */
  387. mar_uint32_t old_members __attribute__((aligned(8)));
  388. /* downlist below */
  389. mar_uint32_t left_nodes __attribute__((aligned(8)));
  390. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  391. };
  392. struct downlist_msg {
  393. mar_uint32_t sender_nodeid;
  394. mar_uint32_t old_members __attribute__((aligned(8)));
  395. mar_uint32_t left_nodes __attribute__((aligned(8)));
  396. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  397. struct list_head list;
  398. };
  399. struct joinlist_msg {
  400. mar_uint32_t sender_nodeid;
  401. uint32_t pid;
  402. mar_cpg_name_t group_name;
  403. struct list_head list;
  404. };
  405. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  406. /*
  407. * Function print group name. It's not reentrant
  408. */
  409. static char *cpg_print_group_name(const mar_cpg_name_t *group)
  410. {
  411. static char res[CPG_MAX_NAME_LENGTH * 4 + 1];
  412. int dest_pos = 0;
  413. char c;
  414. int i;
  415. for (i = 0; i < group->length; i++) {
  416. c = group->value[i];
  417. if (c >= ' ' && c < 0x7f && c != '\\') {
  418. res[dest_pos++] = c;
  419. } else {
  420. if (c == '\\') {
  421. res[dest_pos++] = '\\';
  422. res[dest_pos++] = '\\';
  423. } else {
  424. snprintf(res + dest_pos, sizeof(res) - dest_pos, "\\x%02X", c);
  425. dest_pos += 4;
  426. }
  427. }
  428. }
  429. res[dest_pos] = 0;
  430. return (res);
  431. }
  432. static void cpg_sync_init_v2 (
  433. const unsigned int *trans_list,
  434. size_t trans_list_entries,
  435. const unsigned int *member_list,
  436. size_t member_list_entries,
  437. const struct memb_ring_id *ring_id)
  438. {
  439. int entries;
  440. int i, j;
  441. int found;
  442. my_sync_state = CPGSYNC_DOWNLIST;
  443. memcpy (my_member_list, member_list, member_list_entries *
  444. sizeof (unsigned int));
  445. my_member_list_entries = member_list_entries;
  446. last_sync_ring_id.nodeid = ring_id->rep.nodeid;
  447. last_sync_ring_id.seq = ring_id->seq;
  448. downlist_state = CPG_DOWNLIST_WAITING_FOR_MESSAGES;
  449. entries = 0;
  450. /*
  451. * Determine list of nodeids for downlist message
  452. */
  453. for (i = 0; i < my_old_member_list_entries; i++) {
  454. found = 0;
  455. for (j = 0; j < trans_list_entries; j++) {
  456. if (my_old_member_list[i] == trans_list[j]) {
  457. found = 1;
  458. break;
  459. }
  460. }
  461. if (found == 0) {
  462. g_req_exec_cpg_downlist.nodeids[entries++] =
  463. my_old_member_list[i];
  464. }
  465. }
  466. g_req_exec_cpg_downlist.left_nodes = entries;
  467. }
  468. static int cpg_sync_process (void)
  469. {
  470. int res = -1;
  471. if (my_sync_state == CPGSYNC_DOWNLIST) {
  472. res = cpg_exec_send_downlist();
  473. if (res == -1) {
  474. return (-1);
  475. }
  476. my_sync_state = CPGSYNC_JOINLIST;
  477. }
  478. if (my_sync_state == CPGSYNC_JOINLIST) {
  479. res = cpg_exec_send_joinlist();
  480. }
  481. return (res);
  482. }
  483. static void cpg_sync_activate (void)
  484. {
  485. memcpy (my_old_member_list, my_member_list,
  486. my_member_list_entries * sizeof (unsigned int));
  487. my_old_member_list_entries = my_member_list_entries;
  488. if (downlist_state == CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  489. downlist_master_choose_and_send ();
  490. }
  491. joinlist_inform_clients ();
  492. downlist_messages_delete ();
  493. downlist_state = CPG_DOWNLIST_NONE;
  494. joinlist_messages_delete ();
  495. notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
  496. }
  497. static void cpg_sync_abort (void)
  498. {
  499. downlist_state = CPG_DOWNLIST_NONE;
  500. downlist_messages_delete ();
  501. joinlist_messages_delete ();
  502. }
  503. static int notify_lib_totem_membership (
  504. void *conn,
  505. int member_list_entries,
  506. const unsigned int *member_list)
  507. {
  508. struct list_head *iter;
  509. char *buf;
  510. int size;
  511. struct res_lib_cpg_totem_confchg_callback *res;
  512. size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
  513. sizeof(mar_uint32_t) * (member_list_entries);
  514. buf = alloca(size);
  515. if (!buf)
  516. return CPG_ERR_LIBRARY;
  517. res = (struct res_lib_cpg_totem_confchg_callback *)buf;
  518. res->member_list_entries = member_list_entries;
  519. res->header.size = size;
  520. res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
  521. res->header.error = CS_OK;
  522. memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
  523. memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
  524. if (conn == NULL) {
  525. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  526. struct cpg_pd *cpg_pd = list_entry (iter, struct cpg_pd, list);
  527. api->ipc_dispatch_send (cpg_pd->conn, buf, size);
  528. }
  529. } else {
  530. api->ipc_dispatch_send (conn, buf, size);
  531. }
  532. return CPG_OK;
  533. }
  534. static int notify_lib_joinlist(
  535. const mar_cpg_name_t *group_name,
  536. void *conn,
  537. int joined_list_entries,
  538. mar_cpg_address_t *joined_list,
  539. int left_list_entries,
  540. mar_cpg_address_t *left_list,
  541. int id)
  542. {
  543. int size;
  544. char *buf;
  545. struct list_head *iter;
  546. int count;
  547. struct res_lib_cpg_confchg_callback *res;
  548. mar_cpg_address_t *retgi;
  549. count = 0;
  550. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  551. struct process_info *pi = list_entry (iter, struct process_info, list);
  552. if (mar_name_compare (&pi->group, group_name) == 0) {
  553. int i;
  554. int founded = 0;
  555. for (i = 0; i < left_list_entries; i++) {
  556. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  557. founded++;
  558. }
  559. }
  560. if (!founded)
  561. count++;
  562. }
  563. }
  564. size = sizeof(struct res_lib_cpg_confchg_callback) +
  565. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  566. buf = alloca(size);
  567. if (!buf)
  568. return CPG_ERR_LIBRARY;
  569. res = (struct res_lib_cpg_confchg_callback *)buf;
  570. res->joined_list_entries = joined_list_entries;
  571. res->left_list_entries = left_list_entries;
  572. res->member_list_entries = count;
  573. retgi = res->member_list;
  574. res->header.size = size;
  575. res->header.id = id;
  576. res->header.error = CS_OK;
  577. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  578. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  579. struct process_info *pi=list_entry (iter, struct process_info, list);
  580. if (mar_name_compare (&pi->group, group_name) == 0) {
  581. int i;
  582. int founded = 0;
  583. for (i = 0;i < left_list_entries; i++) {
  584. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  585. founded++;
  586. }
  587. }
  588. if (!founded) {
  589. retgi->nodeid = pi->nodeid;
  590. retgi->pid = pi->pid;
  591. retgi++;
  592. }
  593. }
  594. }
  595. if (left_list_entries) {
  596. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  597. retgi += left_list_entries;
  598. }
  599. if (joined_list_entries) {
  600. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  601. retgi += joined_list_entries;
  602. }
  603. if (conn) {
  604. api->ipc_dispatch_send (conn, buf, size);
  605. } else {
  606. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  607. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  608. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  609. assert (left_list_entries <= 1);
  610. assert (joined_list_entries <= 1);
  611. if (joined_list_entries) {
  612. if (joined_list[0].pid == cpd->pid &&
  613. joined_list[0].nodeid == api->totem_nodeid_get()) {
  614. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  615. }
  616. }
  617. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  618. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  619. api->ipc_dispatch_send (cpd->conn, buf, size);
  620. }
  621. if (left_list_entries) {
  622. if (left_list[0].pid == cpd->pid &&
  623. left_list[0].nodeid == api->totem_nodeid_get() &&
  624. left_list[0].reason == CONFCHG_CPG_REASON_LEAVE) {
  625. cpd->pid = 0;
  626. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  627. cpd->cpd_state = CPD_STATE_UNJOINED;
  628. }
  629. }
  630. }
  631. }
  632. }
  633. /*
  634. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  635. */
  636. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  637. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  638. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  639. cpd->initial_totem_conf_sent = 1;
  640. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  641. }
  642. }
  643. return CPG_OK;
  644. }
  645. static void downlist_log(int loglevel, const char *msg, struct downlist_msg* dl)
  646. {
  647. log_printf (loglevel,
  648. "%s: sender %s; members(old:%d left:%d)",
  649. msg,
  650. api->totem_ifaces_print(dl->sender_nodeid),
  651. dl->old_members,
  652. dl->left_nodes);
  653. }
  654. static struct downlist_msg* downlist_master_choose (void)
  655. {
  656. struct downlist_msg *cmp;
  657. struct downlist_msg *best = NULL;
  658. struct list_head *iter;
  659. uint32_t cmp_members;
  660. uint32_t best_members;
  661. uint32_t i;
  662. int ignore_msg;
  663. for (iter = downlist_messages_head.next;
  664. iter != &downlist_messages_head;
  665. iter = iter->next) {
  666. cmp = list_entry(iter, struct downlist_msg, list);
  667. downlist_log(LOGSYS_LEVEL_DEBUG, "comparing", cmp);
  668. ignore_msg = 0;
  669. for (i = 0; i < cmp->left_nodes; i++) {
  670. if (cmp->nodeids[i] == api->totem_nodeid_get()) {
  671. log_printf (LOG_DEBUG, "Ignoring this entry because I'm in the left list\n");
  672. ignore_msg = 1;
  673. break;
  674. }
  675. }
  676. if (ignore_msg) {
  677. continue ;
  678. }
  679. if (best == NULL) {
  680. best = cmp;
  681. continue;
  682. }
  683. best_members = best->old_members - best->left_nodes;
  684. cmp_members = cmp->old_members - cmp->left_nodes;
  685. if (cmp_members > best_members) {
  686. best = cmp;
  687. } else if (cmp_members == best_members) {
  688. if (cmp->old_members > best->old_members) {
  689. best = cmp;
  690. } else if (cmp->old_members == best->old_members) {
  691. if (cmp->sender_nodeid < best->sender_nodeid) {
  692. best = cmp;
  693. }
  694. }
  695. }
  696. }
  697. assert (best != NULL);
  698. return best;
  699. }
  700. static void downlist_master_choose_and_send (void)
  701. {
  702. struct downlist_msg *stored_msg;
  703. struct list_head *iter;
  704. mar_cpg_address_t left_list;
  705. int i;
  706. downlist_state = CPG_DOWNLIST_APPLYING;
  707. stored_msg = downlist_master_choose ();
  708. if (!stored_msg) {
  709. log_printf (LOGSYS_LEVEL_INFO, "NO chosen downlist");
  710. return;
  711. }
  712. downlist_log(LOGSYS_LEVEL_INFO, "chosen downlist", stored_msg);
  713. /* send events */
  714. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  715. struct process_info *pi = list_entry(iter, struct process_info, list);
  716. iter = iter->next;
  717. for (i = 0; i < stored_msg->left_nodes; i++) {
  718. if (pi->nodeid == stored_msg->nodeids[i]) {
  719. left_list.nodeid = pi->nodeid;
  720. left_list.pid = pi->pid;
  721. left_list.reason = CONFCHG_CPG_REASON_NODEDOWN;
  722. notify_lib_joinlist(&pi->group, NULL,
  723. 0, NULL,
  724. 1, &left_list,
  725. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  726. list_del (&pi->list);
  727. free (pi);
  728. break;
  729. }
  730. }
  731. }
  732. }
  733. static void joinlist_inform_clients (void)
  734. {
  735. struct joinlist_msg *stored_msg;
  736. struct list_head *iter;
  737. unsigned int i;
  738. i = 0;
  739. for (iter = joinlist_messages_head.next;
  740. iter != &joinlist_messages_head;
  741. iter = iter->next) {
  742. stored_msg = list_entry(iter, struct joinlist_msg, list);
  743. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  744. i++, cpg_print_group_name(&stored_msg->group_name),
  745. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  746. stored_msg->pid);
  747. /* Ignore our own messages */
  748. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  749. continue ;
  750. }
  751. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  752. CONFCHG_CPG_REASON_NODEUP);
  753. }
  754. }
  755. static void downlist_messages_delete (void)
  756. {
  757. struct downlist_msg *stored_msg;
  758. struct list_head *iter, *iter_next;
  759. for (iter = downlist_messages_head.next;
  760. iter != &downlist_messages_head;
  761. iter = iter_next) {
  762. iter_next = iter->next;
  763. stored_msg = list_entry(iter, struct downlist_msg, list);
  764. list_del (&stored_msg->list);
  765. free (stored_msg);
  766. }
  767. }
  768. static void joinlist_messages_delete (void)
  769. {
  770. struct joinlist_msg *stored_msg;
  771. struct list_head *iter, *iter_next;
  772. for (iter = joinlist_messages_head.next;
  773. iter != &joinlist_messages_head;
  774. iter = iter_next) {
  775. iter_next = iter->next;
  776. stored_msg = list_entry(iter, struct joinlist_msg, list);
  777. list_del (&stored_msg->list);
  778. free (stored_msg);
  779. }
  780. list_init (&joinlist_messages_head);
  781. }
  782. static int cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  783. {
  784. #ifdef COROSYNC_SOLARIS
  785. logsys_subsys_init();
  786. #endif
  787. list_init (&downlist_messages_head);
  788. list_init (&joinlist_messages_head);
  789. api = corosync_api;
  790. return (0);
  791. }
  792. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  793. {
  794. struct list_head *iter, *iter_next;
  795. struct process_info *pi;
  796. for (iter = cpg_iteration_instance->items_list_head.next;
  797. iter != &cpg_iteration_instance->items_list_head;
  798. iter = iter_next) {
  799. iter_next = iter->next;
  800. pi = list_entry (iter, struct process_info, list);
  801. list_del (&pi->list);
  802. free (pi);
  803. }
  804. list_del (&cpg_iteration_instance->list);
  805. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  806. }
  807. static void cpg_pd_finalize (struct cpg_pd *cpd)
  808. {
  809. struct list_head *iter, *iter_next;
  810. struct cpg_iteration_instance *cpii;
  811. for (iter = cpd->iteration_instance_list_head.next;
  812. iter != &cpd->iteration_instance_list_head;
  813. iter = iter_next) {
  814. iter_next = iter->next;
  815. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  816. cpg_iteration_instance_finalize (cpii);
  817. }
  818. list_del (&cpd->list);
  819. }
  820. static int cpg_lib_exit_fn (void *conn)
  821. {
  822. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  823. int result;
  824. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  825. if (cpd->group_name.length > 0) {
  826. result = cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  827. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  828. if (result == -1) {
  829. /*
  830. * Call this function again later
  831. */
  832. return (result);
  833. }
  834. }
  835. cpg_pd_finalize (cpd);
  836. api->ipc_refcnt_dec (conn);
  837. return (0);
  838. }
  839. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  840. {
  841. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  842. struct iovec req_exec_cpg_iovec;
  843. int result;
  844. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  845. req_exec_cpg_procjoin.pid = pid;
  846. req_exec_cpg_procjoin.reason = reason;
  847. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  848. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  849. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  850. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  851. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  852. return (result);
  853. }
  854. /* Can byteswap join & leave messages */
  855. static void exec_cpg_procjoin_endian_convert (void *msg)
  856. {
  857. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  858. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  859. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  860. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  861. }
  862. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  863. {
  864. char *msg = msg_v;
  865. coroipc_response_header_t *res = (coroipc_response_header_t *)msg;
  866. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(coroipc_response_header_t));
  867. swab_mar_int32_t (&res->size);
  868. while ((const char*)jle < msg + res->size) {
  869. jle->pid = swab32(jle->pid);
  870. swab_mar_cpg_name_t (&jle->group_name);
  871. jle++;
  872. }
  873. }
  874. static void exec_cpg_downlist_endian_convert_old (void *msg)
  875. {
  876. }
  877. static void exec_cpg_downlist_endian_convert (void *msg)
  878. {
  879. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  880. unsigned int i;
  881. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  882. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  883. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  884. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  885. }
  886. }
  887. static void exec_cpg_mcast_endian_convert (void *msg)
  888. {
  889. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  890. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  891. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  892. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  893. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  894. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  895. }
  896. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  897. struct list_head *iter;
  898. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  899. struct process_info *pi = list_entry (iter, struct process_info, list);
  900. iter = iter->next;
  901. if (pi->pid == pid && pi->nodeid == nodeid &&
  902. mar_name_compare (&pi->group, group_name) == 0) {
  903. return pi;
  904. }
  905. }
  906. return NULL;
  907. }
  908. static void do_proc_join(
  909. const mar_cpg_name_t *name,
  910. uint32_t pid,
  911. unsigned int nodeid,
  912. int reason)
  913. {
  914. struct process_info *pi;
  915. struct process_info *pi_entry;
  916. mar_cpg_address_t notify_info;
  917. struct list_head *list;
  918. struct list_head *list_to_add = NULL;
  919. if (process_info_find (name, pid, nodeid) != NULL) {
  920. return ;
  921. }
  922. pi = malloc (sizeof (struct process_info));
  923. if (!pi) {
  924. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  925. return;
  926. }
  927. pi->nodeid = nodeid;
  928. pi->pid = pid;
  929. memcpy(&pi->group, name, sizeof(*name));
  930. list_init(&pi->list);
  931. /*
  932. * Insert new process in sorted order so synchronization works properly
  933. */
  934. list_to_add = &process_info_list_head;
  935. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  936. pi_entry = list_entry(list, struct process_info, list);
  937. if (pi_entry->nodeid > pi->nodeid ||
  938. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  939. break;
  940. }
  941. list_to_add = list;
  942. }
  943. list_add (&pi->list, list_to_add);
  944. notify_info.pid = pi->pid;
  945. notify_info.nodeid = nodeid;
  946. notify_info.reason = reason;
  947. notify_lib_joinlist(&pi->group, NULL,
  948. 1, &notify_info,
  949. 0, NULL,
  950. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  951. }
  952. static void message_handler_req_exec_cpg_downlist_old (
  953. const void *message,
  954. unsigned int nodeid)
  955. {
  956. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node %d",
  957. nodeid);
  958. }
  959. static void message_handler_req_exec_cpg_downlist(
  960. const void *message,
  961. unsigned int nodeid)
  962. {
  963. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  964. int i;
  965. struct list_head *iter;
  966. struct downlist_msg *stored_msg;
  967. int found;
  968. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  969. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  970. req_exec_cpg_downlist->left_nodes, downlist_state);
  971. return;
  972. }
  973. stored_msg = malloc (sizeof (struct downlist_msg));
  974. stored_msg->sender_nodeid = nodeid;
  975. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  976. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  977. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  978. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  979. list_init (&stored_msg->list);
  980. list_add (&stored_msg->list, &downlist_messages_head);
  981. for (i = 0; i < my_member_list_entries; i++) {
  982. found = 0;
  983. for (iter = downlist_messages_head.next;
  984. iter != &downlist_messages_head;
  985. iter = iter->next) {
  986. stored_msg = list_entry(iter, struct downlist_msg, list);
  987. if (my_member_list[i] == stored_msg->sender_nodeid) {
  988. found = 1;
  989. }
  990. }
  991. if (!found) {
  992. return;
  993. }
  994. }
  995. downlist_master_choose_and_send ();
  996. }
  997. static void message_handler_req_exec_cpg_procjoin (
  998. const void *message,
  999. unsigned int nodeid)
  1000. {
  1001. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1002. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d (%s) for pid %u",
  1003. nodeid,
  1004. api->totem_ifaces_print(nodeid),
  1005. (unsigned int)req_exec_cpg_procjoin->pid);
  1006. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1007. req_exec_cpg_procjoin->pid, nodeid,
  1008. CONFCHG_CPG_REASON_JOIN);
  1009. }
  1010. static void message_handler_req_exec_cpg_procleave (
  1011. const void *message,
  1012. unsigned int nodeid)
  1013. {
  1014. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1015. struct process_info *pi;
  1016. struct list_head *iter;
  1017. mar_cpg_address_t notify_info;
  1018. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d\n", nodeid);
  1019. notify_info.pid = req_exec_cpg_procjoin->pid;
  1020. notify_info.nodeid = nodeid;
  1021. notify_info.reason = req_exec_cpg_procjoin->reason;
  1022. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  1023. 0, NULL,
  1024. 1, &notify_info,
  1025. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1026. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  1027. pi = list_entry(iter, struct process_info, list);
  1028. iter = iter->next;
  1029. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  1030. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  1031. list_del (&pi->list);
  1032. free (pi);
  1033. }
  1034. }
  1035. }
  1036. /* Got a proclist from another node */
  1037. static void message_handler_req_exec_cpg_joinlist (
  1038. const void *message_v,
  1039. unsigned int nodeid)
  1040. {
  1041. const char *message = message_v;
  1042. const coroipc_response_header_t *res = (const coroipc_response_header_t *)message;
  1043. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(coroipc_response_header_t));
  1044. struct joinlist_msg *stored_msg;
  1045. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x\n",
  1046. nodeid);
  1047. while ((const char*)jle < message + res->size) {
  1048. stored_msg = malloc (sizeof (struct joinlist_msg));
  1049. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1050. stored_msg->sender_nodeid = nodeid;
  1051. stored_msg->pid = jle->pid;
  1052. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1053. list_init (&stored_msg->list);
  1054. list_add (&stored_msg->list, &joinlist_messages_head);
  1055. jle++;
  1056. }
  1057. }
  1058. static void message_handler_req_exec_cpg_mcast (
  1059. const void *message,
  1060. unsigned int nodeid)
  1061. {
  1062. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1063. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1064. int msglen = req_exec_cpg_mcast->msglen;
  1065. struct list_head *iter, *pi_iter;
  1066. struct cpg_pd *cpd;
  1067. struct iovec iovec[2];
  1068. int known_node = 0;
  1069. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1070. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1071. res_lib_cpg_mcast.msglen = msglen;
  1072. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1073. res_lib_cpg_mcast.nodeid = nodeid;
  1074. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1075. sizeof(mar_cpg_name_t));
  1076. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1077. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1078. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1079. iovec[1].iov_len = msglen;
  1080. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  1081. cpd = list_entry(iter, struct cpg_pd, list);
  1082. iter = iter->next;
  1083. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1084. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1085. if (!known_node) {
  1086. /* Try to find, if we know the node */
  1087. for (pi_iter = process_info_list_head.next;
  1088. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  1089. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  1090. if (pi->nodeid == nodeid &&
  1091. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1092. known_node = 1;
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. if (!known_node) {
  1098. /* Unknown node -> we will not deliver message */
  1099. return ;
  1100. }
  1101. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1102. }
  1103. }
  1104. }
  1105. static int cpg_exec_send_downlist(void)
  1106. {
  1107. struct iovec iov;
  1108. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1109. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1110. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1111. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1112. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1113. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1114. }
  1115. static int cpg_exec_send_joinlist(void)
  1116. {
  1117. int count = 0;
  1118. struct list_head *iter;
  1119. coroipc_response_header_t *res;
  1120. char *buf;
  1121. struct join_list_entry *jle;
  1122. struct iovec req_exec_cpg_iovec;
  1123. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1124. struct process_info *pi = list_entry (iter, struct process_info, list);
  1125. if (pi->nodeid == api->totem_nodeid_get ()) {
  1126. count++;
  1127. }
  1128. }
  1129. /* Nothing to send */
  1130. if (!count)
  1131. return 0;
  1132. buf = alloca(sizeof(coroipc_response_header_t) + sizeof(struct join_list_entry) * count);
  1133. if (!buf) {
  1134. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1135. return -1;
  1136. }
  1137. jle = (struct join_list_entry *)(buf + sizeof(coroipc_response_header_t));
  1138. res = (coroipc_response_header_t *)buf;
  1139. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1140. struct process_info *pi = list_entry (iter, struct process_info, list);
  1141. if (pi->nodeid == api->totem_nodeid_get ()) {
  1142. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1143. jle->pid = pi->pid;
  1144. jle++;
  1145. }
  1146. }
  1147. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1148. res->size = sizeof(coroipc_response_header_t)+sizeof(struct join_list_entry) * count;
  1149. req_exec_cpg_iovec.iov_base = buf;
  1150. req_exec_cpg_iovec.iov_len = res->size;
  1151. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1152. }
  1153. static int cpg_lib_init_fn (void *conn)
  1154. {
  1155. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1156. memset (cpd, 0, sizeof(struct cpg_pd));
  1157. cpd->conn = conn;
  1158. list_add (&cpd->list, &cpg_pd_list_head);
  1159. list_init (&cpd->iteration_instance_list_head);
  1160. api->ipc_refcnt_inc (conn);
  1161. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p\n", conn, cpd);
  1162. return (0);
  1163. }
  1164. /* Join message from the library */
  1165. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1166. {
  1167. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1168. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1169. struct cpg_pd tmp_cpd;
  1170. struct res_lib_cpg_join res_lib_cpg_join;
  1171. cs_error_t error = CPG_OK;
  1172. struct list_head *iter;
  1173. int result;
  1174. /* Test, if we don't have same pid and group name joined */
  1175. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  1176. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  1177. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1178. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1179. /* We have same pid and group name joined -> return error */
  1180. error = CPG_ERR_EXIST;
  1181. goto response_send;
  1182. }
  1183. }
  1184. /*
  1185. * Same check must be done in process info list, because there may be not yet delivered
  1186. * leave of client.
  1187. */
  1188. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1189. struct process_info *pi = list_entry (iter, struct process_info, list);
  1190. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1191. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1192. /* We have same pid and group name joined -> return error */
  1193. error = CPG_ERR_TRY_AGAIN;
  1194. goto response_send;
  1195. }
  1196. }
  1197. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1198. error = CS_ERR_NAME_TOO_LONG;
  1199. goto response_send;
  1200. }
  1201. switch (cpd->cpd_state) {
  1202. case CPD_STATE_UNJOINED:
  1203. error = CPG_OK;
  1204. /*
  1205. * Make copy of cpd, to restore if cpg_node_joinleave_send fails
  1206. */
  1207. memcpy (&tmp_cpd, cpd, sizeof(tmp_cpd));
  1208. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1209. cpd->pid = req_lib_cpg_join->pid;
  1210. cpd->flags = req_lib_cpg_join->flags;
  1211. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1212. sizeof (cpd->group_name));
  1213. result = cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1214. &req_lib_cpg_join->group_name,
  1215. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1216. if (result == -1) {
  1217. error = CPG_ERR_TRY_AGAIN;
  1218. /*
  1219. * Restore cpd
  1220. */
  1221. memcpy (cpd, &tmp_cpd, sizeof(tmp_cpd));
  1222. goto response_send;
  1223. }
  1224. break;
  1225. case CPD_STATE_LEAVE_STARTED:
  1226. error = CPG_ERR_BUSY;
  1227. break;
  1228. case CPD_STATE_JOIN_STARTED:
  1229. error = CPG_ERR_EXIST;
  1230. break;
  1231. case CPD_STATE_JOIN_COMPLETED:
  1232. error = CPG_ERR_EXIST;
  1233. break;
  1234. }
  1235. response_send:
  1236. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1237. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1238. res_lib_cpg_join.header.error = error;
  1239. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1240. }
  1241. /* Leave message from the library */
  1242. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1243. {
  1244. struct res_lib_cpg_leave res_lib_cpg_leave;
  1245. cs_error_t error = CPG_OK;
  1246. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1247. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1248. int result;
  1249. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p\n", conn);
  1250. switch (cpd->cpd_state) {
  1251. case CPD_STATE_UNJOINED:
  1252. error = CPG_ERR_NOT_EXIST;
  1253. break;
  1254. case CPD_STATE_LEAVE_STARTED:
  1255. error = CPG_ERR_NOT_EXIST;
  1256. break;
  1257. case CPD_STATE_JOIN_STARTED:
  1258. error = CPG_ERR_BUSY;
  1259. break;
  1260. case CPD_STATE_JOIN_COMPLETED:
  1261. error = CPG_OK;
  1262. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1263. result = cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1264. &req_lib_cpg_leave->group_name,
  1265. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1266. CONFCHG_CPG_REASON_LEAVE);
  1267. if (result == -1) {
  1268. error = CPG_ERR_TRY_AGAIN;
  1269. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  1270. }
  1271. break;
  1272. }
  1273. /* send return */
  1274. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1275. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1276. res_lib_cpg_leave.header.error = error;
  1277. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1278. }
  1279. /* Finalize message from library */
  1280. static void message_handler_req_lib_cpg_finalize (
  1281. void *conn,
  1282. const void *message)
  1283. {
  1284. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1285. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1286. cs_error_t error = CS_OK;
  1287. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p\n", conn);
  1288. /*
  1289. * We will just remove cpd from list. After this call, connection will be
  1290. * closed on lib side, and cpg_lib_exit_fn will be called
  1291. */
  1292. list_del (&cpd->list);
  1293. list_init (&cpd->list);
  1294. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1295. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1296. res_lib_cpg_finalize.header.error = error;
  1297. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1298. sizeof (res_lib_cpg_finalize));
  1299. }
  1300. /* Mcast message from the library */
  1301. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1302. {
  1303. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1304. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1305. mar_cpg_name_t group_name = cpd->group_name;
  1306. struct iovec req_exec_cpg_iovec[2];
  1307. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1308. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1309. int msglen = req_lib_cpg_mcast->msglen;
  1310. int result;
  1311. cs_error_t error = CPG_ERR_NOT_EXIST;
  1312. TRACE1("got mcast request on %p\n", conn);
  1313. switch (cpd->cpd_state) {
  1314. case CPD_STATE_UNJOINED:
  1315. error = CPG_ERR_NOT_EXIST;
  1316. break;
  1317. case CPD_STATE_LEAVE_STARTED:
  1318. error = CPG_ERR_NOT_EXIST;
  1319. break;
  1320. case CPD_STATE_JOIN_STARTED:
  1321. error = CPG_OK;
  1322. break;
  1323. case CPD_STATE_JOIN_COMPLETED:
  1324. error = CPG_OK;
  1325. break;
  1326. }
  1327. if (error == CPG_OK) {
  1328. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1329. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1330. MESSAGE_REQ_EXEC_CPG_MCAST);
  1331. req_exec_cpg_mcast.pid = cpd->pid;
  1332. req_exec_cpg_mcast.msglen = msglen;
  1333. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1334. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1335. sizeof(mar_cpg_name_t));
  1336. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1337. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1338. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1339. req_exec_cpg_iovec[1].iov_len = msglen;
  1340. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1341. if (result == -1) {
  1342. error = CPG_ERR_TRY_AGAIN;
  1343. }
  1344. }
  1345. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1346. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1347. res_lib_cpg_mcast.header.error = error;
  1348. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1349. sizeof (res_lib_cpg_mcast));
  1350. }
  1351. static void message_handler_req_lib_cpg_membership (void *conn,
  1352. const void *message)
  1353. {
  1354. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1355. (struct req_lib_cpg_membership_get *)message;
  1356. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1357. struct list_head *iter;
  1358. int member_count = 0;
  1359. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1360. res_lib_cpg_membership_get.header.error = CPG_OK;
  1361. res_lib_cpg_membership_get.header.size =
  1362. sizeof (struct res_lib_cpg_membership_get);
  1363. for (iter = process_info_list_head.next;
  1364. iter != &process_info_list_head; iter = iter->next) {
  1365. struct process_info *pi = list_entry (iter, struct process_info, list);
  1366. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1367. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1368. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1369. member_count += 1;
  1370. }
  1371. }
  1372. res_lib_cpg_membership_get.member_count = member_count;
  1373. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1374. sizeof (res_lib_cpg_membership_get));
  1375. }
  1376. static void message_handler_req_lib_cpg_local_get (void *conn,
  1377. const void *message)
  1378. {
  1379. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1380. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1381. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1382. res_lib_cpg_local_get.header.error = CPG_OK;
  1383. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1384. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1385. sizeof (res_lib_cpg_local_get));
  1386. }
  1387. static void message_handler_req_lib_cpg_iteration_initialize (
  1388. void *conn,
  1389. const void *message)
  1390. {
  1391. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1392. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1393. hdb_handle_t cpg_iteration_handle = 0;
  1394. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1395. struct list_head *iter, *iter2;
  1396. struct cpg_iteration_instance *cpg_iteration_instance;
  1397. cs_error_t error = CS_OK;
  1398. int res;
  1399. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize\n");
  1400. /* Because between calling this function and *next can be some operations which will
  1401. * change list, we must do full copy.
  1402. */
  1403. /*
  1404. * Create new iteration instance
  1405. */
  1406. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1407. &cpg_iteration_handle);
  1408. if (res != 0) {
  1409. error = CS_ERR_NO_MEMORY;
  1410. goto response_send;
  1411. }
  1412. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1413. if (res != 0) {
  1414. error = CS_ERR_BAD_HANDLE;
  1415. goto error_destroy;
  1416. }
  1417. list_init (&cpg_iteration_instance->items_list_head);
  1418. cpg_iteration_instance->handle = cpg_iteration_handle;
  1419. /*
  1420. * Create copy of process_info list "grouped by" group name
  1421. */
  1422. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1423. struct process_info *pi = list_entry (iter, struct process_info, list);
  1424. struct process_info *new_pi;
  1425. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1426. /*
  1427. * Try to find processed group name in our list new list
  1428. */
  1429. int found = 0;
  1430. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1431. iter2 != &cpg_iteration_instance->items_list_head;
  1432. iter2 = iter2->next) {
  1433. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1434. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1435. found = 1;
  1436. break;
  1437. }
  1438. }
  1439. if (found) {
  1440. /*
  1441. * We have this name in list -> don't add
  1442. */
  1443. continue ;
  1444. }
  1445. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1446. /*
  1447. * Test pi group name with request
  1448. */
  1449. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1450. /*
  1451. * Not same -> don't add
  1452. */
  1453. continue ;
  1454. }
  1455. new_pi = malloc (sizeof (struct process_info));
  1456. if (!new_pi) {
  1457. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1458. error = CS_ERR_NO_MEMORY;
  1459. goto error_put_destroy;
  1460. }
  1461. memcpy (new_pi, pi, sizeof (struct process_info));
  1462. list_init (&new_pi->list);
  1463. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1464. /*
  1465. * pid and nodeid -> undefined
  1466. */
  1467. new_pi->pid = new_pi->nodeid = 0;
  1468. }
  1469. /*
  1470. * We will return list "grouped" by "group name", so try to find right place to add
  1471. */
  1472. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1473. iter2 != &cpg_iteration_instance->items_list_head;
  1474. iter2 = iter2->next) {
  1475. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1476. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1477. break;
  1478. }
  1479. }
  1480. list_add (&new_pi->list, iter2);
  1481. }
  1482. /*
  1483. * Now we have a full "grouped by" copy of process_info list
  1484. */
  1485. /*
  1486. * Add instance to current cpd list
  1487. */
  1488. list_init (&cpg_iteration_instance->list);
  1489. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1490. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1491. error_put_destroy:
  1492. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1493. error_destroy:
  1494. if (error != CS_OK) {
  1495. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1496. }
  1497. response_send:
  1498. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1499. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1500. res_lib_cpg_iterationinitialize.header.error = error;
  1501. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1502. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1503. sizeof (res_lib_cpg_iterationinitialize));
  1504. }
  1505. static void message_handler_req_lib_cpg_iteration_next (
  1506. void *conn,
  1507. const void *message)
  1508. {
  1509. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1510. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1511. struct cpg_iteration_instance *cpg_iteration_instance;
  1512. cs_error_t error = CS_OK;
  1513. int res;
  1514. struct process_info *pi;
  1515. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next\n");
  1516. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1517. req_lib_cpg_iterationnext->iteration_handle,
  1518. (void *)&cpg_iteration_instance);
  1519. if (res != 0) {
  1520. error = CS_ERR_LIBRARY;
  1521. goto error_exit;
  1522. }
  1523. assert (cpg_iteration_instance);
  1524. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1525. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1526. error = CS_ERR_NO_SECTIONS;
  1527. goto error_put;
  1528. }
  1529. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1530. /*
  1531. * Copy iteration data
  1532. */
  1533. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1534. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1535. memcpy (&res_lib_cpg_iterationnext.description.group,
  1536. &pi->group,
  1537. sizeof (mar_cpg_name_t));
  1538. error_put:
  1539. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1540. error_exit:
  1541. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1542. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1543. res_lib_cpg_iterationnext.header.error = error;
  1544. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1545. sizeof (res_lib_cpg_iterationnext));
  1546. }
  1547. static void message_handler_req_lib_cpg_iteration_finalize (
  1548. void *conn,
  1549. const void *message)
  1550. {
  1551. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1552. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1553. struct cpg_iteration_instance *cpg_iteration_instance;
  1554. cs_error_t error = CS_OK;
  1555. int res;
  1556. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize\n");
  1557. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1558. req_lib_cpg_iterationfinalize->iteration_handle,
  1559. (void *)&cpg_iteration_instance);
  1560. if (res != 0) {
  1561. error = CS_ERR_LIBRARY;
  1562. goto error_exit;
  1563. }
  1564. assert (cpg_iteration_instance);
  1565. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1566. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1567. error_exit:
  1568. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1569. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1570. res_lib_cpg_iterationfinalize.header.error = error;
  1571. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1572. sizeof (res_lib_cpg_iterationfinalize));
  1573. }