ykd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/sysinfo.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <signal.h>
  56. #include <sched.h>
  57. #include <time.h>
  58. #include "handlers.h"
  59. #include "print.h"
  60. #include "swab.h"
  61. #define LOG_SERVICE LOG_SERVICE_YKD
  62. enum ykd_header_values {
  63. YKD_HEADER_SENDSTATE = 0,
  64. YKD_HEADER_ATTEMPT = 1
  65. };
  66. enum ykd_mode {
  67. YKD_MODE_SENDSTATE = 0,
  68. YKD_MODE_ATTEMPT = 1
  69. };
  70. struct ykd_header {
  71. int id;
  72. };
  73. struct ykd_session {
  74. struct totem_ip_address member_list[PROCESSOR_COUNT_MAX];
  75. int member_list_entries;
  76. int session_id;
  77. };
  78. struct ykd_state {
  79. struct ykd_session last_primary;
  80. struct ykd_session last_formed[PROCESSOR_COUNT_MAX];
  81. int last_formed_entries;
  82. struct ykd_session ambiguous_sessions[PROCESSOR_COUNT_MAX];
  83. int ambiguous_sessions_entries;
  84. int session_id;
  85. };
  86. struct state_received {
  87. struct totem_ip_address addr;
  88. int received;
  89. struct ykd_state ykd_state;
  90. };
  91. struct ykd_state ykd_state;
  92. static totempg_groups_handle ykd_group_handle;
  93. static struct state_received state_received_confchg[PROCESSOR_COUNT_MAX];
  94. static int state_received_confchg_entries;
  95. static struct state_received state_received_process[PROCESSOR_COUNT_MAX];
  96. static int state_received_process_entries;
  97. static enum ykd_mode ykd_mode;
  98. static struct totem_ip_address view_list[PROCESSOR_COUNT_MAX];
  99. static int view_list_entries;
  100. static int session_id_max;
  101. static struct ykd_session *last_primary_max;
  102. static struct ykd_session ambiguous_sessions_max[PROCESSOR_COUNT_MAX];
  103. static int ambiguous_sessions_max_entries;
  104. static int primary_designated = 0;
  105. static struct memb_ring_id ykd_ring_id;
  106. static void (*ykd_primary_callback_fn) (
  107. struct totem_ip_address *view_list,
  108. int view_list_entries,
  109. int primary_designated,
  110. struct memb_ring_id *ring_id) = NULL;
  111. void ykd_state_init (void)
  112. {
  113. ykd_state.session_id = 0;
  114. ykd_state.last_formed_entries = 0;
  115. ykd_state.ambiguous_sessions_entries = 0;
  116. ykd_state.last_primary.session_id = 0;
  117. ykd_state.last_primary.member_list_entries = 0;
  118. }
  119. void ykd_state_send (void)
  120. {
  121. struct iovec iovec[2];
  122. struct ykd_header header;
  123. header.id = YKD_HEADER_SENDSTATE;
  124. iovec[0].iov_base = &header;
  125. iovec[0].iov_len = sizeof (struct ykd_header);
  126. iovec[1].iov_base = &ykd_state;
  127. iovec[1].iov_len = sizeof (struct ykd_state);
  128. totempg_groups_mcast_joined (ykd_group_handle, iovec, 2, TOTEMPG_AGREED);
  129. }
  130. void ykd_attempt_send (void)
  131. {
  132. struct iovec iovec;
  133. struct ykd_header header;
  134. header.id = YKD_HEADER_SENDSTATE;
  135. iovec.iov_base = &header;
  136. iovec.iov_len = sizeof (struct ykd_header);
  137. totempg_groups_mcast_joined (ykd_group_handle, &iovec, 1, TOTEMPG_AGREED);
  138. }
  139. void compute (void)
  140. {
  141. int i;
  142. int j;
  143. session_id_max = 0;
  144. last_primary_max = &state_received_process[0].ykd_state.last_primary;
  145. ambiguous_sessions_max_entries = 0;
  146. for (i = 0; i < state_received_process_entries; i++) {
  147. /*
  148. * Calculate maximum session id
  149. */
  150. if (state_received_process[i].ykd_state.session_id > session_id_max) {
  151. session_id_max = state_received_process[i].ykd_state.session_id;
  152. }
  153. /*
  154. * Calculate maximum primary id
  155. */
  156. if (state_received_process[i].ykd_state.last_primary.session_id > last_primary_max->session_id) {
  157. last_primary_max = &state_received_process[i].ykd_state.last_primary;
  158. }
  159. /*
  160. * generate the maximum ambiguous sessions list
  161. */
  162. for (j = 0; j < state_received_process[i].ykd_state.ambiguous_sessions_entries; j++) {
  163. if (state_received_process[i].ykd_state.ambiguous_sessions[j].session_id > last_primary_max->session_id) {
  164. memcpy (&ambiguous_sessions_max[ambiguous_sessions_max_entries],
  165. &state_received_process[i].ykd_state.ambiguous_sessions[j],
  166. sizeof (struct ykd_session));
  167. ambiguous_sessions_max_entries += 1;
  168. }
  169. }
  170. }
  171. }
  172. int subquorum (
  173. struct totem_ip_address *member_list,
  174. int member_list_entries,
  175. struct ykd_session *session)
  176. {
  177. int intersections = 0;
  178. int i;
  179. int j;
  180. for (i = 0; i < member_list_entries; i++) {
  181. for (j = 0; j < session->member_list_entries; j++) {
  182. if (totemip_equal (&member_list[i], &session->member_list[j])) {
  183. intersections += 1;
  184. }
  185. }
  186. }
  187. /*
  188. * even split
  189. */
  190. if (intersections == (session->member_list_entries - intersections)) {
  191. return (1);
  192. } else
  193. /*
  194. * majority split
  195. */
  196. if (intersections > (session->member_list_entries - intersections)) {
  197. return (1);
  198. }
  199. return (0);
  200. }
  201. int decide (void)
  202. {
  203. int i;
  204. /*
  205. * Determine if there is a subquorum
  206. */
  207. if (subquorum (view_list, view_list_entries, last_primary_max) == 0) {
  208. return (0);
  209. }
  210. for (i = 0; i < ambiguous_sessions_max_entries; i++) {
  211. if (subquorum (view_list, view_list_entries, &ambiguous_sessions_max[i]) == 0) {
  212. return (0);
  213. }
  214. }
  215. return (1);
  216. }
  217. static void ykd_deliver_fn (
  218. struct totem_ip_address *source_addr,
  219. struct iovec *iovec,
  220. int iov_len,
  221. int endian_conversion_required)
  222. {
  223. int all_received = 1;
  224. int state_position = 0;
  225. int i;
  226. char *msg_state = iovec->iov_base + sizeof (struct ykd_header);
  227. #ifdef COMPILE_OUT
  228. /*
  229. * Is this barrier from this configuration, if not, ignore it
  230. */
  231. if (memcmp (&req_exec_sync_barrier_start->ring_id, ykd_ring_id,
  232. sizeof (struct memb_ring_id)) != 0) {
  233. */
  234. return (0);
  235. #endif
  236. /*
  237. * Set completion for source_addr's address
  238. */
  239. for (state_position = 0; state_position < state_received_confchg_entries; state_position++) {
  240. if (totemip_equal(source_addr, &state_received_process[state_position].addr)) {
  241. /*
  242. * State position contains the address of the state to modify
  243. * This may be used later by the other algorithms
  244. */
  245. state_received_process[state_position].received = 1;
  246. break;
  247. }
  248. }
  249. /*
  250. * Test if all nodes have submitted their state data
  251. */
  252. for (i = 0; i < state_received_confchg_entries; i++) {
  253. if (state_received_process[i].received == 0) {
  254. all_received = 0;
  255. }
  256. }
  257. switch (ykd_mode) {
  258. case YKD_MODE_SENDSTATE:
  259. /*
  260. * Copy state information for the sending processor
  261. */
  262. memcpy (&state_received_process[state_position].ykd_state,
  263. msg_state, sizeof (struct ykd_state));
  264. /*
  265. * Try to form a component
  266. */
  267. if (all_received) {
  268. for (i = 0; i < state_received_confchg_entries; i++) {
  269. state_received_process[i].received = 0;
  270. }
  271. ykd_mode = YKD_MODE_ATTEMPT;
  272. // TODO resolve optimizes for failure conditions during ykd calculation
  273. // resolve();
  274. compute();
  275. if (decide ()) {
  276. ykd_state.session_id = session_id_max + 1;
  277. memcpy (ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list,
  278. view_list, sizeof (struct totem_ip_address) * view_list_entries);
  279. ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list_entries = view_list_entries;
  280. ykd_state.ambiguous_sessions_entries += 1;
  281. ykd_attempt_send();
  282. }
  283. }
  284. break;
  285. case YKD_MODE_ATTEMPT:
  286. if (all_received) {
  287. log_printf (LOG_LEVEL_NOTICE,
  288. "This processor is within the primary component.\n");
  289. primary_designated = 1;
  290. ykd_primary_callback_fn (
  291. view_list,
  292. view_list_entries,
  293. primary_designated,
  294. &ykd_ring_id);
  295. memcpy (ykd_state.last_primary.member_list, view_list, sizeof (view_list));
  296. ykd_state.last_primary.member_list_entries = view_list_entries;
  297. ykd_state.last_primary.session_id = ykd_state.session_id;
  298. ykd_state.ambiguous_sessions_entries = 0;
  299. }
  300. break;
  301. }
  302. }
  303. int first_run = 1;
  304. static void ykd_confchg_fn (
  305. enum totem_configuration_type configuration_type,
  306. struct totem_ip_address *member_list, int member_list_entries,
  307. struct totem_ip_address *left_list, int left_list_entries,
  308. struct totem_ip_address *joined_list, int joined_list_entries,
  309. struct memb_ring_id *ring_id)
  310. {
  311. int i;
  312. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  313. return;
  314. }
  315. memcpy (&ykd_ring_id, ring_id, sizeof (struct memb_ring_id));
  316. if (first_run) {
  317. totemip_copy (&ykd_state.last_primary.member_list[0], this_ip);
  318. ykd_state.last_primary.member_list_entries = 1;
  319. ykd_state.last_primary.session_id = 0;
  320. first_run = 0;
  321. }
  322. memcpy (view_list, member_list,
  323. member_list_entries * sizeof (struct totem_ip_address));
  324. view_list_entries = member_list_entries;
  325. ykd_mode = YKD_MODE_SENDSTATE;
  326. primary_designated = 0;
  327. ykd_primary_callback_fn (
  328. view_list,
  329. view_list_entries,
  330. primary_designated,
  331. &ykd_ring_id);
  332. memset (&state_received_confchg, 0, sizeof (state_received_confchg));
  333. for (i = 0; i < member_list_entries; i++) {
  334. totemip_copy(&state_received_confchg[i].addr, &member_list[i]);
  335. state_received_confchg[i].received = 0;
  336. }
  337. memcpy (state_received_process, state_received_confchg,
  338. sizeof (state_received_confchg));
  339. state_received_confchg_entries = member_list_entries;
  340. state_received_process_entries = member_list_entries;
  341. ykd_state_send ();
  342. }
  343. struct totempg_group ykd_group = {
  344. .group = "ykd",
  345. .group_len = 3
  346. };
  347. int ykd_init (
  348. void (*primary_callback_fn) (
  349. struct totem_ip_address *view_list,
  350. int view_list_entries,
  351. int primary_designated,
  352. struct memb_ring_id *ring_id))
  353. {
  354. ykd_primary_callback_fn = primary_callback_fn;
  355. totempg_groups_initialize (
  356. &ykd_group_handle,
  357. ykd_deliver_fn,
  358. ykd_confchg_fn);
  359. totempg_groups_join (
  360. ykd_group_handle,
  361. &ykd_group,
  362. 1);
  363. ykd_state_init ();
  364. return (0);
  365. }
  366. /*
  367. * Returns 1 if this processor is in the primary
  368. */
  369. int ykd_primary (void) {
  370. return (primary_designated);
  371. }