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