sync.c 9.8 KB

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