sync.c 15 KB

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