sync.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "ykd.h"
  56. #include "print.h"
  57. #include "swab.h"
  58. #define LOG_SERVICE LOG_SERVICE_SYNC
  59. #define MESSAGE_REQ_SYNC_BARRIER 0
  60. struct barrier_data {
  61. struct totem_ip_address addr;
  62. int completed;
  63. };
  64. static struct memb_ring_id *sync_ring_id;
  65. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  66. static struct sync_callbacks sync_callbacks;
  67. static int sync_processing = 0;
  68. static int sync_callback_loaded = 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 int sync_barrier_send (struct memb_ring_id *ring_id);
  76. static int sync_start_process (enum totem_callback_token_type type, void *data);
  77. static void sync_service_init (struct memb_ring_id *ring_id);
  78. static int sync_service_process (enum totem_callback_token_type type, void *data);
  79. static void sync_deliver_fn (
  80. struct totem_ip_address *source_addr,
  81. struct iovec *iovec,
  82. int iov_len,
  83. int endian_conversion_required);
  84. void sync_primary_callback_fn (
  85. struct totem_ip_address *view_list,
  86. int view_list_entries,
  87. int primary_designated,
  88. struct memb_ring_id *ring_id);
  89. static struct totempg_group sync_group = {
  90. .group = "sync",
  91. .group_len = 4
  92. };
  93. static totempg_groups_handle sync_group_handle;
  94. struct req_exec_sync_barrier_start {
  95. struct req_header header;
  96. struct memb_ring_id ring_id;
  97. };
  98. /*
  99. * Send a barrier data structure
  100. */
  101. static int sync_barrier_send (struct memb_ring_id *ring_id)
  102. {
  103. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  104. struct iovec iovec;
  105. int res;
  106. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  107. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  108. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  109. sizeof (struct memb_ring_id));
  110. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  111. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  112. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  113. return (res);
  114. }
  115. void sync_start_init (struct memb_ring_id *ring_id)
  116. {
  117. totempg_callback_token_create (
  118. &sync_callback_token_handle,
  119. TOTEM_CALLBACK_TOKEN_SENT,
  120. 0, /* don't delete after callback */
  121. sync_start_process,
  122. (void *)ring_id);
  123. }
  124. static void sync_service_init (struct memb_ring_id *ring_id)
  125. {
  126. // AA
  127. sync_callbacks.sync_init ();
  128. totempg_callback_token_destroy (&sync_callback_token_handle);
  129. /*
  130. * Create the token callback for the processing
  131. */
  132. totempg_callback_token_create (
  133. &sync_callback_token_handle,
  134. TOTEM_CALLBACK_TOKEN_SENT,
  135. 0, /* don't delete after callback */
  136. sync_service_process,
  137. (void *)ring_id);
  138. }
  139. static int sync_start_process (enum totem_callback_token_type type, void *data)
  140. {
  141. int res;
  142. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  143. res = sync_barrier_send (ring_id);
  144. if (res == 0) {
  145. /*
  146. * Delete the token callback for the barrier
  147. */
  148. totempg_callback_token_destroy (&sync_callback_token_handle);
  149. }
  150. return (0);
  151. }
  152. void sync_callbacks_load (void)
  153. {
  154. int res;
  155. // TODO rewrite this to get rid of the for (;;)
  156. for (;;) {
  157. res = sync_callbacks_retrieve (sync_recovery_index, &sync_callbacks);
  158. /*
  159. * No more service handlers have sync callbacks at this tie
  160. ` */
  161. if (res == -1) {
  162. sync_processing = 0;
  163. break;
  164. }
  165. sync_recovery_index += 1;
  166. if (sync_callbacks.sync_init != NULL) {
  167. sync_callback_loaded = 1;
  168. break;
  169. }
  170. }
  171. }
  172. static int sync_service_process (enum totem_callback_token_type type, void *data)
  173. {
  174. int res;
  175. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  176. /*
  177. * If process returns 0, then its time to activate
  178. * and start the next service's synchronization
  179. */
  180. res = sync_callbacks.sync_process ();
  181. if (res != 0) {
  182. return (0);
  183. }
  184. /*
  185. * This sync is complete so activate and start next service sync
  186. */
  187. sync_callbacks.sync_activate ();
  188. totempg_callback_token_destroy (&sync_callback_token_handle);
  189. log_printf (LOG_LEVEL_NOTICE,"Synchronization actions done for (%s)\n",sync_callbacks.name);
  190. sync_callbacks_load();
  191. /*
  192. * if sync service found, execute it
  193. */
  194. if (sync_processing && sync_callbacks.sync_init) {
  195. sync_start_init (ring_id);
  196. }
  197. else {
  198. sync_callback_loaded = 0;
  199. }
  200. return (0);
  201. }
  202. void sync_register (
  203. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  204. void (*synchronization_completed) (void))
  205. {
  206. totempg_groups_initialize (
  207. &sync_group_handle,
  208. sync_deliver_fn,
  209. NULL);
  210. totempg_groups_join (
  211. sync_group_handle,
  212. &sync_group,
  213. 1);
  214. ykd_init (sync_primary_callback_fn);
  215. sync_callbacks_retrieve = callbacks_retrieve;
  216. sync_synchronization_completed = synchronization_completed;
  217. }
  218. void sync_primary_callback_fn (
  219. struct totem_ip_address *view_list,
  220. int view_list_entries,
  221. int primary_designated,
  222. struct memb_ring_id *ring_id)
  223. {
  224. int i;
  225. if (primary_designated) {
  226. log_printf (LOG_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  227. } else {
  228. log_printf (LOG_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  229. return;
  230. }
  231. /*
  232. * Execute configuration change for synchronization service
  233. */
  234. sync_processing = 1;
  235. totempg_callback_token_destroy (&sync_callback_token_handle);
  236. sync_ring_id = ring_id;
  237. sync_recovery_index = 0;
  238. sync_callback_loaded = 0;
  239. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  240. for (i = 0; i < view_list_entries; i++) {
  241. totemip_copy(&barrier_data_confchg[i].addr, &view_list[i]);
  242. barrier_data_confchg[i].completed = 0;
  243. }
  244. memcpy (barrier_data_process, barrier_data_confchg,
  245. sizeof (barrier_data_confchg));
  246. barrier_data_confchg_entries = view_list_entries;
  247. sync_start_init (ring_id);
  248. }
  249. static struct memb_ring_id deliver_ring_id;
  250. void sync_endian_convert (struct req_exec_sync_barrier_start *req_exec_sync_barrier_start)
  251. {
  252. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  253. &req_exec_sync_barrier_start->ring_id.rep);
  254. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  255. }
  256. void sync_deliver_fn (
  257. struct totem_ip_address *source_addr,
  258. struct iovec *iovec,
  259. int iov_len,
  260. int endian_conversion_required)
  261. {
  262. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  263. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  264. int i;
  265. if (endian_conversion_required) {
  266. sync_endian_convert (req_exec_sync_barrier_start);
  267. }
  268. int barrier_completed = 1;
  269. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  270. sizeof (struct memb_ring_id));
  271. /*
  272. * Is this barrier from this configuration, if not, ignore it
  273. */
  274. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  275. sizeof (struct memb_ring_id)) != 0) {
  276. return;
  277. }
  278. /*
  279. * Set completion for source_addr's address
  280. */
  281. for (i = 0; i < barrier_data_confchg_entries; i++) {
  282. if (totemip_equal(source_addr, &barrier_data_process[i].addr)) {
  283. barrier_data_process[i].completed = 1;
  284. log_printf (LOG_LEVEL_DEBUG,"Barrier Start Recieved From %s\n", totemip_print(&barrier_data_process[i].addr));
  285. break;
  286. }
  287. }
  288. /*
  289. * Test if barrier is complete
  290. */
  291. for (i = 0; i < barrier_data_confchg_entries; i++) {
  292. log_printf (LOG_LEVEL_DEBUG,"Barrier completion status for %s = %d. \n",
  293. totemip_print(&barrier_data_process[i].addr),
  294. barrier_data_process[i].completed);
  295. if (barrier_data_process[i].completed == 0) {
  296. barrier_completed = 0;
  297. }
  298. }
  299. if (barrier_completed) {
  300. log_printf (LOG_LEVEL_NOTICE,
  301. "Synchronization barrier completed\n");
  302. }
  303. /*
  304. * Start synchronization if the barrier has completed
  305. */
  306. if (barrier_completed) {
  307. memcpy (barrier_data_process, barrier_data_confchg,
  308. sizeof (barrier_data_confchg));
  309. if ( sync_callback_loaded == 0 ) {
  310. sync_callbacks_load();
  311. }
  312. /*
  313. * if sync service found, execute it
  314. */
  315. if (sync_processing && sync_callbacks.sync_init) {
  316. log_printf (LOG_LEVEL_NOTICE,"Synchronization actions starting for (%s)\n",sync_callbacks.name);
  317. sync_service_init (&deliver_ring_id);
  318. }
  319. }
  320. return;
  321. }
  322. int sync_in_process (void)
  323. {
  324. return (sync_processing);
  325. }