sync.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 <config.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <sys/un.h>
  39. #include <sys/ioctl.h>
  40. #include <netinet/in.h>
  41. #include <sys/uio.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <signal.h>
  48. #include <time.h>
  49. #include <unistd.h>
  50. #include <netinet/in.h>
  51. #include <arpa/inet.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 "quorum.h"
  59. #include "main.h"
  60. #include "sync.h"
  61. LOGSYS_DECLARE_SUBSYS ("SYNC", LOG_INFO);
  62. #define MESSAGE_REQ_SYNC_BARRIER 0
  63. #define MESSAGE_REQ_SYNC_REQUEST 1
  64. struct barrier_data {
  65. unsigned int nodeid;
  66. int completed;
  67. };
  68. static struct memb_ring_id *sync_ring_id;
  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 void *sync_request_token_handle;
  76. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  77. static int barrier_data_confchg_entries;
  78. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  79. static int sync_barrier_send (struct memb_ring_id *ring_id);
  80. static int sync_start_process (enum totem_callback_token_type type, void *data);
  81. static void sync_service_init (struct memb_ring_id *ring_id);
  82. static int sync_service_process (enum totem_callback_token_type type, void *data);
  83. static void sync_deliver_fn (
  84. unsigned int nodeid,
  85. struct iovec *iovec,
  86. int iov_len,
  87. int endian_conversion_required);
  88. static void sync_confchg_fn (
  89. enum totem_configuration_type configuration_type,
  90. unsigned int *member_list, int member_list_entries,
  91. unsigned int *left_list, int left_list_entries,
  92. unsigned int *joined_list, int joined_list_entries,
  93. struct memb_ring_id *ring_id);
  94. static struct totempg_group sync_group = {
  95. .group = "sync",
  96. .group_len = 4
  97. };
  98. static hdb_handle_t sync_group_handle;
  99. static char *service_name;
  100. static unsigned int current_members[PROCESSOR_COUNT_MAX];
  101. static unsigned int current_members_cnt;
  102. struct req_exec_sync_barrier_start {
  103. mar_req_header_t header;
  104. struct memb_ring_id ring_id;
  105. };
  106. struct sync_request {
  107. uint32_t name_len;
  108. char name[0] __attribute__((aligned(8)));
  109. };
  110. typedef struct sync_msg {
  111. mar_req_header_t header;
  112. struct memb_ring_id ring_id;
  113. struct sync_request sync_request;
  114. } sync_msg_t;
  115. /*
  116. * Send a barrier data structure
  117. */
  118. static int sync_barrier_send (struct memb_ring_id *ring_id)
  119. {
  120. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  121. struct iovec iovec;
  122. int res;
  123. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  124. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  125. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  126. sizeof (struct memb_ring_id));
  127. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  128. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  129. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  130. return (res);
  131. }
  132. void sync_start_init (struct memb_ring_id *ring_id)
  133. {
  134. totempg_callback_token_create (
  135. &sync_callback_token_handle,
  136. TOTEM_CALLBACK_TOKEN_SENT,
  137. 0, /* don't delete after callback */
  138. sync_start_process,
  139. (void *)ring_id);
  140. }
  141. static void sync_service_init (struct memb_ring_id *ring_id)
  142. {
  143. sync_callbacks.sync_init ();
  144. totempg_callback_token_destroy (&sync_callback_token_handle);
  145. /*
  146. * Create the token callback for the processing
  147. */
  148. totempg_callback_token_create (
  149. &sync_callback_token_handle,
  150. TOTEM_CALLBACK_TOKEN_SENT,
  151. 0, /* don't delete after callback */
  152. sync_service_process,
  153. (void *)ring_id);
  154. }
  155. static int sync_start_process (enum totem_callback_token_type type, void *data)
  156. {
  157. int res;
  158. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  159. res = sync_barrier_send (ring_id);
  160. if (res == 0) {
  161. /*
  162. * Delete the token callback for the barrier
  163. */
  164. totempg_callback_token_destroy (&sync_callback_token_handle);
  165. }
  166. return (0);
  167. }
  168. void sync_callbacks_load (void)
  169. {
  170. int res;
  171. for (;;) {
  172. res = sync_callbacks_retrieve (sync_recovery_index,
  173. &sync_callbacks);
  174. /*
  175. * No more service handlers have sync callbacks at this time
  176. ` */
  177. if (res == -1) {
  178. sync_processing = 0;
  179. break;
  180. }
  181. sync_recovery_index += 1;
  182. if (sync_callbacks.sync_init) {
  183. break;
  184. }
  185. }
  186. }
  187. static int sync_service_process (enum totem_callback_token_type type, void *data)
  188. {
  189. int res;
  190. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  191. /*
  192. * If process operation not from this ring id, then ignore it and stop
  193. * processing
  194. */
  195. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  196. return (0);
  197. }
  198. /*
  199. * If process returns 0, then its time to activate
  200. * and start the next service's synchronization
  201. */
  202. res = sync_callbacks.sync_process ();
  203. if (res != 0) {
  204. return (0);
  205. }
  206. totempg_callback_token_destroy (&sync_callback_token_handle);
  207. sync_start_init (ring_id);
  208. return (0);
  209. }
  210. int sync_register (
  211. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  212. void (*synchronization_completed) (void))
  213. {
  214. unsigned int res;
  215. res = totempg_groups_initialize (
  216. &sync_group_handle,
  217. sync_deliver_fn,
  218. sync_confchg_fn);
  219. if (res == -1) {
  220. log_printf (LOG_LEVEL_ERROR,
  221. "Couldn't initialize groups interface.\n");
  222. return (-1);
  223. }
  224. res = totempg_groups_join (
  225. sync_group_handle,
  226. &sync_group,
  227. 1);
  228. if (res == -1) {
  229. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  230. return (-1);
  231. }
  232. sync_callbacks_retrieve = callbacks_retrieve;
  233. sync_synchronization_completed = synchronization_completed;
  234. return (0);
  235. }
  236. void sync_primary_callback_fn (
  237. unsigned int *view_list,
  238. int view_list_entries,
  239. int primary_designated,
  240. struct memb_ring_id *ring_id)
  241. {
  242. int i;
  243. /*
  244. * Execute configuration change for synchronization service
  245. */
  246. sync_processing = 1;
  247. totempg_callback_token_destroy (&sync_callback_token_handle);
  248. sync_recovery_index = 0;
  249. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  250. for (i = 0; i < view_list_entries; i++) {
  251. barrier_data_confchg[i].nodeid = view_list[i];
  252. barrier_data_confchg[i].completed = 0;
  253. }
  254. memcpy (barrier_data_process, barrier_data_confchg,
  255. sizeof (barrier_data_confchg));
  256. barrier_data_confchg_entries = view_list_entries;
  257. sync_start_init (sync_ring_id);
  258. }
  259. static struct memb_ring_id deliver_ring_id;
  260. void sync_endian_convert (struct req_exec_sync_barrier_start *req_exec_sync_barrier_start)
  261. {
  262. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  263. &req_exec_sync_barrier_start->ring_id.rep);
  264. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  265. }
  266. static void sync_deliver_fn (
  267. unsigned int nodeid,
  268. struct iovec *iovec,
  269. int iov_len,
  270. int endian_conversion_required)
  271. {
  272. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  273. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  274. sync_msg_t *msg = (sync_msg_t *)iovec[0].iov_base;
  275. int i;
  276. if (endian_conversion_required) {
  277. sync_endian_convert (req_exec_sync_barrier_start);
  278. }
  279. int barrier_completed = 1;
  280. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  281. sizeof (struct memb_ring_id));
  282. /*
  283. * Is this barrier from this configuration, if not, ignore it
  284. */
  285. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  286. sizeof (struct memb_ring_id)) != 0) {
  287. return;
  288. }
  289. if (msg->header.id == MESSAGE_REQ_SYNC_REQUEST) {
  290. if (endian_conversion_required) {
  291. swab_mar_uint32_t (&msg->sync_request.name_len);
  292. }
  293. /*
  294. * If there is an ongoing sync, abort it. A requested sync is
  295. * only allowed to abort other requested synchronizations,
  296. * not full synchronizations.
  297. */
  298. if (sync_processing && sync_callbacks.sync_abort) {
  299. sync_callbacks.sync_abort();
  300. sync_callbacks.sync_activate = NULL;
  301. sync_processing = 0;
  302. assert (service_name != NULL);
  303. free (service_name);
  304. service_name = NULL;
  305. }
  306. service_name = malloc (msg->sync_request.name_len);
  307. strcpy (service_name, msg->sync_request.name);
  308. /*
  309. * Start requested synchronization
  310. */
  311. sync_primary_callback_fn (current_members, current_members_cnt, 1,
  312. sync_ring_id);
  313. return;
  314. }
  315. /*
  316. * Set completion for source_addr's address
  317. */
  318. for (i = 0; i < barrier_data_confchg_entries; i++) {
  319. if (nodeid == barrier_data_process[i].nodeid) {
  320. barrier_data_process[i].completed = 1;
  321. log_printf (LOG_LEVEL_DEBUG,
  322. "Barrier Start Recieved From %d\n",
  323. barrier_data_process[i].nodeid);
  324. break;
  325. }
  326. }
  327. /*
  328. * Test if barrier is complete
  329. */
  330. for (i = 0; i < barrier_data_confchg_entries; i++) {
  331. log_printf (LOG_LEVEL_DEBUG,
  332. "Barrier completion status for nodeid %d = %d. \n",
  333. barrier_data_process[i].nodeid,
  334. barrier_data_process[i].completed);
  335. if (barrier_data_process[i].completed == 0) {
  336. barrier_completed = 0;
  337. }
  338. }
  339. if (barrier_completed) {
  340. log_printf (LOG_LEVEL_DEBUG,
  341. "Synchronization barrier completed\n");
  342. }
  343. /*
  344. * This sync is complete so activate and start next service sync
  345. */
  346. if (barrier_completed && sync_callbacks.sync_activate) {
  347. sync_callbacks.sync_activate ();
  348. log_printf (LOG_LEVEL_DEBUG,
  349. "Committing synchronization for (%s)\n",
  350. sync_callbacks.name);
  351. }
  352. /*
  353. * Start synchronization if the barrier has completed
  354. */
  355. if (barrier_completed) {
  356. memcpy (barrier_data_process, barrier_data_confchg,
  357. sizeof (barrier_data_confchg));
  358. sync_callbacks_load();
  359. /*
  360. * if sync service found, execute it
  361. */
  362. if (sync_processing && sync_callbacks.sync_init) {
  363. log_printf (LOG_LEVEL_DEBUG,
  364. "Synchronization actions starting for (%s)\n",
  365. sync_callbacks.name);
  366. sync_service_init (&deliver_ring_id);
  367. }
  368. }
  369. return;
  370. }
  371. static void sync_confchg_fn (
  372. enum totem_configuration_type configuration_type,
  373. unsigned int *member_list, int member_list_entries,
  374. unsigned int *left_list, int left_list_entries,
  375. unsigned int *joined_list, int joined_list_entries,
  376. struct memb_ring_id *ring_id)
  377. {
  378. int i;
  379. sync_ring_id = ring_id;
  380. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  381. return;
  382. }
  383. if (sync_processing && sync_callbacks.sync_abort != NULL) {
  384. sync_callbacks.sync_abort ();
  385. sync_callbacks.sync_activate = NULL;
  386. }
  387. /*
  388. * Save current members and ring ID for later use
  389. */
  390. for (i = 0; i < member_list_entries; i++) {
  391. current_members[i] = member_list[i];
  392. }
  393. current_members_cnt = member_list_entries;
  394. /*
  395. * If no virtual synchrony filter configured, then start
  396. * synchronization process
  397. */
  398. if (quorum_none() == 1) {
  399. sync_primary_callback_fn (
  400. member_list,
  401. member_list_entries,
  402. 1,
  403. ring_id);
  404. }
  405. }
  406. /**
  407. * TOTEM callback function used to multicast a sync_request
  408. * message
  409. * @param type
  410. * @param _name
  411. *
  412. * @return int
  413. */
  414. static int sync_request_send (
  415. enum totem_callback_token_type type, void *_name)
  416. {
  417. int res;
  418. char *name = _name;
  419. sync_msg_t msg;
  420. struct iovec iovec[2];
  421. int name_len;
  422. ENTER();
  423. name_len = strlen (name) + 1;
  424. msg.header.size = sizeof (msg) + name_len;
  425. msg.header.id = MESSAGE_REQ_SYNC_REQUEST;
  426. if (sync_ring_id == NULL) {
  427. log_printf (LOG_LEVEL_ERROR,
  428. "%s sync_ring_id is NULL.\n", __func__);
  429. return 1;
  430. }
  431. memcpy (&msg.ring_id, sync_ring_id, sizeof (struct memb_ring_id));
  432. msg.sync_request.name_len = name_len;
  433. iovec[0].iov_base = (char *)&msg;
  434. iovec[0].iov_len = sizeof (msg);
  435. iovec[1].iov_base = _name;
  436. iovec[1].iov_len = name_len;
  437. res = totempg_groups_mcast_joined (
  438. sync_group_handle, iovec, 2, TOTEMPG_AGREED);
  439. if (res == 0) {
  440. /*
  441. * We managed to multicast the message so delete the token callback
  442. * for the sync request.
  443. */
  444. totempg_callback_token_destroy (&sync_request_token_handle);
  445. }
  446. /*
  447. * if we failed to multicast the message, this function will be called
  448. * again.
  449. */
  450. return (0);
  451. }
  452. int sync_in_process (void)
  453. {
  454. return (sync_processing);
  455. }
  456. /**
  457. * Execute synchronization upon request for the named service
  458. * @param name
  459. *
  460. * @return int
  461. */
  462. int sync_request (char *name)
  463. {
  464. assert (name != NULL);
  465. ENTER();
  466. if (sync_processing) {
  467. return -1;
  468. }
  469. totempg_callback_token_create (&sync_request_token_handle,
  470. TOTEM_CALLBACK_TOKEN_SENT, 0, /* don't delete after callback */
  471. sync_request_send, name);
  472. LEAVE();
  473. return 0;
  474. }