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