123194Smckusick /*
2*69641Skarels * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
363218Sbostic * The Regents of the University of California. All rights reserved.
423194Smckusick *
544489Sbostic * %sccs.include.redist.c%
632789Sbostic *
7*69641Skarels * @(#)tcp_timer.c 8.2 (Berkeley) 05/24/95
823194Smckusick */
95069Swnj
1057947Ssklower #ifndef TUBA_INCLUDE
1156531Sbostic #include <sys/param.h>
1256531Sbostic #include <sys/systm.h>
1356531Sbostic #include <sys/malloc.h>
1456531Sbostic #include <sys/mbuf.h>
1556531Sbostic #include <sys/socket.h>
1656531Sbostic #include <sys/socketvar.h>
1756531Sbostic #include <sys/protosw.h>
1856531Sbostic #include <sys/errno.h>
198697Sroot
20*69641Skarels #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
21*69641Skarels
2256531Sbostic #include <net/if.h>
2356531Sbostic #include <net/route.h>
2410896Ssam
2556531Sbostic #include <netinet/in.h>
2656531Sbostic #include <netinet/in_systm.h>
2756531Sbostic #include <netinet/ip.h>
2856531Sbostic #include <netinet/in_pcb.h>
2956531Sbostic #include <netinet/ip_var.h>
3056531Sbostic #include <netinet/tcp.h>
3156531Sbostic #include <netinet/tcp_fsm.h>
3256531Sbostic #include <netinet/tcp_seq.h>
3356531Sbostic #include <netinet/tcp_timer.h>
3456531Sbostic #include <netinet/tcp_var.h>
3556531Sbostic #include <netinet/tcpip.h>
365069Swnj
3733746Skarels int tcp_keepidle = TCPTV_KEEP_IDLE;
3833746Skarels int tcp_keepintvl = TCPTV_KEEPINTVL;
39*69641Skarels int tcp_keepcnt = TCPTV_KEEPCNT; /* max idle probes */
40*69641Skarels int tcp_maxpersistidle = TCPTV_KEEP_IDLE; /* max idle time in persist */
4133746Skarels int tcp_maxidle;
42*69641Skarels #else /* TUBA_INCLUDE */
43*69641Skarels
44*69641Skarels extern int tcp_maxpersistidle;
4557947Ssklower #endif /* TUBA_INCLUDE */
46*69641Skarels
475069Swnj /*
485069Swnj * Fast timeout routine for processing delayed acks
495069Swnj */
5061335Sbostic void
tcp_fasttimo()515069Swnj tcp_fasttimo()
525069Swnj {
535284Sroot register struct inpcb *inp;
545284Sroot register struct tcpcb *tp;
555284Sroot int s = splnet();
565069Swnj
575691Swnj inp = tcb.inp_next;
585691Swnj if (inp)
595691Swnj for (; inp != &tcb; inp = inp->inp_next)
605284Sroot if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
615284Sroot (tp->t_flags & TF_DELACK)) {
625284Sroot tp->t_flags &= ~TF_DELACK;
635284Sroot tp->t_flags |= TF_ACKNOW;
6430524Skarels tcpstat.tcps_delack++;
655284Sroot (void) tcp_output(tp);
665284Sroot }
675284Sroot splx(s);
685069Swnj }
695069Swnj
705069Swnj /*
715069Swnj * Tcp protocol timeout routine called every 500 ms.
725069Swnj * Updates the timers in all active tcb's and
735069Swnj * causes finite state machine actions if timers expire.
745069Swnj */
7561335Sbostic void
tcp_slowtimo()765069Swnj tcp_slowtimo()
775069Swnj {
786212Swnj register struct inpcb *ip, *ipnxt;
795069Swnj register struct tcpcb *tp;
805069Swnj int s = splnet();
815069Swnj register int i;
825069Swnj
83*69641Skarels tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
845069Swnj /*
855069Swnj * Search through tcb's and update active timers.
865069Swnj */
875245Sroot ip = tcb.inp_next;
885245Sroot if (ip == 0) {
895245Sroot splx(s);
905245Sroot return;
915245Sroot }
9217315Skarels for (; ip != &tcb; ip = ipnxt) {
9317315Skarels ipnxt = ip->inp_next;
945069Swnj tp = intotcpcb(ip);
95*69641Skarels if (tp == 0 || tp->t_state == TCPS_LISTEN)
965260Swnj continue;
975075Swnj for (i = 0; i < TCPT_NTIMERS; i++) {
986212Swnj if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
995069Swnj (void) tcp_usrreq(tp->t_inpcb->inp_socket,
1005069Swnj PRU_SLOWTIMO, (struct mbuf *)0,
10112764Ssam (struct mbuf *)i, (struct mbuf *)0);
1026212Swnj if (ipnxt->inp_prev != ip)
1036212Swnj goto tpgone;
1046212Swnj }
1055069Swnj }
1065165Swnj tp->t_idle++;
1075165Swnj if (tp->t_rtt)
1085165Swnj tp->t_rtt++;
1096212Swnj tpgone:
11017315Skarels ;
1115069Swnj }
1125075Swnj tcp_iss += TCP_ISSINCR/PR_SLOWHZ; /* increment iss */
11327059Skarels #ifdef TCP_COMPAT_42
11426118Skarels if ((int)tcp_iss < 0)
115*69641Skarels tcp_iss = TCP_ISSINCR; /* XXX */
11626118Skarels #endif
11757433Sandrew tcp_now++; /* for timestamps */
1185069Swnj splx(s);
1195069Swnj }
12057947Ssklower #ifndef TUBA_INCLUDE
1215069Swnj
1225069Swnj /*
1235075Swnj * Cancel all timers for TCP tp.
1245069Swnj */
12561335Sbostic void
tcp_canceltimers(tp)1265089Swnj tcp_canceltimers(tp)
1275069Swnj struct tcpcb *tp;
1285069Swnj {
1295069Swnj register int i;
1305069Swnj
1315075Swnj for (i = 0; i < TCPT_NTIMERS; i++)
1325075Swnj tp->t_timer[i] = 0;
1335069Swnj }
1345069Swnj
13532034Skarels int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
13632034Skarels { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
13730762Skarels
138*69641Skarels int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
139*69641Skarels
1405069Swnj /*
1415165Swnj * TCP timer processing.
1425069Swnj */
14310396Ssam struct tcpcb *
tcp_timers(tp,timer)1445075Swnj tcp_timers(tp, timer)
1455069Swnj register struct tcpcb *tp;
1465075Swnj int timer;
1475069Swnj {
14825888Skarels register int rexmt;
1495069Swnj
1505089Swnj switch (timer) {
1515069Swnj
1525165Swnj /*
15324819Skarels * 2 MSL timeout in shutdown went off. If we're closed but
15424819Skarels * still waiting for peer to close and connection has been idle
15524819Skarels * too long, or if 2MSL time is up from TIME_WAIT, delete connection
15624819Skarels * control block. Otherwise, check again in a bit.
1575165Swnj */
1585075Swnj case TCPT_2MSL:
15924819Skarels if (tp->t_state != TCPS_TIME_WAIT &&
16033746Skarels tp->t_idle <= tcp_maxidle)
16133746Skarels tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
16224819Skarels else
16324819Skarels tp = tcp_close(tp);
16410396Ssam break;
1655069Swnj
1665165Swnj /*
1675165Swnj * Retransmission timer went off. Message has not
1685165Swnj * been acked within retransmit interval. Back off
16917362Skarels * to a longer retransmit interval and retransmit one segment.
1705165Swnj */
1715075Swnj case TCPT_REXMT:
17232034Skarels if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
17332034Skarels tp->t_rxtshift = TCP_MAXRXTSHIFT;
17430524Skarels tcpstat.tcps_timeoutdrop++;
17544379Skarels tp = tcp_drop(tp, tp->t_softerror ?
17644379Skarels tp->t_softerror : ETIMEDOUT);
17710396Ssam break;
1785691Swnj }
17930524Skarels tcpstat.tcps_rexmttimeo++;
18044379Skarels rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
18144379Skarels TCPT_RANGESET(tp->t_rxtcur, rexmt,
18244379Skarels tp->t_rttmin, TCPTV_REXMTMAX);
18332034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
18426989Skarels /*
18532034Skarels * If losing, let the lower level know and try for
18632034Skarels * a better route. Also, if we backed off this far,
18732034Skarels * our srtt estimate is probably bogus. Clobber it
18832034Skarels * so we'll take the next rtt measurement as our srtt;
18932034Skarels * move the current srtt into rttvar to keep the current
19032034Skarels * retransmit times until then.
19126989Skarels */
19232034Skarels if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
19326989Skarels in_losing(tp->t_inpcb);
19444379Skarels tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
19532034Skarels tp->t_srtt = 0;
19632034Skarels }
1975165Swnj tp->snd_nxt = tp->snd_una;
19817314Skarels /*
19931726Skarels * If timing a segment in this window, stop the timer.
20017314Skarels */
20131726Skarels tp->t_rtt = 0;
20231442Skarels /*
20331442Skarels * Close the congestion window down to one segment
20431442Skarels * (we'll open it by one segment for each ack we get).
20531442Skarels * Since we probably have a window's worth of unacked
20631442Skarels * data accumulated, this "slow start" keeps us from
20731442Skarels * dumping all that data as back-to-back packets (which
20831442Skarels * might overwhelm an intermediate gateway).
20932100Skarels *
21032100Skarels * There are two phases to the opening: Initially we
21132100Skarels * open by one mss on each ack. This makes the window
21232100Skarels * size increase exponentially with time. If the
21332100Skarels * window is larger than the path can handle, this
21432100Skarels * exponential growth results in dropped packet(s)
21532100Skarels * almost immediately. To get more time between
21632100Skarels * drops but still "push" the network to take advantage
21732100Skarels * of improving conditions, we switch from exponential
21832100Skarels * to linear window opening at some threshhold size.
21932100Skarels * For a threshhold, we use half the current window
22032100Skarels * size, truncated to a multiple of the mss.
22132100Skarels *
22232100Skarels * (the minimum cwnd that will give us exponential
22332100Skarels * growth is 2 mss. We don't allow the threshhold
22432100Skarels * to go below this.)
22531442Skarels */
22632100Skarels {
22744379Skarels u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
22832100Skarels if (win < 2)
22932100Skarels win = 2;
23031442Skarels tp->snd_cwnd = tp->t_maxseg;
23132100Skarels tp->snd_ssthresh = win * tp->t_maxseg;
23244379Skarels tp->t_dupacks = 0;
23332100Skarels }
2345165Swnj (void) tcp_output(tp);
23510396Ssam break;
2365069Swnj
2375165Swnj /*
2385165Swnj * Persistance timer into zero window.
2395165Swnj * Force a byte to be output, if possible.
2405165Swnj */
2415075Swnj case TCPT_PERSIST:
24230524Skarels tcpstat.tcps_persisttimeo++;
243*69641Skarels /*
244*69641Skarels * Hack: if the peer is dead/unreachable, we do not
245*69641Skarels * time out if the window is closed. After a full
246*69641Skarels * backoff, drop the connection if the idle time
247*69641Skarels * (no responses to probes) reaches the maximum
248*69641Skarels * backoff that we would use if retransmitting.
249*69641Skarels */
250*69641Skarels if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
251*69641Skarels (tp->t_idle >= tcp_maxpersistidle ||
252*69641Skarels tp->t_idle >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
253*69641Skarels tcpstat.tcps_persistdrop++;
254*69641Skarels tp = tcp_drop(tp, ETIMEDOUT);
255*69641Skarels break;
256*69641Skarels }
2577156Swnj tcp_setpersist(tp);
2585165Swnj tp->t_force = 1;
2595165Swnj (void) tcp_output(tp);
2605165Swnj tp->t_force = 0;
26110396Ssam break;
2625069Swnj
2635165Swnj /*
2645165Swnj * Keep-alive timer went off; send something
2655165Swnj * or drop connection if idle for too long.
2665165Swnj */
2675075Swnj case TCPT_KEEP:
26830524Skarels tcpstat.tcps_keeptimeo++;
2696304Sroot if (tp->t_state < TCPS_ESTABLISHED)
2706304Sroot goto dropit;
27127193Skarels if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
27227193Skarels tp->t_state <= TCPS_CLOSE_WAIT) {
27333746Skarels if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
2746304Sroot goto dropit;
2756267Sroot /*
27630524Skarels * Send a packet designed to force a response
27730524Skarels * if the peer is up and reachable:
27830524Skarels * either an ACK if the connection is still alive,
27930524Skarels * or an RST if the peer has closed the connection
28030524Skarels * due to timeout or reboot.
28130524Skarels * Using sequence number tp->snd_una-1
28230524Skarels * causes the transmitted zero-length segment
28330524Skarels * to lie outside the receive window;
28430524Skarels * by the protocol spec, this requires the
28530524Skarels * correspondent TCP to respond.
2866267Sroot */
28730524Skarels tcpstat.tcps_keepprobe++;
28831727Skarels #ifdef TCP_COMPAT_42
28931727Skarels /*
29031727Skarels * The keepalive packet must have nonzero length
29131727Skarels * to get a 4.2 host to respond.
29231727Skarels */
29337321Skarels tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
29431727Skarels tp->rcv_nxt - 1, tp->snd_una - 1, 0);
29531727Skarels #else
29637321Skarels tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
29731727Skarels tp->rcv_nxt, tp->snd_una - 1, 0);
29831727Skarels #endif
29933746Skarels tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
30033746Skarels } else
30133746Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle;
30210396Ssam break;
3036304Sroot dropit:
30430524Skarels tcpstat.tcps_keepdrops++;
30510396Ssam tp = tcp_drop(tp, ETIMEDOUT);
30610396Ssam break;
3075069Swnj }
30810396Ssam return (tp);
3095069Swnj }
31057947Ssklower #endif /* TUBA_INCLUDE */
311