cpg.c 62 KB

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