cpg.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  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. return (-1);
  1496. }
  1497. *buf = addr;
  1498. return (0);
  1499. error_close_unlink:
  1500. close (fd);
  1501. unlink(path);
  1502. return -1;
  1503. }
  1504. static inline int zcb_alloc (
  1505. struct cpg_pd *cpd,
  1506. const char *path_to_file,
  1507. size_t size,
  1508. void **addr)
  1509. {
  1510. struct zcb_mapped *zcb_mapped;
  1511. unsigned int res;
  1512. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1513. if (zcb_mapped == NULL) {
  1514. return (-1);
  1515. }
  1516. res = memory_map (
  1517. path_to_file,
  1518. size,
  1519. addr);
  1520. if (res == -1) {
  1521. free (zcb_mapped);
  1522. return (-1);
  1523. }
  1524. list_init (&zcb_mapped->list);
  1525. zcb_mapped->addr = *addr;
  1526. zcb_mapped->size = size;
  1527. list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1528. return (0);
  1529. }
  1530. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1531. {
  1532. unsigned int res;
  1533. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1534. list_del (&zcb_mapped->list);
  1535. free (zcb_mapped);
  1536. return (res);
  1537. }
  1538. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1539. {
  1540. struct list_head *list;
  1541. struct zcb_mapped *zcb_mapped;
  1542. unsigned int res = 0;
  1543. for (list = cpd->zcb_mapped_list_head.next;
  1544. list != &cpd->zcb_mapped_list_head; list = list->next) {
  1545. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1546. if (zcb_mapped->addr == addr) {
  1547. res = zcb_free (zcb_mapped);
  1548. break;
  1549. }
  1550. }
  1551. return (res);
  1552. }
  1553. static inline int zcb_all_free (
  1554. struct cpg_pd *cpd)
  1555. {
  1556. struct list_head *list;
  1557. struct zcb_mapped *zcb_mapped;
  1558. for (list = cpd->zcb_mapped_list_head.next;
  1559. list != &cpd->zcb_mapped_list_head;) {
  1560. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  1561. list = list->next;
  1562. zcb_free (zcb_mapped);
  1563. }
  1564. return (0);
  1565. }
  1566. union u {
  1567. uint64_t server_addr;
  1568. void *server_ptr;
  1569. };
  1570. static uint64_t void2serveraddr (void *server_ptr)
  1571. {
  1572. union u u;
  1573. u.server_ptr = server_ptr;
  1574. return (u.server_addr);
  1575. }
  1576. static void *serveraddr2void (uint64_t server_addr)
  1577. {
  1578. union u u;
  1579. u.server_addr = server_addr;
  1580. return (u.server_ptr);
  1581. };
  1582. static void message_handler_req_lib_cpg_zc_alloc (
  1583. void *conn,
  1584. const void *message)
  1585. {
  1586. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1587. struct qb_ipc_response_header res_header;
  1588. void *addr = NULL;
  1589. struct coroipcs_zc_header *zc_header;
  1590. unsigned int res;
  1591. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1592. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1593. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1594. &addr);
  1595. assert(res == 0);
  1596. zc_header = (struct coroipcs_zc_header *)addr;
  1597. zc_header->server_address = void2serveraddr(addr);
  1598. res_header.size = sizeof (struct qb_ipc_response_header);
  1599. res_header.id = 0;
  1600. api->ipc_response_send (conn,
  1601. &res_header,
  1602. res_header.size);
  1603. }
  1604. static void message_handler_req_lib_cpg_zc_free (
  1605. void *conn,
  1606. const void *message)
  1607. {
  1608. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1609. struct qb_ipc_response_header res_header;
  1610. void *addr = NULL;
  1611. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1612. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1613. addr = serveraddr2void (hdr->server_address);
  1614. zcb_by_addr_free (cpd, addr);
  1615. res_header.size = sizeof (struct qb_ipc_response_header);
  1616. res_header.id = 0;
  1617. api->ipc_response_send (
  1618. conn, &res_header,
  1619. res_header.size);
  1620. }
  1621. /* Fragmented mcast message from the library */
  1622. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1623. {
  1624. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1625. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1626. mar_cpg_name_t group_name = cpd->group_name;
  1627. struct iovec req_exec_cpg_iovec[2];
  1628. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1629. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1630. int msglen = req_lib_cpg_mcast->fraglen;
  1631. int result;
  1632. cs_error_t error = CS_ERR_NOT_EXIST;
  1633. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1634. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1635. switch (cpd->cpd_state) {
  1636. case CPD_STATE_UNJOINED:
  1637. error = CS_ERR_NOT_EXIST;
  1638. break;
  1639. case CPD_STATE_LEAVE_STARTED:
  1640. error = CS_ERR_NOT_EXIST;
  1641. break;
  1642. case CPD_STATE_JOIN_STARTED:
  1643. error = CS_OK;
  1644. break;
  1645. case CPD_STATE_JOIN_COMPLETED:
  1646. error = CS_OK;
  1647. break;
  1648. }
  1649. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1650. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1651. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1652. cpd->initial_transition_counter = cpd->transition_counter;
  1653. }
  1654. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1655. error = CS_ERR_INTERRUPT;
  1656. }
  1657. if (error == CS_OK) {
  1658. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1659. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1660. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1661. req_exec_cpg_mcast.pid = cpd->pid;
  1662. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1663. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1664. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1665. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1666. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1667. sizeof(mar_cpg_name_t));
  1668. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1669. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1670. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1671. req_exec_cpg_iovec[1].iov_len = msglen;
  1672. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1673. assert(result == 0);
  1674. } else {
  1675. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1676. conn, group_name.value, cpd->cpd_state, error);
  1677. }
  1678. res_lib_cpg_partial_send.header.error = error;
  1679. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1680. sizeof (res_lib_cpg_partial_send));
  1681. }
  1682. /* Mcast message from the library */
  1683. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1684. {
  1685. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1686. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1687. mar_cpg_name_t group_name = cpd->group_name;
  1688. struct iovec req_exec_cpg_iovec[2];
  1689. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1690. int msglen = req_lib_cpg_mcast->msglen;
  1691. int result;
  1692. cs_error_t error = CS_ERR_NOT_EXIST;
  1693. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1694. switch (cpd->cpd_state) {
  1695. case CPD_STATE_UNJOINED:
  1696. error = CS_ERR_NOT_EXIST;
  1697. break;
  1698. case CPD_STATE_LEAVE_STARTED:
  1699. error = CS_ERR_NOT_EXIST;
  1700. break;
  1701. case CPD_STATE_JOIN_STARTED:
  1702. error = CS_OK;
  1703. break;
  1704. case CPD_STATE_JOIN_COMPLETED:
  1705. error = CS_OK;
  1706. break;
  1707. }
  1708. if (error == CS_OK) {
  1709. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1710. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1711. MESSAGE_REQ_EXEC_CPG_MCAST);
  1712. req_exec_cpg_mcast.pid = cpd->pid;
  1713. req_exec_cpg_mcast.msglen = msglen;
  1714. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1715. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1716. sizeof(mar_cpg_name_t));
  1717. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1718. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1719. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1720. req_exec_cpg_iovec[1].iov_len = msglen;
  1721. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1722. assert(result == 0);
  1723. } else {
  1724. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1725. conn, group_name.value, cpd->cpd_state, error);
  1726. }
  1727. }
  1728. static void message_handler_req_lib_cpg_zc_execute (
  1729. void *conn,
  1730. const void *message)
  1731. {
  1732. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1733. struct qb_ipc_request_header *header;
  1734. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1735. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1736. struct iovec req_exec_cpg_iovec[2];
  1737. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1738. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1739. int result;
  1740. cs_error_t error = CS_ERR_NOT_EXIST;
  1741. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1742. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1743. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1744. switch (cpd->cpd_state) {
  1745. case CPD_STATE_UNJOINED:
  1746. error = CS_ERR_NOT_EXIST;
  1747. break;
  1748. case CPD_STATE_LEAVE_STARTED:
  1749. error = CS_ERR_NOT_EXIST;
  1750. break;
  1751. case CPD_STATE_JOIN_STARTED:
  1752. error = CS_OK;
  1753. break;
  1754. case CPD_STATE_JOIN_COMPLETED:
  1755. error = CS_OK;
  1756. break;
  1757. }
  1758. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1759. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1760. if (error == CS_OK) {
  1761. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1762. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1763. MESSAGE_REQ_EXEC_CPG_MCAST);
  1764. req_exec_cpg_mcast.pid = cpd->pid;
  1765. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1766. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1767. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1768. sizeof(mar_cpg_name_t));
  1769. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1770. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1771. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1772. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1773. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1774. if (result == 0) {
  1775. res_lib_cpg_mcast.header.error = CS_OK;
  1776. } else {
  1777. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1778. }
  1779. } else {
  1780. res_lib_cpg_mcast.header.error = error;
  1781. }
  1782. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1783. sizeof (res_lib_cpg_mcast));
  1784. }
  1785. static void message_handler_req_lib_cpg_membership (void *conn,
  1786. const void *message)
  1787. {
  1788. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1789. (struct req_lib_cpg_membership_get *)message;
  1790. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1791. struct list_head *iter;
  1792. int member_count = 0;
  1793. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1794. res_lib_cpg_membership_get.header.error = CS_OK;
  1795. res_lib_cpg_membership_get.header.size =
  1796. sizeof (struct res_lib_cpg_membership_get);
  1797. for (iter = process_info_list_head.next;
  1798. iter != &process_info_list_head; iter = iter->next) {
  1799. struct process_info *pi = list_entry (iter, struct process_info, list);
  1800. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1801. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1802. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1803. member_count += 1;
  1804. }
  1805. }
  1806. res_lib_cpg_membership_get.member_count = member_count;
  1807. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1808. sizeof (res_lib_cpg_membership_get));
  1809. }
  1810. static void message_handler_req_lib_cpg_local_get (void *conn,
  1811. const void *message)
  1812. {
  1813. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1814. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1815. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1816. res_lib_cpg_local_get.header.error = CS_OK;
  1817. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1818. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1819. sizeof (res_lib_cpg_local_get));
  1820. }
  1821. static void message_handler_req_lib_cpg_iteration_initialize (
  1822. void *conn,
  1823. const void *message)
  1824. {
  1825. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1826. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1827. hdb_handle_t cpg_iteration_handle = 0;
  1828. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1829. struct list_head *iter, *iter2;
  1830. struct cpg_iteration_instance *cpg_iteration_instance;
  1831. cs_error_t error = CS_OK;
  1832. int res;
  1833. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1834. /* Because between calling this function and *next can be some operations which will
  1835. * change list, we must do full copy.
  1836. */
  1837. /*
  1838. * Create new iteration instance
  1839. */
  1840. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1841. &cpg_iteration_handle);
  1842. if (res != 0) {
  1843. error = CS_ERR_NO_MEMORY;
  1844. goto response_send;
  1845. }
  1846. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1847. if (res != 0) {
  1848. error = CS_ERR_BAD_HANDLE;
  1849. goto error_destroy;
  1850. }
  1851. list_init (&cpg_iteration_instance->items_list_head);
  1852. cpg_iteration_instance->handle = cpg_iteration_handle;
  1853. /*
  1854. * Create copy of process_info list "grouped by" group name
  1855. */
  1856. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1857. struct process_info *pi = list_entry (iter, struct process_info, list);
  1858. struct process_info *new_pi;
  1859. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1860. /*
  1861. * Try to find processed group name in our list new list
  1862. */
  1863. int found = 0;
  1864. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1865. iter2 != &cpg_iteration_instance->items_list_head;
  1866. iter2 = iter2->next) {
  1867. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1868. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1869. found = 1;
  1870. break;
  1871. }
  1872. }
  1873. if (found) {
  1874. /*
  1875. * We have this name in list -> don't add
  1876. */
  1877. continue ;
  1878. }
  1879. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1880. /*
  1881. * Test pi group name with request
  1882. */
  1883. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1884. /*
  1885. * Not same -> don't add
  1886. */
  1887. continue ;
  1888. }
  1889. new_pi = malloc (sizeof (struct process_info));
  1890. if (!new_pi) {
  1891. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1892. error = CS_ERR_NO_MEMORY;
  1893. goto error_put_destroy;
  1894. }
  1895. memcpy (new_pi, pi, sizeof (struct process_info));
  1896. list_init (&new_pi->list);
  1897. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1898. /*
  1899. * pid and nodeid -> undefined
  1900. */
  1901. new_pi->pid = new_pi->nodeid = 0;
  1902. }
  1903. /*
  1904. * We will return list "grouped" by "group name", so try to find right place to add
  1905. */
  1906. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1907. iter2 != &cpg_iteration_instance->items_list_head;
  1908. iter2 = iter2->next) {
  1909. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1910. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1911. break;
  1912. }
  1913. }
  1914. list_add (&new_pi->list, iter2);
  1915. }
  1916. /*
  1917. * Now we have a full "grouped by" copy of process_info list
  1918. */
  1919. /*
  1920. * Add instance to current cpd list
  1921. */
  1922. list_init (&cpg_iteration_instance->list);
  1923. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1924. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1925. error_put_destroy:
  1926. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1927. error_destroy:
  1928. if (error != CS_OK) {
  1929. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1930. }
  1931. response_send:
  1932. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1933. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1934. res_lib_cpg_iterationinitialize.header.error = error;
  1935. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1936. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1937. sizeof (res_lib_cpg_iterationinitialize));
  1938. }
  1939. static void message_handler_req_lib_cpg_iteration_next (
  1940. void *conn,
  1941. const void *message)
  1942. {
  1943. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1944. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1945. struct cpg_iteration_instance *cpg_iteration_instance;
  1946. cs_error_t error = CS_OK;
  1947. int res;
  1948. struct process_info *pi;
  1949. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1950. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1951. req_lib_cpg_iterationnext->iteration_handle,
  1952. (void *)&cpg_iteration_instance);
  1953. if (res != 0) {
  1954. error = CS_ERR_LIBRARY;
  1955. goto error_exit;
  1956. }
  1957. assert (cpg_iteration_instance);
  1958. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1959. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1960. error = CS_ERR_NO_SECTIONS;
  1961. goto error_put;
  1962. }
  1963. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1964. /*
  1965. * Copy iteration data
  1966. */
  1967. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1968. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1969. memcpy (&res_lib_cpg_iterationnext.description.group,
  1970. &pi->group,
  1971. sizeof (mar_cpg_name_t));
  1972. error_put:
  1973. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1974. error_exit:
  1975. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1976. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1977. res_lib_cpg_iterationnext.header.error = error;
  1978. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1979. sizeof (res_lib_cpg_iterationnext));
  1980. }
  1981. static void message_handler_req_lib_cpg_iteration_finalize (
  1982. void *conn,
  1983. const void *message)
  1984. {
  1985. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1986. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1987. struct cpg_iteration_instance *cpg_iteration_instance;
  1988. cs_error_t error = CS_OK;
  1989. int res;
  1990. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1991. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1992. req_lib_cpg_iterationfinalize->iteration_handle,
  1993. (void *)&cpg_iteration_instance);
  1994. if (res != 0) {
  1995. error = CS_ERR_LIBRARY;
  1996. goto error_exit;
  1997. }
  1998. assert (cpg_iteration_instance);
  1999. cpg_iteration_instance_finalize (cpg_iteration_instance);
  2000. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  2001. error_exit:
  2002. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  2003. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  2004. res_lib_cpg_iterationfinalize.header.error = error;
  2005. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  2006. sizeof (res_lib_cpg_iterationfinalize));
  2007. }