reset directory link counts on I/O failure
[unix-history] / usr / src / sys / netinet / tcp_timer.c
CommitLineData
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 *
dbf0c423 7 * @(#)tcp_timer.c 7.18 (Berkeley) %G%
8ae0e4b4 8 */
f03530e6 9
20666ad3
JB
10#include "param.h"
11#include "systm.h"
cc072043 12#include "malloc.h"
20666ad3
JB
13#include "mbuf.h"
14#include "socket.h"
15#include "socketvar.h"
16#include "protosw.h"
17#include "errno.h"
6e7edb25
BJ
18
19#include "../net/if.h"
c124e997 20#include "../net/route.h"
f4d55810 21
20666ad3 22#include "in.h"
20666ad3
JB
23#include "in_systm.h"
24#include "ip.h"
2b25f79c 25#include "in_pcb.h"
20666ad3
JB
26#include "ip_var.h"
27#include "tcp.h"
28#include "tcp_fsm.h"
29#include "tcp_seq.h"
30#include "tcp_timer.h"
31#include "tcp_var.h"
32#include "tcpip.h"
f03530e6 33
8a36cf82
MK
34int tcp_keepidle = TCPTV_KEEP_IDLE;
35int tcp_keepintvl = TCPTV_KEEPINTVL;
36int tcp_maxidle;
f03530e6
BJ
37/*
38 * Fast timeout routine for processing delayed acks
39 */
40tcp_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 */
64tcp_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 97tpgone:
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
f03530e6
BJ
105 splx(s);
106}
107
108/*
a6503abf 109 * Cancel all timers for TCP tp.
f03530e6 110 */
0974b45c 111tcp_canceltimers(tp)
f03530e6
BJ
112 struct tcpcb *tp;
113{
f03530e6
BJ
114 register int i;
115
a6503abf
BJ
116 for (i = 0; i < TCPT_NTIMERS; i++)
117 tp->t_timer[i] = 0;
f03530e6
BJ
118}
119
a6bbda13
MK
120int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
121 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
8b6ad229 122
f03530e6 123/*
405c9168 124 * TCP timer processing.
f03530e6 125 */
0e3936fa 126struct tcpcb *
a6503abf 127tcp_timers(tp, timer)
f03530e6 128 register struct tcpcb *tp;
a6503abf 129 int timer;
f03530e6 130{
2a89e5a6 131 register int rexmt;
f03530e6 132
0974b45c 133 switch (timer) {
f03530e6 134
405c9168 135 /*
6209c5c4
MK
136 * 2 MSL timeout in shutdown went off. If we're closed but
137 * still waiting for peer to close and connection has been idle
138 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
139 * control block. Otherwise, check again in a bit.
405c9168 140 */
a6503abf 141 case TCPT_2MSL:
6209c5c4 142 if (tp->t_state != TCPS_TIME_WAIT &&
8a36cf82
MK
143 tp->t_idle <= tcp_maxidle)
144 tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
6209c5c4
MK
145 else
146 tp = tcp_close(tp);
0e3936fa 147 break;
f03530e6 148
405c9168
BJ
149 /*
150 * Retransmission timer went off. Message has not
151 * been acked within retransmit interval. Back off
eeaf00e3 152 * to a longer retransmit interval and retransmit one segment.
405c9168 153 */
a6503abf 154 case TCPT_REXMT:
a6bbda13
MK
155 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
156 tp->t_rxtshift = TCP_MAXRXTSHIFT;
35f3fc10 157 tcpstat.tcps_timeoutdrop++;
7d999c60
MK
158 tp = tcp_drop(tp, tp->t_softerror ?
159 tp->t_softerror : ETIMEDOUT);
0e3936fa 160 break;
fd5dc5f0 161 }
35f3fc10 162 tcpstat.tcps_rexmttimeo++;
7d999c60
MK
163 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
164 TCPT_RANGESET(tp->t_rxtcur, rexmt,
165 tp->t_rttmin, TCPTV_REXMTMAX);
a6bbda13 166 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
6a4fd140 167 /*
a6bbda13
MK
168 * If losing, let the lower level know and try for
169 * a better route. Also, if we backed off this far,
170 * our srtt estimate is probably bogus. Clobber it
171 * so we'll take the next rtt measurement as our srtt;
172 * move the current srtt into rttvar to keep the current
173 * retransmit times until then.
6a4fd140 174 */
a6bbda13 175 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
9d866d2f 176#if BSD>=43
6a4fd140 177 in_losing(tp->t_inpcb);
9d866d2f 178#endif
7d999c60 179 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
a6bbda13
MK
180 tp->t_srtt = 0;
181 }
405c9168 182 tp->snd_nxt = tp->snd_una;
7872cc0a 183 /*
7cc62c26 184 * If timing a segment in this window, stop the timer.
7872cc0a 185 */
7cc62c26 186 tp->t_rtt = 0;
2e5a76f2
MK
187 /*
188 * Close the congestion window down to one segment
189 * (we'll open it by one segment for each ack we get).
190 * Since we probably have a window's worth of unacked
191 * data accumulated, this "slow start" keeps us from
192 * dumping all that data as back-to-back packets (which
193 * might overwhelm an intermediate gateway).
6f854ef4
MK
194 *
195 * There are two phases to the opening: Initially we
196 * open by one mss on each ack. This makes the window
197 * size increase exponentially with time. If the
198 * window is larger than the path can handle, this
199 * exponential growth results in dropped packet(s)
200 * almost immediately. To get more time between
201 * drops but still "push" the network to take advantage
202 * of improving conditions, we switch from exponential
203 * to linear window opening at some threshhold size.
204 * For a threshhold, we use half the current window
205 * size, truncated to a multiple of the mss.
206 *
207 * (the minimum cwnd that will give us exponential
208 * growth is 2 mss. We don't allow the threshhold
209 * to go below this.)
2e5a76f2 210 */
6f854ef4 211 {
7d999c60 212 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
6f854ef4
MK
213 if (win < 2)
214 win = 2;
2e5a76f2 215 tp->snd_cwnd = tp->t_maxseg;
6f854ef4 216 tp->snd_ssthresh = win * tp->t_maxseg;
7d999c60 217 tp->t_dupacks = 0;
6f854ef4 218 }
405c9168 219 (void) tcp_output(tp);
0e3936fa 220 break;
f03530e6 221
405c9168
BJ
222 /*
223 * Persistance timer into zero window.
224 * Force a byte to be output, if possible.
225 */
a6503abf 226 case TCPT_PERSIST:
35f3fc10 227 tcpstat.tcps_persisttimeo++;
a13c006d 228 tcp_setpersist(tp);
405c9168
BJ
229 tp->t_force = 1;
230 (void) tcp_output(tp);
231 tp->t_force = 0;
0e3936fa 232 break;
f03530e6 233
405c9168
BJ
234 /*
235 * Keep-alive timer went off; send something
236 * or drop connection if idle for too long.
237 */
a6503abf 238 case TCPT_KEEP:
35f3fc10 239 tcpstat.tcps_keeptimeo++;
f3cdd721
BJ
240 if (tp->t_state < TCPS_ESTABLISHED)
241 goto dropit;
c0200c65
MK
242 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
243 tp->t_state <= TCPS_CLOSE_WAIT) {
8a36cf82 244 if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
f3cdd721 245 goto dropit;
3bd14c98 246 /*
35f3fc10
MK
247 * Send a packet designed to force a response
248 * if the peer is up and reachable:
249 * either an ACK if the connection is still alive,
250 * or an RST if the peer has closed the connection
251 * due to timeout or reboot.
252 * Using sequence number tp->snd_una-1
253 * causes the transmitted zero-length segment
254 * to lie outside the receive window;
255 * by the protocol spec, this requires the
256 * correspondent TCP to respond.
3bd14c98 257 */
35f3fc10 258 tcpstat.tcps_keepprobe++;
eeef4ac3
MK
259#ifdef TCP_COMPAT_42
260 /*
261 * The keepalive packet must have nonzero length
262 * to get a 4.2 host to respond.
263 */
cc072043 264 tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
eeef4ac3
MK
265 tp->rcv_nxt - 1, tp->snd_una - 1, 0);
266#else
cc072043 267 tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
eeef4ac3
MK
268 tp->rcv_nxt, tp->snd_una - 1, 0);
269#endif
8a36cf82
MK
270 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
271 } else
272 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
0e3936fa 273 break;
f3cdd721 274 dropit:
35f3fc10 275 tcpstat.tcps_keepdrops++;
0e3936fa
SL
276 tp = tcp_drop(tp, ETIMEDOUT);
277 break;
f03530e6 278 }
0e3936fa 279 return (tp);
f03530e6 280}