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