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