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 static int tcp_keepcnt = TCPTV_KEEPCNT; 190 /* max idle probes */ 191 int tcp_maxpersistidle; 192 /* max idle time in persist */ 193 int tcp_maxidle; 194 195 /* 196 * Tcp protocol timeout routine called every 500 ms. 197 * Updates timestamps used for TCP 198 * causes finite state machine actions if timers expire. 199 */ 200 void 201 tcp_slowtimo(void) 202 { 203 crit_enter(); 204 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 205 crit_exit(); 206 } 207 208 /* 209 * Cancel all timers for TCP tp. 210 */ 211 void 212 tcp_canceltimers(struct tcpcb *tp) 213 { 214 tcp_callout_stop(tp, tp->tt_2msl); 215 tcp_callout_stop(tp, tp->tt_persist); 216 tcp_callout_stop(tp, tp->tt_keep); 217 tcp_callout_stop(tp, tp->tt_rexmt); 218 } 219 220 /* 221 * Caller should be in critical section 222 */ 223 static void 224 tcp_send_timermsg(struct tcpcb *tp, uint32_t task) 225 { 226 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 227 228 KKASSERT(tmsg != NULL && tmsg->tt_cpuid == mycpuid && 229 tmsg->tt_tcb != NULL); 230 231 tmsg->tt_tasks |= task; 232 if (tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) 233 lwkt_sendmsg(tmsg->tt_msgport, &tmsg->tt_msg.lmsg); 234 } 235 236 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 237 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 238 239 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 240 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 241 242 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 243 244 /* Caller should be in critical section */ 245 static struct tcpcb * 246 tcp_timer_delack_handler(struct tcpcb *tp) 247 { 248 tp->t_flags |= TF_ACKNOW; 249 tcpstat.tcps_delack++; 250 tcp_output(tp); 251 return tp; 252 } 253 254 /* 255 * TCP timer processing. 256 */ 257 void 258 tcp_timer_delack(void *xtp) 259 { 260 struct tcpcb *tp = xtp; 261 struct callout *co = &tp->tt_delack->tc_callout; 262 263 crit_enter(); 264 if (callout_pending(co) || !callout_active(co)) { 265 crit_exit(); 266 return; 267 } 268 callout_deactivate(co); 269 tcp_send_timermsg(tp, TCP_TIMER_DELACK); 270 crit_exit(); 271 } 272 273 /* Caller should be in critical section */ 274 static struct tcpcb * 275 tcp_timer_2msl_handler(struct tcpcb *tp) 276 { 277 #ifdef TCPDEBUG 278 int ostate; 279 #endif 280 281 #ifdef TCPDEBUG 282 ostate = tp->t_state; 283 #endif 284 /* 285 * 2 MSL timeout in shutdown went off. If we're closed but 286 * still waiting for peer to close and connection has been idle 287 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 288 * control block. Otherwise, check again in a bit. 289 */ 290 if (tp->t_state != TCPS_TIME_WAIT && 291 (ticks - tp->t_rcvtime) <= tcp_maxidle) { 292 tcp_callout_reset(tp, tp->tt_2msl, tcp_keepintvl, 293 tcp_timer_2msl); 294 } else { 295 tp = tcp_close(tp); 296 } 297 298 #ifdef TCPDEBUG 299 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 300 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 301 #endif 302 return tp; 303 } 304 305 void 306 tcp_timer_2msl(void *xtp) 307 { 308 struct tcpcb *tp = xtp; 309 struct callout *co = &tp->tt_2msl->tc_callout; 310 311 crit_enter(); 312 if (callout_pending(co) || !callout_active(co)) { 313 crit_exit(); 314 return; 315 } 316 callout_deactivate(co); 317 tcp_send_timermsg(tp, TCP_TIMER_2MSL); 318 crit_exit(); 319 } 320 321 /* Caller should be in critical section */ 322 static struct tcpcb * 323 tcp_timer_keep_handler(struct tcpcb *tp) 324 { 325 struct tcptemp *t_template; 326 #ifdef TCPDEBUG 327 int ostate; 328 #endif 329 int keepidle = tcp_getkeepidle(tp); 330 331 #ifdef TCPDEBUG 332 ostate = tp->t_state; 333 #endif 334 /* 335 * Keep-alive timer went off; send something 336 * or drop connection if idle for too long. 337 */ 338 tcpstat.tcps_keeptimeo++; 339 if (tp->t_state < TCPS_ESTABLISHED) 340 goto dropit; 341 if ((always_keepalive || (tp->t_flags & TF_KEEPALIVE) || 342 (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE)) && 343 tp->t_state <= TCPS_CLOSING) { 344 if ((ticks - tp->t_rcvtime) >= keepidle + tcp_maxidle) 345 goto dropit; 346 /* 347 * Send a packet designed to force a response 348 * if the peer is up and reachable: 349 * either an ACK if the connection is still alive, 350 * or an RST if the peer has closed the connection 351 * due to timeout or reboot. 352 * Using sequence number tp->snd_una-1 353 * causes the transmitted zero-length segment 354 * to lie outside the receive window; 355 * by the protocol spec, this requires the 356 * correspondent TCP to respond. 357 */ 358 tcpstat.tcps_keepprobe++; 359 t_template = tcp_maketemplate(tp); 360 if (t_template) { 361 tcp_respond(tp, t_template->tt_ipgen, 362 &t_template->tt_t, NULL, 363 tp->rcv_nxt, tp->snd_una - 1, 0); 364 tcp_freetemplate(t_template); 365 } 366 tcp_callout_reset(tp, tp->tt_keep, tcp_keepintvl, 367 tcp_timer_keep); 368 } else { 369 tcp_callout_reset(tp, tp->tt_keep, keepidle, 370 tcp_timer_keep); 371 } 372 373 #ifdef TCPDEBUG 374 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 375 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 376 #endif 377 return tp; 378 379 dropit: 380 tcpstat.tcps_keepdrops++; 381 tp = tcp_drop(tp, ETIMEDOUT); 382 383 #ifdef TCPDEBUG 384 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 385 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 386 #endif 387 return tp; 388 } 389 390 void 391 tcp_timer_keep(void *xtp) 392 { 393 struct tcpcb *tp = xtp; 394 struct callout *co = &tp->tt_keep->tc_callout; 395 396 crit_enter(); 397 if (callout_pending(co) || !callout_active(co)) { 398 crit_exit(); 399 return; 400 } 401 callout_deactivate(co); 402 tcp_send_timermsg(tp, TCP_TIMER_KEEP); 403 crit_exit(); 404 } 405 406 /* Caller should be in critical section */ 407 static struct tcpcb * 408 tcp_timer_persist_handler(struct tcpcb *tp) 409 { 410 #ifdef TCPDEBUG 411 int ostate; 412 #endif 413 414 #ifdef TCPDEBUG 415 ostate = tp->t_state; 416 #endif 417 /* 418 * Persistance timer into zero window. 419 * Force a byte to be output, if possible. 420 */ 421 tcpstat.tcps_persisttimeo++; 422 /* 423 * Hack: if the peer is dead/unreachable, we do not 424 * time out if the window is closed. After a full 425 * backoff, drop the connection if the idle time 426 * (no responses to probes) reaches the maximum 427 * backoff that we would use if retransmitting. 428 */ 429 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 430 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 431 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 432 tcpstat.tcps_persistdrop++; 433 tp = tcp_drop(tp, ETIMEDOUT); 434 goto out; 435 } 436 tcp_setpersist(tp); 437 tp->t_flags |= TF_FORCE; 438 tcp_output(tp); 439 tp->t_flags &= ~TF_FORCE; 440 441 out: 442 #ifdef TCPDEBUG 443 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 444 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 445 #endif 446 return tp; 447 } 448 449 void 450 tcp_timer_persist(void *xtp) 451 { 452 struct tcpcb *tp = xtp; 453 struct callout *co = &tp->tt_persist->tc_callout; 454 455 crit_enter(); 456 if (callout_pending(co) || !callout_active(co)){ 457 crit_exit(); 458 return; 459 } 460 callout_deactivate(co); 461 tcp_send_timermsg(tp, TCP_TIMER_PERSIST); 462 crit_exit(); 463 } 464 465 void 466 tcp_save_congestion_state(struct tcpcb *tp) 467 { 468 tp->snd_cwnd_prev = tp->snd_cwnd; 469 tp->snd_wacked_prev = tp->snd_wacked; 470 tp->snd_ssthresh_prev = tp->snd_ssthresh; 471 tp->snd_recover_prev = tp->snd_recover; 472 if (IN_FASTRECOVERY(tp)) 473 tp->t_flags |= TF_WASFRECOVERY; 474 else 475 tp->t_flags &= ~TF_WASFRECOVERY; 476 if (tp->t_flags & TF_RCVD_TSTMP) { 477 tp->t_rexmtTS = ticks; 478 tp->t_flags |= TF_FIRSTACCACK; 479 } 480 #ifdef later 481 tcp_sack_save_scoreboard(&tp->scb); 482 #endif 483 } 484 485 void 486 tcp_revert_congestion_state(struct tcpcb *tp) 487 { 488 tp->snd_cwnd = tp->snd_cwnd_prev; 489 tp->snd_wacked = tp->snd_wacked_prev; 490 tp->snd_ssthresh = tp->snd_ssthresh_prev; 491 tp->snd_recover = tp->snd_recover_prev; 492 if (tp->t_flags & TF_WASFRECOVERY) 493 ENTER_FASTRECOVERY(tp); 494 if (tp->t_flags & TF_FASTREXMT) { 495 ++tcpstat.tcps_sndfastrexmitbad; 496 if (tp->t_flags & TF_EARLYREXMT) 497 ++tcpstat.tcps_sndearlyrexmitbad; 498 } else 499 ++tcpstat.tcps_sndrtobad; 500 tp->t_badrxtwin = 0; 501 tp->t_rxtshift = 0; 502 tp->snd_nxt = tp->snd_max; 503 #ifdef later 504 tcp_sack_revert_scoreboard(&tp->scb, tp->snd_una); 505 #endif 506 } 507 508 /* Caller should be in critical section */ 509 static struct tcpcb * 510 tcp_timer_rexmt_handler(struct tcpcb *tp) 511 { 512 int rexmt; 513 #ifdef TCPDEBUG 514 int ostate; 515 #endif 516 517 #ifdef TCPDEBUG 518 ostate = tp->t_state; 519 #endif 520 /* 521 * Retransmission timer went off. Message has not 522 * been acked within retransmit interval. Back off 523 * to a longer retransmit interval and retransmit one segment. 524 */ 525 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 526 tp->t_rxtshift = TCP_MAXRXTSHIFT; 527 tcpstat.tcps_timeoutdrop++; 528 tp = tcp_drop(tp, tp->t_softerror ? 529 tp->t_softerror : ETIMEDOUT); 530 goto out; 531 } 532 if (tp->t_rxtshift == 1) { 533 /* 534 * first retransmit; record ssthresh and cwnd so they can 535 * be recovered if this turns out to be a "bad" retransmit. 536 * A retransmit is considered "bad" if an ACK for this 537 * segment is received within RTT/2 interval; the assumption 538 * here is that the ACK was already in flight. See 539 * "On Estimating End-to-End Network Path Properties" by 540 * Allman and Paxson for more details. 541 */ 542 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 543 tcp_save_congestion_state(tp); 544 tp->t_flags &= ~(TF_FASTREXMT | TF_EARLYREXMT); 545 } 546 /* Throw away SACK blocks on a RTO, as specified by RFC2018. */ 547 tcp_sack_cleanup(&tp->scb); 548 tcpstat.tcps_rexmttimeo++; 549 if (tp->t_state == TCPS_SYN_SENT) 550 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift]; 551 else 552 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 553 TCPT_RANGESET(tp->t_rxtcur, rexmt, 554 tp->t_rttmin, TCPTV_REXMTMAX); 555 /* 556 * Disable rfc1323 if we havn't got any response to 557 * our third SYN to work-around some broken terminal servers 558 * (most of which have hopefully been retired) that have bad VJ 559 * header compression code which trashes TCP segments containing 560 * unknown-to-them TCP options. 561 */ 562 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3)) 563 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP); 564 /* 565 * If losing, let the lower level know and try for 566 * a better route. Also, if we backed off this far, 567 * our srtt estimate is probably bogus. Clobber it 568 * so we'll take the next rtt measurement as our srtt; 569 * move the current srtt into rttvar to keep the current 570 * retransmit times until then. 571 */ 572 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 573 #ifdef INET6 574 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 575 in6_losing(tp->t_inpcb); 576 else 577 #endif 578 in_losing(tp->t_inpcb); 579 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 580 tp->t_srtt = 0; 581 } 582 tp->snd_nxt = tp->snd_una; 583 tp->rexmt_high = tp->snd_una; 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 620 if (win < 2) 621 win = 2; 622 tp->snd_cwnd = tp->t_maxseg; 623 tp->snd_wacked = 0; 624 tp->snd_ssthresh = win * tp->t_maxseg; 625 tp->t_dupacks = 0; 626 } 627 EXIT_FASTRECOVERY(tp); 628 tcp_output(tp); 629 630 out: 631 #ifdef TCPDEBUG 632 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 633 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 634 #endif 635 return tp; 636 } 637 638 void 639 tcp_timer_rexmt(void *xtp) 640 { 641 struct tcpcb *tp = xtp; 642 struct callout *co = &tp->tt_rexmt->tc_callout; 643 644 crit_enter(); 645 if (callout_pending(co) || !callout_active(co)) { 646 crit_exit(); 647 return; 648 } 649 callout_deactivate(co); 650 tcp_send_timermsg(tp, TCP_TIMER_REXMT); 651 crit_exit(); 652 } 653 654 static void 655 tcp_timer_handler(netmsg_t msg) 656 { 657 struct netmsg_tcp_timer *tmsg = (struct netmsg_tcp_timer *)msg; 658 const struct tcp_timer *tt; 659 struct tcpcb *tp; 660 661 crit_enter(); 662 663 KKASSERT(tmsg->tt_cpuid == mycpuid && tmsg->tt_tcb != NULL); 664 tp = tmsg->tt_tcb; 665 666 /* Save pending tasks and reset the tasks in message */ 667 tmsg->tt_running_tasks = tmsg->tt_tasks; 668 tmsg->tt_prev_tasks = tmsg->tt_tasks; 669 tmsg->tt_tasks = 0; 670 671 /* Reply ASAP */ 672 lwkt_replymsg(&tmsg->tt_msg.lmsg, 0); 673 674 if (tmsg->tt_running_tasks == 0) { 675 /* 676 * All of the timers are cancelled when the message 677 * is pending; bail out. 678 */ 679 crit_exit(); 680 return; 681 } 682 683 for (tt = tcp_timer_handlers; tt->tt_handler != NULL; ++tt) { 684 if ((tmsg->tt_running_tasks & tt->tt_task) == 0) 685 continue; 686 687 tmsg->tt_running_tasks &= ~tt->tt_task; 688 tp = tt->tt_handler(tp); 689 if (tp == NULL) 690 break; 691 692 if (tmsg->tt_running_tasks == 0) /* nothing left to do */ 693 break; 694 } 695 696 crit_exit(); 697 } 698 699 void 700 tcp_create_timermsg(struct tcpcb *tp, struct lwkt_port *msgport) 701 { 702 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 703 704 netmsg_init(&tmsg->tt_msg, NULL, &netisr_adone_rport, 705 MSGF_DROPABLE | MSGF_PRIORITY, tcp_timer_handler); 706 tmsg->tt_cpuid = mycpuid; 707 tmsg->tt_msgport = msgport; 708 tmsg->tt_tcb = tp; 709 tmsg->tt_tasks = 0; 710 } 711 712 void 713 tcp_destroy_timermsg(struct tcpcb *tp) 714 { 715 struct netmsg_tcp_timer *tmsg = tp->tt_msg; 716 717 if (tmsg == NULL || /* listen socket */ 718 tmsg->tt_tcb == NULL) /* only tcp_attach() is called */ 719 return; 720 721 KKASSERT(tmsg->tt_cpuid == mycpuid); 722 crit_enter(); 723 if ((tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) == 0) { 724 /* 725 * This message is still pending to be processed; 726 * drop it. 727 */ 728 lwkt_dropmsg(&tmsg->tt_msg.lmsg); 729 } 730 crit_exit(); 731 } 732 733 static __inline void 734 tcp_callout_init(struct tcp_callout *tc, uint32_t task) 735 { 736 callout_init(&tc->tc_callout); 737 tc->tc_task = task; 738 } 739 740 void 741 tcp_inittimers(struct tcpcb *tp) 742 { 743 tcp_callout_init(tp->tt_rexmt, TCP_TIMER_REXMT); 744 tcp_callout_init(tp->tt_persist, TCP_TIMER_PERSIST); 745 tcp_callout_init(tp->tt_keep, TCP_TIMER_KEEP); 746 tcp_callout_init(tp->tt_2msl, TCP_TIMER_2MSL); 747 tcp_callout_init(tp->tt_delack, TCP_TIMER_DELACK); 748 } 749