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