qdevice-net-instance.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <limits.h>
  35. #include "log.h"
  36. #include "qdevice-config.h"
  37. #include "qdevice-net-instance.h"
  38. #include "qnet-config.h"
  39. #include "utils.h"
  40. #include "qdevice-ipc.h"
  41. int
  42. qdevice_net_instance_init(struct qdevice_net_instance *instance,
  43. enum tlv_tls_supported tls_supported,
  44. enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
  45. uint32_t sync_heartbeat_interval, uint32_t cast_vote_timer_interval,
  46. const char *host_addr, uint16_t host_port, const char *cluster_name,
  47. const struct tlv_tie_breaker *tie_breaker, uint32_t connect_timeout,
  48. int force_ip_version, enum tlv_keep_active_partition_tie_breaker keep_active_partition_tie_breaker,
  49. int cmap_fd, int votequorum_fd, int local_socket_fd,
  50. const struct qdevice_advanced_settings *advanced_settings,
  51. int heuristics_pipe_cmd_send_fd, int heuristics_pipe_cmd_recv_fd,
  52. int heuristics_pipe_log_recv_fd)
  53. {
  54. memset(instance, 0, sizeof(*instance));
  55. instance->advanced_settings = advanced_settings;
  56. instance->decision_algorithm = decision_algorithm;
  57. instance->heartbeat_interval = heartbeat_interval;
  58. instance->sync_heartbeat_interval = sync_heartbeat_interval;
  59. instance->cast_vote_timer_interval = cast_vote_timer_interval;
  60. instance->cast_vote_timer = NULL;
  61. instance->host_addr = host_addr;
  62. instance->host_port = host_port;
  63. instance->cluster_name = cluster_name;
  64. instance->connect_timeout = connect_timeout;
  65. instance->last_msg_seq_num = 1;
  66. instance->echo_request_expected_msg_seq_num = 1;
  67. instance->echo_reply_received_msg_seq_num = 1;
  68. instance->force_ip_version = force_ip_version;
  69. instance->last_echo_reply_received_time = ((time_t) -1);
  70. instance->connected_since_time = ((time_t) -1);
  71. instance->keep_active_partition_tie_breaker = keep_active_partition_tie_breaker;
  72. memcpy(&instance->tie_breaker, tie_breaker, sizeof(*tie_breaker));
  73. dynar_init(&instance->receive_buffer, advanced_settings->net_initial_msg_receive_size);
  74. send_buffer_list_init(&instance->send_buffer_list, advanced_settings->net_max_send_buffers,
  75. advanced_settings->net_initial_msg_send_size);
  76. instance->tls_supported = tls_supported;
  77. return (0);
  78. }
  79. void
  80. qdevice_net_instance_clean(struct qdevice_net_instance *instance)
  81. {
  82. dynar_clean(&instance->receive_buffer);
  83. send_buffer_list_free(&instance->send_buffer_list);
  84. instance->skipping_msg = 0;
  85. instance->msg_already_received_bytes = 0;
  86. instance->echo_request_expected_msg_seq_num = instance->echo_reply_received_msg_seq_num;
  87. instance->using_tls = 0;
  88. instance->tls_client_cert_sent = 0;
  89. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT;
  90. instance->schedule_disconnect = 0;
  91. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNDEFINED;
  92. instance->last_echo_reply_received_time = ((time_t) -1);
  93. instance->connected_since_time = ((time_t) -1);
  94. }
  95. int
  96. qdevice_net_instance_destroy(struct qdevice_net_instance *instance)
  97. {
  98. dynar_destroy(&instance->receive_buffer);
  99. send_buffer_list_free(&instance->send_buffer_list);
  100. free((void *)instance->cluster_name);
  101. free((void *)instance->host_addr);
  102. return (0);
  103. }
  104. int
  105. qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
  106. {
  107. char *str;
  108. cmap_handle_t cmap_handle;
  109. enum tlv_tls_supported tls_supported;
  110. int i;
  111. long long int lli;
  112. enum tlv_decision_algorithm_type decision_algorithm;
  113. struct tlv_tie_breaker tie_breaker;
  114. uint32_t heartbeat_interval;
  115. uint32_t sync_heartbeat_interval;
  116. uint32_t cast_vote_timer_interval;
  117. char *host_addr;
  118. int host_port;
  119. char *cluster_name;
  120. uint32_t connect_timeout;
  121. struct qdevice_net_instance *net_instance;
  122. int force_ip_version;
  123. enum tlv_keep_active_partition_tie_breaker keep_active_partition_tb;
  124. cmap_handle = instance->cmap_handle;
  125. net_instance = malloc(sizeof(*net_instance));
  126. if (net_instance == NULL) {
  127. log(LOG_ERR, "Can't alloc qdevice_net_instance");
  128. return (-1);
  129. }
  130. /*
  131. * Check tls
  132. */
  133. tls_supported = QDEVICE_NET_DEFAULT_TLS_SUPPORTED;
  134. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  135. if ((i = utils_parse_bool_str(str)) == -1) {
  136. if (strcasecmp(str, "required") != 0) {
  137. free(str);
  138. log(LOG_ERR, "quorum.device.net.tls value is not valid.");
  139. goto error_free_instance;
  140. } else {
  141. tls_supported = TLV_TLS_REQUIRED;
  142. }
  143. } else {
  144. if (i == 1) {
  145. tls_supported = TLV_TLS_SUPPORTED;
  146. } else {
  147. tls_supported = TLV_TLS_UNSUPPORTED;
  148. }
  149. }
  150. free(str);
  151. }
  152. /*
  153. * Host
  154. */
  155. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  156. log(LOG_ERR, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  157. goto error_free_instance;
  158. }
  159. host_addr = str;
  160. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  161. if (utils_strtonum(str, 1, UINT16_MAX, &lli) == -1) {
  162. log(LOG_ERR, "quorum.device.net.port must be in range 1-%u", UINT16_MAX);
  163. free(str);
  164. goto error_free_host_addr;
  165. }
  166. free(str);
  167. host_port = lli;
  168. } else {
  169. host_port = QNETD_DEFAULT_HOST_PORT;
  170. }
  171. /*
  172. * Cluster name
  173. */
  174. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  175. log(LOG_ERR, "Cluster name (totem.cluster_name) has to be defined.");
  176. goto error_free_host_addr;
  177. }
  178. cluster_name = str;
  179. /*
  180. * Adjust qdevice timeouts to better suit qnetd
  181. */
  182. cast_vote_timer_interval = instance->heartbeat_interval * 0.5;
  183. heartbeat_interval = instance->heartbeat_interval * 0.8;
  184. if (heartbeat_interval < instance->advanced_settings->net_heartbeat_interval_min) {
  185. log(LOG_WARNING, "Heartbeat interval too small %"PRIu32". Adjusting to %"PRIu32".",
  186. heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_min);
  187. heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_min;
  188. }
  189. if (heartbeat_interval > instance->advanced_settings->net_heartbeat_interval_max) {
  190. log(LOG_WARNING, "Heartbeat interval too big %"PRIu32". Adjusting to %"PRIu32".",
  191. heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_max);
  192. heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_max;
  193. }
  194. sync_heartbeat_interval = instance->sync_heartbeat_interval * 0.8;
  195. /*
  196. * Choose decision algorithm
  197. */
  198. if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
  199. decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
  200. } else {
  201. if (strcmp(str, "test") == 0) {
  202. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  203. } else if (strcmp(str, "ffsplit") == 0) {
  204. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
  205. } else if (strcmp(str, "2nodelms") == 0) {
  206. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
  207. } else if (strcmp(str, "lms") == 0) {
  208. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
  209. } else {
  210. log(LOG_ERR, "Unknown decision algorithm %s", str);
  211. free(str);
  212. goto error_free_cluster_name;
  213. }
  214. free(str);
  215. }
  216. if (decision_algorithm == TLV_DECISION_ALGORITHM_TYPE_TEST &&
  217. !instance->advanced_settings->net_test_algorithm_enabled) {
  218. log(LOG_ERR, "Test algorithm is not enabled. You can force enable it by "
  219. "passing -S net_test_algorithm_enabled=on to %s command", QDEVICE_PROGRAM_NAME);
  220. goto error_free_cluster_name;
  221. }
  222. /*
  223. * Load tie_breaker mode
  224. */
  225. memset(&tie_breaker, 0, sizeof(tie_breaker));
  226. if (cmap_get_string(cmap_handle, "quorum.device.net.tie_breaker", &str) != CS_OK) {
  227. tie_breaker.mode = QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE;
  228. } else {
  229. if (strcmp(str, "lowest") == 0) {
  230. tie_breaker.mode = TLV_TIE_BREAKER_MODE_LOWEST;
  231. } else if (strcmp(str, "highest") == 0) {
  232. tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
  233. } else {
  234. if (utils_strtonum(str, 1, UINT32_MAX, &lli) == -1) {
  235. log(LOG_ERR, "tie_breaker must be lowest|highest|valid_node_id");
  236. free(str);
  237. goto error_free_cluster_name;
  238. }
  239. tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
  240. tie_breaker.node_id = lli;
  241. }
  242. free(str);
  243. }
  244. /*
  245. * Get connect timeout
  246. */
  247. if (cmap_get_string(cmap_handle, "quorum.device.net.connect_timeout", &str) != CS_OK) {
  248. connect_timeout = heartbeat_interval;
  249. } else {
  250. if (utils_strtonum(str, instance->advanced_settings->net_min_connect_timeout,
  251. instance->advanced_settings->net_max_connect_timeout, &lli) == -1) {
  252. log(LOG_ERR, "connect_timeout must be valid number in "
  253. "range <%"PRIu32",%"PRIu32">",
  254. instance->advanced_settings->net_min_connect_timeout,
  255. instance->advanced_settings->net_max_connect_timeout);
  256. free(str);
  257. goto error_free_cluster_name;
  258. }
  259. connect_timeout = (uint32_t)lli;
  260. free(str);
  261. }
  262. if (cmap_get_string(cmap_handle, "quorum.device.net.force_ip_version", &str) != CS_OK) {
  263. force_ip_version = 0;
  264. } else {
  265. if ((utils_strtonum(str, 0, 6, &lli) == -1) ||
  266. (lli != 0 && lli != 4 && lli != 6)) {
  267. log(LOG_ERR, "force_ip_version must be one of 0|4|6");
  268. free(str);
  269. goto error_free_cluster_name;
  270. }
  271. force_ip_version = lli;
  272. free(str);
  273. }
  274. keep_active_partition_tb = QDEVICE_NET_DEFAULT_KEEP_ACTIVE_PARTITION_TB;
  275. if (cmap_get_string(cmap_handle, "quorum.device.net.keep_active_partition_tie_breaker",
  276. &str) == CS_OK) {
  277. if ((i = utils_parse_bool_str(str)) == -1) {
  278. log(LOG_ERR, "quorum.device.net.keep_active_partition_tie_breaker "
  279. "value is not valid.");
  280. free(str);
  281. goto error_free_cluster_name;
  282. } else {
  283. if (i == 1) {
  284. keep_active_partition_tb = TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_ENABLED;
  285. } else {
  286. keep_active_partition_tb = TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_DISABLED;
  287. }
  288. }
  289. free(str);
  290. }
  291. /*
  292. * Really initialize instance
  293. */
  294. if (qdevice_net_instance_init(net_instance,
  295. tls_supported, decision_algorithm,
  296. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  297. host_addr, host_port, cluster_name, &tie_breaker, connect_timeout,
  298. force_ip_version, keep_active_partition_tb,
  299. instance->cmap_poll_fd, instance->votequorum_poll_fd,
  300. instance->local_ipc.socket, instance->advanced_settings,
  301. instance->heuristics_instance.pipe_cmd_send,
  302. instance->heuristics_instance.pipe_cmd_recv,
  303. instance->heuristics_instance.pipe_log_recv) == -1) {
  304. log(LOG_ERR, "Can't initialize qdevice-net instance");
  305. goto error_free_instance;
  306. }
  307. net_instance->qdevice_instance_ptr = instance;
  308. instance->model_data = net_instance;
  309. return (0);
  310. error_free_cluster_name:
  311. free(cluster_name);
  312. error_free_host_addr:
  313. free(host_addr);
  314. error_free_instance:
  315. free(net_instance);
  316. return (-1);
  317. }