qdevice-net-algorithm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <sys/types.h>
  35. #include "qnet-config.h"
  36. #include "qdevice-net-algorithm.h"
  37. #include "qdevice-log.h"
  38. #include "qdevice-net-algo-test.h"
  39. #include "qdevice-net-algo-ffsplit.h"
  40. #include "qdevice-net-algo-2nodelms.h"
  41. #include "qdevice-net-algo-lms.h"
  42. static struct qdevice_net_algorithm *qdevice_net_algorithm_array[QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE];
  43. int
  44. qdevice_net_algorithm_init(struct qdevice_net_instance *instance)
  45. {
  46. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  47. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  48. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_init unhandled decision algorithm");
  49. exit(1);
  50. }
  51. return (qdevice_net_algorithm_array[instance->decision_algorithm]->init(instance));
  52. }
  53. int
  54. qdevice_net_algorithm_connected(struct qdevice_net_instance *instance)
  55. {
  56. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  57. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  58. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_connected unhandled decision algorithm");
  59. exit(1);
  60. }
  61. return (qdevice_net_algorithm_array[instance->decision_algorithm]->connected(instance));
  62. }
  63. int
  64. qdevice_net_algorithm_config_node_list_changed(struct qdevice_net_instance *instance,
  65. const struct node_list *nlist, int config_version_set, uint64_t config_version,
  66. int *send_node_list, enum tlv_vote *vote)
  67. {
  68. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  69. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  70. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_connected unhandled decision algorithm");
  71. exit(1);
  72. }
  73. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  74. config_node_list_changed(instance, nlist, config_version_set, config_version,
  75. send_node_list, vote));
  76. }
  77. int
  78. qdevice_net_algorithm_votequorum_node_list_notify(struct qdevice_net_instance *instance,
  79. const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[],
  80. int *send_node_list, enum tlv_vote *vote)
  81. {
  82. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  83. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  84. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_votequorum_node_list_notify "
  85. "unhandled decision algorithm");
  86. exit(1);
  87. }
  88. return (qdevice_net_algorithm_array[instance->decision_algorithm]->votequorum_node_list_notify(
  89. instance, ring_id, node_list_entries, node_list, send_node_list, vote));
  90. }
  91. int
  92. qdevice_net_algorithm_votequorum_quorum_notify(struct qdevice_net_instance *instance,
  93. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[], int *send_node_list,
  94. enum tlv_vote *vote)
  95. {
  96. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  97. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  98. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_votequorum_quorum_notify "
  99. "unhandled decision algorithm");
  100. exit(1);
  101. }
  102. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  103. votequorum_quorum_notify(instance, quorate, node_list_entries, node_list,
  104. send_node_list, vote));
  105. }
  106. int
  107. qdevice_net_algorithm_config_node_list_reply_received(struct qdevice_net_instance *instance,
  108. uint32_t seq_number, int initial, enum tlv_vote *vote)
  109. {
  110. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  111. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  112. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_config_node_list_reply_received "
  113. "unhandled decision algorithm");
  114. exit(1);
  115. }
  116. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  117. config_node_list_reply_received(instance, seq_number, initial, vote));
  118. }
  119. int
  120. qdevice_net_algorithm_membership_node_list_reply_received(struct qdevice_net_instance *instance,
  121. uint32_t seq_number, const struct tlv_ring_id *ring_id, enum tlv_vote *vote)
  122. {
  123. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  124. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  125. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_membership_node_list_reply_received "
  126. "unhandled decision algorithm");
  127. exit(1);
  128. }
  129. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  130. membership_node_list_reply_received(instance, seq_number, ring_id, vote));
  131. }
  132. int
  133. qdevice_net_algorithm_quorum_node_list_reply_received(struct qdevice_net_instance *instance,
  134. uint32_t seq_number, enum tlv_vote *vote)
  135. {
  136. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  137. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  138. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_quorum_node_list_reply_received "
  139. "unhandled decision algorithm");
  140. exit(1);
  141. }
  142. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  143. quorum_node_list_reply_received(instance, seq_number, vote));
  144. }
  145. int
  146. qdevice_net_algorithm_ask_for_vote_reply_received(struct qdevice_net_instance *instance,
  147. uint32_t seq_number, enum tlv_vote *vote)
  148. {
  149. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  150. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  151. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_ask_for_vote_reply_received "
  152. "unhandled decision algorithm");
  153. exit(1);
  154. }
  155. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  156. ask_for_vote_reply_received(instance, seq_number, vote));
  157. }
  158. int
  159. qdevice_net_algorithm_vote_info_received(struct qdevice_net_instance *instance,
  160. uint32_t seq_number, enum tlv_vote *vote)
  161. {
  162. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  163. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  164. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_vote_info_received "
  165. "unhandled decision algorithm");
  166. exit(1);
  167. }
  168. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  169. vote_info_received(instance, seq_number, vote));
  170. }
  171. int
  172. qdevice_net_algorithm_echo_reply_received(struct qdevice_net_instance *instance,
  173. uint32_t seq_number, int is_expected_seq_number)
  174. {
  175. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  176. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  177. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_echo_reply_received "
  178. "unhandled decision algorithm");
  179. exit(1);
  180. }
  181. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  182. echo_reply_received(instance, seq_number, is_expected_seq_number));
  183. }
  184. int
  185. qdevice_net_algorithm_echo_reply_not_received(struct qdevice_net_instance *instance)
  186. {
  187. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  188. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  189. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_echo_reply_not_received "
  190. "unhandled decision algorithm");
  191. exit(1);
  192. }
  193. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  194. echo_reply_not_received(instance));
  195. }
  196. int
  197. qdevice_net_algorithm_disconnected(struct qdevice_net_instance *instance,
  198. enum qdevice_net_disconnect_reason disconnect_reason, int *try_reconnect)
  199. {
  200. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  201. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  202. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_disconnected "
  203. "unhandled decision algorithm");
  204. exit(1);
  205. }
  206. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  207. disconnected(instance, disconnect_reason, try_reconnect));
  208. }
  209. void
  210. qdevice_net_algorithm_destroy(struct qdevice_net_instance *instance)
  211. {
  212. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  213. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  214. qdevice_log(LOG_CRIT, "qdevice_net_algorithm_destroy "
  215. "unhandled decision algorithm");
  216. exit(1);
  217. }
  218. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  219. destroy(instance));
  220. }
  221. int
  222. qdevice_net_algorithm_register(enum tlv_decision_algorithm_type algorithm_number,
  223. struct qdevice_net_algorithm *algorithm)
  224. {
  225. if (algorithm_number >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) {
  226. return (-1);
  227. }
  228. if (qdevice_net_algorithm_array[algorithm_number] != NULL) {
  229. return (-1);
  230. }
  231. qdevice_net_algorithm_array[algorithm_number] = algorithm;
  232. return (0);
  233. }
  234. int
  235. qdevice_net_algorithm_register_all(void)
  236. {
  237. if (qdevice_net_algo_test_register() != 0) {
  238. qdevice_log(LOG_CRIT, "Failed to register decision algorithm 'test' ");
  239. return (-1);
  240. }
  241. if (qdevice_net_algo_ffsplit_register() != 0) {
  242. qdevice_log(LOG_CRIT, "Failed to register decision algorithm 'ffsplit' ");
  243. return (-1);
  244. }
  245. if (qdevice_net_algo_2nodelms_register() != 0) {
  246. qdevice_log(LOG_CRIT, "Failed to register decision algorithm '2nodelms' ");
  247. return (-1);
  248. }
  249. if (qdevice_net_algo_lms_register() != 0) {
  250. qdevice_log(LOG_CRIT, "Failed to register decision algorithm 'lms' ");
  251. return (-1);
  252. }
  253. return (0);
  254. }