qnetd-algo-test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2015 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 <sys/types.h>
  35. #include <string.h>
  36. #include "qnetd-algo-test.h"
  37. #include "qnetd-log.h"
  38. /*
  39. * Called right after client sent init message. This happens after initial accept of client,
  40. * tls handshake and sending basic information about cluster/client.
  41. * Known information:
  42. * - client->cluster_name (client->cluster_name_len)
  43. * - client->node_id (client->node_id_set = 1)
  44. * - client->decision_algorithm
  45. *
  46. * Callback is designed mainly for allocating client->algorithm_data.
  47. *
  48. * client is initialized qnetd_client structure.
  49. *
  50. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  51. * on failure (error is send back to client)
  52. */
  53. enum tlv_reply_error_code
  54. qnetd_algo_test_client_init(struct qnetd_client *client)
  55. {
  56. int *algo_data;
  57. qnetd_log(LOG_INFO, "algo-test: New client connected");
  58. qnetd_log(LOG_INFO, "algo-test: cluster name = %s", client->cluster_name);
  59. qnetd_log(LOG_INFO, "algo-test: tls started = %u", client->tls_started);
  60. qnetd_log(LOG_INFO, "algo-test: tls peer certificate verified = %u",
  61. client->tls_peer_certificate_verified);
  62. qnetd_log(LOG_INFO, "algo-test: node_id = %"PRIx32, client->node_id);
  63. qnetd_log(LOG_INFO, "algo-test: pointer = 0x%p", client);
  64. client->algorithm_data = malloc(sizeof(int));
  65. if (client->algorithm_data == NULL) {
  66. return (-1);
  67. }
  68. algo_data = client->algorithm_data;
  69. *algo_data = 42;
  70. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  71. }
  72. static const char *
  73. qnetd_algo_test_node_state_to_str(enum tlv_node_state node_state)
  74. {
  75. switch (node_state) {
  76. case TLV_NODE_STATE_NOT_SET: return ("not set"); break;
  77. case TLV_NODE_STATE_MEMBER: return ("member"); break;
  78. case TLV_NODE_STATE_DEAD: return ("dead"); break;
  79. case TLV_NODE_STATE_LEAVING: return ("leaving"); break;
  80. default: return ("unhandled"); break;
  81. }
  82. return ("");
  83. }
  84. static void
  85. qnetd_algo_dump_node_list(struct qnetd_client *client, const struct node_list *nodes)
  86. {
  87. int *algo_data;
  88. struct node_list_entry *node_info;
  89. algo_data = client->algorithm_data;
  90. qnetd_log(LOG_INFO, "algo-test: algo data = %u", *algo_data);
  91. TAILQ_FOREACH(node_info, nodes, entries) {
  92. qnetd_log(LOG_INFO, "algo-test: node_id = %"PRIx32", "
  93. "data_center_id = %"PRIx32", "
  94. "node_state = %s", node_info->node_id, node_info->data_center_id,
  95. qnetd_algo_test_node_state_to_str(node_info->node_state));
  96. }
  97. }
  98. /*
  99. * Called after client sent configuration node list
  100. * All client fields are already set. Nodes is actual node list, initial is used
  101. * for distrinquish between initial node list and changed node list.
  102. *
  103. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  104. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  105. *
  106. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  107. * on failure (error is send back to client)
  108. */
  109. enum tlv_reply_error_code
  110. qnetd_algo_test_config_node_list_received(struct qnetd_client *client,
  111. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  112. {
  113. qnetd_log(LOG_INFO, "algo-test: Client %p (cluster %s, node_id %"PRIx32") "
  114. "sent %s node list.", client, client->cluster_name, client->node_id,
  115. (initial ? "initial" : "changed"));
  116. qnetd_algo_dump_node_list(client, nodes);
  117. *result_vote = TLV_VOTE_ACK;
  118. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  119. }
  120. enum tlv_reply_error_code
  121. qnetd_algo_test_membership_node_list_received(struct qnetd_client *client,
  122. const struct node_list *nodes, enum tlv_vote *result_vote)
  123. {
  124. qnetd_log(LOG_INFO, "algo-test: Client %p (cluster %s, node_id %"PRIx32") "
  125. "sent membership node list.", client, client->cluster_name, client->node_id);
  126. qnetd_algo_dump_node_list(client, nodes);
  127. *result_vote = TLV_VOTE_ACK;
  128. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  129. }