qdevice-model-net.c 21 KB

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