1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
30 * $FreeBSD: releng/6.4/sys/netinet/tcp_timer.c 162314 2006-09-15 09:51:05Z glebius $
31 */
32
33 #include "opt_inet6.h"
34 #include "opt_tcpdebug.h"
35 #include "opt_tcp_sack.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mbuf.h>
41 #include <sys/mutex.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/in_systm.h>
53 #ifdef INET6
54 #include <netinet6/in6_pcb.h>
55 #endif
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #ifdef TCPDEBUG
63 #include <netinet/tcp_debug.h>
64 #endif
65
66 int tcp_keepinit;
67 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
68 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
69
70 int tcp_keepidle;
71 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
72 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
73
74 int tcp_keepintvl;
75 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
76 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
77
78 int tcp_delacktime;
79 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
80 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
81 "Time before a delayed ACK is sent");
82
83 int tcp_msl;
84 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
85 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
86
87 int tcp_rexmit_min;
88 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
89 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
90
91 int tcp_rexmit_slop;
92 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
93 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop");
94
95 static int always_keepalive = 1;
96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
97 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
98
99 static int tcp_keepcnt = TCPTV_KEEPCNT;
100 /* max idle probes */
101 int tcp_maxpersistidle;
102 /* max idle time in persist */
103 int tcp_maxidle;
104
105 /*
106 * Tcp protocol timeout routine called every 500 ms.
107 * Updates timestamps used for TCP
108 * causes finite state machine actions if timers expire.
109 */
110 void
111 tcp_slowtimo()
112 {
113
114 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
115 INP_INFO_WLOCK(&tcbinfo);
116 (void) tcp_timer_2msl_tw(0);
117 INP_INFO_WUNLOCK(&tcbinfo);
118 }
119
120 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
121 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
122
123 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
124 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
125
126 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
127
128 /*
129 * TCP timer processing.
130 */
131
132 void
133 tcp_timer_delack(xtp)
134 void *xtp;
135 {
136 struct tcpcb *tp = xtp;
137 struct inpcb *inp;
138
139 INP_INFO_RLOCK(&tcbinfo);
140 inp = tp->t_inpcb;
141 if (inp == NULL) {
142 INP_INFO_RUNLOCK(&tcbinfo);
143 return;
144 }
145 INP_LOCK(inp);
146 INP_INFO_RUNLOCK(&tcbinfo);
147 if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
148 INP_UNLOCK(inp);
149 return;
150 }
151 callout_deactivate(tp->tt_delack);
152
153 tp->t_flags |= TF_ACKNOW;
154 tcpstat.tcps_delack++;
155 (void) tcp_output(tp);
156 INP_UNLOCK(inp);
157 }
158
159 void
160 tcp_timer_2msl(xtp)
161 void *xtp;
162 {
163 struct tcpcb *tp = xtp;
164 struct inpcb *inp;
165 #ifdef TCPDEBUG
166 int ostate;
167
168 ostate = tp->t_state;
169 #endif
170 INP_INFO_WLOCK(&tcbinfo);
171 inp = tp->t_inpcb;
172 if (inp == NULL) {
173 INP_INFO_WUNLOCK(&tcbinfo);
174 return;
175 }
176 INP_LOCK(inp);
177 tcp_free_sackholes(tp);
178 if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
179 INP_UNLOCK(tp->t_inpcb);
180 INP_INFO_WUNLOCK(&tcbinfo);
181 return;
182 }
183 callout_deactivate(tp->tt_2msl);
184 /*
185 * 2 MSL timeout in shutdown went off. If we're closed but
186 * still waiting for peer to close and connection has been idle
187 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
188 * control block. Otherwise, check again in a bit.
189 */
190 if (tp->t_state != TCPS_TIME_WAIT &&
191 (ticks - tp->t_rcvtime) <= tcp_maxidle)
192 callout_reset(tp->tt_2msl, tcp_keepintvl,
193 tcp_timer_2msl, tp);
194 else
195 tp = tcp_close(tp);
196
197 #ifdef TCPDEBUG
198 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
199 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
200 PRU_SLOWTIMO);
201 #endif
202 if (tp != NULL)
203 INP_UNLOCK(inp);
204 INP_INFO_WUNLOCK(&tcbinfo);
205 }
206
207 /*
208 * The timed wait queue contains references to each of the TCP sessions
209 * currently in the TIME_WAIT state. The queue pointers, including the
210 * queue pointers in each tcptw structure, are protected using the global
211 * tcbinfo lock, which must be held over queue iteration and modification.
212 */
213 static TAILQ_HEAD(, tcptw) twq_2msl;
214
215 void
216 tcp_timer_init(void)
217 {
218
219 TAILQ_INIT(&twq_2msl);
220 }
221
222 void
223 tcp_timer_2msl_reset(struct tcptw *tw, int rearm)
224 {
225
226 INP_INFO_WLOCK_ASSERT(&tcbinfo);
227 INP_LOCK_ASSERT(tw->tw_inpcb);
228 if (rearm)
229 TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
230 tw->tw_time = ticks + 2 * tcp_msl;
231 TAILQ_INSERT_TAIL(&twq_2msl, tw, tw_2msl);
232 }
233
234 void
235 tcp_timer_2msl_stop(struct tcptw *tw)
236 {
237
238 INP_INFO_WLOCK_ASSERT(&tcbinfo);
239 TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
240 }
241
242 struct tcptw *
243 tcp_timer_2msl_tw(int reuse)
244 {
245 struct tcptw *tw;
246
247 INP_INFO_WLOCK_ASSERT(&tcbinfo);
248 for (;;) {
249 tw = TAILQ_FIRST(&twq_2msl);
250 if (tw == NULL || (!reuse && tw->tw_time > ticks))
251 break;
252 INP_LOCK(tw->tw_inpcb);
253 tcp_twclose(tw, reuse);
254 if (reuse)
255 return (tw);
256 }
257 return (NULL);
258 }
259
260 void
261 tcp_timer_keep(xtp)
262 void *xtp;
263 {
264 struct tcpcb *tp = xtp;
265 struct tcptemp *t_template;
266 struct inpcb *inp;
267 #ifdef TCPDEBUG
268 int ostate;
269
270 ostate = tp->t_state;
271 #endif
272 INP_INFO_WLOCK(&tcbinfo);
273 inp = tp->t_inpcb;
274 if (!inp) {
275 INP_INFO_WUNLOCK(&tcbinfo);
276 return;
277 }
278 INP_LOCK(inp);
279 if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
280 INP_UNLOCK(inp);
281 INP_INFO_WUNLOCK(&tcbinfo);
282 return;
283 }
284 callout_deactivate(tp->tt_keep);
285 /*
286 * Keep-alive timer went off; send something
287 * or drop connection if idle for too long.
288 */
289 tcpstat.tcps_keeptimeo++;
290 if (tp->t_state < TCPS_ESTABLISHED)
291 goto dropit;
292 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
293 tp->t_state <= TCPS_CLOSING) {
294 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
295 goto dropit;
296 /*
297 * Send a packet designed to force a response
298 * if the peer is up and reachable:
299 * either an ACK if the connection is still alive,
300 * or an RST if the peer has closed the connection
301 * due to timeout or reboot.
302 * Using sequence number tp->snd_una-1
303 * causes the transmitted zero-length segment
304 * to lie outside the receive window;
305 * by the protocol spec, this requires the
306 * correspondent TCP to respond.
307 */
308 tcpstat.tcps_keepprobe++;
309 t_template = tcpip_maketemplate(inp);
310 if (t_template) {
311 tcp_respond(tp, t_template->tt_ipgen,
312 &t_template->tt_t, (struct mbuf *)NULL,
313 tp->rcv_nxt, tp->snd_una - 1, 0);
314 (void) m_free(dtom(t_template));
315 }
316 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
317 } else
318 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
319
320 #ifdef TCPDEBUG
321 if (inp->inp_socket->so_options & SO_DEBUG)
322 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
323 PRU_SLOWTIMO);
324 #endif
325 INP_UNLOCK(inp);
326 INP_INFO_WUNLOCK(&tcbinfo);
327 return;
328
329 dropit:
330 tcpstat.tcps_keepdrops++;
331 tp = tcp_drop(tp, ETIMEDOUT);
332
333 #ifdef TCPDEBUG
334 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
335 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
336 PRU_SLOWTIMO);
337 #endif
338 if (tp != NULL)
339 INP_UNLOCK(tp->t_inpcb);
340 INP_INFO_WUNLOCK(&tcbinfo);
341 }
342
343 void
344 tcp_timer_persist(xtp)
345 void *xtp;
346 {
347 struct tcpcb *tp = xtp;
348 struct inpcb *inp;
349 #ifdef TCPDEBUG
350 int ostate;
351
352 ostate = tp->t_state;
353 #endif
354 INP_INFO_WLOCK(&tcbinfo);
355 inp = tp->t_inpcb;
356 if (!inp) {
357 INP_INFO_WUNLOCK(&tcbinfo);
358 return;
359 }
360 INP_LOCK(inp);
361 if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
362 INP_UNLOCK(inp);
363 INP_INFO_WUNLOCK(&tcbinfo);
364 return;
365 }
366 callout_deactivate(tp->tt_persist);
367 /*
368 * Persistance timer into zero window.
369 * Force a byte to be output, if possible.
370 */
371 tcpstat.tcps_persisttimeo++;
372 /*
373 * Hack: if the peer is dead/unreachable, we do not
374 * time out if the window is closed. After a full
375 * backoff, drop the connection if the idle time
376 * (no responses to probes) reaches the maximum
377 * backoff that we would use if retransmitting.
378 */
379 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
380 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
381 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
382 tcpstat.tcps_persistdrop++;
383 tp = tcp_drop(tp, ETIMEDOUT);
384 goto out;
385 }
386 tcp_setpersist(tp);
387 tp->t_flags |= TF_FORCEDATA;
388 (void) tcp_output(tp);
389 tp->t_flags &= ~TF_FORCEDATA;
390
391 out:
392 #ifdef TCPDEBUG
393 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
394 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
395 PRU_SLOWTIMO);
396 #endif
397 if (tp != NULL)
398 INP_UNLOCK(inp);
399 INP_INFO_WUNLOCK(&tcbinfo);
400 }
401
402 void
403 tcp_timer_rexmt(xtp)
404 void *xtp;
405 {
406 struct tcpcb *tp = xtp;
407 int rexmt;
408 int headlocked;
409 struct inpcb *inp;
410 #ifdef TCPDEBUG
411 int ostate;
412
413 ostate = tp->t_state;
414 #endif
415 INP_INFO_WLOCK(&tcbinfo);
416 headlocked = 1;
417 inp = tp->t_inpcb;
418 if (!inp) {
419 INP_INFO_WUNLOCK(&tcbinfo);
420 return;
421 }
422 INP_LOCK(inp);
423 if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
424 INP_UNLOCK(inp);
425 INP_INFO_WUNLOCK(&tcbinfo);
426 return;
427 }
428 callout_deactivate(tp->tt_rexmt);
429 tcp_free_sackholes(tp);
430 /*
431 * Retransmission timer went off. Message has not
432 * been acked within retransmit interval. Back off
433 * to a longer retransmit interval and retransmit one segment.
434 */
435 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
436 tp->t_rxtshift = TCP_MAXRXTSHIFT;
437 tcpstat.tcps_timeoutdrop++;
438 tp = tcp_drop(tp, tp->t_softerror ?
439 tp->t_softerror : ETIMEDOUT);
440 goto out;
441 }
442 INP_INFO_WUNLOCK(&tcbinfo);
443 headlocked = 0;
444 if (tp->t_rxtshift == 1) {
445 /*
446 * first retransmit; record ssthresh and cwnd so they can
447 * be recovered if this turns out to be a "bad" retransmit.
448 * A retransmit is considered "bad" if an ACK for this
449 * segment is received within RTT/2 interval; the assumption
450 * here is that the ACK was already in flight. See
451 * "On Estimating End-to-End Network Path Properties" by
452 * Allman and Paxson for more details.
453 */
454 tp->snd_cwnd_prev = tp->snd_cwnd;
455 tp->snd_ssthresh_prev = tp->snd_ssthresh;
456 tp->snd_recover_prev = tp->snd_recover;
457 if (IN_FASTRECOVERY(tp))
458 tp->t_flags |= TF_WASFRECOVERY;
459 else
460 tp->t_flags &= ~TF_WASFRECOVERY;
461 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
462 }
463 tcpstat.tcps_rexmttimeo++;
464 if (tp->t_state == TCPS_SYN_SENT)
465 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
466 else
467 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
468 TCPT_RANGESET(tp->t_rxtcur, rexmt,
469 tp->t_rttmin, TCPTV_REXMTMAX);
470 /*
471 * Disable rfc1323 if we havn't got any response to
472 * our third SYN to work-around some broken terminal servers
473 * (most of which have hopefully been retired) that have bad VJ
474 * header compression code which trashes TCP segments containing
475 * unknown-to-them TCP options.
476 */
477 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
478 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
479 /*
480 * If we backed off this far, our srtt estimate is probably bogus.
481 * Clobber it so we'll take the next rtt measurement as our srtt;
482 * move the current srtt into rttvar to keep the current
483 * retransmit times until then.
484 */
485 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
486 #ifdef INET6
487 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
488 in6_losing(tp->t_inpcb);
489 else
490 #endif
491 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
492 tp->t_srtt = 0;
493 }
494 tp->snd_nxt = tp->snd_una;
495 tp->snd_recover = tp->snd_max;
496 /*
497 * Force a segment to be sent.
498 */
499 tp->t_flags |= TF_ACKNOW;
500 /*
501 * If timing a segment in this window, stop the timer.
502 */
503 tp->t_rtttime = 0;
504 /*
505 * Close the congestion window down to one segment
506 * (we'll open it by one segment for each ack we get).
507 * Since we probably have a window's worth of unacked
508 * data accumulated, this "slow start" keeps us from
509 * dumping all that data as back-to-back packets (which
510 * might overwhelm an intermediate gateway).
511 *
512 * There are two phases to the opening: Initially we
513 * open by one mss on each ack. This makes the window
514 * size increase exponentially with time. If the
515 * window is larger than the path can handle, this
516 * exponential growth results in dropped packet(s)
517 * almost immediately. To get more time between
518 * drops but still "push" the network to take advantage
519 * of improving conditions, we switch from exponential
520 * to linear window opening at some threshhold size.
521 * For a threshhold, we use half the current window
522 * size, truncated to a multiple of the mss.
523 *
524 * (the minimum cwnd that will give us exponential
525 * growth is 2 mss. We don't allow the threshhold
526 * to go below this.)
527 */
528 {
529 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
530 if (win < 2)
531 win = 2;
532 tp->snd_cwnd = tp->t_maxseg;
533 tp->snd_ssthresh = win * tp->t_maxseg;
534 tp->t_dupacks = 0;
535 }
536 EXIT_FASTRECOVERY(tp);
537 (void) tcp_output(tp);
538
539 out:
540 #ifdef TCPDEBUG
541 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
542 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
543 PRU_SLOWTIMO);
544 #endif
545 if (tp != NULL)
546 INP_UNLOCK(inp);
547 if (headlocked)
548 INP_INFO_WUNLOCK(&tcbinfo);
549 }
Cache object: b7b3e07b2f7e9448a585aaa425ca6bed
|