cpg.c 58 KB

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