sync.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Copyright (c) 2009-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  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 <time.h>
  47. #include <unistd.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <corosync/corotypes.h>
  51. #include <corosync/swab.h>
  52. #include <corosync/totem/totempg.h>
  53. #include <corosync/totem/totem.h>
  54. #include <corosync/logsys.h>
  55. #include <qb/qbipc_common.h>
  56. #include "schedwrk.h"
  57. #include "quorum.h"
  58. #include "sync.h"
  59. #include "main.h"
  60. LOGSYS_DECLARE_SUBSYS ("SYNC");
  61. #define MESSAGE_REQ_SYNC_BARRIER 0
  62. #define MESSAGE_REQ_SYNC_SERVICE_BUILD 1
  63. #define MESSAGE_REQ_SYNC_MEMB_DETERMINE 2
  64. enum sync_process_state {
  65. INIT,
  66. PROCESS,
  67. ACTIVATE
  68. };
  69. enum sync_state {
  70. SYNC_SERVICELIST_BUILD,
  71. SYNC_PROCESS,
  72. SYNC_BARRIER
  73. };
  74. struct service_entry {
  75. int service_id;
  76. void (*sync_init) (
  77. const unsigned int *trans_list,
  78. size_t trans_list_entries,
  79. const unsigned int *member_list,
  80. size_t member_list_entries,
  81. const struct memb_ring_id *ring_id);
  82. void (*sync_abort) (void);
  83. int (*sync_process) (void);
  84. void (*sync_activate) (void);
  85. enum sync_process_state state;
  86. char name[128];
  87. };
  88. struct processor_entry {
  89. int nodeid;
  90. int received;
  91. };
  92. struct req_exec_memb_determine_message {
  93. struct qb_ipc_request_header header __attribute__((aligned(8)));
  94. struct memb_ring_id ring_id __attribute__((aligned(8)));
  95. };
  96. struct req_exec_service_build_message {
  97. struct qb_ipc_request_header header __attribute__((aligned(8)));
  98. struct memb_ring_id ring_id __attribute__((aligned(8)));
  99. int service_list_entries __attribute__((aligned(8)));
  100. int service_list[128] __attribute__((aligned(8)));
  101. };
  102. struct req_exec_barrier_message {
  103. struct qb_ipc_request_header header __attribute__((aligned(8)));
  104. struct memb_ring_id ring_id __attribute__((aligned(8)));
  105. };
  106. static enum sync_state my_state = SYNC_BARRIER;
  107. static struct memb_ring_id my_ring_id;
  108. static struct memb_ring_id my_memb_determine_ring_id;
  109. static int my_memb_determine = 0;
  110. static unsigned int my_memb_determine_list[PROCESSOR_COUNT_MAX];
  111. static unsigned int my_memb_determine_list_entries = 0;
  112. static int my_processing_idx = 0;
  113. static hdb_handle_t my_schedwrk_handle;
  114. static struct processor_entry my_processor_list[PROCESSOR_COUNT_MAX];
  115. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  116. static unsigned int my_trans_list[PROCESSOR_COUNT_MAX];
  117. static size_t my_member_list_entries = 0;
  118. static size_t my_trans_list_entries = 0;
  119. static int my_processor_list_entries = 0;
  120. static struct service_entry my_service_list[SERVICES_COUNT_MAX];
  121. static int my_service_list_entries = 0;
  122. static const struct memb_ring_id sync_ring_id;
  123. static void (*sync_synchronization_completed) (void);
  124. static void sync_deliver_fn (
  125. unsigned int nodeid,
  126. const void *msg,
  127. unsigned int msg_len,
  128. int endian_conversion_required);
  129. static int schedwrk_processor (const void *context);
  130. static void sync_process_enter (void);
  131. static struct totempg_group sync_group = {
  132. .group = "sync",
  133. .group_len = 4
  134. };
  135. static void *sync_group_handle;
  136. int (*my_sync_callbacks_retrieve) (
  137. int service_id,
  138. struct sync_callbacks *callbacks);
  139. int sync_init (
  140. int (*sync_callbacks_retrieve) (
  141. int service_id,
  142. struct sync_callbacks *callbacks),
  143. void (*synchronization_completed) (void))
  144. {
  145. unsigned int res;
  146. res = totempg_groups_initialize (
  147. &sync_group_handle,
  148. sync_deliver_fn,
  149. NULL);
  150. if (res == -1) {
  151. log_printf (LOGSYS_LEVEL_ERROR,
  152. "Couldn't initialize groups interface.");
  153. return (-1);
  154. }
  155. res = totempg_groups_join (
  156. sync_group_handle,
  157. &sync_group,
  158. 1);
  159. if (res == -1) {
  160. log_printf (LOGSYS_LEVEL_ERROR, "Couldn't join group.");
  161. return (-1);
  162. }
  163. sync_synchronization_completed = synchronization_completed;
  164. my_sync_callbacks_retrieve = sync_callbacks_retrieve;
  165. return (0);
  166. }
  167. static void sync_barrier_handler (unsigned int nodeid, const void *msg)
  168. {
  169. const struct req_exec_barrier_message *req_exec_barrier_message = msg;
  170. int i;
  171. int barrier_reached = 1;
  172. if (memcmp (&my_ring_id, &req_exec_barrier_message->ring_id,
  173. sizeof (struct memb_ring_id)) != 0) {
  174. log_printf (LOGSYS_LEVEL_DEBUG, "barrier for old ring - discarding");
  175. return;
  176. }
  177. for (i = 0; i < my_processor_list_entries; i++) {
  178. if (my_processor_list[i].nodeid == nodeid) {
  179. my_processor_list[i].received = 1;
  180. }
  181. }
  182. for (i = 0; i < my_processor_list_entries; i++) {
  183. if (my_processor_list[i].received == 0) {
  184. barrier_reached = 0;
  185. }
  186. }
  187. if (barrier_reached) {
  188. log_printf (LOGSYS_LEVEL_DEBUG, "Committing synchronization for %s",
  189. my_service_list[my_processing_idx].name);
  190. my_service_list[my_processing_idx].state = ACTIVATE;
  191. if (my_sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  192. my_service_list[my_processing_idx].sync_activate ();
  193. }
  194. my_processing_idx += 1;
  195. if (my_service_list_entries == my_processing_idx) {
  196. my_memb_determine_list_entries = 0;
  197. sync_synchronization_completed ();
  198. } else {
  199. sync_process_enter ();
  200. }
  201. }
  202. }
  203. static void dummy_sync_init (
  204. const unsigned int *trans_list,
  205. size_t trans_list_entries,
  206. const unsigned int *member_list,
  207. size_t member_list_entries,
  208. const struct memb_ring_id *ring_id)
  209. {
  210. }
  211. static void dummy_sync_abort (void)
  212. {
  213. }
  214. static int dummy_sync_process (void)
  215. {
  216. return (0);
  217. }
  218. static void dummy_sync_activate (void)
  219. {
  220. }
  221. static int service_entry_compare (const void *a, const void *b)
  222. {
  223. const struct service_entry *service_entry_a = a;
  224. const struct service_entry *service_entry_b = b;
  225. return (service_entry_a->service_id > service_entry_b->service_id);
  226. }
  227. static void sync_memb_determine (unsigned int nodeid, const void *msg)
  228. {
  229. const struct req_exec_memb_determine_message *req_exec_memb_determine_message = msg;
  230. int found = 0;
  231. int i;
  232. if (memcmp (&req_exec_memb_determine_message->ring_id,
  233. &my_memb_determine_ring_id, sizeof (struct memb_ring_id)) != 0) {
  234. log_printf (LOGSYS_LEVEL_DEBUG, "memb determine for old ring - discarding");
  235. return;
  236. }
  237. my_memb_determine = 1;
  238. for (i = 0; i < my_memb_determine_list_entries; i++) {
  239. if (my_memb_determine_list[i] == nodeid) {
  240. found = 1;
  241. }
  242. }
  243. if (found == 0) {
  244. my_memb_determine_list[my_memb_determine_list_entries] = nodeid;
  245. my_memb_determine_list_entries += 1;
  246. }
  247. }
  248. static void sync_service_build_handler (unsigned int nodeid, const void *msg)
  249. {
  250. const struct req_exec_service_build_message *req_exec_service_build_message = msg;
  251. int i, j;
  252. int barrier_reached = 1;
  253. int found;
  254. int qsort_trigger = 0;
  255. if (memcmp (&my_ring_id, &req_exec_service_build_message->ring_id,
  256. sizeof (struct memb_ring_id)) != 0) {
  257. log_printf (LOGSYS_LEVEL_DEBUG, "service build for old ring - discarding");
  258. return;
  259. }
  260. for (i = 0; i < req_exec_service_build_message->service_list_entries; i++) {
  261. found = 0;
  262. for (j = 0; j < my_service_list_entries; j++) {
  263. if (req_exec_service_build_message->service_list[i] ==
  264. my_service_list[j].service_id) {
  265. found = 1;
  266. break;
  267. }
  268. }
  269. if (found == 0) {
  270. my_service_list[my_service_list_entries].state =
  271. INIT;
  272. my_service_list[my_service_list_entries].service_id =
  273. req_exec_service_build_message->service_list[i];
  274. sprintf (my_service_list[my_service_list_entries].name,
  275. "Uknown External Service (id = %d)\n",
  276. req_exec_service_build_message->service_list[i]);
  277. my_service_list[my_service_list_entries].sync_init =
  278. dummy_sync_init;
  279. my_service_list[my_service_list_entries].sync_abort =
  280. dummy_sync_abort;
  281. my_service_list[my_service_list_entries].sync_process =
  282. dummy_sync_process;
  283. my_service_list[my_service_list_entries].sync_activate =
  284. dummy_sync_activate;
  285. my_service_list_entries += 1;
  286. qsort_trigger = 1;
  287. }
  288. }
  289. if (qsort_trigger) {
  290. qsort (my_service_list, my_service_list_entries,
  291. sizeof (struct service_entry), service_entry_compare);
  292. }
  293. for (i = 0; i < my_processor_list_entries; i++) {
  294. if (my_processor_list[i].nodeid == nodeid) {
  295. my_processor_list[i].received = 1;
  296. }
  297. }
  298. for (i = 0; i < my_processor_list_entries; i++) {
  299. if (my_processor_list[i].received == 0) {
  300. barrier_reached = 0;
  301. }
  302. }
  303. if (barrier_reached) {
  304. sync_process_enter ();
  305. }
  306. }
  307. static void sync_deliver_fn (
  308. unsigned int nodeid,
  309. const void *msg,
  310. unsigned int msg_len,
  311. int endian_conversion_required)
  312. {
  313. struct qb_ipc_request_header *header = (struct qb_ipc_request_header *)msg;
  314. switch (header->id) {
  315. case MESSAGE_REQ_SYNC_BARRIER:
  316. sync_barrier_handler (nodeid, msg);
  317. break;
  318. case MESSAGE_REQ_SYNC_SERVICE_BUILD:
  319. sync_service_build_handler (nodeid, msg);
  320. break;
  321. case MESSAGE_REQ_SYNC_MEMB_DETERMINE:
  322. sync_memb_determine (nodeid, msg);
  323. break;
  324. }
  325. }
  326. static void memb_determine_message_transmit (void)
  327. {
  328. struct iovec iovec;
  329. struct req_exec_memb_determine_message req_exec_memb_determine_message;
  330. req_exec_memb_determine_message.header.size = sizeof (struct req_exec_memb_determine_message);
  331. req_exec_memb_determine_message.header.id = MESSAGE_REQ_SYNC_MEMB_DETERMINE;
  332. memcpy (&req_exec_memb_determine_message.ring_id,
  333. &my_memb_determine_ring_id,
  334. sizeof (struct memb_ring_id));
  335. iovec.iov_base = (char *)&req_exec_memb_determine_message;
  336. iovec.iov_len = sizeof (req_exec_memb_determine_message);
  337. (void)totempg_groups_mcast_joined (sync_group_handle,
  338. &iovec, 1, TOTEMPG_AGREED);
  339. }
  340. static void barrier_message_transmit (void)
  341. {
  342. struct iovec iovec;
  343. struct req_exec_barrier_message req_exec_barrier_message;
  344. req_exec_barrier_message.header.size = sizeof (struct req_exec_barrier_message);
  345. req_exec_barrier_message.header.id = MESSAGE_REQ_SYNC_BARRIER;
  346. memcpy (&req_exec_barrier_message.ring_id, &my_ring_id,
  347. sizeof (struct memb_ring_id));
  348. iovec.iov_base = (char *)&req_exec_barrier_message;
  349. iovec.iov_len = sizeof (req_exec_barrier_message);
  350. (void)totempg_groups_mcast_joined (sync_group_handle,
  351. &iovec, 1, TOTEMPG_AGREED);
  352. }
  353. static void service_build_message_transmit (struct req_exec_service_build_message *service_build_message)
  354. {
  355. struct iovec iovec;
  356. service_build_message->header.size = sizeof (struct req_exec_service_build_message);
  357. service_build_message->header.id = MESSAGE_REQ_SYNC_SERVICE_BUILD;
  358. memcpy (&service_build_message->ring_id, &my_ring_id,
  359. sizeof (struct memb_ring_id));
  360. iovec.iov_base = (void *)service_build_message;
  361. iovec.iov_len = sizeof (struct req_exec_service_build_message);
  362. (void)totempg_groups_mcast_joined (sync_group_handle,
  363. &iovec, 1, TOTEMPG_AGREED);
  364. }
  365. static void sync_barrier_enter (void)
  366. {
  367. my_state = SYNC_BARRIER;
  368. barrier_message_transmit ();
  369. }
  370. static void sync_process_enter (void)
  371. {
  372. int i;
  373. my_state = SYNC_PROCESS;
  374. /*
  375. * No sync services
  376. */
  377. if (my_service_list_entries == 0) {
  378. my_state = SYNC_SERVICELIST_BUILD;
  379. my_memb_determine_list_entries = 0;
  380. sync_synchronization_completed ();
  381. return;
  382. }
  383. for (i = 0; i < my_processor_list_entries; i++) {
  384. my_processor_list[i].received = 0;
  385. }
  386. schedwrk_create (&my_schedwrk_handle,
  387. schedwrk_processor,
  388. NULL);
  389. }
  390. static void sync_servicelist_build_enter (
  391. const unsigned int *member_list,
  392. size_t member_list_entries,
  393. const struct memb_ring_id *ring_id)
  394. {
  395. struct req_exec_service_build_message service_build;
  396. int i;
  397. int res;
  398. struct sync_callbacks sync_callbacks;
  399. my_state = SYNC_SERVICELIST_BUILD;
  400. for (i = 0; i < member_list_entries; i++) {
  401. my_processor_list[i].nodeid = member_list[i];
  402. my_processor_list[i].received = 0;
  403. }
  404. my_processor_list_entries = member_list_entries;
  405. memcpy (my_member_list, member_list,
  406. member_list_entries * sizeof (unsigned int));
  407. my_member_list_entries = member_list_entries;
  408. my_processing_idx = 0;
  409. memset(my_service_list, 0, sizeof (struct service_entry) * SERVICES_COUNT_MAX);
  410. my_service_list_entries = 0;
  411. for (i = 0; i < SERVICES_COUNT_MAX; i++) {
  412. res = my_sync_callbacks_retrieve (i, &sync_callbacks);
  413. if (res == -1) {
  414. continue;
  415. }
  416. if (sync_callbacks.sync_init == NULL) {
  417. continue;
  418. }
  419. my_service_list[my_service_list_entries].state = INIT;
  420. my_service_list[my_service_list_entries].service_id = i;
  421. strcpy (my_service_list[my_service_list_entries].name,
  422. sync_callbacks.name);
  423. my_service_list[my_service_list_entries].sync_init = sync_callbacks.sync_init;
  424. my_service_list[my_service_list_entries].sync_process = sync_callbacks.sync_process;
  425. my_service_list[my_service_list_entries].sync_abort = sync_callbacks.sync_abort;
  426. my_service_list[my_service_list_entries].sync_activate = sync_callbacks.sync_activate;
  427. my_service_list_entries += 1;
  428. }
  429. for (i = 0; i < my_service_list[i].service_id; i++) {
  430. service_build.service_list[i] =
  431. my_service_list[i].service_id;
  432. }
  433. service_build.service_list_entries = i;
  434. service_build_message_transmit (&service_build);
  435. }
  436. static int schedwrk_processor (const void *context)
  437. {
  438. int res = 0;
  439. if (my_service_list[my_processing_idx].state == INIT) {
  440. unsigned int old_trans_list[PROCESSOR_COUNT_MAX];
  441. size_t old_trans_list_entries = 0;
  442. int o, m;
  443. my_service_list[my_processing_idx].state = PROCESS;
  444. memcpy (old_trans_list, my_trans_list, my_trans_list_entries *
  445. sizeof (unsigned int));
  446. old_trans_list_entries = my_trans_list_entries;
  447. my_trans_list_entries = 0;
  448. for (o = 0; o < old_trans_list_entries; o++) {
  449. for (m = 0; m < my_member_list_entries; m++) {
  450. if (old_trans_list[o] == my_member_list[m]) {
  451. my_trans_list[my_trans_list_entries] = my_member_list[m];
  452. my_trans_list_entries++;
  453. break;
  454. }
  455. }
  456. }
  457. if (my_sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  458. my_service_list[my_processing_idx].sync_init (my_trans_list,
  459. my_trans_list_entries, my_member_list,
  460. my_member_list_entries,
  461. &my_ring_id);
  462. }
  463. }
  464. if (my_service_list[my_processing_idx].state == PROCESS) {
  465. my_service_list[my_processing_idx].state = PROCESS;
  466. if (my_sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  467. res = my_service_list[my_processing_idx].sync_process ();
  468. } else {
  469. res = 0;
  470. }
  471. if (res == 0) {
  472. sync_barrier_enter();
  473. } else {
  474. return (-1);
  475. }
  476. }
  477. return (0);
  478. }
  479. void sync_start (
  480. const unsigned int *member_list,
  481. size_t member_list_entries,
  482. const struct memb_ring_id *ring_id)
  483. {
  484. ENTER();
  485. memcpy (&my_ring_id, ring_id, sizeof (struct memb_ring_id));
  486. if (my_memb_determine) {
  487. my_memb_determine = 0;
  488. sync_servicelist_build_enter (my_memb_determine_list,
  489. my_memb_determine_list_entries, ring_id);
  490. } else {
  491. sync_servicelist_build_enter (member_list, member_list_entries,
  492. ring_id);
  493. }
  494. }
  495. void sync_save_transitional (
  496. const unsigned int *member_list,
  497. size_t member_list_entries,
  498. const struct memb_ring_id *ring_id)
  499. {
  500. ENTER();
  501. memcpy (my_trans_list, member_list, member_list_entries *
  502. sizeof (unsigned int));
  503. my_trans_list_entries = member_list_entries;
  504. }
  505. void sync_abort (void)
  506. {
  507. ENTER();
  508. if (my_state == SYNC_PROCESS) {
  509. schedwrk_destroy (my_schedwrk_handle);
  510. if (my_sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  511. my_service_list[my_processing_idx].sync_abort ();
  512. }
  513. }
  514. /* this will cause any "old" barrier messages from causing
  515. * problems.
  516. */
  517. memset (&my_ring_id, 0, sizeof (struct memb_ring_id));
  518. }
  519. void sync_memb_list_determine (const struct memb_ring_id *ring_id)
  520. {
  521. ENTER();
  522. memcpy (&my_memb_determine_ring_id, ring_id,
  523. sizeof (struct memb_ring_id));
  524. memb_determine_message_transmit ();
  525. }
  526. void sync_memb_list_abort (void)
  527. {
  528. ENTER();
  529. my_memb_determine_list_entries = 0;
  530. memset (&my_memb_determine_ring_id, 0, sizeof (struct memb_ring_id));
  531. }