]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/dccp/ccids/ccid3.c
[DCCP] CCID: Allow ccid_{init,exit} to be NULL
[linux-2.6-omap-h63xx.git] / net / dccp / ccids / ccid3.c
1 /*
2  *  net/dccp/ccids/ccid3.c
3  *
4  *  Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5  *  Copyright (c) 2005-6 Ian McDonald <imcdnzl@gmail.com>
6  *
7  *  An implementation of the DCCP protocol
8  *
9  *  This code has been developed by the University of Waikato WAND
10  *  research group. For further information please see http://www.wand.net.nz/
11  *
12  *  This code also uses code from Lulea University, rereleased as GPL by its
13  *  authors:
14  *  Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15  *
16  *  Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17  *  and to make it work as a loadable module in the DCCP stack written by
18  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
19  *
20  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21  *
22  *  This program is free software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation; either version 2 of the License, or
25  *  (at your option) any later version.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to the Free Software
34  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35  */
36
37 #include <linux/config.h>
38 #include "../ccid.h"
39 #include "../dccp.h"
40 #include "lib/packet_history.h"
41 #include "lib/loss_interval.h"
42 #include "lib/tfrc.h"
43 #include "ccid3.h"
44
45 /*
46  * Reason for maths here is to avoid 32 bit overflow when a is big.
47  * With this we get close to the limit.
48  */
49 static inline u32 usecs_div(const u32 a, const u32 b)
50 {
51         const u32 div = a < (UINT_MAX / (USEC_PER_SEC /    10)) ?    10 :
52                         a < (UINT_MAX / (USEC_PER_SEC /    50)) ?    50 :
53                         a < (UINT_MAX / (USEC_PER_SEC /   100)) ?   100 :
54                         a < (UINT_MAX / (USEC_PER_SEC /   500)) ?   500 :
55                         a < (UINT_MAX / (USEC_PER_SEC /  1000)) ?  1000 :
56                         a < (UINT_MAX / (USEC_PER_SEC /  5000)) ?  5000 :
57                         a < (UINT_MAX / (USEC_PER_SEC / 10000)) ? 10000 :
58                         a < (UINT_MAX / (USEC_PER_SEC / 50000)) ? 50000 :
59                                                                  100000;
60         const u32 tmp = a * (USEC_PER_SEC / div);
61         return (b >= 2 * div) ? tmp / (b / div) : tmp;
62 }
63
64 static int ccid3_debug;
65
66 #ifdef CCID3_DEBUG
67 #define ccid3_pr_debug(format, a...) \
68         do { if (ccid3_debug) \
69                 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
70         } while (0)
71 #else
72 #define ccid3_pr_debug(format, a...)
73 #endif
74
75 static struct dccp_tx_hist *ccid3_tx_hist;
76 static struct dccp_rx_hist *ccid3_rx_hist;
77 static struct dccp_li_hist *ccid3_li_hist;
78
79 /* TFRC sender states */
80 enum ccid3_hc_tx_states {
81         TFRC_SSTATE_NO_SENT = 1,
82         TFRC_SSTATE_NO_FBACK,
83         TFRC_SSTATE_FBACK,
84         TFRC_SSTATE_TERM,
85 };
86
87 #ifdef CCID3_DEBUG
88 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
89 {
90         static char *ccid3_state_names[] = {
91         [TFRC_SSTATE_NO_SENT]  = "NO_SENT",
92         [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
93         [TFRC_SSTATE_FBACK]    = "FBACK",
94         [TFRC_SSTATE_TERM]     = "TERM",
95         };
96
97         return ccid3_state_names[state];
98 }
99 #endif
100
101 static inline void ccid3_hc_tx_set_state(struct sock *sk,
102                                          enum ccid3_hc_tx_states state)
103 {
104         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
105         enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
106
107         ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
108                        dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
109                        ccid3_tx_state_name(state));
110         WARN_ON(state == oldstate);
111         hctx->ccid3hctx_state = state;
112 }
113
114 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
115 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
116 {
117         /*
118          * If no feedback spec says t_ipi is 1 second (set elsewhere and then
119          * doubles after every no feedback timer (separate function)
120          */
121         if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
122                 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
123                                                   hctx->ccid3hctx_x);
124 }
125
126 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
127 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
128 {
129         hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
130                                            TFRC_OPSYS_HALF_TIME_GRAN);
131 }
132
133 /*
134  * Update X by
135  *    If (p > 0)
136  *       x_calc = calcX(s, R, p);
137  *       X = max(min(X_calc, 2 * X_recv), s / t_mbi);
138  *    Else
139  *       If (now - tld >= R)
140  *          X = max(min(2 * X, 2 * X_recv), s / R);
141  *          tld = now;
142  */ 
143 static void ccid3_hc_tx_update_x(struct sock *sk)
144 {
145         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
146
147         /* To avoid large error in calcX */
148         if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
149                 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
150                                                      hctx->ccid3hctx_rtt,
151                                                      hctx->ccid3hctx_p);
152                 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
153                                                           2 * hctx->ccid3hctx_x_recv),
154                                                (hctx->ccid3hctx_s /
155                                                 TFRC_MAX_BACK_OFF_TIME));
156         } else {
157                 struct timeval now;
158
159                 dccp_timestamp(sk, &now);
160                 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
161                     hctx->ccid3hctx_rtt) {
162                         hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
163                                                                   hctx->ccid3hctx_x) * 2,
164                                                        usecs_div(hctx->ccid3hctx_s,
165                                                                  hctx->ccid3hctx_rtt));
166                         hctx->ccid3hctx_t_ld = now;
167                 }
168         }
169 }
170
171 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
172 {
173         struct sock *sk = (struct sock *)data;
174         unsigned long next_tmout = 0;
175         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
176
177         bh_lock_sock(sk);
178         if (sock_owned_by_user(sk)) {
179                 /* Try again later. */
180                 /* XXX: set some sensible MIB */
181                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
182                                jiffies + HZ / 5);
183                 goto out;
184         }
185
186         ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
187                        ccid3_tx_state_name(hctx->ccid3hctx_state));
188         
189         switch (hctx->ccid3hctx_state) {
190         case TFRC_SSTATE_TERM:
191                 goto out;
192         case TFRC_SSTATE_NO_FBACK:
193                 /* Halve send rate */
194                 hctx->ccid3hctx_x /= 2;
195                 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
196                                          TFRC_MAX_BACK_OFF_TIME))
197                         hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
198                                              TFRC_MAX_BACK_OFF_TIME);
199
200                 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
201                                "bytes/s\n",
202                                dccp_role(sk), sk,
203                                ccid3_tx_state_name(hctx->ccid3hctx_state),
204                                hctx->ccid3hctx_x);
205                 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
206                                                       hctx->ccid3hctx_x),
207                                         TFRC_INITIAL_TIMEOUT);
208                 /*
209                  * FIXME - not sure above calculation is correct. See section
210                  * 5 of CCID3 11 should adjust tx_t_ipi and double that to
211                  * achieve it really
212                  */
213                 break;
214         case TFRC_SSTATE_FBACK:
215                 /*
216                  * Check if IDLE since last timeout and recv rate is less than
217                  * 4 packets per RTT
218                  */
219                 if (!hctx->ccid3hctx_idle ||
220                     (hctx->ccid3hctx_x_recv >=
221                      4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
222                         ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
223                                        dccp_role(sk), sk,
224                                        ccid3_tx_state_name(hctx->ccid3hctx_state));
225                         /* Halve sending rate */
226
227                         /*  If (X_calc > 2 * X_recv)
228                          *    X_recv = max(X_recv / 2, s / (2 * t_mbi));
229                          *  Else
230                          *    X_recv = X_calc / 4;
231                          */
232                         BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
233                                hctx->ccid3hctx_x_calc == 0);
234
235                         /* check also if p is zero -> x_calc is infinity? */
236                         if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
237                             hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
238                                 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
239                                                                     hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
240                         else
241                                 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
242
243                         /* Update sending rate */
244                         ccid3_hc_tx_update_x(sk);
245                 }
246                 /*
247                  * Schedule no feedback timer to expire in
248                  * max(4 * R, 2 * s / X)
249                  */
250                 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto, 
251                                         2 * usecs_div(hctx->ccid3hctx_s,
252                                                       hctx->ccid3hctx_x));
253                 break;
254         default:
255                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
256                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
257                 dump_stack();
258                 goto out;
259         }
260
261         sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer, 
262                       jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
263         hctx->ccid3hctx_idle = 1;
264 out:
265         bh_unlock_sock(sk);
266         sock_put(sk);
267 }
268
269 static int ccid3_hc_tx_send_packet(struct sock *sk,
270                                    struct sk_buff *skb, int len)
271 {
272         struct dccp_sock *dp = dccp_sk(sk);
273         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
274         struct dccp_tx_hist_entry *new_packet;
275         struct timeval now;
276         long delay;
277         int rc = -ENOTCONN;
278
279         BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
280
281         /* Check if pure ACK or Terminating*/
282         /*
283          * XXX: We only call this function for DATA and DATAACK, on, these
284          * packets can have zero length, but why the comment about "pure ACK"?
285          */
286         if (unlikely(len == 0))
287                 goto out;
288
289         /* See if last packet allocated was not sent */
290         new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
291         if (new_packet == NULL || new_packet->dccphtx_sent) {
292                 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
293                                                     SLAB_ATOMIC);
294
295                 rc = -ENOBUFS;
296                 if (unlikely(new_packet == NULL)) {
297                         LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, not enough "
298                                        "mem to add to history, send refused\n",
299                                        __FUNCTION__, dccp_role(sk), sk);
300                         goto out;
301                 }
302
303                 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
304         }
305
306         dccp_timestamp(sk, &now);
307
308         switch (hctx->ccid3hctx_state) {
309         case TFRC_SSTATE_NO_SENT:
310                 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
311                 hctx->ccid3hctx_no_feedback_timer.data     = (unsigned long)sk;
312                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
313                                jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
314                 hctx->ccid3hctx_last_win_count   = 0;
315                 hctx->ccid3hctx_t_last_win_count = now;
316                 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
317                 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
318
319                 /* Set nominal send time for initial packet */
320                 hctx->ccid3hctx_t_nom = now;
321                 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
322                                   hctx->ccid3hctx_t_ipi);
323                 ccid3_calc_new_delta(hctx);
324                 rc = 0;
325                 break;
326         case TFRC_SSTATE_NO_FBACK:
327         case TFRC_SSTATE_FBACK:
328                 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
329                          hctx->ccid3hctx_delta);
330                 delay /= -1000;
331                 /* divide by -1000 is to convert to ms and get sign right */
332                 rc = delay > 0 ? delay : 0;
333                 break;
334         default:
335                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
336                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
337                 dump_stack();
338                 rc = -EINVAL;
339                 break;
340         }
341
342         /* Can we send? if so add options and add to packet history */
343         if (rc == 0) {
344                 dp->dccps_hc_tx_insert_options = 1;
345                 new_packet->dccphtx_ccval =
346                         DCCP_SKB_CB(skb)->dccpd_ccval =
347                                 hctx->ccid3hctx_last_win_count;
348         }
349 out:
350         return rc;
351 }
352
353 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
354 {
355         const struct dccp_sock *dp = dccp_sk(sk);
356         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
357         struct timeval now;
358
359         BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
360
361         dccp_timestamp(sk, &now);
362
363         /* check if we have sent a data packet */
364         if (len > 0) {
365                 unsigned long quarter_rtt;
366                 struct dccp_tx_hist_entry *packet;
367
368                 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
369                 if (unlikely(packet == NULL)) {
370                         LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
371                                        "exists in history!\n", __FUNCTION__);
372                         return;
373                 }
374                 if (unlikely(packet->dccphtx_sent)) {
375                         LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
376                                        "history!\n", __FUNCTION__);
377                         return;
378                 }
379                 packet->dccphtx_tstamp = now;
380                 packet->dccphtx_seqno  = dp->dccps_gss;
381                 /*
382                  * Check if win_count have changed
383                  * Algorithm in "8.1. Window Counter Valuer" in
384                  * draft-ietf-dccp-ccid3-11.txt
385                  */
386                 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
387                 if (likely(hctx->ccid3hctx_rtt > 8))
388                         quarter_rtt /= hctx->ccid3hctx_rtt / 4;
389
390                 if (quarter_rtt > 0) {
391                         hctx->ccid3hctx_t_last_win_count = now;
392                         hctx->ccid3hctx_last_win_count   = (hctx->ccid3hctx_last_win_count +
393                                                             min_t(unsigned long, quarter_rtt, 5)) % 16;
394                         ccid3_pr_debug("%s, sk=%p, window changed from "
395                                        "%u to %u!\n",
396                                        dccp_role(sk), sk,
397                                        packet->dccphtx_ccval,
398                                        hctx->ccid3hctx_last_win_count);
399                 }
400
401                 hctx->ccid3hctx_idle = 0;
402                 packet->dccphtx_rtt  = hctx->ccid3hctx_rtt;
403                 packet->dccphtx_sent = 1;
404         } else
405                 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
406                                dccp_role(sk), sk, dp->dccps_gss);
407
408         switch (hctx->ccid3hctx_state) {
409         case TFRC_SSTATE_NO_SENT:
410                 /* if first wasn't pure ack */
411                 if (len != 0)
412                         printk(KERN_CRIT "%s: %s, First packet sent is noted "
413                                          "as a data packet\n",
414                                __FUNCTION__, dccp_role(sk));
415                 return;
416         case TFRC_SSTATE_NO_FBACK:
417         case TFRC_SSTATE_FBACK:
418                 if (len > 0) {
419                         hctx->ccid3hctx_t_nom = now;
420                         ccid3_calc_new_t_ipi(hctx);
421                         ccid3_calc_new_delta(hctx);
422                         timeval_add_usecs(&hctx->ccid3hctx_t_nom,
423                                           hctx->ccid3hctx_t_ipi);
424                 }
425                 break;
426         default:
427                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
428                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
429                 dump_stack();
430                 break;
431         }
432 }
433
434 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
435 {
436         const struct dccp_sock *dp = dccp_sk(sk);
437         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
438         struct ccid3_options_received *opt_recv;
439         struct dccp_tx_hist_entry *packet;
440         struct timeval now;
441         unsigned long next_tmout; 
442         u32 t_elapsed;
443         u32 pinv;
444         u32 x_recv;
445         u32 r_sample;
446
447         BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
448
449         /* we are only interested in ACKs */
450         if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
451               DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
452                 return;
453
454         opt_recv = &hctx->ccid3hctx_options_received;
455
456         t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
457         x_recv = opt_recv->ccid3or_receive_rate;
458         pinv = opt_recv->ccid3or_loss_event_rate;
459
460         switch (hctx->ccid3hctx_state) {
461         case TFRC_SSTATE_NO_SENT:
462                 /* FIXME: what to do here? */
463                 return;
464         case TFRC_SSTATE_NO_FBACK:
465         case TFRC_SSTATE_FBACK:
466                 /* Calculate new round trip sample by
467                  * R_sample = (now - t_recvdata) - t_delay */
468                 /* get t_recvdata from history */
469                 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
470                                                  DCCP_SKB_CB(skb)->dccpd_ack_seq);
471                 if (unlikely(packet == NULL)) {
472                         LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, seqno "
473                                        "%llu(%s) does't exist in history!\n",
474                                        __FUNCTION__, dccp_role(sk), sk,
475                             (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
476                                 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
477                         return;
478                 }
479
480                 /* Update RTT */
481                 dccp_timestamp(sk, &now);
482                 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
483                 if (unlikely(r_sample <= t_elapsed))
484                         LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
485                                        "t_elapsed=%uus\n",
486                                        __FUNCTION__, r_sample, t_elapsed);
487                 else
488                         r_sample -= t_elapsed;
489
490                 /* Update RTT estimate by 
491                  * If (No feedback recv)
492                  *    R = R_sample;
493                  * Else
494                  *    R = q * R + (1 - q) * R_sample;
495                  *
496                  * q is a constant, RFC 3448 recomments 0.9
497                  */
498                 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
499                         ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
500                         hctx->ccid3hctx_rtt = r_sample;
501                 } else
502                         hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
503                                               r_sample / 10;
504
505                 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
506                                "r_sample=%us\n", dccp_role(sk), sk,
507                                hctx->ccid3hctx_rtt, r_sample);
508
509                 /* Update timeout interval */
510                 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
511                                               USEC_PER_SEC);
512
513                 /* Update receive rate */
514                 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
515
516                 /* Update loss event rate */
517                 if (pinv == ~0 || pinv == 0)
518                         hctx->ccid3hctx_p = 0;
519                 else {
520                         hctx->ccid3hctx_p = 1000000 / pinv;
521
522                         if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
523                                 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
524                                 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
525                                                dccp_role(sk), sk);
526                         }
527                 }
528
529                 /* unschedule no feedback timer */
530                 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
531
532                 /* Update sending rate */
533                 ccid3_hc_tx_update_x(sk);
534
535                 /* Update next send time */
536                 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
537                                   hctx->ccid3hctx_t_ipi);
538                 ccid3_calc_new_t_ipi(hctx);
539                 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
540                                   hctx->ccid3hctx_t_ipi);
541                 ccid3_calc_new_delta(hctx);
542
543                 /* remove all packets older than the one acked from history */
544                 dccp_tx_hist_purge_older(ccid3_tx_hist,
545                                          &hctx->ccid3hctx_hist, packet);
546                 /*
547                  * As we have calculated new ipi, delta, t_nom it is possible that
548                  * we now can send a packet, so wake up dccp_wait_for_ccids.
549                  */
550                 sk->sk_write_space(sk);
551
552                 /*
553                  * Schedule no feedback timer to expire in
554                  * max(4 * R, 2 * s / X)
555                  */
556                 next_tmout = max(hctx->ccid3hctx_t_rto,
557                                  2 * usecs_div(hctx->ccid3hctx_s,
558                                                hctx->ccid3hctx_x));
559                         
560                 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
561                                "expire in %lu jiffies (%luus)\n",
562                                dccp_role(sk), sk,
563                                usecs_to_jiffies(next_tmout), next_tmout); 
564
565                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer, 
566                                jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
567
568                 /* set idle flag */
569                 hctx->ccid3hctx_idle = 1;   
570                 break;
571         default:
572                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
573                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
574                 dump_stack();
575                 break;
576         }
577 }
578
579 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
580 {
581         const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
582
583         BUG_ON(hctx == NULL);
584
585         if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
586                 return;
587
588          DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
589 }
590
591 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
592                                      unsigned char len, u16 idx,
593                                      unsigned char *value)
594 {
595         int rc = 0;
596         const struct dccp_sock *dp = dccp_sk(sk);
597         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
598         struct ccid3_options_received *opt_recv;
599
600         BUG_ON(hctx == NULL);
601
602         opt_recv = &hctx->ccid3hctx_options_received;
603
604         if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
605                 opt_recv->ccid3or_seqno              = dp->dccps_gsr;
606                 opt_recv->ccid3or_loss_event_rate    = ~0;
607                 opt_recv->ccid3or_loss_intervals_idx = 0;
608                 opt_recv->ccid3or_loss_intervals_len = 0;
609                 opt_recv->ccid3or_receive_rate       = 0;
610         }
611
612         switch (option) {
613         case TFRC_OPT_LOSS_EVENT_RATE:
614                 if (unlikely(len != 4)) {
615                         LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
616                                        "len for TFRC_OPT_LOSS_EVENT_RATE\n",
617                                        __FUNCTION__, dccp_role(sk), sk);
618                         rc = -EINVAL;
619                 } else {
620                         opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
621                         ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
622                                        dccp_role(sk), sk,
623                                        opt_recv->ccid3or_loss_event_rate);
624                 }
625                 break;
626         case TFRC_OPT_LOSS_INTERVALS:
627                 opt_recv->ccid3or_loss_intervals_idx = idx;
628                 opt_recv->ccid3or_loss_intervals_len = len;
629                 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
630                                dccp_role(sk), sk,
631                                opt_recv->ccid3or_loss_intervals_idx,
632                                opt_recv->ccid3or_loss_intervals_len);
633                 break;
634         case TFRC_OPT_RECEIVE_RATE:
635                 if (unlikely(len != 4)) {
636                         LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
637                                        "len for TFRC_OPT_RECEIVE_RATE\n",
638                                        __FUNCTION__, dccp_role(sk), sk);
639                         rc = -EINVAL;
640                 } else {
641                         opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
642                         ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
643                                        dccp_role(sk), sk,
644                                        opt_recv->ccid3or_receive_rate);
645                 }
646                 break;
647         }
648
649         return rc;
650 }
651
652 static int ccid3_hc_tx_init(struct sock *sk)
653 {
654         struct dccp_sock *dp = dccp_sk(sk);
655         struct ccid3_hc_tx_sock *hctx;
656
657         dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
658         if (dp->dccps_hc_tx_ccid_private == NULL)
659                 return -ENOMEM;
660
661         hctx = ccid3_hc_tx_sk(sk);
662         memset(hctx, 0, sizeof(*hctx));
663
664         if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
665             dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
666                 hctx->ccid3hctx_s = dp->dccps_packet_size;
667         else
668                 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
669
670         /* Set transmission rate to 1 packet per second */
671         hctx->ccid3hctx_x     = hctx->ccid3hctx_s;
672         hctx->ccid3hctx_t_rto = USEC_PER_SEC;
673         hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
674         INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
675         init_timer(&hctx->ccid3hctx_no_feedback_timer);
676
677         return 0;
678 }
679
680 static void ccid3_hc_tx_exit(struct sock *sk)
681 {
682         struct dccp_sock *dp = dccp_sk(sk);
683         struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
684
685         BUG_ON(hctx == NULL);
686
687         ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
688         sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
689
690         /* Empty packet history */
691         dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
692
693         kfree(dp->dccps_hc_tx_ccid_private);
694         dp->dccps_hc_tx_ccid_private = NULL;
695 }
696
697 /*
698  * RX Half Connection methods
699  */
700
701 /* TFRC receiver states */
702 enum ccid3_hc_rx_states {
703         TFRC_RSTATE_NO_DATA = 1,
704         TFRC_RSTATE_DATA,
705         TFRC_RSTATE_TERM    = 127,
706 };
707
708 #ifdef CCID3_DEBUG
709 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
710 {
711         static char *ccid3_rx_state_names[] = {
712         [TFRC_RSTATE_NO_DATA] = "NO_DATA",
713         [TFRC_RSTATE_DATA]    = "DATA",
714         [TFRC_RSTATE_TERM]    = "TERM",
715         };
716
717         return ccid3_rx_state_names[state];
718 }
719 #endif
720
721 static inline void ccid3_hc_rx_set_state(struct sock *sk,
722                                          enum ccid3_hc_rx_states state)
723 {
724         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
725         enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
726
727         ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
728                        dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
729                        ccid3_rx_state_name(state));
730         WARN_ON(state == oldstate);
731         hcrx->ccid3hcrx_state = state;
732 }
733
734 static void ccid3_hc_rx_send_feedback(struct sock *sk)
735 {
736         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
737         struct dccp_sock *dp = dccp_sk(sk);
738         struct dccp_rx_hist_entry *packet;
739         struct timeval now;
740
741         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
742
743         dccp_timestamp(sk, &now);
744
745         switch (hcrx->ccid3hcrx_state) {
746         case TFRC_RSTATE_NO_DATA:
747                 hcrx->ccid3hcrx_x_recv = 0;
748                 break;
749         case TFRC_RSTATE_DATA: {
750                 const u32 delta = timeval_delta(&now,
751                                         &hcrx->ccid3hcrx_tstamp_last_feedback);
752                 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
753                                                    delta);
754         }
755                 break;
756         default:
757                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
758                        __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
759                 dump_stack();
760                 return;
761         }
762
763         packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
764         if (unlikely(packet == NULL)) {
765                 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
766                                "in history!\n",
767                                __FUNCTION__, dccp_role(sk), sk);
768                 return;
769         }
770
771         hcrx->ccid3hcrx_tstamp_last_feedback = now;
772         hcrx->ccid3hcrx_last_counter         = packet->dccphrx_ccval;
773         hcrx->ccid3hcrx_seqno_last_counter   = packet->dccphrx_seqno;
774         hcrx->ccid3hcrx_bytes_recv           = 0;
775
776         /* Convert to multiples of 10us */
777         hcrx->ccid3hcrx_elapsed_time =
778                         timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
779         if (hcrx->ccid3hcrx_p == 0)
780                 hcrx->ccid3hcrx_pinv = ~0;
781         else
782                 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
783         dp->dccps_hc_rx_insert_options = 1;
784         dccp_send_ack(sk);
785 }
786
787 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
788 {
789         const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
790         u32 x_recv, pinv;
791
792         BUG_ON(hcrx == NULL);
793
794         if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
795                 return;
796
797         DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
798
799         if (dccp_packet_without_ack(skb))
800                 return;
801                 
802         if (hcrx->ccid3hcrx_elapsed_time != 0)
803                 dccp_insert_option_elapsed_time(sk, skb,
804                                                 hcrx->ccid3hcrx_elapsed_time);
805         dccp_insert_option_timestamp(sk, skb);
806         x_recv = htonl(hcrx->ccid3hcrx_x_recv);
807         pinv   = htonl(hcrx->ccid3hcrx_pinv);
808         dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
809                            &pinv, sizeof(pinv));
810         dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
811                            &x_recv, sizeof(x_recv));
812 }
813
814 /* calculate first loss interval
815  *
816  * returns estimated loss interval in usecs */
817
818 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
819 {
820         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
821         struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
822         u32 rtt, delta, x_recv, fval, p, tmp2;
823         struct timeval tstamp = { 0, };
824         int interval = 0;
825         int win_count = 0;
826         int step = 0;
827         u64 tmp1;
828
829         list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
830                                  dccphrx_node) {
831                 if (dccp_rx_hist_entry_data_packet(entry)) {
832                         tail = entry;
833
834                         switch (step) {
835                         case 0:
836                                 tstamp    = entry->dccphrx_tstamp;
837                                 win_count = entry->dccphrx_ccval;
838                                 step = 1;
839                                 break;
840                         case 1:
841                                 interval = win_count - entry->dccphrx_ccval;
842                                 if (interval < 0)
843                                         interval += TFRC_WIN_COUNT_LIMIT;
844                                 if (interval > 4)
845                                         goto found;
846                                 break;
847                         }
848                 }
849         }
850
851         if (unlikely(step == 0)) {
852                 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
853                                "contains no data packets!\n",
854                                __FUNCTION__, dccp_role(sk), sk);
855                 return ~0;
856         }
857
858         if (unlikely(interval == 0)) {
859                 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
860                                "win_count interval > 0. Defaulting to 1\n",
861                                __FUNCTION__, dccp_role(sk), sk);
862                 interval = 1;
863         }
864 found:
865         rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
866         ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
867                        dccp_role(sk), sk, rtt);
868         if (rtt == 0)
869                 rtt = 1;
870
871         dccp_timestamp(sk, &tstamp);
872         delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
873         x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
874
875         tmp1 = (u64)x_recv * (u64)rtt;
876         do_div(tmp1,10000000);
877         tmp2 = (u32)tmp1;
878         fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
879         /* do not alter order above or you will get overflow on 32 bit */
880         p = tfrc_calc_x_reverse_lookup(fval);
881         ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
882                        "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
883
884         if (p == 0)
885                 return ~0;
886         else
887                 return 1000000 / p; 
888 }
889
890 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
891 {
892         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
893
894         if (seq_loss != DCCP_MAX_SEQNO + 1 &&
895             list_empty(&hcrx->ccid3hcrx_li_hist)) {
896                 struct dccp_li_hist_entry *li_tail;
897
898                 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
899                                                     &hcrx->ccid3hcrx_li_hist,
900                                                     seq_loss, win_loss);
901                 if (li_tail == NULL)
902                         return;
903                 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
904         } else
905                     LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
906                                    "interval\n", __FUNCTION__);
907 }
908
909 static void ccid3_hc_rx_detect_loss(struct sock *sk)
910 {
911         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
912         u8 win_loss;
913         const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
914                                                       &hcrx->ccid3hcrx_li_hist,
915                                                       &win_loss);
916
917         ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
918 }
919
920 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
921 {
922         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
923         const struct dccp_options_received *opt_recv;
924         struct dccp_rx_hist_entry *packet;
925         struct timeval now;
926         u8 win_count;
927         u32 p_prev, r_sample, t_elapsed;
928         int ins;
929
930         BUG_ON(hcrx == NULL ||
931                !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
932                  hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
933
934         opt_recv = &dccp_sk(sk)->dccps_options_received;
935
936         switch (DCCP_SKB_CB(skb)->dccpd_type) {
937         case DCCP_PKT_ACK:
938                 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
939                         return;
940         case DCCP_PKT_DATAACK:
941                 if (opt_recv->dccpor_timestamp_echo == 0)
942                         break;
943                 p_prev = hcrx->ccid3hcrx_rtt;
944                 dccp_timestamp(sk, &now);
945                 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
946                 r_sample = timeval_usecs(&now);
947                 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
948
949                 if (unlikely(r_sample <= t_elapsed))
950                         LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
951                                        "t_elapsed=%uus\n",
952                                        __FUNCTION__, r_sample, t_elapsed);
953                 else
954                         r_sample -= t_elapsed;
955
956                 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
957                         hcrx->ccid3hcrx_rtt = r_sample;
958                 else
959                         hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
960                                               r_sample / 10;
961
962                 if (p_prev != hcrx->ccid3hcrx_rtt)
963                         ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
964                                        dccp_role(sk), hcrx->ccid3hcrx_rtt,
965                                        opt_recv->dccpor_elapsed_time);
966                 break;
967         case DCCP_PKT_DATA:
968                 break;
969         default: /* We're not interested in other packet types, move along */
970                 return;
971         }
972
973         packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
974                                         skb, SLAB_ATOMIC);
975         if (unlikely(packet == NULL)) {
976                 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
977                                 "add rx packet to history, consider it lost!\n",
978                                __FUNCTION__, dccp_role(sk), sk);
979                 return;
980         }
981
982         win_count = packet->dccphrx_ccval;
983
984         ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
985                                       &hcrx->ccid3hcrx_li_hist, packet);
986
987         if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
988                 return;
989
990         switch (hcrx->ccid3hcrx_state) {
991         case TFRC_RSTATE_NO_DATA:
992                 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
993                                "feedback\n",
994                                dccp_role(sk), sk,
995                                dccp_state_name(sk->sk_state), skb);
996                 ccid3_hc_rx_send_feedback(sk);
997                 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
998                 return;
999         case TFRC_RSTATE_DATA:
1000                 hcrx->ccid3hcrx_bytes_recv += skb->len -
1001                                               dccp_hdr(skb)->dccph_doff * 4;
1002                 if (ins != 0)
1003                         break;
1004
1005                 dccp_timestamp(sk, &now);
1006                 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1007                     hcrx->ccid3hcrx_rtt) {
1008                         hcrx->ccid3hcrx_tstamp_last_ack = now;
1009                         ccid3_hc_rx_send_feedback(sk);
1010                 }
1011                 return;
1012         default:
1013                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1014                        __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1015                 dump_stack();
1016                 return;
1017         }
1018
1019         /* Dealing with packet loss */
1020         ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1021                        dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1022
1023         ccid3_hc_rx_detect_loss(sk);
1024         p_prev = hcrx->ccid3hcrx_p;
1025         
1026         /* Calculate loss event rate */
1027         if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
1028                 u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1029
1030                 /* Scaling up by 1000000 as fixed decimal */
1031                 if (i_mean != 0)
1032                         hcrx->ccid3hcrx_p = 1000000 / i_mean;
1033         }
1034
1035         if (hcrx->ccid3hcrx_p > p_prev) {
1036                 ccid3_hc_rx_send_feedback(sk);
1037                 return;
1038         }
1039 }
1040
1041 static int ccid3_hc_rx_init(struct sock *sk)
1042 {
1043         struct dccp_sock *dp = dccp_sk(sk);
1044         struct ccid3_hc_rx_sock *hcrx;
1045
1046         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1047
1048         dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1049         if (dp->dccps_hc_rx_ccid_private == NULL)
1050                 return -ENOMEM;
1051
1052         hcrx = ccid3_hc_rx_sk(sk);
1053         memset(hcrx, 0, sizeof(*hcrx));
1054
1055         if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1056             dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1057                 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1058         else
1059                 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1060
1061         hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1062         INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1063         INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1064         dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1065         hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1066         hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1067         return 0;
1068 }
1069
1070 static void ccid3_hc_rx_exit(struct sock *sk)
1071 {
1072         struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1073         struct dccp_sock *dp = dccp_sk(sk);
1074
1075         BUG_ON(hcrx == NULL);
1076
1077         ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1078
1079         /* Empty packet history */
1080         dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1081
1082         /* Empty loss interval history */
1083         dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1084
1085         kfree(dp->dccps_hc_rx_ccid_private);
1086         dp->dccps_hc_rx_ccid_private = NULL;
1087 }
1088
1089 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1090 {
1091         const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1092
1093         /* Listen socks doesn't have a private CCID block */
1094         if (sk->sk_state == DCCP_LISTEN)
1095                 return;
1096
1097         BUG_ON(hcrx == NULL);
1098
1099         info->tcpi_ca_state     = hcrx->ccid3hcrx_state;
1100         info->tcpi_options      |= TCPI_OPT_TIMESTAMPS;
1101         info->tcpi_rcv_rtt      = hcrx->ccid3hcrx_rtt;
1102 }
1103
1104 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1105 {
1106         const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1107
1108         /* Listen socks doesn't have a private CCID block */
1109         if (sk->sk_state == DCCP_LISTEN)
1110                 return;
1111
1112         BUG_ON(hctx == NULL);
1113
1114         info->tcpi_rto = hctx->ccid3hctx_t_rto;
1115         info->tcpi_rtt = hctx->ccid3hctx_rtt;
1116 }
1117
1118 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
1119                                   u32 __user *optval, int __user *optlen)
1120 {
1121         const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1122         const void *val;
1123         
1124         /* Listen socks doesn't have a private CCID block */
1125         if (sk->sk_state == DCCP_LISTEN)
1126                 return -EINVAL;
1127
1128         switch (optname) {
1129         case DCCP_SOCKOPT_CCID_RX_INFO:
1130                 if (len < sizeof(hcrx->ccid3hcrx_tfrc))
1131                         return -EINVAL;
1132                 len = sizeof(hcrx->ccid3hcrx_tfrc);
1133                 val = &hcrx->ccid3hcrx_tfrc;
1134                 break;
1135         default:
1136                 return -ENOPROTOOPT;
1137         }
1138
1139         if (put_user(len, optlen) || copy_to_user(optval, val, len))
1140                 return -EFAULT;
1141
1142         return 0;
1143 }
1144
1145 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
1146                                   u32 __user *optval, int __user *optlen)
1147 {
1148         const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1149         const void *val;
1150         
1151         /* Listen socks doesn't have a private CCID block */
1152         if (sk->sk_state == DCCP_LISTEN)
1153                 return -EINVAL;
1154
1155         switch (optname) {
1156         case DCCP_SOCKOPT_CCID_TX_INFO:
1157                 if (len < sizeof(hctx->ccid3hctx_tfrc))
1158                         return -EINVAL;
1159                 len = sizeof(hctx->ccid3hctx_tfrc);
1160                 val = &hctx->ccid3hctx_tfrc;
1161                 break;
1162         default:
1163                 return -ENOPROTOOPT;
1164         }
1165
1166         if (put_user(len, optlen) || copy_to_user(optval, val, len))
1167                 return -EFAULT;
1168
1169         return 0;
1170 }
1171
1172 static struct ccid ccid3 = {
1173         .ccid_id                   = 3,
1174         .ccid_name                 = "ccid3",
1175         .ccid_owner                = THIS_MODULE,
1176         .ccid_hc_tx_init           = ccid3_hc_tx_init,
1177         .ccid_hc_tx_exit           = ccid3_hc_tx_exit,
1178         .ccid_hc_tx_send_packet    = ccid3_hc_tx_send_packet,
1179         .ccid_hc_tx_packet_sent    = ccid3_hc_tx_packet_sent,
1180         .ccid_hc_tx_packet_recv    = ccid3_hc_tx_packet_recv,
1181         .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1182         .ccid_hc_tx_parse_options  = ccid3_hc_tx_parse_options,
1183         .ccid_hc_rx_init           = ccid3_hc_rx_init,
1184         .ccid_hc_rx_exit           = ccid3_hc_rx_exit,
1185         .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1186         .ccid_hc_rx_packet_recv    = ccid3_hc_rx_packet_recv,
1187         .ccid_hc_rx_get_info       = ccid3_hc_rx_get_info,
1188         .ccid_hc_tx_get_info       = ccid3_hc_tx_get_info,
1189         .ccid_hc_rx_getsockopt     = ccid3_hc_rx_getsockopt,
1190         .ccid_hc_tx_getsockopt     = ccid3_hc_tx_getsockopt,
1191 };
1192  
1193 module_param(ccid3_debug, int, 0444);
1194 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1195
1196 static __init int ccid3_module_init(void)
1197 {
1198         int rc = -ENOBUFS;
1199
1200         ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1201         if (ccid3_rx_hist == NULL)
1202                 goto out;
1203
1204         ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1205         if (ccid3_tx_hist == NULL)
1206                 goto out_free_rx;
1207
1208         ccid3_li_hist = dccp_li_hist_new("ccid3");
1209         if (ccid3_li_hist == NULL)
1210                 goto out_free_tx;
1211
1212         rc = ccid_register(&ccid3);
1213         if (rc != 0) 
1214                 goto out_free_loss_interval_history;
1215 out:
1216         return rc;
1217
1218 out_free_loss_interval_history:
1219         dccp_li_hist_delete(ccid3_li_hist);
1220         ccid3_li_hist = NULL;
1221 out_free_tx:
1222         dccp_tx_hist_delete(ccid3_tx_hist);
1223         ccid3_tx_hist = NULL;
1224 out_free_rx:
1225         dccp_rx_hist_delete(ccid3_rx_hist);
1226         ccid3_rx_hist = NULL;
1227         goto out;
1228 }
1229 module_init(ccid3_module_init);
1230
1231 static __exit void ccid3_module_exit(void)
1232 {
1233 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1234         /*
1235          * Hack to use while developing, so that we get rid of the control
1236          * sock, that is what keeps a refcount on dccp.ko -acme
1237          */
1238         extern void dccp_ctl_sock_exit(void);
1239
1240         dccp_ctl_sock_exit();
1241 #endif
1242         ccid_unregister(&ccid3);
1243
1244         if (ccid3_tx_hist != NULL) {
1245                 dccp_tx_hist_delete(ccid3_tx_hist);
1246                 ccid3_tx_hist = NULL;
1247         }
1248         if (ccid3_rx_hist != NULL) {
1249                 dccp_rx_hist_delete(ccid3_rx_hist);
1250                 ccid3_rx_hist = NULL;
1251         }
1252         if (ccid3_li_hist != NULL) {
1253                 dccp_li_hist_delete(ccid3_li_hist);
1254                 ccid3_li_hist = NULL;
1255         }
1256 }
1257 module_exit(ccid3_module_exit);
1258
1259 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1260               "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1261 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1262 MODULE_LICENSE("GPL");
1263 MODULE_ALIAS("net-dccp-ccid-3");