Merge qemu-0.9.0
[qemu-kvm/fedora.git] / slirp / tcp_output.c
blobb79bcf1279aadb86ab65322048cb64a82bb36135
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
34 * tcp_output.c,v 1.3 1994/09/15 10:36:55 davidg Exp
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
45 #include <slirp.h>
48 * Since this is only used in "stats socket", we give meaning
49 * names instead of the REAL names
51 char *tcpstates[] = {
52 /* "CLOSED", "LISTEN", "SYN_SENT", "SYN_RCVD", */
53 "REDIRECT", "LISTEN", "SYN_SENT", "SYN_RCVD",
54 "ESTABLISHED", "CLOSE_WAIT", "FIN_WAIT_1", "CLOSING",
55 "LAST_ACK", "FIN_WAIT_2", "TIME_WAIT",
58 u_char tcp_outflags[TCP_NSTATES] = {
59 TH_RST|TH_ACK, 0, TH_SYN, TH_SYN|TH_ACK,
60 TH_ACK, TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
61 TH_FIN|TH_ACK, TH_ACK, TH_ACK,
65 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
68 * Tcp output routine: figure out what should be sent and send it.
70 int
71 tcp_output(tp)
72 register struct tcpcb *tp;
74 register struct socket *so = tp->t_socket;
75 register long len, win;
76 int off, flags, error;
77 register struct mbuf *m;
78 register struct tcpiphdr *ti;
79 u_char opt[MAX_TCPOPTLEN];
80 unsigned optlen, hdrlen;
81 int idle, sendalot;
83 DEBUG_CALL("tcp_output");
84 DEBUG_ARG("tp = %lx", (long )tp);
87 * Determine length of data that should be transmitted,
88 * and flags that will be used.
89 * If there is some data or critical controls (SYN, RST)
90 * to send, then transmit; otherwise, investigate further.
92 idle = (tp->snd_max == tp->snd_una);
93 if (idle && tp->t_idle >= tp->t_rxtcur)
95 * We have been idle for "a while" and no acks are
96 * expected to clock out any data we send --
97 * slow start to get ack "clock" running again.
99 tp->snd_cwnd = tp->t_maxseg;
100 again:
101 sendalot = 0;
102 off = tp->snd_nxt - tp->snd_una;
103 win = min(tp->snd_wnd, tp->snd_cwnd);
105 flags = tcp_outflags[tp->t_state];
107 DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
110 * If in persist timeout with window of 0, send 1 byte.
111 * Otherwise, if window is small but nonzero
112 * and timer expired, we will send what we can
113 * and go to transmit state.
115 if (tp->t_force) {
116 if (win == 0) {
118 * If we still have some data to send, then
119 * clear the FIN bit. Usually this would
120 * happen below when it realizes that we
121 * aren't sending all the data. However,
122 * if we have exactly 1 byte of unset data,
123 * then it won't clear the FIN bit below,
124 * and if we are in persist state, we wind
125 * up sending the packet without recording
126 * that we sent the FIN bit.
128 * We can't just blindly clear the FIN bit,
129 * because if we don't have any more data
130 * to send then the probe will be the FIN
131 * itself.
133 if (off < so->so_snd.sb_cc)
134 flags &= ~TH_FIN;
135 win = 1;
136 } else {
137 tp->t_timer[TCPT_PERSIST] = 0;
138 tp->t_rxtshift = 0;
142 len = min(so->so_snd.sb_cc, win) - off;
144 if (len < 0) {
146 * If FIN has been sent but not acked,
147 * but we haven't been called to retransmit,
148 * len will be -1. Otherwise, window shrank
149 * after we sent into it. If window shrank to 0,
150 * cancel pending retransmit and pull snd_nxt
151 * back to (closed) window. We will enter persist
152 * state below. If the window didn't close completely,
153 * just wait for an ACK.
155 len = 0;
156 if (win == 0) {
157 tp->t_timer[TCPT_REXMT] = 0;
158 tp->snd_nxt = tp->snd_una;
162 if (len > tp->t_maxseg) {
163 len = tp->t_maxseg;
164 sendalot = 1;
166 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
167 flags &= ~TH_FIN;
169 win = sbspace(&so->so_rcv);
172 * Sender silly window avoidance. If connection is idle
173 * and can send all data, a maximum segment,
174 * at least a maximum default-size segment do it,
175 * or are forced, do it; otherwise don't bother.
176 * If peer's buffer is tiny, then send
177 * when window is at least half open.
178 * If retransmitting (possibly after persist timer forced us
179 * to send into a small window), then must resend.
181 if (len) {
182 if (len == tp->t_maxseg)
183 goto send;
184 if ((1 || idle || tp->t_flags & TF_NODELAY) &&
185 len + off >= so->so_snd.sb_cc)
186 goto send;
187 if (tp->t_force)
188 goto send;
189 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
190 goto send;
191 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
192 goto send;
196 * Compare available window to amount of window
197 * known to peer (as advertised window less
198 * next expected input). If the difference is at least two
199 * max size segments, or at least 50% of the maximum possible
200 * window, then want to send a window update to peer.
202 if (win > 0) {
204 * "adv" is the amount we can increase the window,
205 * taking into account that we are limited by
206 * TCP_MAXWIN << tp->rcv_scale.
208 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
209 (tp->rcv_adv - tp->rcv_nxt);
211 if (adv >= (long) (2 * tp->t_maxseg))
212 goto send;
213 if (2 * adv >= (long) so->so_rcv.sb_datalen)
214 goto send;
218 * Send if we owe peer an ACK.
220 if (tp->t_flags & TF_ACKNOW)
221 goto send;
222 if (flags & (TH_SYN|TH_RST))
223 goto send;
224 if (SEQ_GT(tp->snd_up, tp->snd_una))
225 goto send;
227 * If our state indicates that FIN should be sent
228 * and we have not yet done so, or we're retransmitting the FIN,
229 * then we need to send.
231 if (flags & TH_FIN &&
232 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
233 goto send;
236 * TCP window updates are not reliable, rather a polling protocol
237 * using ``persist'' packets is used to insure receipt of window
238 * updates. The three ``states'' for the output side are:
239 * idle not doing retransmits or persists
240 * persisting to move a small or zero window
241 * (re)transmitting and thereby not persisting
243 * tp->t_timer[TCPT_PERSIST]
244 * is set when we are in persist state.
245 * tp->t_force
246 * is set when we are called to send a persist packet.
247 * tp->t_timer[TCPT_REXMT]
248 * is set when we are retransmitting
249 * The output side is idle when both timers are zero.
251 * If send window is too small, there is data to transmit, and no
252 * retransmit or persist is pending, then go to persist state.
253 * If nothing happens soon, send when timer expires:
254 * if window is nonzero, transmit what we can,
255 * otherwise force out a byte.
257 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
258 tp->t_timer[TCPT_PERSIST] == 0) {
259 tp->t_rxtshift = 0;
260 tcp_setpersist(tp);
264 * No reason to send a segment, just return.
266 tcpstat.tcps_didnuttin++;
268 return (0);
270 send:
272 * Before ESTABLISHED, force sending of initial options
273 * unless TCP set not to do any options.
274 * NOTE: we assume that the IP/TCP header plus TCP options
275 * always fit in a single mbuf, leaving room for a maximum
276 * link header, i.e.
277 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
279 optlen = 0;
280 hdrlen = sizeof (struct tcpiphdr);
281 if (flags & TH_SYN) {
282 tp->snd_nxt = tp->iss;
283 if ((tp->t_flags & TF_NOOPT) == 0) {
284 u_int16_t mss;
286 opt[0] = TCPOPT_MAXSEG;
287 opt[1] = 4;
288 mss = htons((u_int16_t) tcp_mss(tp, 0));
289 memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
290 optlen = 4;
292 /* if ((tp->t_flags & TF_REQ_SCALE) &&
293 * ((flags & TH_ACK) == 0 ||
294 * (tp->t_flags & TF_RCVD_SCALE))) {
295 * *((u_int32_t *) (opt + optlen)) = htonl(
296 * TCPOPT_NOP << 24 |
297 * TCPOPT_WINDOW << 16 |
298 * TCPOLEN_WINDOW << 8 |
299 * tp->request_r_scale);
300 * optlen += 4;
307 * Send a timestamp and echo-reply if this is a SYN and our side
308 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
309 * and our peer have sent timestamps in our SYN's.
311 /* if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
312 * (flags & TH_RST) == 0 &&
313 * ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
314 * (tp->t_flags & TF_RCVD_TSTMP))) {
315 * u_int32_t *lp = (u_int32_t *)(opt + optlen);
317 * / * Form timestamp option as shown in appendix A of RFC 1323. * /
318 * *lp++ = htonl(TCPOPT_TSTAMP_HDR);
319 * *lp++ = htonl(tcp_now);
320 * *lp = htonl(tp->ts_recent);
321 * optlen += TCPOLEN_TSTAMP_APPA;
324 hdrlen += optlen;
327 * Adjust data length if insertion of options will
328 * bump the packet length beyond the t_maxseg length.
330 if (len > tp->t_maxseg - optlen) {
331 len = tp->t_maxseg - optlen;
332 sendalot = 1;
336 * Grab a header mbuf, attaching a copy of data to
337 * be transmitted, and initialize the header from
338 * the template for sends on this connection.
340 if (len) {
341 if (tp->t_force && len == 1)
342 tcpstat.tcps_sndprobe++;
343 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
344 tcpstat.tcps_sndrexmitpack++;
345 tcpstat.tcps_sndrexmitbyte += len;
346 } else {
347 tcpstat.tcps_sndpack++;
348 tcpstat.tcps_sndbyte += len;
351 m = m_get();
352 if (m == NULL) {
353 /* error = ENOBUFS; */
354 error = 1;
355 goto out;
357 m->m_data += if_maxlinkhdr;
358 m->m_len = hdrlen;
361 * This will always succeed, since we make sure our mbufs
362 * are big enough to hold one MSS packet + header + ... etc.
364 /* if (len <= MHLEN - hdrlen - max_linkhdr) { */
366 sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
367 m->m_len += len;
369 /* } else {
370 * m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
371 * if (m->m_next == 0)
372 * len = 0;
376 * If we're sending everything we've got, set PUSH.
377 * (This will keep happy those implementations which only
378 * give data to the user when a buffer fills or
379 * a PUSH comes in.)
381 if (off + len == so->so_snd.sb_cc)
382 flags |= TH_PUSH;
383 } else {
384 if (tp->t_flags & TF_ACKNOW)
385 tcpstat.tcps_sndacks++;
386 else if (flags & (TH_SYN|TH_FIN|TH_RST))
387 tcpstat.tcps_sndctrl++;
388 else if (SEQ_GT(tp->snd_up, tp->snd_una))
389 tcpstat.tcps_sndurg++;
390 else
391 tcpstat.tcps_sndwinup++;
393 m = m_get();
394 if (m == NULL) {
395 /* error = ENOBUFS; */
396 error = 1;
397 goto out;
399 m->m_data += if_maxlinkhdr;
400 m->m_len = hdrlen;
403 ti = mtod(m, struct tcpiphdr *);
405 memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
408 * Fill in fields, remembering maximum advertised
409 * window for use in delaying messages about window sizes.
410 * If resending a FIN, be sure not to use a new sequence number.
412 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
413 tp->snd_nxt == tp->snd_max)
414 tp->snd_nxt--;
416 * If we are doing retransmissions, then snd_nxt will
417 * not reflect the first unsent octet. For ACK only
418 * packets, we do not want the sequence number of the
419 * retransmitted packet, we want the sequence number
420 * of the next unsent octet. So, if there is no data
421 * (and no SYN or FIN), use snd_max instead of snd_nxt
422 * when filling in ti_seq. But if we are in persist
423 * state, snd_max might reflect one byte beyond the
424 * right edge of the window, so use snd_nxt in that
425 * case, since we know we aren't doing a retransmission.
426 * (retransmit and persist are mutually exclusive...)
428 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
429 ti->ti_seq = htonl(tp->snd_nxt);
430 else
431 ti->ti_seq = htonl(tp->snd_max);
432 ti->ti_ack = htonl(tp->rcv_nxt);
433 if (optlen) {
434 memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
435 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
437 ti->ti_flags = flags;
439 * Calculate receive window. Don't shrink window,
440 * but avoid silly window syndrome.
442 if (win < (long)(so->so_rcv.sb_datalen / 4) && win < (long)tp->t_maxseg)
443 win = 0;
444 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
445 win = (long)TCP_MAXWIN << tp->rcv_scale;
446 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
447 win = (long)(tp->rcv_adv - tp->rcv_nxt);
448 ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
450 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
451 ti->ti_urp = htons((u_int16_t)(tp->snd_up - ntohl(ti->ti_seq)));
452 #ifdef notdef
453 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
454 ti->ti_urp = htons((u_int16_t)(tp->snd_up - tp->snd_nxt));
455 #endif
456 ti->ti_flags |= TH_URG;
457 } else
459 * If no urgent pointer to send, then we pull
460 * the urgent pointer to the left edge of the send window
461 * so that it doesn't drift into the send window on sequence
462 * number wraparound.
464 tp->snd_up = tp->snd_una; /* drag it along */
467 * Put TCP length in extended header, and then
468 * checksum extended header and data.
470 if (len + optlen)
471 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
472 optlen + len));
473 ti->ti_sum = cksum(m, (int)(hdrlen + len));
476 * In transmit state, time the transmission and arrange for
477 * the retransmit. In persist state, just set snd_max.
479 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
480 tcp_seq startseq = tp->snd_nxt;
483 * Advance snd_nxt over sequence space of this segment.
485 if (flags & (TH_SYN|TH_FIN)) {
486 if (flags & TH_SYN)
487 tp->snd_nxt++;
488 if (flags & TH_FIN) {
489 tp->snd_nxt++;
490 tp->t_flags |= TF_SENTFIN;
493 tp->snd_nxt += len;
494 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
495 tp->snd_max = tp->snd_nxt;
497 * Time this transmission if not a retransmission and
498 * not currently timing anything.
500 if (tp->t_rtt == 0) {
501 tp->t_rtt = 1;
502 tp->t_rtseq = startseq;
503 tcpstat.tcps_segstimed++;
508 * Set retransmit timer if not currently set,
509 * and not doing an ack or a keep-alive probe.
510 * Initial value for retransmit timer is smoothed
511 * round-trip time + 2 * round-trip time variance.
512 * Initialize shift counter which is used for backoff
513 * of retransmit time.
515 if (tp->t_timer[TCPT_REXMT] == 0 &&
516 tp->snd_nxt != tp->snd_una) {
517 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
518 if (tp->t_timer[TCPT_PERSIST]) {
519 tp->t_timer[TCPT_PERSIST] = 0;
520 tp->t_rxtshift = 0;
523 } else
524 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
525 tp->snd_max = tp->snd_nxt + len;
528 * Fill in IP length and desired time to live and
529 * send to IP level. There should be a better way
530 * to handle ttl and tos; we could keep them in
531 * the template, but need a way to checksum without them.
533 m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
537 ((struct ip *)ti)->ip_len = m->m_len;
539 ((struct ip *)ti)->ip_ttl = ip_defttl;
540 ((struct ip *)ti)->ip_tos = so->so_iptos;
542 /* #if BSD >= 43 */
543 /* Don't do IP options... */
544 /* error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
545 * so->so_options & SO_DONTROUTE, 0);
547 error = ip_output(so, m);
549 /* #else
550 * error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
551 * so->so_options & SO_DONTROUTE);
552 * #endif
555 if (error) {
556 out:
557 /* if (error == ENOBUFS) {
558 * tcp_quench(tp->t_inpcb, 0);
559 * return (0);
562 /* if ((error == EHOSTUNREACH || error == ENETDOWN)
563 * && TCPS_HAVERCVDSYN(tp->t_state)) {
564 * tp->t_softerror = error;
565 * return (0);
568 return (error);
570 tcpstat.tcps_sndtotal++;
573 * Data sent (as far as we can tell).
574 * If this advertises a larger window than any other segment,
575 * then remember the size of the advertised window.
576 * Any pending ACK has now been sent.
578 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
579 tp->rcv_adv = tp->rcv_nxt + win;
580 tp->last_ack_sent = tp->rcv_nxt;
581 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
582 if (sendalot)
583 goto again;
585 return (0);
588 void
589 tcp_setpersist(tp)
590 register struct tcpcb *tp;
592 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
594 /* if (tp->t_timer[TCPT_REXMT])
595 * panic("tcp_output REXMT");
598 * Start/restart persistence timer.
600 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
601 t * tcp_backoff[tp->t_rxtshift],
602 TCPTV_PERSMIN, TCPTV_PERSMAX);
603 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
604 tp->t_rxtshift++;