Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / net / dccp / ccids / ccid2.c
1 /*
2  *  Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
3  *
4  *  Changes to meet Linux coding standards, and DCCP infrastructure fixes.
5  *
6  *  Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /*
24  * This implementation should follow RFC 4341
25  */
26 #include <linux/slab.h>
27 #include "../feat.h"
28 #include "ccid2.h"
29
30
31 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
32 static int ccid2_debug;
33 #define ccid2_pr_debug(format, a...)    DCCP_PR_DEBUG(ccid2_debug, format, ##a)
34 #else
35 #define ccid2_pr_debug(format, a...)
36 #endif
37
38 static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
39 {
40         struct ccid2_seq *seqp;
41         int i;
42
43         /* check if we have space to preserve the pointer to the buffer */
44         if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
45                                sizeof(struct ccid2_seq *)))
46                 return -ENOMEM;
47
48         /* allocate buffer and initialize linked list */
49         seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
50         if (seqp == NULL)
51                 return -ENOMEM;
52
53         for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
54                 seqp[i].ccid2s_next = &seqp[i + 1];
55                 seqp[i + 1].ccid2s_prev = &seqp[i];
56         }
57         seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
58         seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
59
60         /* This is the first allocation.  Initiate the head and tail.  */
61         if (hc->tx_seqbufc == 0)
62                 hc->tx_seqh = hc->tx_seqt = seqp;
63         else {
64                 /* link the existing list with the one we just created */
65                 hc->tx_seqh->ccid2s_next = seqp;
66                 seqp->ccid2s_prev = hc->tx_seqh;
67
68                 hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
69                 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
70         }
71
72         /* store the original pointer to the buffer so we can free it */
73         hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
74         hc->tx_seqbufc++;
75
76         return 0;
77 }
78
79 static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
80 {
81         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
82
83         if (hc->tx_pipe < hc->tx_cwnd)
84                 return 0;
85
86         return 1; /* XXX CCID should dequeue when ready instead of polling */
87 }
88
89 static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
90 {
91         struct dccp_sock *dp = dccp_sk(sk);
92         u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
93
94         /*
95          * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
96          * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
97          * acceptable since this causes starvation/deadlock whenever cwnd < 2.
98          * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
99          */
100         if (val == 0 || val > max_ratio) {
101                 DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
102                 val = max_ratio;
103         }
104         if (val > DCCPF_ACK_RATIO_MAX)
105                 val = DCCPF_ACK_RATIO_MAX;
106
107         if (val == dp->dccps_l_ack_ratio)
108                 return;
109
110         ccid2_pr_debug("changing local ack ratio to %u\n", val);
111         dp->dccps_l_ack_ratio = val;
112 }
113
114 static void ccid2_hc_tx_rto_expire(unsigned long data)
115 {
116         struct sock *sk = (struct sock *)data;
117         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
118
119         bh_lock_sock(sk);
120         if (sock_owned_by_user(sk)) {
121                 sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
122                 goto out;
123         }
124
125         ccid2_pr_debug("RTO_EXPIRE\n");
126
127         /* back-off timer */
128         hc->tx_rto <<= 1;
129         if (hc->tx_rto > DCCP_RTO_MAX)
130                 hc->tx_rto = DCCP_RTO_MAX;
131
132         sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
133
134         /* adjust pipe, cwnd etc */
135         hc->tx_ssthresh = hc->tx_cwnd / 2;
136         if (hc->tx_ssthresh < 2)
137                 hc->tx_ssthresh = 2;
138         hc->tx_cwnd     = 1;
139         hc->tx_pipe     = 0;
140
141         /* clear state about stuff we sent */
142         hc->tx_seqt = hc->tx_seqh;
143         hc->tx_packets_acked = 0;
144
145         /* clear ack ratio state. */
146         hc->tx_rpseq    = 0;
147         hc->tx_rpdupack = -1;
148         ccid2_change_l_ack_ratio(sk, 1);
149 out:
150         bh_unlock_sock(sk);
151         sock_put(sk);
152 }
153
154 static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
155 {
156         struct dccp_sock *dp = dccp_sk(sk);
157         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
158         struct ccid2_seq *next;
159
160         hc->tx_pipe++;
161
162         hc->tx_seqh->ccid2s_seq   = dp->dccps_gss;
163         hc->tx_seqh->ccid2s_acked = 0;
164         hc->tx_seqh->ccid2s_sent  = ccid2_time_stamp;
165
166         next = hc->tx_seqh->ccid2s_next;
167         /* check if we need to alloc more space */
168         if (next == hc->tx_seqt) {
169                 if (ccid2_hc_tx_alloc_seq(hc)) {
170                         DCCP_CRIT("packet history - out of memory!");
171                         /* FIXME: find a more graceful way to bail out */
172                         return;
173                 }
174                 next = hc->tx_seqh->ccid2s_next;
175                 BUG_ON(next == hc->tx_seqt);
176         }
177         hc->tx_seqh = next;
178
179         ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
180
181         /*
182          * FIXME: The code below is broken and the variables have been removed
183          * from the socket struct. The `ackloss' variable was always set to 0,
184          * and with arsent there are several problems:
185          *  (i) it doesn't just count the number of Acks, but all sent packets;
186          *  (ii) it is expressed in # of packets, not # of windows, so the
187          *  comparison below uses the wrong formula: Appendix A of RFC 4341
188          *  comes up with the number K = cwnd / (R^2 - R) of consecutive windows
189          *  of data with no lost or marked Ack packets. If arsent were the # of
190          *  consecutive Acks received without loss, then Ack Ratio needs to be
191          *  decreased by 1 when
192          *            arsent >=  K * cwnd / R  =  cwnd^2 / (R^3 - R^2)
193          *  where cwnd / R is the number of Acks received per window of data
194          *  (cf. RFC 4341, App. A). The problems are that
195          *  - arsent counts other packets as well;
196          *  - the comparison uses a formula different from RFC 4341;
197          *  - computing a cubic/quadratic equation each time is too complicated.
198          *  Hence a different algorithm is needed.
199          */
200 #if 0
201         /* Ack Ratio.  Need to maintain a concept of how many windows we sent */
202         hc->tx_arsent++;
203         /* We had an ack loss in this window... */
204         if (hc->tx_ackloss) {
205                 if (hc->tx_arsent >= hc->tx_cwnd) {
206                         hc->tx_arsent  = 0;
207                         hc->tx_ackloss = 0;
208                 }
209         } else {
210                 /* No acks lost up to now... */
211                 /* decrease ack ratio if enough packets were sent */
212                 if (dp->dccps_l_ack_ratio > 1) {
213                         /* XXX don't calculate denominator each time */
214                         int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
215                                     dp->dccps_l_ack_ratio;
216
217                         denom = hc->tx_cwnd * hc->tx_cwnd / denom;
218
219                         if (hc->tx_arsent >= denom) {
220                                 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
221                                 hc->tx_arsent = 0;
222                         }
223                 } else {
224                         /* we can't increase ack ratio further [1] */
225                         hc->tx_arsent = 0; /* or maybe set it to cwnd*/
226                 }
227         }
228 #endif
229
230         sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
231
232 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
233         do {
234                 struct ccid2_seq *seqp = hc->tx_seqt;
235
236                 while (seqp != hc->tx_seqh) {
237                         ccid2_pr_debug("out seq=%llu acked=%d time=%u\n",
238                                        (unsigned long long)seqp->ccid2s_seq,
239                                        seqp->ccid2s_acked, seqp->ccid2s_sent);
240                         seqp = seqp->ccid2s_next;
241                 }
242         } while (0);
243         ccid2_pr_debug("=========\n");
244 #endif
245 }
246
247 /* XXX Lame code duplication!
248  * returns -1 if none was found.
249  * else returns the next offset to use in the function call.
250  */
251 static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
252                            unsigned char **vec, unsigned char *veclen)
253 {
254         const struct dccp_hdr *dh = dccp_hdr(skb);
255         unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
256         unsigned char *opt_ptr;
257         const unsigned char *opt_end = (unsigned char *)dh +
258                                         (dh->dccph_doff * 4);
259         unsigned char opt, len;
260         unsigned char *value;
261
262         BUG_ON(offset < 0);
263         options += offset;
264         opt_ptr = options;
265         if (opt_ptr >= opt_end)
266                 return -1;
267
268         while (opt_ptr != opt_end) {
269                 opt   = *opt_ptr++;
270                 len   = 0;
271                 value = NULL;
272
273                 /* Check if this isn't a single byte option */
274                 if (opt > DCCPO_MAX_RESERVED) {
275                         if (opt_ptr == opt_end)
276                                 goto out_invalid_option;
277
278                         len = *opt_ptr++;
279                         if (len < 3)
280                                 goto out_invalid_option;
281                         /*
282                          * Remove the type and len fields, leaving
283                          * just the value size
284                          */
285                         len     -= 2;
286                         value   = opt_ptr;
287                         opt_ptr += len;
288
289                         if (opt_ptr > opt_end)
290                                 goto out_invalid_option;
291                 }
292
293                 switch (opt) {
294                 case DCCPO_ACK_VECTOR_0:
295                 case DCCPO_ACK_VECTOR_1:
296                         *vec    = value;
297                         *veclen = len;
298                         return offset + (opt_ptr - options);
299                 }
300         }
301
302         return -1;
303
304 out_invalid_option:
305         DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
306         return -1;
307 }
308
309 /**
310  * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm
311  * This code is almost identical with TCP's tcp_rtt_estimator(), since
312  * - it has a higher sampling frequency (recommended by RFC 1323),
313  * - the RTO does not collapse into RTT due to RTTVAR going towards zero,
314  * - it is simple (cf. more complex proposals such as Eifel timer or research
315  *   which suggests that the gain should be set according to window size),
316  * - in tests it was found to work well with CCID2 [gerrit].
317  */
318 static void ccid2_rtt_estimator(struct sock *sk, const long mrtt)
319 {
320         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
321         long m = mrtt ? : 1;
322
323         if (hc->tx_srtt == 0) {
324                 /* First measurement m */
325                 hc->tx_srtt = m << 3;
326                 hc->tx_mdev = m << 1;
327
328                 hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk));
329                 hc->tx_rttvar   = hc->tx_mdev_max;
330
331                 hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
332         } else {
333                 /* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */
334                 m -= (hc->tx_srtt >> 3);
335                 hc->tx_srtt += m;
336
337                 /* Similarly, update scaled mdev with regard to |m| */
338                 if (m < 0) {
339                         m = -m;
340                         m -= (hc->tx_mdev >> 2);
341                         /*
342                          * This neutralises RTO increase when RTT < SRTT - mdev
343                          * (see P. Sarolahti, A. Kuznetsov,"Congestion Control
344                          * in Linux TCP", USENIX 2002, pp. 49-62).
345                          */
346                         if (m > 0)
347                                 m >>= 3;
348                 } else {
349                         m -= (hc->tx_mdev >> 2);
350                 }
351                 hc->tx_mdev += m;
352
353                 if (hc->tx_mdev > hc->tx_mdev_max) {
354                         hc->tx_mdev_max = hc->tx_mdev;
355                         if (hc->tx_mdev_max > hc->tx_rttvar)
356                                 hc->tx_rttvar = hc->tx_mdev_max;
357                 }
358
359                 /*
360                  * Decay RTTVAR at most once per flight, exploiting that
361                  *  1) pipe <= cwnd <= Sequence_Window = W  (RFC 4340, 7.5.2)
362                  *  2) AWL = GSS-W+1 <= GAR <= GSS          (RFC 4340, 7.5.1)
363                  * GAR is a useful bound for FlightSize = pipe.
364                  * AWL is probably too low here, as it over-estimates pipe.
365                  */
366                 if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) {
367                         if (hc->tx_mdev_max < hc->tx_rttvar)
368                                 hc->tx_rttvar -= (hc->tx_rttvar -
369                                                   hc->tx_mdev_max) >> 2;
370                         hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
371                         hc->tx_mdev_max = tcp_rto_min(sk);
372                 }
373         }
374
375         /*
376          * Set RTO from SRTT and RTTVAR
377          * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms.
378          * This agrees with RFC 4341, 5:
379          *      "Because DCCP does not retransmit data, DCCP does not require
380          *       TCP's recommended minimum timeout of one second".
381          */
382         hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar;
383
384         if (hc->tx_rto > DCCP_RTO_MAX)
385                 hc->tx_rto = DCCP_RTO_MAX;
386 }
387
388 static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp,
389                           unsigned int *maxincr)
390 {
391         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
392
393         if (hc->tx_cwnd < hc->tx_ssthresh) {
394                 if (*maxincr > 0 && ++hc->tx_packets_acked == 2) {
395                         hc->tx_cwnd += 1;
396                         *maxincr    -= 1;
397                         hc->tx_packets_acked = 0;
398                 }
399         } else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
400                         hc->tx_cwnd += 1;
401                         hc->tx_packets_acked = 0;
402         }
403         /*
404          * FIXME: RTT is sampled several times per acknowledgment (for each
405          * entry in the Ack Vector), instead of once per Ack (as in TCP SACK).
406          * This causes the RTT to be over-estimated, since the older entries
407          * in the Ack Vector have earlier sending times.
408          * The cleanest solution is to not use the ccid2s_sent field at all
409          * and instead use DCCP timestamps: requires changes in other places.
410          */
411         ccid2_rtt_estimator(sk, ccid2_time_stamp - seqp->ccid2s_sent);
412 }
413
414 static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
415 {
416         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
417
418         if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) {
419                 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
420                 return;
421         }
422
423         hc->tx_last_cong = ccid2_time_stamp;
424
425         hc->tx_cwnd      = hc->tx_cwnd / 2 ? : 1U;
426         hc->tx_ssthresh  = max(hc->tx_cwnd, 2U);
427
428         /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
429         if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd)
430                 ccid2_change_l_ack_ratio(sk, hc->tx_cwnd);
431 }
432
433 static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
434 {
435         struct dccp_sock *dp = dccp_sk(sk);
436         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
437         u64 ackno, seqno;
438         struct ccid2_seq *seqp;
439         unsigned char *vector;
440         unsigned char veclen;
441         int offset = 0;
442         int done = 0;
443         unsigned int maxincr = 0;
444
445         /* check reverse path congestion */
446         seqno = DCCP_SKB_CB(skb)->dccpd_seq;
447
448         /* XXX this whole "algorithm" is broken.  Need to fix it to keep track
449          * of the seqnos of the dupacks so that rpseq and rpdupack are correct
450          * -sorbo.
451          */
452         /* need to bootstrap */
453         if (hc->tx_rpdupack == -1) {
454                 hc->tx_rpdupack = 0;
455                 hc->tx_rpseq    = seqno;
456         } else {
457                 /* check if packet is consecutive */
458                 if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
459                         hc->tx_rpseq = seqno;
460                 /* it's a later packet */
461                 else if (after48(seqno, hc->tx_rpseq)) {
462                         hc->tx_rpdupack++;
463
464                         /* check if we got enough dupacks */
465                         if (hc->tx_rpdupack >= NUMDUPACK) {
466                                 hc->tx_rpdupack = -1; /* XXX lame */
467                                 hc->tx_rpseq    = 0;
468
469                                 ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
470                         }
471                 }
472         }
473
474         /* check forward path congestion */
475         /* still didn't send out new data packets */
476         if (hc->tx_seqh == hc->tx_seqt)
477                 return;
478
479         switch (DCCP_SKB_CB(skb)->dccpd_type) {
480         case DCCP_PKT_ACK:
481         case DCCP_PKT_DATAACK:
482                 break;
483         default:
484                 return;
485         }
486
487         ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
488         if (after48(ackno, hc->tx_high_ack))
489                 hc->tx_high_ack = ackno;
490
491         seqp = hc->tx_seqt;
492         while (before48(seqp->ccid2s_seq, ackno)) {
493                 seqp = seqp->ccid2s_next;
494                 if (seqp == hc->tx_seqh) {
495                         seqp = hc->tx_seqh->ccid2s_prev;
496                         break;
497                 }
498         }
499
500         /*
501          * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
502          * packets per acknowledgement. Rounding up avoids that cwnd is not
503          * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
504          */
505         if (hc->tx_cwnd < hc->tx_ssthresh)
506                 maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
507
508         /* go through all ack vectors */
509         while ((offset = ccid2_ackvector(sk, skb, offset,
510                                          &vector, &veclen)) != -1) {
511                 /* go through this ack vector */
512                 while (veclen--) {
513                         const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
514                         u64 ackno_end_rl = SUB48(ackno, rl);
515
516                         ccid2_pr_debug("ackvec start:%llu end:%llu\n",
517                                        (unsigned long long)ackno,
518                                        (unsigned long long)ackno_end_rl);
519                         /* if the seqno we are analyzing is larger than the
520                          * current ackno, then move towards the tail of our
521                          * seqnos.
522                          */
523                         while (after48(seqp->ccid2s_seq, ackno)) {
524                                 if (seqp == hc->tx_seqt) {
525                                         done = 1;
526                                         break;
527                                 }
528                                 seqp = seqp->ccid2s_prev;
529                         }
530                         if (done)
531                                 break;
532
533                         /* check all seqnos in the range of the vector
534                          * run length
535                          */
536                         while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
537                                 const u8 state = *vector &
538                                                  DCCP_ACKVEC_STATE_MASK;
539
540                                 /* new packet received or marked */
541                                 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
542                                     !seqp->ccid2s_acked) {
543                                         if (state ==
544                                             DCCP_ACKVEC_STATE_ECN_MARKED) {
545                                                 ccid2_congestion_event(sk,
546                                                                        seqp);
547                                         } else
548                                                 ccid2_new_ack(sk, seqp,
549                                                               &maxincr);
550
551                                         seqp->ccid2s_acked = 1;
552                                         ccid2_pr_debug("Got ack for %llu\n",
553                                                        (unsigned long long)seqp->ccid2s_seq);
554                                         hc->tx_pipe--;
555                                 }
556                                 if (seqp == hc->tx_seqt) {
557                                         done = 1;
558                                         break;
559                                 }
560                                 seqp = seqp->ccid2s_prev;
561                         }
562                         if (done)
563                                 break;
564
565                         ackno = SUB48(ackno_end_rl, 1);
566                         vector++;
567                 }
568                 if (done)
569                         break;
570         }
571
572         /* The state about what is acked should be correct now
573          * Check for NUMDUPACK
574          */
575         seqp = hc->tx_seqt;
576         while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
577                 seqp = seqp->ccid2s_next;
578                 if (seqp == hc->tx_seqh) {
579                         seqp = hc->tx_seqh->ccid2s_prev;
580                         break;
581                 }
582         }
583         done = 0;
584         while (1) {
585                 if (seqp->ccid2s_acked) {
586                         done++;
587                         if (done == NUMDUPACK)
588                                 break;
589                 }
590                 if (seqp == hc->tx_seqt)
591                         break;
592                 seqp = seqp->ccid2s_prev;
593         }
594
595         /* If there are at least 3 acknowledgements, anything unacknowledged
596          * below the last sequence number is considered lost
597          */
598         if (done == NUMDUPACK) {
599                 struct ccid2_seq *last_acked = seqp;
600
601                 /* check for lost packets */
602                 while (1) {
603                         if (!seqp->ccid2s_acked) {
604                                 ccid2_pr_debug("Packet lost: %llu\n",
605                                                (unsigned long long)seqp->ccid2s_seq);
606                                 /* XXX need to traverse from tail -> head in
607                                  * order to detect multiple congestion events in
608                                  * one ack vector.
609                                  */
610                                 ccid2_congestion_event(sk, seqp);
611                                 hc->tx_pipe--;
612                         }
613                         if (seqp == hc->tx_seqt)
614                                 break;
615                         seqp = seqp->ccid2s_prev;
616                 }
617
618                 hc->tx_seqt = last_acked;
619         }
620
621         /* trim acked packets in tail */
622         while (hc->tx_seqt != hc->tx_seqh) {
623                 if (!hc->tx_seqt->ccid2s_acked)
624                         break;
625
626                 hc->tx_seqt = hc->tx_seqt->ccid2s_next;
627         }
628
629         /* restart RTO timer if not all outstanding data has been acked */
630         if (hc->tx_pipe == 0)
631                 sk_stop_timer(sk, &hc->tx_rtotimer);
632         else
633                 sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
634 }
635
636 static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
637 {
638         struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
639         struct dccp_sock *dp = dccp_sk(sk);
640         u32 max_ratio;
641
642         /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
643         hc->tx_ssthresh = ~0U;
644
645         /* Use larger initial windows (RFC 4341, section 5). */
646         hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache);
647
648         /* Make sure that Ack Ratio is enabled and within bounds. */
649         max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
650         if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
651                 dp->dccps_l_ack_ratio = max_ratio;
652
653         /* XXX init ~ to window size... */
654         if (ccid2_hc_tx_alloc_seq(hc))
655                 return -ENOMEM;
656
657         hc->tx_rto       = DCCP_TIMEOUT_INIT;
658         hc->tx_rpdupack  = -1;
659         hc->tx_last_cong = ccid2_time_stamp;
660         setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire,
661                         (unsigned long)sk);
662         return 0;
663 }
664
665 static void ccid2_hc_tx_exit(struct sock *sk)
666 {
667         struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
668         int i;
669
670         sk_stop_timer(sk, &hc->tx_rtotimer);
671
672         for (i = 0; i < hc->tx_seqbufc; i++)
673                 kfree(hc->tx_seqbuf[i]);
674         hc->tx_seqbufc = 0;
675 }
676
677 static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
678 {
679         const struct dccp_sock *dp = dccp_sk(sk);
680         struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
681
682         switch (DCCP_SKB_CB(skb)->dccpd_type) {
683         case DCCP_PKT_DATA:
684         case DCCP_PKT_DATAACK:
685                 hc->rx_data++;
686                 if (hc->rx_data >= dp->dccps_r_ack_ratio) {
687                         dccp_send_ack(sk);
688                         hc->rx_data = 0;
689                 }
690                 break;
691         }
692 }
693
694 struct ccid_operations ccid2_ops = {
695         .ccid_id                = DCCPC_CCID2,
696         .ccid_name              = "TCP-like",
697         .ccid_hc_tx_obj_size    = sizeof(struct ccid2_hc_tx_sock),
698         .ccid_hc_tx_init        = ccid2_hc_tx_init,
699         .ccid_hc_tx_exit        = ccid2_hc_tx_exit,
700         .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
701         .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
702         .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
703         .ccid_hc_rx_obj_size    = sizeof(struct ccid2_hc_rx_sock),
704         .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
705 };
706
707 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
708 module_param(ccid2_debug, bool, 0644);
709 MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
710 #endif