qdevice-net-ipc-cmd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "log.h"
  35. #include "qdevice-net-ipc-cmd.h"
  36. #include "dynar-str.h"
  37. #include "qdevice-net-algorithm.h"
  38. #include "utils.h"
  39. static int
  40. qdevice_net_ipc_cmd_status_add_header(struct dynar *outbuf, int verbose)
  41. {
  42. return ((dynar_str_catf(outbuf, "Qdevice-net information\n") != -1) &&
  43. (dynar_str_catf(outbuf, "----------------------\n") != -1));
  44. }
  45. static int
  46. qdevice_net_ipc_cmd_status_add_tie_breaker(struct qdevice_net_instance *instance,
  47. struct dynar *outbuf, int verbose)
  48. {
  49. if (dynar_str_catf(outbuf, "Tie-breaker:\t\t") == -1) {
  50. return (0);
  51. }
  52. switch (instance->tie_breaker.mode) {
  53. case TLV_TIE_BREAKER_MODE_LOWEST:
  54. if (dynar_str_catf(outbuf, "Node with lowest node ID") == -1) {
  55. return (0);
  56. }
  57. break;
  58. case TLV_TIE_BREAKER_MODE_HIGHEST:
  59. if (dynar_str_catf(outbuf, "Node with highest node ID") == -1) {
  60. return (0);
  61. }
  62. break;
  63. case TLV_TIE_BREAKER_MODE_NODE_ID:
  64. if (dynar_str_catf(outbuf, "Node with node ID "UTILS_PRI_NODE_ID,
  65. instance->tie_breaker.node_id) == -1) {
  66. return (0);
  67. }
  68. break;
  69. }
  70. return (dynar_str_catf(outbuf, "\n") != -1);
  71. }
  72. static int
  73. qdevice_net_ipc_cmd_status_add_kap_tb_info(struct qdevice_net_instance *instance,
  74. struct dynar *outbuf, int verbose)
  75. {
  76. const char *kap_tb_str;
  77. if (!verbose) {
  78. return (1);
  79. }
  80. kap_tb_str = "";
  81. switch (instance->decision_algorithm) {
  82. case TLV_DECISION_ALGORITHM_TYPE_TEST:
  83. kap_tb_str = "Unsupported by algorithm";
  84. break;
  85. case TLV_DECISION_ALGORITHM_TYPE_2NODELMS:
  86. case TLV_DECISION_ALGORITHM_TYPE_LMS:
  87. kap_tb_str = "Algorithm hard-coded";
  88. break;
  89. case TLV_DECISION_ALGORITHM_TYPE_FFSPLIT:
  90. if (instance->keep_active_partition_tie_breaker) {
  91. if (instance->state !=
  92. QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS ||
  93. instance->server_supports_keep_active_partition_tie_breaker) {
  94. kap_tb_str = "Enabled";
  95. } else {
  96. kap_tb_str = "Unsupported by QNetd";
  97. }
  98. } else {
  99. kap_tb_str = "Disabled";
  100. }
  101. break;
  102. }
  103. return (dynar_str_catf(outbuf, "KAP Tie-breaker:\t%s\n", kap_tb_str) != -1);
  104. }
  105. static int
  106. qdevice_net_ipc_cmd_status_add_basic_info(struct qdevice_net_instance *instance,
  107. struct dynar *outbuf, int verbose)
  108. {
  109. if (dynar_str_catf(outbuf, "Cluster name:\t\t%s\n", instance->cluster_name) == -1) {
  110. return (0);
  111. }
  112. if (dynar_str_catf(outbuf, "QNetd host:\t\t%s:%"PRIu16"\n",
  113. instance->host_addr, instance->host_port) == -1) {
  114. return (0);
  115. }
  116. if (verbose && instance->force_ip_version != 0) {
  117. if (dynar_str_catf(outbuf, "Force IP version:\t%u\n",
  118. instance->force_ip_version) == -1) {
  119. return (0);
  120. }
  121. }
  122. if (verbose) {
  123. if ((dynar_str_catf(outbuf, "Connect timeout:\t%"PRIu32"ms\n",
  124. instance->connect_timeout) == -1) ||
  125. (dynar_str_catf(outbuf, "HB interval:\t\t%"PRIu32"ms\n",
  126. instance->heartbeat_interval) == -1) ||
  127. (dynar_str_catf(outbuf, "VQ vote timer interval:\t%"PRIu32"ms\n",
  128. instance->cast_vote_timer_interval) == -1)) {
  129. return (0);
  130. }
  131. if (dynar_str_catf(outbuf, "TLS:\t\t\t%s\n",
  132. tlv_tls_supported_to_str(instance->tls_supported)) == -1) {
  133. return (0);
  134. }
  135. }
  136. if (dynar_str_catf(outbuf, "Algorithm:\t\t%s\n",
  137. tlv_decision_algorithm_type_to_str(instance->decision_algorithm)) == -1) {
  138. return (0);
  139. }
  140. return (1);
  141. }
  142. static int
  143. qdevice_net_ipc_cmd_status_add_poll_timer_status(struct qdevice_net_instance *instance,
  144. struct dynar *outbuf, int verbose)
  145. {
  146. if (!verbose) {
  147. return (1);
  148. }
  149. if (dynar_str_catf(outbuf, "Poll timer running:\t%s",
  150. (instance->cast_vote_timer != NULL ? "Yes" : "No")) == -1) {
  151. return (0);
  152. }
  153. if (instance->cast_vote_timer != NULL && instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  154. if (dynar_str_catf(outbuf, " (cast vote)") == -1) {
  155. return (0);
  156. }
  157. }
  158. return (dynar_str_catf(outbuf, "\n") != -1);
  159. }
  160. static int
  161. qdevice_net_ipc_cmd_status_add_state(struct qdevice_net_instance *instance,
  162. struct dynar *outbuf, int verbose)
  163. {
  164. const char *state;
  165. if (instance->schedule_disconnect) {
  166. state = "Disconnected";
  167. } else {
  168. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  169. state = "Connected";
  170. } else {
  171. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT ||
  172. !instance->non_blocking_client.destroyed) {
  173. state = "Connecting";
  174. } else {
  175. state = "Connect failed";
  176. }
  177. }
  178. }
  179. return (dynar_str_catf(outbuf, "State:\t\t\t%s\n", state) != -1);
  180. }
  181. static int
  182. qdevice_net_ipc_cmd_status_add_heuristics(struct qdevice_net_instance *instance,
  183. struct dynar *outbuf, int verbose)
  184. {
  185. enum qdevice_heuristics_mode active_heuristics_mode;
  186. int heuristics_enabled;
  187. active_heuristics_mode = instance->qdevice_instance_ptr->heuristics_instance.mode;
  188. heuristics_enabled = (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  189. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC);
  190. if (!heuristics_enabled) {
  191. return (1);
  192. }
  193. if (dynar_str_catf(outbuf, "Heuristics result:\t%s",
  194. tlv_heuristics_to_str(instance->latest_heuristics_result)) == -1) {
  195. return (0);
  196. }
  197. if (verbose) {
  198. if (dynar_str_catf(outbuf, " (regular: %s, membership: %s, connect: %s)",
  199. tlv_heuristics_to_str(instance->latest_regular_heuristics_result),
  200. tlv_heuristics_to_str(instance->latest_vq_heuristics_result),
  201. tlv_heuristics_to_str(instance->latest_connect_heuristics_result)) == -1) {
  202. return (0);
  203. }
  204. }
  205. return (dynar_str_catf(outbuf, "\n") != -1);
  206. }
  207. static int
  208. qdevice_net_ipc_cmd_status_add_tls_state(struct qdevice_net_instance *instance,
  209. struct dynar *outbuf, int verbose)
  210. {
  211. if (!verbose || instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  212. return (1);
  213. }
  214. if (dynar_str_catf(outbuf, "TLS active:\t\t%s", (instance->using_tls ? "Yes" : "No")) == -1) {
  215. return (0);
  216. }
  217. if (instance->using_tls && instance->tls_client_cert_sent) {
  218. if (dynar_str_catf(outbuf, " (client certificate sent)") == -1) {
  219. return (0);
  220. }
  221. }
  222. return (dynar_str_catf(outbuf, "\n") != -1);
  223. }
  224. static int
  225. qdevice_net_ipc_cmd_status_add_times(struct qdevice_net_instance *instance,
  226. struct dynar *outbuf, int verbose)
  227. {
  228. struct tm tm_res;
  229. if (!verbose || instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  230. return (1);
  231. }
  232. if (instance->connected_since_time != ((time_t) -1)) {
  233. localtime_r(&instance->connected_since_time, &tm_res);
  234. if (dynar_str_catf(outbuf, "Connected since:\t%04d-%02d-%02dT%02d:%02d:%02d\n",
  235. tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
  236. tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec) == -1) {
  237. return (0);
  238. }
  239. }
  240. if (instance->last_echo_reply_received_time != ((time_t) -1)) {
  241. localtime_r(&instance->last_echo_reply_received_time, &tm_res);
  242. if (dynar_str_catf(outbuf, "Echo reply received:\t%04d-%02d-%02dT%02d:%02d:%02d\n",
  243. tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
  244. tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec) == -1) {
  245. return (0);
  246. }
  247. }
  248. return (1);
  249. }
  250. int
  251. qdevice_net_ipc_cmd_status(struct qdevice_net_instance *instance, struct dynar *outbuf, int verbose)
  252. {
  253. if (qdevice_net_ipc_cmd_status_add_header(outbuf, verbose) &&
  254. qdevice_net_ipc_cmd_status_add_basic_info(instance, outbuf, verbose) &&
  255. qdevice_net_ipc_cmd_status_add_tie_breaker(instance, outbuf, verbose) &&
  256. qdevice_net_ipc_cmd_status_add_kap_tb_info(instance, outbuf, verbose) &&
  257. qdevice_net_ipc_cmd_status_add_poll_timer_status(instance, outbuf, verbose) &&
  258. qdevice_net_ipc_cmd_status_add_state(instance, outbuf, verbose) &&
  259. qdevice_net_ipc_cmd_status_add_heuristics(instance, outbuf, verbose) &&
  260. qdevice_net_ipc_cmd_status_add_tls_state(instance, outbuf, verbose) &&
  261. qdevice_net_ipc_cmd_status_add_times(instance, outbuf, verbose)) {
  262. return (1);
  263. }
  264. return (0);
  265. }