cpg.c 56 KB

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