[DCCP] CCID2: Allocate seq records on demand
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / dccp / ccids / ccid2.c
CommitLineData
2a91aa39
AB
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: draft-ietf-dccp-ccid2-10.txt
27 *
28 * BUGS:
29 * - sequence number wrapping
2a91aa39
AB
30 */
31
2a91aa39
AB
32#include "../ccid.h"
33#include "../dccp.h"
34#include "ccid2.h"
35
36static int ccid2_debug;
37
8d424f6c 38#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
2a91aa39
AB
39#define ccid2_pr_debug(format, a...) \
40 do { if (ccid2_debug) \
41 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
42 } while (0)
43#else
44#define ccid2_pr_debug(format, a...)
45#endif
46
8d424f6c 47#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
2a91aa39
AB
48static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
49{
50 int len = 0;
2a91aa39 51 int pipe = 0;
c0c736db 52 struct ccid2_seq *seqp = hctx->ccid2hctx_seqh;
2a91aa39
AB
53
54 /* there is data in the chain */
55 if (seqp != hctx->ccid2hctx_seqt) {
56 seqp = seqp->ccid2s_prev;
57 len++;
58 if (!seqp->ccid2s_acked)
59 pipe++;
60
61 while (seqp != hctx->ccid2hctx_seqt) {
c0c736db 62 struct ccid2_seq *prev = seqp->ccid2s_prev;
2a91aa39 63
2a91aa39
AB
64 len++;
65 if (!prev->ccid2s_acked)
66 pipe++;
67
68 /* packets are sent sequentially */
69 BUG_ON(seqp->ccid2s_seq <= prev->ccid2s_seq);
29651cda
AB
70 BUG_ON(time_before(seqp->ccid2s_sent,
71 prev->ccid2s_sent));
2a91aa39
AB
72
73 seqp = prev;
74 }
75 }
76
77 BUG_ON(pipe != hctx->ccid2hctx_pipe);
78 ccid2_pr_debug("len of chain=%d\n", len);
79
80 do {
81 seqp = seqp->ccid2s_prev;
82 len++;
c0c736db 83 } while (seqp != hctx->ccid2hctx_seqh);
2a91aa39 84
2a91aa39 85 ccid2_pr_debug("total len=%d\n", len);
07978aab 86 BUG_ON(len != hctx->ccid2hctx_seqbufc * CCID2_SEQBUF_LEN);
2a91aa39
AB
87}
88#else
89#define ccid2_hc_tx_check_sanity(hctx) do {} while (0)
90#endif
91
07978aab
AB
92static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx, int num,
93 gfp_t gfp)
94{
95 struct ccid2_seq *seqp;
96 int i;
97
98 /* check if we have space to preserve the pointer to the buffer */
99 if (hctx->ccid2hctx_seqbufc >= (sizeof(hctx->ccid2hctx_seqbuf) /
100 sizeof(struct ccid2_seq*)))
101 return -ENOMEM;
102
103 /* allocate buffer and initialize linked list */
104 seqp = kmalloc(sizeof(*seqp) * num, gfp);
105 if (seqp == NULL)
106 return -ENOMEM;
107
108 for (i = 0; i < (num - 1); i++) {
109 seqp[i].ccid2s_next = &seqp[i + 1];
110 seqp[i + 1].ccid2s_prev = &seqp[i];
111 }
112 seqp[num - 1].ccid2s_next = seqp;
113 seqp->ccid2s_prev = &seqp[num - 1];
114
115 /* This is the first allocation. Initiate the head and tail. */
116 if (hctx->ccid2hctx_seqbufc == 0)
117 hctx->ccid2hctx_seqh = hctx->ccid2hctx_seqt = seqp;
118 else {
119 /* link the existing list with the one we just created */
120 hctx->ccid2hctx_seqh->ccid2s_next = seqp;
121 seqp->ccid2s_prev = hctx->ccid2hctx_seqh;
122
123 hctx->ccid2hctx_seqt->ccid2s_prev = &seqp[num - 1];
124 seqp[num - 1].ccid2s_next = hctx->ccid2hctx_seqt;
125 }
126
127 /* store the original pointer to the buffer so we can free it */
128 hctx->ccid2hctx_seqbuf[hctx->ccid2hctx_seqbufc] = seqp;
129 hctx->ccid2hctx_seqbufc++;
130
131 return 0;
132}
133
2a91aa39
AB
134static int ccid2_hc_tx_send_packet(struct sock *sk,
135 struct sk_buff *skb, int len)
136{
137 struct ccid2_hc_tx_sock *hctx;
138
139 switch (DCCP_SKB_CB(skb)->dccpd_type) {
140 case 0: /* XXX data packets from userland come through like this */
141 case DCCP_PKT_DATA:
142 case DCCP_PKT_DATAACK:
143 break;
144 /* No congestion control on other packets */
145 default:
146 return 0;
147 }
148
149 hctx = ccid2_hc_tx_sk(sk);
150
151 ccid2_pr_debug("pipe=%d cwnd=%d\n", hctx->ccid2hctx_pipe,
152 hctx->ccid2hctx_cwnd);
153
154 if (hctx->ccid2hctx_pipe < hctx->ccid2hctx_cwnd) {
155 /* OK we can send... make sure previous packet was sent off */
156 if (!hctx->ccid2hctx_sendwait) {
157 hctx->ccid2hctx_sendwait = 1;
158 return 0;
159 }
160 }
161
446dec30 162 return 1; /* XXX CCID should dequeue when ready instead of polling */
2a91aa39
AB
163}
164
165static void ccid2_change_l_ack_ratio(struct sock *sk, int val)
166{
167 struct dccp_sock *dp = dccp_sk(sk);
168 /*
169 * XXX I don't really agree with val != 2. If cwnd is 1, ack ratio
170 * should be 1... it shouldn't be allowed to become 2.
171 * -sorbo.
172 */
173 if (val != 2) {
c0c736db 174 const struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
2a91aa39
AB
175 int max = hctx->ccid2hctx_cwnd / 2;
176
177 /* round up */
178 if (hctx->ccid2hctx_cwnd & 1)
179 max++;
180
181 if (val > max)
182 val = max;
183 }
184
185 ccid2_pr_debug("changing local ack ratio to %d\n", val);
186 WARN_ON(val <= 0);
187 dp->dccps_l_ack_ratio = val;
188}
189
190static void ccid2_change_cwnd(struct sock *sk, int val)
191{
192 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
193
194 if (val == 0)
195 val = 1;
196
197 /* XXX do we need to change ack ratio? */
198 ccid2_pr_debug("change cwnd to %d\n", val);
199
200 BUG_ON(val < 1);
201 hctx->ccid2hctx_cwnd = val;
202}
203
204static void ccid2_start_rto_timer(struct sock *sk);
205
206static void ccid2_hc_tx_rto_expire(unsigned long data)
207{
208 struct sock *sk = (struct sock *)data;
209 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
210 long s;
211
2a91aa39
AB
212 bh_lock_sock(sk);
213 if (sock_owned_by_user(sk)) {
214 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
215 jiffies + HZ / 5);
216 goto out;
217 }
218
219 ccid2_pr_debug("RTO_EXPIRE\n");
220
221 ccid2_hc_tx_check_sanity(hctx);
222
223 /* back-off timer */
224 hctx->ccid2hctx_rto <<= 1;
225
226 s = hctx->ccid2hctx_rto / HZ;
227 if (s > 60)
228 hctx->ccid2hctx_rto = 60 * HZ;
229
230 ccid2_start_rto_timer(sk);
231
232 /* adjust pipe, cwnd etc */
233 hctx->ccid2hctx_pipe = 0;
234 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd >> 1;
235 if (hctx->ccid2hctx_ssthresh < 2)
236 hctx->ccid2hctx_ssthresh = 2;
237 ccid2_change_cwnd(sk, 1);
238
239 /* clear state about stuff we sent */
240 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
241 hctx->ccid2hctx_ssacks = 0;
242 hctx->ccid2hctx_acks = 0;
243 hctx->ccid2hctx_sent = 0;
244
245 /* clear ack ratio state. */
246 hctx->ccid2hctx_arsent = 0;
247 hctx->ccid2hctx_ackloss = 0;
248 hctx->ccid2hctx_rpseq = 0;
249 hctx->ccid2hctx_rpdupack = -1;
250 ccid2_change_l_ack_ratio(sk, 1);
251 ccid2_hc_tx_check_sanity(hctx);
252out:
253 bh_unlock_sock(sk);
77ff72d5 254 sock_put(sk);
2a91aa39
AB
255}
256
257static void ccid2_start_rto_timer(struct sock *sk)
258{
259 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
260
261 ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->ccid2hctx_rto);
262
263 BUG_ON(timer_pending(&hctx->ccid2hctx_rtotimer));
264 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
265 jiffies + hctx->ccid2hctx_rto);
266}
267
268static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, int len)
269{
270 struct dccp_sock *dp = dccp_sk(sk);
271 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
07978aab 272 struct ccid2_seq *next;
2a91aa39
AB
273 u64 seq;
274
275 ccid2_hc_tx_check_sanity(hctx);
276
277 BUG_ON(!hctx->ccid2hctx_sendwait);
278 hctx->ccid2hctx_sendwait = 0;
279 hctx->ccid2hctx_pipe++;
280 BUG_ON(hctx->ccid2hctx_pipe < 0);
281
282 /* There is an issue. What if another packet is sent between
283 * packet_send() and packet_sent(). Then the sequence number would be
284 * wrong.
285 * -sorbo.
286 */
287 seq = dp->dccps_gss;
288
289 hctx->ccid2hctx_seqh->ccid2s_seq = seq;
290 hctx->ccid2hctx_seqh->ccid2s_acked = 0;
291 hctx->ccid2hctx_seqh->ccid2s_sent = jiffies;
2a91aa39 292
07978aab
AB
293 next = hctx->ccid2hctx_seqh->ccid2s_next;
294 /* check if we need to alloc more space */
295 if (next == hctx->ccid2hctx_seqt) {
296 int rc;
2a91aa39 297
07978aab
AB
298 ccid2_pr_debug("allocating more space in history\n");
299 rc = ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_KERNEL);
300 BUG_ON(rc); /* XXX what do we do? */
301
302 next = hctx->ccid2hctx_seqh->ccid2s_next;
303 BUG_ON(next == hctx->ccid2hctx_seqt);
2a91aa39 304 }
07978aab
AB
305 hctx->ccid2hctx_seqh = next;
306
307 ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->ccid2hctx_cwnd,
308 hctx->ccid2hctx_pipe);
2a91aa39
AB
309
310 hctx->ccid2hctx_sent++;
311
312 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
313 hctx->ccid2hctx_arsent++;
314 /* We had an ack loss in this window... */
315 if (hctx->ccid2hctx_ackloss) {
316 if (hctx->ccid2hctx_arsent >= hctx->ccid2hctx_cwnd) {
c0c736db
ACM
317 hctx->ccid2hctx_arsent = 0;
318 hctx->ccid2hctx_ackloss = 0;
2a91aa39 319 }
c0c736db
ACM
320 } else {
321 /* No acks lost up to now... */
2a91aa39
AB
322 /* decrease ack ratio if enough packets were sent */
323 if (dp->dccps_l_ack_ratio > 1) {
324 /* XXX don't calculate denominator each time */
c0c736db
ACM
325 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
326 dp->dccps_l_ack_ratio;
2a91aa39 327
2a91aa39
AB
328 denom = hctx->ccid2hctx_cwnd * hctx->ccid2hctx_cwnd / denom;
329
330 if (hctx->ccid2hctx_arsent >= denom) {
331 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
332 hctx->ccid2hctx_arsent = 0;
333 }
c0c736db
ACM
334 } else {
335 /* we can't increase ack ratio further [1] */
2a91aa39
AB
336 hctx->ccid2hctx_arsent = 0; /* or maybe set it to cwnd*/
337 }
338 }
339
340 /* setup RTO timer */
c0c736db 341 if (!timer_pending(&hctx->ccid2hctx_rtotimer))
2a91aa39 342 ccid2_start_rto_timer(sk);
c0c736db 343
8d424f6c 344#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
2a91aa39
AB
345 ccid2_pr_debug("pipe=%d\n", hctx->ccid2hctx_pipe);
346 ccid2_pr_debug("Sent: seq=%llu\n", seq);
347 do {
348 struct ccid2_seq *seqp = hctx->ccid2hctx_seqt;
349
350 while (seqp != hctx->ccid2hctx_seqh) {
351 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
352 seqp->ccid2s_seq, seqp->ccid2s_acked,
353 seqp->ccid2s_sent);
354 seqp = seqp->ccid2s_next;
355 }
c0c736db 356 } while (0);
2a91aa39
AB
357 ccid2_pr_debug("=========\n");
358 ccid2_hc_tx_check_sanity(hctx);
359#endif
360}
361
362/* XXX Lame code duplication!
363 * returns -1 if none was found.
364 * else returns the next offset to use in the function call.
365 */
366static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
367 unsigned char **vec, unsigned char *veclen)
368{
369 const struct dccp_hdr *dh = dccp_hdr(skb);
370 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
371 unsigned char *opt_ptr;
372 const unsigned char *opt_end = (unsigned char *)dh +
373 (dh->dccph_doff * 4);
374 unsigned char opt, len;
375 unsigned char *value;
376
377 BUG_ON(offset < 0);
378 options += offset;
379 opt_ptr = options;
380 if (opt_ptr >= opt_end)
381 return -1;
382
383 while (opt_ptr != opt_end) {
384 opt = *opt_ptr++;
385 len = 0;
386 value = NULL;
387
388 /* Check if this isn't a single byte option */
389 if (opt > DCCPO_MAX_RESERVED) {
390 if (opt_ptr == opt_end)
391 goto out_invalid_option;
392
393 len = *opt_ptr++;
394 if (len < 3)
395 goto out_invalid_option;
396 /*
397 * Remove the type and len fields, leaving
398 * just the value size
399 */
400 len -= 2;
401 value = opt_ptr;
402 opt_ptr += len;
403
404 if (opt_ptr > opt_end)
405 goto out_invalid_option;
406 }
407
408 switch (opt) {
409 case DCCPO_ACK_VECTOR_0:
410 case DCCPO_ACK_VECTOR_1:
411 *vec = value;
412 *veclen = len;
413 return offset + (opt_ptr - options);
2a91aa39
AB
414 }
415 }
416
417 return -1;
418
419out_invalid_option:
420 BUG_ON(1); /* should never happen... options were previously parsed ! */
421 return -1;
422}
423
77ff72d5 424static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
2a91aa39 425{
77ff72d5
AB
426 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
427
428 sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
429 ccid2_pr_debug("deleted RTO timer\n");
2a91aa39
AB
430}
431
432static inline void ccid2_new_ack(struct sock *sk,
433 struct ccid2_seq *seqp,
434 unsigned int *maxincr)
435{
436 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
437
438 /* slow start */
439 if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
440 hctx->ccid2hctx_acks = 0;
441
442 /* We can increase cwnd at most maxincr [ack_ratio/2] */
443 if (*maxincr) {
444 /* increase every 2 acks */
445 hctx->ccid2hctx_ssacks++;
446 if (hctx->ccid2hctx_ssacks == 2) {
447 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
448 hctx->ccid2hctx_ssacks = 0;
449 *maxincr = *maxincr - 1;
450 }
c0c736db
ACM
451 } else {
452 /* increased cwnd enough for this single ack */
2a91aa39
AB
453 hctx->ccid2hctx_ssacks = 0;
454 }
c0c736db 455 } else {
2a91aa39
AB
456 hctx->ccid2hctx_ssacks = 0;
457 hctx->ccid2hctx_acks++;
458
459 if (hctx->ccid2hctx_acks >= hctx->ccid2hctx_cwnd) {
460 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
461 hctx->ccid2hctx_acks = 0;
462 }
463 }
464
465 /* update RTO */
466 if (hctx->ccid2hctx_srtt == -1 ||
29651cda
AB
467 time_after(jiffies, hctx->ccid2hctx_lastrtt + hctx->ccid2hctx_srtt)) {
468 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
2a91aa39
AB
469 int s;
470
471 /* first measurement */
472 if (hctx->ccid2hctx_srtt == -1) {
473 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
474 r, jiffies, seqp->ccid2s_seq);
475 hctx->ccid2hctx_srtt = r;
476 hctx->ccid2hctx_rttvar = r >> 1;
c0c736db 477 } else {
2a91aa39
AB
478 /* RTTVAR */
479 long tmp = hctx->ccid2hctx_srtt - r;
480 if (tmp < 0)
481 tmp *= -1;
482
483 tmp >>= 2;
484 hctx->ccid2hctx_rttvar *= 3;
485 hctx->ccid2hctx_rttvar >>= 2;
486 hctx->ccid2hctx_rttvar += tmp;
487
488 /* SRTT */
489 hctx->ccid2hctx_srtt *= 7;
490 hctx->ccid2hctx_srtt >>= 3;
491 tmp = r >> 3;
492 hctx->ccid2hctx_srtt += tmp;
493 }
494 s = hctx->ccid2hctx_rttvar << 2;
495 /* clock granularity is 1 when based on jiffies */
496 if (!s)
497 s = 1;
498 hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
499
500 /* must be at least a second */
501 s = hctx->ccid2hctx_rto / HZ;
502 /* DCCP doesn't require this [but I like it cuz my code sux] */
503#if 1
504 if (s < 1)
505 hctx->ccid2hctx_rto = HZ;
506#endif
507 /* max 60 seconds */
508 if (s > 60)
509 hctx->ccid2hctx_rto = HZ * 60;
510
511 hctx->ccid2hctx_lastrtt = jiffies;
512
513 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
514 hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
515 hctx->ccid2hctx_rto, HZ, r);
516 hctx->ccid2hctx_sent = 0;
517 }
518
519 /* we got a new ack, so re-start RTO timer */
77ff72d5 520 ccid2_hc_tx_kill_rto_timer(sk);
2a91aa39
AB
521 ccid2_start_rto_timer(sk);
522}
523
77ff72d5 524static void ccid2_hc_tx_dec_pipe(struct sock *sk)
2a91aa39 525{
77ff72d5
AB
526 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
527
2a91aa39
AB
528 hctx->ccid2hctx_pipe--;
529 BUG_ON(hctx->ccid2hctx_pipe < 0);
530
531 if (hctx->ccid2hctx_pipe == 0)
77ff72d5 532 ccid2_hc_tx_kill_rto_timer(sk);
2a91aa39
AB
533}
534
535static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
536{
537 struct dccp_sock *dp = dccp_sk(sk);
538 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
539 u64 ackno, seqno;
540 struct ccid2_seq *seqp;
541 unsigned char *vector;
542 unsigned char veclen;
543 int offset = 0;
544 int done = 0;
545 int loss = 0;
546 unsigned int maxincr = 0;
547
548 ccid2_hc_tx_check_sanity(hctx);
549 /* check reverse path congestion */
550 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
551
552 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
553 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
554 * -sorbo.
555 */
556 /* need to bootstrap */
557 if (hctx->ccid2hctx_rpdupack == -1) {
558 hctx->ccid2hctx_rpdupack = 0;
559 hctx->ccid2hctx_rpseq = seqno;
c0c736db 560 } else {
2a91aa39 561 /* check if packet is consecutive */
c0c736db 562 if ((hctx->ccid2hctx_rpseq + 1) == seqno)
2a91aa39 563 hctx->ccid2hctx_rpseq++;
2a91aa39
AB
564 /* it's a later packet */
565 else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
566 hctx->ccid2hctx_rpdupack++;
567
568 /* check if we got enough dupacks */
569 if (hctx->ccid2hctx_rpdupack >=
570 hctx->ccid2hctx_numdupack) {
2a91aa39
AB
571 hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
572 hctx->ccid2hctx_rpseq = 0;
573
574 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio << 1);
575 }
576 }
577 }
578
579 /* check forward path congestion */
580 /* still didn't send out new data packets */
581 if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
582 return;
583
584 switch (DCCP_SKB_CB(skb)->dccpd_type) {
585 case DCCP_PKT_ACK:
586 case DCCP_PKT_DATAACK:
587 break;
2a91aa39
AB
588 default:
589 return;
590 }
591
592 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
593 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
594
595 /* If in slow-start, cwnd can increase at most Ack Ratio / 2 packets for
596 * this single ack. I round up.
597 * -sorbo.
598 */
599 maxincr = dp->dccps_l_ack_ratio >> 1;
600 maxincr++;
601
602 /* go through all ack vectors */
603 while ((offset = ccid2_ackvector(sk, skb, offset,
604 &vector, &veclen)) != -1) {
605 /* go through this ack vector */
606 while (veclen--) {
607 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
608 u64 ackno_end_rl;
609
610 dccp_set_seqno(&ackno_end_rl, ackno - rl);
611 ccid2_pr_debug("ackvec start:%llu end:%llu\n", ackno,
612 ackno_end_rl);
613 /* if the seqno we are analyzing is larger than the
614 * current ackno, then move towards the tail of our
615 * seqnos.
616 */
617 while (after48(seqp->ccid2s_seq, ackno)) {
618 if (seqp == hctx->ccid2hctx_seqt) {
619 done = 1;
620 break;
621 }
622 seqp = seqp->ccid2s_prev;
623 }
624 if (done)
625 break;
626
627 /* check all seqnos in the range of the vector
628 * run length
629 */
630 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
8e27e465
AB
631 const u8 state = *vector &
632 DCCP_ACKVEC_STATE_MASK;
2a91aa39
AB
633
634 /* new packet received or marked */
635 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
636 !seqp->ccid2s_acked) {
637 if (state ==
638 DCCP_ACKVEC_STATE_ECN_MARKED) {
639 loss = 1;
c0c736db 640 } else
2a91aa39
AB
641 ccid2_new_ack(sk, seqp,
642 &maxincr);
2a91aa39
AB
643
644 seqp->ccid2s_acked = 1;
645 ccid2_pr_debug("Got ack for %llu\n",
646 seqp->ccid2s_seq);
77ff72d5 647 ccid2_hc_tx_dec_pipe(sk);
2a91aa39
AB
648 }
649 if (seqp == hctx->ccid2hctx_seqt) {
650 done = 1;
651 break;
652 }
653 seqp = seqp->ccid2s_next;
654 }
655 if (done)
656 break;
657
658
659 dccp_set_seqno(&ackno, ackno_end_rl - 1);
660 vector++;
661 }
662 if (done)
663 break;
664 }
665
666 /* The state about what is acked should be correct now
667 * Check for NUMDUPACK
668 */
669 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
670 done = 0;
671 while (1) {
672 if (seqp->ccid2s_acked) {
673 done++;
c0c736db 674 if (done == hctx->ccid2hctx_numdupack)
2a91aa39 675 break;
2a91aa39 676 }
c0c736db 677 if (seqp == hctx->ccid2hctx_seqt)
2a91aa39 678 break;
2a91aa39
AB
679 seqp = seqp->ccid2s_prev;
680 }
681
682 /* If there are at least 3 acknowledgements, anything unacknowledged
683 * below the last sequence number is considered lost
684 */
685 if (done == hctx->ccid2hctx_numdupack) {
686 struct ccid2_seq *last_acked = seqp;
687
688 /* check for lost packets */
689 while (1) {
690 if (!seqp->ccid2s_acked) {
691 loss = 1;
77ff72d5 692 ccid2_hc_tx_dec_pipe(sk);
2a91aa39
AB
693 }
694 if (seqp == hctx->ccid2hctx_seqt)
695 break;
696 seqp = seqp->ccid2s_prev;
697 }
698
699 hctx->ccid2hctx_seqt = last_acked;
700 }
701
702 /* trim acked packets in tail */
703 while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
704 if (!hctx->ccid2hctx_seqt->ccid2s_acked)
705 break;
706
707 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
708 }
709
710 if (loss) {
711 /* XXX do bit shifts guarantee a 0 as the new bit? */
712 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd >> 1);
713 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd;
714 if (hctx->ccid2hctx_ssthresh < 2)
715 hctx->ccid2hctx_ssthresh = 2;
716 }
717
718 ccid2_hc_tx_check_sanity(hctx);
719}
720
91f0ebf7 721static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
2a91aa39 722{
91f0ebf7 723 struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
2a91aa39 724
2a91aa39 725 hctx->ccid2hctx_cwnd = 1;
d458c25c
AB
726 /* Initialize ssthresh to infinity. This means that we will exit the
727 * initial slow-start after the first packet loss. This is what we
728 * want.
729 */
730 hctx->ccid2hctx_ssthresh = ~0;
2a91aa39 731 hctx->ccid2hctx_numdupack = 3;
07978aab 732 hctx->ccid2hctx_seqbufc = 0;
2a91aa39
AB
733
734 /* XXX init ~ to window size... */
07978aab 735 if (ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_ATOMIC) != 0)
2a91aa39 736 return -ENOMEM;
91f0ebf7 737
2a91aa39
AB
738 hctx->ccid2hctx_sent = 0;
739 hctx->ccid2hctx_rto = 3 * HZ;
740 hctx->ccid2hctx_srtt = -1;
741 hctx->ccid2hctx_rttvar = -1;
742 hctx->ccid2hctx_lastrtt = 0;
743 hctx->ccid2hctx_rpdupack = -1;
744
745 hctx->ccid2hctx_rtotimer.function = &ccid2_hc_tx_rto_expire;
746 hctx->ccid2hctx_rtotimer.data = (unsigned long)sk;
747 init_timer(&hctx->ccid2hctx_rtotimer);
748
749 ccid2_hc_tx_check_sanity(hctx);
750 return 0;
751}
752
753static void ccid2_hc_tx_exit(struct sock *sk)
754{
77ff72d5 755 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
07978aab 756 int i;
2a91aa39 757
77ff72d5 758 ccid2_hc_tx_kill_rto_timer(sk);
07978aab
AB
759
760 for (i = 0; i < hctx->ccid2hctx_seqbufc; i++)
761 kfree(hctx->ccid2hctx_seqbuf[i]);
762 hctx->ccid2hctx_seqbufc = 0;
2a91aa39
AB
763}
764
765static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
766{
767 const struct dccp_sock *dp = dccp_sk(sk);
768 struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
769
770 switch (DCCP_SKB_CB(skb)->dccpd_type) {
771 case DCCP_PKT_DATA:
772 case DCCP_PKT_DATAACK:
773 hcrx->ccid2hcrx_data++;
774 if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
775 dccp_send_ack(sk);
776 hcrx->ccid2hcrx_data = 0;
777 }
778 break;
779 }
780}
781
91f0ebf7 782static struct ccid_operations ccid2 = {
2a91aa39
AB
783 .ccid_id = 2,
784 .ccid_name = "ccid2",
785 .ccid_owner = THIS_MODULE,
91f0ebf7 786 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
2a91aa39
AB
787 .ccid_hc_tx_init = ccid2_hc_tx_init,
788 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
789 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
790 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
791 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
91f0ebf7 792 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
2a91aa39
AB
793 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
794};
795
796module_param(ccid2_debug, int, 0444);
797MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
798
799static __init int ccid2_module_init(void)
800{
801 return ccid_register(&ccid2);
802}
803module_init(ccid2_module_init);
804
805static __exit void ccid2_module_exit(void)
806{
807 ccid_unregister(&ccid2);
808}
809module_exit(ccid2_module_exit);
810
811MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
c0c736db 812MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
2a91aa39
AB
813MODULE_LICENSE("GPL");
814MODULE_ALIAS("net-dccp-ccid-2");