sync.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2005 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/sysinfo.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <sys/uio.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <signal.h>
  47. #include <time.h>
  48. #include <unistd.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include "../include/saAis.h"
  52. #include "main.h"
  53. #include "sync.h"
  54. #include "totempg.h"
  55. #include "totempg.h"
  56. #include "print.h"
  57. #define LOG_SERVICE LOG_SERVICE_SYNC
  58. struct barrier_data {
  59. struct totem_ip_address addr;
  60. int completed;
  61. };
  62. static struct memb_ring_id *sync_ring_id;
  63. static struct sync_callbacks *sync_callbacks;
  64. static int sync_callback_count;
  65. static int sync_processing = 0;
  66. static void (*sync_synchronization_completed) (void);
  67. static int sync_recovery_index = 0;
  68. static void *sync_callback_token_handle = 0;
  69. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  70. static int barrier_data_confchg_entries;
  71. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  72. static int sync_barrier_send (struct memb_ring_id *ring_id);
  73. static int sync_start_process (enum totem_callback_token_type type, void *data);
  74. static void sync_service_init (struct memb_ring_id *ring_id);
  75. static int sync_service_process (enum totem_callback_token_type type, void *data);
  76. struct req_exec_sync_barrier_start {
  77. struct req_header header;
  78. struct memb_ring_id ring_id;
  79. };
  80. /*
  81. * Send a barrier data structure
  82. */
  83. static int sync_barrier_send (struct memb_ring_id *ring_id)
  84. {
  85. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  86. struct iovec iovec;
  87. int res;
  88. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  89. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_EXEC_SYNC_BARRIER;
  90. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  91. sizeof (struct memb_ring_id));
  92. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  93. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  94. res = totempg_mcast (&iovec, 1, TOTEMPG_AGREED);
  95. return (res);
  96. }
  97. void sync_start_init (struct memb_ring_id *ring_id)
  98. {
  99. totempg_callback_token_create (
  100. &sync_callback_token_handle,
  101. TOTEM_CALLBACK_TOKEN_SENT,
  102. 0, /* don't delete after callback */
  103. sync_start_process,
  104. (void *)ring_id);
  105. }
  106. static void sync_service_init (struct memb_ring_id *ring_id)
  107. {
  108. sync_callbacks[sync_recovery_index].sync_init ();
  109. totempg_callback_token_destroy (&sync_callback_token_handle);
  110. /*
  111. * Create the token callback for the processing
  112. */
  113. totempg_callback_token_create (
  114. &sync_callback_token_handle,
  115. TOTEM_CALLBACK_TOKEN_SENT,
  116. 0, /* don't delete after callback */
  117. sync_service_process,
  118. (void *)ring_id);
  119. }
  120. static int sync_start_process (enum totem_callback_token_type type, void *data)
  121. {
  122. int res;
  123. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  124. res = sync_barrier_send (ring_id);
  125. if (res == 0) {
  126. /*
  127. * Delete the token callback for the barrier
  128. */
  129. totempg_callback_token_destroy (&sync_callback_token_handle);
  130. }
  131. return (0);
  132. }
  133. static int sync_service_process (enum totem_callback_token_type type, void *data)
  134. {
  135. int res;
  136. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  137. /*
  138. * If process returns 0, then its time to activate
  139. * and start the next service's synchronization
  140. */
  141. res = sync_callbacks[sync_recovery_index].sync_process ();
  142. if (res != 0) {
  143. return (0);
  144. }
  145. /*
  146. * This sync is complete so activate and start next service sync
  147. */
  148. sync_callbacks[sync_recovery_index].sync_activate ();
  149. totempg_callback_token_destroy (&sync_callback_token_handle);
  150. sync_recovery_index += 1;
  151. if (sync_recovery_index > sync_callback_count) {
  152. sync_processing = 0;
  153. } else {
  154. sync_start_init (ring_id);
  155. }
  156. return (0);
  157. }
  158. void sync_register (
  159. struct sync_callbacks *callbacks,
  160. int callback_count,
  161. void (*synchronization_completed) (void))
  162. {
  163. sync_callbacks = callbacks;
  164. sync_callback_count = callback_count;
  165. sync_synchronization_completed = synchronization_completed;
  166. }
  167. void sync_confchg_fn (
  168. enum totem_configuration_type configuration_type,
  169. struct totem_ip_address *member_list, int member_list_entries,
  170. struct totem_ip_address *left_list, int left_list_entries,
  171. struct totem_ip_address *joined_list, int joined_list_entries,
  172. struct memb_ring_id *ring_id)
  173. {
  174. int i;
  175. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  176. return;
  177. }
  178. sync_processing = 1;
  179. totempg_callback_token_destroy (&sync_callback_token_handle);
  180. sync_ring_id = ring_id;
  181. sync_recovery_index = 0;
  182. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  183. for (i = 0; i < member_list_entries; i++) {
  184. totemip_copy(&barrier_data_confchg[i].addr, &member_list[i]);
  185. barrier_data_confchg[i].completed = 0;
  186. }
  187. memcpy (barrier_data_process, barrier_data_confchg,
  188. sizeof (barrier_data_confchg));
  189. barrier_data_confchg_entries = member_list_entries;
  190. sync_start_init (ring_id);
  191. }
  192. static struct memb_ring_id deliver_ring_id;
  193. int sync_deliver_fn (void *msg, struct totem_ip_address *source_addr,
  194. int endian_conversion_needed)
  195. {
  196. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  197. (struct req_exec_sync_barrier_start *)msg;
  198. int i;
  199. int barrier_completed = 1;
  200. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  201. sizeof (struct memb_ring_id));
  202. /*
  203. * Is this barrier from this configuration, if not, ignore it
  204. */
  205. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  206. sizeof (struct memb_ring_id)) != 0) {
  207. return (0);
  208. }
  209. /*
  210. * Set completion for source_addr's address
  211. */
  212. for (i = 0; i < barrier_data_confchg_entries; i++) {
  213. if (totemip_equal(source_addr, &barrier_data_process[i].addr)) {
  214. barrier_data_process[i].completed = 1;
  215. break;
  216. }
  217. }
  218. /*
  219. * Test if barrier is complete
  220. */
  221. for (i = 0; i < barrier_data_confchg_entries; i++) {
  222. if (barrier_data_process[i].completed == 0) {
  223. barrier_completed = 0;
  224. }
  225. }
  226. if (barrier_completed) {
  227. log_printf (LOG_LEVEL_NOTICE,
  228. "Synchronization barrier completed\n");
  229. }
  230. /*
  231. * Start synchronization if the barrier has completed
  232. */
  233. if (barrier_completed) {
  234. memcpy (barrier_data_process, barrier_data_confchg,
  235. sizeof (barrier_data_confchg));
  236. if (sync_recovery_index < sync_callback_count) {
  237. sync_service_init (&deliver_ring_id);
  238. } else {
  239. sync_processing = 0;
  240. }
  241. }
  242. return (0);
  243. }
  244. int sync_in_process (void)
  245. {
  246. return (sync_processing);
  247. }