qdevice-model-net.c 21 KB

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