qdevice-net-send.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. struct node_list_entry *node_info;
  114. size_t zi;
  115. /*
  116. * Send initial node list
  117. */
  118. if (qdevice_net_cmap_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  119. qdevice_net_log(LOG_ERR, "Can't get initial configuration node list.");
  120. return (-1);
  121. }
  122. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  123. if (send_buffer == NULL) {
  124. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  125. "node list msg");
  126. node_list_free(&nlist);
  127. return (-1);
  128. }
  129. send_config_version = qdevice_net_cmap_get_config_version(instance->cmap_handle,
  130. &config_version);
  131. instance->last_msg_seq_num++;
  132. qdevice_net_log(LOG_DEBUG, "Sending config node list seq=%"PRIu32".",
  133. instance->last_msg_seq_num);
  134. qdevice_net_log(LOG_DEBUG, " Node list:");
  135. zi = 0;
  136. TAILQ_FOREACH(node_info, &nlist, entries) {
  137. qdevice_net_log(LOG_DEBUG, " %zu node_id = %"PRIx32", "
  138. "data_center_id = %"PRIx32, zi, node_info->node_id, node_info->data_center_id);
  139. zi++;
  140. }
  141. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  142. (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG),
  143. 0, NULL, send_config_version, config_version, 0, TLV_QUORATE_INQUORATE, &nlist) == 0) {
  144. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  145. node_list_free(&nlist);
  146. return (-1);
  147. }
  148. node_list_free(&nlist);
  149. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  150. return (0);
  151. }
  152. int
  153. qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance,
  154. const struct tlv_ring_id *ring_id,
  155. uint32_t node_list_entries, uint32_t node_list[])
  156. {
  157. struct node_list nlist;
  158. struct send_buffer_list_entry *send_buffer;
  159. uint32_t i;
  160. node_list_init(&nlist);
  161. for (i = 0; i < node_list_entries; i++) {
  162. if (node_list_add(&nlist, node_list[i], 0, TLV_NODE_STATE_NOT_SET) == NULL) {
  163. qdevice_net_log(LOG_ERR, "Can't allocate membership node list.");
  164. node_list_free(&nlist);
  165. return (-1);
  166. }
  167. }
  168. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  169. if (send_buffer == NULL) {
  170. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for membership "
  171. "node list msg");
  172. node_list_free(&nlist);
  173. return (-1);
  174. }
  175. instance->last_msg_seq_num++;
  176. qdevice_net_log(LOG_DEBUG, "Sending membership node list seq=%"PRIu32".",
  177. instance->last_msg_seq_num);
  178. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  179. TLV_NODE_LIST_TYPE_MEMBERSHIP,
  180. 1, ring_id, 0, 0, 0, 0, &nlist) == 0) {
  181. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for membership list msg");
  182. node_list_free(&nlist);
  183. return (-1);
  184. }
  185. node_list_free(&nlist);
  186. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  187. return (0);
  188. }
  189. int
  190. qdevice_net_send_quorum_node_list(struct qdevice_net_instance *instance,
  191. enum tlv_quorate quorate,
  192. uint32_t node_list_entries, votequorum_node_t node_list[])
  193. {
  194. struct node_list nlist;
  195. struct send_buffer_list_entry *send_buffer;
  196. uint32_t i;
  197. node_list_init(&nlist);
  198. for (i = 0; i < node_list_entries; i++) {
  199. if (node_list[i].nodeid == 0) {
  200. continue;
  201. }
  202. if (node_list_add(&nlist, node_list[i].nodeid, 0,
  203. qdevice_net_votequorum_node_state_to_tlv(node_list[i].state)) == NULL) {
  204. qdevice_net_log(LOG_ERR, "Can't allocate quorum node list.");
  205. node_list_free(&nlist);
  206. return (-1);
  207. }
  208. }
  209. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  210. if (send_buffer == NULL) {
  211. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for quorum "
  212. "node list msg");
  213. node_list_free(&nlist);
  214. return (-1);
  215. }
  216. instance->last_msg_seq_num++;
  217. qdevice_net_log(LOG_DEBUG, "Sending quorum node list seq=%"PRIu32".",
  218. instance->last_msg_seq_num);
  219. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  220. TLV_NODE_LIST_TYPE_QUORUM,
  221. 0, NULL, 0, 0, 1, quorate, &nlist) == 0) {
  222. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for quorum list msg");
  223. node_list_free(&nlist);
  224. return (-1);
  225. }
  226. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  227. return (0);
  228. }