qdevice-net-send.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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-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. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  43. if (send_buffer == NULL) {
  44. qdevice_net_log(LOG_CRIT, "Can't allocate send list buffer for reply msg.");
  45. return (-1);
  46. }
  47. instance->echo_request_expected_msg_seq_num++;
  48. if (msg_create_echo_request(&send_buffer->buffer, 1,
  49. instance->echo_request_expected_msg_seq_num) == -1) {
  50. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for echo request msg");
  51. return (-1);
  52. }
  53. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  54. return (0);
  55. }
  56. int
  57. qdevice_net_send_init(struct qdevice_net_instance *instance)
  58. {
  59. enum msg_type *supported_msgs;
  60. size_t no_supported_msgs;
  61. enum tlv_opt_type *supported_opts;
  62. size_t no_supported_opts;
  63. struct send_buffer_list_entry *send_buffer;
  64. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  65. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  66. instance->last_msg_seq_num++;
  67. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  68. if (send_buffer == NULL) {
  69. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for init msg");
  70. return (-1);
  71. }
  72. if (msg_create_init(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  73. instance->decision_algorithm,
  74. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  75. instance->node_id) == 0) {
  76. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for init msg");
  77. return (-1);
  78. }
  79. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  80. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY;
  81. return (0);
  82. }
  83. int
  84. qdevice_net_send_ask_for_vote(struct qdevice_net_instance *instance)
  85. {
  86. struct send_buffer_list_entry *send_buffer;
  87. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  88. if (send_buffer == NULL) {
  89. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for ask for vote msg");
  90. return (-1);
  91. }
  92. instance->last_msg_seq_num++;
  93. qdevice_net_log(LOG_DEBUG, "Sending ask for vote seq = %"PRIu32,
  94. instance->last_msg_seq_num);
  95. if (msg_create_ask_for_vote(&send_buffer->buffer, instance->last_msg_seq_num) == 0) {
  96. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for ask for vote msg");
  97. return (-1);
  98. }
  99. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  100. return (0);
  101. }
  102. static
  103. void qdevice_net_log_debug_node_list(const struct node_list *nlist)
  104. {
  105. struct node_list_entry *node_info;
  106. size_t zi;
  107. qdevice_net_log(LOG_DEBUG, " Node list:");
  108. zi = 0;
  109. TAILQ_FOREACH(node_info, nlist, entries) {
  110. qdevice_net_log(LOG_DEBUG, " %zu node_id = %"PRIx32", "
  111. "data_center_id = %"PRIx32", node_state = %s",
  112. zi, node_info->node_id, node_info->data_center_id,
  113. tlv_node_state_to_str(node_info->node_state));
  114. zi++;
  115. }
  116. }
  117. int
  118. qdevice_net_send_config_node_list(struct qdevice_net_instance *instance, int initial, int force_send)
  119. {
  120. struct node_list nlist;
  121. struct send_buffer_list_entry *send_buffer;
  122. uint64_t config_version;
  123. int send_config_version;
  124. if (qdevice_net_cmap_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  125. qdevice_net_log(LOG_ERR, "Can't get initial configuration node list.");
  126. return (-1);
  127. }
  128. /*
  129. * Send only if list changed or force was used
  130. */
  131. if (!force_send && node_list_eq(&instance->last_sent_config_node_list, &nlist)) {
  132. return (0);
  133. }
  134. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  135. if (send_buffer == NULL) {
  136. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  137. "node list msg");
  138. node_list_free(&nlist);
  139. return (-1);
  140. }
  141. send_config_version = qdevice_net_cmap_get_config_version(instance->cmap_handle,
  142. &config_version);
  143. instance->last_msg_seq_num++;
  144. qdevice_net_log(LOG_DEBUG, "Sending config node list seq = %"PRIu32,
  145. instance->last_msg_seq_num);
  146. qdevice_net_log_debug_node_list(&nlist);
  147. node_list_free(&instance->last_sent_config_node_list);
  148. if (node_list_clone(&instance->last_sent_config_node_list, &nlist) != 0) {
  149. qdevice_net_log(LOG_ERR, "Can't allocate last sent config node list clone");
  150. node_list_free(&nlist);
  151. return (-1);
  152. }
  153. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  154. (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG),
  155. 0, NULL, send_config_version, config_version, 0, TLV_QUORATE_INQUORATE, &nlist) == 0) {
  156. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  157. node_list_free(&nlist);
  158. return (-1);
  159. }
  160. node_list_free(&nlist);
  161. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  162. return (0);
  163. }
  164. int
  165. qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance,
  166. const struct tlv_ring_id *ring_id,
  167. uint32_t node_list_entries, uint32_t node_list[])
  168. {
  169. struct node_list nlist;
  170. struct send_buffer_list_entry *send_buffer;
  171. uint32_t i;
  172. node_list_init(&nlist);
  173. for (i = 0; i < node_list_entries; i++) {
  174. if (node_list_add(&nlist, node_list[i], 0, TLV_NODE_STATE_NOT_SET) == NULL) {
  175. qdevice_net_log(LOG_ERR, "Can't allocate membership node list.");
  176. node_list_free(&nlist);
  177. return (-1);
  178. }
  179. }
  180. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  181. if (send_buffer == NULL) {
  182. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for membership "
  183. "node list msg");
  184. node_list_free(&nlist);
  185. return (-1);
  186. }
  187. instance->last_msg_seq_num++;
  188. qdevice_net_log(LOG_DEBUG, "Sending membership node list seq = %"PRIu32", "
  189. "ringid = (%"PRIx32".%"PRIx64").", instance->last_msg_seq_num,
  190. ring_id->node_id, ring_id->seq);
  191. qdevice_net_log_debug_node_list(&nlist);
  192. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  193. TLV_NODE_LIST_TYPE_MEMBERSHIP,
  194. 1, ring_id, 0, 0, 0, 0, &nlist) == 0) {
  195. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for membership list msg");
  196. node_list_free(&nlist);
  197. return (-1);
  198. }
  199. memcpy(&instance->last_sent_ring_id, ring_id, sizeof(instance->last_sent_ring_id));
  200. node_list_free(&nlist);
  201. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  202. return (0);
  203. }
  204. int
  205. qdevice_net_send_quorum_node_list(struct qdevice_net_instance *instance,
  206. enum tlv_quorate quorate,
  207. uint32_t node_list_entries, votequorum_node_t node_list[])
  208. {
  209. struct node_list nlist;
  210. struct send_buffer_list_entry *send_buffer;
  211. uint32_t i;
  212. node_list_init(&nlist);
  213. for (i = 0; i < node_list_entries; i++) {
  214. if (node_list[i].nodeid == 0) {
  215. continue;
  216. }
  217. if (node_list_add(&nlist, node_list[i].nodeid, 0,
  218. qdevice_net_votequorum_node_state_to_tlv(node_list[i].state)) == NULL) {
  219. qdevice_net_log(LOG_ERR, "Can't allocate quorum node list.");
  220. node_list_free(&nlist);
  221. return (-1);
  222. }
  223. }
  224. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  225. if (send_buffer == NULL) {
  226. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for quorum "
  227. "node list msg");
  228. node_list_free(&nlist);
  229. return (-1);
  230. }
  231. instance->last_msg_seq_num++;
  232. qdevice_net_log(LOG_DEBUG, "Sending quorum node list seq = %"PRIu32", quorate = %u",
  233. instance->last_msg_seq_num, quorate);
  234. qdevice_net_log_debug_node_list(&nlist);
  235. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  236. TLV_NODE_LIST_TYPE_QUORUM,
  237. 0, NULL, 0, 0, 1, quorate, &nlist) == 0) {
  238. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for quorum list msg");
  239. node_list_free(&nlist);
  240. return (-1);
  241. }
  242. node_list_free(&nlist);
  243. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  244. return (0);
  245. }