xref: /csrg-svn/sys/netinet/tcp_output.c (revision 33620)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  *	@(#)tcp_output.c	7.14 (Berkeley) 02/29/88
13  */
14 
15 #include "param.h"
16 #include "systm.h"
17 #include "mbuf.h"
18 #include "protosw.h"
19 #include "socket.h"
20 #include "socketvar.h"
21 #include "errno.h"
22 
23 #include "../net/route.h"
24 
25 #include "in.h"
26 #include "in_pcb.h"
27 #include "in_systm.h"
28 #include "ip.h"
29 #include "ip_var.h"
30 #include "tcp.h"
31 #define	TCPOUTFLAGS
32 #include "tcp_fsm.h"
33 #include "tcp_seq.h"
34 #include "tcp_timer.h"
35 #include "tcp_var.h"
36 #include "tcpip.h"
37 #include "tcp_debug.h"
38 
39 /*
40  * Initial options.
41  */
42 u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
43 
44 /*
45  * Tcp output routine: figure out what should be sent and send it.
46  */
47 tcp_output(tp)
48 	register struct tcpcb *tp;
49 {
50 	register struct socket *so = tp->t_inpcb->inp_socket;
51 	register long len, win;
52 	struct mbuf *m0;
53 	int off, flags, error;
54 	register struct mbuf *m;
55 	register struct tcpiphdr *ti;
56 	u_char *opt;
57 	unsigned optlen = 0;
58 	int idle, sendalot;
59 
60 	/*
61 	 * Determine length of data that should be transmitted,
62 	 * and flags that will be used.
63 	 * If there is some data or critical controls (SYN, RST)
64 	 * to send, then transmit; otherwise, investigate further.
65 	 */
66 	idle = (tp->snd_max == tp->snd_una);
67 again:
68 	sendalot = 0;
69 	off = tp->snd_nxt - tp->snd_una;
70 	win = MIN(tp->snd_wnd, tp->snd_cwnd);
71 
72 	/*
73 	 * If in persist timeout with window of 0, send 1 byte.
74 	 * Otherwise, if window is small but nonzero
75 	 * and timer expired, we will send what we can
76 	 * and go to transmit state.
77 	 */
78 	if (tp->t_force) {
79 		if (win == 0)
80 			win = 1;
81 		else {
82 			tp->t_timer[TCPT_PERSIST] = 0;
83 			tp->t_rxtshift = 0;
84 		}
85 	}
86 
87 	len = MIN(so->so_snd.sb_cc, win) - off;
88 	flags = tcp_outflags[tp->t_state];
89 
90 	if (len < 0) {
91 		/*
92 		 * If FIN has been sent but not acked,
93 		 * but we haven't been called to retransmit,
94 		 * len will be -1.  Otherwise, window shrank
95 		 * after we sent into it.  If window shrank to 0,
96 		 * cancel pending retransmit and pull snd_nxt
97 		 * back to (closed) window.  We will enter persist
98 		 * state below.  If the window didn't close completely,
99 		 * just wait for an ACK.
100 		 */
101 		len = 0;
102 		if (win == 0) {
103 			tp->t_timer[TCPT_REXMT] = 0;
104 			tp->snd_nxt = tp->snd_una;
105 		}
106 	}
107 	if (len > tp->t_maxseg) {
108 		len = tp->t_maxseg;
109 		sendalot = 1;
110 	}
111 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
112 		flags &= ~TH_FIN;
113 	win = sbspace(&so->so_rcv);
114 
115 
116 	/*
117 	 * If our state indicates that FIN should be sent
118 	 * and we have not yet done so, or we're retransmitting the FIN,
119 	 * then we need to send.
120 	 */
121 	if (flags & TH_FIN &&
122 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
123 		goto send;
124 	/*
125 	 * Send if we owe peer an ACK.
126 	 */
127 	if (tp->t_flags & TF_ACKNOW)
128 		goto send;
129 	if (flags & (TH_SYN|TH_RST))
130 		goto send;
131 	if (SEQ_GT(tp->snd_up, tp->snd_una))
132 		goto send;
133 
134 	/*
135 	 * Sender silly window avoidance.  If connection is idle
136 	 * and can send all data, a maximum segment,
137 	 * at least a maximum default-size segment do it,
138 	 * or are forced, do it; otherwise don't bother.
139 	 * If peer's buffer is tiny, then send
140 	 * when window is at least half open.
141 	 * If retransmitting (possibly after persist timer forced us
142 	 * to send into a small window), then must resend.
143 	 */
144 	if (len) {
145 		if (len == tp->t_maxseg)
146 			goto send;
147 		if ((idle || tp->t_flags & TF_NODELAY) &&
148 		    len + off >= so->so_snd.sb_cc)
149 			goto send;
150 		if (tp->t_force)
151 			goto send;
152 		if (len >= tp->max_sndwnd / 2)
153 			goto send;
154 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
155 			goto send;
156 	}
157 
158 	/*
159 	 * Compare available window to amount of window
160 	 * known to peer (as advertised window less
161 	 * next expected input).  If the difference is at least two
162 	 * max size segments or at least 35% of the maximum possible
163 	 * window, then want to send a window update to peer.
164 	 */
165 	if (win > 0) {
166 		int adv = win - (tp->rcv_adv - tp->rcv_nxt);
167 
168 		if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg)
169 			goto send;
170 		if (100 * adv / so->so_rcv.sb_hiwat >= 35)
171 			goto send;
172 	}
173 
174 	/*
175 	 * TCP window updates are not reliable, rather a polling protocol
176 	 * using ``persist'' packets is used to insure receipt of window
177 	 * updates.  The three ``states'' for the output side are:
178 	 *	idle			not doing retransmits or persists
179 	 *	persisting		to move a small or zero window
180 	 *	(re)transmitting	and thereby not persisting
181 	 *
182 	 * tp->t_timer[TCPT_PERSIST]
183 	 *	is set when we are in persist state.
184 	 * tp->t_force
185 	 *	is set when we are called to send a persist packet.
186 	 * tp->t_timer[TCPT_REXMT]
187 	 *	is set when we are retransmitting
188 	 * The output side is idle when both timers are zero.
189 	 *
190 	 * If send window is too small, there is data to transmit, and no
191 	 * retransmit or persist is pending, then go to persist state.
192 	 * If nothing happens soon, send when timer expires:
193 	 * if window is nonzero, transmit what we can,
194 	 * otherwise force out a byte.
195 	 */
196 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
197 	    tp->t_timer[TCPT_PERSIST] == 0) {
198 		tp->t_rxtshift = 0;
199 		tcp_setpersist(tp);
200 	}
201 
202 	/*
203 	 * No reason to send a segment, just return.
204 	 */
205 	return (0);
206 
207 send:
208 	/*
209 	 * Grab a header mbuf, attaching a copy of data to
210 	 * be transmitted, and initialize the header from
211 	 * the template for sends on this connection.
212 	 */
213 	MGET(m, M_DONTWAIT, MT_HEADER);
214 	if (m == NULL)
215 		return (ENOBUFS);
216 #define	MAXLINKHDR	32		/* belongs elsewhere */
217 #define	DATASPACE  (MMAXOFF - (MMINOFF + MAXLINKHDR + sizeof (struct tcpiphdr)))
218 	m->m_off = MMINOFF + MAXLINKHDR;
219 	m->m_len = sizeof (struct tcpiphdr);
220 	ti = mtod(m, struct tcpiphdr *);
221 	if (len) {
222 		if (tp->t_force && len == 1)
223 			tcpstat.tcps_sndprobe++;
224 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
225 			tcpstat.tcps_sndrexmitpack++;
226 			tcpstat.tcps_sndrexmitbyte += len;
227 		} else {
228 			tcpstat.tcps_sndpack++;
229 			tcpstat.tcps_sndbyte += len;
230 		}
231 		if (len <= DATASPACE) {
232 			m_copydata(so->so_snd.sb_mb, off, len,
233 			    mtod(m, caddr_t) + sizeof(struct tcpiphdr));
234 			m->m_len += len;
235 		} else {
236 			m->m_next = m_copy(so->so_snd.sb_mb, off, len);
237 			if (m->m_next == 0)
238 				len = 0;
239 		}
240 	} else if (tp->t_flags & TF_ACKNOW)
241 		tcpstat.tcps_sndacks++;
242 	else if (flags & (TH_SYN|TH_FIN|TH_RST))
243 		tcpstat.tcps_sndctrl++;
244 	else if (SEQ_GT(tp->snd_up, tp->snd_una))
245 		tcpstat.tcps_sndurg++;
246 	else
247 		tcpstat.tcps_sndwinup++;
248 
249 	if (tp->t_template == 0)
250 		panic("tcp_output");
251 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
252 
253 	/*
254 	 * Fill in fields, remembering maximum advertised
255 	 * window for use in delaying messages about window sizes.
256 	 * If resending a FIN, be sure not to use a new sequence number.
257 	 */
258 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
259 	    tp->snd_nxt == tp->snd_max)
260 		tp->snd_nxt--;
261 	ti->ti_seq = htonl(tp->snd_nxt);
262 	ti->ti_ack = htonl(tp->rcv_nxt);
263 	/*
264 	 * Before ESTABLISHED, force sending of initial options
265 	 * unless TCP set to not do any options.
266 	 */
267 	opt = NULL;
268 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
269 		u_short mss;
270 
271 		mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
272 		if (mss > IP_MSS - sizeof(struct tcpiphdr)) {
273 			opt = tcp_initopt;
274 			optlen = sizeof (tcp_initopt);
275 			*(u_short *)(opt + 2) = htons(mss);
276 		}
277 	}
278 	if (opt) {
279 		m0 = m->m_next;
280 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
281 		if (m->m_next == 0) {
282 			(void) m_free(m);
283 			m_freem(m0);
284 			return (ENOBUFS);
285 		}
286 		m->m_next->m_next = m0;
287 		m0 = m->m_next;
288 		m0->m_len = optlen;
289 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
290 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
291 		while (m0->m_len & 0x3) {
292 			*opt++ = TCPOPT_EOL;
293 			m0->m_len++;
294 		}
295 		optlen = m0->m_len;
296 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
297 	}
298 	ti->ti_flags = flags;
299 	/*
300 	 * Calculate receive window.  Don't shrink window,
301 	 * but avoid silly window syndrome.
302 	 */
303 	if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg)
304 		win = 0;
305 	if (win > IP_MAXPACKET)
306 		win = IP_MAXPACKET;
307 	if (win < (int)(tp->rcv_adv - tp->rcv_nxt))
308 		win = (int)(tp->rcv_adv - tp->rcv_nxt);
309 	ti->ti_win = htons((u_short)win);
310 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
311 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
312 		ti->ti_flags |= TH_URG;
313 	} else
314 		/*
315 		 * If no urgent pointer to send, then we pull
316 		 * the urgent pointer to the left edge of the send window
317 		 * so that it doesn't drift into the send window on sequence
318 		 * number wraparound.
319 		 */
320 		tp->snd_up = tp->snd_una;		/* drag it along */
321 	/*
322 	 * If anything to send and we can send it all, set PUSH.
323 	 * (This will keep happy those implementations which only
324 	 * give data to the user when a buffer fills or a PUSH comes in.)
325 	 */
326 	if (len && off+len == so->so_snd.sb_cc)
327 		ti->ti_flags |= TH_PUSH;
328 
329 	/*
330 	 * Put TCP length in extended header, and then
331 	 * checksum extended header and data.
332 	 */
333 	if (len + optlen)
334 		ti->ti_len = htons((u_short)(sizeof(struct tcphdr) +
335 		    optlen + len));
336 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
337 
338 	/*
339 	 * In transmit state, time the transmission and arrange for
340 	 * the retransmit.  In persist state, just set snd_max.
341 	 */
342 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
343 		tcp_seq startseq = tp->snd_nxt;
344 
345 		/*
346 		 * Advance snd_nxt over sequence space of this segment.
347 		 */
348 		if (flags & TH_SYN)
349 			tp->snd_nxt++;
350 		if (flags & TH_FIN) {
351 			tp->snd_nxt++;
352 			tp->t_flags |= TF_SENTFIN;
353 		}
354 		tp->snd_nxt += len;
355 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
356 			tp->snd_max = tp->snd_nxt;
357 			/*
358 			 * Time this transmission if not a retransmission and
359 			 * not currently timing anything.
360 			 */
361 			if (tp->t_rtt == 0) {
362 				tp->t_rtt = 1;
363 				tp->t_rtseq = startseq;
364 				tcpstat.tcps_segstimed++;
365 			}
366 		}
367 
368 		/*
369 		 * Set retransmit timer if not currently set,
370 		 * and not doing an ack or a keep-alive probe.
371 		 * Initial value for retransmit timer is smoothed
372 		 * round-trip time + 2 * round-trip time variance.
373 		 * Initialize shift counter which is used for backoff
374 		 * of retransmit time.
375 		 */
376 		if (tp->t_timer[TCPT_REXMT] == 0 &&
377 		    tp->snd_nxt != tp->snd_una) {
378 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
379 			if (tp->t_timer[TCPT_PERSIST]) {
380 				tp->t_timer[TCPT_PERSIST] = 0;
381 				tp->t_rxtshift = 0;
382 			}
383 		}
384 	} else
385 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
386 			tp->snd_max = tp->snd_nxt + len;
387 
388 	/*
389 	 * Trace.
390 	 */
391 	if (so->so_options & SO_DEBUG)
392 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
393 
394 	/*
395 	 * Fill in IP length and desired time to live and
396 	 * send to IP level.
397 	 */
398 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
399 	((struct ip *)ti)->ip_ttl = TCP_TTL;
400 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
401 	    so->so_options & SO_DONTROUTE);
402 	if (error) {
403 		if (error == ENOBUFS) {
404 			tcp_quench(tp->t_inpcb);
405 			return (0);
406 		}
407 		return (error);
408 	}
409 	tcpstat.tcps_sndtotal++;
410 
411 	/*
412 	 * Data sent (as far as we can tell).
413 	 * If this advertises a larger window than any other segment,
414 	 * then remember the size of the advertised window.
415 	 * Any pending ACK has now been sent.
416 	 */
417 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
418 		tp->rcv_adv = tp->rcv_nxt + win;
419 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
420 	if (sendalot)
421 		goto again;
422 	return (0);
423 }
424 
425 tcp_setpersist(tp)
426 	register struct tcpcb *tp;
427 {
428 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
429 
430 	if (tp->t_timer[TCPT_REXMT])
431 		panic("tcp_output REXMT");
432 	/*
433 	 * Start/restart persistance timer.
434 	 */
435 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
436 	    t * tcp_backoff[tp->t_rxtshift],
437 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
438 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
439 		tp->t_rxtshift++;
440 }
441