syncv2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (c) 2009 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/lcr/lcr_ifact.h>
  55. #include <corosync/engine/logsys.h>
  56. #include <corosync/coroipc_types.h>
  57. #include "schedwrk.h"
  58. #include "quorum.h"
  59. #include "sync.h"
  60. #include "syncv2.h"
  61. LOGSYS_DECLARE_SUBSYS ("SYNCV2");
  62. #define MESSAGE_REQ_SYNC_BARRIER 0
  63. #define MESSAGE_REQ_SYNC_SERVICE_BUILD 1
  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) (void);
  77. void (*sync_abort) (void);
  78. int (*sync_process) (void);
  79. void (*sync_activate) (void);
  80. enum sync_process_state state;
  81. char name[128];
  82. };
  83. struct processor_entry {
  84. int nodeid;
  85. int received;
  86. };
  87. struct req_exec_service_build_message {
  88. coroipc_request_header_t header;
  89. struct memb_ring_id ring_id;
  90. int service_list[128];
  91. int service_list_entries;
  92. };
  93. struct req_exec_barrier_message {
  94. coroipc_request_header_t header;
  95. struct memb_ring_id ring_id;
  96. };
  97. static enum sync_state my_state = SYNC_BARRIER;
  98. static struct memb_ring_id my_ring_id;
  99. static int my_processing_idx = 0;
  100. static hdb_handle_t my_schedwrk_handle;
  101. static struct processor_entry my_processor_list[128];
  102. static int my_processor_list_entries = 0;
  103. static struct service_entry my_service_list[128];
  104. static int my_service_list_entries = 0;
  105. static const struct memb_ring_id sync_ring_id;
  106. static struct service_entry my_initial_service_list[128];
  107. static int my_initial_service_list_entries;
  108. static void (*sync_synchronization_completed) (void);
  109. static void sync_deliver_fn (
  110. unsigned int nodeid,
  111. const void *msg,
  112. unsigned int msg_len,
  113. int endian_conversion_required);
  114. static int schedwrk_processor (const void *context);
  115. static void sync_process_enter (void);
  116. static struct totempg_group sync_group = {
  117. .group = "syncv2",
  118. .group_len = 6
  119. };
  120. static hdb_handle_t sync_group_handle;
  121. int sync_v2_init (
  122. int (*sync_callbacks_retrieve) (
  123. int service_id,
  124. struct sync_callbacks *callbacks),
  125. void (*synchronization_completed) (void))
  126. {
  127. unsigned int res;
  128. int i;
  129. struct sync_callbacks sync_callbacks;
  130. res = totempg_groups_initialize (
  131. &sync_group_handle,
  132. sync_deliver_fn,
  133. NULL);
  134. if (res == -1) {
  135. log_printf (LOGSYS_LEVEL_ERROR,
  136. "Couldn't initialize groups interface.\n");
  137. return (-1);
  138. }
  139. res = totempg_groups_join (
  140. sync_group_handle,
  141. &sync_group,
  142. 1);
  143. if (res == -1) {
  144. log_printf (LOGSYS_LEVEL_ERROR, "Couldn't join group.\n");
  145. return (-1);
  146. }
  147. sync_synchronization_completed = synchronization_completed;
  148. for (i = 0; i < 64; i++) {
  149. res = sync_callbacks_retrieve (i, &sync_callbacks);
  150. if (res == -1) {
  151. continue;
  152. }
  153. if (sync_callbacks.sync_init == NULL) {
  154. continue;
  155. }
  156. my_initial_service_list[my_initial_service_list_entries].state =
  157. INIT;
  158. my_initial_service_list[my_initial_service_list_entries].service_id = i;
  159. strcpy (my_initial_service_list[my_initial_service_list_entries].name,
  160. sync_callbacks.name);
  161. my_initial_service_list[my_initial_service_list_entries].sync_init = sync_callbacks.sync_init;
  162. my_initial_service_list[my_initial_service_list_entries].sync_process = sync_callbacks.sync_process;
  163. my_initial_service_list[my_initial_service_list_entries].sync_abort = sync_callbacks.sync_abort;
  164. my_initial_service_list[my_initial_service_list_entries].sync_activate = sync_callbacks.sync_activate;
  165. my_initial_service_list_entries += 1;
  166. }
  167. return (0);
  168. }
  169. static void sync_barrier_handler (unsigned int nodeid, const void *msg)
  170. {
  171. const struct req_exec_barrier_message *req_exec_barrier_message = msg;
  172. int i;
  173. int barrier_reached = 1;
  174. if (memcmp (&my_ring_id, &req_exec_barrier_message->ring_id,
  175. sizeof (struct memb_ring_id)) != 0) {
  176. return;
  177. }
  178. for (i = 0; i < my_processor_list_entries; i++) {
  179. if (my_processor_list[i].nodeid == nodeid) {
  180. my_processor_list[i].received = 1;
  181. }
  182. }
  183. for (i = 0; i < my_processor_list_entries; i++) {
  184. if (my_processor_list[i].received == 0) {
  185. barrier_reached = 0;
  186. }
  187. }
  188. if (barrier_reached) {
  189. my_processing_idx += 1;
  190. if (my_service_list_entries == my_processing_idx) {
  191. sync_synchronization_completed ();
  192. } else {
  193. sync_process_enter ();
  194. }
  195. }
  196. }
  197. static void dummy_sync_init (void)
  198. {
  199. }
  200. static void dummy_sync_abort (void)
  201. {
  202. }
  203. static int dummy_sync_process (void)
  204. {
  205. return (0);
  206. }
  207. static void dummy_sync_activate (void)
  208. {
  209. }
  210. static int service_entry_compare (const void *a, const void *b)
  211. {
  212. const struct service_entry *service_entry_a = a;
  213. const struct service_entry *service_entry_b = b;
  214. return (service_entry_a->service_id > service_entry_b->service_id);
  215. }
  216. static void sync_service_build_handler (unsigned int nodeid, const void *msg)
  217. {
  218. const struct req_exec_service_build_message *req_exec_service_build_message = msg;
  219. int i, j;
  220. int barrier_reached = 1;
  221. int found;
  222. int qsort_trigger = 0;
  223. if (memcmp (&my_ring_id, &req_exec_service_build_message->ring_id,
  224. sizeof (struct memb_ring_id)) != 0) {
  225. return;
  226. }
  227. for (i = 0; i < req_exec_service_build_message->service_list_entries; i++) {
  228. found = 0;
  229. for (j = 0; j < my_service_list_entries; j++) {
  230. if (req_exec_service_build_message->service_list[i] ==
  231. my_service_list[j].service_id) {
  232. found = 1;
  233. break;
  234. }
  235. }
  236. if (found == 0) {
  237. my_service_list[my_service_list_entries].state =
  238. INIT;
  239. my_service_list[my_service_list_entries].service_id =
  240. req_exec_service_build_message->service_list[i];
  241. sprintf (my_service_list[my_service_list_entries].name,
  242. "External Service (id = %d)\n",
  243. req_exec_service_build_message->service_list[i]);
  244. my_service_list[my_service_list_entries].sync_init =
  245. dummy_sync_init;
  246. my_service_list[my_service_list_entries].sync_abort =
  247. dummy_sync_abort;
  248. my_service_list[my_service_list_entries].sync_process =
  249. dummy_sync_process;
  250. my_service_list[my_service_list_entries].sync_activate =
  251. dummy_sync_activate;
  252. my_service_list_entries += 1;
  253. qsort_trigger = 1;
  254. }
  255. }
  256. if (qsort_trigger) {
  257. qsort (my_service_list, my_service_list_entries,
  258. sizeof (struct service_entry), service_entry_compare);
  259. }
  260. for (i = 0; i < my_processor_list_entries; i++) {
  261. if (my_processor_list[i].nodeid == nodeid) {
  262. my_processor_list[i].received = 1;
  263. }
  264. }
  265. for (i = 0; i < my_processor_list_entries; i++) {
  266. if (my_processor_list[i].received == 0) {
  267. barrier_reached = 0;
  268. }
  269. }
  270. if (barrier_reached) {
  271. sync_process_enter ();
  272. }
  273. }
  274. static void sync_deliver_fn (
  275. unsigned int nodeid,
  276. const void *msg,
  277. unsigned int msg_len,
  278. int endian_conversion_required)
  279. {
  280. coroipc_request_header_t *header = (coroipc_request_header_t *)msg;
  281. switch (header->id) {
  282. case MESSAGE_REQ_SYNC_BARRIER:
  283. sync_barrier_handler (nodeid, msg);
  284. break;
  285. case MESSAGE_REQ_SYNC_SERVICE_BUILD:
  286. sync_service_build_handler (nodeid, msg);
  287. break;
  288. }
  289. }
  290. static void barrier_message_transmit (void)
  291. {
  292. struct iovec iovec;
  293. struct req_exec_barrier_message req_exec_barrier_message;
  294. int res;
  295. req_exec_barrier_message.header.size = sizeof (struct req_exec_barrier_message);
  296. req_exec_barrier_message.header.id = MESSAGE_REQ_SYNC_BARRIER;
  297. memcpy (&req_exec_barrier_message.ring_id, &my_ring_id,
  298. sizeof (struct memb_ring_id));
  299. iovec.iov_base = (char *)&req_exec_barrier_message;
  300. iovec.iov_len = sizeof (req_exec_barrier_message);
  301. res = totempg_groups_mcast_joined (sync_group_handle,
  302. &iovec, 1, TOTEMPG_AGREED);
  303. }
  304. static void service_build_message_transmit (struct req_exec_service_build_message *service_build_message)
  305. {
  306. struct iovec iovec;
  307. int res;
  308. service_build_message->header.size = sizeof (struct req_exec_service_build_message);
  309. service_build_message->header.id = MESSAGE_REQ_SYNC_SERVICE_BUILD;
  310. memcpy (&service_build_message->ring_id, &my_ring_id,
  311. sizeof (struct memb_ring_id));
  312. iovec.iov_base = (void *)service_build_message;
  313. iovec.iov_len = sizeof (struct req_exec_service_build_message);
  314. res = totempg_groups_mcast_joined (sync_group_handle,
  315. &iovec, 1, TOTEMPG_AGREED);
  316. }
  317. static void sync_barrier_enter (void)
  318. {
  319. my_state = SYNC_BARRIER;
  320. barrier_message_transmit ();
  321. }
  322. static void sync_process_enter (void)
  323. {
  324. int i;
  325. my_state = SYNC_PROCESS;
  326. /*
  327. * No syncv2 services
  328. */
  329. assert (my_service_list_entries);
  330. if (my_service_list_entries == 0) {
  331. my_state = SYNC_SERVICELIST_BUILD;
  332. sync_synchronization_completed ();
  333. return;
  334. }
  335. for (i = 0; i < my_processor_list_entries; i++) {
  336. my_processor_list[i].received = 0;
  337. }
  338. schedwrk_create (&my_schedwrk_handle,
  339. schedwrk_processor,
  340. NULL);
  341. }
  342. static void sync_servicelist_build_enter (
  343. const unsigned int *member_list,
  344. size_t member_list_entries,
  345. const struct memb_ring_id *ring_id)
  346. {
  347. struct req_exec_service_build_message service_build;
  348. int i;
  349. my_state = SYNC_SERVICELIST_BUILD;
  350. for (i = 0; i < member_list_entries; i++) {
  351. my_processor_list[i].nodeid = member_list[i];
  352. my_processor_list[i].received = 0;
  353. }
  354. my_processor_list_entries = member_list_entries;
  355. my_processing_idx = 0;
  356. memcpy (my_service_list, my_initial_service_list,
  357. sizeof (struct service_entry) *
  358. my_initial_service_list_entries);
  359. my_service_list_entries = my_initial_service_list_entries;
  360. for (i = 0; i < my_initial_service_list[i].service_id; i++) {
  361. service_build.service_list[i] =
  362. my_initial_service_list[i].service_id;
  363. }
  364. service_build.service_list_entries = i;
  365. service_build_message_transmit (&service_build);
  366. }
  367. static int schedwrk_processor (const void *context)
  368. {
  369. int res;
  370. if (my_service_list[my_processing_idx].state == INIT) {
  371. my_service_list[my_processing_idx].state = PROCESS;
  372. my_service_list[my_processing_idx].sync_init ();
  373. }
  374. if (my_service_list[my_processing_idx].state == PROCESS) {
  375. my_service_list[my_processing_idx].state = PROCESS;
  376. res = my_service_list[my_processing_idx].sync_process ();
  377. if (res != -1) {
  378. my_service_list[my_processing_idx].state = ACTIVATE;
  379. } else {
  380. return (-1);
  381. }
  382. }
  383. if (my_service_list[my_processing_idx].state == ACTIVATE) {
  384. my_service_list[my_processing_idx].state = ACTIVATE;
  385. my_service_list[my_processing_idx].sync_activate ();
  386. log_printf (LOGSYS_LEVEL_DEBUG, "Committing synchronization for %s\n",
  387. my_service_list[my_processing_idx].name);
  388. sync_barrier_enter();
  389. }
  390. return (0);
  391. }
  392. void sync_v2_start (
  393. const unsigned int *member_list,
  394. size_t member_list_entries,
  395. const struct memb_ring_id *ring_id)
  396. {
  397. memcpy (&my_ring_id, ring_id, sizeof (struct memb_ring_id));
  398. sync_servicelist_build_enter (member_list, member_list_entries, ring_id);
  399. }
  400. void sync_v2_abort (void)
  401. {
  402. if (my_state == SYNC_PROCESS) {
  403. schedwrk_destroy (my_schedwrk_handle);
  404. my_service_list[my_processing_idx].sync_abort ();
  405. }
  406. }