outqueue.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel reference Implementation
  8. *
  9. * These functions implement the sctp_outq class. The outqueue handles
  10. * bundling and queueing of outgoing SCTP chunks.
  11. *
  12. * The SCTP reference implementation is free software;
  13. * you can redistribute it and/or modify it under the terms of
  14. * the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2, or (at your option)
  16. * any later version.
  17. *
  18. * The SCTP reference implementation is distributed in the hope that it
  19. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  20. * ************************
  21. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with GNU CC; see the file COPYING. If not, write to
  26. * the Free Software Foundation, 59 Temple Place - Suite 330,
  27. * Boston, MA 02111-1307, USA.
  28. *
  29. * Please send any bug reports or fixes you make to the
  30. * email address(es):
  31. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  32. *
  33. * Or submit a bug report through the following website:
  34. * http://www.sf.net/projects/lksctp
  35. *
  36. * Written or modified by:
  37. * La Monte H.P. Yarroll <piggy@acm.org>
  38. * Karl Knutson <karl@athena.chicago.il.us>
  39. * Perry Melange <pmelange@null.cc.uic.edu>
  40. * Xingang Guo <xingang.guo@intel.com>
  41. * Hui Huang <hui.huang@nokia.com>
  42. * Sridhar Samudrala <sri@us.ibm.com>
  43. * Jon Grimm <jgrimm@us.ibm.com>
  44. *
  45. * Any bugs reported given to us we will try to fix... any fixes shared will
  46. * be incorporated into the next SCTP release.
  47. */
  48. #include <linux/types.h>
  49. #include <linux/list.h> /* For struct list_head */
  50. #include <linux/socket.h>
  51. #include <linux/ip.h>
  52. #include <net/sock.h> /* For skb_set_owner_w */
  53. #include <net/sctp/sctp.h>
  54. #include <net/sctp/sm.h>
  55. /* Declare internal functions here. */
  56. static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
  57. static void sctp_check_transmitted(struct sctp_outq *q,
  58. struct list_head *transmitted_queue,
  59. struct sctp_transport *transport,
  60. struct sctp_sackhdr *sack,
  61. __u32 highest_new_tsn);
  62. static void sctp_mark_missing(struct sctp_outq *q,
  63. struct list_head *transmitted_queue,
  64. struct sctp_transport *transport,
  65. __u32 highest_new_tsn,
  66. int count_of_newacks);
  67. static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
  68. /* Add data to the front of the queue. */
  69. static inline void sctp_outq_head_data(struct sctp_outq *q,
  70. struct sctp_chunk *ch)
  71. {
  72. __skb_queue_head(&q->out, (struct sk_buff *)ch);
  73. q->out_qlen += ch->skb->len;
  74. return;
  75. }
  76. /* Take data from the front of the queue. */
  77. static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
  78. {
  79. struct sctp_chunk *ch;
  80. ch = (struct sctp_chunk *)__skb_dequeue(&q->out);
  81. if (ch)
  82. q->out_qlen -= ch->skb->len;
  83. return ch;
  84. }
  85. /* Add data chunk to the end of the queue. */
  86. static inline void sctp_outq_tail_data(struct sctp_outq *q,
  87. struct sctp_chunk *ch)
  88. {
  89. __skb_queue_tail(&q->out, (struct sk_buff *)ch);
  90. q->out_qlen += ch->skb->len;
  91. return;
  92. }
  93. /*
  94. * SFR-CACC algorithm:
  95. * D) If count_of_newacks is greater than or equal to 2
  96. * and t was not sent to the current primary then the
  97. * sender MUST NOT increment missing report count for t.
  98. */
  99. static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
  100. struct sctp_transport *transport,
  101. int count_of_newacks)
  102. {
  103. if (count_of_newacks >=2 && transport != primary)
  104. return 1;
  105. return 0;
  106. }
  107. /*
  108. * SFR-CACC algorithm:
  109. * F) If count_of_newacks is less than 2, let d be the
  110. * destination to which t was sent. If cacc_saw_newack
  111. * is 0 for destination d, then the sender MUST NOT
  112. * increment missing report count for t.
  113. */
  114. static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
  115. int count_of_newacks)
  116. {
  117. if (count_of_newacks < 2 && !transport->cacc.cacc_saw_newack)
  118. return 1;
  119. return 0;
  120. }
  121. /*
  122. * SFR-CACC algorithm:
  123. * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
  124. * execute steps C, D, F.
  125. *
  126. * C has been implemented in sctp_outq_sack
  127. */
  128. static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
  129. struct sctp_transport *transport,
  130. int count_of_newacks)
  131. {
  132. if (!primary->cacc.cycling_changeover) {
  133. if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
  134. return 1;
  135. if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
  136. return 1;
  137. return 0;
  138. }
  139. return 0;
  140. }
  141. /*
  142. * SFR-CACC algorithm:
  143. * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
  144. * than next_tsn_at_change of the current primary, then
  145. * the sender MUST NOT increment missing report count
  146. * for t.
  147. */
  148. static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
  149. {
  150. if (primary->cacc.cycling_changeover &&
  151. TSN_lt(tsn, primary->cacc.next_tsn_at_change))
  152. return 1;
  153. return 0;
  154. }
  155. /*
  156. * SFR-CACC algorithm:
  157. * 3) If the missing report count for TSN t is to be
  158. * incremented according to [RFC2960] and
  159. * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
  160. * then the sender MUST futher execute steps 3.1 and
  161. * 3.2 to determine if the missing report count for
  162. * TSN t SHOULD NOT be incremented.
  163. *
  164. * 3.3) If 3.1 and 3.2 do not dictate that the missing
  165. * report count for t should not be incremented, then
  166. * the sender SOULD increment missing report count for
  167. * t (according to [RFC2960] and [SCTP_STEWART_2002]).
  168. */
  169. static inline int sctp_cacc_skip(struct sctp_transport *primary,
  170. struct sctp_transport *transport,
  171. int count_of_newacks,
  172. __u32 tsn)
  173. {
  174. if (primary->cacc.changeover_active &&
  175. (sctp_cacc_skip_3_1(primary, transport, count_of_newacks)
  176. || sctp_cacc_skip_3_2(primary, tsn)))
  177. return 1;
  178. return 0;
  179. }
  180. /* Initialize an existing sctp_outq. This does the boring stuff.
  181. * You still need to define handlers if you really want to DO
  182. * something with this structure...
  183. */
  184. void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
  185. {
  186. q->asoc = asoc;
  187. skb_queue_head_init(&q->out);
  188. skb_queue_head_init(&q->control);
  189. INIT_LIST_HEAD(&q->retransmit);
  190. INIT_LIST_HEAD(&q->sacked);
  191. INIT_LIST_HEAD(&q->abandoned);
  192. q->outstanding_bytes = 0;
  193. q->empty = 1;
  194. q->cork = 0;
  195. q->malloced = 0;
  196. q->out_qlen = 0;
  197. }
  198. /* Free the outqueue structure and any related pending chunks.
  199. */
  200. void sctp_outq_teardown(struct sctp_outq *q)
  201. {
  202. struct sctp_transport *transport;
  203. struct list_head *lchunk, *pos, *temp;
  204. struct sctp_chunk *chunk;
  205. /* Throw away unacknowledged chunks. */
  206. list_for_each(pos, &q->asoc->peer.transport_addr_list) {
  207. transport = list_entry(pos, struct sctp_transport, transports);
  208. while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
  209. chunk = list_entry(lchunk, struct sctp_chunk,
  210. transmitted_list);
  211. /* Mark as part of a failed message. */
  212. sctp_chunk_fail(chunk, q->error);
  213. sctp_chunk_free(chunk);
  214. }
  215. }
  216. /* Throw away chunks that have been gap ACKed. */
  217. list_for_each_safe(lchunk, temp, &q->sacked) {
  218. list_del_init(lchunk);
  219. chunk = list_entry(lchunk, struct sctp_chunk,
  220. transmitted_list);
  221. sctp_chunk_fail(chunk, q->error);
  222. sctp_chunk_free(chunk);
  223. }
  224. /* Throw away any chunks in the retransmit queue. */
  225. list_for_each_safe(lchunk, temp, &q->retransmit) {
  226. list_del_init(lchunk);
  227. chunk = list_entry(lchunk, struct sctp_chunk,
  228. transmitted_list);
  229. sctp_chunk_fail(chunk, q->error);
  230. sctp_chunk_free(chunk);
  231. }
  232. /* Throw away any chunks that are in the abandoned queue. */
  233. list_for_each_safe(lchunk, temp, &q->abandoned) {
  234. list_del_init(lchunk);
  235. chunk = list_entry(lchunk, struct sctp_chunk,
  236. transmitted_list);
  237. sctp_chunk_fail(chunk, q->error);
  238. sctp_chunk_free(chunk);
  239. }
  240. /* Throw away any leftover data chunks. */
  241. while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
  242. /* Mark as send failure. */
  243. sctp_chunk_fail(chunk, q->error);
  244. sctp_chunk_free(chunk);
  245. }
  246. q->error = 0;
  247. /* Throw away any leftover control chunks. */
  248. while ((chunk = (struct sctp_chunk *) skb_dequeue(&q->control)) != NULL)
  249. sctp_chunk_free(chunk);
  250. }
  251. /* Free the outqueue structure and any related pending chunks. */
  252. void sctp_outq_free(struct sctp_outq *q)
  253. {
  254. /* Throw away leftover chunks. */
  255. sctp_outq_teardown(q);
  256. /* If we were kmalloc()'d, free the memory. */
  257. if (q->malloced)
  258. kfree(q);
  259. }
  260. /* Put a new chunk in an sctp_outq. */
  261. int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
  262. {
  263. int error = 0;
  264. SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])\n",
  265. q, chunk, chunk && chunk->chunk_hdr ?
  266. sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
  267. : "Illegal Chunk");
  268. /* If it is data, queue it up, otherwise, send it
  269. * immediately.
  270. */
  271. if (SCTP_CID_DATA == chunk->chunk_hdr->type) {
  272. /* Is it OK to queue data chunks? */
  273. /* From 9. Termination of Association
  274. *
  275. * When either endpoint performs a shutdown, the
  276. * association on each peer will stop accepting new
  277. * data from its user and only deliver data in queue
  278. * at the time of sending or receiving the SHUTDOWN
  279. * chunk.
  280. */
  281. switch (q->asoc->state) {
  282. case SCTP_STATE_EMPTY:
  283. case SCTP_STATE_CLOSED:
  284. case SCTP_STATE_SHUTDOWN_PENDING:
  285. case SCTP_STATE_SHUTDOWN_SENT:
  286. case SCTP_STATE_SHUTDOWN_RECEIVED:
  287. case SCTP_STATE_SHUTDOWN_ACK_SENT:
  288. /* Cannot send after transport endpoint shutdown */
  289. error = -ESHUTDOWN;
  290. break;
  291. default:
  292. SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])\n",
  293. q, chunk, chunk && chunk->chunk_hdr ?
  294. sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
  295. : "Illegal Chunk");
  296. sctp_outq_tail_data(q, chunk);
  297. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  298. SCTP_INC_STATS(SCTP_MIB_OUTUNORDERCHUNKS);
  299. else
  300. SCTP_INC_STATS(SCTP_MIB_OUTORDERCHUNKS);
  301. q->empty = 0;
  302. break;
  303. };
  304. } else {
  305. __skb_queue_tail(&q->control, (struct sk_buff *) chunk);
  306. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  307. }
  308. if (error < 0)
  309. return error;
  310. if (!q->cork)
  311. error = sctp_outq_flush(q, 0);
  312. return error;
  313. }
  314. /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
  315. * and the abandoned list are in ascending order.
  316. */
  317. static void sctp_insert_list(struct list_head *head, struct list_head *new)
  318. {
  319. struct list_head *pos;
  320. struct sctp_chunk *nchunk, *lchunk;
  321. __u32 ntsn, ltsn;
  322. int done = 0;
  323. nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
  324. ntsn = ntohl(nchunk->subh.data_hdr->tsn);
  325. list_for_each(pos, head) {
  326. lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
  327. ltsn = ntohl(lchunk->subh.data_hdr->tsn);
  328. if (TSN_lt(ntsn, ltsn)) {
  329. list_add(new, pos->prev);
  330. done = 1;
  331. break;
  332. }
  333. }
  334. if (!done)
  335. list_add_tail(new, head);
  336. }
  337. /* Mark all the eligible packets on a transport for retransmission. */
  338. void sctp_retransmit_mark(struct sctp_outq *q,
  339. struct sctp_transport *transport,
  340. __u8 fast_retransmit)
  341. {
  342. struct list_head *lchunk, *ltemp;
  343. struct sctp_chunk *chunk;
  344. /* Walk through the specified transmitted queue. */
  345. list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
  346. chunk = list_entry(lchunk, struct sctp_chunk,
  347. transmitted_list);
  348. /* If the chunk is abandoned, move it to abandoned list. */
  349. if (sctp_chunk_abandoned(chunk)) {
  350. list_del_init(lchunk);
  351. sctp_insert_list(&q->abandoned, lchunk);
  352. continue;
  353. }
  354. /* If we are doing retransmission due to a fast retransmit,
  355. * only the chunk's that are marked for fast retransmit
  356. * should be added to the retransmit queue. If we are doing
  357. * retransmission due to a timeout or pmtu discovery, only the
  358. * chunks that are not yet acked should be added to the
  359. * retransmit queue.
  360. */
  361. if ((fast_retransmit && chunk->fast_retransmit) ||
  362. (!fast_retransmit && !chunk->tsn_gap_acked)) {
  363. /* RFC 2960 6.2.1 Processing a Received SACK
  364. *
  365. * C) Any time a DATA chunk is marked for
  366. * retransmission (via either T3-rtx timer expiration
  367. * (Section 6.3.3) or via fast retransmit
  368. * (Section 7.2.4)), add the data size of those
  369. * chunks to the rwnd.
  370. */
  371. q->asoc->peer.rwnd += sctp_data_size(chunk);
  372. q->outstanding_bytes -= sctp_data_size(chunk);
  373. transport->flight_size -= sctp_data_size(chunk);
  374. /* sctpimpguide-05 Section 2.8.2
  375. * M5) If a T3-rtx timer expires, the
  376. * 'TSN.Missing.Report' of all affected TSNs is set
  377. * to 0.
  378. */
  379. chunk->tsn_missing_report = 0;
  380. /* If a chunk that is being used for RTT measurement
  381. * has to be retransmitted, we cannot use this chunk
  382. * anymore for RTT measurements. Reset rto_pending so
  383. * that a new RTT measurement is started when a new
  384. * data chunk is sent.
  385. */
  386. if (chunk->rtt_in_progress) {
  387. chunk->rtt_in_progress = 0;
  388. transport->rto_pending = 0;
  389. }
  390. /* Move the chunk to the retransmit queue. The chunks
  391. * on the retransmit queue are always kept in order.
  392. */
  393. list_del_init(lchunk);
  394. sctp_insert_list(&q->retransmit, lchunk);
  395. }
  396. }
  397. SCTP_DEBUG_PRINTK("%s: transport: %p, fast_retransmit: %d, "
  398. "cwnd: %d, ssthresh: %d, flight_size: %d, "
  399. "pba: %d\n", __FUNCTION__,
  400. transport, fast_retransmit,
  401. transport->cwnd, transport->ssthresh,
  402. transport->flight_size,
  403. transport->partial_bytes_acked);
  404. }
  405. /* Mark all the eligible packets on a transport for retransmission and force
  406. * one packet out.
  407. */
  408. void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
  409. sctp_retransmit_reason_t reason)
  410. {
  411. int error = 0;
  412. __u8 fast_retransmit = 0;
  413. switch(reason) {
  414. case SCTP_RTXR_T3_RTX:
  415. sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
  416. /* Update the retran path if the T3-rtx timer has expired for
  417. * the current retran path.
  418. */
  419. if (transport == transport->asoc->peer.retran_path)
  420. sctp_assoc_update_retran_path(transport->asoc);
  421. break;
  422. case SCTP_RTXR_FAST_RTX:
  423. sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
  424. fast_retransmit = 1;
  425. break;
  426. case SCTP_RTXR_PMTUD:
  427. default:
  428. break;
  429. }
  430. sctp_retransmit_mark(q, transport, fast_retransmit);
  431. /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
  432. * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
  433. * following the procedures outlined in C1 - C5.
  434. */
  435. sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
  436. error = sctp_outq_flush(q, /* rtx_timeout */ 1);
  437. if (error)
  438. q->asoc->base.sk->sk_err = -error;
  439. }
  440. /*
  441. * Transmit DATA chunks on the retransmit queue. Upon return from
  442. * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
  443. * need to be transmitted by the caller.
  444. * We assume that pkt->transport has already been set.
  445. *
  446. * The return value is a normal kernel error return value.
  447. */
  448. static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
  449. int rtx_timeout, int *start_timer)
  450. {
  451. struct list_head *lqueue;
  452. struct list_head *lchunk, *lchunk1;
  453. struct sctp_transport *transport = pkt->transport;
  454. sctp_xmit_t status;
  455. struct sctp_chunk *chunk, *chunk1;
  456. struct sctp_association *asoc;
  457. int error = 0;
  458. asoc = q->asoc;
  459. lqueue = &q->retransmit;
  460. /* RFC 2960 6.3.3 Handle T3-rtx Expiration
  461. *
  462. * E3) Determine how many of the earliest (i.e., lowest TSN)
  463. * outstanding DATA chunks for the address for which the
  464. * T3-rtx has expired will fit into a single packet, subject
  465. * to the MTU constraint for the path corresponding to the
  466. * destination transport address to which the retransmission
  467. * is being sent (this may be different from the address for
  468. * which the timer expires [see Section 6.4]). Call this value
  469. * K. Bundle and retransmit those K DATA chunks in a single
  470. * packet to the destination endpoint.
  471. *
  472. * [Just to be painfully clear, if we are retransmitting
  473. * because a timeout just happened, we should send only ONE
  474. * packet of retransmitted data.]
  475. */
  476. lchunk = sctp_list_dequeue(lqueue);
  477. while (lchunk) {
  478. chunk = list_entry(lchunk, struct sctp_chunk,
  479. transmitted_list);
  480. /* Make sure that Gap Acked TSNs are not retransmitted. A
  481. * simple approach is just to move such TSNs out of the
  482. * way and into a 'transmitted' queue and skip to the
  483. * next chunk.
  484. */
  485. if (chunk->tsn_gap_acked) {
  486. list_add_tail(lchunk, &transport->transmitted);
  487. lchunk = sctp_list_dequeue(lqueue);
  488. continue;
  489. }
  490. /* Attempt to append this chunk to the packet. */
  491. status = sctp_packet_append_chunk(pkt, chunk);
  492. switch (status) {
  493. case SCTP_XMIT_PMTU_FULL:
  494. /* Send this packet. */
  495. if ((error = sctp_packet_transmit(pkt)) == 0)
  496. *start_timer = 1;
  497. /* If we are retransmitting, we should only
  498. * send a single packet.
  499. */
  500. if (rtx_timeout) {
  501. list_add(lchunk, lqueue);
  502. lchunk = NULL;
  503. }
  504. /* Bundle lchunk in the next round. */
  505. break;
  506. case SCTP_XMIT_RWND_FULL:
  507. /* Send this packet. */
  508. if ((error = sctp_packet_transmit(pkt)) == 0)
  509. *start_timer = 1;
  510. /* Stop sending DATA as there is no more room
  511. * at the receiver.
  512. */
  513. list_add(lchunk, lqueue);
  514. lchunk = NULL;
  515. break;
  516. case SCTP_XMIT_NAGLE_DELAY:
  517. /* Send this packet. */
  518. if ((error = sctp_packet_transmit(pkt)) == 0)
  519. *start_timer = 1;
  520. /* Stop sending DATA because of nagle delay. */
  521. list_add(lchunk, lqueue);
  522. lchunk = NULL;
  523. break;
  524. default:
  525. /* The append was successful, so add this chunk to
  526. * the transmitted list.
  527. */
  528. list_add_tail(lchunk, &transport->transmitted);
  529. /* Mark the chunk as ineligible for fast retransmit
  530. * after it is retransmitted.
  531. */
  532. chunk->fast_retransmit = 0;
  533. *start_timer = 1;
  534. q->empty = 0;
  535. /* Retrieve a new chunk to bundle. */
  536. lchunk = sctp_list_dequeue(lqueue);
  537. break;
  538. };
  539. /* If we are here due to a retransmit timeout or a fast
  540. * retransmit and if there are any chunks left in the retransmit
  541. * queue that could not fit in the PMTU sized packet, they need * to be marked as ineligible for a subsequent fast retransmit.
  542. */
  543. if (rtx_timeout && !lchunk) {
  544. list_for_each(lchunk1, lqueue) {
  545. chunk1 = list_entry(lchunk1, struct sctp_chunk,
  546. transmitted_list);
  547. chunk1->fast_retransmit = 0;
  548. }
  549. }
  550. }
  551. return error;
  552. }
  553. /* Cork the outqueue so queued chunks are really queued. */
  554. int sctp_outq_uncork(struct sctp_outq *q)
  555. {
  556. int error = 0;
  557. if (q->cork) {
  558. q->cork = 0;
  559. error = sctp_outq_flush(q, 0);
  560. }
  561. return error;
  562. }
  563. /*
  564. * Try to flush an outqueue.
  565. *
  566. * Description: Send everything in q which we legally can, subject to
  567. * congestion limitations.
  568. * * Note: This function can be called from multiple contexts so appropriate
  569. * locking concerns must be made. Today we use the sock lock to protect
  570. * this function.
  571. */
  572. int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
  573. {
  574. struct sctp_packet *packet;
  575. struct sctp_packet singleton;
  576. struct sctp_association *asoc = q->asoc;
  577. __u16 sport = asoc->base.bind_addr.port;
  578. __u16 dport = asoc->peer.port;
  579. __u32 vtag = asoc->peer.i.init_tag;
  580. struct sk_buff_head *queue;
  581. struct sctp_transport *transport = NULL;
  582. struct sctp_transport *new_transport;
  583. struct sctp_chunk *chunk;
  584. sctp_xmit_t status;
  585. int error = 0;
  586. int start_timer = 0;
  587. /* These transports have chunks to send. */
  588. struct list_head transport_list;
  589. struct list_head *ltransport;
  590. INIT_LIST_HEAD(&transport_list);
  591. packet = NULL;
  592. /*
  593. * 6.10 Bundling
  594. * ...
  595. * When bundling control chunks with DATA chunks, an
  596. * endpoint MUST place control chunks first in the outbound
  597. * SCTP packet. The transmitter MUST transmit DATA chunks
  598. * within a SCTP packet in increasing order of TSN.
  599. * ...
  600. */
  601. queue = &q->control;
  602. while ((chunk = (struct sctp_chunk *)skb_dequeue(queue)) != NULL) {
  603. /* Pick the right transport to use. */
  604. new_transport = chunk->transport;
  605. if (!new_transport) {
  606. new_transport = asoc->peer.active_path;
  607. } else if (!new_transport->active) {
  608. /* If the chunk is Heartbeat or Heartbeat Ack,
  609. * send it to chunk->transport, even if it's
  610. * inactive.
  611. *
  612. * 3.3.6 Heartbeat Acknowledgement:
  613. * ...
  614. * A HEARTBEAT ACK is always sent to the source IP
  615. * address of the IP datagram containing the
  616. * HEARTBEAT chunk to which this ack is responding.
  617. * ...
  618. */
  619. if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
  620. chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK)
  621. new_transport = asoc->peer.active_path;
  622. }
  623. /* Are we switching transports?
  624. * Take care of transport locks.
  625. */
  626. if (new_transport != transport) {
  627. transport = new_transport;
  628. if (list_empty(&transport->send_ready)) {
  629. list_add_tail(&transport->send_ready,
  630. &transport_list);
  631. }
  632. packet = &transport->packet;
  633. sctp_packet_config(packet, vtag,
  634. asoc->peer.ecn_capable);
  635. }
  636. switch (chunk->chunk_hdr->type) {
  637. /*
  638. * 6.10 Bundling
  639. * ...
  640. * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
  641. * COMPLETE with any other chunks. [Send them immediately.]
  642. */
  643. case SCTP_CID_INIT:
  644. case SCTP_CID_INIT_ACK:
  645. case SCTP_CID_SHUTDOWN_COMPLETE:
  646. sctp_packet_init(&singleton, transport, sport, dport);
  647. sctp_packet_config(&singleton, vtag, 0);
  648. sctp_packet_append_chunk(&singleton, chunk);
  649. error = sctp_packet_transmit(&singleton);
  650. if (error < 0)
  651. return error;
  652. break;
  653. case SCTP_CID_ABORT:
  654. case SCTP_CID_SACK:
  655. case SCTP_CID_HEARTBEAT:
  656. case SCTP_CID_HEARTBEAT_ACK:
  657. case SCTP_CID_SHUTDOWN:
  658. case SCTP_CID_SHUTDOWN_ACK:
  659. case SCTP_CID_ERROR:
  660. case SCTP_CID_COOKIE_ECHO:
  661. case SCTP_CID_COOKIE_ACK:
  662. case SCTP_CID_ECN_ECNE:
  663. case SCTP_CID_ECN_CWR:
  664. case SCTP_CID_ASCONF:
  665. case SCTP_CID_ASCONF_ACK:
  666. case SCTP_CID_FWD_TSN:
  667. sctp_packet_transmit_chunk(packet, chunk);
  668. break;
  669. default:
  670. /* We built a chunk with an illegal type! */
  671. BUG();
  672. };
  673. }
  674. /* Is it OK to send data chunks? */
  675. switch (asoc->state) {
  676. case SCTP_STATE_COOKIE_ECHOED:
  677. /* Only allow bundling when this packet has a COOKIE-ECHO
  678. * chunk.
  679. */
  680. if (!packet || !packet->has_cookie_echo)
  681. break;
  682. /* fallthru */
  683. case SCTP_STATE_ESTABLISHED:
  684. case SCTP_STATE_SHUTDOWN_PENDING:
  685. case SCTP_STATE_SHUTDOWN_RECEIVED:
  686. /*
  687. * RFC 2960 6.1 Transmission of DATA Chunks
  688. *
  689. * C) When the time comes for the sender to transmit,
  690. * before sending new DATA chunks, the sender MUST
  691. * first transmit any outstanding DATA chunks which
  692. * are marked for retransmission (limited by the
  693. * current cwnd).
  694. */
  695. if (!list_empty(&q->retransmit)) {
  696. if (transport == asoc->peer.retran_path)
  697. goto retran;
  698. /* Switch transports & prepare the packet. */
  699. transport = asoc->peer.retran_path;
  700. if (list_empty(&transport->send_ready)) {
  701. list_add_tail(&transport->send_ready,
  702. &transport_list);
  703. }
  704. packet = &transport->packet;
  705. sctp_packet_config(packet, vtag,
  706. asoc->peer.ecn_capable);
  707. retran:
  708. error = sctp_outq_flush_rtx(q, packet,
  709. rtx_timeout, &start_timer);
  710. if (start_timer)
  711. sctp_transport_reset_timers(transport);
  712. /* This can happen on COOKIE-ECHO resend. Only
  713. * one chunk can get bundled with a COOKIE-ECHO.
  714. */
  715. if (packet->has_cookie_echo)
  716. goto sctp_flush_out;
  717. /* Don't send new data if there is still data
  718. * waiting to retransmit.
  719. */
  720. if (!list_empty(&q->retransmit))
  721. goto sctp_flush_out;
  722. }
  723. /* Finally, transmit new packets. */
  724. start_timer = 0;
  725. queue = &q->out;
  726. while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
  727. /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
  728. * stream identifier.
  729. */
  730. if (chunk->sinfo.sinfo_stream >=
  731. asoc->c.sinit_num_ostreams) {
  732. /* Mark as failed send. */
  733. sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
  734. sctp_chunk_free(chunk);
  735. continue;
  736. }
  737. /* Has this chunk expired? */
  738. if (sctp_chunk_abandoned(chunk)) {
  739. sctp_chunk_fail(chunk, 0);
  740. sctp_chunk_free(chunk);
  741. continue;
  742. }
  743. /* If there is a specified transport, use it.
  744. * Otherwise, we want to use the active path.
  745. */
  746. new_transport = chunk->transport;
  747. if (!new_transport || !new_transport->active)
  748. new_transport = asoc->peer.active_path;
  749. /* Change packets if necessary. */
  750. if (new_transport != transport) {
  751. transport = new_transport;
  752. /* Schedule to have this transport's
  753. * packet flushed.
  754. */
  755. if (list_empty(&transport->send_ready)) {
  756. list_add_tail(&transport->send_ready,
  757. &transport_list);
  758. }
  759. packet = &transport->packet;
  760. sctp_packet_config(packet, vtag,
  761. asoc->peer.ecn_capable);
  762. }
  763. SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
  764. q, chunk,
  765. chunk && chunk->chunk_hdr ?
  766. sctp_cname(SCTP_ST_CHUNK(
  767. chunk->chunk_hdr->type))
  768. : "Illegal Chunk");
  769. SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
  770. "%p skb->users %d.\n",
  771. ntohl(chunk->subh.data_hdr->tsn),
  772. chunk->skb ?chunk->skb->head : NULL,
  773. chunk->skb ?
  774. atomic_read(&chunk->skb->users) : -1);
  775. /* Add the chunk to the packet. */
  776. status = sctp_packet_transmit_chunk(packet, chunk);
  777. switch (status) {
  778. case SCTP_XMIT_PMTU_FULL:
  779. case SCTP_XMIT_RWND_FULL:
  780. case SCTP_XMIT_NAGLE_DELAY:
  781. /* We could not append this chunk, so put
  782. * the chunk back on the output queue.
  783. */
  784. SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
  785. "not transmit TSN: 0x%x, status: %d\n",
  786. ntohl(chunk->subh.data_hdr->tsn),
  787. status);
  788. sctp_outq_head_data(q, chunk);
  789. goto sctp_flush_out;
  790. break;
  791. case SCTP_XMIT_OK:
  792. break;
  793. default:
  794. BUG();
  795. }
  796. /* BUG: We assume that the sctp_packet_transmit()
  797. * call below will succeed all the time and add the
  798. * chunk to the transmitted list and restart the
  799. * timers.
  800. * It is possible that the call can fail under OOM
  801. * conditions.
  802. *
  803. * Is this really a problem? Won't this behave
  804. * like a lost TSN?
  805. */
  806. list_add_tail(&chunk->transmitted_list,
  807. &transport->transmitted);
  808. sctp_transport_reset_timers(transport);
  809. q->empty = 0;
  810. /* Only let one DATA chunk get bundled with a
  811. * COOKIE-ECHO chunk.
  812. */
  813. if (packet->has_cookie_echo)
  814. goto sctp_flush_out;
  815. }
  816. break;
  817. default:
  818. /* Do nothing. */
  819. break;
  820. }
  821. sctp_flush_out:
  822. /* Before returning, examine all the transports touched in
  823. * this call. Right now, we bluntly force clear all the
  824. * transports. Things might change after we implement Nagle.
  825. * But such an examination is still required.
  826. *
  827. * --xguo
  828. */
  829. while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
  830. struct sctp_transport *t = list_entry(ltransport,
  831. struct sctp_transport,
  832. send_ready);
  833. packet = &t->packet;
  834. if (!sctp_packet_empty(packet))
  835. error = sctp_packet_transmit(packet);
  836. }
  837. return error;
  838. }
  839. /* Update unack_data based on the incoming SACK chunk */
  840. static void sctp_sack_update_unack_data(struct sctp_association *assoc,
  841. struct sctp_sackhdr *sack)
  842. {
  843. sctp_sack_variable_t *frags;
  844. __u16 unack_data;
  845. int i;
  846. unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
  847. frags = sack->variable;
  848. for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
  849. unack_data -= ((ntohs(frags[i].gab.end) -
  850. ntohs(frags[i].gab.start) + 1));
  851. }
  852. assoc->unack_data = unack_data;
  853. }
  854. /* Return the highest new tsn that is acknowledged by the given SACK chunk. */
  855. static __u32 sctp_highest_new_tsn(struct sctp_sackhdr *sack,
  856. struct sctp_association *asoc)
  857. {
  858. struct list_head *ltransport, *lchunk;
  859. struct sctp_transport *transport;
  860. struct sctp_chunk *chunk;
  861. __u32 highest_new_tsn, tsn;
  862. struct list_head *transport_list = &asoc->peer.transport_addr_list;
  863. highest_new_tsn = ntohl(sack->cum_tsn_ack);
  864. list_for_each(ltransport, transport_list) {
  865. transport = list_entry(ltransport, struct sctp_transport,
  866. transports);
  867. list_for_each(lchunk, &transport->transmitted) {
  868. chunk = list_entry(lchunk, struct sctp_chunk,
  869. transmitted_list);
  870. tsn = ntohl(chunk->subh.data_hdr->tsn);
  871. if (!chunk->tsn_gap_acked &&
  872. TSN_lt(highest_new_tsn, tsn) &&
  873. sctp_acked(sack, tsn))
  874. highest_new_tsn = tsn;
  875. }
  876. }
  877. return highest_new_tsn;
  878. }
  879. /* This is where we REALLY process a SACK.
  880. *
  881. * Process the SACK against the outqueue. Mostly, this just frees
  882. * things off the transmitted queue.
  883. */
  884. int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
  885. {
  886. struct sctp_association *asoc = q->asoc;
  887. struct sctp_transport *transport;
  888. struct sctp_chunk *tchunk = NULL;
  889. struct list_head *lchunk, *transport_list, *pos, *temp;
  890. sctp_sack_variable_t *frags = sack->variable;
  891. __u32 sack_ctsn, ctsn, tsn;
  892. __u32 highest_tsn, highest_new_tsn;
  893. __u32 sack_a_rwnd;
  894. unsigned outstanding;
  895. struct sctp_transport *primary = asoc->peer.primary_path;
  896. int count_of_newacks = 0;
  897. /* Grab the association's destination address list. */
  898. transport_list = &asoc->peer.transport_addr_list;
  899. sack_ctsn = ntohl(sack->cum_tsn_ack);
  900. /*
  901. * SFR-CACC algorithm:
  902. * On receipt of a SACK the sender SHOULD execute the
  903. * following statements.
  904. *
  905. * 1) If the cumulative ack in the SACK passes next tsn_at_change
  906. * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
  907. * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
  908. * all destinations.
  909. */
  910. if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
  911. primary->cacc.changeover_active = 0;
  912. list_for_each(pos, transport_list) {
  913. transport = list_entry(pos, struct sctp_transport,
  914. transports);
  915. transport->cacc.cycling_changeover = 0;
  916. }
  917. }
  918. /*
  919. * SFR-CACC algorithm:
  920. * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
  921. * is set the receiver of the SACK MUST take the following actions:
  922. *
  923. * A) Initialize the cacc_saw_newack to 0 for all destination
  924. * addresses.
  925. */
  926. if (sack->num_gap_ack_blocks > 0 &&
  927. primary->cacc.changeover_active) {
  928. list_for_each(pos, transport_list) {
  929. transport = list_entry(pos, struct sctp_transport,
  930. transports);
  931. transport->cacc.cacc_saw_newack = 0;
  932. }
  933. }
  934. /* Get the highest TSN in the sack. */
  935. highest_tsn = sack_ctsn;
  936. if (sack->num_gap_ack_blocks)
  937. highest_tsn +=
  938. ntohs(frags[ntohs(sack->num_gap_ack_blocks) - 1].gab.end);
  939. if (TSN_lt(asoc->highest_sacked, highest_tsn)) {
  940. highest_new_tsn = highest_tsn;
  941. asoc->highest_sacked = highest_tsn;
  942. } else {
  943. highest_new_tsn = sctp_highest_new_tsn(sack, asoc);
  944. }
  945. /* Run through the retransmit queue. Credit bytes received
  946. * and free those chunks that we can.
  947. */
  948. sctp_check_transmitted(q, &q->retransmit, NULL, sack, highest_new_tsn);
  949. sctp_mark_missing(q, &q->retransmit, NULL, highest_new_tsn, 0);
  950. /* Run through the transmitted queue.
  951. * Credit bytes received and free those chunks which we can.
  952. *
  953. * This is a MASSIVE candidate for optimization.
  954. */
  955. list_for_each(pos, transport_list) {
  956. transport = list_entry(pos, struct sctp_transport,
  957. transports);
  958. sctp_check_transmitted(q, &transport->transmitted,
  959. transport, sack, highest_new_tsn);
  960. /*
  961. * SFR-CACC algorithm:
  962. * C) Let count_of_newacks be the number of
  963. * destinations for which cacc_saw_newack is set.
  964. */
  965. if (transport->cacc.cacc_saw_newack)
  966. count_of_newacks ++;
  967. }
  968. list_for_each(pos, transport_list) {
  969. transport = list_entry(pos, struct sctp_transport,
  970. transports);
  971. sctp_mark_missing(q, &transport->transmitted, transport,
  972. highest_new_tsn, count_of_newacks);
  973. }
  974. /* Move the Cumulative TSN Ack Point if appropriate. */
  975. if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn))
  976. asoc->ctsn_ack_point = sack_ctsn;
  977. /* Update unack_data field in the assoc. */
  978. sctp_sack_update_unack_data(asoc, sack);
  979. ctsn = asoc->ctsn_ack_point;
  980. /* Throw away stuff rotting on the sack queue. */
  981. list_for_each_safe(lchunk, temp, &q->sacked) {
  982. tchunk = list_entry(lchunk, struct sctp_chunk,
  983. transmitted_list);
  984. tsn = ntohl(tchunk->subh.data_hdr->tsn);
  985. if (TSN_lte(tsn, ctsn))
  986. sctp_chunk_free(tchunk);
  987. }
  988. /* ii) Set rwnd equal to the newly received a_rwnd minus the
  989. * number of bytes still outstanding after processing the
  990. * Cumulative TSN Ack and the Gap Ack Blocks.
  991. */
  992. sack_a_rwnd = ntohl(sack->a_rwnd);
  993. outstanding = q->outstanding_bytes;
  994. if (outstanding < sack_a_rwnd)
  995. sack_a_rwnd -= outstanding;
  996. else
  997. sack_a_rwnd = 0;
  998. asoc->peer.rwnd = sack_a_rwnd;
  999. sctp_generate_fwdtsn(q, sack_ctsn);
  1000. SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
  1001. __FUNCTION__, sack_ctsn);
  1002. SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
  1003. "%p is 0x%x. Adv peer ack point: 0x%x\n",
  1004. __FUNCTION__, asoc, ctsn, asoc->adv_peer_ack_point);
  1005. /* See if all chunks are acked.
  1006. * Make sure the empty queue handler will get run later.
  1007. */
  1008. q->empty = skb_queue_empty(&q->out) && skb_queue_empty(&q->control) &&
  1009. list_empty(&q->retransmit);
  1010. if (!q->empty)
  1011. goto finish;
  1012. list_for_each(pos, transport_list) {
  1013. transport = list_entry(pos, struct sctp_transport,
  1014. transports);
  1015. q->empty = q->empty && list_empty(&transport->transmitted);
  1016. if (!q->empty)
  1017. goto finish;
  1018. }
  1019. SCTP_DEBUG_PRINTK("sack queue is empty.\n");
  1020. finish:
  1021. return q->empty;
  1022. }
  1023. /* Is the outqueue empty? */
  1024. int sctp_outq_is_empty(const struct sctp_outq *q)
  1025. {
  1026. return q->empty;
  1027. }
  1028. /********************************************************************
  1029. * 2nd Level Abstractions
  1030. ********************************************************************/
  1031. /* Go through a transport's transmitted list or the association's retransmit
  1032. * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
  1033. * The retransmit list will not have an associated transport.
  1034. *
  1035. * I added coherent debug information output. --xguo
  1036. *
  1037. * Instead of printing 'sacked' or 'kept' for each TSN on the
  1038. * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
  1039. * KEPT TSN6-TSN7, etc.
  1040. */
  1041. static void sctp_check_transmitted(struct sctp_outq *q,
  1042. struct list_head *transmitted_queue,
  1043. struct sctp_transport *transport,
  1044. struct sctp_sackhdr *sack,
  1045. __u32 highest_new_tsn_in_sack)
  1046. {
  1047. struct list_head *lchunk;
  1048. struct sctp_chunk *tchunk;
  1049. struct list_head tlist;
  1050. __u32 tsn;
  1051. __u32 sack_ctsn;
  1052. __u32 rtt;
  1053. __u8 restart_timer = 0;
  1054. int bytes_acked = 0;
  1055. /* These state variables are for coherent debug output. --xguo */
  1056. #if SCTP_DEBUG
  1057. __u32 dbg_ack_tsn = 0; /* An ACKed TSN range starts here... */
  1058. __u32 dbg_last_ack_tsn = 0; /* ...and finishes here. */
  1059. __u32 dbg_kept_tsn = 0; /* An un-ACKed range starts here... */
  1060. __u32 dbg_last_kept_tsn = 0; /* ...and finishes here. */
  1061. /* 0 : The last TSN was ACKed.
  1062. * 1 : The last TSN was NOT ACKed (i.e. KEPT).
  1063. * -1: We need to initialize.
  1064. */
  1065. int dbg_prt_state = -1;
  1066. #endif /* SCTP_DEBUG */
  1067. sack_ctsn = ntohl(sack->cum_tsn_ack);
  1068. INIT_LIST_HEAD(&tlist);
  1069. /* The while loop will skip empty transmitted queues. */
  1070. while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
  1071. tchunk = list_entry(lchunk, struct sctp_chunk,
  1072. transmitted_list);
  1073. if (sctp_chunk_abandoned(tchunk)) {
  1074. /* Move the chunk to abandoned list. */
  1075. sctp_insert_list(&q->abandoned, lchunk);
  1076. continue;
  1077. }
  1078. tsn = ntohl(tchunk->subh.data_hdr->tsn);
  1079. if (sctp_acked(sack, tsn)) {
  1080. /* If this queue is the retransmit queue, the
  1081. * retransmit timer has already reclaimed
  1082. * the outstanding bytes for this chunk, so only
  1083. * count bytes associated with a transport.
  1084. */
  1085. if (transport) {
  1086. /* If this chunk is being used for RTT
  1087. * measurement, calculate the RTT and update
  1088. * the RTO using this value.
  1089. *
  1090. * 6.3.1 C5) Karn's algorithm: RTT measurements
  1091. * MUST NOT be made using packets that were
  1092. * retransmitted (and thus for which it is
  1093. * ambiguous whether the reply was for the
  1094. * first instance of the packet or a later
  1095. * instance).
  1096. */
  1097. if (!tchunk->tsn_gap_acked &&
  1098. !tchunk->resent &&
  1099. tchunk->rtt_in_progress) {
  1100. rtt = jiffies - tchunk->sent_at;
  1101. sctp_transport_update_rto(transport,
  1102. rtt);
  1103. }
  1104. }
  1105. if (TSN_lte(tsn, sack_ctsn)) {
  1106. /* RFC 2960 6.3.2 Retransmission Timer Rules
  1107. *
  1108. * R3) Whenever a SACK is received
  1109. * that acknowledges the DATA chunk
  1110. * with the earliest outstanding TSN
  1111. * for that address, restart T3-rtx
  1112. * timer for that address with its
  1113. * current RTO.
  1114. */
  1115. restart_timer = 1;
  1116. if (!tchunk->tsn_gap_acked) {
  1117. tchunk->tsn_gap_acked = 1;
  1118. bytes_acked += sctp_data_size(tchunk);
  1119. /*
  1120. * SFR-CACC algorithm:
  1121. * 2) If the SACK contains gap acks
  1122. * and the flag CHANGEOVER_ACTIVE is
  1123. * set the receiver of the SACK MUST
  1124. * take the following action:
  1125. *
  1126. * B) For each TSN t being acked that
  1127. * has not been acked in any SACK so
  1128. * far, set cacc_saw_newack to 1 for
  1129. * the destination that the TSN was
  1130. * sent to.
  1131. */
  1132. if (transport &&
  1133. sack->num_gap_ack_blocks &&
  1134. q->asoc->peer.primary_path->cacc.
  1135. changeover_active)
  1136. transport->cacc.cacc_saw_newack
  1137. = 1;
  1138. }
  1139. list_add_tail(&tchunk->transmitted_list,
  1140. &q->sacked);
  1141. } else {
  1142. /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
  1143. * M2) Each time a SACK arrives reporting
  1144. * 'Stray DATA chunk(s)' record the highest TSN
  1145. * reported as newly acknowledged, call this
  1146. * value 'HighestTSNinSack'. A newly
  1147. * acknowledged DATA chunk is one not
  1148. * previously acknowledged in a SACK.
  1149. *
  1150. * When the SCTP sender of data receives a SACK
  1151. * chunk that acknowledges, for the first time,
  1152. * the receipt of a DATA chunk, all the still
  1153. * unacknowledged DATA chunks whose TSN is
  1154. * older than that newly acknowledged DATA
  1155. * chunk, are qualified as 'Stray DATA chunks'.
  1156. */
  1157. if (!tchunk->tsn_gap_acked) {
  1158. tchunk->tsn_gap_acked = 1;
  1159. bytes_acked += sctp_data_size(tchunk);
  1160. }
  1161. list_add_tail(lchunk, &tlist);
  1162. }
  1163. #if SCTP_DEBUG
  1164. switch (dbg_prt_state) {
  1165. case 0: /* last TSN was ACKed */
  1166. if (dbg_last_ack_tsn + 1 == tsn) {
  1167. /* This TSN belongs to the
  1168. * current ACK range.
  1169. */
  1170. break;
  1171. }
  1172. if (dbg_last_ack_tsn != dbg_ack_tsn) {
  1173. /* Display the end of the
  1174. * current range.
  1175. */
  1176. SCTP_DEBUG_PRINTK("-%08x",
  1177. dbg_last_ack_tsn);
  1178. }
  1179. /* Start a new range. */
  1180. SCTP_DEBUG_PRINTK(",%08x", tsn);
  1181. dbg_ack_tsn = tsn;
  1182. break;
  1183. case 1: /* The last TSN was NOT ACKed. */
  1184. if (dbg_last_kept_tsn != dbg_kept_tsn) {
  1185. /* Display the end of current range. */
  1186. SCTP_DEBUG_PRINTK("-%08x",
  1187. dbg_last_kept_tsn);
  1188. }
  1189. SCTP_DEBUG_PRINTK("\n");
  1190. /* FALL THROUGH... */
  1191. default:
  1192. /* This is the first-ever TSN we examined. */
  1193. /* Start a new range of ACK-ed TSNs. */
  1194. SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
  1195. dbg_prt_state = 0;
  1196. dbg_ack_tsn = tsn;
  1197. };
  1198. dbg_last_ack_tsn = tsn;
  1199. #endif /* SCTP_DEBUG */
  1200. } else {
  1201. if (tchunk->tsn_gap_acked) {
  1202. SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
  1203. "data TSN: 0x%x\n",
  1204. __FUNCTION__,
  1205. tsn);
  1206. tchunk->tsn_gap_acked = 0;
  1207. bytes_acked -= sctp_data_size(tchunk);
  1208. /* RFC 2960 6.3.2 Retransmission Timer Rules
  1209. *
  1210. * R4) Whenever a SACK is received missing a
  1211. * TSN that was previously acknowledged via a
  1212. * Gap Ack Block, start T3-rtx for the
  1213. * destination address to which the DATA
  1214. * chunk was originally
  1215. * transmitted if it is not already running.
  1216. */
  1217. restart_timer = 1;
  1218. }
  1219. list_add_tail(lchunk, &tlist);
  1220. #if SCTP_DEBUG
  1221. /* See the above comments on ACK-ed TSNs. */
  1222. switch (dbg_prt_state) {
  1223. case 1:
  1224. if (dbg_last_kept_tsn + 1 == tsn)
  1225. break;
  1226. if (dbg_last_kept_tsn != dbg_kept_tsn)
  1227. SCTP_DEBUG_PRINTK("-%08x",
  1228. dbg_last_kept_tsn);
  1229. SCTP_DEBUG_PRINTK(",%08x", tsn);
  1230. dbg_kept_tsn = tsn;
  1231. break;
  1232. case 0:
  1233. if (dbg_last_ack_tsn != dbg_ack_tsn)
  1234. SCTP_DEBUG_PRINTK("-%08x",
  1235. dbg_last_ack_tsn);
  1236. SCTP_DEBUG_PRINTK("\n");
  1237. /* FALL THROUGH... */
  1238. default:
  1239. SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
  1240. dbg_prt_state = 1;
  1241. dbg_kept_tsn = tsn;
  1242. };
  1243. dbg_last_kept_tsn = tsn;
  1244. #endif /* SCTP_DEBUG */
  1245. }
  1246. }
  1247. #if SCTP_DEBUG
  1248. /* Finish off the last range, displaying its ending TSN. */
  1249. switch (dbg_prt_state) {
  1250. case 0:
  1251. if (dbg_last_ack_tsn != dbg_ack_tsn) {
  1252. SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_ack_tsn);
  1253. } else {
  1254. SCTP_DEBUG_PRINTK("\n");
  1255. }
  1256. break;
  1257. case 1:
  1258. if (dbg_last_kept_tsn != dbg_kept_tsn) {
  1259. SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_kept_tsn);
  1260. } else {
  1261. SCTP_DEBUG_PRINTK("\n");
  1262. }
  1263. };
  1264. #endif /* SCTP_DEBUG */
  1265. if (transport) {
  1266. if (bytes_acked) {
  1267. /* 8.2. When an outstanding TSN is acknowledged,
  1268. * the endpoint shall clear the error counter of
  1269. * the destination transport address to which the
  1270. * DATA chunk was last sent.
  1271. * The association's overall error counter is
  1272. * also cleared.
  1273. */
  1274. transport->error_count = 0;
  1275. transport->asoc->overall_error_count = 0;
  1276. /* Mark the destination transport address as
  1277. * active if it is not so marked.
  1278. */
  1279. if (!transport->active) {
  1280. sctp_assoc_control_transport(
  1281. transport->asoc,
  1282. transport,
  1283. SCTP_TRANSPORT_UP,
  1284. SCTP_RECEIVED_SACK);
  1285. }
  1286. sctp_transport_raise_cwnd(transport, sack_ctsn,
  1287. bytes_acked);
  1288. transport->flight_size -= bytes_acked;
  1289. q->outstanding_bytes -= bytes_acked;
  1290. } else {
  1291. /* RFC 2960 6.1, sctpimpguide-06 2.15.2
  1292. * When a sender is doing zero window probing, it
  1293. * should not timeout the association if it continues
  1294. * to receive new packets from the receiver. The
  1295. * reason is that the receiver MAY keep its window
  1296. * closed for an indefinite time.
  1297. * A sender is doing zero window probing when the
  1298. * receiver's advertised window is zero, and there is
  1299. * only one data chunk in flight to the receiver.
  1300. */
  1301. if (!q->asoc->peer.rwnd &&
  1302. !list_empty(&tlist) &&
  1303. (sack_ctsn+2 == q->asoc->next_tsn)) {
  1304. SCTP_DEBUG_PRINTK("%s: SACK received for zero "
  1305. "window probe: %u\n",
  1306. __FUNCTION__, sack_ctsn);
  1307. q->asoc->overall_error_count = 0;
  1308. transport->error_count = 0;
  1309. }
  1310. }
  1311. /* RFC 2960 6.3.2 Retransmission Timer Rules
  1312. *
  1313. * R2) Whenever all outstanding data sent to an address have
  1314. * been acknowledged, turn off the T3-rtx timer of that
  1315. * address.
  1316. */
  1317. if (!transport->flight_size) {
  1318. if (timer_pending(&transport->T3_rtx_timer) &&
  1319. del_timer(&transport->T3_rtx_timer)) {
  1320. sctp_transport_put(transport);
  1321. }
  1322. } else if (restart_timer) {
  1323. if (!mod_timer(&transport->T3_rtx_timer,
  1324. jiffies + transport->rto))
  1325. sctp_transport_hold(transport);
  1326. }
  1327. }
  1328. list_splice(&tlist, transmitted_queue);
  1329. }
  1330. /* Mark chunks as missing and consequently may get retransmitted. */
  1331. static void sctp_mark_missing(struct sctp_outq *q,
  1332. struct list_head *transmitted_queue,
  1333. struct sctp_transport *transport,
  1334. __u32 highest_new_tsn_in_sack,
  1335. int count_of_newacks)
  1336. {
  1337. struct sctp_chunk *chunk;
  1338. struct list_head *pos;
  1339. __u32 tsn;
  1340. char do_fast_retransmit = 0;
  1341. struct sctp_transport *primary = q->asoc->peer.primary_path;
  1342. list_for_each(pos, transmitted_queue) {
  1343. chunk = list_entry(pos, struct sctp_chunk, transmitted_list);
  1344. tsn = ntohl(chunk->subh.data_hdr->tsn);
  1345. /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
  1346. * 'Unacknowledged TSN's', if the TSN number of an
  1347. * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
  1348. * value, increment the 'TSN.Missing.Report' count on that
  1349. * chunk if it has NOT been fast retransmitted or marked for
  1350. * fast retransmit already.
  1351. */
  1352. if (!chunk->fast_retransmit &&
  1353. !chunk->tsn_gap_acked &&
  1354. TSN_lt(tsn, highest_new_tsn_in_sack)) {
  1355. /* SFR-CACC may require us to skip marking
  1356. * this chunk as missing.
  1357. */
  1358. if (!transport || !sctp_cacc_skip(primary, transport,
  1359. count_of_newacks, tsn)) {
  1360. chunk->tsn_missing_report++;
  1361. SCTP_DEBUG_PRINTK(
  1362. "%s: TSN 0x%x missing counter: %d\n",
  1363. __FUNCTION__, tsn,
  1364. chunk->tsn_missing_report);
  1365. }
  1366. }
  1367. /*
  1368. * M4) If any DATA chunk is found to have a
  1369. * 'TSN.Missing.Report'
  1370. * value larger than or equal to 4, mark that chunk for
  1371. * retransmission and start the fast retransmit procedure.
  1372. */
  1373. if (chunk->tsn_missing_report >= 4) {
  1374. chunk->fast_retransmit = 1;
  1375. do_fast_retransmit = 1;
  1376. }
  1377. }
  1378. if (transport) {
  1379. if (do_fast_retransmit)
  1380. sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
  1381. SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
  1382. "ssthresh: %d, flight_size: %d, pba: %d\n",
  1383. __FUNCTION__, transport, transport->cwnd,
  1384. transport->ssthresh, transport->flight_size,
  1385. transport->partial_bytes_acked);
  1386. }
  1387. }
  1388. /* Is the given TSN acked by this packet? */
  1389. static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
  1390. {
  1391. int i;
  1392. sctp_sack_variable_t *frags;
  1393. __u16 gap;
  1394. __u32 ctsn = ntohl(sack->cum_tsn_ack);
  1395. if (TSN_lte(tsn, ctsn))
  1396. goto pass;
  1397. /* 3.3.4 Selective Acknowledgement (SACK) (3):
  1398. *
  1399. * Gap Ack Blocks:
  1400. * These fields contain the Gap Ack Blocks. They are repeated
  1401. * for each Gap Ack Block up to the number of Gap Ack Blocks
  1402. * defined in the Number of Gap Ack Blocks field. All DATA
  1403. * chunks with TSNs greater than or equal to (Cumulative TSN
  1404. * Ack + Gap Ack Block Start) and less than or equal to
  1405. * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
  1406. * Block are assumed to have been received correctly.
  1407. */
  1408. frags = sack->variable;
  1409. gap = tsn - ctsn;
  1410. for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
  1411. if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
  1412. TSN_lte(gap, ntohs(frags[i].gab.end)))
  1413. goto pass;
  1414. }
  1415. return 0;
  1416. pass:
  1417. return 1;
  1418. }
  1419. static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
  1420. int nskips, __u16 stream)
  1421. {
  1422. int i;
  1423. for (i = 0; i < nskips; i++) {
  1424. if (skiplist[i].stream == stream)
  1425. return i;
  1426. }
  1427. return i;
  1428. }
  1429. /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
  1430. static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
  1431. {
  1432. struct sctp_association *asoc = q->asoc;
  1433. struct sctp_chunk *ftsn_chunk = NULL;
  1434. struct sctp_fwdtsn_skip ftsn_skip_arr[10];
  1435. int nskips = 0;
  1436. int skip_pos = 0;
  1437. __u32 tsn;
  1438. struct sctp_chunk *chunk;
  1439. struct list_head *lchunk, *temp;
  1440. /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
  1441. * received SACK.
  1442. *
  1443. * If (Advanced.Peer.Ack.Point < SackCumAck), then update
  1444. * Advanced.Peer.Ack.Point to be equal to SackCumAck.
  1445. */
  1446. if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
  1447. asoc->adv_peer_ack_point = ctsn;
  1448. /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
  1449. * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
  1450. * the chunk next in the out-queue space is marked as "abandoned" as
  1451. * shown in the following example:
  1452. *
  1453. * Assuming that a SACK arrived with the Cumulative TSN ACK 102
  1454. * and the Advanced.Peer.Ack.Point is updated to this value:
  1455. *
  1456. * out-queue at the end of ==> out-queue after Adv.Ack.Point
  1457. * normal SACK processing local advancement
  1458. * ... ...
  1459. * Adv.Ack.Pt-> 102 acked 102 acked
  1460. * 103 abandoned 103 abandoned
  1461. * 104 abandoned Adv.Ack.P-> 104 abandoned
  1462. * 105 105
  1463. * 106 acked 106 acked
  1464. * ... ...
  1465. *
  1466. * In this example, the data sender successfully advanced the
  1467. * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
  1468. */
  1469. list_for_each_safe(lchunk, temp, &q->abandoned) {
  1470. chunk = list_entry(lchunk, struct sctp_chunk,
  1471. transmitted_list);
  1472. tsn = ntohl(chunk->subh.data_hdr->tsn);
  1473. /* Remove any chunks in the abandoned queue that are acked by
  1474. * the ctsn.
  1475. */
  1476. if (TSN_lte(tsn, ctsn)) {
  1477. list_del_init(lchunk);
  1478. if (!chunk->tsn_gap_acked) {
  1479. chunk->transport->flight_size -=
  1480. sctp_data_size(chunk);
  1481. q->outstanding_bytes -= sctp_data_size(chunk);
  1482. }
  1483. sctp_chunk_free(chunk);
  1484. } else {
  1485. if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
  1486. asoc->adv_peer_ack_point = tsn;
  1487. if (chunk->chunk_hdr->flags &
  1488. SCTP_DATA_UNORDERED)
  1489. continue;
  1490. skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
  1491. nskips,
  1492. chunk->subh.data_hdr->stream);
  1493. ftsn_skip_arr[skip_pos].stream =
  1494. chunk->subh.data_hdr->stream;
  1495. ftsn_skip_arr[skip_pos].ssn =
  1496. chunk->subh.data_hdr->ssn;
  1497. if (skip_pos == nskips)
  1498. nskips++;
  1499. if (nskips == 10)
  1500. break;
  1501. } else
  1502. break;
  1503. }
  1504. }
  1505. /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
  1506. * is greater than the Cumulative TSN ACK carried in the received
  1507. * SACK, the data sender MUST send the data receiver a FORWARD TSN
  1508. * chunk containing the latest value of the
  1509. * "Advanced.Peer.Ack.Point".
  1510. *
  1511. * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
  1512. * list each stream and sequence number in the forwarded TSN. This
  1513. * information will enable the receiver to easily find any
  1514. * stranded TSN's waiting on stream reorder queues. Each stream
  1515. * SHOULD only be reported once; this means that if multiple
  1516. * abandoned messages occur in the same stream then only the
  1517. * highest abandoned stream sequence number is reported. If the
  1518. * total size of the FORWARD TSN does NOT fit in a single MTU then
  1519. * the sender of the FORWARD TSN SHOULD lower the
  1520. * Advanced.Peer.Ack.Point to the last TSN that will fit in a
  1521. * single MTU.
  1522. */
  1523. if (asoc->adv_peer_ack_point > ctsn)
  1524. ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
  1525. nskips, &ftsn_skip_arr[0]);
  1526. if (ftsn_chunk) {
  1527. __skb_queue_tail(&q->control, (struct sk_buff *)ftsn_chunk);
  1528. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  1529. }
  1530. }