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