vsf_quorum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 2008-2012 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 "quorum.h"
  55. #include <corosync/corotypes.h>
  56. #include <qb/qbipc_common.h>
  57. #include <corosync/corodefs.h>
  58. #include <corosync/swab.h>
  59. #include <corosync/list.h>
  60. #include <corosync/mar_gen.h>
  61. #include <corosync/ipc_quorum.h>
  62. #include <corosync/mar_gen.h>
  63. #include <corosync/coroapi.h>
  64. #include <corosync/logsys.h>
  65. #include <corosync/icmap.h>
  66. #include "service.h"
  67. #include "votequorum.h"
  68. #include "vsf_ykd.h"
  69. LOGSYS_DECLARE_SUBSYS ("QUORUM");
  70. struct quorum_pd {
  71. unsigned char track_flags;
  72. int tracking_enabled;
  73. struct list_head list;
  74. void *conn;
  75. };
  76. struct internal_callback_pd {
  77. struct list_head list;
  78. quorum_callback_fn_t callback;
  79. void *context;
  80. };
  81. static void message_handler_req_lib_quorum_getquorate (void *conn,
  82. const void *msg);
  83. static void message_handler_req_lib_quorum_trackstart (void *conn,
  84. const void *msg);
  85. static void message_handler_req_lib_quorum_trackstop (void *conn,
  86. const void *msg);
  87. static void message_handler_req_lib_quorum_gettype (void *conn,
  88. const void *msg);
  89. static void send_library_notification(void *conn);
  90. static void send_internal_notification(void);
  91. static char *quorum_exec_init_fn (struct corosync_api_v1 *api);
  92. static int quorum_lib_init_fn (void *conn);
  93. static int quorum_lib_exit_fn (void *conn);
  94. static int primary_designated = 0;
  95. static int quorum_type = 0;
  96. static struct corosync_api_v1 *corosync_api;
  97. static struct list_head lib_trackers_list;
  98. static struct list_head internal_trackers_list;
  99. static struct memb_ring_id quorum_ring_id;
  100. static size_t quorum_view_list_entries = 0;
  101. static int quorum_view_list[PROCESSOR_COUNT_MAX];
  102. struct quorum_services_api_ver1 *quorum_iface = NULL;
  103. static char view_buf[64];
  104. static void log_view_list(const unsigned int *view_list, size_t view_list_entries)
  105. {
  106. int total = (int)view_list_entries;
  107. int len, pos, ret;
  108. int i = 0;
  109. while (1) {
  110. len = sizeof(view_buf);
  111. pos = 0;
  112. memset(view_buf, 0, len);
  113. for (; i < total; i++) {
  114. ret = snprintf(view_buf + pos, len - pos, " %d", view_list[i]);
  115. if (ret >= len - pos)
  116. break;
  117. pos += ret;
  118. }
  119. log_printf (LOGSYS_LEVEL_NOTICE, "Members[%d]:%s%s",
  120. total, view_buf, i < total ? "\\" : "");
  121. if (i == total)
  122. break;
  123. }
  124. }
  125. /* Internal quorum API function */
  126. static void quorum_api_set_quorum(const unsigned int *view_list,
  127. size_t view_list_entries,
  128. int quorum, struct memb_ring_id *ring_id)
  129. {
  130. int old_quorum = primary_designated;
  131. primary_designated = quorum;
  132. if (primary_designated && !old_quorum) {
  133. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.");
  134. } else if (!primary_designated && old_quorum) {
  135. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.");
  136. }
  137. quorum_view_list_entries = view_list_entries;
  138. memcpy(&quorum_ring_id, ring_id, sizeof (quorum_ring_id));
  139. memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
  140. log_view_list(view_list, view_list_entries);
  141. /* Tell internal listeners */
  142. send_internal_notification();
  143. /* Tell IPC listeners */
  144. send_library_notification(NULL);
  145. }
  146. static struct corosync_lib_handler quorum_lib_service[] =
  147. {
  148. { /* 0 */
  149. .lib_handler_fn = message_handler_req_lib_quorum_getquorate,
  150. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  151. },
  152. { /* 1 */
  153. .lib_handler_fn = message_handler_req_lib_quorum_trackstart,
  154. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  155. },
  156. { /* 2 */
  157. .lib_handler_fn = message_handler_req_lib_quorum_trackstop,
  158. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  159. },
  160. { /* 3 */
  161. .lib_handler_fn = message_handler_req_lib_quorum_gettype,
  162. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  163. }
  164. };
  165. static struct corosync_service_engine quorum_service_handler = {
  166. .name = "corosync cluster quorum service v0.1",
  167. .id = QUORUM_SERVICE,
  168. .priority = 1,
  169. .private_data_size = sizeof (struct quorum_pd),
  170. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  171. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  172. .lib_init_fn = quorum_lib_init_fn,
  173. .lib_exit_fn = quorum_lib_exit_fn,
  174. .lib_engine = quorum_lib_service,
  175. .exec_init_fn = quorum_exec_init_fn,
  176. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler)
  177. };
  178. struct corosync_service_engine *vsf_quorum_get_service_engine_ver0 (void)
  179. {
  180. return (&quorum_service_handler);
  181. }
  182. /* -------------------------------------------------- */
  183. /*
  184. * Internal API functions for corosync
  185. */
  186. static int quorum_quorate(void)
  187. {
  188. return primary_designated;
  189. }
  190. static int quorum_register_callback(quorum_callback_fn_t function, void *context)
  191. {
  192. struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
  193. if (!pd)
  194. return -1;
  195. pd->context = context;
  196. pd->callback = function;
  197. list_add (&pd->list, &internal_trackers_list);
  198. return 0;
  199. }
  200. static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
  201. {
  202. struct internal_callback_pd *pd;
  203. struct list_head *tmp;
  204. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  205. pd = list_entry(tmp, struct internal_callback_pd, list);
  206. if (pd->callback == function && pd->context == context) {
  207. list_del(&pd->list);
  208. free(pd);
  209. return 0;
  210. }
  211. }
  212. return -1;
  213. }
  214. static struct quorum_callin_functions callins = {
  215. .quorate = quorum_quorate,
  216. .register_callback = quorum_register_callback,
  217. .unregister_callback = quorum_unregister_callback
  218. };
  219. /* --------------------------------------------------------------------- */
  220. static char *quorum_exec_init_fn (struct corosync_api_v1 *api)
  221. {
  222. char *quorum_module = NULL;
  223. char *error;
  224. corosync_api = api;
  225. list_init (&lib_trackers_list);
  226. list_init (&internal_trackers_list);
  227. /*
  228. * Tell corosync we have a quorum engine.
  229. */
  230. api->quorum_initialize(&callins);
  231. /*
  232. * Look for a quorum provider
  233. */
  234. if (icmap_get_string("quorum.provider", &quorum_module) == CS_OK) {
  235. log_printf (LOGSYS_LEVEL_NOTICE,
  236. "Using quorum provider %s", quorum_module);
  237. error = (char *)"Invalid quorum provider";
  238. if (strcmp (quorum_module, "corosync_votequorum") == 0) {
  239. error = votequorum_init (api, quorum_api_set_quorum);
  240. quorum_type = 1;
  241. }
  242. if (strcmp (quorum_module, "corosync_ykd") == 0) {
  243. error = ykd_init (api, quorum_api_set_quorum);
  244. quorum_type = 1;
  245. }
  246. if (error) {
  247. log_printf (LOGSYS_LEVEL_CRIT,
  248. "Quorum provider: %s failed to initialize.",
  249. quorum_module);
  250. free(quorum_module);
  251. return (error);
  252. }
  253. }
  254. if (quorum_module) {
  255. free(quorum_module);
  256. quorum_module = NULL;
  257. }
  258. /*
  259. * setting quorum_type and primary_designated in the right order is important
  260. * always try to lookup/init a quorum module, then revert back to be quorate
  261. */
  262. if (quorum_type == 0) {
  263. primary_designated = 1;
  264. }
  265. return (NULL);
  266. }
  267. static int quorum_lib_init_fn (void *conn)
  268. {
  269. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  270. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
  271. list_init (&pd->list);
  272. pd->conn = conn;
  273. return (0);
  274. }
  275. static int quorum_lib_exit_fn (void *conn)
  276. {
  277. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  278. log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p", conn);
  279. if (quorum_pd->tracking_enabled) {
  280. list_del (&quorum_pd->list);
  281. list_init (&quorum_pd->list);
  282. }
  283. return (0);
  284. }
  285. static void send_internal_notification(void)
  286. {
  287. struct list_head *tmp;
  288. struct internal_callback_pd *pd;
  289. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  290. pd = list_entry(tmp, struct internal_callback_pd, list);
  291. pd->callback(primary_designated, pd->context);
  292. }
  293. }
  294. static void send_library_notification(void *conn)
  295. {
  296. int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
  297. char buf[size];
  298. struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
  299. struct list_head *tmp;
  300. int i;
  301. log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d", conn, size);
  302. res_lib_quorum_notification->quorate = primary_designated;
  303. res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
  304. res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
  305. for (i=0; i<quorum_view_list_entries; i++) {
  306. res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
  307. }
  308. res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
  309. res_lib_quorum_notification->header.size = size;
  310. res_lib_quorum_notification->header.error = CS_OK;
  311. /* Send it to all interested parties */
  312. if (conn) {
  313. corosync_api->ipc_dispatch_send(conn, res_lib_quorum_notification, size);
  314. }
  315. else {
  316. struct quorum_pd *qpd;
  317. for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
  318. qpd = list_entry(tmp, struct quorum_pd, list);
  319. corosync_api->ipc_dispatch_send(qpd->conn,
  320. res_lib_quorum_notification, size);
  321. }
  322. }
  323. return;
  324. }
  325. static void message_handler_req_lib_quorum_getquorate (void *conn,
  326. const void *msg)
  327. {
  328. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  329. log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p", conn);
  330. /* send status */
  331. res_lib_quorum_getquorate.quorate = primary_designated;
  332. res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
  333. res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
  334. res_lib_quorum_getquorate.header.error = CS_OK;
  335. corosync_api->ipc_response_send(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
  336. }
  337. static void message_handler_req_lib_quorum_trackstart (void *conn,
  338. const void *msg)
  339. {
  340. const struct req_lib_quorum_trackstart *req_lib_quorum_trackstart = msg;
  341. struct qb_ipc_response_header res;
  342. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  343. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p", conn);
  344. /*
  345. * If an immediate listing of the current cluster membership
  346. * is requested, generate membership list
  347. */
  348. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  349. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  350. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  351. send_library_notification(conn);
  352. }
  353. /*
  354. * Record requests for tracking
  355. */
  356. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  357. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  358. quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
  359. quorum_pd->tracking_enabled = 1;
  360. list_add (&quorum_pd->list, &lib_trackers_list);
  361. }
  362. /* send status */
  363. res.size = sizeof(res);
  364. res.id = MESSAGE_RES_QUORUM_TRACKSTART;
  365. res.error = CS_OK;
  366. corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
  367. }
  368. static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
  369. {
  370. struct qb_ipc_response_header res;
  371. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  372. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p", conn);
  373. if (quorum_pd->tracking_enabled) {
  374. res.error = CS_OK;
  375. quorum_pd->tracking_enabled = 0;
  376. list_del (&quorum_pd->list);
  377. list_init (&quorum_pd->list);
  378. } else {
  379. res.error = CS_ERR_NOT_EXIST;
  380. }
  381. /* send status */
  382. res.size = sizeof(res);
  383. res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
  384. res.error = CS_OK;
  385. corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
  386. }
  387. static void message_handler_req_lib_quorum_gettype (void *conn,
  388. const void *msg)
  389. {
  390. struct res_lib_quorum_gettype res_lib_quorum_gettype;
  391. log_printf(LOGSYS_LEVEL_DEBUG, "got quorum_type request on %p", conn);
  392. /* send status */
  393. res_lib_quorum_gettype.quorum_type = quorum_type;
  394. res_lib_quorum_gettype.header.size = sizeof(res_lib_quorum_gettype);
  395. res_lib_quorum_gettype.header.id = MESSAGE_RES_QUORUM_GETTYPE;
  396. res_lib_quorum_gettype.header.error = CS_OK;
  397. corosync_api->ipc_response_send(conn, &res_lib_quorum_gettype, sizeof(res_lib_quorum_gettype));
  398. }