qdevice-model-net.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * Copyright (c) 2015-2020 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 "log.h"
  36. #include "qdevice-model.h"
  37. #include "qdevice-model-net.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-heuristics.h"
  43. #include "qdevice-net-send.h"
  44. #include "qdevice-net-socket.h"
  45. #include "qdevice-net-votequorum.h"
  46. #include "qnet-config.h"
  47. #include "nss-sock.h"
  48. /*
  49. * Callbacks
  50. */
  51. static int
  52. check_schedule_disconnect_cb(void *user_data1, void *user_data2)
  53. {
  54. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  55. if (instance->schedule_disconnect) {
  56. return (-1);
  57. }
  58. return (0);
  59. }
  60. static int
  61. qdevice_model_net_timer_connect_timeout(void *data1, void *data2)
  62. {
  63. struct qdevice_net_instance *instance;
  64. instance = (struct qdevice_net_instance *)data1;
  65. log(LOG_ERR, "Connect timeout");
  66. instance->schedule_disconnect = 1;
  67. instance->connect_timer = NULL;
  68. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_CONNECT_TO_THE_SERVER;
  69. return (0);
  70. }
  71. /*
  72. * Exported functions
  73. */
  74. int
  75. qdevice_model_net_init(struct qdevice_instance *instance)
  76. {
  77. struct qdevice_net_instance *net_instance;
  78. log(LOG_DEBUG, "Initializing qdevice_net_instance");
  79. if (qdevice_net_instance_init_from_cmap(instance) != 0) {
  80. return (-1);
  81. }
  82. net_instance = instance->model_data;
  83. log(LOG_DEBUG, "Registering algorithms");
  84. if (qdevice_net_algorithm_register_all() != 0) {
  85. return (-1);
  86. }
  87. log(LOG_DEBUG, "Initializing NSS");
  88. if (nss_sock_init_nss((net_instance->tls_supported != TLV_TLS_UNSUPPORTED ?
  89. instance->advanced_settings->net_nss_db_dir : NULL)) != 0) {
  90. log_nss(LOG_ERR, "Can't init nss");
  91. return (-1);
  92. }
  93. if (qdevice_net_cast_vote_timer_update(net_instance, TLV_VOTE_ASK_LATER) != 0) {
  94. log(LOG_ERR, "Can't update cast vote timer");
  95. return (-1);
  96. }
  97. if (qdevice_net_algorithm_init(net_instance) != 0) {
  98. log(LOG_ERR, "Algorithm init failed");
  99. return (-1);
  100. }
  101. if (qdevice_net_heuristics_init(net_instance) != 0) {
  102. log(LOG_ERR, "Can't initialize net heuristics");
  103. return (-1);
  104. }
  105. return (0);
  106. }
  107. int
  108. qdevice_model_net_destroy(struct qdevice_instance *instance)
  109. {
  110. struct qdevice_net_instance *net_instance;
  111. net_instance = instance->model_data;
  112. log(LOG_DEBUG, "Destroying algorithm");
  113. qdevice_net_algorithm_destroy(net_instance);
  114. log(LOG_DEBUG, "Destroying qdevice_net_instance");
  115. qdevice_net_instance_destroy(net_instance);
  116. log(LOG_DEBUG, "Shutting down NSS");
  117. SSL_ClearSessionCache();
  118. if (NSS_Shutdown() != SECSuccess) {
  119. log_nss(LOG_WARNING, "Can't shutdown NSS");
  120. }
  121. if (PR_Cleanup() != PR_SUCCESS) {
  122. log_nss(LOG_WARNING, "Can't shutdown NSPR");
  123. }
  124. free(net_instance);
  125. return (0);
  126. }
  127. static PRIntn
  128. qdevice_model_net_get_af(const struct qdevice_net_instance *instance)
  129. {
  130. PRIntn af;
  131. af = PR_AF_UNSPEC;
  132. if (instance->force_ip_version == 4) {
  133. af = PR_AF_INET;
  134. }
  135. if (instance->force_ip_version == 6) {
  136. af = PR_AF_INET6;
  137. }
  138. return (af);
  139. }
  140. /*
  141. * 0 - Continue
  142. * -1 - End loop
  143. */
  144. int
  145. qdevice_model_net_pre_poll_loop(struct qdevice_instance *instance)
  146. {
  147. struct qdevice_net_instance *net_instance;
  148. int res;
  149. net_instance = instance->model_data;
  150. net_instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT;
  151. net_instance->socket = NULL;
  152. net_instance->connect_timer = timer_list_add(pr_poll_loop_get_timer_list(&instance->main_poll_loop),
  153. net_instance->connect_timeout, qdevice_model_net_timer_connect_timeout,
  154. (void *)net_instance, NULL);
  155. if (net_instance->connect_timer == NULL) {
  156. log(LOG_CRIT, "Can't schedule connect timer");
  157. return (-1);
  158. }
  159. log(LOG_DEBUG, "Trying connect to qnetd server %s:%u (timeout = %ums)",
  160. net_instance->host_addr, net_instance->host_port, net_instance->connect_timeout);
  161. res = nss_sock_non_blocking_client_init(net_instance->host_addr,
  162. net_instance->host_port, qdevice_model_net_get_af(net_instance),
  163. &net_instance->non_blocking_client);
  164. if (res == -1) {
  165. log_nss(LOG_ERR, "Can't initialize non blocking client connection");
  166. }
  167. res = nss_sock_non_blocking_client_try_next(&net_instance->non_blocking_client);
  168. if (res == -1) {
  169. log_nss(LOG_ERR, "Can't connect to qnetd host");
  170. /*
  171. * To prevent adding non_blocking_client into loop but still keep loop running
  172. */
  173. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  174. }
  175. res = qdevice_net_socket_add_to_main_poll_loop(net_instance);
  176. if (res == -1) {
  177. goto error_free_non_blocking_client;
  178. }
  179. res = pr_poll_loop_add_pre_poll_cb(&instance->main_poll_loop, check_schedule_disconnect_cb,
  180. net_instance, NULL);
  181. if (res == -1) {
  182. log(LOG_CRIT, "Can't add pre poll callback to main loop");
  183. goto error_del_from_main_poll_loop;
  184. }
  185. return (0);
  186. error_del_from_main_poll_loop:
  187. (void)qdevice_net_socket_del_from_main_poll_loop(net_instance);
  188. error_free_non_blocking_client:
  189. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  190. return (-1);
  191. }
  192. /*
  193. * 1 - Restart loop
  194. * 0 - End loop with no error
  195. * -1 - End loop with error -1
  196. */
  197. int
  198. qdevice_model_net_post_poll_loop(struct qdevice_instance *instance,
  199. enum qdevice_model_post_poll_loop_exit_reason exit_reason)
  200. {
  201. struct qdevice_net_instance *net_instance;
  202. int restart_loop;
  203. int ret_val;
  204. enum tlv_vote vote;
  205. int delay_before_reconnect;
  206. net_instance = instance->model_data;
  207. restart_loop = 1;
  208. ret_val = -1;
  209. if (net_instance->connect_timer != NULL) {
  210. timer_list_entry_delete(pr_poll_loop_get_timer_list(&instance->main_poll_loop),
  211. net_instance->connect_timer);
  212. net_instance->connect_timer = NULL;
  213. }
  214. if (net_instance->echo_request_timer != NULL) {
  215. timer_list_entry_delete(pr_poll_loop_get_timer_list(&instance->main_poll_loop),
  216. net_instance->echo_request_timer);
  217. net_instance->echo_request_timer = NULL;
  218. }
  219. /*
  220. * Map qdevice exit_reason to qdevice-net disconnect reason
  221. */
  222. switch (exit_reason) {
  223. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_MODEL:
  224. /*
  225. * Disconnect reason should be already set
  226. */
  227. break;
  228. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_VOTEQUORUM_CLOSED:
  229. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_COROSYNC_CONNECTION_CLOSED;
  230. break;
  231. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_CMAP_CLOSED:
  232. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_COROSYNC_CONNECTION_CLOSED;
  233. break;
  234. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_HEURISTICS_CLOSED:
  235. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_HEURISTICS_WORKER_CLOSED;
  236. break;
  237. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_IPC_SOCKET_CLOSED:
  238. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_LOCAL_SOCKET_CLOSED;
  239. break;
  240. /*
  241. * Default not handled intentionally. Compiler shows warning when new exit reason is added
  242. */
  243. }
  244. restart_loop = qdevice_net_disconnect_reason_try_reconnect(net_instance->disconnect_reason);
  245. /*
  246. * Unpause cast vote timer, because if it is paused we cannot remove tracking
  247. */
  248. qdevice_net_cast_vote_timer_set_paused(net_instance, 0);
  249. vote = TLV_VOTE_NO_CHANGE;
  250. if (qdevice_net_algorithm_disconnected(net_instance,
  251. net_instance->disconnect_reason, &restart_loop, &vote) != 0) {
  252. log(LOG_ERR, "Algorithm returned error, force exit");
  253. return (-1);
  254. } else {
  255. log(LOG_DEBUG, "Algorithm result vote is %s",
  256. tlv_vote_to_str(vote));
  257. }
  258. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  259. log(LOG_ERR, "qdevice_model_net_run fatal error. "
  260. " Can't update cast vote timer vote");
  261. }
  262. if (qdevice_net_disconnect_reason_force_disconnect(net_instance->disconnect_reason)) {
  263. restart_loop = 0;
  264. }
  265. /*
  266. * Return 0 only when local socket was closed -> regular exit
  267. */
  268. if (net_instance->disconnect_reason == QDEVICE_NET_DISCONNECT_REASON_LOCAL_SOCKET_CLOSED) {
  269. ret_val = 0;
  270. }
  271. /*
  272. * Remove pre poll cb
  273. */
  274. if (pr_poll_loop_del_pre_poll_cb(&instance->main_poll_loop, check_schedule_disconnect_cb) == -1) {
  275. log(LOG_ERR, "Can't delete pre poll callback from main loop");
  276. restart_loop = 0;
  277. ret_val = -1;
  278. }
  279. /*
  280. * Remove socket from loop
  281. */
  282. if (qdevice_net_socket_del_from_main_poll_loop(net_instance) == -1) {
  283. restart_loop = 0;
  284. ret_val = -1;
  285. }
  286. if (net_instance->socket != NULL) {
  287. if (PR_Close(net_instance->socket) != PR_SUCCESS) {
  288. log_nss(LOG_WARNING, "Unable to close connection");
  289. }
  290. net_instance->socket = NULL;
  291. }
  292. if (!net_instance->non_blocking_client.destroyed) {
  293. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  294. }
  295. if (net_instance->non_blocking_client.socket != NULL) {
  296. if (PR_Close(net_instance->non_blocking_client.socket) != PR_SUCCESS) {
  297. log_nss(LOG_WARNING, "Unable to close non-blocking client connection");
  298. }
  299. net_instance->non_blocking_client.socket = NULL;
  300. }
  301. if (restart_loop &&
  302. net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT) {
  303. /*
  304. * Give qnetd server a little time before reconnect.
  305. *
  306. * Coverity reports weak_crypto error on following line, what's not a problem
  307. * because cryptographically secure pseudorandom number generator is not needed.
  308. */
  309. delay_before_reconnect = random() %
  310. (int)(net_instance->cast_vote_timer_interval * 0.9);
  311. log(LOG_DEBUG, "Sleeping for %u ms before reconnect",
  312. delay_before_reconnect);
  313. (void)poll(NULL, 0, delay_before_reconnect);
  314. }
  315. qdevice_net_instance_clean(net_instance);
  316. if (restart_loop) {
  317. return (1);
  318. }
  319. return (ret_val);
  320. }
  321. /*
  322. * Called when cmap reload (or nodelist) was requested.
  323. *
  324. * nlist is node list
  325. * config_version is valid only if config_version_set != 0
  326. *
  327. * Should return 0 if processing should continue or -1 to call exit
  328. */
  329. int
  330. qdevice_model_net_config_node_list_changed(struct qdevice_instance *instance,
  331. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  332. {
  333. struct qdevice_net_instance *net_instance;
  334. int send_node_list;
  335. enum tlv_vote vote;
  336. net_instance = instance->model_data;
  337. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  338. /*
  339. * Nodelist changed, but connection to qnetd not initiated yet.
  340. */
  341. send_node_list = 0;
  342. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  343. vote = TLV_VOTE_NACK;
  344. } else {
  345. vote = TLV_VOTE_NO_CHANGE;
  346. }
  347. } else {
  348. send_node_list = 1;
  349. vote = TLV_VOTE_NO_CHANGE;
  350. }
  351. if (qdevice_net_algorithm_config_node_list_changed(net_instance, nlist, config_version_set,
  352. config_version, &send_node_list, &vote) != 0) {
  353. log(LOG_ERR, "Algorithm returned error, Disconnecting");
  354. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_CONFIG_NODE_LIST_CHANGED_ERR;
  355. net_instance->schedule_disconnect = 1;
  356. return (0);
  357. } else {
  358. log(LOG_DEBUG, "Algorithm decided to %s node list and result vote is %s",
  359. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  360. }
  361. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  362. log(LOG_CRIT, "qdevice_model_net_config_node_list_changed fatal error. "
  363. " Can't update cast vote timer vote");
  364. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  365. net_instance->schedule_disconnect = 1;
  366. return (0);
  367. }
  368. if (send_node_list) {
  369. if (qdevice_net_send_config_node_list(net_instance, nlist, config_version_set,
  370. config_version, 0) != 0) {
  371. net_instance->schedule_disconnect = 1;
  372. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  373. return (0);
  374. }
  375. }
  376. return (0);
  377. }
  378. /*
  379. * Called when cmap reload (or nodelist) was requested, but it was not possible to
  380. * get node list.
  381. *
  382. * Should return 0 if processing should continue or -1 to call exit
  383. */
  384. int
  385. qdevice_model_net_get_config_node_list_failed(struct qdevice_instance *instance)
  386. {
  387. struct qdevice_net_instance *net_instance;
  388. net_instance = instance->model_data;
  389. net_instance->schedule_disconnect = 1;
  390. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  391. return (0);
  392. }
  393. int
  394. qdevice_model_net_votequorum_quorum_notify(struct qdevice_instance *instance,
  395. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  396. {
  397. struct qdevice_net_instance *net_instance;
  398. int send_node_list;
  399. enum tlv_vote vote;
  400. net_instance = instance->model_data;
  401. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  402. /*
  403. * Nodelist changed, but connection to qnetd not initiated yet.
  404. */
  405. send_node_list = 0;
  406. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  407. vote = TLV_VOTE_NACK;
  408. } else {
  409. vote = TLV_VOTE_NO_CHANGE;
  410. }
  411. } else {
  412. send_node_list = 1;
  413. vote = TLV_VOTE_NO_CHANGE;
  414. }
  415. if (qdevice_net_algorithm_votequorum_quorum_notify(net_instance, quorate,
  416. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  417. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  418. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_QUORUM_NOTIFY_ERR;
  419. net_instance->schedule_disconnect = 1;
  420. return (0);
  421. } else {
  422. log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  423. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  424. }
  425. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  426. log(LOG_CRIT, "qdevice_model_net_votequorum_quorum_notify fatal error. "
  427. " Can't update cast vote timer vote");
  428. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  429. net_instance->schedule_disconnect = 1;
  430. return (0);
  431. }
  432. if (send_node_list) {
  433. if (qdevice_net_send_quorum_node_list(net_instance,
  434. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  435. node_list_entries, node_list) != 0) {
  436. /*
  437. * Fatal error -> schedule disconnect
  438. */
  439. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  440. net_instance->schedule_disconnect = 1;
  441. return (0);
  442. }
  443. }
  444. return (0);
  445. }
  446. int
  447. qdevice_model_net_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance,
  448. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[],
  449. enum qdevice_heuristics_exec_result heuristics_exec_result)
  450. {
  451. struct qdevice_net_instance *net_instance;
  452. struct tlv_ring_id tlv_rid;
  453. enum tlv_vote vote;
  454. enum tlv_heuristics heuristics;
  455. int send_node_list;
  456. net_instance = instance->model_data;
  457. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  458. heuristics = qdevice_net_heuristics_exec_result_to_tlv(heuristics_exec_result);
  459. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  460. /*
  461. * Nodelist changed, but connection to qnetd not initiated yet.
  462. */
  463. send_node_list = 0;
  464. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  465. vote = TLV_VOTE_NACK;
  466. } else {
  467. vote = TLV_VOTE_NO_CHANGE;
  468. }
  469. } else {
  470. send_node_list = 1;
  471. vote = TLV_VOTE_WAIT_FOR_REPLY;
  472. }
  473. if (qdevice_net_algorithm_votequorum_node_list_heuristics_notify(net_instance, &tlv_rid,
  474. node_list_entries, node_list, &send_node_list, &vote, &heuristics) != 0) {
  475. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  476. net_instance->disconnect_reason =
  477. QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_HEURISTICS_NOTIFY_ERR;
  478. net_instance->schedule_disconnect = 1;
  479. return (0);
  480. } else {
  481. log(LOG_DEBUG, "Algorithm decided to %s list, result vote is %s and heuristics is %s",
  482. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote),
  483. tlv_heuristics_to_str(heuristics));
  484. }
  485. if (send_node_list) {
  486. if (qdevice_net_send_membership_node_list(net_instance, &tlv_rid,
  487. node_list_entries, node_list, heuristics) != 0) {
  488. /*
  489. * Fatal error -> schedule disconnect
  490. */
  491. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  492. net_instance->schedule_disconnect = 1;
  493. return (0);
  494. }
  495. }
  496. /*
  497. * Unpause cast vote timer
  498. */
  499. qdevice_net_cast_vote_timer_set_paused(net_instance, 0);
  500. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  501. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  502. "Can't update cast vote timer");
  503. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  504. net_instance->schedule_disconnect = 1;
  505. return (0);
  506. }
  507. net_instance->latest_vq_heuristics_result = heuristics;
  508. net_instance->latest_heuristics_result = heuristics;
  509. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  510. return (0);
  511. }
  512. return (0);
  513. }
  514. int
  515. qdevice_model_net_votequorum_node_list_notify(struct qdevice_instance *instance,
  516. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  517. {
  518. struct qdevice_net_instance *net_instance;
  519. struct tlv_ring_id tlv_rid;
  520. enum tlv_vote vote;
  521. int pause_cast_vote_timer;
  522. net_instance = instance->model_data;
  523. /*
  524. * Stop regular heuristics till qdevice_model_net_votequorum_node_list_heuristics_notify
  525. * is called
  526. */
  527. if (qdevice_net_heuristics_stop_timer(net_instance) != 0) {
  528. return (0);
  529. }
  530. pause_cast_vote_timer = 1;
  531. vote = TLV_VOTE_NO_CHANGE;
  532. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  533. net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  534. /*
  535. * Nodelist changed and vote timer still votes ACK. It's needed to start voting
  536. * NACK.
  537. */
  538. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  539. vote = TLV_VOTE_NACK;
  540. }
  541. }
  542. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  543. if (qdevice_net_algorithm_votequorum_node_list_notify(net_instance, &tlv_rid,
  544. node_list_entries, node_list, &pause_cast_vote_timer, &vote) != 0) {
  545. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  546. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_NOTIFY_ERR;
  547. net_instance->schedule_disconnect = 1;
  548. return (0);
  549. } else {
  550. log(LOG_DEBUG, "Algorithm decided to %s cast vote timer and result vote is %s ",
  551. (pause_cast_vote_timer ? "pause" : "not pause"), tlv_vote_to_str(vote));
  552. }
  553. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  554. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  555. "Can't update cast vote timer");
  556. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  557. net_instance->schedule_disconnect = 1;
  558. return (0);
  559. }
  560. qdevice_net_cast_vote_timer_set_paused(net_instance, pause_cast_vote_timer);
  561. return (0);
  562. }
  563. int
  564. qdevice_model_net_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  565. uint32_t expected_votes)
  566. {
  567. struct qdevice_net_instance *net_instance;
  568. enum tlv_vote vote;
  569. net_instance = instance->model_data;
  570. log(LOG_DEBUG, "qdevice_model_net_votequorum_expected_votes_notify"
  571. " (expected votes old=%"PRIu32" / new=%"PRIu32")",
  572. net_instance->qdevice_instance_ptr->vq_expected_votes, expected_votes);
  573. vote = TLV_VOTE_NO_CHANGE;
  574. if (qdevice_net_algorithm_votequorum_expected_votes_notify(net_instance, expected_votes,
  575. &vote) != 0) {
  576. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  577. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_EXPECTED_VOTES_NOTIFY_ERR;
  578. net_instance->schedule_disconnect = 1;
  579. return (0);
  580. } else {
  581. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(vote));
  582. }
  583. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  584. log(LOG_CRIT, "qdevice_model_net_votequorum_expected_votes_notify fatal error. "
  585. " Can't update cast vote timer vote");
  586. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  587. net_instance->schedule_disconnect = 1;
  588. return (0);
  589. }
  590. return (0);
  591. }
  592. int
  593. qdevice_model_net_cmap_changed(struct qdevice_instance *instance,
  594. const struct qdevice_cmap_change_events *events)
  595. {
  596. struct qdevice_net_instance *net_instance;
  597. enum qdevice_heuristics_mode active_heuristics_mode;
  598. int heuristics_enabled;
  599. net_instance = instance->model_data;
  600. if (events->heuristics) {
  601. active_heuristics_mode = instance->heuristics_instance.mode;
  602. heuristics_enabled = (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  603. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC);
  604. if (net_instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  605. !net_instance->server_supports_heuristics && heuristics_enabled) {
  606. log(LOG_ERR, "Heuristics are enabled but not supported by the server");
  607. net_instance->disconnect_reason =
  608. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_OPT;
  609. net_instance->schedule_disconnect = 1;
  610. return (0);
  611. }
  612. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  613. return (0);
  614. }
  615. }
  616. return (0);
  617. }
  618. int
  619. qdevice_model_net_ipc_cmd_status(struct qdevice_instance *instance,
  620. struct dynar *outbuf, int verbose)
  621. {
  622. struct qdevice_net_instance *net_instance;
  623. net_instance = instance->model_data;
  624. if (!qdevice_net_ipc_cmd_status(net_instance, outbuf, verbose)) {
  625. return (-1);
  626. }
  627. return (0);
  628. }
  629. static struct qdevice_model qdevice_model_net = {
  630. .name = "net",
  631. .init = qdevice_model_net_init,
  632. .destroy = qdevice_model_net_destroy,
  633. .pre_poll_loop = qdevice_model_net_pre_poll_loop,
  634. .post_poll_loop = qdevice_model_net_post_poll_loop,
  635. .get_config_node_list_failed = qdevice_model_net_get_config_node_list_failed,
  636. .config_node_list_changed = qdevice_model_net_config_node_list_changed,
  637. .votequorum_quorum_notify = qdevice_model_net_votequorum_quorum_notify,
  638. .votequorum_node_list_notify = qdevice_model_net_votequorum_node_list_notify,
  639. .votequorum_node_list_heuristics_notify = qdevice_model_net_votequorum_node_list_heuristics_notify,
  640. .votequorum_expected_votes_notify = qdevice_model_net_votequorum_expected_votes_notify,
  641. .cmap_changed = qdevice_model_net_cmap_changed,
  642. .ipc_cmd_status = qdevice_model_net_ipc_cmd_status,
  643. };
  644. int
  645. qdevice_model_net_register(void)
  646. {
  647. return (qdevice_model_register(QDEVICE_MODEL_TYPE_NET, &qdevice_model_net));
  648. }