cpg.c 68 KB

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