cpg.c 60 KB

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