2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gpxe/list.h>
22 #include <gpxe/process.h>
23 #include <gpxe/init.h>
24 #include <gpxe/retry.h>
30 * A retry timer is a binary exponential backoff timer. It can be
31 * used to build automatic retransmission into network protocols.
33 * This implementation of the timer is designed to satisfy RFC 2988
34 * and therefore be usable as a TCP retransmission timer.
39 /** Default timeout value */
40 #define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
42 /** Limit after which the timeout will be deemed permanent */
43 #define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
45 /* The theoretical minimum that the algorithm in stop_timer() can
46 * adjust the timeout back down to is seven ticks, so set the minimum
47 * timeout to at least that value for the sake of consistency.
54 /** List of running timers */
55 static LIST_HEAD ( timers
);
60 * @v timer Retry timer
62 * This starts the timer running with the current timeout value. If
63 * stop_timer() is not called before the timer expires, the timer will
64 * be stopped and the timer's callback function will be called.
66 void start_timer ( struct retry_timer
*timer
) {
67 if ( ! timer_running ( timer
) )
68 list_add ( &timer
->list
, &timers
);
69 timer
->start
= currticks();
70 if ( timer
->timeout
< MIN_TIMEOUT
)
71 timer
->timeout
= MIN_TIMEOUT
;
72 DBG2 ( "Timer %p started at time %ld (expires at %ld)\n",
73 timer
, timer
->start
, ( timer
->start
+ timer
->timeout
) );
77 * Start timer with no delay
79 * @v timer Retry timer
81 * This starts the timer running with a zero timeout value.
83 void start_timer_nodelay ( struct retry_timer
*timer
) {
84 start_timer ( timer
);
91 * @v timer Retry timer
93 * This stops the timer and updates the timer's timeout value.
95 void stop_timer ( struct retry_timer
*timer
) {
96 unsigned long old_timeout
= timer
->timeout
;
97 unsigned long now
= currticks();
98 unsigned long runtime
;
100 /* If timer was already stopped, do nothing */
101 if ( ! timer_running ( timer
) )
104 list_del ( &timer
->list
);
105 runtime
= ( now
- timer
->start
);
107 DBG2 ( "Timer %p stopped at time %ld (ran for %ld)\n",
108 timer
, now
, runtime
);
110 /* Update timer. Variables are:
112 * r = round-trip time estimate (i.e. runtime)
113 * t = timeout value (i.e. timer->timeout)
114 * s = smoothed round-trip time
116 * By choice, we set t = 4s, i.e. allow for four times the
117 * normal round-trip time to pass before retransmitting.
119 * We want to smooth according to s := ( 7 s + r ) / 8
121 * Since we don't actually store s, this reduces to
122 * t := ( 7 t / 8 ) + ( r / 2 )
125 if ( timer
->count
) {
128 timer
->timeout
-= ( timer
->timeout
>> 3 );
129 timer
->timeout
+= ( runtime
>> 1 );
130 if ( timer
->timeout
!= old_timeout
) {
131 DBG ( "Timer %p timeout updated to %ld\n",
132 timer
, timer
->timeout
);
138 * Handle expired timer
140 * @v timer Retry timer
142 static void timer_expired ( struct retry_timer
*timer
) {
145 /* Stop timer without performing RTT calculations */
146 DBG2 ( "Timer %p stopped at time %ld on expiry\n",
147 timer
, currticks() );
148 list_del ( &timer
->list
);
152 /* Back off the timeout value */
153 timer
->timeout
<<= 1;
154 if ( ( fail
= ( timer
->timeout
> MAX_TIMEOUT
) ) )
155 timer
->timeout
= MAX_TIMEOUT
;
156 DBG ( "Timer %p timeout backed off to %ld\n",
157 timer
, timer
->timeout
);
159 /* Call expiry callback */
160 timer
->expired ( timer
, fail
);
164 * Single-step the retry timer list
166 * @v process Retry timer process
168 static void retry_step ( struct process
*process __unused
) {
169 struct retry_timer
*timer
;
170 struct retry_timer
*tmp
;
171 unsigned long now
= currticks();
174 list_for_each_entry_safe ( timer
, tmp
, &timers
, list
) {
175 used
= ( now
- timer
->start
);
176 if ( used
>= timer
->timeout
)
177 timer_expired ( timer
);
181 /** Retry timer process */
182 struct process retry_process __permanent_process
= {