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