xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 5276)
1 /* tcp_timer.c 4.9 81/12/20 */
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 /*
24  * Fast timeout routine for processing delayed acks
25  */
26 tcp_fasttimo()
27 {
28 
29 COUNT(TCP_FASTTIMO);
30 }
31 
32 /*
33  * Tcp protocol timeout routine called every 500 ms.
34  * Updates the timers in all active tcb's and
35  * causes finite state machine actions if timers expire.
36  */
37 tcp_slowtimo()
38 {
39 	register struct inpcb *ip;
40 	register struct tcpcb *tp;
41 	int s = splnet();
42 	register int i;
43 COUNT(TCP_SLOWTIMO);
44 
45 	/*
46 	 * Search through tcb's and update active timers.
47 	 */
48 	ip = tcb.inp_next;
49 	if (ip == 0) {
50 		splx(s);
51 		return;
52 	}
53 	for (; ip != &tcb; ip = ip->inp_next) {
54 		tp = intotcpcb(ip);
55 		if (tp == 0)
56 			continue;
57 		for (i = 0; i < TCPT_NTIMERS; i++) {
58 			if (tp->t_timer[i] && --tp->t_timer[i] == 0)
59 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
60 				    PRU_SLOWTIMO, (struct mbuf *)0,
61 				    (caddr_t)i);
62 		}
63 		tp->t_idle++;
64 		if (tp->t_rtt)
65 			tp->t_rtt++;
66 	}
67 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
68 	splx(s);
69 }
70 
71 /*
72  * Cancel all timers for TCP tp.
73  */
74 tcp_canceltimers(tp)
75 	struct tcpcb *tp;
76 {
77 	register int i;
78 
79 COUNT(TCP_CANCELTIMERS);
80 	for (i = 0; i < TCPT_NTIMERS; i++)
81 		tp->t_timer[i] = 0;
82 }
83 
84 /*
85  * TCP timer processing.
86  */
87 tcp_timers(tp, timer)
88 	register struct tcpcb *tp;
89 	int timer;
90 {
91 
92 COUNT(TCP_TIMERS);
93 	switch (timer) {
94 
95 	/*
96 	 * 2 MSL timeout in shutdown went off.  Delete connection
97 	 * control block.
98 	 */
99 	case TCPT_2MSL:
100 		tcp_close(tp);
101 		return;
102 
103 	/*
104 	 * Retransmission timer went off.  Message has not
105 	 * been acked within retransmit interval.  Back off
106 	 * to a longer retransmit interval and retransmit all
107 	 * unacknowledged messages in the window.
108 	 */
109 	case TCPT_REXMT:
110 		tp->t_rxtshift++;
111 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
112 		    ((int)(2 * tp->t_srtt)) << tp->t_rxtshift,
113 		    TCPTV_MIN, TCPTV_MAX);
114 printf("rexmt set to %d\n", tp->t_timer[TCPT_REXMT]);
115 		tp->snd_nxt = tp->snd_una;
116 		/* this only transmits one segment! */
117 		(void) tcp_output(tp);
118 		return;
119 
120 	/*
121 	 * Persistance timer into zero window.
122 	 * Force a byte to be output, if possible.
123 	 */
124 	case TCPT_PERSIST:
125 		tp->t_force = 1;
126 		(void) tcp_output(tp);
127 		tp->t_force = 0;
128 		TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
129 		    2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
130 		return;
131 
132 	/*
133 	 * Keep-alive timer went off; send something
134 	 * or drop connection if idle for too long.
135 	 */
136 	case TCPT_KEEP:
137 		if (tp->t_state < TCPS_ESTABLISHED ||
138 		    tp->t_idle >= TCPTV_MAXIDLE) {
139 			tcp_drop(tp, ETIMEDOUT);
140 			return;
141 		}
142 		tcp_respond(tp->t_template, tp->rcv_nxt, tp->snd_una-1, 0);
143 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
144 		return;
145 	}
146 }
147