]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_timer.c
3d340b3694f340c3a31d4e27cfe327f412201006
[apple/xnu.git] / bsd / netinet / tcp_timer.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
58 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
59 */
60
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/mbuf.h>
66 #include <sys/sysctl.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/protosw.h>
70
71 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
72
73 #include <net/route.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_pcb.h>
78 #if INET6
79 #include <netinet6/in6_pcb.h>
80 #endif
81 #include <netinet/ip_var.h>
82 #include <netinet/tcp.h>
83 #include <netinet/tcp_fsm.h>
84 #include <netinet/tcp_seq.h>
85 #include <netinet/tcp_timer.h>
86 #include <netinet/tcp_var.h>
87 #include <netinet/tcpip.h>
88 #if TCPDEBUG
89 #include <netinet/tcp_debug.h>
90 #endif
91 #include <sys/kdebug.h>
92
93 #define DBG_FNC_TCP_FAST NETDBG_CODE(DBG_NETTCP, (5 << 8))
94 #define DBG_FNC_TCP_SLOW NETDBG_CODE(DBG_NETTCP, (5 << 8) | 1)
95
96 /*
97 * NOTE - WARNING
98 *
99 *
100 *
101 *
102 */
103 static int
104 sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
105 {
106 int error, s, tt;
107
108 tt = *(int *)oidp->oid_arg1;
109 s = tt * 1000 / hz;
110
111 error = sysctl_handle_int(oidp, &s, 0, req);
112 if (error || !req->newptr)
113 return (error);
114
115 tt = s * hz / 1000;
116 if (tt < 1)
117 return (EINVAL);
118
119 *(int *)oidp->oid_arg1 = tt;
120 return (0);
121 }
122
123 int tcp_keepinit;
124 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
125 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
126
127 int tcp_keepidle;
128 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
129 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
130
131 int tcp_keepintvl;
132 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
133 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
134
135 int tcp_delacktime;
136 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
137 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
138 "Time before a delayed ACK is sent");
139
140 int tcp_msl;
141 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
142 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
143
144 static int always_keepalive = 0;
145 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
146 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
147
148 static int tcp_keepcnt = TCPTV_KEEPCNT;
149 /* max idle probes */
150 int tcp_maxpersistidle;
151 /* max idle time in persist */
152 int tcp_maxidle;
153
154 struct inpcbhead time_wait_slots[N_TIME_WAIT_SLOTS];
155 int cur_tw_slot = 0;
156
157 u_long *delack_bitmask;
158
159
160 void add_to_time_wait(tp)
161 struct tcpcb *tp;
162 {
163 int tw_slot;
164
165 LIST_REMOVE(tp->t_inpcb, inp_list);
166
167 if (tp->t_timer[TCPT_2MSL] == 0)
168 tp->t_timer[TCPT_2MSL] = 1;
169
170 tp->t_rcvtime += tp->t_timer[TCPT_2MSL] & (N_TIME_WAIT_SLOTS - 1);
171 tw_slot = (tp->t_timer[TCPT_2MSL] & (N_TIME_WAIT_SLOTS - 1)) + cur_tw_slot;
172 if (tw_slot >= N_TIME_WAIT_SLOTS)
173 tw_slot -= N_TIME_WAIT_SLOTS;
174
175 LIST_INSERT_HEAD(&time_wait_slots[tw_slot], tp->t_inpcb, inp_list);
176 }
177
178
179
180
181
182 /*
183 * Fast timeout routine for processing delayed acks
184 */
185 void
186 tcp_fasttimo()
187 {
188 register struct inpcb *inp;
189 register struct tcpcb *tp;
190
191
192 register u_long i,j;
193 register u_long temp_mask;
194 register u_long elem_base = 0;
195 struct inpcbhead *head;
196 int s = splnet();
197
198 static
199 int delack_checked = 0;
200
201 KERNEL_DEBUG(DBG_FNC_TCP_FAST | DBG_FUNC_START, 0,0,0,0,0);
202
203 if (!tcp_delack_enabled)
204 return;
205
206 for (i=0; i < (tcbinfo.hashsize / 32); i++) {
207 if (delack_bitmask[i]) {
208 temp_mask = 1;
209 for (j=0; j < 32; j++) {
210 if (temp_mask & delack_bitmask[i]) {
211 head = &tcbinfo.hashbase[elem_base + j];
212 for (inp=head->lh_first; inp != 0; inp = inp->inp_hash.le_next) {
213 delack_checked++;
214 if ((tp = (struct tcpcb *)inp->inp_ppcb) && (tp->t_flags & TF_DELACK)) {
215 tp->t_flags &= ~TF_DELACK;
216 tp->t_flags |= TF_ACKNOW;
217 tcpstat.tcps_delack++;
218 (void) tcp_output(tp);
219 }
220 }
221 }
222 temp_mask <<= 1;
223 }
224 delack_bitmask[i] = 0;
225 }
226 elem_base += 32;
227 }
228 KERNEL_DEBUG(DBG_FNC_TCP_FAST | DBG_FUNC_END, delack_checked,tcpstat.tcps_delack,0,0,0);
229 splx(s);
230
231 }
232
233 /*
234 * Tcp protocol timeout routine called every 500 ms.
235 * Updates the timers in all active tcb's and
236 * causes finite state machine actions if timers expire.
237 */
238 void
239 tcp_slowtimo()
240 {
241 register struct inpcb *ip, *ipnxt;
242 register struct tcpcb *tp;
243 register int i;
244 int s;
245 #if TCPDEBUG
246 int ostate;
247 #endif
248 #if KDEBUG
249 static int tws_checked;
250 #endif
251
252 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_START, 0,0,0,0,0);
253 s = splnet();
254
255 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
256
257 ip = tcb.lh_first;
258 if (ip == NULL) {
259 splx(s);
260 return;
261 }
262 /*
263 * Search through tcb's and update active timers.
264 */
265 for (; ip != NULL; ip = ipnxt) {
266 ipnxt = ip->inp_list.le_next;
267 tp = intotcpcb(ip);
268 if (tp == 0 || tp->t_state == TCPS_LISTEN)
269 continue;
270 /*
271 * Bogus state when port owned by SharedIP with loopback as the
272 * only configured interface: BlueBox does not filters loopback
273 */
274 if (tp->t_state == TCP_NSTATES)
275 continue;
276
277 for (i = 0; i < TCPT_NTIMERS; i++) {
278 if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
279 #if TCPDEBUG
280 ostate = tp->t_state;
281 #endif
282 tp = tcp_timers(tp, i);
283 if (tp == NULL)
284 goto tpgone;
285 #if TCPDEBUG
286 if (tp->t_inpcb->inp_socket->so_options
287 & SO_DEBUG)
288 tcp_trace(TA_USER, ostate, tp,
289 (void *)0,
290 (struct tcphdr *)0,
291 PRU_SLOWTIMO);
292 #endif
293 }
294 }
295 tp->t_rcvtime++;
296 tp->t_starttime++;
297 if (tp->t_rtttime)
298 tp->t_rtttime++;
299 tpgone:
300 ;
301 }
302
303 #if KDEBUG
304 tws_checked = 0;
305 #endif
306 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_NONE, tws_checked,0,0,0,0);
307
308 /*
309 * Process the items in the current time-wait slot
310 */
311
312 for (ip = time_wait_slots[cur_tw_slot].lh_first; ip; ip = ipnxt)
313 {
314 #if KDEBUG
315 tws_checked++;
316 #endif
317 ipnxt = ip->inp_list.le_next;
318 tp = intotcpcb(ip);
319 if (tp->t_timer[TCPT_2MSL] >= N_TIME_WAIT_SLOTS) {
320 tp->t_timer[TCPT_2MSL] -= N_TIME_WAIT_SLOTS;
321 tp->t_rcvtime += N_TIME_WAIT_SLOTS;
322 }
323 else
324 tp->t_timer[TCPT_2MSL] = 0;
325
326 if (tp->t_timer[TCPT_2MSL] == 0)
327 tp = tcp_timers(tp, TCPT_2MSL);
328 }
329
330 if (++cur_tw_slot >= N_TIME_WAIT_SLOTS)
331 cur_tw_slot = 0;
332 tcp_now++; /* for timestamps */
333 splx(s);
334 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_END, tws_checked, cur_tw_slot,0,0,0);
335 }
336
337 /*
338 * Cancel all timers for TCP tp.
339 */
340 void
341 tcp_canceltimers(tp)
342 struct tcpcb *tp;
343 {
344 register int i;
345
346 for (i = 0; i < TCPT_NTIMERS; i++)
347 tp->t_timer[i] = 0;
348 }
349
350 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
351 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
352
353 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
354 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
355
356 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
357
358 /*
359 * TCP timer processing.
360 */
361 struct tcpcb *
362 tcp_timers(tp, timer)
363 register struct tcpcb *tp;
364 int timer;
365 {
366 register int rexmt;
367 struct socket *so_tmp;
368 struct tcptemp *t_template;
369
370 #if TCPDEBUG
371 int ostate;
372 #endif
373
374 #if INET6
375 int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV4) == 0;
376 #endif /* INET6 */
377
378
379 switch (timer) {
380
381 /*
382 * 2 MSL timeout in shutdown went off. If we're closed but
383 * still waiting for peer to close and connection has been idle
384 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
385 * control block. Otherwise, check again in a bit.
386 */
387 case TCPT_2MSL:
388 if (tp->t_state != TCPS_TIME_WAIT &&
389 tp->t_rcvtime <= tcp_maxidle) {
390 tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
391 add_to_time_wait(tp);
392 }
393 else
394 tp = tcp_close(tp);
395 break;
396
397 /*
398 * Retransmission timer went off. Message has not
399 * been acked within retransmit interval. Back off
400 * to a longer retransmit interval and retransmit one segment.
401 */
402 case TCPT_REXMT:
403 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
404 tp->t_rxtshift = TCP_MAXRXTSHIFT;
405 tcpstat.tcps_timeoutdrop++;
406 so_tmp = tp->t_inpcb->inp_socket;
407 tp = tcp_drop(tp, tp->t_softerror ?
408 tp->t_softerror : ETIMEDOUT);
409 postevent(so_tmp, 0, EV_TIMEOUT);
410 break;
411 }
412
413 if (tp->t_rxtshift == 1) {
414 /*
415 * first retransmit; record ssthresh and cwnd so they can
416 * be recovered if this turns out to be a "bad" retransmit.
417 * A retransmit is considered "bad" if an ACK for this
418 * segment is received within RTT/2 interval; the assumption
419 * here is that the ACK was already in flight. See
420 * "On Estimating End-to-End Network Path Properties" by
421 * Allman and Paxson for more details.
422 */
423 tp->snd_cwnd_prev = tp->snd_cwnd;
424 tp->snd_ssthresh_prev = tp->snd_ssthresh;
425 tp->t_badrxtwin = tcp_now + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
426 }
427 tcpstat.tcps_rexmttimeo++;
428 if (tp->t_state == TCPS_SYN_SENT)
429 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
430 else
431 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
432 TCPT_RANGESET(tp->t_rxtcur, rexmt,
433 tp->t_rttmin, TCPTV_REXMTMAX);
434 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
435
436 /*
437 * Disable rfc1323 and rfc1644 if we havn't got any response to
438 * our third SYN to work-around some broken terminal servers
439 * (most of which have hopefully been retired) that have bad VJ
440 * header compression code which trashes TCP segments containing
441 * unknown-to-them TCP options.
442 */
443 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
444 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC);
445 /*
446 * If losing, let the lower level know and try for
447 * a better route. Also, if we backed off this far,
448 * our srtt estimate is probably bogus. Clobber it
449 * so we'll take the next rtt measurement as our srtt;
450 * move the current srtt into rttvar to keep the current
451 * retransmit times until then.
452 */
453 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
454 #if INET6
455 if (isipv6)
456 in6_losing(tp->t_inpcb);
457 else
458 #endif /* INET6 */
459 in_losing(tp->t_inpcb);
460 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
461 tp->t_srtt = 0;
462 }
463 tp->snd_nxt = tp->snd_una;
464 /*
465 * Note: We overload snd_recover to function also as the
466 * snd_last variable described in RFC 2582
467 */
468 tp->snd_recover = tp->snd_max;
469 /*
470 * Force a segment to be sent.
471 */
472 tp->t_flags |= TF_ACKNOW;
473 /*
474 * If timing a segment in this window, stop the timer.
475 */
476 tp->t_rtttime = 0;
477 /*
478 * Close the congestion window down to one segment
479 * (we'll open it by one segment for each ack we get).
480 * Since we probably have a window's worth of unacked
481 * data accumulated, this "slow start" keeps us from
482 * dumping all that data as back-to-back packets (which
483 * might overwhelm an intermediate gateway).
484 *
485 * There are two phases to the opening: Initially we
486 * open by one mss on each ack. This makes the window
487 * size increase exponentially with time. If the
488 * window is larger than the path can handle, this
489 * exponential growth results in dropped packet(s)
490 * almost immediately. To get more time between
491 * drops but still "push" the network to take advantage
492 * of improving conditions, we switch from exponential
493 * to linear window opening at some threshhold size.
494 * For a threshhold, we use half the current window
495 * size, truncated to a multiple of the mss.
496 *
497 * (the minimum cwnd that will give us exponential
498 * growth is 2 mss. We don't allow the threshhold
499 * to go below this.)
500 */
501 {
502 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
503 if (win < 2)
504 win = 2;
505 tp->snd_cwnd = tp->t_maxseg;
506 tp->snd_ssthresh = win * tp->t_maxseg;
507 tp->t_dupacks = 0;
508 }
509 (void) tcp_output(tp);
510 break;
511
512 /*
513 * Persistance timer into zero window.
514 * Force a byte to be output, if possible.
515 */
516 case TCPT_PERSIST:
517 tcpstat.tcps_persisttimeo++;
518 /*
519 * Hack: if the peer is dead/unreachable, we do not
520 * time out if the window is closed. After a full
521 * backoff, drop the connection if the idle time
522 * (no responses to probes) reaches the maximum
523 * backoff that we would use if retransmitting.
524 */
525 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
526 (tp->t_rcvtime >= tcp_maxpersistidle ||
527 tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
528 tcpstat.tcps_persistdrop++;
529 so_tmp = tp->t_inpcb->inp_socket;
530 tp = tcp_drop(tp, ETIMEDOUT);
531 postevent(so_tmp, 0, EV_TIMEOUT);
532 break;
533 }
534 tcp_setpersist(tp);
535 tp->t_force = 1;
536 (void) tcp_output(tp);
537 tp->t_force = 0;
538 break;
539
540 /*
541 * Keep-alive timer went off; send something
542 * or drop connection if idle for too long.
543 */
544 case TCPT_KEEP:
545 tcpstat.tcps_keeptimeo++;
546 if (tp->t_state < TCPS_ESTABLISHED)
547 goto dropit;
548 if ((always_keepalive ||
549 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
550 tp->t_state <= TCPS_CLOSING) {
551 if (tp->t_rcvtime >= TCP_KEEPIDLE(tp) + tcp_maxidle)
552 goto dropit;
553 /*
554 * Send a packet designed to force a response
555 * if the peer is up and reachable:
556 * either an ACK if the connection is still alive,
557 * or an RST if the peer has closed the connection
558 * due to timeout or reboot.
559 * Using sequence number tp->snd_una-1
560 * causes the transmitted zero-length segment
561 * to lie outside the receive window;
562 * by the protocol spec, this requires the
563 * correspondent TCP to respond.
564 */
565 tcpstat.tcps_keepprobe++;
566 t_template = tcp_maketemplate(tp);
567 if (t_template) {
568 tcp_respond(tp, t_template->tt_ipgen,
569 &t_template->tt_t, (struct mbuf *)NULL,
570 tp->rcv_nxt, tp->snd_una - 1, 0);
571 (void) m_free(dtom(t_template));
572 }
573 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
574 } else
575 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp);
576 break;
577
578 #if TCPDEBUG
579 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
580 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
581 PRU_SLOWTIMO);
582 #endif
583 dropit:
584 tcpstat.tcps_keepdrops++;
585 so_tmp = tp->t_inpcb->inp_socket;
586 tp = tcp_drop(tp, ETIMEDOUT);
587 postevent(so_tmp, 0, EV_TIMEOUT);
588 break;
589 }
590 return (tp);
591 }