qdevice-net-algorithm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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-net-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_net_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_net_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_votequorum_node_list_notify(struct qdevice_net_instance *instance,
  65. const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[],
  66. 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_net_log(LOG_CRIT, "qdevice_net_algorithm_votequorum_node_list_notify "
  71. "unhandled decision algorithm");
  72. exit(1);
  73. }
  74. return (qdevice_net_algorithm_array[instance->decision_algorithm]->votequorum_node_list_notify(
  75. instance, ring_id, node_list_entries, node_list, vote));
  76. }
  77. int
  78. qdevice_net_algorithm_votequorum_quorum_notify(struct qdevice_net_instance *instance,
  79. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[], int *send_node_list)
  80. {
  81. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  82. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  83. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_votequorum_quorum_notify "
  84. "unhandled decision algorithm");
  85. exit(1);
  86. }
  87. return (qdevice_net_algorithm_array[instance->decision_algorithm]->votequorum_quorum_notify(
  88. instance, quorate, node_list_entries, node_list, send_node_list));
  89. }
  90. int
  91. qdevice_net_algorithm_config_node_list_reply_received(struct qdevice_net_instance *instance,
  92. uint32_t seq_number, int initial, enum tlv_vote *vote)
  93. {
  94. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  95. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  96. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_config_node_list_reply_received "
  97. "unhandled decision algorithm");
  98. exit(1);
  99. }
  100. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  101. config_node_list_reply_received(instance, seq_number, initial, vote));
  102. }
  103. int
  104. qdevice_net_algorithm_membership_node_list_reply_received(struct qdevice_net_instance *instance,
  105. uint32_t seq_number, const struct tlv_ring_id *ring_id, enum tlv_vote *vote)
  106. {
  107. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  108. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  109. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_membership_node_list_reply_received "
  110. "unhandled decision algorithm");
  111. exit(1);
  112. }
  113. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  114. membership_node_list_reply_received(instance, seq_number, ring_id, vote));
  115. }
  116. int
  117. qdevice_net_algorithm_quorum_node_list_reply_received(struct qdevice_net_instance *instance,
  118. uint32_t seq_number, enum tlv_vote *vote)
  119. {
  120. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  121. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  122. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_quorum_node_list_reply_received "
  123. "unhandled decision algorithm");
  124. exit(1);
  125. }
  126. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  127. quorum_node_list_reply_received(instance, seq_number, vote));
  128. }
  129. int
  130. qdevice_net_algorithm_ask_for_vote_reply_received(struct qdevice_net_instance *instance,
  131. uint32_t seq_number, enum tlv_vote *vote)
  132. {
  133. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  134. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  135. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_ask_for_vote_reply_received "
  136. "unhandled decision algorithm");
  137. exit(1);
  138. }
  139. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  140. ask_for_vote_reply_received(instance, seq_number, vote));
  141. }
  142. int
  143. qdevice_net_algorithm_vote_info_received(struct qdevice_net_instance *instance,
  144. uint32_t seq_number, enum tlv_vote *vote)
  145. {
  146. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  147. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  148. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_vote_info_received "
  149. "unhandled decision algorithm");
  150. exit(1);
  151. }
  152. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  153. vote_info_received(instance, seq_number, vote));
  154. }
  155. int
  156. qdevice_net_algorithm_echo_reply_received(struct qdevice_net_instance *instance,
  157. uint32_t seq_number, int is_expected_seq_number)
  158. {
  159. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  160. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  161. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_echo_reply_received "
  162. "unhandled decision algorithm");
  163. exit(1);
  164. }
  165. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  166. echo_reply_received(instance, seq_number, is_expected_seq_number));
  167. }
  168. int
  169. qdevice_net_algorithm_echo_reply_not_received(struct qdevice_net_instance *instance)
  170. {
  171. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  172. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  173. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_echo_reply_not_received "
  174. "unhandled decision algorithm");
  175. exit(1);
  176. }
  177. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  178. echo_reply_not_received(instance));
  179. }
  180. int
  181. qdevice_net_algorithm_disconnected(struct qdevice_net_instance *instance,
  182. enum qdevice_net_disconnect_reason disconnect_reason, int *try_reconnect)
  183. {
  184. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  185. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  186. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_disconnected "
  187. "unhandled decision algorithm");
  188. exit(1);
  189. }
  190. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  191. disconnected(instance, disconnect_reason, try_reconnect));
  192. }
  193. void
  194. qdevice_net_algorithm_destroy(struct qdevice_net_instance *instance)
  195. {
  196. if (instance->decision_algorithm >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE ||
  197. qdevice_net_algorithm_array[instance->decision_algorithm] == NULL) {
  198. qdevice_net_log(LOG_CRIT, "qdevice_net_algorithm_destroy "
  199. "unhandled decision algorithm");
  200. exit(1);
  201. }
  202. return (qdevice_net_algorithm_array[instance->decision_algorithm]->
  203. destroy(instance));
  204. }
  205. int
  206. qdevice_net_algorithm_register(enum tlv_decision_algorithm_type algorithm_number,
  207. struct qdevice_net_algorithm *algorithm)
  208. {
  209. if (algorithm_number >= QDEVICE_NET_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) {
  210. return (-1);
  211. }
  212. if (qdevice_net_algorithm_array[algorithm_number] != NULL) {
  213. return (-1);
  214. }
  215. qdevice_net_algorithm_array[algorithm_number] = algorithm;
  216. return (0);
  217. }
  218. void
  219. qdevice_net_algorithm_register_all(void)
  220. {
  221. if (qdevice_net_algo_test_register() != 0) {
  222. qdevice_net_log(LOG_CRIT, "Failed to register decision algorithm 'test' ");
  223. exit(1);
  224. }
  225. if (qdevice_net_algo_ffsplit_register() != 0) {
  226. qdevice_net_log(LOG_CRIT, "Failed to register decision algorithm 'ffsplit' ");
  227. exit(1);
  228. }
  229. if (qdevice_net_algo_2nodelms_register() != 0) {
  230. qdevice_net_log(LOG_CRIT, "Failed to register decision algorithm '2nodelms' ");
  231. exit(1);
  232. }
  233. if (qdevice_net_algo_lms_register() != 0) {
  234. qdevice_net_log(LOG_CRIT, "Failed to register decision algorithm 'lms' ");
  235. exit(1);
  236. }
  237. }