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