igmp_timer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 contributors
  23. * may be used to endorse or promote products derived from this software
  24. * 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 LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * 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 CONFIG_CPP_HAVE_VARARGS
  66. # ifdef IGMP_GTMRDEBUG
  67. # define gtmrerr(format, ...) nerr(format, ##__VA_ARGS__)
  68. # define gtmrinfo(format, ...) ninfo(format, ##__VA_ARGS__)
  69. # else
  70. # define gtmrerr(x...)
  71. # define gtmrinfo(x...)
  72. # endif
  73. #else
  74. # ifdef IGMP_GTMRDEBUG
  75. # define gtmrerr nerr
  76. # define gtmrinfo ninfo
  77. # else
  78. # define gtmrerr (void)
  79. # define gtmrinfo (void)
  80. # endif
  81. #endif
  82. /****************************************************************************
  83. * Private Functions
  84. ****************************************************************************/
  85. /****************************************************************************
  86. * Name: igmp_timeout_work
  87. *
  88. * Description:
  89. * Timeout watchdog work.
  90. *
  91. * Assumptions:
  92. * This function is called from a work queue thread.
  93. *
  94. ****************************************************************************/
  95. static void igmp_timeout_work(FAR void *arg)
  96. {
  97. FAR struct igmp_group_s *group;
  98. int ret;
  99. /* If the state is DELAYING_MEMBER then we send a report for this group */
  100. group = (FAR struct igmp_group_s *)arg;
  101. DEBUGASSERT(group != NULL);
  102. /* If the group exists and is no an IDLE MEMBER, then it must be a DELAYING
  103. * member. Race conditions are avoided because (1) the timer is not started
  104. * until after the first IGMPv2_MEMBERSHIP_REPORT during the join, and (2)
  105. * the timer is cancelled before sending the IGMP_LEAVE_GROUP during a leave.
  106. */
  107. net_lock();
  108. if (!IS_IDLEMEMBER(group->flags))
  109. {
  110. /* Schedule (and forget) the Membership Report. NOTE:
  111. * Since we are executing from a timer interrupt, we cannot wait
  112. * for the message to be sent.
  113. */
  114. IGMP_STATINCR(g_netstats.igmp.report_sched);
  115. ret = igmp_schedmsg(group, IGMPv2_MEMBERSHIP_REPORT);
  116. if (ret < 0)
  117. {
  118. nerr("ERROR: Failed to schedule message: %d\n", ret);
  119. }
  120. /* Also note: The Membership Report is sent at most two times becasue
  121. * the timer is not reset here. Hmm.. does this mean that the group
  122. * is stranded if both reports were lost? This is consistent with the
  123. * RFC that states: "To cover the possibility of the initial Membership
  124. * Report being lost or damaged, it is recommended that it be repeated
  125. * once or twice after short delays [Unsolicited Report Interval]..."
  126. */
  127. }
  128. net_unlock();
  129. }
  130. /****************************************************************************
  131. * Name: igmp_timeout
  132. *
  133. * Description:
  134. * Timeout watchdog handler.
  135. *
  136. * Assumptions:
  137. * This function is called from the wdog timer handler which runs in the
  138. * context of the timer interrupt handler.
  139. *
  140. ****************************************************************************/
  141. static void igmp_timeout(int argc, uint32_t arg, ...)
  142. {
  143. FAR struct igmp_group_s *group;
  144. int ret;
  145. ninfo("Timeout!\n");
  146. /* Recover the reference to the group */
  147. group = (FAR struct igmp_group_s *)arg;
  148. DEBUGASSERT(argc == 1 && group != NULL);
  149. /* Perform the timeout-related operations on (preferably) the low priority
  150. * work queue.
  151. */
  152. ret = work_queue(LPWORK, &group->work, igmp_timeout_work, group, 0);
  153. if (ret < 0)
  154. {
  155. nerr("ERROR: Failed to queue timeout work: %d\n", ret);
  156. }
  157. }
  158. /****************************************************************************
  159. * Public Functions
  160. ****************************************************************************/
  161. /****************************************************************************
  162. * Name: igmp_startticks and igmp_starttimer
  163. *
  164. * Description:
  165. * Start the IGMP timer with differing time units (ticks or deciseconds).
  166. *
  167. * Assumptions:
  168. * This function may be called from most any context.
  169. *
  170. ****************************************************************************/
  171. void igmp_startticks(FAR struct igmp_group_s *group, unsigned int ticks)
  172. {
  173. int ret;
  174. /* Start the timer */
  175. gtmrinfo("ticks: %d\n", ticks);
  176. ret = wd_start(group->wdog, ticks, igmp_timeout, 1, (uint32_t)group);
  177. DEBUGASSERT(ret == OK);
  178. UNUSED(ret);
  179. }
  180. void igmp_starttimer(FAR struct igmp_group_s *group, uint8_t decisecs)
  181. {
  182. /* Convert the decisec value to system clock ticks and start the timer.
  183. * Important!! this should be a random timer from 0 to decisecs
  184. */
  185. gtmrinfo("decisecs: %d\n", decisecs);
  186. igmp_startticks(group, net_dsec2tick(decisecs));
  187. }
  188. /****************************************************************************
  189. * Name: igmp_cmptimer
  190. *
  191. * Description:
  192. * Compare the timer remaining on the watching timer to the deci-second
  193. * value. If maxticks > ticks-remaining, then (1) cancel the timer (to
  194. * avoid race conditions) and return true.
  195. *
  196. * Assumptions:
  197. * This function may be called from most any context. If true is returned
  198. * then the caller must call igmp_startticks() to restart the timer
  199. *
  200. ****************************************************************************/
  201. bool igmp_cmptimer(FAR struct igmp_group_s *group, int maxticks)
  202. {
  203. irqstate_t flags;
  204. int remaining;
  205. /* Disable interrupts so that there is no race condition with the actual
  206. * timer expiration.
  207. */
  208. flags = enter_critical_section();
  209. /* Get the timer remaining on the watchdog. A time of <= zero means that
  210. * the watchdog was never started.
  211. */
  212. remaining = wd_gettime(group->wdog);
  213. /* A remaining time of zero means that the watchdog was never started
  214. * or has already expired. That case should be covered in the following
  215. * test as well.
  216. */
  217. gtmrinfo("maxticks: %d remaining: %d\n", maxticks, remaining);
  218. if (maxticks > remaining)
  219. {
  220. /* Cancel the watchdog timer and return true */
  221. wd_cancel(group->wdog);
  222. leave_critical_section(flags);
  223. return true;
  224. }
  225. leave_critical_section(flags);
  226. return false;
  227. }
  228. #endif /* CONFIG_NET_IGMP */