c7d83e3c1648ceaf63d49d9867f554cbda042fa7
[safe/jmp/linux-2.6] / net / dccp / ccids / ccid2.c
1 /*
2  *  net/dccp/ccids/ccid2.c
3  *
4  *  Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
5  *
6  *  Changes to meet Linux coding standards, and DCCP infrastructure fixes.
7  *
8  *  Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /*
26  * This implementation should follow RFC 4341
27  */
28 #include "../feat.h"
29 #include "../ccid.h"
30 #include "../dccp.h"
31 #include "ccid2.h"
32
33
34 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
35 static int ccid2_debug;
36 #define ccid2_pr_debug(format, a...)    DCCP_PR_DEBUG(ccid2_debug, format, ##a)
37
38 static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
39 {
40         int len = 0;
41         int pipe = 0;
42         struct ccid2_seq *seqp = hctx->seqh;
43
44         /* there is data in the chain */
45         if (seqp != hctx->seqt) {
46                 seqp = seqp->ccid2s_prev;
47                 len++;
48                 if (!seqp->ccid2s_acked)
49                         pipe++;
50
51                 while (seqp != hctx->seqt) {
52                         struct ccid2_seq *prev = seqp->ccid2s_prev;
53
54                         len++;
55                         if (!prev->ccid2s_acked)
56                                 pipe++;
57
58                         /* packets are sent sequentially */
59                         BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
60                                                 prev->ccid2s_seq ) >= 0);
61                         BUG_ON(time_before(seqp->ccid2s_sent,
62                                            prev->ccid2s_sent));
63
64                         seqp = prev;
65                 }
66         }
67
68         BUG_ON(pipe != hctx->pipe);
69         ccid2_pr_debug("len of chain=%d\n", len);
70
71         do {
72                 seqp = seqp->ccid2s_prev;
73                 len++;
74         } while (seqp != hctx->seqh);
75
76         ccid2_pr_debug("total len=%d\n", len);
77         BUG_ON(len != hctx->seqbufc * CCID2_SEQBUF_LEN);
78 }
79 #else
80 #define ccid2_pr_debug(format, a...)
81 #define ccid2_hc_tx_check_sanity(hctx)
82 #endif
83
84 static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx)
85 {
86         struct ccid2_seq *seqp;
87         int i;
88
89         /* check if we have space to preserve the pointer to the buffer */
90         if (hctx->seqbufc >= sizeof(hctx->seqbuf) / sizeof(struct ccid2_seq *))
91                 return -ENOMEM;
92
93         /* allocate buffer and initialize linked list */
94         seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
95         if (seqp == NULL)
96                 return -ENOMEM;
97
98         for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
99                 seqp[i].ccid2s_next = &seqp[i + 1];
100                 seqp[i + 1].ccid2s_prev = &seqp[i];
101         }
102         seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
103         seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
104
105         /* This is the first allocation.  Initiate the head and tail.  */
106         if (hctx->seqbufc == 0)
107                 hctx->seqh = hctx->seqt = seqp;
108         else {
109                 /* link the existing list with the one we just created */
110                 hctx->seqh->ccid2s_next = seqp;
111                 seqp->ccid2s_prev = hctx->seqh;
112
113                 hctx->seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
114                 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hctx->seqt;
115         }
116
117         /* store the original pointer to the buffer so we can free it */
118         hctx->seqbuf[hctx->seqbufc] = seqp;
119         hctx->seqbufc++;
120
121         return 0;
122 }
123
124 static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
125 {
126         if (ccid2_cwnd_network_limited(ccid2_hc_tx_sk(sk)))
127                 return CCID_PACKET_WILL_DEQUEUE_LATER;
128         return CCID_PACKET_SEND_AT_ONCE;
129 }
130
131 static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
132 {
133         struct dccp_sock *dp = dccp_sk(sk);
134         u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->cwnd, 2);
135
136         /*
137          * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
138          * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
139          * acceptable since this causes starvation/deadlock whenever cwnd < 2.
140          * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
141          */
142         if (val == 0 || val > max_ratio) {
143                 DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
144                 val = max_ratio;
145         }
146         if (val > DCCPF_ACK_RATIO_MAX)
147                 val = DCCPF_ACK_RATIO_MAX;
148
149         if (val == dp->dccps_l_ack_ratio)
150                 return;
151
152         ccid2_pr_debug("changing local ack ratio to %u\n", val);
153         dp->dccps_l_ack_ratio = val;
154 }
155
156 static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val)
157 {
158         ccid2_pr_debug("change SRTT to %ld\n", val);
159         hctx->srtt = val;
160 }
161
162 static void ccid2_start_rto_timer(struct sock *sk);
163
164 static void ccid2_hc_tx_rto_expire(unsigned long data)
165 {
166         struct sock *sk = (struct sock *)data;
167         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
168         const bool sender_was_blocked = ccid2_cwnd_network_limited(hctx);
169         long s;
170
171         bh_lock_sock(sk);
172         if (sock_owned_by_user(sk)) {
173                 sk_reset_timer(sk, &hctx->rtotimer, jiffies + HZ / 5);
174                 goto out;
175         }
176
177         ccid2_pr_debug("RTO_EXPIRE\n");
178
179         ccid2_hc_tx_check_sanity(hctx);
180
181         /* back-off timer */
182         hctx->rto <<= 1;
183
184         s = hctx->rto / HZ;
185         if (s > 60)
186                 hctx->rto = 60 * HZ;
187
188         /* adjust pipe, cwnd etc */
189         hctx->ssthresh = hctx->cwnd / 2;
190         if (hctx->ssthresh < 2)
191                 hctx->ssthresh = 2;
192         hctx->cwnd = 1;
193         hctx->pipe = 0;
194
195         /* clear state about stuff we sent */
196         hctx->seqt = hctx->seqh;
197         hctx->packets_acked = 0;
198
199         /* clear ack ratio state. */
200         hctx->rpseq    = 0;
201         hctx->rpdupack = -1;
202         ccid2_change_l_ack_ratio(sk, 1);
203         ccid2_hc_tx_check_sanity(hctx);
204
205         /* if we were blocked before, we may now send cwnd=1 packet */
206         if (sender_was_blocked)
207                 tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
208         ccid2_start_rto_timer(sk);
209 out:
210         bh_unlock_sock(sk);
211         sock_put(sk);
212 }
213
214 static void ccid2_start_rto_timer(struct sock *sk)
215 {
216         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
217
218         ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->rto);
219
220         BUG_ON(timer_pending(&hctx->rtotimer));
221         sk_reset_timer(sk, &hctx->rtotimer,
222                        jiffies + hctx->rto);
223 }
224
225 static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
226 {
227         struct dccp_sock *dp = dccp_sk(sk);
228         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
229         struct ccid2_seq *next;
230
231         hctx->pipe++;
232
233         hctx->seqh->ccid2s_seq   = dp->dccps_gss;
234         hctx->seqh->ccid2s_acked = 0;
235         hctx->seqh->ccid2s_sent  = jiffies;
236
237         next = hctx->seqh->ccid2s_next;
238         /* check if we need to alloc more space */
239         if (next == hctx->seqt) {
240                 if (ccid2_hc_tx_alloc_seq(hctx)) {
241                         DCCP_CRIT("packet history - out of memory!");
242                         /* FIXME: find a more graceful way to bail out */
243                         return;
244                 }
245                 next = hctx->seqh->ccid2s_next;
246                 BUG_ON(next == hctx->seqt);
247         }
248         hctx->seqh = next;
249
250         ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->cwnd, hctx->pipe);
251
252         /*
253          * FIXME: The code below is broken and the variables have been removed
254          * from the socket struct. The `ackloss' variable was always set to 0,
255          * and with arsent there are several problems:
256          *  (i) it doesn't just count the number of Acks, but all sent packets;
257          *  (ii) it is expressed in # of packets, not # of windows, so the
258          *  comparison below uses the wrong formula: Appendix A of RFC 4341
259          *  comes up with the number K = cwnd / (R^2 - R) of consecutive windows
260          *  of data with no lost or marked Ack packets. If arsent were the # of
261          *  consecutive Acks received without loss, then Ack Ratio needs to be
262          *  decreased by 1 when
263          *            arsent >=  K * cwnd / R  =  cwnd^2 / (R^3 - R^2)
264          *  where cwnd / R is the number of Acks received per window of data
265          *  (cf. RFC 4341, App. A). The problems are that
266          *  - arsent counts other packets as well;
267          *  - the comparison uses a formula different from RFC 4341;
268          *  - computing a cubic/quadratic equation each time is too complicated.
269          *  Hence a different algorithm is needed.
270          */
271 #if 0
272         /* Ack Ratio.  Need to maintain a concept of how many windows we sent */
273         hctx->arsent++;
274         /* We had an ack loss in this window... */
275         if (hctx->ackloss) {
276                 if (hctx->arsent >= hctx->cwnd) {
277                         hctx->arsent  = 0;
278                         hctx->ackloss = 0;
279                 }
280         } else {
281                 /* No acks lost up to now... */
282                 /* decrease ack ratio if enough packets were sent */
283                 if (dp->dccps_l_ack_ratio > 1) {
284                         /* XXX don't calculate denominator each time */
285                         int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
286                                     dp->dccps_l_ack_ratio;
287
288                         denom = hctx->cwnd * hctx->cwnd / denom;
289
290                         if (hctx->arsent >= denom) {
291                                 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
292                                 hctx->arsent = 0;
293                         }
294                 } else {
295                         /* we can't increase ack ratio further [1] */
296                         hctx->arsent = 0; /* or maybe set it to cwnd*/
297                 }
298         }
299 #endif
300
301         /* setup RTO timer */
302         if (!timer_pending(&hctx->rtotimer))
303                 ccid2_start_rto_timer(sk);
304
305 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
306         do {
307                 struct ccid2_seq *seqp = hctx->seqt;
308
309                 while (seqp != hctx->seqh) {
310                         ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
311                                        (unsigned long long)seqp->ccid2s_seq,
312                                        seqp->ccid2s_acked, seqp->ccid2s_sent);
313                         seqp = seqp->ccid2s_next;
314                 }
315         } while (0);
316         ccid2_pr_debug("=========\n");
317         ccid2_hc_tx_check_sanity(hctx);
318 #endif
319 }
320
321 static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
322 {
323         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
324
325         sk_stop_timer(sk, &hctx->rtotimer);
326         ccid2_pr_debug("deleted RTO timer\n");
327 }
328
329 static inline void ccid2_new_ack(struct sock *sk,
330                                  struct ccid2_seq *seqp,
331                                  unsigned int *maxincr)
332 {
333         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
334
335         if (hctx->cwnd < hctx->ssthresh) {
336                 if (*maxincr > 0 && ++hctx->packets_acked == 2) {
337                         hctx->cwnd += 1;
338                         *maxincr   -= 1;
339                         hctx->packets_acked = 0;
340                 }
341         } else if (++hctx->packets_acked >= hctx->cwnd) {
342                         hctx->cwnd += 1;
343                         hctx->packets_acked = 0;
344         }
345
346         /* update RTO */
347         if (hctx->srtt == -1 ||
348             time_after(jiffies, hctx->lastrtt + hctx->srtt)) {
349                 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
350                 int s;
351
352                 /* first measurement */
353                 if (hctx->srtt == -1) {
354                         ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
355                                        r, jiffies,
356                                        (unsigned long long)seqp->ccid2s_seq);
357                         ccid2_change_srtt(hctx, r);
358                         hctx->rttvar = r >> 1;
359                 } else {
360                         /* RTTVAR */
361                         long tmp = hctx->srtt - r;
362                         long srtt;
363
364                         if (tmp < 0)
365                                 tmp *= -1;
366
367                         tmp >>= 2;
368                         hctx->rttvar *= 3;
369                         hctx->rttvar >>= 2;
370                         hctx->rttvar += tmp;
371
372                         /* SRTT */
373                         srtt = hctx->srtt;
374                         srtt *= 7;
375                         srtt >>= 3;
376                         tmp = r >> 3;
377                         srtt += tmp;
378                         ccid2_change_srtt(hctx, srtt);
379                 }
380                 s = hctx->rttvar << 2;
381                 /* clock granularity is 1 when based on jiffies */
382                 if (!s)
383                         s = 1;
384                 hctx->rto = hctx->srtt + s;
385
386                 /* must be at least a second */
387                 s = hctx->rto / HZ;
388                 /* DCCP doesn't require this [but I like it cuz my code sux] */
389 #if 1
390                 if (s < 1)
391                         hctx->rto = HZ;
392 #endif
393                 /* max 60 seconds */
394                 if (s > 60)
395                         hctx->rto = HZ * 60;
396
397                 hctx->lastrtt = jiffies;
398
399                 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
400                                hctx->srtt, hctx->rttvar,
401                                hctx->rto, HZ, r);
402         }
403
404         /* we got a new ack, so re-start RTO timer */
405         ccid2_hc_tx_kill_rto_timer(sk);
406         ccid2_start_rto_timer(sk);
407 }
408
409 static void ccid2_hc_tx_dec_pipe(struct sock *sk)
410 {
411         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
412
413         if (hctx->pipe == 0)
414                 DCCP_BUG("pipe == 0");
415         else
416                 hctx->pipe--;
417
418         if (hctx->pipe == 0)
419                 ccid2_hc_tx_kill_rto_timer(sk);
420 }
421
422 static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
423 {
424         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
425
426         if (time_before(seqp->ccid2s_sent, hctx->last_cong)) {
427                 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
428                 return;
429         }
430
431         hctx->last_cong = jiffies;
432
433         hctx->cwnd     = hctx->cwnd / 2 ? : 1U;
434         hctx->ssthresh = max(hctx->cwnd, 2U);
435
436         /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
437         if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->cwnd)
438                 ccid2_change_l_ack_ratio(sk, hctx->cwnd);
439 }
440
441 static int ccid2_hc_tx_parse_options(struct sock *sk, u8 packet_type,
442                                      u8 option, u8 *optval, u8 optlen)
443 {
444         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
445
446         switch (option) {
447         case DCCPO_ACK_VECTOR_0:
448         case DCCPO_ACK_VECTOR_1:
449                 return dccp_ackvec_parsed_add(&hctx->av_chunks, optval, optlen,
450                                               option - DCCPO_ACK_VECTOR_0);
451         }
452         return 0;
453 }
454
455 static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
456 {
457         struct dccp_sock *dp = dccp_sk(sk);
458         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
459         const bool sender_was_blocked = ccid2_cwnd_network_limited(hctx);
460         struct dccp_ackvec_parsed *avp;
461         u64 ackno, seqno;
462         struct ccid2_seq *seqp;
463         int done = 0;
464         unsigned int maxincr = 0;
465
466         ccid2_hc_tx_check_sanity(hctx);
467         /* check reverse path congestion */
468         seqno = DCCP_SKB_CB(skb)->dccpd_seq;
469
470         /* XXX this whole "algorithm" is broken.  Need to fix it to keep track
471          * of the seqnos of the dupacks so that rpseq and rpdupack are correct
472          * -sorbo.
473          */
474         /* need to bootstrap */
475         if (hctx->rpdupack == -1) {
476                 hctx->rpdupack = 0;
477                 hctx->rpseq = seqno;
478         } else {
479                 /* check if packet is consecutive */
480                 if (dccp_delta_seqno(hctx->rpseq, seqno) == 1)
481                         hctx->rpseq = seqno;
482                 /* it's a later packet */
483                 else if (after48(seqno, hctx->rpseq)) {
484                         hctx->rpdupack++;
485
486                         /* check if we got enough dupacks */
487                         if (hctx->rpdupack >= NUMDUPACK) {
488                                 hctx->rpdupack = -1; /* XXX lame */
489                                 hctx->rpseq = 0;
490
491                                 ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
492                         }
493                 }
494         }
495
496         /* check forward path congestion */
497         if (dccp_packet_without_ack(skb))
498                 return;
499
500         /* still didn't send out new data packets */
501         if (hctx->seqh == hctx->seqt)
502                 goto done;
503
504         ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
505         if (after48(ackno, hctx->high_ack))
506                 hctx->high_ack = ackno;
507
508         seqp = hctx->seqt;
509         while (before48(seqp->ccid2s_seq, ackno)) {
510                 seqp = seqp->ccid2s_next;
511                 if (seqp == hctx->seqh) {
512                         seqp = hctx->seqh->ccid2s_prev;
513                         break;
514                 }
515         }
516
517         /*
518          * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
519          * packets per acknowledgement. Rounding up avoids that cwnd is not
520          * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
521          */
522         if (hctx->cwnd < hctx->ssthresh)
523                 maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
524
525         /* go through all ack vectors */
526         list_for_each_entry(avp, &hctx->av_chunks, node) {
527                 /* go through this ack vector */
528                 for (; avp->len--; avp->vec++) {
529                         u64 ackno_end_rl = SUB48(ackno,
530                                                  dccp_ackvec_runlen(avp->vec));
531
532                         ccid2_pr_debug("ackvec %llu |%u,%u|\n",
533                                        (unsigned long long)ackno,
534                                        dccp_ackvec_state(avp->vec) >> 6,
535                                        dccp_ackvec_runlen(avp->vec));
536                         /* if the seqno we are analyzing is larger than the
537                          * current ackno, then move towards the tail of our
538                          * seqnos.
539                          */
540                         while (after48(seqp->ccid2s_seq, ackno)) {
541                                 if (seqp == hctx->seqt) {
542                                         done = 1;
543                                         break;
544                                 }
545                                 seqp = seqp->ccid2s_prev;
546                         }
547                         if (done)
548                                 break;
549
550                         /* check all seqnos in the range of the vector
551                          * run length
552                          */
553                         while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
554                                 const u8 state = dccp_ackvec_state(avp->vec);
555
556                                 /* new packet received or marked */
557                                 if (state != DCCPAV_NOT_RECEIVED &&
558                                     !seqp->ccid2s_acked) {
559                                         if (state == DCCPAV_ECN_MARKED)
560                                                 ccid2_congestion_event(sk,
561                                                                        seqp);
562                                         else
563                                                 ccid2_new_ack(sk, seqp,
564                                                               &maxincr);
565
566                                         seqp->ccid2s_acked = 1;
567                                         ccid2_pr_debug("Got ack for %llu\n",
568                                                        (unsigned long long)seqp->ccid2s_seq);
569                                         ccid2_hc_tx_dec_pipe(sk);
570                                 }
571                                 if (seqp == hctx->seqt) {
572                                         done = 1;
573                                         break;
574                                 }
575                                 seqp = seqp->ccid2s_prev;
576                         }
577                         if (done)
578                                 break;
579
580                         ackno = SUB48(ackno_end_rl, 1);
581                 }
582                 if (done)
583                         break;
584         }
585
586         /* The state about what is acked should be correct now
587          * Check for NUMDUPACK
588          */
589         seqp = hctx->seqt;
590         while (before48(seqp->ccid2s_seq, hctx->high_ack)) {
591                 seqp = seqp->ccid2s_next;
592                 if (seqp == hctx->seqh) {
593                         seqp = hctx->seqh->ccid2s_prev;
594                         break;
595                 }
596         }
597         done = 0;
598         while (1) {
599                 if (seqp->ccid2s_acked) {
600                         done++;
601                         if (done == NUMDUPACK)
602                                 break;
603                 }
604                 if (seqp == hctx->seqt)
605                         break;
606                 seqp = seqp->ccid2s_prev;
607         }
608
609         /* If there are at least 3 acknowledgements, anything unacknowledged
610          * below the last sequence number is considered lost
611          */
612         if (done == NUMDUPACK) {
613                 struct ccid2_seq *last_acked = seqp;
614
615                 /* check for lost packets */
616                 while (1) {
617                         if (!seqp->ccid2s_acked) {
618                                 ccid2_pr_debug("Packet lost: %llu\n",
619                                                (unsigned long long)seqp->ccid2s_seq);
620                                 /* XXX need to traverse from tail -> head in
621                                  * order to detect multiple congestion events in
622                                  * one ack vector.
623                                  */
624                                 ccid2_congestion_event(sk, seqp);
625                                 ccid2_hc_tx_dec_pipe(sk);
626                         }
627                         if (seqp == hctx->seqt)
628                                 break;
629                         seqp = seqp->ccid2s_prev;
630                 }
631
632                 hctx->seqt = last_acked;
633         }
634
635         /* trim acked packets in tail */
636         while (hctx->seqt != hctx->seqh) {
637                 if (!hctx->seqt->ccid2s_acked)
638                         break;
639
640                 hctx->seqt = hctx->seqt->ccid2s_next;
641         }
642
643         ccid2_hc_tx_check_sanity(hctx);
644 done:
645         /* check if incoming Acks allow pending packets to be sent */
646         if (sender_was_blocked && !ccid2_cwnd_network_limited(hctx))
647                 tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
648         dccp_ackvec_parsed_cleanup(&hctx->av_chunks);
649 }
650
651 static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
652 {
653         struct ccid2_hc_tx_sock *hctx = 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         hctx->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         hctx->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(hctx->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(hctx))
674                 return -ENOMEM;
675
676         hctx->rto        = 3 * HZ;
677         ccid2_change_srtt(hctx, -1);
678         hctx->rttvar    = -1;
679         hctx->rpdupack  = -1;
680         hctx->last_cong = jiffies;
681         setup_timer(&hctx->rtotimer, ccid2_hc_tx_rto_expire, (unsigned long)sk);
682         INIT_LIST_HEAD(&hctx->av_chunks);
683
684         ccid2_hc_tx_check_sanity(hctx);
685         return 0;
686 }
687
688 static void ccid2_hc_tx_exit(struct sock *sk)
689 {
690         struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
691         int i;
692
693         ccid2_hc_tx_kill_rto_timer(sk);
694
695         for (i = 0; i < hctx->seqbufc; i++)
696                 kfree(hctx->seqbuf[i]);
697         hctx->seqbufc = 0;
698 }
699
700 static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
701 {
702         const struct dccp_sock *dp = dccp_sk(sk);
703         struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
704
705         switch (DCCP_SKB_CB(skb)->dccpd_type) {
706         case DCCP_PKT_DATA:
707         case DCCP_PKT_DATAACK:
708                 hcrx->data++;
709                 if (hcrx->data >= dp->dccps_r_ack_ratio) {
710                         dccp_send_ack(sk);
711                         hcrx->data = 0;
712                 }
713                 break;
714         }
715 }
716
717 static struct ccid_operations ccid2 = {
718         .ccid_id                  = DCCPC_CCID2,
719         .ccid_name                = "TCP-like",
720         .ccid_owner               = THIS_MODULE,
721         .ccid_hc_tx_obj_size      = sizeof(struct ccid2_hc_tx_sock),
722         .ccid_hc_tx_init          = ccid2_hc_tx_init,
723         .ccid_hc_tx_exit          = ccid2_hc_tx_exit,
724         .ccid_hc_tx_send_packet   = ccid2_hc_tx_send_packet,
725         .ccid_hc_tx_packet_sent   = ccid2_hc_tx_packet_sent,
726         .ccid_hc_tx_parse_options = ccid2_hc_tx_parse_options,
727         .ccid_hc_tx_packet_recv   = ccid2_hc_tx_packet_recv,
728         .ccid_hc_rx_obj_size      = sizeof(struct ccid2_hc_rx_sock),
729         .ccid_hc_rx_packet_recv   = ccid2_hc_rx_packet_recv,
730 };
731
732 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
733 module_param(ccid2_debug, bool, 0644);
734 MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
735 #endif
736
737 static __init int ccid2_module_init(void)
738 {
739         return ccid_register(&ccid2);
740 }
741 module_init(ccid2_module_init);
742
743 static __exit void ccid2_module_exit(void)
744 {
745         ccid_unregister(&ccid2);
746 }
747 module_exit(ccid2_module_exit);
748
749 MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
750 MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
751 MODULE_LICENSE("GPL");
752 MODULE_ALIAS("net-dccp-ccid-2");