outqueue.c 49 KB

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