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