qnetd-ipc-cmd.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_no_clusters(&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->max_client_send_size, instance->max_client_receive_size) == -1) {
  70. return (-1);
  71. }
  72. return (0);
  73. }
  74. static int
  75. qnetd_ipc_cmd_add_tie_breaker(const struct qnetd_client *client,
  76. struct dynar *outbuf)
  77. {
  78. if (dynar_str_catf(outbuf, " Tie-breaker:\t") == -1) {
  79. return (0);
  80. }
  81. switch (client->tie_breaker.mode) {
  82. case TLV_TIE_BREAKER_MODE_LOWEST:
  83. if (dynar_str_catf(outbuf, "Node with lowest node ID") == -1) {
  84. return (0);
  85. }
  86. break;
  87. case TLV_TIE_BREAKER_MODE_HIGHEST:
  88. if (dynar_str_catf(outbuf, "Node with highest node ID") == -1) {
  89. return (0);
  90. }
  91. break;
  92. case TLV_TIE_BREAKER_MODE_NODE_ID:
  93. if (dynar_str_catf(outbuf, "Node with node ID "UTILS_PRI_NODE_ID,
  94. client->tie_breaker.node_id) == -1) {
  95. return (0);
  96. }
  97. break;
  98. }
  99. return (dynar_str_catf(outbuf, "\n") != -1);
  100. }
  101. static int
  102. qnetd_ipc_cmd_list_add_node_list(struct dynar *outbuf, int verbose, const struct node_list *nlist)
  103. {
  104. struct node_list_entry *node_info;
  105. int i;
  106. i = 0;
  107. TAILQ_FOREACH(node_info, nlist, entries) {
  108. if (i != 0) {
  109. if (dynar_str_catf(outbuf, ", ") == -1) {
  110. return (-1);
  111. }
  112. }
  113. if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, node_info->node_id) == -1) {
  114. return (-1);
  115. }
  116. if (node_info->data_center_id != 0) {
  117. if (dynar_str_catf(outbuf, " (" UTILS_PRI_DATACENTER_ID ")",
  118. node_info->data_center_id) == -1) {
  119. return (-1);
  120. }
  121. }
  122. i++;
  123. }
  124. return (0);
  125. }
  126. static int
  127. qnetd_ipc_cmd_list_add_client_info(const struct qnetd_client *client, struct dynar *outbuf,
  128. int verbose, size_t client_no)
  129. {
  130. if (dynar_str_catf(outbuf, " Node ID "UTILS_PRI_NODE_ID":\n",
  131. client->node_id) == -1) {
  132. return (-1);
  133. }
  134. if (dynar_str_catf(outbuf, " Client address:\t\t%s\n",
  135. client->addr_str) == -1) {
  136. return (-1);
  137. }
  138. if (verbose) {
  139. if (dynar_str_catf(outbuf, " HB interval:\t\t%"PRIu32"ms\n",
  140. client->heartbeat_interval) == -1) {
  141. return (-1);
  142. }
  143. }
  144. if (client->config_version_set) {
  145. if (dynar_str_catf(outbuf, " Configuration version:\t"
  146. UTILS_PRI_CONFIG_VERSION"\n", client->config_version) == -1) {
  147. return (-1);
  148. }
  149. }
  150. if (!node_list_is_empty(&client->configuration_node_list)) {
  151. if ((dynar_str_catf(outbuf, " Configured node list:\t") == -1) ||
  152. (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose,
  153. &client->configuration_node_list) == -1) ||
  154. (dynar_str_catf(outbuf, "\n") == -1)) {
  155. return (-1);
  156. }
  157. }
  158. if (verbose) {
  159. if (dynar_str_catf(outbuf, " Ring ID:\t\t"UTILS_PRI_RING_ID"\n",
  160. client->last_ring_id.node_id, client->last_ring_id.seq) == -1) {
  161. return (-1);
  162. }
  163. }
  164. if (!node_list_is_empty(&client->last_membership_node_list)) {
  165. if ((dynar_str_catf(outbuf, " Membership node list:\t") == -1) ||
  166. (qnetd_ipc_cmd_list_add_node_list(outbuf, verbose,
  167. &client->last_membership_node_list) == -1) ||
  168. (dynar_str_catf(outbuf, "\n") == -1)) {
  169. return (-1);
  170. }
  171. }
  172. if (verbose) {
  173. if (dynar_str_catf(outbuf, " TLS active:\t\t%s",
  174. (client->tls_started ? "Yes" : "No")) == -1) {
  175. return (-1);
  176. }
  177. if (client->tls_started && client->tls_peer_certificate_verified) {
  178. if (dynar_str_catf(outbuf, " (client certificate verified)") == -1) {
  179. return (-1);
  180. }
  181. }
  182. if (dynar_str_catf(outbuf, "\n") == -1) {
  183. return (-1);
  184. }
  185. }
  186. if (client->last_sent_vote != TLV_VOTE_UNDEFINED) {
  187. if (dynar_str_catf(outbuf, " Vote:\t\t\t%s",
  188. tlv_vote_to_str(client->last_sent_vote)) == -1) {
  189. return (-1);
  190. }
  191. if (client->last_sent_ack_nack_vote != TLV_VOTE_UNDEFINED) {
  192. if (dynar_str_catf(outbuf, " (%s)",
  193. tlv_vote_to_str(client->last_sent_ack_nack_vote)) == -1) {
  194. return (-1);
  195. }
  196. }
  197. if (dynar_str_catf(outbuf, "\n") == -1) {
  198. return (-1);
  199. }
  200. }
  201. return (0);
  202. }
  203. int
  204. qnetd_ipc_cmd_list(struct qnetd_instance *instance, struct dynar *outbuf, int verbose,
  205. const char *cluster_name)
  206. {
  207. struct qnetd_cluster *cluster;
  208. struct qnetd_client *client;
  209. size_t cluster_no, client_no;
  210. cluster_no = 0;
  211. TAILQ_FOREACH(cluster, &instance->clusters, entries) {
  212. if (cluster_name != NULL && strcmp(cluster_name, "") != 0 &&
  213. strcmp(cluster_name, cluster->cluster_name) != 0) {
  214. continue ;
  215. }
  216. if (dynar_str_catf(outbuf, "Cluster \"%s\":\n", cluster->cluster_name) == -1) {
  217. return (-1);
  218. }
  219. client_no = 0;
  220. TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) {
  221. if (client_no == 0) {
  222. if (dynar_str_catf(outbuf, " Algorithm:\t\t%s\n",
  223. tlv_decision_algorithm_type_to_str(
  224. client->decision_algorithm)) == -1) {
  225. return (-1);
  226. }
  227. if (!qnetd_ipc_cmd_add_tie_breaker(client, outbuf)) {
  228. return (-1);
  229. }
  230. }
  231. if (qnetd_ipc_cmd_list_add_client_info(client, outbuf, verbose,
  232. client_no) == -1) {
  233. return (-1);
  234. }
  235. client_no++;
  236. }
  237. cluster_no++;
  238. }
  239. return (0);
  240. }