1 /**
2 * @file
3 * Stack-internal timers implementation.
4 * This file includes timer callbacks for stack-internal timers as well as
5 * functions to set up or stop timers and check for expired timers.
6 *
7 */
8
9 /*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <adam@sics.se>
38 * Simon Goldschmidt
39 *
40 */
41
42 #include "lwip/opt.h"
43
44 #include "lwip/timeouts.h"
45 #include "lwip/priv/tcp_priv.h"
46
47 #include "lwip/def.h"
48 #include "lwip/memp.h"
49 #include "lwip/priv/tcpip_priv.h"
50
51 #include "lwip/ip4_frag.h"
52 #include "lwip/etharp.h"
53 #include "lwip/dhcp.h"
54 #include "lwip/autoip.h"
55 #include "lwip/igmp.h"
56 #include "lwip/dns.h"
57 #include "lwip/nd6.h"
58 #include "lwip/ip6_frag.h"
59 #include "lwip/mld6.h"
60 #include "lwip/dhcp6.h"
61 #include "lwip/sys.h"
62 #include "lwip/pbuf.h"
63
64 #if !LWIP_LOWPOWER
65
66 #if LWIP_DEBUG_TIMERNAMES
67 #define HANDLER(x) x, #x
68 #else /* LWIP_DEBUG_TIMERNAMES */
69 #define HANDLER(x) x
70 #endif /* LWIP_DEBUG_TIMERNAMES */
71
72 #define LWIP_MAX_TIMEOUT 0x7fffffff
73
74 /* Check if timer's expiry time is greater than time and care about u32_t wraparounds */
75 #define TIME_LESS_THAN(t, compare_to) ( (((u32_t)((t)-(compare_to))) > LWIP_MAX_TIMEOUT) ? 1 : 0 )
76
77 /** This array contains all stack-internal cyclic timers. To get the number of
78 * timers, use LWIP_ARRAYSIZE() */
79 const struct lwip_cyclic_timer lwip_cyclic_timers[] = {
80 #if LWIP_TCP
81 /* The TCP timer is a special case: it does not have to run always and
82 is triggered to start from TCP using tcp_timer_needed() */
83 {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)},
84 #endif /* LWIP_TCP */
85 #if LWIP_IPV4
86 #if IP_REASSEMBLY
87 {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
88 #endif /* IP_REASSEMBLY */
89 #if LWIP_ARP
90 {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
91 #endif /* LWIP_ARP */
92 #if LWIP_DHCP
93 {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
94 {DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
95 #endif /* LWIP_DHCP */
96 #if LWIP_AUTOIP
97 {AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
98 #endif /* LWIP_AUTOIP */
99 #if LWIP_IGMP
100 {IGMP_TMR_INTERVAL, HANDLER(igmp_tmr)},
101 #endif /* LWIP_IGMP */
102 #endif /* LWIP_IPV4 */
103 #if LWIP_DNS
104 {DNS_TMR_INTERVAL, HANDLER(dns_tmr)},
105 #endif /* LWIP_DNS */
106 #if LWIP_IPV6
107 {ND6_TMR_INTERVAL, HANDLER(nd6_tmr)},
108 #if LWIP_IPV6_REASS
109 {IP6_REASS_TMR_INTERVAL, HANDLER(ip6_reass_tmr)},
110 #endif /* LWIP_IPV6_REASS */
111 #if LWIP_IPV6_MLD
112 {MLD6_TMR_INTERVAL, HANDLER(mld6_tmr)},
113 #endif /* LWIP_IPV6_MLD */
114 #if LWIP_IPV6_DHCP6
115 {DHCP6_TIMER_MSECS, HANDLER(dhcp6_tmr)},
116 #endif /* LWIP_IPV6_DHCP6 */
117 #endif /* LWIP_IPV6 */
118 };
119 const int lwip_num_cyclic_timers = LWIP_ARRAYSIZE(lwip_cyclic_timers);
120
121 #if LWIP_TIMERS && !LWIP_TIMERS_CUSTOM
122
123 /** The one and only timeout list */
124 static struct sys_timeo *next_timeout;
125
126 static u32_t current_timeout_due_time;
127
128 #if LWIP_TESTMODE
129 struct sys_timeo**
sys_timeouts_get_next_timeout(void)130 sys_timeouts_get_next_timeout(void)
131 {
132 return &next_timeout;
133 }
134 #endif
135
136 #if LWIP_TCP
137 /** global variable that shows if the tcp timer is currently scheduled or not */
138 static int tcpip_tcp_timer_active;
139
140 /**
141 * Timer callback function that calls tcp_tmr() and reschedules itself.
142 *
143 * @param arg unused argument
144 */
145 static void
tcpip_tcp_timer(void * arg)146 tcpip_tcp_timer(void *arg)
147 {
148 LWIP_UNUSED_ARG(arg);
149
150 /* call TCP timer handler */
151 tcp_tmr();
152 /* timer still needed? */
153 if (tcp_active_pcbs || tcp_tw_pcbs) {
154 /* restart timer */
155 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
156 } else {
157 /* disable timer */
158 tcpip_tcp_timer_active = 0;
159 }
160 }
161
162 /**
163 * Called from TCP_REG when registering a new PCB:
164 * the reason is to have the TCP timer only running when
165 * there are active (or time-wait) PCBs.
166 */
167 void
tcp_timer_needed(void)168 tcp_timer_needed(void)
169 {
170 LWIP_ASSERT_CORE_LOCKED();
171
172 /* timer is off but needed again? */
173 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
174 /* enable and start timer */
175 tcpip_tcp_timer_active = 1;
176 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
177 }
178 }
179 #endif /* LWIP_TCP */
180
181 static void
182 #if LWIP_DEBUG_TIMERNAMES
sys_timeout_abs(u32_t abs_time,sys_timeout_handler handler,void * arg,const char * handler_name)183 sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg, const char *handler_name)
184 #else /* LWIP_DEBUG_TIMERNAMES */
185 sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg)
186 #endif
187 {
188 struct sys_timeo *timeout, *t;
189
190 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
191 if (timeout == NULL) {
192 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
193 return;
194 }
195
196 timeout->next = NULL;
197 timeout->h = handler;
198 timeout->arg = arg;
199 timeout->time = abs_time;
200
201 #if LWIP_DEBUG_TIMERNAMES
202 timeout->handler_name = handler_name;
203 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p abs_time=%"U32_F" handler=%s arg=%p\n",
204 (void *)timeout, abs_time, handler_name, (void *)arg));
205 #endif /* LWIP_DEBUG_TIMERNAMES */
206
207 if (next_timeout == NULL) {
208 next_timeout = timeout;
209 return;
210 }
211 if (TIME_LESS_THAN(timeout->time, next_timeout->time)) {
212 timeout->next = next_timeout;
213 next_timeout = timeout;
214 } else {
215 for (t = next_timeout; t != NULL; t = t->next) {
216 if ((t->next == NULL) || TIME_LESS_THAN(timeout->time, t->next->time)) {
217 timeout->next = t->next;
218 t->next = timeout;
219 break;
220 }
221 }
222 }
223 }
224
225 /**
226 * Timer callback function that calls cyclic->handler() and reschedules itself.
227 *
228 * @param arg unused argument
229 */
230 #if !LWIP_TESTMODE
231 static
232 #endif
233 void
lwip_cyclic_timer(void * arg)234 lwip_cyclic_timer(void *arg)
235 {
236 u32_t now;
237 u32_t next_timeout_time;
238 const struct lwip_cyclic_timer *cyclic = (const struct lwip_cyclic_timer *)arg;
239
240 #if LWIP_DEBUG_TIMERNAMES
241 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
242 #endif
243 cyclic->handler();
244
245 now = sys_now();
246 next_timeout_time = (u32_t)(current_timeout_due_time + cyclic->interval_ms); /* overflow handled by TIME_LESS_THAN macro */
247 if (TIME_LESS_THAN(next_timeout_time, now)) {
248 /* timer would immediately expire again -> "overload" -> restart without any correction */
249 #if LWIP_DEBUG_TIMERNAMES
250 sys_timeout_abs((u32_t)(now + cyclic->interval_ms), lwip_cyclic_timer, arg, cyclic->handler_name);
251 #else
252 sys_timeout_abs((u32_t)(now + cyclic->interval_ms), lwip_cyclic_timer, arg);
253 #endif
254
255 } else {
256 /* correct cyclic interval with handler execution delay and sys_check_timeouts jitter */
257 #if LWIP_DEBUG_TIMERNAMES
258 sys_timeout_abs(next_timeout_time, lwip_cyclic_timer, arg, cyclic->handler_name);
259 #else
260 sys_timeout_abs(next_timeout_time, lwip_cyclic_timer, arg);
261 #endif
262 }
263 }
264
265 /** Initialize this module */
sys_timeouts_init(void)266 void sys_timeouts_init(void)
267 {
268 size_t i;
269 /* tcp_tmr() at index 0 is started on demand */
270 for (i = (LWIP_TCP ? 1 : 0); i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) {
271 /* we have to cast via size_t to get rid of const warning
272 (this is OK as cyclic_timer() casts back to const* */
273 sys_timeout(lwip_cyclic_timers[i].interval_ms, lwip_cyclic_timer, LWIP_CONST_CAST(void *, &lwip_cyclic_timers[i]));
274 }
275 }
276
277 /**
278 * Create a one-shot timer (aka timeout). Timeouts are processed in the
279 * following cases:
280 * - while waiting for a message using sys_timeouts_mbox_fetch()
281 * - by calling sys_check_timeouts() (NO_SYS==1 only)
282 *
283 * @param msecs time in milliseconds after that the timer should expire
284 * @param handler callback function to call when msecs have elapsed
285 * @param arg argument to pass to the callback function
286 */
287 #if LWIP_DEBUG_TIMERNAMES
288 void
sys_timeout_debug(u32_t msecs,sys_timeout_handler handler,void * arg,const char * handler_name)289 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char *handler_name)
290 #else /* LWIP_DEBUG_TIMERNAMES */
291 void
292 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
293 #endif /* LWIP_DEBUG_TIMERNAMES */
294 {
295 u32_t next_timeout_time;
296
297 LWIP_ASSERT_CORE_LOCKED();
298
299 LWIP_ASSERT("Timeout time too long, max is LWIP_UINT32_MAX/4 msecs", msecs <= (LWIP_UINT32_MAX / 4));
300
301 next_timeout_time = (u32_t)(sys_now() + msecs); /* overflow handled by TIME_LESS_THAN macro */
302
303 #if LWIP_DEBUG_TIMERNAMES
304 sys_timeout_abs(next_timeout_time, handler, arg, handler_name);
305 #else
306 sys_timeout_abs(next_timeout_time, handler, arg);
307 #endif
308 }
309
310 /**
311 * Go through timeout list (for this task only) and remove the first matching
312 * entry (subsequent entries remain untouched), even though the timeout has not
313 * triggered yet.
314 *
315 * @param handler callback function that would be called by the timeout
316 * @param arg callback argument that would be passed to handler
317 */
318 void
sys_untimeout(sys_timeout_handler handler,void * arg)319 sys_untimeout(sys_timeout_handler handler, void *arg)
320 {
321 struct sys_timeo *prev_t, *t;
322
323 LWIP_ASSERT_CORE_LOCKED();
324
325 if (next_timeout == NULL) {
326 return;
327 }
328
329 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
330 if ((t->h == handler) && (t->arg == arg)) {
331 /* We have a match */
332 /* Unlink from previous in list */
333 if (prev_t == NULL) {
334 next_timeout = t->next;
335 } else {
336 prev_t->next = t->next;
337 }
338 memp_free(MEMP_SYS_TIMEOUT, t);
339 return;
340 }
341 }
342 return;
343 }
344
345 /**
346 * @ingroup lwip_nosys
347 * Handle timeouts for NO_SYS==1 (i.e. without using
348 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
349 * handler functions when timeouts expire.
350 *
351 * Must be called periodically from your main loop.
352 */
353 void
sys_check_timeouts(void)354 sys_check_timeouts(void)
355 {
356 u32_t now;
357
358 LWIP_ASSERT_CORE_LOCKED();
359
360 /* Process only timers expired at the start of the function. */
361 now = sys_now();
362
363 do {
364 struct sys_timeo *tmptimeout;
365 sys_timeout_handler handler;
366 void *arg;
367
368 PBUF_CHECK_FREE_OOSEQ();
369
370 tmptimeout = next_timeout;
371 if (tmptimeout == NULL) {
372 return;
373 }
374
375 if (TIME_LESS_THAN(now, tmptimeout->time)) {
376 return;
377 }
378
379 /* Timeout has expired */
380 next_timeout = tmptimeout->next;
381 handler = tmptimeout->h;
382 arg = tmptimeout->arg;
383 current_timeout_due_time = tmptimeout->time;
384 #if LWIP_DEBUG_TIMERNAMES
385 if (handler != NULL) {
386 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s t=%"U32_F" arg=%p\n",
387 tmptimeout->handler_name, sys_now() - tmptimeout->time, arg));
388 }
389 #endif /* LWIP_DEBUG_TIMERNAMES */
390 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
391 if (handler != NULL) {
392 handler(arg);
393 }
394 LWIP_TCPIP_THREAD_ALIVE();
395
396 /* Repeat until all expired timers have been called */
397 } while (1);
398 }
399
400 /** Rebase the timeout times to the current time.
401 * This is necessary if sys_check_timeouts() hasn't been called for a long
402 * time (e.g. while saving energy) to prevent all timer functions of that
403 * period being called.
404 */
405 void
sys_restart_timeouts(void)406 sys_restart_timeouts(void)
407 {
408 u32_t now;
409 u32_t base;
410 struct sys_timeo *t;
411
412 if (next_timeout == NULL) {
413 return;
414 }
415
416 now = sys_now();
417 base = next_timeout->time;
418
419 for (t = next_timeout; t != NULL; t = t->next) {
420 t->time = (t->time - base) + now;
421 }
422 }
423
424 /** Return the time left before the next timeout is due. If no timeouts are
425 * enqueued, returns 0xffffffff
426 */
427 u32_t
sys_timeouts_sleeptime(void)428 sys_timeouts_sleeptime(void)
429 {
430 u32_t now;
431
432 LWIP_ASSERT_CORE_LOCKED();
433
434 if (next_timeout == NULL) {
435 return SYS_TIMEOUTS_SLEEPTIME_INFINITE;
436 }
437 now = sys_now();
438 if (TIME_LESS_THAN(next_timeout->time, now)) {
439 return 0;
440 } else {
441 u32_t ret = (u32_t)(next_timeout->time - now);
442 LWIP_ASSERT("invalid sleeptime", ret <= LWIP_MAX_TIMEOUT);
443 return ret;
444 }
445 }
446
447 #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
448 /* Satisfy the TCP code which calls this function */
449 void
tcp_timer_needed(void)450 tcp_timer_needed(void)
451 {
452 }
453 #endif /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
454 #endif /* !LWIP_LOWPOWER */
455