xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 5952)
1 /* tcp_timer.c 4.15 82/02/25 */
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../h/protosw.h"
9 #include "../net/in.h"
10 #include "../net/in_pcb.h"
11 #include "../net/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/ip.h"
14 #include "../net/ip_var.h"
15 #include "../net/tcp.h"
16 #include "../net/tcp_fsm.h"
17 #include "../net/tcp_seq.h"
18 #include "../net/tcp_timer.h"
19 #include "../net/tcp_var.h"
20 #include "../net/tcpip.h"
21 #include "../errno.h"
22 
23 int	tcpnodelack = 0;
24 /*
25  * Fast timeout routine for processing delayed acks
26  */
27 tcp_fasttimo()
28 {
29 	register struct inpcb *inp;
30 	register struct tcpcb *tp;
31 	int s = splnet();
32 COUNT(TCP_FASTTIMO);
33 
34 	inp = tcb.inp_next;
35 	if (inp)
36 	for (; inp != &tcb; inp = inp->inp_next)
37 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
38 		    (tp->t_flags & TF_DELACK)) {
39 			tp->t_flags &= ~TF_DELACK;
40 			tp->t_flags |= TF_ACKNOW;
41 			(void) tcp_output(tp);
42 		}
43 	splx(s);
44 }
45 
46 /*
47  * Tcp protocol timeout routine called every 500 ms.
48  * Updates the timers in all active tcb's and
49  * causes finite state machine actions if timers expire.
50  */
51 tcp_slowtimo()
52 {
53 	register struct inpcb *ip;
54 	register struct tcpcb *tp;
55 	int s = splnet();
56 	register int i;
57 COUNT(TCP_SLOWTIMO);
58 
59 	/*
60 	 * Search through tcb's and update active timers.
61 	 */
62 	ip = tcb.inp_next;
63 	if (ip == 0) {
64 		splx(s);
65 		return;
66 	}
67 	for (; ip != &tcb; ip = ip->inp_next) {
68 		tp = intotcpcb(ip);
69 		if (tp == 0)
70 			continue;
71 		for (i = 0; i < TCPT_NTIMERS; i++) {
72 			if (tp->t_timer[i] && --tp->t_timer[i] == 0)
73 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
74 				    PRU_SLOWTIMO, (struct mbuf *)0,
75 				    (caddr_t)i);
76 		}
77 		tp->t_idle++;
78 		if (tp->t_rtt)
79 			tp->t_rtt++;
80 	}
81 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
82 	splx(s);
83 }
84 
85 /*
86  * Cancel all timers for TCP tp.
87  */
88 tcp_canceltimers(tp)
89 	struct tcpcb *tp;
90 {
91 	register int i;
92 
93 COUNT(TCP_CANCELTIMERS);
94 	for (i = 0; i < TCPT_NTIMERS; i++)
95 		tp->t_timer[i] = 0;
96 }
97 
98 int	tcprexmtprint;
99 /*
100  * TCP timer processing.
101  */
102 tcp_timers(tp, timer)
103 	register struct tcpcb *tp;
104 	int timer;
105 {
106 
107 COUNT(TCP_TIMERS);
108 	switch (timer) {
109 
110 	/*
111 	 * 2 MSL timeout in shutdown went off.  Delete connection
112 	 * control block.
113 	 */
114 	case TCPT_2MSL:
115 		tcp_close(tp);
116 		return;
117 
118 	/*
119 	 * Retransmission timer went off.  Message has not
120 	 * been acked within retransmit interval.  Back off
121 	 * to a longer retransmit interval and retransmit all
122 	 * unacknowledged messages in the window.
123 	 */
124 	case TCPT_REXMT:
125 		tp->t_rxtshift++;
126 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
127 			tcp_drop(tp, ETIMEDOUT);
128 			return;
129 		}
130 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
131 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
132 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
133 		    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
134 		    TCPTV_MIN, TCPTV_MAX);
135 if (tcprexmtprint)
136 printf("rexmt set to %d\n", tp->t_timer[TCPT_REXMT]);
137 		tp->snd_nxt = tp->snd_una;
138 		/* this only transmits one segment! */
139 		(void) tcp_output(tp);
140 		return;
141 
142 	/*
143 	 * Persistance timer into zero window.
144 	 * Force a byte to be output, if possible.
145 	 */
146 	case TCPT_PERSIST:
147 		tp->t_force = 1;
148 		(void) tcp_output(tp);
149 		tp->t_force = 0;
150 		TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
151 		    tcp_beta * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
152 		return;
153 
154 	/*
155 	 * Keep-alive timer went off; send something
156 	 * or drop connection if idle for too long.
157 	 */
158 	case TCPT_KEEP:
159 		if (tp->t_state < TCPS_ESTABLISHED ||
160 		    tp->t_idle >= TCPTV_MAXIDLE) {
161 			tcp_drop(tp, ETIMEDOUT);
162 			return;
163 		}
164 		if (tp->t_inpcb->inp_socket->so_options & SO_NOKEEPALIVE)
165 			tp->t_idle = 0;
166 		else
167 			tcp_respond(tp,
168 			    tp->t_template, tp->rcv_nxt, tp->snd_una-1, 0);
169 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
170 		return;
171 
172 #ifdef TCPTRUEOOB
173 	/*
174 	 * Out-of-band data retransmit timer.
175 	 */
176 	case TCPT_OOBREXMT:
177 		if (tp->t_flags & TF_NOOPT)
178 			return;
179 		(void) tcp_output(tp);
180 		TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT],
181 		    2 * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
182 		return;
183 #endif
184 	}
185 }
186