tcp: Expose tcp_keepcnt through sysctl net.inet.tcp.keepcnt
[dragonfly.git] / sys / netinet / tcp_timer.c
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
66d6c637
JH
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.
f23061d4 19 *
66d6c637
JH
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/*
984263bc
MD
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 $
09517e50 68 * $DragonFly: src/sys/netinet/tcp_timer.c,v 1.17 2008/03/30 20:39:01 dillon Exp $
984263bc
MD
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>
2b57d013
MD
83#include <sys/thread.h>
84#include <sys/globaldata.h>
1cae611f 85#include <sys/thread2.h>
0f758523 86#include <sys/msgport2.h>
984263bc
MD
87
88#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
89
90#include <net/route.h>
0f758523 91#include <net/netmsg2.h>
984263bc
MD
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>
a48c5dd5 104#include <netinet/tcp_timer2.h>
984263bc
MD
105#include <netinet/tcp_var.h>
106#include <netinet/tcpip.h>
107#ifdef TCPDEBUG
108#include <netinet/tcp_debug.h>
109#endif
110
0f758523
SZ
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
117static struct tcpcb *tcp_timer_rexmt_handler(struct tcpcb *);
118static struct tcpcb *tcp_timer_persist_handler(struct tcpcb *);
119static struct tcpcb *tcp_timer_keep_handler(struct tcpcb *);
120static struct tcpcb *tcp_timer_2msl_handler(struct tcpcb *);
121static struct tcpcb *tcp_timer_delack_handler(struct tcpcb *);
122
123static 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 },
0f758523 128 { TCP_TIMER_REXMT, tcp_timer_rexmt_handler },
a48c5dd5 129 { TCP_TIMER_PERSIST, tcp_timer_persist_handler },
0f758523
SZ
130 { TCP_TIMER_KEEP, tcp_timer_keep_handler },
131 { TCP_TIMER_2MSL, tcp_timer_2msl_handler },
132 { 0, NULL }
133};
134
984263bc
MD
135static int
136sysctl_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;
f23061d4 152 return (0);
984263bc
MD
153}
154
155int tcp_keepinit;
156SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
c9458756 157 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "Time to establish TCP connection");
984263bc
MD
158
159int tcp_keepidle;
160SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
c9458756 161 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "Time before TCP keepalive probes begin");
984263bc
MD
162
163int tcp_keepintvl;
164SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
c9458756 165 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "Time between TCP keepalive probes");
984263bc
MD
166
167int tcp_delacktime;
168SYSCTL_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");
f23061d4 171
984263bc
MD
172int tcp_msl;
173SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
174 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
175
176int tcp_rexmit_min;
177SYSCTL_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
180int tcp_rexmit_slop;
181SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
8acdb67c
JH
182 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
183 "Retransmission Timer Slop");
984263bc 184
09517e50 185static int always_keepalive = 1;
f23061d4 186SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
984263bc
MD
187 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
188
9269e6b6 189/* max idle probes */
984263bc 190static int tcp_keepcnt = TCPTV_KEEPCNT;
9269e6b6
SZ
191SYSCTL_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 */
984263bc 195int tcp_maxpersistidle;
9269e6b6 196
984263bc
MD
197int tcp_maxidle;
198
199/*
200 * Tcp protocol timeout routine called every 500 ms.
201 * Updates timestamps used for TCP
202 * causes finite state machine actions if timers expire.
203 */
204void
8a3125c6 205tcp_slowtimo(void)
984263bc 206{
1cae611f 207 crit_enter();
984263bc 208 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
1cae611f 209 crit_exit();
984263bc
MD
210}
211
212/*
213 * Cancel all timers for TCP tp.
214 */
215void
8a3125c6 216tcp_canceltimers(struct tcpcb *tp)
984263bc 217{
a48c5dd5
SZ
218 tcp_callout_stop(tp, tp->tt_2msl);
219 tcp_callout_stop(tp, tp->tt_persist);
220 tcp_callout_stop(tp, tp->tt_keep);
221 tcp_callout_stop(tp, tp->tt_rexmt);
984263bc
MD
222}
223
0f758523
SZ
224/*
225 * Caller should be in critical section
226 */
227static void
228tcp_send_timermsg(struct tcpcb *tp, uint32_t task)
229{
230 struct netmsg_tcp_timer *tmsg = tp->tt_msg;
231
232 KKASSERT(tmsg != NULL && tmsg->tt_cpuid == mycpuid &&
233 tmsg->tt_tcb != NULL);
234
235 tmsg->tt_tasks |= task;
002c1265
MD
236 if (tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE)
237 lwkt_sendmsg(tmsg->tt_msgport, &tmsg->tt_msg.lmsg);
0f758523
SZ
238}
239
984263bc
MD
240int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
241 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
242
243int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
244 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
245
246static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
247
0f758523
SZ
248/* Caller should be in critical section */
249static struct tcpcb *
250tcp_timer_delack_handler(struct tcpcb *tp)
251{
252 tp->t_flags |= TF_ACKNOW;
253 tcpstat.tcps_delack++;
254 tcp_output(tp);
255 return tp;
256}
257
984263bc
MD
258/*
259 * TCP timer processing.
260 */
261void
8a3125c6 262tcp_timer_delack(void *xtp)
984263bc
MD
263{
264 struct tcpcb *tp = xtp;
a48c5dd5 265 struct callout *co = &tp->tt_delack->tc_callout;
984263bc 266
1cae611f 267 crit_enter();
a48c5dd5 268 if (callout_pending(co) || !callout_active(co)) {
1cae611f 269 crit_exit();
984263bc
MD
270 return;
271 }
a48c5dd5 272 callout_deactivate(co);
0f758523 273 tcp_send_timermsg(tp, TCP_TIMER_DELACK);
1cae611f 274 crit_exit();
984263bc
MD
275}
276
0f758523
SZ
277/* Caller should be in critical section */
278static struct tcpcb *
279tcp_timer_2msl_handler(struct tcpcb *tp)
984263bc 280{
984263bc
MD
281#ifdef TCPDEBUG
282 int ostate;
0f758523 283#endif
984263bc 284
0f758523 285#ifdef TCPDEBUG
984263bc
MD
286 ostate = tp->t_state;
287#endif
984263bc
MD
288 /*
289 * 2 MSL timeout in shutdown went off. If we're closed but
290 * still waiting for peer to close and connection has been idle
291 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
292 * control block. Otherwise, check again in a bit.
293 */
294 if (tp->t_state != TCPS_TIME_WAIT &&
a48c5dd5
SZ
295 (ticks - tp->t_rcvtime) <= tcp_maxidle) {
296 tcp_callout_reset(tp, tp->tt_2msl, tcp_keepintvl,
297 tcp_timer_2msl);
298 } else {
984263bc 299 tp = tcp_close(tp);
a48c5dd5 300 }
984263bc
MD
301
302#ifdef TCPDEBUG
303 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
f23061d4 304 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
984263bc 305#endif
0f758523 306 return tp;
984263bc
MD
307}
308
309void
0f758523 310tcp_timer_2msl(void *xtp)
984263bc
MD
311{
312 struct tcpcb *tp = xtp;
a48c5dd5 313 struct callout *co = &tp->tt_2msl->tc_callout;
0f758523
SZ
314
315 crit_enter();
a48c5dd5 316 if (callout_pending(co) || !callout_active(co)) {
0f758523
SZ
317 crit_exit();
318 return;
319 }
a48c5dd5 320 callout_deactivate(co);
0f758523
SZ
321 tcp_send_timermsg(tp, TCP_TIMER_2MSL);
322 crit_exit();
323}
324
325/* Caller should be in critical section */
326static struct tcpcb *
327tcp_timer_keep_handler(struct tcpcb *tp)
328{
984263bc 329 struct tcptemp *t_template;
984263bc
MD
330#ifdef TCPDEBUG
331 int ostate;
0f758523 332#endif
2a0c7c52 333 int keepidle = tcp_getkeepidle(tp);
984263bc 334
0f758523 335#ifdef TCPDEBUG
984263bc
MD
336 ostate = tp->t_state;
337#endif
984263bc
MD
338 /*
339 * Keep-alive timer went off; send something
340 * or drop connection if idle for too long.
341 */
342 tcpstat.tcps_keeptimeo++;
343 if (tp->t_state < TCPS_ESTABLISHED)
344 goto dropit;
efca2b8e
MD
345 if ((always_keepalive || (tp->t_flags & TF_KEEPALIVE) ||
346 (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE)) &&
984263bc 347 tp->t_state <= TCPS_CLOSING) {
2a0c7c52 348 if ((ticks - tp->t_rcvtime) >= keepidle + tcp_maxidle)
984263bc
MD
349 goto dropit;
350 /*
351 * Send a packet designed to force a response
352 * if the peer is up and reachable:
353 * either an ACK if the connection is still alive,
354 * or an RST if the peer has closed the connection
355 * due to timeout or reboot.
356 * Using sequence number tp->snd_una-1
357 * causes the transmitted zero-length segment
358 * to lie outside the receive window;
359 * by the protocol spec, this requires the
360 * correspondent TCP to respond.
361 */
362 tcpstat.tcps_keepprobe++;
363 t_template = tcp_maketemplate(tp);
364 if (t_template) {
365 tcp_respond(tp, t_template->tt_ipgen,
2038fb68 366 &t_template->tt_t, NULL,
984263bc 367 tp->rcv_nxt, tp->snd_una - 1, 0);
dd2b0fb4 368 tcp_freetemplate(t_template);
984263bc 369 }
a48c5dd5
SZ
370 tcp_callout_reset(tp, tp->tt_keep, tcp_keepintvl,
371 tcp_timer_keep);
372 } else {
2a0c7c52 373 tcp_callout_reset(tp, tp->tt_keep, keepidle,
a48c5dd5
SZ
374 tcp_timer_keep);
375 }
984263bc
MD
376
377#ifdef TCPDEBUG
378 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
f23061d4 379 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
984263bc 380#endif
0f758523 381 return tp;
984263bc
MD
382
383dropit:
384 tcpstat.tcps_keepdrops++;
385 tp = tcp_drop(tp, ETIMEDOUT);
386
387#ifdef TCPDEBUG
388 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
f23061d4 389 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
984263bc 390#endif
0f758523 391 return tp;
984263bc
MD
392}
393
394void
0f758523 395tcp_timer_keep(void *xtp)
984263bc
MD
396{
397 struct tcpcb *tp = xtp;
a48c5dd5 398 struct callout *co = &tp->tt_keep->tc_callout;
984263bc 399
1cae611f 400 crit_enter();
a48c5dd5 401 if (callout_pending(co) || !callout_active(co)) {
1cae611f 402 crit_exit();
984263bc
MD
403 return;
404 }
a48c5dd5 405 callout_deactivate(co);
0f758523
SZ
406 tcp_send_timermsg(tp, TCP_TIMER_KEEP);
407 crit_exit();
408}
409
410/* Caller should be in critical section */
411static struct tcpcb *
412tcp_timer_persist_handler(struct tcpcb *tp)
413{
414#ifdef TCPDEBUG
415 int ostate;
416#endif
417
418#ifdef TCPDEBUG
419 ostate = tp->t_state;
420#endif
984263bc
MD
421 /*
422 * Persistance timer into zero window.
423 * Force a byte to be output, if possible.
424 */
425 tcpstat.tcps_persisttimeo++;
426 /*
427 * Hack: if the peer is dead/unreachable, we do not
428 * time out if the window is closed. After a full
429 * backoff, drop the connection if the idle time
430 * (no responses to probes) reaches the maximum
431 * backoff that we would use if retransmitting.
432 */
433 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
434 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
435 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
436 tcpstat.tcps_persistdrop++;
437 tp = tcp_drop(tp, ETIMEDOUT);
438 goto out;
439 }
440 tcp_setpersist(tp);
c1d3b892 441 tp->t_flags |= TF_FORCE;
f23061d4 442 tcp_output(tp);
c1d3b892 443 tp->t_flags &= ~TF_FORCE;
984263bc
MD
444
445out:
446#ifdef TCPDEBUG
447 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
f23061d4 448 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
984263bc 449#endif
0f758523
SZ
450 return tp;
451}
452
453void
454tcp_timer_persist(void *xtp)
455{
456 struct tcpcb *tp = xtp;
a48c5dd5 457 struct callout *co = &tp->tt_persist->tc_callout;
0f758523
SZ
458
459 crit_enter();
a48c5dd5 460 if (callout_pending(co) || !callout_active(co)){
0f758523
SZ
461 crit_exit();
462 return;
463 }
a48c5dd5 464 callout_deactivate(co);
0f758523 465 tcp_send_timermsg(tp, TCP_TIMER_PERSIST);
1cae611f 466 crit_exit();
984263bc
MD
467}
468
469void
efd4b327
JH
470tcp_save_congestion_state(struct tcpcb *tp)
471{
472 tp->snd_cwnd_prev = tp->snd_cwnd;
8acdb67c 473 tp->snd_wacked_prev = tp->snd_wacked;
efd4b327
JH
474 tp->snd_ssthresh_prev = tp->snd_ssthresh;
475 tp->snd_recover_prev = tp->snd_recover;
476 if (IN_FASTRECOVERY(tp))
8acdb67c 477 tp->t_flags |= TF_WASFRECOVERY;
efd4b327 478 else
8acdb67c 479 tp->t_flags &= ~TF_WASFRECOVERY;
efd4b327
JH
480 if (tp->t_flags & TF_RCVD_TSTMP) {
481 tp->t_rexmtTS = ticks;
482 tp->t_flags |= TF_FIRSTACCACK;
483 }
91489f6b
JH
484#ifdef later
485 tcp_sack_save_scoreboard(&tp->scb);
486#endif
efd4b327
JH
487}
488
489void
bfdb979e
JH
490tcp_revert_congestion_state(struct tcpcb *tp)
491{
492 tp->snd_cwnd = tp->snd_cwnd_prev;
8acdb67c 493 tp->snd_wacked = tp->snd_wacked_prev;
bfdb979e
JH
494 tp->snd_ssthresh = tp->snd_ssthresh_prev;
495 tp->snd_recover = tp->snd_recover_prev;
496 if (tp->t_flags & TF_WASFRECOVERY)
8acdb67c 497 ENTER_FASTRECOVERY(tp);
8819433a 498 if (tp->t_flags & TF_FASTREXMT) {
bfdb979e 499 ++tcpstat.tcps_sndfastrexmitbad;
8819433a
JH
500 if (tp->t_flags & TF_EARLYREXMT)
501 ++tcpstat.tcps_sndearlyrexmitbad;
502 } else
bfdb979e
JH
503 ++tcpstat.tcps_sndrtobad;
504 tp->t_badrxtwin = 0;
505 tp->t_rxtshift = 0;
506 tp->snd_nxt = tp->snd_max;
91489f6b
JH
507#ifdef later
508 tcp_sack_revert_scoreboard(&tp->scb, tp->snd_una);
509#endif
bfdb979e
JH
510}
511
0f758523
SZ
512/* Caller should be in critical section */
513static struct tcpcb *
514tcp_timer_rexmt_handler(struct tcpcb *tp)
984263bc 515{
984263bc
MD
516 int rexmt;
517#ifdef TCPDEBUG
518 int ostate;
0f758523 519#endif
984263bc 520
0f758523 521#ifdef TCPDEBUG
984263bc
MD
522 ostate = tp->t_state;
523#endif
984263bc
MD
524 /*
525 * Retransmission timer went off. Message has not
526 * been acked within retransmit interval. Back off
527 * to a longer retransmit interval and retransmit one segment.
528 */
529 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
530 tp->t_rxtshift = TCP_MAXRXTSHIFT;
531 tcpstat.tcps_timeoutdrop++;
532 tp = tcp_drop(tp, tp->t_softerror ?
533 tp->t_softerror : ETIMEDOUT);
534 goto out;
535 }
536 if (tp->t_rxtshift == 1) {
537 /*
538 * first retransmit; record ssthresh and cwnd so they can
f23061d4
JH
539 * be recovered if this turns out to be a "bad" retransmit.
540 * A retransmit is considered "bad" if an ACK for this
984263bc 541 * segment is received within RTT/2 interval; the assumption
f23061d4 542 * here is that the ACK was already in flight. See
984263bc
MD
543 * "On Estimating End-to-End Network Path Properties" by
544 * Allman and Paxson for more details.
545 */
984263bc 546 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
efd4b327 547 tcp_save_congestion_state(tp);
8819433a 548 tp->t_flags &= ~(TF_FASTREXMT | TF_EARLYREXMT);
984263bc 549 }
91489f6b
JH
550 /* Throw away SACK blocks on a RTO, as specified by RFC2018. */
551 tcp_sack_cleanup(&tp->scb);
984263bc
MD
552 tcpstat.tcps_rexmttimeo++;
553 if (tp->t_state == TCPS_SYN_SENT)
554 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
555 else
556 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
557 TCPT_RANGESET(tp->t_rxtcur, rexmt,
558 tp->t_rttmin, TCPTV_REXMTMAX);
559 /*
27b8aee3 560 * Disable rfc1323 if we havn't got any response to
f23061d4
JH
561 * our third SYN to work-around some broken terminal servers
562 * (most of which have hopefully been retired) that have bad VJ
563 * header compression code which trashes TCP segments containing
984263bc
MD
564 * unknown-to-them TCP options.
565 */
566 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
27b8aee3 567 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
984263bc
MD
568 /*
569 * If losing, let the lower level know and try for
570 * a better route. Also, if we backed off this far,
571 * our srtt estimate is probably bogus. Clobber it
572 * so we'll take the next rtt measurement as our srtt;
573 * move the current srtt into rttvar to keep the current
574 * retransmit times until then.
575 */
576 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
577#ifdef INET6
578 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
579 in6_losing(tp->t_inpcb);
580 else
581#endif
582 in_losing(tp->t_inpcb);
583 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
584 tp->t_srtt = 0;
585 }
586 tp->snd_nxt = tp->snd_una;
91489f6b 587 tp->rexmt_high = tp->snd_una;
9845754e 588 tp->snd_recover = tp->snd_max;
984263bc
MD
589 /*
590 * Force a segment to be sent.
591 */
592 tp->t_flags |= TF_ACKNOW;
593 /*
594 * If timing a segment in this window, stop the timer.
595 */
596 tp->t_rtttime = 0;
597 /*
598 * Close the congestion window down to one segment
599 * (we'll open it by one segment for each ack we get).
600 * Since we probably have a window's worth of unacked
601 * data accumulated, this "slow start" keeps us from
602 * dumping all that data as back-to-back packets (which
603 * might overwhelm an intermediate gateway).
604 *
605 * There are two phases to the opening: Initially we
606 * open by one mss on each ack. This makes the window
607 * size increase exponentially with time. If the
608 * window is larger than the path can handle, this
609 * exponential growth results in dropped packet(s)
610 * almost immediately. To get more time between
611 * drops but still "push" the network to take advantage
612 * of improving conditions, we switch from exponential
613 * to linear window opening at some threshhold size.
614 * For a threshhold, we use half the current window
615 * size, truncated to a multiple of the mss.
616 *
617 * (the minimum cwnd that will give us exponential
618 * growth is 2 mss. We don't allow the threshhold
619 * to go below this.)
620 */
621 {
622 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
8acdb67c 623
984263bc
MD
624 if (win < 2)
625 win = 2;
626 tp->snd_cwnd = tp->t_maxseg;
8acdb67c 627 tp->snd_wacked = 0;
984263bc
MD
628 tp->snd_ssthresh = win * tp->t_maxseg;
629 tp->t_dupacks = 0;
630 }
9845754e 631 EXIT_FASTRECOVERY(tp);
f23061d4 632 tcp_output(tp);
984263bc
MD
633
634out:
635#ifdef TCPDEBUG
636 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
f23061d4 637 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
984263bc 638#endif
0f758523
SZ
639 return tp;
640}
641
642void
643tcp_timer_rexmt(void *xtp)
644{
645 struct tcpcb *tp = xtp;
a48c5dd5 646 struct callout *co = &tp->tt_rexmt->tc_callout;
0f758523
SZ
647
648 crit_enter();
a48c5dd5 649 if (callout_pending(co) || !callout_active(co)) {
0f758523
SZ
650 crit_exit();
651 return;
652 }
a48c5dd5 653 callout_deactivate(co);
0f758523
SZ
654 tcp_send_timermsg(tp, TCP_TIMER_REXMT);
655 crit_exit();
656}
657
658static void
002c1265 659tcp_timer_handler(netmsg_t msg)
0f758523 660{
002c1265 661 struct netmsg_tcp_timer *tmsg = (struct netmsg_tcp_timer *)msg;
0f758523
SZ
662 const struct tcp_timer *tt;
663 struct tcpcb *tp;
0f758523
SZ
664
665 crit_enter();
666
667 KKASSERT(tmsg->tt_cpuid == mycpuid && tmsg->tt_tcb != NULL);
668 tp = tmsg->tt_tcb;
669
670 /* Save pending tasks and reset the tasks in message */
a48c5dd5
SZ
671 tmsg->tt_running_tasks = tmsg->tt_tasks;
672 tmsg->tt_prev_tasks = tmsg->tt_tasks;
0f758523
SZ
673 tmsg->tt_tasks = 0;
674
675 /* Reply ASAP */
002c1265 676 lwkt_replymsg(&tmsg->tt_msg.lmsg, 0);
0f758523 677
a48c5dd5
SZ
678 if (tmsg->tt_running_tasks == 0) {
679 /*
680 * All of the timers are cancelled when the message
681 * is pending; bail out.
682 */
683 crit_exit();
684 return;
685 }
686
0f758523 687 for (tt = tcp_timer_handlers; tt->tt_handler != NULL; ++tt) {
a48c5dd5 688 if ((tmsg->tt_running_tasks & tt->tt_task) == 0)
0f758523
SZ
689 continue;
690
a48c5dd5 691 tmsg->tt_running_tasks &= ~tt->tt_task;
0f758523
SZ
692 tp = tt->tt_handler(tp);
693 if (tp == NULL)
694 break;
695
a48c5dd5 696 if (tmsg->tt_running_tasks == 0) /* nothing left to do */
0f758523
SZ
697 break;
698 }
699
700 crit_exit();
701}
702
703void
3db1c8a3 704tcp_create_timermsg(struct tcpcb *tp, struct lwkt_port *msgport)
0f758523
SZ
705{
706 struct netmsg_tcp_timer *tmsg = tp->tt_msg;
707
002c1265 708 netmsg_init(&tmsg->tt_msg, NULL, &netisr_adone_rport,
d7d63d5d 709 MSGF_DROPABLE | MSGF_PRIORITY, tcp_timer_handler);
0f758523 710 tmsg->tt_cpuid = mycpuid;
3db1c8a3 711 tmsg->tt_msgport = msgport;
0f758523
SZ
712 tmsg->tt_tcb = tp;
713 tmsg->tt_tasks = 0;
714}
715
716void
717tcp_destroy_timermsg(struct tcpcb *tp)
718{
719 struct netmsg_tcp_timer *tmsg = tp->tt_msg;
720
721 if (tmsg == NULL || /* listen socket */
722 tmsg->tt_tcb == NULL) /* only tcp_attach() is called */
723 return;
724
725 KKASSERT(tmsg->tt_cpuid == mycpuid);
726 crit_enter();
002c1265 727 if ((tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) == 0) {
0f758523
SZ
728 /*
729 * This message is still pending to be processed;
730 * drop it.
731 */
002c1265 732 lwkt_dropmsg(&tmsg->tt_msg.lmsg);
0f758523 733 }
1cae611f 734 crit_exit();
984263bc 735}
a48c5dd5
SZ
736
737static __inline void
738tcp_callout_init(struct tcp_callout *tc, uint32_t task)
739{
740 callout_init(&tc->tc_callout);
741 tc->tc_task = task;
742}
743
744void
745tcp_inittimers(struct tcpcb *tp)
746{
747 tcp_callout_init(tp->tt_rexmt, TCP_TIMER_REXMT);
748 tcp_callout_init(tp->tt_persist, TCP_TIMER_PERSIST);
749 tcp_callout_init(tp->tt_keep, TCP_TIMER_KEEP);
750 tcp_callout_init(tp->tt_2msl, TCP_TIMER_2MSL);
751 tcp_callout_init(tp->tt_delack, TCP_TIMER_DELACK);
752}