qdevice-net-instance.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2015-2017 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 "qdevice-config.h"
  35. #include "qdevice-log.h"
  36. #include "qdevice-net-instance.h"
  37. #include "qnet-config.h"
  38. #include "utils.h"
  39. #include "qdevice-net-poll-array-user-data.h"
  40. #include "qdevice-ipc.h"
  41. /*
  42. * Needed for creating nspr handle from unix fd
  43. */
  44. #include <private/pprio.h>
  45. int
  46. qdevice_net_instance_init(struct qdevice_net_instance *instance,
  47. enum tlv_tls_supported tls_supported,
  48. enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
  49. uint32_t sync_heartbeat_interval, uint32_t cast_vote_timer_interval,
  50. const char *host_addr, uint16_t host_port, const char *cluster_name,
  51. const struct tlv_tie_breaker *tie_breaker, uint32_t connect_timeout,
  52. int force_ip_version, int cmap_fd, int votequorum_fd, int local_socket_fd,
  53. const struct qdevice_advanced_settings *advanced_settings,
  54. int heuristics_pipe_cmd_send_fd, int heuristics_pipe_cmd_recv_fd,
  55. int heuristics_pipe_log_recv_fd)
  56. {
  57. memset(instance, 0, sizeof(*instance));
  58. instance->advanced_settings = advanced_settings;
  59. instance->decision_algorithm = decision_algorithm;
  60. instance->heartbeat_interval = heartbeat_interval;
  61. instance->sync_heartbeat_interval = sync_heartbeat_interval;
  62. instance->cast_vote_timer_interval = cast_vote_timer_interval;
  63. instance->cast_vote_timer = NULL;
  64. instance->host_addr = host_addr;
  65. instance->host_port = host_port;
  66. instance->cluster_name = cluster_name;
  67. instance->connect_timeout = connect_timeout;
  68. instance->last_msg_seq_num = 1;
  69. instance->echo_request_expected_msg_seq_num = 1;
  70. instance->echo_reply_received_msg_seq_num = 1;
  71. instance->force_ip_version = force_ip_version;
  72. instance->last_echo_reply_received_time = ((time_t) -1);
  73. instance->connected_since_time = ((time_t) -1);
  74. memcpy(&instance->tie_breaker, tie_breaker, sizeof(*tie_breaker));
  75. dynar_init(&instance->receive_buffer, advanced_settings->net_initial_msg_receive_size);
  76. send_buffer_list_init(&instance->send_buffer_list, advanced_settings->net_max_send_buffers,
  77. advanced_settings->net_initial_msg_send_size);
  78. timer_list_init(&instance->main_timer_list);
  79. pr_poll_array_init(&instance->poll_array, sizeof(struct qdevice_net_poll_array_user_data));
  80. instance->tls_supported = tls_supported;
  81. if ((instance->cmap_poll_fd = PR_CreateSocketPollFd(cmap_fd)) == NULL) {
  82. qdevice_log_nss(LOG_CRIT, "Can't create NSPR cmap poll fd");
  83. return (-1);
  84. }
  85. if ((instance->votequorum_poll_fd = PR_CreateSocketPollFd(votequorum_fd)) == NULL) {
  86. qdevice_log_nss(LOG_CRIT, "Can't create NSPR votequorum poll fd");
  87. return (-1);
  88. }
  89. if ((instance->ipc_socket_poll_fd = PR_CreateSocketPollFd(local_socket_fd)) == NULL) {
  90. qdevice_log_nss(LOG_CRIT, "Can't create NSPR IPC socket poll fd");
  91. return (-1);
  92. }
  93. if ((instance->heuristics_pipe_cmd_send_poll_fd =
  94. PR_CreateSocketPollFd(heuristics_pipe_cmd_send_fd)) == NULL) {
  95. qdevice_log_nss(LOG_CRIT, "Can't create NSPR heuristics pipe command send poll fd");
  96. return (-1);
  97. }
  98. if ((instance->heuristics_pipe_cmd_recv_poll_fd =
  99. PR_CreateSocketPollFd(heuristics_pipe_cmd_recv_fd)) == NULL) {
  100. qdevice_log_nss(LOG_CRIT, "Can't create NSPR heuristics pipe command recv poll fd");
  101. return (-1);
  102. }
  103. if ((instance->heuristics_pipe_log_recv_poll_fd =
  104. PR_CreateSocketPollFd(heuristics_pipe_log_recv_fd)) == NULL) {
  105. qdevice_log_nss(LOG_CRIT, "Can't create NSPR heuristics pipe log recv poll fd");
  106. return (-1);
  107. }
  108. return (0);
  109. }
  110. void
  111. qdevice_net_instance_clean(struct qdevice_net_instance *instance)
  112. {
  113. dynar_clean(&instance->receive_buffer);
  114. send_buffer_list_free(&instance->send_buffer_list);
  115. instance->skipping_msg = 0;
  116. instance->msg_already_received_bytes = 0;
  117. instance->echo_request_expected_msg_seq_num = instance->echo_reply_received_msg_seq_num;
  118. instance->using_tls = 0;
  119. instance->tls_client_cert_sent = 0;
  120. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT;
  121. instance->schedule_disconnect = 0;
  122. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNDEFINED;
  123. instance->last_echo_reply_received_time = ((time_t) -1);
  124. instance->connected_since_time = ((time_t) -1);
  125. }
  126. int
  127. qdevice_net_instance_destroy(struct qdevice_net_instance *instance)
  128. {
  129. struct unix_socket_client *ipc_client;
  130. const struct unix_socket_client_list *ipc_client_list;
  131. struct qdevice_ipc_user_data *qdevice_ipc_user_data;
  132. PRFileDesc *prfd;
  133. ipc_client_list = &instance->qdevice_instance_ptr->local_ipc.clients;
  134. TAILQ_FOREACH(ipc_client, ipc_client_list, entries) {
  135. qdevice_ipc_user_data = (struct qdevice_ipc_user_data *)ipc_client->user_data;
  136. prfd = (PRFileDesc *)qdevice_ipc_user_data->model_data;
  137. if (PR_DestroySocketPollFd(prfd) != PR_SUCCESS) {
  138. qdevice_log_nss(LOG_WARNING, "Unable to destroy client IPC poll socket fd");
  139. }
  140. }
  141. dynar_destroy(&instance->receive_buffer);
  142. send_buffer_list_free(&instance->send_buffer_list);
  143. pr_poll_array_destroy(&instance->poll_array);
  144. timer_list_free(&instance->main_timer_list);
  145. free((void *)instance->cluster_name);
  146. free((void *)instance->host_addr);
  147. if (PR_DestroySocketPollFd(instance->votequorum_poll_fd) != PR_SUCCESS) {
  148. qdevice_log_nss(LOG_WARNING, "Unable to close votequorum connection fd");
  149. }
  150. if (PR_DestroySocketPollFd(instance->cmap_poll_fd) != PR_SUCCESS) {
  151. qdevice_log_nss(LOG_WARNING, "Unable to close votequorum connection fd");
  152. }
  153. if (PR_DestroySocketPollFd(instance->ipc_socket_poll_fd) != PR_SUCCESS) {
  154. qdevice_log_nss(LOG_WARNING, "Unable to close local socket poll fd");
  155. }
  156. if (PR_DestroySocketPollFd(instance->heuristics_pipe_cmd_send_poll_fd) != PR_SUCCESS) {
  157. qdevice_log_nss(LOG_WARNING, "Unable to close heuristics pipe command send poll fd");
  158. return (-1);
  159. }
  160. if (PR_DestroySocketPollFd(instance->heuristics_pipe_cmd_recv_poll_fd) != PR_SUCCESS) {
  161. qdevice_log_nss(LOG_WARNING, "Unable to close heuristics pipe command recv poll fd");
  162. return (-1);
  163. }
  164. if (PR_DestroySocketPollFd(instance->heuristics_pipe_log_recv_poll_fd) != PR_SUCCESS) {
  165. qdevice_log_nss(LOG_WARNING, "Unable to close heuristics pipe log recv poll fd");
  166. return (-1);
  167. }
  168. return (0);
  169. }
  170. int
  171. qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
  172. {
  173. char *str;
  174. cmap_handle_t cmap_handle;
  175. enum tlv_tls_supported tls_supported;
  176. int i;
  177. long int li;
  178. enum tlv_decision_algorithm_type decision_algorithm;
  179. struct tlv_tie_breaker tie_breaker;
  180. uint32_t heartbeat_interval;
  181. uint32_t sync_heartbeat_interval;
  182. uint32_t cast_vote_timer_interval;
  183. char *host_addr;
  184. int host_port;
  185. char *ep;
  186. char *cluster_name;
  187. uint32_t connect_timeout;
  188. struct qdevice_net_instance *net_instance;
  189. int force_ip_version;
  190. cmap_handle = instance->cmap_handle;
  191. net_instance = malloc(sizeof(*net_instance));
  192. if (net_instance == NULL) {
  193. qdevice_log(LOG_ERR, "Can't alloc qdevice_net_instance");
  194. return (-1);
  195. }
  196. /*
  197. * Check tls
  198. */
  199. tls_supported = QDEVICE_NET_DEFAULT_TLS_SUPPORTED;
  200. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  201. if ((i = utils_parse_bool_str(str)) == -1) {
  202. if (strcasecmp(str, "required") != 0) {
  203. free(str);
  204. qdevice_log(LOG_ERR, "quorum.device.net.tls value is not valid.");
  205. goto error_free_instance;
  206. } else {
  207. tls_supported = TLV_TLS_REQUIRED;
  208. }
  209. } else {
  210. if (i == 1) {
  211. tls_supported = TLV_TLS_SUPPORTED;
  212. } else {
  213. tls_supported = TLV_TLS_UNSUPPORTED;
  214. }
  215. }
  216. free(str);
  217. }
  218. /*
  219. * Host
  220. */
  221. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  222. qdevice_log(LOG_ERR, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  223. goto error_free_instance;
  224. }
  225. host_addr = str;
  226. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  227. host_port = strtol(str, &ep, 10);
  228. free(str);
  229. if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
  230. qdevice_log(LOG_ERR, "quorum.device.net.port must be in range 0-65535");
  231. goto error_free_host_addr;
  232. }
  233. } else {
  234. host_port = QNETD_DEFAULT_HOST_PORT;
  235. }
  236. /*
  237. * Cluster name
  238. */
  239. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  240. qdevice_log(LOG_ERR, "Cluster name (totem.cluster_name) has to be defined.");
  241. goto error_free_host_addr;
  242. }
  243. cluster_name = str;
  244. /*
  245. * Adjust qdevice timeouts to better suit qnetd
  246. */
  247. cast_vote_timer_interval = instance->heartbeat_interval * 0.5;
  248. heartbeat_interval = instance->heartbeat_interval * 0.8;
  249. if (heartbeat_interval < instance->advanced_settings->net_heartbeat_interval_min) {
  250. qdevice_log(LOG_WARNING, "Heartbeat interval too small %"PRIu32". Adjusting to %"PRIu32".",
  251. heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_min);
  252. heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_min;
  253. }
  254. if (heartbeat_interval > instance->advanced_settings->net_heartbeat_interval_max) {
  255. qdevice_log(LOG_WARNING, "Heartbeat interval too big %"PRIu32". Adjusting to %"PRIu32".",
  256. heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_max);
  257. heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_max;
  258. }
  259. sync_heartbeat_interval = instance->sync_heartbeat_interval * 0.8;
  260. /*
  261. * Choose decision algorithm
  262. */
  263. if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
  264. decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
  265. } else {
  266. if (strcmp(str, "test") == 0) {
  267. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  268. } else if (strcmp(str, "ffsplit") == 0) {
  269. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
  270. } else if (strcmp(str, "2nodelms") == 0) {
  271. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
  272. } else if (strcmp(str, "lms") == 0) {
  273. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
  274. } else {
  275. qdevice_log(LOG_ERR, "Unknown decision algorithm %s", str);
  276. free(str);
  277. goto error_free_cluster_name;
  278. }
  279. free(str);
  280. }
  281. if (decision_algorithm == TLV_DECISION_ALGORITHM_TYPE_TEST &&
  282. !instance->advanced_settings->net_test_algorithm_enabled) {
  283. qdevice_log(LOG_ERR, "Test algorithm is not enabled. You can force enable it by "
  284. "passing -S net_test_algorithm_enabled=on to %s command", QDEVICE_PROGRAM_NAME);
  285. goto error_free_cluster_name;
  286. }
  287. /*
  288. * Load tie_breaker mode
  289. */
  290. memset(&tie_breaker, 0, sizeof(tie_breaker));
  291. if (cmap_get_string(cmap_handle, "quorum.device.net.tie_breaker", &str) != CS_OK) {
  292. tie_breaker.mode = QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE;
  293. } else {
  294. if (strcmp(str, "lowest") == 0) {
  295. tie_breaker.mode = TLV_TIE_BREAKER_MODE_LOWEST;
  296. } else if (strcmp(str, "highest") == 0) {
  297. tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
  298. } else {
  299. li = strtol(str, &ep, 10);
  300. if (li <= 0 || li > ((uint32_t)~0) || *ep != '\0') {
  301. qdevice_log(LOG_ERR, "tie_breaker must be lowest|highest|valid_node_id");
  302. free(str);
  303. goto error_free_cluster_name;
  304. }
  305. tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
  306. tie_breaker.node_id = li;
  307. }
  308. free(str);
  309. }
  310. /*
  311. * Get connect timeout
  312. */
  313. if (cmap_get_string(cmap_handle, "quorum.device.net.connect_timeout", &str) != CS_OK) {
  314. connect_timeout = heartbeat_interval;
  315. } else {
  316. li = strtol(str, &ep, 10);
  317. if (li < instance->advanced_settings->net_min_connect_timeout ||
  318. li > instance->advanced_settings->net_max_connect_timeout || *ep != '\0') {
  319. qdevice_log(LOG_ERR, "connect_timeout must be valid number in "
  320. "range <%"PRIu32",%"PRIu32">",
  321. instance->advanced_settings->net_min_connect_timeout,
  322. instance->advanced_settings->net_max_connect_timeout);
  323. free(str);
  324. goto error_free_cluster_name;
  325. }
  326. connect_timeout = li;
  327. free(str);
  328. }
  329. if (cmap_get_string(cmap_handle, "quorum.device.net.force_ip_version", &str) != CS_OK) {
  330. force_ip_version = 0;
  331. } else {
  332. li = strtol(str, &ep, 10);
  333. if ((li != 0 && li != 4 && li != 6) || *ep != '\0') {
  334. qdevice_log(LOG_ERR, "force_ip_version must be one of 0|4|6");
  335. free(str);
  336. goto error_free_cluster_name;
  337. }
  338. force_ip_version = li;
  339. free(str);
  340. }
  341. /*
  342. * Really initialize instance
  343. */
  344. if (qdevice_net_instance_init(net_instance,
  345. tls_supported, decision_algorithm,
  346. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  347. host_addr, host_port, cluster_name, &tie_breaker, connect_timeout,
  348. force_ip_version,
  349. instance->cmap_poll_fd, instance->votequorum_poll_fd,
  350. instance->local_ipc.socket, instance->advanced_settings,
  351. instance->heuristics_instance.pipe_cmd_send,
  352. instance->heuristics_instance.pipe_cmd_recv,
  353. instance->heuristics_instance.pipe_log_recv) == -1) {
  354. qdevice_log(LOG_ERR, "Can't initialize qdevice-net instance");
  355. goto error_free_instance;
  356. }
  357. net_instance->qdevice_instance_ptr = instance;
  358. instance->model_data = net_instance;
  359. return (0);
  360. error_free_cluster_name:
  361. free(cluster_name);
  362. error_free_host_addr:
  363. free(host_addr);
  364. error_free_instance:
  365. free(net_instance);
  366. return (-1);
  367. }