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