cpg.c 57 KB

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