cpg.c 57 KB

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