tcp_output.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
  30. * tcp_output.c,v 1.3 1994/09/15 10:36:55 davidg Exp
  31. */
  32. /*
  33. * Changes and additions relating to SLiRP
  34. * Copyright (c) 1995 Danny Gasparovski.
  35. *
  36. * Please read the file COPYRIGHT for the
  37. * terms and conditions of the copyright.
  38. */
  39. #include "qemu/osdep.h"
  40. #include <slirp.h>
  41. static const u_char tcp_outflags[TCP_NSTATES] = {
  42. TH_RST|TH_ACK, 0, TH_SYN, TH_SYN|TH_ACK,
  43. TH_ACK, TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
  44. TH_FIN|TH_ACK, TH_ACK, TH_ACK,
  45. };
  46. #undef MAX_TCPOPTLEN
  47. #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
  48. /*
  49. * Tcp output routine: figure out what should be sent and send it.
  50. */
  51. int
  52. tcp_output(struct tcpcb *tp)
  53. {
  54. register struct socket *so = tp->t_socket;
  55. register long len, win;
  56. int off, flags, error;
  57. register struct mbuf *m;
  58. register struct tcpiphdr *ti;
  59. u_char opt[MAX_TCPOPTLEN];
  60. unsigned optlen, hdrlen;
  61. int idle, sendalot;
  62. DEBUG_CALL("tcp_output");
  63. DEBUG_ARG("tp = %p", tp);
  64. /*
  65. * Determine length of data that should be transmitted,
  66. * and flags that will be used.
  67. * If there is some data or critical controls (SYN, RST)
  68. * to send, then transmit; otherwise, investigate further.
  69. */
  70. idle = (tp->snd_max == tp->snd_una);
  71. if (idle && tp->t_idle >= tp->t_rxtcur)
  72. /*
  73. * We have been idle for "a while" and no acks are
  74. * expected to clock out any data we send --
  75. * slow start to get ack "clock" running again.
  76. */
  77. tp->snd_cwnd = tp->t_maxseg;
  78. again:
  79. sendalot = 0;
  80. off = tp->snd_nxt - tp->snd_una;
  81. win = min(tp->snd_wnd, tp->snd_cwnd);
  82. flags = tcp_outflags[tp->t_state];
  83. DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
  84. /*
  85. * If in persist timeout with window of 0, send 1 byte.
  86. * Otherwise, if window is small but nonzero
  87. * and timer expired, we will send what we can
  88. * and go to transmit state.
  89. */
  90. if (tp->t_force) {
  91. if (win == 0) {
  92. /*
  93. * If we still have some data to send, then
  94. * clear the FIN bit. Usually this would
  95. * happen below when it realizes that we
  96. * aren't sending all the data. However,
  97. * if we have exactly 1 byte of unset data,
  98. * then it won't clear the FIN bit below,
  99. * and if we are in persist state, we wind
  100. * up sending the packet without recording
  101. * that we sent the FIN bit.
  102. *
  103. * We can't just blindly clear the FIN bit,
  104. * because if we don't have any more data
  105. * to send then the probe will be the FIN
  106. * itself.
  107. */
  108. if (off < so->so_snd.sb_cc)
  109. flags &= ~TH_FIN;
  110. win = 1;
  111. } else {
  112. tp->t_timer[TCPT_PERSIST] = 0;
  113. tp->t_rxtshift = 0;
  114. }
  115. }
  116. len = min(so->so_snd.sb_cc, win) - off;
  117. if (len < 0) {
  118. /*
  119. * If FIN has been sent but not acked,
  120. * but we haven't been called to retransmit,
  121. * len will be -1. Otherwise, window shrank
  122. * after we sent into it. If window shrank to 0,
  123. * cancel pending retransmit and pull snd_nxt
  124. * back to (closed) window. We will enter persist
  125. * state below. If the window didn't close completely,
  126. * just wait for an ACK.
  127. */
  128. len = 0;
  129. if (win == 0) {
  130. tp->t_timer[TCPT_REXMT] = 0;
  131. tp->snd_nxt = tp->snd_una;
  132. }
  133. }
  134. if (len > tp->t_maxseg) {
  135. len = tp->t_maxseg;
  136. sendalot = 1;
  137. }
  138. if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
  139. flags &= ~TH_FIN;
  140. win = sbspace(&so->so_rcv);
  141. /*
  142. * Sender silly window avoidance. If connection is idle
  143. * and can send all data, a maximum segment,
  144. * at least a maximum default-size segment do it,
  145. * or are forced, do it; otherwise don't bother.
  146. * If peer's buffer is tiny, then send
  147. * when window is at least half open.
  148. * If retransmitting (possibly after persist timer forced us
  149. * to send into a small window), then must resend.
  150. */
  151. if (len) {
  152. if (len == tp->t_maxseg)
  153. goto send;
  154. if ((1 || idle || tp->t_flags & TF_NODELAY) &&
  155. len + off >= so->so_snd.sb_cc)
  156. goto send;
  157. if (tp->t_force)
  158. goto send;
  159. if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
  160. goto send;
  161. if (SEQ_LT(tp->snd_nxt, tp->snd_max))
  162. goto send;
  163. }
  164. /*
  165. * Compare available window to amount of window
  166. * known to peer (as advertised window less
  167. * next expected input). If the difference is at least two
  168. * max size segments, or at least 50% of the maximum possible
  169. * window, then want to send a window update to peer.
  170. */
  171. if (win > 0) {
  172. /*
  173. * "adv" is the amount we can increase the window,
  174. * taking into account that we are limited by
  175. * TCP_MAXWIN << tp->rcv_scale.
  176. */
  177. long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
  178. (tp->rcv_adv - tp->rcv_nxt);
  179. if (adv >= (long) (2 * tp->t_maxseg))
  180. goto send;
  181. if (2 * adv >= (long) so->so_rcv.sb_datalen)
  182. goto send;
  183. }
  184. /*
  185. * Send if we owe peer an ACK.
  186. */
  187. if (tp->t_flags & TF_ACKNOW)
  188. goto send;
  189. if (flags & (TH_SYN|TH_RST))
  190. goto send;
  191. if (SEQ_GT(tp->snd_up, tp->snd_una))
  192. goto send;
  193. /*
  194. * If our state indicates that FIN should be sent
  195. * and we have not yet done so, or we're retransmitting the FIN,
  196. * then we need to send.
  197. */
  198. if (flags & TH_FIN &&
  199. ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
  200. goto send;
  201. /*
  202. * TCP window updates are not reliable, rather a polling protocol
  203. * using ``persist'' packets is used to insure receipt of window
  204. * updates. The three ``states'' for the output side are:
  205. * idle not doing retransmits or persists
  206. * persisting to move a small or zero window
  207. * (re)transmitting and thereby not persisting
  208. *
  209. * tp->t_timer[TCPT_PERSIST]
  210. * is set when we are in persist state.
  211. * tp->t_force
  212. * is set when we are called to send a persist packet.
  213. * tp->t_timer[TCPT_REXMT]
  214. * is set when we are retransmitting
  215. * The output side is idle when both timers are zero.
  216. *
  217. * If send window is too small, there is data to transmit, and no
  218. * retransmit or persist is pending, then go to persist state.
  219. * If nothing happens soon, send when timer expires:
  220. * if window is nonzero, transmit what we can,
  221. * otherwise force out a byte.
  222. */
  223. if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
  224. tp->t_timer[TCPT_PERSIST] == 0) {
  225. tp->t_rxtshift = 0;
  226. tcp_setpersist(tp);
  227. }
  228. /*
  229. * No reason to send a segment, just return.
  230. */
  231. return (0);
  232. send:
  233. /*
  234. * Before ESTABLISHED, force sending of initial options
  235. * unless TCP set not to do any options.
  236. * NOTE: we assume that the IP/TCP header plus TCP options
  237. * always fit in a single mbuf, leaving room for a maximum
  238. * link header, i.e.
  239. * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
  240. */
  241. optlen = 0;
  242. hdrlen = sizeof (struct tcpiphdr);
  243. if (flags & TH_SYN) {
  244. tp->snd_nxt = tp->iss;
  245. if ((tp->t_flags & TF_NOOPT) == 0) {
  246. uint16_t mss;
  247. opt[0] = TCPOPT_MAXSEG;
  248. opt[1] = 4;
  249. mss = htons((uint16_t) tcp_mss(tp, 0));
  250. memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
  251. optlen = 4;
  252. }
  253. }
  254. hdrlen += optlen;
  255. /*
  256. * Adjust data length if insertion of options will
  257. * bump the packet length beyond the t_maxseg length.
  258. */
  259. if (len > tp->t_maxseg - optlen) {
  260. len = tp->t_maxseg - optlen;
  261. sendalot = 1;
  262. }
  263. /*
  264. * Grab a header mbuf, attaching a copy of data to
  265. * be transmitted, and initialize the header from
  266. * the template for sends on this connection.
  267. */
  268. if (len) {
  269. m = m_get(so->slirp);
  270. if (m == NULL) {
  271. error = 1;
  272. goto out;
  273. }
  274. m->m_data += IF_MAXLINKHDR;
  275. m->m_len = hdrlen;
  276. sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
  277. m->m_len += len;
  278. /*
  279. * If we're sending everything we've got, set PUSH.
  280. * (This will keep happy those implementations which only
  281. * give data to the user when a buffer fills or
  282. * a PUSH comes in.)
  283. */
  284. if (off + len == so->so_snd.sb_cc)
  285. flags |= TH_PUSH;
  286. } else {
  287. m = m_get(so->slirp);
  288. if (m == NULL) {
  289. error = 1;
  290. goto out;
  291. }
  292. m->m_data += IF_MAXLINKHDR;
  293. m->m_len = hdrlen;
  294. }
  295. ti = mtod(m, struct tcpiphdr *);
  296. memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
  297. /*
  298. * Fill in fields, remembering maximum advertised
  299. * window for use in delaying messages about window sizes.
  300. * If resending a FIN, be sure not to use a new sequence number.
  301. */
  302. if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
  303. tp->snd_nxt == tp->snd_max)
  304. tp->snd_nxt--;
  305. /*
  306. * If we are doing retransmissions, then snd_nxt will
  307. * not reflect the first unsent octet. For ACK only
  308. * packets, we do not want the sequence number of the
  309. * retransmitted packet, we want the sequence number
  310. * of the next unsent octet. So, if there is no data
  311. * (and no SYN or FIN), use snd_max instead of snd_nxt
  312. * when filling in ti_seq. But if we are in persist
  313. * state, snd_max might reflect one byte beyond the
  314. * right edge of the window, so use snd_nxt in that
  315. * case, since we know we aren't doing a retransmission.
  316. * (retransmit and persist are mutually exclusive...)
  317. */
  318. if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
  319. ti->ti_seq = htonl(tp->snd_nxt);
  320. else
  321. ti->ti_seq = htonl(tp->snd_max);
  322. ti->ti_ack = htonl(tp->rcv_nxt);
  323. if (optlen) {
  324. memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
  325. ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
  326. }
  327. ti->ti_flags = flags;
  328. /*
  329. * Calculate receive window. Don't shrink window,
  330. * but avoid silly window syndrome.
  331. */
  332. if (win < (long)(so->so_rcv.sb_datalen / 4) && win < (long)tp->t_maxseg)
  333. win = 0;
  334. if (win > (long)TCP_MAXWIN << tp->rcv_scale)
  335. win = (long)TCP_MAXWIN << tp->rcv_scale;
  336. if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
  337. win = (long)(tp->rcv_adv - tp->rcv_nxt);
  338. ti->ti_win = htons((uint16_t) (win>>tp->rcv_scale));
  339. if (SEQ_GT(tp->snd_up, tp->snd_una)) {
  340. ti->ti_urp = htons((uint16_t)(tp->snd_up - ntohl(ti->ti_seq)));
  341. ti->ti_flags |= TH_URG;
  342. } else
  343. /*
  344. * If no urgent pointer to send, then we pull
  345. * the urgent pointer to the left edge of the send window
  346. * so that it doesn't drift into the send window on sequence
  347. * number wraparound.
  348. */
  349. tp->snd_up = tp->snd_una; /* drag it along */
  350. /*
  351. * Put TCP length in extended header, and then
  352. * checksum extended header and data.
  353. */
  354. if (len + optlen)
  355. ti->ti_len = htons((uint16_t)(sizeof (struct tcphdr) +
  356. optlen + len));
  357. ti->ti_sum = cksum(m, (int)(hdrlen + len));
  358. /*
  359. * In transmit state, time the transmission and arrange for
  360. * the retransmit. In persist state, just set snd_max.
  361. */
  362. if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
  363. tcp_seq startseq = tp->snd_nxt;
  364. /*
  365. * Advance snd_nxt over sequence space of this segment.
  366. */
  367. if (flags & (TH_SYN|TH_FIN)) {
  368. if (flags & TH_SYN)
  369. tp->snd_nxt++;
  370. if (flags & TH_FIN) {
  371. tp->snd_nxt++;
  372. tp->t_flags |= TF_SENTFIN;
  373. }
  374. }
  375. tp->snd_nxt += len;
  376. if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
  377. tp->snd_max = tp->snd_nxt;
  378. /*
  379. * Time this transmission if not a retransmission and
  380. * not currently timing anything.
  381. */
  382. if (tp->t_rtt == 0) {
  383. tp->t_rtt = 1;
  384. tp->t_rtseq = startseq;
  385. }
  386. }
  387. /*
  388. * Set retransmit timer if not currently set,
  389. * and not doing an ack or a keep-alive probe.
  390. * Initial value for retransmit timer is smoothed
  391. * round-trip time + 2 * round-trip time variance.
  392. * Initialize shift counter which is used for backoff
  393. * of retransmit time.
  394. */
  395. if (tp->t_timer[TCPT_REXMT] == 0 &&
  396. tp->snd_nxt != tp->snd_una) {
  397. tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
  398. if (tp->t_timer[TCPT_PERSIST]) {
  399. tp->t_timer[TCPT_PERSIST] = 0;
  400. tp->t_rxtshift = 0;
  401. }
  402. }
  403. } else
  404. if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
  405. tp->snd_max = tp->snd_nxt + len;
  406. /*
  407. * Fill in IP length and desired time to live and
  408. * send to IP level. There should be a better way
  409. * to handle ttl and tos; we could keep them in
  410. * the template, but need a way to checksum without them.
  411. */
  412. m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
  413. {
  414. ((struct ip *)ti)->ip_len = m->m_len;
  415. ((struct ip *)ti)->ip_ttl = IPDEFTTL;
  416. ((struct ip *)ti)->ip_tos = so->so_iptos;
  417. error = ip_output(so, m);
  418. }
  419. if (error) {
  420. out:
  421. return (error);
  422. }
  423. /*
  424. * Data sent (as far as we can tell).
  425. * If this advertises a larger window than any other segment,
  426. * then remember the size of the advertised window.
  427. * Any pending ACK has now been sent.
  428. */
  429. if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
  430. tp->rcv_adv = tp->rcv_nxt + win;
  431. tp->last_ack_sent = tp->rcv_nxt;
  432. tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
  433. if (sendalot)
  434. goto again;
  435. return (0);
  436. }
  437. void
  438. tcp_setpersist(struct tcpcb *tp)
  439. {
  440. int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
  441. /*
  442. * Start/restart persistence timer.
  443. */
  444. TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
  445. t * tcp_backoff[tp->t_rxtshift],
  446. TCPTV_PERSMIN, TCPTV_PERSMAX);
  447. if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
  448. tp->t_rxtshift++;
  449. }