qdevice-model.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_run(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_run unhandled model");
  64. exit(EXIT_FAILURE);
  65. }
  66. return (qdevice_model_array[instance->model_type]->run(instance));
  67. }
  68. int
  69. qdevice_model_get_config_node_list_failed(struct qdevice_instance *instance)
  70. {
  71. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  72. qdevice_model_array[instance->model_type] == NULL) {
  73. log(LOG_CRIT, "qdevice_model_run unhandled model");
  74. exit(EXIT_FAILURE);
  75. }
  76. return (qdevice_model_array[instance->model_type]->get_config_node_list_failed(instance));
  77. }
  78. int
  79. qdevice_model_config_node_list_changed(struct qdevice_instance *instance,
  80. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  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_run unhandled model");
  85. exit(EXIT_FAILURE);
  86. }
  87. return (qdevice_model_array[instance->model_type]->
  88. config_node_list_changed(instance, nlist, config_version_set, config_version));
  89. }
  90. int
  91. qdevice_model_votequorum_quorum_notify(struct qdevice_instance *instance,
  92. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  93. {
  94. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  95. qdevice_model_array[instance->model_type] == NULL) {
  96. log(LOG_CRIT, "qdevice_model_votequorum_quorum_notify unhandled model");
  97. exit(EXIT_FAILURE);
  98. }
  99. return (qdevice_model_array[instance->model_type]->
  100. votequorum_quorum_notify(instance, quorate, node_list_entries, node_list));
  101. }
  102. int
  103. qdevice_model_votequorum_node_list_notify(struct qdevice_instance *instance,
  104. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  105. {
  106. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  107. qdevice_model_array[instance->model_type] == NULL) {
  108. log(LOG_CRIT, "qdevice_model_votequorum_node_list_notify unhandled model");
  109. exit(EXIT_FAILURE);
  110. }
  111. return (qdevice_model_array[instance->model_type]->
  112. votequorum_node_list_notify(instance, votequorum_ring_id, node_list_entries, node_list));
  113. }
  114. int
  115. qdevice_model_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance,
  116. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[],
  117. enum qdevice_heuristics_exec_result heuristics_exec_result)
  118. {
  119. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  120. qdevice_model_array[instance->model_type] == NULL) {
  121. log(LOG_CRIT, "qdevice_model_votequorum_node_list_heuristics_notify unhandled model");
  122. exit(EXIT_FAILURE);
  123. }
  124. return (qdevice_model_array[instance->model_type]->
  125. votequorum_node_list_heuristics_notify(instance, votequorum_ring_id, node_list_entries,
  126. node_list, heuristics_exec_result));
  127. }
  128. int
  129. qdevice_model_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  130. uint32_t expected_votes)
  131. {
  132. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  133. qdevice_model_array[instance->model_type] == NULL) {
  134. log(LOG_CRIT, "qdevice_model_votequorum_expected_votes_notify unhandled model");
  135. exit(EXIT_FAILURE);
  136. }
  137. return (qdevice_model_array[instance->model_type]->
  138. votequorum_expected_votes_notify(instance, expected_votes));
  139. }
  140. int
  141. qdevice_model_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose)
  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_ipc_cmd_status unhandled model");
  146. exit(EXIT_FAILURE);
  147. }
  148. return (qdevice_model_array[instance->model_type]->
  149. ipc_cmd_status(instance, outbuf, verbose));
  150. }
  151. int
  152. qdevice_model_cmap_changed(struct qdevice_instance *instance,
  153. const struct qdevice_cmap_change_events *events)
  154. {
  155. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  156. qdevice_model_array[instance->model_type] == NULL) {
  157. log(LOG_CRIT, "qdevice_model_cmap_chaged unhandled model");
  158. exit(EXIT_FAILURE);
  159. }
  160. return (qdevice_model_array[instance->model_type]->
  161. cmap_changed(instance, events));
  162. }
  163. int
  164. qdevice_model_register(enum qdevice_model_type model_type,
  165. struct qdevice_model *model)
  166. {
  167. if (model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE) {
  168. return (-1);
  169. }
  170. if (qdevice_model_array[model_type] != NULL) {
  171. return (-1);
  172. }
  173. qdevice_model_array[model_type] = model;
  174. return (0);
  175. }
  176. void
  177. qdevice_model_register_all(void)
  178. {
  179. if (qdevice_model_net_register() != 0) {
  180. log(LOG_CRIT, "Failed to register model 'net' ");
  181. exit(EXIT_FAILURE);
  182. }
  183. }
  184. int
  185. qdevice_model_str_to_type(const char *str, enum qdevice_model_type *model_type)
  186. {
  187. int i;
  188. for (i = 0; i < QDEVICE_MODEL_TYPE_ARRAY_SIZE; i++) {
  189. if (qdevice_model_array[i] != NULL &&
  190. strcmp(qdevice_model_array[i]->name, str) == 0) {
  191. *model_type = i;
  192. return (0);
  193. }
  194. }
  195. return (-1);
  196. }
  197. const char *
  198. qdevice_model_type_to_str(enum qdevice_model_type model_type)
  199. {
  200. switch (model_type) {
  201. case QDEVICE_MODEL_TYPE_NET: return ("Net"); break;
  202. case QDEVICE_MODEL_TYPE_ARRAY_SIZE: return ("Unknown model"); break;
  203. /*
  204. * Default is not defined intentionally. Compiler shows warning when new model is added
  205. */
  206. }
  207. return ("Unknown model");
  208. }