qdevice-model-net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <config.h>
  35. #include "qdevice-model.h"
  36. #include "qdevice-model-net.h"
  37. #include "qdevice-log.h"
  38. #include "qdevice-net-cast-vote-timer.h"
  39. #include "qdevice-net-instance.h"
  40. #include "qdevice-net-algorithm.h"
  41. #include "qdevice-net-poll.h"
  42. #include "qdevice-net-send.h"
  43. #include "qdevice-net-votequorum.h"
  44. #include "qnet-config.h"
  45. #include "nss-sock.h"
  46. int
  47. qdevice_model_net_init(struct qdevice_instance *instance)
  48. {
  49. struct qdevice_net_instance *net_instance;
  50. qdevice_log(LOG_DEBUG, "Initializing qdevice_net_instance");
  51. if (qdevice_net_instance_init_from_cmap(instance) != 0) {
  52. return (-1);
  53. }
  54. net_instance = instance->model_data;
  55. qdevice_log(LOG_DEBUG, "Registering algorithms");
  56. if (qdevice_net_algorithm_register_all() != 0) {
  57. return (-1);
  58. }
  59. qdevice_log(LOG_DEBUG, "Initializing NSS");
  60. if (nss_sock_init_nss((net_instance->tls_supported != TLV_TLS_UNSUPPORTED ?
  61. (char *)QDEVICE_NET_NSS_DB_DIR : NULL)) != 0) {
  62. qdevice_log_nss(LOG_ERR, "Can't init nss");
  63. return (-1);
  64. }
  65. if (qdevice_net_cast_vote_timer_update(net_instance, TLV_VOTE_ASK_LATER) != 0) {
  66. qdevice_log(LOG_ERR, "Can't update cast vote timer");
  67. return (-1);
  68. }
  69. if (qdevice_net_algorithm_init(net_instance) != 0) {
  70. qdevice_log(LOG_ERR, "Algorithm init failed");
  71. return (-1);
  72. }
  73. return (0);
  74. }
  75. int
  76. qdevice_model_net_destroy(struct qdevice_instance *instance)
  77. {
  78. struct qdevice_net_instance *net_instance;
  79. net_instance = instance->model_data;
  80. qdevice_log(LOG_DEBUG, "Destroying algorithm");
  81. qdevice_net_algorithm_destroy(net_instance);
  82. qdevice_log(LOG_DEBUG, "Destroying qdevice_net_instance");
  83. qdevice_net_instance_destroy(net_instance);
  84. qdevice_log(LOG_DEBUG, "Shutting down NSS");
  85. SSL_ClearSessionCache();
  86. if (NSS_Shutdown() != SECSuccess) {
  87. qdevice_log_nss(LOG_WARNING, "Can't shutdown NSS");
  88. }
  89. PR_Cleanup();
  90. free(net_instance);
  91. return (0);
  92. }
  93. static int
  94. qdevice_model_net_timer_connect_timeout(void *data1, void *data2)
  95. {
  96. struct qdevice_net_instance *instance;
  97. instance = (struct qdevice_net_instance *)data1;
  98. qdevice_log(LOG_ERR, "Connect timeout");
  99. instance->schedule_disconnect = 1;
  100. instance->connect_timer = NULL;
  101. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_CONNECT_TO_THE_SERVER;
  102. return (0);
  103. }
  104. int
  105. qdevice_model_net_run(struct qdevice_instance *instance)
  106. {
  107. struct qdevice_net_instance *net_instance;
  108. int try_connect;
  109. int res;
  110. net_instance = instance->model_data;
  111. qdevice_log(LOG_DEBUG, "Executing qdevice-net");
  112. try_connect = 1;
  113. while (try_connect) {
  114. net_instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT;
  115. net_instance->socket = NULL;
  116. net_instance->connect_timer = timer_list_add(&net_instance->main_timer_list,
  117. net_instance->connect_timeout, qdevice_model_net_timer_connect_timeout,
  118. (void *)net_instance, NULL);
  119. if (net_instance->connect_timer == NULL) {
  120. qdevice_log(LOG_CRIT, "Can't schedule connect timer");
  121. try_connect = 0;
  122. break ;
  123. }
  124. qdevice_log(LOG_DEBUG, "Trying connect to qnetd server %s:%u (timeout = %u)",
  125. net_instance->host_addr, net_instance->host_port, net_instance->connect_timeout);
  126. res = nss_sock_non_blocking_client_init(net_instance->host_addr,
  127. net_instance->host_port, PR_AF_UNSPEC, &net_instance->non_blocking_client);
  128. if (res == -1) {
  129. qdevice_log_nss(LOG_ERR, "Can't initialize non blocking client connection");
  130. }
  131. res = nss_sock_non_blocking_client_try_next(&net_instance->non_blocking_client);
  132. if (res == -1) {
  133. qdevice_log_nss(LOG_ERR, "Can't connect to qnetd host");
  134. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  135. }
  136. while (qdevice_net_poll(net_instance) == 0) {
  137. };
  138. if (net_instance->connect_timer != NULL) {
  139. timer_list_delete(&net_instance->main_timer_list, net_instance->connect_timer);
  140. net_instance->connect_timer = NULL;
  141. }
  142. if (net_instance->echo_request_timer != NULL) {
  143. timer_list_delete(&net_instance->main_timer_list, net_instance->echo_request_timer);
  144. net_instance->echo_request_timer = NULL;
  145. }
  146. try_connect = qdevice_net_disconnect_reason_try_reconnect(net_instance->disconnect_reason);
  147. if (qdevice_net_algorithm_disconnected(net_instance,
  148. net_instance->disconnect_reason, &try_connect) != 0) {
  149. qdevice_log(LOG_ERR, "Algorithm returned error, force exit");
  150. return (-1);
  151. }
  152. if (net_instance->disconnect_reason ==
  153. QDEVICE_NET_DISCONNECT_REASON_COROSYNC_CONNECTION_CLOSED) {
  154. try_connect = 0;
  155. }
  156. if (net_instance->socket != NULL) {
  157. if (PR_Close(net_instance->socket) != PR_SUCCESS) {
  158. qdevice_log_nss(LOG_WARNING, "Unable to close connection");
  159. }
  160. net_instance->socket = NULL;
  161. }
  162. if (!net_instance->non_blocking_client.destroyed) {
  163. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  164. }
  165. if (net_instance->non_blocking_client.socket != NULL) {
  166. if (PR_Close(net_instance->non_blocking_client.socket) != PR_SUCCESS) {
  167. qdevice_log_nss(LOG_WARNING, "Unable to close non-blocking client connection");
  168. }
  169. net_instance->non_blocking_client.socket = NULL;
  170. }
  171. qdevice_net_instance_clean(net_instance);
  172. }
  173. return (0);
  174. }
  175. /*
  176. * Called when cmap reload (or nodelist) was requested.
  177. *
  178. * nlist is node list
  179. * config_version is valid only if config_version_set != 0
  180. *
  181. * Should return 0 if processing should continue or -1 to call exit
  182. */
  183. int
  184. qdevice_model_net_config_node_list_changed(struct qdevice_instance *instance,
  185. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  186. {
  187. struct qdevice_net_instance *net_instance;
  188. int send_node_list;
  189. enum tlv_vote vote;
  190. net_instance = instance->model_data;
  191. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  192. /*
  193. * Nodelist changed, but connection to qnetd not initiated yet.
  194. */
  195. send_node_list = 0;
  196. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  197. vote = TLV_VOTE_NACK;
  198. } else {
  199. vote = TLV_VOTE_NO_CHANGE;
  200. }
  201. } else {
  202. send_node_list = 1;
  203. vote = TLV_VOTE_NO_CHANGE;
  204. }
  205. if (qdevice_net_algorithm_config_node_list_changed(net_instance, nlist, config_version_set,
  206. config_version, &send_node_list, &vote) != 0) {
  207. qdevice_log(LOG_ERR, "Algorithm returned error, force exit");
  208. return (-1);
  209. } else {
  210. qdevice_log(LOG_DEBUG, "Algorithm decided to %s node list and result vote is %s",
  211. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  212. }
  213. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  214. qdevice_log(LOG_CRIT, "qdevice_model_net_config_node_list_changed fatal error. "
  215. " Can't update cast vote timer vote");
  216. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  217. net_instance->schedule_disconnect = 1;
  218. }
  219. if (send_node_list) {
  220. if (qdevice_net_send_config_node_list(net_instance, nlist, config_version_set,
  221. config_version, 0) != 0) {
  222. net_instance->schedule_disconnect = 1;
  223. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  224. }
  225. }
  226. return (0);
  227. }
  228. /*
  229. * Called when cmap reload (or nodelist) was requested, but it was not possible to
  230. * get node list.
  231. *
  232. * Should return 0 if processing should continue or -1 to call exit
  233. */
  234. int
  235. qdevice_model_net_get_config_node_list_failed(struct qdevice_instance *instance)
  236. {
  237. struct qdevice_net_instance *net_instance;
  238. net_instance = instance->model_data;
  239. net_instance->schedule_disconnect = 1;
  240. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  241. return (0);
  242. }
  243. int
  244. qdevice_model_net_votequorum_quorum_notify(struct qdevice_instance *instance,
  245. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  246. {
  247. struct qdevice_net_instance *net_instance;
  248. int send_node_list;
  249. enum tlv_vote vote;
  250. net_instance = instance->model_data;
  251. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  252. /*
  253. * Nodelist changed, but connection to qnetd not initiated yet.
  254. */
  255. send_node_list = 0;
  256. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  257. vote = TLV_VOTE_NACK;
  258. } else {
  259. vote = TLV_VOTE_NO_CHANGE;
  260. }
  261. } else {
  262. send_node_list = 1;
  263. vote = TLV_VOTE_NO_CHANGE;
  264. }
  265. if (qdevice_net_algorithm_votequorum_quorum_notify(net_instance, quorate,
  266. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  267. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  268. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_QUORUM_NOTIFY_ERR;
  269. net_instance->schedule_disconnect = 1;
  270. } else {
  271. qdevice_log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  272. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  273. }
  274. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  275. qdevice_log(LOG_CRIT, "qdevice_model_net_votequorum_quorum_notify fatal error. "
  276. " Can't update cast vote timer vote");
  277. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  278. net_instance->schedule_disconnect = 1;
  279. }
  280. if (send_node_list) {
  281. if (qdevice_net_send_quorum_node_list(net_instance,
  282. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  283. node_list_entries, node_list) != 0) {
  284. /*
  285. * Fatal error -> schedule disconnect
  286. */
  287. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  288. net_instance->schedule_disconnect = 1;
  289. }
  290. }
  291. return (0);
  292. }
  293. int
  294. qdevice_model_net_votequorum_node_list_notify(struct qdevice_instance *instance,
  295. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  296. {
  297. struct qdevice_net_instance *net_instance;
  298. struct tlv_ring_id tlv_rid;
  299. enum tlv_vote vote;
  300. int send_node_list;
  301. net_instance = instance->model_data;
  302. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  303. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  304. /*
  305. * Nodelist changed, but connection to qnetd not initiated yet.
  306. */
  307. send_node_list = 0;
  308. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  309. vote = TLV_VOTE_NACK;
  310. } else {
  311. vote = TLV_VOTE_NO_CHANGE;
  312. }
  313. } else {
  314. send_node_list = 1;
  315. vote = TLV_VOTE_NO_CHANGE;
  316. }
  317. if (qdevice_net_algorithm_votequorum_node_list_notify(net_instance, &tlv_rid,
  318. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  319. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  320. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_NOTIFY_ERR;
  321. net_instance->schedule_disconnect = 1;
  322. } else {
  323. qdevice_log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  324. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  325. }
  326. if (send_node_list) {
  327. if (qdevice_net_send_membership_node_list(net_instance, &tlv_rid,
  328. node_list_entries, node_list) != 0) {
  329. /*
  330. * Fatal error -> schedule disconnect
  331. */
  332. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  333. net_instance->schedule_disconnect = 1;
  334. }
  335. }
  336. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  337. qdevice_log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  338. "Can't update cast vote timer");
  339. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  340. net_instance->schedule_disconnect = 1;
  341. }
  342. return (0);
  343. }
  344. int
  345. qdevice_model_net_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  346. uint32_t expected_votes)
  347. {
  348. struct qdevice_net_instance *net_instance;
  349. net_instance = instance->model_data;
  350. return (0);
  351. }
  352. static struct qdevice_model qdevice_model_net = {
  353. .name = "net",
  354. .init = qdevice_model_net_init,
  355. .destroy = qdevice_model_net_destroy,
  356. .run = qdevice_model_net_run,
  357. .get_config_node_list_failed = qdevice_model_net_get_config_node_list_failed,
  358. .config_node_list_changed = qdevice_model_net_config_node_list_changed,
  359. .votequorum_quorum_notify = qdevice_model_net_votequorum_quorum_notify,
  360. .votequorum_node_list_notify = qdevice_model_net_votequorum_node_list_notify,
  361. .votequorum_expected_votes_notify = qdevice_model_net_votequorum_expected_votes_notify,
  362. };
  363. int
  364. qdevice_model_net_register(void)
  365. {
  366. return (qdevice_model_register(QDEVICE_MODEL_TYPE_NET, &qdevice_model_net));
  367. }