qdevice-model-net.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. delay_before_reconnect = random() %
  307. (int)(net_instance->cast_vote_timer_interval * 0.9);
  308. log(LOG_DEBUG, "Sleeping for %u ms before reconnect",
  309. delay_before_reconnect);
  310. (void)poll(NULL, 0, delay_before_reconnect);
  311. }
  312. qdevice_net_instance_clean(net_instance);
  313. if (restart_loop) {
  314. return (1);
  315. }
  316. return (ret_val);
  317. }
  318. /*
  319. * Called when cmap reload (or nodelist) was requested.
  320. *
  321. * nlist is node list
  322. * config_version is valid only if config_version_set != 0
  323. *
  324. * Should return 0 if processing should continue or -1 to call exit
  325. */
  326. int
  327. qdevice_model_net_config_node_list_changed(struct qdevice_instance *instance,
  328. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  329. {
  330. struct qdevice_net_instance *net_instance;
  331. int send_node_list;
  332. enum tlv_vote vote;
  333. net_instance = instance->model_data;
  334. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  335. /*
  336. * Nodelist changed, but connection to qnetd not initiated yet.
  337. */
  338. send_node_list = 0;
  339. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  340. vote = TLV_VOTE_NACK;
  341. } else {
  342. vote = TLV_VOTE_NO_CHANGE;
  343. }
  344. } else {
  345. send_node_list = 1;
  346. vote = TLV_VOTE_NO_CHANGE;
  347. }
  348. if (qdevice_net_algorithm_config_node_list_changed(net_instance, nlist, config_version_set,
  349. config_version, &send_node_list, &vote) != 0) {
  350. log(LOG_ERR, "Algorithm returned error, Disconnecting");
  351. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_CONFIG_NODE_LIST_CHANGED_ERR;
  352. net_instance->schedule_disconnect = 1;
  353. return (0);
  354. } else {
  355. log(LOG_DEBUG, "Algorithm decided to %s node list and result vote is %s",
  356. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  357. }
  358. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  359. log(LOG_CRIT, "qdevice_model_net_config_node_list_changed fatal error. "
  360. " Can't update cast vote timer vote");
  361. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  362. net_instance->schedule_disconnect = 1;
  363. return (0);
  364. }
  365. if (send_node_list) {
  366. if (qdevice_net_send_config_node_list(net_instance, nlist, config_version_set,
  367. config_version, 0) != 0) {
  368. net_instance->schedule_disconnect = 1;
  369. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  370. return (0);
  371. }
  372. }
  373. return (0);
  374. }
  375. /*
  376. * Called when cmap reload (or nodelist) was requested, but it was not possible to
  377. * get node list.
  378. *
  379. * Should return 0 if processing should continue or -1 to call exit
  380. */
  381. int
  382. qdevice_model_net_get_config_node_list_failed(struct qdevice_instance *instance)
  383. {
  384. struct qdevice_net_instance *net_instance;
  385. net_instance = instance->model_data;
  386. net_instance->schedule_disconnect = 1;
  387. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  388. return (0);
  389. }
  390. int
  391. qdevice_model_net_votequorum_quorum_notify(struct qdevice_instance *instance,
  392. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  393. {
  394. struct qdevice_net_instance *net_instance;
  395. int send_node_list;
  396. enum tlv_vote vote;
  397. net_instance = instance->model_data;
  398. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  399. /*
  400. * Nodelist changed, but connection to qnetd not initiated yet.
  401. */
  402. send_node_list = 0;
  403. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  404. vote = TLV_VOTE_NACK;
  405. } else {
  406. vote = TLV_VOTE_NO_CHANGE;
  407. }
  408. } else {
  409. send_node_list = 1;
  410. vote = TLV_VOTE_NO_CHANGE;
  411. }
  412. if (qdevice_net_algorithm_votequorum_quorum_notify(net_instance, quorate,
  413. node_list_entries, node_list, &send_node_list, &vote) != 0) {
  414. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  415. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_QUORUM_NOTIFY_ERR;
  416. net_instance->schedule_disconnect = 1;
  417. return (0);
  418. } else {
  419. log(LOG_DEBUG, "Algorithm decided to %s list and result vote is %s",
  420. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote));
  421. }
  422. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  423. log(LOG_CRIT, "qdevice_model_net_votequorum_quorum_notify fatal error. "
  424. " Can't update cast vote timer vote");
  425. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  426. net_instance->schedule_disconnect = 1;
  427. return (0);
  428. }
  429. if (send_node_list) {
  430. if (qdevice_net_send_quorum_node_list(net_instance,
  431. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  432. node_list_entries, node_list) != 0) {
  433. /*
  434. * Fatal error -> schedule disconnect
  435. */
  436. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  437. net_instance->schedule_disconnect = 1;
  438. return (0);
  439. }
  440. }
  441. return (0);
  442. }
  443. int
  444. qdevice_model_net_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance,
  445. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[],
  446. enum qdevice_heuristics_exec_result heuristics_exec_result)
  447. {
  448. struct qdevice_net_instance *net_instance;
  449. struct tlv_ring_id tlv_rid;
  450. enum tlv_vote vote;
  451. enum tlv_heuristics heuristics;
  452. int send_node_list;
  453. net_instance = instance->model_data;
  454. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  455. heuristics = qdevice_net_heuristics_exec_result_to_tlv(heuristics_exec_result);
  456. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  457. /*
  458. * Nodelist changed, but connection to qnetd not initiated yet.
  459. */
  460. send_node_list = 0;
  461. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  462. vote = TLV_VOTE_NACK;
  463. } else {
  464. vote = TLV_VOTE_NO_CHANGE;
  465. }
  466. } else {
  467. send_node_list = 1;
  468. vote = TLV_VOTE_WAIT_FOR_REPLY;
  469. }
  470. if (qdevice_net_algorithm_votequorum_node_list_heuristics_notify(net_instance, &tlv_rid,
  471. node_list_entries, node_list, &send_node_list, &vote, &heuristics) != 0) {
  472. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  473. net_instance->disconnect_reason =
  474. QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_HEURISTICS_NOTIFY_ERR;
  475. net_instance->schedule_disconnect = 1;
  476. return (0);
  477. } else {
  478. log(LOG_DEBUG, "Algorithm decided to %s list, result vote is %s and heuristics is %s",
  479. (send_node_list ? "send" : "not send"), tlv_vote_to_str(vote),
  480. tlv_heuristics_to_str(heuristics));
  481. }
  482. if (send_node_list) {
  483. if (qdevice_net_send_membership_node_list(net_instance, &tlv_rid,
  484. node_list_entries, node_list, heuristics) != 0) {
  485. /*
  486. * Fatal error -> schedule disconnect
  487. */
  488. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  489. net_instance->schedule_disconnect = 1;
  490. return (0);
  491. }
  492. }
  493. /*
  494. * Unpause cast vote timer
  495. */
  496. qdevice_net_cast_vote_timer_set_paused(net_instance, 0);
  497. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  498. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  499. "Can't update cast vote timer");
  500. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  501. net_instance->schedule_disconnect = 1;
  502. return (0);
  503. }
  504. net_instance->latest_vq_heuristics_result = heuristics;
  505. net_instance->latest_heuristics_result = heuristics;
  506. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  507. return (0);
  508. }
  509. return (0);
  510. }
  511. int
  512. qdevice_model_net_votequorum_node_list_notify(struct qdevice_instance *instance,
  513. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  514. {
  515. struct qdevice_net_instance *net_instance;
  516. struct tlv_ring_id tlv_rid;
  517. enum tlv_vote vote;
  518. int pause_cast_vote_timer;
  519. net_instance = instance->model_data;
  520. /*
  521. * Stop regular heuristics till qdevice_model_net_votequorum_node_list_heuristics_notify
  522. * is called
  523. */
  524. if (qdevice_net_heuristics_stop_timer(net_instance) != 0) {
  525. return (0);
  526. }
  527. pause_cast_vote_timer = 1;
  528. vote = TLV_VOTE_NO_CHANGE;
  529. if (net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  530. net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  531. /*
  532. * Nodelist changed and vote timer still votes ACK. It's needed to start voting
  533. * NACK.
  534. */
  535. if (net_instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  536. vote = TLV_VOTE_NACK;
  537. }
  538. }
  539. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, &votequorum_ring_id);
  540. if (qdevice_net_algorithm_votequorum_node_list_notify(net_instance, &tlv_rid,
  541. node_list_entries, node_list, &pause_cast_vote_timer, &vote) != 0) {
  542. log(LOG_ERR, "Algorithm returned error. Disconnecting.");
  543. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_NODE_LIST_NOTIFY_ERR;
  544. net_instance->schedule_disconnect = 1;
  545. return (0);
  546. } else {
  547. log(LOG_DEBUG, "Algorithm decided to %s cast vote timer and result vote is %s ",
  548. (pause_cast_vote_timer ? "pause" : "not pause"), tlv_vote_to_str(vote));
  549. }
  550. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  551. log(LOG_CRIT, "qdevice_model_net_votequorum_node_list_notify fatal error "
  552. "Can't update cast vote timer");
  553. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  554. net_instance->schedule_disconnect = 1;
  555. return (0);
  556. }
  557. qdevice_net_cast_vote_timer_set_paused(net_instance, pause_cast_vote_timer);
  558. return (0);
  559. }
  560. int
  561. qdevice_model_net_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  562. uint32_t expected_votes)
  563. {
  564. struct qdevice_net_instance *net_instance;
  565. enum tlv_vote vote;
  566. net_instance = instance->model_data;
  567. log(LOG_DEBUG, "qdevice_model_net_votequorum_expected_votes_notify"
  568. " (expected votes old=%"PRIu32" / new=%"PRIu32")",
  569. net_instance->qdevice_instance_ptr->vq_expected_votes, expected_votes);
  570. vote = TLV_VOTE_NO_CHANGE;
  571. if (qdevice_net_algorithm_votequorum_expected_votes_notify(net_instance, expected_votes,
  572. &vote) != 0) {
  573. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  574. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTEQUORUM_EXPECTED_VOTES_NOTIFY_ERR;
  575. net_instance->schedule_disconnect = 1;
  576. return (0);
  577. } else {
  578. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(vote));
  579. }
  580. if (qdevice_net_cast_vote_timer_update(net_instance, vote) != 0) {
  581. log(LOG_CRIT, "qdevice_model_net_votequorum_expected_votes_notify fatal error. "
  582. " Can't update cast vote timer vote");
  583. net_instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  584. net_instance->schedule_disconnect = 1;
  585. return (0);
  586. }
  587. return (0);
  588. }
  589. int
  590. qdevice_model_net_cmap_changed(struct qdevice_instance *instance,
  591. const struct qdevice_cmap_change_events *events)
  592. {
  593. struct qdevice_net_instance *net_instance;
  594. enum qdevice_heuristics_mode active_heuristics_mode;
  595. int heuristics_enabled;
  596. net_instance = instance->model_data;
  597. if (events->heuristics) {
  598. active_heuristics_mode = instance->heuristics_instance.mode;
  599. heuristics_enabled = (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  600. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC);
  601. if (net_instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS &&
  602. !net_instance->server_supports_heuristics && heuristics_enabled) {
  603. log(LOG_ERR, "Heuristics are enabled but not supported by the server");
  604. net_instance->disconnect_reason =
  605. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_OPT;
  606. net_instance->schedule_disconnect = 1;
  607. return (0);
  608. }
  609. if (qdevice_net_heuristics_schedule_timer(net_instance) != 0) {
  610. return (0);
  611. }
  612. }
  613. return (0);
  614. }
  615. int
  616. qdevice_model_net_ipc_cmd_status(struct qdevice_instance *instance,
  617. struct dynar *outbuf, int verbose)
  618. {
  619. struct qdevice_net_instance *net_instance;
  620. net_instance = instance->model_data;
  621. if (!qdevice_net_ipc_cmd_status(net_instance, outbuf, verbose)) {
  622. return (-1);
  623. }
  624. return (0);
  625. }
  626. static struct qdevice_model qdevice_model_net = {
  627. .name = "net",
  628. .init = qdevice_model_net_init,
  629. .destroy = qdevice_model_net_destroy,
  630. .pre_poll_loop = qdevice_model_net_pre_poll_loop,
  631. .post_poll_loop = qdevice_model_net_post_poll_loop,
  632. .get_config_node_list_failed = qdevice_model_net_get_config_node_list_failed,
  633. .config_node_list_changed = qdevice_model_net_config_node_list_changed,
  634. .votequorum_quorum_notify = qdevice_model_net_votequorum_quorum_notify,
  635. .votequorum_node_list_notify = qdevice_model_net_votequorum_node_list_notify,
  636. .votequorum_node_list_heuristics_notify = qdevice_model_net_votequorum_node_list_heuristics_notify,
  637. .votequorum_expected_votes_notify = qdevice_model_net_votequorum_expected_votes_notify,
  638. .cmap_changed = qdevice_model_net_cmap_changed,
  639. .ipc_cmd_status = qdevice_model_net_ipc_cmd_status,
  640. };
  641. int
  642. qdevice_model_net_register(void)
  643. {
  644. return (qdevice_model_register(QDEVICE_MODEL_TYPE_NET, &qdevice_model_net));
  645. }