qdevice-net-send.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "qdevice-log.h"
  35. #include "qdevice-log-debug.h"
  36. #include "qdevice-net-send.h"
  37. #include "qdevice-cmap.h"
  38. #include "qdevice-net-votequorum.h"
  39. #include "msg.h"
  40. #include "utils.h"
  41. int
  42. qdevice_net_send_echo_request(struct qdevice_net_instance *instance)
  43. {
  44. struct send_buffer_list_entry *send_buffer;
  45. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  46. if (send_buffer == NULL) {
  47. qdevice_log(LOG_CRIT, "Can't allocate send list buffer for reply msg.");
  48. return (-1);
  49. }
  50. instance->echo_request_expected_msg_seq_num++;
  51. if (msg_create_echo_request(&send_buffer->buffer, 1,
  52. instance->echo_request_expected_msg_seq_num) == -1) {
  53. qdevice_log(LOG_ERR, "Can't allocate send buffer for echo request msg");
  54. return (-1);
  55. }
  56. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  57. return (0);
  58. }
  59. int
  60. qdevice_net_send_preinit(struct qdevice_net_instance *instance)
  61. {
  62. struct send_buffer_list_entry *send_buffer;
  63. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  64. if (send_buffer == NULL) {
  65. qdevice_log(LOG_ERR, "Can't allocate send list buffer for preinit msg");
  66. return (-1);
  67. }
  68. if (msg_create_preinit(&send_buffer->buffer, instance->cluster_name, 1,
  69. instance->last_msg_seq_num) == 0) {
  70. qdevice_log(LOG_ERR, "Can't allocate buffer");
  71. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  72. return (-1);
  73. }
  74. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  75. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY;
  76. return (0);
  77. }
  78. int
  79. qdevice_net_send_init(struct qdevice_net_instance *instance)
  80. {
  81. enum msg_type *supported_msgs;
  82. size_t no_supported_msgs;
  83. enum tlv_opt_type *supported_opts;
  84. size_t no_supported_opts;
  85. struct send_buffer_list_entry *send_buffer;
  86. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  87. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  88. instance->last_msg_seq_num++;
  89. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  90. if (send_buffer == NULL) {
  91. qdevice_log(LOG_ERR, "Can't allocate send list buffer for init msg");
  92. return (-1);
  93. }
  94. if (msg_create_init(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  95. instance->decision_algorithm,
  96. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  97. instance->qdevice_instance_ptr->node_id, instance->heartbeat_interval,
  98. &instance->tie_breaker) == 0) {
  99. qdevice_log(LOG_ERR, "Can't allocate send buffer for init msg");
  100. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  101. return (-1);
  102. }
  103. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  104. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY;
  105. return (0);
  106. }
  107. int
  108. qdevice_net_send_ask_for_vote(struct qdevice_net_instance *instance)
  109. {
  110. struct send_buffer_list_entry *send_buffer;
  111. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  112. if (send_buffer == NULL) {
  113. qdevice_log(LOG_ERR, "Can't allocate send list buffer for ask for vote msg");
  114. return (-1);
  115. }
  116. instance->last_msg_seq_num++;
  117. qdevice_log(LOG_DEBUG, "Sending ask for vote seq = "UTILS_PRI_MSG_SEQ,
  118. instance->last_msg_seq_num);
  119. if (msg_create_ask_for_vote(&send_buffer->buffer, instance->last_msg_seq_num) == 0) {
  120. qdevice_log(LOG_ERR, "Can't allocate send buffer for ask for vote msg");
  121. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  122. return (-1);
  123. }
  124. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  125. return (0);
  126. }
  127. int
  128. qdevice_net_send_config_node_list(struct qdevice_net_instance *instance,
  129. const struct node_list *nlist, int config_version_set, uint64_t config_version,
  130. int initial)
  131. {
  132. struct send_buffer_list_entry *send_buffer;
  133. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  134. if (send_buffer == NULL) {
  135. qdevice_log(LOG_ERR, "Can't allocate send list buffer for config "
  136. "node list msg");
  137. return (-1);
  138. }
  139. instance->last_msg_seq_num++;
  140. qdevice_log(LOG_DEBUG, "Sending config node list seq = "UTILS_PRI_MSG_SEQ,
  141. instance->last_msg_seq_num);
  142. qdevice_log_debug_dump_node_list(nlist);
  143. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  144. (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG),
  145. 0, NULL, config_version_set, config_version, 0, TLV_QUORATE_INQUORATE, nlist) == 0) {
  146. qdevice_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  147. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  148. return (-1);
  149. }
  150. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  151. return (0);
  152. }
  153. int
  154. qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance,
  155. const struct tlv_ring_id *ring_id,
  156. uint32_t node_list_entries, uint32_t node_list[])
  157. {
  158. struct node_list nlist;
  159. struct send_buffer_list_entry *send_buffer;
  160. uint32_t i;
  161. node_list_init(&nlist);
  162. for (i = 0; i < node_list_entries; i++) {
  163. if (node_list_add(&nlist, node_list[i], 0, TLV_NODE_STATE_NOT_SET) == NULL) {
  164. qdevice_log(LOG_ERR, "Can't allocate membership node list.");
  165. node_list_free(&nlist);
  166. return (-1);
  167. }
  168. }
  169. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  170. if (send_buffer == NULL) {
  171. qdevice_log(LOG_ERR, "Can't allocate send list buffer for membership "
  172. "node list msg");
  173. node_list_free(&nlist);
  174. return (-1);
  175. }
  176. instance->last_msg_seq_num++;
  177. qdevice_log(LOG_DEBUG, "Sending membership node list seq = "UTILS_PRI_MSG_SEQ", "
  178. "ringid = ("UTILS_PRI_RING_ID").", instance->last_msg_seq_num,
  179. ring_id->node_id, ring_id->seq);
  180. qdevice_log_debug_dump_node_list(&nlist);
  181. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  182. TLV_NODE_LIST_TYPE_MEMBERSHIP,
  183. 1, ring_id, 0, 0, 0, 0, &nlist) == 0) {
  184. qdevice_log(LOG_ERR, "Can't allocate send buffer for membership list msg");
  185. node_list_free(&nlist);
  186. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  187. return (-1);
  188. }
  189. memcpy(&instance->last_sent_ring_id, ring_id, sizeof(instance->last_sent_ring_id));
  190. node_list_free(&nlist);
  191. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  192. return (0);
  193. }
  194. int
  195. qdevice_net_send_quorum_node_list(struct qdevice_net_instance *instance,
  196. enum tlv_quorate quorate,
  197. uint32_t node_list_entries, votequorum_node_t node_list[])
  198. {
  199. struct node_list nlist;
  200. struct send_buffer_list_entry *send_buffer;
  201. uint32_t i;
  202. node_list_init(&nlist);
  203. for (i = 0; i < node_list_entries; i++) {
  204. if (node_list[i].nodeid == 0) {
  205. continue;
  206. }
  207. if (node_list_add(&nlist, node_list[i].nodeid, 0,
  208. qdevice_net_votequorum_node_state_to_tlv(node_list[i].state)) == NULL) {
  209. qdevice_log(LOG_ERR, "Can't allocate quorum node list.");
  210. node_list_free(&nlist);
  211. return (-1);
  212. }
  213. }
  214. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  215. if (send_buffer == NULL) {
  216. qdevice_log(LOG_ERR, "Can't allocate send list buffer for quorum "
  217. "node list msg");
  218. node_list_free(&nlist);
  219. return (-1);
  220. }
  221. instance->last_msg_seq_num++;
  222. qdevice_log(LOG_DEBUG, "Sending quorum node list seq = "UTILS_PRI_MSG_SEQ", quorate = %u",
  223. instance->last_msg_seq_num, quorate);
  224. qdevice_log_debug_dump_node_list(&nlist);
  225. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  226. TLV_NODE_LIST_TYPE_QUORUM,
  227. 0, NULL, 0, 0, 1, quorate, &nlist) == 0) {
  228. qdevice_log(LOG_ERR, "Can't allocate send buffer for quorum list msg");
  229. node_list_free(&nlist);
  230. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  231. return (-1);
  232. }
  233. node_list_free(&nlist);
  234. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  235. return (0);
  236. }