corosync  2.4.3
vsf_quorum.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2015 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 
35 #include <config.h>
36 
37 #include <pwd.h>
38 #include <grp.h>
39 #include <sys/types.h>
40 #include <sys/poll.h>
41 #include <sys/uio.h>
42 #include <sys/mman.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <sched.h>
55 #include <time.h>
56 
57 #include "quorum.h"
58 #include <corosync/corotypes.h>
59 #include <qb/qbipc_common.h>
60 #include <corosync/corodefs.h>
61 #include <corosync/swab.h>
62 #include <corosync/list.h>
63 #include <corosync/mar_gen.h>
64 #include <corosync/ipc_quorum.h>
65 #include <corosync/coroapi.h>
66 #include <corosync/logsys.h>
67 #include <corosync/icmap.h>
68 
69 #include "service.h"
70 #include "votequorum.h"
71 #include "vsf_ykd.h"
72 
73 LOGSYS_DECLARE_SUBSYS ("QUORUM");
74 
75 struct quorum_pd {
76  unsigned char track_flags;
77  int tracking_enabled;
78  struct list_head list;
79  void *conn;
80 };
81 
83  struct list_head list;
85  void *context;
86 };
87 
88 static void message_handler_req_lib_quorum_getquorate (void *conn,
89  const void *msg);
90 static void message_handler_req_lib_quorum_trackstart (void *conn,
91  const void *msg);
92 static void message_handler_req_lib_quorum_trackstop (void *conn,
93  const void *msg);
94 static void message_handler_req_lib_quorum_gettype (void *conn,
95  const void *msg);
96 static void send_library_notification(void *conn);
97 static void send_internal_notification(void);
98 static char *quorum_exec_init_fn (struct corosync_api_v1 *api);
99 static int quorum_lib_init_fn (void *conn);
100 static int quorum_lib_exit_fn (void *conn);
101 
102 static int primary_designated = 0;
103 static int quorum_type = 0;
104 static struct corosync_api_v1 *corosync_api;
105 static struct list_head lib_trackers_list;
106 static struct list_head internal_trackers_list;
107 static struct memb_ring_id quorum_ring_id;
108 static size_t quorum_view_list_entries = 0;
109 static int quorum_view_list[PROCESSOR_COUNT_MAX];
110 struct quorum_services_api_ver1 *quorum_iface = NULL;
111 static char view_buf[64];
112 
113 static void log_view_list(const unsigned int *view_list, size_t view_list_entries)
114 {
115  int total = (int)view_list_entries;
116  int len, pos, ret;
117  int i = 0;
118 
119  while (1) {
120  len = sizeof(view_buf);
121  pos = 0;
122  memset(view_buf, 0, len);
123 
124  for (; i < total; i++) {
125  ret = snprintf(view_buf + pos, len - pos, " %u", view_list[i]);
126  if (ret >= len - pos)
127  break;
128  pos += ret;
129  }
130  log_printf (LOGSYS_LEVEL_NOTICE, "Members[%d]:%s%s",
131  total, view_buf, i < total ? "\\" : "");
132 
133  if (i == total)
134  break;
135  }
136 }
137 
138 /* Internal quorum API function */
139 static void quorum_api_set_quorum(const unsigned int *view_list,
140  size_t view_list_entries,
141  int quorum, struct memb_ring_id *ring_id)
142 {
143  int old_quorum = primary_designated;
144  primary_designated = quorum;
145 
146  if (primary_designated && !old_quorum) {
147  log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.");
148  } else if (!primary_designated && old_quorum) {
149  log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.");
150  }
151 
152  quorum_view_list_entries = view_list_entries;
153  memcpy(&quorum_ring_id, ring_id, sizeof (quorum_ring_id));
154  memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
155 
156  log_view_list(view_list, view_list_entries);
157 
158  /* Tell internal listeners */
159  send_internal_notification();
160 
161  /* Tell IPC listeners */
162  send_library_notification(NULL);
163 }
164 
165 static struct corosync_lib_handler quorum_lib_service[] =
166 {
167  { /* 0 */
168  .lib_handler_fn = message_handler_req_lib_quorum_getquorate,
169  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
170  },
171  { /* 1 */
172  .lib_handler_fn = message_handler_req_lib_quorum_trackstart,
173  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
174  },
175  { /* 2 */
176  .lib_handler_fn = message_handler_req_lib_quorum_trackstop,
177  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
178  },
179  { /* 3 */
180  .lib_handler_fn = message_handler_req_lib_quorum_gettype,
181  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
182  }
183 };
184 
185 static struct corosync_service_engine quorum_service_handler = {
186  .name = "corosync cluster quorum service v0.1",
187  .id = QUORUM_SERVICE,
188  .priority = 1,
189  .private_data_size = sizeof (struct quorum_pd),
190  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
191  .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
192  .lib_init_fn = quorum_lib_init_fn,
193  .lib_exit_fn = quorum_lib_exit_fn,
194  .lib_engine = quorum_lib_service,
195  .exec_init_fn = quorum_exec_init_fn,
196  .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler)
197 };
198 
200 {
201  return (&quorum_service_handler);
202 }
203 
204 /* -------------------------------------------------- */
205 
206 
207 /*
208  * Internal API functions for corosync
209  */
210 
211 static int quorum_quorate(void)
212 {
213  return primary_designated;
214 }
215 
216 
217 static int quorum_register_callback(quorum_callback_fn_t function, void *context)
218 {
219  struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
220  if (!pd)
221  return -1;
222 
223  pd->context = context;
224  pd->callback = function;
225  list_add (&pd->list, &internal_trackers_list);
226 
227  return 0;
228 }
229 
230 static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
231 {
232  struct internal_callback_pd *pd;
233  struct list_head *tmp;
234 
235  for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
236 
237  pd = list_entry(tmp, struct internal_callback_pd, list);
238  if (pd->callback == function && pd->context == context) {
239  list_del(&pd->list);
240  free(pd);
241  return 0;
242  }
243  }
244  return -1;
245 }
246 
247 static struct quorum_callin_functions callins = {
248  .quorate = quorum_quorate,
249  .register_callback = quorum_register_callback,
250  .unregister_callback = quorum_unregister_callback
251 };
252 
253 /* --------------------------------------------------------------------- */
254 
255 static char *quorum_exec_init_fn (struct corosync_api_v1 *api)
256 {
257  char *quorum_module = NULL;
258  char *error;
259 
260  corosync_api = api;
261  list_init (&lib_trackers_list);
262  list_init (&internal_trackers_list);
263 
264  /*
265  * Tell corosync we have a quorum engine.
266  */
267  api->quorum_initialize(&callins);
268 
269  /*
270  * Look for a quorum provider
271  */
272  if (icmap_get_string("quorum.provider", &quorum_module) == CS_OK) {
274  "Using quorum provider %s", quorum_module);
275 
276  error = (char *)"Invalid quorum provider";
277 
278  if (strcmp (quorum_module, "corosync_votequorum") == 0) {
279  error = votequorum_init (api, quorum_api_set_quorum);
280  quorum_type = 1;
281  }
282  if (strcmp (quorum_module, "corosync_ykd") == 0) {
283  error = ykd_init (api, quorum_api_set_quorum);
284  quorum_type = 1;
285  }
286  if (error) {
288  "Quorum provider: %s failed to initialize.",
289  quorum_module);
290  free(quorum_module);
291  return (error);
292  }
293  }
294 
295  if (quorum_module) {
296  free(quorum_module);
297  quorum_module = NULL;
298  }
299 
300  /*
301  * setting quorum_type and primary_designated in the right order is important
302  * always try to lookup/init a quorum module, then revert back to be quorate
303  */
304 
305  if (quorum_type == 0) {
306  primary_designated = 1;
307  }
308 
309  return (NULL);
310 }
311 
312 static int quorum_lib_init_fn (void *conn)
313 {
314  struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
315 
316  log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
317 
318  list_init (&pd->list);
319  pd->conn = conn;
320 
321  return (0);
322 }
323 
324 static int quorum_lib_exit_fn (void *conn)
325 {
326  struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
327 
328  log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p", conn);
329 
330  if (quorum_pd->tracking_enabled) {
331  list_del (&quorum_pd->list);
332  list_init (&quorum_pd->list);
333  }
334  return (0);
335 }
336 
337 
338 static void send_internal_notification(void)
339 {
340  struct list_head *tmp;
341  struct internal_callback_pd *pd;
342 
343  for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
344 
345  pd = list_entry(tmp, struct internal_callback_pd, list);
346 
347  pd->callback(primary_designated, pd->context);
348  }
349 }
350 
351 static void send_library_notification(void *conn)
352 {
353  int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
354  char buf[size];
355  struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
356  struct list_head *tmp;
357  int i;
358 
359  log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d", conn, size);
360 
361  res_lib_quorum_notification->quorate = primary_designated;
362  res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
363  res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
364  for (i=0; i<quorum_view_list_entries; i++) {
365  res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
366  }
367 
368  res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
369  res_lib_quorum_notification->header.size = size;
370  res_lib_quorum_notification->header.error = CS_OK;
371 
372  /* Send it to all interested parties */
373  if (conn) {
374  corosync_api->ipc_dispatch_send(conn, res_lib_quorum_notification, size);
375  }
376  else {
377  struct quorum_pd *qpd;
378 
379  for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
380 
381  qpd = list_entry(tmp, struct quorum_pd, list);
382 
383  corosync_api->ipc_dispatch_send(qpd->conn,
384  res_lib_quorum_notification, size);
385  }
386  }
387  return;
388 }
389 
390 static void message_handler_req_lib_quorum_getquorate (void *conn,
391  const void *msg)
392 {
393  struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
394 
395  log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p", conn);
396 
397  /* send status */
398  res_lib_quorum_getquorate.quorate = primary_designated;
399  res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
400  res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
401  res_lib_quorum_getquorate.header.error = CS_OK;
402  corosync_api->ipc_response_send(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
403 }
404 
405 static void message_handler_req_lib_quorum_trackstart (void *conn,
406  const void *msg)
407 {
409  struct qb_ipc_response_header res;
410  struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
411  cs_error_t error = CS_OK;
412 
413  log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p", conn);
414 
415  /*
416  * If an immediate listing of the current cluster membership
417  * is requested, generate membership list
418  */
419  if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CURRENT ||
420  req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES) {
421  log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
422  send_library_notification(conn);
423  }
424 
425  if (quorum_pd->tracking_enabled) {
426  error = CS_ERR_EXIST;
427  goto response_send;
428  }
429 
430  /*
431  * Record requests for tracking
432  */
433  if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
434  req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
435 
436  quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
437  quorum_pd->tracking_enabled = 1;
438 
439  list_add (&quorum_pd->list, &lib_trackers_list);
440  }
441 
442 response_send:
443  /* send status */
444  res.size = sizeof(res);
446  res.error = error;
447  corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
448 }
449 
450 static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
451 {
452  struct qb_ipc_response_header res;
453  struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
454 
455  log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p", conn);
456 
457  if (quorum_pd->tracking_enabled) {
458  res.error = CS_OK;
459  quorum_pd->tracking_enabled = 0;
460  list_del (&quorum_pd->list);
461  list_init (&quorum_pd->list);
462  } else {
463  res.error = CS_ERR_NOT_EXIST;
464  }
465 
466  /* send status */
467  res.size = sizeof(res);
469  res.error = CS_OK;
470  corosync_api->ipc_response_send(conn, &res, sizeof(struct qb_ipc_response_header));
471 }
472 
473 static void message_handler_req_lib_quorum_gettype (void *conn,
474  const void *msg)
475 {
476  struct res_lib_quorum_gettype res_lib_quorum_gettype;
477 
478  log_printf(LOGSYS_LEVEL_DEBUG, "got quorum_type request on %p", conn);
479 
480  /* send status */
481  res_lib_quorum_gettype.quorum_type = quorum_type;
482  res_lib_quorum_gettype.header.size = sizeof(res_lib_quorum_gettype);
483  res_lib_quorum_gettype.header.id = MESSAGE_RES_QUORUM_GETTYPE;
484  res_lib_quorum_gettype.header.error = CS_OK;
485  corosync_api->ipc_response_send(conn, &res_lib_quorum_gettype, sizeof(res_lib_quorum_gettype));
486 }
487 
int(* quorate)(void)
Definition: coroapi.h:205
void *(* ipc_private_data_get)(void *conn)
Definition: coroapi.h:256
The res_lib_quorum_getquorate struct.
Definition: ipc_quorum.h:72
const char * name
Definition: coroapi.h:492
struct list_head list
Definition: vsf_quorum.c:83
struct list_head * next
Definition: list.h:47
The corosync_service_engine struct.
Definition: coroapi.h:491
The req_lib_quorum_trackstart struct.
Definition: ipc_quorum.h:64
int(* ipc_response_send)(void *conn, const void *msg, size_t mlen)
Definition: coroapi.h:258
char * votequorum_init(struct corosync_api_v1 *api, quorum_set_quorate_fn_t q_set_quorate_fn)
char * ykd_init(struct corosync_api_v1 *corosync_api, quorum_set_quorate_fn_t set_primary)
Definition: vsf_ykd.c:511
int tracking_enabled
quorum_callback_fn_t callback
Definition: vsf_quorum.c:84
#define CS_TRACK_CURRENT
Definition: corotypes.h:87
struct quorum_services_api_ver1 * quorum_iface
Definition: vsf_quorum.c:110
The corosync_lib_handler struct.
Definition: coroapi.h:468
The res_lib_quorum_notification struct.
Definition: ipc_quorum.h:80
Definition: list.h:46
The quorum_callin_functions struct.
Definition: coroapi.h:204
#define log_printf(level, format, args...)
Definition: logsys.h:319
#define CS_TRACK_CHANGES
Definition: corotypes.h:88
LOGSYS_DECLARE_SUBSYS("QUORUM")
mar_uint32_t view_list[]
Definition: ipc_quorum.h:85
struct list_head list
#define CS_TRACK_CHANGES_ONLY
Definition: corotypes.h:89
int(* quorum_initialize)(struct quorum_callin_functions *fns)
Definition: coroapi.h:393
Linked list API.
cs_error_t
The cs_error_t enum.
Definition: corotypes.h:94
unsigned char track_flags
#define LOGSYS_LEVEL_DEBUG
Definition: logsys.h:74
The corosync_api_v1 struct.
Definition: coroapi.h:225
void(* quorum_callback_fn_t)(int quorate, void *context)
The quorum_callback_fn_t callback.
Definition: coroapi.h:199
The res_lib_quorum_gettype struct.
Definition: ipc_quorum.h:91
unsigned int track_flags
Definition: ipc_quorum.h:66
#define PROCESSOR_COUNT_MAX
Definition: coroapi.h:96
The memb_ring_id struct.
Definition: coroapi.h:122
mar_uint32_t quorum_type
Definition: ipc_quorum.h:93
cs_error_t icmap_get_string(const char *key_name, char **str)
Shortcut for icmap_get for string type.
Definition: icmap.c:896
#define LOGSYS_LEVEL_CRIT
Definition: logsys.h:69
#define list_entry(ptr, type, member)
Definition: list.h:84
#define LOGSYS_LEVEL_NOTICE
Definition: logsys.h:72
unsigned long long seq
Definition: coroapi.h:124
void(* lib_handler_fn)(void *conn, const void *msg)
Definition: coroapi.h:469
int(* ipc_dispatch_send)(void *conn, const void *msg, size_t mlen)
Definition: coroapi.h:263
struct memb_ring_id ring_id
Definition: totemsrp.c:64
struct corosync_service_engine * vsf_quorum_get_service_engine_ver0(void)
Definition: vsf_quorum.c:199