outqueue.c 50 KB

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