igmp_timer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /****************************************************************************
  2. * net/igmp/igmp_timer.c
  3. *
  4. * Copyright (C) 2010-2011, 2014, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
  8. * lwIP TCP/IP stack by Steve Reynolds:
  9. *
  10. * Copyright (c) 2002 CITEL Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of CITEL Technologies Ltd nor the names of its
  23. * contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <nuttx/compiler.h>
  44. #include <assert.h>
  45. #include <debug.h>
  46. #include <nuttx/wdog.h>
  47. #include <nuttx/irq.h>
  48. #include <nuttx/wqueue.h>
  49. #include <nuttx/net/netconfig.h>
  50. #include <nuttx/net/net.h>
  51. #include <nuttx/net/netstats.h>
  52. #include <nuttx/net/igmp.h>
  53. #include "devif/devif.h"
  54. #include "igmp/igmp.h"
  55. #include "utils/utils.h"
  56. #ifdef CONFIG_NET_IGMP
  57. /****************************************************************************
  58. * Pre-processor Definitions
  59. ****************************************************************************/
  60. /* Debug ********************************************************************/
  61. #undef IGMP_GTMRDEBUG /* Define to enable detailed IGMP group debug */
  62. #ifndef CONFIG_NET_IGMP
  63. # undef IGMP_GTMRDEBUG
  64. #endif
  65. #ifdef IGMP_GTMRDEBUG
  66. # define gtmrerr nerr
  67. # define gtmrinfo ninfo
  68. #else
  69. # define gtmrerr _none
  70. # define gtmrinfo _none
  71. #endif
  72. /****************************************************************************
  73. * Private Functions
  74. ****************************************************************************/
  75. /****************************************************************************
  76. * Name: igmp_timeout_work
  77. *
  78. * Description:
  79. * Timeout watchdog work.
  80. *
  81. * Assumptions:
  82. * This function is called from a work queue thread.
  83. *
  84. ****************************************************************************/
  85. static void igmp_timeout_work(FAR void *arg)
  86. {
  87. FAR struct igmp_group_s *group;
  88. int ret;
  89. /* If the state is DELAYING_MEMBER then we send a report for this group */
  90. group = (FAR struct igmp_group_s *)arg;
  91. DEBUGASSERT(group != NULL);
  92. /* If the group exists and is no an IDLE MEMBER, then it must be a DELAYING
  93. * member. Race conditions are avoided because (1) the timer is not started
  94. * until after the first IGMPv2_MEMBERSHIP_REPORT during the join, and (2)
  95. * the timer is cancelled before sending the IGMP_LEAVE_GROUP during leave.
  96. */
  97. net_lock();
  98. if (!IS_IDLEMEMBER(group->flags))
  99. {
  100. /* Schedule (and forget) the Membership Report. NOTE:
  101. * Since we are executing from a timer interrupt, we cannot wait
  102. * for the message to be sent.
  103. */
  104. IGMP_STATINCR(g_netstats.igmp.report_sched);
  105. ret = igmp_schedmsg(group, IGMPv2_MEMBERSHIP_REPORT);
  106. if (ret < 0)
  107. {
  108. nerr("ERROR: Failed to schedule message: %d\n", ret);
  109. }
  110. /* Also note: The Membership Report is sent at most two times because
  111. * the timer is not reset here. Hmm.. does this mean that the group
  112. * is stranded if both reports were lost? This is consistent with the
  113. * RFC that states: "To cover the possibility of the initial Membership
  114. * Report being lost or damaged, it is recommended that it be repeated
  115. * once or twice after short delays [Unsolicited Report Interval]..."
  116. */
  117. }
  118. net_unlock();
  119. }
  120. /****************************************************************************
  121. * Name: igmp_timeout
  122. *
  123. * Description:
  124. * Timeout watchdog handler.
  125. *
  126. * Assumptions:
  127. * This function is called from the wdog timer handler which runs in the
  128. * context of the timer interrupt handler.
  129. *
  130. ****************************************************************************/
  131. static void igmp_timeout(wdparm_t arg)
  132. {
  133. FAR struct igmp_group_s *group;
  134. int ret;
  135. ninfo("Timeout!\n");
  136. /* Recover the reference to the group */
  137. group = (FAR struct igmp_group_s *)arg;
  138. DEBUGASSERT(group != NULL);
  139. /* Perform the timeout-related operations on (preferably) the low priority
  140. * work queue.
  141. */
  142. ret = work_queue(LPWORK, &group->work, igmp_timeout_work, group, 0);
  143. if (ret < 0)
  144. {
  145. nerr("ERROR: Failed to queue timeout work: %d\n", ret);
  146. }
  147. }
  148. /****************************************************************************
  149. * Public Functions
  150. ****************************************************************************/
  151. /****************************************************************************
  152. * Name: igmp_startticks and igmp_starttimer
  153. *
  154. * Description:
  155. * Start the IGMP timer with differing time units (ticks or deciseconds).
  156. *
  157. * Assumptions:
  158. * This function may be called from most any context.
  159. *
  160. ****************************************************************************/
  161. void igmp_startticks(FAR struct igmp_group_s *group, unsigned int ticks)
  162. {
  163. int ret;
  164. /* Start the timer */
  165. gtmrinfo("ticks: %d\n", ticks);
  166. ret = wd_start(&group->wdog, ticks, igmp_timeout, (wdparm_t)group);
  167. DEBUGASSERT(ret == OK);
  168. UNUSED(ret);
  169. }
  170. void igmp_starttimer(FAR struct igmp_group_s *group, uint8_t decisecs)
  171. {
  172. /* Convert the decisec value to system clock ticks and start the timer.
  173. * Important!! this should be a random timer from 0 to decisecs
  174. */
  175. gtmrinfo("decisecs: %d\n", decisecs);
  176. igmp_startticks(group, net_dsec2tick(decisecs));
  177. }
  178. /****************************************************************************
  179. * Name: igmp_cmptimer
  180. *
  181. * Description:
  182. * Compare the timer remaining on the watching timer to the deci-second
  183. * value. If maxticks > ticks-remaining, then (1) cancel the timer (to
  184. * avoid race conditions) and return true.
  185. *
  186. * Assumptions:
  187. * This function may be called from most any context. If true is returned
  188. * then the caller must call igmp_startticks() to restart the timer
  189. *
  190. ****************************************************************************/
  191. bool igmp_cmptimer(FAR struct igmp_group_s *group, int maxticks)
  192. {
  193. irqstate_t flags;
  194. int remaining;
  195. /* Disable interrupts so that there is no race condition with the actual
  196. * timer expiration.
  197. */
  198. flags = enter_critical_section();
  199. /* Get the timer remaining on the watchdog. A time of <= zero means that
  200. * the watchdog was never started.
  201. */
  202. remaining = wd_gettime(&group->wdog);
  203. /* A remaining time of zero means that the watchdog was never started
  204. * or has already expired. That case should be covered in the following
  205. * test as well.
  206. */
  207. gtmrinfo("maxticks: %d remaining: %d\n", maxticks, remaining);
  208. if (maxticks > remaining)
  209. {
  210. /* Cancel the watchdog timer and return true */
  211. wd_cancel(&group->wdog);
  212. leave_critical_section(flags);
  213. return true;
  214. }
  215. leave_critical_section(flags);
  216. return false;
  217. }
  218. #endif /* CONFIG_NET_IGMP */