VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_output.c@ 14470

最後變更 在這個檔案從14470是 14470,由 vboxsync 提交於 16 年 前

slirp: remove tabs

  • 屬性 svn:eol-style 設為 native
檔案大小: 22.9 KB
 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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.
20 *
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.
32 *
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
35 */
36
37/*
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
40 *
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
43 */
44
45#include <slirp.h>
46
47/*
48 * Since this is only used in "stats socket", we give meaning
49 * names instead of the REAL names
50 */
51const char * const 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",
56};
57
58static const 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,
62};
63
64
65#define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
66
67/*
68 * Tcp output routine: figure out what should be sent and send it.
69 */
70int
71tcp_output(PNATState pData, register struct tcpcb *tp)
72{
73 register struct socket *so = tp->t_socket;
74 register long len, win;
75 int off, flags, error;
76 register struct mbuf *m;
77 register struct tcpiphdr *ti;
78 u_char opt[MAX_TCPOPTLEN];
79 unsigned optlen, hdrlen;
80 int idle, sendalot;
81
82 DEBUG_CALL("tcp_output");
83 DEBUG_ARG("tp = %lx", (long )tp);
84
85 /*
86 * Determine length of data that should be transmitted,
87 * and flags that will be used.
88 * If there is some data or critical controls (SYN, RST)
89 * to send, then transmit; otherwise, investigate further.
90 */
91 idle = (tp->snd_max == tp->snd_una);
92 if (idle && tp->t_idle >= tp->t_rxtcur)
93 /*
94 * We have been idle for "a while" and no acks are
95 * expected to clock out any data we send --
96 * slow start to get ack "clock" running again.
97 */
98 tp->snd_cwnd = tp->t_maxseg;
99again:
100 sendalot = 0;
101 off = tp->snd_nxt - tp->snd_una;
102 win = min(tp->snd_wnd, tp->snd_cwnd);
103
104 flags = tcp_outflags[tp->t_state];
105
106 DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
107
108 /*
109 * If in persist timeout with window of 0, send 1 byte.
110 * Otherwise, if window is small but nonzero
111 * and timer expired, we will send what we can
112 * and go to transmit state.
113 */
114 if (tp->t_force) {
115 if (win == 0) {
116 /*
117 * If we still have some data to send, then
118 * clear the FIN bit. Usually this would
119 * happen below when it realizes that we
120 * aren't sending all the data. However,
121 * if we have exactly 1 byte of unset data,
122 * then it won't clear the FIN bit below,
123 * and if we are in persist state, we wind
124 * up sending the packet without recording
125 * that we sent the FIN bit.
126 *
127 * We can't just blindly clear the FIN bit,
128 * because if we don't have any more data
129 * to send then the probe will be the FIN
130 * itself.
131 */
132 if (off < so->so_snd.sb_cc)
133 flags &= ~TH_FIN;
134 win = 1;
135 } else {
136 tp->t_timer[TCPT_PERSIST] = 0;
137 tp->t_rxtshift = 0;
138 }
139 }
140
141 len = min(so->so_snd.sb_cc, win) - off;
142
143 if (len < 0) {
144 /*
145 * If FIN has been sent but not acked,
146 * but we haven't been called to retransmit,
147 * len will be -1. Otherwise, window shrank
148 * after we sent into it. If window shrank to 0,
149 * cancel pending retransmit and pull snd_nxt
150 * back to (closed) window. We will enter persist
151 * state below. If the window didn't close completely,
152 * just wait for an ACK.
153 */
154 len = 0;
155 if (win == 0) {
156 tp->t_timer[TCPT_REXMT] = 0;
157 tp->snd_nxt = tp->snd_una;
158 }
159 }
160
161 if (len > tp->t_maxseg) {
162 len = tp->t_maxseg;
163 sendalot = 1;
164 }
165 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
166 flags &= ~TH_FIN;
167
168 win = sbspace(&so->so_rcv);
169
170 /*
171 * Sender silly window avoidance. If connection is idle
172 * and can send all data, a maximum segment,
173 * at least a maximum default-size segment do it,
174 * or are forced, do it; otherwise don't bother.
175 * If peer's buffer is tiny, then send
176 * when window is at least half open.
177 * If retransmitting (possibly after persist timer forced us
178 * to send into a small window), then must resend.
179 */
180 if (len) {
181 if (len == tp->t_maxseg)
182 goto send;
183 if ((1 || idle || tp->t_flags & TF_NODELAY) &&
184 len + off >= so->so_snd.sb_cc)
185 goto send;
186 if (tp->t_force)
187 goto send;
188 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
189 goto send;
190 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
191 goto send;
192 }
193
194 /*
195 * Compare available window to amount of window
196 * known to peer (as advertised window less
197 * next expected input). If the difference is at least two
198 * max size segments, or at least 50% of the maximum possible
199 * window, then want to send a window update to peer.
200 */
201 if (win > 0) {
202 /*
203 * "adv" is the amount we can increase the window,
204 * taking into account that we are limited by
205 * TCP_MAXWIN << tp->rcv_scale.
206 */
207 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
208 (tp->rcv_adv - tp->rcv_nxt);
209
210 if (adv >= (long) (2 * tp->t_maxseg))
211 goto send;
212 if (2 * adv >= (long) so->so_rcv.sb_datalen)
213 goto send;
214 }
215
216 /*
217 * Send if we owe peer an ACK.
218 */
219 if (tp->t_flags & TF_ACKNOW)
220 goto send;
221 if (flags & (TH_SYN|TH_RST))
222 goto send;
223 if (SEQ_GT(tp->snd_up, tp->snd_una))
224 goto send;
225 /*
226 * If our state indicates that FIN should be sent
227 * and we have not yet done so, or we're retransmitting the FIN,
228 * then we need to send.
229 */
230 if (flags & TH_FIN &&
231 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
232 goto send;
233
234 /*
235 * TCP window updates are not reliable, rather a polling protocol
236 * using ``persist'' packets is used to insure receipt of window
237 * updates. The three ``states'' for the output side are:
238 * idle not doing retransmits or persists
239 * persisting to move a small or zero window
240 * (re)transmitting and thereby not persisting
241 *
242 * tp->t_timer[TCPT_PERSIST]
243 * is set when we are in persist state.
244 * tp->t_force
245 * is set when we are called to send a persist packet.
246 * tp->t_timer[TCPT_REXMT]
247 * is set when we are retransmitting
248 * The output side is idle when both timers are zero.
249 *
250 * If send window is too small, there is data to transmit, and no
251 * retransmit or persist is pending, then go to persist state.
252 * If nothing happens soon, send when timer expires:
253 * if window is nonzero, transmit what we can,
254 * otherwise force out a byte.
255 */
256 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
257 tp->t_timer[TCPT_PERSIST] == 0) {
258 tp->t_rxtshift = 0;
259 tcp_setpersist(tp);
260 }
261
262 /*
263 * No reason to send a segment, just return.
264 */
265 tcpstat.tcps_didnuttin++;
266
267 return (0);
268
269send:
270 /*
271 * Before ESTABLISHED, force sending of initial options
272 * unless TCP set not to do any options.
273 * NOTE: we assume that the IP/TCP header plus TCP options
274 * always fit in a single mbuf, leaving room for a maximum
275 * link header, i.e.
276 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
277 */
278 optlen = 0;
279 hdrlen = sizeof (struct tcpiphdr);
280 if (flags & TH_SYN) {
281 tp->snd_nxt = tp->iss;
282 if ((tp->t_flags & TF_NOOPT) == 0) {
283 u_int16_t mss;
284
285 opt[0] = TCPOPT_MAXSEG;
286 opt[1] = 4;
287 mss = htons((u_int16_t) tcp_mss(pData, tp, 0));
288 memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
289 optlen = 4;
290
291/* if ((tp->t_flags & TF_REQ_SCALE) &&
292 * ((flags & TH_ACK) == 0 ||
293 * (tp->t_flags & TF_RCVD_SCALE))) {
294 * *((u_int32_t *) (opt + optlen)) = htonl(
295 * TCPOPT_NOP << 24 |
296 * TCPOPT_WINDOW << 16 |
297 * TCPOLEN_WINDOW << 8 |
298 * tp->request_r_scale);
299 * optlen += 4;
300 * }
301 */
302 }
303 }
304
305 /*
306 * Send a timestamp and echo-reply if this is a SYN and our side
307 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
308 * and our peer have sent timestamps in our SYN's.
309 */
310/* if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
311 * (flags & TH_RST) == 0 &&
312 * ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
313 * (tp->t_flags & TF_RCVD_TSTMP))) {
314 * u_int32_t *lp = (u_int32_t *)(opt + optlen);
315 *
316 * / * Form timestamp option as shown in appendix A of RFC 1323. * /
317 * *lp++ = htonl(TCPOPT_TSTAMP_HDR);
318 * *lp++ = htonl(tcp_now);
319 * *lp = htonl(tp->ts_recent);
320 * optlen += TCPOLEN_TSTAMP_APPA;
321 * }
322 */
323 hdrlen += optlen;
324
325 /*
326 * Adjust data length if insertion of options will
327 * bump the packet length beyond the t_maxseg length.
328 */
329 if (len > tp->t_maxseg - optlen) {
330 len = tp->t_maxseg - optlen;
331 sendalot = 1;
332 }
333
334 /*
335 * Grab a header mbuf, attaching a copy of data to
336 * be transmitted, and initialize the header from
337 * the template for sends on this connection.
338 */
339 if (len) {
340 if (tp->t_force && len == 1)
341 tcpstat.tcps_sndprobe++;
342 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
343 tcpstat.tcps_sndrexmitpack++;
344 tcpstat.tcps_sndrexmitbyte += len;
345 } else {
346 tcpstat.tcps_sndpack++;
347 tcpstat.tcps_sndbyte += len;
348 }
349
350 m = m_get(pData);
351 if (m == NULL) {
352/* error = ENOBUFS; */
353 error = 1;
354 goto out;
355 }
356 m->m_data += if_maxlinkhdr;
357 m->m_len = hdrlen;
358
359 /*
360 * This will always succeed, since we make sure our mbufs
361 * are big enough to hold one MSS packet + header + ... etc.
362 */
363/* if (len <= MHLEN - hdrlen - max_linkhdr) { */
364
365 sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
366 m->m_len += len;
367
368/* } else {
369 * m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
370 * if (m->m_next == 0)
371 * len = 0;
372 * }
373 */
374 /*
375 * If we're sending everything we've got, set PUSH.
376 * (This will keep happy those implementations which only
377 * give data to the user when a buffer fills or
378 * a PUSH comes in.)
379 */
380 if (off + len == so->so_snd.sb_cc)
381 flags |= TH_PUSH;
382 } else {
383 if (tp->t_flags & TF_ACKNOW)
384 tcpstat.tcps_sndacks++;
385 else if (flags & (TH_SYN|TH_FIN|TH_RST))
386 tcpstat.tcps_sndctrl++;
387 else if (SEQ_GT(tp->snd_up, tp->snd_una))
388 tcpstat.tcps_sndurg++;
389 else
390 tcpstat.tcps_sndwinup++;
391
392 m = m_get(pData);
393 if (m == NULL) {
394/* error = ENOBUFS; */
395 error = 1;
396 goto out;
397 }
398 m->m_data += if_maxlinkhdr;
399 m->m_len = hdrlen;
400 }
401
402 ti = mtod(m, struct tcpiphdr *);
403
404 memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
405
406 /*
407 * Fill in fields, remembering maximum advertised
408 * window for use in delaying messages about window sizes.
409 * If resending a FIN, be sure not to use a new sequence number.
410 */
411 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
412 tp->snd_nxt == tp->snd_max)
413 tp->snd_nxt--;
414 /*
415 * If we are doing retransmissions, then snd_nxt will
416 * not reflect the first unsent octet. For ACK only
417 * packets, we do not want the sequence number of the
418 * retransmitted packet, we want the sequence number
419 * of the next unsent octet. So, if there is no data
420 * (and no SYN or FIN), use snd_max instead of snd_nxt
421 * when filling in ti_seq. But if we are in persist
422 * state, snd_max might reflect one byte beyond the
423 * right edge of the window, so use snd_nxt in that
424 * case, since we know we aren't doing a retransmission.
425 * (retransmit and persist are mutually exclusive...)
426 */
427 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
428 ti->ti_seq = htonl(tp->snd_nxt);
429 else
430 ti->ti_seq = htonl(tp->snd_max);
431 ti->ti_ack = htonl(tp->rcv_nxt);
432 if (optlen) {
433 memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
434 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
435 }
436 ti->ti_flags = flags;
437 /*
438 * Calculate receive window. Don't shrink window,
439 * but avoid silly window syndrome.
440 */
441 if (win < (long)(so->so_rcv.sb_datalen / 4) && win < (long)tp->t_maxseg)
442 win = 0;
443 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
444 win = (long)TCP_MAXWIN << tp->rcv_scale;
445 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
446 win = (long)(tp->rcv_adv - tp->rcv_nxt);
447 ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
448
449 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
450 ti->ti_urp = htons((u_int16_t)(tp->snd_up - ntohl(ti->ti_seq)));
451#ifdef notdef
452 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
453 ti->ti_urp = htons((u_int16_t)(tp->snd_up - tp->snd_nxt));
454#endif
455 ti->ti_flags |= TH_URG;
456 } else
457 /*
458 * If no urgent pointer to send, then we pull
459 * the urgent pointer to the left edge of the send window
460 * so that it doesn't drift into the send window on sequence
461 * number wraparound.
462 */
463 tp->snd_up = tp->snd_una; /* drag it along */
464
465 /*
466 * Put TCP length in extended header, and then
467 * checksum extended header and data.
468 */
469 if (len + optlen)
470 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
471 optlen + len));
472 ti->ti_sum = cksum(m, (int)(hdrlen + len));
473
474 /*
475 * In transmit state, time the transmission and arrange for
476 * the retransmit. In persist state, just set snd_max.
477 */
478 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
479 tcp_seq startseq = tp->snd_nxt;
480
481 /*
482 * Advance snd_nxt over sequence space of this segment.
483 */
484 if (flags & (TH_SYN|TH_FIN)) {
485 if (flags & TH_SYN)
486 tp->snd_nxt++;
487 if (flags & TH_FIN) {
488 tp->snd_nxt++;
489 tp->t_flags |= TF_SENTFIN;
490 }
491 }
492 tp->snd_nxt += len;
493 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
494 tp->snd_max = tp->snd_nxt;
495 /*
496 * Time this transmission if not a retransmission and
497 * not currently timing anything.
498 */
499 if (tp->t_rtt == 0) {
500 tp->t_rtt = 1;
501 tp->t_rtseq = startseq;
502 tcpstat.tcps_segstimed++;
503 }
504 }
505
506 /*
507 * Set retransmit timer if not currently set,
508 * and not doing an ack or a keep-alive probe.
509 * Initial value for retransmit timer is smoothed
510 * round-trip time + 2 * round-trip time variance.
511 * Initialize shift counter which is used for backoff
512 * of retransmit time.
513 */
514 if (tp->t_timer[TCPT_REXMT] == 0 &&
515 tp->snd_nxt != tp->snd_una) {
516 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
517 if (tp->t_timer[TCPT_PERSIST]) {
518 tp->t_timer[TCPT_PERSIST] = 0;
519 tp->t_rxtshift = 0;
520 }
521 }
522 } else
523 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
524 tp->snd_max = tp->snd_nxt + len;
525
526 /*
527 * Fill in IP length and desired time to live and
528 * send to IP level. There should be a better way
529 * to handle ttl and tos; we could keep them in
530 * the template, but need a way to checksum without them.
531 */
532 m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
533
534 {
535
536 ((struct ip *)ti)->ip_len = m->m_len;
537
538 ((struct ip *)ti)->ip_ttl = ip_defttl;
539 ((struct ip *)ti)->ip_tos = so->so_iptos;
540
541/* #if BSD >= 43 */
542 /* Don't do IP options... */
543/* error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
544 * so->so_options & SO_DONTROUTE, 0);
545 */
546 error = ip_output(pData, so, m);
547
548/* #else
549 * error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
550 * so->so_options & SO_DONTROUTE);
551 * #endif
552 */
553 }
554 if (error) {
555out:
556/* if (error == ENOBUFS) {
557 * tcp_quench(tp->t_inpcb, 0);
558 * return (0);
559 * }
560 */
561/* if ((error == EHOSTUNREACH || error == ENETDOWN)
562 * && TCPS_HAVERCVDSYN(tp->t_state)) {
563 * tp->t_softerror = error;
564 * return (0);
565 * }
566 */
567 return (error);
568 }
569 tcpstat.tcps_sndtotal++;
570
571 /*
572 * Data sent (as far as we can tell).
573 * If this advertises a larger window than any other segment,
574 * then remember the size of the advertised window.
575 * Any pending ACK has now been sent.
576 */
577 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
578 tp->rcv_adv = tp->rcv_nxt + win;
579 tp->last_ack_sent = tp->rcv_nxt;
580 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
581 if (sendalot)
582 goto again;
583
584 return (0);
585}
586
587void
588tcp_setpersist(tp)
589 register struct tcpcb *tp;
590{
591 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
592
593/* if (tp->t_timer[TCPT_REXMT])
594 * panic("tcp_output REXMT");
595 */
596 /*
597 * Start/restart persistence timer.
598 */
599 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
600 t * tcp_backoff[tp->t_rxtshift],
601 TCPTV_PERSMIN, TCPTV_PERSMAX);
602 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
603 tp->t_rxtshift++;
604}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette