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