cpg.c 60 KB

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