qdevice-model-net.c 14 KB

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