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