sctp_callout.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * a) Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * b) Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the distribution.
  17. *
  18. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE 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. #if defined(__Userspace__)
  35. #include <sys/types.h>
  36. #if !defined(_WIN32)
  37. #include <sys/wait.h>
  38. #include <unistd.h>
  39. #include <pthread.h>
  40. #endif
  41. #if defined(__native_client__)
  42. #include <sys/select.h>
  43. #endif
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <user_atomic.h>
  49. #include <netinet/sctp_sysctl.h>
  50. #include <netinet/sctp_pcb.h>
  51. #else
  52. #include <netinet/sctp_os.h>
  53. #include <netinet/sctp_callout.h>
  54. #include <netinet/sctp_pcb.h>
  55. #endif
  56. #include <netinet/sctputil.h>
  57. /*
  58. * Callout/Timer routines for OS that doesn't have them
  59. */
  60. #if defined(__APPLE__) || defined(__Userspace__)
  61. static uint32_t ticks = 0;
  62. #else
  63. extern int ticks;
  64. #endif
  65. uint32_t sctp_get_tick_count(void) {
  66. uint32_t ret;
  67. SCTP_TIMERQ_LOCK();
  68. ret = ticks;
  69. SCTP_TIMERQ_UNLOCK();
  70. return ret;
  71. }
  72. /*
  73. * SCTP_TIMERQ_LOCK protects:
  74. * - SCTP_BASE_INFO(callqueue)
  75. * - sctp_os_timer_next: next timer to check
  76. */
  77. static sctp_os_timer_t *sctp_os_timer_next = NULL;
  78. void
  79. sctp_os_timer_init(sctp_os_timer_t *c)
  80. {
  81. memset(c, 0, sizeof(*c));
  82. }
  83. int
  84. sctp_os_timer_start(sctp_os_timer_t *c, uint32_t to_ticks, void (*ftn) (void *),
  85. void *arg)
  86. {
  87. int ret = 0;
  88. /* paranoia */
  89. if ((c == NULL) || (ftn == NULL))
  90. return (ret);
  91. SCTP_TIMERQ_LOCK();
  92. /* check to see if we're rescheduling a timer */
  93. if (c->c_flags & SCTP_CALLOUT_PENDING) {
  94. ret = 1;
  95. if (c == sctp_os_timer_next) {
  96. sctp_os_timer_next = TAILQ_NEXT(c, tqe);
  97. }
  98. TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
  99. /*
  100. * part of the normal "stop a pending callout" process
  101. * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING
  102. * flags. We don't bother since we are setting these
  103. * below and we still hold the lock.
  104. */
  105. }
  106. /*
  107. * We could unlock/splx here and lock/spl at the TAILQ_INSERT_TAIL,
  108. * but there's no point since doing this setup doesn't take much time.
  109. */
  110. if (to_ticks == 0)
  111. to_ticks = 1;
  112. c->c_arg = arg;
  113. c->c_flags = (SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
  114. c->c_func = ftn;
  115. c->c_time = ticks + to_ticks;
  116. TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(callqueue), c, tqe);
  117. SCTP_TIMERQ_UNLOCK();
  118. return (ret);
  119. }
  120. int
  121. sctp_os_timer_stop(sctp_os_timer_t *c)
  122. {
  123. SCTP_TIMERQ_LOCK();
  124. /*
  125. * Don't attempt to delete a callout that's not on the queue.
  126. */
  127. if ((c->c_flags & SCTP_CALLOUT_PENDING) == 0) {
  128. c->c_flags &= ~SCTP_CALLOUT_ACTIVE;
  129. SCTP_TIMERQ_UNLOCK();
  130. return (0);
  131. }
  132. c->c_flags &= ~(SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
  133. if (c == sctp_os_timer_next) {
  134. sctp_os_timer_next = TAILQ_NEXT(c, tqe);
  135. }
  136. TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
  137. SCTP_TIMERQ_UNLOCK();
  138. return (1);
  139. }
  140. void
  141. sctp_handle_tick(uint32_t elapsed_ticks)
  142. {
  143. sctp_os_timer_t *c;
  144. void (*c_func)(void *);
  145. void *c_arg;
  146. SCTP_TIMERQ_LOCK();
  147. /* update our tick count */
  148. ticks += elapsed_ticks;
  149. c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue));
  150. while (c) {
  151. if (SCTP_UINT32_GE(ticks, c->c_time)) {
  152. sctp_os_timer_next = TAILQ_NEXT(c, tqe);
  153. TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
  154. c_func = c->c_func;
  155. c_arg = c->c_arg;
  156. c->c_flags &= ~SCTP_CALLOUT_PENDING;
  157. SCTP_TIMERQ_UNLOCK();
  158. c_func(c_arg);
  159. SCTP_TIMERQ_LOCK();
  160. c = sctp_os_timer_next;
  161. } else {
  162. c = TAILQ_NEXT(c, tqe);
  163. }
  164. }
  165. sctp_os_timer_next = NULL;
  166. SCTP_TIMERQ_UNLOCK();
  167. }
  168. #if defined(__APPLE__) && !defined(__Userspace__)
  169. void
  170. sctp_timeout(void *arg SCTP_UNUSED)
  171. {
  172. sctp_handle_tick(SCTP_BASE_VAR(sctp_main_timer_ticks));
  173. sctp_start_main_timer();
  174. }
  175. #endif
  176. #if defined(__Userspace__)
  177. #define TIMEOUT_INTERVAL 10
  178. void *
  179. user_sctp_timer_iterate(void *arg)
  180. {
  181. sctp_userspace_set_threadname("SCTP timer");
  182. for (;;) {
  183. #if defined(_WIN32)
  184. Sleep(TIMEOUT_INTERVAL);
  185. #else
  186. struct timespec amount, remaining;
  187. remaining.tv_sec = 0;
  188. remaining.tv_nsec = TIMEOUT_INTERVAL * 1000 * 1000;
  189. do {
  190. amount = remaining;
  191. } while (nanosleep(&amount, &remaining) == -1);
  192. #endif
  193. if (atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 1, 1)) {
  194. break;
  195. }
  196. sctp_handle_tick(sctp_msecs_to_ticks(TIMEOUT_INTERVAL));
  197. }
  198. return (NULL);
  199. }
  200. void
  201. sctp_start_timer_thread(void)
  202. {
  203. /*
  204. * No need to do SCTP_TIMERQ_LOCK_INIT();
  205. * here, it is being done in sctp_pcb_init()
  206. */
  207. int rc;
  208. rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(timer_thread), user_sctp_timer_iterate);
  209. if (rc) {
  210. SCTP_PRINTF("ERROR; return code from sctp_thread_create() is %d\n", rc);
  211. } else {
  212. SCTP_BASE_VAR(timer_thread_started) = 1;
  213. }
  214. }
  215. void
  216. sctp_stop_timer_thread(void)
  217. {
  218. atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 0, 1);
  219. if (SCTP_BASE_VAR(timer_thread_started)) {
  220. #if defined(_WIN32)
  221. WaitForSingleObject(SCTP_BASE_VAR(timer_thread), INFINITE);
  222. CloseHandle(SCTP_BASE_VAR(timer_thread));
  223. #else
  224. pthread_join(SCTP_BASE_VAR(timer_thread), NULL);
  225. #endif
  226. }
  227. }
  228. #endif