sync.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "ykd.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. static int sync_deliver_fn (
  77. struct totem_ip_address *source_addr,
  78. struct iovec *iovec,
  79. int iov_len,
  80. int endian_conversion_required);
  81. void sync_primary_callback_fn (
  82. struct totem_ip_address *view_list,
  83. int view_list_entries,
  84. int primary_designated,
  85. struct memb_ring_id *ring_id);
  86. static struct totempg_group sync_group = {
  87. .group = "sync",
  88. .group_len = 4
  89. };
  90. static totempg_groups_handle sync_group_handle;
  91. struct req_exec_sync_barrier_start {
  92. struct req_header header;
  93. struct memb_ring_id ring_id;
  94. };
  95. /*
  96. * Send a barrier data structure
  97. */
  98. static int sync_barrier_send (struct memb_ring_id *ring_id)
  99. {
  100. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  101. struct iovec iovec;
  102. int res;
  103. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  104. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_EXEC_SYNC_BARRIER;
  105. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  106. sizeof (struct memb_ring_id));
  107. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  108. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  109. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  110. return (res);
  111. }
  112. void sync_start_init (struct memb_ring_id *ring_id)
  113. {
  114. totempg_callback_token_create (
  115. &sync_callback_token_handle,
  116. TOTEM_CALLBACK_TOKEN_SENT,
  117. 0, /* don't delete after callback */
  118. sync_start_process,
  119. (void *)ring_id);
  120. }
  121. static void sync_service_init (struct memb_ring_id *ring_id)
  122. {
  123. sync_callbacks[sync_recovery_index].sync_init ();
  124. totempg_callback_token_destroy (&sync_callback_token_handle);
  125. /*
  126. * Create the token callback for the processing
  127. */
  128. totempg_callback_token_create (
  129. &sync_callback_token_handle,
  130. TOTEM_CALLBACK_TOKEN_SENT,
  131. 0, /* don't delete after callback */
  132. sync_service_process,
  133. (void *)ring_id);
  134. }
  135. static int sync_start_process (enum totem_callback_token_type type, void *data)
  136. {
  137. int res;
  138. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  139. res = sync_barrier_send (ring_id);
  140. if (res == 0) {
  141. /*
  142. * Delete the token callback for the barrier
  143. */
  144. totempg_callback_token_destroy (&sync_callback_token_handle);
  145. }
  146. return (0);
  147. }
  148. static int sync_service_process (enum totem_callback_token_type type, void *data)
  149. {
  150. int res;
  151. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  152. /*
  153. * If process returns 0, then its time to activate
  154. * and start the next service's synchronization
  155. */
  156. res = sync_callbacks[sync_recovery_index].sync_process ();
  157. if (res != 0) {
  158. return (0);
  159. }
  160. /*
  161. * This sync is complete so activate and start next service sync
  162. */
  163. sync_callbacks[sync_recovery_index].sync_activate ();
  164. totempg_callback_token_destroy (&sync_callback_token_handle);
  165. sync_recovery_index += 1;
  166. if (sync_recovery_index > sync_callback_count) {
  167. sync_processing = 0;
  168. } else {
  169. sync_start_init (ring_id);
  170. }
  171. return (0);
  172. }
  173. void sync_register (
  174. struct sync_callbacks *callbacks,
  175. int callback_count,
  176. void (*synchronization_completed) (void))
  177. {
  178. totempg_groups_initialize (
  179. &sync_group_handle,
  180. sync_deliver_fn,
  181. NULL);
  182. totempg_groups_join (
  183. sync_group_handle,
  184. &sync_group,
  185. 1);
  186. ykd_init (sync_primary_callback_fn);
  187. sync_callbacks = callbacks;
  188. sync_callback_count = callback_count;
  189. sync_synchronization_completed = synchronization_completed;
  190. }
  191. void sync_primary_callback_fn (
  192. struct totem_ip_address *view_list,
  193. int view_list_entries,
  194. int primary_designated,
  195. struct memb_ring_id *ring_id)
  196. {
  197. int i;
  198. if (primary_designated) {
  199. log_printf (LOG_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  200. } else {
  201. log_printf (LOG_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  202. return;
  203. }
  204. /*
  205. * Execute configuration change for synchronization service
  206. */
  207. sync_processing = 1;
  208. totempg_callback_token_destroy (&sync_callback_token_handle);
  209. sync_ring_id = ring_id;
  210. sync_recovery_index = 0;
  211. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  212. for (i = 0; i < view_list_entries; i++) {
  213. totemip_copy(&barrier_data_confchg[i].addr, &view_list[i]);
  214. barrier_data_confchg[i].completed = 0;
  215. }
  216. memcpy (barrier_data_process, barrier_data_confchg,
  217. sizeof (barrier_data_confchg));
  218. barrier_data_confchg_entries = view_list_entries;
  219. sync_start_init (ring_id);
  220. }
  221. static struct memb_ring_id deliver_ring_id;
  222. int sync_deliver_fn (
  223. struct totem_ip_address *source_addr,
  224. struct iovec *iovec,
  225. int iov_len,
  226. int endian_conversion_required)
  227. {
  228. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  229. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  230. int i;
  231. int barrier_completed = 1;
  232. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  233. sizeof (struct memb_ring_id));
  234. /*
  235. * Is this barrier from this configuration, if not, ignore it
  236. */
  237. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  238. sizeof (struct memb_ring_id)) != 0) {
  239. return (0);
  240. }
  241. /*
  242. * Set completion for source_addr's address
  243. */
  244. for (i = 0; i < barrier_data_confchg_entries; i++) {
  245. if (totemip_equal(source_addr, &barrier_data_process[i].addr)) {
  246. barrier_data_process[i].completed = 1;
  247. break;
  248. }
  249. }
  250. /*
  251. * Test if barrier is complete
  252. */
  253. for (i = 0; i < barrier_data_confchg_entries; i++) {
  254. if (barrier_data_process[i].completed == 0) {
  255. barrier_completed = 0;
  256. }
  257. }
  258. if (barrier_completed) {
  259. log_printf (LOG_LEVEL_NOTICE,
  260. "Synchronization barrier completed\n");
  261. }
  262. /*
  263. * Start synchronization if the barrier has completed
  264. */
  265. if (barrier_completed) {
  266. memcpy (barrier_data_process, barrier_data_confchg,
  267. sizeof (barrier_data_confchg));
  268. if (sync_recovery_index < sync_callback_count) {
  269. sync_service_init (&deliver_ring_id);
  270. } else {
  271. sync_processing = 0;
  272. }
  273. }
  274. return (0);
  275. }
  276. int sync_in_process (void)
  277. {
  278. return (sync_processing);
  279. }