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