qdevice-net-instance.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2015-2016 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-log.h"
  35. #include "qdevice-net-instance.h"
  36. #include "qnet-config.h"
  37. #include "utils.h"
  38. /*
  39. * Needed for creating nspr handle from unix fd
  40. */
  41. #include <private/pprio.h>
  42. int
  43. qdevice_net_instance_init(struct qdevice_net_instance *instance, size_t initial_receive_size,
  44. size_t initial_send_size, size_t min_send_size, size_t max_send_buffers,
  45. size_t max_receive_size,
  46. enum tlv_tls_supported tls_supported,
  47. enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
  48. uint32_t sync_heartbeat_interval, uint32_t cast_vote_timer_interval,
  49. const char *host_addr, uint16_t host_port, const char *cluster_name,
  50. const struct tlv_tie_breaker *tie_breaker, uint32_t connect_timeout,
  51. int force_ip_version, int cmap_fd, int votequorum_fd, int local_socket_fd)
  52. {
  53. memset(instance, 0, sizeof(*instance));
  54. instance->initial_receive_size = initial_receive_size;
  55. instance->initial_send_size = initial_send_size;
  56. instance->min_send_size = min_send_size;
  57. instance->max_receive_size = max_receive_size;
  58. instance->decision_algorithm = decision_algorithm;
  59. instance->heartbeat_interval = heartbeat_interval;
  60. instance->sync_heartbeat_interval = sync_heartbeat_interval;
  61. instance->cast_vote_timer_interval = cast_vote_timer_interval;
  62. instance->cast_vote_timer = NULL;
  63. instance->host_addr = host_addr;
  64. instance->host_port = host_port;
  65. instance->cluster_name = cluster_name;
  66. instance->connect_timeout = connect_timeout;
  67. instance->last_msg_seq_num = 1;
  68. instance->echo_request_expected_msg_seq_num = 1;
  69. instance->echo_reply_received_msg_seq_num = 1;
  70. instance->force_ip_version = force_ip_version;
  71. memcpy(&instance->tie_breaker, tie_breaker, sizeof(*tie_breaker));
  72. dynar_init(&instance->receive_buffer, initial_receive_size);
  73. send_buffer_list_init(&instance->send_buffer_list, max_send_buffers,
  74. initial_send_size);
  75. timer_list_init(&instance->main_timer_list);
  76. instance->tls_supported = tls_supported;
  77. if ((instance->cmap_poll_fd = PR_CreateSocketPollFd(cmap_fd)) == NULL) {
  78. qdevice_log_nss(LOG_CRIT, "Can't create NSPR cmap poll fd");
  79. return (-1);
  80. }
  81. if ((instance->votequorum_poll_fd = PR_CreateSocketPollFd(votequorum_fd)) == NULL) {
  82. qdevice_log_nss(LOG_CRIT, "Can't create NSPR votequorum poll fd");
  83. return (-1);
  84. }
  85. if ((instance->local_socket_poll_fd = PR_CreateSocketPollFd(local_socket_fd)) == NULL) {
  86. qdevice_log_nss(LOG_CRIT, "Can't create NSPR local socket poll fd");
  87. return (-1);
  88. }
  89. return (0);
  90. }
  91. void
  92. qdevice_net_instance_clean(struct qdevice_net_instance *instance)
  93. {
  94. dynar_clean(&instance->receive_buffer);
  95. send_buffer_list_free(&instance->send_buffer_list);
  96. instance->skipping_msg = 0;
  97. instance->msg_already_received_bytes = 0;
  98. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY;
  99. instance->echo_request_expected_msg_seq_num = instance->echo_reply_received_msg_seq_num;
  100. instance->using_tls = 0;
  101. instance->schedule_disconnect = 0;
  102. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNDEFINED;
  103. }
  104. int
  105. qdevice_net_instance_destroy(struct qdevice_net_instance *instance)
  106. {
  107. dynar_destroy(&instance->receive_buffer);
  108. send_buffer_list_free(&instance->send_buffer_list);
  109. timer_list_free(&instance->main_timer_list);
  110. free((void *)instance->cluster_name);
  111. free((void *)instance->host_addr);
  112. if (PR_DestroySocketPollFd(instance->votequorum_poll_fd) != PR_SUCCESS) {
  113. qdevice_log_nss(LOG_WARNING, "Unable to close votequorum connection fd");
  114. }
  115. if (PR_DestroySocketPollFd(instance->cmap_poll_fd) != PR_SUCCESS) {
  116. qdevice_log_nss(LOG_WARNING, "Unable to close votequorum connection fd");
  117. }
  118. if (PR_DestroySocketPollFd(instance->local_socket_poll_fd) != PR_SUCCESS) {
  119. qdevice_log_nss(LOG_WARNING, "Unable to close local socket poll fd");
  120. }
  121. return (0);
  122. }
  123. int
  124. qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
  125. {
  126. char *str;
  127. cmap_handle_t cmap_handle;
  128. enum tlv_tls_supported tls_supported;
  129. int i;
  130. long int li;
  131. enum tlv_decision_algorithm_type decision_algorithm;
  132. struct tlv_tie_breaker tie_breaker;
  133. uint32_t heartbeat_interval;
  134. uint32_t sync_heartbeat_interval;
  135. uint32_t cast_vote_timer_interval;
  136. char *host_addr;
  137. int host_port;
  138. char *ep;
  139. char *cluster_name;
  140. uint32_t connect_timeout;
  141. struct qdevice_net_instance *net_instance;
  142. int force_ip_version;
  143. cmap_handle = instance->cmap_handle;
  144. net_instance = malloc(sizeof(*net_instance));
  145. if (net_instance == NULL) {
  146. qdevice_log(LOG_ERR, "Can't alloc qdevice_net_instance");
  147. return (-1);
  148. }
  149. /*
  150. * Check tls
  151. */
  152. tls_supported = QDEVICE_NET_DEFAULT_TLS_SUPPORTED;
  153. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  154. if ((i = utils_parse_bool_str(str)) == -1) {
  155. if (strcasecmp(str, "required") != 0) {
  156. free(str);
  157. qdevice_log(LOG_ERR, "quorum.device.net.tls value is not valid.");
  158. goto error_free_instance;
  159. } else {
  160. tls_supported = TLV_TLS_REQUIRED;
  161. }
  162. } else {
  163. if (i == 1) {
  164. tls_supported = TLV_TLS_SUPPORTED;
  165. } else {
  166. tls_supported = TLV_TLS_UNSUPPORTED;
  167. }
  168. }
  169. free(str);
  170. }
  171. /*
  172. * Host
  173. */
  174. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  175. qdevice_log(LOG_ERR, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  176. goto error_free_instance;
  177. }
  178. host_addr = str;
  179. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  180. host_port = strtol(str, &ep, 10);
  181. free(str);
  182. if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
  183. qdevice_log(LOG_ERR, "quorum.device.net.port must be in range 0-65535");
  184. goto error_free_host_addr;
  185. }
  186. } else {
  187. host_port = QNETD_DEFAULT_HOST_PORT;
  188. }
  189. /*
  190. * Cluster name
  191. */
  192. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  193. qdevice_log(LOG_ERR, "Cluster name (totem.cluster_name) has to be defined.");
  194. goto error_free_host_addr;
  195. }
  196. cluster_name = str;
  197. /*
  198. * Adjust qdevice timeouts to better suit qnetd
  199. */
  200. cast_vote_timer_interval = instance->heartbeat_interval * 0.5;
  201. heartbeat_interval = instance->heartbeat_interval * 0.8;
  202. if (heartbeat_interval < QDEVICE_HEARTBEAT_INTERVAL_MIN) {
  203. qdevice_log(LOG_WARNING, "Heartbeat interval too small %"PRIu32". Adjusting to %"PRIu32".",
  204. heartbeat_interval, QNETD_HEARTBEAT_INTERVAL_MIN);
  205. heartbeat_interval = QNETD_HEARTBEAT_INTERVAL_MIN;
  206. }
  207. if (heartbeat_interval > QDEVICE_HEARTBEAT_INTERVAL_MAX) {
  208. qdevice_log(LOG_WARNING, "Heartbeat interval too big %"PRIu32". Adjusting to %"PRIu32".",
  209. heartbeat_interval, QNETD_HEARTBEAT_INTERVAL_MAX);
  210. heartbeat_interval = QNETD_HEARTBEAT_INTERVAL_MAX;
  211. }
  212. sync_heartbeat_interval = instance->sync_heartbeat_interval * 0.8;
  213. /*
  214. * Choose decision algorithm
  215. */
  216. if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
  217. decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
  218. } else {
  219. if (strcmp(str, "test") == 0) {
  220. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  221. } else if (strcmp(str, "ffsplit") == 0) {
  222. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
  223. } else if (strcmp(str, "2nodelms") == 0) {
  224. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
  225. } else if (strcmp(str, "lms") == 0) {
  226. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
  227. } else {
  228. qdevice_log(LOG_ERR, "Unknown decision algorithm %s", str);
  229. free(str);
  230. goto error_free_cluster_name;
  231. }
  232. free(str);
  233. }
  234. /*
  235. * Load tie_breaker mode
  236. */
  237. memset(&tie_breaker, 0, sizeof(tie_breaker));
  238. if (cmap_get_string(cmap_handle, "quorum.device.net.tie_breaker", &str) != CS_OK) {
  239. tie_breaker.mode = QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE;
  240. } else {
  241. if (strcmp(str, "lowest") == 0) {
  242. tie_breaker.mode = TLV_TIE_BREAKER_MODE_LOWEST;
  243. } else if (strcmp(str, "highest") == 0) {
  244. tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
  245. } else {
  246. li = strtol(str, &ep, 10);
  247. if (li <= 0 || li > ((uint32_t)~0) || *ep != '\0') {
  248. qdevice_log(LOG_ERR, "tie_breaker must be lowest|highest|valid_node_id");
  249. free(str);
  250. goto error_free_cluster_name;
  251. }
  252. tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
  253. tie_breaker.node_id = li;
  254. }
  255. free(str);
  256. }
  257. /*
  258. * Get connect timeout
  259. */
  260. if (cmap_get_string(cmap_handle, "quorum.device.net.connect_timeout", &str) != CS_OK) {
  261. connect_timeout = heartbeat_interval;
  262. } else {
  263. li = strtol(str, &ep, 10);
  264. if (li < QDEVICE_NET_MIN_CONNECT_TIMEOUT || li > QDEVICE_NET_MAX_CONNECT_TIMEOUT || *ep != '\0') {
  265. qdevice_log(LOG_ERR, "connect_timeout must be valid number in range <%lu,%lu>",
  266. QDEVICE_NET_MIN_CONNECT_TIMEOUT, QDEVICE_NET_MAX_CONNECT_TIMEOUT);
  267. free(str);
  268. goto error_free_cluster_name;
  269. }
  270. connect_timeout = li;
  271. free(str);
  272. }
  273. if (cmap_get_string(cmap_handle, "quorum.device.net.force_ip_version", &str) != CS_OK) {
  274. force_ip_version = 0;
  275. } else {
  276. li = strtol(str, &ep, 10);
  277. if ((li != 0 && li != 4 && li != 6) || *ep != '\0') {
  278. qdevice_log(LOG_ERR, "force_ip_version must be one of 0|4|6");
  279. free(str);
  280. goto error_free_cluster_name;
  281. }
  282. force_ip_version = li;
  283. free(str);
  284. }
  285. /*
  286. * Really initialize instance
  287. */
  288. if (qdevice_net_instance_init(net_instance,
  289. QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE, QDEVICE_NET_INITIAL_MSG_SEND_SIZE,
  290. QDEVICE_NET_MIN_MSG_SEND_SIZE, QDEVICE_NET_MAX_SEND_BUFFERS,
  291. QDEVICE_NET_MAX_MSG_RECEIVE_SIZE,
  292. tls_supported, decision_algorithm,
  293. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  294. host_addr, host_port, cluster_name, &tie_breaker, connect_timeout,
  295. force_ip_version,
  296. instance->cmap_poll_fd, instance->votequorum_poll_fd,
  297. instance->local_socket_fd) == -1) {
  298. qdevice_log(LOG_ERR, "Can't initialize qdevice-net instance");
  299. goto error_free_instance;
  300. }
  301. net_instance->qdevice_instance_ptr = instance;
  302. instance->model_data = net_instance;
  303. return (0);
  304. error_free_cluster_name:
  305. free(cluster_name);
  306. error_free_host_addr:
  307. free(host_addr);
  308. error_free_instance:
  309. free(net_instance);
  310. return (-1);
  311. }