sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.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 <config.h>
  52. #include <corosync/corotypes.h>
  53. #include <corosync/swab.h>
  54. #include <corosync/totem/totempg.h>
  55. #include <corosync/totem/totem.h>
  56. #include <corosync/lcr/lcr_ifact.h>
  57. #include <corosync/engine/logsys.h>
  58. #include <corosync/ipc_gen.h>
  59. #include "quorum.h"
  60. #include "sync.h"
  61. LOGSYS_DECLARE_SUBSYS ("SYNC", LOG_INFO);
  62. #define MESSAGE_REQ_SYNC_BARRIER 0
  63. struct barrier_data {
  64. unsigned int nodeid;
  65. int completed;
  66. };
  67. static const struct memb_ring_id *sync_ring_id;
  68. static int vsf_none = 0;
  69. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  70. static struct sync_callbacks sync_callbacks;
  71. static int sync_processing = 0;
  72. static void (*sync_synchronization_completed) (void);
  73. static int sync_recovery_index = 0;
  74. static void *sync_callback_token_handle = 0;
  75. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  76. static size_t barrier_data_confchg_entries;
  77. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  78. static struct openais_vsf_iface_ver0 *vsf_iface;
  79. static int sync_barrier_send (const struct memb_ring_id *ring_id);
  80. static int sync_start_process (enum totem_callback_token_type type,
  81. const void *data);
  82. static void sync_service_init (struct memb_ring_id *ring_id);
  83. static int sync_service_process (enum totem_callback_token_type type,
  84. const void *data);
  85. static void sync_deliver_fn (
  86. unsigned int nodeid,
  87. struct iovec *iovec,
  88. int iov_len,
  89. int endian_conversion_required);
  90. static void sync_confchg_fn (
  91. enum totem_configuration_type configuration_type,
  92. const unsigned int *member_list, size_t member_list_entries,
  93. const unsigned int *left_list, size_t left_list_entries,
  94. const unsigned int *joined_list, size_t joined_list_entries,
  95. const struct memb_ring_id *ring_id);
  96. static void sync_primary_callback_fn (
  97. const unsigned int *view_list,
  98. size_t view_list_entries,
  99. int primary_designated,
  100. const struct memb_ring_id *ring_id);
  101. static struct totempg_group sync_group = {
  102. .group = "sync",
  103. .group_len = 4
  104. };
  105. static hdb_handle_t sync_group_handle;
  106. struct req_exec_sync_barrier_start {
  107. mar_req_header_t header;
  108. struct memb_ring_id ring_id;
  109. };
  110. /*
  111. * Send a barrier data structure
  112. */
  113. static int sync_barrier_send (const struct memb_ring_id *ring_id)
  114. {
  115. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  116. struct iovec iovec;
  117. int res;
  118. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  119. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  120. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  121. sizeof (struct memb_ring_id));
  122. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  123. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  124. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  125. return (res);
  126. }
  127. static void sync_start_init (const struct memb_ring_id *ring_id)
  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_start_process,
  134. ring_id);
  135. }
  136. static void sync_service_init (struct memb_ring_id *ring_id)
  137. {
  138. sync_callbacks.sync_init ();
  139. totempg_callback_token_destroy (&sync_callback_token_handle);
  140. /*
  141. * Create the token callback for the processing
  142. */
  143. totempg_callback_token_create (
  144. &sync_callback_token_handle,
  145. TOTEM_CALLBACK_TOKEN_SENT,
  146. 0, /* don't delete after callback */
  147. sync_service_process,
  148. ring_id);
  149. }
  150. static int sync_start_process (enum totem_callback_token_type type,
  151. const void *data)
  152. {
  153. int res;
  154. const struct memb_ring_id *ring_id = data;
  155. res = sync_barrier_send (ring_id);
  156. if (res == 0) {
  157. /*
  158. * Delete the token callback for the barrier
  159. */
  160. totempg_callback_token_destroy (&sync_callback_token_handle);
  161. }
  162. return (0);
  163. }
  164. static void sync_callbacks_load (void)
  165. {
  166. int res;
  167. for (;;) {
  168. res = sync_callbacks_retrieve (sync_recovery_index,
  169. &sync_callbacks);
  170. /*
  171. * No more service handlers have sync callbacks at this time
  172. ` */
  173. if (res == -1) {
  174. sync_processing = 0;
  175. break;
  176. }
  177. sync_recovery_index += 1;
  178. if (sync_callbacks.sync_init) {
  179. break;
  180. }
  181. }
  182. }
  183. static int sync_service_process (enum totem_callback_token_type type,
  184. const void *data)
  185. {
  186. int res;
  187. const struct memb_ring_id *ring_id = data;
  188. /*
  189. * If process operation not from this ring id, then ignore it and stop
  190. * processing
  191. */
  192. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  193. return (0);
  194. }
  195. /*
  196. * If process returns 0, then its time to activate
  197. * and start the next service's synchronization
  198. */
  199. res = sync_callbacks.sync_process ();
  200. if (res != 0) {
  201. return (0);
  202. }
  203. totempg_callback_token_destroy (&sync_callback_token_handle);
  204. sync_start_init (ring_id);
  205. return (0);
  206. }
  207. int sync_register (
  208. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  209. void (*synchronization_completed) (void))
  210. {
  211. unsigned int res;
  212. res = totempg_groups_initialize (
  213. &sync_group_handle,
  214. sync_deliver_fn,
  215. sync_confchg_fn);
  216. if (res == -1) {
  217. log_printf (LOG_LEVEL_ERROR,
  218. "Couldn't initialize groups interface.\n");
  219. return (-1);
  220. }
  221. res = totempg_groups_join (
  222. sync_group_handle,
  223. &sync_group,
  224. 1);
  225. if (res == -1) {
  226. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  227. return (-1);
  228. }
  229. sync_callbacks_retrieve = callbacks_retrieve;
  230. sync_synchronization_completed = synchronization_completed;
  231. return (0);
  232. }
  233. static void sync_primary_callback_fn (
  234. const unsigned int *view_list,
  235. size_t view_list_entries,
  236. int primary_designated,
  237. const struct memb_ring_id *ring_id)
  238. {
  239. int i;
  240. if (primary_designated) {
  241. log_printf (LOG_LEVEL_DEBUG, "This node is within the primary component and will provide service.\n");
  242. } else {
  243. log_printf (LOG_LEVEL_DEBUG, "This node is within the non-primary component and will NOT provide any services.\n");
  244. return;
  245. }
  246. /*
  247. * Execute configuration change for synchronization service
  248. */
  249. sync_processing = 1;
  250. totempg_callback_token_destroy (&sync_callback_token_handle);
  251. sync_recovery_index = 0;
  252. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  253. for (i = 0; i < view_list_entries; i++) {
  254. barrier_data_confchg[i].nodeid = view_list[i];
  255. barrier_data_confchg[i].completed = 0;
  256. }
  257. memcpy (barrier_data_process, barrier_data_confchg,
  258. sizeof (barrier_data_confchg));
  259. barrier_data_confchg_entries = view_list_entries;
  260. sync_start_init (sync_ring_id);
  261. }
  262. static struct memb_ring_id deliver_ring_id;
  263. static void sync_endian_convert (struct req_exec_sync_barrier_start
  264. *req_exec_sync_barrier_start)
  265. {
  266. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  267. &req_exec_sync_barrier_start->ring_id.rep);
  268. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  269. }
  270. static void sync_deliver_fn (
  271. unsigned int nodeid,
  272. struct iovec *iovec,
  273. int iov_len,
  274. int endian_conversion_required)
  275. {
  276. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  277. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  278. unsigned int barrier_completed;
  279. int i;
  280. log_printf (LOG_LEVEL_DEBUG, "confchg entries %d\n", barrier_data_confchg_entries);
  281. if (endian_conversion_required) {
  282. sync_endian_convert (req_exec_sync_barrier_start);
  283. }
  284. barrier_completed = 1;
  285. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  286. sizeof (struct memb_ring_id));
  287. /*
  288. * Is this barrier from this configuration, if not, ignore it
  289. */
  290. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  291. sizeof (struct memb_ring_id)) != 0) {
  292. return;
  293. }
  294. /*
  295. * Set completion for source_addr's address
  296. */
  297. for (i = 0; i < barrier_data_confchg_entries; i++) {
  298. if (nodeid == barrier_data_process[i].nodeid) {
  299. barrier_data_process[i].completed = 1;
  300. log_printf (LOG_LEVEL_DEBUG,
  301. "Barrier Start Recieved From %d\n",
  302. barrier_data_process[i].nodeid);
  303. break;
  304. }
  305. }
  306. /*
  307. * Test if barrier is complete
  308. */
  309. for (i = 0; i < barrier_data_confchg_entries; i++) {
  310. log_printf (LOG_LEVEL_DEBUG,
  311. "Barrier completion status for nodeid %d = %d. \n",
  312. barrier_data_process[i].nodeid,
  313. barrier_data_process[i].completed);
  314. if (barrier_data_process[i].completed == 0) {
  315. barrier_completed = 0;
  316. }
  317. }
  318. if (barrier_completed) {
  319. log_printf (LOG_LEVEL_DEBUG,
  320. "Synchronization barrier completed\n");
  321. }
  322. /*
  323. * This sync is complete so activate and start next service sync
  324. */
  325. if (barrier_completed && sync_callbacks.sync_activate) {
  326. sync_callbacks.sync_activate ();
  327. log_printf (LOG_LEVEL_DEBUG,
  328. "Committing synchronization for (%s)\n",
  329. sync_callbacks.name);
  330. }
  331. /*
  332. * Start synchronization if the barrier has completed
  333. */
  334. if (barrier_completed) {
  335. memcpy (barrier_data_process, barrier_data_confchg,
  336. sizeof (barrier_data_confchg));
  337. sync_callbacks_load();
  338. /*
  339. * if sync service found, execute it
  340. */
  341. if (sync_processing && sync_callbacks.sync_init) {
  342. log_printf (LOG_LEVEL_DEBUG,
  343. "Synchronization actions starting for (%s)\n",
  344. sync_callbacks.name);
  345. sync_service_init (&deliver_ring_id);
  346. }
  347. }
  348. return;
  349. }
  350. static void sync_confchg_fn (
  351. enum totem_configuration_type configuration_type,
  352. const unsigned int *member_list, size_t member_list_entries,
  353. const unsigned int *left_list, size_t left_list_entries,
  354. const unsigned int *joined_list, size_t joined_list_entries,
  355. const struct memb_ring_id *ring_id)
  356. {
  357. sync_ring_id = ring_id;
  358. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  359. return;
  360. }
  361. if (sync_processing && sync_callbacks.sync_abort != NULL) {
  362. sync_callbacks.sync_abort ();
  363. sync_callbacks.sync_activate = NULL;
  364. }
  365. sync_primary_callback_fn (
  366. member_list,
  367. member_list_entries,
  368. 1,
  369. ring_id);
  370. }
  371. int sync_in_process (void)
  372. {
  373. return (sync_processing);
  374. }
  375. int sync_primary_designated (void)
  376. {
  377. return (1);
  378. }