qdevice-model-net.c 15 KB

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