outqueue.c 49 KB

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