Linux 2.6.14.3
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / outqueue.c
blobefb72faba20cd50aec065d4fe498c85ecae8d4ae
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 fast_retransmit)
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);
399 continue;
402 /* If we are doing retransmission due to a fast retransmit,
403 * only the chunk's that are marked for fast retransmit
404 * should be added to the retransmit queue. If we are doing
405 * retransmission due to a timeout or pmtu discovery, only the
406 * chunks that are not yet acked should be added to the
407 * retransmit queue.
409 if ((fast_retransmit && chunk->fast_retransmit) ||
410 (!fast_retransmit && !chunk->tsn_gap_acked)) {
411 /* RFC 2960 6.2.1 Processing a Received SACK
413 * C) Any time a DATA chunk is marked for
414 * retransmission (via either T3-rtx timer expiration
415 * (Section 6.3.3) or via fast retransmit
416 * (Section 7.2.4)), add the data size of those
417 * chunks to the rwnd.
419 q->asoc->peer.rwnd += sctp_data_size(chunk);
420 q->outstanding_bytes -= sctp_data_size(chunk);
421 transport->flight_size -= sctp_data_size(chunk);
423 /* sctpimpguide-05 Section 2.8.2
424 * M5) If a T3-rtx timer expires, the
425 * 'TSN.Missing.Report' of all affected TSNs is set
426 * to 0.
428 chunk->tsn_missing_report = 0;
430 /* If a chunk that is being used for RTT measurement
431 * has to be retransmitted, we cannot use this chunk
432 * anymore for RTT measurements. Reset rto_pending so
433 * that a new RTT measurement is started when a new
434 * data chunk is sent.
436 if (chunk->rtt_in_progress) {
437 chunk->rtt_in_progress = 0;
438 transport->rto_pending = 0;
441 /* Move the chunk to the retransmit queue. The chunks
442 * on the retransmit queue are always kept in order.
444 list_del_init(lchunk);
445 sctp_insert_list(&q->retransmit, lchunk);
449 SCTP_DEBUG_PRINTK("%s: transport: %p, fast_retransmit: %d, "
450 "cwnd: %d, ssthresh: %d, flight_size: %d, "
451 "pba: %d\n", __FUNCTION__,
452 transport, fast_retransmit,
453 transport->cwnd, transport->ssthresh,
454 transport->flight_size,
455 transport->partial_bytes_acked);
459 /* Mark all the eligible packets on a transport for retransmission and force
460 * one packet out.
462 void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
463 sctp_retransmit_reason_t reason)
465 int error = 0;
466 __u8 fast_retransmit = 0;
468 switch(reason) {
469 case SCTP_RTXR_T3_RTX:
470 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
471 /* Update the retran path if the T3-rtx timer has expired for
472 * the current retran path.
474 if (transport == transport->asoc->peer.retran_path)
475 sctp_assoc_update_retran_path(transport->asoc);
476 break;
477 case SCTP_RTXR_FAST_RTX:
478 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
479 fast_retransmit = 1;
480 break;
481 case SCTP_RTXR_PMTUD:
482 default:
483 break;
486 sctp_retransmit_mark(q, transport, fast_retransmit);
488 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
489 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
490 * following the procedures outlined in C1 - C5.
492 sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
494 error = sctp_outq_flush(q, /* rtx_timeout */ 1);
496 if (error)
497 q->asoc->base.sk->sk_err = -error;
501 * Transmit DATA chunks on the retransmit queue. Upon return from
502 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
503 * need to be transmitted by the caller.
504 * We assume that pkt->transport has already been set.
506 * The return value is a normal kernel error return value.
508 static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
509 int rtx_timeout, int *start_timer)
511 struct list_head *lqueue;
512 struct list_head *lchunk, *lchunk1;
513 struct sctp_transport *transport = pkt->transport;
514 sctp_xmit_t status;
515 struct sctp_chunk *chunk, *chunk1;
516 struct sctp_association *asoc;
517 int error = 0;
519 asoc = q->asoc;
520 lqueue = &q->retransmit;
522 /* RFC 2960 6.3.3 Handle T3-rtx Expiration
524 * E3) Determine how many of the earliest (i.e., lowest TSN)
525 * outstanding DATA chunks for the address for which the
526 * T3-rtx has expired will fit into a single packet, subject
527 * to the MTU constraint for the path corresponding to the
528 * destination transport address to which the retransmission
529 * is being sent (this may be different from the address for
530 * which the timer expires [see Section 6.4]). Call this value
531 * K. Bundle and retransmit those K DATA chunks in a single
532 * packet to the destination endpoint.
534 * [Just to be painfully clear, if we are retransmitting
535 * because a timeout just happened, we should send only ONE
536 * packet of retransmitted data.]
538 lchunk = sctp_list_dequeue(lqueue);
540 while (lchunk) {
541 chunk = list_entry(lchunk, struct sctp_chunk,
542 transmitted_list);
544 /* Make sure that Gap Acked TSNs are not retransmitted. A
545 * simple approach is just to move such TSNs out of the
546 * way and into a 'transmitted' queue and skip to the
547 * next chunk.
549 if (chunk->tsn_gap_acked) {
550 list_add_tail(lchunk, &transport->transmitted);
551 lchunk = sctp_list_dequeue(lqueue);
552 continue;
555 /* Attempt to append this chunk to the packet. */
556 status = sctp_packet_append_chunk(pkt, chunk);
558 switch (status) {
559 case SCTP_XMIT_PMTU_FULL:
560 /* Send this packet. */
561 if ((error = sctp_packet_transmit(pkt)) == 0)
562 *start_timer = 1;
564 /* If we are retransmitting, we should only
565 * send a single packet.
567 if (rtx_timeout) {
568 list_add(lchunk, lqueue);
569 lchunk = NULL;
572 /* Bundle lchunk in the next round. */
573 break;
575 case SCTP_XMIT_RWND_FULL:
576 /* Send this packet. */
577 if ((error = sctp_packet_transmit(pkt)) == 0)
578 *start_timer = 1;
580 /* Stop sending DATA as there is no more room
581 * at the receiver.
583 list_add(lchunk, lqueue);
584 lchunk = NULL;
585 break;
587 case SCTP_XMIT_NAGLE_DELAY:
588 /* Send this packet. */
589 if ((error = sctp_packet_transmit(pkt)) == 0)
590 *start_timer = 1;
592 /* Stop sending DATA because of nagle delay. */
593 list_add(lchunk, lqueue);
594 lchunk = NULL;
595 break;
597 default:
598 /* The append was successful, so add this chunk to
599 * the transmitted list.
601 list_add_tail(lchunk, &transport->transmitted);
603 /* Mark the chunk as ineligible for fast retransmit
604 * after it is retransmitted.
606 chunk->fast_retransmit = 0;
608 *start_timer = 1;
609 q->empty = 0;
611 /* Retrieve a new chunk to bundle. */
612 lchunk = sctp_list_dequeue(lqueue);
613 break;
616 /* If we are here due to a retransmit timeout or a fast
617 * retransmit and if there are any chunks left in the retransmit
618 * queue that could not fit in the PMTU sized packet, they need * to be marked as ineligible for a subsequent fast retransmit.
620 if (rtx_timeout && !lchunk) {
621 list_for_each(lchunk1, lqueue) {
622 chunk1 = list_entry(lchunk1, struct sctp_chunk,
623 transmitted_list);
624 chunk1->fast_retransmit = 0;
629 return error;
632 /* Cork the outqueue so queued chunks are really queued. */
633 int sctp_outq_uncork(struct sctp_outq *q)
635 int error = 0;
636 if (q->cork) {
637 q->cork = 0;
638 error = sctp_outq_flush(q, 0);
640 return error;
644 * Try to flush an outqueue.
646 * Description: Send everything in q which we legally can, subject to
647 * congestion limitations.
648 * * Note: This function can be called from multiple contexts so appropriate
649 * locking concerns must be made. Today we use the sock lock to protect
650 * this function.
652 int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
654 struct sctp_packet *packet;
655 struct sctp_packet singleton;
656 struct sctp_association *asoc = q->asoc;
657 __u16 sport = asoc->base.bind_addr.port;
658 __u16 dport = asoc->peer.port;
659 __u32 vtag = asoc->peer.i.init_tag;
660 struct sctp_transport *transport = NULL;
661 struct sctp_transport *new_transport;
662 struct sctp_chunk *chunk, *tmp;
663 sctp_xmit_t status;
664 int error = 0;
665 int start_timer = 0;
667 /* These transports have chunks to send. */
668 struct list_head transport_list;
669 struct list_head *ltransport;
671 INIT_LIST_HEAD(&transport_list);
672 packet = NULL;
675 * 6.10 Bundling
676 * ...
677 * When bundling control chunks with DATA chunks, an
678 * endpoint MUST place control chunks first in the outbound
679 * SCTP packet. The transmitter MUST transmit DATA chunks
680 * within a SCTP packet in increasing order of TSN.
681 * ...
684 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
685 list_del_init(&chunk->list);
687 /* Pick the right transport to use. */
688 new_transport = chunk->transport;
690 if (!new_transport) {
691 new_transport = asoc->peer.active_path;
692 } else if (new_transport->state == SCTP_INACTIVE) {
693 /* If the chunk is Heartbeat or Heartbeat Ack,
694 * send it to chunk->transport, even if it's
695 * inactive.
697 * 3.3.6 Heartbeat Acknowledgement:
698 * ...
699 * A HEARTBEAT ACK is always sent to the source IP
700 * address of the IP datagram containing the
701 * HEARTBEAT chunk to which this ack is responding.
702 * ...
704 if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
705 chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK)
706 new_transport = asoc->peer.active_path;
709 /* Are we switching transports?
710 * Take care of transport locks.
712 if (new_transport != transport) {
713 transport = new_transport;
714 if (list_empty(&transport->send_ready)) {
715 list_add_tail(&transport->send_ready,
716 &transport_list);
718 packet = &transport->packet;
719 sctp_packet_config(packet, vtag,
720 asoc->peer.ecn_capable);
723 switch (chunk->chunk_hdr->type) {
725 * 6.10 Bundling
726 * ...
727 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
728 * COMPLETE with any other chunks. [Send them immediately.]
730 case SCTP_CID_INIT:
731 case SCTP_CID_INIT_ACK:
732 case SCTP_CID_SHUTDOWN_COMPLETE:
733 sctp_packet_init(&singleton, transport, sport, dport);
734 sctp_packet_config(&singleton, vtag, 0);
735 sctp_packet_append_chunk(&singleton, chunk);
736 error = sctp_packet_transmit(&singleton);
737 if (error < 0)
738 return error;
739 break;
741 case SCTP_CID_ABORT:
742 case SCTP_CID_SACK:
743 case SCTP_CID_HEARTBEAT:
744 case SCTP_CID_HEARTBEAT_ACK:
745 case SCTP_CID_SHUTDOWN:
746 case SCTP_CID_SHUTDOWN_ACK:
747 case SCTP_CID_ERROR:
748 case SCTP_CID_COOKIE_ECHO:
749 case SCTP_CID_COOKIE_ACK:
750 case SCTP_CID_ECN_ECNE:
751 case SCTP_CID_ECN_CWR:
752 case SCTP_CID_ASCONF:
753 case SCTP_CID_ASCONF_ACK:
754 case SCTP_CID_FWD_TSN:
755 sctp_packet_transmit_chunk(packet, chunk);
756 break;
758 default:
759 /* We built a chunk with an illegal type! */
760 BUG();
764 /* Is it OK to send data chunks? */
765 switch (asoc->state) {
766 case SCTP_STATE_COOKIE_ECHOED:
767 /* Only allow bundling when this packet has a COOKIE-ECHO
768 * chunk.
770 if (!packet || !packet->has_cookie_echo)
771 break;
773 /* fallthru */
774 case SCTP_STATE_ESTABLISHED:
775 case SCTP_STATE_SHUTDOWN_PENDING:
776 case SCTP_STATE_SHUTDOWN_RECEIVED:
778 * RFC 2960 6.1 Transmission of DATA Chunks
780 * C) When the time comes for the sender to transmit,
781 * before sending new DATA chunks, the sender MUST
782 * first transmit any outstanding DATA chunks which
783 * are marked for retransmission (limited by the
784 * current cwnd).
786 if (!list_empty(&q->retransmit)) {
787 if (transport == asoc->peer.retran_path)
788 goto retran;
790 /* Switch transports & prepare the packet. */
792 transport = asoc->peer.retran_path;
794 if (list_empty(&transport->send_ready)) {
795 list_add_tail(&transport->send_ready,
796 &transport_list);
799 packet = &transport->packet;
800 sctp_packet_config(packet, vtag,
801 asoc->peer.ecn_capable);
802 retran:
803 error = sctp_outq_flush_rtx(q, packet,
804 rtx_timeout, &start_timer);
806 if (start_timer)
807 sctp_transport_reset_timers(transport);
809 /* This can happen on COOKIE-ECHO resend. Only
810 * one chunk can get bundled with a COOKIE-ECHO.
812 if (packet->has_cookie_echo)
813 goto sctp_flush_out;
815 /* Don't send new data if there is still data
816 * waiting to retransmit.
818 if (!list_empty(&q->retransmit))
819 goto sctp_flush_out;
822 /* Finally, transmit new packets. */
823 start_timer = 0;
824 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
825 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
826 * stream identifier.
828 if (chunk->sinfo.sinfo_stream >=
829 asoc->c.sinit_num_ostreams) {
831 /* Mark as failed send. */
832 sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
833 sctp_chunk_free(chunk);
834 continue;
837 /* Has this chunk expired? */
838 if (sctp_chunk_abandoned(chunk)) {
839 sctp_chunk_fail(chunk, 0);
840 sctp_chunk_free(chunk);
841 continue;
844 /* If there is a specified transport, use it.
845 * Otherwise, we want to use the active path.
847 new_transport = chunk->transport;
848 if (!new_transport ||
849 new_transport->state == SCTP_INACTIVE)
850 new_transport = asoc->peer.active_path;
852 /* Change packets if necessary. */
853 if (new_transport != transport) {
854 transport = new_transport;
856 /* Schedule to have this transport's
857 * packet flushed.
859 if (list_empty(&transport->send_ready)) {
860 list_add_tail(&transport->send_ready,
861 &transport_list);
864 packet = &transport->packet;
865 sctp_packet_config(packet, vtag,
866 asoc->peer.ecn_capable);
869 SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
870 q, chunk,
871 chunk && chunk->chunk_hdr ?
872 sctp_cname(SCTP_ST_CHUNK(
873 chunk->chunk_hdr->type))
874 : "Illegal Chunk");
876 SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
877 "%p skb->users %d.\n",
878 ntohl(chunk->subh.data_hdr->tsn),
879 chunk->skb ?chunk->skb->head : NULL,
880 chunk->skb ?
881 atomic_read(&chunk->skb->users) : -1);
883 /* Add the chunk to the packet. */
884 status = sctp_packet_transmit_chunk(packet, chunk);
886 switch (status) {
887 case SCTP_XMIT_PMTU_FULL:
888 case SCTP_XMIT_RWND_FULL:
889 case SCTP_XMIT_NAGLE_DELAY:
890 /* We could not append this chunk, so put
891 * the chunk back on the output queue.
893 SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
894 "not transmit TSN: 0x%x, status: %d\n",
895 ntohl(chunk->subh.data_hdr->tsn),
896 status);
897 sctp_outq_head_data(q, chunk);
898 goto sctp_flush_out;
899 break;
901 case SCTP_XMIT_OK:
902 break;
904 default:
905 BUG();
908 /* BUG: We assume that the sctp_packet_transmit()
909 * call below will succeed all the time and add the
910 * chunk to the transmitted list and restart the
911 * timers.
912 * It is possible that the call can fail under OOM
913 * conditions.
915 * Is this really a problem? Won't this behave
916 * like a lost TSN?
918 list_add_tail(&chunk->transmitted_list,
919 &transport->transmitted);
921 sctp_transport_reset_timers(transport);
923 q->empty = 0;
925 /* Only let one DATA chunk get bundled with a
926 * COOKIE-ECHO chunk.
928 if (packet->has_cookie_echo)
929 goto sctp_flush_out;
931 break;
933 default:
934 /* Do nothing. */
935 break;
938 sctp_flush_out:
940 /* Before returning, examine all the transports touched in
941 * this call. Right now, we bluntly force clear all the
942 * transports. Things might change after we implement Nagle.
943 * But such an examination is still required.
945 * --xguo
947 while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
948 struct sctp_transport *t = list_entry(ltransport,
949 struct sctp_transport,
950 send_ready);
951 packet = &t->packet;
952 if (!sctp_packet_empty(packet))
953 error = sctp_packet_transmit(packet);
956 return error;
959 /* Update unack_data based on the incoming SACK chunk */
960 static void sctp_sack_update_unack_data(struct sctp_association *assoc,
961 struct sctp_sackhdr *sack)
963 sctp_sack_variable_t *frags;
964 __u16 unack_data;
965 int i;
967 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
969 frags = sack->variable;
970 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
971 unack_data -= ((ntohs(frags[i].gab.end) -
972 ntohs(frags[i].gab.start) + 1));
975 assoc->unack_data = unack_data;
978 /* Return the highest new tsn that is acknowledged by the given SACK chunk. */
979 static __u32 sctp_highest_new_tsn(struct sctp_sackhdr *sack,
980 struct sctp_association *asoc)
982 struct list_head *ltransport, *lchunk;
983 struct sctp_transport *transport;
984 struct sctp_chunk *chunk;
985 __u32 highest_new_tsn, tsn;
986 struct list_head *transport_list = &asoc->peer.transport_addr_list;
988 highest_new_tsn = ntohl(sack->cum_tsn_ack);
990 list_for_each(ltransport, transport_list) {
991 transport = list_entry(ltransport, struct sctp_transport,
992 transports);
993 list_for_each(lchunk, &transport->transmitted) {
994 chunk = list_entry(lchunk, struct sctp_chunk,
995 transmitted_list);
996 tsn = ntohl(chunk->subh.data_hdr->tsn);
998 if (!chunk->tsn_gap_acked &&
999 TSN_lt(highest_new_tsn, tsn) &&
1000 sctp_acked(sack, tsn))
1001 highest_new_tsn = tsn;
1005 return highest_new_tsn;
1008 /* This is where we REALLY process a SACK.
1010 * Process the SACK against the outqueue. Mostly, this just frees
1011 * things off the transmitted queue.
1013 int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
1015 struct sctp_association *asoc = q->asoc;
1016 struct sctp_transport *transport;
1017 struct sctp_chunk *tchunk = NULL;
1018 struct list_head *lchunk, *transport_list, *pos, *temp;
1019 sctp_sack_variable_t *frags = sack->variable;
1020 __u32 sack_ctsn, ctsn, tsn;
1021 __u32 highest_tsn, highest_new_tsn;
1022 __u32 sack_a_rwnd;
1023 unsigned outstanding;
1024 struct sctp_transport *primary = asoc->peer.primary_path;
1025 int count_of_newacks = 0;
1027 /* Grab the association's destination address list. */
1028 transport_list = &asoc->peer.transport_addr_list;
1030 sack_ctsn = ntohl(sack->cum_tsn_ack);
1033 * SFR-CACC algorithm:
1034 * On receipt of a SACK the sender SHOULD execute the
1035 * following statements.
1037 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1038 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1039 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1040 * all destinations.
1042 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1043 primary->cacc.changeover_active = 0;
1044 list_for_each(pos, transport_list) {
1045 transport = list_entry(pos, struct sctp_transport,
1046 transports);
1047 transport->cacc.cycling_changeover = 0;
1052 * SFR-CACC algorithm:
1053 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1054 * is set the receiver of the SACK MUST take the following actions:
1056 * A) Initialize the cacc_saw_newack to 0 for all destination
1057 * addresses.
1059 if (sack->num_gap_ack_blocks > 0 &&
1060 primary->cacc.changeover_active) {
1061 list_for_each(pos, transport_list) {
1062 transport = list_entry(pos, struct sctp_transport,
1063 transports);
1064 transport->cacc.cacc_saw_newack = 0;
1068 /* Get the highest TSN in the sack. */
1069 highest_tsn = sack_ctsn;
1070 if (sack->num_gap_ack_blocks)
1071 highest_tsn +=
1072 ntohs(frags[ntohs(sack->num_gap_ack_blocks) - 1].gab.end);
1074 if (TSN_lt(asoc->highest_sacked, highest_tsn)) {
1075 highest_new_tsn = highest_tsn;
1076 asoc->highest_sacked = highest_tsn;
1077 } else {
1078 highest_new_tsn = sctp_highest_new_tsn(sack, asoc);
1081 /* Run through the retransmit queue. Credit bytes received
1082 * and free those chunks that we can.
1084 sctp_check_transmitted(q, &q->retransmit, NULL, sack, highest_new_tsn);
1085 sctp_mark_missing(q, &q->retransmit, NULL, highest_new_tsn, 0);
1087 /* Run through the transmitted queue.
1088 * Credit bytes received and free those chunks which we can.
1090 * This is a MASSIVE candidate for optimization.
1092 list_for_each(pos, transport_list) {
1093 transport = list_entry(pos, struct sctp_transport,
1094 transports);
1095 sctp_check_transmitted(q, &transport->transmitted,
1096 transport, sack, highest_new_tsn);
1098 * SFR-CACC algorithm:
1099 * C) Let count_of_newacks be the number of
1100 * destinations for which cacc_saw_newack is set.
1102 if (transport->cacc.cacc_saw_newack)
1103 count_of_newacks ++;
1106 list_for_each(pos, transport_list) {
1107 transport = list_entry(pos, struct sctp_transport,
1108 transports);
1109 sctp_mark_missing(q, &transport->transmitted, transport,
1110 highest_new_tsn, count_of_newacks);
1113 /* Move the Cumulative TSN Ack Point if appropriate. */
1114 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn))
1115 asoc->ctsn_ack_point = sack_ctsn;
1117 /* Update unack_data field in the assoc. */
1118 sctp_sack_update_unack_data(asoc, sack);
1120 ctsn = asoc->ctsn_ack_point;
1122 /* Throw away stuff rotting on the sack queue. */
1123 list_for_each_safe(lchunk, temp, &q->sacked) {
1124 tchunk = list_entry(lchunk, struct sctp_chunk,
1125 transmitted_list);
1126 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1127 if (TSN_lte(tsn, ctsn))
1128 sctp_chunk_free(tchunk);
1131 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1132 * number of bytes still outstanding after processing the
1133 * Cumulative TSN Ack and the Gap Ack Blocks.
1136 sack_a_rwnd = ntohl(sack->a_rwnd);
1137 outstanding = q->outstanding_bytes;
1139 if (outstanding < sack_a_rwnd)
1140 sack_a_rwnd -= outstanding;
1141 else
1142 sack_a_rwnd = 0;
1144 asoc->peer.rwnd = sack_a_rwnd;
1146 sctp_generate_fwdtsn(q, sack_ctsn);
1148 SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
1149 __FUNCTION__, sack_ctsn);
1150 SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
1151 "%p is 0x%x. Adv peer ack point: 0x%x\n",
1152 __FUNCTION__, asoc, ctsn, asoc->adv_peer_ack_point);
1154 /* See if all chunks are acked.
1155 * Make sure the empty queue handler will get run later.
1157 q->empty = (list_empty(&q->out_chunk_list) &&
1158 list_empty(&q->control_chunk_list) &&
1159 list_empty(&q->retransmit));
1160 if (!q->empty)
1161 goto finish;
1163 list_for_each(pos, transport_list) {
1164 transport = list_entry(pos, struct sctp_transport,
1165 transports);
1166 q->empty = q->empty && list_empty(&transport->transmitted);
1167 if (!q->empty)
1168 goto finish;
1171 SCTP_DEBUG_PRINTK("sack queue is empty.\n");
1172 finish:
1173 return q->empty;
1176 /* Is the outqueue empty? */
1177 int sctp_outq_is_empty(const struct sctp_outq *q)
1179 return q->empty;
1182 /********************************************************************
1183 * 2nd Level Abstractions
1184 ********************************************************************/
1186 /* Go through a transport's transmitted list or the association's retransmit
1187 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1188 * The retransmit list will not have an associated transport.
1190 * I added coherent debug information output. --xguo
1192 * Instead of printing 'sacked' or 'kept' for each TSN on the
1193 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1194 * KEPT TSN6-TSN7, etc.
1196 static void sctp_check_transmitted(struct sctp_outq *q,
1197 struct list_head *transmitted_queue,
1198 struct sctp_transport *transport,
1199 struct sctp_sackhdr *sack,
1200 __u32 highest_new_tsn_in_sack)
1202 struct list_head *lchunk;
1203 struct sctp_chunk *tchunk;
1204 struct list_head tlist;
1205 __u32 tsn;
1206 __u32 sack_ctsn;
1207 __u32 rtt;
1208 __u8 restart_timer = 0;
1209 int bytes_acked = 0;
1211 /* These state variables are for coherent debug output. --xguo */
1213 #if SCTP_DEBUG
1214 __u32 dbg_ack_tsn = 0; /* An ACKed TSN range starts here... */
1215 __u32 dbg_last_ack_tsn = 0; /* ...and finishes here. */
1216 __u32 dbg_kept_tsn = 0; /* An un-ACKed range starts here... */
1217 __u32 dbg_last_kept_tsn = 0; /* ...and finishes here. */
1219 /* 0 : The last TSN was ACKed.
1220 * 1 : The last TSN was NOT ACKed (i.e. KEPT).
1221 * -1: We need to initialize.
1223 int dbg_prt_state = -1;
1224 #endif /* SCTP_DEBUG */
1226 sack_ctsn = ntohl(sack->cum_tsn_ack);
1228 INIT_LIST_HEAD(&tlist);
1230 /* The while loop will skip empty transmitted queues. */
1231 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1232 tchunk = list_entry(lchunk, struct sctp_chunk,
1233 transmitted_list);
1235 if (sctp_chunk_abandoned(tchunk)) {
1236 /* Move the chunk to abandoned list. */
1237 sctp_insert_list(&q->abandoned, lchunk);
1238 continue;
1241 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1242 if (sctp_acked(sack, tsn)) {
1243 /* If this queue is the retransmit queue, the
1244 * retransmit timer has already reclaimed
1245 * the outstanding bytes for this chunk, so only
1246 * count bytes associated with a transport.
1248 if (transport) {
1249 /* If this chunk is being used for RTT
1250 * measurement, calculate the RTT and update
1251 * the RTO using this value.
1253 * 6.3.1 C5) Karn's algorithm: RTT measurements
1254 * MUST NOT be made using packets that were
1255 * retransmitted (and thus for which it is
1256 * ambiguous whether the reply was for the
1257 * first instance of the packet or a later
1258 * instance).
1260 if (!tchunk->tsn_gap_acked &&
1261 !tchunk->resent &&
1262 tchunk->rtt_in_progress) {
1263 rtt = jiffies - tchunk->sent_at;
1264 sctp_transport_update_rto(transport,
1265 rtt);
1268 if (TSN_lte(tsn, sack_ctsn)) {
1269 /* RFC 2960 6.3.2 Retransmission Timer Rules
1271 * R3) Whenever a SACK is received
1272 * that acknowledges the DATA chunk
1273 * with the earliest outstanding TSN
1274 * for that address, restart T3-rtx
1275 * timer for that address with its
1276 * current RTO.
1278 restart_timer = 1;
1280 if (!tchunk->tsn_gap_acked) {
1281 tchunk->tsn_gap_acked = 1;
1282 bytes_acked += sctp_data_size(tchunk);
1284 * SFR-CACC algorithm:
1285 * 2) If the SACK contains gap acks
1286 * and the flag CHANGEOVER_ACTIVE is
1287 * set the receiver of the SACK MUST
1288 * take the following action:
1290 * B) For each TSN t being acked that
1291 * has not been acked in any SACK so
1292 * far, set cacc_saw_newack to 1 for
1293 * the destination that the TSN was
1294 * sent to.
1296 if (transport &&
1297 sack->num_gap_ack_blocks &&
1298 q->asoc->peer.primary_path->cacc.
1299 changeover_active)
1300 transport->cacc.cacc_saw_newack
1301 = 1;
1304 list_add_tail(&tchunk->transmitted_list,
1305 &q->sacked);
1306 } else {
1307 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1308 * M2) Each time a SACK arrives reporting
1309 * 'Stray DATA chunk(s)' record the highest TSN
1310 * reported as newly acknowledged, call this
1311 * value 'HighestTSNinSack'. A newly
1312 * acknowledged DATA chunk is one not
1313 * previously acknowledged in a SACK.
1315 * When the SCTP sender of data receives a SACK
1316 * chunk that acknowledges, for the first time,
1317 * the receipt of a DATA chunk, all the still
1318 * unacknowledged DATA chunks whose TSN is
1319 * older than that newly acknowledged DATA
1320 * chunk, are qualified as 'Stray DATA chunks'.
1322 if (!tchunk->tsn_gap_acked) {
1323 tchunk->tsn_gap_acked = 1;
1324 bytes_acked += sctp_data_size(tchunk);
1326 list_add_tail(lchunk, &tlist);
1329 #if SCTP_DEBUG
1330 switch (dbg_prt_state) {
1331 case 0: /* last TSN was ACKed */
1332 if (dbg_last_ack_tsn + 1 == tsn) {
1333 /* This TSN belongs to the
1334 * current ACK range.
1336 break;
1339 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1340 /* Display the end of the
1341 * current range.
1343 SCTP_DEBUG_PRINTK("-%08x",
1344 dbg_last_ack_tsn);
1347 /* Start a new range. */
1348 SCTP_DEBUG_PRINTK(",%08x", tsn);
1349 dbg_ack_tsn = tsn;
1350 break;
1352 case 1: /* The last TSN was NOT ACKed. */
1353 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1354 /* Display the end of current range. */
1355 SCTP_DEBUG_PRINTK("-%08x",
1356 dbg_last_kept_tsn);
1359 SCTP_DEBUG_PRINTK("\n");
1361 /* FALL THROUGH... */
1362 default:
1363 /* This is the first-ever TSN we examined. */
1364 /* Start a new range of ACK-ed TSNs. */
1365 SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
1366 dbg_prt_state = 0;
1367 dbg_ack_tsn = tsn;
1370 dbg_last_ack_tsn = tsn;
1371 #endif /* SCTP_DEBUG */
1373 } else {
1374 if (tchunk->tsn_gap_acked) {
1375 SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
1376 "data TSN: 0x%x\n",
1377 __FUNCTION__,
1378 tsn);
1379 tchunk->tsn_gap_acked = 0;
1381 bytes_acked -= sctp_data_size(tchunk);
1383 /* RFC 2960 6.3.2 Retransmission Timer Rules
1385 * R4) Whenever a SACK is received missing a
1386 * TSN that was previously acknowledged via a
1387 * Gap Ack Block, start T3-rtx for the
1388 * destination address to which the DATA
1389 * chunk was originally
1390 * transmitted if it is not already running.
1392 restart_timer = 1;
1395 list_add_tail(lchunk, &tlist);
1397 #if SCTP_DEBUG
1398 /* See the above comments on ACK-ed TSNs. */
1399 switch (dbg_prt_state) {
1400 case 1:
1401 if (dbg_last_kept_tsn + 1 == tsn)
1402 break;
1404 if (dbg_last_kept_tsn != dbg_kept_tsn)
1405 SCTP_DEBUG_PRINTK("-%08x",
1406 dbg_last_kept_tsn);
1408 SCTP_DEBUG_PRINTK(",%08x", tsn);
1409 dbg_kept_tsn = tsn;
1410 break;
1412 case 0:
1413 if (dbg_last_ack_tsn != dbg_ack_tsn)
1414 SCTP_DEBUG_PRINTK("-%08x",
1415 dbg_last_ack_tsn);
1416 SCTP_DEBUG_PRINTK("\n");
1418 /* FALL THROUGH... */
1419 default:
1420 SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
1421 dbg_prt_state = 1;
1422 dbg_kept_tsn = tsn;
1425 dbg_last_kept_tsn = tsn;
1426 #endif /* SCTP_DEBUG */
1430 #if SCTP_DEBUG
1431 /* Finish off the last range, displaying its ending TSN. */
1432 switch (dbg_prt_state) {
1433 case 0:
1434 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1435 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_ack_tsn);
1436 } else {
1437 SCTP_DEBUG_PRINTK("\n");
1439 break;
1441 case 1:
1442 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1443 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_kept_tsn);
1444 } else {
1445 SCTP_DEBUG_PRINTK("\n");
1448 #endif /* SCTP_DEBUG */
1449 if (transport) {
1450 if (bytes_acked) {
1451 /* 8.2. When an outstanding TSN is acknowledged,
1452 * the endpoint shall clear the error counter of
1453 * the destination transport address to which the
1454 * DATA chunk was last sent.
1455 * The association's overall error counter is
1456 * also cleared.
1458 transport->error_count = 0;
1459 transport->asoc->overall_error_count = 0;
1461 /* Mark the destination transport address as
1462 * active if it is not so marked.
1464 if (transport->state == SCTP_INACTIVE) {
1465 sctp_assoc_control_transport(
1466 transport->asoc,
1467 transport,
1468 SCTP_TRANSPORT_UP,
1469 SCTP_RECEIVED_SACK);
1472 sctp_transport_raise_cwnd(transport, sack_ctsn,
1473 bytes_acked);
1475 transport->flight_size -= bytes_acked;
1476 q->outstanding_bytes -= bytes_acked;
1477 } else {
1478 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1479 * When a sender is doing zero window probing, it
1480 * should not timeout the association if it continues
1481 * to receive new packets from the receiver. The
1482 * reason is that the receiver MAY keep its window
1483 * closed for an indefinite time.
1484 * A sender is doing zero window probing when the
1485 * receiver's advertised window is zero, and there is
1486 * only one data chunk in flight to the receiver.
1488 if (!q->asoc->peer.rwnd &&
1489 !list_empty(&tlist) &&
1490 (sack_ctsn+2 == q->asoc->next_tsn)) {
1491 SCTP_DEBUG_PRINTK("%s: SACK received for zero "
1492 "window probe: %u\n",
1493 __FUNCTION__, sack_ctsn);
1494 q->asoc->overall_error_count = 0;
1495 transport->error_count = 0;
1499 /* RFC 2960 6.3.2 Retransmission Timer Rules
1501 * R2) Whenever all outstanding data sent to an address have
1502 * been acknowledged, turn off the T3-rtx timer of that
1503 * address.
1505 if (!transport->flight_size) {
1506 if (timer_pending(&transport->T3_rtx_timer) &&
1507 del_timer(&transport->T3_rtx_timer)) {
1508 sctp_transport_put(transport);
1510 } else if (restart_timer) {
1511 if (!mod_timer(&transport->T3_rtx_timer,
1512 jiffies + transport->rto))
1513 sctp_transport_hold(transport);
1517 list_splice(&tlist, transmitted_queue);
1520 /* Mark chunks as missing and consequently may get retransmitted. */
1521 static void sctp_mark_missing(struct sctp_outq *q,
1522 struct list_head *transmitted_queue,
1523 struct sctp_transport *transport,
1524 __u32 highest_new_tsn_in_sack,
1525 int count_of_newacks)
1527 struct sctp_chunk *chunk;
1528 struct list_head *pos;
1529 __u32 tsn;
1530 char do_fast_retransmit = 0;
1531 struct sctp_transport *primary = q->asoc->peer.primary_path;
1533 list_for_each(pos, transmitted_queue) {
1535 chunk = list_entry(pos, struct sctp_chunk, transmitted_list);
1536 tsn = ntohl(chunk->subh.data_hdr->tsn);
1538 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1539 * 'Unacknowledged TSN's', if the TSN number of an
1540 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1541 * value, increment the 'TSN.Missing.Report' count on that
1542 * chunk if it has NOT been fast retransmitted or marked for
1543 * fast retransmit already.
1545 if (!chunk->fast_retransmit &&
1546 !chunk->tsn_gap_acked &&
1547 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1549 /* SFR-CACC may require us to skip marking
1550 * this chunk as missing.
1552 if (!transport || !sctp_cacc_skip(primary, transport,
1553 count_of_newacks, tsn)) {
1554 chunk->tsn_missing_report++;
1556 SCTP_DEBUG_PRINTK(
1557 "%s: TSN 0x%x missing counter: %d\n",
1558 __FUNCTION__, tsn,
1559 chunk->tsn_missing_report);
1563 * M4) If any DATA chunk is found to have a
1564 * 'TSN.Missing.Report'
1565 * value larger than or equal to 4, mark that chunk for
1566 * retransmission and start the fast retransmit procedure.
1569 if (chunk->tsn_missing_report >= 4) {
1570 chunk->fast_retransmit = 1;
1571 do_fast_retransmit = 1;
1575 if (transport) {
1576 if (do_fast_retransmit)
1577 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1579 SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
1580 "ssthresh: %d, flight_size: %d, pba: %d\n",
1581 __FUNCTION__, transport, transport->cwnd,
1582 transport->ssthresh, transport->flight_size,
1583 transport->partial_bytes_acked);
1587 /* Is the given TSN acked by this packet? */
1588 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1590 int i;
1591 sctp_sack_variable_t *frags;
1592 __u16 gap;
1593 __u32 ctsn = ntohl(sack->cum_tsn_ack);
1595 if (TSN_lte(tsn, ctsn))
1596 goto pass;
1598 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1600 * Gap Ack Blocks:
1601 * These fields contain the Gap Ack Blocks. They are repeated
1602 * for each Gap Ack Block up to the number of Gap Ack Blocks
1603 * defined in the Number of Gap Ack Blocks field. All DATA
1604 * chunks with TSNs greater than or equal to (Cumulative TSN
1605 * Ack + Gap Ack Block Start) and less than or equal to
1606 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1607 * Block are assumed to have been received correctly.
1610 frags = sack->variable;
1611 gap = tsn - ctsn;
1612 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
1613 if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
1614 TSN_lte(gap, ntohs(frags[i].gab.end)))
1615 goto pass;
1618 return 0;
1619 pass:
1620 return 1;
1623 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
1624 int nskips, __u16 stream)
1626 int i;
1628 for (i = 0; i < nskips; i++) {
1629 if (skiplist[i].stream == stream)
1630 return i;
1632 return i;
1635 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1636 static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1638 struct sctp_association *asoc = q->asoc;
1639 struct sctp_chunk *ftsn_chunk = NULL;
1640 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1641 int nskips = 0;
1642 int skip_pos = 0;
1643 __u32 tsn;
1644 struct sctp_chunk *chunk;
1645 struct list_head *lchunk, *temp;
1647 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1648 * received SACK.
1650 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1651 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1653 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1654 asoc->adv_peer_ack_point = ctsn;
1656 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1657 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1658 * the chunk next in the out-queue space is marked as "abandoned" as
1659 * shown in the following example:
1661 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1662 * and the Advanced.Peer.Ack.Point is updated to this value:
1664 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1665 * normal SACK processing local advancement
1666 * ... ...
1667 * Adv.Ack.Pt-> 102 acked 102 acked
1668 * 103 abandoned 103 abandoned
1669 * 104 abandoned Adv.Ack.P-> 104 abandoned
1670 * 105 105
1671 * 106 acked 106 acked
1672 * ... ...
1674 * In this example, the data sender successfully advanced the
1675 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1677 list_for_each_safe(lchunk, temp, &q->abandoned) {
1678 chunk = list_entry(lchunk, struct sctp_chunk,
1679 transmitted_list);
1680 tsn = ntohl(chunk->subh.data_hdr->tsn);
1682 /* Remove any chunks in the abandoned queue that are acked by
1683 * the ctsn.
1685 if (TSN_lte(tsn, ctsn)) {
1686 list_del_init(lchunk);
1687 if (!chunk->tsn_gap_acked) {
1688 chunk->transport->flight_size -=
1689 sctp_data_size(chunk);
1690 q->outstanding_bytes -= sctp_data_size(chunk);
1692 sctp_chunk_free(chunk);
1693 } else {
1694 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1695 asoc->adv_peer_ack_point = tsn;
1696 if (chunk->chunk_hdr->flags &
1697 SCTP_DATA_UNORDERED)
1698 continue;
1699 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1700 nskips,
1701 chunk->subh.data_hdr->stream);
1702 ftsn_skip_arr[skip_pos].stream =
1703 chunk->subh.data_hdr->stream;
1704 ftsn_skip_arr[skip_pos].ssn =
1705 chunk->subh.data_hdr->ssn;
1706 if (skip_pos == nskips)
1707 nskips++;
1708 if (nskips == 10)
1709 break;
1710 } else
1711 break;
1715 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1716 * is greater than the Cumulative TSN ACK carried in the received
1717 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1718 * chunk containing the latest value of the
1719 * "Advanced.Peer.Ack.Point".
1721 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1722 * list each stream and sequence number in the forwarded TSN. This
1723 * information will enable the receiver to easily find any
1724 * stranded TSN's waiting on stream reorder queues. Each stream
1725 * SHOULD only be reported once; this means that if multiple
1726 * abandoned messages occur in the same stream then only the
1727 * highest abandoned stream sequence number is reported. If the
1728 * total size of the FORWARD TSN does NOT fit in a single MTU then
1729 * the sender of the FORWARD TSN SHOULD lower the
1730 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1731 * single MTU.
1733 if (asoc->adv_peer_ack_point > ctsn)
1734 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
1735 nskips, &ftsn_skip_arr[0]);
1737 if (ftsn_chunk) {
1738 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1739 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);