BadVPN – Blame information for rev 1

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