sync.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Copyright (c) 2006-2007 Red Hat, Inc.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * Author: Hans Feldt
  8. *
  9. * All rights reserved.
  10. *
  11. *
  12. * This software licensed under BSD license, the text of which follows:
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  36. * THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/un.h>
  41. #include <sys/ioctl.h>
  42. #include <netinet/in.h>
  43. #include <sys/uio.h>
  44. #include <unistd.h>
  45. #include <fcntl.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include <signal.h>
  50. #include <time.h>
  51. #include <unistd.h>
  52. #include <netinet/in.h>
  53. #include <arpa/inet.h>
  54. #include "../include/saAis.h"
  55. #include "main.h"
  56. #include "sync.h"
  57. #include "totempg.h"
  58. #include "totem.h"
  59. #include "vsf.h"
  60. #include "../lcr/lcr_ifact.h"
  61. #include "logsys.h"
  62. #include "util.h"
  63. LOGSYS_DECLARE_SUBSYS ("SYNC", LOG_INFO);
  64. #define MESSAGE_REQ_SYNC_BARRIER 0
  65. #define MESSAGE_REQ_SYNC_REQUEST 1
  66. struct barrier_data {
  67. unsigned int nodeid;
  68. int completed;
  69. };
  70. static struct memb_ring_id *sync_ring_id;
  71. static int vsf_none = 0;
  72. static int (*sync_callbacks_retrieve) (int sync_id,
  73. struct sync_callbacks *callack);
  74. static struct sync_callbacks sync_callbacks;
  75. static int sync_processing = 0;
  76. static void (*sync_synchronization_completed) (void);
  77. static int sync_recovery_index = 0;
  78. static void *sync_callback_token_handle = 0;
  79. static void *sync_request_token_handle;
  80. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  81. static struct openais_vsf_iface_ver0 *vsf_iface;
  82. static int sync_barrier_send (struct memb_ring_id *ring_id);
  83. static int sync_start_process (
  84. enum totem_callback_token_type type, void *data);
  85. static void sync_service_init (struct memb_ring_id *ring_id);
  86. static int sync_service_process (
  87. enum totem_callback_token_type type, void *data);
  88. static void sync_deliver_fn (
  89. unsigned int nodeid,
  90. struct iovec *iovec,
  91. int iov_len,
  92. int endian_conversion_required);
  93. static void sync_confchg_fn (
  94. enum totem_configuration_type configuration_type,
  95. unsigned int *member_list, int member_list_entries,
  96. unsigned int *left_list, int left_list_entries,
  97. unsigned int *joined_list, int joined_list_entries,
  98. struct memb_ring_id *ring_id);
  99. static void sync_primary_callback_fn (
  100. unsigned int *view_list,
  101. int view_list_entries,
  102. int primary_designated,
  103. struct memb_ring_id *ring_id);
  104. static struct totempg_group sync_group = {
  105. .group = "sync",
  106. .group_len = 4
  107. };
  108. static totempg_groups_handle sync_group_handle;
  109. static char *service_name;
  110. static struct memb_ring_id deliver_ring_id;
  111. static unsigned int current_members[PROCESSOR_COUNT_MAX];
  112. static unsigned int current_members_cnt;
  113. struct sync_barrier_start {
  114. };
  115. struct sync_request {
  116. uint32_t name_len;
  117. char name[0] __attribute__((aligned(8)));
  118. };
  119. typedef struct sync_msg {
  120. mar_req_header_t header;
  121. struct memb_ring_id ring_id;
  122. union {
  123. struct sync_barrier_start sync_barrier_start;
  124. struct sync_request sync_request;
  125. };
  126. } sync_msg_t;
  127. /*
  128. * Send a barrier data structure
  129. */
  130. static int sync_barrier_send (struct memb_ring_id *ring_id)
  131. {
  132. sync_msg_t msg;
  133. struct iovec iovec;
  134. int res;
  135. msg.header.size = sizeof (sync_msg_t);
  136. msg.header.id = MESSAGE_REQ_SYNC_BARRIER;
  137. memcpy (&msg.ring_id, ring_id, sizeof (struct memb_ring_id));
  138. iovec.iov_base = (char *)&msg;
  139. iovec.iov_len = sizeof (msg);
  140. res = totempg_groups_mcast_joined (
  141. sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  142. return (res);
  143. }
  144. static void sync_start_init (struct memb_ring_id *ring_id)
  145. {
  146. ENTER("");
  147. totempg_callback_token_create (
  148. &sync_callback_token_handle,
  149. TOTEM_CALLBACK_TOKEN_SENT,
  150. 0, /* don't delete after callback */
  151. sync_start_process,
  152. (void *)ring_id);
  153. LEAVE("");
  154. }
  155. static void sync_service_init (struct memb_ring_id *ring_id)
  156. {
  157. ENTER("");
  158. sync_callbacks.sync_init ();
  159. totempg_callback_token_destroy (&sync_callback_token_handle);
  160. /*
  161. * Create the token callback for the processing
  162. */
  163. totempg_callback_token_create (
  164. &sync_callback_token_handle,
  165. TOTEM_CALLBACK_TOKEN_SENT,
  166. 0, /* don't delete after callback */
  167. sync_service_process,
  168. (void *)ring_id);
  169. LEAVE("");
  170. }
  171. static int sync_start_process (
  172. enum totem_callback_token_type type, void *data)
  173. {
  174. int res;
  175. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  176. ENTER("");
  177. res = sync_barrier_send (ring_id);
  178. if (res == 0) {
  179. /*
  180. * Delete the token callback for the barrier
  181. */
  182. totempg_callback_token_destroy (&sync_callback_token_handle);
  183. }
  184. LEAVE("");
  185. return (0);
  186. }
  187. static void sync_callbacks_load (void)
  188. {
  189. int res;
  190. ENTER("");
  191. for (;;) {
  192. res = sync_callbacks_retrieve (sync_recovery_index,
  193. &sync_callbacks);
  194. /*
  195. * No more service handlers have sync callbacks at this time
  196. */
  197. if (res == -1) {
  198. sync_processing = 0;
  199. break;
  200. }
  201. if ((service_name != NULL) &&
  202. strcmp (sync_callbacks.name, service_name) != 0) {
  203. sync_recovery_index += 1;
  204. continue;
  205. }
  206. sync_recovery_index += 1;
  207. if (sync_callbacks.sync_init) {
  208. break;
  209. }
  210. }
  211. LEAVE("");
  212. }
  213. static int sync_service_process (
  214. enum totem_callback_token_type type, void *data)
  215. {
  216. int res;
  217. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  218. ENTER("");
  219. /*
  220. * If process operation not from this ring id, then ignore it and stop
  221. * processing
  222. */
  223. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  224. goto end;
  225. }
  226. /*
  227. * If process returns 0, then its time to activate
  228. * and start the next service's synchronization
  229. */
  230. res = sync_callbacks.sync_process ();
  231. if (res != 0) {
  232. goto end;
  233. }
  234. totempg_callback_token_destroy (&sync_callback_token_handle);
  235. sync_start_init (ring_id);
  236. end:
  237. LEAVE("");
  238. return (0);
  239. }
  240. int sync_register (
  241. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  242. void (*synchronization_completed) (void),
  243. char *vsf_type)
  244. {
  245. unsigned int res;
  246. unsigned int vsf_handle;
  247. void *vsf_iface_p;
  248. char openais_vsf_type[1024];
  249. res = totempg_groups_initialize (
  250. &sync_group_handle,
  251. sync_deliver_fn,
  252. sync_confchg_fn);
  253. if (res == -1) {
  254. log_printf (LOG_LEVEL_ERROR,
  255. "Couldn't initialize groups interface.\n");
  256. return (-1);
  257. }
  258. res = totempg_groups_join (
  259. sync_group_handle,
  260. &sync_group,
  261. 1);
  262. if (res == -1) {
  263. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  264. return (-1);
  265. }
  266. if (strcmp (vsf_type, "none") == 0) {
  267. log_printf (LOG_LEVEL_NOTICE,
  268. "Not using a virtual synchrony filter.\n");
  269. vsf_none = 1;
  270. } else {
  271. vsf_none = 0;
  272. sprintf (openais_vsf_type, "openais_vsf_%s", vsf_type);
  273. res = lcr_ifact_reference (
  274. &vsf_handle,
  275. openais_vsf_type,
  276. 0,
  277. &vsf_iface_p,
  278. 0);
  279. if (res == -1) {
  280. log_printf (LOG_LEVEL_NOTICE,
  281. "Couldn't load virtual synchrony filter %s\n",
  282. vsf_type);
  283. return (-1);
  284. }
  285. log_printf (LOG_LEVEL_NOTICE,
  286. "Using virtual synchrony filter %s\n", openais_vsf_type);
  287. vsf_iface = (struct openais_vsf_iface_ver0 *)vsf_iface_p;
  288. vsf_iface->init (sync_primary_callback_fn);
  289. }
  290. sync_callbacks_retrieve = callbacks_retrieve;
  291. sync_synchronization_completed = synchronization_completed;
  292. return (0);
  293. }
  294. static void sync_primary_callback_fn (
  295. unsigned int *view_list,
  296. int view_list_entries,
  297. int primary_designated,
  298. struct memb_ring_id *ring_id)
  299. {
  300. int i;
  301. ENTER("");
  302. if (primary_designated) {
  303. log_printf (LOG_LEVEL_NOTICE,
  304. "This node is within the primary component and will provide"
  305. " service.\n");
  306. } else {
  307. log_printf (LOG_LEVEL_NOTICE,
  308. "This node is within the non-primary component and will NOT"
  309. " provide any services.\n");
  310. return;
  311. }
  312. /*
  313. * Execute configuration change for synchronization service
  314. */
  315. sync_processing = 1;
  316. totempg_callback_token_destroy (&sync_callback_token_handle);
  317. sync_recovery_index = 0;
  318. for (i = 0; i < view_list_entries; i++) {
  319. barrier_data_process[i].nodeid = view_list[i];
  320. barrier_data_process[i].completed = 0;
  321. }
  322. sync_start_init (sync_ring_id);
  323. LEAVE("");
  324. }
  325. static void sync_deliver_fn (
  326. unsigned int nodeid,
  327. struct iovec *iovec,
  328. int iov_len,
  329. int endian_conversion_required)
  330. {
  331. int i;
  332. int barrier_completed;
  333. sync_msg_t *msg = (sync_msg_t *)iovec[0].iov_base;
  334. ENTER("type %d, len %d", msg->header.id, (int)iovec[0].iov_len);
  335. if (endian_conversion_required) {
  336. swab_mar_req_header_t (&msg->header);
  337. swab_memb_ring_id_t (&msg->ring_id);
  338. }
  339. /*
  340. * If this message is not from this configuration, ignore it
  341. */
  342. if (memcmp (&msg->ring_id, sync_ring_id,
  343. sizeof (struct memb_ring_id)) != 0) {
  344. goto end;
  345. }
  346. if (msg->header.id == MESSAGE_REQ_SYNC_REQUEST) {
  347. if (endian_conversion_required) {
  348. swab_mar_uint32_t (&msg->sync_request.name_len);
  349. }
  350. /*
  351. * If there is an ongoing sync, abort it. A requested sync is
  352. * only allowed to abort other requested synchronizations,
  353. * not full synchronizations.
  354. */
  355. if (sync_processing && sync_callbacks.sync_abort) {
  356. sync_callbacks.sync_abort();
  357. sync_callbacks.sync_activate = NULL;
  358. sync_processing = 0;
  359. assert (service_name != NULL);
  360. free (service_name);
  361. service_name = NULL;
  362. }
  363. service_name = malloc (msg->sync_request.name_len);
  364. strcpy (service_name, msg->sync_request.name);
  365. /*
  366. * Start requested synchronization
  367. */
  368. sync_primary_callback_fn (current_members, current_members_cnt, 1,
  369. sync_ring_id);
  370. goto end;
  371. }
  372. barrier_completed = 1;
  373. memcpy (&deliver_ring_id, &msg->ring_id, sizeof (struct memb_ring_id));
  374. /*
  375. * Set completion for source_addr's address
  376. */
  377. for (i = 0; i < current_members_cnt; i++) {
  378. if (nodeid == barrier_data_process[i].nodeid) {
  379. barrier_data_process[i].completed = 1;
  380. log_printf (LOG_LEVEL_DEBUG,
  381. "Barrier Start Received From %d\n",
  382. barrier_data_process[i].nodeid);
  383. break;
  384. }
  385. }
  386. /*
  387. * Test if barrier is complete
  388. */
  389. for (i = 0; i < current_members_cnt; i++) {
  390. log_printf (LOG_LEVEL_DEBUG,
  391. "Barrier completion status for nodeid %d = %d. \n",
  392. barrier_data_process[i].nodeid,
  393. barrier_data_process[i].completed);
  394. if (barrier_data_process[i].completed == 0) {
  395. barrier_completed = 0;
  396. }
  397. }
  398. if (barrier_completed) {
  399. log_printf (LOG_LEVEL_DEBUG, "Synchronization barrier completed\n");
  400. /*
  401. * This sync is complete so activate and start next service sync
  402. */
  403. if (sync_callbacks.sync_activate) {
  404. log_printf (LOG_LEVEL_DEBUG,
  405. "Committing synchronization for (%s)\n",
  406. sync_callbacks.name);
  407. sync_callbacks.sync_activate ();
  408. }
  409. /*
  410. * Start synchronization if the barrier has completed
  411. */
  412. for (i = 0; i < current_members_cnt; i++) {
  413. barrier_data_process[i].nodeid = current_members[i];
  414. barrier_data_process[i].completed = 0;
  415. }
  416. sync_callbacks_load();
  417. /*
  418. * if sync service found, execute it
  419. */
  420. if (sync_processing && sync_callbacks.sync_init) {
  421. log_printf (LOG_LEVEL_DEBUG,
  422. "Synchronization actions starting for (%s)\n",
  423. sync_callbacks.name);
  424. sync_service_init (&deliver_ring_id);
  425. } else {
  426. if (service_name != NULL) {
  427. free (service_name);
  428. service_name = NULL;
  429. }
  430. }
  431. }
  432. end:
  433. LEAVE("");
  434. }
  435. static void sync_confchg_fn (
  436. enum totem_configuration_type configuration_type,
  437. unsigned int *member_list, int member_list_entries,
  438. unsigned int *left_list, int left_list_entries,
  439. unsigned int *joined_list, int joined_list_entries,
  440. struct memb_ring_id *ring_id)
  441. {
  442. int i;
  443. ENTER("");
  444. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  445. LEAVE("");
  446. return;
  447. }
  448. /*
  449. * Save current members and ring ID for later use
  450. */
  451. for (i = 0; i < member_list_entries; i++) {
  452. current_members[i] = member_list[i];
  453. }
  454. current_members_cnt = member_list_entries;
  455. sync_ring_id = ring_id;
  456. /*
  457. * If no virtual synchrony filter configured.
  458. */
  459. if (vsf_none == 1) {
  460. /*
  461. * If there is an ongoing synchronization, abort it.
  462. */
  463. if (sync_processing && sync_callbacks.sync_abort) {
  464. sync_callbacks.sync_abort();
  465. sync_callbacks.sync_activate = NULL;
  466. sync_processing = 0;
  467. if (service_name != NULL) {
  468. free (service_name);
  469. service_name = NULL;
  470. }
  471. }
  472. /*
  473. * Start new synchronization process
  474. */
  475. sync_primary_callback_fn (
  476. member_list, member_list_entries, 1, ring_id);
  477. }
  478. LEAVE("");
  479. }
  480. /**
  481. * TOTEM callback function used to multicast a sync_request
  482. * message
  483. * @param type
  484. * @param _name
  485. *
  486. * @return int
  487. */
  488. static int sync_request_send (
  489. enum totem_callback_token_type type, void *_name)
  490. {
  491. int res;
  492. char *name = _name;
  493. sync_msg_t msg;
  494. struct iovec iovec[2];
  495. int name_len;
  496. ENTER("'%s'", name);
  497. name_len = strlen (name) + 1;
  498. msg.header.size = sizeof (msg) + name_len;
  499. msg.header.id = MESSAGE_REQ_SYNC_REQUEST;
  500. memcpy (&msg.ring_id, sync_ring_id, sizeof (struct memb_ring_id));
  501. msg.sync_request.name_len = name_len;
  502. iovec[0].iov_base = (char *)&msg;
  503. iovec[0].iov_len = sizeof (msg);
  504. iovec[1].iov_base = _name;
  505. iovec[1].iov_len = name_len;
  506. res = totempg_groups_mcast_joined (
  507. sync_group_handle, iovec, 2, TOTEMPG_AGREED);
  508. if (res == 0) {
  509. /*
  510. * We managed to multicast the message so delete the token callback
  511. * for the sync request.
  512. */
  513. totempg_callback_token_destroy (&sync_request_token_handle);
  514. }
  515. /*
  516. * if we failed to multicast the message, this function will be called
  517. * again.
  518. */
  519. LEAVE("");
  520. return (0);
  521. }
  522. int sync_in_process (void)
  523. {
  524. return (sync_processing);
  525. }
  526. int sync_primary_designated (void)
  527. {
  528. if (vsf_none == 1) {
  529. return (1);
  530. } else {
  531. return (vsf_iface->primary());
  532. }
  533. }
  534. /**
  535. * Execute synchronization upon request for the named service
  536. * @param name
  537. *
  538. * @return int
  539. */
  540. int sync_request (char *name)
  541. {
  542. assert (name != NULL);
  543. ENTER("'%s'", name);
  544. if (sync_processing) {
  545. return -1;
  546. }
  547. totempg_callback_token_create (&sync_request_token_handle,
  548. TOTEM_CALLBACK_TOKEN_SENT, 0, /* don't delete after callback */
  549. sync_request_send, name);
  550. LEAVE("");
  551. return 0;
  552. }