qdevice-model.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2015-2020 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 "log.h"
  35. #include "qdevice-model.h"
  36. #include "qdevice-model-net.h"
  37. static struct qdevice_model *qdevice_model_array[QDEVICE_MODEL_TYPE_ARRAY_SIZE];
  38. int
  39. qdevice_model_init(struct qdevice_instance *instance)
  40. {
  41. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  42. qdevice_model_array[instance->model_type] == NULL) {
  43. log(LOG_CRIT, "qdevice_model_init unhandled model");
  44. exit(EXIT_FAILURE);
  45. }
  46. return (qdevice_model_array[instance->model_type]->init(instance));
  47. }
  48. int
  49. qdevice_model_destroy(struct qdevice_instance *instance)
  50. {
  51. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  52. qdevice_model_array[instance->model_type] == NULL) {
  53. log(LOG_CRIT, "qdevice_model_destroy unhandled model");
  54. exit(EXIT_FAILURE);
  55. }
  56. return (qdevice_model_array[instance->model_type]->destroy(instance));
  57. }
  58. int
  59. qdevice_model_pre_poll_loop(struct qdevice_instance *instance)
  60. {
  61. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  62. qdevice_model_array[instance->model_type] == NULL) {
  63. log(LOG_CRIT, "qdevice_model_pre_poll_loop unhandled model");
  64. exit(EXIT_FAILURE);
  65. }
  66. return (qdevice_model_array[instance->model_type]->pre_poll_loop(instance));
  67. }
  68. int
  69. qdevice_model_post_poll_loop(struct qdevice_instance *instance,
  70. enum qdevice_model_post_poll_loop_exit_reason exit_reason)
  71. {
  72. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  73. qdevice_model_array[instance->model_type] == NULL) {
  74. log(LOG_CRIT, "qdevice_model_post_poll_loop unhandled model");
  75. exit(EXIT_FAILURE);
  76. }
  77. return (qdevice_model_array[instance->model_type]->post_poll_loop(instance, exit_reason));
  78. }
  79. int
  80. qdevice_model_get_config_node_list_failed(struct qdevice_instance *instance)
  81. {
  82. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  83. qdevice_model_array[instance->model_type] == NULL) {
  84. log(LOG_CRIT, "qdevice_model_get_config_node_list_failed unhandled model");
  85. exit(EXIT_FAILURE);
  86. }
  87. return (qdevice_model_array[instance->model_type]->get_config_node_list_failed(instance));
  88. }
  89. int
  90. qdevice_model_config_node_list_changed(struct qdevice_instance *instance,
  91. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  92. {
  93. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  94. qdevice_model_array[instance->model_type] == NULL) {
  95. log(LOG_CRIT, "qdevice_model_config_node_list_changed unhandled model");
  96. exit(EXIT_FAILURE);
  97. }
  98. return (qdevice_model_array[instance->model_type]->
  99. config_node_list_changed(instance, nlist, config_version_set, config_version));
  100. }
  101. int
  102. qdevice_model_votequorum_quorum_notify(struct qdevice_instance *instance,
  103. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  104. {
  105. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  106. qdevice_model_array[instance->model_type] == NULL) {
  107. log(LOG_CRIT, "qdevice_model_votequorum_quorum_notify unhandled model");
  108. exit(EXIT_FAILURE);
  109. }
  110. return (qdevice_model_array[instance->model_type]->
  111. votequorum_quorum_notify(instance, quorate, node_list_entries, node_list));
  112. }
  113. int
  114. qdevice_model_votequorum_node_list_notify(struct qdevice_instance *instance,
  115. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  116. {
  117. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  118. qdevice_model_array[instance->model_type] == NULL) {
  119. log(LOG_CRIT, "qdevice_model_votequorum_node_list_notify unhandled model");
  120. exit(EXIT_FAILURE);
  121. }
  122. return (qdevice_model_array[instance->model_type]->
  123. votequorum_node_list_notify(instance, votequorum_ring_id, node_list_entries, node_list));
  124. }
  125. int
  126. qdevice_model_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance,
  127. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[],
  128. enum qdevice_heuristics_exec_result heuristics_exec_result)
  129. {
  130. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  131. qdevice_model_array[instance->model_type] == NULL) {
  132. log(LOG_CRIT, "qdevice_model_votequorum_node_list_heuristics_notify unhandled model");
  133. exit(EXIT_FAILURE);
  134. }
  135. return (qdevice_model_array[instance->model_type]->
  136. votequorum_node_list_heuristics_notify(instance, votequorum_ring_id, node_list_entries,
  137. node_list, heuristics_exec_result));
  138. }
  139. int
  140. qdevice_model_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  141. uint32_t expected_votes)
  142. {
  143. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  144. qdevice_model_array[instance->model_type] == NULL) {
  145. log(LOG_CRIT, "qdevice_model_votequorum_expected_votes_notify unhandled model");
  146. exit(EXIT_FAILURE);
  147. }
  148. return (qdevice_model_array[instance->model_type]->
  149. votequorum_expected_votes_notify(instance, expected_votes));
  150. }
  151. int
  152. qdevice_model_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose)
  153. {
  154. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  155. qdevice_model_array[instance->model_type] == NULL) {
  156. log(LOG_CRIT, "qdevice_model_ipc_cmd_status unhandled model");
  157. exit(EXIT_FAILURE);
  158. }
  159. return (qdevice_model_array[instance->model_type]->
  160. ipc_cmd_status(instance, outbuf, verbose));
  161. }
  162. int
  163. qdevice_model_cmap_changed(struct qdevice_instance *instance,
  164. const struct qdevice_cmap_change_events *events)
  165. {
  166. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  167. qdevice_model_array[instance->model_type] == NULL) {
  168. log(LOG_CRIT, "qdevice_model_cmap_chaged unhandled model");
  169. exit(EXIT_FAILURE);
  170. }
  171. return (qdevice_model_array[instance->model_type]->
  172. cmap_changed(instance, events));
  173. }
  174. int
  175. qdevice_model_register(enum qdevice_model_type model_type,
  176. struct qdevice_model *model)
  177. {
  178. if (model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE) {
  179. return (-1);
  180. }
  181. if (qdevice_model_array[model_type] != NULL) {
  182. return (-1);
  183. }
  184. qdevice_model_array[model_type] = model;
  185. return (0);
  186. }
  187. void
  188. qdevice_model_register_all(void)
  189. {
  190. if (qdevice_model_net_register() != 0) {
  191. log(LOG_CRIT, "Failed to register model 'net' ");
  192. exit(EXIT_FAILURE);
  193. }
  194. }
  195. int
  196. qdevice_model_str_to_type(const char *str, enum qdevice_model_type *model_type)
  197. {
  198. int i;
  199. for (i = 0; i < QDEVICE_MODEL_TYPE_ARRAY_SIZE; i++) {
  200. if (qdevice_model_array[i] != NULL &&
  201. strcmp(qdevice_model_array[i]->name, str) == 0) {
  202. *model_type = i;
  203. return (0);
  204. }
  205. }
  206. return (-1);
  207. }
  208. const char *
  209. qdevice_model_type_to_str(enum qdevice_model_type model_type)
  210. {
  211. switch (model_type) {
  212. case QDEVICE_MODEL_TYPE_NET: return ("Net"); break;
  213. case QDEVICE_MODEL_TYPE_ARRAY_SIZE: return ("Unknown model"); break;
  214. /*
  215. * Default is not defined intentionally. Compiler shows warning when new model is added
  216. */
  217. }
  218. return ("Unknown model");
  219. }