qdevice-model-net.c 15 KB

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