sync.c 8.8 KB

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