qdevice-net-cast-vote-timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2015 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 <err.h>
  35. #include "qnet-config.h"
  36. #include "qdevice-net-log.h"
  37. #include "qdevice-net-cast-vote-timer.h"
  38. static int
  39. qdevice_net_cast_vote_timer_callback(void *data1, void *data2)
  40. {
  41. struct qdevice_net_instance *instance;
  42. cs_error_t res;
  43. int cast_vote;
  44. instance = (struct qdevice_net_instance *)data1;
  45. switch (instance->cast_vote_timer_vote) {
  46. case TLV_VOTE_ACK:
  47. cast_vote = 1;
  48. break;
  49. case TLV_VOTE_NACK:
  50. cast_vote = 0;
  51. break;
  52. case TLV_VOTE_ASK_LATER:
  53. case TLV_VOTE_WAIT_FOR_REPLY:
  54. default:
  55. errx(1, "qdevice_net_timer_cast_vote: Unhandled cast_vote_timer_vote %u\n",
  56. instance->cast_vote_timer_vote);
  57. break;
  58. }
  59. res = votequorum_qdevice_poll(instance->votequorum_handle,
  60. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME, cast_vote,
  61. instance->last_received_votequorum_ring_id);
  62. if (res != CS_OK && res != CS_ERR_TRY_AGAIN) {
  63. qdevice_net_log(LOG_CRIT, "Can't call votequorum_qdevice_poll. Error %u", res);
  64. instance->schedule_disconnect = 1;
  65. return (0);
  66. }
  67. /*
  68. * Schedule this function callback again
  69. */
  70. return (-1);
  71. }
  72. int
  73. qdevice_net_cast_vote_timer_update(struct qdevice_net_instance *instance, enum tlv_vote vote)
  74. {
  75. int timer_needs_running;
  76. switch (vote) {
  77. case TLV_VOTE_ACK:
  78. case TLV_VOTE_NACK:
  79. timer_needs_running = 1;
  80. break;
  81. case TLV_VOTE_WAIT_FOR_REPLY:
  82. case TLV_VOTE_ASK_LATER:
  83. timer_needs_running = 0;
  84. break;
  85. default:
  86. errx(1, "qdevice_net_cast_vote_timer_update_vote: Unhandled vote parameter %u\n",
  87. vote);
  88. break;
  89. }
  90. instance->cast_vote_timer_vote = vote;
  91. if (timer_needs_running) {
  92. if (instance->cast_vote_timer == NULL) {
  93. instance->cast_vote_timer = timer_list_add(&instance->main_timer_list,
  94. instance->cast_vote_timer_interval,
  95. qdevice_net_cast_vote_timer_callback, (void *)instance, NULL);
  96. if (instance->cast_vote_timer == NULL) {
  97. qdevice_net_log(LOG_ERR, "Can't schedule sending of votequroum poll");
  98. return (-1);
  99. }
  100. }
  101. if (qdevice_net_cast_vote_timer_callback((void *)instance, NULL) != -1) {
  102. return (-1);
  103. }
  104. } else {
  105. if (instance->cast_vote_timer != NULL) {
  106. timer_list_delete(&instance->main_timer_list, instance->cast_vote_timer);
  107. instance->cast_vote_timer = NULL;
  108. }
  109. }
  110. return (0);
  111. }