syncv2.c 13 KB

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