corosync  3.0.0
exec/cpg.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006-2015 Red Hat, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Christine Caulfield (ccaulfie@redhat.com)
7  * Author: Jan Friesse (jfriesse@redhat.com)
8  *
9  * This software licensed under BSD license, the text of which follows:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * - Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  * - Neither the name of the MontaVista Software, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 
38 #ifdef HAVE_ALLOCA_H
39 #include <alloca.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/ioctl.h>
45 #include <netinet/in.h>
46 #include <sys/uio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <time.h>
53 #include <assert.h>
54 #include <arpa/inet.h>
55 #include <sys/mman.h>
56 
57 #include <qb/qblist.h>
58 #include <qb/qbmap.h>
59 
60 #include <corosync/corotypes.h>
61 #include <qb/qbipc_common.h>
62 #include <corosync/corodefs.h>
63 #include <corosync/logsys.h>
64 #include <corosync/coroapi.h>
65 
66 #include <corosync/cpg.h>
67 #include <corosync/ipc_cpg.h>
68 
69 #ifndef MAP_ANONYMOUS
70 #define MAP_ANONYMOUS MAP_ANON
71 #endif
72 
73 #include "service.h"
74 
75 LOGSYS_DECLARE_SUBSYS ("CPG");
76 
77 #define GROUP_HASH_SIZE 32
78 
87 };
88 
89 struct zcb_mapped {
90  struct qb_list_head list;
91  void *addr;
92  size_t size;
93 };
94 /*
95  * state` exec deliver
96  * match group name, pid -> if matched deliver for YES:
97  * XXX indicates impossible state
98  *
99  * join leave mcast
100  * UNJOINED XXX XXX NO
101  * LEAVE_STARTED XXX YES(unjoined_enter) YES
102  * JOIN_STARTED YES(join_started_enter) XXX NO
103  * JOIN_COMPLETED XXX NO YES
104  *
105  * join_started_enter
106  * set JOIN_COMPLETED
107  * add entry to process_info list
108  * unjoined_enter
109  * set UNJOINED
110  * delete entry from process_info list
111  *
112  *
113  * library accept join error codes
114  * UNJOINED YES(CS_OK) set JOIN_STARTED
115  * LEAVE_STARTED NO(CS_ERR_BUSY)
116  * JOIN_STARTED NO(CS_ERR_EXIST)
117  * JOIN_COMPlETED NO(CS_ERR_EXIST)
118  *
119  * library accept leave error codes
120  * UNJOINED NO(CS_ERR_NOT_EXIST)
121  * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
122  * JOIN_STARTED NO(CS_ERR_BUSY)
123  * JOIN_COMPLETED YES(CS_OK) set LEAVE_STARTED
124  *
125  * library accept mcast
126  * UNJOINED NO(CS_ERR_NOT_EXIST)
127  * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
128  * JOIN_STARTED YES(CS_OK)
129  * JOIN_COMPLETED YES(CS_OK)
130  */
131 enum cpd_state {
136 };
137 
141 };
142 
143 static struct qb_list_head joinlist_messages_head;
144 
145 struct cpg_pd {
146  void *conn;
148  uint32_t pid;
150  unsigned int flags;
152  uint64_t transition_counter; /* These two are used when sending fragmented messages */
154  struct qb_list_head list;
155  struct qb_list_head iteration_instance_list_head;
156  struct qb_list_head zcb_mapped_list_head;
157 };
158 
161  struct qb_list_head list;
162  struct qb_list_head items_list_head; /* List of process_info */
163  struct qb_list_head *current_pointer;
164 };
165 
166 DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
167 
168 QB_LIST_DECLARE (cpg_pd_list_head);
169 
170 static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
171 
172 static unsigned int my_member_list_entries;
173 
174 static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
175 
176 static unsigned int my_old_member_list_entries = 0;
177 
178 static struct corosync_api_v1 *api = NULL;
179 
180 static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
181 
182 static mar_cpg_ring_id_t last_sync_ring_id;
183 
184 struct process_info {
185  unsigned int nodeid;
186  uint32_t pid;
188  struct qb_list_head list; /* on the group_info members list */
189 };
190 QB_LIST_DECLARE (process_info_list_head);
191 
193  uint32_t pid;
195 };
196 
197 /*
198  * Service Interfaces required by service_message_handler struct
199  */
200 static char *cpg_exec_init_fn (struct corosync_api_v1 *);
201 
202 static int cpg_lib_init_fn (void *conn);
203 
204 static int cpg_lib_exit_fn (void *conn);
205 
206 static void message_handler_req_exec_cpg_procjoin (
207  const void *message,
208  unsigned int nodeid);
209 
210 static void message_handler_req_exec_cpg_procleave (
211  const void *message,
212  unsigned int nodeid);
213 
214 static void message_handler_req_exec_cpg_joinlist (
215  const void *message,
216  unsigned int nodeid);
217 
218 static void message_handler_req_exec_cpg_mcast (
219  const void *message,
220  unsigned int nodeid);
221 
222 static void message_handler_req_exec_cpg_partial_mcast (
223  const void *message,
224  unsigned int nodeid);
225 
226 static void message_handler_req_exec_cpg_downlist_old (
227  const void *message,
228  unsigned int nodeid);
229 
230 static void message_handler_req_exec_cpg_downlist (
231  const void *message,
232  unsigned int nodeid);
233 
234 static void exec_cpg_procjoin_endian_convert (void *msg);
235 
236 static void exec_cpg_joinlist_endian_convert (void *msg);
237 
238 static void exec_cpg_mcast_endian_convert (void *msg);
239 
240 static void exec_cpg_partial_mcast_endian_convert (void *msg);
241 
242 static void exec_cpg_downlist_endian_convert_old (void *msg);
243 
244 static void exec_cpg_downlist_endian_convert (void *msg);
245 
246 static void message_handler_req_lib_cpg_join (void *conn, const void *message);
247 
248 static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
249 
250 static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
251 
252 static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
253 
254 static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message);
255 
256 static void message_handler_req_lib_cpg_membership (void *conn,
257  const void *message);
258 
259 static void message_handler_req_lib_cpg_local_get (void *conn,
260  const void *message);
261 
262 static void message_handler_req_lib_cpg_iteration_initialize (
263  void *conn,
264  const void *message);
265 
266 static void message_handler_req_lib_cpg_iteration_next (
267  void *conn,
268  const void *message);
269 
270 static void message_handler_req_lib_cpg_iteration_finalize (
271  void *conn,
272  const void *message);
273 
274 static void message_handler_req_lib_cpg_zc_alloc (
275  void *conn,
276  const void *message);
277 
278 static void message_handler_req_lib_cpg_zc_free (
279  void *conn,
280  const void *message);
281 
282 static void message_handler_req_lib_cpg_zc_execute (
283  void *conn,
284  const void *message);
285 
286 static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
287 
288 static int cpg_exec_send_downlist(void);
289 
290 static int cpg_exec_send_joinlist(void);
291 
292 static void downlist_inform_clients (void);
293 
294 static void joinlist_inform_clients (void);
295 
296 static void joinlist_messages_delete (void);
297 
298 static void cpg_sync_init (
299  const unsigned int *trans_list,
300  size_t trans_list_entries,
301  const unsigned int *member_list,
302  size_t member_list_entries,
303  const struct memb_ring_id *ring_id);
304 
305 static int cpg_sync_process (void);
306 
307 static void cpg_sync_activate (void);
308 
309 static void cpg_sync_abort (void);
310 
311 static void do_proc_join(
312  const mar_cpg_name_t *name,
313  uint32_t pid,
314  unsigned int nodeid,
315  int reason);
316 
317 static void do_proc_leave(
318  const mar_cpg_name_t *name,
319  uint32_t pid,
320  unsigned int nodeid,
321  int reason);
322 
323 static int notify_lib_totem_membership (
324  void *conn,
325  int member_list_entries,
326  const unsigned int *member_list);
327 
328 static inline int zcb_all_free (
329  struct cpg_pd *cpd);
330 
331 static char *cpg_print_group_name (
332  const mar_cpg_name_t *group);
333 
334 /*
335  * Library Handler Definition
336  */
337 static struct corosync_lib_handler cpg_lib_engine[] =
338 {
339  { /* 0 - MESSAGE_REQ_CPG_JOIN */
340  .lib_handler_fn = message_handler_req_lib_cpg_join,
341  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
342  },
343  { /* 1 - MESSAGE_REQ_CPG_LEAVE */
344  .lib_handler_fn = message_handler_req_lib_cpg_leave,
345  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
346  },
347  { /* 2 - MESSAGE_REQ_CPG_MCAST */
348  .lib_handler_fn = message_handler_req_lib_cpg_mcast,
349  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
350  },
351  { /* 3 - MESSAGE_REQ_CPG_MEMBERSHIP */
352  .lib_handler_fn = message_handler_req_lib_cpg_membership,
353  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
354  },
355  { /* 4 - MESSAGE_REQ_CPG_LOCAL_GET */
356  .lib_handler_fn = message_handler_req_lib_cpg_local_get,
357  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
358  },
359  { /* 5 - MESSAGE_REQ_CPG_ITERATIONINITIALIZE */
360  .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
361  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
362  },
363  { /* 6 - MESSAGE_REQ_CPG_ITERATIONNEXT */
364  .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
365  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
366  },
367  { /* 7 - MESSAGE_REQ_CPG_ITERATIONFINALIZE */
368  .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
369  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
370  },
371  { /* 8 - MESSAGE_REQ_CPG_FINALIZE */
372  .lib_handler_fn = message_handler_req_lib_cpg_finalize,
373  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
374  },
375  { /* 9 */
376  .lib_handler_fn = message_handler_req_lib_cpg_zc_alloc,
377  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
378  },
379  { /* 10 */
380  .lib_handler_fn = message_handler_req_lib_cpg_zc_free,
381  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
382  },
383  { /* 11 */
384  .lib_handler_fn = message_handler_req_lib_cpg_zc_execute,
385  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
386  },
387  { /* 12 */
388  .lib_handler_fn = message_handler_req_lib_cpg_partial_mcast,
389  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
390  },
391 
392 };
393 
394 static struct corosync_exec_handler cpg_exec_engine[] =
395 {
396  { /* 0 - MESSAGE_REQ_EXEC_CPG_PROCJOIN */
397  .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
398  .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
399  },
400  { /* 1 - MESSAGE_REQ_EXEC_CPG_PROCLEAVE */
401  .exec_handler_fn = message_handler_req_exec_cpg_procleave,
402  .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
403  },
404  { /* 2 - MESSAGE_REQ_EXEC_CPG_JOINLIST */
405  .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
406  .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
407  },
408  { /* 3 - MESSAGE_REQ_EXEC_CPG_MCAST */
409  .exec_handler_fn = message_handler_req_exec_cpg_mcast,
410  .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
411  },
412  { /* 4 - MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD */
413  .exec_handler_fn = message_handler_req_exec_cpg_downlist_old,
414  .exec_endian_convert_fn = exec_cpg_downlist_endian_convert_old
415  },
416  { /* 5 - MESSAGE_REQ_EXEC_CPG_DOWNLIST */
417  .exec_handler_fn = message_handler_req_exec_cpg_downlist,
418  .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
419  },
420  { /* 6 - MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST */
421  .exec_handler_fn = message_handler_req_exec_cpg_partial_mcast,
422  .exec_endian_convert_fn = exec_cpg_partial_mcast_endian_convert
423  },
424 };
425 
427  .name = "corosync cluster closed process group service v1.01",
428  .id = CPG_SERVICE,
429  .priority = 1,
430  .private_data_size = sizeof (struct cpg_pd),
431  .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
432  .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
433  .lib_init_fn = cpg_lib_init_fn,
434  .lib_exit_fn = cpg_lib_exit_fn,
435  .lib_engine = cpg_lib_engine,
436  .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
437  .exec_init_fn = cpg_exec_init_fn,
438  .exec_dump_fn = NULL,
439  .exec_engine = cpg_exec_engine,
440  .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
441  .sync_init = cpg_sync_init,
442  .sync_process = cpg_sync_process,
443  .sync_activate = cpg_sync_activate,
444  .sync_abort = cpg_sync_abort
445 };
446 
448 {
449  return (&cpg_service_engine);
450 }
451 
453  struct qb_ipc_request_header header __attribute__((aligned(8)));
454  mar_cpg_name_t group_name __attribute__((aligned(8)));
455  mar_uint32_t pid __attribute__((aligned(8)));
456  mar_uint32_t reason __attribute__((aligned(8)));
457 };
458 
460  struct qb_ipc_request_header header __attribute__((aligned(8)));
461  mar_cpg_name_t group_name __attribute__((aligned(8)));
462  mar_uint32_t msglen __attribute__((aligned(8)));
463  mar_uint32_t pid __attribute__((aligned(8)));
464  mar_message_source_t source __attribute__((aligned(8)));
465  mar_uint8_t message[] __attribute__((aligned(8)));
466 };
467 
469  struct qb_ipc_request_header header __attribute__((aligned(8)));
470  mar_cpg_name_t group_name __attribute__((aligned(8)));
471  mar_uint32_t msglen __attribute__((aligned(8)));
472  mar_uint32_t fraglen __attribute__((aligned(8)));
473  mar_uint32_t pid __attribute__((aligned(8)));
474  mar_uint32_t type __attribute__((aligned(8)));
475  mar_message_source_t source __attribute__((aligned(8)));
476  mar_uint8_t message[] __attribute__((aligned(8)));
477 };
478 
480  struct qb_ipc_request_header header __attribute__((aligned(8)));
481  mar_uint32_t left_nodes __attribute__((aligned(8)));
482  mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
483 };
484 
486  struct qb_ipc_request_header header __attribute__((aligned(8)));
487  /* merge decisions */
488  mar_uint32_t old_members __attribute__((aligned(8)));
489  /* downlist below */
490  mar_uint32_t left_nodes __attribute__((aligned(8)));
491  mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
492 };
493 
494 struct joinlist_msg {
496  uint32_t pid;
498  struct qb_list_head list;
499 };
500 
501 static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
502 
503 /*
504  * Function print group name. It's not reentrant
505  */
506 static char *cpg_print_group_name(const mar_cpg_name_t *group)
507 {
508  static char res[CPG_MAX_NAME_LENGTH * 4 + 1];
509  int dest_pos = 0;
510  char c;
511  int i;
512 
513  for (i = 0; i < group->length; i++) {
514  c = group->value[i];
515 
516  if (c >= ' ' && c < 0x7f && c != '\\') {
517  res[dest_pos++] = c;
518  } else {
519  if (c == '\\') {
520  res[dest_pos++] = '\\';
521  res[dest_pos++] = '\\';
522  } else {
523  snprintf(res + dest_pos, sizeof(res) - dest_pos, "\\x%02X", c);
524  dest_pos += 4;
525  }
526  }
527  }
528  res[dest_pos] = 0;
529 
530  return (res);
531 }
532 
533 static void cpg_sync_init (
534  const unsigned int *trans_list,
535  size_t trans_list_entries,
536  const unsigned int *member_list,
537  size_t member_list_entries,
538  const struct memb_ring_id *ring_id)
539 {
540  int entries;
541  int i, j;
542  int found;
543 
544  my_sync_state = CPGSYNC_DOWNLIST;
545 
546  memcpy (my_member_list, member_list, member_list_entries *
547  sizeof (unsigned int));
548  my_member_list_entries = member_list_entries;
549 
550  last_sync_ring_id.nodeid = ring_id->nodeid;
551  last_sync_ring_id.seq = ring_id->seq;
552 
553  entries = 0;
554  /*
555  * Determine list of nodeids for downlist message
556  */
557  for (i = 0; i < my_old_member_list_entries; i++) {
558  found = 0;
559  for (j = 0; j < trans_list_entries; j++) {
560  if (my_old_member_list[i] == trans_list[j]) {
561  found = 1;
562  break;
563  }
564  }
565  if (found == 0) {
566  g_req_exec_cpg_downlist.nodeids[entries++] =
567  my_old_member_list[i];
568  }
569  }
570  g_req_exec_cpg_downlist.left_nodes = entries;
571 }
572 
573 static int cpg_sync_process (void)
574 {
575  int res = -1;
576 
577  if (my_sync_state == CPGSYNC_DOWNLIST) {
578  res = cpg_exec_send_downlist();
579  if (res == -1) {
580  return (-1);
581  }
582  my_sync_state = CPGSYNC_JOINLIST;
583  }
584  if (my_sync_state == CPGSYNC_JOINLIST) {
585  res = cpg_exec_send_joinlist();
586  }
587  return (res);
588 }
589 
590 static void cpg_sync_activate (void)
591 {
592  memcpy (my_old_member_list, my_member_list,
593  my_member_list_entries * sizeof (unsigned int));
594  my_old_member_list_entries = my_member_list_entries;
595 
596  downlist_inform_clients ();
597 
598  joinlist_inform_clients ();
599 
600  joinlist_messages_delete ();
601 
602  notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
603 }
604 
605 static void cpg_sync_abort (void)
606 {
607 
608  joinlist_messages_delete ();
609 }
610 
611 static int notify_lib_totem_membership (
612  void *conn,
613  int member_list_entries,
614  const unsigned int *member_list)
615 {
616  struct qb_list_head *iter;
617  char *buf;
618  int size;
620 
621  size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
622  sizeof(mar_uint32_t) * (member_list_entries);
623  buf = alloca(size);
624  if (!buf)
625  return CS_ERR_LIBRARY;
626 
627  res = (struct res_lib_cpg_totem_confchg_callback *)buf;
628  res->member_list_entries = member_list_entries;
629  res->header.size = size;
631  res->header.error = CS_OK;
632 
633  memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
634  memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
635 
636  if (conn == NULL) {
637  qb_list_for_each(iter, &cpg_pd_list_head) {
638  struct cpg_pd *cpg_pd = qb_list_entry (iter, struct cpg_pd, list);
639  api->ipc_dispatch_send (cpg_pd->conn, buf, size);
640  }
641  } else {
642  api->ipc_dispatch_send (conn, buf, size);
643  }
644 
645  return CS_OK;
646 }
647 
648 static int notify_lib_joinlist(
649  const mar_cpg_name_t *group_name,
650  void *conn,
651  int joined_list_entries,
652  mar_cpg_address_t *joined_list,
653  int left_list_entries,
654  mar_cpg_address_t *left_list,
655  int id)
656 {
657  int size;
658  char *buf;
659  struct qb_list_head *iter;
660  int count;
661  struct res_lib_cpg_confchg_callback *res;
662  mar_cpg_address_t *retgi;
663 
664  count = 0;
665 
666  qb_list_for_each(iter, &process_info_list_head) {
667  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
668  if (mar_name_compare (&pi->group, group_name) == 0) {
669  int i;
670  int founded = 0;
671 
672  for (i = 0; i < left_list_entries; i++) {
673  if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
674  founded++;
675  }
676  }
677 
678  if (!founded)
679  count++;
680  }
681  }
682 
683  size = sizeof(struct res_lib_cpg_confchg_callback) +
684  sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
685  buf = alloca(size);
686  if (!buf)
687  return CS_ERR_LIBRARY;
688 
689  res = (struct res_lib_cpg_confchg_callback *)buf;
690  res->joined_list_entries = joined_list_entries;
691  res->left_list_entries = left_list_entries;
692  res->member_list_entries = count;
693  retgi = res->member_list;
694  res->header.size = size;
695  res->header.id = id;
696  res->header.error = CS_OK;
697  memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
698 
699  qb_list_for_each(iter, &process_info_list_head) {
700  struct process_info *pi=qb_list_entry (iter, struct process_info, list);
701 
702  if (mar_name_compare (&pi->group, group_name) == 0) {
703  int i;
704  int founded = 0;
705 
706  for (i = 0;i < left_list_entries; i++) {
707  if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
708  founded++;
709  }
710  }
711 
712  if (!founded) {
713  retgi->nodeid = pi->nodeid;
714  retgi->pid = pi->pid;
715  retgi++;
716  }
717  }
718  }
719 
720  if (left_list_entries) {
721  memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
722  retgi += left_list_entries;
723  }
724 
725  if (joined_list_entries) {
726  memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
727  retgi += joined_list_entries;
728  }
729 
730  if (conn) {
731  api->ipc_dispatch_send (conn, buf, size);
732  } else {
733  qb_list_for_each(iter, &cpg_pd_list_head) {
734  struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
735  if (mar_name_compare (&cpd->group_name, group_name) == 0) {
736  assert (joined_list_entries <= 1);
737  if (joined_list_entries) {
738  if (joined_list[0].pid == cpd->pid &&
739  joined_list[0].nodeid == api->totem_nodeid_get()) {
741  }
742  }
743  if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
745 
746  api->ipc_dispatch_send (cpd->conn, buf, size);
747  cpd->transition_counter++;
748  }
749  if (left_list_entries) {
750  if (left_list[0].pid == cpd->pid &&
751  left_list[0].nodeid == api->totem_nodeid_get() &&
752  left_list[0].reason == CONFCHG_CPG_REASON_LEAVE) {
753 
754  cpd->pid = 0;
755  memset (&cpd->group_name, 0, sizeof(cpd->group_name));
757  }
758  }
759  }
760  }
761  }
762 
763 
764  /*
765  * Traverse thru cpds and send totem membership for cpd, where it is not send yet
766  */
767  qb_list_for_each(iter, &cpg_pd_list_head) {
768  struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
769 
771  cpd->initial_totem_conf_sent = 1;
772 
773  notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
774  }
775  }
776 
777  return CS_OK;
778 }
779 
780 static void downlist_log(const char *msg, struct req_exec_cpg_downlist *dl)
781 {
782  log_printf (LOG_DEBUG,
783  "%s: members(old:%d left:%d)",
784  msg,
785  dl->old_members,
786  dl->left_nodes);
787 }
788 
789 static void downlist_inform_clients (void)
790 {
791  struct qb_list_head *iter, *tmp_iter;
792  struct process_info *left_pi;
793  qb_map_t *group_map;
794  struct cpg_name cpg_group;
795  mar_cpg_name_t group;
796  struct confchg_data{
797  struct cpg_name cpg_group;
799  int left_list_entries;
800  struct qb_list_head list;
801  } *pcd;
802  qb_map_iter_t *miter;
803  int i, size;
804 
805  downlist_log("my downlist", &g_req_exec_cpg_downlist);
806 
807  group_map = qb_skiplist_create();
808 
809  /*
810  * only the cpg groups included in left nodes should receive
811  * confchg event, so we will collect these cpg groups and
812  * relative left_lists here.
813  */
814  qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
815  struct process_info *pi = qb_list_entry(iter, struct process_info, list);
816 
817  left_pi = NULL;
818  for (i = 0; i < g_req_exec_cpg_downlist.left_nodes; i++) {
819 
820  if (pi->nodeid == g_req_exec_cpg_downlist.nodeids[i]) {
821  left_pi = pi;
822  break;
823  }
824  }
825 
826  if (left_pi) {
827  marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
828  cpg_group.value[cpg_group.length] = 0;
829 
830  pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
831  if (pcd == NULL) {
832  pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
833  memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
834  qb_map_put(group_map, pcd->cpg_group.value, pcd);
835  }
836  size = pcd->left_list_entries;
837  pcd->left_list[size].nodeid = left_pi->nodeid;
838  pcd->left_list[size].pid = left_pi->pid;
839  pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
840  pcd->left_list_entries++;
841  qb_list_del (&left_pi->list);
842  free (left_pi);
843  }
844  }
845 
846  /* send only one confchg event per cpg group */
847  miter = qb_map_iter_create(group_map);
848  while (qb_map_iter_next(miter, (void **)&pcd)) {
849  marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
850 
851  log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
852  for (i=0; i<pcd->left_list_entries; i++) {
853  log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
854  i, cpg_print_group_name(&group),
855  (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
856  pcd->left_list[i].pid);
857  }
858 
859  /* send confchg event */
860  notify_lib_joinlist(&group, NULL,
861  0, NULL,
862  pcd->left_list_entries,
863  pcd->left_list,
865 
866  free(pcd);
867  }
868  qb_map_iter_free(miter);
869  qb_map_destroy(group_map);
870 }
871 
872 /*
873  * Remove processes that might have left the group while we were suspended.
874  */
875 static void joinlist_remove_zombie_pi_entries (void)
876 {
877  struct qb_list_head *pi_iter, *tmp_iter;
878  struct qb_list_head *jl_iter;
879  struct process_info *pi;
880  struct joinlist_msg *stored_msg;
881  int found;
882 
883  qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
884  pi = qb_list_entry (pi_iter, struct process_info, list);
885 
886  /*
887  * Ignore local node
888  */
889  if (pi->nodeid == api->totem_nodeid_get()) {
890  continue ;
891  }
892 
893  /*
894  * Try to find message in joinlist messages
895  */
896  found = 0;
897  qb_list_for_each(jl_iter, &joinlist_messages_head) {
898  stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
899 
900  if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
901  continue ;
902  }
903 
904  if (pi->nodeid == stored_msg->sender_nodeid &&
905  pi->pid == stored_msg->pid &&
906  mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
907  found = 1;
908  break ;
909  }
910  }
911 
912  if (!found) {
913  do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
914  }
915  }
916 }
917 
918 static void joinlist_inform_clients (void)
919 {
920  struct joinlist_msg *stored_msg;
921  struct qb_list_head *iter;
922  unsigned int i;
923 
924  i = 0;
925  qb_list_for_each(iter, &joinlist_messages_head) {
926  stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
927 
928  log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
929  i++, cpg_print_group_name(&stored_msg->group_name),
930  (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
931  stored_msg->pid);
932 
933  /* Ignore our own messages */
934  if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
935  continue ;
936  }
937 
938  do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
940  }
941 
942  joinlist_remove_zombie_pi_entries ();
943 }
944 
945 static void joinlist_messages_delete (void)
946 {
947  struct joinlist_msg *stored_msg;
948  struct qb_list_head *iter, *tmp_iter;
949 
950  qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
951  stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
952  qb_list_del (&stored_msg->list);
953  free (stored_msg);
954  }
955  qb_list_init (&joinlist_messages_head);
956 }
957 
958 static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
959 {
960  qb_list_init (&joinlist_messages_head);
961  api = corosync_api;
962  return (NULL);
963 }
964 
965 static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
966 {
967  struct qb_list_head *iter, *tmp_iter;
968  struct process_info *pi;
969 
970  qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
971  pi = qb_list_entry (iter, struct process_info, list);
972  qb_list_del (&pi->list);
973  free (pi);
974  }
975 
976  qb_list_del (&cpg_iteration_instance->list);
977  hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
978 }
979 
980 static void cpg_pd_finalize (struct cpg_pd *cpd)
981 {
982  struct qb_list_head *iter, *tmp_iter;
983  struct cpg_iteration_instance *cpii;
984 
985  zcb_all_free(cpd);
986  qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
987  cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
988 
989  cpg_iteration_instance_finalize (cpii);
990  }
991 
992  qb_list_del (&cpd->list);
993 }
994 
995 static int cpg_lib_exit_fn (void *conn)
996 {
997  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
998 
999  log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
1000 
1001  if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
1002  cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
1004  }
1005 
1006  cpg_pd_finalize (cpd);
1007 
1008  api->ipc_refcnt_dec (conn);
1009  return (0);
1010 }
1011 
1012 static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
1013 {
1015  struct iovec req_exec_cpg_iovec;
1016  int result;
1017 
1018  memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
1019  req_exec_cpg_procjoin.pid = pid;
1020  req_exec_cpg_procjoin.reason = reason;
1021 
1022  req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
1024 
1025  req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
1026  req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
1027 
1028  result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
1029 
1030  return (result);
1031 }
1032 
1033 /* Can byteswap join & leave messages */
1034 static void exec_cpg_procjoin_endian_convert (void *msg)
1035 {
1037 
1038  req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
1039  swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
1040  req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
1041 }
1042 
1043 static void exec_cpg_joinlist_endian_convert (void *msg_v)
1044 {
1045  char *msg = msg_v;
1046  struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
1047  struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
1048 
1049  swab_mar_int32_t (&res->size);
1050 
1051  while ((const char*)jle < msg + res->size) {
1052  jle->pid = swab32(jle->pid);
1053  swab_mar_cpg_name_t (&jle->group_name);
1054  jle++;
1055  }
1056 }
1057 
1058 static void exec_cpg_downlist_endian_convert_old (void *msg)
1059 {
1060 }
1061 
1062 static void exec_cpg_downlist_endian_convert (void *msg)
1063 {
1065  unsigned int i;
1066 
1067  req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
1068  req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
1069 
1070  for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
1071  req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
1072  }
1073 }
1074 
1075 
1076 static void exec_cpg_mcast_endian_convert (void *msg)
1077 {
1078  struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
1079 
1080  swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
1081  swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
1082  req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
1083  req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
1084  swab_mar_message_source_t (&req_exec_cpg_mcast->source);
1085 }
1086 
1087 static void exec_cpg_partial_mcast_endian_convert (void *msg)
1088 {
1090 
1091  swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
1092  swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
1093  req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
1094  req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
1095  req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
1096  req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
1097  swab_mar_message_source_t (&req_exec_cpg_mcast->source);
1098 }
1099 
1100 static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
1101  struct qb_list_head *iter;
1102 
1103  qb_list_for_each(iter, &process_info_list_head) {
1104  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1105 
1106  if (pi->pid == pid && pi->nodeid == nodeid &&
1107  mar_name_compare (&pi->group, group_name) == 0) {
1108  return pi;
1109  }
1110  }
1111 
1112  return NULL;
1113 }
1114 
1115 static void do_proc_join(
1116  const mar_cpg_name_t *name,
1117  uint32_t pid,
1118  unsigned int nodeid,
1119  int reason)
1120 {
1121  struct process_info *pi;
1122  struct process_info *pi_entry;
1123  mar_cpg_address_t notify_info;
1124  struct qb_list_head *list;
1125  struct qb_list_head *list_to_add = NULL;
1126 
1127  if (process_info_find (name, pid, nodeid) != NULL) {
1128  return ;
1129  }
1130  pi = malloc (sizeof (struct process_info));
1131  if (!pi) {
1132  log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
1133  return;
1134  }
1135  pi->nodeid = nodeid;
1136  pi->pid = pid;
1137  memcpy(&pi->group, name, sizeof(*name));
1138  qb_list_init(&pi->list);
1139 
1140  /*
1141  * Insert new process in sorted order so synchronization works properly
1142  */
1143  list_to_add = &process_info_list_head;
1144  qb_list_for_each(list, &process_info_list_head) {
1145  pi_entry = qb_list_entry(list, struct process_info, list);
1146  if (pi_entry->nodeid > pi->nodeid ||
1147  (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
1148 
1149  break;
1150  }
1151  list_to_add = list;
1152  }
1153  qb_list_add (&pi->list, list_to_add);
1154 
1155  notify_info.pid = pi->pid;
1156  notify_info.nodeid = nodeid;
1157  notify_info.reason = reason;
1158 
1159  notify_lib_joinlist(&pi->group, NULL,
1160  1, &notify_info,
1161  0, NULL,
1163 }
1164 
1165 static void do_proc_leave(
1166  const mar_cpg_name_t *name,
1167  uint32_t pid,
1168  unsigned int nodeid,
1169  int reason)
1170 {
1171  struct process_info *pi;
1172  struct qb_list_head *iter, *tmp_iter;
1173  mar_cpg_address_t notify_info;
1174 
1175  notify_info.pid = pid;
1176  notify_info.nodeid = nodeid;
1177  notify_info.reason = reason;
1178 
1179  notify_lib_joinlist(name, NULL,
1180  0, NULL,
1181  1, &notify_info,
1183 
1184  qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
1185  pi = qb_list_entry(iter, struct process_info, list);
1186 
1187  if (pi->pid == pid && pi->nodeid == nodeid &&
1188  mar_name_compare (&pi->group, name)==0) {
1189  qb_list_del (&pi->list);
1190  free (pi);
1191  }
1192  }
1193 }
1194 
1195 static void message_handler_req_exec_cpg_downlist_old (
1196  const void *message,
1197  unsigned int nodeid)
1198 {
1199  log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node 0x%x",
1200  nodeid);
1201 }
1202 
1203 static void message_handler_req_exec_cpg_downlist(
1204  const void *message,
1205  unsigned int nodeid)
1206 {
1207  const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
1208 
1209  log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received",
1210  req_exec_cpg_downlist->left_nodes);
1211 }
1212 
1213 
1214 static void message_handler_req_exec_cpg_procjoin (
1215  const void *message,
1216  unsigned int nodeid)
1217 {
1218  const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
1219 
1220  log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node 0x%x (%s) for pid %u",
1221  nodeid,
1222  api->totem_ifaces_print(nodeid),
1223  (unsigned int)req_exec_cpg_procjoin->pid);
1224 
1225  do_proc_join (&req_exec_cpg_procjoin->group_name,
1226  req_exec_cpg_procjoin->pid, nodeid,
1228 }
1229 
1230 static void message_handler_req_exec_cpg_procleave (
1231  const void *message,
1232  unsigned int nodeid)
1233 {
1234  const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
1235 
1236  log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node 0x%x (%s) for pid %u",
1237  nodeid,
1238  api->totem_ifaces_print(nodeid),
1239  (unsigned int)req_exec_cpg_procjoin->pid);
1240 
1241  do_proc_leave (&req_exec_cpg_procjoin->group_name,
1242  req_exec_cpg_procjoin->pid, nodeid,
1243  req_exec_cpg_procjoin->reason);
1244 }
1245 
1246 
1247 /* Got a proclist from another node */
1248 static void message_handler_req_exec_cpg_joinlist (
1249  const void *message_v,
1250  unsigned int nodeid)
1251 {
1252  const char *message = message_v;
1253  const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
1254  const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
1255  struct joinlist_msg *stored_msg;
1256 
1257  log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node 0x%x",
1258  nodeid);
1259 
1260  while ((const char*)jle < message + res->size) {
1261  stored_msg = malloc (sizeof (struct joinlist_msg));
1262  memset(stored_msg, 0, sizeof (struct joinlist_msg));
1263  stored_msg->sender_nodeid = nodeid;
1264  stored_msg->pid = jle->pid;
1265  memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
1266  qb_list_init (&stored_msg->list);
1267  qb_list_add (&stored_msg->list, &joinlist_messages_head);
1268  jle++;
1269  }
1270 }
1271 
1272 static void message_handler_req_exec_cpg_mcast (
1273  const void *message,
1274  unsigned int nodeid)
1275 {
1276  const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
1278  int msglen = req_exec_cpg_mcast->msglen;
1279  struct qb_list_head *iter, *pi_iter, *tmp_iter;
1280  struct cpg_pd *cpd;
1281  struct iovec iovec[2];
1282  int known_node = 0;
1283 
1285  res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
1286  res_lib_cpg_mcast.msglen = msglen;
1287  res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
1288  res_lib_cpg_mcast.nodeid = nodeid;
1289 
1290  memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
1291  sizeof(mar_cpg_name_t));
1292  iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
1293  iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
1294 
1295  iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
1296  iovec[1].iov_len = msglen;
1297 
1298  qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1299  cpd = qb_list_entry(iter, struct cpg_pd, list);
1301  && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
1302 
1303  if (!known_node) {
1304  /* Try to find, if we know the node */
1305  qb_list_for_each(pi_iter, &process_info_list_head) {
1306  struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
1307 
1308  if (pi->nodeid == nodeid &&
1309  mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
1310  known_node = 1;
1311  break;
1312  }
1313  }
1314  }
1315 
1316  if (!known_node) {
1317  log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
1318  return ;
1319  }
1320 
1321  api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
1322  }
1323  }
1324 }
1325 
1326 static void message_handler_req_exec_cpg_partial_mcast (
1327  const void *message,
1328  unsigned int nodeid)
1329 {
1330  const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
1332  int msglen = req_exec_cpg_mcast->fraglen;
1333  struct qb_list_head *iter, *pi_iter, *tmp_iter;
1334  struct cpg_pd *cpd;
1335  struct iovec iovec[2];
1336  int known_node = 0;
1337 
1338  log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node %d, size = %d bytes\n", nodeid, msglen);
1339 
1341  res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
1342  res_lib_cpg_mcast.fraglen = msglen;
1343  res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
1344  res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
1345  res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
1346  res_lib_cpg_mcast.nodeid = nodeid;
1347 
1348  memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
1349  sizeof(mar_cpg_name_t));
1350  iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
1351  iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
1352 
1353  iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
1354  iovec[1].iov_len = msglen;
1355 
1356  qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1357  cpd = qb_list_entry(iter, struct cpg_pd, list);
1358 
1360  && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
1361 
1362  if (!known_node) {
1363  /* Try to find, if we know the node */
1364  qb_list_for_each(pi_iter, &process_info_list_head) {
1365  struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
1366 
1367  if (pi->nodeid == nodeid &&
1368  mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
1369  known_node = 1;
1370  break;
1371  }
1372  }
1373  }
1374 
1375  if (!known_node) {
1376  log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
1377  return ;
1378  }
1379 
1380  api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
1381  }
1382  }
1383 }
1384 
1385 
1386 static int cpg_exec_send_downlist(void)
1387 {
1388  struct iovec iov;
1389 
1390  g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
1391  g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
1392 
1393  g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
1394 
1395  iov.iov_base = (void *)&g_req_exec_cpg_downlist;
1396  iov.iov_len = g_req_exec_cpg_downlist.header.size;
1397 
1398  return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
1399 }
1400 
1401 static int cpg_exec_send_joinlist(void)
1402 {
1403  int count = 0;
1404  struct qb_list_head *iter;
1405  struct qb_ipc_response_header *res;
1406  char *buf;
1407  struct join_list_entry *jle;
1408  struct iovec req_exec_cpg_iovec;
1409 
1410  qb_list_for_each(iter, &process_info_list_head) {
1411  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1412 
1413  if (pi->nodeid == api->totem_nodeid_get ()) {
1414  count++;
1415  }
1416  }
1417 
1418  /* Nothing to send */
1419  if (!count)
1420  return 0;
1421 
1422  buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
1423  if (!buf) {
1424  log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
1425  return -1;
1426  }
1427 
1428  jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
1429  res = (struct qb_ipc_response_header *)buf;
1430 
1431  qb_list_for_each(iter, &process_info_list_head) {
1432  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1433 
1434  if (pi->nodeid == api->totem_nodeid_get ()) {
1435  memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
1436  jle->pid = pi->pid;
1437  jle++;
1438  }
1439  }
1440 
1442  res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
1443 
1444  req_exec_cpg_iovec.iov_base = buf;
1445  req_exec_cpg_iovec.iov_len = res->size;
1446 
1447  return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
1448 }
1449 
1450 static int cpg_lib_init_fn (void *conn)
1451 {
1452  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1453  memset (cpd, 0, sizeof(struct cpg_pd));
1454  cpd->conn = conn;
1455  qb_list_add (&cpd->list, &cpg_pd_list_head);
1456 
1457  qb_list_init (&cpd->iteration_instance_list_head);
1458  qb_list_init (&cpd->zcb_mapped_list_head);
1459 
1460  api->ipc_refcnt_inc (conn);
1461  log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
1462  return (0);
1463 }
1464 
1465 /* Join message from the library */
1466 static void message_handler_req_lib_cpg_join (void *conn, const void *message)
1467 {
1468  const struct req_lib_cpg_join *req_lib_cpg_join = message;
1469  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1471  cs_error_t error = CS_OK;
1472  struct qb_list_head *iter;
1473 
1474  /* Test, if we don't have same pid and group name joined */
1475  qb_list_for_each(iter, &cpg_pd_list_head) {
1476  struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
1477 
1478  if (cpd_item->pid == req_lib_cpg_join->pid &&
1479  mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
1480 
1481  /* We have same pid and group name joined -> return error */
1482  error = CS_ERR_EXIST;
1483  goto response_send;
1484  }
1485  }
1486 
1487  /*
1488  * Same check must be done in process info list, because there may be not yet delivered
1489  * leave of client.
1490  */
1491  qb_list_for_each(iter, &process_info_list_head) {
1492  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1493 
1494  if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
1495  mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
1496  /* We have same pid and group name joined -> return error */
1497  error = CS_ERR_TRY_AGAIN;
1498  goto response_send;
1499  }
1500  }
1501 
1502  if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
1503  error = CS_ERR_NAME_TOO_LONG;
1504  goto response_send;
1505  }
1506 
1507  switch (cpd->cpd_state) {
1508  case CPD_STATE_UNJOINED:
1509  error = CS_OK;
1511  cpd->pid = req_lib_cpg_join->pid;
1512  cpd->flags = req_lib_cpg_join->flags;
1513  memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
1514  sizeof (cpd->group_name));
1515 
1516  cpg_node_joinleave_send (req_lib_cpg_join->pid,
1517  &req_lib_cpg_join->group_name,
1519  break;
1521  error = CS_ERR_BUSY;
1522  break;
1524  error = CS_ERR_EXIST;
1525  break;
1527  error = CS_ERR_EXIST;
1528  break;
1529  }
1530 
1531 response_send:
1532  res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
1534  res_lib_cpg_join.header.error = error;
1535  api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
1536 }
1537 
1538 /* Leave message from the library */
1539 static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
1540 {
1542  cs_error_t error = CS_OK;
1543  struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
1544  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1545 
1546  log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
1547 
1548  switch (cpd->cpd_state) {
1549  case CPD_STATE_UNJOINED:
1550  error = CS_ERR_NOT_EXIST;
1551  break;
1553  error = CS_ERR_NOT_EXIST;
1554  break;
1556  error = CS_ERR_BUSY;
1557  break;
1559  error = CS_OK;
1561  cpg_node_joinleave_send (req_lib_cpg_leave->pid,
1562  &req_lib_cpg_leave->group_name,
1565  break;
1566  }
1567 
1568  /* send return */
1569  res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
1571  res_lib_cpg_leave.header.error = error;
1573 }
1574 
1575 /* Finalize message from library */
1576 static void message_handler_req_lib_cpg_finalize (
1577  void *conn,
1578  const void *message)
1579 {
1580  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1582  cs_error_t error = CS_OK;
1583 
1584  log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
1585 
1586  /*
1587  * We will just remove cpd from list. After this call, connection will be
1588  * closed on lib side, and cpg_lib_exit_fn will be called
1589  */
1590  qb_list_del (&cpd->list);
1591  qb_list_init (&cpd->list);
1592 
1593  res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
1595  res_lib_cpg_finalize.header.error = error;
1596 
1598  sizeof (res_lib_cpg_finalize));
1599 }
1600 
1601 static int
1602 memory_map (
1603  const char *path,
1604  size_t bytes,
1605  void **buf)
1606 {
1607  int32_t fd;
1608  void *addr;
1609  int32_t res;
1610 
1611  fd = open (path, O_RDWR, 0600);
1612 
1613  unlink (path);
1614 
1615  if (fd == -1) {
1616  return (-1);
1617  }
1618 
1619  res = ftruncate (fd, bytes);
1620  if (res == -1) {
1621  goto error_close_unlink;
1622  }
1623 
1624  addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
1625  MAP_SHARED, fd, 0);
1626 
1627  if (addr == MAP_FAILED) {
1628  goto error_close_unlink;
1629  }
1630 #ifdef MADV_NOSYNC
1631  madvise(addr, bytes, MADV_NOSYNC);
1632 #endif
1633 
1634  res = close (fd);
1635  if (res) {
1636  munmap (addr, bytes);
1637  return (-1);
1638  }
1639  *buf = addr;
1640  return (0);
1641 
1642 error_close_unlink:
1643  close (fd);
1644  unlink(path);
1645  return -1;
1646 }
1647 
1648 static inline int zcb_alloc (
1649  struct cpg_pd *cpd,
1650  const char *path_to_file,
1651  size_t size,
1652  void **addr)
1653 {
1654  struct zcb_mapped *zcb_mapped;
1655  unsigned int res;
1656 
1657  zcb_mapped = malloc (sizeof (struct zcb_mapped));
1658  if (zcb_mapped == NULL) {
1659  return (-1);
1660  }
1661 
1662  res = memory_map (
1663  path_to_file,
1664  size,
1665  addr);
1666  if (res == -1) {
1667  free (zcb_mapped);
1668  return (-1);
1669  }
1670 
1671  qb_list_init (&zcb_mapped->list);
1672  zcb_mapped->addr = *addr;
1673  zcb_mapped->size = size;
1674  qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
1675  return (0);
1676 }
1677 
1678 
1679 static inline int zcb_free (struct zcb_mapped *zcb_mapped)
1680 {
1681  unsigned int res;
1682 
1683  res = munmap (zcb_mapped->addr, zcb_mapped->size);
1684  qb_list_del (&zcb_mapped->list);
1685  free (zcb_mapped);
1686  return (res);
1687 }
1688 
1689 static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
1690 {
1691  struct qb_list_head *list, *tmp_iter;
1692  struct zcb_mapped *zcb_mapped;
1693  unsigned int res = 0;
1694 
1695  qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
1696  zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
1697 
1698  if (zcb_mapped->addr == addr) {
1699  res = zcb_free (zcb_mapped);
1700  break;
1701  }
1702 
1703  }
1704  return (res);
1705 }
1706 
1707 static inline int zcb_all_free (
1708  struct cpg_pd *cpd)
1709 {
1710  struct qb_list_head *list, *tmp_iter;
1711  struct zcb_mapped *zcb_mapped;
1712 
1713  qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
1714  zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
1715 
1716  zcb_free (zcb_mapped);
1717  }
1718  return (0);
1719 }
1720 
1721 union u {
1722  uint64_t server_addr;
1723  void *server_ptr;
1724 };
1725 
1726 static uint64_t void2serveraddr (void *server_ptr)
1727 {
1728  union u u;
1729 
1730  u.server_ptr = server_ptr;
1731  return (u.server_addr);
1732 }
1733 
1734 static void *serveraddr2void (uint64_t server_addr)
1735 {
1736  union u u;
1737 
1739  return (u.server_ptr);
1740 };
1741 
1742 static void message_handler_req_lib_cpg_zc_alloc (
1743  void *conn,
1744  const void *message)
1745 {
1747  struct qb_ipc_response_header res_header;
1748  void *addr = NULL;
1749  struct coroipcs_zc_header *zc_header;
1750  unsigned int res;
1751  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1752 
1753  log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
1754 
1755  res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
1756  &addr);
1757  assert(res == 0);
1758 
1759  zc_header = (struct coroipcs_zc_header *)addr;
1760  zc_header->server_address = void2serveraddr(addr);
1761 
1762  res_header.size = sizeof (struct qb_ipc_response_header);
1763  res_header.id = 0;
1764  api->ipc_response_send (conn,
1765  &res_header,
1766  res_header.size);
1767 }
1768 
1769 static void message_handler_req_lib_cpg_zc_free (
1770  void *conn,
1771  const void *message)
1772 {
1774  struct qb_ipc_response_header res_header;
1775  void *addr = NULL;
1776  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1777 
1778  log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
1779 
1780  addr = serveraddr2void (hdr->server_address);
1781 
1782  zcb_by_addr_free (cpd, addr);
1783 
1784  res_header.size = sizeof (struct qb_ipc_response_header);
1785  res_header.id = 0;
1786  api->ipc_response_send (
1787  conn, &res_header,
1788  res_header.size);
1789 }
1790 
1791 /* Fragmented mcast message from the library */
1792 static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
1793 {
1794  const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
1795  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1796  mar_cpg_name_t group_name = cpd->group_name;
1797 
1798  struct iovec req_exec_cpg_iovec[2];
1799  struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
1800  struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
1801  int msglen = req_lib_cpg_mcast->fraglen;
1802  int result;
1803  cs_error_t error = CS_ERR_NOT_EXIST;
1804 
1805  log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
1806  log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
1807 
1808  switch (cpd->cpd_state) {
1809  case CPD_STATE_UNJOINED:
1810  error = CS_ERR_NOT_EXIST;
1811  break;
1813  error = CS_ERR_NOT_EXIST;
1814  break;
1816  error = CS_OK;
1817  break;
1819  error = CS_OK;
1820  break;
1821  }
1822 
1823  res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
1824  res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
1825 
1826  if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
1828  }
1830  error = CS_ERR_INTERRUPT;
1831  }
1832 
1833  if (error == CS_OK) {
1834  req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
1835  req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
1837  req_exec_cpg_mcast.pid = cpd->pid;
1838  req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
1839  req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
1840  req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
1841  api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
1842  memcpy(&req_exec_cpg_mcast.group_name, &group_name,
1843  sizeof(mar_cpg_name_t));
1844 
1845  req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
1846  req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
1847  req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
1848  req_exec_cpg_iovec[1].iov_len = msglen;
1849 
1850  result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
1851  assert(result == 0);
1852  } else {
1853  log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
1854  conn, group_name.value, cpd->cpd_state, error);
1855  }
1856 
1857  res_lib_cpg_partial_send.header.error = error;
1858  api->ipc_response_send (conn, &res_lib_cpg_partial_send,
1859  sizeof (res_lib_cpg_partial_send));
1860 }
1861 
1862 /* Mcast message from the library */
1863 static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
1864 {
1865  const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
1866  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1867  mar_cpg_name_t group_name = cpd->group_name;
1868 
1869  struct iovec req_exec_cpg_iovec[2];
1870  struct req_exec_cpg_mcast req_exec_cpg_mcast;
1871  int msglen = req_lib_cpg_mcast->msglen;
1872  int result;
1873  cs_error_t error = CS_ERR_NOT_EXIST;
1874 
1875  log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
1876 
1877  switch (cpd->cpd_state) {
1878  case CPD_STATE_UNJOINED:
1879  error = CS_ERR_NOT_EXIST;
1880  break;
1882  error = CS_ERR_NOT_EXIST;
1883  break;
1885  error = CS_OK;
1886  break;
1888  error = CS_OK;
1889  break;
1890  }
1891 
1892  if (error == CS_OK) {
1893  req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
1894  req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
1896  req_exec_cpg_mcast.pid = cpd->pid;
1897  req_exec_cpg_mcast.msglen = msglen;
1898  api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
1899  memcpy(&req_exec_cpg_mcast.group_name, &group_name,
1900  sizeof(mar_cpg_name_t));
1901 
1902  req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
1903  req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
1904  req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
1905  req_exec_cpg_iovec[1].iov_len = msglen;
1906 
1907  result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
1908  assert(result == 0);
1909  } else {
1910  log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
1911  conn, group_name.value, cpd->cpd_state, error);
1912  }
1913 }
1914 
1915 static void message_handler_req_lib_cpg_zc_execute (
1916  void *conn,
1917  const void *message)
1918 {
1920  struct qb_ipc_request_header *header;
1921  struct res_lib_cpg_mcast res_lib_cpg_mcast;
1922  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
1923  struct iovec req_exec_cpg_iovec[2];
1924  struct req_exec_cpg_mcast req_exec_cpg_mcast;
1926  int result;
1927  cs_error_t error = CS_ERR_NOT_EXIST;
1928 
1929  log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
1930 
1931  header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
1932  req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
1933 
1934  switch (cpd->cpd_state) {
1935  case CPD_STATE_UNJOINED:
1936  error = CS_ERR_NOT_EXIST;
1937  break;
1939  error = CS_ERR_NOT_EXIST;
1940  break;
1942  error = CS_OK;
1943  break;
1945  error = CS_OK;
1946  break;
1947  }
1948 
1949  res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
1950  res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
1951  if (error == CS_OK) {
1952  req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
1953  req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
1955  req_exec_cpg_mcast.pid = cpd->pid;
1956  req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
1957  api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
1958  memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
1959  sizeof(mar_cpg_name_t));
1960 
1961  req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
1962  req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
1963  req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
1964  req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
1965 
1966  result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
1967  if (result == 0) {
1968  res_lib_cpg_mcast.header.error = CS_OK;
1969  } else {
1970  res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
1971  }
1972  } else {
1973  res_lib_cpg_mcast.header.error = error;
1974  }
1975 
1976  api->ipc_response_send (conn, &res_lib_cpg_mcast,
1977  sizeof (res_lib_cpg_mcast));
1978 
1979 }
1980 
1981 static void message_handler_req_lib_cpg_membership (void *conn,
1982  const void *message)
1983 {
1985  (struct req_lib_cpg_membership_get *)message;
1986  struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
1987  struct qb_list_head *iter;
1988  int member_count = 0;
1989 
1990  res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
1991  res_lib_cpg_membership_get.header.error = CS_OK;
1992  res_lib_cpg_membership_get.header.size =
1993  sizeof (struct res_lib_cpg_membership_get);
1994 
1995  qb_list_for_each(iter, &process_info_list_head) {
1996  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
1997  if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
1998  res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
1999  res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
2000  member_count += 1;
2001  }
2002  }
2003  res_lib_cpg_membership_get.member_count = member_count;
2004 
2005  api->ipc_response_send (conn, &res_lib_cpg_membership_get,
2006  sizeof (res_lib_cpg_membership_get));
2007 }
2008 
2009 static void message_handler_req_lib_cpg_local_get (void *conn,
2010  const void *message)
2011 {
2012  struct res_lib_cpg_local_get res_lib_cpg_local_get;
2013 
2014  res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
2015  res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
2016  res_lib_cpg_local_get.header.error = CS_OK;
2017  res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
2018 
2019  api->ipc_response_send (conn, &res_lib_cpg_local_get,
2020  sizeof (res_lib_cpg_local_get));
2021 }
2022 
2023 static void message_handler_req_lib_cpg_iteration_initialize (
2024  void *conn,
2025  const void *message)
2026 {
2028  struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
2029  hdb_handle_t cpg_iteration_handle = 0;
2030  struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
2031  struct qb_list_head *iter, *iter2;
2032  struct cpg_iteration_instance *cpg_iteration_instance;
2033  cs_error_t error = CS_OK;
2034  int res;
2035 
2036  log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
2037 
2038  /* Because between calling this function and *next can be some operations which will
2039  * change list, we must do full copy.
2040  */
2041 
2042  /*
2043  * Create new iteration instance
2044  */
2045  res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
2046  &cpg_iteration_handle);
2047 
2048  if (res != 0) {
2049  error = CS_ERR_NO_MEMORY;
2050  goto response_send;
2051  }
2052 
2053  res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
2054 
2055  if (res != 0) {
2056  error = CS_ERR_BAD_HANDLE;
2057  goto error_destroy;
2058  }
2059 
2060  qb_list_init (&cpg_iteration_instance->items_list_head);
2061  cpg_iteration_instance->handle = cpg_iteration_handle;
2062 
2063  /*
2064  * Create copy of process_info list "grouped by" group name
2065  */
2066  qb_list_for_each(iter, &process_info_list_head) {
2067  struct process_info *pi = qb_list_entry (iter, struct process_info, list);
2068  struct process_info *new_pi;
2069 
2070  if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
2071  /*
2072  * Try to find processed group name in our list new list
2073  */
2074  int found = 0;
2075 
2076  qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
2077  struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
2078 
2079  if (mar_name_compare (&pi2->group, &pi->group) == 0) {
2080  found = 1;
2081  break;
2082  }
2083  }
2084 
2085  if (found) {
2086  /*
2087  * We have this name in list -> don't add
2088  */
2089  continue ;
2090  }
2091  } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
2092  /*
2093  * Test pi group name with request
2094  */
2095  if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
2096  /*
2097  * Not same -> don't add
2098  */
2099  continue ;
2100  }
2101 
2102  new_pi = malloc (sizeof (struct process_info));
2103  if (!new_pi) {
2104  log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
2105 
2106  error = CS_ERR_NO_MEMORY;
2107 
2108  goto error_put_destroy;
2109  }
2110 
2111  memcpy (new_pi, pi, sizeof (struct process_info));
2112  qb_list_init (&new_pi->list);
2113 
2114  if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
2115  /*
2116  * pid and nodeid -> undefined
2117  */
2118  new_pi->pid = new_pi->nodeid = 0;
2119  }
2120 
2121  /*
2122  * We will return list "grouped" by "group name", so try to find right place to add
2123  */
2124  qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
2125  struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
2126 
2127  if (mar_name_compare (&pi2->group, &pi->group) == 0) {
2128  break;
2129  }
2130  }
2131 
2132  qb_list_add (&new_pi->list, iter2);
2133  }
2134 
2135  /*
2136  * Now we have a full "grouped by" copy of process_info list
2137  */
2138 
2139  /*
2140  * Add instance to current cpd list
2141  */
2142  qb_list_init (&cpg_iteration_instance->list);
2143  qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
2144 
2145  cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
2146 
2147 error_put_destroy:
2148  hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2149 error_destroy:
2150  if (error != CS_OK) {
2151  hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2152  }
2153 
2154 response_send:
2155  res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
2156  res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
2157  res_lib_cpg_iterationinitialize.header.error = error;
2158  res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
2159 
2160  api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
2161  sizeof (res_lib_cpg_iterationinitialize));
2162 }
2163 
2164 static void message_handler_req_lib_cpg_iteration_next (
2165  void *conn,
2166  const void *message)
2167 {
2168  const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
2169  struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
2170  struct cpg_iteration_instance *cpg_iteration_instance;
2171  cs_error_t error = CS_OK;
2172  int res;
2173  struct process_info *pi;
2174 
2175  log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
2176 
2177  res = hdb_handle_get (&cpg_iteration_handle_t_db,
2178  req_lib_cpg_iterationnext->iteration_handle,
2179  (void *)&cpg_iteration_instance);
2180 
2181  if (res != 0) {
2182  error = CS_ERR_LIBRARY;
2183  goto error_exit;
2184  }
2185 
2186  assert (cpg_iteration_instance);
2187 
2188  cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
2189 
2190  if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
2191  error = CS_ERR_NO_SECTIONS;
2192  goto error_put;
2193  }
2194 
2195  pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
2196 
2197  /*
2198  * Copy iteration data
2199  */
2200  res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
2201  res_lib_cpg_iterationnext.description.pid = pi->pid;
2202  memcpy (&res_lib_cpg_iterationnext.description.group,
2203  &pi->group,
2204  sizeof (mar_cpg_name_t));
2205 
2206 error_put:
2207  hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
2208 error_exit:
2209  res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
2210  res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
2211  res_lib_cpg_iterationnext.header.error = error;
2212 
2213  api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
2214  sizeof (res_lib_cpg_iterationnext));
2215 }
2216 
2217 static void message_handler_req_lib_cpg_iteration_finalize (
2218  void *conn,
2219  const void *message)
2220 {
2222  struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
2223  struct cpg_iteration_instance *cpg_iteration_instance;
2224  cs_error_t error = CS_OK;
2225  int res;
2226 
2227  log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
2228 
2229  res = hdb_handle_get (&cpg_iteration_handle_t_db,
2230  req_lib_cpg_iterationfinalize->iteration_handle,
2231  (void *)&cpg_iteration_instance);
2232 
2233  if (res != 0) {
2234  error = CS_ERR_LIBRARY;
2235  goto error_exit;
2236  }
2237 
2238  assert (cpg_iteration_instance);
2239 
2240  cpg_iteration_instance_finalize (cpg_iteration_instance);
2241  hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
2242 
2243 error_exit:
2244  res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
2245  res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
2246  res_lib_cpg_iterationfinalize.header.error = error;
2247 
2248  api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
2249  sizeof (res_lib_cpg_iterationfinalize));
2250 }
void *(* ipc_private_data_get)(void *conn)
Definition: coroapi.h:256
#define TOTEM_AGREED
Definition: coroapi.h:102
int initial_totem_conf_sent
Definition: exec/cpg.c:151
const char * name
Definition: coroapi.h:491
Definition: exec/cpg.c:1721
mar_cpg_address_t member_list[]
Definition: ipc_cpg.h:390
mar_req_coroipcc_zc_free_t struct
Definition: ipc_cpg.h:481
#define CPG_MAX_NAME_LENGTH
Definition: cpg.h:115
mar_cpg_address_t struct
Definition: ipc_cpg.h:155
uint64_t initial_transition_counter
Definition: exec/cpg.c:153
#define LOGSYS_LEVEL_TRACE
Definition: logsys.h:77
mar_uint32_t sender_nodeid
Definition: exec/cpg.c:495
#define CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF
Definition: cpg.h:192
The req_lib_cpg_join struct.
Definition: ipc_cpg.h:251
mar_req_coroipcc_zc_alloc_t struct
Definition: ipc_cpg.h:472
The cpg_name struct.
Definition: cpg.h:119
The corosync_service_engine struct.
Definition: coroapi.h:490
int(* ipc_dispatch_iov_send)(void *conn, const struct iovec *iov, unsigned int iov_len)
Definition: coroapi.h:265
int(* ipc_response_send)(void *conn, const void *msg, size_t mlen)
Definition: coroapi.h:258
struct corosync_service_engine * cpg_get_service_engine_ver0(void)
Definition: exec/cpg.c:447
cpg_sync_state
Definition: exec/cpg.c:138
The res_lib_cpg_partial_deliver_callback struct.
Definition: ipc_cpg.h:345
The req_lib_cpg_mcast struct.
Definition: ipc_cpg.h:304
mar_cpg_name_t group
Definition: exec/cpg.c:187
The corosync_lib_handler struct.
Definition: coroapi.h:467
The res_lib_cpg_membership_get struct.
Definition: ipc_cpg.h:375
The res_lib_cpg_iterationnext struct.
Definition: ipc_cpg.h:449
hdb_handle_t handle
Definition: exec/cpg.c:160
uint32_t pid
Definition: exec/cpg.c:186
#define CPG_MEMBERS_MAX
Definition: cpg.h:124
The res_lib_cpg_iterationinitialize struct.
Definition: ipc_cpg.h:433
The corosync_exec_handler struct.
Definition: coroapi.h:475
struct qb_list_head list
Definition: exec/cpg.c:498
int(* totem_mcast)(const struct iovec *iovec, unsigned int iov_len, unsigned int guarantee)
Definition: coroapi.h:279
coroipcs_zc_header struct
Definition: ipc_cpg.h:498
uint64_t transition_counter
Definition: exec/cpg.c:152
#define log_printf(level, format, args...)
Definition: logsys.h:323
The res_lib_cpg_partial_send struct.
Definition: ipc_cpg.h:297
void(* exec_handler_fn)(const void *msg, unsigned int nodeid)
Definition: coroapi.h:476
uint64_t server_address
Definition: ipc_cpg.h:500
void * conn
Definition: exec/cpg.c:146
#define SERVICE_ID_MAKE(a, b)
Definition: coroapi.h:458
The req_lib_cpg_iterationinitialize struct.
Definition: ipc_cpg.h:424
struct qb_list_head list
Definition: exec/cpg.c:90
#define LOGSYS_LEVEL_WARNING
Definition: logsys.h:73
The res_lib_cpg_join struct.
Definition: ipc_cpg.h:261
unsigned int flags
Definition: exec/cpg.c:150
unsigned int(* totem_nodeid_get)(void)
Definition: coroapi.h:275
uint32_t pid
Definition: exec/cpg.c:496
void(* ipc_refcnt_dec)(void *conn)
Definition: coroapi.h:270
struct qb_list_head * current_pointer
Definition: exec/cpg.c:163
struct qb_list_head list
Definition: exec/cpg.c:161
mar_req_coroipcc_zc_execute_t struct
Definition: ipc_cpg.h:490
The res_lib_cpg_mcast struct.
Definition: ipc_cpg.h:326
#define LOGSYS_LEVEL_ERROR
Definition: logsys.h:72
size_t size
Definition: exec/cpg.c:92
mar_cpg_name_t struct
Definition: ipc_cpg.h:112
void * server_ptr
Definition: exec/cpg.c:1723
uint32_t pid
Definition: exec/cpg.c:148
cs_error_t
The cs_error_t enum.
Definition: corotypes.h:94
The req_lib_cpg_leave struct.
Definition: ipc_cpg.h:408
#define LOGSYS_LEVEL_DEBUG
Definition: logsys.h:76
mar_cpg_address_t member_list[PROCESSOR_COUNT_MAX]
Definition: ipc_cpg.h:378
mar_cpg_name_t group_name
Definition: exec/cpg.c:194
The req_lib_cpg_iterationfinalize struct.
Definition: ipc_cpg.h:457
uint8_t mar_uint8_t
Definition: mar_gen.h:51
struct qb_list_head list
Definition: exec/cpg.c:154
mar_cpg_name_t group_name
Definition: exec/cpg.c:147
The corosync_api_v1 struct.
Definition: coroapi.h:225
typedef __attribute__
cpg_message_req_types
Definition: exec/cpg.c:79
LOGSYS_DECLARE_SUBSYS("CPG")
DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db, NULL)
struct totem_message_header header
Definition: totemsrp.c:260
#define swab32(x)
The swab32 macro.
Definition: swab.h:51
enum cpd_state cpd_state
Definition: exec/cpg.c:149
The res_lib_cpg_finalize struct.
Definition: ipc_cpg.h:275
uint32_t mar_uint32_t
Definition: mar_gen.h:53
unsigned int nodeid
Definition: exec/cpg.c:185
struct qb_list_head items_list_head
Definition: exec/cpg.c:162
struct qb_list_head zcb_mapped_list_head
Definition: exec/cpg.c:156
struct qb_list_head list
Definition: exec/cpg.c:188
The res_lib_cpg_local_get struct.
Definition: ipc_cpg.h:289
#define PROCESSOR_COUNT_MAX
Definition: coroapi.h:96
Definition: exec/cpg.c:192
qb_handle_t hdb_handle_t
Definition: hdb.h:52
The memb_ring_id struct.
Definition: coroapi.h:122
The res_lib_cpg_iterationfinalize struct.
Definition: ipc_cpg.h:465
struct corosync_service_engine cpg_service_engine
Definition: exec/cpg.c:426
The req_lib_cpg_partial_mcast struct.
Definition: ipc_cpg.h:314
unsigned int nodeid
Definition: coroapi.h:123
The req_lib_cpg_iterationnext struct.
Definition: ipc_cpg.h:441
const char *(* totem_ifaces_print)(unsigned int nodeid)
Definition: coroapi.h:290
uint32_t pid
Definition: exec/cpg.c:193
The res_lib_cpg_confchg_callback struct.
Definition: ipc_cpg.h:384
void * addr
Definition: exec/cpg.c:91
mar_cpg_name_t group_name
Definition: exec/cpg.c:497
unsigned long long seq
Definition: coroapi.h:124
struct qb_list_head iteration_instance_list_head
Definition: exec/cpg.c:155
void(* lib_handler_fn)(void *conn, const void *msg)
Definition: coroapi.h:468
char type
Definition: totem.h:55
The req_lib_cpg_membership_get struct.
Definition: ipc_cpg.h:367
QB_LIST_DECLARE(cpg_pd_list_head)
int(* ipc_dispatch_send)(void *conn, const void *msg, size_t mlen)
Definition: coroapi.h:263
uint64_t server_addr
Definition: exec/cpg.c:1722
The res_lib_cpg_leave struct.
Definition: ipc_cpg.h:417
unsigned int nodeid
Definition: coroapi.h:75
struct memb_ring_id ring_id
Definition: totemsrp.c:264
mar_cpg_ring_id_t struct
Definition: ipc_cpg.h:230
void(* ipc_source_set)(mar_message_source_t *source, void *conn)
Definition: coroapi.h:252
cpd_state
Definition: exec/cpg.c:131
The res_lib_cpg_totem_confchg_callback struct.
Definition: ipc_cpg.h:398
Message from another node.
Definition: ipc_cpg.h:333
The mar_message_source_t struct.
Definition: coroapi.h:50
void(* ipc_refcnt_inc)(void *conn)
Definition: coroapi.h:268