4
0

vsf_ykd.c 13 KB

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