ykd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2005 MontaVista Software, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  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 <signal.h>
  55. #include <sched.h>
  56. #include <time.h>
  57. #include "main.h"
  58. #include "print.h"
  59. #include "swab.h"
  60. #define LOG_SERVICE LOG_SERVICE_YKD
  61. enum ykd_header_values {
  62. YKD_HEADER_SENDSTATE = 0,
  63. YKD_HEADER_ATTEMPT = 1
  64. };
  65. enum ykd_mode {
  66. YKD_MODE_SENDSTATE = 0,
  67. YKD_MODE_ATTEMPT = 1
  68. };
  69. struct ykd_header {
  70. int id;
  71. };
  72. struct ykd_session {
  73. struct totem_ip_address member_list[PROCESSOR_COUNT_MAX];
  74. int member_list_entries;
  75. int session_id;
  76. };
  77. struct ykd_state {
  78. struct ykd_session last_primary;
  79. struct ykd_session last_formed[PROCESSOR_COUNT_MAX];
  80. int last_formed_entries;
  81. struct ykd_session ambiguous_sessions[PROCESSOR_COUNT_MAX];
  82. int ambiguous_sessions_entries;
  83. int session_id;
  84. };
  85. struct state_received {
  86. struct totem_ip_address addr;
  87. int received;
  88. struct ykd_state ykd_state;
  89. };
  90. struct ykd_state ykd_state;
  91. static totempg_groups_handle ykd_group_handle;
  92. static struct state_received state_received_confchg[PROCESSOR_COUNT_MAX];
  93. static int state_received_confchg_entries;
  94. static struct state_received state_received_process[PROCESSOR_COUNT_MAX];
  95. static int state_received_process_entries;
  96. static enum ykd_mode ykd_mode;
  97. static struct totem_ip_address view_list[PROCESSOR_COUNT_MAX];
  98. static int view_list_entries;
  99. static int session_id_max;
  100. static struct ykd_session *last_primary_max;
  101. static struct ykd_session ambiguous_sessions_max[PROCESSOR_COUNT_MAX];
  102. static int ambiguous_sessions_max_entries;
  103. static int primary_designated = 0;
  104. static struct memb_ring_id ykd_ring_id;
  105. static void *ykd_attempt_send_callback_token_handle = 0;
  106. static void *ykd_state_send_callback_token_handle = 0;
  107. static void (*ykd_primary_callback_fn) (
  108. struct totem_ip_address *view_list,
  109. int view_list_entries,
  110. int primary_designated,
  111. struct memb_ring_id *ring_id) = NULL;
  112. void ykd_state_init (void)
  113. {
  114. ykd_state.session_id = 0;
  115. ykd_state.last_formed_entries = 0;
  116. ykd_state.ambiguous_sessions_entries = 0;
  117. ykd_state.last_primary.session_id = 0;
  118. ykd_state.last_primary.member_list_entries = 0;
  119. }
  120. int ykd_state_send_msg (enum totem_callback_token_type type, void *context)
  121. {
  122. struct iovec iovec[2];
  123. struct ykd_header header;
  124. int res;
  125. header.id = YKD_HEADER_SENDSTATE;
  126. iovec[0].iov_base = &header;
  127. iovec[0].iov_len = sizeof (struct ykd_header);
  128. iovec[1].iov_base = &ykd_state;
  129. iovec[1].iov_len = sizeof (struct ykd_state);
  130. res = totempg_groups_mcast_joined (ykd_group_handle, iovec, 2,
  131. TOTEMPG_AGREED);
  132. return (res);
  133. }
  134. void ykd_state_send (void)
  135. {
  136. totempg_callback_token_create (
  137. &ykd_state_send_callback_token_handle,
  138. TOTEM_CALLBACK_TOKEN_SENT,
  139. 1, /* delete after callback */
  140. ykd_state_send_msg,
  141. NULL);
  142. }
  143. int ykd_attempt_send_msg (enum totem_callback_token_type type, void *context)
  144. {
  145. struct iovec iovec;
  146. struct ykd_header header;
  147. int res;
  148. header.id = YKD_HEADER_SENDSTATE;
  149. iovec.iov_base = &header;
  150. iovec.iov_len = sizeof (struct ykd_header);
  151. res = totempg_groups_mcast_joined (ykd_group_handle, &iovec, 1,
  152. TOTEMPG_AGREED);
  153. return (res);
  154. }
  155. void ykd_attempt_send (void)
  156. {
  157. totempg_callback_token_create (
  158. &ykd_attempt_send_callback_token_handle,
  159. TOTEM_CALLBACK_TOKEN_SENT,
  160. 1, /* delete after callback */
  161. ykd_attempt_send_msg,
  162. NULL);
  163. }
  164. void compute (void)
  165. {
  166. int i;
  167. int j;
  168. session_id_max = 0;
  169. last_primary_max = &state_received_process[0].ykd_state.last_primary;
  170. ambiguous_sessions_max_entries = 0;
  171. for (i = 0; i < state_received_process_entries; i++) {
  172. /*
  173. * Calculate maximum session id
  174. */
  175. if (state_received_process[i].ykd_state.session_id > session_id_max) {
  176. session_id_max = state_received_process[i].ykd_state.session_id;
  177. }
  178. /*
  179. * Calculate maximum primary id
  180. */
  181. if (state_received_process[i].ykd_state.last_primary.session_id > last_primary_max->session_id) {
  182. last_primary_max = &state_received_process[i].ykd_state.last_primary;
  183. }
  184. /*
  185. * generate the maximum ambiguous sessions list
  186. */
  187. for (j = 0; j < state_received_process[i].ykd_state.ambiguous_sessions_entries; j++) {
  188. if (state_received_process[i].ykd_state.ambiguous_sessions[j].session_id > last_primary_max->session_id) {
  189. memcpy (&ambiguous_sessions_max[ambiguous_sessions_max_entries],
  190. &state_received_process[i].ykd_state.ambiguous_sessions[j],
  191. sizeof (struct ykd_session));
  192. ambiguous_sessions_max_entries += 1;
  193. }
  194. }
  195. }
  196. }
  197. int subquorum (
  198. struct totem_ip_address *member_list,
  199. int member_list_entries,
  200. struct ykd_session *session)
  201. {
  202. int intersections = 0;
  203. int i;
  204. int j;
  205. for (i = 0; i < member_list_entries; i++) {
  206. for (j = 0; j < session->member_list_entries; j++) {
  207. if (totemip_equal (&member_list[i], &session->member_list[j])) {
  208. intersections += 1;
  209. }
  210. }
  211. }
  212. /*
  213. * even split
  214. */
  215. if (intersections == (session->member_list_entries - intersections)) {
  216. return (1);
  217. } else
  218. /*
  219. * majority split
  220. */
  221. if (intersections > (session->member_list_entries - intersections)) {
  222. return (1);
  223. }
  224. return (0);
  225. }
  226. int decide (void)
  227. {
  228. int i;
  229. printf ("decide 1\n");
  230. /*
  231. * Determine if there is a subquorum
  232. */
  233. if (subquorum (view_list, view_list_entries, last_primary_max) == 0) {
  234. return (0);
  235. }
  236. printf ("decide 2\n");
  237. for (i = 0; i < ambiguous_sessions_max_entries; i++) {
  238. if (subquorum (view_list, view_list_entries, &ambiguous_sessions_max[i]) == 0) {
  239. return (0);
  240. }
  241. }
  242. printf ("decide 3\n");
  243. return (1);
  244. }
  245. void ykd_session_endian_convert (struct ykd_session *ykd_session)
  246. {
  247. int i;
  248. ykd_session->member_list_entries = 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. totemip_copy_endian_convert (&ykd_session->member_list[i], &ykd_session->member_list[i]);
  252. }
  253. }
  254. void ykd_state_endian_convert (struct ykd_state *ykd_state)
  255. {
  256. int i;
  257. ykd_session_endian_convert (&ykd_state->last_primary);
  258. ykd_state->last_formed_entries = swab32 (ykd_state->last_formed_entries);
  259. ykd_state->ambiguous_sessions_entries = swab32 (ykd_state->ambiguous_sessions_entries);
  260. ykd_state->session_id = swab32 (ykd_state->session_id);
  261. for (i = 0; i < ykd_state->last_formed_entries; i++) {
  262. ykd_session_endian_convert (&ykd_state->last_formed[i]);
  263. }
  264. for (i = 0; i < ykd_state->ambiguous_sessions_entries; i++) {
  265. ykd_session_endian_convert (&ykd_state->ambiguous_sessions[i]);
  266. }
  267. }
  268. static void ykd_deliver_fn (
  269. struct totem_ip_address *source_addr,
  270. struct iovec *iovec,
  271. int iov_len,
  272. int endian_conversion_required)
  273. {
  274. int all_received = 1;
  275. int state_position = 0;
  276. int i;
  277. char *msg_state = iovec->iov_base + sizeof (struct ykd_header);
  278. /*
  279. * If this is a localhost address, this node is always primary
  280. */
  281. if (totemip_localhost_check (source_addr)) {
  282. log_printf (LOG_LEVEL_NOTICE,
  283. "This processor is within the primary component.\n");
  284. primary_designated = 1;
  285. ykd_primary_callback_fn (
  286. view_list,
  287. view_list_entries,
  288. primary_designated,
  289. &ykd_ring_id);
  290. return;
  291. }
  292. if (endian_conversion_required) {
  293. printf ("endian convert\n");
  294. ykd_state_endian_convert ((struct ykd_state *)msg_state);
  295. }
  296. /*
  297. * Set completion for source_addr's address
  298. */
  299. for (state_position = 0; state_position < state_received_confchg_entries; state_position++) {
  300. if (totemip_equal(source_addr, &state_received_process[state_position].addr)) {
  301. /*
  302. * State position contains the address of the state to modify
  303. * This may be used later by the other algorithms
  304. */
  305. state_received_process[state_position].received = 1;
  306. break;
  307. }
  308. }
  309. /*
  310. * Test if all nodes have submitted their state data
  311. */
  312. for (i = 0; i < state_received_confchg_entries; i++) {
  313. if (state_received_process[i].received == 0) {
  314. all_received = 0;
  315. }
  316. }
  317. switch (ykd_mode) {
  318. case YKD_MODE_SENDSTATE:
  319. /*
  320. * Copy state information for the sending processor
  321. */
  322. memcpy (&state_received_process[state_position].ykd_state,
  323. msg_state, sizeof (struct ykd_state));
  324. /*
  325. * Try to form a component
  326. */
  327. if (all_received) {
  328. for (i = 0; i < state_received_confchg_entries; i++) {
  329. state_received_process[i].received = 0;
  330. }
  331. ykd_mode = YKD_MODE_ATTEMPT;
  332. // TODO resolve optimizes for failure conditions during ykd calculation
  333. // resolve();
  334. compute();
  335. if (decide ()) {
  336. ykd_state.session_id = session_id_max + 1;
  337. memcpy (ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list,
  338. view_list, sizeof (struct totem_ip_address) * view_list_entries);
  339. ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list_entries = view_list_entries;
  340. ykd_state.ambiguous_sessions_entries += 1;
  341. ykd_attempt_send();
  342. }
  343. }
  344. break;
  345. case YKD_MODE_ATTEMPT:
  346. if (all_received) {
  347. log_printf (LOG_LEVEL_NOTICE,
  348. "This processor is within the primary component.\n");
  349. primary_designated = 1;
  350. ykd_primary_callback_fn (
  351. view_list,
  352. view_list_entries,
  353. primary_designated,
  354. &ykd_ring_id);
  355. memcpy (ykd_state.last_primary.member_list, view_list, sizeof (view_list));
  356. ykd_state.last_primary.member_list_entries = view_list_entries;
  357. ykd_state.last_primary.session_id = ykd_state.session_id;
  358. ykd_state.ambiguous_sessions_entries = 0;
  359. }
  360. break;
  361. }
  362. }
  363. int first_run = 1;
  364. static void ykd_confchg_fn (
  365. enum totem_configuration_type configuration_type,
  366. struct totem_ip_address *member_list, int member_list_entries,
  367. struct totem_ip_address *left_list, int left_list_entries,
  368. struct totem_ip_address *joined_list, int joined_list_entries,
  369. struct memb_ring_id *ring_id)
  370. {
  371. int i;
  372. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  373. return;
  374. }
  375. memcpy (&ykd_ring_id, ring_id, sizeof (struct memb_ring_id));
  376. if (first_run) {
  377. totemip_copy (&ykd_state.last_primary.member_list[0], this_ip);
  378. ykd_state.last_primary.member_list_entries = 1;
  379. ykd_state.last_primary.session_id = 0;
  380. first_run = 0;
  381. }
  382. memcpy (view_list, member_list,
  383. member_list_entries * sizeof (struct totem_ip_address));
  384. view_list_entries = member_list_entries;
  385. ykd_mode = YKD_MODE_SENDSTATE;
  386. primary_designated = 0;
  387. ykd_primary_callback_fn (
  388. view_list,
  389. view_list_entries,
  390. primary_designated,
  391. &ykd_ring_id);
  392. memset (&state_received_confchg, 0, sizeof (state_received_confchg));
  393. for (i = 0; i < member_list_entries; i++) {
  394. totemip_copy(&state_received_confchg[i].addr, &member_list[i]);
  395. state_received_confchg[i].received = 0;
  396. }
  397. memcpy (state_received_process, state_received_confchg,
  398. sizeof (state_received_confchg));
  399. state_received_confchg_entries = member_list_entries;
  400. state_received_process_entries = member_list_entries;
  401. ykd_state_send ();
  402. }
  403. struct totempg_group ykd_group = {
  404. .group = "ykd",
  405. .group_len = 3
  406. };
  407. int ykd_init (
  408. void (*primary_callback_fn) (
  409. struct totem_ip_address *view_list,
  410. int view_list_entries,
  411. int primary_designated,
  412. struct memb_ring_id *ring_id))
  413. {
  414. ykd_primary_callback_fn = primary_callback_fn;
  415. totempg_groups_initialize (
  416. &ykd_group_handle,
  417. ykd_deliver_fn,
  418. ykd_confchg_fn);
  419. totempg_groups_join (
  420. ykd_group_handle,
  421. &ykd_group,
  422. 1);
  423. ykd_state_init ();
  424. return (0);
  425. }
  426. /*
  427. * Returns 1 if this processor is in the primary
  428. */
  429. int ykd_primary (void) {
  430. return (primary_designated);
  431. }