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