sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <sys/ioctl.h>
  38. #include <netinet/in.h>
  39. #include <sys/uio.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <signal.h>
  46. #include <time.h>
  47. #include <unistd.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include "../include/saAis.h"
  51. #include "main.h"
  52. #include "sync.h"
  53. #include "totempg.h"
  54. #include "totemip.h"
  55. #include "totem.h"
  56. #include "vsf.h"
  57. #include "../lcr/lcr_ifact.h"
  58. #include "print.h"
  59. #define MESSAGE_REQ_SYNC_BARRIER 0
  60. struct barrier_data {
  61. unsigned int nodeid;
  62. int completed;
  63. };
  64. static struct memb_ring_id *sync_ring_id;
  65. static int vsf_none = 0;
  66. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  67. static struct sync_callbacks sync_callbacks;
  68. static int sync_processing = 0;
  69. static void (*sync_synchronization_completed) (void);
  70. static int sync_recovery_index = 0;
  71. static void *sync_callback_token_handle = 0;
  72. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  73. static int barrier_data_confchg_entries;
  74. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  75. static struct openais_vsf_iface_ver0 *vsf_iface;
  76. static int sync_barrier_send (struct memb_ring_id *ring_id);
  77. static int sync_start_process (enum totem_callback_token_type type, void *data);
  78. static void sync_service_init (struct memb_ring_id *ring_id);
  79. static int sync_service_process (enum totem_callback_token_type type, void *data);
  80. static void sync_deliver_fn (
  81. unsigned int nodeid,
  82. struct iovec *iovec,
  83. int iov_len,
  84. int endian_conversion_required);
  85. static void sync_confchg_fn (
  86. enum totem_configuration_type configuration_type,
  87. unsigned int *member_list, int member_list_entries,
  88. unsigned int *left_list, int left_list_entries,
  89. unsigned int *joined_list, int joined_list_entries,
  90. struct memb_ring_id *ring_id);
  91. static void sync_primary_callback_fn (
  92. unsigned int *view_list,
  93. int view_list_entries,
  94. int primary_designated,
  95. struct memb_ring_id *ring_id);
  96. static struct totempg_group sync_group = {
  97. .group = "sync",
  98. .group_len = 4
  99. };
  100. static totempg_groups_handle sync_group_handle;
  101. struct req_exec_sync_barrier_start {
  102. mar_req_header_t header;
  103. struct memb_ring_id ring_id;
  104. };
  105. /*
  106. * Send a barrier data structure
  107. */
  108. static int sync_barrier_send (struct memb_ring_id *ring_id)
  109. {
  110. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  111. struct iovec iovec;
  112. int res;
  113. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  114. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  115. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  116. sizeof (struct memb_ring_id));
  117. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  118. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  119. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  120. return (res);
  121. }
  122. void sync_start_init (struct memb_ring_id *ring_id)
  123. {
  124. totempg_callback_token_create (
  125. &sync_callback_token_handle,
  126. TOTEM_CALLBACK_TOKEN_SENT,
  127. 0, /* don't delete after callback */
  128. sync_start_process,
  129. (void *)ring_id);
  130. }
  131. static void sync_service_init (struct memb_ring_id *ring_id)
  132. {
  133. sync_callbacks.sync_init ();
  134. totempg_callback_token_destroy (&sync_callback_token_handle);
  135. /*
  136. * Create the token callback for the processing
  137. */
  138. totempg_callback_token_create (
  139. &sync_callback_token_handle,
  140. TOTEM_CALLBACK_TOKEN_SENT,
  141. 0, /* don't delete after callback */
  142. sync_service_process,
  143. (void *)ring_id);
  144. }
  145. static int sync_start_process (enum totem_callback_token_type type, void *data)
  146. {
  147. int res;
  148. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  149. res = sync_barrier_send (ring_id);
  150. if (res == 0) {
  151. /*
  152. * Delete the token callback for the barrier
  153. */
  154. totempg_callback_token_destroy (&sync_callback_token_handle);
  155. }
  156. return (0);
  157. }
  158. void sync_callbacks_load (void)
  159. {
  160. int res;
  161. // TODO rewrite this to get rid of the for (;;)
  162. for (;;) {
  163. res = sync_callbacks_retrieve (sync_recovery_index, &sync_callbacks);
  164. /*
  165. * No more service handlers have sync callbacks at this time
  166. */
  167. if (res == -1) {
  168. sync_processing = 0;
  169. break;
  170. }
  171. sync_recovery_index += 1;
  172. if (sync_callbacks.sync_init) {
  173. break;
  174. }
  175. }
  176. }
  177. static int sync_service_process (enum totem_callback_token_type type, void *data)
  178. {
  179. int res;
  180. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  181. /*
  182. * If process operation not from this ring id, then ignore it and stop
  183. * processing
  184. */
  185. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  186. return (0);
  187. }
  188. /*
  189. * If process returns 0, then its time to activate
  190. * and start the next service's synchronization
  191. */
  192. res = sync_callbacks.sync_process ();
  193. if (res != 0) {
  194. return (0);
  195. }
  196. totempg_callback_token_destroy (&sync_callback_token_handle);
  197. sync_start_init (ring_id);
  198. return (0);
  199. }
  200. int sync_register (
  201. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  202. void (*synchronization_completed) (void),
  203. char *vsf_type)
  204. {
  205. unsigned int res;
  206. unsigned int vsf_handle;
  207. void *vsf_iface_p;
  208. char openais_vsf_type[1024];
  209. log_init ("SYNC");
  210. res = totempg_groups_initialize (
  211. &sync_group_handle,
  212. sync_deliver_fn,
  213. sync_confchg_fn);
  214. if (res == -1) {
  215. log_printf (LOG_LEVEL_ERROR,
  216. "Couldn't initialize groups interface.\n");
  217. return (-1);
  218. }
  219. res = totempg_groups_join (
  220. sync_group_handle,
  221. &sync_group,
  222. 1);
  223. if (res == -1) {
  224. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  225. return (-1);
  226. }
  227. if (strcmp (vsf_type, "none") == 0) {
  228. log_printf (LOG_LEVEL_NOTICE,
  229. "Not using a virtual synchrony filter.\n");
  230. vsf_none = 1;
  231. } else {
  232. vsf_none = 0;
  233. sprintf (openais_vsf_type, "openais_vsf_%s", vsf_type);
  234. res = lcr_ifact_reference (
  235. &vsf_handle,
  236. openais_vsf_type,
  237. 0,
  238. &vsf_iface_p,
  239. 0);
  240. if (res == -1) {
  241. log_printf (LOG_LEVEL_NOTICE,
  242. "Couldn't load virtual synchrony filter %s\n",
  243. vsf_type);
  244. return (-1);
  245. }
  246. log_printf (LOG_LEVEL_NOTICE,
  247. "Using virtual synchrony filter %s\n", openais_vsf_type);
  248. vsf_iface = (struct openais_vsf_iface_ver0 *)vsf_iface_p;
  249. vsf_iface->init (sync_primary_callback_fn);
  250. }
  251. sync_callbacks_retrieve = callbacks_retrieve;
  252. sync_synchronization_completed = synchronization_completed;
  253. return (0);
  254. }
  255. static void sync_primary_callback_fn (
  256. unsigned int *view_list,
  257. int view_list_entries,
  258. int primary_designated,
  259. struct memb_ring_id *ring_id)
  260. {
  261. int i;
  262. if (primary_designated) {
  263. log_printf (LOG_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  264. } else {
  265. log_printf (LOG_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  266. return;
  267. }
  268. /*
  269. * Execute configuration change for synchronization service
  270. */
  271. sync_processing = 1;
  272. totempg_callback_token_destroy (&sync_callback_token_handle);
  273. sync_recovery_index = 0;
  274. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  275. for (i = 0; i < view_list_entries; i++) {
  276. barrier_data_confchg[i].nodeid = view_list[i];
  277. barrier_data_confchg[i].completed = 0;
  278. }
  279. memcpy (barrier_data_process, barrier_data_confchg,
  280. sizeof (barrier_data_confchg));
  281. barrier_data_confchg_entries = view_list_entries;
  282. sync_start_init (sync_ring_id);
  283. }
  284. static struct memb_ring_id deliver_ring_id;
  285. void sync_endian_convert (struct req_exec_sync_barrier_start *req_exec_sync_barrier_start)
  286. {
  287. /* XXX no swab on mar_req_header_t? */
  288. swab_memb_ring_id_t (&req_exec_sync_barrier_start->ring_id);
  289. }
  290. static void sync_deliver_fn (
  291. unsigned int nodeid,
  292. struct iovec *iovec,
  293. int iov_len,
  294. int endian_conversion_required)
  295. {
  296. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  297. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  298. int i;
  299. if (endian_conversion_required) {
  300. sync_endian_convert (req_exec_sync_barrier_start);
  301. }
  302. int barrier_completed = 1;
  303. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  304. sizeof (struct memb_ring_id));
  305. /*
  306. * Is this barrier from this configuration, if not, ignore it
  307. */
  308. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  309. sizeof (struct memb_ring_id)) != 0) {
  310. return;
  311. }
  312. /*
  313. * Set completion for source_addr's address
  314. */
  315. for (i = 0; i < barrier_data_confchg_entries; i++) {
  316. if (nodeid == barrier_data_process[i].nodeid) {
  317. barrier_data_process[i].completed = 1;
  318. log_printf (LOG_LEVEL_DEBUG,
  319. "Barrier Start Recieved From %d\n",
  320. barrier_data_process[i].nodeid);
  321. break;
  322. }
  323. }
  324. /*
  325. * Test if barrier is complete
  326. */
  327. for (i = 0; i < barrier_data_confchg_entries; i++) {
  328. log_printf (LOG_LEVEL_DEBUG,
  329. "Barrier completion status for nodeid %d = %d. \n",
  330. barrier_data_process[i].nodeid,
  331. barrier_data_process[i].completed);
  332. if (barrier_data_process[i].completed == 0) {
  333. barrier_completed = 0;
  334. }
  335. }
  336. if (barrier_completed) {
  337. log_printf (LOG_LEVEL_DEBUG,
  338. "Synchronization barrier completed\n");
  339. }
  340. /*
  341. * This sync is complete so activate and start next service sync
  342. */
  343. if (barrier_completed && sync_callbacks.sync_activate) {
  344. sync_callbacks.sync_activate ();
  345. log_printf (LOG_LEVEL_DEBUG,
  346. "Committing synchronization for (%s)\n",
  347. sync_callbacks.name);
  348. }
  349. /*
  350. * Start synchronization if the barrier has completed
  351. */
  352. if (barrier_completed) {
  353. memcpy (barrier_data_process, barrier_data_confchg,
  354. sizeof (barrier_data_confchg));
  355. sync_callbacks_load();
  356. /*
  357. * if sync service found, execute it
  358. */
  359. if (sync_processing && sync_callbacks.sync_init) {
  360. log_printf (LOG_LEVEL_DEBUG,
  361. "Synchronization actions starting for (%s)\n",
  362. sync_callbacks.name);
  363. sync_service_init (&deliver_ring_id);
  364. }
  365. }
  366. return;
  367. }
  368. static void sync_confchg_fn (
  369. enum totem_configuration_type configuration_type,
  370. unsigned int *member_list, int member_list_entries,
  371. unsigned int *left_list, int left_list_entries,
  372. unsigned int *joined_list, int joined_list_entries,
  373. struct memb_ring_id *ring_id)
  374. {
  375. sync_ring_id = ring_id;
  376. /*
  377. * If no virtual synchrony filter configured, then start
  378. * synchronization process
  379. */
  380. if (vsf_none == 1) {
  381. sync_primary_callback_fn (
  382. member_list,
  383. member_list_entries,
  384. 1,
  385. ring_id);
  386. }
  387. }
  388. int sync_in_process (void)
  389. {
  390. return (sync_processing);
  391. }
  392. int sync_primary_designated (void)
  393. {
  394. if (vsf_none == 1) {
  395. return (1);
  396. } else {
  397. return (vsf_iface->primary());
  398. }
  399. }