qdevice-net-algo-test.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <string.h>
  36. #include "qdevice-net-algo-test.h"
  37. #include "qdevice-net-log.h"
  38. #include "qdevice-net-send.h"
  39. #include "qdevice-net-cast-vote-timer.h"
  40. /*
  41. * Called after qdevice_net_instance is initialized. Connection to server is not yet
  42. * established. Used mainly for allocating instance->algorithm_data.
  43. *
  44. * Callback should return 0 on success or -1 on failure.
  45. */
  46. int
  47. qdevice_net_algo_test_init(struct qdevice_net_instance *instance)
  48. {
  49. instance->algorithm_data = NULL;
  50. qnetd_log(LOG_INFO, "algo-test: Initialized");
  51. return (0);
  52. }
  53. /*
  54. * Called after qdevice connected to qnetd.
  55. *
  56. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  57. */
  58. int
  59. qdevice_net_algo_test_connected(struct qdevice_net_instance *instance)
  60. {
  61. qnetd_log(LOG_INFO, "algo-test: Connected");
  62. return (0);
  63. }
  64. /*
  65. * Called after votequorum node list notify is dispatched.
  66. *
  67. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  68. *
  69. * Main responsibility for this function is to set vote variable (variable is set by caller to
  70. * TLV_VOTE_WAIT_FOR_REPLY).
  71. *
  72. * TLV_VOTE_ACK = do not send list and vote ACK
  73. * TLV_VOTE_NACK = do not send list and vote NACK
  74. * TLV_VOTE_NO_CHANGE = do not send list and no change in vote
  75. * TLV_VOTE_WAIT_FOR_REPLY = send list and wait with voting until reply is received
  76. * TLV_VOTE_ASK_LATER = send list but do not change vote
  77. */
  78. int
  79. qdevice_net_algo_test_votequorum_node_list_notify(struct qdevice_net_instance *instance,
  80. const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[],
  81. enum tlv_vote *vote)
  82. {
  83. qnetd_log(LOG_INFO, "algo-test: Votequorum list notify");
  84. *vote = TLV_VOTE_WAIT_FOR_REPLY;
  85. return (0);
  86. }
  87. /*
  88. * Called after votequorum quorum notify is dispatched.
  89. *
  90. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  91. *
  92. * Main responsibility is to set send_node_list (set by caller to 1). If send_node_list is set to
  93. * non zero, node list is send to qnetd
  94. */
  95. int
  96. qdevice_net_algo_test_votequorum_quorum_notify(struct qdevice_net_instance *instance,
  97. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[], int *send_node_list)
  98. {
  99. qnetd_log(LOG_INFO, "algo-test: Votequorum quorum notify");
  100. *send_node_list = 1;
  101. return (0);
  102. }
  103. /*
  104. * Called when config node list reply is received. Vote is set to value returned by server (and can
  105. * be overwriten by algorithm).
  106. *
  107. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  108. */
  109. int
  110. qdevice_net_algo_test_config_node_list_reply_received(struct qdevice_net_instance *instance,
  111. uint32_t seq_number, int initial, enum tlv_vote *vote)
  112. {
  113. qnetd_log(LOG_INFO, "algo-test: Config node list reply");
  114. return (0);
  115. }
  116. /*
  117. * Called when membership node list reply (reply for votequorum votequorum_nodelist_notify_fn)
  118. * is received. Vote is set to value returned by server (and can be overwriten by algorithm).
  119. *
  120. * Also if server returned TLV_VOTE_ASK_LATER, it's good idea to create timer (call timer_list_add
  121. * with instance->main_timer_list parameter) and ask for reply (qdevice_net_send_ask_for_vote).
  122. * Another option may be to wait for vote_info message (if server algorithm is configured so).
  123. *
  124. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  125. */
  126. int
  127. qdevice_net_algo_test_membership_node_list_reply_received(struct qdevice_net_instance *instance,
  128. uint32_t seq_number, const struct tlv_ring_id *ring_id, enum tlv_vote *vote)
  129. {
  130. qnetd_log(LOG_INFO, "algo-test: Membership node list reply");
  131. return (0);
  132. }
  133. /*
  134. * Called when quorum node list reply (reply for votequorum votequorum_quorum_notify_fn)
  135. * is received. Vote is set to value returned by server (and can be overwriten by algorithm).
  136. *
  137. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  138. */
  139. int
  140. qdevice_net_algo_test_quorum_node_list_reply_received(struct qdevice_net_instance *instance,
  141. uint32_t seq_number, enum tlv_vote *vote)
  142. {
  143. qnetd_log(LOG_INFO, "algo-test: Quorum node list reply");
  144. return (0);
  145. }
  146. /*
  147. * Called when reply for ask for vote message was received.
  148. * Vote is set to value returned by server (and can be overwriten by algorithm).
  149. *
  150. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  151. */
  152. int
  153. qdevice_net_algo_test_ask_for_vote_reply_received(struct qdevice_net_instance *instance,
  154. uint32_t seq_number, enum tlv_vote *vote)
  155. {
  156. qnetd_log(LOG_INFO, "algo-test: Ask for vote reply received");
  157. return (0);
  158. }
  159. /*
  160. * Called when vote info message from server was received.
  161. * Vote is set to value sent by server (and can be overwriten by algorithm).
  162. *
  163. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  164. */
  165. int
  166. qdevice_net_algo_test_vote_info_received(struct qdevice_net_instance *instance,
  167. uint32_t seq_number, enum tlv_vote *vote)
  168. {
  169. qnetd_log(LOG_INFO, "algo-test: Vote info received");
  170. return (0);
  171. }
  172. /*
  173. * Called when echo reply message was received.
  174. * is_expected_seq_number is set to 1 if received seq_number was equal to last sent echo request.
  175. *
  176. * Callback should return 0 on success or -1 on failure (-> disconnect client).
  177. */
  178. int
  179. qdevice_net_algo_test_echo_reply_received(struct qdevice_net_instance *instance,
  180. uint32_t seq_number, int is_expected_seq_number)
  181. {
  182. qnetd_log(LOG_INFO, "algo-test: Echo reply received");
  183. return (is_expected_seq_number ? 0 : -1);
  184. }
  185. /*
  186. * Called when client is about to send echo request but echo reply to previous echo request
  187. * was not yet received.
  188. *
  189. * Callback should return 0 if processing should continue (echo request is not send but timer is
  190. * scheduled again) otherwise -1 (-> disconnect client).
  191. */
  192. int
  193. qdevice_net_algo_test_echo_reply_not_received(struct qdevice_net_instance *instance)
  194. {
  195. qnetd_log(LOG_INFO, "algo-test: Echo reply not received");
  196. return (-1);
  197. }
  198. /*
  199. * Called when client disconnect from server.
  200. *
  201. * disconnect_reason contains one of QDEVICE_NET_DISCONNECT_REASON_
  202. * try_reconnect can be set to non zero value if reconnect to server should be tried
  203. *
  204. * Callback should return 0 on success, -1 on failure (-> force exit)
  205. */
  206. int
  207. qdevice_net_algo_test_disconnected(struct qdevice_net_instance *instance,
  208. enum qdevice_net_disconnect_reason disconnect_reason, int *try_reconnect)
  209. {
  210. qnetd_log(LOG_INFO, "algo-test: Disconnected");
  211. return (0);
  212. }
  213. /*
  214. * Called when qdevice-net is going down.
  215. */
  216. void
  217. qdevice_net_algo_test_destroy(struct qdevice_net_instance *instance)
  218. {
  219. qnetd_log(LOG_INFO, "algo-test: Destroy");
  220. }
  221. static struct qdevice_net_algorithm qdevice_net_algo_test = {
  222. .init = qdevice_net_algo_test_init,
  223. .connected = qdevice_net_algo_test_connected,
  224. .votequorum_node_list_notify = qdevice_net_algo_test_votequorum_node_list_notify,
  225. .votequorum_quorum_notify = qdevice_net_algo_test_votequorum_quorum_notify,
  226. .config_node_list_reply_received = qdevice_net_algo_test_config_node_list_reply_received,
  227. .membership_node_list_reply_received = qdevice_net_algo_test_membership_node_list_reply_received,
  228. .quorum_node_list_reply_received = qdevice_net_algo_test_quorum_node_list_reply_received,
  229. .ask_for_vote_reply_received = qdevice_net_algo_test_ask_for_vote_reply_received,
  230. .vote_info_received = qdevice_net_algo_test_vote_info_received,
  231. .echo_reply_received = qdevice_net_algo_test_echo_reply_received,
  232. .echo_reply_not_received = qdevice_net_algo_test_echo_reply_not_received,
  233. .disconnected = qdevice_net_algo_test_disconnected,
  234. .destroy = qdevice_net_algo_test_destroy,
  235. };
  236. int
  237. qdevice_net_algo_test_register(void)
  238. {
  239. return (qdevice_net_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_TEST, &qdevice_net_algo_test));
  240. }