4
0

sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 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 <corosync/saAis.h>
  52. #include <corosync/swab.h>
  53. #include <corosync/totem/totempg.h>
  54. #include <corosync/totem/totem.h>
  55. #include <corosync/lcr/lcr_ifact.h>
  56. #include <corosync/engine/logsys.h>
  57. #include "main.h"
  58. #include "sync.h"
  59. #include "vsf.h"
  60. LOGSYS_DECLARE_SUBSYS ("SYNC", LOG_INFO);
  61. #define MESSAGE_REQ_SYNC_BARRIER 0
  62. struct barrier_data {
  63. unsigned int nodeid;
  64. int completed;
  65. };
  66. static struct memb_ring_id *sync_ring_id;
  67. static int vsf_none = 0;
  68. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  69. static struct sync_callbacks sync_callbacks;
  70. static int sync_processing = 0;
  71. static void (*sync_synchronization_completed) (void);
  72. static int sync_recovery_index = 0;
  73. static void *sync_callback_token_handle = 0;
  74. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  75. static int barrier_data_confchg_entries;
  76. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  77. static struct openais_vsf_iface_ver0 *vsf_iface;
  78. static int sync_barrier_send (struct memb_ring_id *ring_id);
  79. static int sync_start_process (enum totem_callback_token_type type, void *data);
  80. static void sync_service_init (struct memb_ring_id *ring_id);
  81. static int sync_service_process (enum totem_callback_token_type type, void *data);
  82. static void sync_deliver_fn (
  83. unsigned int nodeid,
  84. struct iovec *iovec,
  85. int iov_len,
  86. int endian_conversion_required);
  87. static void sync_confchg_fn (
  88. enum totem_configuration_type configuration_type,
  89. unsigned int *member_list, int member_list_entries,
  90. unsigned int *left_list, int left_list_entries,
  91. unsigned int *joined_list, int joined_list_entries,
  92. struct memb_ring_id *ring_id);
  93. static void sync_primary_callback_fn (
  94. unsigned int *view_list,
  95. int view_list_entries,
  96. int primary_designated,
  97. struct memb_ring_id *ring_id);
  98. static struct totempg_group sync_group = {
  99. .group = "sync",
  100. .group_len = 4
  101. };
  102. static totempg_groups_handle sync_group_handle;
  103. struct req_exec_sync_barrier_start {
  104. mar_req_header_t header;
  105. struct memb_ring_id ring_id;
  106. };
  107. /*
  108. * Send a barrier data structure
  109. */
  110. static int sync_barrier_send (struct memb_ring_id *ring_id)
  111. {
  112. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  113. struct iovec iovec;
  114. int res;
  115. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  116. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  117. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  118. sizeof (struct memb_ring_id));
  119. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  120. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  121. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  122. return (res);
  123. }
  124. void sync_start_init (struct memb_ring_id *ring_id)
  125. {
  126. totempg_callback_token_create (
  127. &sync_callback_token_handle,
  128. TOTEM_CALLBACK_TOKEN_SENT,
  129. 0, /* don't delete after callback */
  130. sync_start_process,
  131. (void *)ring_id);
  132. }
  133. static void sync_service_init (struct memb_ring_id *ring_id)
  134. {
  135. sync_callbacks.sync_init ();
  136. totempg_callback_token_destroy (&sync_callback_token_handle);
  137. /*
  138. * Create the token callback for the processing
  139. */
  140. totempg_callback_token_create (
  141. &sync_callback_token_handle,
  142. TOTEM_CALLBACK_TOKEN_SENT,
  143. 0, /* don't delete after callback */
  144. sync_service_process,
  145. (void *)ring_id);
  146. }
  147. static int sync_start_process (enum totem_callback_token_type type, void *data)
  148. {
  149. int res;
  150. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  151. res = sync_barrier_send (ring_id);
  152. if (res == 0) {
  153. /*
  154. * Delete the token callback for the barrier
  155. */
  156. totempg_callback_token_destroy (&sync_callback_token_handle);
  157. }
  158. return (0);
  159. }
  160. void sync_callbacks_load (void)
  161. {
  162. int res;
  163. for (;;) {
  164. res = sync_callbacks_retrieve (sync_recovery_index,
  165. &sync_callbacks);
  166. /*
  167. * No more service handlers have sync callbacks at this time
  168. ` */
  169. if (res == -1) {
  170. sync_processing = 0;
  171. break;
  172. }
  173. sync_recovery_index += 1;
  174. if (sync_callbacks.sync_init) {
  175. break;
  176. }
  177. }
  178. }
  179. static int sync_service_process (enum totem_callback_token_type type, void *data)
  180. {
  181. int res;
  182. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  183. /*
  184. * If process operation not from this ring id, then ignore it and stop
  185. * processing
  186. */
  187. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  188. return (0);
  189. }
  190. /*
  191. * If process returns 0, then its time to activate
  192. * and start the next service's synchronization
  193. */
  194. res = sync_callbacks.sync_process ();
  195. if (res != 0) {
  196. return (0);
  197. }
  198. totempg_callback_token_destroy (&sync_callback_token_handle);
  199. sync_start_init (ring_id);
  200. return (0);
  201. }
  202. int sync_register (
  203. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  204. void (*synchronization_completed) (void),
  205. char *vsf_type)
  206. {
  207. unsigned int res;
  208. unsigned int vsf_handle;
  209. void *vsf_iface_p;
  210. char openais_vsf_type[1024];
  211. res = totempg_groups_initialize (
  212. &sync_group_handle,
  213. sync_deliver_fn,
  214. sync_confchg_fn);
  215. if (res == -1) {
  216. log_printf (LOG_LEVEL_ERROR,
  217. "Couldn't initialize groups interface.\n");
  218. return (-1);
  219. }
  220. res = totempg_groups_join (
  221. sync_group_handle,
  222. &sync_group,
  223. 1);
  224. if (res == -1) {
  225. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  226. return (-1);
  227. }
  228. if (strcmp (vsf_type, "none") == 0) {
  229. log_printf (LOG_LEVEL_NOTICE,
  230. "Not using a virtual synchrony filter.\n");
  231. vsf_none = 1;
  232. } else {
  233. vsf_none = 0;
  234. sprintf (openais_vsf_type, "openais_vsf_%s", vsf_type);
  235. res = lcr_ifact_reference (
  236. &vsf_handle,
  237. openais_vsf_type,
  238. 0,
  239. &vsf_iface_p,
  240. 0);
  241. if (res == -1) {
  242. log_printf (LOG_LEVEL_NOTICE,
  243. "Couldn't load virtual synchrony filter %s\n",
  244. vsf_type);
  245. return (-1);
  246. }
  247. log_printf (LOG_LEVEL_NOTICE,
  248. "Using virtual synchrony filter %s\n", openais_vsf_type);
  249. vsf_iface = (struct openais_vsf_iface_ver0 *)vsf_iface_p;
  250. vsf_iface->init (sync_primary_callback_fn);
  251. }
  252. sync_callbacks_retrieve = callbacks_retrieve;
  253. sync_synchronization_completed = synchronization_completed;
  254. return (0);
  255. }
  256. static void sync_primary_callback_fn (
  257. unsigned int *view_list,
  258. int view_list_entries,
  259. int primary_designated,
  260. struct memb_ring_id *ring_id)
  261. {
  262. int i;
  263. if (primary_designated) {
  264. log_printf (LOG_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  265. } else {
  266. log_printf (LOG_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  267. return;
  268. }
  269. /*
  270. * Execute configuration change for synchronization service
  271. */
  272. sync_processing = 1;
  273. totempg_callback_token_destroy (&sync_callback_token_handle);
  274. sync_recovery_index = 0;
  275. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  276. for (i = 0; i < view_list_entries; i++) {
  277. barrier_data_confchg[i].nodeid = view_list[i];
  278. barrier_data_confchg[i].completed = 0;
  279. }
  280. memcpy (barrier_data_process, barrier_data_confchg,
  281. sizeof (barrier_data_confchg));
  282. barrier_data_confchg_entries = view_list_entries;
  283. sync_start_init (sync_ring_id);
  284. }
  285. static struct memb_ring_id deliver_ring_id;
  286. void sync_endian_convert (struct req_exec_sync_barrier_start *req_exec_sync_barrier_start)
  287. {
  288. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  289. &req_exec_sync_barrier_start->ring_id.rep);
  290. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  291. }
  292. static void sync_deliver_fn (
  293. unsigned int nodeid,
  294. struct iovec *iovec,
  295. int iov_len,
  296. int endian_conversion_required)
  297. {
  298. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  299. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  300. int i;
  301. if (endian_conversion_required) {
  302. sync_endian_convert (req_exec_sync_barrier_start);
  303. }
  304. int barrier_completed = 1;
  305. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  306. sizeof (struct memb_ring_id));
  307. /*
  308. * Is this barrier from this configuration, if not, ignore it
  309. */
  310. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  311. sizeof (struct memb_ring_id)) != 0) {
  312. return;
  313. }
  314. /*
  315. * Set completion for source_addr's address
  316. */
  317. for (i = 0; i < barrier_data_confchg_entries; i++) {
  318. if (nodeid == barrier_data_process[i].nodeid) {
  319. barrier_data_process[i].completed = 1;
  320. log_printf (LOG_LEVEL_DEBUG,
  321. "Barrier Start Recieved From %d\n",
  322. barrier_data_process[i].nodeid);
  323. break;
  324. }
  325. }
  326. /*
  327. * Test if barrier is complete
  328. */
  329. for (i = 0; i < barrier_data_confchg_entries; i++) {
  330. log_printf (LOG_LEVEL_DEBUG,
  331. "Barrier completion status for nodeid %d = %d. \n",
  332. barrier_data_process[i].nodeid,
  333. barrier_data_process[i].completed);
  334. if (barrier_data_process[i].completed == 0) {
  335. barrier_completed = 0;
  336. }
  337. }
  338. if (barrier_completed) {
  339. log_printf (LOG_LEVEL_DEBUG,
  340. "Synchronization barrier completed\n");
  341. }
  342. /*
  343. * This sync is complete so activate and start next service sync
  344. */
  345. if (barrier_completed && sync_callbacks.sync_activate) {
  346. sync_callbacks.sync_activate ();
  347. log_printf (LOG_LEVEL_DEBUG,
  348. "Committing synchronization for (%s)\n",
  349. sync_callbacks.name);
  350. }
  351. /*
  352. * Start synchronization if the barrier has completed
  353. */
  354. if (barrier_completed) {
  355. memcpy (barrier_data_process, barrier_data_confchg,
  356. sizeof (barrier_data_confchg));
  357. sync_callbacks_load();
  358. /*
  359. * if sync service found, execute it
  360. */
  361. if (sync_processing && sync_callbacks.sync_init) {
  362. log_printf (LOG_LEVEL_DEBUG,
  363. "Synchronization actions starting for (%s)\n",
  364. sync_callbacks.name);
  365. sync_service_init (&deliver_ring_id);
  366. }
  367. }
  368. return;
  369. }
  370. static void sync_confchg_fn (
  371. enum totem_configuration_type configuration_type,
  372. unsigned int *member_list, int member_list_entries,
  373. unsigned int *left_list, int left_list_entries,
  374. unsigned int *joined_list, int joined_list_entries,
  375. struct memb_ring_id *ring_id)
  376. {
  377. sync_ring_id = ring_id;
  378. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  379. return;
  380. }
  381. if (sync_processing && sync_callbacks.sync_abort != NULL) {
  382. sync_callbacks.sync_abort ();
  383. sync_callbacks.sync_activate = NULL;
  384. }
  385. /*
  386. * If no virtual synchrony filter configured, then start
  387. * synchronization process
  388. */
  389. if (vsf_none == 1) {
  390. sync_primary_callback_fn (
  391. member_list,
  392. member_list_entries,
  393. 1,
  394. ring_id);
  395. }
  396. }
  397. int sync_in_process (void)
  398. {
  399. return (sync_processing);
  400. }
  401. int sync_primary_designated (void)
  402. {
  403. if (vsf_none == 1) {
  404. return (1);
  405. } else {
  406. return (vsf_iface->primary());
  407. }
  408. }