qdevice-net-send.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "qdevice-net-log.h"
  35. #include "qdevice-net-send.h"
  36. #include "qdevice-net-votequorum.h"
  37. #include "msg.h"
  38. int
  39. qdevice_net_send_echo_request(struct qdevice_net_instance *instance)
  40. {
  41. struct send_buffer_list_entry *send_buffer;
  42. if (instance->echo_reply_received_msg_seq_num !=
  43. instance->echo_request_expected_msg_seq_num) {
  44. qdevice_net_log(LOG_ERR, "Server didn't send echo reply message on time. "
  45. "Disconnecting from server.");
  46. return (-1);
  47. }
  48. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  49. if (send_buffer == NULL) {
  50. qdevice_net_log(LOG_CRIT, "Can't allocate send list buffer for reply msg.");
  51. return (-1);
  52. }
  53. instance->echo_request_expected_msg_seq_num++;
  54. if (msg_create_echo_request(&send_buffer->buffer, 1,
  55. instance->echo_request_expected_msg_seq_num) == -1) {
  56. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for echo request msg");
  57. return (-1);
  58. }
  59. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  60. return (0);
  61. }
  62. int
  63. qdevice_net_send_init(struct qdevice_net_instance *instance)
  64. {
  65. enum msg_type *supported_msgs;
  66. size_t no_supported_msgs;
  67. enum tlv_opt_type *supported_opts;
  68. size_t no_supported_opts;
  69. struct send_buffer_list_entry *send_buffer;
  70. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  71. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  72. instance->last_msg_seq_num++;
  73. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  74. if (send_buffer == NULL) {
  75. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for init msg");
  76. return (-1);
  77. }
  78. if (msg_create_init(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  79. instance->decision_algorithm,
  80. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  81. instance->node_id) == 0) {
  82. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for init msg");
  83. return (-1);
  84. }
  85. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  86. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY;
  87. return (0);
  88. }
  89. int
  90. qdevice_net_send_ask_for_vote(struct qdevice_net_instance *instance)
  91. {
  92. struct send_buffer_list_entry *send_buffer;
  93. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  94. if (send_buffer == NULL) {
  95. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for ask for vote msg");
  96. return (-1);
  97. }
  98. instance->last_msg_seq_num++;
  99. if (msg_create_ask_for_vote(&send_buffer->buffer, instance->last_msg_seq_num) == 0) {
  100. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for ask for vote msg");
  101. return (-1);
  102. }
  103. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  104. return (0);
  105. }
  106. int
  107. qdevice_net_send_config_node_list(struct qdevice_net_instance *instance, int initial)
  108. {
  109. struct node_list nlist;
  110. struct send_buffer_list_entry *send_buffer;
  111. uint64_t config_version;
  112. int send_config_version;
  113. /*
  114. * Send initial node list
  115. */
  116. if (qdevice_net_cmap_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  117. qdevice_net_log(LOG_ERR, "Can't get initial configuration node list.");
  118. return (-1);
  119. }
  120. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  121. if (send_buffer == NULL) {
  122. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  123. "node list msg");
  124. node_list_free(&nlist);
  125. return (-1);
  126. }
  127. send_config_version = qdevice_net_cmap_get_config_version(instance->cmap_handle,
  128. &config_version);
  129. instance->last_msg_seq_num++;
  130. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  131. (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG),
  132. 0, NULL, send_config_version, config_version, 0, TLV_QUORATE_INQUORATE, &nlist) == 0) {
  133. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  134. node_list_free(&nlist);
  135. return (-1);
  136. }
  137. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  138. return (0);
  139. }
  140. int
  141. qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance,
  142. enum tlv_quorate quorate, const struct tlv_ring_id *ring_id,
  143. uint32_t node_list_entries, votequorum_node_t node_list[])
  144. {
  145. struct node_list nlist;
  146. struct send_buffer_list_entry *send_buffer;
  147. uint32_t i;
  148. node_list_init(&nlist);
  149. for (i = 0; i < node_list_entries; i++) {
  150. if (node_list[i].nodeid == 0) {
  151. continue;
  152. }
  153. if (node_list_add(&nlist, node_list[i].nodeid, 0,
  154. qdevice_net_votequorum_node_state_to_tlv(node_list[i].state)) == NULL) {
  155. qdevice_net_log(LOG_ERR, "Can't allocate membership node list.");
  156. node_list_free(&nlist);
  157. return (-1);
  158. }
  159. }
  160. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  161. if (send_buffer == NULL) {
  162. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  163. "node list msg");
  164. node_list_free(&nlist);
  165. return (-1);
  166. }
  167. instance->last_msg_seq_num++;
  168. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  169. TLV_NODE_LIST_TYPE_MEMBERSHIP,
  170. 1, ring_id, 0, 0, 1, quorate, &nlist) == 0) {
  171. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  172. node_list_free(&nlist);
  173. return (-1);
  174. }
  175. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  176. return (0);
  177. }