qdevice-net-ipc-cmd.c 9.3 KB

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