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