cpg.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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/qblist.h>
  57. #include <qb/qbmap.h>
  58. #include <corosync/corotypes.h>
  59. #include <qb/qbipc_common.h>
  60. #include <corosync/corodefs.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 qb_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 qb_list_head downlist_messages_head;
  139. static struct qb_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 qb_list_head list;
  150. struct qb_list_head iteration_instance_list_head;
  151. struct qb_list_head zcb_mapped_list_head;
  152. };
  153. struct cpg_iteration_instance {
  154. hdb_handle_t handle;
  155. struct qb_list_head list;
  156. struct qb_list_head items_list_head; /* List of process_info */
  157. struct qb_list_head *current_pointer;
  158. };
  159. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  160. QB_LIST_DECLARE (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 qb_list_head list; /* on the group_info members list */
  173. };
  174. QB_LIST_DECLARE (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 qb_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 qb_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 qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  557. struct cpg_pd *cpg_pd = qb_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 qb_list_head *iter;
  577. int count;
  578. struct res_lib_cpg_confchg_callback *res;
  579. mar_cpg_address_t *retgi;
  580. count = 0;
  581. qb_list_for_each(iter, &process_info_list_head) {
  582. struct process_info *pi = qb_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. qb_list_for_each(iter, &process_info_list_head) {
  610. struct process_info *pi=qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  638. struct cpg_pd *cpd = qb_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. qb_list_for_each(iter, &cpg_pd_list_head) {
  668. struct cpg_pd *cpd = qb_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 qb_list_head *iter;
  690. uint32_t cmp_members;
  691. uint32_t best_members;
  692. uint32_t i;
  693. int ignore_msg;
  694. qb_list_for_each(iter, &downlist_messages_head) {
  695. cmp = qb_list_entry(iter, struct downlist_msg, list);
  696. downlist_log("comparing", cmp);
  697. ignore_msg = 0;
  698. for (i = 0; i < cmp->left_nodes; i++) {
  699. if (cmp->nodeids[i] == api->totem_nodeid_get()) {
  700. log_printf (LOG_DEBUG, "Ignoring this entry because I'm in the left list\n");
  701. ignore_msg = 1;
  702. break;
  703. }
  704. }
  705. if (ignore_msg) {
  706. continue ;
  707. }
  708. if (best == NULL) {
  709. best = cmp;
  710. continue;
  711. }
  712. best_members = best->old_members - best->left_nodes;
  713. cmp_members = cmp->old_members - cmp->left_nodes;
  714. if (cmp_members > best_members) {
  715. best = cmp;
  716. } else if (cmp_members == best_members) {
  717. if (cmp->old_members > best->old_members) {
  718. best = cmp;
  719. } else if (cmp->old_members == best->old_members) {
  720. if (cmp->sender_nodeid < best->sender_nodeid) {
  721. best = cmp;
  722. }
  723. }
  724. }
  725. }
  726. assert (best != NULL);
  727. return best;
  728. }
  729. static void downlist_master_choose_and_send (void)
  730. {
  731. struct downlist_msg *stored_msg;
  732. struct qb_list_head *iter, *tmp_iter;
  733. struct process_info *left_pi;
  734. qb_map_t *group_map;
  735. struct cpg_name cpg_group;
  736. mar_cpg_name_t group;
  737. struct confchg_data{
  738. struct cpg_name cpg_group;
  739. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  740. int left_list_entries;
  741. struct qb_list_head list;
  742. } *pcd;
  743. qb_map_iter_t *miter;
  744. int i, size;
  745. downlist_state = CPG_DOWNLIST_APPLYING;
  746. stored_msg = downlist_master_choose ();
  747. if (!stored_msg) {
  748. log_printf (LOGSYS_LEVEL_DEBUG, "NO chosen downlist");
  749. return;
  750. }
  751. downlist_log("chosen downlist", stored_msg);
  752. group_map = qb_skiplist_create();
  753. /*
  754. * only the cpg groups included in left nodes should receive
  755. * confchg event, so we will collect these cpg groups and
  756. * relative left_lists here.
  757. */
  758. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  759. struct process_info *pi = qb_list_entry(iter, struct process_info, list);
  760. left_pi = NULL;
  761. for (i = 0; i < stored_msg->left_nodes; i++) {
  762. if (pi->nodeid == stored_msg->nodeids[i]) {
  763. left_pi = pi;
  764. break;
  765. }
  766. }
  767. if (left_pi) {
  768. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  769. cpg_group.value[cpg_group.length] = 0;
  770. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  771. if (pcd == NULL) {
  772. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  773. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  774. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  775. }
  776. size = pcd->left_list_entries;
  777. pcd->left_list[size].nodeid = left_pi->nodeid;
  778. pcd->left_list[size].pid = left_pi->pid;
  779. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  780. pcd->left_list_entries++;
  781. qb_list_del (&left_pi->list);
  782. free (left_pi);
  783. }
  784. }
  785. /* send only one confchg event per cpg group */
  786. miter = qb_map_iter_create(group_map);
  787. while (qb_map_iter_next(miter, (void **)&pcd)) {
  788. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  789. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  790. for (i=0; i<pcd->left_list_entries; i++) {
  791. log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
  792. i, cpg_print_group_name(&group),
  793. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  794. pcd->left_list[i].pid);
  795. }
  796. /* send confchg event */
  797. notify_lib_joinlist(&group, NULL,
  798. 0, NULL,
  799. pcd->left_list_entries,
  800. pcd->left_list,
  801. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  802. free(pcd);
  803. }
  804. qb_map_iter_free(miter);
  805. qb_map_destroy(group_map);
  806. }
  807. /*
  808. * Remove processes that might have left the group while we were suspended.
  809. */
  810. static void joinlist_remove_zombie_pi_entries (void)
  811. {
  812. struct qb_list_head *pi_iter, *tmp_iter;
  813. struct qb_list_head *jl_iter;
  814. struct process_info *pi;
  815. struct joinlist_msg *stored_msg;
  816. int found;
  817. qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
  818. pi = qb_list_entry (pi_iter, struct process_info, list);
  819. /*
  820. * Ignore local node
  821. */
  822. if (pi->nodeid == api->totem_nodeid_get()) {
  823. continue ;
  824. }
  825. /*
  826. * Try to find message in joinlist messages
  827. */
  828. found = 0;
  829. qb_list_for_each(jl_iter, &joinlist_messages_head) {
  830. stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
  831. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  832. continue ;
  833. }
  834. if (pi->nodeid == stored_msg->sender_nodeid &&
  835. pi->pid == stored_msg->pid &&
  836. mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
  837. found = 1;
  838. break ;
  839. }
  840. }
  841. if (!found) {
  842. do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
  843. }
  844. }
  845. }
  846. static void joinlist_inform_clients (void)
  847. {
  848. struct joinlist_msg *stored_msg;
  849. struct qb_list_head *iter;
  850. unsigned int i;
  851. i = 0;
  852. qb_list_for_each(iter, &joinlist_messages_head) {
  853. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  854. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  855. i++, cpg_print_group_name(&stored_msg->group_name),
  856. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  857. stored_msg->pid);
  858. /* Ignore our own messages */
  859. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  860. continue ;
  861. }
  862. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  863. CONFCHG_CPG_REASON_NODEUP);
  864. }
  865. joinlist_remove_zombie_pi_entries ();
  866. }
  867. static void downlist_messages_delete (void)
  868. {
  869. struct downlist_msg *stored_msg;
  870. struct qb_list_head *iter, *tmp_iter;
  871. qb_list_for_each_safe(iter, tmp_iter, &downlist_messages_head) {
  872. stored_msg = qb_list_entry(iter, struct downlist_msg, list);
  873. qb_list_del (&stored_msg->list);
  874. free (stored_msg);
  875. }
  876. }
  877. static void joinlist_messages_delete (void)
  878. {
  879. struct joinlist_msg *stored_msg;
  880. struct qb_list_head *iter, *tmp_iter;
  881. qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
  882. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  883. qb_list_del (&stored_msg->list);
  884. free (stored_msg);
  885. }
  886. qb_list_init (&joinlist_messages_head);
  887. }
  888. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  889. {
  890. qb_list_init (&downlist_messages_head);
  891. qb_list_init (&joinlist_messages_head);
  892. api = corosync_api;
  893. return (NULL);
  894. }
  895. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  896. {
  897. struct qb_list_head *iter, *tmp_iter;
  898. struct process_info *pi;
  899. qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
  900. pi = qb_list_entry (iter, struct process_info, list);
  901. qb_list_del (&pi->list);
  902. free (pi);
  903. }
  904. qb_list_del (&cpg_iteration_instance->list);
  905. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  906. }
  907. static void cpg_pd_finalize (struct cpg_pd *cpd)
  908. {
  909. struct qb_list_head *iter, *tmp_iter;
  910. struct cpg_iteration_instance *cpii;
  911. zcb_all_free(cpd);
  912. qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
  913. cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
  914. cpg_iteration_instance_finalize (cpii);
  915. }
  916. qb_list_del (&cpd->list);
  917. }
  918. static int cpg_lib_exit_fn (void *conn)
  919. {
  920. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  921. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  922. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  923. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  924. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  925. }
  926. cpg_pd_finalize (cpd);
  927. api->ipc_refcnt_dec (conn);
  928. return (0);
  929. }
  930. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  931. {
  932. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  933. struct iovec req_exec_cpg_iovec;
  934. int result;
  935. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  936. req_exec_cpg_procjoin.pid = pid;
  937. req_exec_cpg_procjoin.reason = reason;
  938. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  939. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  940. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  941. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  942. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  943. return (result);
  944. }
  945. /* Can byteswap join & leave messages */
  946. static void exec_cpg_procjoin_endian_convert (void *msg)
  947. {
  948. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  949. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  950. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  951. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  952. }
  953. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  954. {
  955. char *msg = msg_v;
  956. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  957. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  958. swab_mar_int32_t (&res->size);
  959. while ((const char*)jle < msg + res->size) {
  960. jle->pid = swab32(jle->pid);
  961. swab_mar_cpg_name_t (&jle->group_name);
  962. jle++;
  963. }
  964. }
  965. static void exec_cpg_downlist_endian_convert_old (void *msg)
  966. {
  967. }
  968. static void exec_cpg_downlist_endian_convert (void *msg)
  969. {
  970. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  971. unsigned int i;
  972. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  973. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  974. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  975. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  976. }
  977. }
  978. static void exec_cpg_mcast_endian_convert (void *msg)
  979. {
  980. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  981. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  982. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  983. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  984. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  985. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  986. }
  987. static void exec_cpg_partial_mcast_endian_convert (void *msg)
  988. {
  989. struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
  990. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  991. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  992. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  993. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  994. req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
  995. req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
  996. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  997. }
  998. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  999. struct qb_list_head *iter;
  1000. qb_list_for_each(iter, &process_info_list_head) {
  1001. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1002. if (pi->pid == pid && pi->nodeid == nodeid &&
  1003. mar_name_compare (&pi->group, group_name) == 0) {
  1004. return pi;
  1005. }
  1006. }
  1007. return NULL;
  1008. }
  1009. static void do_proc_join(
  1010. const mar_cpg_name_t *name,
  1011. uint32_t pid,
  1012. unsigned int nodeid,
  1013. int reason)
  1014. {
  1015. struct process_info *pi;
  1016. struct process_info *pi_entry;
  1017. mar_cpg_address_t notify_info;
  1018. struct qb_list_head *list;
  1019. struct qb_list_head *list_to_add = NULL;
  1020. if (process_info_find (name, pid, nodeid) != NULL) {
  1021. return ;
  1022. }
  1023. pi = malloc (sizeof (struct process_info));
  1024. if (!pi) {
  1025. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1026. return;
  1027. }
  1028. pi->nodeid = nodeid;
  1029. pi->pid = pid;
  1030. memcpy(&pi->group, name, sizeof(*name));
  1031. qb_list_init(&pi->list);
  1032. /*
  1033. * Insert new process in sorted order so synchronization works properly
  1034. */
  1035. list_to_add = &process_info_list_head;
  1036. qb_list_for_each(list, &process_info_list_head) {
  1037. pi_entry = qb_list_entry(list, struct process_info, list);
  1038. if (pi_entry->nodeid > pi->nodeid ||
  1039. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  1040. break;
  1041. }
  1042. list_to_add = list;
  1043. }
  1044. qb_list_add (&pi->list, list_to_add);
  1045. notify_info.pid = pi->pid;
  1046. notify_info.nodeid = nodeid;
  1047. notify_info.reason = reason;
  1048. notify_lib_joinlist(&pi->group, NULL,
  1049. 1, &notify_info,
  1050. 0, NULL,
  1051. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1052. }
  1053. static void do_proc_leave(
  1054. const mar_cpg_name_t *name,
  1055. uint32_t pid,
  1056. unsigned int nodeid,
  1057. int reason)
  1058. {
  1059. struct process_info *pi;
  1060. struct qb_list_head *iter, *tmp_iter;
  1061. mar_cpg_address_t notify_info;
  1062. notify_info.pid = pid;
  1063. notify_info.nodeid = nodeid;
  1064. notify_info.reason = reason;
  1065. notify_lib_joinlist(name, NULL,
  1066. 0, NULL,
  1067. 1, &notify_info,
  1068. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1069. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  1070. pi = qb_list_entry(iter, struct process_info, list);
  1071. if (pi->pid == pid && pi->nodeid == nodeid &&
  1072. mar_name_compare (&pi->group, name)==0) {
  1073. qb_list_del (&pi->list);
  1074. free (pi);
  1075. }
  1076. }
  1077. }
  1078. static void message_handler_req_exec_cpg_downlist_old (
  1079. const void *message,
  1080. unsigned int nodeid)
  1081. {
  1082. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node 0x%x",
  1083. nodeid);
  1084. }
  1085. static void message_handler_req_exec_cpg_downlist(
  1086. const void *message,
  1087. unsigned int nodeid)
  1088. {
  1089. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1090. int i;
  1091. struct qb_list_head *iter;
  1092. struct downlist_msg *stored_msg;
  1093. int found;
  1094. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  1095. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  1096. req_exec_cpg_downlist->left_nodes, downlist_state);
  1097. return;
  1098. }
  1099. stored_msg = malloc (sizeof (struct downlist_msg));
  1100. stored_msg->sender_nodeid = nodeid;
  1101. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  1102. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  1103. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  1104. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  1105. qb_list_init (&stored_msg->list);
  1106. qb_list_add (&stored_msg->list, &downlist_messages_head);
  1107. for (i = 0; i < my_member_list_entries; i++) {
  1108. found = 0;
  1109. qb_list_for_each(iter, &downlist_messages_head) {
  1110. stored_msg = qb_list_entry(iter, struct downlist_msg, list);
  1111. if (my_member_list[i] == stored_msg->sender_nodeid) {
  1112. found = 1;
  1113. }
  1114. }
  1115. if (!found) {
  1116. return;
  1117. }
  1118. }
  1119. downlist_master_choose_and_send ();
  1120. }
  1121. static void message_handler_req_exec_cpg_procjoin (
  1122. const void *message,
  1123. unsigned int nodeid)
  1124. {
  1125. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1126. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node 0x%x (%s) for pid %u",
  1127. nodeid,
  1128. api->totem_ifaces_print(nodeid),
  1129. (unsigned int)req_exec_cpg_procjoin->pid);
  1130. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1131. req_exec_cpg_procjoin->pid, nodeid,
  1132. CONFCHG_CPG_REASON_JOIN);
  1133. }
  1134. static void message_handler_req_exec_cpg_procleave (
  1135. const void *message,
  1136. unsigned int nodeid)
  1137. {
  1138. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1139. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node 0x%x (%s) for pid %u",
  1140. nodeid,
  1141. api->totem_ifaces_print(nodeid),
  1142. (unsigned int)req_exec_cpg_procjoin->pid);
  1143. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1144. req_exec_cpg_procjoin->pid, nodeid,
  1145. req_exec_cpg_procjoin->reason);
  1146. }
  1147. /* Got a proclist from another node */
  1148. static void message_handler_req_exec_cpg_joinlist (
  1149. const void *message_v,
  1150. unsigned int nodeid)
  1151. {
  1152. const char *message = message_v;
  1153. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1154. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1155. struct joinlist_msg *stored_msg;
  1156. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node 0x%x",
  1157. nodeid);
  1158. while ((const char*)jle < message + res->size) {
  1159. stored_msg = malloc (sizeof (struct joinlist_msg));
  1160. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1161. stored_msg->sender_nodeid = nodeid;
  1162. stored_msg->pid = jle->pid;
  1163. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1164. qb_list_init (&stored_msg->list);
  1165. qb_list_add (&stored_msg->list, &joinlist_messages_head);
  1166. jle++;
  1167. }
  1168. }
  1169. static void message_handler_req_exec_cpg_mcast (
  1170. const void *message,
  1171. unsigned int nodeid)
  1172. {
  1173. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1174. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1175. int msglen = req_exec_cpg_mcast->msglen;
  1176. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1177. struct cpg_pd *cpd;
  1178. struct iovec iovec[2];
  1179. int known_node = 0;
  1180. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1181. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1182. res_lib_cpg_mcast.msglen = msglen;
  1183. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1184. res_lib_cpg_mcast.nodeid = nodeid;
  1185. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1186. sizeof(mar_cpg_name_t));
  1187. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1188. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1189. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1190. iovec[1].iov_len = msglen;
  1191. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1192. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1193. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1194. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1195. if (!known_node) {
  1196. /* Try to find, if we know the node */
  1197. qb_list_for_each(pi_iter, &process_info_list_head) {
  1198. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1199. if (pi->nodeid == nodeid &&
  1200. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1201. known_node = 1;
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. if (!known_node) {
  1207. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1208. return ;
  1209. }
  1210. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1211. }
  1212. }
  1213. }
  1214. static void message_handler_req_exec_cpg_partial_mcast (
  1215. const void *message,
  1216. unsigned int nodeid)
  1217. {
  1218. const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
  1219. struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
  1220. int msglen = req_exec_cpg_mcast->fraglen;
  1221. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1222. struct cpg_pd *cpd;
  1223. struct iovec iovec[2];
  1224. int known_node = 0;
  1225. log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node %d, size = %d bytes\n", nodeid, msglen);
  1226. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
  1227. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1228. res_lib_cpg_mcast.fraglen = msglen;
  1229. res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
  1230. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1231. res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
  1232. res_lib_cpg_mcast.nodeid = nodeid;
  1233. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1234. sizeof(mar_cpg_name_t));
  1235. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1236. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1237. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1238. iovec[1].iov_len = msglen;
  1239. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1240. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1241. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1242. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1243. if (!known_node) {
  1244. /* Try to find, if we know the node */
  1245. qb_list_for_each(pi_iter, &process_info_list_head) {
  1246. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1247. if (pi->nodeid == nodeid &&
  1248. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1249. known_node = 1;
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. if (!known_node) {
  1255. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1256. return ;
  1257. }
  1258. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1259. }
  1260. }
  1261. }
  1262. static int cpg_exec_send_downlist(void)
  1263. {
  1264. struct iovec iov;
  1265. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1266. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1267. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1268. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1269. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1270. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1271. }
  1272. static int cpg_exec_send_joinlist(void)
  1273. {
  1274. int count = 0;
  1275. struct qb_list_head *iter;
  1276. struct qb_ipc_response_header *res;
  1277. char *buf;
  1278. struct join_list_entry *jle;
  1279. struct iovec req_exec_cpg_iovec;
  1280. qb_list_for_each(iter, &process_info_list_head) {
  1281. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1282. if (pi->nodeid == api->totem_nodeid_get ()) {
  1283. count++;
  1284. }
  1285. }
  1286. /* Nothing to send */
  1287. if (!count)
  1288. return 0;
  1289. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1290. if (!buf) {
  1291. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1292. return -1;
  1293. }
  1294. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1295. res = (struct qb_ipc_response_header *)buf;
  1296. qb_list_for_each(iter, &process_info_list_head) {
  1297. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1298. if (pi->nodeid == api->totem_nodeid_get ()) {
  1299. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1300. jle->pid = pi->pid;
  1301. jle++;
  1302. }
  1303. }
  1304. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1305. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1306. req_exec_cpg_iovec.iov_base = buf;
  1307. req_exec_cpg_iovec.iov_len = res->size;
  1308. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1309. }
  1310. static int cpg_lib_init_fn (void *conn)
  1311. {
  1312. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1313. memset (cpd, 0, sizeof(struct cpg_pd));
  1314. cpd->conn = conn;
  1315. qb_list_add (&cpd->list, &cpg_pd_list_head);
  1316. qb_list_init (&cpd->iteration_instance_list_head);
  1317. qb_list_init (&cpd->zcb_mapped_list_head);
  1318. api->ipc_refcnt_inc (conn);
  1319. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1320. return (0);
  1321. }
  1322. /* Join message from the library */
  1323. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1324. {
  1325. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1326. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1327. struct res_lib_cpg_join res_lib_cpg_join;
  1328. cs_error_t error = CS_OK;
  1329. struct qb_list_head *iter;
  1330. /* Test, if we don't have same pid and group name joined */
  1331. qb_list_for_each(iter, &cpg_pd_list_head) {
  1332. struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
  1333. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1334. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1335. /* We have same pid and group name joined -> return error */
  1336. error = CS_ERR_EXIST;
  1337. goto response_send;
  1338. }
  1339. }
  1340. /*
  1341. * Same check must be done in process info list, because there may be not yet delivered
  1342. * leave of client.
  1343. */
  1344. qb_list_for_each(iter, &process_info_list_head) {
  1345. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1346. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1347. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1348. /* We have same pid and group name joined -> return error */
  1349. error = CS_ERR_TRY_AGAIN;
  1350. goto response_send;
  1351. }
  1352. }
  1353. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1354. error = CS_ERR_NAME_TOO_LONG;
  1355. goto response_send;
  1356. }
  1357. switch (cpd->cpd_state) {
  1358. case CPD_STATE_UNJOINED:
  1359. error = CS_OK;
  1360. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1361. cpd->pid = req_lib_cpg_join->pid;
  1362. cpd->flags = req_lib_cpg_join->flags;
  1363. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1364. sizeof (cpd->group_name));
  1365. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1366. &req_lib_cpg_join->group_name,
  1367. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1368. break;
  1369. case CPD_STATE_LEAVE_STARTED:
  1370. error = CS_ERR_BUSY;
  1371. break;
  1372. case CPD_STATE_JOIN_STARTED:
  1373. error = CS_ERR_EXIST;
  1374. break;
  1375. case CPD_STATE_JOIN_COMPLETED:
  1376. error = CS_ERR_EXIST;
  1377. break;
  1378. }
  1379. response_send:
  1380. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1381. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1382. res_lib_cpg_join.header.error = error;
  1383. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1384. }
  1385. /* Leave message from the library */
  1386. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1387. {
  1388. struct res_lib_cpg_leave res_lib_cpg_leave;
  1389. cs_error_t error = CS_OK;
  1390. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1391. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1392. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1393. switch (cpd->cpd_state) {
  1394. case CPD_STATE_UNJOINED:
  1395. error = CS_ERR_NOT_EXIST;
  1396. break;
  1397. case CPD_STATE_LEAVE_STARTED:
  1398. error = CS_ERR_NOT_EXIST;
  1399. break;
  1400. case CPD_STATE_JOIN_STARTED:
  1401. error = CS_ERR_BUSY;
  1402. break;
  1403. case CPD_STATE_JOIN_COMPLETED:
  1404. error = CS_OK;
  1405. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1406. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1407. &req_lib_cpg_leave->group_name,
  1408. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1409. CONFCHG_CPG_REASON_LEAVE);
  1410. break;
  1411. }
  1412. /* send return */
  1413. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1414. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1415. res_lib_cpg_leave.header.error = error;
  1416. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1417. }
  1418. /* Finalize message from library */
  1419. static void message_handler_req_lib_cpg_finalize (
  1420. void *conn,
  1421. const void *message)
  1422. {
  1423. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1424. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1425. cs_error_t error = CS_OK;
  1426. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1427. /*
  1428. * We will just remove cpd from list. After this call, connection will be
  1429. * closed on lib side, and cpg_lib_exit_fn will be called
  1430. */
  1431. qb_list_del (&cpd->list);
  1432. qb_list_init (&cpd->list);
  1433. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1434. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1435. res_lib_cpg_finalize.header.error = error;
  1436. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1437. sizeof (res_lib_cpg_finalize));
  1438. }
  1439. static int
  1440. memory_map (
  1441. const char *path,
  1442. size_t bytes,
  1443. void **buf)
  1444. {
  1445. int32_t fd;
  1446. void *addr;
  1447. int32_t res;
  1448. fd = open (path, O_RDWR, 0600);
  1449. unlink (path);
  1450. if (fd == -1) {
  1451. return (-1);
  1452. }
  1453. res = ftruncate (fd, bytes);
  1454. if (res == -1) {
  1455. goto error_close_unlink;
  1456. }
  1457. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  1458. MAP_SHARED, fd, 0);
  1459. if (addr == MAP_FAILED) {
  1460. goto error_close_unlink;
  1461. }
  1462. #ifdef MADV_NOSYNC
  1463. madvise(addr, bytes, MADV_NOSYNC);
  1464. #endif
  1465. res = close (fd);
  1466. if (res) {
  1467. munmap (addr, bytes);
  1468. return (-1);
  1469. }
  1470. *buf = addr;
  1471. return (0);
  1472. error_close_unlink:
  1473. close (fd);
  1474. unlink(path);
  1475. return -1;
  1476. }
  1477. static inline int zcb_alloc (
  1478. struct cpg_pd *cpd,
  1479. const char *path_to_file,
  1480. size_t size,
  1481. void **addr)
  1482. {
  1483. struct zcb_mapped *zcb_mapped;
  1484. unsigned int res;
  1485. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1486. if (zcb_mapped == NULL) {
  1487. return (-1);
  1488. }
  1489. res = memory_map (
  1490. path_to_file,
  1491. size,
  1492. addr);
  1493. if (res == -1) {
  1494. free (zcb_mapped);
  1495. return (-1);
  1496. }
  1497. qb_list_init (&zcb_mapped->list);
  1498. zcb_mapped->addr = *addr;
  1499. zcb_mapped->size = size;
  1500. qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1501. return (0);
  1502. }
  1503. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1504. {
  1505. unsigned int res;
  1506. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1507. qb_list_del (&zcb_mapped->list);
  1508. free (zcb_mapped);
  1509. return (res);
  1510. }
  1511. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1512. {
  1513. struct qb_list_head *list, *tmp_iter;
  1514. struct zcb_mapped *zcb_mapped;
  1515. unsigned int res = 0;
  1516. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1517. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1518. if (zcb_mapped->addr == addr) {
  1519. res = zcb_free (zcb_mapped);
  1520. break;
  1521. }
  1522. }
  1523. return (res);
  1524. }
  1525. static inline int zcb_all_free (
  1526. struct cpg_pd *cpd)
  1527. {
  1528. struct qb_list_head *list, *tmp_iter;
  1529. struct zcb_mapped *zcb_mapped;
  1530. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1531. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1532. zcb_free (zcb_mapped);
  1533. }
  1534. return (0);
  1535. }
  1536. union u {
  1537. uint64_t server_addr;
  1538. void *server_ptr;
  1539. };
  1540. static uint64_t void2serveraddr (void *server_ptr)
  1541. {
  1542. union u u;
  1543. u.server_ptr = server_ptr;
  1544. return (u.server_addr);
  1545. }
  1546. static void *serveraddr2void (uint64_t server_addr)
  1547. {
  1548. union u u;
  1549. u.server_addr = server_addr;
  1550. return (u.server_ptr);
  1551. };
  1552. static void message_handler_req_lib_cpg_zc_alloc (
  1553. void *conn,
  1554. const void *message)
  1555. {
  1556. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1557. struct qb_ipc_response_header res_header;
  1558. void *addr = NULL;
  1559. struct coroipcs_zc_header *zc_header;
  1560. unsigned int res;
  1561. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1562. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1563. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1564. &addr);
  1565. assert(res == 0);
  1566. zc_header = (struct coroipcs_zc_header *)addr;
  1567. zc_header->server_address = void2serveraddr(addr);
  1568. res_header.size = sizeof (struct qb_ipc_response_header);
  1569. res_header.id = 0;
  1570. api->ipc_response_send (conn,
  1571. &res_header,
  1572. res_header.size);
  1573. }
  1574. static void message_handler_req_lib_cpg_zc_free (
  1575. void *conn,
  1576. const void *message)
  1577. {
  1578. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1579. struct qb_ipc_response_header res_header;
  1580. void *addr = NULL;
  1581. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1582. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1583. addr = serveraddr2void (hdr->server_address);
  1584. zcb_by_addr_free (cpd, addr);
  1585. res_header.size = sizeof (struct qb_ipc_response_header);
  1586. res_header.id = 0;
  1587. api->ipc_response_send (
  1588. conn, &res_header,
  1589. res_header.size);
  1590. }
  1591. /* Fragmented mcast message from the library */
  1592. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1593. {
  1594. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1595. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1596. mar_cpg_name_t group_name = cpd->group_name;
  1597. struct iovec req_exec_cpg_iovec[2];
  1598. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1599. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1600. int msglen = req_lib_cpg_mcast->fraglen;
  1601. int result;
  1602. cs_error_t error = CS_ERR_NOT_EXIST;
  1603. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1604. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1605. switch (cpd->cpd_state) {
  1606. case CPD_STATE_UNJOINED:
  1607. error = CS_ERR_NOT_EXIST;
  1608. break;
  1609. case CPD_STATE_LEAVE_STARTED:
  1610. error = CS_ERR_NOT_EXIST;
  1611. break;
  1612. case CPD_STATE_JOIN_STARTED:
  1613. error = CS_OK;
  1614. break;
  1615. case CPD_STATE_JOIN_COMPLETED:
  1616. error = CS_OK;
  1617. break;
  1618. }
  1619. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1620. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1621. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1622. cpd->initial_transition_counter = cpd->transition_counter;
  1623. }
  1624. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1625. error = CS_ERR_INTERRUPT;
  1626. }
  1627. if (error == CS_OK) {
  1628. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1629. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1630. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1631. req_exec_cpg_mcast.pid = cpd->pid;
  1632. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1633. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1634. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1635. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1636. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1637. sizeof(mar_cpg_name_t));
  1638. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1639. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1640. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1641. req_exec_cpg_iovec[1].iov_len = msglen;
  1642. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1643. assert(result == 0);
  1644. } else {
  1645. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1646. conn, group_name.value, cpd->cpd_state, error);
  1647. }
  1648. res_lib_cpg_partial_send.header.error = error;
  1649. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1650. sizeof (res_lib_cpg_partial_send));
  1651. }
  1652. /* Mcast message from the library */
  1653. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1654. {
  1655. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1656. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1657. mar_cpg_name_t group_name = cpd->group_name;
  1658. struct iovec req_exec_cpg_iovec[2];
  1659. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1660. int msglen = req_lib_cpg_mcast->msglen;
  1661. int result;
  1662. cs_error_t error = CS_ERR_NOT_EXIST;
  1663. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1664. switch (cpd->cpd_state) {
  1665. case CPD_STATE_UNJOINED:
  1666. error = CS_ERR_NOT_EXIST;
  1667. break;
  1668. case CPD_STATE_LEAVE_STARTED:
  1669. error = CS_ERR_NOT_EXIST;
  1670. break;
  1671. case CPD_STATE_JOIN_STARTED:
  1672. error = CS_OK;
  1673. break;
  1674. case CPD_STATE_JOIN_COMPLETED:
  1675. error = CS_OK;
  1676. break;
  1677. }
  1678. if (error == CS_OK) {
  1679. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1680. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1681. MESSAGE_REQ_EXEC_CPG_MCAST);
  1682. req_exec_cpg_mcast.pid = cpd->pid;
  1683. req_exec_cpg_mcast.msglen = msglen;
  1684. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1685. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1686. sizeof(mar_cpg_name_t));
  1687. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1688. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1689. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1690. req_exec_cpg_iovec[1].iov_len = msglen;
  1691. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1692. assert(result == 0);
  1693. } else {
  1694. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1695. conn, group_name.value, cpd->cpd_state, error);
  1696. }
  1697. }
  1698. static void message_handler_req_lib_cpg_zc_execute (
  1699. void *conn,
  1700. const void *message)
  1701. {
  1702. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1703. struct qb_ipc_request_header *header;
  1704. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1705. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1706. struct iovec req_exec_cpg_iovec[2];
  1707. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1708. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1709. int result;
  1710. cs_error_t error = CS_ERR_NOT_EXIST;
  1711. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1712. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1713. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1714. switch (cpd->cpd_state) {
  1715. case CPD_STATE_UNJOINED:
  1716. error = CS_ERR_NOT_EXIST;
  1717. break;
  1718. case CPD_STATE_LEAVE_STARTED:
  1719. error = CS_ERR_NOT_EXIST;
  1720. break;
  1721. case CPD_STATE_JOIN_STARTED:
  1722. error = CS_OK;
  1723. break;
  1724. case CPD_STATE_JOIN_COMPLETED:
  1725. error = CS_OK;
  1726. break;
  1727. }
  1728. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1729. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1730. if (error == CS_OK) {
  1731. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1732. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1733. MESSAGE_REQ_EXEC_CPG_MCAST);
  1734. req_exec_cpg_mcast.pid = cpd->pid;
  1735. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1736. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1737. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1738. sizeof(mar_cpg_name_t));
  1739. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1740. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1741. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1742. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1743. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1744. if (result == 0) {
  1745. res_lib_cpg_mcast.header.error = CS_OK;
  1746. } else {
  1747. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1748. }
  1749. } else {
  1750. res_lib_cpg_mcast.header.error = error;
  1751. }
  1752. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1753. sizeof (res_lib_cpg_mcast));
  1754. }
  1755. static void message_handler_req_lib_cpg_membership (void *conn,
  1756. const void *message)
  1757. {
  1758. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1759. (struct req_lib_cpg_membership_get *)message;
  1760. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1761. struct qb_list_head *iter;
  1762. int member_count = 0;
  1763. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1764. res_lib_cpg_membership_get.header.error = CS_OK;
  1765. res_lib_cpg_membership_get.header.size =
  1766. sizeof (struct res_lib_cpg_membership_get);
  1767. qb_list_for_each(iter, &process_info_list_head) {
  1768. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1769. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1770. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1771. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1772. member_count += 1;
  1773. }
  1774. }
  1775. res_lib_cpg_membership_get.member_count = member_count;
  1776. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1777. sizeof (res_lib_cpg_membership_get));
  1778. }
  1779. static void message_handler_req_lib_cpg_local_get (void *conn,
  1780. const void *message)
  1781. {
  1782. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1783. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1784. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1785. res_lib_cpg_local_get.header.error = CS_OK;
  1786. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1787. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1788. sizeof (res_lib_cpg_local_get));
  1789. }
  1790. static void message_handler_req_lib_cpg_iteration_initialize (
  1791. void *conn,
  1792. const void *message)
  1793. {
  1794. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1795. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1796. hdb_handle_t cpg_iteration_handle = 0;
  1797. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1798. struct qb_list_head *iter, *iter2;
  1799. struct cpg_iteration_instance *cpg_iteration_instance;
  1800. cs_error_t error = CS_OK;
  1801. int res;
  1802. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1803. /* Because between calling this function and *next can be some operations which will
  1804. * change list, we must do full copy.
  1805. */
  1806. /*
  1807. * Create new iteration instance
  1808. */
  1809. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1810. &cpg_iteration_handle);
  1811. if (res != 0) {
  1812. error = CS_ERR_NO_MEMORY;
  1813. goto response_send;
  1814. }
  1815. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1816. if (res != 0) {
  1817. error = CS_ERR_BAD_HANDLE;
  1818. goto error_destroy;
  1819. }
  1820. qb_list_init (&cpg_iteration_instance->items_list_head);
  1821. cpg_iteration_instance->handle = cpg_iteration_handle;
  1822. /*
  1823. * Create copy of process_info list "grouped by" group name
  1824. */
  1825. qb_list_for_each(iter, &process_info_list_head) {
  1826. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1827. struct process_info *new_pi;
  1828. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1829. /*
  1830. * Try to find processed group name in our list new list
  1831. */
  1832. int found = 0;
  1833. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1834. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1835. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1836. found = 1;
  1837. break;
  1838. }
  1839. }
  1840. if (found) {
  1841. /*
  1842. * We have this name in list -> don't add
  1843. */
  1844. continue ;
  1845. }
  1846. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1847. /*
  1848. * Test pi group name with request
  1849. */
  1850. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1851. /*
  1852. * Not same -> don't add
  1853. */
  1854. continue ;
  1855. }
  1856. new_pi = malloc (sizeof (struct process_info));
  1857. if (!new_pi) {
  1858. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1859. error = CS_ERR_NO_MEMORY;
  1860. goto error_put_destroy;
  1861. }
  1862. memcpy (new_pi, pi, sizeof (struct process_info));
  1863. qb_list_init (&new_pi->list);
  1864. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1865. /*
  1866. * pid and nodeid -> undefined
  1867. */
  1868. new_pi->pid = new_pi->nodeid = 0;
  1869. }
  1870. /*
  1871. * We will return list "grouped" by "group name", so try to find right place to add
  1872. */
  1873. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1874. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1875. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1876. break;
  1877. }
  1878. }
  1879. qb_list_add (&new_pi->list, iter2);
  1880. }
  1881. /*
  1882. * Now we have a full "grouped by" copy of process_info list
  1883. */
  1884. /*
  1885. * Add instance to current cpd list
  1886. */
  1887. qb_list_init (&cpg_iteration_instance->list);
  1888. qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1889. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1890. error_put_destroy:
  1891. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1892. error_destroy:
  1893. if (error != CS_OK) {
  1894. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1895. }
  1896. response_send:
  1897. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1898. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1899. res_lib_cpg_iterationinitialize.header.error = error;
  1900. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1901. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1902. sizeof (res_lib_cpg_iterationinitialize));
  1903. }
  1904. static void message_handler_req_lib_cpg_iteration_next (
  1905. void *conn,
  1906. const void *message)
  1907. {
  1908. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1909. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1910. struct cpg_iteration_instance *cpg_iteration_instance;
  1911. cs_error_t error = CS_OK;
  1912. int res;
  1913. struct process_info *pi;
  1914. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1915. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1916. req_lib_cpg_iterationnext->iteration_handle,
  1917. (void *)&cpg_iteration_instance);
  1918. if (res != 0) {
  1919. error = CS_ERR_LIBRARY;
  1920. goto error_exit;
  1921. }
  1922. assert (cpg_iteration_instance);
  1923. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1924. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1925. error = CS_ERR_NO_SECTIONS;
  1926. goto error_put;
  1927. }
  1928. pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1929. /*
  1930. * Copy iteration data
  1931. */
  1932. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1933. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1934. memcpy (&res_lib_cpg_iterationnext.description.group,
  1935. &pi->group,
  1936. sizeof (mar_cpg_name_t));
  1937. error_put:
  1938. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1939. error_exit:
  1940. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1941. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1942. res_lib_cpg_iterationnext.header.error = error;
  1943. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1944. sizeof (res_lib_cpg_iterationnext));
  1945. }
  1946. static void message_handler_req_lib_cpg_iteration_finalize (
  1947. void *conn,
  1948. const void *message)
  1949. {
  1950. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1951. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1952. struct cpg_iteration_instance *cpg_iteration_instance;
  1953. cs_error_t error = CS_OK;
  1954. int res;
  1955. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1956. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1957. req_lib_cpg_iterationfinalize->iteration_handle,
  1958. (void *)&cpg_iteration_instance);
  1959. if (res != 0) {
  1960. error = CS_ERR_LIBRARY;
  1961. goto error_exit;
  1962. }
  1963. assert (cpg_iteration_instance);
  1964. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1965. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1966. error_exit:
  1967. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1968. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1969. res_lib_cpg_iterationfinalize.header.error = error;
  1970. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1971. sizeof (res_lib_cpg_iterationfinalize));
  1972. }