2 * SPDX-License-Identifier: BSD-3-Clause
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
35 #if defined(__Userspace__)
36 #include <sys/types.h>
42 #if defined(__native_client__)
43 #include <sys/select.h>
49 #include <user_atomic.h>
50 #include <netinet/sctp_sysctl.h>
51 #include <netinet/sctp_pcb.h>
53 #include <netinet/sctp_os.h>
54 #include <netinet/sctp_callout.h>
55 #include <netinet/sctp_pcb.h>
57 #include <netinet/sctputil.h>
60 * Callout/Timer routines for OS that doesn't have them
62 #if defined(__APPLE__) || defined(__Userspace__)
63 static uint32_t ticks = 0;
68 uint32_t sctp_get_tick_count(void) {
78 * SCTP_TIMERQ_LOCK protects:
79 * - SCTP_BASE_INFO(callqueue)
80 * - sctp_os_timer_next: next timer to check
82 static sctp_os_timer_t *sctp_os_timer_next = NULL;
85 sctp_os_timer_init(sctp_os_timer_t *c)
87 memset(c, 0, sizeof(*c));
91 sctp_os_timer_start(sctp_os_timer_t *c, uint32_t to_ticks, void (*ftn) (void *),
97 if ((c == NULL) || (ftn == NULL))
101 /* check to see if we're rescheduling a timer */
102 if (c->c_flags & SCTP_CALLOUT_PENDING) {
104 if (c == sctp_os_timer_next) {
105 sctp_os_timer_next = TAILQ_NEXT(c, tqe);
107 TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
109 * part of the normal "stop a pending callout" process
110 * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING
111 * flags. We don't bother since we are setting these
112 * below and we still hold the lock.
117 * We could unlock/splx here and lock/spl at the TAILQ_INSERT_TAIL,
118 * but there's no point since doing this setup doesn't take much time.
124 c->c_flags = (SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
126 c->c_time = ticks + to_ticks;
127 TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(callqueue), c, tqe);
128 SCTP_TIMERQ_UNLOCK();
133 sctp_os_timer_stop(sctp_os_timer_t *c)
137 * Don't attempt to delete a callout that's not on the queue.
139 if (!(c->c_flags & SCTP_CALLOUT_PENDING)) {
140 c->c_flags &= ~SCTP_CALLOUT_ACTIVE;
141 SCTP_TIMERQ_UNLOCK();
144 c->c_flags &= ~(SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
145 if (c == sctp_os_timer_next) {
146 sctp_os_timer_next = TAILQ_NEXT(c, tqe);
148 TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
149 SCTP_TIMERQ_UNLOCK();
154 sctp_handle_tick(uint32_t elapsed_ticks)
157 void (*c_func)(void *);
161 /* update our tick count */
162 ticks += elapsed_ticks;
163 c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue));
165 if (SCTP_UINT32_GE(ticks, c->c_time)) {
166 sctp_os_timer_next = TAILQ_NEXT(c, tqe);
167 TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
170 c->c_flags &= ~SCTP_CALLOUT_PENDING;
171 SCTP_TIMERQ_UNLOCK();
174 c = sctp_os_timer_next;
176 c = TAILQ_NEXT(c, tqe);
179 sctp_os_timer_next = NULL;
180 SCTP_TIMERQ_UNLOCK();
183 #if defined(__APPLE__) && !defined(__Userspace__)
185 sctp_timeout(void *arg SCTP_UNUSED)
187 sctp_handle_tick(SCTP_BASE_VAR(sctp_main_timer_ticks));
188 sctp_start_main_timer();
192 #if defined(__Userspace__)
193 #define TIMEOUT_INTERVAL 10
196 user_sctp_timer_iterate(void *arg)
198 sctp_userspace_set_threadname("SCTP timer");
201 Sleep(TIMEOUT_INTERVAL);
203 struct timespec amount, remaining;
205 remaining.tv_sec = 0;
206 remaining.tv_nsec = TIMEOUT_INTERVAL * 1000 * 1000;
209 } while (nanosleep(&amount, &remaining) == -1);
211 if (atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 1, 1)) {
214 sctp_handle_tick(sctp_msecs_to_ticks(TIMEOUT_INTERVAL));
220 sctp_start_timer_thread(void)
223 * No need to do SCTP_TIMERQ_LOCK_INIT();
224 * here, it is being done in sctp_pcb_init()
228 rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(timer_thread), user_sctp_timer_iterate);
230 SCTP_PRINTF("ERROR; return code from sctp_thread_create() is %d\n", rc);
232 SCTP_BASE_VAR(timer_thread_started) = 1;
237 sctp_stop_timer_thread(void)
239 atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 0, 1);
240 if (SCTP_BASE_VAR(timer_thread_started)) {
242 WaitForSingleObject(SCTP_BASE_VAR(timer_thread), INFINITE);
243 CloseHandle(SCTP_BASE_VAR(timer_thread));
245 pthread_join(SCTP_BASE_VAR(timer_thread), NULL);