qnetd-ipc-cmd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2015-2016 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 "dynar-str.h"
  35. #include "qnetd-ipc-cmd.h"
  36. #include "qnetd-log.h"
  37. #include "utils.h"
  38. int
  39. qnetd_ipc_cmd_status(struct qnetd_instance *instance, struct dynar *outbuf, int verbose)
  40. {
  41. if (dynar_str_catf(outbuf, "QNetd address:\t\t\t%s:%"PRIu16"\n",
  42. (instance->host_addr != NULL ? instance->host_addr : "*"), instance->host_port) == -1) {
  43. return (-1);
  44. }
  45. if (dynar_str_catf(outbuf, "TLS:\t\t\t\t%s%s\n",
  46. tlv_tls_supported_to_str(instance->tls_supported),
  47. ((instance->tls_supported != TLV_TLS_UNSUPPORTED && instance->tls_client_cert_required) ?
  48. " (client certificate required)" : "")) == -1) {
  49. return (-1);
  50. }
  51. if (dynar_str_catf(outbuf, "Connected clients:\t\t%zu\n",
  52. qnetd_client_list_no_clients(&instance->clients)) == -1) {
  53. return (-1);
  54. }
  55. if (dynar_str_catf(outbuf, "Connected clusters:\t\t%zu\n",
  56. qnetd_cluster_list_size(&instance->clusters)) == -1) {
  57. return (-1);
  58. }
  59. if (!verbose) {
  60. return (0);
  61. }
  62. if (instance->max_clients != 0) {
  63. if (dynar_str_catf(outbuf, "Maximum allowed clients:\t%zu\n",
  64. instance->max_clients) == -1) {
  65. return (-1);
  66. }
  67. }
  68. if (dynar_str_catf(outbuf, "Maximum send/receive size:\t%zu/%zu bytes\n",
  69. instance->advanced_settings->max_client_send_size,
  70. instance->advanced_settings->max_client_receive_size) == -1) {
  71. return (-1);
  72. }
  73. return (0);
  74. }
  75. static int
  76. qnetd_ipc_cmd_add_tie_breaker(const struct qnetd_client *client,
  77. struct dynar *outbuf)
  78. {
  79. if (dynar_str_catf(outbuf, " Tie-breaker:\t") == -1) {
  80. return (0);
  81. }
  82. switch (client->tie_breaker.mode) {
  83. case TLV_TIE_BREAKER_MODE_LOWEST:
  84. if (dynar_str_catf(outbuf, "Node with lowest node ID") == -1) {
  85. return (0);
  86. }
  87. break;
  88. case TLV_TIE_BREAKER_MODE_HIGHEST:
  89. if (dynar_str_catf(outbuf, "Node with highest node ID") == -1) {
  90. return (0);
  91. }
  92. break;
  93. case TLV_TIE_BREAKER_MODE_NODE_ID:
  94. if (dynar_str_catf(outbuf, "Node with node ID "UTILS_PRI_NODE_ID,
  95. client->tie_breaker.node_id) == -1) {
  96. return (0);
  97. }
  98. break;
  99. }
  100. return (dynar_str_catf(outbuf, "\n") != -1);
  101. }
  102. static int
  103. qnetd_ipc_cmd_list_add_node_list(struct dynar *outbuf, int verbose, const struct node_list *nlist)
  104. {
  105. struct node_list_entry *node_info;
  106. int i;
  107. i = 0;
  108. TAILQ_FOREACH(node_info, nlist, entries) {
  109. if (i != 0) {
  110. if (dynar_str_catf(outbuf, ", ") == -1) {
  111. return (-1);
  112. }
  113. }
  114. if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, node_info->node_id) == -1) {
  115. return (-1);
  116. }
  117. if (node_info->data_center_id != 0) {
  118. if (dynar_str_catf(outbuf, " (" UTILS_PRI_DATACENTER_ID ")",
  119. node_info->data_center_id) == -1) {
  120. return (-1);
  121. }
  122. }
  123. i++;
  124. }
  125. return (0);
  126. }
  127. static int
  128. qnetd_ipc_cmd_list_add_client_info(const struct qnetd_client *client, struct dynar *outbuf,
  129. int verbose, size_t client_no)
  130. {
  131. if (dynar_str_catf(outbuf, " Node ID "UTILS_PRI_NODE_ID":\n",
  132. client->node_id) == -1) {
  133. return (-1);
  134. }
  135. if (dynar_str_catf(outbuf, " Client address:\t\t%s\n",
  136. client->addr_str) == -1) {
  137. return (-1);
  138. }
  139. if (verbose) {
  140. if (dynar_str_catf(outbuf, " HB interval:\t\t%"PRIu32"ms\n",
  141. client->heartbeat_interval) == -1) {
  142. return (-1);
  143. }
  144. }
  145. if (client->config_version_set) {
  146. if (dynar_str_catf(outbuf, " Configuration version:\t"
  147. UTILS_PRI_CONFIG_VERSION"\n", client->config_version) == -1) {
  148. return (-1);
  149. }
  150. }
  151. if (!node_list_is_empty(&client->configuration_node_list)) {
  152. if ((dynar_str_catf(outbuf, " Configured node list:\t") == -1) ||
  153. (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose,
  154. &client->configuration_node_list) == -1) ||
  155. (dynar_str_catf(outbuf, "\n") == -1)) {
  156. return (-1);
  157. }
  158. }
  159. if (verbose) {
  160. if (dynar_str_catf(outbuf, " Ring ID:\t\t"UTILS_PRI_RING_ID"\n",
  161. client->last_ring_id.node_id, client->last_ring_id.seq) == -1) {
  162. return (-1);
  163. }
  164. }
  165. if (!node_list_is_empty(&client->last_membership_node_list)) {
  166. if ((dynar_str_catf(outbuf, " Membership node list:\t") == -1) ||
  167. (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose,
  168. &client->last_membership_node_list) == -1) ||
  169. (dynar_str_catf(outbuf, "\n") == -1)) {
  170. return (-1);
  171. }
  172. }
  173. if (client->last_heuristics != TLV_HEURISTICS_UNDEFINED || verbose) {
  174. if (dynar_str_catf(outbuf, " Heuristics:\t\t%s",
  175. tlv_heuristics_to_str(client->last_heuristics)) == -1) {
  176. return (-1);
  177. }
  178. if (verbose) {
  179. if (dynar_str_catf(outbuf, " (membership: %s, regular: %s)",
  180. tlv_heuristics_to_str(client->last_membership_heuristics),
  181. tlv_heuristics_to_str(client->last_regular_heuristics)) == -1) {
  182. return (-1);
  183. }
  184. }
  185. if (dynar_str_catf(outbuf, "\n") == -1) {
  186. return (-1);
  187. }
  188. }
  189. if (verbose) {
  190. if (dynar_str_catf(outbuf, " TLS active:\t\t%s",
  191. (client->tls_started ? "Yes" : "No")) == -1) {
  192. return (-1);
  193. }
  194. if (client->tls_started && client->tls_peer_certificate_verified) {
  195. if (dynar_str_catf(outbuf, " (client certificate verified)") == -1) {
  196. return (-1);
  197. }
  198. }
  199. if (dynar_str_catf(outbuf, "\n") == -1) {
  200. return (-1);
  201. }
  202. }
  203. if (client->last_sent_vote != TLV_VOTE_UNDEFINED) {
  204. if (dynar_str_catf(outbuf, " Vote:\t\t\t%s",
  205. tlv_vote_to_str(client->last_sent_vote)) == -1) {
  206. return (-1);
  207. }
  208. if (client->last_sent_ack_nack_vote != TLV_VOTE_UNDEFINED) {
  209. if (dynar_str_catf(outbuf, " (%s)",
  210. tlv_vote_to_str(client->last_sent_ack_nack_vote)) == -1) {
  211. return (-1);
  212. }
  213. }
  214. if (dynar_str_catf(outbuf, "\n") == -1) {
  215. return (-1);
  216. }
  217. }
  218. return (0);
  219. }
  220. int
  221. qnetd_ipc_cmd_list(struct qnetd_instance *instance, struct dynar *outbuf, int verbose,
  222. const char *cluster_name)
  223. {
  224. struct qnetd_cluster *cluster;
  225. struct qnetd_client *client;
  226. size_t cluster_no, client_no;
  227. cluster_no = 0;
  228. TAILQ_FOREACH(cluster, &instance->clusters, entries) {
  229. if (cluster_name != NULL && strcmp(cluster_name, "") != 0 &&
  230. strcmp(cluster_name, cluster->cluster_name) != 0) {
  231. continue;
  232. }
  233. if (dynar_str_catf(outbuf, "Cluster \"%s\":\n", cluster->cluster_name) == -1) {
  234. return (-1);
  235. }
  236. client_no = 0;
  237. TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) {
  238. if (client_no == 0) {
  239. if (dynar_str_catf(outbuf, " Algorithm:\t\t%s\n",
  240. tlv_decision_algorithm_type_to_str(
  241. client->decision_algorithm)) == -1) {
  242. return (-1);
  243. }
  244. if (!qnetd_ipc_cmd_add_tie_breaker(client, outbuf)) {
  245. return (-1);
  246. }
  247. }
  248. if (qnetd_ipc_cmd_list_add_client_info(client, outbuf, verbose,
  249. client_no) == -1) {
  250. return (-1);
  251. }
  252. client_no++;
  253. }
  254. cluster_no++;
  255. }
  256. return (0);
  257. }