sync.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/ais_types.h"
  52. #include "../include/ipc_gen.h"
  53. #include "sync.h"
  54. #include "totempg.h"
  55. #include "print.h"
  56. #define LOG_SERVICE LOG_SERVICE_SYNC
  57. struct barrier_data {
  58. struct in_addr addr;
  59. int completed;
  60. };
  61. static struct memb_ring_id *sync_ring_id;
  62. static struct sync_callbacks *sync_callbacks;
  63. static int sync_callback_count;
  64. static void (*sync_synchronization_completed) (void);
  65. static int sync_recovery_index = 0;
  66. static void *sync_callback_token_handle;
  67. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  68. static int barrier_data_confchg_entries;
  69. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  70. static void sync_barrier_start (struct memb_ring_id *ring_id);
  71. static void sync_service_init (struct memb_ring_id *ring_id);
  72. static int sync_service_process (enum totempg_callback_token_type type, void *data);
  73. struct req_exec_sync_barrier_start {
  74. struct req_header header;
  75. struct memb_ring_id ring_id;
  76. };
  77. /*
  78. * Start barrier process
  79. */
  80. static void sync_barrier_start (struct memb_ring_id *ring_id)
  81. {
  82. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  83. struct iovec iovec;
  84. int result;
  85. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  86. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_EXEC_SYNC_BARRIER;
  87. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  88. sizeof (struct memb_ring_id));
  89. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  90. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  91. result = totempg_mcast (&iovec, 1, TOTEMPG_AGREED);
  92. }
  93. static void sync_service_init (struct memb_ring_id *ring_id)
  94. {
  95. sync_callbacks[sync_recovery_index].sync_init ();
  96. totemsrp_callback_token_create (&sync_callback_token_handle,
  97. TOTEMPG_CALLBACK_TOKEN_SENT,
  98. 0, /* don't delete after callback */
  99. sync_service_process,
  100. (void *)ring_id);
  101. }
  102. static int sync_service_process (enum totempg_callback_token_type type, void *data)
  103. {
  104. int res;
  105. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  106. /*
  107. * If process returns 0, then its time to activate
  108. * and start the next service's synchronization
  109. */
  110. res = sync_callbacks[sync_recovery_index].sync_process ();
  111. if (res != 0) {
  112. return (0);
  113. }
  114. /*
  115. * This sync is complete so activate and start next service sync
  116. */
  117. sync_callbacks[sync_recovery_index].sync_activate ();
  118. sync_recovery_index += 1;
  119. totemsrp_callback_token_destroy (sync_callback_token_handle);
  120. if (sync_recovery_index > sync_callback_count) {
  121. } else {
  122. sync_barrier_start (ring_id);
  123. }
  124. return (0);
  125. }
  126. void sync_register (struct sync_callbacks *callbacks, int callback_count,
  127. void (*synchronization_completed) (void))
  128. {
  129. sync_callbacks = callbacks;
  130. sync_callback_count = callback_count;
  131. sync_synchronization_completed = synchronization_completed;
  132. }
  133. void sync_confchg_fn (
  134. enum totempg_configuration_type configuration_type,
  135. struct in_addr *member_list, void *member_list_private,
  136. int member_list_entries,
  137. struct in_addr *left_list, void *left_list_private,
  138. int left_list_entries,
  139. struct in_addr *joined_list, void *joined_list_private,
  140. int joined_list_entries,
  141. struct memb_ring_id *ring_id)
  142. {
  143. int i;
  144. if (configuration_type != TOTEMPG_CONFIGURATION_REGULAR) {
  145. return;
  146. }
  147. sync_ring_id = ring_id;
  148. sync_recovery_index = 0;
  149. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  150. for (i = 0; i < member_list_entries; i++) {
  151. barrier_data_confchg[i].addr.s_addr = member_list[i].s_addr;
  152. barrier_data_confchg[i].completed = 0;
  153. }
  154. memcpy (barrier_data_process, barrier_data_confchg,
  155. sizeof (barrier_data_confchg));
  156. barrier_data_confchg_entries = member_list_entries;
  157. sync_barrier_start (ring_id);
  158. }
  159. int sync_deliver_fn (void *msg, struct in_addr source_addr,
  160. int endian_conversion_needed)
  161. {
  162. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  163. (struct req_exec_sync_barrier_start *)msg;
  164. int i;
  165. int barrier_completed = 1;
  166. /*
  167. * Is this barrier from this configuration, if not, ignore it
  168. */
  169. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  170. sizeof (struct memb_ring_id)) != 0) {
  171. return (0);
  172. }
  173. /*
  174. * Set completion for source_addr's address
  175. */
  176. for (i = 0; i < barrier_data_confchg_entries; i++) {
  177. if (source_addr.s_addr == barrier_data_process[i].addr.s_addr) {
  178. barrier_data_process[i].completed = 1;
  179. break;
  180. }
  181. }
  182. /*
  183. * Test if barrier is complete
  184. */
  185. for (i = 0; i < barrier_data_confchg_entries; i++) {
  186. if (barrier_data_process[i].completed == 0) {
  187. barrier_completed = 0;
  188. }
  189. }
  190. if (barrier_completed) {
  191. log_printf (LOG_LEVEL_NOTICE,
  192. "Synchronization barrier completed\n");
  193. }
  194. /*
  195. * Start synchronization if the barrier has completed
  196. */
  197. if (barrier_completed) {
  198. memcpy (barrier_data_process, barrier_data_confchg,
  199. sizeof (barrier_data_confchg));
  200. if (sync_recovery_index < sync_callback_count) {
  201. sync_service_init (&req_exec_sync_barrier_start->ring_id);
  202. }
  203. }
  204. return (0);
  205. }