cpg.c 60 KB

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