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 #include "netif/lowpan6.h"
64 #include "lwip/nat64.h"
65 #include "lwip/ip6in4.h"
66
67 #if !LWIP_LOWPOWER
68
69 #if LWIP_DEBUG_TIMERNAMES
70 #define HANDLER(x) x, #x
71 #else /* LWIP_DEBUG_TIMERNAMES */
72 #define HANDLER(x) x
73 #endif /* LWIP_DEBUG_TIMERNAMES */
74
75 #define LWIP_MAX_TIMEOUT 0x7fffffff
76
77 /* Check if timer's expiry time is greater than time and care about u32_t wraparounds */
78 #define TIME_LESS_THAN(t, compare_to) ( (((u32_t)((t)-(compare_to))) > LWIP_MAX_TIMEOUT) ? 1 : 0 )
79
80 /** This array contains all stack-internal cyclic timers. To get the number of
81 * timers, use LWIP_ARRAYSIZE() */
82 const struct lwip_cyclic_timer lwip_cyclic_timers[] = {
83 #if LWIP_TCP
84 /* The TCP timer is a special case: it does not have to run always and
85 is triggered to start from TCP using tcp_timer_needed() */
86 {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)},
87 #endif /* LWIP_TCP */
88 #if LWIP_IPV4
89 #if IP_REASSEMBLY
90 {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
91 #endif /* IP_REASSEMBLY */
92 #if LWIP_ARP
93 {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
94 #endif /* LWIP_ARP */
95 #if LWIP_DHCP
96 {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
97 {DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
98 #endif /* LWIP_DHCP */
99 #if LWIP_AUTOIP
100 {AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
101 #endif /* LWIP_AUTOIP */
102 #if LWIP_IGMP
103 {IGMP_TMR_INTERVAL, HANDLER(igmp_tmr)},
104 #endif /* LWIP_IGMP */
105 #endif /* LWIP_IPV4 */
106 #if LWIP_DNS
107 {DNS_TMR_INTERVAL, HANDLER(dns_tmr)},
108 #endif /* LWIP_DNS */
109 #if LWIP_NAT64
110 {NAT64_TMR_INTERVAL, HANDLER(nat64_tmr)},
111 #endif
112 #if LWIP_IP6IN4
113 {IP6IN4_TMR_INTERVAL, HANDLER(ip6in4_tmr)},
114 #endif
115 #if LWIP_IPV6
116 {ND6_TMR_INTERVAL, HANDLER(nd6_tmr)},
117 #if LWIP_IPV6_REASS
118 {IP6_REASS_TMR_INTERVAL, HANDLER(ip6_reass_tmr)},
119 #endif /* LWIP_IPV6_REASS */
120 #if LWIP_IPV6_MLD
121 {MLD6_TMR_INTERVAL, HANDLER(mld6_tmr)},
122 #endif /* LWIP_IPV6_MLD */
123 #if LWIP_IPV6_DHCP6
124 {DHCP6_TIMER_MSECS, HANDLER(dhcp6_tmr)},
125 #endif /* LWIP_IPV6_DHCP6 */
126 #if LWIP_6LOWPAN
127 {LOWPAN6_TMR_INTERVAL, HANDLER(lowpan6_tmr)},
128 #endif
129 #endif /* LWIP_IPV6 */
130 };
131 const int lwip_num_cyclic_timers = LWIP_ARRAYSIZE(lwip_cyclic_timers);
132
133 #if LWIP_TIMERS && !LWIP_TIMERS_CUSTOM
134
135 /** The one and only timeout list */
136 static struct sys_timeo *next_timeout;
137
138 static u32_t current_timeout_due_time;
139
140 #if LWIP_TESTMODE
141 struct sys_timeo**
sys_timeouts_get_next_timeout(void)142 sys_timeouts_get_next_timeout(void)
143 {
144 return &next_timeout;
145 }
146 #endif
147
148 #if LWIP_TCP
149 /** global variable that shows if the tcp timer is currently scheduled or not */
150 static int tcpip_tcp_timer_active;
151
152 /**
153 * Timer callback function that calls tcp_tmr() and reschedules itself.
154 *
155 * @param arg unused argument
156 */
157 static void
tcpip_tcp_timer(void * arg)158 tcpip_tcp_timer(void *arg)
159 {
160 LWIP_UNUSED_ARG(arg);
161
162 /* call TCP timer handler */
163 tcp_tmr();
164 /* timer still needed? */
165 if (tcp_active_pcbs || tcp_tw_pcbs) {
166 /* restart timer */
167 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
168 } else {
169 /* disable timer */
170 tcpip_tcp_timer_active = 0;
171 }
172 }
173
174 /**
175 * Called from TCP_REG when registering a new PCB:
176 * the reason is to have the TCP timer only running when
177 * there are active (or time-wait) PCBs.
178 */
179 void
tcp_timer_needed(void)180 tcp_timer_needed(void)
181 {
182 LWIP_ASSERT_CORE_LOCKED();
183
184 /* timer is off but needed again? */
185 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
186 /* enable and start timer */
187 tcpip_tcp_timer_active = 1;
188 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
189 }
190 }
191 #endif /* LWIP_TCP */
192
193 static err_t
194 #if LWIP_DEBUG_TIMERNAMES
sys_timeout_abs(u32_t abs_time,sys_timeout_handler handler,void * arg,const char * handler_name)195 sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg, const char *handler_name)
196 #else /* LWIP_DEBUG_TIMERNAMES */
197 sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg)
198 #endif
199 {
200 struct sys_timeo *timeout, *t;
201
202 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
203 if (timeout == NULL) {
204 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
205 return ERR_MEM;
206 }
207
208 timeout->next = NULL;
209 timeout->h = handler;
210 timeout->arg = arg;
211 timeout->time = abs_time;
212
213 #if LWIP_DEBUG_TIMERNAMES
214 timeout->handler_name = handler_name;
215 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p abs_time=%"U32_F" handler=%s arg=%p\n",
216 (void *)timeout, abs_time, handler_name, (void *)arg));
217 #endif /* LWIP_DEBUG_TIMERNAMES */
218
219 if (next_timeout == NULL) {
220 next_timeout = timeout;
221 return ERR_OK;
222 }
223 if (TIME_LESS_THAN(timeout->time, next_timeout->time)) {
224 timeout->next = next_timeout;
225 next_timeout = timeout;
226 } else {
227 for (t = next_timeout; t != NULL; t = t->next) {
228 if ((t->next == NULL) || TIME_LESS_THAN(timeout->time, t->next->time)) {
229 timeout->next = t->next;
230 t->next = timeout;
231 break;
232 }
233 }
234 }
235 return ERR_OK;
236 }
237
238 /**
239 * Timer callback function that calls cyclic->handler() and reschedules itself.
240 *
241 * @param arg unused argument
242 */
243 #if !LWIP_TESTMODE
244 static
245 #endif
246 void
lwip_cyclic_timer(void * arg)247 lwip_cyclic_timer(void *arg)
248 {
249 u32_t now;
250 u32_t next_timeout_time;
251 const struct lwip_cyclic_timer *cyclic = (const struct lwip_cyclic_timer *)arg;
252
253 #if LWIP_DEBUG_TIMERNAMES
254 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
255 #endif
256 cyclic->handler();
257
258 now = sys_now();
259 next_timeout_time = (u32_t)(current_timeout_due_time + cyclic->interval_ms); /* overflow handled by TIME_LESS_THAN macro */
260 if (TIME_LESS_THAN(next_timeout_time, now)) {
261 /* timer would immediately expire again -> "overload" -> restart without any correction */
262 #if LWIP_DEBUG_TIMERNAMES
263 sys_timeout_abs((u32_t)(now + cyclic->interval_ms), lwip_cyclic_timer, arg, cyclic->handler_name);
264 #else
265 sys_timeout_abs((u32_t)(now + cyclic->interval_ms), lwip_cyclic_timer, arg);
266 #endif
267
268 } else {
269 /* correct cyclic interval with handler execution delay and sys_check_timeouts jitter */
270 #if LWIP_DEBUG_TIMERNAMES
271 sys_timeout_abs(next_timeout_time, lwip_cyclic_timer, arg, cyclic->handler_name);
272 #else
273 sys_timeout_abs(next_timeout_time, lwip_cyclic_timer, arg);
274 #endif
275 }
276 }
277
278 /** Initialize this module */
sys_timeouts_init(void)279 void sys_timeouts_init(void)
280 {
281 size_t i;
282 /* tcp_tmr() at index 0 is started on demand */
283 for (i = (LWIP_TCP ? 1 : 0); i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) {
284 /* we have to cast via size_t to get rid of const warning
285 (this is OK as cyclic_timer() casts back to const* */
286 sys_timeout(lwip_cyclic_timers[i].interval_ms, lwip_cyclic_timer, LWIP_CONST_CAST(void *, &lwip_cyclic_timers[i]));
287 }
288 }
289
290 /**
291 * Create a one-shot timer (aka timeout). Timeouts are processed in the
292 * following cases:
293 * - while waiting for a message using sys_timeouts_mbox_fetch()
294 * - by calling sys_check_timeouts() (NO_SYS==1 only)
295 *
296 * @param msecs time in milliseconds after that the timer should expire
297 * @param handler callback function to call when msecs have elapsed
298 * @param arg argument to pass to the callback function
299 */
300 #if LWIP_DEBUG_TIMERNAMES
301 err_t
sys_timeout_debug(u32_t msecs,sys_timeout_handler handler,void * arg,const char * handler_name)302 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char *handler_name)
303 #else /* LWIP_DEBUG_TIMERNAMES */
304 err_t
305 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
306 #endif /* LWIP_DEBUG_TIMERNAMES */
307 {
308 u32_t next_timeout_time;
309
310 LWIP_ASSERT_CORE_LOCKED();
311
312 LWIP_ASSERT("Timeout time too long, max is LWIP_UINT32_MAX/4 msecs", msecs <= (LWIP_UINT32_MAX / 4));
313
314 next_timeout_time = (u32_t)(sys_now() + msecs); /* overflow handled by TIME_LESS_THAN macro */
315
316 #if LWIP_DEBUG_TIMERNAMES
317 return sys_timeout_abs(next_timeout_time, handler, arg, handler_name);
318 #else
319 return sys_timeout_abs(next_timeout_time, handler, arg);
320 #endif
321 }
322
323 /**
324 * Go through timeout list (for this task only) and remove the first matching
325 * entry (subsequent entries remain untouched), even though the timeout has not
326 * triggered yet.
327 *
328 * @param handler callback function that would be called by the timeout
329 * @param arg callback argument that would be passed to handler
330 */
331 void
sys_untimeout(sys_timeout_handler handler,void * arg)332 sys_untimeout(sys_timeout_handler handler, void *arg)
333 {
334 struct sys_timeo *prev_t, *t;
335
336 LWIP_ASSERT_CORE_LOCKED();
337
338 if (next_timeout == NULL) {
339 return;
340 }
341
342 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
343 if ((t->h == handler) && (t->arg == arg)) {
344 /* We have a match */
345 /* Unlink from previous in list */
346 if (prev_t == NULL) {
347 next_timeout = t->next;
348 } else {
349 prev_t->next = t->next;
350 }
351 memp_free(MEMP_SYS_TIMEOUT, t);
352 return;
353 }
354 }
355 return;
356 }
357
358 /**
359 * @ingroup lwip_nosys
360 * Handle timeouts for NO_SYS==1 (i.e. without using
361 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
362 * handler functions when timeouts expire.
363 *
364 * Must be called periodically from your main loop.
365 */
366 void
sys_check_timeouts(void)367 sys_check_timeouts(void)
368 {
369 u32_t now;
370
371 LWIP_ASSERT_CORE_LOCKED();
372
373 /* Process only timers expired at the start of the function. */
374 now = sys_now();
375
376 do {
377 struct sys_timeo *tmptimeout;
378 sys_timeout_handler handler;
379 void *arg;
380
381 PBUF_CHECK_FREE_OOSEQ();
382
383 tmptimeout = next_timeout;
384 if (tmptimeout == NULL) {
385 return;
386 }
387
388 if (TIME_LESS_THAN(now, tmptimeout->time)) {
389 return;
390 }
391
392 /* Timeout has expired */
393 next_timeout = tmptimeout->next;
394 handler = tmptimeout->h;
395 arg = tmptimeout->arg;
396 current_timeout_due_time = tmptimeout->time;
397 #if LWIP_DEBUG_TIMERNAMES
398 if (handler != NULL) {
399 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s t=%"U32_F" arg=%p\n",
400 tmptimeout->handler_name, sys_now() - tmptimeout->time, arg));
401 }
402 #endif /* LWIP_DEBUG_TIMERNAMES */
403 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
404 if (handler != NULL) {
405 handler(arg);
406 }
407 LWIP_TCPIP_THREAD_ALIVE();
408
409 /* Repeat until all expired timers have been called */
410 } while (1);
411 }
412
413 /** Rebase the timeout times to the current time.
414 * This is necessary if sys_check_timeouts() hasn't been called for a long
415 * time (e.g. while saving energy) to prevent all timer functions of that
416 * period being called.
417 */
418 void
sys_restart_timeouts(void)419 sys_restart_timeouts(void)
420 {
421 u32_t now;
422 u32_t base;
423 struct sys_timeo *t;
424
425 if (next_timeout == NULL) {
426 return;
427 }
428
429 now = sys_now();
430 base = next_timeout->time;
431
432 for (t = next_timeout; t != NULL; t = t->next) {
433 t->time = (t->time - base) + now;
434 }
435 }
436
437 /** Return the time left before the next timeout is due. If no timeouts are
438 * enqueued, returns 0xffffffff
439 */
440 u32_t
sys_timeouts_sleeptime(void)441 sys_timeouts_sleeptime(void)
442 {
443 u32_t now;
444
445 LWIP_ASSERT_CORE_LOCKED();
446
447 if (next_timeout == NULL) {
448 return SYS_TIMEOUTS_SLEEPTIME_INFINITE;
449 }
450 now = sys_now();
451 if (TIME_LESS_THAN(next_timeout->time, now)) {
452 return 0;
453 } else {
454 u32_t ret = (u32_t)(next_timeout->time - now);
455 LWIP_ASSERT("invalid sleeptime", ret <= LWIP_MAX_TIMEOUT);
456 return ret;
457 }
458 }
459
460 #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
461 /* Satisfy the TCP code which calls this function */
462 void
tcp_timer_needed(void)463 tcp_timer_needed(void)
464 {
465 }
466 #endif /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
467 #endif /* !LWIP_LOWPOWER */
468