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