41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <netinet/in.h>
54 #include <arpa/inet.h>
57 #include <qb/qblist.h>
61 #include <qb/qbipc_common.h>
70 #define MAP_ANONYMOUS MAP_ANON
77 #define GROUP_HASH_SIZE 32
143 static struct qb_list_head joinlist_messages_head;
172 static unsigned int my_member_list_entries;
176 static unsigned int my_old_member_list_entries = 0;
202 static int cpg_lib_init_fn (
void *conn);
204 static int cpg_lib_exit_fn (
void *conn);
206 static void message_handler_req_exec_cpg_procjoin (
210 static void message_handler_req_exec_cpg_procleave (
214 static void message_handler_req_exec_cpg_joinlist (
218 static void message_handler_req_exec_cpg_mcast (
222 static void message_handler_req_exec_cpg_partial_mcast (
226 static void message_handler_req_exec_cpg_downlist_old (
230 static void message_handler_req_exec_cpg_downlist (
234 static void exec_cpg_procjoin_endian_convert (
void *msg);
236 static void exec_cpg_joinlist_endian_convert (
void *msg);
238 static void exec_cpg_mcast_endian_convert (
void *msg);
240 static void exec_cpg_partial_mcast_endian_convert (
void *msg);
242 static void exec_cpg_downlist_endian_convert_old (
void *msg);
244 static void exec_cpg_downlist_endian_convert (
void *msg);
246 static void message_handler_req_lib_cpg_join (
void *conn,
const void *message);
248 static void message_handler_req_lib_cpg_leave (
void *conn,
const void *message);
250 static void message_handler_req_lib_cpg_finalize (
void *conn,
const void *message);
252 static void message_handler_req_lib_cpg_mcast (
void *conn,
const void *message);
254 static void message_handler_req_lib_cpg_partial_mcast (
void *conn,
const void *message);
256 static void message_handler_req_lib_cpg_membership (
void *conn,
257 const void *message);
259 static void message_handler_req_lib_cpg_local_get (
void *conn,
260 const void *message);
262 static void message_handler_req_lib_cpg_iteration_initialize (
264 const void *message);
266 static void message_handler_req_lib_cpg_iteration_next (
268 const void *message);
270 static void message_handler_req_lib_cpg_iteration_finalize (
272 const void *message);
274 static void message_handler_req_lib_cpg_zc_alloc (
276 const void *message);
278 static void message_handler_req_lib_cpg_zc_free (
280 const void *message);
282 static void message_handler_req_lib_cpg_zc_execute (
284 const void *message);
286 static int cpg_node_joinleave_send (
unsigned int pid,
const mar_cpg_name_t *group_name,
int fn,
int reason);
288 static int cpg_exec_send_downlist(
void);
290 static int cpg_exec_send_joinlist(
void);
292 static void downlist_inform_clients (
void);
294 static void joinlist_inform_clients (
void);
296 static void joinlist_messages_delete (
void);
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,
305 static int cpg_sync_process (
void);
307 static void cpg_sync_activate (
void);
309 static void cpg_sync_abort (
void);
311 static void do_proc_join(
317 static void do_proc_leave(
323 static int notify_lib_totem_membership (
325 int member_list_entries,
326 const unsigned int *member_list);
328 static inline int zcb_all_free (
331 static char *cpg_print_group_name (
344 .lib_handler_fn = message_handler_req_lib_cpg_leave,
348 .lib_handler_fn = message_handler_req_lib_cpg_mcast,
352 .lib_handler_fn = message_handler_req_lib_cpg_membership,
356 .lib_handler_fn = message_handler_req_lib_cpg_local_get,
360 .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
364 .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
368 .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
372 .lib_handler_fn = message_handler_req_lib_cpg_finalize,
376 .lib_handler_fn = message_handler_req_lib_cpg_zc_alloc,
380 .lib_handler_fn = message_handler_req_lib_cpg_zc_free,
384 .lib_handler_fn = message_handler_req_lib_cpg_zc_execute,
388 .lib_handler_fn = message_handler_req_lib_cpg_partial_mcast,
398 .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
401 .exec_handler_fn = message_handler_req_exec_cpg_procleave,
402 .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
405 .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
406 .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
409 .exec_handler_fn = message_handler_req_exec_cpg_mcast,
410 .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
413 .exec_handler_fn = message_handler_req_exec_cpg_downlist_old,
414 .exec_endian_convert_fn = exec_cpg_downlist_endian_convert_old
417 .exec_handler_fn = message_handler_req_exec_cpg_downlist,
418 .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
421 .exec_handler_fn = message_handler_req_exec_cpg_partial_mcast,
422 .exec_endian_convert_fn = exec_cpg_partial_mcast_endian_convert
427 .
name =
"corosync cluster closed process group service v1.01",
430 .private_data_size =
sizeof (
struct cpg_pd),
433 .lib_init_fn = cpg_lib_init_fn,
434 .lib_exit_fn = cpg_lib_exit_fn,
435 .lib_engine = cpg_lib_engine,
437 .exec_init_fn = cpg_exec_init_fn,
438 .exec_dump_fn = NULL,
439 .exec_engine = cpg_exec_engine,
441 .sync_init = cpg_sync_init,
442 .sync_process = cpg_sync_process,
443 .sync_activate = cpg_sync_activate,
444 .sync_abort = cpg_sync_abort
513 for (i = 0; i < group->length; i++) {
516 if (c >=
' ' && c < 0x7f && c !=
'\\') {
520 res[dest_pos++] =
'\\';
521 res[dest_pos++] =
'\\';
523 snprintf(res + dest_pos,
sizeof(res) - dest_pos,
"\\x%02X", c);
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,
546 memcpy (my_member_list, member_list, member_list_entries *
547 sizeof (
unsigned int));
548 my_member_list_entries = member_list_entries;
557 for (i = 0; i < my_old_member_list_entries; i++) {
559 for (j = 0; j < trans_list_entries; j++) {
560 if (my_old_member_list[i] == trans_list[j]) {
566 g_req_exec_cpg_downlist.nodeids[entries++] =
567 my_old_member_list[i];
570 g_req_exec_cpg_downlist.left_nodes = entries;
573 static int cpg_sync_process (
void)
578 res = cpg_exec_send_downlist();
585 res = cpg_exec_send_joinlist();
590 static void cpg_sync_activate (
void)
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;
596 downlist_inform_clients ();
598 joinlist_inform_clients ();
600 joinlist_messages_delete ();
602 notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
605 static void cpg_sync_abort (
void)
608 joinlist_messages_delete ();
611 static int notify_lib_totem_membership (
613 int member_list_entries,
614 const unsigned int *member_list)
616 struct qb_list_head *iter;
628 res->member_list_entries = member_list_entries;
629 res->header.size = size;
631 res->header.error =
CS_OK;
637 qb_list_for_each(iter, &cpg_pd_list_head) {
648 static int notify_lib_joinlist(
651 int joined_list_entries,
653 int left_list_entries,
659 struct qb_list_head *iter;
666 qb_list_for_each(iter, &process_info_list_head) {
668 if (mar_name_compare (&pi->
group, group_name) == 0) {
672 for (i = 0; i < left_list_entries; i++) {
673 if (left_list[i].
nodeid == pi->
nodeid && left_list[i].pid == pi->
pid) {
690 res->joined_list_entries = joined_list_entries;
691 res->left_list_entries = left_list_entries;
692 res->member_list_entries = count;
694 res->header.size = size;
696 res->header.error =
CS_OK;
699 qb_list_for_each(iter, &process_info_list_head) {
702 if (mar_name_compare (&pi->
group, group_name) == 0) {
706 for (i = 0;i < left_list_entries; i++) {
707 if (left_list[i].
nodeid == pi->
nodeid && left_list[i].pid == pi->
pid) {
713 retgi->nodeid = pi->
nodeid;
714 retgi->pid = pi->
pid;
721 if (left_list_entries) {
723 retgi += left_list_entries;
726 if (joined_list_entries) {
728 retgi += joined_list_entries;
734 qb_list_for_each(iter, &cpg_pd_list_head) {
737 assert (joined_list_entries <= 1);
738 if (joined_list_entries) {
739 if (joined_list[0].
pid == cpd->
pid &&
750 if (left_list_entries) {
751 if (left_list[0].
pid == cpd->
pid &&
768 qb_list_for_each(iter, &cpg_pd_list_head) {
774 notify_lib_totem_membership (cpd->
conn, my_old_member_list_entries, my_old_member_list);
784 "%s: members(old:%d left:%d)",
790 static void downlist_inform_clients (
void)
792 struct qb_list_head *iter, *tmp_iter;
800 int left_list_entries;
801 struct qb_list_head list;
803 qb_map_iter_t *miter;
806 downlist_log(
"my downlist", &g_req_exec_cpg_downlist);
808 group_map = qb_skiplist_create();
815 qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
819 for (i = 0; i < g_req_exec_cpg_downlist.left_nodes; i++) {
821 if (pi->
nodeid == g_req_exec_cpg_downlist.nodeids[i]) {
828 marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->
group);
829 cpg_group.value[cpg_group.length] = 0;
831 pcd = (
struct confchg_data *)qb_map_get(group_map, cpg_group.value);
833 pcd = (
struct confchg_data *)calloc(1,
sizeof(
struct confchg_data));
834 memcpy(&pcd->cpg_group, &cpg_group,
sizeof(
struct cpg_name));
835 qb_map_put(group_map, pcd->cpg_group.value, pcd);
837 size = pcd->left_list_entries;
838 pcd->left_list[size].nodeid = left_pi->
nodeid;
839 pcd->left_list[size].pid = left_pi->
pid;
841 pcd->left_list_entries++;
842 qb_list_del (&left_pi->
list);
848 miter = qb_map_iter_create(group_map);
849 while (qb_map_iter_next(miter, (
void **)&pcd)) {
850 marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
852 log_printf (LOG_DEBUG,
"left_list_entries:%d", pcd->left_list_entries);
853 for (i=0; i<pcd->left_list_entries; i++) {
854 log_printf (LOG_DEBUG,
"left_list[%d] group:%s, ip:%s, pid:%d",
855 i, cpg_print_group_name(&group),
857 pcd->left_list[i].pid);
861 notify_lib_joinlist(&group, NULL,
863 pcd->left_list_entries,
869 qb_map_iter_free(miter);
870 qb_map_destroy(group_map);
876 static void joinlist_remove_zombie_pi_entries (
void)
878 struct qb_list_head *pi_iter, *tmp_iter;
879 struct qb_list_head *jl_iter;
884 qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
898 qb_list_for_each(jl_iter, &joinlist_messages_head) {
906 pi->
pid == stored_msg->
pid &&
919 static void joinlist_inform_clients (
void)
922 struct qb_list_head *iter;
926 qb_list_for_each(iter, &joinlist_messages_head) {
927 stored_msg = qb_list_entry(iter,
struct joinlist_msg, list);
929 log_printf (LOG_DEBUG,
"joinlist_messages[%u] group:%s, ip:%s, pid:%d",
930 i++, cpg_print_group_name(&stored_msg->
group_name),
943 joinlist_remove_zombie_pi_entries ();
946 static void joinlist_messages_delete (
void)
949 struct qb_list_head *iter, *tmp_iter;
951 qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
952 stored_msg = qb_list_entry(iter,
struct joinlist_msg, list);
953 qb_list_del (&stored_msg->
list);
956 qb_list_init (&joinlist_messages_head);
961 qb_list_init (&joinlist_messages_head);
968 struct qb_list_head *iter, *tmp_iter;
973 qb_list_del (&pi->
list);
981 static void cpg_pd_finalize (
struct cpg_pd *cpd)
983 struct qb_list_head *iter, *tmp_iter;
990 cpg_iteration_instance_finalize (cpii);
993 qb_list_del (&cpd->
list);
996 static int cpg_lib_exit_fn (
void *conn)
1007 cpg_pd_finalize (cpd);
1016 struct iovec req_exec_cpg_iovec;
1035 static void exec_cpg_procjoin_endian_convert (
void *msg)
1044 static void exec_cpg_joinlist_endian_convert (
void *msg_v)
1047 struct qb_ipc_response_header *res = (
struct qb_ipc_response_header *)msg;
1050 swab_mar_int32_t (&res->size);
1052 while ((
const char*)jle < msg + res->size) {
1059 static void exec_cpg_downlist_endian_convert_old (
void *msg)
1063 static void exec_cpg_downlist_endian_convert (
void *msg)
1077 static void exec_cpg_mcast_endian_convert (
void *msg)
1088 static void exec_cpg_partial_mcast_endian_convert (
void *msg)
1102 struct qb_list_head *iter;
1104 qb_list_for_each(iter, &process_info_list_head) {
1108 mar_name_compare (&pi->
group, group_name) == 0) {
1116 static void do_proc_join(
1125 struct qb_list_head *list;
1126 struct qb_list_head *list_to_add = NULL;
1128 if (process_info_find (name, pid,
nodeid) != NULL) {
1138 memcpy(&pi->
group, name,
sizeof(*name));
1139 qb_list_init(&pi->
list);
1144 list_to_add = &process_info_list_head;
1145 qb_list_for_each(list, &process_info_list_head) {
1146 pi_entry = qb_list_entry(list,
struct process_info, list);
1154 qb_list_add (&pi->
list, list_to_add);
1156 notify_info.pid = pi->
pid;
1157 notify_info.nodeid =
nodeid;
1158 notify_info.reason = reason;
1160 notify_lib_joinlist(&pi->
group, NULL,
1166 static void do_proc_leave(
1173 struct qb_list_head *iter, *tmp_iter;
1176 notify_info.pid = pid;
1177 notify_info.nodeid =
nodeid;
1178 notify_info.reason = reason;
1180 notify_lib_joinlist(name, NULL,
1185 qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
1189 mar_name_compare (&pi->
group, name)==0) {
1190 qb_list_del (&pi->
list);
1196 static void message_handler_req_exec_cpg_downlist_old (
1197 const void *message,
1204 static void message_handler_req_exec_cpg_downlist(
1205 const void *message,
1215 static void message_handler_req_exec_cpg_procjoin (
1216 const void *message,
1231 static void message_handler_req_exec_cpg_procleave (
1232 const void *message,
1249 static void message_handler_req_exec_cpg_joinlist (
1250 const void *message_v,
1253 const char *message = message_v;
1254 const struct qb_ipc_response_header *res = (
const struct qb_ipc_response_header *)message;
1261 while ((
const char*)jle < message + res->size) {
1265 stored_msg->
pid = jle->
pid;
1267 qb_list_init (&stored_msg->
list);
1268 qb_list_add (&stored_msg->
list, &joinlist_messages_head);
1273 static void message_handler_req_exec_cpg_mcast (
1274 const void *message,
1280 struct qb_list_head *iter, *pi_iter, *tmp_iter;
1282 struct iovec iovec[2];
1297 iovec[1].iov_len = msglen;
1299 qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1300 cpd = qb_list_entry(iter,
struct cpg_pd, list);
1306 qb_list_for_each(pi_iter, &process_info_list_head) {
1327 static void message_handler_req_exec_cpg_partial_mcast (
1328 const void *message,
1334 struct qb_list_head *iter, *pi_iter, *tmp_iter;
1336 struct iovec iovec[2];
1355 iovec[1].iov_len = msglen;
1357 qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
1358 cpd = qb_list_entry(iter,
struct cpg_pd, list);
1365 qb_list_for_each(pi_iter, &process_info_list_head) {
1387 static int cpg_exec_send_downlist(
void)
1394 g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
1396 iov.iov_base = (
void *)&g_req_exec_cpg_downlist;
1397 iov.iov_len = g_req_exec_cpg_downlist.header.size;
1402 static int cpg_exec_send_joinlist(
void)
1405 struct qb_list_head *iter;
1406 struct qb_ipc_response_header *res;
1409 struct iovec req_exec_cpg_iovec;
1411 qb_list_for_each(iter, &process_info_list_head) {
1423 buf = alloca(
sizeof(
struct qb_ipc_response_header) +
sizeof(
struct join_list_entry) * count);
1429 jle = (
struct join_list_entry *)(buf +
sizeof(
struct qb_ipc_response_header));
1430 res = (
struct qb_ipc_response_header *)buf;
1432 qb_list_for_each(iter, &process_info_list_head) {
1443 res->size =
sizeof(
struct qb_ipc_response_header)+sizeof(struct
join_list_entry) * count;
1445 req_exec_cpg_iovec.iov_base = buf;
1446 req_exec_cpg_iovec.iov_len = res->size;
1451 static int cpg_lib_init_fn (
void *conn)
1454 memset (cpd, 0,
sizeof(
struct cpg_pd));
1456 qb_list_add (&cpd->
list, &cpg_pd_list_head);
1467 static void message_handler_req_lib_cpg_join (
void *
conn,
const void *message)
1473 struct qb_list_head *iter;
1476 qb_list_for_each(iter, &cpg_pd_list_head) {
1492 qb_list_for_each(iter, &process_info_list_head) {
1540 static void message_handler_req_lib_cpg_leave (
void *conn,
const void *message)
1577 static void message_handler_req_lib_cpg_finalize (
1579 const void *message)
1591 qb_list_del (&cpd->
list);
1592 qb_list_init (&cpd->
list);
1612 fd = open (path, O_RDWR, 0600);
1620 res = ftruncate (fd, bytes);
1622 goto error_close_unlink;
1625 addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
1628 if (
addr == MAP_FAILED) {
1629 goto error_close_unlink;
1632 madvise(
addr, bytes, MADV_NOSYNC);
1637 munmap (
addr, bytes);
1649 static inline int zcb_alloc (
1651 const char *path_to_file,
1690 static inline int zcb_by_addr_free (
struct cpg_pd *cpd,
void *
addr)
1692 struct qb_list_head *list, *tmp_iter;
1694 unsigned int res = 0;
1708 static inline int zcb_all_free (
1711 struct qb_list_head *list, *tmp_iter;
1727 static uint64_t void2serveraddr (
void *server_ptr)
1735 static void *serveraddr2void (uint64_t
server_addr)
1743 static void message_handler_req_lib_cpg_zc_alloc (
1745 const void *message)
1748 struct qb_ipc_response_header res_header;
1756 res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
1763 res_header.size =
sizeof (
struct qb_ipc_response_header);
1770 static void message_handler_req_lib_cpg_zc_free (
1772 const void *message)
1775 struct qb_ipc_response_header res_header;
1781 addr = serveraddr2void (hdr->server_address);
1783 zcb_by_addr_free (cpd,
addr);
1785 res_header.size =
sizeof (
struct qb_ipc_response_header);
1793 static void message_handler_req_lib_cpg_partial_mcast (
void *conn,
const void *message)
1799 struct iovec req_exec_cpg_iovec[2];
1834 if (error ==
CS_OK) {
1849 req_exec_cpg_iovec[1].iov_len = msglen;
1852 assert(result == 0);
1855 conn, group_name.value, cpd->
cpd_state, error);
1864 static void message_handler_req_lib_cpg_mcast (
void *conn,
const void *message)
1870 struct iovec req_exec_cpg_iovec[2];
1893 if (error ==
CS_OK) {
1906 req_exec_cpg_iovec[1].iov_len = msglen;
1909 assert(result == 0);
1912 conn, group_name.value, cpd->
cpd_state, error);
1916 static void message_handler_req_lib_cpg_zc_execute (
1918 const void *message)
1921 struct qb_ipc_request_header *
header;
1924 struct iovec req_exec_cpg_iovec[2];
1932 header = (
struct qb_ipc_request_header *)(((
char *)serveraddr2void(hdr->server_address) + sizeof (
struct coroipcs_zc_header)));
1952 if (error ==
CS_OK) {
1982 static void message_handler_req_lib_cpg_membership (
void *conn,
1983 const void *message)
1988 struct qb_list_head *iter;
1989 int member_count = 0;
1996 qb_list_for_each(iter, &process_info_list_head) {
2010 static void message_handler_req_lib_cpg_local_get (
void *conn,
2011 const void *message)
2024 static void message_handler_req_lib_cpg_iteration_initialize (
2026 const void *message)
2032 struct qb_list_head *iter, *iter2;
2047 &cpg_iteration_handle);
2054 res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (
void *)&
cpg_iteration_instance);
2067 qb_list_for_each(iter, &process_info_list_head) {
2080 if (mar_name_compare (&pi2->
group, &pi->
group) == 0) {
2109 goto error_put_destroy;
2113 qb_list_init (&new_pi->
list);
2128 if (mar_name_compare (&pi2->
group, &pi->
group) == 0) {
2133 qb_list_add (&new_pi->
list, iter2);
2149 hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2151 if (error !=
CS_OK) {
2152 hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
2165 static void message_handler_req_lib_cpg_iteration_next (
2167 const void *message)
2178 res = hdb_handle_get (&cpg_iteration_handle_t_db,
2218 static void message_handler_req_lib_cpg_iteration_finalize (
2220 const void *message)
2230 res = hdb_handle_get (&cpg_iteration_handle_t_db,