qdevice-model-net.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. 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_DEBUG, "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 (qdevice_net_disconnect_reason_force_disconnect(net_instance->disconnect_reason)) {
  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,
  202. random() % instance->advanced_settings->net_delay_before_reconnect);
  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, Disconnecting");
  240. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_CONFIG_NODE_LIST_CHANGED_ERR;
  241. net_instance->schedule_disconnect = 1;
  242. return (0);
  243. } else {
  244. qdevice_log(LOG_DEBUG, "Algorithm decided to %s node list and result vote is %s",
  245. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  246. }
  247. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  248. qdevice_log(LOG_CRIT, "qdevice_model_net_config_node_list_changed fatal error. "
  249. " Can't update cast vote timer vote");
  250. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  251. net_instance->schedule_disconnect = 1;
  252. return (0);
  253. }
  254. if (send_node_list) {
  255. if (qdevice_net_send_config_node_list(net_instance, nlist, config_version_set,
  256. config_version, 0) != 0) {
  257. net_instance->schedule_disconnect = 1;
  258. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  259. return (0);
  260. }
  261. }
  262. return (0);
  263. }
  264. /*
  265. * Called when cmap reload (or nodelist) was requested, but it was not possible to
  266. * get node list.
  267. *
  268. * Should return 0 if processing should continue or -1 to call exit
  269. */
  270. int
  271. qdevice_model_net_get_config_node_list_failed(struct qdevice_instance *instance)
  272. {
  273. struct qdevice_net_instance *net_instance;
  274. net_instance = instance->model_data;
  275. net_instance->schedule_disconnect = 1;
  276. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  277. return (0);
  278. }
  279. int
  280. qdevice_model_net_votequorum_quorum_notify(struct qdevice_instance *instance,
  281. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  282. {
  283. struct qdevice_net_instance *net_instance;
  284. int send_node_list;
  285. enum tlv_vote vote;
  286. net_instance = instance->model_data;
  287. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  288. /*
  289. * Nodelist changed, but connection to qnetd not initiated yet.
  290. */
  291. send_node_list = 0;
  292. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  293. vote = TLV_VOTE_NACK;
  294. } else {
  295. vote = TLV_VOTE_NO_CHANGE;
  296. }
  297. } else {
  298. send_node_list = 1;
  299. vote = TLV_VOTE_NO_CHANGE;
  300. }
  301. if (qdevice_net_algorithm_votequorum_quorum_notify(net_instance, quorate,
  302. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  303. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  304. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_QUORUM_NOTIFY_ERR;
  305. net_instance->schedule_disconnect = 1;
  306. return (0);
  307. } else {
  308. qdevice_log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  309. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  310. }
  311. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  312. qdevice_log(LOG_CRIT, "qdevice_model_net_votequorum_quorum_notify fatal error. "
  313. " Can't update cast vote timer vote");
  314. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  315. net_instance->schedule_disconnect = 1;
  316. return (0);
  317. }
  318. if (send_node_list) {
  319. if (qdevice_net_send_quorum_node_list(net_instance,
  320. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  321. node_list_entries, node_list) != 0) {
  322. /*
  323. * Fatal error -> schedule disconnect
  324. */
  325. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  326. net_instance->schedule_disconnect = 1;
  327. return (0);
  328. }
  329. }
  330. return (0);
  331. }
  332. int
  333. qdevice_model_net_votequorum_node_list_notify(struct qdevice_instance *instance,
  334. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  335. {
  336. struct qdevice_net_instance *net_instance;
  337. struct tlv_ring_id tlv_rid;
  338. enum tlv_vote vote;
  339. int send_node_list;
  340. net_instance = instance->model_data;
  341. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  342. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  343. /*
  344. * Nodelist changed, but connection to qnetd not initiated yet.
  345. */
  346. send_node_list = 0;
  347. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  348. vote = TLV_VOTE_NACK;
  349. } else {
  350. vote = TLV_VOTE_NO_CHANGE;
  351. }
  352. } else {
  353. send_node_list = 1;
  354. vote = TLV_VOTE_WAIT_FOR_REPLY;
  355. }
  356. if (qdevice_net_algorithm_votequorum_node_list_notify(net_instance, &tlv_rid,
  357. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  358. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  359. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_NOTIFY_ERR;
  360. net_instance->schedule_disconnect = 1;
  361. return (0);
  362. } else {
  363. qdevice_log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  364. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  365. }
  366. if (send_node_list) {
  367. if (qdevice_net_send_membership_node_list(net_instance, &tlv_rid,
  368. node_list_entries, node_list) != 0) {
  369. /*
  370. * Fatal error -> schedule disconnect
  371. */
  372. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  373. net_instance->schedule_disconnect = 1;
  374. return (0);
  375. }
  376. }
  377. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  378. qdevice_log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  379. "Can't update cast vote timer");
  380. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  381. net_instance->schedule_disconnect = 1;
  382. return (0);
  383. }
  384. return (0);
  385. }
  386. int
  387. qdevice_model_net_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  388. uint32_t expected_votes)
  389. {
  390. struct qdevice_net_instance *net_instance;
  391. enum tlv_vote vote;
  392. net_instance = instance->model_data;
  393. qdevice_log(LOG_DEBUG, "qdevice_model_net_votequorum_expected_votes_notify"
  394. " (expected votes old=%"PRIu32" / new=%"PRIu32")",
  395. net_instance->qdevice_instance_ptr->vq_expected_votes, expected_votes);
  396. vote = TLV_VOTE_NO_CHANGE;
  397. if (qdevice_net_algorithm_votequorum_expected_votes_notify(net_instance, expected_votes,
  398. &vote) != 0) {
  399. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  400. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_EXPECTED_VOTES_NOTIFY_ERR;
  401. net_instance->schedule_disconnect = 1;
  402. return (0);
  403. } else {
  404. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(vote));
  405. }
  406. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  407. qdevice_log(LOG_CRIT, "qdevice_model_net_votequorum_expected_votes_notify fatal error. "
  408. " Can't update cast vote timer vote");
  409. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  410. net_instance->schedule_disconnect = 1;
  411. return (0);
  412. }
  413. return (0);
  414. }
  415. int
  416. qdevice_model_net_ipc_cmd_status(struct qdevice_instance *instance,
  417. struct dynar *outbuf, int verbose)
  418. {
  419. struct qdevice_net_instance *net_instance;
  420. net_instance = instance->model_data;
  421. if (!qdevice_net_ipc_cmd_status(net_instance, outbuf, verbose)) {
  422. return (-1);
  423. }
  424. return (0);
  425. }
  426. static struct qdevice_model qdevice_model_net = {
  427. .name = "net",
  428. .init = qdevice_model_net_init,
  429. .destroy = qdevice_model_net_destroy,
  430. .run = qdevice_model_net_run,
  431. .get_config_node_list_failed = qdevice_model_net_get_config_node_list_failed,
  432. .config_node_list_changed = qdevice_model_net_config_node_list_changed,
  433. .votequorum_quorum_notify = qdevice_model_net_votequorum_quorum_notify,
  434. .votequorum_node_list_notify = qdevice_model_net_votequorum_node_list_notify,
  435. .votequorum_expected_votes_notify = qdevice_model_net_votequorum_expected_votes_notify,
  436. .ipc_cmd_status = qdevice_model_net_ipc_cmd_status,
  437. };
  438. int
  439. qdevice_model_net_register(void)
  440. {
  441. return (qdevice_model_register(QDEVICE_MODEL_TYPE_NET, &qdevice_model_net));
  442. }