|
@@ -27,61 +27,42 @@
|
27
|
27
|
*
|
28
|
28
|
* Retry timers
|
29
|
29
|
*
|
30
|
|
- * A retry timer is a truncated binary exponential backoff timer. It
|
31
|
|
- * can be used to build automatic retransmission into network
|
32
|
|
- * protocols.
|
|
30
|
+ * A retry timer is a binary exponential backoff timer. It can be
|
|
31
|
+ * used to build automatic retransmission into network protocols.
|
33
|
32
|
*/
|
34
|
33
|
|
35
|
|
-/** List of running timers */
|
36
|
|
-static LIST_HEAD ( timers );
|
|
34
|
+/** Default timeout value */
|
|
35
|
+#define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
|
37
|
36
|
|
38
|
|
-/**
|
39
|
|
- * Reload timer
|
40
|
|
- *
|
41
|
|
- * @v timer Retry timer
|
42
|
|
- *
|
43
|
|
- * This reloads the timer with a new expiry time. The expiry time
|
44
|
|
- * will be the timer's base timeout value, shifted left by the number
|
45
|
|
- * of retries (i.e. the number of timer expiries since the last timer
|
46
|
|
- * reset).
|
47
|
|
- */
|
48
|
|
-static void reload_timer ( struct retry_timer *timer ) {
|
49
|
|
- unsigned int exp;
|
50
|
|
-
|
51
|
|
- exp = timer->retries;
|
52
|
|
- if ( exp > BACKOFF_LIMIT )
|
53
|
|
- exp = BACKOFF_LIMIT;
|
54
|
|
- timer->expiry = currticks() + ( timer->base << exp );
|
55
|
|
-}
|
|
37
|
+/** Limit after which the timeout will be deemed permanent */
|
|
38
|
+#define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
|
56
|
39
|
|
57
|
|
-/**
|
58
|
|
- * Reset timer
|
59
|
|
- *
|
60
|
|
- * @v timer Retry timer
|
61
|
|
- *
|
62
|
|
- * This resets the timer, i.e. clears its retry count and starts it
|
63
|
|
- * running with its base timeout value.
|
64
|
|
- *
|
65
|
|
- * Note that it is explicitly permitted to call reset_timer() on an
|
66
|
|
- * inactive timer.
|
|
40
|
+/* The theoretical minimum that the algorithm in stop_timer() can
|
|
41
|
+ * adjust the timeout back down to is seven ticks, so set the minimum
|
|
42
|
+ * timeout to at least that value for the sake of consistency.
|
67
|
43
|
*/
|
68
|
|
-void reset_timer ( struct retry_timer *timer ) {
|
69
|
|
- timer->retries = 0;
|
70
|
|
- reload_timer ( timer );
|
71
|
|
-}
|
|
44
|
+#if MIN_TIMEOUT < 7
|
|
45
|
+#undef MIN_TIMEOUT
|
|
46
|
+#define MIN_TIMEOUT 7
|
|
47
|
+#endif
|
|
48
|
+
|
|
49
|
+/** List of running timers */
|
|
50
|
+static LIST_HEAD ( timers );
|
72
|
51
|
|
73
|
52
|
/**
|
74
|
53
|
* Start timer
|
75
|
54
|
*
|
76
|
55
|
* @v timer Retry timer
|
77
|
56
|
*
|
78
|
|
- * This resets the timer and starts it running (i.e. adds it to the
|
79
|
|
- * list of running timers). The retry_timer::base and
|
80
|
|
- * retry_timer::callback fields must have been filled in.
|
|
57
|
+ * This starts the timer running with the current timeout value. If
|
|
58
|
+ * stop_timer() is not called before the timer expires, the timer will
|
|
59
|
+ * be stopped and the timer's callback function will be called.
|
81
|
60
|
*/
|
82
|
61
|
void start_timer ( struct retry_timer *timer ) {
|
83
|
62
|
list_add ( &timer->list, &timers );
|
84
|
|
- reset_timer ( timer );
|
|
63
|
+ timer->start = currticks();
|
|
64
|
+ if ( timer->timeout < MIN_TIMEOUT )
|
|
65
|
+ timer->timeout = MIN_TIMEOUT;
|
85
|
66
|
}
|
86
|
67
|
|
87
|
68
|
/**
|
|
@@ -89,11 +70,36 @@ void start_timer ( struct retry_timer *timer ) {
|
89
|
70
|
*
|
90
|
71
|
* @v timer Retry timer
|
91
|
72
|
*
|
92
|
|
- * This stops the timer (i.e. removes it from the list of running
|
93
|
|
- * timers).
|
|
73
|
+ * This stops the timer and updates the timer's timeout value.
|
94
|
74
|
*/
|
95
|
75
|
void stop_timer ( struct retry_timer *timer ) {
|
|
76
|
+ unsigned long old_timeout = timer->timeout;
|
|
77
|
+ unsigned long runtime;
|
|
78
|
+
|
96
|
79
|
list_del ( &timer->list );
|
|
80
|
+ runtime = currticks() - timer->start;
|
|
81
|
+
|
|
82
|
+ /* Update timer. Variables are:
|
|
83
|
+ *
|
|
84
|
+ * r = round-trip time estimate (i.e. runtime)
|
|
85
|
+ * t = timeout value (i.e. timer->timeout)
|
|
86
|
+ * s = smoothed round-trip time
|
|
87
|
+ *
|
|
88
|
+ * By choice, we set t = 4s, i.e. allow for four times the
|
|
89
|
+ * normal round-trip time to pass before retransmitting.
|
|
90
|
+ *
|
|
91
|
+ * We want to smooth according to s := ( 7 s + r ) / 8
|
|
92
|
+ *
|
|
93
|
+ * Since we don't actually store s, this reduces to
|
|
94
|
+ * t := ( 7 t / 8 ) + ( r / 2 )
|
|
95
|
+ *
|
|
96
|
+ */
|
|
97
|
+ timer->timeout -= ( timer->timeout >> 3 );
|
|
98
|
+ timer->timeout += ( runtime >> 1 );
|
|
99
|
+ if ( timer->timeout != old_timeout ) {
|
|
100
|
+ DBG ( "Timer updated to %dms\n",
|
|
101
|
+ ( ( 1000 * timer->timeout ) / TICKS_PER_SEC ) );
|
|
102
|
+ }
|
97
|
103
|
}
|
98
|
104
|
|
99
|
105
|
/**
|
|
@@ -105,12 +111,22 @@ static void retry_step ( struct process *process ) {
|
105
|
111
|
struct retry_timer *timer;
|
106
|
112
|
struct retry_timer *tmp;
|
107
|
113
|
unsigned long now = currticks();
|
|
114
|
+ unsigned long used;
|
|
115
|
+ int fail;
|
108
|
116
|
|
109
|
117
|
list_for_each_entry_safe ( timer, tmp, &timers, list ) {
|
110
|
|
- if ( timer->expiry <= now ) {
|
111
|
|
- timer->retries++;
|
112
|
|
- reload_timer ( timer );
|
113
|
|
- timer->expired ( timer );
|
|
118
|
+ used = ( now - timer->start );
|
|
119
|
+ if ( used >= timer->timeout ) {
|
|
120
|
+ /* Stop timer without performing RTT calculations */
|
|
121
|
+ list_del ( &timer->list );
|
|
122
|
+ /* Back off the timeout value */
|
|
123
|
+ timer->timeout <<= 1;
|
|
124
|
+ if ( ( fail = ( timer->timeout > MAX_TIMEOUT ) ) )
|
|
125
|
+ timer->timeout = MAX_TIMEOUT;
|
|
126
|
+ DBG ( "Timer backed off to %dms\n",
|
|
127
|
+ ( ( 1000 * timer->timeout ) / TICKS_PER_SEC ) );
|
|
128
|
+ /* Call expiry callback */
|
|
129
|
+ timer->expired ( timer, fail );
|
114
|
130
|
}
|
115
|
131
|
}
|
116
|
132
|
|