1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 67 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.14 2003/02/03 02:33:41 hsu Exp $ 68 * $DragonFly: src/sys/netinet/tcp_timer.c,v 1.17 2008/03/30 20:39:01 dillon Exp $ 69 */ 70 71 #include "opt_compat.h" 72 #include "opt_inet6.h" 73 #include "opt_tcpdebug.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/mbuf.h> 79 #include <sys/sysctl.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/protosw.h> 83 #include <sys/thread.h> 84 #include <sys/globaldata.h> 85 #include <sys/thread2.h> 86 #include <sys/msgport2.h> 87 88 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 89 90 #include <net/route.h> 91 #include <net/netmsg2.h> 92 93 #include <netinet/in.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/in_pcb.h> 96 #ifdef INET6 97 #include <netinet6/in6_pcb.h> 98 #endif 99 #include <netinet/ip_var.h> 100 #include <netinet/tcp.h> 101 #include <netinet/tcp_fsm.h> 102 #include <netinet/tcp_seq.h> 103 #include <netinet/tcp_timer.h> 104 #include <netinet/tcp_timer2.h> 105 #include <netinet/tcp_var.h> 106 #include <netinet/tcpip.h> 107 #ifdef TCPDEBUG 108 #include <netinet/tcp_debug.h> 109 #endif 110 111 #define TCP_TIMER_REXMT 0x01 112 #define TCP_TIMER_PERSIST 0x02 113 #define TCP_TIMER_KEEP 0x04 114 #define TCP_TIMER_2MSL 0x08 115 #define TCP_TIMER_DELACK 0x10 116 117 static struct tcpcb *tcp_timer_rexmt_handler(struct tcpcb *); 118 static struct tcpcb *tcp_timer_persist_handler(struct tcpcb *); 119 static struct tcpcb *tcp_timer_keep_handler(struct tcpcb *); 120 static struct tcpcb *tcp_timer_2msl_handler(struct tcpcb *); 121 static struct tcpcb *tcp_timer_delack_handler(struct tcpcb *); 122 123 static const struct tcp_timer { 124 uint32_t tt_task; 125 struct tcpcb *(*tt_handler)(struct tcpcb *); 126 } tcp_timer_handlers[] = { 127 { TCP_TIMER_DELACK, tcp_timer_delack_handler }, 128 { TCP_TIMER_REXMT, tcp_timer_rexmt_handler }, 129 { TCP_TIMER_PERSIST, tcp_timer_persist_handler }, 130 { TCP_TIMER_KEEP, tcp_timer_keep_handler }, 131 { TCP_TIMER_2MSL, tcp_timer_2msl_handler }, 132 { 0, NULL } 133 }; 134 135 static int 136 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 137 { 138 int error, s, tt; 139 140 tt = *(int *)oidp->oid_arg1; 141 s = (int)((int64_t)tt * 1000 / hz); 142 143 error = sysctl_handle_int(oidp, &s, 0, req); 144 if (error || !req->newptr) 145 return (error); 146 147 tt = (int)((int64_t)s * hz / 1000); 148 if (tt < 1) 149 return (EINVAL); 150 151 *(int *)oidp->oid_arg1 = tt; 152 return (0); 153 } 154 155 int tcp_keepinit; 156 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 157 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "Time to establish TCP connection"); 158 159 int tcp_keepidle; 160 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 161 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "Time before TCP keepalive probes begin"); 162 163 int tcp_keepintvl; 164 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 165 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "Time between TCP keepalive probes"); 166 167 int tcp_delacktime; 168 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, 169 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I", 170 "Time before a delayed ACK is sent"); 171 172 int tcp_msl; 173 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 174 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 175 176 int tcp_rexmit_min; 177 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW, 178 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout"); 179 180 int tcp_rexmit_slop; 181 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW, 182 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", 183 "Retransmission Timer Slop"); 184 185 static int always_keepalive = 1; 186 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 187 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 188 189 /* max idle probes */ 190 int tcp_keepcnt = TCPTV_KEEPCNT; 191 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, 192 &tcp_keepcnt, 0, "Maximum number of keepalive probes to be sent"); 193 194 /* max idle time in persist */ 195 int tcp_maxpersistidle; 196 197 /* 198 * Cancel all timers for TCP tp. 199 */ 200 void 201 tcp_canceltimers(struct tcpcb *tp) 202 { 203 tcp_callout_stop(tp, tp->tt_2msl); 204 tcp_callout_stop(tp, tp->tt_persist); 205 tcp_callout_stop(tp, tp->tt_keep); 206 tcp_callout_stop(tp, tp->tt_rexmt); 207 } 208 209 /* 210 * Caller should be in critical section 211 */ 212 static void 213 tcp_send_timermsg(struct tcpcb *tp, uint32_t task) 214 { 215 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 216 217 KKASSERT(tmsg != NULL && tmsg->tt_cpuid == mycpuid && 218 tmsg->tt_tcb != NULL); 219 220 tmsg->tt_tasks |= task; 221 if (tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) 222 lwkt_sendmsg(tmsg->tt_msgport, &tmsg->tt_msg.lmsg); 223 } 224 225 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 226 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 227 228 int tcp_syn_backoff_low[TCP_MAXRXTSHIFT + 1] = 229 { 1, 1, 2, 4, 8, 8, 16, 16, 32, 64, 64, 64, 64 }; 230 231 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 232 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 233 234 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 235 236 /* Caller should be in critical section */ 237 static struct tcpcb * 238 tcp_timer_delack_handler(struct tcpcb *tp) 239 { 240 tp->t_flags |= TF_ACKNOW; 241 tcpstat.tcps_delack++; 242 tcp_output(tp); 243 return tp; 244 } 245 246 /* 247 * TCP timer processing. 248 */ 249 void 250 tcp_timer_delack(void *xtp) 251 { 252 struct tcpcb *tp = xtp; 253 struct callout *co = &tp->tt_delack->tc_callout; 254 255 crit_enter(); 256 if (callout_pending(co) || !callout_active(co)) { 257 crit_exit(); 258 return; 259 } 260 callout_deactivate(co); 261 tcp_send_timermsg(tp, TCP_TIMER_DELACK); 262 crit_exit(); 263 } 264 265 /* Caller should be in critical section */ 266 static struct tcpcb * 267 tcp_timer_2msl_handler(struct tcpcb *tp) 268 { 269 #ifdef TCPDEBUG 270 int ostate; 271 #endif 272 273 #ifdef TCPDEBUG 274 ostate = tp->t_state; 275 #endif 276 /* 277 * 2 MSL timeout in shutdown went off. If we're closed but 278 * still waiting for peer to close and connection has been idle 279 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 280 * control block. Otherwise, check again in a bit. 281 */ 282 if (tp->t_state != TCPS_TIME_WAIT && 283 (ticks - tp->t_rcvtime) <= tp->t_maxidle) { 284 tcp_callout_reset(tp, tp->tt_2msl, tp->t_keepintvl, 285 tcp_timer_2msl); 286 } else { 287 tp = tcp_close(tp); 288 } 289 290 #ifdef TCPDEBUG 291 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 292 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 293 #endif 294 return tp; 295 } 296 297 void 298 tcp_timer_2msl(void *xtp) 299 { 300 struct tcpcb *tp = xtp; 301 struct callout *co = &tp->tt_2msl->tc_callout; 302 303 crit_enter(); 304 if (callout_pending(co) || !callout_active(co)) { 305 crit_exit(); 306 return; 307 } 308 callout_deactivate(co); 309 tcp_send_timermsg(tp, TCP_TIMER_2MSL); 310 crit_exit(); 311 } 312 313 /* Caller should be in critical section */ 314 static struct tcpcb * 315 tcp_timer_keep_handler(struct tcpcb *tp) 316 { 317 struct tcptemp *t_template; 318 #ifdef TCPDEBUG 319 int ostate = tp->t_state; 320 #endif 321 322 /* 323 * Keep-alive timer went off; send something 324 * or drop connection if idle for too long. 325 */ 326 tcpstat.tcps_keeptimeo++; 327 if (tp->t_state < TCPS_ESTABLISHED) 328 goto dropit; 329 if ((always_keepalive || (tp->t_flags & TF_KEEPALIVE) || 330 (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE)) && 331 tp->t_state <= TCPS_CLOSING) { 332 if ((ticks - tp->t_rcvtime) >= tp->t_keepidle + tp->t_maxidle) 333 goto dropit; 334 /* 335 * Send a packet designed to force a response 336 * if the peer is up and reachable: 337 * either an ACK if the connection is still alive, 338 * or an RST if the peer has closed the connection 339 * due to timeout or reboot. 340 * Using sequence number tp->snd_una-1 341 * causes the transmitted zero-length segment 342 * to lie outside the receive window; 343 * by the protocol spec, this requires the 344 * correspondent TCP to respond. 345 */ 346 tcpstat.tcps_keepprobe++; 347 t_template = tcp_maketemplate(tp); 348 if (t_template) { 349 tcp_respond(tp, t_template->tt_ipgen, 350 &t_template->tt_t, NULL, 351 tp->rcv_nxt, tp->snd_una - 1, 0); 352 tcp_freetemplate(t_template); 353 } 354 tcp_callout_reset(tp, tp->tt_keep, tp->t_keepintvl, 355 tcp_timer_keep); 356 } else { 357 tcp_callout_reset(tp, tp->tt_keep, tp->t_keepidle, 358 tcp_timer_keep); 359 } 360 361 #ifdef TCPDEBUG 362 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 363 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 364 #endif 365 return tp; 366 367 dropit: 368 tcpstat.tcps_keepdrops++; 369 tp = tcp_drop(tp, ETIMEDOUT); 370 371 #ifdef TCPDEBUG 372 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 373 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 374 #endif 375 return tp; 376 } 377 378 void 379 tcp_timer_keep(void *xtp) 380 { 381 struct tcpcb *tp = xtp; 382 struct callout *co = &tp->tt_keep->tc_callout; 383 384 crit_enter(); 385 if (callout_pending(co) || !callout_active(co)) { 386 crit_exit(); 387 return; 388 } 389 callout_deactivate(co); 390 tcp_send_timermsg(tp, TCP_TIMER_KEEP); 391 crit_exit(); 392 } 393 394 /* Caller should be in critical section */ 395 static struct tcpcb * 396 tcp_timer_persist_handler(struct tcpcb *tp) 397 { 398 #ifdef TCPDEBUG 399 int ostate; 400 #endif 401 402 #ifdef TCPDEBUG 403 ostate = tp->t_state; 404 #endif 405 /* 406 * Persistance timer into zero window. 407 * Force a byte to be output, if possible. 408 */ 409 tcpstat.tcps_persisttimeo++; 410 /* 411 * Hack: if the peer is dead/unreachable, we do not 412 * time out if the window is closed. After a full 413 * backoff, drop the connection if the idle time 414 * (no responses to probes) reaches the maximum 415 * backoff that we would use if retransmitting. 416 */ 417 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 418 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 419 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 420 tcpstat.tcps_persistdrop++; 421 tp = tcp_drop(tp, ETIMEDOUT); 422 goto out; 423 } 424 tcp_setpersist(tp); 425 tp->t_flags |= TF_FORCE; 426 tcp_output(tp); 427 tp->t_flags &= ~TF_FORCE; 428 429 out: 430 #ifdef TCPDEBUG 431 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 432 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 433 #endif 434 return tp; 435 } 436 437 void 438 tcp_timer_persist(void *xtp) 439 { 440 struct tcpcb *tp = xtp; 441 struct callout *co = &tp->tt_persist->tc_callout; 442 443 crit_enter(); 444 if (callout_pending(co) || !callout_active(co)){ 445 crit_exit(); 446 return; 447 } 448 callout_deactivate(co); 449 tcp_send_timermsg(tp, TCP_TIMER_PERSIST); 450 crit_exit(); 451 } 452 453 void 454 tcp_save_congestion_state(struct tcpcb *tp) 455 { 456 tp->snd_cwnd_prev = tp->snd_cwnd; 457 tp->snd_wacked_prev = tp->snd_wacked; 458 tp->snd_ssthresh_prev = tp->snd_ssthresh; 459 tp->snd_recover_prev = tp->snd_recover; 460 if (IN_FASTRECOVERY(tp)) 461 tp->t_flags |= TF_WASFRECOVERY; 462 else 463 tp->t_flags &= ~TF_WASFRECOVERY; 464 if (tp->t_flags & TF_RCVD_TSTMP) { 465 tp->t_rexmtTS = ticks; 466 tp->t_flags |= TF_FIRSTACCACK; 467 } 468 #ifdef later 469 tcp_sack_save_scoreboard(&tp->scb); 470 #endif 471 } 472 473 void 474 tcp_revert_congestion_state(struct tcpcb *tp) 475 { 476 tp->snd_cwnd = tp->snd_cwnd_prev; 477 tp->snd_wacked = tp->snd_wacked_prev; 478 tp->snd_ssthresh = tp->snd_ssthresh_prev; 479 tp->snd_recover = tp->snd_recover_prev; 480 if (tp->t_flags & TF_WASFRECOVERY) 481 ENTER_FASTRECOVERY(tp); 482 if (tp->t_flags & TF_FASTREXMT) { 483 ++tcpstat.tcps_sndfastrexmitbad; 484 if (tp->t_flags & TF_EARLYREXMT) 485 ++tcpstat.tcps_sndearlyrexmitbad; 486 } else { 487 ++tcpstat.tcps_sndrtobad; 488 tp->snd_last = ticks; 489 } 490 tp->t_badrxtwin = 0; 491 tp->t_rxtshift = 0; 492 tp->snd_nxt = tp->snd_max; 493 #ifdef later 494 tcp_sack_revert_scoreboard(&tp->scb, tp->snd_una); 495 #endif 496 } 497 498 /* Caller should be in critical section */ 499 static struct tcpcb * 500 tcp_timer_rexmt_handler(struct tcpcb *tp) 501 { 502 int rexmt; 503 #ifdef TCPDEBUG 504 int ostate; 505 #endif 506 507 #ifdef TCPDEBUG 508 ostate = tp->t_state; 509 #endif 510 /* 511 * Retransmission timer went off. Message has not 512 * been acked within retransmit interval. Back off 513 * to a longer retransmit interval and retransmit one segment. 514 */ 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 goto out; 521 } 522 if (tp->t_rxtshift == 1) { 523 /* 524 * first retransmit; record ssthresh and cwnd so they can 525 * be recovered if this turns out to be a "bad" retransmit. 526 * A retransmit is considered "bad" if an ACK for this 527 * segment is received within RTT/2 interval; the assumption 528 * here is that the ACK was already in flight. See 529 * "On Estimating End-to-End Network Path Properties" by 530 * Allman and Paxson for more details. 531 */ 532 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 533 tcp_save_congestion_state(tp); 534 tp->t_flags &= ~(TF_FASTREXMT | TF_EARLYREXMT); 535 } 536 if (tp->t_state == TCPS_SYN_SENT || tp->t_state == TCPS_SYN_RECEIVED) { 537 /* 538 * Record the time that we spent in SYN or SYN|ACK 539 * retransmition. 540 * 541 * Needed by RFC3390 and RFC6298. 542 */ 543 tp->t_rxtsyn += tp->t_rxtcur; 544 } 545 /* Throw away SACK blocks on a RTO, as specified by RFC2018. */ 546 tcp_sack_cleanup(&tp->scb); 547 tcpstat.tcps_rexmttimeo++; 548 if (tp->t_state == TCPS_SYN_SENT) { 549 if (tcp_low_rtobase) { 550 rexmt = TCP_REXMTVAL(tp) * 551 tcp_syn_backoff_low[tp->t_rxtshift]; 552 } else { 553 rexmt = TCP_REXMTVAL(tp) * 554 tcp_syn_backoff[tp->t_rxtshift]; 555 } 556 } else { 557 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 558 } 559 TCPT_RANGESET(tp->t_rxtcur, rexmt, 560 tp->t_rttmin, TCPTV_REXMTMAX); 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 #ifdef INET6 571 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 572 in6_losing(tp->t_inpcb); 573 else 574 #endif 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 tp->rexmt_high = tp->snd_una; 581 tp->t_flags &= ~TF_SACKRESCUED; 582 tp->snd_recover = tp->snd_max; 583 /* 584 * Force a segment to be sent. 585 */ 586 tp->t_flags |= TF_ACKNOW; 587 /* 588 * If timing a segment in this window, stop the timer. 589 */ 590 tp->t_rtttime = 0; 591 /* 592 * Close the congestion window down to one segment 593 * (we'll open it by one segment for each ack we get). 594 * Since we probably have a window's worth of unacked 595 * data accumulated, this "slow start" keeps us from 596 * dumping all that data as back-to-back packets (which 597 * might overwhelm an intermediate gateway). 598 * 599 * There are two phases to the opening: Initially we 600 * open by one mss on each ack. This makes the window 601 * size increase exponentially with time. If the 602 * window is larger than the path can handle, this 603 * exponential growth results in dropped packet(s) 604 * almost immediately. To get more time between 605 * drops but still "push" the network to take advantage 606 * of improving conditions, we switch from exponential 607 * to linear window opening at some threshhold size. 608 * For a threshhold, we use half the current window 609 * size, truncated to a multiple of the mss. 610 * 611 * (the minimum cwnd that will give us exponential 612 * growth is 2 mss. We don't allow the threshhold 613 * to go below this.) 614 */ 615 { 616 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 617 618 if (win < 2) 619 win = 2; 620 tp->snd_cwnd = tp->t_maxseg; 621 tp->snd_wacked = 0; 622 tp->snd_ssthresh = win * tp->t_maxseg; 623 tp->t_dupacks = 0; 624 } 625 EXIT_FASTRECOVERY(tp); 626 tcp_output(tp); 627 628 out: 629 #ifdef TCPDEBUG 630 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 631 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 632 #endif 633 return tp; 634 } 635 636 void 637 tcp_timer_rexmt(void *xtp) 638 { 639 struct tcpcb *tp = xtp; 640 struct callout *co = &tp->tt_rexmt->tc_callout; 641 642 crit_enter(); 643 if (callout_pending(co) || !callout_active(co)) { 644 crit_exit(); 645 return; 646 } 647 callout_deactivate(co); 648 tcp_send_timermsg(tp, TCP_TIMER_REXMT); 649 crit_exit(); 650 } 651 652 static void 653 tcp_timer_handler(netmsg_t msg) 654 { 655 struct netmsg_tcp_timer *tmsg = (struct netmsg_tcp_timer *)msg; 656 const struct tcp_timer *tt; 657 struct tcpcb *tp; 658 659 crit_enter(); 660 661 KKASSERT(tmsg->tt_cpuid == mycpuid && tmsg->tt_tcb != NULL); 662 tp = tmsg->tt_tcb; 663 664 /* Save pending tasks and reset the tasks in message */ 665 tmsg->tt_running_tasks = tmsg->tt_tasks; 666 tmsg->tt_prev_tasks = tmsg->tt_tasks; 667 tmsg->tt_tasks = 0; 668 669 /* Reply ASAP */ 670 lwkt_replymsg(&tmsg->tt_msg.lmsg, 0); 671 672 if (tmsg->tt_running_tasks == 0) { 673 /* 674 * All of the timers are cancelled when the message 675 * is pending; bail out. 676 */ 677 crit_exit(); 678 return; 679 } 680 681 for (tt = tcp_timer_handlers; tt->tt_handler != NULL; ++tt) { 682 if ((tmsg->tt_running_tasks & tt->tt_task) == 0) 683 continue; 684 685 tmsg->tt_running_tasks &= ~tt->tt_task; 686 tp = tt->tt_handler(tp); 687 if (tp == NULL) 688 break; 689 690 if (tmsg->tt_running_tasks == 0) /* nothing left to do */ 691 break; 692 } 693 694 crit_exit(); 695 } 696 697 void 698 tcp_create_timermsg(struct tcpcb *tp, struct lwkt_port *msgport) 699 { 700 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 701 702 netmsg_init(&tmsg->tt_msg, NULL, &netisr_adone_rport, 703 MSGF_DROPABLE | MSGF_PRIORITY, tcp_timer_handler); 704 tmsg->tt_cpuid = mycpuid; 705 tmsg->tt_msgport = msgport; 706 tmsg->tt_tcb = tp; 707 tmsg->tt_tasks = 0; 708 } 709 710 void 711 tcp_destroy_timermsg(struct tcpcb *tp) 712 { 713 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 714 715 if (tmsg == NULL || /* listen socket */ 716 tmsg->tt_tcb == NULL) /* only tcp_attach() is called */ 717 return; 718 719 KKASSERT(tmsg->tt_cpuid == mycpuid); 720 crit_enter(); 721 if ((tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) == 0) { 722 /* 723 * This message is still pending to be processed; 724 * drop it. 725 */ 726 lwkt_dropmsg(&tmsg->tt_msg.lmsg); 727 } 728 crit_exit(); 729 } 730 731 static __inline void 732 tcp_callout_init(struct tcp_callout *tc, uint32_t task) 733 { 734 callout_init_mp(&tc->tc_callout); 735 tc->tc_task = task; 736 } 737 738 void 739 tcp_inittimers(struct tcpcb *tp) 740 { 741 tcp_callout_init(tp->tt_rexmt, TCP_TIMER_REXMT); 742 tcp_callout_init(tp->tt_persist, TCP_TIMER_PERSIST); 743 tcp_callout_init(tp->tt_keep, TCP_TIMER_KEEP); 744 tcp_callout_init(tp->tt_2msl, TCP_TIMER_2MSL); 745 tcp_callout_init(tp->tt_delack, TCP_TIMER_DELACK); 746 } 747