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