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