cpg.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * Author: Jan Friesse (jfriesse@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #ifdef HAVE_ALLOCA_H
  37. #include <alloca.h>
  38. #endif
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <netinet/in.h>
  44. #include <sys/uio.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <time.h>
  51. #include <unistd.h>
  52. #include <netinet/in.h>
  53. #include <arpa/inet.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/coroipc_types.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/list.h>
  58. #include <corosync/jhash.h>
  59. #include <corosync/lcr/lcr_comp.h>
  60. #include <corosync/engine/logsys.h>
  61. #include <corosync/engine/coroapi.h>
  62. #include <corosync/cpg.h>
  63. #include <corosync/ipc_cpg.h>
  64. LOGSYS_DECLARE_SUBSYS ("CPG");
  65. #define GROUP_HASH_SIZE 32
  66. enum cpg_message_req_types {
  67. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  68. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  69. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  70. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  71. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 4
  72. };
  73. /*
  74. * state` exec deliver
  75. * match group name, pid -> if matched deliver for YES:
  76. * XXX indicates impossible state
  77. *
  78. * join leave mcast
  79. * UNJOINED XXX XXX NO
  80. * LEAVE_STARTED XXX YES(unjoined_enter) YES
  81. * JOIN_STARTED YES(join_started_enter) XXX NO
  82. * JOIN_COMPLETED XXX NO YES
  83. *
  84. * join_started_enter
  85. * set JOIN_COMPLETED
  86. * add entry to process_info list
  87. * unjoined_enter
  88. * set UNJOINED
  89. * delete entry from process_info list
  90. *
  91. *
  92. * library accept join error codes
  93. * UNJOINED YES(CPG_OK) set JOIN_STARTED
  94. * LEAVE_STARTED NO(CPG_ERR_BUSY)
  95. * JOIN_STARTED NO(CPG_ERR_EXIST)
  96. * JOIN_COMPlETED NO(CPG_ERR_EXIST)
  97. *
  98. * library accept leave error codes
  99. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  100. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  101. * JOIN_STARTED NO(CPG_ERR_BUSY)
  102. * JOIN_COMPLETED YES(CPG_OK) set LEAVE_STARTED
  103. *
  104. * library accept mcast
  105. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  106. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  107. * JOIN_STARTED YES(CPG_OK)
  108. * JOIN_COMPLETED YES(CPG_OK)
  109. */
  110. enum cpd_state {
  111. CPD_STATE_UNJOINED,
  112. CPD_STATE_LEAVE_STARTED,
  113. CPD_STATE_JOIN_STARTED,
  114. CPD_STATE_JOIN_COMPLETED
  115. };
  116. enum cpg_sync_state {
  117. CPGSYNC_DOWNLIST,
  118. CPGSYNC_JOINLIST
  119. };
  120. struct cpg_pd {
  121. void *conn;
  122. mar_cpg_name_t group_name;
  123. uint32_t pid;
  124. enum cpd_state cpd_state;
  125. unsigned int flags;
  126. int initial_totem_conf_sent;
  127. struct list_head list;
  128. struct list_head iteration_instance_list_head;
  129. };
  130. struct cpg_iteration_instance {
  131. hdb_handle_t handle;
  132. struct list_head list;
  133. struct list_head items_list_head; /* List of process_info */
  134. struct list_head *current_pointer;
  135. };
  136. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  137. DECLARE_LIST_INIT(cpg_pd_list_head);
  138. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  139. static unsigned int my_member_list_entries;
  140. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  141. static unsigned int my_old_member_list_entries = 0;
  142. static struct corosync_api_v1 *api = NULL;
  143. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  144. static mar_cpg_ring_id_t last_sync_ring_id;
  145. struct process_info {
  146. unsigned int nodeid;
  147. uint32_t pid;
  148. mar_cpg_name_t group;
  149. struct list_head list; /* on the group_info members list */
  150. };
  151. DECLARE_LIST_INIT(process_info_list_head);
  152. struct join_list_entry {
  153. uint32_t pid;
  154. mar_cpg_name_t group_name;
  155. };
  156. /*
  157. * Service Interfaces required by service_message_handler struct
  158. */
  159. static int cpg_exec_init_fn (struct corosync_api_v1 *);
  160. static int cpg_lib_init_fn (void *conn);
  161. static int cpg_lib_exit_fn (void *conn);
  162. static void message_handler_req_exec_cpg_procjoin (
  163. const void *message,
  164. unsigned int nodeid);
  165. static void message_handler_req_exec_cpg_procleave (
  166. const void *message,
  167. unsigned int nodeid);
  168. static void message_handler_req_exec_cpg_joinlist (
  169. const void *message,
  170. unsigned int nodeid);
  171. static void message_handler_req_exec_cpg_mcast (
  172. const void *message,
  173. unsigned int nodeid);
  174. static void message_handler_req_exec_cpg_downlist (
  175. const void *message,
  176. unsigned int nodeid);
  177. static void exec_cpg_procjoin_endian_convert (void *msg);
  178. static void exec_cpg_joinlist_endian_convert (void *msg);
  179. static void exec_cpg_mcast_endian_convert (void *msg);
  180. static void exec_cpg_downlist_endian_convert (void *msg);
  181. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  182. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  183. static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
  184. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  185. static void message_handler_req_lib_cpg_membership (void *conn,
  186. const void *message);
  187. static void message_handler_req_lib_cpg_local_get (void *conn,
  188. const void *message);
  189. static void message_handler_req_lib_cpg_iteration_initialize (
  190. void *conn,
  191. const void *message);
  192. static void message_handler_req_lib_cpg_iteration_next (
  193. void *conn,
  194. const void *message);
  195. static void message_handler_req_lib_cpg_iteration_finalize (
  196. void *conn,
  197. const void *message);
  198. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  199. static int cpg_exec_send_downlist(void);
  200. static int cpg_exec_send_joinlist(void);
  201. static void cpg_sync_init_v2 (
  202. const unsigned int *trans_list,
  203. size_t trans_list_entries,
  204. const unsigned int *member_list,
  205. size_t member_list_entries,
  206. const struct memb_ring_id *ring_id);
  207. static int cpg_sync_process (void);
  208. static void cpg_sync_activate (void);
  209. static void cpg_sync_abort (void);
  210. static int notify_lib_totem_membership (
  211. void *conn,
  212. int member_list_entries,
  213. const unsigned int *member_list);
  214. /*
  215. * Library Handler Definition
  216. */
  217. static struct corosync_lib_handler cpg_lib_engine[] =
  218. {
  219. { /* 0 */
  220. .lib_handler_fn = message_handler_req_lib_cpg_join,
  221. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  222. },
  223. { /* 1 */
  224. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  225. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  226. },
  227. { /* 2 */
  228. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  229. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  230. },
  231. { /* 3 */
  232. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  233. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  234. },
  235. { /* 4 */
  236. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  237. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  238. },
  239. { /* 5 */
  240. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  241. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  242. },
  243. { /* 6 */
  244. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  245. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  246. },
  247. { /* 7 */
  248. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  249. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  250. },
  251. { /* 8 */
  252. .lib_handler_fn = message_handler_req_lib_cpg_finalize,
  253. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  254. },
  255. };
  256. static struct corosync_exec_handler cpg_exec_engine[] =
  257. {
  258. { /* 0 */
  259. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  260. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  261. },
  262. { /* 1 */
  263. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  264. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  265. },
  266. { /* 2 */
  267. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  268. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  269. },
  270. { /* 3 */
  271. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  272. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  273. },
  274. { /* 4 */
  275. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  276. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  277. },
  278. };
  279. struct corosync_service_engine cpg_service_engine = {
  280. .name = "corosync cluster closed process group service v1.01",
  281. .id = CPG_SERVICE,
  282. .priority = 1,
  283. .private_data_size = sizeof (struct cpg_pd),
  284. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  285. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  286. .lib_init_fn = cpg_lib_init_fn,
  287. .lib_exit_fn = cpg_lib_exit_fn,
  288. .lib_engine = cpg_lib_engine,
  289. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  290. .exec_init_fn = cpg_exec_init_fn,
  291. .exec_dump_fn = NULL,
  292. .exec_engine = cpg_exec_engine,
  293. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  294. .sync_mode = CS_SYNC_V1_APIV2,
  295. .sync_init = (sync_init_v1_fn_t)cpg_sync_init_v2,
  296. .sync_process = cpg_sync_process,
  297. .sync_activate = cpg_sync_activate,
  298. .sync_abort = cpg_sync_abort
  299. };
  300. /*
  301. * Dynamic loader definition
  302. */
  303. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void);
  304. static struct corosync_service_engine_iface_ver0 cpg_service_engine_iface = {
  305. .corosync_get_service_engine_ver0 = cpg_get_service_engine_ver0
  306. };
  307. static struct lcr_iface corosync_cpg_ver0[1] = {
  308. {
  309. .name = "corosync_cpg",
  310. .version = 0,
  311. .versions_replace = 0,
  312. .versions_replace_count = 0,
  313. .dependencies = 0,
  314. .dependency_count = 0,
  315. .constructor = NULL,
  316. .destructor = NULL,
  317. .interfaces = NULL
  318. }
  319. };
  320. static struct lcr_comp cpg_comp_ver0 = {
  321. .iface_count = 1,
  322. .ifaces = corosync_cpg_ver0
  323. };
  324. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  325. {
  326. return (&cpg_service_engine);
  327. }
  328. #ifdef COROSYNC_SOLARIS
  329. void corosync_lcr_component_register (void);
  330. void corosync_lcr_component_register (void) {
  331. #else
  332. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  333. #endif
  334. lcr_interfaces_set (&corosync_cpg_ver0[0], &cpg_service_engine_iface);
  335. lcr_component_register (&cpg_comp_ver0);
  336. }
  337. struct req_exec_cpg_procjoin {
  338. coroipc_request_header_t header __attribute__((aligned(8)));
  339. mar_cpg_name_t group_name __attribute__((aligned(8)));
  340. mar_uint32_t pid __attribute__((aligned(8)));
  341. mar_uint32_t reason __attribute__((aligned(8)));
  342. };
  343. struct req_exec_cpg_mcast {
  344. coroipc_request_header_t header __attribute__((aligned(8)));
  345. mar_cpg_name_t group_name __attribute__((aligned(8)));
  346. mar_uint32_t msglen __attribute__((aligned(8)));
  347. mar_uint32_t pid __attribute__((aligned(8)));
  348. mar_message_source_t source __attribute__((aligned(8)));
  349. mar_uint8_t message[] __attribute__((aligned(8)));
  350. };
  351. struct req_exec_cpg_downlist {
  352. coroipc_request_header_t header __attribute__((aligned(8)));
  353. mar_uint32_t left_nodes __attribute__((aligned(8)));
  354. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  355. };
  356. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  357. static int memb_list_remove_value (unsigned int *list,
  358. size_t list_entries, int value)
  359. {
  360. int j;
  361. int found = 0;
  362. for (j = 0; j < list_entries; j++) {
  363. if (list[j] == value) {
  364. /* mark next values to be copied down */
  365. found = 1;
  366. }
  367. else if (found) {
  368. list[j-1] = list[j];
  369. }
  370. }
  371. if (found)
  372. return (list_entries - 1);
  373. else
  374. return list_entries;
  375. }
  376. static void cpg_sync_init_v2 (
  377. const unsigned int *trans_list,
  378. size_t trans_list_entries,
  379. const unsigned int *member_list,
  380. size_t member_list_entries,
  381. const struct memb_ring_id *ring_id)
  382. {
  383. unsigned int lowest_nodeid = 0xffffffff;
  384. int entries;
  385. int i, j;
  386. int found;
  387. my_sync_state = CPGSYNC_DOWNLIST;
  388. memcpy (my_member_list, member_list, member_list_entries *
  389. sizeof (unsigned int));
  390. my_member_list_entries = member_list_entries;
  391. last_sync_ring_id.nodeid = ring_id->rep.nodeid;
  392. last_sync_ring_id.seq = ring_id->seq;
  393. for (i = 0; i < my_old_member_list_entries; i++) {
  394. found = 0;
  395. for (j = 0; j < trans_list_entries; j++) {
  396. if (my_old_member_list[i] == trans_list[j]) {
  397. found = 1;
  398. break;
  399. }
  400. }
  401. if (found == 0) {
  402. my_member_list_entries = memb_list_remove_value (
  403. my_member_list, my_member_list_entries,
  404. my_old_member_list[i]);
  405. }
  406. }
  407. for (i = 0; i < my_member_list_entries; i++) {
  408. if (my_member_list[i] < lowest_nodeid) {
  409. lowest_nodeid = my_member_list[i];
  410. }
  411. }
  412. entries = 0;
  413. if (lowest_nodeid == api->totem_nodeid_get()) {
  414. /*
  415. * Determine list of nodeids for downlist message
  416. */
  417. for (i = 0; i < my_old_member_list_entries; i++) {
  418. found = 0;
  419. for (j = 0; j < trans_list_entries; j++) {
  420. if (my_old_member_list[i] == trans_list[j]) {
  421. found = 1;
  422. break;
  423. }
  424. }
  425. if (found == 0) {
  426. g_req_exec_cpg_downlist.nodeids[entries++] =
  427. my_old_member_list[i];
  428. }
  429. }
  430. }
  431. g_req_exec_cpg_downlist.left_nodes = entries;
  432. }
  433. static int cpg_sync_process (void)
  434. {
  435. int res = -1;
  436. if (my_sync_state == CPGSYNC_DOWNLIST) {
  437. res = cpg_exec_send_downlist();
  438. if (res == -1) {
  439. return (-1);
  440. }
  441. my_sync_state = CPGSYNC_JOINLIST;
  442. }
  443. if (my_sync_state == CPGSYNC_JOINLIST) {
  444. res = cpg_exec_send_joinlist();
  445. }
  446. return (res);
  447. }
  448. static void cpg_sync_activate (void)
  449. {
  450. memcpy (my_old_member_list, my_member_list,
  451. my_member_list_entries * sizeof (unsigned int));
  452. my_old_member_list_entries = my_member_list_entries;
  453. notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
  454. }
  455. static void cpg_sync_abort (void)
  456. {
  457. }
  458. static int notify_lib_totem_membership (
  459. void *conn,
  460. int member_list_entries,
  461. const unsigned int *member_list)
  462. {
  463. struct list_head *iter;
  464. char *buf;
  465. int size;
  466. struct res_lib_cpg_totem_confchg_callback *res;
  467. size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
  468. sizeof(mar_uint32_t) * (member_list_entries);
  469. buf = alloca(size);
  470. if (!buf)
  471. return CPG_ERR_LIBRARY;
  472. res = (struct res_lib_cpg_totem_confchg_callback *)buf;
  473. res->member_list_entries = member_list_entries;
  474. res->header.size = size;
  475. res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
  476. res->header.error = CS_OK;
  477. memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
  478. memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
  479. if (conn == NULL) {
  480. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  481. struct cpg_pd *cpg_pd = list_entry (iter, struct cpg_pd, list);
  482. api->ipc_dispatch_send (cpg_pd->conn, buf, size);
  483. }
  484. } else {
  485. api->ipc_dispatch_send (conn, buf, size);
  486. }
  487. return CPG_OK;
  488. }
  489. static int notify_lib_joinlist(
  490. const mar_cpg_name_t *group_name,
  491. void *conn,
  492. int joined_list_entries,
  493. mar_cpg_address_t *joined_list,
  494. int left_list_entries,
  495. mar_cpg_address_t *left_list,
  496. int id)
  497. {
  498. int size;
  499. char *buf;
  500. struct list_head *iter;
  501. int count;
  502. struct res_lib_cpg_confchg_callback *res;
  503. mar_cpg_address_t *retgi;
  504. count = 0;
  505. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  506. struct process_info *pi = list_entry (iter, struct process_info, list);
  507. if (mar_name_compare (&pi->group, group_name) == 0) {
  508. int i;
  509. int founded = 0;
  510. for (i = 0; i < left_list_entries; i++) {
  511. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  512. founded++;
  513. }
  514. }
  515. if (!founded)
  516. count++;
  517. }
  518. }
  519. size = sizeof(struct res_lib_cpg_confchg_callback) +
  520. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  521. buf = alloca(size);
  522. if (!buf)
  523. return CPG_ERR_LIBRARY;
  524. res = (struct res_lib_cpg_confchg_callback *)buf;
  525. res->joined_list_entries = joined_list_entries;
  526. res->left_list_entries = left_list_entries;
  527. res->member_list_entries = count;
  528. retgi = res->member_list;
  529. res->header.size = size;
  530. res->header.id = id;
  531. res->header.error = CS_OK;
  532. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  533. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  534. struct process_info *pi=list_entry (iter, struct process_info, list);
  535. if (mar_name_compare (&pi->group, group_name) == 0) {
  536. int i;
  537. int founded = 0;
  538. for (i = 0;i < left_list_entries; i++) {
  539. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  540. founded++;
  541. }
  542. }
  543. if (!founded) {
  544. retgi->nodeid = pi->nodeid;
  545. retgi->pid = pi->pid;
  546. retgi++;
  547. }
  548. }
  549. }
  550. if (left_list_entries) {
  551. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  552. retgi += left_list_entries;
  553. }
  554. if (joined_list_entries) {
  555. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  556. retgi += joined_list_entries;
  557. }
  558. if (conn) {
  559. api->ipc_dispatch_send (conn, buf, size);
  560. } else {
  561. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  562. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  563. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  564. assert (left_list_entries <= 1);
  565. assert (joined_list_entries <= 1);
  566. if (joined_list_entries) {
  567. if (joined_list[0].pid == cpd->pid &&
  568. joined_list[0].nodeid == api->totem_nodeid_get()) {
  569. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  570. }
  571. }
  572. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  573. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  574. api->ipc_dispatch_send (cpd->conn, buf, size);
  575. }
  576. if (left_list_entries) {
  577. if (left_list[0].pid == cpd->pid &&
  578. left_list[0].nodeid == api->totem_nodeid_get()) {
  579. cpd->pid = 0;
  580. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  581. cpd->cpd_state = CPD_STATE_UNJOINED;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. /*
  588. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  589. */
  590. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  591. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  592. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  593. cpd->initial_totem_conf_sent = 1;
  594. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  595. }
  596. }
  597. return CPG_OK;
  598. }
  599. static int cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  600. {
  601. #ifdef COROSYNC_SOLARIS
  602. logsys_subsys_init();
  603. #endif
  604. api = corosync_api;
  605. return (0);
  606. }
  607. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  608. {
  609. struct list_head *iter, *iter_next;
  610. struct process_info *pi;
  611. for (iter = cpg_iteration_instance->items_list_head.next;
  612. iter != &cpg_iteration_instance->items_list_head;
  613. iter = iter_next) {
  614. iter_next = iter->next;
  615. pi = list_entry (iter, struct process_info, list);
  616. list_del (&pi->list);
  617. free (pi);
  618. }
  619. list_del (&cpg_iteration_instance->list);
  620. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  621. }
  622. static void cpg_pd_finalize (struct cpg_pd *cpd)
  623. {
  624. struct list_head *iter, *iter_next;
  625. struct cpg_iteration_instance *cpii;
  626. for (iter = cpd->iteration_instance_list_head.next;
  627. iter != &cpd->iteration_instance_list_head;
  628. iter = iter_next) {
  629. iter_next = iter->next;
  630. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  631. cpg_iteration_instance_finalize (cpii);
  632. }
  633. list_del (&cpd->list);
  634. }
  635. static int cpg_lib_exit_fn (void *conn)
  636. {
  637. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  638. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  639. if (cpd->group_name.length > 0) {
  640. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  641. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  642. }
  643. cpg_pd_finalize (cpd);
  644. api->ipc_refcnt_dec (conn);
  645. return (0);
  646. }
  647. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  648. {
  649. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  650. struct iovec req_exec_cpg_iovec;
  651. int result;
  652. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  653. req_exec_cpg_procjoin.pid = pid;
  654. req_exec_cpg_procjoin.reason = reason;
  655. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  656. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  657. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  658. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  659. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  660. return (result);
  661. }
  662. /* Can byteswap join & leave messages */
  663. static void exec_cpg_procjoin_endian_convert (void *msg)
  664. {
  665. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  666. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  667. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  668. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  669. }
  670. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  671. {
  672. char *msg = msg_v;
  673. coroipc_response_header_t *res = (coroipc_response_header_t *)msg;
  674. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(coroipc_response_header_t));
  675. swab_mar_int32_t (&res->size);
  676. while ((const char*)jle < msg + res->size) {
  677. jle->pid = swab32(jle->pid);
  678. swab_mar_cpg_name_t (&jle->group_name);
  679. jle++;
  680. }
  681. }
  682. static void exec_cpg_downlist_endian_convert (void *msg)
  683. {
  684. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  685. unsigned int i;
  686. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  687. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  688. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  689. }
  690. }
  691. static void exec_cpg_mcast_endian_convert (void *msg)
  692. {
  693. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  694. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  695. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  696. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  697. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  698. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  699. }
  700. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  701. struct list_head *iter;
  702. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  703. struct process_info *pi = list_entry (iter, struct process_info, list);
  704. iter = iter->next;
  705. if (pi->pid == pid && pi->nodeid == nodeid &&
  706. mar_name_compare (&pi->group, group_name) == 0) {
  707. return pi;
  708. }
  709. }
  710. return NULL;
  711. }
  712. static void do_proc_join(
  713. const mar_cpg_name_t *name,
  714. uint32_t pid,
  715. unsigned int nodeid,
  716. int reason)
  717. {
  718. struct process_info *pi;
  719. struct process_info *pi_entry;
  720. mar_cpg_address_t notify_info;
  721. struct list_head *list;
  722. struct list_head *list_to_add = NULL;
  723. if (process_info_find (name, pid, nodeid) != NULL) {
  724. return ;
  725. }
  726. pi = malloc (sizeof (struct process_info));
  727. if (!pi) {
  728. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  729. return;
  730. }
  731. pi->nodeid = nodeid;
  732. pi->pid = pid;
  733. memcpy(&pi->group, name, sizeof(*name));
  734. list_init(&pi->list);
  735. /*
  736. * Insert new process in sorted order so synchronization works properly
  737. */
  738. list_to_add = &process_info_list_head;
  739. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  740. pi_entry = list_entry(list, struct process_info, list);
  741. if (pi_entry->nodeid > pi->nodeid ||
  742. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  743. break;
  744. }
  745. list_to_add = list;
  746. }
  747. list_splice (&pi->list, list_to_add);
  748. notify_info.pid = pi->pid;
  749. notify_info.nodeid = nodeid;
  750. notify_info.reason = reason;
  751. notify_lib_joinlist(&pi->group, NULL,
  752. 1, &notify_info,
  753. 0, NULL,
  754. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  755. }
  756. static void message_handler_req_exec_cpg_downlist (
  757. const void *message,
  758. unsigned int nodeid)
  759. {
  760. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  761. int i;
  762. mar_cpg_address_t left_list[1];
  763. struct list_head *iter;
  764. /*
  765. FOR OPTIMALIZATION - Make list of lists
  766. */
  767. log_printf (LOGSYS_LEVEL_DEBUG, "downlist left_list: %d\n", req_exec_cpg_downlist->left_nodes);
  768. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  769. struct process_info *pi = list_entry(iter, struct process_info, list);
  770. iter = iter->next;
  771. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  772. if (pi->nodeid == req_exec_cpg_downlist->nodeids[i]) {
  773. left_list[0].nodeid = pi->nodeid;
  774. left_list[0].pid = pi->pid;
  775. left_list[0].reason = CONFCHG_CPG_REASON_NODEDOWN;
  776. notify_lib_joinlist(&pi->group, NULL,
  777. 0, NULL,
  778. 1, left_list,
  779. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  780. list_del (&pi->list);
  781. free (pi);
  782. break;
  783. }
  784. }
  785. }
  786. }
  787. static void message_handler_req_exec_cpg_procjoin (
  788. const void *message,
  789. unsigned int nodeid)
  790. {
  791. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  792. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d\n", nodeid);
  793. do_proc_join (&req_exec_cpg_procjoin->group_name,
  794. req_exec_cpg_procjoin->pid, nodeid,
  795. CONFCHG_CPG_REASON_JOIN);
  796. }
  797. static void message_handler_req_exec_cpg_procleave (
  798. const void *message,
  799. unsigned int nodeid)
  800. {
  801. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  802. struct process_info *pi;
  803. struct list_head *iter;
  804. mar_cpg_address_t notify_info;
  805. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d\n", nodeid);
  806. notify_info.pid = req_exec_cpg_procjoin->pid;
  807. notify_info.nodeid = nodeid;
  808. notify_info.reason = req_exec_cpg_procjoin->reason;
  809. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  810. 0, NULL,
  811. 1, &notify_info,
  812. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  813. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  814. pi = list_entry(iter, struct process_info, list);
  815. iter = iter->next;
  816. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  817. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  818. list_del (&pi->list);
  819. free (pi);
  820. }
  821. }
  822. }
  823. /* Got a proclist from another node */
  824. static void message_handler_req_exec_cpg_joinlist (
  825. const void *message_v,
  826. unsigned int nodeid)
  827. {
  828. const char *message = message_v;
  829. const coroipc_response_header_t *res = (const coroipc_response_header_t *)message;
  830. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(coroipc_response_header_t));
  831. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x\n",
  832. nodeid);
  833. /* Ignore our own messages */
  834. if (nodeid == api->totem_nodeid_get()) {
  835. return;
  836. }
  837. while ((const char*)jle < message + res->size) {
  838. do_proc_join (&jle->group_name, jle->pid, nodeid,
  839. CONFCHG_CPG_REASON_NODEUP);
  840. jle++;
  841. }
  842. }
  843. static void message_handler_req_exec_cpg_mcast (
  844. const void *message,
  845. unsigned int nodeid)
  846. {
  847. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  848. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  849. int msglen = req_exec_cpg_mcast->msglen;
  850. struct list_head *iter, *pi_iter;
  851. struct cpg_pd *cpd;
  852. struct iovec iovec[2];
  853. int known_node = 0;
  854. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  855. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  856. res_lib_cpg_mcast.msglen = msglen;
  857. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  858. res_lib_cpg_mcast.nodeid = nodeid;
  859. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  860. sizeof(mar_cpg_name_t));
  861. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  862. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  863. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  864. iovec[1].iov_len = msglen;
  865. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  866. cpd = list_entry(iter, struct cpg_pd, list);
  867. iter = iter->next;
  868. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  869. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  870. if (!known_node) {
  871. /* Try to find, if we know the node */
  872. for (pi_iter = process_info_list_head.next;
  873. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  874. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  875. if (pi->nodeid == nodeid &&
  876. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  877. known_node = 1;
  878. break;
  879. }
  880. }
  881. }
  882. if (!known_node) {
  883. /* Unknown node -> we will not deliver message */
  884. return ;
  885. }
  886. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  887. }
  888. }
  889. }
  890. static int cpg_exec_send_downlist(void)
  891. {
  892. struct iovec iov;
  893. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  894. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  895. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  896. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  897. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  898. }
  899. static int cpg_exec_send_joinlist(void)
  900. {
  901. int count = 0;
  902. struct list_head *iter;
  903. coroipc_response_header_t *res;
  904. char *buf;
  905. struct join_list_entry *jle;
  906. struct iovec req_exec_cpg_iovec;
  907. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  908. struct process_info *pi = list_entry (iter, struct process_info, list);
  909. if (pi->nodeid == api->totem_nodeid_get ()) {
  910. count++;
  911. }
  912. }
  913. /* Nothing to send */
  914. if (!count)
  915. return 0;
  916. buf = alloca(sizeof(coroipc_response_header_t) + sizeof(struct join_list_entry) * count);
  917. if (!buf) {
  918. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  919. return -1;
  920. }
  921. jle = (struct join_list_entry *)(buf + sizeof(coroipc_response_header_t));
  922. res = (coroipc_response_header_t *)buf;
  923. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  924. struct process_info *pi = list_entry (iter, struct process_info, list);
  925. if (pi->nodeid == api->totem_nodeid_get ()) {
  926. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  927. jle->pid = pi->pid;
  928. jle++;
  929. }
  930. }
  931. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  932. res->size = sizeof(coroipc_response_header_t)+sizeof(struct join_list_entry) * count;
  933. req_exec_cpg_iovec.iov_base = buf;
  934. req_exec_cpg_iovec.iov_len = res->size;
  935. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  936. }
  937. static int cpg_lib_init_fn (void *conn)
  938. {
  939. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  940. memset (cpd, 0, sizeof(struct cpg_pd));
  941. cpd->conn = conn;
  942. list_add (&cpd->list, &cpg_pd_list_head);
  943. list_init (&cpd->iteration_instance_list_head);
  944. api->ipc_refcnt_inc (conn);
  945. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p\n", conn, cpd);
  946. return (0);
  947. }
  948. /* Join message from the library */
  949. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  950. {
  951. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  952. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  953. struct res_lib_cpg_join res_lib_cpg_join;
  954. cs_error_t error = CPG_OK;
  955. struct list_head *iter;
  956. /* Test, if we don't have same pid and group name joined */
  957. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  958. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  959. if (cpd_item->pid == req_lib_cpg_join->pid &&
  960. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  961. /* We have same pid and group name joined -> return error */
  962. error = CPG_ERR_EXIST;
  963. goto response_send;
  964. }
  965. }
  966. /*
  967. * Same check must be done in process info list, because there may be not yet delivered
  968. * leave of client.
  969. */
  970. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  971. struct process_info *pi = list_entry (iter, struct process_info, list);
  972. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  973. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  974. /* We have same pid and group name joined -> return error */
  975. error = CPG_ERR_TRY_AGAIN;
  976. goto response_send;
  977. }
  978. }
  979. switch (cpd->cpd_state) {
  980. case CPD_STATE_UNJOINED:
  981. error = CPG_OK;
  982. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  983. cpd->pid = req_lib_cpg_join->pid;
  984. cpd->flags = req_lib_cpg_join->flags;
  985. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  986. sizeof (cpd->group_name));
  987. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  988. &req_lib_cpg_join->group_name,
  989. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  990. break;
  991. case CPD_STATE_LEAVE_STARTED:
  992. error = CPG_ERR_BUSY;
  993. break;
  994. case CPD_STATE_JOIN_STARTED:
  995. error = CPG_ERR_EXIST;
  996. break;
  997. case CPD_STATE_JOIN_COMPLETED:
  998. error = CPG_ERR_EXIST;
  999. break;
  1000. }
  1001. response_send:
  1002. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1003. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1004. res_lib_cpg_join.header.error = error;
  1005. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1006. }
  1007. /* Leave message from the library */
  1008. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1009. {
  1010. struct res_lib_cpg_leave res_lib_cpg_leave;
  1011. cs_error_t error = CPG_OK;
  1012. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1013. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1014. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p\n", conn);
  1015. switch (cpd->cpd_state) {
  1016. case CPD_STATE_UNJOINED:
  1017. error = CPG_ERR_NOT_EXIST;
  1018. break;
  1019. case CPD_STATE_LEAVE_STARTED:
  1020. error = CPG_ERR_NOT_EXIST;
  1021. break;
  1022. case CPD_STATE_JOIN_STARTED:
  1023. error = CPG_ERR_BUSY;
  1024. break;
  1025. case CPD_STATE_JOIN_COMPLETED:
  1026. error = CPG_OK;
  1027. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1028. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1029. &req_lib_cpg_leave->group_name,
  1030. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1031. CONFCHG_CPG_REASON_LEAVE);
  1032. break;
  1033. }
  1034. /* send return */
  1035. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1036. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1037. res_lib_cpg_leave.header.error = error;
  1038. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1039. }
  1040. /* Finalize message from library */
  1041. static void message_handler_req_lib_cpg_finalize (
  1042. void *conn,
  1043. const void *message)
  1044. {
  1045. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1046. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1047. cs_error_t error = CS_OK;
  1048. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p\n", conn);
  1049. /*
  1050. * We will just remove cpd from list. After this call, connection will be
  1051. * closed on lib side, and cpg_lib_exit_fn will be called
  1052. */
  1053. list_del (&cpd->list);
  1054. list_init (&cpd->list);
  1055. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1056. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1057. res_lib_cpg_finalize.header.error = error;
  1058. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1059. sizeof (res_lib_cpg_finalize));
  1060. }
  1061. /* Mcast message from the library */
  1062. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1063. {
  1064. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1065. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1066. mar_cpg_name_t group_name = cpd->group_name;
  1067. struct iovec req_exec_cpg_iovec[2];
  1068. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1069. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1070. int msglen = req_lib_cpg_mcast->msglen;
  1071. int result;
  1072. cs_error_t error = CPG_ERR_NOT_EXIST;
  1073. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p\n", conn);
  1074. switch (cpd->cpd_state) {
  1075. case CPD_STATE_UNJOINED:
  1076. error = CPG_ERR_NOT_EXIST;
  1077. break;
  1078. case CPD_STATE_LEAVE_STARTED:
  1079. error = CPG_ERR_NOT_EXIST;
  1080. break;
  1081. case CPD_STATE_JOIN_STARTED:
  1082. error = CPG_OK;
  1083. break;
  1084. case CPD_STATE_JOIN_COMPLETED:
  1085. error = CPG_OK;
  1086. break;
  1087. }
  1088. if (error == CPG_OK) {
  1089. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1090. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1091. MESSAGE_REQ_EXEC_CPG_MCAST);
  1092. req_exec_cpg_mcast.pid = cpd->pid;
  1093. req_exec_cpg_mcast.msglen = msglen;
  1094. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1095. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1096. sizeof(mar_cpg_name_t));
  1097. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1098. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1099. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1100. req_exec_cpg_iovec[1].iov_len = msglen;
  1101. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1102. assert(result == 0);
  1103. }
  1104. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1105. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1106. res_lib_cpg_mcast.header.error = error;
  1107. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1108. sizeof (res_lib_cpg_mcast));
  1109. }
  1110. static void message_handler_req_lib_cpg_membership (void *conn,
  1111. const void *message)
  1112. {
  1113. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1114. (struct req_lib_cpg_membership_get *)message;
  1115. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1116. struct list_head *iter;
  1117. int member_count = 0;
  1118. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1119. res_lib_cpg_membership_get.header.error = CPG_OK;
  1120. res_lib_cpg_membership_get.header.size =
  1121. sizeof (struct req_lib_cpg_membership_get);
  1122. for (iter = process_info_list_head.next;
  1123. iter != &process_info_list_head; iter = iter->next) {
  1124. struct process_info *pi = list_entry (iter, struct process_info, list);
  1125. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1126. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1127. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1128. member_count += 1;
  1129. }
  1130. }
  1131. res_lib_cpg_membership_get.member_count = member_count;
  1132. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1133. sizeof (res_lib_cpg_membership_get));
  1134. }
  1135. static void message_handler_req_lib_cpg_local_get (void *conn,
  1136. const void *message)
  1137. {
  1138. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1139. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1140. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1141. res_lib_cpg_local_get.header.error = CPG_OK;
  1142. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1143. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1144. sizeof (res_lib_cpg_local_get));
  1145. }
  1146. static void message_handler_req_lib_cpg_iteration_initialize (
  1147. void *conn,
  1148. const void *message)
  1149. {
  1150. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1151. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1152. hdb_handle_t cpg_iteration_handle = 0;
  1153. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1154. struct list_head *iter, *iter2;
  1155. struct cpg_iteration_instance *cpg_iteration_instance;
  1156. cs_error_t error = CS_OK;
  1157. int res;
  1158. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize\n");
  1159. /* Because between calling this function and *next can be some operations which will
  1160. * change list, we must do full copy.
  1161. */
  1162. /*
  1163. * Create new iteration instance
  1164. */
  1165. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1166. &cpg_iteration_handle);
  1167. if (res != 0) {
  1168. error = CS_ERR_NO_MEMORY;
  1169. goto response_send;
  1170. }
  1171. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1172. if (res != 0) {
  1173. error = CS_ERR_BAD_HANDLE;
  1174. goto error_destroy;
  1175. }
  1176. list_init (&cpg_iteration_instance->items_list_head);
  1177. cpg_iteration_instance->handle = cpg_iteration_handle;
  1178. /*
  1179. * Create copy of process_info list "grouped by" group name
  1180. */
  1181. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1182. struct process_info *pi = list_entry (iter, struct process_info, list);
  1183. struct process_info *new_pi;
  1184. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1185. /*
  1186. * Try to find processed group name in our list new list
  1187. */
  1188. int found = 0;
  1189. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1190. iter2 != &cpg_iteration_instance->items_list_head;
  1191. iter2 = iter2->next) {
  1192. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1193. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1194. found = 1;
  1195. break;
  1196. }
  1197. }
  1198. if (found) {
  1199. /*
  1200. * We have this name in list -> don't add
  1201. */
  1202. continue ;
  1203. }
  1204. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1205. /*
  1206. * Test pi group name with request
  1207. */
  1208. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1209. /*
  1210. * Not same -> don't add
  1211. */
  1212. continue ;
  1213. }
  1214. new_pi = malloc (sizeof (struct process_info));
  1215. if (!new_pi) {
  1216. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1217. error = CS_ERR_NO_MEMORY;
  1218. goto error_put_destroy;
  1219. }
  1220. memcpy (new_pi, pi, sizeof (struct process_info));
  1221. list_init (&new_pi->list);
  1222. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1223. /*
  1224. * pid and nodeid -> undefined
  1225. */
  1226. new_pi->pid = new_pi->nodeid = 0;
  1227. }
  1228. /*
  1229. * We will return list "grouped" by "group name", so try to find right place to add
  1230. */
  1231. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1232. iter2 != &cpg_iteration_instance->items_list_head;
  1233. iter2 = iter2->next) {
  1234. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1235. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1236. break;
  1237. }
  1238. }
  1239. list_add (&new_pi->list, iter2);
  1240. }
  1241. /*
  1242. * Now we have a full "grouped by" copy of process_info list
  1243. */
  1244. /*
  1245. * Add instance to current cpd list
  1246. */
  1247. list_init (&cpg_iteration_instance->list);
  1248. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1249. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1250. error_put_destroy:
  1251. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1252. error_destroy:
  1253. if (error != CS_OK) {
  1254. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1255. }
  1256. response_send:
  1257. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1258. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1259. res_lib_cpg_iterationinitialize.header.error = error;
  1260. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1261. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1262. sizeof (res_lib_cpg_iterationinitialize));
  1263. }
  1264. static void message_handler_req_lib_cpg_iteration_next (
  1265. void *conn,
  1266. const void *message)
  1267. {
  1268. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1269. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1270. struct cpg_iteration_instance *cpg_iteration_instance;
  1271. cs_error_t error = CS_OK;
  1272. int res;
  1273. struct process_info *pi;
  1274. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next\n");
  1275. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1276. req_lib_cpg_iterationnext->iteration_handle,
  1277. (void *)&cpg_iteration_instance);
  1278. if (res != 0) {
  1279. error = CS_ERR_LIBRARY;
  1280. goto error_exit;
  1281. }
  1282. assert (cpg_iteration_instance);
  1283. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1284. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1285. error = CS_ERR_NO_SECTIONS;
  1286. goto error_put;
  1287. }
  1288. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1289. /*
  1290. * Copy iteration data
  1291. */
  1292. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1293. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1294. memcpy (&res_lib_cpg_iterationnext.description.group,
  1295. &pi->group,
  1296. sizeof (mar_cpg_name_t));
  1297. error_put:
  1298. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1299. error_exit:
  1300. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1301. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1302. res_lib_cpg_iterationnext.header.error = error;
  1303. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1304. sizeof (res_lib_cpg_iterationnext));
  1305. }
  1306. static void message_handler_req_lib_cpg_iteration_finalize (
  1307. void *conn,
  1308. const void *message)
  1309. {
  1310. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1311. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1312. struct cpg_iteration_instance *cpg_iteration_instance;
  1313. cs_error_t error = CS_OK;
  1314. int res;
  1315. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize\n");
  1316. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1317. req_lib_cpg_iterationfinalize->iteration_handle,
  1318. (void *)&cpg_iteration_instance);
  1319. if (res != 0) {
  1320. error = CS_ERR_LIBRARY;
  1321. goto error_exit;
  1322. }
  1323. assert (cpg_iteration_instance);
  1324. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1325. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1326. error_exit:
  1327. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1328. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1329. res_lib_cpg_iterationfinalize.header.error = error;
  1330. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1331. sizeof (res_lib_cpg_iterationfinalize));
  1332. }