Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
7d999c60 | 2 | * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. |
2b6b6284 | 3 | * All rights reserved. |
8ae0e4b4 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
2b6b6284 | 6 | * |
69d96ae2 | 7 | * @(#)tcp_timer.c 7.20 (Berkeley) %G% |
8ae0e4b4 | 8 | */ |
f03530e6 | 9 | |
5548a02f KB |
10 | #include <sys/param.h> |
11 | #include <sys/systm.h> | |
12 | #include <sys/malloc.h> | |
13 | #include <sys/mbuf.h> | |
14 | #include <sys/socket.h> | |
15 | #include <sys/socketvar.h> | |
16 | #include <sys/protosw.h> | |
17 | #include <sys/errno.h> | |
6e7edb25 | 18 | |
5548a02f KB |
19 | #include <net/if.h> |
20 | #include <net/route.h> | |
f4d55810 | 21 | |
5548a02f KB |
22 | #include <netinet/in.h> |
23 | #include <netinet/in_systm.h> | |
24 | #include <netinet/ip.h> | |
25 | #include <netinet/in_pcb.h> | |
26 | #include <netinet/ip_var.h> | |
27 | #include <netinet/tcp.h> | |
28 | #include <netinet/tcp_fsm.h> | |
29 | #include <netinet/tcp_seq.h> | |
30 | #include <netinet/tcp_timer.h> | |
31 | #include <netinet/tcp_var.h> | |
32 | #include <netinet/tcpip.h> | |
f03530e6 | 33 | |
8a36cf82 MK |
34 | int tcp_keepidle = TCPTV_KEEP_IDLE; |
35 | int tcp_keepintvl = TCPTV_KEEPINTVL; | |
36 | int tcp_maxidle; | |
f03530e6 BJ |
37 | /* |
38 | * Fast timeout routine for processing delayed acks | |
39 | */ | |
40 | tcp_fasttimo() | |
41 | { | |
b8b9174f BJ |
42 | register struct inpcb *inp; |
43 | register struct tcpcb *tp; | |
44 | int s = splnet(); | |
b8b9174f | 45 | |
fd5dc5f0 BJ |
46 | inp = tcb.inp_next; |
47 | if (inp) | |
48 | for (; inp != &tcb; inp = inp->inp_next) | |
b8b9174f BJ |
49 | if ((tp = (struct tcpcb *)inp->inp_ppcb) && |
50 | (tp->t_flags & TF_DELACK)) { | |
51 | tp->t_flags &= ~TF_DELACK; | |
52 | tp->t_flags |= TF_ACKNOW; | |
35f3fc10 | 53 | tcpstat.tcps_delack++; |
b8b9174f BJ |
54 | (void) tcp_output(tp); |
55 | } | |
56 | splx(s); | |
f03530e6 BJ |
57 | } |
58 | ||
59 | /* | |
60 | * Tcp protocol timeout routine called every 500 ms. | |
61 | * Updates the timers in all active tcb's and | |
62 | * causes finite state machine actions if timers expire. | |
63 | */ | |
64 | tcp_slowtimo() | |
65 | { | |
1e977657 | 66 | register struct inpcb *ip, *ipnxt; |
f03530e6 BJ |
67 | register struct tcpcb *tp; |
68 | int s = splnet(); | |
f03530e6 | 69 | register int i; |
f03530e6 | 70 | |
8a36cf82 | 71 | tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl; |
f03530e6 BJ |
72 | /* |
73 | * Search through tcb's and update active timers. | |
74 | */ | |
4aed14e3 BJ |
75 | ip = tcb.inp_next; |
76 | if (ip == 0) { | |
77 | splx(s); | |
78 | return; | |
79 | } | |
039b88d5 MK |
80 | for (; ip != &tcb; ip = ipnxt) { |
81 | ipnxt = ip->inp_next; | |
f03530e6 | 82 | tp = intotcpcb(ip); |
37de812c BJ |
83 | if (tp == 0) |
84 | continue; | |
a6503abf | 85 | for (i = 0; i < TCPT_NTIMERS; i++) { |
1e977657 | 86 | if (tp->t_timer[i] && --tp->t_timer[i] == 0) { |
f03530e6 BJ |
87 | (void) tcp_usrreq(tp->t_inpcb->inp_socket, |
88 | PRU_SLOWTIMO, (struct mbuf *)0, | |
755d8841 | 89 | (struct mbuf *)i, (struct mbuf *)0); |
1e977657 BJ |
90 | if (ipnxt->inp_prev != ip) |
91 | goto tpgone; | |
92 | } | |
f03530e6 | 93 | } |
405c9168 BJ |
94 | tp->t_idle++; |
95 | if (tp->t_rtt) | |
96 | tp->t_rtt++; | |
1e977657 | 97 | tpgone: |
039b88d5 | 98 | ; |
f03530e6 | 99 | } |
a6503abf | 100 | tcp_iss += TCP_ISSINCR/PR_SLOWHZ; /* increment iss */ |
367bed15 | 101 | #ifdef TCP_COMPAT_42 |
c50542f3 MK |
102 | if ((int)tcp_iss < 0) |
103 | tcp_iss = 0; /* XXX */ | |
104 | #endif | |
69d96ae2 | 105 | tcp_now++; /* for timestamps */ |
f03530e6 BJ |
106 | splx(s); |
107 | } | |
108 | ||
109 | /* | |
a6503abf | 110 | * Cancel all timers for TCP tp. |
f03530e6 | 111 | */ |
0974b45c | 112 | tcp_canceltimers(tp) |
f03530e6 BJ |
113 | struct tcpcb *tp; |
114 | { | |
f03530e6 BJ |
115 | register int i; |
116 | ||
a6503abf BJ |
117 | for (i = 0; i < TCPT_NTIMERS; i++) |
118 | tp->t_timer[i] = 0; | |
f03530e6 BJ |
119 | } |
120 | ||
a6bbda13 MK |
121 | int tcp_backoff[TCP_MAXRXTSHIFT + 1] = |
122 | { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; | |
8b6ad229 | 123 | |
f03530e6 | 124 | /* |
405c9168 | 125 | * TCP timer processing. |
f03530e6 | 126 | */ |
0e3936fa | 127 | struct tcpcb * |
a6503abf | 128 | tcp_timers(tp, timer) |
f03530e6 | 129 | register struct tcpcb *tp; |
a6503abf | 130 | int timer; |
f03530e6 | 131 | { |
2a89e5a6 | 132 | register int rexmt; |
f03530e6 | 133 | |
0974b45c | 134 | switch (timer) { |
f03530e6 | 135 | |
405c9168 | 136 | /* |
6209c5c4 MK |
137 | * 2 MSL timeout in shutdown went off. If we're closed but |
138 | * still waiting for peer to close and connection has been idle | |
139 | * too long, or if 2MSL time is up from TIME_WAIT, delete connection | |
140 | * control block. Otherwise, check again in a bit. | |
405c9168 | 141 | */ |
a6503abf | 142 | case TCPT_2MSL: |
6209c5c4 | 143 | if (tp->t_state != TCPS_TIME_WAIT && |
8a36cf82 MK |
144 | tp->t_idle <= tcp_maxidle) |
145 | tp->t_timer[TCPT_2MSL] = tcp_keepintvl; | |
6209c5c4 MK |
146 | else |
147 | tp = tcp_close(tp); | |
0e3936fa | 148 | break; |
f03530e6 | 149 | |
405c9168 BJ |
150 | /* |
151 | * Retransmission timer went off. Message has not | |
152 | * been acked within retransmit interval. Back off | |
eeaf00e3 | 153 | * to a longer retransmit interval and retransmit one segment. |
405c9168 | 154 | */ |
a6503abf | 155 | case TCPT_REXMT: |
a6bbda13 MK |
156 | if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { |
157 | tp->t_rxtshift = TCP_MAXRXTSHIFT; | |
35f3fc10 | 158 | tcpstat.tcps_timeoutdrop++; |
7d999c60 MK |
159 | tp = tcp_drop(tp, tp->t_softerror ? |
160 | tp->t_softerror : ETIMEDOUT); | |
0e3936fa | 161 | break; |
fd5dc5f0 | 162 | } |
35f3fc10 | 163 | tcpstat.tcps_rexmttimeo++; |
7d999c60 MK |
164 | rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; |
165 | TCPT_RANGESET(tp->t_rxtcur, rexmt, | |
166 | tp->t_rttmin, TCPTV_REXMTMAX); | |
a6bbda13 | 167 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; |
6a4fd140 | 168 | /* |
a6bbda13 MK |
169 | * If losing, let the lower level know and try for |
170 | * a better route. Also, if we backed off this far, | |
171 | * our srtt estimate is probably bogus. Clobber it | |
172 | * so we'll take the next rtt measurement as our srtt; | |
173 | * move the current srtt into rttvar to keep the current | |
174 | * retransmit times until then. | |
6a4fd140 | 175 | */ |
a6bbda13 | 176 | if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { |
9d866d2f | 177 | #if BSD>=43 |
6a4fd140 | 178 | in_losing(tp->t_inpcb); |
9d866d2f | 179 | #endif |
7d999c60 | 180 | tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); |
a6bbda13 MK |
181 | tp->t_srtt = 0; |
182 | } | |
405c9168 | 183 | tp->snd_nxt = tp->snd_una; |
7872cc0a | 184 | /* |
7cc62c26 | 185 | * If timing a segment in this window, stop the timer. |
7872cc0a | 186 | */ |
7cc62c26 | 187 | tp->t_rtt = 0; |
2e5a76f2 MK |
188 | /* |
189 | * Close the congestion window down to one segment | |
190 | * (we'll open it by one segment for each ack we get). | |
191 | * Since we probably have a window's worth of unacked | |
192 | * data accumulated, this "slow start" keeps us from | |
193 | * dumping all that data as back-to-back packets (which | |
194 | * might overwhelm an intermediate gateway). | |
6f854ef4 MK |
195 | * |
196 | * There are two phases to the opening: Initially we | |
197 | * open by one mss on each ack. This makes the window | |
198 | * size increase exponentially with time. If the | |
199 | * window is larger than the path can handle, this | |
200 | * exponential growth results in dropped packet(s) | |
201 | * almost immediately. To get more time between | |
202 | * drops but still "push" the network to take advantage | |
203 | * of improving conditions, we switch from exponential | |
204 | * to linear window opening at some threshhold size. | |
205 | * For a threshhold, we use half the current window | |
206 | * size, truncated to a multiple of the mss. | |
207 | * | |
208 | * (the minimum cwnd that will give us exponential | |
209 | * growth is 2 mss. We don't allow the threshhold | |
210 | * to go below this.) | |
2e5a76f2 | 211 | */ |
6f854ef4 | 212 | { |
7d999c60 | 213 | u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; |
6f854ef4 MK |
214 | if (win < 2) |
215 | win = 2; | |
2e5a76f2 | 216 | tp->snd_cwnd = tp->t_maxseg; |
6f854ef4 | 217 | tp->snd_ssthresh = win * tp->t_maxseg; |
7d999c60 | 218 | tp->t_dupacks = 0; |
6f854ef4 | 219 | } |
405c9168 | 220 | (void) tcp_output(tp); |
0e3936fa | 221 | break; |
f03530e6 | 222 | |
405c9168 BJ |
223 | /* |
224 | * Persistance timer into zero window. | |
225 | * Force a byte to be output, if possible. | |
226 | */ | |
a6503abf | 227 | case TCPT_PERSIST: |
35f3fc10 | 228 | tcpstat.tcps_persisttimeo++; |
a13c006d | 229 | tcp_setpersist(tp); |
405c9168 BJ |
230 | tp->t_force = 1; |
231 | (void) tcp_output(tp); | |
232 | tp->t_force = 0; | |
0e3936fa | 233 | break; |
f03530e6 | 234 | |
405c9168 BJ |
235 | /* |
236 | * Keep-alive timer went off; send something | |
237 | * or drop connection if idle for too long. | |
238 | */ | |
a6503abf | 239 | case TCPT_KEEP: |
35f3fc10 | 240 | tcpstat.tcps_keeptimeo++; |
f3cdd721 BJ |
241 | if (tp->t_state < TCPS_ESTABLISHED) |
242 | goto dropit; | |
c0200c65 MK |
243 | if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE && |
244 | tp->t_state <= TCPS_CLOSE_WAIT) { | |
8a36cf82 | 245 | if (tp->t_idle >= tcp_keepidle + tcp_maxidle) |
f3cdd721 | 246 | goto dropit; |
3bd14c98 | 247 | /* |
35f3fc10 MK |
248 | * Send a packet designed to force a response |
249 | * if the peer is up and reachable: | |
250 | * either an ACK if the connection is still alive, | |
251 | * or an RST if the peer has closed the connection | |
252 | * due to timeout or reboot. | |
253 | * Using sequence number tp->snd_una-1 | |
254 | * causes the transmitted zero-length segment | |
255 | * to lie outside the receive window; | |
256 | * by the protocol spec, this requires the | |
257 | * correspondent TCP to respond. | |
3bd14c98 | 258 | */ |
35f3fc10 | 259 | tcpstat.tcps_keepprobe++; |
eeef4ac3 MK |
260 | #ifdef TCP_COMPAT_42 |
261 | /* | |
262 | * The keepalive packet must have nonzero length | |
263 | * to get a 4.2 host to respond. | |
264 | */ | |
cc072043 | 265 | tcp_respond(tp, tp->t_template, (struct mbuf *)NULL, |
eeef4ac3 MK |
266 | tp->rcv_nxt - 1, tp->snd_una - 1, 0); |
267 | #else | |
cc072043 | 268 | tcp_respond(tp, tp->t_template, (struct mbuf *)NULL, |
eeef4ac3 MK |
269 | tp->rcv_nxt, tp->snd_una - 1, 0); |
270 | #endif | |
8a36cf82 MK |
271 | tp->t_timer[TCPT_KEEP] = tcp_keepintvl; |
272 | } else | |
273 | tp->t_timer[TCPT_KEEP] = tcp_keepidle; | |
0e3936fa | 274 | break; |
f3cdd721 | 275 | dropit: |
35f3fc10 | 276 | tcpstat.tcps_keepdrops++; |
0e3936fa SL |
277 | tp = tcp_drop(tp, ETIMEDOUT); |
278 | break; | |
f03530e6 | 279 | } |
0e3936fa | 280 | return (tp); |
f03530e6 | 281 | } |