4
0

cpg.c 57 KB

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