tcp_output.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Implementation of the Transmission Control Protocol(TCP).
  7. *
  8. * Version: $Id: tcp_output.c,v 1.146 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  13. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  14. * Florian La Roche, <flla@stud.uni-sb.de>
  15. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  16. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  17. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  18. * Matthew Dillon, <dillon@apollo.west.oic.com>
  19. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  20. * Jorge Cwik, <jorge@laser.satlink.net>
  21. */
  22. /*
  23. * Changes: Pedro Roque : Retransmit queue handled by TCP.
  24. * : Fragmentation on mtu decrease
  25. * : Segment collapse on retransmit
  26. * : AF independence
  27. *
  28. * Linus Torvalds : send_delayed_ack
  29. * David S. Miller : Charge memory using the right skb
  30. * during syn/ack processing.
  31. * David S. Miller : Output engine completely rewritten.
  32. * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
  33. * Cacophonix Gaul : draft-minshall-nagle-01
  34. * J Hadi Salim : ECN support
  35. *
  36. */
  37. #include <net/tcp.h>
  38. #include <linux/compiler.h>
  39. #include <linux/module.h>
  40. #include <linux/smp_lock.h>
  41. /* People can turn this off for buggy TCP's found in printers etc. */
  42. int sysctl_tcp_retrans_collapse __read_mostly = 1;
  43. /* People can turn this on to work with those rare, broken TCPs that
  44. * interpret the window field as a signed quantity.
  45. */
  46. int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
  47. /* This limits the percentage of the congestion window which we
  48. * will allow a single TSO frame to consume. Building TSO frames
  49. * which are too large can cause TCP streams to be bursty.
  50. */
  51. int sysctl_tcp_tso_win_divisor __read_mostly = 3;
  52. int sysctl_tcp_mtu_probing __read_mostly = 0;
  53. int sysctl_tcp_base_mss __read_mostly = 512;
  54. /* By default, RFC2861 behavior. */
  55. int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
  56. static void update_send_head(struct sock *sk, struct tcp_sock *tp,
  57. struct sk_buff *skb)
  58. {
  59. sk->sk_send_head = skb->next;
  60. if (sk->sk_send_head == (struct sk_buff *)&sk->sk_write_queue)
  61. sk->sk_send_head = NULL;
  62. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  63. tcp_packets_out_inc(sk, tp, skb);
  64. }
  65. /* SND.NXT, if window was not shrunk.
  66. * If window has been shrunk, what should we make? It is not clear at all.
  67. * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
  68. * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
  69. * invalid. OK, let's make this for now:
  70. */
  71. static inline __u32 tcp_acceptable_seq(struct sock *sk, struct tcp_sock *tp)
  72. {
  73. if (!before(tp->snd_una+tp->snd_wnd, tp->snd_nxt))
  74. return tp->snd_nxt;
  75. else
  76. return tp->snd_una+tp->snd_wnd;
  77. }
  78. /* Calculate mss to advertise in SYN segment.
  79. * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
  80. *
  81. * 1. It is independent of path mtu.
  82. * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
  83. * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
  84. * attached devices, because some buggy hosts are confused by
  85. * large MSS.
  86. * 4. We do not make 3, we advertise MSS, calculated from first
  87. * hop device mtu, but allow to raise it to ip_rt_min_advmss.
  88. * This may be overridden via information stored in routing table.
  89. * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
  90. * probably even Jumbo".
  91. */
  92. static __u16 tcp_advertise_mss(struct sock *sk)
  93. {
  94. struct tcp_sock *tp = tcp_sk(sk);
  95. struct dst_entry *dst = __sk_dst_get(sk);
  96. int mss = tp->advmss;
  97. if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
  98. mss = dst_metric(dst, RTAX_ADVMSS);
  99. tp->advmss = mss;
  100. }
  101. return (__u16)mss;
  102. }
  103. /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
  104. * This is the first part of cwnd validation mechanism. */
  105. static void tcp_cwnd_restart(struct sock *sk, struct dst_entry *dst)
  106. {
  107. struct tcp_sock *tp = tcp_sk(sk);
  108. s32 delta = tcp_time_stamp - tp->lsndtime;
  109. u32 restart_cwnd = tcp_init_cwnd(tp, dst);
  110. u32 cwnd = tp->snd_cwnd;
  111. tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
  112. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  113. restart_cwnd = min(restart_cwnd, cwnd);
  114. while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
  115. cwnd >>= 1;
  116. tp->snd_cwnd = max(cwnd, restart_cwnd);
  117. tp->snd_cwnd_stamp = tcp_time_stamp;
  118. tp->snd_cwnd_used = 0;
  119. }
  120. static void tcp_event_data_sent(struct tcp_sock *tp,
  121. struct sk_buff *skb, struct sock *sk)
  122. {
  123. struct inet_connection_sock *icsk = inet_csk(sk);
  124. const u32 now = tcp_time_stamp;
  125. if (sysctl_tcp_slow_start_after_idle &&
  126. (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
  127. tcp_cwnd_restart(sk, __sk_dst_get(sk));
  128. tp->lsndtime = now;
  129. /* If it is a reply for ato after last received
  130. * packet, enter pingpong mode.
  131. */
  132. if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
  133. icsk->icsk_ack.pingpong = 1;
  134. }
  135. static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
  136. {
  137. tcp_dec_quickack_mode(sk, pkts);
  138. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  139. }
  140. /* Determine a window scaling and initial window to offer.
  141. * Based on the assumption that the given amount of space
  142. * will be offered. Store the results in the tp structure.
  143. * NOTE: for smooth operation initial space offering should
  144. * be a multiple of mss if possible. We assume here that mss >= 1.
  145. * This MUST be enforced by all callers.
  146. */
  147. void tcp_select_initial_window(int __space, __u32 mss,
  148. __u32 *rcv_wnd, __u32 *window_clamp,
  149. int wscale_ok, __u8 *rcv_wscale)
  150. {
  151. unsigned int space = (__space < 0 ? 0 : __space);
  152. /* If no clamp set the clamp to the max possible scaled window */
  153. if (*window_clamp == 0)
  154. (*window_clamp) = (65535 << 14);
  155. space = min(*window_clamp, space);
  156. /* Quantize space offering to a multiple of mss if possible. */
  157. if (space > mss)
  158. space = (space / mss) * mss;
  159. /* NOTE: offering an initial window larger than 32767
  160. * will break some buggy TCP stacks. If the admin tells us
  161. * it is likely we could be speaking with such a buggy stack
  162. * we will truncate our initial window offering to 32K-1
  163. * unless the remote has sent us a window scaling option,
  164. * which we interpret as a sign the remote TCP is not
  165. * misinterpreting the window field as a signed quantity.
  166. */
  167. if (sysctl_tcp_workaround_signed_windows)
  168. (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
  169. else
  170. (*rcv_wnd) = space;
  171. (*rcv_wscale) = 0;
  172. if (wscale_ok) {
  173. /* Set window scaling on max possible window
  174. * See RFC1323 for an explanation of the limit to 14
  175. */
  176. space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
  177. space = min_t(u32, space, *window_clamp);
  178. while (space > 65535 && (*rcv_wscale) < 14) {
  179. space >>= 1;
  180. (*rcv_wscale)++;
  181. }
  182. }
  183. /* Set initial window to value enough for senders,
  184. * following RFC2414. Senders, not following this RFC,
  185. * will be satisfied with 2.
  186. */
  187. if (mss > (1<<*rcv_wscale)) {
  188. int init_cwnd = 4;
  189. if (mss > 1460*3)
  190. init_cwnd = 2;
  191. else if (mss > 1460)
  192. init_cwnd = 3;
  193. if (*rcv_wnd > init_cwnd*mss)
  194. *rcv_wnd = init_cwnd*mss;
  195. }
  196. /* Set the clamp no higher than max representable value */
  197. (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
  198. }
  199. /* Chose a new window to advertise, update state in tcp_sock for the
  200. * socket, and return result with RFC1323 scaling applied. The return
  201. * value can be stuffed directly into th->window for an outgoing
  202. * frame.
  203. */
  204. static u16 tcp_select_window(struct sock *sk)
  205. {
  206. struct tcp_sock *tp = tcp_sk(sk);
  207. u32 cur_win = tcp_receive_window(tp);
  208. u32 new_win = __tcp_select_window(sk);
  209. /* Never shrink the offered window */
  210. if(new_win < cur_win) {
  211. /* Danger Will Robinson!
  212. * Don't update rcv_wup/rcv_wnd here or else
  213. * we will not be able to advertise a zero
  214. * window in time. --DaveM
  215. *
  216. * Relax Will Robinson.
  217. */
  218. new_win = cur_win;
  219. }
  220. tp->rcv_wnd = new_win;
  221. tp->rcv_wup = tp->rcv_nxt;
  222. /* Make sure we do not exceed the maximum possible
  223. * scaled window.
  224. */
  225. if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
  226. new_win = min(new_win, MAX_TCP_WINDOW);
  227. else
  228. new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
  229. /* RFC1323 scaling applied */
  230. new_win >>= tp->rx_opt.rcv_wscale;
  231. /* If we advertise zero window, disable fast path. */
  232. if (new_win == 0)
  233. tp->pred_flags = 0;
  234. return new_win;
  235. }
  236. static void tcp_build_and_update_options(__be32 *ptr, struct tcp_sock *tp,
  237. __u32 tstamp)
  238. {
  239. if (tp->rx_opt.tstamp_ok) {
  240. *ptr++ = htonl((TCPOPT_NOP << 24) |
  241. (TCPOPT_NOP << 16) |
  242. (TCPOPT_TIMESTAMP << 8) |
  243. TCPOLEN_TIMESTAMP);
  244. *ptr++ = htonl(tstamp);
  245. *ptr++ = htonl(tp->rx_opt.ts_recent);
  246. }
  247. if (tp->rx_opt.eff_sacks) {
  248. struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
  249. int this_sack;
  250. *ptr++ = htonl((TCPOPT_NOP << 24) |
  251. (TCPOPT_NOP << 16) |
  252. (TCPOPT_SACK << 8) |
  253. (TCPOLEN_SACK_BASE + (tp->rx_opt.eff_sacks *
  254. TCPOLEN_SACK_PERBLOCK)));
  255. for(this_sack = 0; this_sack < tp->rx_opt.eff_sacks; this_sack++) {
  256. *ptr++ = htonl(sp[this_sack].start_seq);
  257. *ptr++ = htonl(sp[this_sack].end_seq);
  258. }
  259. if (tp->rx_opt.dsack) {
  260. tp->rx_opt.dsack = 0;
  261. tp->rx_opt.eff_sacks--;
  262. }
  263. }
  264. }
  265. /* Construct a tcp options header for a SYN or SYN_ACK packet.
  266. * If this is every changed make sure to change the definition of
  267. * MAX_SYN_SIZE to match the new maximum number of options that you
  268. * can generate.
  269. */
  270. static void tcp_syn_build_options(__be32 *ptr, int mss, int ts, int sack,
  271. int offer_wscale, int wscale, __u32 tstamp,
  272. __u32 ts_recent)
  273. {
  274. /* We always get an MSS option.
  275. * The option bytes which will be seen in normal data
  276. * packets should timestamps be used, must be in the MSS
  277. * advertised. But we subtract them from tp->mss_cache so
  278. * that calculations in tcp_sendmsg are simpler etc.
  279. * So account for this fact here if necessary. If we
  280. * don't do this correctly, as a receiver we won't
  281. * recognize data packets as being full sized when we
  282. * should, and thus we won't abide by the delayed ACK
  283. * rules correctly.
  284. * SACKs don't matter, we never delay an ACK when we
  285. * have any of those going out.
  286. */
  287. *ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
  288. if (ts) {
  289. if(sack)
  290. *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
  291. (TCPOLEN_SACK_PERM << 16) |
  292. (TCPOPT_TIMESTAMP << 8) |
  293. TCPOLEN_TIMESTAMP);
  294. else
  295. *ptr++ = htonl((TCPOPT_NOP << 24) |
  296. (TCPOPT_NOP << 16) |
  297. (TCPOPT_TIMESTAMP << 8) |
  298. TCPOLEN_TIMESTAMP);
  299. *ptr++ = htonl(tstamp); /* TSVAL */
  300. *ptr++ = htonl(ts_recent); /* TSECR */
  301. } else if(sack)
  302. *ptr++ = htonl((TCPOPT_NOP << 24) |
  303. (TCPOPT_NOP << 16) |
  304. (TCPOPT_SACK_PERM << 8) |
  305. TCPOLEN_SACK_PERM);
  306. if (offer_wscale)
  307. *ptr++ = htonl((TCPOPT_NOP << 24) |
  308. (TCPOPT_WINDOW << 16) |
  309. (TCPOLEN_WINDOW << 8) |
  310. (wscale));
  311. }
  312. /* This routine actually transmits TCP packets queued in by
  313. * tcp_do_sendmsg(). This is used by both the initial
  314. * transmission and possible later retransmissions.
  315. * All SKB's seen here are completely headerless. It is our
  316. * job to build the TCP header, and pass the packet down to
  317. * IP so it can do the same plus pass the packet off to the
  318. * device.
  319. *
  320. * We are working here with either a clone of the original
  321. * SKB, or a fresh unique copy made by the retransmit engine.
  322. */
  323. static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, gfp_t gfp_mask)
  324. {
  325. const struct inet_connection_sock *icsk = inet_csk(sk);
  326. struct inet_sock *inet;
  327. struct tcp_sock *tp;
  328. struct tcp_skb_cb *tcb;
  329. int tcp_header_size;
  330. struct tcphdr *th;
  331. int sysctl_flags;
  332. int err;
  333. BUG_ON(!skb || !tcp_skb_pcount(skb));
  334. /* If congestion control is doing timestamping, we must
  335. * take such a timestamp before we potentially clone/copy.
  336. */
  337. if (icsk->icsk_ca_ops->rtt_sample)
  338. __net_timestamp(skb);
  339. if (likely(clone_it)) {
  340. if (unlikely(skb_cloned(skb)))
  341. skb = pskb_copy(skb, gfp_mask);
  342. else
  343. skb = skb_clone(skb, gfp_mask);
  344. if (unlikely(!skb))
  345. return -ENOBUFS;
  346. }
  347. inet = inet_sk(sk);
  348. tp = tcp_sk(sk);
  349. tcb = TCP_SKB_CB(skb);
  350. tcp_header_size = tp->tcp_header_len;
  351. #define SYSCTL_FLAG_TSTAMPS 0x1
  352. #define SYSCTL_FLAG_WSCALE 0x2
  353. #define SYSCTL_FLAG_SACK 0x4
  354. sysctl_flags = 0;
  355. if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
  356. tcp_header_size = sizeof(struct tcphdr) + TCPOLEN_MSS;
  357. if(sysctl_tcp_timestamps) {
  358. tcp_header_size += TCPOLEN_TSTAMP_ALIGNED;
  359. sysctl_flags |= SYSCTL_FLAG_TSTAMPS;
  360. }
  361. if (sysctl_tcp_window_scaling) {
  362. tcp_header_size += TCPOLEN_WSCALE_ALIGNED;
  363. sysctl_flags |= SYSCTL_FLAG_WSCALE;
  364. }
  365. if (sysctl_tcp_sack) {
  366. sysctl_flags |= SYSCTL_FLAG_SACK;
  367. if (!(sysctl_flags & SYSCTL_FLAG_TSTAMPS))
  368. tcp_header_size += TCPOLEN_SACKPERM_ALIGNED;
  369. }
  370. } else if (unlikely(tp->rx_opt.eff_sacks)) {
  371. /* A SACK is 2 pad bytes, a 2 byte header, plus
  372. * 2 32-bit sequence numbers for each SACK block.
  373. */
  374. tcp_header_size += (TCPOLEN_SACK_BASE_ALIGNED +
  375. (tp->rx_opt.eff_sacks *
  376. TCPOLEN_SACK_PERBLOCK));
  377. }
  378. if (tcp_packets_in_flight(tp) == 0)
  379. tcp_ca_event(sk, CA_EVENT_TX_START);
  380. th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  381. skb->h.th = th;
  382. /* Build TCP header and checksum it. */
  383. th->source = inet->sport;
  384. th->dest = inet->dport;
  385. th->seq = htonl(tcb->seq);
  386. th->ack_seq = htonl(tp->rcv_nxt);
  387. *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
  388. tcb->flags);
  389. if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
  390. /* RFC1323: The window in SYN & SYN/ACK segments
  391. * is never scaled.
  392. */
  393. th->window = htons(tp->rcv_wnd);
  394. } else {
  395. th->window = htons(tcp_select_window(sk));
  396. }
  397. th->check = 0;
  398. th->urg_ptr = 0;
  399. if (unlikely(tp->urg_mode &&
  400. between(tp->snd_up, tcb->seq+1, tcb->seq+0xFFFF))) {
  401. th->urg_ptr = htons(tp->snd_up-tcb->seq);
  402. th->urg = 1;
  403. }
  404. if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
  405. tcp_syn_build_options((__be32 *)(th + 1),
  406. tcp_advertise_mss(sk),
  407. (sysctl_flags & SYSCTL_FLAG_TSTAMPS),
  408. (sysctl_flags & SYSCTL_FLAG_SACK),
  409. (sysctl_flags & SYSCTL_FLAG_WSCALE),
  410. tp->rx_opt.rcv_wscale,
  411. tcb->when,
  412. tp->rx_opt.ts_recent);
  413. } else {
  414. tcp_build_and_update_options((__be32 *)(th + 1),
  415. tp, tcb->when);
  416. TCP_ECN_send(sk, tp, skb, tcp_header_size);
  417. }
  418. icsk->icsk_af_ops->send_check(sk, skb->len, skb);
  419. if (likely(tcb->flags & TCPCB_FLAG_ACK))
  420. tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
  421. if (skb->len != tcp_header_size)
  422. tcp_event_data_sent(tp, skb, sk);
  423. if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
  424. TCP_INC_STATS(TCP_MIB_OUTSEGS);
  425. err = icsk->icsk_af_ops->queue_xmit(skb, sk, 0);
  426. if (likely(err <= 0))
  427. return err;
  428. tcp_enter_cwr(sk);
  429. return net_xmit_eval(err);
  430. #undef SYSCTL_FLAG_TSTAMPS
  431. #undef SYSCTL_FLAG_WSCALE
  432. #undef SYSCTL_FLAG_SACK
  433. }
  434. /* This routine just queue's the buffer
  435. *
  436. * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
  437. * otherwise socket can stall.
  438. */
  439. static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
  440. {
  441. struct tcp_sock *tp = tcp_sk(sk);
  442. /* Advance write_seq and place onto the write_queue. */
  443. tp->write_seq = TCP_SKB_CB(skb)->end_seq;
  444. skb_header_release(skb);
  445. __skb_queue_tail(&sk->sk_write_queue, skb);
  446. sk_charge_skb(sk, skb);
  447. /* Queue it, remembering where we must start sending. */
  448. if (sk->sk_send_head == NULL)
  449. sk->sk_send_head = skb;
  450. }
  451. static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
  452. {
  453. if (skb->len <= mss_now || !sk_can_gso(sk)) {
  454. /* Avoid the costly divide in the normal
  455. * non-TSO case.
  456. */
  457. skb_shinfo(skb)->gso_segs = 1;
  458. skb_shinfo(skb)->gso_size = 0;
  459. skb_shinfo(skb)->gso_type = 0;
  460. } else {
  461. unsigned int factor;
  462. factor = skb->len + (mss_now - 1);
  463. factor /= mss_now;
  464. skb_shinfo(skb)->gso_segs = factor;
  465. skb_shinfo(skb)->gso_size = mss_now;
  466. skb_shinfo(skb)->gso_type = sk->sk_gso_type;
  467. }
  468. }
  469. /* Function to create two new TCP segments. Shrinks the given segment
  470. * to the specified size and appends a new segment with the rest of the
  471. * packet to the list. This won't be called frequently, I hope.
  472. * Remember, these are still headerless SKBs at this point.
  473. */
  474. int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len, unsigned int mss_now)
  475. {
  476. struct tcp_sock *tp = tcp_sk(sk);
  477. struct sk_buff *buff;
  478. int nsize, old_factor;
  479. int nlen;
  480. u16 flags;
  481. BUG_ON(len > skb->len);
  482. clear_all_retrans_hints(tp);
  483. nsize = skb_headlen(skb) - len;
  484. if (nsize < 0)
  485. nsize = 0;
  486. if (skb_cloned(skb) &&
  487. skb_is_nonlinear(skb) &&
  488. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  489. return -ENOMEM;
  490. /* Get a new skb... force flag on. */
  491. buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
  492. if (buff == NULL)
  493. return -ENOMEM; /* We'll just try again later. */
  494. sk_charge_skb(sk, buff);
  495. nlen = skb->len - len - nsize;
  496. buff->truesize += nlen;
  497. skb->truesize -= nlen;
  498. /* Correct the sequence numbers. */
  499. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  500. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  501. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  502. /* PSH and FIN should only be set in the second packet. */
  503. flags = TCP_SKB_CB(skb)->flags;
  504. TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  505. TCP_SKB_CB(buff)->flags = flags;
  506. TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
  507. TCP_SKB_CB(skb)->sacked &= ~TCPCB_AT_TAIL;
  508. if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
  509. /* Copy and checksum data tail into the new buffer. */
  510. buff->csum = csum_partial_copy_nocheck(skb->data + len, skb_put(buff, nsize),
  511. nsize, 0);
  512. skb_trim(skb, len);
  513. skb->csum = csum_block_sub(skb->csum, buff->csum, len);
  514. } else {
  515. skb->ip_summed = CHECKSUM_PARTIAL;
  516. skb_split(skb, buff, len);
  517. }
  518. buff->ip_summed = skb->ip_summed;
  519. /* Looks stupid, but our code really uses when of
  520. * skbs, which it never sent before. --ANK
  521. */
  522. TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
  523. buff->tstamp = skb->tstamp;
  524. old_factor = tcp_skb_pcount(skb);
  525. /* Fix up tso_factor for both original and new SKB. */
  526. tcp_set_skb_tso_segs(sk, skb, mss_now);
  527. tcp_set_skb_tso_segs(sk, buff, mss_now);
  528. /* If this packet has been sent out already, we must
  529. * adjust the various packet counters.
  530. */
  531. if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
  532. int diff = old_factor - tcp_skb_pcount(skb) -
  533. tcp_skb_pcount(buff);
  534. tp->packets_out -= diff;
  535. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
  536. tp->sacked_out -= diff;
  537. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
  538. tp->retrans_out -= diff;
  539. if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
  540. tp->lost_out -= diff;
  541. tp->left_out -= diff;
  542. }
  543. if (diff > 0) {
  544. /* Adjust Reno SACK estimate. */
  545. if (!tp->rx_opt.sack_ok) {
  546. tp->sacked_out -= diff;
  547. if ((int)tp->sacked_out < 0)
  548. tp->sacked_out = 0;
  549. tcp_sync_left_out(tp);
  550. }
  551. tp->fackets_out -= diff;
  552. if ((int)tp->fackets_out < 0)
  553. tp->fackets_out = 0;
  554. }
  555. }
  556. /* Link BUFF into the send queue. */
  557. skb_header_release(buff);
  558. __skb_append(skb, buff, &sk->sk_write_queue);
  559. return 0;
  560. }
  561. /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
  562. * eventually). The difference is that pulled data not copied, but
  563. * immediately discarded.
  564. */
  565. static void __pskb_trim_head(struct sk_buff *skb, int len)
  566. {
  567. int i, k, eat;
  568. eat = len;
  569. k = 0;
  570. for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
  571. if (skb_shinfo(skb)->frags[i].size <= eat) {
  572. put_page(skb_shinfo(skb)->frags[i].page);
  573. eat -= skb_shinfo(skb)->frags[i].size;
  574. } else {
  575. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  576. if (eat) {
  577. skb_shinfo(skb)->frags[k].page_offset += eat;
  578. skb_shinfo(skb)->frags[k].size -= eat;
  579. eat = 0;
  580. }
  581. k++;
  582. }
  583. }
  584. skb_shinfo(skb)->nr_frags = k;
  585. skb->tail = skb->data;
  586. skb->data_len -= len;
  587. skb->len = skb->data_len;
  588. }
  589. int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
  590. {
  591. if (skb_cloned(skb) &&
  592. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  593. return -ENOMEM;
  594. /* If len == headlen, we avoid __skb_pull to preserve alignment. */
  595. if (unlikely(len < skb_headlen(skb)))
  596. __skb_pull(skb, len);
  597. else
  598. __pskb_trim_head(skb, len - skb_headlen(skb));
  599. TCP_SKB_CB(skb)->seq += len;
  600. skb->ip_summed = CHECKSUM_PARTIAL;
  601. skb->truesize -= len;
  602. sk->sk_wmem_queued -= len;
  603. sk->sk_forward_alloc += len;
  604. sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
  605. /* Any change of skb->len requires recalculation of tso
  606. * factor and mss.
  607. */
  608. if (tcp_skb_pcount(skb) > 1)
  609. tcp_set_skb_tso_segs(sk, skb, tcp_current_mss(sk, 1));
  610. return 0;
  611. }
  612. /* Not accounting for SACKs here. */
  613. int tcp_mtu_to_mss(struct sock *sk, int pmtu)
  614. {
  615. struct tcp_sock *tp = tcp_sk(sk);
  616. struct inet_connection_sock *icsk = inet_csk(sk);
  617. int mss_now;
  618. /* Calculate base mss without TCP options:
  619. It is MMS_S - sizeof(tcphdr) of rfc1122
  620. */
  621. mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
  622. /* Clamp it (mss_clamp does not include tcp options) */
  623. if (mss_now > tp->rx_opt.mss_clamp)
  624. mss_now = tp->rx_opt.mss_clamp;
  625. /* Now subtract optional transport overhead */
  626. mss_now -= icsk->icsk_ext_hdr_len;
  627. /* Then reserve room for full set of TCP options and 8 bytes of data */
  628. if (mss_now < 48)
  629. mss_now = 48;
  630. /* Now subtract TCP options size, not including SACKs */
  631. mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
  632. return mss_now;
  633. }
  634. /* Inverse of above */
  635. int tcp_mss_to_mtu(struct sock *sk, int mss)
  636. {
  637. struct tcp_sock *tp = tcp_sk(sk);
  638. struct inet_connection_sock *icsk = inet_csk(sk);
  639. int mtu;
  640. mtu = mss +
  641. tp->tcp_header_len +
  642. icsk->icsk_ext_hdr_len +
  643. icsk->icsk_af_ops->net_header_len;
  644. return mtu;
  645. }
  646. void tcp_mtup_init(struct sock *sk)
  647. {
  648. struct tcp_sock *tp = tcp_sk(sk);
  649. struct inet_connection_sock *icsk = inet_csk(sk);
  650. icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
  651. icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
  652. icsk->icsk_af_ops->net_header_len;
  653. icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
  654. icsk->icsk_mtup.probe_size = 0;
  655. }
  656. /* This function synchronize snd mss to current pmtu/exthdr set.
  657. tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
  658. for TCP options, but includes only bare TCP header.
  659. tp->rx_opt.mss_clamp is mss negotiated at connection setup.
  660. It is minimum of user_mss and mss received with SYN.
  661. It also does not include TCP options.
  662. inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
  663. tp->mss_cache is current effective sending mss, including
  664. all tcp options except for SACKs. It is evaluated,
  665. taking into account current pmtu, but never exceeds
  666. tp->rx_opt.mss_clamp.
  667. NOTE1. rfc1122 clearly states that advertised MSS
  668. DOES NOT include either tcp or ip options.
  669. NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
  670. are READ ONLY outside this function. --ANK (980731)
  671. */
  672. unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
  673. {
  674. struct tcp_sock *tp = tcp_sk(sk);
  675. struct inet_connection_sock *icsk = inet_csk(sk);
  676. int mss_now;
  677. if (icsk->icsk_mtup.search_high > pmtu)
  678. icsk->icsk_mtup.search_high = pmtu;
  679. mss_now = tcp_mtu_to_mss(sk, pmtu);
  680. /* Bound mss with half of window */
  681. if (tp->max_window && mss_now > (tp->max_window>>1))
  682. mss_now = max((tp->max_window>>1), 68U - tp->tcp_header_len);
  683. /* And store cached results */
  684. icsk->icsk_pmtu_cookie = pmtu;
  685. if (icsk->icsk_mtup.enabled)
  686. mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
  687. tp->mss_cache = mss_now;
  688. return mss_now;
  689. }
  690. /* Compute the current effective MSS, taking SACKs and IP options,
  691. * and even PMTU discovery events into account.
  692. *
  693. * LARGESEND note: !urg_mode is overkill, only frames up to snd_up
  694. * cannot be large. However, taking into account rare use of URG, this
  695. * is not a big flaw.
  696. */
  697. unsigned int tcp_current_mss(struct sock *sk, int large_allowed)
  698. {
  699. struct tcp_sock *tp = tcp_sk(sk);
  700. struct dst_entry *dst = __sk_dst_get(sk);
  701. u32 mss_now;
  702. u16 xmit_size_goal;
  703. int doing_tso = 0;
  704. mss_now = tp->mss_cache;
  705. if (large_allowed && sk_can_gso(sk) && !tp->urg_mode)
  706. doing_tso = 1;
  707. if (dst) {
  708. u32 mtu = dst_mtu(dst);
  709. if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
  710. mss_now = tcp_sync_mss(sk, mtu);
  711. }
  712. if (tp->rx_opt.eff_sacks)
  713. mss_now -= (TCPOLEN_SACK_BASE_ALIGNED +
  714. (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
  715. xmit_size_goal = mss_now;
  716. if (doing_tso) {
  717. xmit_size_goal = (65535 -
  718. inet_csk(sk)->icsk_af_ops->net_header_len -
  719. inet_csk(sk)->icsk_ext_hdr_len -
  720. tp->tcp_header_len);
  721. if (tp->max_window &&
  722. (xmit_size_goal > (tp->max_window >> 1)))
  723. xmit_size_goal = max((tp->max_window >> 1),
  724. 68U - tp->tcp_header_len);
  725. xmit_size_goal -= (xmit_size_goal % mss_now);
  726. }
  727. tp->xmit_size_goal = xmit_size_goal;
  728. return mss_now;
  729. }
  730. /* Congestion window validation. (RFC2861) */
  731. static void tcp_cwnd_validate(struct sock *sk, struct tcp_sock *tp)
  732. {
  733. __u32 packets_out = tp->packets_out;
  734. if (packets_out >= tp->snd_cwnd) {
  735. /* Network is feed fully. */
  736. tp->snd_cwnd_used = 0;
  737. tp->snd_cwnd_stamp = tcp_time_stamp;
  738. } else {
  739. /* Network starves. */
  740. if (tp->packets_out > tp->snd_cwnd_used)
  741. tp->snd_cwnd_used = tp->packets_out;
  742. if ((s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
  743. tcp_cwnd_application_limited(sk);
  744. }
  745. }
  746. static unsigned int tcp_window_allows(struct tcp_sock *tp, struct sk_buff *skb, unsigned int mss_now, unsigned int cwnd)
  747. {
  748. u32 window, cwnd_len;
  749. window = (tp->snd_una + tp->snd_wnd - TCP_SKB_CB(skb)->seq);
  750. cwnd_len = mss_now * cwnd;
  751. return min(window, cwnd_len);
  752. }
  753. /* Can at least one segment of SKB be sent right now, according to the
  754. * congestion window rules? If so, return how many segments are allowed.
  755. */
  756. static inline unsigned int tcp_cwnd_test(struct tcp_sock *tp, struct sk_buff *skb)
  757. {
  758. u32 in_flight, cwnd;
  759. /* Don't be strict about the congestion window for the final FIN. */
  760. if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
  761. return 1;
  762. in_flight = tcp_packets_in_flight(tp);
  763. cwnd = tp->snd_cwnd;
  764. if (in_flight < cwnd)
  765. return (cwnd - in_flight);
  766. return 0;
  767. }
  768. /* This must be invoked the first time we consider transmitting
  769. * SKB onto the wire.
  770. */
  771. static int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
  772. {
  773. int tso_segs = tcp_skb_pcount(skb);
  774. if (!tso_segs ||
  775. (tso_segs > 1 &&
  776. tcp_skb_mss(skb) != mss_now)) {
  777. tcp_set_skb_tso_segs(sk, skb, mss_now);
  778. tso_segs = tcp_skb_pcount(skb);
  779. }
  780. return tso_segs;
  781. }
  782. static inline int tcp_minshall_check(const struct tcp_sock *tp)
  783. {
  784. return after(tp->snd_sml,tp->snd_una) &&
  785. !after(tp->snd_sml, tp->snd_nxt);
  786. }
  787. /* Return 0, if packet can be sent now without violation Nagle's rules:
  788. * 1. It is full sized.
  789. * 2. Or it contains FIN. (already checked by caller)
  790. * 3. Or TCP_NODELAY was set.
  791. * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
  792. * With Minshall's modification: all sent small packets are ACKed.
  793. */
  794. static inline int tcp_nagle_check(const struct tcp_sock *tp,
  795. const struct sk_buff *skb,
  796. unsigned mss_now, int nonagle)
  797. {
  798. return (skb->len < mss_now &&
  799. ((nonagle&TCP_NAGLE_CORK) ||
  800. (!nonagle &&
  801. tp->packets_out &&
  802. tcp_minshall_check(tp))));
  803. }
  804. /* Return non-zero if the Nagle test allows this packet to be
  805. * sent now.
  806. */
  807. static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
  808. unsigned int cur_mss, int nonagle)
  809. {
  810. /* Nagle rule does not apply to frames, which sit in the middle of the
  811. * write_queue (they have no chances to get new data).
  812. *
  813. * This is implemented in the callers, where they modify the 'nonagle'
  814. * argument based upon the location of SKB in the send queue.
  815. */
  816. if (nonagle & TCP_NAGLE_PUSH)
  817. return 1;
  818. /* Don't use the nagle rule for urgent data (or for the final FIN). */
  819. if (tp->urg_mode ||
  820. (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
  821. return 1;
  822. if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
  823. return 1;
  824. return 0;
  825. }
  826. /* Does at least the first segment of SKB fit into the send window? */
  827. static inline int tcp_snd_wnd_test(struct tcp_sock *tp, struct sk_buff *skb, unsigned int cur_mss)
  828. {
  829. u32 end_seq = TCP_SKB_CB(skb)->end_seq;
  830. if (skb->len > cur_mss)
  831. end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
  832. return !after(end_seq, tp->snd_una + tp->snd_wnd);
  833. }
  834. /* This checks if the data bearing packet SKB (usually sk->sk_send_head)
  835. * should be put on the wire right now. If so, it returns the number of
  836. * packets allowed by the congestion window.
  837. */
  838. static unsigned int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
  839. unsigned int cur_mss, int nonagle)
  840. {
  841. struct tcp_sock *tp = tcp_sk(sk);
  842. unsigned int cwnd_quota;
  843. tcp_init_tso_segs(sk, skb, cur_mss);
  844. if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
  845. return 0;
  846. cwnd_quota = tcp_cwnd_test(tp, skb);
  847. if (cwnd_quota &&
  848. !tcp_snd_wnd_test(tp, skb, cur_mss))
  849. cwnd_quota = 0;
  850. return cwnd_quota;
  851. }
  852. static inline int tcp_skb_is_last(const struct sock *sk,
  853. const struct sk_buff *skb)
  854. {
  855. return skb->next == (struct sk_buff *)&sk->sk_write_queue;
  856. }
  857. int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp)
  858. {
  859. struct sk_buff *skb = sk->sk_send_head;
  860. return (skb &&
  861. tcp_snd_test(sk, skb, tcp_current_mss(sk, 1),
  862. (tcp_skb_is_last(sk, skb) ?
  863. TCP_NAGLE_PUSH :
  864. tp->nonagle)));
  865. }
  866. /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
  867. * which is put after SKB on the list. It is very much like
  868. * tcp_fragment() except that it may make several kinds of assumptions
  869. * in order to speed up the splitting operation. In particular, we
  870. * know that all the data is in scatter-gather pages, and that the
  871. * packet has never been sent out before (and thus is not cloned).
  872. */
  873. static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, unsigned int mss_now)
  874. {
  875. struct sk_buff *buff;
  876. int nlen = skb->len - len;
  877. u16 flags;
  878. /* All of a TSO frame must be composed of paged data. */
  879. if (skb->len != skb->data_len)
  880. return tcp_fragment(sk, skb, len, mss_now);
  881. buff = sk_stream_alloc_pskb(sk, 0, 0, GFP_ATOMIC);
  882. if (unlikely(buff == NULL))
  883. return -ENOMEM;
  884. sk_charge_skb(sk, buff);
  885. buff->truesize += nlen;
  886. skb->truesize -= nlen;
  887. /* Correct the sequence numbers. */
  888. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  889. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  890. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  891. /* PSH and FIN should only be set in the second packet. */
  892. flags = TCP_SKB_CB(skb)->flags;
  893. TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  894. TCP_SKB_CB(buff)->flags = flags;
  895. /* This packet was never sent out yet, so no SACK bits. */
  896. TCP_SKB_CB(buff)->sacked = 0;
  897. buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
  898. skb_split(skb, buff, len);
  899. /* Fix up tso_factor for both original and new SKB. */
  900. tcp_set_skb_tso_segs(sk, skb, mss_now);
  901. tcp_set_skb_tso_segs(sk, buff, mss_now);
  902. /* Link BUFF into the send queue. */
  903. skb_header_release(buff);
  904. __skb_append(skb, buff, &sk->sk_write_queue);
  905. return 0;
  906. }
  907. /* Try to defer sending, if possible, in order to minimize the amount
  908. * of TSO splitting we do. View it as a kind of TSO Nagle test.
  909. *
  910. * This algorithm is from John Heffner.
  911. */
  912. static int tcp_tso_should_defer(struct sock *sk, struct tcp_sock *tp, struct sk_buff *skb)
  913. {
  914. const struct inet_connection_sock *icsk = inet_csk(sk);
  915. u32 send_win, cong_win, limit, in_flight;
  916. if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
  917. goto send_now;
  918. if (icsk->icsk_ca_state != TCP_CA_Open)
  919. goto send_now;
  920. /* Defer for less than two clock ticks. */
  921. if (!tp->tso_deferred && ((jiffies<<1)>>1) - (tp->tso_deferred>>1) > 1)
  922. goto send_now;
  923. in_flight = tcp_packets_in_flight(tp);
  924. BUG_ON(tcp_skb_pcount(skb) <= 1 ||
  925. (tp->snd_cwnd <= in_flight));
  926. send_win = (tp->snd_una + tp->snd_wnd) - TCP_SKB_CB(skb)->seq;
  927. /* From in_flight test above, we know that cwnd > in_flight. */
  928. cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
  929. limit = min(send_win, cong_win);
  930. /* If a full-sized TSO skb can be sent, do it. */
  931. if (limit >= 65536)
  932. goto send_now;
  933. if (sysctl_tcp_tso_win_divisor) {
  934. u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
  935. /* If at least some fraction of a window is available,
  936. * just use it.
  937. */
  938. chunk /= sysctl_tcp_tso_win_divisor;
  939. if (limit >= chunk)
  940. goto send_now;
  941. } else {
  942. /* Different approach, try not to defer past a single
  943. * ACK. Receiver should ACK every other full sized
  944. * frame, so if we have space for more than 3 frames
  945. * then send now.
  946. */
  947. if (limit > tcp_max_burst(tp) * tp->mss_cache)
  948. goto send_now;
  949. }
  950. /* Ok, it looks like it is advisable to defer. */
  951. tp->tso_deferred = 1 | (jiffies<<1);
  952. return 1;
  953. send_now:
  954. tp->tso_deferred = 0;
  955. return 0;
  956. }
  957. /* Create a new MTU probe if we are ready.
  958. * Returns 0 if we should wait to probe (no cwnd available),
  959. * 1 if a probe was sent,
  960. * -1 otherwise */
  961. static int tcp_mtu_probe(struct sock *sk)
  962. {
  963. struct tcp_sock *tp = tcp_sk(sk);
  964. struct inet_connection_sock *icsk = inet_csk(sk);
  965. struct sk_buff *skb, *nskb, *next;
  966. int len;
  967. int probe_size;
  968. unsigned int pif;
  969. int copy;
  970. int mss_now;
  971. /* Not currently probing/verifying,
  972. * not in recovery,
  973. * have enough cwnd, and
  974. * not SACKing (the variable headers throw things off) */
  975. if (!icsk->icsk_mtup.enabled ||
  976. icsk->icsk_mtup.probe_size ||
  977. inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
  978. tp->snd_cwnd < 11 ||
  979. tp->rx_opt.eff_sacks)
  980. return -1;
  981. /* Very simple search strategy: just double the MSS. */
  982. mss_now = tcp_current_mss(sk, 0);
  983. probe_size = 2*tp->mss_cache;
  984. if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
  985. /* TODO: set timer for probe_converge_event */
  986. return -1;
  987. }
  988. /* Have enough data in the send queue to probe? */
  989. len = 0;
  990. if ((skb = sk->sk_send_head) == NULL)
  991. return -1;
  992. while ((len += skb->len) < probe_size && !tcp_skb_is_last(sk, skb))
  993. skb = skb->next;
  994. if (len < probe_size)
  995. return -1;
  996. /* Receive window check. */
  997. if (after(TCP_SKB_CB(skb)->seq + probe_size, tp->snd_una + tp->snd_wnd)) {
  998. if (tp->snd_wnd < probe_size)
  999. return -1;
  1000. else
  1001. return 0;
  1002. }
  1003. /* Do we need to wait to drain cwnd? */
  1004. pif = tcp_packets_in_flight(tp);
  1005. if (pif + 2 > tp->snd_cwnd) {
  1006. /* With no packets in flight, don't stall. */
  1007. if (pif == 0)
  1008. return -1;
  1009. else
  1010. return 0;
  1011. }
  1012. /* We're allowed to probe. Build it now. */
  1013. if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
  1014. return -1;
  1015. sk_charge_skb(sk, nskb);
  1016. skb = sk->sk_send_head;
  1017. __skb_insert(nskb, skb->prev, skb, &sk->sk_write_queue);
  1018. sk->sk_send_head = nskb;
  1019. TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
  1020. TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
  1021. TCP_SKB_CB(nskb)->flags = TCPCB_FLAG_ACK;
  1022. TCP_SKB_CB(nskb)->sacked = 0;
  1023. nskb->csum = 0;
  1024. nskb->ip_summed = skb->ip_summed;
  1025. len = 0;
  1026. while (len < probe_size) {
  1027. next = skb->next;
  1028. copy = min_t(int, skb->len, probe_size - len);
  1029. if (nskb->ip_summed)
  1030. skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
  1031. else
  1032. nskb->csum = skb_copy_and_csum_bits(skb, 0,
  1033. skb_put(nskb, copy), copy, nskb->csum);
  1034. if (skb->len <= copy) {
  1035. /* We've eaten all the data from this skb.
  1036. * Throw it away. */
  1037. TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags;
  1038. __skb_unlink(skb, &sk->sk_write_queue);
  1039. sk_stream_free_skb(sk, skb);
  1040. } else {
  1041. TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags &
  1042. ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  1043. if (!skb_shinfo(skb)->nr_frags) {
  1044. skb_pull(skb, copy);
  1045. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1046. skb->csum = csum_partial(skb->data, skb->len, 0);
  1047. } else {
  1048. __pskb_trim_head(skb, copy);
  1049. tcp_set_skb_tso_segs(sk, skb, mss_now);
  1050. }
  1051. TCP_SKB_CB(skb)->seq += copy;
  1052. }
  1053. len += copy;
  1054. skb = next;
  1055. }
  1056. tcp_init_tso_segs(sk, nskb, nskb->len);
  1057. /* We're ready to send. If this fails, the probe will
  1058. * be resegmented into mss-sized pieces by tcp_write_xmit(). */
  1059. TCP_SKB_CB(nskb)->when = tcp_time_stamp;
  1060. if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
  1061. /* Decrement cwnd here because we are sending
  1062. * effectively two packets. */
  1063. tp->snd_cwnd--;
  1064. update_send_head(sk, tp, nskb);
  1065. icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
  1066. tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
  1067. tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
  1068. return 1;
  1069. }
  1070. return -1;
  1071. }
  1072. /* This routine writes packets to the network. It advances the
  1073. * send_head. This happens as incoming acks open up the remote
  1074. * window for us.
  1075. *
  1076. * Returns 1, if no segments are in flight and we have queued segments, but
  1077. * cannot send anything now because of SWS or another problem.
  1078. */
  1079. static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
  1080. {
  1081. struct tcp_sock *tp = tcp_sk(sk);
  1082. struct sk_buff *skb;
  1083. unsigned int tso_segs, sent_pkts;
  1084. int cwnd_quota;
  1085. int result;
  1086. /* If we are closed, the bytes will have to remain here.
  1087. * In time closedown will finish, we empty the write queue and all
  1088. * will be happy.
  1089. */
  1090. if (unlikely(sk->sk_state == TCP_CLOSE))
  1091. return 0;
  1092. sent_pkts = 0;
  1093. /* Do MTU probing. */
  1094. if ((result = tcp_mtu_probe(sk)) == 0) {
  1095. return 0;
  1096. } else if (result > 0) {
  1097. sent_pkts = 1;
  1098. }
  1099. while ((skb = sk->sk_send_head)) {
  1100. unsigned int limit;
  1101. tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
  1102. BUG_ON(!tso_segs);
  1103. cwnd_quota = tcp_cwnd_test(tp, skb);
  1104. if (!cwnd_quota)
  1105. break;
  1106. if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
  1107. break;
  1108. if (tso_segs == 1) {
  1109. if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
  1110. (tcp_skb_is_last(sk, skb) ?
  1111. nonagle : TCP_NAGLE_PUSH))))
  1112. break;
  1113. } else {
  1114. if (tcp_tso_should_defer(sk, tp, skb))
  1115. break;
  1116. }
  1117. limit = mss_now;
  1118. if (tso_segs > 1) {
  1119. limit = tcp_window_allows(tp, skb,
  1120. mss_now, cwnd_quota);
  1121. if (skb->len < limit) {
  1122. unsigned int trim = skb->len % mss_now;
  1123. if (trim)
  1124. limit = skb->len - trim;
  1125. }
  1126. }
  1127. if (skb->len > limit &&
  1128. unlikely(tso_fragment(sk, skb, limit, mss_now)))
  1129. break;
  1130. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1131. if (unlikely(tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC)))
  1132. break;
  1133. /* Advance the send_head. This one is sent out.
  1134. * This call will increment packets_out.
  1135. */
  1136. update_send_head(sk, tp, skb);
  1137. tcp_minshall_update(tp, mss_now, skb);
  1138. sent_pkts++;
  1139. }
  1140. if (likely(sent_pkts)) {
  1141. tcp_cwnd_validate(sk, tp);
  1142. return 0;
  1143. }
  1144. return !tp->packets_out && sk->sk_send_head;
  1145. }
  1146. /* Push out any pending frames which were held back due to
  1147. * TCP_CORK or attempt at coalescing tiny packets.
  1148. * The socket must be locked by the caller.
  1149. */
  1150. void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
  1151. unsigned int cur_mss, int nonagle)
  1152. {
  1153. struct sk_buff *skb = sk->sk_send_head;
  1154. if (skb) {
  1155. if (tcp_write_xmit(sk, cur_mss, nonagle))
  1156. tcp_check_probe_timer(sk, tp);
  1157. }
  1158. }
  1159. /* Send _single_ skb sitting at the send head. This function requires
  1160. * true push pending frames to setup probe timer etc.
  1161. */
  1162. void tcp_push_one(struct sock *sk, unsigned int mss_now)
  1163. {
  1164. struct tcp_sock *tp = tcp_sk(sk);
  1165. struct sk_buff *skb = sk->sk_send_head;
  1166. unsigned int tso_segs, cwnd_quota;
  1167. BUG_ON(!skb || skb->len < mss_now);
  1168. tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
  1169. cwnd_quota = tcp_snd_test(sk, skb, mss_now, TCP_NAGLE_PUSH);
  1170. if (likely(cwnd_quota)) {
  1171. unsigned int limit;
  1172. BUG_ON(!tso_segs);
  1173. limit = mss_now;
  1174. if (tso_segs > 1) {
  1175. limit = tcp_window_allows(tp, skb,
  1176. mss_now, cwnd_quota);
  1177. if (skb->len < limit) {
  1178. unsigned int trim = skb->len % mss_now;
  1179. if (trim)
  1180. limit = skb->len - trim;
  1181. }
  1182. }
  1183. if (skb->len > limit &&
  1184. unlikely(tso_fragment(sk, skb, limit, mss_now)))
  1185. return;
  1186. /* Send it out now. */
  1187. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1188. if (likely(!tcp_transmit_skb(sk, skb, 1, sk->sk_allocation))) {
  1189. update_send_head(sk, tp, skb);
  1190. tcp_cwnd_validate(sk, tp);
  1191. return;
  1192. }
  1193. }
  1194. }
  1195. /* This function returns the amount that we can raise the
  1196. * usable window based on the following constraints
  1197. *
  1198. * 1. The window can never be shrunk once it is offered (RFC 793)
  1199. * 2. We limit memory per socket
  1200. *
  1201. * RFC 1122:
  1202. * "the suggested [SWS] avoidance algorithm for the receiver is to keep
  1203. * RECV.NEXT + RCV.WIN fixed until:
  1204. * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
  1205. *
  1206. * i.e. don't raise the right edge of the window until you can raise
  1207. * it at least MSS bytes.
  1208. *
  1209. * Unfortunately, the recommended algorithm breaks header prediction,
  1210. * since header prediction assumes th->window stays fixed.
  1211. *
  1212. * Strictly speaking, keeping th->window fixed violates the receiver
  1213. * side SWS prevention criteria. The problem is that under this rule
  1214. * a stream of single byte packets will cause the right side of the
  1215. * window to always advance by a single byte.
  1216. *
  1217. * Of course, if the sender implements sender side SWS prevention
  1218. * then this will not be a problem.
  1219. *
  1220. * BSD seems to make the following compromise:
  1221. *
  1222. * If the free space is less than the 1/4 of the maximum
  1223. * space available and the free space is less than 1/2 mss,
  1224. * then set the window to 0.
  1225. * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
  1226. * Otherwise, just prevent the window from shrinking
  1227. * and from being larger than the largest representable value.
  1228. *
  1229. * This prevents incremental opening of the window in the regime
  1230. * where TCP is limited by the speed of the reader side taking
  1231. * data out of the TCP receive queue. It does nothing about
  1232. * those cases where the window is constrained on the sender side
  1233. * because the pipeline is full.
  1234. *
  1235. * BSD also seems to "accidentally" limit itself to windows that are a
  1236. * multiple of MSS, at least until the free space gets quite small.
  1237. * This would appear to be a side effect of the mbuf implementation.
  1238. * Combining these two algorithms results in the observed behavior
  1239. * of having a fixed window size at almost all times.
  1240. *
  1241. * Below we obtain similar behavior by forcing the offered window to
  1242. * a multiple of the mss when it is feasible to do so.
  1243. *
  1244. * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
  1245. * Regular options like TIMESTAMP are taken into account.
  1246. */
  1247. u32 __tcp_select_window(struct sock *sk)
  1248. {
  1249. struct inet_connection_sock *icsk = inet_csk(sk);
  1250. struct tcp_sock *tp = tcp_sk(sk);
  1251. /* MSS for the peer's data. Previous versions used mss_clamp
  1252. * here. I don't know if the value based on our guesses
  1253. * of peer's MSS is better for the performance. It's more correct
  1254. * but may be worse for the performance because of rcv_mss
  1255. * fluctuations. --SAW 1998/11/1
  1256. */
  1257. int mss = icsk->icsk_ack.rcv_mss;
  1258. int free_space = tcp_space(sk);
  1259. int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
  1260. int window;
  1261. if (mss > full_space)
  1262. mss = full_space;
  1263. if (free_space < full_space/2) {
  1264. icsk->icsk_ack.quick = 0;
  1265. if (tcp_memory_pressure)
  1266. tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U*tp->advmss);
  1267. if (free_space < mss)
  1268. return 0;
  1269. }
  1270. if (free_space > tp->rcv_ssthresh)
  1271. free_space = tp->rcv_ssthresh;
  1272. /* Don't do rounding if we are using window scaling, since the
  1273. * scaled window will not line up with the MSS boundary anyway.
  1274. */
  1275. window = tp->rcv_wnd;
  1276. if (tp->rx_opt.rcv_wscale) {
  1277. window = free_space;
  1278. /* Advertise enough space so that it won't get scaled away.
  1279. * Import case: prevent zero window announcement if
  1280. * 1<<rcv_wscale > mss.
  1281. */
  1282. if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
  1283. window = (((window >> tp->rx_opt.rcv_wscale) + 1)
  1284. << tp->rx_opt.rcv_wscale);
  1285. } else {
  1286. /* Get the largest window that is a nice multiple of mss.
  1287. * Window clamp already applied above.
  1288. * If our current window offering is within 1 mss of the
  1289. * free space we just keep it. This prevents the divide
  1290. * and multiply from happening most of the time.
  1291. * We also don't do any window rounding when the free space
  1292. * is too small.
  1293. */
  1294. if (window <= free_space - mss || window > free_space)
  1295. window = (free_space/mss)*mss;
  1296. }
  1297. return window;
  1298. }
  1299. /* Attempt to collapse two adjacent SKB's during retransmission. */
  1300. static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb, int mss_now)
  1301. {
  1302. struct tcp_sock *tp = tcp_sk(sk);
  1303. struct sk_buff *next_skb = skb->next;
  1304. /* The first test we must make is that neither of these two
  1305. * SKB's are still referenced by someone else.
  1306. */
  1307. if (!skb_cloned(skb) && !skb_cloned(next_skb)) {
  1308. int skb_size = skb->len, next_skb_size = next_skb->len;
  1309. u16 flags = TCP_SKB_CB(skb)->flags;
  1310. /* Also punt if next skb has been SACK'd. */
  1311. if(TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
  1312. return;
  1313. /* Next skb is out of window. */
  1314. if (after(TCP_SKB_CB(next_skb)->end_seq, tp->snd_una+tp->snd_wnd))
  1315. return;
  1316. /* Punt if not enough space exists in the first SKB for
  1317. * the data in the second, or the total combined payload
  1318. * would exceed the MSS.
  1319. */
  1320. if ((next_skb_size > skb_tailroom(skb)) ||
  1321. ((skb_size + next_skb_size) > mss_now))
  1322. return;
  1323. BUG_ON(tcp_skb_pcount(skb) != 1 ||
  1324. tcp_skb_pcount(next_skb) != 1);
  1325. /* changing transmit queue under us so clear hints */
  1326. clear_all_retrans_hints(tp);
  1327. /* Ok. We will be able to collapse the packet. */
  1328. __skb_unlink(next_skb, &sk->sk_write_queue);
  1329. memcpy(skb_put(skb, next_skb_size), next_skb->data, next_skb_size);
  1330. skb->ip_summed = next_skb->ip_summed;
  1331. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1332. skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
  1333. /* Update sequence range on original skb. */
  1334. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
  1335. /* Merge over control information. */
  1336. flags |= TCP_SKB_CB(next_skb)->flags; /* This moves PSH/FIN etc. over */
  1337. TCP_SKB_CB(skb)->flags = flags;
  1338. /* All done, get rid of second SKB and account for it so
  1339. * packet counting does not break.
  1340. */
  1341. TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked&(TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
  1342. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_SACKED_RETRANS)
  1343. tp->retrans_out -= tcp_skb_pcount(next_skb);
  1344. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_LOST) {
  1345. tp->lost_out -= tcp_skb_pcount(next_skb);
  1346. tp->left_out -= tcp_skb_pcount(next_skb);
  1347. }
  1348. /* Reno case is special. Sigh... */
  1349. if (!tp->rx_opt.sack_ok && tp->sacked_out) {
  1350. tcp_dec_pcount_approx(&tp->sacked_out, next_skb);
  1351. tp->left_out -= tcp_skb_pcount(next_skb);
  1352. }
  1353. /* Not quite right: it can be > snd.fack, but
  1354. * it is better to underestimate fackets.
  1355. */
  1356. tcp_dec_pcount_approx(&tp->fackets_out, next_skb);
  1357. tcp_packets_out_dec(tp, next_skb);
  1358. sk_stream_free_skb(sk, next_skb);
  1359. }
  1360. }
  1361. /* Do a simple retransmit without using the backoff mechanisms in
  1362. * tcp_timer. This is used for path mtu discovery.
  1363. * The socket is already locked here.
  1364. */
  1365. void tcp_simple_retransmit(struct sock *sk)
  1366. {
  1367. const struct inet_connection_sock *icsk = inet_csk(sk);
  1368. struct tcp_sock *tp = tcp_sk(sk);
  1369. struct sk_buff *skb;
  1370. unsigned int mss = tcp_current_mss(sk, 0);
  1371. int lost = 0;
  1372. sk_stream_for_retrans_queue(skb, sk) {
  1373. if (skb->len > mss &&
  1374. !(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
  1375. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  1376. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  1377. tp->retrans_out -= tcp_skb_pcount(skb);
  1378. }
  1379. if (!(TCP_SKB_CB(skb)->sacked&TCPCB_LOST)) {
  1380. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  1381. tp->lost_out += tcp_skb_pcount(skb);
  1382. lost = 1;
  1383. }
  1384. }
  1385. }
  1386. clear_all_retrans_hints(tp);
  1387. if (!lost)
  1388. return;
  1389. tcp_sync_left_out(tp);
  1390. /* Don't muck with the congestion window here.
  1391. * Reason is that we do not increase amount of _data_
  1392. * in network, but units changed and effective
  1393. * cwnd/ssthresh really reduced now.
  1394. */
  1395. if (icsk->icsk_ca_state != TCP_CA_Loss) {
  1396. tp->high_seq = tp->snd_nxt;
  1397. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  1398. tp->prior_ssthresh = 0;
  1399. tp->undo_marker = 0;
  1400. tcp_set_ca_state(sk, TCP_CA_Loss);
  1401. }
  1402. tcp_xmit_retransmit_queue(sk);
  1403. }
  1404. /* This retransmits one SKB. Policy decisions and retransmit queue
  1405. * state updates are done by the caller. Returns non-zero if an
  1406. * error occurred which prevented the send.
  1407. */
  1408. int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  1409. {
  1410. struct tcp_sock *tp = tcp_sk(sk);
  1411. struct inet_connection_sock *icsk = inet_csk(sk);
  1412. unsigned int cur_mss = tcp_current_mss(sk, 0);
  1413. int err;
  1414. /* Inconslusive MTU probe */
  1415. if (icsk->icsk_mtup.probe_size) {
  1416. icsk->icsk_mtup.probe_size = 0;
  1417. }
  1418. /* Do not sent more than we queued. 1/4 is reserved for possible
  1419. * copying overhead: fragmentation, tunneling, mangling etc.
  1420. */
  1421. if (atomic_read(&sk->sk_wmem_alloc) >
  1422. min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
  1423. return -EAGAIN;
  1424. if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
  1425. if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
  1426. BUG();
  1427. if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
  1428. return -ENOMEM;
  1429. }
  1430. /* If receiver has shrunk his window, and skb is out of
  1431. * new window, do not retransmit it. The exception is the
  1432. * case, when window is shrunk to zero. In this case
  1433. * our retransmit serves as a zero window probe.
  1434. */
  1435. if (!before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)
  1436. && TCP_SKB_CB(skb)->seq != tp->snd_una)
  1437. return -EAGAIN;
  1438. if (skb->len > cur_mss) {
  1439. if (tcp_fragment(sk, skb, cur_mss, cur_mss))
  1440. return -ENOMEM; /* We'll try again later. */
  1441. }
  1442. /* Collapse two adjacent packets if worthwhile and we can. */
  1443. if(!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
  1444. (skb->len < (cur_mss >> 1)) &&
  1445. (skb->next != sk->sk_send_head) &&
  1446. (skb->next != (struct sk_buff *)&sk->sk_write_queue) &&
  1447. (skb_shinfo(skb)->nr_frags == 0 && skb_shinfo(skb->next)->nr_frags == 0) &&
  1448. (tcp_skb_pcount(skb) == 1 && tcp_skb_pcount(skb->next) == 1) &&
  1449. (sysctl_tcp_retrans_collapse != 0))
  1450. tcp_retrans_try_collapse(sk, skb, cur_mss);
  1451. if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
  1452. return -EHOSTUNREACH; /* Routing failure or similar. */
  1453. /* Some Solaris stacks overoptimize and ignore the FIN on a
  1454. * retransmit when old data is attached. So strip it off
  1455. * since it is cheap to do so and saves bytes on the network.
  1456. */
  1457. if(skb->len > 0 &&
  1458. (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
  1459. tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
  1460. if (!pskb_trim(skb, 0)) {
  1461. TCP_SKB_CB(skb)->seq = TCP_SKB_CB(skb)->end_seq - 1;
  1462. skb_shinfo(skb)->gso_segs = 1;
  1463. skb_shinfo(skb)->gso_size = 0;
  1464. skb_shinfo(skb)->gso_type = 0;
  1465. skb->ip_summed = CHECKSUM_NONE;
  1466. skb->csum = 0;
  1467. }
  1468. }
  1469. /* Make a copy, if the first transmission SKB clone we made
  1470. * is still in somebody's hands, else make a clone.
  1471. */
  1472. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1473. err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  1474. if (err == 0) {
  1475. /* Update global TCP statistics. */
  1476. TCP_INC_STATS(TCP_MIB_RETRANSSEGS);
  1477. tp->total_retrans++;
  1478. #if FASTRETRANS_DEBUG > 0
  1479. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  1480. if (net_ratelimit())
  1481. printk(KERN_DEBUG "retrans_out leaked.\n");
  1482. }
  1483. #endif
  1484. TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
  1485. tp->retrans_out += tcp_skb_pcount(skb);
  1486. /* Save stamp of the first retransmit. */
  1487. if (!tp->retrans_stamp)
  1488. tp->retrans_stamp = TCP_SKB_CB(skb)->when;
  1489. tp->undo_retrans++;
  1490. /* snd_nxt is stored to detect loss of retransmitted segment,
  1491. * see tcp_input.c tcp_sacktag_write_queue().
  1492. */
  1493. TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
  1494. }
  1495. return err;
  1496. }
  1497. /* This gets called after a retransmit timeout, and the initially
  1498. * retransmitted data is acknowledged. It tries to continue
  1499. * resending the rest of the retransmit queue, until either
  1500. * we've sent it all or the congestion window limit is reached.
  1501. * If doing SACK, the first ACK which comes back for a timeout
  1502. * based retransmit packet might feed us FACK information again.
  1503. * If so, we use it to avoid unnecessarily retransmissions.
  1504. */
  1505. void tcp_xmit_retransmit_queue(struct sock *sk)
  1506. {
  1507. const struct inet_connection_sock *icsk = inet_csk(sk);
  1508. struct tcp_sock *tp = tcp_sk(sk);
  1509. struct sk_buff *skb;
  1510. int packet_cnt;
  1511. if (tp->retransmit_skb_hint) {
  1512. skb = tp->retransmit_skb_hint;
  1513. packet_cnt = tp->retransmit_cnt_hint;
  1514. }else{
  1515. skb = sk->sk_write_queue.next;
  1516. packet_cnt = 0;
  1517. }
  1518. /* First pass: retransmit lost packets. */
  1519. if (tp->lost_out) {
  1520. sk_stream_for_retrans_queue_from(skb, sk) {
  1521. __u8 sacked = TCP_SKB_CB(skb)->sacked;
  1522. /* we could do better than to assign each time */
  1523. tp->retransmit_skb_hint = skb;
  1524. tp->retransmit_cnt_hint = packet_cnt;
  1525. /* Assume this retransmit will generate
  1526. * only one packet for congestion window
  1527. * calculation purposes. This works because
  1528. * tcp_retransmit_skb() will chop up the
  1529. * packet to be MSS sized and all the
  1530. * packet counting works out.
  1531. */
  1532. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  1533. return;
  1534. if (sacked & TCPCB_LOST) {
  1535. if (!(sacked&(TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
  1536. if (tcp_retransmit_skb(sk, skb)) {
  1537. tp->retransmit_skb_hint = NULL;
  1538. return;
  1539. }
  1540. if (icsk->icsk_ca_state != TCP_CA_Loss)
  1541. NET_INC_STATS_BH(LINUX_MIB_TCPFASTRETRANS);
  1542. else
  1543. NET_INC_STATS_BH(LINUX_MIB_TCPSLOWSTARTRETRANS);
  1544. if (skb ==
  1545. skb_peek(&sk->sk_write_queue))
  1546. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  1547. inet_csk(sk)->icsk_rto,
  1548. TCP_RTO_MAX);
  1549. }
  1550. packet_cnt += tcp_skb_pcount(skb);
  1551. if (packet_cnt >= tp->lost_out)
  1552. break;
  1553. }
  1554. }
  1555. }
  1556. /* OK, demanded retransmission is finished. */
  1557. /* Forward retransmissions are possible only during Recovery. */
  1558. if (icsk->icsk_ca_state != TCP_CA_Recovery)
  1559. return;
  1560. /* No forward retransmissions in Reno are possible. */
  1561. if (!tp->rx_opt.sack_ok)
  1562. return;
  1563. /* Yeah, we have to make difficult choice between forward transmission
  1564. * and retransmission... Both ways have their merits...
  1565. *
  1566. * For now we do not retransmit anything, while we have some new
  1567. * segments to send.
  1568. */
  1569. if (tcp_may_send_now(sk, tp))
  1570. return;
  1571. if (tp->forward_skb_hint) {
  1572. skb = tp->forward_skb_hint;
  1573. packet_cnt = tp->forward_cnt_hint;
  1574. } else{
  1575. skb = sk->sk_write_queue.next;
  1576. packet_cnt = 0;
  1577. }
  1578. sk_stream_for_retrans_queue_from(skb, sk) {
  1579. tp->forward_cnt_hint = packet_cnt;
  1580. tp->forward_skb_hint = skb;
  1581. /* Similar to the retransmit loop above we
  1582. * can pretend that the retransmitted SKB
  1583. * we send out here will be composed of one
  1584. * real MSS sized packet because tcp_retransmit_skb()
  1585. * will fragment it if necessary.
  1586. */
  1587. if (++packet_cnt > tp->fackets_out)
  1588. break;
  1589. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  1590. break;
  1591. if (TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
  1592. continue;
  1593. /* Ok, retransmit it. */
  1594. if (tcp_retransmit_skb(sk, skb)) {
  1595. tp->forward_skb_hint = NULL;
  1596. break;
  1597. }
  1598. if (skb == skb_peek(&sk->sk_write_queue))
  1599. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  1600. inet_csk(sk)->icsk_rto,
  1601. TCP_RTO_MAX);
  1602. NET_INC_STATS_BH(LINUX_MIB_TCPFORWARDRETRANS);
  1603. }
  1604. }
  1605. /* Send a fin. The caller locks the socket for us. This cannot be
  1606. * allowed to fail queueing a FIN frame under any circumstances.
  1607. */
  1608. void tcp_send_fin(struct sock *sk)
  1609. {
  1610. struct tcp_sock *tp = tcp_sk(sk);
  1611. struct sk_buff *skb = skb_peek_tail(&sk->sk_write_queue);
  1612. int mss_now;
  1613. /* Optimization, tack on the FIN if we have a queue of
  1614. * unsent frames. But be careful about outgoing SACKS
  1615. * and IP options.
  1616. */
  1617. mss_now = tcp_current_mss(sk, 1);
  1618. if (sk->sk_send_head != NULL) {
  1619. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
  1620. TCP_SKB_CB(skb)->end_seq++;
  1621. tp->write_seq++;
  1622. } else {
  1623. /* Socket is locked, keep trying until memory is available. */
  1624. for (;;) {
  1625. skb = alloc_skb_fclone(MAX_TCP_HEADER, GFP_KERNEL);
  1626. if (skb)
  1627. break;
  1628. yield();
  1629. }
  1630. /* Reserve space for headers and prepare control bits. */
  1631. skb_reserve(skb, MAX_TCP_HEADER);
  1632. skb->csum = 0;
  1633. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
  1634. TCP_SKB_CB(skb)->sacked = 0;
  1635. skb_shinfo(skb)->gso_segs = 1;
  1636. skb_shinfo(skb)->gso_size = 0;
  1637. skb_shinfo(skb)->gso_type = 0;
  1638. /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
  1639. TCP_SKB_CB(skb)->seq = tp->write_seq;
  1640. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  1641. tcp_queue_skb(sk, skb);
  1642. }
  1643. __tcp_push_pending_frames(sk, tp, mss_now, TCP_NAGLE_OFF);
  1644. }
  1645. /* We get here when a process closes a file descriptor (either due to
  1646. * an explicit close() or as a byproduct of exit()'ing) and there
  1647. * was unread data in the receive queue. This behavior is recommended
  1648. * by draft-ietf-tcpimpl-prob-03.txt section 3.10. -DaveM
  1649. */
  1650. void tcp_send_active_reset(struct sock *sk, gfp_t priority)
  1651. {
  1652. struct tcp_sock *tp = tcp_sk(sk);
  1653. struct sk_buff *skb;
  1654. /* NOTE: No TCP options attached and we never retransmit this. */
  1655. skb = alloc_skb(MAX_TCP_HEADER, priority);
  1656. if (!skb) {
  1657. NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
  1658. return;
  1659. }
  1660. /* Reserve space for headers and prepare control bits. */
  1661. skb_reserve(skb, MAX_TCP_HEADER);
  1662. skb->csum = 0;
  1663. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
  1664. TCP_SKB_CB(skb)->sacked = 0;
  1665. skb_shinfo(skb)->gso_segs = 1;
  1666. skb_shinfo(skb)->gso_size = 0;
  1667. skb_shinfo(skb)->gso_type = 0;
  1668. /* Send it off. */
  1669. TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk, tp);
  1670. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  1671. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1672. if (tcp_transmit_skb(sk, skb, 0, priority))
  1673. NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
  1674. }
  1675. /* WARNING: This routine must only be called when we have already sent
  1676. * a SYN packet that crossed the incoming SYN that caused this routine
  1677. * to get called. If this assumption fails then the initial rcv_wnd
  1678. * and rcv_wscale values will not be correct.
  1679. */
  1680. int tcp_send_synack(struct sock *sk)
  1681. {
  1682. struct sk_buff* skb;
  1683. skb = skb_peek(&sk->sk_write_queue);
  1684. if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {
  1685. printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");
  1686. return -EFAULT;
  1687. }
  1688. if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {
  1689. if (skb_cloned(skb)) {
  1690. struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
  1691. if (nskb == NULL)
  1692. return -ENOMEM;
  1693. __skb_unlink(skb, &sk->sk_write_queue);
  1694. skb_header_release(nskb);
  1695. __skb_queue_head(&sk->sk_write_queue, nskb);
  1696. sk_stream_free_skb(sk, skb);
  1697. sk_charge_skb(sk, nskb);
  1698. skb = nskb;
  1699. }
  1700. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
  1701. TCP_ECN_send_synack(tcp_sk(sk), skb);
  1702. }
  1703. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1704. return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  1705. }
  1706. /*
  1707. * Prepare a SYN-ACK.
  1708. */
  1709. struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
  1710. struct request_sock *req)
  1711. {
  1712. struct inet_request_sock *ireq = inet_rsk(req);
  1713. struct tcp_sock *tp = tcp_sk(sk);
  1714. struct tcphdr *th;
  1715. int tcp_header_size;
  1716. struct sk_buff *skb;
  1717. skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
  1718. if (skb == NULL)
  1719. return NULL;
  1720. /* Reserve space for headers. */
  1721. skb_reserve(skb, MAX_TCP_HEADER);
  1722. skb->dst = dst_clone(dst);
  1723. tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +
  1724. (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +
  1725. (ireq->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +
  1726. /* SACK_PERM is in the place of NOP NOP of TS */
  1727. ((ireq->sack_ok && !ireq->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));
  1728. skb->h.th = th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  1729. memset(th, 0, sizeof(struct tcphdr));
  1730. th->syn = 1;
  1731. th->ack = 1;
  1732. TCP_ECN_make_synack(req, th);
  1733. th->source = inet_sk(sk)->sport;
  1734. th->dest = ireq->rmt_port;
  1735. TCP_SKB_CB(skb)->seq = tcp_rsk(req)->snt_isn;
  1736. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  1737. TCP_SKB_CB(skb)->sacked = 0;
  1738. skb_shinfo(skb)->gso_segs = 1;
  1739. skb_shinfo(skb)->gso_size = 0;
  1740. skb_shinfo(skb)->gso_type = 0;
  1741. th->seq = htonl(TCP_SKB_CB(skb)->seq);
  1742. th->ack_seq = htonl(tcp_rsk(req)->rcv_isn + 1);
  1743. if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
  1744. __u8 rcv_wscale;
  1745. /* Set this up on the first call only */
  1746. req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
  1747. /* tcp_full_space because it is guaranteed to be the first packet */
  1748. tcp_select_initial_window(tcp_full_space(sk),
  1749. dst_metric(dst, RTAX_ADVMSS) - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
  1750. &req->rcv_wnd,
  1751. &req->window_clamp,
  1752. ireq->wscale_ok,
  1753. &rcv_wscale);
  1754. ireq->rcv_wscale = rcv_wscale;
  1755. }
  1756. /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
  1757. th->window = htons(req->rcv_wnd);
  1758. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1759. tcp_syn_build_options((__be32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok,
  1760. ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale,
  1761. TCP_SKB_CB(skb)->when,
  1762. req->ts_recent);
  1763. skb->csum = 0;
  1764. th->doff = (tcp_header_size >> 2);
  1765. TCP_INC_STATS(TCP_MIB_OUTSEGS);
  1766. return skb;
  1767. }
  1768. /*
  1769. * Do all connect socket setups that can be done AF independent.
  1770. */
  1771. static void tcp_connect_init(struct sock *sk)
  1772. {
  1773. struct dst_entry *dst = __sk_dst_get(sk);
  1774. struct tcp_sock *tp = tcp_sk(sk);
  1775. __u8 rcv_wscale;
  1776. /* We'll fix this up when we get a response from the other end.
  1777. * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
  1778. */
  1779. tp->tcp_header_len = sizeof(struct tcphdr) +
  1780. (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
  1781. /* If user gave his TCP_MAXSEG, record it to clamp */
  1782. if (tp->rx_opt.user_mss)
  1783. tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
  1784. tp->max_window = 0;
  1785. tcp_mtup_init(sk);
  1786. tcp_sync_mss(sk, dst_mtu(dst));
  1787. if (!tp->window_clamp)
  1788. tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
  1789. tp->advmss = dst_metric(dst, RTAX_ADVMSS);
  1790. tcp_initialize_rcv_mss(sk);
  1791. tcp_select_initial_window(tcp_full_space(sk),
  1792. tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
  1793. &tp->rcv_wnd,
  1794. &tp->window_clamp,
  1795. sysctl_tcp_window_scaling,
  1796. &rcv_wscale);
  1797. tp->rx_opt.rcv_wscale = rcv_wscale;
  1798. tp->rcv_ssthresh = tp->rcv_wnd;
  1799. sk->sk_err = 0;
  1800. sock_reset_flag(sk, SOCK_DONE);
  1801. tp->snd_wnd = 0;
  1802. tcp_init_wl(tp, tp->write_seq, 0);
  1803. tp->snd_una = tp->write_seq;
  1804. tp->snd_sml = tp->write_seq;
  1805. tp->rcv_nxt = 0;
  1806. tp->rcv_wup = 0;
  1807. tp->copied_seq = 0;
  1808. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
  1809. inet_csk(sk)->icsk_retransmits = 0;
  1810. tcp_clear_retrans(tp);
  1811. }
  1812. /*
  1813. * Build a SYN and send it off.
  1814. */
  1815. int tcp_connect(struct sock *sk)
  1816. {
  1817. struct tcp_sock *tp = tcp_sk(sk);
  1818. struct sk_buff *buff;
  1819. tcp_connect_init(sk);
  1820. buff = alloc_skb_fclone(MAX_TCP_HEADER + 15, sk->sk_allocation);
  1821. if (unlikely(buff == NULL))
  1822. return -ENOBUFS;
  1823. /* Reserve space for headers. */
  1824. skb_reserve(buff, MAX_TCP_HEADER);
  1825. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;
  1826. TCP_ECN_send_syn(sk, tp, buff);
  1827. TCP_SKB_CB(buff)->sacked = 0;
  1828. skb_shinfo(buff)->gso_segs = 1;
  1829. skb_shinfo(buff)->gso_size = 0;
  1830. skb_shinfo(buff)->gso_type = 0;
  1831. buff->csum = 0;
  1832. tp->snd_nxt = tp->write_seq;
  1833. TCP_SKB_CB(buff)->seq = tp->write_seq++;
  1834. TCP_SKB_CB(buff)->end_seq = tp->write_seq;
  1835. /* Send it off. */
  1836. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1837. tp->retrans_stamp = TCP_SKB_CB(buff)->when;
  1838. skb_header_release(buff);
  1839. __skb_queue_tail(&sk->sk_write_queue, buff);
  1840. sk_charge_skb(sk, buff);
  1841. tp->packets_out += tcp_skb_pcount(buff);
  1842. tcp_transmit_skb(sk, buff, 1, GFP_KERNEL);
  1843. /* We change tp->snd_nxt after the tcp_transmit_skb() call
  1844. * in order to make this packet get counted in tcpOutSegs.
  1845. */
  1846. tp->snd_nxt = tp->write_seq;
  1847. tp->pushed_seq = tp->write_seq;
  1848. TCP_INC_STATS(TCP_MIB_ACTIVEOPENS);
  1849. /* Timer for repeating the SYN until an answer. */
  1850. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  1851. inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
  1852. return 0;
  1853. }
  1854. /* Send out a delayed ack, the caller does the policy checking
  1855. * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
  1856. * for details.
  1857. */
  1858. void tcp_send_delayed_ack(struct sock *sk)
  1859. {
  1860. struct inet_connection_sock *icsk = inet_csk(sk);
  1861. int ato = icsk->icsk_ack.ato;
  1862. unsigned long timeout;
  1863. if (ato > TCP_DELACK_MIN) {
  1864. const struct tcp_sock *tp = tcp_sk(sk);
  1865. int max_ato = HZ/2;
  1866. if (icsk->icsk_ack.pingpong || (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
  1867. max_ato = TCP_DELACK_MAX;
  1868. /* Slow path, intersegment interval is "high". */
  1869. /* If some rtt estimate is known, use it to bound delayed ack.
  1870. * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
  1871. * directly.
  1872. */
  1873. if (tp->srtt) {
  1874. int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);
  1875. if (rtt < max_ato)
  1876. max_ato = rtt;
  1877. }
  1878. ato = min(ato, max_ato);
  1879. }
  1880. /* Stay within the limit we were given */
  1881. timeout = jiffies + ato;
  1882. /* Use new timeout only if there wasn't a older one earlier. */
  1883. if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
  1884. /* If delack timer was blocked or is about to expire,
  1885. * send ACK now.
  1886. */
  1887. if (icsk->icsk_ack.blocked ||
  1888. time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
  1889. tcp_send_ack(sk);
  1890. return;
  1891. }
  1892. if (!time_before(timeout, icsk->icsk_ack.timeout))
  1893. timeout = icsk->icsk_ack.timeout;
  1894. }
  1895. icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
  1896. icsk->icsk_ack.timeout = timeout;
  1897. sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
  1898. }
  1899. /* This routine sends an ack and also updates the window. */
  1900. void tcp_send_ack(struct sock *sk)
  1901. {
  1902. /* If we have been reset, we may not send again. */
  1903. if (sk->sk_state != TCP_CLOSE) {
  1904. struct tcp_sock *tp = tcp_sk(sk);
  1905. struct sk_buff *buff;
  1906. /* We are not putting this on the write queue, so
  1907. * tcp_transmit_skb() will set the ownership to this
  1908. * sock.
  1909. */
  1910. buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1911. if (buff == NULL) {
  1912. inet_csk_schedule_ack(sk);
  1913. inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
  1914. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  1915. TCP_DELACK_MAX, TCP_RTO_MAX);
  1916. return;
  1917. }
  1918. /* Reserve space for headers and prepare control bits. */
  1919. skb_reserve(buff, MAX_TCP_HEADER);
  1920. buff->csum = 0;
  1921. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;
  1922. TCP_SKB_CB(buff)->sacked = 0;
  1923. skb_shinfo(buff)->gso_segs = 1;
  1924. skb_shinfo(buff)->gso_size = 0;
  1925. skb_shinfo(buff)->gso_type = 0;
  1926. /* Send it off, this clears delayed acks for us. */
  1927. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk, tp);
  1928. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1929. tcp_transmit_skb(sk, buff, 0, GFP_ATOMIC);
  1930. }
  1931. }
  1932. /* This routine sends a packet with an out of date sequence
  1933. * number. It assumes the other end will try to ack it.
  1934. *
  1935. * Question: what should we make while urgent mode?
  1936. * 4.4BSD forces sending single byte of data. We cannot send
  1937. * out of window data, because we have SND.NXT==SND.MAX...
  1938. *
  1939. * Current solution: to send TWO zero-length segments in urgent mode:
  1940. * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
  1941. * out-of-date with SND.UNA-1 to probe window.
  1942. */
  1943. static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
  1944. {
  1945. struct tcp_sock *tp = tcp_sk(sk);
  1946. struct sk_buff *skb;
  1947. /* We don't queue it, tcp_transmit_skb() sets ownership. */
  1948. skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1949. if (skb == NULL)
  1950. return -1;
  1951. /* Reserve space for headers and set control bits. */
  1952. skb_reserve(skb, MAX_TCP_HEADER);
  1953. skb->csum = 0;
  1954. TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
  1955. TCP_SKB_CB(skb)->sacked = urgent;
  1956. skb_shinfo(skb)->gso_segs = 1;
  1957. skb_shinfo(skb)->gso_size = 0;
  1958. skb_shinfo(skb)->gso_type = 0;
  1959. /* Use a previous sequence. This should cause the other
  1960. * end to send an ack. Don't queue or clone SKB, just
  1961. * send it.
  1962. */
  1963. TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;
  1964. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  1965. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1966. return tcp_transmit_skb(sk, skb, 0, GFP_ATOMIC);
  1967. }
  1968. int tcp_write_wakeup(struct sock *sk)
  1969. {
  1970. if (sk->sk_state != TCP_CLOSE) {
  1971. struct tcp_sock *tp = tcp_sk(sk);
  1972. struct sk_buff *skb;
  1973. if ((skb = sk->sk_send_head) != NULL &&
  1974. before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {
  1975. int err;
  1976. unsigned int mss = tcp_current_mss(sk, 0);
  1977. unsigned int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;
  1978. if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
  1979. tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
  1980. /* We are probing the opening of a window
  1981. * but the window size is != 0
  1982. * must have been a result SWS avoidance ( sender )
  1983. */
  1984. if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
  1985. skb->len > mss) {
  1986. seg_size = min(seg_size, mss);
  1987. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1988. if (tcp_fragment(sk, skb, seg_size, mss))
  1989. return -1;
  1990. } else if (!tcp_skb_pcount(skb))
  1991. tcp_set_skb_tso_segs(sk, skb, mss);
  1992. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1993. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1994. err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  1995. if (!err) {
  1996. update_send_head(sk, tp, skb);
  1997. }
  1998. return err;
  1999. } else {
  2000. if (tp->urg_mode &&
  2001. between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))
  2002. tcp_xmit_probe_skb(sk, TCPCB_URG);
  2003. return tcp_xmit_probe_skb(sk, 0);
  2004. }
  2005. }
  2006. return -1;
  2007. }
  2008. /* A window probe timeout has occurred. If window is not closed send
  2009. * a partial packet else a zero probe.
  2010. */
  2011. void tcp_send_probe0(struct sock *sk)
  2012. {
  2013. struct inet_connection_sock *icsk = inet_csk(sk);
  2014. struct tcp_sock *tp = tcp_sk(sk);
  2015. int err;
  2016. err = tcp_write_wakeup(sk);
  2017. if (tp->packets_out || !sk->sk_send_head) {
  2018. /* Cancel probe timer, if it is not required. */
  2019. icsk->icsk_probes_out = 0;
  2020. icsk->icsk_backoff = 0;
  2021. return;
  2022. }
  2023. if (err <= 0) {
  2024. if (icsk->icsk_backoff < sysctl_tcp_retries2)
  2025. icsk->icsk_backoff++;
  2026. icsk->icsk_probes_out++;
  2027. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  2028. min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
  2029. TCP_RTO_MAX);
  2030. } else {
  2031. /* If packet was not sent due to local congestion,
  2032. * do not backoff and do not remember icsk_probes_out.
  2033. * Let local senders to fight for local resources.
  2034. *
  2035. * Use accumulated backoff yet.
  2036. */
  2037. if (!icsk->icsk_probes_out)
  2038. icsk->icsk_probes_out = 1;
  2039. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  2040. min(icsk->icsk_rto << icsk->icsk_backoff,
  2041. TCP_RESOURCE_PROBE_INTERVAL),
  2042. TCP_RTO_MAX);
  2043. }
  2044. }
  2045. EXPORT_SYMBOL(tcp_connect);
  2046. EXPORT_SYMBOL(tcp_make_synack);
  2047. EXPORT_SYMBOL(tcp_simple_retransmit);
  2048. EXPORT_SYMBOL(tcp_sync_mss);
  2049. EXPORT_SYMBOL(sysctl_tcp_tso_win_divisor);
  2050. EXPORT_SYMBOL(tcp_mtup_init);