qdevice-model-net.c 17 KB

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