qdevice-model-net.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. res = qdevice_net_socket_add_to_main_poll_loop(net_instance);
  172. if (res == -1) {
  173. goto error_free_non_blocking_client;
  174. }
  175. res = pr_poll_loop_add_pre_poll_cb(&instance->main_poll_loop, check_schedule_disconnect_cb,
  176. net_instance, NULL);
  177. if (res == -1) {
  178. log(LOG_CRIT, "Can't add pre poll callback to main loop");
  179. goto error_del_from_main_poll_loop;
  180. }
  181. return (0);
  182. error_del_from_main_poll_loop:
  183. (void)qdevice_net_socket_del_from_main_poll_loop(net_instance);
  184. error_free_non_blocking_client:
  185. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  186. return (-1);
  187. }
  188. /*
  189. * 1 - Restart loop
  190. * 0 - End loop with no error
  191. * -1 - End loop with error -1
  192. */
  193. int
  194. qdevice_model_net_post_poll_loop(struct qdevice_instance *instance,
  195. enum qdevice_model_post_poll_loop_exit_reason exit_reason)
  196. {
  197. struct qdevice_net_instance *net_instance;
  198. int restart_loop;
  199. int ret_val;
  200. enum tlv_vote vote;
  201. int delay_before_reconnect;
  202. net_instance = instance->model_data;
  203. restart_loop = 1;
  204. ret_val = -1;
  205. if (net_instance->connect_timer != NULL) {
  206. timer_list_entry_delete(pr_poll_loop_get_timer_list(&instance->main_poll_loop),
  207. net_instance->connect_timer);
  208. net_instance->connect_timer = NULL;
  209. }
  210. if (net_instance->echo_request_timer != NULL) {
  211. timer_list_entry_delete(pr_poll_loop_get_timer_list(&instance->main_poll_loop),
  212. net_instance->echo_request_timer);
  213. net_instance->echo_request_timer = NULL;
  214. }
  215. /*
  216. * Map qdevice exit_reason to qdevice-net disconnect reason
  217. */
  218. switch (exit_reason) {
  219. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_MODEL:
  220. /*
  221. * Disconnect reason should be already set
  222. */
  223. break;
  224. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_VOTEQUORUM_CLOSED:
  225. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_COROSYNC_CONNECTION_CLOSED;
  226. break;
  227. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_CMAP_CLOSED:
  228. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_COROSYNC_CONNECTION_CLOSED;
  229. break;
  230. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_HEURISTICS_CLOSED:
  231. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_HEURISTICS_WORKER_CLOSED;
  232. break;
  233. case QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_IPC_SOCKET_CLOSED:
  234. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_LOCAL_SOCKET_CLOSED;
  235. break;
  236. /*
  237. * Default not handled intentionally. Compiler shows warning when new exit reason is added
  238. */
  239. }
  240. restart_loop = qdevice_net_disconnect_reason_try_reconnect(net_instance->disconnect_reason);
  241. /*
  242. * Unpause cast vote timer, because if it is paused we cannot remove tracking
  243. */
  244. qdevice_net_cast_vote_timer_set_paused(net_instance, 0);
  245. vote = TLV_VOTE_NO_CHANGE;
  246. if (qdevice_net_algorithm_disconnected(net_instance,
  247. net_instance->disconnect_reason, &restart_loop, &vote) != 0) {
  248. log(LOG_ERR, "Algorithm returned error, force exit");
  249. return (-1);
  250. } else {
  251. log(LOG_DEBUG, "Algorithm result vote is %s",
  252. tlv_vote_to_str(vote));
  253. }
  254. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  255. log(LOG_ERR, "qdevice_model_net_run fatal error. "
  256. " Can't update cast vote timer vote");
  257. }
  258. if (qdevice_net_disconnect_reason_force_disconnect(net_instance->disconnect_reason)) {
  259. restart_loop = 0;
  260. }
  261. /*
  262. * Return 0 only when local socket was closed -> regular exit
  263. */
  264. if (net_instance->disconnect_reason == QDEVICE_NET_DISCONNECT_REASON_LOCAL_SOCKET_CLOSED) {
  265. ret_val = 0;
  266. }
  267. /*
  268. * Remove pre poll cb
  269. */
  270. if (pr_poll_loop_del_pre_poll_cb(&instance->main_poll_loop, check_schedule_disconnect_cb) == -1) {
  271. log(LOG_ERR, "Can't delete pre poll callback from main loop");
  272. restart_loop = 0;
  273. ret_val = -1;
  274. }
  275. /*
  276. * Remove socket from loop
  277. */
  278. if (qdevice_net_socket_del_from_main_poll_loop(net_instance) == -1) {
  279. restart_loop = 0;
  280. ret_val = -1;
  281. }
  282. if (net_instance->socket != NULL) {
  283. if (PR_Close(net_instance->socket) != PR_SUCCESS) {
  284. log_nss(LOG_WARNING, "Unable to close connection");
  285. }
  286. net_instance->socket = NULL;
  287. }
  288. if (!net_instance->non_blocking_client.destroyed) {
  289. nss_sock_non_blocking_client_destroy(&net_instance->non_blocking_client);
  290. }
  291. if (net_instance->non_blocking_client.socket != NULL) {
  292. if (PR_Close(net_instance->non_blocking_client.socket) != PR_SUCCESS) {
  293. log_nss(LOG_WARNING, "Unable to close non-blocking client connection");
  294. }
  295. net_instance->non_blocking_client.socket = NULL;
  296. }
  297. if (restart_loop &&
  298. net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT) {
  299. /*
  300. * Give qnetd server a little time before reconnect
  301. */
  302. delay_before_reconnect = random() %
  303. (int)(net_instance->cast_vote_timer_interval * 0.9);
  304. log(LOG_DEBUG, "Sleeping for %u ms before reconnect",
  305. delay_before_reconnect);
  306. (void)poll(NULL, 0, delay_before_reconnect);
  307. }
  308. qdevice_net_instance_clean(net_instance);
  309. if (restart_loop) {
  310. return (1);
  311. }
  312. return (ret_val);
  313. }
  314. /*
  315. * Called when cmap reload (or nodelist) was requested.
  316. *
  317. * nlist is node list
  318. * config_version is valid only if config_version_set != 0
  319. *
  320. * Should return 0 if processing should continue or -1 to call exit
  321. */
  322. int
  323. qdevice_model_net_config_node_list_changed(struct qdevice_instance *instance,
  324. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  325. {
  326. struct qdevice_net_instance *net_instance;
  327. int send_node_list;
  328. enum tlv_vote vote;
  329. net_instance = instance->model_data;
  330. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  331. /*
  332. * Nodelist changed, but connection to qnetd not initiated yet.
  333. */
  334. send_node_list = 0;
  335. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  336. vote = TLV_VOTE_NACK;
  337. } else {
  338. vote = TLV_VOTE_NO_CHANGE;
  339. }
  340. } else {
  341. send_node_list = 1;
  342. vote = TLV_VOTE_NO_CHANGE;
  343. }
  344. if (qdevice_net_algorithm_config_node_list_changed(net_instance, nlist, config_version_set,
  345. config_version, &send_node_list, &vote) != 0) {
  346. log(LOG_ERR, "Algorithm returned error, Disconnecting");
  347. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_CONFIG_NODE_LIST_CHANGED_ERR;
  348. net_instance->schedule_disconnect = 1;
  349. return (0);
  350. } else {
  351. log(LOG_DEBUG, "Algorithm decided to %s node list and result vote is %s",
  352. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  353. }
  354. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  355. log(LOG_CRIT, "qdevice_model_net_config_node_list_changed fatal error. "
  356. " Can't update cast vote timer vote");
  357. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  358. net_instance->schedule_disconnect = 1;
  359. return (0);
  360. }
  361. if (send_node_list) {
  362. if (qdevice_net_send_config_node_list(net_instance, nlist, config_version_set,
  363. config_version, 0) != 0) {
  364. net_instance->schedule_disconnect = 1;
  365. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  366. return (0);
  367. }
  368. }
  369. return (0);
  370. }
  371. /*
  372. * Called when cmap reload (or nodelist) was requested, but it was not possible to
  373. * get node list.
  374. *
  375. * Should return 0 if processing should continue or -1 to call exit
  376. */
  377. int
  378. qdevice_model_net_get_config_node_list_failed(struct qdevice_instance *instance)
  379. {
  380. struct qdevice_net_instance *net_instance;
  381. net_instance = instance->model_data;
  382. net_instance->schedule_disconnect = 1;
  383. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  384. return (0);
  385. }
  386. int
  387. qdevice_model_net_votequorum_quorum_notify(struct qdevice_instance *instance,
  388. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  389. {
  390. struct qdevice_net_instance *net_instance;
  391. int send_node_list;
  392. enum tlv_vote vote;
  393. net_instance = instance->model_data;
  394. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  395. /*
  396. * Nodelist changed, but connection to qnetd not initiated yet.
  397. */
  398. send_node_list = 0;
  399. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  400. vote = TLV_VOTE_NACK;
  401. } else {
  402. vote = TLV_VOTE_NO_CHANGE;
  403. }
  404. } else {
  405. send_node_list = 1;
  406. vote = TLV_VOTE_NO_CHANGE;
  407. }
  408. if (qdevice_net_algorithm_votequorum_quorum_notify(net_instance, quorate,
  409. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  410. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  411. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_QUORUM_NOTIFY_ERR;
  412. net_instance->schedule_disconnect = 1;
  413. return (0);
  414. } else {
  415. log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  416. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  417. }
  418. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  419. log(LOG_CRIT, "qdevice_model_net_votequorum_quorum_notify fatal error. "
  420. " Can't update cast vote timer vote");
  421. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  422. net_instance->schedule_disconnect = 1;
  423. return (0);
  424. }
  425. if (send_node_list) {
  426. if (qdevice_net_send_quorum_node_list(net_instance,
  427. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  428. node_list_entries, node_list) != 0) {
  429. /*
  430. * Fatal error -> schedule disconnect
  431. */
  432. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  433. net_instance->schedule_disconnect = 1;
  434. return (0);
  435. }
  436. }
  437. return (0);
  438. }
  439. int
  440. qdevice_model_net_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance,
  441. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[],
  442. enum qdevice_heuristics_exec_result heuristics_exec_result)
  443. {
  444. struct qdevice_net_instance *net_instance;
  445. struct tlv_ring_id tlv_rid;
  446. enum tlv_vote vote;
  447. enum tlv_heuristics heuristics;
  448. int send_node_list;
  449. net_instance = instance->model_data;
  450. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  451. heuristics = qdevice_net_heuristics_exec_result_to_tlv(heuristics_exec_result);
  452. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  453. /*
  454. * Nodelist changed, but connection to qnetd not initiated yet.
  455. */
  456. send_node_list = 0;
  457. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  458. vote = TLV_VOTE_NACK;
  459. } else {
  460. vote = TLV_VOTE_NO_CHANGE;
  461. }
  462. } else {
  463. send_node_list = 1;
  464. vote = TLV_VOTE_WAIT_FOR_REPLY;
  465. }
  466. if (qdevice_net_algorithm_votequorum_node_list_heuristics_notify(net_instance, &tlv_rid,
  467. node_list_entries, node_list, &send_node_list, &vote, &heuristics) != 0) {
  468. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  469. net_instance->disconnect_reason =
  470. QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_HEURISTICS_NOTIFY_ERR;
  471. net_instance->schedule_disconnect = 1;
  472. return (0);
  473. } else {
  474. log(LOG_DEBUG, "Algorithm decided to %s list, result vote is %s and heuristics is %s",
  475. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote),
  476. tlv_heuristics_to_str(heuristics));
  477. }
  478. if (send_node_list) {
  479. if (qdevice_net_send_membership_node_list(net_instance, &tlv_rid,
  480. node_list_entries, node_list, heuristics) != 0) {
  481. /*
  482. * Fatal error -> schedule disconnect
  483. */
  484. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  485. net_instance->schedule_disconnect = 1;
  486. return (0);
  487. }
  488. }
  489. /*
  490. * Unpause cast vote timer
  491. */
  492. qdevice_net_cast_vote_timer_set_paused(net_instance, 0);
  493. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  494. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  495. "Can't update cast vote timer");
  496. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  497. net_instance->schedule_disconnect = 1;
  498. return (0);
  499. }
  500. net_instance->latest_vq_heuristics_result = heuristics;
  501. net_instance->latest_heuristics_result = heuristics;
  502. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  503. return (0);
  504. }
  505. return (0);
  506. }
  507. int
  508. qdevice_model_net_votequorum_node_list_notify(struct qdevice_instance *instance,
  509. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  510. {
  511. struct qdevice_net_instance *net_instance;
  512. struct tlv_ring_id tlv_rid;
  513. enum tlv_vote vote;
  514. int pause_cast_vote_timer;
  515. net_instance = instance->model_data;
  516. /*
  517. * Stop regular heuristics till qdevice_model_net_votequorum_node_list_heuristics_notify
  518. * is called
  519. */
  520. if (qdevice_net_heuristics_stop_timer(net_instance) != 0) {
  521. return (0);
  522. }
  523. pause_cast_vote_timer = 1;
  524. vote = TLV_VOTE_NO_CHANGE;
  525. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  526. net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  527. /*
  528. * Nodelist changed and vote timer still votes ACK. It's needed to start voting
  529. * NACK.
  530. */
  531. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  532. vote = TLV_VOTE_NACK;
  533. }
  534. }
  535. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  536. if (qdevice_net_algorithm_votequorum_node_list_notify(net_instance, &tlv_rid,
  537. node_list_entries, node_list, &pause_cast_vote_timer, &vote) != 0) {
  538. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  539. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_NOTIFY_ERR;
  540. net_instance->schedule_disconnect = 1;
  541. return (0);
  542. } else {
  543. log(LOG_DEBUG, "Algorithm decided to %s cast vote timer and result vote is %s ",
  544. (pause_cast_vote_timer ? "pause" : "not pause"), tlv_vote_to_str(vote));
  545. }
  546. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  547. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  548. "Can't update cast vote timer");
  549. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  550. net_instance->schedule_disconnect = 1;
  551. return (0);
  552. }
  553. qdevice_net_cast_vote_timer_set_paused(net_instance, pause_cast_vote_timer);
  554. return (0);
  555. }
  556. int
  557. qdevice_model_net_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  558. uint32_t expected_votes)
  559. {
  560. struct qdevice_net_instance *net_instance;
  561. enum tlv_vote vote;
  562. net_instance = instance->model_data;
  563. log(LOG_DEBUG, "qdevice_model_net_votequorum_expected_votes_notify"
  564. " (expected votes old=%"PRIu32" / new=%"PRIu32")",
  565. net_instance->qdevice_instance_ptr->vq_expected_votes, expected_votes);
  566. vote = TLV_VOTE_NO_CHANGE;
  567. if (qdevice_net_algorithm_votequorum_expected_votes_notify(net_instance, expected_votes,
  568. &vote) != 0) {
  569. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  570. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_EXPECTED_VOTES_NOTIFY_ERR;
  571. net_instance->schedule_disconnect = 1;
  572. return (0);
  573. } else {
  574. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(vote));
  575. }
  576. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  577. log(LOG_CRIT, "qdevice_model_net_votequorum_expected_votes_notify fatal error. "
  578. " Can't update cast vote timer vote");
  579. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  580. net_instance->schedule_disconnect = 1;
  581. return (0);
  582. }
  583. return (0);
  584. }
  585. int
  586. qdevice_model_net_cmap_changed(struct qdevice_instance *instance,
  587. const struct qdevice_cmap_change_events *events)
  588. {
  589. struct qdevice_net_instance *net_instance;
  590. enum qdevice_heuristics_mode active_heuristics_mode;
  591. int heuristics_enabled;
  592. net_instance = instance->model_data;
  593. if (events->heuristics) {
  594. active_heuristics_mode = instance->heuristics_instance.mode;
  595. heuristics_enabled = (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  596. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC);
  597. if (net_instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  598. !net_instance->server_supports_heuristics && heuristics_enabled) {
  599. log(LOG_ERR, "Heuristics are enabled but not supported by the server");
  600. net_instance->disconnect_reason =
  601. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_OPT;
  602. net_instance->schedule_disconnect = 1;
  603. return (0);
  604. }
  605. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  606. return (0);
  607. }
  608. }
  609. return (0);
  610. }
  611. int
  612. qdevice_model_net_ipc_cmd_status(struct qdevice_instance *instance,
  613. struct dynar *outbuf, int verbose)
  614. {
  615. struct qdevice_net_instance *net_instance;
  616. net_instance = instance->model_data;
  617. if (!qdevice_net_ipc_cmd_status(net_instance, outbuf, verbose)) {
  618. return (-1);
  619. }
  620. return (0);
  621. }
  622. static struct qdevice_model qdevice_model_net = {
  623. .name = "net",
  624. .init = qdevice_model_net_init,
  625. .destroy = qdevice_model_net_destroy,
  626. .pre_poll_loop = qdevice_model_net_pre_poll_loop,
  627. .post_poll_loop = qdevice_model_net_post_poll_loop,
  628. .get_config_node_list_failed = qdevice_model_net_get_config_node_list_failed,
  629. .config_node_list_changed = qdevice_model_net_config_node_list_changed,
  630. .votequorum_quorum_notify = qdevice_model_net_votequorum_quorum_notify,
  631. .votequorum_node_list_notify = qdevice_model_net_votequorum_node_list_notify,
  632. .votequorum_node_list_heuristics_notify = qdevice_model_net_votequorum_node_list_heuristics_notify,
  633. .votequorum_expected_votes_notify = qdevice_model_net_votequorum_expected_votes_notify,
  634. .cmap_changed = qdevice_model_net_cmap_changed,
  635. .ipc_cmd_status = qdevice_model_net_ipc_cmd_status,
  636. };
  637. int
  638. qdevice_model_net_register(void)
  639. {
  640. return (qdevice_model_register(QDEVICE_MODEL_TYPE_NET, &qdevice_model_net));
  641. }