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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. case TLV_VOTE_NO_CHANGE:
  55. default:
  56. errx(1, "qdevice_net_timer_cast_vote: Unhandled cast_vote_timer_vote %u\n",
  57. instance->cast_vote_timer_vote);
  58. break;
  59. }
  60. res = votequorum_qdevice_poll(instance->votequorum_handle,
  61. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME, cast_vote,
  62. instance->last_received_votequorum_ring_id);
  63. if (res != CS_OK && res != CS_ERR_TRY_AGAIN) {
  64. if (res == CS_ERR_MESSAGE_ERROR) {
  65. qdevice_net_log(LOG_INFO, "votequorum_qdevice_poll called with old ring id,"
  66. " rescheduling timer");
  67. } else {
  68. qdevice_net_log(LOG_CRIT, "Can't call votequorum_qdevice_poll. Error %u",
  69. res);
  70. instance->schedule_disconnect = 1;
  71. return (0);
  72. }
  73. }
  74. /*
  75. * Schedule this function callback again
  76. */
  77. return (-1);
  78. }
  79. int
  80. qdevice_net_cast_vote_timer_update(struct qdevice_net_instance *instance, enum tlv_vote vote)
  81. {
  82. int timer_needs_running;
  83. switch (vote) {
  84. case TLV_VOTE_ACK:
  85. case TLV_VOTE_NACK:
  86. timer_needs_running = 1;
  87. break;
  88. case TLV_VOTE_WAIT_FOR_REPLY:
  89. case TLV_VOTE_ASK_LATER:
  90. timer_needs_running = 0;
  91. break;
  92. case TLV_VOTE_NO_CHANGE:
  93. errx(1, "qdevice_net_cast_vote_timer_update_vote: Incorrect vote parameter %u\n",
  94. vote);
  95. break;
  96. default:
  97. errx(1, "qdevice_net_cast_vote_timer_update_vote: Unhandled vote parameter %u\n",
  98. vote);
  99. break;
  100. }
  101. instance->cast_vote_timer_vote = vote;
  102. if (timer_needs_running) {
  103. if (instance->cast_vote_timer == NULL) {
  104. instance->cast_vote_timer = timer_list_add(&instance->main_timer_list,
  105. instance->cast_vote_timer_interval,
  106. qdevice_net_cast_vote_timer_callback, (void *)instance, NULL);
  107. if (instance->cast_vote_timer == NULL) {
  108. qdevice_net_log(LOG_ERR, "Can't schedule sending of "
  109. "votequorum poll");
  110. return (-1);
  111. } else {
  112. qdevice_net_log(LOG_DEBUG, "Cast vote timer is now scheduled every "
  113. "%"PRIu32"ms.", instance->cast_vote_timer_interval);
  114. }
  115. }
  116. if (qdevice_net_cast_vote_timer_callback((void *)instance, NULL) != -1) {
  117. return (-1);
  118. }
  119. } else {
  120. if (instance->cast_vote_timer != NULL) {
  121. timer_list_delete(&instance->main_timer_list, instance->cast_vote_timer);
  122. instance->cast_vote_timer = NULL;
  123. qdevice_net_log(LOG_DEBUG, "Cast vote timer is now stopped.");
  124. } else {
  125. qdevice_net_log(LOG_DEBUG, "Cast vote timer remains stopped.");
  126. }
  127. }
  128. return (0);
  129. }