vsf_ykd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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 "main.h"
  57. #include "print.h"
  58. #include "swab.h"
  59. #include "vsf.h"
  60. #include "../lcr/lcr_comp.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_attempt_send_callback_token_handle = 0;
  107. static void *ykd_state_send_callback_token_handle = 0;
  108. static void (*ykd_primary_callback_fn) (
  109. struct totem_ip_address *view_list,
  110. int view_list_entries,
  111. int primary_designated,
  112. struct memb_ring_id *ring_id) = NULL;
  113. void ykd_state_init (void)
  114. {
  115. ykd_state.session_id = 0;
  116. ykd_state.last_formed_entries = 0;
  117. ykd_state.ambiguous_sessions_entries = 0;
  118. ykd_state.last_primary.session_id = 0;
  119. ykd_state.last_primary.member_list_entries = 0;
  120. }
  121. static int ykd_state_send_msg (enum totem_callback_token_type type, void *context)
  122. {
  123. struct iovec iovec[2];
  124. struct ykd_header header;
  125. int res;
  126. header.id = YKD_HEADER_SENDSTATE;
  127. iovec[0].iov_base = &header;
  128. iovec[0].iov_len = sizeof (struct ykd_header);
  129. iovec[1].iov_base = &ykd_state;
  130. iovec[1].iov_len = sizeof (struct ykd_state);
  131. res = totempg_groups_mcast_joined (ykd_group_handle, iovec, 2,
  132. TOTEMPG_AGREED);
  133. return (res);
  134. }
  135. static void ykd_state_send (void)
  136. {
  137. totempg_callback_token_create (
  138. &ykd_state_send_callback_token_handle,
  139. TOTEM_CALLBACK_TOKEN_SENT,
  140. 1, /* delete after callback */
  141. ykd_state_send_msg,
  142. NULL);
  143. }
  144. static int ykd_attempt_send_msg (enum totem_callback_token_type type, void *context)
  145. {
  146. struct iovec iovec;
  147. struct ykd_header header;
  148. int res;
  149. header.id = YKD_HEADER_SENDSTATE;
  150. iovec.iov_base = &header;
  151. iovec.iov_len = sizeof (struct ykd_header);
  152. res = totempg_groups_mcast_joined (ykd_group_handle, &iovec, 1,
  153. TOTEMPG_AGREED);
  154. return (res);
  155. }
  156. static void ykd_attempt_send (void)
  157. {
  158. totempg_callback_token_create (
  159. &ykd_attempt_send_callback_token_handle,
  160. TOTEM_CALLBACK_TOKEN_SENT,
  161. 1, /* delete after callback */
  162. ykd_attempt_send_msg,
  163. NULL);
  164. }
  165. static void compute (void)
  166. {
  167. int i;
  168. int j;
  169. session_id_max = 0;
  170. last_primary_max = &state_received_process[0].ykd_state.last_primary;
  171. ambiguous_sessions_max_entries = 0;
  172. for (i = 0; i < state_received_process_entries; i++) {
  173. /*
  174. * Calculate maximum session id
  175. */
  176. if (state_received_process[i].ykd_state.session_id > session_id_max) {
  177. session_id_max = state_received_process[i].ykd_state.session_id;
  178. }
  179. /*
  180. * Calculate maximum primary id
  181. */
  182. if (state_received_process[i].ykd_state.last_primary.session_id > last_primary_max->session_id) {
  183. last_primary_max = &state_received_process[i].ykd_state.last_primary;
  184. }
  185. /*
  186. * generate the maximum ambiguous sessions list
  187. */
  188. for (j = 0; j < state_received_process[i].ykd_state.ambiguous_sessions_entries; j++) {
  189. if (state_received_process[i].ykd_state.ambiguous_sessions[j].session_id > last_primary_max->session_id) {
  190. memcpy (&ambiguous_sessions_max[ambiguous_sessions_max_entries],
  191. &state_received_process[i].ykd_state.ambiguous_sessions[j],
  192. sizeof (struct ykd_session));
  193. ambiguous_sessions_max_entries += 1;
  194. }
  195. }
  196. }
  197. }
  198. static int subquorum (
  199. struct totem_ip_address *member_list,
  200. int member_list_entries,
  201. struct ykd_session *session)
  202. {
  203. int intersections = 0;
  204. int i;
  205. int j;
  206. for (i = 0; i < member_list_entries; i++) {
  207. for (j = 0; j < session->member_list_entries; j++) {
  208. if (totemip_equal (&member_list[i], &session->member_list[j])) {
  209. intersections += 1;
  210. }
  211. }
  212. }
  213. /*
  214. * even split
  215. */
  216. if (intersections == (session->member_list_entries - intersections)) {
  217. return (1);
  218. } else
  219. /*
  220. * majority split
  221. */
  222. if (intersections > (session->member_list_entries - intersections)) {
  223. return (1);
  224. }
  225. return (0);
  226. }
  227. static int decide (void)
  228. {
  229. int i;
  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. for (i = 0; i < ambiguous_sessions_max_entries; i++) {
  237. if (subquorum (view_list, view_list_entries, &ambiguous_sessions_max[i]) == 0) {
  238. return (0);
  239. }
  240. }
  241. return (1);
  242. }
  243. static void ykd_session_endian_convert (struct ykd_session *ykd_session)
  244. {
  245. int i;
  246. ykd_session->member_list_entries = swab32 (ykd_session->member_list_entries);
  247. ykd_session->session_id = swab32 (ykd_session->session_id);
  248. for (i = 0; i < ykd_session->member_list_entries; i++) {
  249. totemip_copy_endian_convert (&ykd_session->member_list[i], &ykd_session->member_list[i]);
  250. }
  251. }
  252. static void ykd_state_endian_convert (struct ykd_state *ykd_state)
  253. {
  254. int i;
  255. ykd_session_endian_convert (&ykd_state->last_primary);
  256. ykd_state->last_formed_entries = swab32 (ykd_state->last_formed_entries);
  257. ykd_state->ambiguous_sessions_entries = swab32 (ykd_state->ambiguous_sessions_entries);
  258. ykd_state->session_id = swab32 (ykd_state->session_id);
  259. for (i = 0; i < ykd_state->last_formed_entries; i++) {
  260. ykd_session_endian_convert (&ykd_state->last_formed[i]);
  261. }
  262. for (i = 0; i < ykd_state->ambiguous_sessions_entries; i++) {
  263. ykd_session_endian_convert (&ykd_state->ambiguous_sessions[i]);
  264. }
  265. }
  266. static void ykd_deliver_fn (
  267. struct totem_ip_address *source_addr,
  268. struct iovec *iovec,
  269. int iov_len,
  270. int endian_conversion_required)
  271. {
  272. int all_received = 1;
  273. int state_position = 0;
  274. int i;
  275. char *msg_state = iovec->iov_base + sizeof (struct ykd_header);
  276. /*
  277. * If this is a localhost address, this node is always primary
  278. */
  279. if (totemip_localhost_check (source_addr)) {
  280. log_printf (LOG_LEVEL_NOTICE,
  281. "This processor is within the primary component.\n");
  282. primary_designated = 1;
  283. ykd_primary_callback_fn (
  284. view_list,
  285. view_list_entries,
  286. primary_designated,
  287. &ykd_ring_id);
  288. return;
  289. }
  290. if (endian_conversion_required) {
  291. ykd_state_endian_convert ((struct ykd_state *)msg_state);
  292. }
  293. /*
  294. * Set completion for source_addr's address
  295. */
  296. for (state_position = 0; state_position < state_received_confchg_entries; state_position++) {
  297. if (totemip_equal(source_addr, &state_received_process[state_position].addr)) {
  298. /*
  299. * State position contains the address of the state to modify
  300. * This may be used later by the other algorithms
  301. */
  302. state_received_process[state_position].received = 1;
  303. break;
  304. }
  305. }
  306. /*
  307. * Test if all nodes have submitted their state data
  308. */
  309. for (i = 0; i < state_received_confchg_entries; i++) {
  310. if (state_received_process[i].received == 0) {
  311. all_received = 0;
  312. }
  313. }
  314. switch (ykd_mode) {
  315. case YKD_MODE_SENDSTATE:
  316. /*
  317. * Copy state information for the sending processor
  318. */
  319. memcpy (&state_received_process[state_position].ykd_state,
  320. msg_state, sizeof (struct ykd_state));
  321. /*
  322. * Try to form a component
  323. */
  324. if (all_received) {
  325. for (i = 0; i < state_received_confchg_entries; i++) {
  326. state_received_process[i].received = 0;
  327. }
  328. ykd_mode = YKD_MODE_ATTEMPT;
  329. // TODO resolve optimizes for failure conditions during ykd calculation
  330. // resolve();
  331. compute();
  332. if (decide ()) {
  333. ykd_state.session_id = session_id_max + 1;
  334. memcpy (ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list,
  335. view_list, sizeof (struct totem_ip_address) * view_list_entries);
  336. ykd_state.ambiguous_sessions[ykd_state.ambiguous_sessions_entries].member_list_entries = view_list_entries;
  337. ykd_state.ambiguous_sessions_entries += 1;
  338. ykd_attempt_send();
  339. }
  340. }
  341. break;
  342. case YKD_MODE_ATTEMPT:
  343. if (all_received) {
  344. log_printf (LOG_LEVEL_NOTICE,
  345. "This processor is within the primary component.\n");
  346. primary_designated = 1;
  347. ykd_primary_callback_fn (
  348. view_list,
  349. view_list_entries,
  350. primary_designated,
  351. &ykd_ring_id);
  352. memcpy (ykd_state.last_primary.member_list, view_list, sizeof (view_list));
  353. ykd_state.last_primary.member_list_entries = view_list_entries;
  354. ykd_state.last_primary.session_id = ykd_state.session_id;
  355. ykd_state.ambiguous_sessions_entries = 0;
  356. }
  357. break;
  358. }
  359. }
  360. int first_run = 1;
  361. static void ykd_confchg_fn (
  362. enum totem_configuration_type configuration_type,
  363. struct totem_ip_address *member_list, int member_list_entries,
  364. struct totem_ip_address *left_list, int left_list_entries,
  365. struct totem_ip_address *joined_list, int joined_list_entries,
  366. struct memb_ring_id *ring_id)
  367. {
  368. int i;
  369. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  370. return;
  371. }
  372. memcpy (&ykd_ring_id, ring_id, sizeof (struct memb_ring_id));
  373. if (first_run) {
  374. totemip_copy (&ykd_state.last_primary.member_list[0], this_ip);
  375. ykd_state.last_primary.member_list_entries = 1;
  376. ykd_state.last_primary.session_id = 0;
  377. first_run = 0;
  378. }
  379. memcpy (view_list, member_list,
  380. member_list_entries * sizeof (struct totem_ip_address));
  381. view_list_entries = member_list_entries;
  382. ykd_mode = YKD_MODE_SENDSTATE;
  383. primary_designated = 0;
  384. ykd_primary_callback_fn (
  385. view_list,
  386. view_list_entries,
  387. primary_designated,
  388. &ykd_ring_id);
  389. memset (&state_received_confchg, 0, sizeof (state_received_confchg));
  390. for (i = 0; i < member_list_entries; i++) {
  391. totemip_copy(&state_received_confchg[i].addr, &member_list[i]);
  392. state_received_confchg[i].received = 0;
  393. }
  394. memcpy (state_received_process, state_received_confchg,
  395. sizeof (state_received_confchg));
  396. state_received_confchg_entries = member_list_entries;
  397. state_received_process_entries = member_list_entries;
  398. ykd_state_send ();
  399. }
  400. struct totempg_group ykd_group = {
  401. .group = "ykd",
  402. .group_len = 3
  403. };
  404. static int ykd_init (
  405. void (*primary_callback_fn) (
  406. struct totem_ip_address *view_list,
  407. int view_list_entries,
  408. int primary_designated,
  409. struct memb_ring_id *ring_id))
  410. {
  411. ykd_primary_callback_fn = primary_callback_fn;
  412. totempg_groups_initialize (
  413. &ykd_group_handle,
  414. ykd_deliver_fn,
  415. ykd_confchg_fn);
  416. totempg_groups_join (
  417. ykd_group_handle,
  418. &ykd_group,
  419. 1);
  420. ykd_state_init ();
  421. return (0);
  422. }
  423. /*
  424. * Returns 1 if this processor is in the primary
  425. */
  426. static int ykd_primary (void) {
  427. return (primary_designated);
  428. }
  429. /*
  430. * lcrso object definition
  431. */
  432. static struct openais_vsf_iface_ver0 vsf_ykd_iface_ver0 = {
  433. .init = ykd_init,
  434. .primary = ykd_primary
  435. };
  436. static struct lcr_iface openais_vsf_ykd_ver0[1] = {
  437. {
  438. .name = "openais_vsf_ykd",
  439. .version = 0,
  440. .versions_replace = 0,
  441. .versions_replace_count = 0,
  442. .dependencies = 0,
  443. .dependency_count = 0,
  444. .constructor = NULL,
  445. .destructor = NULL,
  446. .interfaces = (void **)(void *)&vsf_ykd_iface_ver0,
  447. }
  448. };
  449. static struct lcr_comp vsf_ykd_comp_ver0 = {
  450. .iface_count = 1,
  451. .ifaces = openais_vsf_ykd_ver0
  452. };
  453. __attribute__ ((constructor)) static void vsf_ykd_comp_register (void) {
  454. lcr_component_register (&vsf_ykd_comp_ver0);
  455. }