vsf_ykd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@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 CONTRIBUTORS "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 <assert.h>
  36. #include <pwd.h>
  37. #include <grp.h>
  38. #include <sys/types.h>
  39. #include <sys/poll.h>
  40. #include <sys/uio.h>
  41. #include <sys/mman.h>
  42. #include <sys/socket.h>
  43. #include <sys/un.h>
  44. #include <sys/time.h>
  45. #include <sys/resource.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <signal.h>
  54. #include <sched.h>
  55. #include <time.h>
  56. #include <corosync/engine/logsys.h>
  57. #include <corosync/ipc_gen.h>
  58. #include <corosync/engine/coroapi.h>
  59. #include <corosync/engine/quorum.h>
  60. #include <corosync/swab.h>
  61. #include <corosync/lcr/lcr_comp.h>
  62. LOGSYS_DECLARE_SUBSYS ("YKD", LOG_INFO);
  63. #define YKD_PROCESSOR_COUNT_MAX 32
  64. enum ykd_header_values {
  65. YKD_HEADER_SENDSTATE = 0,
  66. YKD_HEADER_ATTEMPT = 1
  67. };
  68. enum ykd_mode {
  69. YKD_MODE_SENDSTATE = 0,
  70. YKD_MODE_ATTEMPT = 1
  71. };
  72. struct ykd_header {
  73. int id;
  74. };
  75. struct ykd_session {
  76. unsigned int member_list[YKD_PROCESSOR_COUNT_MAX];
  77. int member_list_entries;
  78. int session_id;
  79. };
  80. struct ykd_state {
  81. struct ykd_session last_primary;
  82. struct ykd_session last_formed[YKD_PROCESSOR_COUNT_MAX];
  83. int last_formed_entries;
  84. struct ykd_session ambiguous_sessions[YKD_PROCESSOR_COUNT_MAX];
  85. int ambiguous_sessions_entries;
  86. int session_id;
  87. };
  88. struct state_received {
  89. unsigned int nodeid;
  90. int received;
  91. struct ykd_state ykd_state;
  92. };
  93. struct ykd_state ykd_state;
  94. static hdb_handle_t ykd_group_handle;
  95. static struct state_received state_received_confchg[YKD_PROCESSOR_COUNT_MAX];
  96. static int state_received_confchg_entries;
  97. static struct state_received state_received_process[YKD_PROCESSOR_COUNT_MAX];
  98. static int state_received_process_entries;
  99. static enum ykd_mode ykd_mode;
  100. static unsigned int view_list[YKD_PROCESSOR_COUNT_MAX];
  101. static int view_list_entries;
  102. static int session_id_max;
  103. static struct ykd_session *last_primary_max;
  104. static struct ykd_session ambiguous_sessions_max[YKD_PROCESSOR_COUNT_MAX];
  105. static int ambiguous_sessions_max_entries;
  106. static int primary_designated = 0;
  107. static struct memb_ring_id ykd_ring_id;
  108. static void *ykd_attempt_send_callback_token_handle = 0;
  109. static void *ykd_state_send_callback_token_handle = 0;
  110. static struct corosync_api_v1 *api;
  111. static void (*ykd_primary_callback_fn) (
  112. unsigned int *view_list,
  113. int view_list_entries,
  114. int primary_designated,
  115. struct memb_ring_id *ring_id) = NULL;
  116. void ykd_state_init (void)
  117. {
  118. ykd_state.session_id = 0;
  119. ykd_state.last_formed_entries = 0;
  120. ykd_state.ambiguous_sessions_entries = 0;
  121. ykd_state.last_primary.session_id = 0;
  122. ykd_state.last_primary.member_list_entries = 0;
  123. }
  124. static int ykd_state_send_msg (enum totem_callback_token_type type, void *context)
  125. {
  126. struct iovec iovec[2];
  127. struct ykd_header header;
  128. int res;
  129. header.id = YKD_HEADER_SENDSTATE;
  130. iovec[0].iov_base = (char *)&header;
  131. iovec[0].iov_len = sizeof (struct ykd_header);
  132. iovec[1].iov_base = (char *)&ykd_state;
  133. iovec[1].iov_len = sizeof (struct ykd_state);
  134. res = api->tpg_joined_mcast (ykd_group_handle, iovec, 2,
  135. TOTEM_AGREED);
  136. return (res);
  137. }
  138. static void ykd_state_send (void)
  139. {
  140. api->totem_callback_token_create (
  141. &ykd_state_send_callback_token_handle,
  142. TOTEM_CALLBACK_TOKEN_SENT,
  143. 1, /* delete after callback */
  144. ykd_state_send_msg,
  145. NULL);
  146. }
  147. static int ykd_attempt_send_msg (enum totem_callback_token_type type, void *context)
  148. {
  149. struct iovec iovec;
  150. struct ykd_header header;
  151. int res;
  152. header.id = YKD_HEADER_SENDSTATE;
  153. iovec.iov_base = (char *)&header;
  154. iovec.iov_len = sizeof (struct ykd_header);
  155. res = api->tpg_joined_mcast (ykd_group_handle, &iovec, 1,
  156. TOTEM_AGREED);
  157. return (res);
  158. }
  159. static void ykd_attempt_send (void)
  160. {
  161. api->totem_callback_token_create (
  162. &ykd_attempt_send_callback_token_handle,
  163. TOTEM_CALLBACK_TOKEN_SENT,
  164. 1, /* delete after callback */
  165. ykd_attempt_send_msg,
  166. NULL);
  167. }
  168. static void compute (void)
  169. {
  170. int i;
  171. int j;
  172. session_id_max = 0;
  173. last_primary_max = &state_received_process[0].ykd_state.last_primary;
  174. ambiguous_sessions_max_entries = 0;
  175. for (i = 0; i < state_received_process_entries; i++) {
  176. /*
  177. * Calculate maximum session id
  178. */
  179. if (state_received_process[i].ykd_state.session_id > session_id_max) {
  180. session_id_max = state_received_process[i].ykd_state.session_id;
  181. }
  182. /*
  183. * Calculate maximum primary id
  184. */
  185. if (state_received_process[i].ykd_state.last_primary.session_id > last_primary_max->session_id) {
  186. last_primary_max = &state_received_process[i].ykd_state.last_primary;
  187. }
  188. /*
  189. * generate the maximum ambiguous sessions list
  190. */
  191. for (j = 0; j < state_received_process[i].ykd_state.ambiguous_sessions_entries; j++) {
  192. if (state_received_process[i].ykd_state.ambiguous_sessions[j].session_id > last_primary_max->session_id) {
  193. memcpy (&ambiguous_sessions_max[ambiguous_sessions_max_entries],
  194. &state_received_process[i].ykd_state.ambiguous_sessions[j],
  195. sizeof (struct ykd_session));
  196. ambiguous_sessions_max_entries += 1;
  197. }
  198. }
  199. }
  200. }
  201. static int subquorum (
  202. unsigned int *member_list,
  203. int member_list_entries,
  204. struct ykd_session *session)
  205. {
  206. int intersections = 0;
  207. int i;
  208. int j;
  209. for (i = 0; i < member_list_entries; i++) {
  210. for (j = 0; j < session->member_list_entries; j++) {
  211. if (member_list[i] == session->member_list[j]) {
  212. intersections += 1;
  213. }
  214. }
  215. }
  216. /*
  217. * even split
  218. */
  219. if (intersections == (session->member_list_entries - intersections)) {
  220. return (1);
  221. } else
  222. /*
  223. * majority split
  224. */
  225. if (intersections > (session->member_list_entries - intersections)) {
  226. return (1);
  227. }
  228. return (0);
  229. }
  230. static int decide (void)
  231. {
  232. int i;
  233. /*
  234. * Determine if there is a subquorum
  235. */
  236. if (subquorum (view_list, view_list_entries, last_primary_max) == 0) {
  237. return (0);
  238. }
  239. for (i = 0; i < ambiguous_sessions_max_entries; i++) {
  240. if (subquorum (view_list, view_list_entries, &ambiguous_sessions_max[i]) == 0) {
  241. return (0);
  242. }
  243. }
  244. return (1);
  245. }
  246. static void ykd_session_endian_convert (struct ykd_session *ykd_session)
  247. {
  248. int i;
  249. ykd_session->member_list_entries =
  250. swab32 (ykd_session->member_list_entries);
  251. ykd_session->session_id = swab32 (ykd_session->session_id);
  252. for (i = 0; i < ykd_session->member_list_entries; i++) {
  253. ykd_session->member_list[i] =
  254. swab32 (ykd_session->member_list[i]);
  255. }
  256. }
  257. static void ykd_state_endian_convert (struct ykd_state *ykd_state)
  258. {
  259. int i;
  260. ykd_session_endian_convert (&ykd_state->last_primary);
  261. ykd_state->last_formed_entries = swab32 (ykd_state->last_formed_entries);
  262. ykd_state->ambiguous_sessions_entries = swab32 (ykd_state->ambiguous_sessions_entries);
  263. ykd_state->session_id = swab32 (ykd_state->session_id);
  264. for (i = 0; i < ykd_state->last_formed_entries; i++) {
  265. ykd_session_endian_convert (&ykd_state->last_formed[i]);
  266. }
  267. for (i = 0; i < ykd_state->ambiguous_sessions_entries; i++) {
  268. ykd_session_endian_convert (&ykd_state->ambiguous_sessions[i]);
  269. }
  270. }
  271. static void ykd_deliver_fn (
  272. unsigned int nodeid,
  273. struct iovec *iovec,
  274. int iov_len,
  275. int endian_conversion_required)
  276. {
  277. int all_received = 1;
  278. int state_position = 0;
  279. int i;
  280. char *msg_state = iovec->iov_base + sizeof (struct ykd_header);
  281. /*
  282. * If this is a localhost address, this node is always primary
  283. */
  284. #ifdef TODO
  285. if (totemip_localhost_check (source_addr)) {
  286. log_printf (LOG_LEVEL_NOTICE,
  287. "This processor is within the primary component.\n");
  288. primary_designated = 1;
  289. ykd_primary_callback_fn (
  290. view_list,
  291. view_list_entries,
  292. primary_designated,
  293. &ykd_ring_id);
  294. return;
  295. }
  296. #endif
  297. if (endian_conversion_required &&
  298. (iovec->iov_len > sizeof (struct ykd_header))) {
  299. ykd_state_endian_convert ((struct ykd_state *)msg_state);
  300. }
  301. /*
  302. * Set completion for source_addr's address
  303. */
  304. for (state_position = 0; state_position < state_received_confchg_entries; state_position++) {
  305. if (nodeid == state_received_process[state_position].nodeid) {
  306. /*
  307. * State position contains the address of the state to modify
  308. * This may be used later by the other algorithms
  309. */
  310. state_received_process[state_position].received = 1;
  311. break;
  312. }
  313. }
  314. /*
  315. * Test if all nodes have submitted their state data
  316. */
  317. for (i = 0; i < state_received_confchg_entries; i++) {
  318. if (state_received_process[i].received == 0) {
  319. all_received = 0;
  320. }
  321. }
  322. switch (ykd_mode) {
  323. case YKD_MODE_SENDSTATE:
  324. assert (iovec->iov_len > sizeof (struct ykd_header));
  325. /*
  326. * Copy state information for the sending processor
  327. */
  328. memcpy (&state_received_process[state_position].ykd_state,
  329. msg_state, sizeof (struct ykd_state));
  330. /*
  331. * Try to form a component
  332. */
  333. if (all_received) {
  334. for (i = 0; i < state_received_confchg_entries; i++) {
  335. state_received_process[i].received = 0;
  336. }
  337. ykd_mode = YKD_MODE_ATTEMPT;
  338. // TODO resolve optimizes for failure conditions during ykd calculation
  339. // resolve();
  340. compute();
  341. if (decide ()) {
  342. ykd_state.session_id = session_id_max + 1;
  343. memcpy (ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list,
  344. view_list, sizeof (unsigned int) * view_list_entries);
  345. ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list_entries = view_list_entries;
  346. ykd_state.ambiguous_sessions_entries += 1;
  347. ykd_attempt_send();
  348. }
  349. }
  350. break;
  351. case YKD_MODE_ATTEMPT:
  352. if (all_received) {
  353. log_printf (LOG_LEVEL_NOTICE,
  354. "This processor is within the primary component.\n");
  355. primary_designated = 1;
  356. ykd_primary_callback_fn (
  357. view_list,
  358. view_list_entries,
  359. primary_designated,
  360. &ykd_ring_id);
  361. memcpy (ykd_state.last_primary.member_list, view_list, sizeof (view_list));
  362. ykd_state.last_primary.member_list_entries = view_list_entries;
  363. ykd_state.last_primary.session_id = ykd_state.session_id;
  364. ykd_state.ambiguous_sessions_entries = 0;
  365. }
  366. break;
  367. }
  368. }
  369. int first_run = 1;
  370. static void ykd_confchg_fn (
  371. enum totem_configuration_type configuration_type,
  372. unsigned int *member_list, int member_list_entries,
  373. unsigned int *left_list, int left_list_entries,
  374. unsigned int *joined_list, int joined_list_entries,
  375. struct memb_ring_id *ring_id)
  376. {
  377. int i;
  378. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  379. return;
  380. }
  381. memcpy (&ykd_ring_id, ring_id, sizeof (struct memb_ring_id));
  382. if (first_run) {
  383. ykd_state.last_primary.member_list[0] = api->totem_nodeid_get();
  384. ykd_state.last_primary.member_list_entries = 1;
  385. ykd_state.last_primary.session_id = 0;
  386. first_run = 0;
  387. }
  388. memcpy (view_list, member_list,
  389. member_list_entries * sizeof (unsigned int));
  390. view_list_entries = member_list_entries;
  391. ykd_mode = YKD_MODE_SENDSTATE;
  392. primary_designated = 0;
  393. ykd_primary_callback_fn (
  394. view_list,
  395. view_list_entries,
  396. primary_designated,
  397. &ykd_ring_id);
  398. memset (&state_received_confchg, 0, sizeof (state_received_confchg));
  399. for (i = 0; i < member_list_entries; i++) {
  400. state_received_confchg[i].nodeid = member_list[i];
  401. state_received_confchg[i].received = 0;
  402. }
  403. memcpy (state_received_process, state_received_confchg,
  404. sizeof (state_received_confchg));
  405. state_received_confchg_entries = member_list_entries;
  406. state_received_process_entries = member_list_entries;
  407. ykd_state_send ();
  408. }
  409. struct corosync_tpg_group ykd_group = {
  410. .group = "ykd",
  411. .group_len = 3
  412. };
  413. static void ykd_init (
  414. struct corosync_api_v1 *corosync_api,
  415. quorum_set_quorate_fn_t set_primary)
  416. {
  417. ykd_primary_callback_fn = set_primary;
  418. api = corosync_api;
  419. api->tpg_init (
  420. &ykd_group_handle,
  421. ykd_deliver_fn,
  422. ykd_confchg_fn);
  423. api->tpg_join (
  424. ykd_group_handle,
  425. &ykd_group,
  426. 1);
  427. ykd_state_init ();
  428. }
  429. /*
  430. * lcrso object definition
  431. */
  432. static struct quorum_services_api_ver1 vsf_ykd_iface_ver0 = {
  433. .init = ykd_init,
  434. };
  435. static struct lcr_iface corosync_vsf_ykd_ver0[1] = {
  436. {
  437. .name = "corosync_quorum_ykd",
  438. .version = 0,
  439. .versions_replace = 0,
  440. .versions_replace_count = 0,
  441. .dependencies = 0,
  442. .dependency_count = 0,
  443. .constructor = NULL,
  444. .destructor = NULL,
  445. .interfaces = (void **)(void *)&vsf_ykd_iface_ver0,
  446. }
  447. };
  448. static struct lcr_comp vsf_ykd_comp_ver0 = {
  449. .iface_count = 1,
  450. .ifaces = corosync_vsf_ykd_ver0
  451. };
  452. __attribute__ ((constructor)) static void vsf_ykd_comp_register (void) {
  453. lcr_component_register (&vsf_ykd_comp_ver0);
  454. }