ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sctp / transport.c
1 /* SCTP kernel reference 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
7  *
8  * This file is part of the SCTP kernel reference Implementation
9  *
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.
13  *
14  * The SCTP reference 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.
19  *
20  * The SCTP reference 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.
25  *
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.
30  *
31  * Please send any bug reports or fixes you make to the
32  * email address(es):
33  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
34  *
35  * Or submit a bug report through the following website:
36  *    http://www.sf.net/projects/lksctp
37  *
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>
46  *
47  * Any bugs reported given to us we will try to fix... any fixes shared will
48  * be incorporated into the next SCTP release.
49  */
50
51 #include <linux/types.h>
52 #include <net/sctp/sctp.h>
53 #include <net/sctp/sm.h>
54
55 /* 1st Level Abstractions.  */
56
57 /* Allocate and initialize a new transport.  */
58 struct sctp_transport *sctp_transport_new(const union sctp_addr *addr, int gfp)
59 {
60         struct sctp_transport *transport;
61
62         transport = t_new(struct sctp_transport, gfp);
63         if (!transport)
64                 goto fail;
65
66         if (!sctp_transport_init(transport, addr, gfp))
67                 goto fail_init;
68
69         transport->malloced = 1;
70         SCTP_DBG_OBJCNT_INC(transport);
71
72         return transport;
73
74 fail_init:
75         kfree(transport);
76
77 fail:
78         return NULL;
79 }
80
81 /* Initialize a new transport from provided memory.  */
82 struct sctp_transport *sctp_transport_init(struct sctp_transport *peer,
83                                            const union sctp_addr *addr,
84                                            int gfp)
85 {
86         /* Copy in the address.  */
87         peer->ipaddr = *addr;
88         peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
89         peer->asoc = NULL;
90
91         peer->dst = NULL;
92         memset(&peer->saddr, 0, sizeof(union sctp_addr));
93
94         /* From 6.3.1 RTO Calculation:
95          *
96          * C1) Until an RTT measurement has been made for a packet sent to the
97          * given destination transport address, set RTO to the protocol
98          * parameter 'RTO.Initial'.
99          */
100         peer->rtt = 0;
101         peer->rto = sctp_rto_initial;
102         peer->rttvar = 0;
103         peer->srtt = 0;
104         peer->rto_pending = 0;
105
106         peer->last_time_heard = jiffies;
107         peer->last_time_used = jiffies;
108         peer->last_time_ecne_reduced = jiffies;
109
110         peer->active = SCTP_ACTIVE;
111         peer->hb_allowed = 0;
112
113         /* Initialize the default path max_retrans.  */
114         peer->max_retrans = sctp_max_retrans_path;
115         peer->error_threshold = 0;
116         peer->error_count = 0;
117
118         INIT_LIST_HEAD(&peer->transmitted);
119         INIT_LIST_HEAD(&peer->send_ready);
120         INIT_LIST_HEAD(&peer->transports);
121
122         /* Set up the retransmission timer.  */
123         init_timer(&peer->T3_rtx_timer);
124         peer->T3_rtx_timer.function = sctp_generate_t3_rtx_event;
125         peer->T3_rtx_timer.data = (unsigned long)peer;
126
127         /* Set up the heartbeat timer. */
128         init_timer(&peer->hb_timer);
129         peer->hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
130         peer->hb_timer.function = sctp_generate_heartbeat_event;
131         peer->hb_timer.data = (unsigned long)peer;
132
133         atomic_set(&peer->refcnt, 1);
134         peer->dead = 0;
135
136         peer->malloced = 0;
137
138         /* Initialize the state information for SFR-CACC */
139         peer->cacc.changeover_active = 0;
140         peer->cacc.cycling_changeover = 0;
141         peer->cacc.next_tsn_at_change = 0;
142         peer->cacc.cacc_saw_newack = 0;
143
144         return peer;
145 }
146
147 /* This transport is no longer needed.  Free up if possible, or
148  * delay until it last reference count.
149  */
150 void sctp_transport_free(struct sctp_transport *transport)
151 {
152         transport->dead = 1;
153
154         /* Try to delete the heartbeat timer.  */
155         if (del_timer(&transport->hb_timer))
156                 sctp_transport_put(transport);
157
158         sctp_transport_put(transport);
159 }
160
161 /* Destroy the transport data structure.
162  * Assumes there are no more users of this structure.
163  */
164 void sctp_transport_destroy(struct sctp_transport *transport)
165 {
166         SCTP_ASSERT(transport->dead, "Transport is not dead", return);
167
168         if (transport->asoc)
169                 sctp_association_put(transport->asoc);
170
171         sctp_packet_free(&transport->packet);
172
173         dst_release(transport->dst);
174         kfree(transport);
175         SCTP_DBG_OBJCNT_DEC(transport);
176 }
177
178 /* Start T3_rtx timer if it is not already running and update the heartbeat
179  * timer.  This routine is called every time a DATA chunk is sent.
180  */
181 void sctp_transport_reset_timers(struct sctp_transport *transport)
182 {
183         /* RFC 2960 6.3.2 Retransmission Timer Rules
184          *
185          * R1) Every time a DATA chunk is sent to any address(including a
186          * retransmission), if the T3-rtx timer of that address is not running
187          * start it running so that it will expire after the RTO of that
188          * address.
189          */
190
191         if (!timer_pending(&transport->T3_rtx_timer))
192                 if (!mod_timer(&transport->T3_rtx_timer,
193                                jiffies + transport->rto))
194                         sctp_transport_hold(transport);
195
196         /* When a data chunk is sent, reset the heartbeat interval.  */
197         if (!mod_timer(&transport->hb_timer,
198                        sctp_transport_timeout(transport)))
199             sctp_transport_hold(transport);
200 }
201
202 /* This transport has been assigned to an association.
203  * Initialize fields from the association or from the sock itself.
204  * Register the reference count in the association.
205  */
206 void sctp_transport_set_owner(struct sctp_transport *transport,
207                               struct sctp_association *asoc)
208 {
209         transport->asoc = asoc;
210         sctp_association_hold(asoc);
211 }
212
213 /* Initialize the pmtu of a transport. */
214 void sctp_transport_pmtu(struct sctp_transport *transport)
215 {
216         struct dst_entry *dst;
217
218         dst = transport->af_specific->get_dst(NULL, &transport->ipaddr, NULL);
219
220         if (dst) {
221                 transport->pmtu = dst_pmtu(dst);
222                 dst_release(dst);
223         } else
224                 transport->pmtu = SCTP_DEFAULT_MAXSEGMENT;
225 }
226
227 /* Caches the dst entry and source address for a transport's destination
228  * address.
229  */
230 void sctp_transport_route(struct sctp_transport *transport,
231                           union sctp_addr *saddr, struct sctp_opt *opt)
232 {
233         struct sctp_association *asoc = transport->asoc;
234         struct sctp_af *af = transport->af_specific;
235         union sctp_addr *daddr = &transport->ipaddr;
236         struct dst_entry *dst;
237
238         dst = af->get_dst(asoc, daddr, saddr);
239
240         if (saddr)
241                 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
242         else
243                 af->get_saddr(asoc, dst, daddr, &transport->saddr);
244
245         transport->dst = dst;
246         if (dst) {
247                 transport->pmtu = dst_pmtu(dst);
248
249                 /* Initialize sk->sk_rcv_saddr, if the transport is the
250                  * association's active path for getsockname().
251                  */ 
252                 if (asoc && (transport == asoc->peer.active_path))
253                         af->to_sk_saddr(&transport->saddr, asoc->base.sk);
254         } else
255                 transport->pmtu = SCTP_DEFAULT_MAXSEGMENT;
256 }
257
258 /* Hold a reference to a transport.  */
259 void sctp_transport_hold(struct sctp_transport *transport)
260 {
261         atomic_inc(&transport->refcnt);
262 }
263
264 /* Release a reference to a transport and clean up
265  * if there are no more references.
266  */
267 void sctp_transport_put(struct sctp_transport *transport)
268 {
269         if (atomic_dec_and_test(&transport->refcnt))
270                 sctp_transport_destroy(transport);
271 }
272
273 /* Update transport's RTO based on the newly calculated RTT. */
274 void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
275 {
276         /* Check for valid transport.  */
277         SCTP_ASSERT(tp, "NULL transport", return);
278
279         /* We should not be doing any RTO updates unless rto_pending is set.  */
280         SCTP_ASSERT(tp->rto_pending, "rto_pending not set", return);
281
282         if (tp->rttvar || tp->srtt) {
283                 /* 6.3.1 C3) When a new RTT measurement R' is made, set
284                  * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
285                  * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
286                  */
287
288                 /* Note:  The above algorithm has been rewritten to
289                  * express rto_beta and rto_alpha as inverse powers
290                  * of two.
291                  * For example, assuming the default value of RTO.Alpha of
292                  * 1/8, rto_alpha would be expressed as 3.
293                  */
294                 tp->rttvar = tp->rttvar - (tp->rttvar >> sctp_rto_beta)
295                         + ((abs(tp->srtt - rtt)) >> sctp_rto_beta);
296                 tp->srtt = tp->srtt - (tp->srtt >> sctp_rto_alpha)
297                         + (rtt >> sctp_rto_alpha);
298         } else {
299                 /* 6.3.1 C2) When the first RTT measurement R is made, set
300                  * SRTT <- R, RTTVAR <- R/2.
301                  */
302                 tp->srtt = rtt;
303                 tp->rttvar = rtt >> 1;
304         }
305
306         /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
307          * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
308          */
309         if (tp->rttvar == 0)
310                 tp->rttvar = SCTP_CLOCK_GRANULARITY;
311
312         /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
313         tp->rto = tp->srtt + (tp->rttvar << 2);
314
315         /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
316          * seconds then it is rounded up to RTO.Min seconds.
317          */
318         if (tp->rto < tp->asoc->rto_min)
319                 tp->rto = tp->asoc->rto_min;
320
321         /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
322          * at least RTO.max seconds.
323          */
324         if (tp->rto > tp->asoc->rto_max)
325                 tp->rto = tp->asoc->rto_max;
326
327         tp->rtt = rtt;
328
329         /* Reset rto_pending so that a new RTT measurement is started when a
330          * new data chunk is sent.
331          */
332         tp->rto_pending = 0;
333
334         SCTP_DEBUG_PRINTK("%s: transport: %p, rtt: %d, srtt: %d "
335                           "rttvar: %d, rto: %d\n", __FUNCTION__,
336                           tp, rtt, tp->srtt, tp->rttvar, tp->rto);
337 }
338
339 /* This routine updates the transport's cwnd and partial_bytes_acked
340  * parameters based on the bytes acked in the received SACK.
341  */
342 void sctp_transport_raise_cwnd(struct sctp_transport *transport,
343                                __u32 sack_ctsn, __u32 bytes_acked)
344 {
345         __u32 cwnd, ssthresh, flight_size, pba, pmtu;
346
347         cwnd = transport->cwnd;
348         flight_size = transport->flight_size;
349
350         /* The appropriate cwnd increase algorithm is performed if, and only
351          * if the cumulative TSN has advanced and the congestion window is
352          * being fully utilized.
353          */
354         if ((transport->asoc->ctsn_ack_point >= sack_ctsn) ||
355             (flight_size < cwnd))
356                 return;
357
358         ssthresh = transport->ssthresh;
359         pba = transport->partial_bytes_acked;
360         pmtu = transport->asoc->pmtu;
361
362         if (cwnd <= ssthresh) {
363                 /* RFC 2960 7.2.1, sctpimpguide-05 2.14.2 When cwnd is less
364                  * than or equal to ssthresh an SCTP endpoint MUST use the
365                  * slow start algorithm to increase cwnd only if the current
366                  * congestion window is being fully utilized and an incoming
367                  * SACK advances the Cumulative TSN Ack Point. Only when these
368                  * two conditions are met can the cwnd be increased otherwise
369                  * the cwnd MUST not be increased. If these conditions are met
370                  * then cwnd MUST be increased by at most the lesser of
371                  * 1) the total size of the previously outstanding DATA
372                  * chunk(s) acknowledged, and 2) the destination's path MTU.
373                  */
374                 if (bytes_acked > pmtu)
375                         cwnd += pmtu;
376                 else
377                         cwnd += bytes_acked;
378                 SCTP_DEBUG_PRINTK("%s: SLOW START: transport: %p, "
379                                   "bytes_acked: %d, cwnd: %d, ssthresh: %d, "
380                                   "flight_size: %d, pba: %d\n",
381                                   __FUNCTION__,
382                                   transport, bytes_acked, cwnd,
383                                   ssthresh, flight_size, pba);
384         } else {
385                 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
386                  * upon each SACK arrival that advances the Cumulative TSN Ack
387                  * Point, increase partial_bytes_acked by the total number of
388                  * bytes of all new chunks acknowledged in that SACK including
389                  * chunks acknowledged by the new Cumulative TSN Ack and by
390                  * Gap Ack Blocks.
391                  *
392                  * When partial_bytes_acked is equal to or greater than cwnd
393                  * and before the arrival of the SACK the sender had cwnd or
394                  * more bytes of data outstanding (i.e., before arrival of the
395                  * SACK, flightsize was greater than or equal to cwnd),
396                  * increase cwnd by MTU, and reset partial_bytes_acked to
397                  * (partial_bytes_acked - cwnd).
398                  */
399                 pba += bytes_acked;
400                 if (pba >= cwnd) {
401                         cwnd += pmtu;
402                         pba = ((cwnd < pba) ? (pba - cwnd) : 0);
403                 }
404                 SCTP_DEBUG_PRINTK("%s: CONGESTION AVOIDANCE: "
405                                   "transport: %p, bytes_acked: %d, cwnd: %d, "
406                                   "ssthresh: %d, flight_size: %d, pba: %d\n",
407                                   __FUNCTION__,
408                                   transport, bytes_acked, cwnd,
409                                   ssthresh, flight_size, pba);
410         }
411
412         transport->cwnd = cwnd;
413         transport->partial_bytes_acked = pba;
414 }
415
416 /* This routine is used to lower the transport's cwnd when congestion is
417  * detected.
418  */
419 void sctp_transport_lower_cwnd(struct sctp_transport *transport,
420                                sctp_lower_cwnd_t reason)
421 {
422         switch (reason) {
423         case SCTP_LOWER_CWND_T3_RTX:
424                 /* RFC 2960 Section 7.2.3, sctpimpguide-05 Section 2.9.2
425                  * When the T3-rtx timer expires on an address, SCTP should
426                  * perform slow start by:
427                  *      ssthresh = max(cwnd/2, 2*MTU)
428                  *      cwnd = 1*MTU
429                  *      partial_bytes_acked = 0
430                  */
431                 transport->ssthresh = max(transport->cwnd/2,
432                                           2*transport->asoc->pmtu);
433                 transport->cwnd = transport->asoc->pmtu;
434                 break;
435
436         case SCTP_LOWER_CWND_FAST_RTX:
437                 /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
438                  * destination address(es) to which the missing DATA chunks
439                  * were last sent, according to the formula described in
440                  * Section 7.2.3.
441                  *
442                  * RFC 2960 7.2.3, sctpimpguide-05 2.9.2 Upon detection of
443                  * packet losses from SACK (see Section 7.2.4), An endpoint
444                  * should do the following:
445                  *      ssthresh = max(cwnd/2, 2*MTU)
446                  *      cwnd = ssthresh
447                  *      partial_bytes_acked = 0
448                  */
449                 transport->ssthresh = max(transport->cwnd/2,
450                                           2*transport->asoc->pmtu);
451                 transport->cwnd = transport->ssthresh;
452                 break;
453
454         case SCTP_LOWER_CWND_ECNE:
455                 /* RFC 2481 Section 6.1.2.
456                  * If the sender receives an ECN-Echo ACK packet
457                  * then the sender knows that congestion was encountered in the
458                  * network on the path from the sender to the receiver. The
459                  * indication of congestion should be treated just as a
460                  * congestion loss in non-ECN Capable TCP. That is, the TCP
461                  * source halves the congestion window "cwnd" and reduces the
462                  * slow start threshold "ssthresh".
463                  * A critical condition is that TCP does not react to
464                  * congestion indications more than once every window of
465                  * data (or more loosely more than once every round-trip time).
466                  */
467                 if ((jiffies - transport->last_time_ecne_reduced) >
468                     transport->rtt) {
469                         transport->ssthresh = max(transport->cwnd/2,
470                                                   2*transport->asoc->pmtu);
471                         transport->cwnd = transport->ssthresh;
472                         transport->last_time_ecne_reduced = jiffies;
473                 }
474                 break;
475
476         case SCTP_LOWER_CWND_INACTIVE:
477                 /* RFC 2960 Section 7.2.1, sctpimpguide-05 Section 2.14.2
478                  * When the association does not transmit data on a given
479                  * transport address within an RTO, the cwnd of the transport
480                  * address should be adjusted to 2*MTU.
481                  * NOTE: Although the draft recommends that this check needs
482                  * to be done every RTO interval, we do it every hearbeat
483                  * interval.
484                  */
485                 if ((jiffies - transport->last_time_used) > transport->rto)
486                         transport->cwnd = 2*transport->asoc->pmtu;
487                 break;
488         };
489
490         transport->partial_bytes_acked = 0;
491         SCTP_DEBUG_PRINTK("%s: transport: %p reason: %d cwnd: "
492                           "%d ssthresh: %d\n", __FUNCTION__,
493                           transport, reason,
494                           transport->cwnd, transport->ssthresh);
495 }
496
497 /* What is the next timeout value for this transport? */
498 unsigned long sctp_transport_timeout(struct sctp_transport *t)
499 {
500         unsigned long timeout;
501         timeout = t->hb_interval + t->rto + sctp_jitter(t->rto);
502         timeout += jiffies;
503         return timeout;
504 }