sync.c 15 KB

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