diff --git a/qdevices/qdevice-ipc-cmd.c b/qdevices/qdevice-ipc-cmd.c index ff00ec0b..2fc14035 100644 --- a/qdevices/qdevice-ipc-cmd.c +++ b/qdevices/qdevice-ipc-cmd.c @@ -1,265 +1,265 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "qdevice-ipc-cmd.h" #include "qdevice-log.h" #include "qdevice-model.h" #include "dynar-str.h" #include "utils.h" static int qdevice_ipc_cmd_status_add_header(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { return ((dynar_str_catf(outbuf, "Qdevice information\n") != -1) && (dynar_str_catf(outbuf, "-------------------\n") != -1)); } static int qdevice_ipc_cmd_status_add_model(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { return (dynar_str_catf(outbuf, "Model:\t\t\t%s\n", qdevice_model_type_to_str(instance->model_type)) != -1); } static int qdevice_ipc_cmd_status_add_nodeid(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { return (dynar_str_catf(outbuf, "Node ID:\t\t"UTILS_PRI_NODE_ID"\n", instance->node_id) != -1); } static int qdevice_ipc_cmd_status_add_intervals(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { if (!verbose) { return (1); } return ((dynar_str_catf(outbuf, "HB interval:\t\t%"PRIu32"ms\n", instance->heartbeat_interval) != -1) && (dynar_str_catf(outbuf, "Sync HB interval:\t%"PRIu32"ms\n", instance->sync_heartbeat_interval) != -1)); } static int qdevice_ipc_cmd_status_add_config_node_list(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { struct node_list_entry *node_info; size_t zi; if (instance->config_node_list_version_set) { if (dynar_str_catf(outbuf, "Configuration version:\t"UTILS_PRI_CONFIG_VERSION"\n", instance->config_node_list_version) == -1) { return (0); } } if (dynar_str_catf(outbuf, "Configured node list:\n") == -1) { return (0); } zi = 0; TAILQ_FOREACH(node_info, &instance->config_node_list, entries) { if ((dynar_str_catf(outbuf, " %zu\tNode ID = "UTILS_PRI_NODE_ID, zi, node_info->node_id) == -1) || (node_info->data_center_id != 0 && dynar_str_catf(outbuf, ", Data center ID = " UTILS_PRI_DATACENTER_ID, node_info->data_center_id) == -1) || (dynar_str_catf(outbuf, "\n") == -1)) { return (0); } zi++; } return (1); } static int qdevice_ipc_cmd_status_add_membership_node_list(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { uint32_t u32; if (verbose && dynar_str_catf(outbuf, "Ring ID:\t\t"UTILS_PRI_RING_ID"\n", instance->vq_node_list_ring_id.nodeid, instance->vq_node_list_ring_id.seq) == -1) { return (0); } if (dynar_str_catf(outbuf, "Membership node list:\t") == -1) { return (0); } for (u32 = 0; u32 < instance->vq_node_list_entries; u32++) { if (u32 != 0) { if (dynar_str_catf(outbuf, ", ") == -1) { return (0); } } if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, instance->vq_node_list[u32]) == -1) { return (0); } } if (dynar_str_catf(outbuf, "\n") == -1) { return (0); } return (1); } static const char * qdevice_ipc_cmd_vq_nodestate_to_str(uint32_t state) { switch (state) { case VOTEQUORUM_NODESTATE_MEMBER: return ("member"); break; case VOTEQUORUM_NODESTATE_DEAD: return ("dead"); break; case VOTEQUORUM_NODESTATE_LEAVING: return ("leaving"); break; default: qdevice_log(LOG_ERR, "qdevice_ipc_cmd_vq_nodestate_to_str: Unhandled votequorum " "node state %"PRIu32, state); exit(1); break; } return ("Unhandled votequorum node state"); } static int qdevice_ipc_cmd_status_add_quorum_node_list(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { uint32_t u32; votequorum_node_t *node; if (!verbose) { return (1); } if (dynar_str_catf(outbuf, "Quorate:\t\t%s\n", (instance->vq_quorum_quorate ? "Yes" : "No")) == -1) { return (0); } if (dynar_str_catf(outbuf, "Quorum node list:\n") == -1) { return (0); } for (u32 = 0; u32 < instance->vq_quorum_node_list_entries; u32++) { node = &instance->vq_quorum_node_list[u32]; if (node->nodeid == 0) { - continue ; + continue; } if (dynar_str_catf(outbuf, " %"PRIu32"\tNode ID = "UTILS_PRI_NODE_ID ", State = %s\n", u32, node->nodeid, qdevice_ipc_cmd_vq_nodestate_to_str(node->state)) == -1) { return (0); } } return (1); } static int qdevice_ipc_cmd_status_add_expected_votes(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { if (!verbose) { return (1); } return (dynar_str_catf(outbuf, "Expected votes:\t\t"UTILS_PRI_EXPECTED_VOTES"\n", instance->vq_expected_votes) != -1); } static int qdevice_ipc_cmd_status_add_last_poll(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { struct tm tm_res; if (!verbose) { return (1); } if (instance->vq_last_poll == ((time_t) -1)) { return (dynar_str_catf(outbuf, "Last poll call:\t\tNever\n") != -1); } localtime_r(&instance->vq_last_poll, &tm_res); if (dynar_str_catf(outbuf, "Last poll call:\t\t%04d-%02d-%02dT%02d:%02d:%02d%s\n", tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday, tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec, (instance->vq_last_poll_cast_vote ? " (cast vote)" : "")) == -1) { return (0); } return (1); } int qdevice_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) { if (qdevice_ipc_cmd_status_add_header(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_model(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_nodeid(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_intervals(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_config_node_list(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_membership_node_list(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_quorum_node_list(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_expected_votes(instance, outbuf, verbose) && qdevice_ipc_cmd_status_add_last_poll(instance, outbuf, verbose) && dynar_str_catf(outbuf, "\n") != -1 && qdevice_model_ipc_cmd_status(instance, outbuf, verbose) != -1) { return (0); } return (-1); } diff --git a/qdevices/qdevice-net-algo-ffsplit.c b/qdevices/qdevice-net-algo-ffsplit.c index 6fa513b8..8c7c9422 100644 --- a/qdevices/qdevice-net-algo-ffsplit.c +++ b/qdevices/qdevice-net-algo-ffsplit.c @@ -1,292 +1,292 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "qdevice-net-algo-ffsplit.h" #include "qdevice-log.h" #include "qdevice-net-send.h" #include "qdevice-net-cast-vote-timer.h" #include "qdevice-votequorum.h" #include "utils.h" static int check_vqinfo_validity(struct qdevice_net_instance *instance) { struct qdevice_instance *qdev_instance; struct votequorum_info vq_info; cs_error_t cs_res; struct node_list_entry *node; uint32_t node_id; qdev_instance = instance->qdevice_instance_ptr; TAILQ_FOREACH(node, &qdev_instance->config_node_list, entries) { node_id = node->node_id; cs_res = votequorum_getinfo(qdev_instance->votequorum_handle, node_id, &vq_info); if (cs_res == CS_ERR_NOT_EXIST) { - continue ; + continue; } else if (cs_res != CS_OK) { qdevice_log(LOG_CRIT, "Can't get votequorum information for node " UTILS_PRI_NODE_ID ". Error %s", node_id, cs_strerror(cs_res)); return (-1); } if (vq_info.node_votes != 1) { qdevice_log(LOG_CRIT, "50:50 split algorithm works only if all nodes have " "exactly 1 vote. Node " UTILS_PRI_NODE_ID " has %u votes!", node_id, vq_info.node_votes); return (-1); } if (vq_info.qdevice_votes != 1) { qdevice_log(LOG_CRIT, "50:50 split algorithm works only if qdevice has " "exactly 1 vote. Node "UTILS_PRI_NODE_ID" has %u votes!", node_id, vq_info.qdevice_votes); return (-1); } } return (0); } static int check_cmap_validity(struct qdevice_net_instance *instance) { struct qdevice_instance *qdev_instance; uint32_t qdevice_votes; qdev_instance = instance->qdevice_instance_ptr; if (cmap_get_uint32(qdev_instance->cmap_handle, "quorum.device.votes", &qdevice_votes) != CS_OK || qdevice_votes != 1) { qdevice_log(LOG_CRIT, "50:50 split algorithm works only if quorum.device.votes" " configuration key is set to 1!"); return (-1); } return (0); } int qdevice_net_algo_ffsplit_init(struct qdevice_net_instance *instance) { if (check_cmap_validity(instance) != 0 || check_vqinfo_validity(instance) != 0) { return (-1); } return (0); } int qdevice_net_algo_ffsplit_connected(struct qdevice_net_instance *instance, int *send_config_node_list, int *send_membership_node_list, int *send_quorum_node_list, enum tlv_vote *vote) { return (0); } int qdevice_net_algo_ffsplit_config_node_list_changed(struct qdevice_net_instance *instance, const struct node_list *nlist, int config_version_set, uint64_t config_version, int *send_node_list, enum tlv_vote *vote) { if (check_vqinfo_validity(instance) != 0) { return (-1); } return (0); } int qdevice_net_algo_ffsplit_votequorum_node_list_notify(struct qdevice_net_instance *instance, const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[], int *send_node_list, enum tlv_vote *vote) { return (0); } int qdevice_net_algo_ffsplit_votequorum_quorum_notify(struct qdevice_net_instance *instance, uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[], int *send_node_list, enum tlv_vote *vote) { return (0); } int qdevice_net_algo_ffsplit_votequorum_expected_votes_notify(struct qdevice_net_instance *instance, uint32_t expected_votes, enum tlv_vote *vote) { if (check_vqinfo_validity(instance) != 0) { return (-1); } return (0); } int qdevice_net_algo_ffsplit_config_node_list_reply_received(struct qdevice_net_instance *instance, uint32_t seq_number, int initial, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote) { if (!ring_id_is_valid) { *vote = TLV_VOTE_NO_CHANGE; } return (0); } int qdevice_net_algo_ffsplit_membership_node_list_reply_received(struct qdevice_net_instance *instance, uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote) { if (!ring_id_is_valid) { *vote = TLV_VOTE_NO_CHANGE; } return (0); } int qdevice_net_algo_ffsplit_quorum_node_list_reply_received(struct qdevice_net_instance *instance, uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote) { if (!ring_id_is_valid) { *vote = TLV_VOTE_NO_CHANGE; } return (0); } int qdevice_net_algo_ffsplit_ask_for_vote_reply_received(struct qdevice_net_instance *instance, uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote) { if (!ring_id_is_valid) { *vote = TLV_VOTE_NO_CHANGE; } return (0); } int qdevice_net_algo_ffsplit_vote_info_received(struct qdevice_net_instance *instance, uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote) { if (!ring_id_is_valid) { *vote = TLV_VOTE_NO_CHANGE; } return (0); } int qdevice_net_algo_ffsplit_echo_reply_received(struct qdevice_net_instance *instance, uint32_t seq_number, int is_expected_seq_number) { return (is_expected_seq_number ? 0 : -1); } int qdevice_net_algo_ffsplit_echo_reply_not_received(struct qdevice_net_instance *instance) { return (-1); } int qdevice_net_algo_ffsplit_disconnected(struct qdevice_net_instance *instance, enum qdevice_net_disconnect_reason disconnect_reason, int *try_reconnect, enum tlv_vote *vote) { /* * We cannot depend on default behavior (until there is no change -> use old vote). * This could create two quorate clusters (2:2 -> first half get ACK -> first half * disconnects from qnetd -> second half get ACK -> two quorate clusters) */ *vote = TLV_VOTE_NACK; return (0); } void qdevice_net_algo_ffsplit_destroy(struct qdevice_net_instance *instance) { } static struct qdevice_net_algorithm qdevice_net_algo_ffsplit = { .init = qdevice_net_algo_ffsplit_init, .connected = qdevice_net_algo_ffsplit_connected, .config_node_list_changed = qdevice_net_algo_ffsplit_config_node_list_changed, .votequorum_node_list_notify = qdevice_net_algo_ffsplit_votequorum_node_list_notify, .votequorum_quorum_notify = qdevice_net_algo_ffsplit_votequorum_quorum_notify, .votequorum_expected_votes_notify = qdevice_net_algo_ffsplit_votequorum_expected_votes_notify, .config_node_list_reply_received = qdevice_net_algo_ffsplit_config_node_list_reply_received, .membership_node_list_reply_received = qdevice_net_algo_ffsplit_membership_node_list_reply_received, .quorum_node_list_reply_received = qdevice_net_algo_ffsplit_quorum_node_list_reply_received, .ask_for_vote_reply_received = qdevice_net_algo_ffsplit_ask_for_vote_reply_received, .vote_info_received = qdevice_net_algo_ffsplit_vote_info_received, .echo_reply_received = qdevice_net_algo_ffsplit_echo_reply_received, .echo_reply_not_received = qdevice_net_algo_ffsplit_echo_reply_not_received, .disconnected = qdevice_net_algo_ffsplit_disconnected, .destroy = qdevice_net_algo_ffsplit_destroy, }; int qdevice_net_algo_ffsplit_register(void) { return (qdevice_net_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_FFSPLIT, &qdevice_net_algo_ffsplit)); } diff --git a/qdevices/qnet-config.h b/qdevices/qnet-config.h index d2772816..cb92b02c 100644 --- a/qdevices/qnet-config.h +++ b/qdevices/qnet-config.h @@ -1,142 +1,142 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _QNET_CONFIG_H_ #define _QNET_CONFIG_H_ #include #include "tlv.h" #ifdef __cplusplus extern "C" { #endif /* * There are "hardcoded" defaults for both qnetd and qdevice-net. It's not so good * idea to change them as long as you are not 100% sure what you are doing. Also * most of them can be changed in CLI via advanced_settings (-S). */ #define QNETD_PROGRAM_NAME "corosync-qnetd" #define QNETD_DEFAULT_HOST_PORT 5403 #define QNETD_DEFAULT_LISTEN_BACKLOG 10 #define QNETD_MIN_LISTEN_BACKLOG 1 -#define QNETD_DEFAULT_MAX_CLIENT_SEND_BUFFERS 10 +#define QNETD_DEFAULT_MAX_CLIENT_SEND_BUFFERS 32 #define QNETD_MIN_CLIENT_SEND_BUFFERS 2 #define QNETD_DEFAULT_MAX_CLIENT_SEND_SIZE (1 << 15) #define QNETD_DEFAULT_MAX_CLIENT_RECEIVE_SIZE (1 << 15) #define QNETD_MIN_CLIENT_RECEIVE_SEND_SIZE 16 #define QNETD_DEFAULT_MAX_CLIENTS 0 #define QNETD_DEFAULT_NSS_DB_DIR COROSYSCONFDIR "/qdevice/net/qnetd/nssdb" #define QNETD_DEFAULT_CERT_NICKNAME "QNetd Cert" #define QNETD_DEFAULT_TLS_SUPPORTED TLV_TLS_SUPPORTED #define QNETD_DEFAULT_TLS_CLIENT_CERT_REQUIRED 1 #define QNETD_DEFAULT_HEARTBEAT_INTERVAL_MIN (1*1000) #define QNETD_DEFAULT_HEARTBEAT_INTERVAL_MAX (2*60*1000) #define QNETD_MIN_HEARTBEAT_INTERVAL 1 #define QNETD_DEFAULT_DPD_ENABLED 1 #define QNETD_DEFAULT_DPD_INTERVAL (10*1000) #define QNETD_MIN_DPD_INTERVAL 1 #define QNETD_DEFAULT_LOCK_FILE LOCALSTATEDIR"/run/corosync-qnetd.pid" #define QNETD_DEFAULT_LOCAL_SOCKET_FILE LOCALSTATEDIR"/run/corosync-qnetd.sock" #define QNETD_DEFAULT_LOCAL_SOCKET_BACKLOG 10 #define QNETD_MIN_LOCAL_SOCKET_BACKLOG 1 #define QNETD_DEFAULT_IPC_MAX_CLIENTS 10 #define QNETD_MIN_IPC_MAX_CLIENTS 0 #define QNETD_DEFAULT_IPC_MAX_RECEIVE_SIZE (4*1024) #define QNETD_DEFAULT_IPC_MAX_SEND_SIZE (10*1024*1024) #define QNETD_MIN_IPC_RECEIVE_SEND_SIZE 1024 #define QNETD_TOOL_PROGRAM_NAME "corosync-qnetd-tool" #define QDEVICE_NET_DEFAULT_NSS_DB_DIR COROSYSCONFDIR "/qdevice/net/node/nssdb" #define QDEVICE_NET_DEFAULT_INITIAL_MSG_RECEIVE_SIZE (1 << 15) #define QDEVICE_NET_DEFAULT_INITIAL_MSG_SEND_SIZE (1 << 15) #define QDEVICE_NET_DEFAULT_MIN_MSG_SEND_SIZE QDEVICE_NET_DEFAULT_INITIAL_MSG_SEND_SIZE #define QDEVICE_NET_DEFAULT_MAX_MSG_RECEIVE_SIZE (1 << 24) #define QDEVICE_NET_DEFAULT_MAX_SEND_BUFFERS 10 #define QDEVICE_NET_MIN_MAX_SEND_BUFFERS 2 #define QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE 16 #define QDEVICE_NET_DEFAULT_NSS_QNETD_CN "Qnetd Server" #define QDEVICE_NET_DEFAULT_NSS_CLIENT_CERT_NICKNAME "Cluster Cert" #define QDEVICE_NET_DEFAULT_ALGORITHM TLV_DECISION_ALGORITHM_TYPE_TEST #define QDEVICE_NET_DEFAULT_TLS_SUPPORTED TLV_TLS_SUPPORTED #define QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE TLV_TIE_BREAKER_MODE_LOWEST #define QDEVICE_NET_DEFAULT_HEARTBEAT_INTERVAL_MIN QNETD_DEFAULT_HEARTBEAT_INTERVAL_MIN #define QDEVICE_NET_DEFAULT_HEARTBEAT_INTERVAL_MAX QNETD_DEFAULT_HEARTBEAT_INTERVAL_MAX #define QDEVICE_NET_MIN_HEARTBEAT_INTERVAL 1 #define QDEVICE_NET_DEFAULT_MIN_CONNECT_TIMEOUT (1*1000) #define QDEVICE_NET_DEFAULT_MAX_CONNECT_TIMEOUT (2*60*1000) #define QDEVICE_NET_MIN_CONNECT_TIMEOUT 1 #ifdef DEBUG #define QDEVICE_NET_DEFAULT_TEST_ALGORITHM_ENABLED 1 #else #define QDEVICE_NET_DEFAULT_TEST_ALGORITHM_ENABLED 0 #endif #define QDEVICE_NET_DEFAULT_DELAY_BEFORE_RECONNECT (1000) #define QDEVICE_NET_MIN_DELAY_BEFORE_RECONNECT 1 /* * Decision algorithms supported by qnetd */ #define QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE 4 extern enum tlv_decision_algorithm_type qnetd_static_supported_decision_algorithms[QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE]; #define QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE #ifdef __cplusplus } #endif #endif /* _QNET_CONFIG_H_ */ diff --git a/qdevices/qnetd-algo-ffsplit.c b/qdevices/qnetd-algo-ffsplit.c index fb8d2d33..40618d20 100644 --- a/qdevices/qnetd-algo-ffsplit.c +++ b/qdevices/qnetd-algo-ffsplit.c @@ -1,298 +1,742 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "qnetd-algo-ffsplit.h" #include "qnetd-log.h" +#include "qnetd-log-debug.h" #include "qnetd-cluster-list.h" #include "qnetd-cluster.h" +#include "qnetd-client-send.h" -struct ffsplit_cluster_data { - uint8_t leader_set; - uint32_t leader_id; +enum qnetd_algo_ffsplit_cluster_state { + QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE, + QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_STABLE_MEMBERSHIP, + QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS, + QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS, +}; + +struct qnetd_algo_ffsplit_cluster_data { + enum qnetd_algo_ffsplit_cluster_state cluster_state; + const struct node_list *quorate_partition_node_list; +}; + +enum qnetd_algo_ffsplit_client_state { + QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE, + QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK, + QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK, +}; + +struct qnetd_algo_ffsplit_client_data { + enum qnetd_algo_ffsplit_client_state client_state; + uint32_t vote_info_expected_seq_num; }; enum tlv_reply_error_code qnetd_algo_ffsplit_client_init(struct qnetd_client *client) { - struct ffsplit_cluster_data *cluster_data; + struct qnetd_algo_ffsplit_cluster_data *cluster_data; + struct qnetd_algo_ffsplit_client_data *client_data; if (qnetd_cluster_size(client->cluster) == 1) { - cluster_data = malloc(sizeof(struct ffsplit_cluster_data)); + cluster_data = malloc(sizeof(*cluster_data)); if (cluster_data == NULL) { qnetd_log(LOG_ERR, "ffsplit: Can't initialize cluster data for client %s", client->addr_str); return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR); } memset(cluster_data, 0, sizeof(*cluster_data)); + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE; + cluster_data->quorate_partition_node_list = NULL; client->cluster->algorithm_data = cluster_data; } + client_data = malloc(sizeof(*client_data)); + if (client_data == NULL) { + qnetd_log(LOG_ERR, "ffsplit: Can't initialize node data for client %s", + client->addr_str); + + return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR); + } + memset(client_data, 0, sizeof(*client_data)); + client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE; + client->algorithm_data = client_data; + return (TLV_REPLY_ERROR_CODE_NO_ERROR); } static int -qnetd_algo_ffsplit_is_prefered_partition(struct qnetd_client *client, +qnetd_algo_ffsplit_is_prefered_partition(const struct qnetd_client *client, const struct node_list *config_node_list, const struct node_list *membership_node_list) { uint32_t prefered_node_id; struct node_list_entry *node_entry; switch (client->tie_breaker.mode) { case TLV_TIE_BREAKER_MODE_LOWEST: node_entry = TAILQ_FIRST(config_node_list); prefered_node_id = node_entry->node_id; TAILQ_FOREACH(node_entry, config_node_list, entries) { if (node_entry->node_id < prefered_node_id) { prefered_node_id = node_entry->node_id; } } break; case TLV_TIE_BREAKER_MODE_HIGHEST: node_entry = TAILQ_FIRST(config_node_list); prefered_node_id = node_entry->node_id; TAILQ_FOREACH(node_entry, config_node_list, entries) { if (node_entry->node_id > prefered_node_id) { prefered_node_id = node_entry->node_id; } } break; case TLV_TIE_BREAKER_MODE_NODE_ID: prefered_node_id = client->tie_breaker.node_id; break; } return (node_list_find_node_id(membership_node_list, prefered_node_id) != NULL); } -static enum tlv_vote -qnetd_algo_ffsplit_do(struct qnetd_client *client, const struct node_list *config_node_list, +static int +qnetd_algo_ffsplit_is_membership_stable(const struct qnetd_client *client, int client_leaving, + const struct tlv_ring_id *ring_id, const struct node_list *config_node_list, const struct node_list *membership_node_list) { - struct ffplist_cluster_data *cluster_data; + const struct qnetd_client *iter_client1, *iter_client2; + const struct node_list *config_node_list1, *config_node_list2; + const struct node_list *membership_node_list1, *membership_node_list2; + const struct node_list_entry *iter_node1, *iter_node2; + const struct node_list_entry *iter_node3, *iter_node4; + const struct tlv_ring_id *ring_id1, *ring_id2; + + /* + * Test if all active clients share same config list. + */ + TAILQ_FOREACH(iter_client1, &client->cluster->client_list, cluster_entries) { + TAILQ_FOREACH(iter_client2, &client->cluster->client_list, cluster_entries) { + if (iter_client1 == iter_client2) { + continue; + } + + if (iter_client1->node_id == client->node_id) { + if (client_leaving) { + continue; + } + + config_node_list1 = config_node_list; + } else { + config_node_list1 = &iter_client1->configuration_node_list; + } + + if (iter_client2->node_id == client->node_id) { + if (client_leaving) { + continue; + } + + config_node_list2 = config_node_list; + } else { + config_node_list2 = &iter_client2->configuration_node_list; + } - cluster_data = (struct ffplist_cluster_data *)client->cluster->algorithm_data; + /* + * Walk thru all node ids in given config node list... + */ + TAILQ_FOREACH(iter_node1, config_node_list1, entries) { + /* + * ... and try to find given node id in other list + */ + iter_node2 = node_list_find_node_id(config_node_list2, iter_node1->node_id); + + if (iter_node2 == NULL) { + /* + * Node with iter_node1->node_id was not found in + * config_node_list2 -> lists doesn't match + */ + return (0); + } + } + } + } + + /* + * Test if same partitions share same ring ids and membership node list + */ + TAILQ_FOREACH(iter_client1, &client->cluster->client_list, cluster_entries) { + if (iter_client1->node_id == client->node_id) { + if (client_leaving) { + continue; + } + + membership_node_list1 = membership_node_list; + ring_id1 = ring_id; + } else { + membership_node_list1 = &iter_client1->last_membership_node_list; + ring_id1 = &iter_client1->last_ring_id; + } + + /* + * Walk thru all memberships nodes + */ + TAILQ_FOREACH(iter_node1, membership_node_list1, entries) { + /* + * try to find client with given node id + */ + iter_client2 = qnetd_cluster_find_client_by_node_id(client->cluster, + iter_node1->node_id); + if (iter_client2 == NULL) { + /* + * Client with given id is not connected + */ + continue; + } + + if (iter_client2->node_id == client->node_id) { + if (client_leaving) { + continue; + } + + membership_node_list2 = membership_node_list; + ring_id2 = ring_id; + } else { + membership_node_list2 = &iter_client2->last_membership_node_list; + ring_id2 = &iter_client2->last_ring_id; + } + + /* + * Compare ring ids + */ + if (!tlv_ring_id_eq(ring_id1, ring_id2)) { + return (0); + } + + /* + * Now compare that membership node list equals, so walk thru all + * members ... + */ + TAILQ_FOREACH(iter_node3, membership_node_list1, entries) { + /* + * ... and try to find given node id in other membership node list + */ + iter_node4 = node_list_find_node_id(membership_node_list2, iter_node3->node_id); + + if (iter_node4 == NULL) { + /* + * Node with iter_node3->node_id was not found in + * membership_node_list2 -> lists doesn't match + */ + return (0); + } + } + } + } + + return (1); +} + +/* + * 0 - Not quarate + * 1 - Quorate but not fulfilling tie breaker or quorate with more than 50% + * 2 - Quorate and fulfilling tie breaker + */ +static unsigned int +qnetd_algo_ffsplit_get_partition_score(const struct qnetd_client *client, + const struct node_list *config_node_list, const struct node_list *membership_node_list) +{ if (node_list_size(config_node_list) % 2 != 0) { /* * Odd clusters never split into 50:50. */ if (node_list_size(membership_node_list) > node_list_size(config_node_list) / 2) { - return (TLV_VOTE_ACK); + return (1); } else { - return (TLV_VOTE_NACK); + return (0); } } else { if (node_list_size(membership_node_list) > node_list_size(config_node_list) / 2) { - return (TLV_VOTE_ACK); + return (1); } else if (node_list_size(membership_node_list) < node_list_size(config_node_list) / 2) { - return (TLV_VOTE_NACK); + return (0); } else { /* * 50:50 split */ if (qnetd_algo_ffsplit_is_prefered_partition(client, config_node_list, membership_node_list)) { - return (TLV_VOTE_ACK); + return (2); } else { - return (TLV_VOTE_NACK); + return (1); + } + } + } + + qnetd_log(LOG_CRIT, "qnetd_algo_ffsplit_get_partition_score unhandled case"); + exit(1); + + return (-1); +} + +static const struct node_list * +qnetd_algo_ffsplit_select_partition(const struct qnetd_client *client, int client_leaving, + const struct node_list *config_node_list, const struct node_list *membership_node_list) +{ + unsigned int highest_score, iter_score; + const struct qnetd_client *iter_client; + const struct node_list *result_node_list; + const struct node_list *iter_config_node_list; + const struct node_list *iter_membership_node_list; + + highest_score = 0; + result_node_list = NULL; + + /* + * Get highest score + */ + TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) { + if (iter_client->node_id == client->node_id) { + if (client_leaving) { + continue; + } + + iter_config_node_list = config_node_list; + iter_membership_node_list = membership_node_list; + } else { + iter_config_node_list = &iter_client->configuration_node_list; + iter_membership_node_list = &iter_client->last_membership_node_list; + } + + if ((iter_score = qnetd_algo_ffsplit_get_partition_score(iter_client, iter_config_node_list, + iter_membership_node_list)) > highest_score) { + highest_score = iter_score; + result_node_list = iter_membership_node_list; + } + } + + return (result_node_list); +} + +/* + * Update state of all nodes to match quorate_partition_node_list + */ +static void +qnetd_algo_ffsplit_update_nodes_state(struct qnetd_client *client, int client_leaving, + const struct node_list *quorate_partition_node_list) +{ + const struct qnetd_client *iter_client; + struct qnetd_algo_ffsplit_client_data *iter_client_data; + + TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) { + iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data; + + if (iter_client->node_id == client->node_id && client_leaving) { + iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE; + + continue; + } + + if (quorate_partition_node_list == NULL || + node_list_find_node_id(quorate_partition_node_list, iter_client->node_id) == NULL) { + iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK; + } else { + iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK; + } + } +} + +/* + * Send vote info. If client_leaving is set, client is ignored. if send_acks + * is set, only ACK votes are send (nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK state), + * otherwise only NACK votes are send (nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK state) + * + * Returns number of send votes + */ +static size_t +qnetd_algo_ffsplit_send_votes(struct qnetd_client *client, int client_leaving, + const struct tlv_ring_id *ring_id, int send_acks) +{ + size_t sent_votes; + struct qnetd_client *iter_client; + struct qnetd_algo_ffsplit_client_data *iter_client_data; + const struct tlv_ring_id *ring_id_to_send; + enum tlv_vote vote_to_send; + + sent_votes = 0; + + TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) { + if (iter_client->node_id == client->node_id) { + if (client_leaving) { + continue; } + + ring_id_to_send = ring_id; + } else { + ring_id_to_send = &iter_client->last_ring_id; + } + + iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data; + vote_to_send = TLV_VOTE_UNDEFINED; + + if (send_acks) { + if (iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK) { + vote_to_send = TLV_VOTE_ACK; + } + } else { + if (iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK) { + vote_to_send = TLV_VOTE_NACK; + } + } + + if (vote_to_send != TLV_VOTE_UNDEFINED) { + iter_client_data->vote_info_expected_seq_num++; + sent_votes++; + + if (qnetd_client_send_vote_info(iter_client, + iter_client_data->vote_info_expected_seq_num, ring_id_to_send, + vote_to_send) == -1) { + client->schedule_disconnect = 1; + } + } + } + + return (sent_votes); +} + +/* + * Return number of clients in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK state if sending_acks is + * set or number of nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK state if sending_acks is + * not set + */ +static size_t +qnetd_algo_ffsplit_no_clients_in_sending_state(struct qnetd_client *client, int sending_acks) +{ + size_t no_clients; + struct qnetd_client *iter_client; + struct qnetd_algo_ffsplit_client_data *iter_client_data; + + no_clients = 0; + + TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) { + iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data; + + if (sending_acks && + iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK) { + no_clients++; + } + + if (!sending_acks && + iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK) { + no_clients++; } } + + return (no_clients); +} + +static enum tlv_vote +qnetd_algo_ffsplit_do(struct qnetd_client *client, int client_leaving, + const struct tlv_ring_id *ring_id, const struct node_list *config_node_list, + const struct node_list *membership_node_list) +{ + struct qnetd_algo_ffsplit_cluster_data *cluster_data; + const struct node_list *quorate_partition_node_list; + + cluster_data = (struct qnetd_algo_ffsplit_cluster_data *)client->cluster->algorithm_data; + + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_STABLE_MEMBERSHIP; + + if (!qnetd_algo_ffsplit_is_membership_stable(client, client_leaving, + ring_id, config_node_list, membership_node_list)) { + /* + * Wait until membership is stable + */ + qnetd_log(LOG_DEBUG, "ffsplit: Membership for cluster %s is not yet stable", client->cluster_name); + + return (TLV_VOTE_WAIT_FOR_REPLY); + } + + qnetd_log(LOG_DEBUG, "ffsplit: Membership for cluster %s is now stable", client->cluster_name); + + quorate_partition_node_list = qnetd_algo_ffsplit_select_partition(client, client_leaving, + config_node_list, membership_node_list); + cluster_data->quorate_partition_node_list = quorate_partition_node_list; + + if (quorate_partition_node_list == NULL) { + qnetd_log(LOG_DEBUG, "ffsplit: No quorate partition was selected"); + } else { + qnetd_log(LOG_DEBUG, "ffsplit: Quorate partition selected"); + qnetd_log_debug_dump_node_list(client, quorate_partition_node_list); + } + + qnetd_algo_ffsplit_update_nodes_state(client, client_leaving, quorate_partition_node_list); + + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS; + + if (qnetd_algo_ffsplit_send_votes(client, client_leaving, ring_id, 0) == 0) { + qnetd_log(LOG_DEBUG, "ffsplit: No client gets NACK"); + /* + * No one gets nack -> send acks + */ + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS; + + if (qnetd_algo_ffsplit_send_votes(client, client_leaving, ring_id, 1) == 0) { + qnetd_log(LOG_DEBUG, "ffsplit: No client gets ACK"); + /* + * No one gets acks -> finished + */ + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE; + } + } + + return (TLV_VOTE_NO_CHANGE); } enum tlv_reply_error_code qnetd_algo_ffsplit_config_node_list_received(struct qnetd_client *client, uint32_t msg_seq_num, int config_version_set, uint64_t config_version, const struct node_list *nodes, int initial, enum tlv_vote *result_vote) { if (node_list_size(nodes) == 0) { /* * Empty node list shouldn't happen */ qnetd_log(LOG_ERR, "ffsplit: Received empty config node list for client %s", client->addr_str); return (TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST); } if (node_list_find_node_id(nodes, client->node_id) == NULL) { /* * Current node is not in node list */ qnetd_log(LOG_ERR, "ffsplit: Received config node list without client %s", client->addr_str); return (TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST); } if (initial || node_list_size(&client->last_membership_node_list) == 0) { /* * Initial node list -> membership is going to be send by client */ *result_vote = TLV_VOTE_ASK_LATER; } else { - *result_vote = qnetd_algo_ffsplit_do(client, nodes, &client->last_membership_node_list); + *result_vote = qnetd_algo_ffsplit_do(client, 0, &client->last_ring_id, + nodes, &client->last_membership_node_list); } return (TLV_REPLY_ERROR_CODE_NO_ERROR); } /* * Called after client sent membership node list. * All client fields are already set. Nodes is actual node list. * msg_seq_num is 32-bit number set by client. If client sent config file version, * config_version_set is set to 1 and config_version contains valid config file version. * ring_id and quorate are copied from client votequorum callback. * * Function has to return result_vote. This can be one of ack/nack, ask_later (client * should ask later for a vote) or wait_for_reply (client should wait for reply). * * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_* * on failure (error is send back to client) */ enum tlv_reply_error_code qnetd_algo_ffsplit_membership_node_list_received(struct qnetd_client *client, uint32_t msg_seq_num, const struct tlv_ring_id *ring_id, const struct node_list *nodes, enum tlv_vote *result_vote) { if (node_list_size(nodes) == 0) { /* * Empty node list shouldn't happen */ qnetd_log(LOG_ERR, "ffsplit: Received empty membership node list for client %s", client->addr_str); return (TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST); } if (node_list_find_node_id(nodes, client->node_id) == NULL) { /* * Current node is not in node list */ qnetd_log(LOG_ERR, "ffsplit: Received membership node list without client %s", client->addr_str); return (TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST); } if (node_list_size(&client->configuration_node_list) == 0) { /* * Config node list not received -> it's going to be sent later */ *result_vote = TLV_VOTE_ASK_LATER; } else { - *result_vote = qnetd_algo_ffsplit_do(client, &client->configuration_node_list, nodes); + *result_vote = qnetd_algo_ffsplit_do(client, 0, ring_id, + &client->configuration_node_list, nodes); } return (TLV_REPLY_ERROR_CODE_NO_ERROR); } enum tlv_reply_error_code qnetd_algo_ffsplit_quorum_node_list_received(struct qnetd_client *client, uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes, enum tlv_vote *result_vote) { /* * Quorum node list is informative -> no change */ *result_vote = TLV_VOTE_NO_CHANGE; return (TLV_REPLY_ERROR_CODE_NO_ERROR); } void qnetd_algo_ffsplit_client_disconnect(struct qnetd_client *client, int server_going_down) { + (void)qnetd_algo_ffsplit_do(client, 1, &client->last_ring_id, + &client->configuration_node_list, &client->last_membership_node_list); + + free(client->algorithm_data); + if (qnetd_cluster_size(client->cluster) == 1) { /* * Last client in the cluster */ free(client->cluster->algorithm_data); } } enum tlv_reply_error_code qnetd_algo_ffsplit_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num, enum tlv_vote *result_vote) { + /* + * Ask for vote is not supported in current algorithm + */ return (TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM_MESSAGE); } enum tlv_reply_error_code qnetd_algo_ffsplit_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num) { + struct qnetd_algo_ffsplit_cluster_data *cluster_data; + struct qnetd_algo_ffsplit_client_data *client_data; - return (TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM_MESSAGE); + cluster_data = (struct qnetd_algo_ffsplit_cluster_data *)client->cluster->algorithm_data; + client_data = (struct qnetd_algo_ffsplit_client_data *)client->algorithm_data; + + if (client_data->vote_info_expected_seq_num != msg_seq_num) { + qnetd_log(LOG_DEBUG, "ffsplit: Received old vote info reply from client %s", + client->addr_str); + + return (TLV_REPLY_ERROR_CODE_NO_ERROR); + } + + client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE; + + if (cluster_data->cluster_state != QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS && + cluster_data->cluster_state != QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS) { + return (TLV_REPLY_ERROR_CODE_NO_ERROR); + } + + if (cluster_data->cluster_state == QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS) { + if (qnetd_algo_ffsplit_no_clients_in_sending_state(client, 0) == 0) { + qnetd_log(LOG_DEBUG, "ffsplit: All NACK votes sent for cluster %s", + client->cluster_name); + + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS; + + if (qnetd_algo_ffsplit_send_votes(client, 0, &client->last_ring_id, 1) == 0) { + qnetd_log(LOG_DEBUG, "ffsplit: No client gets ACK"); + /* + * No one gets acks -> finished + */ + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE; + } + } + } else { + if (qnetd_algo_ffsplit_no_clients_in_sending_state(client, 1) == 0) { + qnetd_log(LOG_DEBUG, "ffsplit: All ACK votes sent for cluster %s", + client->cluster_name); + + cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE; + } + } + + return (TLV_REPLY_ERROR_CODE_NO_ERROR); } enum tlv_reply_error_code qnetd_algo_ffsplit_timer_callback(struct qnetd_client *client, int *reschedule_timer, int *send_vote, enum tlv_vote *result_vote) { return (TLV_REPLY_ERROR_CODE_NO_ERROR); } static struct qnetd_algorithm qnetd_algo_ffsplit = { .init = qnetd_algo_ffsplit_client_init, .config_node_list_received = qnetd_algo_ffsplit_config_node_list_received, .membership_node_list_received = qnetd_algo_ffsplit_membership_node_list_received, .quorum_node_list_received = qnetd_algo_ffsplit_quorum_node_list_received, .client_disconnect = qnetd_algo_ffsplit_client_disconnect, .ask_for_vote_received = qnetd_algo_ffsplit_ask_for_vote_received, .vote_info_reply_received = qnetd_algo_ffsplit_vote_info_reply_received, .timer_callback = qnetd_algo_ffsplit_timer_callback, }; enum tlv_reply_error_code qnetd_algo_ffsplit_register() { return (qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_FFSPLIT, &qnetd_algo_ffsplit)); } diff --git a/qdevices/qnetd-client-algo-timer.c b/qdevices/qnetd-client-algo-timer.c index 05ecd53d..35336f02 100644 --- a/qdevices/qnetd-client-algo-timer.c +++ b/qdevices/qnetd-client-algo-timer.c @@ -1,145 +1,146 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "qnetd-log.h" #include "qnetd-client-algo-timer.h" #include "qnetd-client-send.h" #include "qnetd-algorithm.h" #include "timer-list.h" static int qnetd_client_algo_timer_callback(void *data1, void *data2) { struct qnetd_client *client; enum tlv_vote result_vote; int send_vote; int reschedule_timer; enum tlv_reply_error_code reply_error_code; client = (struct qnetd_client *)data1; result_vote = TLV_VOTE_WAIT_FOR_REPLY; send_vote = 0; reschedule_timer = 0; reply_error_code = qnetd_algorithm_timer_callback(client, &reschedule_timer, &send_vote, &result_vote); if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) { qnetd_log(LOG_ERR, "Algorithm for client %s returned error code. " "Sending error reply.", client->addr_str); if (qnetd_client_send_err(client, 0, 0, reply_error_code) != 0) { client->schedule_disconnect = 1; return (0); } return (0); } else { qnetd_log(LOG_DEBUG, "Algorithm for client %s decided to %s timer and %s vote " "with value %s", client->addr_str, (reschedule_timer ? "reschedule" : "not reschedule"), (send_vote ? "send" : "not send"), tlv_vote_to_str(result_vote)); } if (send_vote) { client->algo_timer_vote_info_msq_seq_number++; if (qnetd_client_send_vote_info(client, - client->algo_timer_vote_info_msq_seq_number, result_vote) != 0) { + client->algo_timer_vote_info_msq_seq_number, &client->last_ring_id, + result_vote) != 0) { client->schedule_disconnect = 1; return (0); } } if (reschedule_timer) { /* * Timer list makes sure to schedule callback again */ return (-1); } client->algo_timer = NULL; return (0); } int qnetd_client_algo_timer_is_scheduled(struct qnetd_client *client) { return (client->algo_timer != NULL); } int qnetd_client_algo_timer_schedule_timeout(struct qnetd_client *client, uint32_t timeout) { if (qnetd_client_algo_timer_is_scheduled(client)) { if (qnetd_client_algo_timer_abort(client) != 0) { qnetd_log(LOG_ERR, "Can't abort algo timer"); return (-1); } } client->algo_timer = timer_list_add(client->main_timer_list, timeout, qnetd_client_algo_timer_callback, (void *)client, NULL); if (client->algo_timer == NULL) { qnetd_log(LOG_ERR, "Can't schedule algo timer"); return (-1); } return (0); } int qnetd_client_algo_timer_schedule(struct qnetd_client *client) { return (qnetd_client_algo_timer_schedule_timeout(client, client->heartbeat_interval / 4)); } int qnetd_client_algo_timer_abort(struct qnetd_client *client) { if (qnetd_client_algo_timer_is_scheduled(client)) { timer_list_delete(client->main_timer_list, client->algo_timer); client->algo_timer = NULL; } return (0); } diff --git a/qdevices/qnetd-client-msg-received.c b/qdevices/qnetd-client-msg-received.c index ba8e3586..7207e332 100644 --- a/qdevices/qnetd-client-msg-received.c +++ b/qdevices/qnetd-client-msg-received.c @@ -1,1150 +1,1143 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "qnetd-algorithm.h" #include "qnetd-instance.h" #include "qnetd-log.h" #include "qnetd-log-debug.h" #include "qnetd-client-send.h" #include "msg.h" #include "nss-sock.h" #include "qnetd-client-msg-received.h" /* * 0 - Success * -1 - Disconnect client * -2 - Error reply sent, but no need to disconnect client */ static int qnetd_client_msg_received_check_tls(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int check_certificate; int tls_required; CERTCertificate *peer_cert; int case_processed; check_certificate = 0; tls_required = 0; case_processed = 0; switch (instance->tls_supported) { case TLV_TLS_UNSUPPORTED: case_processed = 1; tls_required = 0; check_certificate = 0; break; case TLV_TLS_SUPPORTED: case_processed = 1; tls_required = 0; if (client->tls_started && instance->tls_client_cert_required && !client->tls_peer_certificate_verified) { check_certificate = 1; } break; case TLV_TLS_REQUIRED: case_processed = 1; tls_required = 1; if (instance->tls_client_cert_required && !client->tls_peer_certificate_verified) { check_certificate = 1; } break; /* * Default is not defined intentionally. Compiler shows warning when new * tls supported is added */ } if (!case_processed) { qnetd_log(LOG_ERR, "Unhandled instance tls supported %u", instance->tls_supported); exit(1); } if (tls_required && !client->tls_started) { qnetd_log(LOG_ERR, "TLS is required but doesn't started yet. " "Sending back error message"); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_TLS_REQUIRED) != 0) { return (-1); } return (-2); } if (check_certificate) { peer_cert = SSL_PeerCertificate(client->socket); if (peer_cert == NULL) { qnetd_log(LOG_ERR, "Client doesn't sent valid certificate. " "Disconnecting client"); return (-1); } if (CERT_VerifyCertName(peer_cert, client->cluster_name) != SECSuccess) { qnetd_log(LOG_ERR, "Client doesn't sent certificate with valid CN. " "Disconnecting client"); CERT_DestroyCertificate(peer_cert); return (-1); } CERT_DestroyCertificate(peer_cert); client->tls_peer_certificate_verified = 1; } return (0); } static int qnetd_client_msg_received_preinit(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { struct send_buffer_list_entry *send_buffer; if (msg->cluster_name == NULL) { qnetd_log(LOG_ERR, "Received preinit message without cluster name. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } client->cluster_name = malloc(msg->cluster_name_len + 1); if (client->cluster_name == NULL) { qnetd_log(LOG_ERR, "Can't allocate cluster name. Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INTERNAL_ERROR) != 0) { return (-1); } return (0); } memset(client->cluster_name, 0, msg->cluster_name_len + 1); memcpy(client->cluster_name, msg->cluster_name, msg->cluster_name_len); client->cluster_name_len = msg->cluster_name_len; client->preinit_received = 1; send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc preinit reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_preinit_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number, instance->tls_supported, instance->tls_client_cert_required) == 0) { qnetd_log(LOG_ERR, "Can't alloc preinit reply msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); }; send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_unexpected_msg(struct qnetd_client *client, const struct msg_decoded *msg, const char *msg_str) { qnetd_log(LOG_ERR, "Received %s message. Sending back error message", msg_str); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE) != 0) { return (-1); } return (0); } static int qnetd_client_msg_received_preinit_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "preinit reply")); } static int qnetd_client_msg_received_starttls(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { PRFileDesc *new_pr_fd; if (!client->preinit_received) { qnetd_log(LOG_ERR, "Received starttls before preinit message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED) != 0) { return (-1); } return (0); } if ((new_pr_fd = nss_sock_start_ssl_as_server(client->socket, instance->server.cert, instance->server.private_key, instance->tls_client_cert_required, 0, NULL)) == NULL) { qnetd_log_nss(LOG_ERR, "Can't start TLS. Disconnecting client."); return (-1); } client->tls_started = 1; client->tls_peer_certificate_verified = 0; client->socket = new_pr_fd; return (0); } static int qnetd_client_msg_received_server_error(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "server error")); } /* * Checks if new client send information are valid. It means: * - in cluster is no duplicate node with same nodeid * - it has same tie_breaker as other nodes in cluster * - it has same algorithm as other nodes in cluster */ static enum tlv_reply_error_code qnetd_client_msg_received_init_check_new_client(struct qnetd_instance *instance, struct qnetd_client *new_client) { struct qnetd_cluster *cluster; struct qnetd_client *client; cluster = qnetd_cluster_list_find_by_name(&instance->clusters, new_client->cluster_name, new_client->cluster_name_len); if (cluster == NULL) { return (TLV_REPLY_ERROR_CODE_NO_ERROR); } TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) { if (!tlv_tie_breaker_eq(&new_client->tie_breaker, &client->tie_breaker)) { qnetd_log(LOG_ERR, "Received init message contains tie-breaker which " "differs from rest of cluster. Sending error reply"); return (TLV_REPLY_ERROR_CODE_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES); } if (new_client->decision_algorithm != client->decision_algorithm) { qnetd_log(LOG_ERR, "Received init message contains algorithm which " "differs from rest of cluster. Sending error reply"); return (TLV_REPLY_ERROR_CODE_ALGORITHM_DIFFERS_FROM_OTHER_NODES); } if (new_client->node_id == client->node_id) { qnetd_log(LOG_ERR, "Received init message contains node id which is " "duplicate of other node in cluster. Sending error reply"); return (TLV_REPLY_ERROR_CODE_DUPLICATE_NODE_ID); } } return (TLV_REPLY_ERROR_CODE_NO_ERROR); } static int qnetd_client_msg_received_init(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int res; size_t zi; enum msg_type *supported_msgs; size_t no_supported_msgs; enum tlv_opt_type *supported_opts; size_t no_supported_opts; struct send_buffer_list_entry *send_buffer; enum tlv_reply_error_code reply_error_code; struct qnetd_cluster *cluster; supported_msgs = NULL; supported_opts = NULL; no_supported_msgs = 0; no_supported_opts = 0; reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->preinit_received) { qnetd_log(LOG_ERR, "Received init before preinit message. Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED; } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->node_id_set) { qnetd_log(LOG_ERR, "Received init message without node id set. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION; } else { client->node_id_set = 1; client->node_id = msg->node_id; } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->ring_id_set) { qnetd_log(LOG_ERR, "Received init message without ring id set. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION; } else { memcpy(&client->last_ring_id, &msg->ring_id, sizeof(struct tlv_ring_id)); } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->heartbeat_interval_set) { qnetd_log(LOG_ERR, "Received init message without heartbeat interval set. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION; } else { if (msg->heartbeat_interval < instance->advanced_settings->heartbeat_interval_min || msg->heartbeat_interval > instance->advanced_settings->heartbeat_interval_max) { qnetd_log(LOG_ERR, "Client requested invalid heartbeat interval %u. " "Sending error reply.", msg->heartbeat_interval); reply_error_code = TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL; } else { client->heartbeat_interval = msg->heartbeat_interval; } } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->tie_breaker_set) { qnetd_log(LOG_ERR, "Received init message without tie-breaker set. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION; } else { memcpy(&client->tie_breaker, &msg->tie_breaker, sizeof(msg->tie_breaker)); } if (msg->supported_messages != NULL) { /* * Client sent supported messages. For now this is ignored but in the future * this may be used to ensure backward compatibility. */ /* for (i = 0; i < msg->no_supported_messages; i++) { qnetd_log(LOG_DEBUG, "Client supports %u message", (int)msg->supported_messages[i]); } */ /* * Sent back supported messages */ msg_get_supported_messages(&supported_msgs, &no_supported_msgs); } if (msg->supported_options != NULL) { /* * Client sent supported options. For now this is ignored but in the future * this may be used to ensure backward compatibility. */ /* for (i = 0; i < msg->no_supported_options; i++) { qnetd_log(LOG_DEBUG, "Client supports %u option", (int)msg->supported_messages[i]); } */ /* * Send back supported options */ tlv_get_supported_options(&supported_opts, &no_supported_opts); } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->decision_algorithm_set) { qnetd_log(LOG_ERR, "Received init message without decision algorithm. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION; } else { /* * Check if decision algorithm requested by client is supported */ res = 0; for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE && !res; zi++) { if (qnetd_static_supported_decision_algorithms[zi] == msg->decision_algorithm) { res = 1; } } if (!res) { qnetd_log(LOG_ERR, "Client requested unsupported decision algorithm %u. " "Sending error reply.", msg->decision_algorithm); reply_error_code = TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM; } client->decision_algorithm = msg->decision_algorithm; } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) { reply_error_code = qnetd_client_msg_received_init_check_new_client(instance, client); } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) { cluster = qnetd_cluster_list_add_client(&instance->clusters, client); if (cluster == NULL) { qnetd_log(LOG_ERR, "Can't add client to cluster list. " "Sending error reply."); reply_error_code = TLV_REPLY_ERROR_CODE_INTERNAL_ERROR; } else { client->cluster = cluster; client->cluster_list = &instance->clusters; } } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) { qnetd_log_debug_new_client_connected(client); reply_error_code = qnetd_algorithm_client_init(client); } if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) { /* * Correct init received */ client->init_received = 1; } else { qnetd_log(LOG_ERR, "Algorithm returned error code. Sending error reply."); } send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc init reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_init_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number, reply_error_code, supported_msgs, no_supported_msgs, supported_opts, no_supported_opts, instance->advanced_settings->max_client_receive_size, instance->advanced_settings->max_client_send_size, qnetd_static_supported_decision_algorithms, QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) == -1) { qnetd_log(LOG_ERR, "Can't alloc init reply msg. Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); } send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_init_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "init reply")); } static int qnetd_client_msg_received_set_option_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "set option reply")); } static int qnetd_client_msg_received_set_option(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int res; struct send_buffer_list_entry *send_buffer; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->init_received) { qnetd_log(LOG_ERR, "Received set option message before init message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) { return (-1); } return (0); } if (msg->heartbeat_interval_set) { /* * Check if heartbeat interval is valid */ if (msg->heartbeat_interval < instance->advanced_settings->heartbeat_interval_min || msg->heartbeat_interval > instance->advanced_settings->heartbeat_interval_max) { qnetd_log(LOG_ERR, "Client requested invalid heartbeat interval %u. " "Sending error reply.", msg->heartbeat_interval); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL) != 0) { return (-1); } return (0); } client->heartbeat_interval = msg->heartbeat_interval; } send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc set option reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_set_option_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number, client->heartbeat_interval) == -1) { qnetd_log(LOG_ERR, "Can't alloc set option reply msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); } send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_echo_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "echo reply")); } static int qnetd_client_msg_received_echo_request(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg, const struct dynar *msg_orig) { int res; struct send_buffer_list_entry *send_buffer; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->init_received) { qnetd_log(LOG_ERR, "Received echo request before init message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) { return (-1); } return (0); } send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc echo reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_echo_reply(&send_buffer->buffer, msg_orig) == -1) { qnetd_log(LOG_ERR, "Can't alloc echo reply msg. Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); } send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_node_list(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int res; struct send_buffer_list_entry *send_buffer; enum tlv_reply_error_code reply_error_code; enum tlv_vote result_vote; int case_processed; reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->init_received) { qnetd_log(LOG_ERR, "Received node list message before init message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) { return (-1); } return (0); } if (!msg->node_list_type_set) { qnetd_log(LOG_ERR, "Received node list message without node list type set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } if (!msg->seq_number_set) { qnetd_log(LOG_ERR, "Received node list message without seq number set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } result_vote = TLV_VOTE_NO_CHANGE; case_processed = 0; switch (msg->node_list_type) { case TLV_NODE_LIST_TYPE_INITIAL_CONFIG: case TLV_NODE_LIST_TYPE_CHANGED_CONFIG: case_processed = 1; qnetd_log_debug_config_node_list_received(client, msg->seq_number, msg->config_version_set, msg->config_version, &msg->nodes, (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG)); reply_error_code = qnetd_algorithm_config_node_list_received(client, msg->seq_number, msg->config_version_set, msg->config_version, &msg->nodes, (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG), &result_vote); break; case TLV_NODE_LIST_TYPE_MEMBERSHIP: case_processed = 1; if (!msg->ring_id_set) { qnetd_log(LOG_ERR, "Received node list message without ring id number set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } qnetd_log_debug_membership_node_list_received(client, msg->seq_number, &msg->ring_id, &msg->nodes); reply_error_code = qnetd_algorithm_membership_node_list_received(client, msg->seq_number, &msg->ring_id, &msg->nodes, &result_vote); break; case TLV_NODE_LIST_TYPE_QUORUM: case_processed = 1; if (!msg->quorate_set) { qnetd_log(LOG_ERR, "Received quorum list message without quorate set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } qnetd_log_debug_quorum_node_list_received(client, msg->seq_number,msg->quorate, &msg->nodes); reply_error_code = qnetd_algorithm_quorum_node_list_received(client, msg->seq_number,msg->quorate, &msg->nodes, &result_vote); break; /* * Default is not defined intentionally. Compiler shows warning when new * node list type is added */ } if (!case_processed) { qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. " "Unhandled node_list_type"); exit(1); } if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) { qnetd_log(LOG_ERR, "Algorithm returned error code. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, reply_error_code) != 0) { return (-1); } return (0); } else { qnetd_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote)); } - if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP && - result_vote == TLV_VOTE_NO_CHANGE) { - qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. " - "node_list_type is membership and algorithm result vote is no_change"); - exit(1); - } - /* * Store node list for future use */ case_processed = 0; switch (msg->node_list_type) { case TLV_NODE_LIST_TYPE_INITIAL_CONFIG: case TLV_NODE_LIST_TYPE_CHANGED_CONFIG: case_processed = 1; node_list_free(&client->configuration_node_list); if (node_list_clone(&client->configuration_node_list, &msg->nodes) == -1) { qnetd_log(LOG_ERR, "Can't alloc config node list clone. " "Disconnecting client connection."); return (-1); } client->config_version_set = msg->config_version_set; client->config_version = msg->config_version; break; case TLV_NODE_LIST_TYPE_MEMBERSHIP: case_processed = 1; node_list_free(&client->last_membership_node_list); if (node_list_clone(&client->last_membership_node_list, &msg->nodes) == -1) { qnetd_log(LOG_ERR, "Can't alloc membership node list clone. " "Disconnecting client connection."); return (-1); } memcpy(&client->last_ring_id, &msg->ring_id, sizeof(struct tlv_ring_id)); break; case TLV_NODE_LIST_TYPE_QUORUM: case_processed = 1; node_list_free(&client->last_quorum_node_list); if (node_list_clone(&client->last_quorum_node_list, &msg->nodes) == -1) { qnetd_log(LOG_ERR, "Can't alloc quorum node list clone. " "Disconnecting client connection."); return (-1); } break; /* * Default is not defined intentionally. Compiler shows warning when new * node list type is added */ } if (!case_processed) { qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. " "Unhandled node_list_type"); exit(1); } /* * Store result vote */ client->last_sent_vote = result_vote; if (result_vote == TLV_VOTE_ACK || result_vote == TLV_VOTE_NACK) { client->last_sent_ack_nack_vote = result_vote; } send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc node list reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_node_list_reply(&send_buffer->buffer, msg->seq_number, msg->node_list_type, &client->last_ring_id, result_vote) == -1) { qnetd_log(LOG_ERR, "Can't alloc node list reply msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); } send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_node_list_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "node list reply")); } static int qnetd_client_msg_received_ask_for_vote(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int res; struct send_buffer_list_entry *send_buffer; enum tlv_reply_error_code reply_error_code; enum tlv_vote result_vote; reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->init_received) { qnetd_log(LOG_ERR, "Received ask for vote message before init message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) { return (-1); } return (0); } if (!msg->seq_number_set) { qnetd_log(LOG_ERR, "Received ask for vote message without seq number set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } qnetd_log_debug_ask_for_vote_received(client, msg->seq_number); reply_error_code = qnetd_algorithm_ask_for_vote_received(client, msg->seq_number, &result_vote); if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) { qnetd_log(LOG_ERR, "Algorithm returned error code. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, reply_error_code) != 0) { return (-1); } return (0); } else { qnetd_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote)); } /* * Store result vote */ client->last_sent_vote = result_vote; if (result_vote == TLV_VOTE_ACK || result_vote == TLV_VOTE_NACK) { client->last_sent_ack_nack_vote = result_vote; } send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_ask_for_vote_reply(&send_buffer->buffer, msg->seq_number, &client->last_ring_id, result_vote) == -1) { qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); } send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } static int qnetd_client_msg_received_ask_for_vote_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "ask for vote reply")); } static int qnetd_client_msg_received_vote_info(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { return (qnetd_client_msg_received_unexpected_msg(client, msg, "vote info")); } static int qnetd_client_msg_received_vote_info_reply(struct qnetd_instance *instance, struct qnetd_client *client, const struct msg_decoded *msg) { int res; enum tlv_reply_error_code reply_error_code; reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR; if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) { return (res == -1 ? -1 : 0); } if (!client->init_received) { qnetd_log(LOG_ERR, "Received vote info reply before init message. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) { return (-1); } return (0); } if (!msg->seq_number_set) { qnetd_log(LOG_ERR, "Received vote info reply message without seq number set. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) { return (-1); } return (0); } qnetd_log_debug_vote_info_reply_received(client, msg->seq_number); reply_error_code = qnetd_algorithm_vote_info_reply_received(client, msg->seq_number); if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) { qnetd_log(LOG_ERR, "Algorithm returned error code. " "Sending error reply."); if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number, reply_error_code) != 0) { return (-1); } return (0); } return (0); } int qnetd_client_msg_received(struct qnetd_instance *instance, struct qnetd_client *client) { struct msg_decoded msg; int res; int ret_val; int msg_processed; client->dpd_msg_received_since_last_check = 1; msg_decoded_init(&msg); res = msg_decode(&client->receive_buffer, &msg); if (res != 0) { /* * Error occurred. Send server error. */ qnetd_log_msg_decode_error(res); qnetd_log(LOG_INFO, "Sending back error message"); if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number, TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG) != 0) { return (-1); } return (0); } ret_val = 0; msg_processed = 0; switch (msg.type) { case MSG_TYPE_PREINIT: msg_processed = 1; ret_val = qnetd_client_msg_received_preinit(instance, client, &msg); break; case MSG_TYPE_PREINIT_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_preinit_reply(instance, client, &msg); break; case MSG_TYPE_STARTTLS: msg_processed = 1; ret_val = qnetd_client_msg_received_starttls(instance, client, &msg); break; case MSG_TYPE_INIT: msg_processed = 1; ret_val = qnetd_client_msg_received_init(instance, client, &msg); break; case MSG_TYPE_INIT_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_init_reply(instance, client, &msg); break; case MSG_TYPE_SERVER_ERROR: msg_processed = 1; ret_val = qnetd_client_msg_received_server_error(instance, client, &msg); break; case MSG_TYPE_SET_OPTION: msg_processed = 1; ret_val = qnetd_client_msg_received_set_option(instance, client, &msg); break; case MSG_TYPE_SET_OPTION_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_set_option_reply(instance, client, &msg); break; case MSG_TYPE_ECHO_REQUEST: msg_processed = 1; ret_val = qnetd_client_msg_received_echo_request(instance, client, &msg, &client->receive_buffer); break; case MSG_TYPE_ECHO_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_echo_reply(instance, client, &msg); break; case MSG_TYPE_NODE_LIST: msg_processed = 1; ret_val = qnetd_client_msg_received_node_list(instance, client, &msg); break; case MSG_TYPE_NODE_LIST_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_node_list_reply(instance, client, &msg); break; case MSG_TYPE_ASK_FOR_VOTE: msg_processed = 1; ret_val = qnetd_client_msg_received_ask_for_vote(instance, client, &msg); break; case MSG_TYPE_ASK_FOR_VOTE_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_ask_for_vote_reply(instance, client, &msg); break; case MSG_TYPE_VOTE_INFO: msg_processed = 1; ret_val = qnetd_client_msg_received_vote_info(instance, client, &msg); break; case MSG_TYPE_VOTE_INFO_REPLY: msg_processed = 1; ret_val = qnetd_client_msg_received_vote_info_reply(instance, client, &msg); break; /* * Default is not defined intentionally. Compiler shows warning when new * msg type is added. */ } if (!msg_processed) { qnetd_log(LOG_ERR, "Unsupported message %u received from client. " "Sending back error message", msg.type); if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number, TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE) != 0) { ret_val = -1; } } msg_decoded_destroy(&msg); return (ret_val); } diff --git a/qdevices/qnetd-client-send.c b/qdevices/qnetd-client-send.c index 4c48717e..072e4674 100644 --- a/qdevices/qnetd-client-send.c +++ b/qdevices/qnetd-client-send.c @@ -1,108 +1,107 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "qnetd-client-send.h" #include "qnetd-log.h" #include "qnetd-log-debug.h" #include "msg.h" int qnetd_client_send_err(struct qnetd_client *client, int add_msg_seq_number, uint32_t msg_seq_number, enum tlv_reply_error_code reply) { struct send_buffer_list_entry *send_buffer; send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc server error msg from list. " "Disconnecting client connection."); return (-1); } if (msg_create_server_error(&send_buffer->buffer, add_msg_seq_number, msg_seq_number, reply) == 0) { qnetd_log(LOG_ERR, "Can't alloc server error msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); }; send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } int qnetd_client_send_vote_info(struct qnetd_client *client, uint32_t msg_seq_number, - enum tlv_vote vote) + const struct tlv_ring_id *ring_id, enum tlv_vote vote) { struct send_buffer_list_entry *send_buffer; /* * Store result vote */ client->last_sent_vote = vote; if (vote == TLV_VOTE_ACK || vote == TLV_VOTE_NACK) { client->last_sent_ack_nack_vote = vote; } qnetd_log_debug_send_vote_info(client, msg_seq_number, vote); send_buffer = send_buffer_list_get_new(&client->send_buffer_list); if (send_buffer == NULL) { qnetd_log(LOG_ERR, "Can't alloc vote info msg from list. " "Disconnecting client connection."); return (-1); } - if (msg_create_vote_info(&send_buffer->buffer, msg_seq_number, - &client->last_ring_id, vote) == 0) { + if (msg_create_vote_info(&send_buffer->buffer, msg_seq_number, ring_id, vote) == 0) { qnetd_log(LOG_ERR, "Can't alloc vote info msg. " "Disconnecting client connection."); send_buffer_list_discard_new(&client->send_buffer_list, send_buffer); return (-1); }; send_buffer_list_put(&client->send_buffer_list, send_buffer); return (0); } diff --git a/qdevices/qnetd-client-send.h b/qdevices/qnetd-client-send.h index 31f32993..229f8a02 100644 --- a/qdevices/qnetd-client-send.h +++ b/qdevices/qnetd-client-send.h @@ -1,56 +1,56 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _QNETD_CLIENT_SEND_H_ #define _QNETD_CLIENT_SEND_H_ #include #include "qnetd-client.h" #ifdef __cplusplus extern "C" { #endif extern int qnetd_client_send_err(struct qnetd_client *client, int add_msg_seq_number, uint32_t msg_seq_number, enum tlv_reply_error_code reply); extern int qnetd_client_send_vote_info(struct qnetd_client *client, - uint32_t msg_seq_number, enum tlv_vote vote); + uint32_t msg_seq_number, const struct tlv_ring_id *ring_id, enum tlv_vote vote); #ifdef __cplusplus } #endif #endif /* _QNETD_CLIENT_SEND_H_ */ diff --git a/qdevices/qnetd-cluster.c b/qdevices/qnetd-cluster.c index 49d3b076..32c2d3b3 100644 --- a/qdevices/qnetd-cluster.c +++ b/qdevices/qnetd-cluster.c @@ -1,81 +1,95 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "qnetd-cluster.h" int qnetd_cluster_init(struct qnetd_cluster *cluster, const char *cluster_name, size_t cluster_name_len) { memset(cluster, 0, sizeof(*cluster)); cluster->cluster_name = malloc(cluster_name_len + 1); if (cluster->cluster_name == NULL) { return (-1); } memset(cluster->cluster_name, 0, cluster_name_len + 1); memcpy(cluster->cluster_name, cluster_name, cluster_name_len); cluster->cluster_name_len = cluster_name_len; TAILQ_INIT(&cluster->client_list); return (0); } void qnetd_cluster_destroy(struct qnetd_cluster *cluster) { free(cluster->cluster_name); cluster->cluster_name = NULL; } size_t qnetd_cluster_size(const struct qnetd_cluster *cluster) { size_t res; struct qnetd_client *client; res = 0; TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) { res++; } return (res); } + +struct qnetd_client * +qnetd_cluster_find_client_by_node_id(const struct qnetd_cluster *cluster, uint32_t node_id) +{ + struct qnetd_client *client; + + TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) { + if (client->node_id == node_id) { + return (client); + } + } + + return (NULL); +} diff --git a/qdevices/qnetd-cluster.h b/qdevices/qnetd-cluster.h index 128e675d..98d66149 100644 --- a/qdevices/qnetd-cluster.h +++ b/qdevices/qnetd-cluster.h @@ -1,69 +1,72 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _QNETD_CLUSTER_H_ #define _QNETD_CLUSTER_H_ #include #include #include #include "tlv.h" #include "qnetd-client-list.h" #ifdef __cplusplus extern "C" { #endif struct qnetd_cluster { char *cluster_name; size_t cluster_name_len; void *algorithm_data; struct qnetd_client_list client_list; TAILQ_ENTRY(qnetd_cluster) entries; }; -extern int qnetd_cluster_init(struct qnetd_cluster *cluster, +extern int qnetd_cluster_init(struct qnetd_cluster *cluster, const char *cluster_name, size_t cluster_name_len); -extern void qnetd_cluster_destroy(struct qnetd_cluster *cluster); +extern void qnetd_cluster_destroy(struct qnetd_cluster *cluster); -extern size_t qnetd_cluster_size(const struct qnetd_cluster *cluster); +extern size_t qnetd_cluster_size(const struct qnetd_cluster *cluster); + +extern struct qnetd_client *qnetd_cluster_find_client_by_node_id( + const struct qnetd_cluster *cluster, uint32_t node_id); #ifdef __cplusplus } #endif #endif /* _QNETD_CLUSTER_H_ */ diff --git a/qdevices/qnetd-ipc-cmd.c b/qdevices/qnetd-ipc-cmd.c index 630f9a25..d8ed5a28 100644 --- a/qdevices/qnetd-ipc-cmd.c +++ b/qdevices/qnetd-ipc-cmd.c @@ -1,288 +1,288 @@ /* * Copyright (c) 2015-2016 Red Hat, Inc. * * All rights reserved. * * Author: Jan Friesse (jfriesse@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the Red Hat, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "dynar-str.h" #include "qnetd-ipc-cmd.h" #include "qnetd-log.h" #include "utils.h" int qnetd_ipc_cmd_status(struct qnetd_instance *instance, struct dynar *outbuf, int verbose) { if (dynar_str_catf(outbuf, "QNetd address:\t\t\t%s:%"PRIu16"\n", (instance->host_addr != NULL ? instance->host_addr : "*"), instance->host_port) == -1) { return (-1); } if (dynar_str_catf(outbuf, "TLS:\t\t\t\t%s%s\n", tlv_tls_supported_to_str(instance->tls_supported), ((instance->tls_supported != TLV_TLS_UNSUPPORTED && instance->tls_client_cert_required) ? " (client certificate required)" : "")) == -1) { return (-1); } if (dynar_str_catf(outbuf, "Connected clients:\t\t%zu\n", qnetd_client_list_no_clients(&instance->clients)) == -1) { return (-1); } if (dynar_str_catf(outbuf, "Connected clusters:\t\t%zu\n", qnetd_cluster_list_size(&instance->clusters)) == -1) { return (-1); } if (!verbose) { return (0); } if (instance->max_clients != 0) { if (dynar_str_catf(outbuf, "Maximum allowed clients:\t%zu\n", instance->max_clients) == -1) { return (-1); } } if (dynar_str_catf(outbuf, "Maximum send/receive size:\t%zu/%zu bytes\n", instance->advanced_settings->max_client_send_size, instance->advanced_settings->max_client_receive_size) == -1) { return (-1); } return (0); } static int qnetd_ipc_cmd_add_tie_breaker(const struct qnetd_client *client, struct dynar *outbuf) { if (dynar_str_catf(outbuf, " Tie-breaker:\t") == -1) { return (0); } switch (client->tie_breaker.mode) { case TLV_TIE_BREAKER_MODE_LOWEST: if (dynar_str_catf(outbuf, "Node with lowest node ID") == -1) { return (0); } break; case TLV_TIE_BREAKER_MODE_HIGHEST: if (dynar_str_catf(outbuf, "Node with highest node ID") == -1) { return (0); } break; case TLV_TIE_BREAKER_MODE_NODE_ID: if (dynar_str_catf(outbuf, "Node with node ID "UTILS_PRI_NODE_ID, client->tie_breaker.node_id) == -1) { return (0); } break; } return (dynar_str_catf(outbuf, "\n") != -1); } static int qnetd_ipc_cmd_list_add_node_list(struct dynar *outbuf, int verbose, const struct node_list *nlist) { struct node_list_entry *node_info; int i; i = 0; TAILQ_FOREACH(node_info, nlist, entries) { if (i != 0) { if (dynar_str_catf(outbuf, ", ") == -1) { return (-1); } } if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, node_info->node_id) == -1) { return (-1); } if (node_info->data_center_id != 0) { if (dynar_str_catf(outbuf, " (" UTILS_PRI_DATACENTER_ID ")", node_info->data_center_id) == -1) { return (-1); } } i++; } return (0); } static int qnetd_ipc_cmd_list_add_client_info(const struct qnetd_client *client, struct dynar *outbuf, int verbose, size_t client_no) { if (dynar_str_catf(outbuf, " Node ID "UTILS_PRI_NODE_ID":\n", client->node_id) == -1) { return (-1); } if (dynar_str_catf(outbuf, " Client address:\t\t%s\n", client->addr_str) == -1) { return (-1); } if (verbose) { if (dynar_str_catf(outbuf, " HB interval:\t\t%"PRIu32"ms\n", client->heartbeat_interval) == -1) { return (-1); } } if (client->config_version_set) { if (dynar_str_catf(outbuf, " Configuration version:\t" UTILS_PRI_CONFIG_VERSION"\n", client->config_version) == -1) { return (-1); } } if (!node_list_is_empty(&client->configuration_node_list)) { if ((dynar_str_catf(outbuf, " Configured node list:\t") == -1) || (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose, &client->configuration_node_list) == -1) || (dynar_str_catf(outbuf, "\n") == -1)) { return (-1); } } if (verbose) { if (dynar_str_catf(outbuf, " Ring ID:\t\t"UTILS_PRI_RING_ID"\n", client->last_ring_id.node_id, client->last_ring_id.seq) == -1) { return (-1); } } if (!node_list_is_empty(&client->last_membership_node_list)) { if ((dynar_str_catf(outbuf, " Membership node list:\t") == -1) || (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose, &client->last_membership_node_list) == -1) || (dynar_str_catf(outbuf, "\n") == -1)) { return (-1); } } if (verbose) { if (dynar_str_catf(outbuf, " TLS active:\t\t%s", (client->tls_started ? "Yes" : "No")) == -1) { return (-1); } if (client->tls_started && client->tls_peer_certificate_verified) { if (dynar_str_catf(outbuf, " (client certificate verified)") == -1) { return (-1); } } if (dynar_str_catf(outbuf, "\n") == -1) { return (-1); } } if (client->last_sent_vote != TLV_VOTE_UNDEFINED) { if (dynar_str_catf(outbuf, " Vote:\t\t\t%s", tlv_vote_to_str(client->last_sent_vote)) == -1) { return (-1); } if (client->last_sent_ack_nack_vote != TLV_VOTE_UNDEFINED) { if (dynar_str_catf(outbuf, " (%s)", tlv_vote_to_str(client->last_sent_ack_nack_vote)) == -1) { return (-1); } } if (dynar_str_catf(outbuf, "\n") == -1) { return (-1); } } return (0); } int qnetd_ipc_cmd_list(struct qnetd_instance *instance, struct dynar *outbuf, int verbose, const char *cluster_name) { struct qnetd_cluster *cluster; struct qnetd_client *client; size_t cluster_no, client_no; cluster_no = 0; TAILQ_FOREACH(cluster, &instance->clusters, entries) { if (cluster_name != NULL && strcmp(cluster_name, "") != 0 && strcmp(cluster_name, cluster->cluster_name) != 0) { - continue ; + continue; } if (dynar_str_catf(outbuf, "Cluster \"%s\":\n", cluster->cluster_name) == -1) { return (-1); } client_no = 0; TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) { if (client_no == 0) { if (dynar_str_catf(outbuf, " Algorithm:\t\t%s\n", tlv_decision_algorithm_type_to_str( client->decision_algorithm)) == -1) { return (-1); } if (!qnetd_ipc_cmd_add_tie_breaker(client, outbuf)) { return (-1); } } if (qnetd_ipc_cmd_list_add_client_info(client, outbuf, verbose, client_no) == -1) { return (-1); } client_no++; } cluster_no++; } return (0); }