xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 5691)
1 /* tcp_timer.c 4.14 82/02/03 */
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 /*
99  * TCP timer processing.
100  */
101 tcp_timers(tp, timer)
102 	register struct tcpcb *tp;
103 	int timer;
104 {
105 
106 COUNT(TCP_TIMERS);
107 	switch (timer) {
108 
109 	/*
110 	 * 2 MSL timeout in shutdown went off.  Delete connection
111 	 * control block.
112 	 */
113 	case TCPT_2MSL:
114 		tcp_close(tp);
115 		return;
116 
117 	/*
118 	 * Retransmission timer went off.  Message has not
119 	 * been acked within retransmit interval.  Back off
120 	 * to a longer retransmit interval and retransmit all
121 	 * unacknowledged messages in the window.
122 	 */
123 	case TCPT_REXMT:
124 		tp->t_rxtshift++;
125 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
126 			tcp_drop(tp, ETIMEDOUT);
127 			return;
128 		}
129 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
130 		    ((int)(2 * tp->t_srtt)),
131 		    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 /* printf("rexmt set to %d\n", tp->t_timer[TCPT_REXMT]); */
136 		tp->snd_nxt = tp->snd_una;
137 		/* this only transmits one segment! */
138 		(void) tcp_output(tp);
139 		return;
140 
141 	/*
142 	 * Persistance timer into zero window.
143 	 * Force a byte to be output, if possible.
144 	 */
145 	case TCPT_PERSIST:
146 		tp->t_force = 1;
147 		(void) tcp_output(tp);
148 		tp->t_force = 0;
149 		TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
150 		    2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
151 		return;
152 
153 	/*
154 	 * Keep-alive timer went off; send something
155 	 * or drop connection if idle for too long.
156 	 */
157 	case TCPT_KEEP:
158 		if (tp->t_state < TCPS_ESTABLISHED ||
159 		    tp->t_idle >= TCPTV_MAXIDLE) {
160 			tcp_drop(tp, ETIMEDOUT);
161 			return;
162 		}
163 		if (tp->t_inpcb->inp_socket->so_options & SO_NOKEEPALIVE)
164 			tp->t_idle = 0;
165 		else
166 			tcp_respond(tp,
167 			    tp->t_template, tp->rcv_nxt, tp->snd_una-1, 0);
168 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
169 		return;
170 
171 #ifdef TCPTRUEOOB
172 	/*
173 	 * Out-of-band data retransmit timer.
174 	 */
175 	case TCPT_OOBREXMT:
176 		if (tp->t_flags & TF_NOOPT)
177 			return;
178 		(void) tcp_output(tp);
179 		TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT],
180 		    2 * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
181 		return;
182 #endif
183 	}
184 }
185