qdevice-model-net.c 14 KB

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