SCTP: Fix difference cases of retransmit.
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / sctp / outqueue.c
blob99a3db5d5fae4b1c6f3d500240b654615fbfb043
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.
7 * This file is part of the SCTP kernel reference Implementation
9 * These functions implement the sctp_outq class. The outqueue handles
10 * bundling and queueing of outgoing SCTP chunks.
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.
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.
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.
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
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>
45 * Any bugs reported given to us we will try to fix... any fixes shared will
46 * be incorporated into the next SCTP release.
49 #include <linux/types.h>
50 #include <linux/list.h> /* For struct list_head */
51 #include <linux/socket.h>
52 #include <linux/ip.h>
53 #include <net/sock.h> /* For skb_set_owner_w */
55 #include <net/sctp/sctp.h>
56 #include <net/sctp/sm.h>
58 /* Declare internal functions here. */
59 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
60 static void sctp_check_transmitted(struct sctp_outq *q,
61 struct list_head *transmitted_queue,
62 struct sctp_transport *transport,
63 struct sctp_sackhdr *sack,
64 __u32 highest_new_tsn);
66 static void sctp_mark_missing(struct sctp_outq *q,
67 struct list_head *transmitted_queue,
68 struct sctp_transport *transport,
69 __u32 highest_new_tsn,
70 int count_of_newacks);
72 static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
74 /* Add data to the front of the queue. */
75 static inline void sctp_outq_head_data(struct sctp_outq *q,
76 struct sctp_chunk *ch)
78 list_add(&ch->list, &q->out_chunk_list);
79 q->out_qlen += ch->skb->len;
80 return;
83 /* Take data from the front of the queue. */
84 static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
86 struct sctp_chunk *ch = NULL;
88 if (!list_empty(&q->out_chunk_list)) {
89 struct list_head *entry = q->out_chunk_list.next;
91 ch = list_entry(entry, struct sctp_chunk, list);
92 list_del_init(entry);
93 q->out_qlen -= ch->skb->len;
95 return ch;
97 /* Add data chunk to the end of the queue. */
98 static inline void sctp_outq_tail_data(struct sctp_outq *q,
99 struct sctp_chunk *ch)
101 list_add_tail(&ch->list, &q->out_chunk_list);
102 q->out_qlen += ch->skb->len;
103 return;
107 * SFR-CACC algorithm:
108 * D) If count_of_newacks is greater than or equal to 2
109 * and t was not sent to the current primary then the
110 * sender MUST NOT increment missing report count for t.
112 static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
113 struct sctp_transport *transport,
114 int count_of_newacks)
116 if (count_of_newacks >=2 && transport != primary)
117 return 1;
118 return 0;
122 * SFR-CACC algorithm:
123 * F) If count_of_newacks is less than 2, let d be the
124 * destination to which t was sent. If cacc_saw_newack
125 * is 0 for destination d, then the sender MUST NOT
126 * increment missing report count for t.
128 static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
129 int count_of_newacks)
131 if (count_of_newacks < 2 && !transport->cacc.cacc_saw_newack)
132 return 1;
133 return 0;
137 * SFR-CACC algorithm:
138 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
139 * execute steps C, D, F.
141 * C has been implemented in sctp_outq_sack
143 static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
144 struct sctp_transport *transport,
145 int count_of_newacks)
147 if (!primary->cacc.cycling_changeover) {
148 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
149 return 1;
150 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
151 return 1;
152 return 0;
154 return 0;
158 * SFR-CACC algorithm:
159 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
160 * than next_tsn_at_change of the current primary, then
161 * the sender MUST NOT increment missing report count
162 * for t.
164 static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
166 if (primary->cacc.cycling_changeover &&
167 TSN_lt(tsn, primary->cacc.next_tsn_at_change))
168 return 1;
169 return 0;
173 * SFR-CACC algorithm:
174 * 3) If the missing report count for TSN t is to be
175 * incremented according to [RFC2960] and
176 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
177 * then the sender MUST futher execute steps 3.1 and
178 * 3.2 to determine if the missing report count for
179 * TSN t SHOULD NOT be incremented.
181 * 3.3) If 3.1 and 3.2 do not dictate that the missing
182 * report count for t should not be incremented, then
183 * the sender SOULD increment missing report count for
184 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
186 static inline int sctp_cacc_skip(struct sctp_transport *primary,
187 struct sctp_transport *transport,
188 int count_of_newacks,
189 __u32 tsn)
191 if (primary->cacc.changeover_active &&
192 (sctp_cacc_skip_3_1(primary, transport, count_of_newacks)
193 || sctp_cacc_skip_3_2(primary, tsn)))
194 return 1;
195 return 0;
198 /* Initialize an existing sctp_outq. This does the boring stuff.
199 * You still need to define handlers if you really want to DO
200 * something with this structure...
202 void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
204 q->asoc = asoc;
205 INIT_LIST_HEAD(&q->out_chunk_list);
206 INIT_LIST_HEAD(&q->control_chunk_list);
207 INIT_LIST_HEAD(&q->retransmit);
208 INIT_LIST_HEAD(&q->sacked);
209 INIT_LIST_HEAD(&q->abandoned);
211 q->outstanding_bytes = 0;
212 q->empty = 1;
213 q->cork = 0;
215 q->malloced = 0;
216 q->out_qlen = 0;
219 /* Free the outqueue structure and any related pending chunks.
221 void sctp_outq_teardown(struct sctp_outq *q)
223 struct sctp_transport *transport;
224 struct list_head *lchunk, *pos, *temp;
225 struct sctp_chunk *chunk, *tmp;
227 /* Throw away unacknowledged chunks. */
228 list_for_each(pos, &q->asoc->peer.transport_addr_list) {
229 transport = list_entry(pos, struct sctp_transport, transports);
230 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
231 chunk = list_entry(lchunk, struct sctp_chunk,
232 transmitted_list);
233 /* Mark as part of a failed message. */
234 sctp_chunk_fail(chunk, q->error);
235 sctp_chunk_free(chunk);
239 /* Throw away chunks that have been gap ACKed. */
240 list_for_each_safe(lchunk, temp, &q->sacked) {
241 list_del_init(lchunk);
242 chunk = list_entry(lchunk, struct sctp_chunk,
243 transmitted_list);
244 sctp_chunk_fail(chunk, q->error);
245 sctp_chunk_free(chunk);
248 /* Throw away any chunks in the retransmit queue. */
249 list_for_each_safe(lchunk, temp, &q->retransmit) {
250 list_del_init(lchunk);
251 chunk = list_entry(lchunk, struct sctp_chunk,
252 transmitted_list);
253 sctp_chunk_fail(chunk, q->error);
254 sctp_chunk_free(chunk);
257 /* Throw away any chunks that are in the abandoned queue. */
258 list_for_each_safe(lchunk, temp, &q->abandoned) {
259 list_del_init(lchunk);
260 chunk = list_entry(lchunk, struct sctp_chunk,
261 transmitted_list);
262 sctp_chunk_fail(chunk, q->error);
263 sctp_chunk_free(chunk);
266 /* Throw away any leftover data chunks. */
267 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
269 /* Mark as send failure. */
270 sctp_chunk_fail(chunk, q->error);
271 sctp_chunk_free(chunk);
274 q->error = 0;
276 /* Throw away any leftover control chunks. */
277 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
278 list_del_init(&chunk->list);
279 sctp_chunk_free(chunk);
283 /* Free the outqueue structure and any related pending chunks. */
284 void sctp_outq_free(struct sctp_outq *q)
286 /* Throw away leftover chunks. */
287 sctp_outq_teardown(q);
289 /* If we were kmalloc()'d, free the memory. */
290 if (q->malloced)
291 kfree(q);
294 /* Put a new chunk in an sctp_outq. */
295 int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
297 int error = 0;
299 SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])\n",
300 q, chunk, chunk && chunk->chunk_hdr ?
301 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
302 : "Illegal Chunk");
304 /* If it is data, queue it up, otherwise, send it
305 * immediately.
307 if (SCTP_CID_DATA == chunk->chunk_hdr->type) {
308 /* Is it OK to queue data chunks? */
309 /* From 9. Termination of Association
311 * When either endpoint performs a shutdown, the
312 * association on each peer will stop accepting new
313 * data from its user and only deliver data in queue
314 * at the time of sending or receiving the SHUTDOWN
315 * chunk.
317 switch (q->asoc->state) {
318 case SCTP_STATE_EMPTY:
319 case SCTP_STATE_CLOSED:
320 case SCTP_STATE_SHUTDOWN_PENDING:
321 case SCTP_STATE_SHUTDOWN_SENT:
322 case SCTP_STATE_SHUTDOWN_RECEIVED:
323 case SCTP_STATE_SHUTDOWN_ACK_SENT:
324 /* Cannot send after transport endpoint shutdown */
325 error = -ESHUTDOWN;
326 break;
328 default:
329 SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])\n",
330 q, chunk, chunk && chunk->chunk_hdr ?
331 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
332 : "Illegal Chunk");
334 sctp_outq_tail_data(q, chunk);
335 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
336 SCTP_INC_STATS(SCTP_MIB_OUTUNORDERCHUNKS);
337 else
338 SCTP_INC_STATS(SCTP_MIB_OUTORDERCHUNKS);
339 q->empty = 0;
340 break;
342 } else {
343 list_add_tail(&chunk->list, &q->control_chunk_list);
344 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
347 if (error < 0)
348 return error;
350 if (!q->cork)
351 error = sctp_outq_flush(q, 0);
353 return error;
356 /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
357 * and the abandoned list are in ascending order.
359 static void sctp_insert_list(struct list_head *head, struct list_head *new)
361 struct list_head *pos;
362 struct sctp_chunk *nchunk, *lchunk;
363 __u32 ntsn, ltsn;
364 int done = 0;
366 nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
367 ntsn = ntohl(nchunk->subh.data_hdr->tsn);
369 list_for_each(pos, head) {
370 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
371 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
372 if (TSN_lt(ntsn, ltsn)) {
373 list_add(new, pos->prev);
374 done = 1;
375 break;
378 if (!done)
379 list_add_tail(new, head);
382 /* Mark all the eligible packets on a transport for retransmission. */
383 void sctp_retransmit_mark(struct sctp_outq *q,
384 struct sctp_transport *transport,
385 __u8 reason)
387 struct list_head *lchunk, *ltemp;
388 struct sctp_chunk *chunk;
390 /* Walk through the specified transmitted queue. */
391 list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
392 chunk = list_entry(lchunk, struct sctp_chunk,
393 transmitted_list);
395 /* If the chunk is abandoned, move it to abandoned list. */
396 if (sctp_chunk_abandoned(chunk)) {
397 list_del_init(lchunk);
398 sctp_insert_list(&q->abandoned, lchunk);
400 /* If this chunk has not been previousely acked,
401 * stop considering it 'outstanding'. Our peer
402 * will most likely never see it since it will
403 * not be retransmitted
405 if (!chunk->tsn_gap_acked) {
406 chunk->transport->flight_size -=
407 sctp_data_size(chunk);
408 q->outstanding_bytes -= sctp_data_size(chunk);
409 q->asoc->peer.rwnd += (sctp_data_size(chunk) +
410 sizeof(struct sk_buff));
412 continue;
415 /* If we are doing retransmission due to a timeout or pmtu
416 * discovery, only the chunks that are not yet acked should
417 * be added to the retransmit queue.
419 if ((reason == SCTP_RTXR_FAST_RTX &&
420 (chunk->fast_retransmit > 0)) ||
421 (reason != SCTP_RTXR_FAST_RTX && !chunk->tsn_gap_acked)) {
422 /* If this chunk was sent less then 1 rto ago, do not
423 * retransmit this chunk, but give the peer time
424 * to acknowlege it. Do this only when
425 * retransmitting due to T3 timeout.
427 if (reason == SCTP_RTXR_T3_RTX &&
428 (jiffies - chunk->sent_at) < transport->last_rto)
429 continue;
431 /* RFC 2960 6.2.1 Processing a Received SACK
433 * C) Any time a DATA chunk is marked for
434 * retransmission (via either T3-rtx timer expiration
435 * (Section 6.3.3) or via fast retransmit
436 * (Section 7.2.4)), add the data size of those
437 * chunks to the rwnd.
439 q->asoc->peer.rwnd += (sctp_data_size(chunk) +
440 sizeof(struct sk_buff));
441 q->outstanding_bytes -= sctp_data_size(chunk);
442 transport->flight_size -= sctp_data_size(chunk);
444 /* sctpimpguide-05 Section 2.8.2
445 * M5) If a T3-rtx timer expires, the
446 * 'TSN.Missing.Report' of all affected TSNs is set
447 * to 0.
449 chunk->tsn_missing_report = 0;
451 /* If a chunk that is being used for RTT measurement
452 * has to be retransmitted, we cannot use this chunk
453 * anymore for RTT measurements. Reset rto_pending so
454 * that a new RTT measurement is started when a new
455 * data chunk is sent.
457 if (chunk->rtt_in_progress) {
458 chunk->rtt_in_progress = 0;
459 transport->rto_pending = 0;
462 /* Move the chunk to the retransmit queue. The chunks
463 * on the retransmit queue are always kept in order.
465 list_del_init(lchunk);
466 sctp_insert_list(&q->retransmit, lchunk);
470 SCTP_DEBUG_PRINTK("%s: transport: %p, reason: %d, "
471 "cwnd: %d, ssthresh: %d, flight_size: %d, "
472 "pba: %d\n", __FUNCTION__,
473 transport, reason,
474 transport->cwnd, transport->ssthresh,
475 transport->flight_size,
476 transport->partial_bytes_acked);
480 /* Mark all the eligible packets on a transport for retransmission and force
481 * one packet out.
483 void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
484 sctp_retransmit_reason_t reason)
486 int error = 0;
488 switch(reason) {
489 case SCTP_RTXR_T3_RTX:
490 SCTP_INC_STATS(SCTP_MIB_T3_RETRANSMITS);
491 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
492 /* Update the retran path if the T3-rtx timer has expired for
493 * the current retran path.
495 if (transport == transport->asoc->peer.retran_path)
496 sctp_assoc_update_retran_path(transport->asoc);
497 break;
498 case SCTP_RTXR_FAST_RTX:
499 SCTP_INC_STATS(SCTP_MIB_FAST_RETRANSMITS);
500 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
501 break;
502 case SCTP_RTXR_PMTUD:
503 SCTP_INC_STATS(SCTP_MIB_PMTUD_RETRANSMITS);
504 break;
505 case SCTP_RTXR_T1_RTX:
506 SCTP_INC_STATS(SCTP_MIB_T1_RETRANSMITS);
507 break;
508 default:
509 BUG();
512 sctp_retransmit_mark(q, transport, reason);
514 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
515 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
516 * following the procedures outlined in C1 - C5.
518 sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
520 error = sctp_outq_flush(q, /* rtx_timeout */ 1);
522 if (error)
523 q->asoc->base.sk->sk_err = -error;
527 * Transmit DATA chunks on the retransmit queue. Upon return from
528 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
529 * need to be transmitted by the caller.
530 * We assume that pkt->transport has already been set.
532 * The return value is a normal kernel error return value.
534 static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
535 int rtx_timeout, int *start_timer)
537 struct list_head *lqueue;
538 struct list_head *lchunk, *lchunk1;
539 struct sctp_transport *transport = pkt->transport;
540 sctp_xmit_t status;
541 struct sctp_chunk *chunk, *chunk1;
542 struct sctp_association *asoc;
543 int error = 0;
545 asoc = q->asoc;
546 lqueue = &q->retransmit;
548 /* RFC 2960 6.3.3 Handle T3-rtx Expiration
550 * E3) Determine how many of the earliest (i.e., lowest TSN)
551 * outstanding DATA chunks for the address for which the
552 * T3-rtx has expired will fit into a single packet, subject
553 * to the MTU constraint for the path corresponding to the
554 * destination transport address to which the retransmission
555 * is being sent (this may be different from the address for
556 * which the timer expires [see Section 6.4]). Call this value
557 * K. Bundle and retransmit those K DATA chunks in a single
558 * packet to the destination endpoint.
560 * [Just to be painfully clear, if we are retransmitting
561 * because a timeout just happened, we should send only ONE
562 * packet of retransmitted data.]
564 lchunk = sctp_list_dequeue(lqueue);
566 while (lchunk) {
567 chunk = list_entry(lchunk, struct sctp_chunk,
568 transmitted_list);
570 /* Make sure that Gap Acked TSNs are not retransmitted. A
571 * simple approach is just to move such TSNs out of the
572 * way and into a 'transmitted' queue and skip to the
573 * next chunk.
575 if (chunk->tsn_gap_acked) {
576 list_add_tail(lchunk, &transport->transmitted);
577 lchunk = sctp_list_dequeue(lqueue);
578 continue;
581 /* Attempt to append this chunk to the packet. */
582 status = sctp_packet_append_chunk(pkt, chunk);
584 switch (status) {
585 case SCTP_XMIT_PMTU_FULL:
586 /* Send this packet. */
587 if ((error = sctp_packet_transmit(pkt)) == 0)
588 *start_timer = 1;
590 /* If we are retransmitting, we should only
591 * send a single packet.
593 if (rtx_timeout) {
594 list_add(lchunk, lqueue);
595 lchunk = NULL;
598 /* Bundle lchunk in the next round. */
599 break;
601 case SCTP_XMIT_RWND_FULL:
602 /* Send this packet. */
603 if ((error = sctp_packet_transmit(pkt)) == 0)
604 *start_timer = 1;
606 /* Stop sending DATA as there is no more room
607 * at the receiver.
609 list_add(lchunk, lqueue);
610 lchunk = NULL;
611 break;
613 case SCTP_XMIT_NAGLE_DELAY:
614 /* Send this packet. */
615 if ((error = sctp_packet_transmit(pkt)) == 0)
616 *start_timer = 1;
618 /* Stop sending DATA because of nagle delay. */
619 list_add(lchunk, lqueue);
620 lchunk = NULL;
621 break;
623 default:
624 /* The append was successful, so add this chunk to
625 * the transmitted list.
627 list_add_tail(lchunk, &transport->transmitted);
629 /* Mark the chunk as ineligible for fast retransmit
630 * after it is retransmitted.
632 if (chunk->fast_retransmit > 0)
633 chunk->fast_retransmit = -1;
635 *start_timer = 1;
636 q->empty = 0;
638 /* Retrieve a new chunk to bundle. */
639 lchunk = sctp_list_dequeue(lqueue);
640 break;
643 /* If we are here due to a retransmit timeout or a fast
644 * retransmit and if there are any chunks left in the retransmit
645 * queue that could not fit in the PMTU sized packet, they need
646 * to be marked as ineligible for a subsequent fast retransmit.
648 if (rtx_timeout && !lchunk) {
649 list_for_each(lchunk1, lqueue) {
650 chunk1 = list_entry(lchunk1, struct sctp_chunk,
651 transmitted_list);
652 if (chunk1->fast_retransmit > 0)
653 chunk1->fast_retransmit = -1;
658 return error;
661 /* Cork the outqueue so queued chunks are really queued. */
662 int sctp_outq_uncork(struct sctp_outq *q)
664 int error = 0;
665 if (q->cork) {
666 q->cork = 0;
667 error = sctp_outq_flush(q, 0);
669 return error;
673 * Try to flush an outqueue.
675 * Description: Send everything in q which we legally can, subject to
676 * congestion limitations.
677 * * Note: This function can be called from multiple contexts so appropriate
678 * locking concerns must be made. Today we use the sock lock to protect
679 * this function.
681 int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
683 struct sctp_packet *packet;
684 struct sctp_packet singleton;
685 struct sctp_association *asoc = q->asoc;
686 __u16 sport = asoc->base.bind_addr.port;
687 __u16 dport = asoc->peer.port;
688 __u32 vtag = asoc->peer.i.init_tag;
689 struct sctp_transport *transport = NULL;
690 struct sctp_transport *new_transport;
691 struct sctp_chunk *chunk, *tmp;
692 sctp_xmit_t status;
693 int error = 0;
694 int start_timer = 0;
696 /* These transports have chunks to send. */
697 struct list_head transport_list;
698 struct list_head *ltransport;
700 INIT_LIST_HEAD(&transport_list);
701 packet = NULL;
704 * 6.10 Bundling
705 * ...
706 * When bundling control chunks with DATA chunks, an
707 * endpoint MUST place control chunks first in the outbound
708 * SCTP packet. The transmitter MUST transmit DATA chunks
709 * within a SCTP packet in increasing order of TSN.
710 * ...
713 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
714 list_del_init(&chunk->list);
716 /* Pick the right transport to use. */
717 new_transport = chunk->transport;
719 if (!new_transport) {
720 new_transport = asoc->peer.active_path;
721 } else if ((new_transport->state == SCTP_INACTIVE) ||
722 (new_transport->state == SCTP_UNCONFIRMED)) {
723 /* If the chunk is Heartbeat or Heartbeat Ack,
724 * send it to chunk->transport, even if it's
725 * inactive.
727 * 3.3.6 Heartbeat Acknowledgement:
728 * ...
729 * A HEARTBEAT ACK is always sent to the source IP
730 * address of the IP datagram containing the
731 * HEARTBEAT chunk to which this ack is responding.
732 * ...
734 if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
735 chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK)
736 new_transport = asoc->peer.active_path;
739 /* Are we switching transports?
740 * Take care of transport locks.
742 if (new_transport != transport) {
743 transport = new_transport;
744 if (list_empty(&transport->send_ready)) {
745 list_add_tail(&transport->send_ready,
746 &transport_list);
748 packet = &transport->packet;
749 sctp_packet_config(packet, vtag,
750 asoc->peer.ecn_capable);
753 switch (chunk->chunk_hdr->type) {
755 * 6.10 Bundling
756 * ...
757 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
758 * COMPLETE with any other chunks. [Send them immediately.]
760 case SCTP_CID_INIT:
761 case SCTP_CID_INIT_ACK:
762 case SCTP_CID_SHUTDOWN_COMPLETE:
763 sctp_packet_init(&singleton, transport, sport, dport);
764 sctp_packet_config(&singleton, vtag, 0);
765 sctp_packet_append_chunk(&singleton, chunk);
766 error = sctp_packet_transmit(&singleton);
767 if (error < 0)
768 return error;
769 break;
771 case SCTP_CID_ABORT:
772 case SCTP_CID_SACK:
773 case SCTP_CID_HEARTBEAT:
774 case SCTP_CID_HEARTBEAT_ACK:
775 case SCTP_CID_SHUTDOWN:
776 case SCTP_CID_SHUTDOWN_ACK:
777 case SCTP_CID_ERROR:
778 case SCTP_CID_COOKIE_ECHO:
779 case SCTP_CID_COOKIE_ACK:
780 case SCTP_CID_ECN_ECNE:
781 case SCTP_CID_ECN_CWR:
782 case SCTP_CID_ASCONF:
783 case SCTP_CID_ASCONF_ACK:
784 case SCTP_CID_FWD_TSN:
785 sctp_packet_transmit_chunk(packet, chunk);
786 break;
788 default:
789 /* We built a chunk with an illegal type! */
790 BUG();
794 /* Is it OK to send data chunks? */
795 switch (asoc->state) {
796 case SCTP_STATE_COOKIE_ECHOED:
797 /* Only allow bundling when this packet has a COOKIE-ECHO
798 * chunk.
800 if (!packet || !packet->has_cookie_echo)
801 break;
803 /* fallthru */
804 case SCTP_STATE_ESTABLISHED:
805 case SCTP_STATE_SHUTDOWN_PENDING:
806 case SCTP_STATE_SHUTDOWN_RECEIVED:
808 * RFC 2960 6.1 Transmission of DATA Chunks
810 * C) When the time comes for the sender to transmit,
811 * before sending new DATA chunks, the sender MUST
812 * first transmit any outstanding DATA chunks which
813 * are marked for retransmission (limited by the
814 * current cwnd).
816 if (!list_empty(&q->retransmit)) {
817 if (transport == asoc->peer.retran_path)
818 goto retran;
820 /* Switch transports & prepare the packet. */
822 transport = asoc->peer.retran_path;
824 if (list_empty(&transport->send_ready)) {
825 list_add_tail(&transport->send_ready,
826 &transport_list);
829 packet = &transport->packet;
830 sctp_packet_config(packet, vtag,
831 asoc->peer.ecn_capable);
832 retran:
833 error = sctp_outq_flush_rtx(q, packet,
834 rtx_timeout, &start_timer);
836 if (start_timer)
837 sctp_transport_reset_timers(transport);
839 /* This can happen on COOKIE-ECHO resend. Only
840 * one chunk can get bundled with a COOKIE-ECHO.
842 if (packet->has_cookie_echo)
843 goto sctp_flush_out;
845 /* Don't send new data if there is still data
846 * waiting to retransmit.
848 if (!list_empty(&q->retransmit))
849 goto sctp_flush_out;
852 /* Finally, transmit new packets. */
853 start_timer = 0;
854 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
855 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
856 * stream identifier.
858 if (chunk->sinfo.sinfo_stream >=
859 asoc->c.sinit_num_ostreams) {
861 /* Mark as failed send. */
862 sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
863 sctp_chunk_free(chunk);
864 continue;
867 /* Has this chunk expired? */
868 if (sctp_chunk_abandoned(chunk)) {
869 sctp_chunk_fail(chunk, 0);
870 sctp_chunk_free(chunk);
871 continue;
874 /* If there is a specified transport, use it.
875 * Otherwise, we want to use the active path.
877 new_transport = chunk->transport;
878 if (!new_transport ||
879 ((new_transport->state == SCTP_INACTIVE) ||
880 (new_transport->state == SCTP_UNCONFIRMED)))
881 new_transport = asoc->peer.active_path;
883 /* Change packets if necessary. */
884 if (new_transport != transport) {
885 transport = new_transport;
887 /* Schedule to have this transport's
888 * packet flushed.
890 if (list_empty(&transport->send_ready)) {
891 list_add_tail(&transport->send_ready,
892 &transport_list);
895 packet = &transport->packet;
896 sctp_packet_config(packet, vtag,
897 asoc->peer.ecn_capable);
900 SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
901 q, chunk,
902 chunk && chunk->chunk_hdr ?
903 sctp_cname(SCTP_ST_CHUNK(
904 chunk->chunk_hdr->type))
905 : "Illegal Chunk");
907 SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
908 "%p skb->users %d.\n",
909 ntohl(chunk->subh.data_hdr->tsn),
910 chunk->skb ?chunk->skb->head : NULL,
911 chunk->skb ?
912 atomic_read(&chunk->skb->users) : -1);
914 /* Add the chunk to the packet. */
915 status = sctp_packet_transmit_chunk(packet, chunk);
917 switch (status) {
918 case SCTP_XMIT_PMTU_FULL:
919 case SCTP_XMIT_RWND_FULL:
920 case SCTP_XMIT_NAGLE_DELAY:
921 /* We could not append this chunk, so put
922 * the chunk back on the output queue.
924 SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
925 "not transmit TSN: 0x%x, status: %d\n",
926 ntohl(chunk->subh.data_hdr->tsn),
927 status);
928 sctp_outq_head_data(q, chunk);
929 goto sctp_flush_out;
930 break;
932 case SCTP_XMIT_OK:
933 break;
935 default:
936 BUG();
939 /* BUG: We assume that the sctp_packet_transmit()
940 * call below will succeed all the time and add the
941 * chunk to the transmitted list and restart the
942 * timers.
943 * It is possible that the call can fail under OOM
944 * conditions.
946 * Is this really a problem? Won't this behave
947 * like a lost TSN?
949 list_add_tail(&chunk->transmitted_list,
950 &transport->transmitted);
952 sctp_transport_reset_timers(transport);
954 q->empty = 0;
956 /* Only let one DATA chunk get bundled with a
957 * COOKIE-ECHO chunk.
959 if (packet->has_cookie_echo)
960 goto sctp_flush_out;
962 break;
964 default:
965 /* Do nothing. */
966 break;
969 sctp_flush_out:
971 /* Before returning, examine all the transports touched in
972 * this call. Right now, we bluntly force clear all the
973 * transports. Things might change after we implement Nagle.
974 * But such an examination is still required.
976 * --xguo
978 while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
979 struct sctp_transport *t = list_entry(ltransport,
980 struct sctp_transport,
981 send_ready);
982 packet = &t->packet;
983 if (!sctp_packet_empty(packet))
984 error = sctp_packet_transmit(packet);
987 return error;
990 /* Update unack_data based on the incoming SACK chunk */
991 static void sctp_sack_update_unack_data(struct sctp_association *assoc,
992 struct sctp_sackhdr *sack)
994 sctp_sack_variable_t *frags;
995 __u16 unack_data;
996 int i;
998 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1000 frags = sack->variable;
1001 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1002 unack_data -= ((ntohs(frags[i].gab.end) -
1003 ntohs(frags[i].gab.start) + 1));
1006 assoc->unack_data = unack_data;
1009 /* Return the highest new tsn that is acknowledged by the given SACK chunk. */
1010 static __u32 sctp_highest_new_tsn(struct sctp_sackhdr *sack,
1011 struct sctp_association *asoc)
1013 struct list_head *ltransport, *lchunk;
1014 struct sctp_transport *transport;
1015 struct sctp_chunk *chunk;
1016 __u32 highest_new_tsn, tsn;
1017 struct list_head *transport_list = &asoc->peer.transport_addr_list;
1019 highest_new_tsn = ntohl(sack->cum_tsn_ack);
1021 list_for_each(ltransport, transport_list) {
1022 transport = list_entry(ltransport, struct sctp_transport,
1023 transports);
1024 list_for_each(lchunk, &transport->transmitted) {
1025 chunk = list_entry(lchunk, struct sctp_chunk,
1026 transmitted_list);
1027 tsn = ntohl(chunk->subh.data_hdr->tsn);
1029 if (!chunk->tsn_gap_acked &&
1030 TSN_lt(highest_new_tsn, tsn) &&
1031 sctp_acked(sack, tsn))
1032 highest_new_tsn = tsn;
1036 return highest_new_tsn;
1039 /* This is where we REALLY process a SACK.
1041 * Process the SACK against the outqueue. Mostly, this just frees
1042 * things off the transmitted queue.
1044 int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
1046 struct sctp_association *asoc = q->asoc;
1047 struct sctp_transport *transport;
1048 struct sctp_chunk *tchunk = NULL;
1049 struct list_head *lchunk, *transport_list, *pos, *temp;
1050 sctp_sack_variable_t *frags = sack->variable;
1051 __u32 sack_ctsn, ctsn, tsn;
1052 __u32 highest_tsn, highest_new_tsn;
1053 __u32 sack_a_rwnd;
1054 unsigned outstanding;
1055 struct sctp_transport *primary = asoc->peer.primary_path;
1056 int count_of_newacks = 0;
1058 /* Grab the association's destination address list. */
1059 transport_list = &asoc->peer.transport_addr_list;
1061 sack_ctsn = ntohl(sack->cum_tsn_ack);
1064 * SFR-CACC algorithm:
1065 * On receipt of a SACK the sender SHOULD execute the
1066 * following statements.
1068 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1069 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1070 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1071 * all destinations.
1073 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1074 primary->cacc.changeover_active = 0;
1075 list_for_each(pos, transport_list) {
1076 transport = list_entry(pos, struct sctp_transport,
1077 transports);
1078 transport->cacc.cycling_changeover = 0;
1083 * SFR-CACC algorithm:
1084 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1085 * is set the receiver of the SACK MUST take the following actions:
1087 * A) Initialize the cacc_saw_newack to 0 for all destination
1088 * addresses.
1090 if (sack->num_gap_ack_blocks &&
1091 primary->cacc.changeover_active) {
1092 list_for_each(pos, transport_list) {
1093 transport = list_entry(pos, struct sctp_transport,
1094 transports);
1095 transport->cacc.cacc_saw_newack = 0;
1099 /* Get the highest TSN in the sack. */
1100 highest_tsn = sack_ctsn;
1101 if (sack->num_gap_ack_blocks)
1102 highest_tsn +=
1103 ntohs(frags[ntohs(sack->num_gap_ack_blocks) - 1].gab.end);
1105 if (TSN_lt(asoc->highest_sacked, highest_tsn)) {
1106 highest_new_tsn = highest_tsn;
1107 asoc->highest_sacked = highest_tsn;
1108 } else {
1109 highest_new_tsn = sctp_highest_new_tsn(sack, asoc);
1112 /* Run through the retransmit queue. Credit bytes received
1113 * and free those chunks that we can.
1115 sctp_check_transmitted(q, &q->retransmit, NULL, sack, highest_new_tsn);
1116 sctp_mark_missing(q, &q->retransmit, NULL, highest_new_tsn, 0);
1118 /* Run through the transmitted queue.
1119 * Credit bytes received and free those chunks which we can.
1121 * This is a MASSIVE candidate for optimization.
1123 list_for_each(pos, transport_list) {
1124 transport = list_entry(pos, struct sctp_transport,
1125 transports);
1126 sctp_check_transmitted(q, &transport->transmitted,
1127 transport, sack, highest_new_tsn);
1129 * SFR-CACC algorithm:
1130 * C) Let count_of_newacks be the number of
1131 * destinations for which cacc_saw_newack is set.
1133 if (transport->cacc.cacc_saw_newack)
1134 count_of_newacks ++;
1137 list_for_each(pos, transport_list) {
1138 transport = list_entry(pos, struct sctp_transport,
1139 transports);
1140 sctp_mark_missing(q, &transport->transmitted, transport,
1141 highest_new_tsn, count_of_newacks);
1144 /* Move the Cumulative TSN Ack Point if appropriate. */
1145 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn))
1146 asoc->ctsn_ack_point = sack_ctsn;
1148 /* Update unack_data field in the assoc. */
1149 sctp_sack_update_unack_data(asoc, sack);
1151 ctsn = asoc->ctsn_ack_point;
1153 /* Throw away stuff rotting on the sack queue. */
1154 list_for_each_safe(lchunk, temp, &q->sacked) {
1155 tchunk = list_entry(lchunk, struct sctp_chunk,
1156 transmitted_list);
1157 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1158 if (TSN_lte(tsn, ctsn))
1159 sctp_chunk_free(tchunk);
1162 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1163 * number of bytes still outstanding after processing the
1164 * Cumulative TSN Ack and the Gap Ack Blocks.
1167 sack_a_rwnd = ntohl(sack->a_rwnd);
1168 outstanding = q->outstanding_bytes;
1170 if (outstanding < sack_a_rwnd)
1171 sack_a_rwnd -= outstanding;
1172 else
1173 sack_a_rwnd = 0;
1175 asoc->peer.rwnd = sack_a_rwnd;
1177 sctp_generate_fwdtsn(q, sack_ctsn);
1179 SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
1180 __FUNCTION__, sack_ctsn);
1181 SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
1182 "%p is 0x%x. Adv peer ack point: 0x%x\n",
1183 __FUNCTION__, asoc, ctsn, asoc->adv_peer_ack_point);
1185 /* See if all chunks are acked.
1186 * Make sure the empty queue handler will get run later.
1188 q->empty = (list_empty(&q->out_chunk_list) &&
1189 list_empty(&q->control_chunk_list) &&
1190 list_empty(&q->retransmit));
1191 if (!q->empty)
1192 goto finish;
1194 list_for_each(pos, transport_list) {
1195 transport = list_entry(pos, struct sctp_transport,
1196 transports);
1197 q->empty = q->empty && list_empty(&transport->transmitted);
1198 if (!q->empty)
1199 goto finish;
1202 SCTP_DEBUG_PRINTK("sack queue is empty.\n");
1203 finish:
1204 return q->empty;
1207 /* Is the outqueue empty? */
1208 int sctp_outq_is_empty(const struct sctp_outq *q)
1210 return q->empty;
1213 /********************************************************************
1214 * 2nd Level Abstractions
1215 ********************************************************************/
1217 /* Go through a transport's transmitted list or the association's retransmit
1218 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1219 * The retransmit list will not have an associated transport.
1221 * I added coherent debug information output. --xguo
1223 * Instead of printing 'sacked' or 'kept' for each TSN on the
1224 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1225 * KEPT TSN6-TSN7, etc.
1227 static void sctp_check_transmitted(struct sctp_outq *q,
1228 struct list_head *transmitted_queue,
1229 struct sctp_transport *transport,
1230 struct sctp_sackhdr *sack,
1231 __u32 highest_new_tsn_in_sack)
1233 struct list_head *lchunk;
1234 struct sctp_chunk *tchunk;
1235 struct list_head tlist;
1236 __u32 tsn;
1237 __u32 sack_ctsn;
1238 __u32 rtt;
1239 __u8 restart_timer = 0;
1240 int bytes_acked = 0;
1242 /* These state variables are for coherent debug output. --xguo */
1244 #if SCTP_DEBUG
1245 __u32 dbg_ack_tsn = 0; /* An ACKed TSN range starts here... */
1246 __u32 dbg_last_ack_tsn = 0; /* ...and finishes here. */
1247 __u32 dbg_kept_tsn = 0; /* An un-ACKed range starts here... */
1248 __u32 dbg_last_kept_tsn = 0; /* ...and finishes here. */
1250 /* 0 : The last TSN was ACKed.
1251 * 1 : The last TSN was NOT ACKed (i.e. KEPT).
1252 * -1: We need to initialize.
1254 int dbg_prt_state = -1;
1255 #endif /* SCTP_DEBUG */
1257 sack_ctsn = ntohl(sack->cum_tsn_ack);
1259 INIT_LIST_HEAD(&tlist);
1261 /* The while loop will skip empty transmitted queues. */
1262 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1263 tchunk = list_entry(lchunk, struct sctp_chunk,
1264 transmitted_list);
1266 if (sctp_chunk_abandoned(tchunk)) {
1267 /* Move the chunk to abandoned list. */
1268 sctp_insert_list(&q->abandoned, lchunk);
1270 /* If this chunk has not been acked, stop
1271 * considering it as 'outstanding'.
1273 if (!tchunk->tsn_gap_acked) {
1274 tchunk->transport->flight_size -=
1275 sctp_data_size(tchunk);
1276 q->outstanding_bytes -= sctp_data_size(tchunk);
1278 continue;
1281 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1282 if (sctp_acked(sack, tsn)) {
1283 /* If this queue is the retransmit queue, the
1284 * retransmit timer has already reclaimed
1285 * the outstanding bytes for this chunk, so only
1286 * count bytes associated with a transport.
1288 if (transport) {
1289 /* If this chunk is being used for RTT
1290 * measurement, calculate the RTT and update
1291 * the RTO using this value.
1293 * 6.3.1 C5) Karn's algorithm: RTT measurements
1294 * MUST NOT be made using packets that were
1295 * retransmitted (and thus for which it is
1296 * ambiguous whether the reply was for the
1297 * first instance of the packet or a later
1298 * instance).
1300 if (!tchunk->tsn_gap_acked &&
1301 !tchunk->resent &&
1302 tchunk->rtt_in_progress) {
1303 tchunk->rtt_in_progress = 0;
1304 rtt = jiffies - tchunk->sent_at;
1305 sctp_transport_update_rto(transport,
1306 rtt);
1309 if (TSN_lte(tsn, sack_ctsn)) {
1310 /* RFC 2960 6.3.2 Retransmission Timer Rules
1312 * R3) Whenever a SACK is received
1313 * that acknowledges the DATA chunk
1314 * with the earliest outstanding TSN
1315 * for that address, restart T3-rtx
1316 * timer for that address with its
1317 * current RTO.
1319 restart_timer = 1;
1321 if (!tchunk->tsn_gap_acked) {
1322 tchunk->tsn_gap_acked = 1;
1323 bytes_acked += sctp_data_size(tchunk);
1325 * SFR-CACC algorithm:
1326 * 2) If the SACK contains gap acks
1327 * and the flag CHANGEOVER_ACTIVE is
1328 * set the receiver of the SACK MUST
1329 * take the following action:
1331 * B) For each TSN t being acked that
1332 * has not been acked in any SACK so
1333 * far, set cacc_saw_newack to 1 for
1334 * the destination that the TSN was
1335 * sent to.
1337 if (transport &&
1338 sack->num_gap_ack_blocks &&
1339 q->asoc->peer.primary_path->cacc.
1340 changeover_active)
1341 transport->cacc.cacc_saw_newack
1342 = 1;
1345 list_add_tail(&tchunk->transmitted_list,
1346 &q->sacked);
1347 } else {
1348 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1349 * M2) Each time a SACK arrives reporting
1350 * 'Stray DATA chunk(s)' record the highest TSN
1351 * reported as newly acknowledged, call this
1352 * value 'HighestTSNinSack'. A newly
1353 * acknowledged DATA chunk is one not
1354 * previously acknowledged in a SACK.
1356 * When the SCTP sender of data receives a SACK
1357 * chunk that acknowledges, for the first time,
1358 * the receipt of a DATA chunk, all the still
1359 * unacknowledged DATA chunks whose TSN is
1360 * older than that newly acknowledged DATA
1361 * chunk, are qualified as 'Stray DATA chunks'.
1363 if (!tchunk->tsn_gap_acked) {
1364 tchunk->tsn_gap_acked = 1;
1365 bytes_acked += sctp_data_size(tchunk);
1367 list_add_tail(lchunk, &tlist);
1370 #if SCTP_DEBUG
1371 switch (dbg_prt_state) {
1372 case 0: /* last TSN was ACKed */
1373 if (dbg_last_ack_tsn + 1 == tsn) {
1374 /* This TSN belongs to the
1375 * current ACK range.
1377 break;
1380 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1381 /* Display the end of the
1382 * current range.
1384 SCTP_DEBUG_PRINTK("-%08x",
1385 dbg_last_ack_tsn);
1388 /* Start a new range. */
1389 SCTP_DEBUG_PRINTK(",%08x", tsn);
1390 dbg_ack_tsn = tsn;
1391 break;
1393 case 1: /* The last TSN was NOT ACKed. */
1394 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1395 /* Display the end of current range. */
1396 SCTP_DEBUG_PRINTK("-%08x",
1397 dbg_last_kept_tsn);
1400 SCTP_DEBUG_PRINTK("\n");
1402 /* FALL THROUGH... */
1403 default:
1404 /* This is the first-ever TSN we examined. */
1405 /* Start a new range of ACK-ed TSNs. */
1406 SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
1407 dbg_prt_state = 0;
1408 dbg_ack_tsn = tsn;
1411 dbg_last_ack_tsn = tsn;
1412 #endif /* SCTP_DEBUG */
1414 } else {
1415 if (tchunk->tsn_gap_acked) {
1416 SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
1417 "data TSN: 0x%x\n",
1418 __FUNCTION__,
1419 tsn);
1420 tchunk->tsn_gap_acked = 0;
1422 bytes_acked -= sctp_data_size(tchunk);
1424 /* RFC 2960 6.3.2 Retransmission Timer Rules
1426 * R4) Whenever a SACK is received missing a
1427 * TSN that was previously acknowledged via a
1428 * Gap Ack Block, start T3-rtx for the
1429 * destination address to which the DATA
1430 * chunk was originally
1431 * transmitted if it is not already running.
1433 restart_timer = 1;
1436 list_add_tail(lchunk, &tlist);
1438 #if SCTP_DEBUG
1439 /* See the above comments on ACK-ed TSNs. */
1440 switch (dbg_prt_state) {
1441 case 1:
1442 if (dbg_last_kept_tsn + 1 == tsn)
1443 break;
1445 if (dbg_last_kept_tsn != dbg_kept_tsn)
1446 SCTP_DEBUG_PRINTK("-%08x",
1447 dbg_last_kept_tsn);
1449 SCTP_DEBUG_PRINTK(",%08x", tsn);
1450 dbg_kept_tsn = tsn;
1451 break;
1453 case 0:
1454 if (dbg_last_ack_tsn != dbg_ack_tsn)
1455 SCTP_DEBUG_PRINTK("-%08x",
1456 dbg_last_ack_tsn);
1457 SCTP_DEBUG_PRINTK("\n");
1459 /* FALL THROUGH... */
1460 default:
1461 SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
1462 dbg_prt_state = 1;
1463 dbg_kept_tsn = tsn;
1466 dbg_last_kept_tsn = tsn;
1467 #endif /* SCTP_DEBUG */
1471 #if SCTP_DEBUG
1472 /* Finish off the last range, displaying its ending TSN. */
1473 switch (dbg_prt_state) {
1474 case 0:
1475 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1476 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_ack_tsn);
1477 } else {
1478 SCTP_DEBUG_PRINTK("\n");
1480 break;
1482 case 1:
1483 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1484 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_kept_tsn);
1485 } else {
1486 SCTP_DEBUG_PRINTK("\n");
1489 #endif /* SCTP_DEBUG */
1490 if (transport) {
1491 if (bytes_acked) {
1492 /* 8.2. When an outstanding TSN is acknowledged,
1493 * the endpoint shall clear the error counter of
1494 * the destination transport address to which the
1495 * DATA chunk was last sent.
1496 * The association's overall error counter is
1497 * also cleared.
1499 transport->error_count = 0;
1500 transport->asoc->overall_error_count = 0;
1502 /* Mark the destination transport address as
1503 * active if it is not so marked.
1505 if ((transport->state == SCTP_INACTIVE) ||
1506 (transport->state == SCTP_UNCONFIRMED)) {
1507 sctp_assoc_control_transport(
1508 transport->asoc,
1509 transport,
1510 SCTP_TRANSPORT_UP,
1511 SCTP_RECEIVED_SACK);
1514 sctp_transport_raise_cwnd(transport, sack_ctsn,
1515 bytes_acked);
1517 transport->flight_size -= bytes_acked;
1518 q->outstanding_bytes -= bytes_acked;
1519 } else {
1520 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1521 * When a sender is doing zero window probing, it
1522 * should not timeout the association if it continues
1523 * to receive new packets from the receiver. The
1524 * reason is that the receiver MAY keep its window
1525 * closed for an indefinite time.
1526 * A sender is doing zero window probing when the
1527 * receiver's advertised window is zero, and there is
1528 * only one data chunk in flight to the receiver.
1530 if (!q->asoc->peer.rwnd &&
1531 !list_empty(&tlist) &&
1532 (sack_ctsn+2 == q->asoc->next_tsn)) {
1533 SCTP_DEBUG_PRINTK("%s: SACK received for zero "
1534 "window probe: %u\n",
1535 __FUNCTION__, sack_ctsn);
1536 q->asoc->overall_error_count = 0;
1537 transport->error_count = 0;
1541 /* RFC 2960 6.3.2 Retransmission Timer Rules
1543 * R2) Whenever all outstanding data sent to an address have
1544 * been acknowledged, turn off the T3-rtx timer of that
1545 * address.
1547 if (!transport->flight_size) {
1548 if (timer_pending(&transport->T3_rtx_timer) &&
1549 del_timer(&transport->T3_rtx_timer)) {
1550 sctp_transport_put(transport);
1552 } else if (restart_timer) {
1553 if (!mod_timer(&transport->T3_rtx_timer,
1554 jiffies + transport->rto))
1555 sctp_transport_hold(transport);
1559 list_splice(&tlist, transmitted_queue);
1562 /* Mark chunks as missing and consequently may get retransmitted. */
1563 static void sctp_mark_missing(struct sctp_outq *q,
1564 struct list_head *transmitted_queue,
1565 struct sctp_transport *transport,
1566 __u32 highest_new_tsn_in_sack,
1567 int count_of_newacks)
1569 struct sctp_chunk *chunk;
1570 struct list_head *pos;
1571 __u32 tsn;
1572 char do_fast_retransmit = 0;
1573 struct sctp_transport *primary = q->asoc->peer.primary_path;
1575 list_for_each(pos, transmitted_queue) {
1577 chunk = list_entry(pos, struct sctp_chunk, transmitted_list);
1578 tsn = ntohl(chunk->subh.data_hdr->tsn);
1580 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1581 * 'Unacknowledged TSN's', if the TSN number of an
1582 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1583 * value, increment the 'TSN.Missing.Report' count on that
1584 * chunk if it has NOT been fast retransmitted or marked for
1585 * fast retransmit already.
1587 if (!chunk->fast_retransmit &&
1588 !chunk->tsn_gap_acked &&
1589 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1591 /* SFR-CACC may require us to skip marking
1592 * this chunk as missing.
1594 if (!transport || !sctp_cacc_skip(primary, transport,
1595 count_of_newacks, tsn)) {
1596 chunk->tsn_missing_report++;
1598 SCTP_DEBUG_PRINTK(
1599 "%s: TSN 0x%x missing counter: %d\n",
1600 __FUNCTION__, tsn,
1601 chunk->tsn_missing_report);
1605 * M4) If any DATA chunk is found to have a
1606 * 'TSN.Missing.Report'
1607 * value larger than or equal to 3, mark that chunk for
1608 * retransmission and start the fast retransmit procedure.
1611 if (chunk->tsn_missing_report >= 3) {
1612 chunk->fast_retransmit = 1;
1613 do_fast_retransmit = 1;
1617 if (transport) {
1618 if (do_fast_retransmit)
1619 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1621 SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
1622 "ssthresh: %d, flight_size: %d, pba: %d\n",
1623 __FUNCTION__, transport, transport->cwnd,
1624 transport->ssthresh, transport->flight_size,
1625 transport->partial_bytes_acked);
1629 /* Is the given TSN acked by this packet? */
1630 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1632 int i;
1633 sctp_sack_variable_t *frags;
1634 __u16 gap;
1635 __u32 ctsn = ntohl(sack->cum_tsn_ack);
1637 if (TSN_lte(tsn, ctsn))
1638 goto pass;
1640 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1642 * Gap Ack Blocks:
1643 * These fields contain the Gap Ack Blocks. They are repeated
1644 * for each Gap Ack Block up to the number of Gap Ack Blocks
1645 * defined in the Number of Gap Ack Blocks field. All DATA
1646 * chunks with TSNs greater than or equal to (Cumulative TSN
1647 * Ack + Gap Ack Block Start) and less than or equal to
1648 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1649 * Block are assumed to have been received correctly.
1652 frags = sack->variable;
1653 gap = tsn - ctsn;
1654 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
1655 if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
1656 TSN_lte(gap, ntohs(frags[i].gab.end)))
1657 goto pass;
1660 return 0;
1661 pass:
1662 return 1;
1665 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
1666 int nskips, __be16 stream)
1668 int i;
1670 for (i = 0; i < nskips; i++) {
1671 if (skiplist[i].stream == stream)
1672 return i;
1674 return i;
1677 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1678 static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1680 struct sctp_association *asoc = q->asoc;
1681 struct sctp_chunk *ftsn_chunk = NULL;
1682 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1683 int nskips = 0;
1684 int skip_pos = 0;
1685 __u32 tsn;
1686 struct sctp_chunk *chunk;
1687 struct list_head *lchunk, *temp;
1689 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1690 * received SACK.
1692 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1693 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1695 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1696 asoc->adv_peer_ack_point = ctsn;
1698 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1699 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1700 * the chunk next in the out-queue space is marked as "abandoned" as
1701 * shown in the following example:
1703 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1704 * and the Advanced.Peer.Ack.Point is updated to this value:
1706 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1707 * normal SACK processing local advancement
1708 * ... ...
1709 * Adv.Ack.Pt-> 102 acked 102 acked
1710 * 103 abandoned 103 abandoned
1711 * 104 abandoned Adv.Ack.P-> 104 abandoned
1712 * 105 105
1713 * 106 acked 106 acked
1714 * ... ...
1716 * In this example, the data sender successfully advanced the
1717 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1719 list_for_each_safe(lchunk, temp, &q->abandoned) {
1720 chunk = list_entry(lchunk, struct sctp_chunk,
1721 transmitted_list);
1722 tsn = ntohl(chunk->subh.data_hdr->tsn);
1724 /* Remove any chunks in the abandoned queue that are acked by
1725 * the ctsn.
1727 if (TSN_lte(tsn, ctsn)) {
1728 list_del_init(lchunk);
1729 sctp_chunk_free(chunk);
1730 } else {
1731 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1732 asoc->adv_peer_ack_point = tsn;
1733 if (chunk->chunk_hdr->flags &
1734 SCTP_DATA_UNORDERED)
1735 continue;
1736 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1737 nskips,
1738 chunk->subh.data_hdr->stream);
1739 ftsn_skip_arr[skip_pos].stream =
1740 chunk->subh.data_hdr->stream;
1741 ftsn_skip_arr[skip_pos].ssn =
1742 chunk->subh.data_hdr->ssn;
1743 if (skip_pos == nskips)
1744 nskips++;
1745 if (nskips == 10)
1746 break;
1747 } else
1748 break;
1752 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1753 * is greater than the Cumulative TSN ACK carried in the received
1754 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1755 * chunk containing the latest value of the
1756 * "Advanced.Peer.Ack.Point".
1758 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1759 * list each stream and sequence number in the forwarded TSN. This
1760 * information will enable the receiver to easily find any
1761 * stranded TSN's waiting on stream reorder queues. Each stream
1762 * SHOULD only be reported once; this means that if multiple
1763 * abandoned messages occur in the same stream then only the
1764 * highest abandoned stream sequence number is reported. If the
1765 * total size of the FORWARD TSN does NOT fit in a single MTU then
1766 * the sender of the FORWARD TSN SHOULD lower the
1767 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1768 * single MTU.
1770 if (asoc->adv_peer_ack_point > ctsn)
1771 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
1772 nskips, &ftsn_skip_arr[0]);
1774 if (ftsn_chunk) {
1775 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1776 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);