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