vsf_quorum.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (c) 2008, 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@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 Red Hat 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 <pwd.h>
  36. #include <grp.h>
  37. #include <sys/types.h>
  38. #include <sys/poll.h>
  39. #include <sys/uio.h>
  40. #include <sys/mman.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <corosync/corotypes.h>
  55. #include <qb/qbipc_common.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/swab.h>
  58. #include <corosync/list.h>
  59. #include <corosync/mar_gen.h>
  60. #include <corosync/ipc_quorum.h>
  61. #include <corosync/lcr/lcr_comp.h>
  62. #include <corosync/lcr/lcr_ifact.h>
  63. #include <corosync/mar_gen.h>
  64. #include <corosync/coroapi.h>
  65. #include <corosync/logsys.h>
  66. #include <corosync/engine/quorum.h>
  67. #include <corosync/icmap.h>
  68. LOGSYS_DECLARE_SUBSYS ("QUORUM");
  69. struct quorum_pd {
  70. unsigned char track_flags;
  71. int tracking_enabled;
  72. struct list_head list;
  73. void *conn;
  74. };
  75. struct internal_callback_pd {
  76. struct list_head list;
  77. quorum_callback_fn_t callback;
  78. void *context;
  79. };
  80. static void message_handler_req_lib_quorum_getquorate (void *conn,
  81. const void *msg);
  82. static void message_handler_req_lib_quorum_trackstart (void *conn,
  83. const void *msg);
  84. static void message_handler_req_lib_quorum_trackstop (void *conn,
  85. const void *msg);
  86. static void message_handler_req_lib_quorum_gettype (void *conn,
  87. const void *msg);
  88. static void send_library_notification(void *conn);
  89. static void send_internal_notification(void);
  90. static int quorum_exec_init_fn (struct corosync_api_v1 *api);
  91. static int quorum_lib_init_fn (void *conn);
  92. static int quorum_lib_exit_fn (void *conn);
  93. static int primary_designated = 0;
  94. static int quorum_type = 0;
  95. static struct corosync_api_v1 *corosync_api;
  96. static struct list_head lib_trackers_list;
  97. static struct list_head internal_trackers_list;
  98. static struct memb_ring_id quorum_ring_id;
  99. static size_t quorum_view_list_entries = 0;
  100. static int quorum_view_list[PROCESSOR_COUNT_MAX];
  101. struct quorum_services_api_ver1 *quorum_iface = NULL;
  102. static char view_buf[64];
  103. static void log_view_list(const unsigned int *view_list, size_t view_list_entries)
  104. {
  105. int total = (int)view_list_entries;
  106. int len, pos, ret;
  107. int i = 0;
  108. while (1) {
  109. len = sizeof(view_buf);
  110. pos = 0;
  111. memset(view_buf, 0, len);
  112. for (; i < total; i++) {
  113. ret = snprintf(view_buf + pos, len - pos, " %d", view_list[i]);
  114. if (ret >= len - pos)
  115. break;
  116. pos += ret;
  117. }
  118. log_printf (LOGSYS_LEVEL_NOTICE, "Members[%d]:%s%s",
  119. total, view_buf, i < total ? "\\" : "");
  120. if (i == total)
  121. break;
  122. }
  123. }
  124. /* Internal quorum API function */
  125. static void quorum_api_set_quorum(const unsigned int *view_list,
  126. size_t view_list_entries,
  127. int quorum, struct memb_ring_id *ring_id)
  128. {
  129. int old_quorum = primary_designated;
  130. primary_designated = quorum;
  131. if (primary_designated && !old_quorum) {
  132. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  133. } else if (!primary_designated && old_quorum) {
  134. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  135. }
  136. quorum_view_list_entries = view_list_entries;
  137. memcpy(&quorum_ring_id, ring_id, sizeof (quorum_ring_id));
  138. memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
  139. log_view_list(view_list, view_list_entries);
  140. /* Tell internal listeners */
  141. send_internal_notification();
  142. /* Tell IPC listeners */
  143. send_library_notification(NULL);
  144. }
  145. static struct corosync_lib_handler quorum_lib_service[] =
  146. {
  147. { /* 0 */
  148. .lib_handler_fn = message_handler_req_lib_quorum_getquorate,
  149. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  150. },
  151. { /* 1 */
  152. .lib_handler_fn = message_handler_req_lib_quorum_trackstart,
  153. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  154. },
  155. { /* 2 */
  156. .lib_handler_fn = message_handler_req_lib_quorum_trackstop,
  157. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  158. },
  159. { /* 3 */
  160. .lib_handler_fn = message_handler_req_lib_quorum_gettype,
  161. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  162. }
  163. };
  164. static struct corosync_service_engine quorum_service_handler = {
  165. .name = "corosync cluster quorum service v0.1",
  166. .id = QUORUM_SERVICE,
  167. .private_data_size = sizeof (struct quorum_pd),
  168. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  169. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  170. .lib_init_fn = quorum_lib_init_fn,
  171. .lib_exit_fn = quorum_lib_exit_fn,
  172. .lib_engine = quorum_lib_service,
  173. .exec_init_fn = quorum_exec_init_fn,
  174. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  175. .sync_mode = CS_SYNC_V1
  176. };
  177. static struct lcr_iface corosync_quorum_ver0[1] = {
  178. {
  179. .name = "corosync_quorum",
  180. .version = 0,
  181. .versions_replace = 0,
  182. .versions_replace_count = 0,
  183. .dependencies = 0,
  184. .dependency_count = 0,
  185. .constructor = NULL,
  186. .destructor = NULL,
  187. .interfaces = NULL,
  188. },
  189. };
  190. static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
  191. {
  192. return (&quorum_service_handler);
  193. }
  194. static struct lcr_comp quorum_comp_ver0 = {
  195. .iface_count = 1,
  196. .ifaces = corosync_quorum_ver0
  197. };
  198. static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
  199. .corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
  200. };
  201. #ifdef COROSYNC_SOLARIS
  202. void corosync_lcr_component_register (void);
  203. void corosync_lcr_component_register (void) {
  204. #else
  205. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  206. #endif
  207. lcr_component_register (&quorum_comp_ver0);
  208. lcr_interfaces_set (&corosync_quorum_ver0[0], &quorum_service_handler_iface);
  209. }
  210. /* -------------------------------------------------- */
  211. /*
  212. * Internal API functions for corosync
  213. */
  214. static int quorum_quorate(void)
  215. {
  216. return primary_designated;
  217. }
  218. static int quorum_register_callback(quorum_callback_fn_t function, void *context)
  219. {
  220. struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
  221. if (!pd)
  222. return -1;
  223. pd->context = context;
  224. pd->callback = function;
  225. list_add (&pd->list, &internal_trackers_list);
  226. return 0;
  227. }
  228. static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
  229. {
  230. struct internal_callback_pd *pd;
  231. struct list_head *tmp;
  232. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  233. pd = list_entry(tmp, struct internal_callback_pd, list);
  234. if (pd->callback == function && pd->context == context) {
  235. list_del(&pd->list);
  236. return 0;
  237. }
  238. }
  239. return -1;
  240. }
  241. static struct quorum_callin_functions callins = {
  242. .quorate = quorum_quorate,
  243. .register_callback = quorum_register_callback,
  244. .unregister_callback = quorum_unregister_callback
  245. };
  246. /* --------------------------------------------------------------------- */
  247. static int quorum_exec_init_fn (struct corosync_api_v1 *api)
  248. {
  249. hdb_handle_t q_handle;
  250. char *quorum_module;
  251. int res;
  252. void *quorum_iface_p;
  253. void* _start;
  254. void* _stop;
  255. #ifdef COROSYNC_SOLARIS
  256. logsys_subsys_init();
  257. #endif
  258. corosync_api = api;
  259. list_init (&lib_trackers_list);
  260. list_init (&internal_trackers_list);
  261. /*
  262. * Tell corosync we have a quorum engine.
  263. */
  264. api->quorum_initialize(&callins);
  265. /*
  266. * Look for a quorum provider
  267. */
  268. if (icmap_get_string("quorum.provider", &quorum_module) == CS_OK) {
  269. res = lcr_ifact_reference (
  270. &q_handle,
  271. quorum_module,
  272. 0,
  273. &quorum_iface_p,
  274. 0);
  275. if (res == -1) {
  276. log_printf (LOGSYS_LEVEL_NOTICE,
  277. "Couldn't load quorum provider %s\n",
  278. quorum_module);
  279. free(quorum_module);
  280. return (-1);
  281. }
  282. log_printf (LOGSYS_LEVEL_NOTICE,
  283. "Using quorum provider %s\n", quorum_module);
  284. /*
  285. * Register the log sites with libqb
  286. */
  287. _start = lcr_ifact_addr_get(q_handle, "__start___verbose");
  288. _stop = lcr_ifact_addr_get(q_handle, "__stop___verbose");
  289. qb_log_callsites_register(_start, _stop);
  290. quorum_iface = (struct quorum_services_api_ver1 *)quorum_iface_p;
  291. quorum_iface->init (api, quorum_api_set_quorum);
  292. quorum_type = 1;
  293. free(quorum_module);
  294. }
  295. if (!quorum_iface) {
  296. /*
  297. * With no quorum provider, we are always quorate
  298. */
  299. primary_designated = 1;
  300. quorum_type = 0;
  301. }
  302. return (0);
  303. }
  304. static int quorum_lib_init_fn (void *conn)
  305. {
  306. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  307. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
  308. list_init (&pd->list);
  309. pd->conn = conn;
  310. return (0);
  311. }
  312. static int quorum_lib_exit_fn (void *conn)
  313. {
  314. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  315. log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p\n", conn);
  316. if (quorum_pd->tracking_enabled) {
  317. list_del (&quorum_pd->list);
  318. list_init (&quorum_pd->list);
  319. }
  320. return (0);
  321. }
  322. static void send_internal_notification(void)
  323. {
  324. struct list_head *tmp;
  325. struct internal_callback_pd *pd;
  326. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  327. pd = list_entry(tmp, struct internal_callback_pd, list);
  328. pd->callback(primary_designated, pd->context);
  329. }
  330. }
  331. static void send_library_notification(void *conn)
  332. {
  333. int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
  334. char buf[size];
  335. struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
  336. struct list_head *tmp;
  337. int i;
  338. log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d\n", conn, size);
  339. res_lib_quorum_notification->quorate = primary_designated;
  340. res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
  341. res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
  342. for (i=0; i<quorum_view_list_entries; i++) {
  343. res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
  344. }
  345. res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
  346. res_lib_quorum_notification->header.size = size;
  347. res_lib_quorum_notification->header.error = CS_OK;
  348. /* Send it to all interested parties */
  349. if (conn) {
  350. corosync_api->ipc_dispatch_send(conn, res_lib_quorum_notification, size);
  351. }
  352. else {
  353. struct quorum_pd *qpd;
  354. for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
  355. qpd = list_entry(tmp, struct quorum_pd, list);
  356. corosync_api->ipc_dispatch_send(qpd->conn,
  357. res_lib_quorum_notification, size);
  358. }
  359. }
  360. return;
  361. }
  362. static void message_handler_req_lib_quorum_getquorate (void *conn,
  363. const void *msg)
  364. {
  365. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  366. log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p\n", conn);
  367. /* send status */
  368. res_lib_quorum_getquorate.quorate = primary_designated;
  369. res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
  370. res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
  371. res_lib_quorum_getquorate.header.error = CS_OK;
  372. corosync_api->ipc_response_send(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
  373. }
  374. static void message_handler_req_lib_quorum_trackstart (void *conn,
  375. const void *msg)
  376. {
  377. const struct req_lib_quorum_trackstart *req_lib_quorum_trackstart = msg;
  378. struct qb_ipc_response_header res;
  379. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  380. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p\n", conn);
  381. /*
  382. * If an immediate listing of the current cluster membership
  383. * is requested, generate membership list
  384. */
  385. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  386. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  387. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  388. send_library_notification(conn);
  389. }
  390. /*
  391. * Record requests for tracking
  392. */
  393. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  394. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  395. quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
  396. quorum_pd->tracking_enabled = 1;
  397. list_add (&quorum_pd->list, &lib_trackers_list);
  398. }
  399. /* send status */
  400. res.size = sizeof(res);
  401. res.id = MESSAGE_RES_QUORUM_TRACKSTART;
  402. res.error = CS_OK;
  403. corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
  404. }
  405. static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
  406. {
  407. struct qb_ipc_response_header res;
  408. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  409. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p\n", conn);
  410. if (quorum_pd->tracking_enabled) {
  411. res.error = CS_OK;
  412. quorum_pd->tracking_enabled = 0;
  413. list_del (&quorum_pd->list);
  414. list_init (&quorum_pd->list);
  415. } else {
  416. res.error = CS_ERR_NOT_EXIST;
  417. }
  418. /* send status */
  419. res.size = sizeof(res);
  420. res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
  421. res.error = CS_OK;
  422. corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
  423. }
  424. static void message_handler_req_lib_quorum_gettype (void *conn,
  425. const void *msg)
  426. {
  427. struct res_lib_quorum_gettype res_lib_quorum_gettype;
  428. log_printf(LOGSYS_LEVEL_DEBUG, "got quorum_type request on %p\n", conn);
  429. /* send status */
  430. res_lib_quorum_gettype.quorum_type = quorum_type;
  431. res_lib_quorum_gettype.header.size = sizeof(res_lib_quorum_gettype);
  432. res_lib_quorum_gettype.header.id = MESSAGE_RES_QUORUM_GETTYPE;
  433. res_lib_quorum_gettype.header.error = CS_OK;
  434. corosync_api->ipc_response_send(conn, &res_lib_quorum_gettype, sizeof(res_lib_quorum_gettype));
  435. }