cpg.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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 (%s) for pid %u",
  1039. nodeid,
  1040. api->totem_ifaces_print(nodeid),
  1041. (unsigned int)req_exec_cpg_procjoin->pid);
  1042. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1043. req_exec_cpg_procjoin->pid, nodeid,
  1044. CONFCHG_CPG_REASON_JOIN);
  1045. }
  1046. static void message_handler_req_exec_cpg_procleave (
  1047. const void *message,
  1048. unsigned int nodeid)
  1049. {
  1050. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1051. struct process_info *pi;
  1052. struct list_head *iter;
  1053. mar_cpg_address_t notify_info;
  1054. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d", nodeid);
  1055. notify_info.pid = req_exec_cpg_procjoin->pid;
  1056. notify_info.nodeid = nodeid;
  1057. notify_info.reason = req_exec_cpg_procjoin->reason;
  1058. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  1059. 0, NULL,
  1060. 1, &notify_info,
  1061. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1062. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  1063. pi = list_entry(iter, struct process_info, list);
  1064. iter = iter->next;
  1065. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  1066. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  1067. list_del (&pi->list);
  1068. free (pi);
  1069. }
  1070. }
  1071. }
  1072. /* Got a proclist from another node */
  1073. static void message_handler_req_exec_cpg_joinlist (
  1074. const void *message_v,
  1075. unsigned int nodeid)
  1076. {
  1077. const char *message = message_v;
  1078. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1079. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1080. struct joinlist_msg *stored_msg;
  1081. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x",
  1082. nodeid);
  1083. while ((const char*)jle < message + res->size) {
  1084. stored_msg = malloc (sizeof (struct joinlist_msg));
  1085. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1086. stored_msg->sender_nodeid = nodeid;
  1087. stored_msg->pid = jle->pid;
  1088. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1089. list_init (&stored_msg->list);
  1090. list_add (&stored_msg->list, &joinlist_messages_head);
  1091. jle++;
  1092. }
  1093. }
  1094. static void message_handler_req_exec_cpg_mcast (
  1095. const void *message,
  1096. unsigned int nodeid)
  1097. {
  1098. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1099. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1100. int msglen = req_exec_cpg_mcast->msglen;
  1101. struct list_head *iter, *pi_iter;
  1102. struct cpg_pd *cpd;
  1103. struct iovec iovec[2];
  1104. int known_node = 0;
  1105. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1106. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1107. res_lib_cpg_mcast.msglen = msglen;
  1108. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1109. res_lib_cpg_mcast.nodeid = nodeid;
  1110. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1111. sizeof(mar_cpg_name_t));
  1112. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1113. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1114. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1115. iovec[1].iov_len = msglen;
  1116. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  1117. cpd = list_entry(iter, struct cpg_pd, list);
  1118. iter = iter->next;
  1119. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1120. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1121. if (!known_node) {
  1122. /* Try to find, if we know the node */
  1123. for (pi_iter = process_info_list_head.next;
  1124. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  1125. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  1126. if (pi->nodeid == nodeid &&
  1127. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1128. known_node = 1;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. if (!known_node) {
  1134. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1135. return ;
  1136. }
  1137. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1138. }
  1139. }
  1140. }
  1141. static int cpg_exec_send_downlist(void)
  1142. {
  1143. struct iovec iov;
  1144. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1145. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1146. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1147. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1148. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1149. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1150. }
  1151. static int cpg_exec_send_joinlist(void)
  1152. {
  1153. int count = 0;
  1154. struct list_head *iter;
  1155. struct qb_ipc_response_header *res;
  1156. char *buf;
  1157. struct join_list_entry *jle;
  1158. struct iovec req_exec_cpg_iovec;
  1159. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1160. struct process_info *pi = list_entry (iter, struct process_info, list);
  1161. if (pi->nodeid == api->totem_nodeid_get ()) {
  1162. count++;
  1163. }
  1164. }
  1165. /* Nothing to send */
  1166. if (!count)
  1167. return 0;
  1168. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1169. if (!buf) {
  1170. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1171. return -1;
  1172. }
  1173. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1174. res = (struct qb_ipc_response_header *)buf;
  1175. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1176. struct process_info *pi = list_entry (iter, struct process_info, list);
  1177. if (pi->nodeid == api->totem_nodeid_get ()) {
  1178. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1179. jle->pid = pi->pid;
  1180. jle++;
  1181. }
  1182. }
  1183. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1184. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1185. req_exec_cpg_iovec.iov_base = buf;
  1186. req_exec_cpg_iovec.iov_len = res->size;
  1187. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1188. }
  1189. static int cpg_lib_init_fn (void *conn)
  1190. {
  1191. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1192. memset (cpd, 0, sizeof(struct cpg_pd));
  1193. cpd->conn = conn;
  1194. list_add (&cpd->list, &cpg_pd_list_head);
  1195. list_init (&cpd->iteration_instance_list_head);
  1196. list_init (&cpd->zcb_mapped_list_head);
  1197. api->ipc_refcnt_inc (conn);
  1198. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1199. return (0);
  1200. }
  1201. /* Join message from the library */
  1202. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1203. {
  1204. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1205. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1206. struct res_lib_cpg_join res_lib_cpg_join;
  1207. cs_error_t error = CS_OK;
  1208. struct list_head *iter;
  1209. /* Test, if we don't have same pid and group name joined */
  1210. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  1211. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  1212. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1213. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1214. /* We have same pid and group name joined -> return error */
  1215. error = CS_ERR_EXIST;
  1216. goto response_send;
  1217. }
  1218. }
  1219. /*
  1220. * Same check must be done in process info list, because there may be not yet delivered
  1221. * leave of client.
  1222. */
  1223. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1224. struct process_info *pi = list_entry (iter, struct process_info, list);
  1225. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1226. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1227. /* We have same pid and group name joined -> return error */
  1228. error = CS_ERR_TRY_AGAIN;
  1229. goto response_send;
  1230. }
  1231. }
  1232. switch (cpd->cpd_state) {
  1233. case CPD_STATE_UNJOINED:
  1234. error = CS_OK;
  1235. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1236. cpd->pid = req_lib_cpg_join->pid;
  1237. cpd->flags = req_lib_cpg_join->flags;
  1238. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1239. sizeof (cpd->group_name));
  1240. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1241. &req_lib_cpg_join->group_name,
  1242. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1243. break;
  1244. case CPD_STATE_LEAVE_STARTED:
  1245. error = CS_ERR_BUSY;
  1246. break;
  1247. case CPD_STATE_JOIN_STARTED:
  1248. error = CS_ERR_EXIST;
  1249. break;
  1250. case CPD_STATE_JOIN_COMPLETED:
  1251. error = CS_ERR_EXIST;
  1252. break;
  1253. }
  1254. response_send:
  1255. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1256. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1257. res_lib_cpg_join.header.error = error;
  1258. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1259. }
  1260. /* Leave message from the library */
  1261. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1262. {
  1263. struct res_lib_cpg_leave res_lib_cpg_leave;
  1264. cs_error_t error = CS_OK;
  1265. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1266. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1267. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1268. switch (cpd->cpd_state) {
  1269. case CPD_STATE_UNJOINED:
  1270. error = CS_ERR_NOT_EXIST;
  1271. break;
  1272. case CPD_STATE_LEAVE_STARTED:
  1273. error = CS_ERR_NOT_EXIST;
  1274. break;
  1275. case CPD_STATE_JOIN_STARTED:
  1276. error = CS_ERR_BUSY;
  1277. break;
  1278. case CPD_STATE_JOIN_COMPLETED:
  1279. error = CS_OK;
  1280. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1281. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1282. &req_lib_cpg_leave->group_name,
  1283. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1284. CONFCHG_CPG_REASON_LEAVE);
  1285. break;
  1286. }
  1287. /* send return */
  1288. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1289. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1290. res_lib_cpg_leave.header.error = error;
  1291. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1292. }
  1293. /* Finalize message from library */
  1294. static void message_handler_req_lib_cpg_finalize (
  1295. void *conn,
  1296. const void *message)
  1297. {
  1298. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1299. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1300. cs_error_t error = CS_OK;
  1301. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1302. /*
  1303. * We will just remove cpd from list. After this call, connection will be
  1304. * closed on lib side, and cpg_lib_exit_fn will be called
  1305. */
  1306. list_del (&cpd->list);
  1307. list_init (&cpd->list);
  1308. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1309. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1310. res_lib_cpg_finalize.header.error = error;
  1311. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1312. sizeof (res_lib_cpg_finalize));
  1313. }
  1314. static int
  1315. memory_map (
  1316. const char *path,
  1317. size_t bytes,
  1318. void **buf)
  1319. {
  1320. int32_t fd;
  1321. void *addr_orig;
  1322. void *addr;
  1323. int32_t res;
  1324. fd = open (path, O_RDWR, 0600);
  1325. unlink (path);
  1326. if (fd == -1) {
  1327. return (-1);
  1328. }
  1329. res = ftruncate (fd, bytes);
  1330. if (res == -1) {
  1331. goto error_close_unlink;
  1332. }
  1333. addr_orig = mmap (NULL, bytes, PROT_NONE,
  1334. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  1335. if (addr_orig == MAP_FAILED) {
  1336. goto error_close_unlink;
  1337. }
  1338. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  1339. MAP_FIXED | MAP_SHARED, fd, 0);
  1340. if (addr != addr_orig) {
  1341. munmap(addr_orig, bytes);
  1342. goto error_close_unlink;
  1343. }
  1344. #ifdef COROSYNC_BSD
  1345. madvise(addr, bytes, MADV_NOSYNC);
  1346. #endif
  1347. res = close (fd);
  1348. if (res) {
  1349. return (-1);
  1350. }
  1351. *buf = addr_orig;
  1352. return (0);
  1353. error_close_unlink:
  1354. close (fd);
  1355. unlink(path);
  1356. return -1;
  1357. }
  1358. static inline int zcb_alloc (
  1359. struct cpg_pd *cpd,
  1360. const char *path_to_file,
  1361. size_t size,
  1362. void **addr)
  1363. {
  1364. struct zcb_mapped *zcb_mapped;
  1365. unsigned int res;
  1366. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1367. if (zcb_mapped == NULL) {
  1368. return (-1);
  1369. }
  1370. res = memory_map (
  1371. path_to_file,
  1372. size,
  1373. addr);
  1374. if (res == -1) {
  1375. free (zcb_mapped);
  1376. return (-1);
  1377. }
  1378. list_init (&zcb_mapped->list);
  1379. zcb_mapped->addr = *addr;
  1380. zcb_mapped->size = size;
  1381. list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1382. return (0);
  1383. }
  1384. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1385. {
  1386. unsigned int res;
  1387. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1388. list_del (&zcb_mapped->list);
  1389. free (zcb_mapped);
  1390. return (res);
  1391. }
  1392. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1393. {
  1394. struct list_head *list;
  1395. struct zcb_mapped *zcb_mapped;
  1396. unsigned int res = 0;
  1397. for (list = cpd->zcb_mapped_list_head.next;
  1398. list != &cpd->zcb_mapped_list_head; list = list->next) {
  1399. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1400. if (zcb_mapped->addr == addr) {
  1401. res = zcb_free (zcb_mapped);
  1402. break;
  1403. }
  1404. }
  1405. return (res);
  1406. }
  1407. static inline int zcb_all_free (
  1408. struct cpg_pd *cpd)
  1409. {
  1410. struct list_head *list;
  1411. struct zcb_mapped *zcb_mapped;
  1412. for (list = cpd->zcb_mapped_list_head.next;
  1413. list != &cpd->zcb_mapped_list_head;) {
  1414. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1415. list = list->next;
  1416. zcb_free (zcb_mapped);
  1417. }
  1418. return (0);
  1419. }
  1420. union u {
  1421. uint64_t server_addr;
  1422. void *server_ptr;
  1423. };
  1424. static uint64_t void2serveraddr (void *server_ptr)
  1425. {
  1426. union u u;
  1427. u.server_ptr = server_ptr;
  1428. return (u.server_addr);
  1429. }
  1430. static void *serveraddr2void (uint64_t server_addr)
  1431. {
  1432. union u u;
  1433. u.server_addr = server_addr;
  1434. return (u.server_ptr);
  1435. };
  1436. static void message_handler_req_lib_cpg_zc_alloc (
  1437. void *conn,
  1438. const void *message)
  1439. {
  1440. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1441. struct qb_ipc_response_header res_header;
  1442. void *addr = NULL;
  1443. struct coroipcs_zc_header *zc_header;
  1444. unsigned int res;
  1445. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1446. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1447. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1448. &addr);
  1449. assert(res == 0);
  1450. zc_header = (struct coroipcs_zc_header *)addr;
  1451. zc_header->server_address = void2serveraddr(addr);
  1452. res_header.size = sizeof (struct qb_ipc_response_header);
  1453. res_header.id = 0;
  1454. api->ipc_response_send (conn,
  1455. &res_header,
  1456. res_header.size);
  1457. }
  1458. static void message_handler_req_lib_cpg_zc_free (
  1459. void *conn,
  1460. const void *message)
  1461. {
  1462. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1463. struct qb_ipc_response_header res_header;
  1464. void *addr = NULL;
  1465. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1466. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1467. addr = serveraddr2void (hdr->server_address);
  1468. zcb_by_addr_free (cpd, addr);
  1469. res_header.size = sizeof (struct qb_ipc_response_header);
  1470. res_header.id = 0;
  1471. api->ipc_response_send (
  1472. conn, &res_header,
  1473. res_header.size);
  1474. }
  1475. /* Mcast message from the library */
  1476. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1477. {
  1478. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1479. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1480. mar_cpg_name_t group_name = cpd->group_name;
  1481. struct iovec req_exec_cpg_iovec[2];
  1482. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1483. int msglen = req_lib_cpg_mcast->msglen;
  1484. int result;
  1485. cs_error_t error = CS_ERR_NOT_EXIST;
  1486. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p", conn);
  1487. switch (cpd->cpd_state) {
  1488. case CPD_STATE_UNJOINED:
  1489. error = CS_ERR_NOT_EXIST;
  1490. break;
  1491. case CPD_STATE_LEAVE_STARTED:
  1492. error = CS_ERR_NOT_EXIST;
  1493. break;
  1494. case CPD_STATE_JOIN_STARTED:
  1495. error = CS_OK;
  1496. break;
  1497. case CPD_STATE_JOIN_COMPLETED:
  1498. error = CS_OK;
  1499. break;
  1500. }
  1501. if (error == CS_OK) {
  1502. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1503. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1504. MESSAGE_REQ_EXEC_CPG_MCAST);
  1505. req_exec_cpg_mcast.pid = cpd->pid;
  1506. req_exec_cpg_mcast.msglen = msglen;
  1507. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1508. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1509. sizeof(mar_cpg_name_t));
  1510. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1511. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1512. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1513. req_exec_cpg_iovec[1].iov_len = msglen;
  1514. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1515. assert(result == 0);
  1516. } else {
  1517. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1518. conn, group_name.value, cpd->cpd_state, error);
  1519. }
  1520. }
  1521. static void message_handler_req_lib_cpg_zc_execute (
  1522. void *conn,
  1523. const void *message)
  1524. {
  1525. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1526. struct qb_ipc_request_header *header;
  1527. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1528. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1529. struct iovec req_exec_cpg_iovec[2];
  1530. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1531. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1532. int result;
  1533. cs_error_t error = CS_ERR_NOT_EXIST;
  1534. log_printf(LOGSYS_LEVEL_DEBUG, "got ZC mcast request on %p", conn);
  1535. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1536. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1537. switch (cpd->cpd_state) {
  1538. case CPD_STATE_UNJOINED:
  1539. error = CS_ERR_NOT_EXIST;
  1540. break;
  1541. case CPD_STATE_LEAVE_STARTED:
  1542. error = CS_ERR_NOT_EXIST;
  1543. break;
  1544. case CPD_STATE_JOIN_STARTED:
  1545. error = CS_OK;
  1546. break;
  1547. case CPD_STATE_JOIN_COMPLETED:
  1548. error = CS_OK;
  1549. break;
  1550. }
  1551. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1552. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1553. if (error == CS_OK) {
  1554. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1555. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1556. MESSAGE_REQ_EXEC_CPG_MCAST);
  1557. req_exec_cpg_mcast.pid = cpd->pid;
  1558. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1559. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1560. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1561. sizeof(mar_cpg_name_t));
  1562. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1563. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1564. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1565. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1566. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1567. if (result == 0) {
  1568. res_lib_cpg_mcast.header.error = CS_OK;
  1569. } else {
  1570. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1571. }
  1572. } else {
  1573. res_lib_cpg_mcast.header.error = error;
  1574. }
  1575. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1576. sizeof (res_lib_cpg_mcast));
  1577. }
  1578. static void message_handler_req_lib_cpg_membership (void *conn,
  1579. const void *message)
  1580. {
  1581. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1582. (struct req_lib_cpg_membership_get *)message;
  1583. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1584. struct list_head *iter;
  1585. int member_count = 0;
  1586. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1587. res_lib_cpg_membership_get.header.error = CS_OK;
  1588. res_lib_cpg_membership_get.header.size =
  1589. sizeof (struct req_lib_cpg_membership_get);
  1590. for (iter = process_info_list_head.next;
  1591. iter != &process_info_list_head; iter = iter->next) {
  1592. struct process_info *pi = list_entry (iter, struct process_info, list);
  1593. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1594. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1595. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1596. member_count += 1;
  1597. }
  1598. }
  1599. res_lib_cpg_membership_get.member_count = member_count;
  1600. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1601. sizeof (res_lib_cpg_membership_get));
  1602. }
  1603. static void message_handler_req_lib_cpg_local_get (void *conn,
  1604. const void *message)
  1605. {
  1606. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1607. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1608. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1609. res_lib_cpg_local_get.header.error = CS_OK;
  1610. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1611. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1612. sizeof (res_lib_cpg_local_get));
  1613. }
  1614. static void message_handler_req_lib_cpg_iteration_initialize (
  1615. void *conn,
  1616. const void *message)
  1617. {
  1618. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1619. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1620. hdb_handle_t cpg_iteration_handle = 0;
  1621. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1622. struct list_head *iter, *iter2;
  1623. struct cpg_iteration_instance *cpg_iteration_instance;
  1624. cs_error_t error = CS_OK;
  1625. int res;
  1626. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1627. /* Because between calling this function and *next can be some operations which will
  1628. * change list, we must do full copy.
  1629. */
  1630. /*
  1631. * Create new iteration instance
  1632. */
  1633. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1634. &cpg_iteration_handle);
  1635. if (res != 0) {
  1636. error = CS_ERR_NO_MEMORY;
  1637. goto response_send;
  1638. }
  1639. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1640. if (res != 0) {
  1641. error = CS_ERR_BAD_HANDLE;
  1642. goto error_destroy;
  1643. }
  1644. list_init (&cpg_iteration_instance->items_list_head);
  1645. cpg_iteration_instance->handle = cpg_iteration_handle;
  1646. /*
  1647. * Create copy of process_info list "grouped by" group name
  1648. */
  1649. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1650. struct process_info *pi = list_entry (iter, struct process_info, list);
  1651. struct process_info *new_pi;
  1652. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1653. /*
  1654. * Try to find processed group name in our list new list
  1655. */
  1656. int found = 0;
  1657. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1658. iter2 != &cpg_iteration_instance->items_list_head;
  1659. iter2 = iter2->next) {
  1660. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1661. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1662. found = 1;
  1663. break;
  1664. }
  1665. }
  1666. if (found) {
  1667. /*
  1668. * We have this name in list -> don't add
  1669. */
  1670. continue ;
  1671. }
  1672. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1673. /*
  1674. * Test pi group name with request
  1675. */
  1676. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1677. /*
  1678. * Not same -> don't add
  1679. */
  1680. continue ;
  1681. }
  1682. new_pi = malloc (sizeof (struct process_info));
  1683. if (!new_pi) {
  1684. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1685. error = CS_ERR_NO_MEMORY;
  1686. goto error_put_destroy;
  1687. }
  1688. memcpy (new_pi, pi, sizeof (struct process_info));
  1689. list_init (&new_pi->list);
  1690. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1691. /*
  1692. * pid and nodeid -> undefined
  1693. */
  1694. new_pi->pid = new_pi->nodeid = 0;
  1695. }
  1696. /*
  1697. * We will return list "grouped" by "group name", so try to find right place to add
  1698. */
  1699. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1700. iter2 != &cpg_iteration_instance->items_list_head;
  1701. iter2 = iter2->next) {
  1702. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1703. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1704. break;
  1705. }
  1706. }
  1707. list_add (&new_pi->list, iter2);
  1708. }
  1709. /*
  1710. * Now we have a full "grouped by" copy of process_info list
  1711. */
  1712. /*
  1713. * Add instance to current cpd list
  1714. */
  1715. list_init (&cpg_iteration_instance->list);
  1716. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1717. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1718. error_put_destroy:
  1719. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1720. error_destroy:
  1721. if (error != CS_OK) {
  1722. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1723. }
  1724. response_send:
  1725. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1726. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1727. res_lib_cpg_iterationinitialize.header.error = error;
  1728. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1729. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1730. sizeof (res_lib_cpg_iterationinitialize));
  1731. }
  1732. static void message_handler_req_lib_cpg_iteration_next (
  1733. void *conn,
  1734. const void *message)
  1735. {
  1736. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1737. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1738. struct cpg_iteration_instance *cpg_iteration_instance;
  1739. cs_error_t error = CS_OK;
  1740. int res;
  1741. struct process_info *pi;
  1742. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1743. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1744. req_lib_cpg_iterationnext->iteration_handle,
  1745. (void *)&cpg_iteration_instance);
  1746. if (res != 0) {
  1747. error = CS_ERR_LIBRARY;
  1748. goto error_exit;
  1749. }
  1750. assert (cpg_iteration_instance);
  1751. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1752. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1753. error = CS_ERR_NO_SECTIONS;
  1754. goto error_put;
  1755. }
  1756. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1757. /*
  1758. * Copy iteration data
  1759. */
  1760. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1761. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1762. memcpy (&res_lib_cpg_iterationnext.description.group,
  1763. &pi->group,
  1764. sizeof (mar_cpg_name_t));
  1765. error_put:
  1766. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1767. error_exit:
  1768. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1769. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1770. res_lib_cpg_iterationnext.header.error = error;
  1771. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1772. sizeof (res_lib_cpg_iterationnext));
  1773. }
  1774. static void message_handler_req_lib_cpg_iteration_finalize (
  1775. void *conn,
  1776. const void *message)
  1777. {
  1778. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1779. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1780. struct cpg_iteration_instance *cpg_iteration_instance;
  1781. cs_error_t error = CS_OK;
  1782. int res;
  1783. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1784. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1785. req_lib_cpg_iterationfinalize->iteration_handle,
  1786. (void *)&cpg_iteration_instance);
  1787. if (res != 0) {
  1788. error = CS_ERR_LIBRARY;
  1789. goto error_exit;
  1790. }
  1791. assert (cpg_iteration_instance);
  1792. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1793. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1794. error_exit:
  1795. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1796. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1797. res_lib_cpg_iterationfinalize.header.error = error;
  1798. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1799. sizeof (res_lib_cpg_iterationfinalize));
  1800. }