outqueue.c 49 KB

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