xref: /csrg-svn/sys/netinet/tcp_output.c (revision 17361)
1 /*	tcp_output.c	6.7	84/11/14	*/
2 
3 #include "param.h"
4 #include "systm.h"
5 #include "mbuf.h"
6 #include "protosw.h"
7 #include "socket.h"
8 #include "socketvar.h"
9 #include "errno.h"
10 
11 #include "../net/route.h"
12 
13 #include "in.h"
14 #include "in_pcb.h"
15 #include "in_systm.h"
16 #include "ip.h"
17 #include "ip_var.h"
18 #include "tcp.h"
19 #define	TCPOUTFLAGS
20 #include "tcp_fsm.h"
21 #include "tcp_seq.h"
22 #include "tcp_timer.h"
23 #include "tcp_var.h"
24 #include "tcpip.h"
25 #include "tcp_debug.h"
26 
27 /*
28  * Initial options.
29  */
30 u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
31 
32 /*
33  * Tcp output routine: figure out what should be sent and send it.
34  */
35 tcp_output(tp)
36 	register struct tcpcb *tp;
37 {
38 	register struct socket *so = tp->t_inpcb->inp_socket;
39 	register int len;
40 	struct mbuf *m0;
41 	int off, flags, win, error;
42 	register struct mbuf *m;
43 	register struct tcpiphdr *ti;
44 	u_char *opt;
45 	unsigned optlen = 0;
46 	int sendalot;
47 
48 	/*
49 	 * Determine length of data that should be transmitted,
50 	 * and flags that will be used.
51 	 * If there is some data or critical controls (SYN, RST)
52 	 * to send, then transmit; otherwise, investigate further.
53 	 */
54 again:
55 	sendalot = 0;
56 	off = tp->snd_nxt - tp->snd_una;
57 	win = MIN(tp->snd_wnd, tp->snd_cwnd) + tp->t_force;
58 	len = MIN(so->so_snd.sb_cc, win) - off;
59 	if (len < 0)
60 		return (0);	/* ??? */	/* past FIN */
61 	if (len > tp->t_maxseg) {
62 		len = tp->t_maxseg;
63 		/*
64 		 * Don't send more than one segment if retransmitting.
65 		 */
66 		if (tp->t_rxtshift == 0)
67 			sendalot = 1;
68 	}
69 
70 	flags = tcp_outflags[tp->t_state];
71 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
72 		flags &= ~TH_FIN;
73 	if (flags & (TH_SYN|TH_RST|TH_FIN))
74 		goto send;
75 	if (SEQ_GT(tp->snd_up, tp->snd_una))
76 		goto send;
77 
78 	/*
79 	 * Sender silly window avoidance.  If connection is idle
80 	 * and can send all data, a maximum segment,
81 	 * at least a maximum default-size segment do it,
82 	 * or are forced, do it; otherwise don't bother.
83 	 */
84 	if (len) {
85 		if (len == tp->t_maxseg || len >= so->so_snd.sb_cc) /* off = 0*/
86 			goto send;
87 		if (len >= TCP_MSS)	/* a lot */
88 			goto send;
89 		if (tp->t_force)
90 			goto send;
91 	}
92 
93 	/*
94 	 * Send if we owe peer an ACK.
95 	 */
96 	if (tp->t_flags&TF_ACKNOW)
97 		goto send;
98 
99 
100 	/*
101 	 * Calculate available window, and also amount
102 	 * of window known to peer (as advertised window less
103 	 * next expected input.)  If the difference is 35% or more of the
104 	 * maximum possible window, then want to send a window update to peer.
105 	 */
106 	win = sbspace(&so->so_rcv);
107 	if (win > 0 &&
108 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
109 		goto send;
110 
111 	/*
112 	 * TCP window updates are not reliable, rather a polling protocol
113 	 * using ``persist'' packets is used to insure receipt of window
114 	 * updates.  The three ``states'' for the output side are:
115 	 *	idle			not doing retransmits or persists
116 	 *	persisting		to move a zero window
117 	 *	(re)transmitting	and thereby not persisting
118 	 *
119 	 * tp->t_timer[TCPT_PERSIST]
120 	 *	is set when we are in persist state.
121 	 * tp->t_force
122 	 *	is set when we are called to send a persist packet.
123 	 * tp->t_timer[TCPT_REXMT]
124 	 *	is set when we are retransmitting
125 	 * The output side is idle when both timers are zero.
126 	 *
127 	 * If send window is closed, there is data to transmit, and no
128 	 * retransmit or persist is pending, then go to persist state,
129 	 * arranging to force out a byte to get more current window information
130 	 * if nothing happens soon.
131 	 */
132 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
133 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
134 		tp->t_rxtshift = 0;
135 		tcp_setpersist(tp);
136 	}
137 
138 	/*
139 	 * No reason to send a segment, just return.
140 	 */
141 	return (0);
142 
143 send:
144 	/*
145 	 * Grab a header mbuf, attaching a copy of data to
146 	 * be transmitted, and initialize the header from
147 	 * the template for sends on this connection.
148 	 */
149 	MGET(m, M_DONTWAIT, MT_HEADER);
150 	if (m == NULL)
151 		return (ENOBUFS);
152 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
153 	m->m_len = sizeof (struct tcpiphdr);
154 	if (len) {
155 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
156 		if (m->m_next == 0)
157 			len = 0;
158 	}
159 	ti = mtod(m, struct tcpiphdr *);
160 	if (tp->t_template == 0)
161 		panic("tcp_output");
162 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
163 
164 	/*
165 	 * Fill in fields, remembering maximum advertised
166 	 * window for use in delaying messages about window sizes.
167 	 */
168 	ti->ti_seq = tp->snd_nxt;
169 	ti->ti_ack = tp->rcv_nxt;
170 	ti->ti_seq = htonl(ti->ti_seq);
171 	ti->ti_ack = htonl(ti->ti_ack);
172 	/*
173 	 * Before ESTABLISHED, force sending of initial options
174 	 * unless TCP set to not do any options.
175 	 */
176 	if (tp->t_state < TCPS_ESTABLISHED) {
177 		int mss;
178 
179 		if (tp->t_flags&TF_NOOPT)
180 			goto noopt;
181 		mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
182 		if (mss <= IP_MSS - sizeof(struct tcpiphdr))
183 			goto noopt;
184 		opt = tcp_initopt;
185 		optlen = sizeof (tcp_initopt);
186 		*(u_short *)(opt + 2) = htons(mss);
187 	} else {
188 		if (tp->t_tcpopt == 0)
189 			goto noopt;
190 		opt = mtod(tp->t_tcpopt, u_char *);
191 		optlen = tp->t_tcpopt->m_len;
192 	}
193 	if (opt) {
194 		m0 = m->m_next;
195 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
196 		if (m->m_next == 0) {
197 			(void) m_free(m);
198 			m_freem(m0);
199 			return (ENOBUFS);
200 		}
201 		m->m_next->m_next = m0;
202 		m0 = m->m_next;
203 		m0->m_len = optlen;
204 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
205 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
206 		while (m0->m_len & 0x3) {
207 			*opt++ = TCPOPT_EOL;
208 			m0->m_len++;
209 		}
210 		optlen = m0->m_len;
211 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
212 	}
213 noopt:
214 	ti->ti_flags = flags;
215 	win = sbspace(&so->so_rcv);
216 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
217 		win = 0;
218 	if (win > 0)
219 		ti->ti_win = htons((u_short)win);
220 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
221 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
222 		ti->ti_urp = htons(ti->ti_urp);
223 		ti->ti_flags |= TH_URG;
224 	} else
225 		/*
226 		 * If no urgent pointer to send, then we pull
227 		 * the urgent pointer to the left edge of the send window
228 		 * so that it doesn't drift into the send window on sequence
229 		 * number wraparound.
230 		 */
231 		tp->snd_up = tp->snd_una;		/* drag it along */
232 	/*
233 	 * If anything to send and we can send it all, set PUSH.
234 	 * (This will keep happy those implementations which only
235 	 * give data to the user when a buffer fills or a PUSH comes in.)
236 	 */
237 	if (len && off+len == so->so_snd.sb_cc)
238 		ti->ti_flags |= TH_PUSH;
239 
240 	/*
241 	 * Put TCP length in extended header, and then
242 	 * checksum extended header and data.
243 	 */
244 	if (len + optlen) {
245 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
246 		ti->ti_len = htons((u_short)ti->ti_len);
247 	}
248 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
249 
250 	/*
251 	 * In transmit state, time the transmission and arrange for
252 	 * the retransmit.  In persist state, reset persist time for
253 	 * next persist.
254 	 */
255 	if (tp->t_force == 0) {
256 		/*
257 		 * Advance snd_nxt over sequence space of this segment.
258 		 */
259 		if (flags & (TH_SYN|TH_FIN))
260 			tp->snd_nxt++;
261 		tp->snd_nxt += len;
262 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
263 			tp->snd_max = tp->snd_nxt;
264 			/*
265 			 * Time this transmission if not a retransmission and
266 			 * not currently timing anything.
267 			 */
268 			if (tp->t_rtt == 0) {
269 				tp->t_rtt = 1;
270 				tp->t_rtseq = tp->snd_nxt - len;
271 			}
272 		}
273 
274 		/*
275 		 * Set retransmit timer if not currently set.
276 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
277 		 * Initialize shift counter which is used for exponential
278 		 * backoff of retransmit time.
279 		 */
280 		if (tp->t_timer[TCPT_REXMT] == 0 &&
281 		    tp->snd_nxt != tp->snd_una) {
282 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
283 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
284 			tp->t_rxtshift = 0;
285 		}
286 		tp->t_timer[TCPT_PERSIST] = 0;
287 	} else {
288 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
289 			tp->snd_max = tp->snd_una+1;
290 	}
291 
292 	/*
293 	 * Trace.
294 	 */
295 	if (so->so_options & SO_DEBUG)
296 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
297 
298 	/*
299 	 * Fill in IP length and desired time to live and
300 	 * send to IP level.
301 	 */
302 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
303 	((struct ip *)ti)->ip_ttl = TCP_TTL;
304 	if (so->so_options & SO_DONTROUTE)
305 		error =
306 		   ip_output(m, tp->t_ipopt, (struct route *)0, IP_ROUTETOIF);
307 	else
308 		error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0);
309 	if (error)
310 		return (error);
311 
312 	/*
313 	 * Data sent (as far as we can tell).
314 	 * If this advertises a larger window than any other segment,
315 	 * then remember the size of the advertised window.
316 	 * Drop send for purpose of ACK requirements.
317 	 */
318 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
319 		tp->rcv_adv = tp->rcv_nxt + win;
320 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
321 	if (sendalot && tp->t_force == 0)
322 		goto again;
323 	return (0);
324 }
325 
326 tcp_setpersist(tp)
327 	register struct tcpcb *tp;
328 {
329 
330 	if (tp->t_timer[TCPT_REXMT])
331 		panic("tcp_output REXMT");
332 	/*
333 	 * Start/restart persistance timer.
334 	 */
335 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
336 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
337 	    TCPTV_PERSMIN, TCPTV_MAX);
338 	tp->t_rxtshift++;
339 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
340 		tp->t_rxtshift = 0;
341 }
342