vsf_quorum.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (c) 2008 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 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 <assert.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 <signal.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <corosync/engine/logsys.h>
  56. #include <corosync/swab.h>
  57. #include <corosync/list.h>
  58. #include <corosync/ipc_gen.h>
  59. #include <corosync/ipc_quorum.h>
  60. #include <corosync/lcr/lcr_comp.h>
  61. #include <corosync/engine/coroapi.h>
  62. #include "vsf.h"
  63. #include "quorum.h"
  64. LOGSYS_DECLARE_SUBSYS ("QUORUM", LOG_INFO);
  65. struct quorum_pd {
  66. unsigned char track_flags;
  67. int tracking_enabled;
  68. struct list_head list;
  69. void *conn;
  70. };
  71. static void message_handler_req_lib_quorum_getquorate (void *conn, void *msg);
  72. static void message_handler_req_lib_quorum_trackstart (void *conn, void *msg);
  73. static void message_handler_req_lib_quorum_trackstop (void *conn, void *msg);
  74. static int send_quorum_notification(void *conn);
  75. static int quorum_exec_init_fn (struct corosync_api_v1 *api);
  76. static int quorum_lib_init_fn (void *conn);
  77. static int quorum_lib_exit_fn (void *conn);
  78. static int primary_designated = 0;
  79. static struct corosync_api_v1 *corosync_api;
  80. static struct list_head trackers_list;
  81. static struct memb_ring_id quorum_ring_id;
  82. static int quorum_view_list_entries = 0;
  83. static int quorum_view_list[PROCESSOR_COUNT_MAX];
  84. static void (*quorum_primary_callback_fn) (
  85. unsigned int *view_list,
  86. int view_list_entries,
  87. int primary_designated,
  88. struct memb_ring_id *ring_id);
  89. /* Internal quorum API function */
  90. static void quorum_api_set_quorum(unsigned int *view_list,
  91. int view_list_entries,
  92. int quorum, struct memb_ring_id *ring_id)
  93. {
  94. primary_designated = quorum;
  95. memcpy(&quorum_ring_id, &ring_id, sizeof (quorum_ring_id));
  96. quorum_view_list_entries = view_list_entries;
  97. memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
  98. /* Tell sync() */
  99. quorum_primary_callback_fn(view_list, view_list_entries,
  100. primary_designated, &quorum_ring_id);
  101. /* Tell IPC listeners */
  102. send_quorum_notification(NULL);
  103. }
  104. static int quorum_init (
  105. void (*primary_callback_fn) (
  106. unsigned int *view_list,
  107. int view_list_entries,
  108. int primary_designated,
  109. struct memb_ring_id *ring_id))
  110. {
  111. quorum_primary_callback_fn = primary_callback_fn;
  112. return (0);
  113. }
  114. /*
  115. * Returns 1 if this processor is in the primary (has quorum)
  116. */
  117. static int quorum_primary (void)
  118. {
  119. return (primary_designated);
  120. }
  121. /*
  122. * lcrso object definition
  123. */
  124. static struct corosync_vsf_iface_ver0 vsf_quorum_iface_ver0 = {
  125. .init = quorum_init,
  126. .primary = quorum_primary
  127. };
  128. static struct quorum_services_api_ver1 quorum_service_api_v1 = {
  129. .quorum_api_set_quorum = quorum_api_set_quorum
  130. };
  131. static struct corosync_lib_handler quorum_lib_service[] =
  132. {
  133. { /* 0 */
  134. .lib_handler_fn = message_handler_req_lib_quorum_getquorate,
  135. .response_size = sizeof (struct res_lib_quorum_getquorate),
  136. .response_id = MESSAGE_RES_QUORUM_GETQUORATE,
  137. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  138. },
  139. { /* 1 */
  140. .lib_handler_fn = message_handler_req_lib_quorum_trackstart,
  141. .response_size = sizeof (mar_res_header_t),
  142. .response_id = MESSAGE_RES_QUORUM_NOTIFICATION,
  143. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  144. },
  145. { /* 2 */
  146. .lib_handler_fn = message_handler_req_lib_quorum_trackstop,
  147. .response_size = sizeof (mar_res_header_t),
  148. .response_id = MESSAGE_RES_QUORUM_TRACKSTOP,
  149. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  150. }
  151. };
  152. static struct corosync_service_engine quorum_service_handler = {
  153. .name = "corosync cluster quorum service v0.1",
  154. .id = QUORUM_SERVICE,
  155. .private_data_size = sizeof (struct quorum_pd),
  156. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED,
  157. .allow_inquorate = COROSYNC_LIB_ALLOW_INQUORATE,
  158. .lib_init_fn = quorum_lib_init_fn,
  159. .lib_exit_fn = quorum_lib_exit_fn,
  160. .lib_engine = quorum_lib_service,
  161. .exec_init_fn = quorum_exec_init_fn,
  162. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  163. };
  164. static struct lcr_iface corosync_vsf_quorum_ver0[3] = {
  165. { /* the VSF handler */
  166. .name = "corosync_vsf_quorum",
  167. .version = 0,
  168. .versions_replace = 0,
  169. .versions_replace_count = 0,
  170. .dependencies = 0,
  171. .dependency_count = 0,
  172. .constructor = NULL,
  173. .destructor = NULL,
  174. .interfaces = (void **)(void *)&vsf_quorum_iface_ver0,
  175. },
  176. { /* API for quorum users to call */
  177. .name = "corosync_quorum_api",
  178. .version = 0,
  179. .versions_replace = 0,
  180. .versions_replace_count = 0,
  181. .dependencies = 0,
  182. .dependency_count = 0,
  183. .constructor = NULL,
  184. .destructor = NULL,
  185. .interfaces = NULL
  186. },
  187. { /* Library calls */
  188. .name = "corosync_quorum",
  189. .version = 0,
  190. .versions_replace = 0,
  191. .versions_replace_count = 0,
  192. .dependencies = 0,
  193. .dependency_count = 0,
  194. .constructor = NULL,
  195. .destructor = NULL,
  196. .interfaces = NULL,
  197. },
  198. };
  199. static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
  200. {
  201. return (&quorum_service_handler);
  202. }
  203. static struct lcr_comp vsf_quorum_comp_ver0 = {
  204. .iface_count = 3,
  205. .ifaces = corosync_vsf_quorum_ver0
  206. };
  207. static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
  208. .corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
  209. };
  210. __attribute__ ((constructor)) static void vsf_quorum_comp_register (void) {
  211. lcr_component_register (&vsf_quorum_comp_ver0);
  212. lcr_interfaces_set (&corosync_vsf_quorum_ver0[0], &vsf_quorum_iface_ver0);
  213. lcr_interfaces_set (&corosync_vsf_quorum_ver0[1], &quorum_service_api_v1);
  214. lcr_interfaces_set (&corosync_vsf_quorum_ver0[2], &quorum_service_handler_iface);
  215. }
  216. /* -------------------------------------------------- */
  217. static int quorum_exec_init_fn (struct corosync_api_v1 *api)
  218. {
  219. corosync_api = api;
  220. list_init (&trackers_list);
  221. return (0);
  222. }
  223. static int quorum_lib_init_fn (void *conn)
  224. {
  225. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  226. log_printf(LOG_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
  227. list_init (&pd->list);
  228. pd->conn = conn;
  229. return (0);
  230. }
  231. static int quorum_lib_exit_fn (void *conn)
  232. {
  233. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  234. log_printf(LOG_LEVEL_DEBUG, "lib_exit_fn: conn=%p\n", conn);
  235. if (quorum_pd->tracking_enabled) {
  236. list_del (&quorum_pd->list);
  237. list_init (&quorum_pd->list);
  238. }
  239. return (0);
  240. }
  241. static int send_quorum_notification(void *conn)
  242. {
  243. int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
  244. char *buf[size];
  245. struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
  246. struct list_head *tmp;
  247. int i;
  248. log_printf(LOG_LEVEL_DEBUG, "sending quorum notification to %p, length = %d\n", conn, size);
  249. res_lib_quorum_notification->quorate = primary_designated;
  250. res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
  251. res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
  252. for (i=0; i<quorum_view_list_entries; i++) {
  253. res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
  254. }
  255. res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
  256. res_lib_quorum_notification->header.size = size;
  257. res_lib_quorum_notification->header.error = SA_AIS_OK;
  258. /* Send it to all interested parties */
  259. if (conn) {
  260. return corosync_api->ipc_conn_send_response(conn, res_lib_quorum_notification, size);
  261. }
  262. else {
  263. struct quorum_pd *qpd;
  264. for (tmp = trackers_list.next; tmp != &trackers_list; tmp = tmp->next) {
  265. qpd = list_entry(tmp, struct quorum_pd, list);
  266. corosync_api->ipc_conn_send_response(corosync_api->ipc_conn_partner_get(qpd->conn),
  267. res_lib_quorum_notification, size);
  268. }
  269. }
  270. return (0);
  271. }
  272. static void message_handler_req_lib_quorum_getquorate (void *conn, void *msg)
  273. {
  274. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  275. log_printf(LOG_LEVEL_DEBUG, "got quorate request on %p\n", conn);
  276. /* send status */
  277. res_lib_quorum_getquorate.quorate = primary_designated;
  278. res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
  279. res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
  280. res_lib_quorum_getquorate.header.error = SA_AIS_OK;
  281. corosync_api->ipc_conn_send_response(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
  282. }
  283. static void message_handler_req_lib_quorum_trackstart (void *conn, void *msg)
  284. {
  285. struct req_lib_quorum_trackstart *req_lib_quorum_trackstart = (struct req_lib_quorum_trackstart *)msg;
  286. mar_res_header_t res;
  287. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  288. log_printf(LOG_LEVEL_DEBUG, "got trackstart request on %p\n", conn);
  289. /*
  290. * If an immediate listing of the current cluster membership
  291. * is requested, generate membership list
  292. */
  293. if (req_lib_quorum_trackstart->track_flags & SA_TRACK_CURRENT ||
  294. req_lib_quorum_trackstart->track_flags & SA_TRACK_CHANGES) {
  295. log_printf(LOG_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  296. send_quorum_notification(corosync_api->ipc_conn_partner_get (conn));
  297. }
  298. /*
  299. * Record requests for tracking
  300. */
  301. if (req_lib_quorum_trackstart->track_flags & SA_TRACK_CHANGES ||
  302. req_lib_quorum_trackstart->track_flags & SA_TRACK_CHANGES_ONLY) {
  303. quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
  304. quorum_pd->tracking_enabled = 1;
  305. list_add (&quorum_pd->list, &trackers_list);
  306. }
  307. /* send status */
  308. res.size = sizeof(res);
  309. res.id = MESSAGE_RES_QUORUM_TRACKSTART;
  310. res.error = SA_AIS_OK;
  311. corosync_api->ipc_conn_send_response(conn, &res, sizeof(mar_res_header_t));
  312. }
  313. static void message_handler_req_lib_quorum_trackstop (void *conn, void *msg)
  314. {
  315. mar_res_header_t res;
  316. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  317. log_printf(LOG_LEVEL_DEBUG, "got trackstop request on %p\n", conn);
  318. if (quorum_pd->tracking_enabled) {
  319. res.error = SA_AIS_OK;
  320. quorum_pd->tracking_enabled = 0;
  321. list_del (&quorum_pd->list);
  322. list_init (&quorum_pd->list);
  323. } else {
  324. res.error = SA_AIS_ERR_NOT_EXIST;
  325. }
  326. /* send status */
  327. res.size = sizeof(res);
  328. res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
  329. res.error = SA_AIS_OK;
  330. corosync_api->ipc_conn_send_response(conn, &res, sizeof(mar_res_header_t));
  331. }