Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux/fpc-iii.git] / net / sctp / transport.c
blobf4938f6c5abe09656f33e3ec04d583f00a4eafa7
1 /* SCTP kernel implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2003 International Business Machines Corp.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 La Monte H.P. Yarroll
8 * This file is part of the SCTP kernel implementation
10 * This module provides the abstraction for an SCTP tranport representing
11 * a remote transport address. For local transport addresses, we just use
12 * union sctp_addr.
14 * This SCTP implementation is free software;
15 * you can redistribute it and/or modify it under the terms of
16 * the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
18 * any later version.
20 * This SCTP implementation is distributed in the hope that it
21 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22 * ************************
23 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with GNU CC; see the file COPYING. If not, write to
28 * the Free Software Foundation, 59 Temple Place - Suite 330,
29 * Boston, MA 02111-1307, USA.
31 * Please send any bug reports or fixes you make to the
32 * email address(es):
33 * lksctp developers <lksctp-developers@lists.sourceforge.net>
35 * Or submit a bug report through the following website:
36 * http://www.sf.net/projects/lksctp
38 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Karl Knutson <karl@athena.chicago.il.us>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Xingang Guo <xingang.guo@intel.com>
43 * Hui Huang <hui.huang@nokia.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
51 #include <linux/types.h>
52 #include <linux/random.h>
53 #include <net/sctp/sctp.h>
54 #include <net/sctp/sm.h>
56 /* 1st Level Abstractions. */
58 /* Initialize a new transport from provided memory. */
59 static struct sctp_transport *sctp_transport_init(struct sctp_transport *peer,
60 const union sctp_addr *addr,
61 gfp_t gfp)
63 /* Copy in the address. */
64 peer->ipaddr = *addr;
65 peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
66 peer->asoc = NULL;
68 peer->dst = NULL;
69 memset(&peer->saddr, 0, sizeof(union sctp_addr));
71 /* From 6.3.1 RTO Calculation:
73 * C1) Until an RTT measurement has been made for a packet sent to the
74 * given destination transport address, set RTO to the protocol
75 * parameter 'RTO.Initial'.
77 peer->last_rto = peer->rto = msecs_to_jiffies(sctp_rto_initial);
78 peer->rtt = 0;
79 peer->rttvar = 0;
80 peer->srtt = 0;
81 peer->rto_pending = 0;
83 peer->last_time_heard = jiffies;
84 peer->last_time_used = jiffies;
85 peer->last_time_ecne_reduced = jiffies;
87 peer->init_sent_count = 0;
89 peer->param_flags = SPP_HB_DISABLE |
90 SPP_PMTUD_ENABLE |
91 SPP_SACKDELAY_ENABLE;
92 peer->hbinterval = 0;
94 /* Initialize the default path max_retrans. */
95 peer->pathmaxrxt = sctp_max_retrans_path;
96 peer->error_count = 0;
98 INIT_LIST_HEAD(&peer->transmitted);
99 INIT_LIST_HEAD(&peer->send_ready);
100 INIT_LIST_HEAD(&peer->transports);
102 setup_timer(&peer->T3_rtx_timer, sctp_generate_t3_rtx_event,
103 (unsigned long)peer);
104 setup_timer(&peer->hb_timer, sctp_generate_heartbeat_event,
105 (unsigned long)peer);
107 /* Initialize the 64-bit random nonce sent with heartbeat. */
108 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
110 atomic_set(&peer->refcnt, 1);
111 peer->dead = 0;
113 peer->malloced = 0;
115 /* Initialize the state information for SFR-CACC */
116 peer->cacc.changeover_active = 0;
117 peer->cacc.cycling_changeover = 0;
118 peer->cacc.next_tsn_at_change = 0;
119 peer->cacc.cacc_saw_newack = 0;
121 return peer;
124 /* Allocate and initialize a new transport. */
125 struct sctp_transport *sctp_transport_new(const union sctp_addr *addr,
126 gfp_t gfp)
128 struct sctp_transport *transport;
130 transport = t_new(struct sctp_transport, gfp);
131 if (!transport)
132 goto fail;
134 if (!sctp_transport_init(transport, addr, gfp))
135 goto fail_init;
137 transport->malloced = 1;
138 SCTP_DBG_OBJCNT_INC(transport);
140 return transport;
142 fail_init:
143 kfree(transport);
145 fail:
146 return NULL;
149 /* This transport is no longer needed. Free up if possible, or
150 * delay until it last reference count.
152 void sctp_transport_free(struct sctp_transport *transport)
154 transport->dead = 1;
156 /* Try to delete the heartbeat timer. */
157 if (del_timer(&transport->hb_timer))
158 sctp_transport_put(transport);
160 /* Delete the T3_rtx timer if it's active.
161 * There is no point in not doing this now and letting
162 * structure hang around in memory since we know
163 * the tranport is going away.
165 if (timer_pending(&transport->T3_rtx_timer) &&
166 del_timer(&transport->T3_rtx_timer))
167 sctp_transport_put(transport);
170 sctp_transport_put(transport);
173 /* Destroy the transport data structure.
174 * Assumes there are no more users of this structure.
176 static void sctp_transport_destroy(struct sctp_transport *transport)
178 SCTP_ASSERT(transport->dead, "Transport is not dead", return);
180 if (transport->asoc)
181 sctp_association_put(transport->asoc);
183 sctp_packet_free(&transport->packet);
185 dst_release(transport->dst);
186 kfree(transport);
187 SCTP_DBG_OBJCNT_DEC(transport);
190 /* Start T3_rtx timer if it is not already running and update the heartbeat
191 * timer. This routine is called every time a DATA chunk is sent.
193 void sctp_transport_reset_timers(struct sctp_transport *transport)
195 /* RFC 2960 6.3.2 Retransmission Timer Rules
197 * R1) Every time a DATA chunk is sent to any address(including a
198 * retransmission), if the T3-rtx timer of that address is not running
199 * start it running so that it will expire after the RTO of that
200 * address.
203 if (!timer_pending(&transport->T3_rtx_timer))
204 if (!mod_timer(&transport->T3_rtx_timer,
205 jiffies + transport->rto))
206 sctp_transport_hold(transport);
208 /* When a data chunk is sent, reset the heartbeat interval. */
209 if (!mod_timer(&transport->hb_timer,
210 sctp_transport_timeout(transport)))
211 sctp_transport_hold(transport);
214 /* This transport has been assigned to an association.
215 * Initialize fields from the association or from the sock itself.
216 * Register the reference count in the association.
218 void sctp_transport_set_owner(struct sctp_transport *transport,
219 struct sctp_association *asoc)
221 transport->asoc = asoc;
222 sctp_association_hold(asoc);
225 /* Initialize the pmtu of a transport. */
226 void sctp_transport_pmtu(struct sctp_transport *transport)
228 struct dst_entry *dst;
230 dst = transport->af_specific->get_dst(NULL, &transport->ipaddr, NULL);
232 if (dst) {
233 transport->pathmtu = dst_mtu(dst);
234 dst_release(dst);
235 } else
236 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
239 /* this is a complete rip-off from __sk_dst_check
240 * the cookie is always 0 since this is how it's used in the
241 * pmtu code
243 static struct dst_entry *sctp_transport_dst_check(struct sctp_transport *t)
245 struct dst_entry *dst = t->dst;
247 if (dst && dst->obsolete && dst->ops->check(dst, 0) == NULL) {
248 dst_release(t->dst);
249 t->dst = NULL;
250 return NULL;
253 return dst;
256 void sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
258 struct dst_entry *dst;
260 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
261 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
262 "using default minimum of %d\n",
263 __func__, pmtu,
264 SCTP_DEFAULT_MINSEGMENT);
265 /* Use default minimum segment size and disable
266 * pmtu discovery on this transport.
268 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
269 } else {
270 t->pathmtu = pmtu;
273 dst = sctp_transport_dst_check(t);
274 if (dst)
275 dst->ops->update_pmtu(dst, pmtu);
278 /* Caches the dst entry and source address for a transport's destination
279 * address.
281 void sctp_transport_route(struct sctp_transport *transport,
282 union sctp_addr *saddr, struct sctp_sock *opt)
284 struct sctp_association *asoc = transport->asoc;
285 struct sctp_af *af = transport->af_specific;
286 union sctp_addr *daddr = &transport->ipaddr;
287 struct dst_entry *dst;
289 dst = af->get_dst(asoc, daddr, saddr);
291 if (saddr)
292 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
293 else
294 af->get_saddr(asoc, dst, daddr, &transport->saddr);
296 transport->dst = dst;
297 if ((transport->param_flags & SPP_PMTUD_DISABLE) && transport->pathmtu) {
298 return;
300 if (dst) {
301 transport->pathmtu = dst_mtu(dst);
303 /* Initialize sk->sk_rcv_saddr, if the transport is the
304 * association's active path for getsockname().
306 if (asoc && (transport == asoc->peer.active_path))
307 opt->pf->af->to_sk_saddr(&transport->saddr,
308 asoc->base.sk);
309 } else
310 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
313 /* Hold a reference to a transport. */
314 void sctp_transport_hold(struct sctp_transport *transport)
316 atomic_inc(&transport->refcnt);
319 /* Release a reference to a transport and clean up
320 * if there are no more references.
322 void sctp_transport_put(struct sctp_transport *transport)
324 if (atomic_dec_and_test(&transport->refcnt))
325 sctp_transport_destroy(transport);
328 /* Update transport's RTO based on the newly calculated RTT. */
329 void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
331 /* Check for valid transport. */
332 SCTP_ASSERT(tp, "NULL transport", return);
334 /* We should not be doing any RTO updates unless rto_pending is set. */
335 SCTP_ASSERT(tp->rto_pending, "rto_pending not set", return);
337 if (tp->rttvar || tp->srtt) {
338 /* 6.3.1 C3) When a new RTT measurement R' is made, set
339 * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
340 * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
343 /* Note: The above algorithm has been rewritten to
344 * express rto_beta and rto_alpha as inverse powers
345 * of two.
346 * For example, assuming the default value of RTO.Alpha of
347 * 1/8, rto_alpha would be expressed as 3.
349 tp->rttvar = tp->rttvar - (tp->rttvar >> sctp_rto_beta)
350 + ((abs(tp->srtt - rtt)) >> sctp_rto_beta);
351 tp->srtt = tp->srtt - (tp->srtt >> sctp_rto_alpha)
352 + (rtt >> sctp_rto_alpha);
353 } else {
354 /* 6.3.1 C2) When the first RTT measurement R is made, set
355 * SRTT <- R, RTTVAR <- R/2.
357 tp->srtt = rtt;
358 tp->rttvar = rtt >> 1;
361 /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
362 * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
364 if (tp->rttvar == 0)
365 tp->rttvar = SCTP_CLOCK_GRANULARITY;
367 /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
368 tp->rto = tp->srtt + (tp->rttvar << 2);
370 /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
371 * seconds then it is rounded up to RTO.Min seconds.
373 if (tp->rto < tp->asoc->rto_min)
374 tp->rto = tp->asoc->rto_min;
376 /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
377 * at least RTO.max seconds.
379 if (tp->rto > tp->asoc->rto_max)
380 tp->rto = tp->asoc->rto_max;
382 tp->rtt = rtt;
383 tp->last_rto = tp->rto;
385 /* Reset rto_pending so that a new RTT measurement is started when a
386 * new data chunk is sent.
388 tp->rto_pending = 0;
390 SCTP_DEBUG_PRINTK("%s: transport: %p, rtt: %d, srtt: %d "
391 "rttvar: %d, rto: %ld\n", __func__,
392 tp, rtt, tp->srtt, tp->rttvar, tp->rto);
395 /* This routine updates the transport's cwnd and partial_bytes_acked
396 * parameters based on the bytes acked in the received SACK.
398 void sctp_transport_raise_cwnd(struct sctp_transport *transport,
399 __u32 sack_ctsn, __u32 bytes_acked)
401 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
403 cwnd = transport->cwnd;
404 flight_size = transport->flight_size;
406 /* The appropriate cwnd increase algorithm is performed if, and only
407 * if the cumulative TSN has advanced and the congestion window is
408 * being fully utilized.
410 if ((transport->asoc->ctsn_ack_point >= sack_ctsn) ||
411 (flight_size < cwnd))
412 return;
414 ssthresh = transport->ssthresh;
415 pba = transport->partial_bytes_acked;
416 pmtu = transport->asoc->pathmtu;
418 if (cwnd <= ssthresh) {
419 /* RFC 2960 7.2.1, sctpimpguide-05 2.14.2 When cwnd is less
420 * than or equal to ssthresh an SCTP endpoint MUST use the
421 * slow start algorithm to increase cwnd only if the current
422 * congestion window is being fully utilized and an incoming
423 * SACK advances the Cumulative TSN Ack Point. Only when these
424 * two conditions are met can the cwnd be increased otherwise
425 * the cwnd MUST not be increased. If these conditions are met
426 * then cwnd MUST be increased by at most the lesser of
427 * 1) the total size of the previously outstanding DATA
428 * chunk(s) acknowledged, and 2) the destination's path MTU.
430 if (bytes_acked > pmtu)
431 cwnd += pmtu;
432 else
433 cwnd += bytes_acked;
434 SCTP_DEBUG_PRINTK("%s: SLOW START: transport: %p, "
435 "bytes_acked: %d, cwnd: %d, ssthresh: %d, "
436 "flight_size: %d, pba: %d\n",
437 __func__,
438 transport, bytes_acked, cwnd,
439 ssthresh, flight_size, pba);
440 } else {
441 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
442 * upon each SACK arrival that advances the Cumulative TSN Ack
443 * Point, increase partial_bytes_acked by the total number of
444 * bytes of all new chunks acknowledged in that SACK including
445 * chunks acknowledged by the new Cumulative TSN Ack and by
446 * Gap Ack Blocks.
448 * When partial_bytes_acked is equal to or greater than cwnd
449 * and before the arrival of the SACK the sender had cwnd or
450 * more bytes of data outstanding (i.e., before arrival of the
451 * SACK, flightsize was greater than or equal to cwnd),
452 * increase cwnd by MTU, and reset partial_bytes_acked to
453 * (partial_bytes_acked - cwnd).
455 pba += bytes_acked;
456 if (pba >= cwnd) {
457 cwnd += pmtu;
458 pba = ((cwnd < pba) ? (pba - cwnd) : 0);
460 SCTP_DEBUG_PRINTK("%s: CONGESTION AVOIDANCE: "
461 "transport: %p, bytes_acked: %d, cwnd: %d, "
462 "ssthresh: %d, flight_size: %d, pba: %d\n",
463 __func__,
464 transport, bytes_acked, cwnd,
465 ssthresh, flight_size, pba);
468 transport->cwnd = cwnd;
469 transport->partial_bytes_acked = pba;
472 /* This routine is used to lower the transport's cwnd when congestion is
473 * detected.
475 void sctp_transport_lower_cwnd(struct sctp_transport *transport,
476 sctp_lower_cwnd_t reason)
478 switch (reason) {
479 case SCTP_LOWER_CWND_T3_RTX:
480 /* RFC 2960 Section 7.2.3, sctpimpguide
481 * When the T3-rtx timer expires on an address, SCTP should
482 * perform slow start by:
483 * ssthresh = max(cwnd/2, 4*MTU)
484 * cwnd = 1*MTU
485 * partial_bytes_acked = 0
487 transport->ssthresh = max(transport->cwnd/2,
488 4*transport->asoc->pathmtu);
489 transport->cwnd = transport->asoc->pathmtu;
490 break;
492 case SCTP_LOWER_CWND_FAST_RTX:
493 /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
494 * destination address(es) to which the missing DATA chunks
495 * were last sent, according to the formula described in
496 * Section 7.2.3.
498 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
499 * losses from SACK (see Section 7.2.4), An endpoint
500 * should do the following:
501 * ssthresh = max(cwnd/2, 4*MTU)
502 * cwnd = ssthresh
503 * partial_bytes_acked = 0
505 transport->ssthresh = max(transport->cwnd/2,
506 4*transport->asoc->pathmtu);
507 transport->cwnd = transport->ssthresh;
508 break;
510 case SCTP_LOWER_CWND_ECNE:
511 /* RFC 2481 Section 6.1.2.
512 * If the sender receives an ECN-Echo ACK packet
513 * then the sender knows that congestion was encountered in the
514 * network on the path from the sender to the receiver. The
515 * indication of congestion should be treated just as a
516 * congestion loss in non-ECN Capable TCP. That is, the TCP
517 * source halves the congestion window "cwnd" and reduces the
518 * slow start threshold "ssthresh".
519 * A critical condition is that TCP does not react to
520 * congestion indications more than once every window of
521 * data (or more loosely more than once every round-trip time).
523 if ((jiffies - transport->last_time_ecne_reduced) >
524 transport->rtt) {
525 transport->ssthresh = max(transport->cwnd/2,
526 4*transport->asoc->pathmtu);
527 transport->cwnd = transport->ssthresh;
528 transport->last_time_ecne_reduced = jiffies;
530 break;
532 case SCTP_LOWER_CWND_INACTIVE:
533 /* RFC 2960 Section 7.2.1, sctpimpguide
534 * When the endpoint does not transmit data on a given
535 * transport address, the cwnd of the transport address
536 * should be adjusted to max(cwnd/2, 4*MTU) per RTO.
537 * NOTE: Although the draft recommends that this check needs
538 * to be done every RTO interval, we do it every hearbeat
539 * interval.
541 if ((jiffies - transport->last_time_used) > transport->rto)
542 transport->cwnd = max(transport->cwnd/2,
543 4*transport->asoc->pathmtu);
544 break;
547 transport->partial_bytes_acked = 0;
548 SCTP_DEBUG_PRINTK("%s: transport: %p reason: %d cwnd: "
549 "%d ssthresh: %d\n", __func__,
550 transport, reason,
551 transport->cwnd, transport->ssthresh);
554 /* What is the next timeout value for this transport? */
555 unsigned long sctp_transport_timeout(struct sctp_transport *t)
557 unsigned long timeout;
558 timeout = t->rto + sctp_jitter(t->rto);
559 if (t->state != SCTP_UNCONFIRMED)
560 timeout += t->hbinterval;
561 timeout += jiffies;
562 return timeout;
565 /* Reset transport variables to their initial values */
566 void sctp_transport_reset(struct sctp_transport *t)
568 struct sctp_association *asoc = t->asoc;
570 /* RFC 2960 (bis), Section 5.2.4
571 * All the congestion control parameters (e.g., cwnd, ssthresh)
572 * related to this peer MUST be reset to their initial values
573 * (see Section 6.2.1)
575 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
576 t->ssthresh = asoc->peer.i.a_rwnd;
577 t->last_rto = t->rto = asoc->rto_initial;
578 t->rtt = 0;
579 t->srtt = 0;
580 t->rttvar = 0;
582 /* Reset these additional varibles so that we have a clean
583 * slate.
585 t->partial_bytes_acked = 0;
586 t->flight_size = 0;
587 t->error_count = 0;
588 t->rto_pending = 0;
590 /* Initialize the state information for SFR-CACC */
591 t->cacc.changeover_active = 0;
592 t->cacc.cycling_changeover = 0;
593 t->cacc.next_tsn_at_change = 0;
594 t->cacc.cacc_saw_newack = 0;