ccid2.c 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
 *
 *  Changes to meet Linux coding standards, and DCCP infrastructure fixes.
 *
 *  Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
24
 * This implementation should follow RFC 4341
25
 */
26
#include <linux/slab.h>
27
#include "../feat.h"
28 29 30 31 32
#include "../ccid.h"
#include "../dccp.h"
#include "ccid2.h"


33
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
34 35
static int ccid2_debug;
#define ccid2_pr_debug(format, a...)	DCCP_PR_DEBUG(ccid2_debug, format, ##a)
36

37
static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hc)
38 39 40
{
	int len = 0;
	int pipe = 0;
41
	struct ccid2_seq *seqp = hc->tx_seqh;
42 43

	/* there is data in the chain */
44
	if (seqp != hc->tx_seqt) {
45 46 47 48 49
		seqp = seqp->ccid2s_prev;
		len++;
		if (!seqp->ccid2s_acked)
			pipe++;

50
		while (seqp != hc->tx_seqt) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
			struct ccid2_seq *prev = seqp->ccid2s_prev;

			len++;
			if (!prev->ccid2s_acked)
				pipe++;

			/* packets are sent sequentially */
			BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
						prev->ccid2s_seq ) >= 0);
			BUG_ON(time_before(seqp->ccid2s_sent,
					   prev->ccid2s_sent));

			seqp = prev;
		}
	}

67
	BUG_ON(pipe != hc->tx_pipe);
68 69 70 71 72
	ccid2_pr_debug("len of chain=%d\n", len);

	do {
		seqp = seqp->ccid2s_prev;
		len++;
73
	} while (seqp != hc->tx_seqh);
74 75

	ccid2_pr_debug("total len=%d\n", len);
76
	BUG_ON(len != hc->tx_seqbufc * CCID2_SEQBUF_LEN);
77
}
78
#else
79
#define ccid2_pr_debug(format, a...)
80
#define ccid2_hc_tx_check_sanity(hc)
81 82
#endif

83
static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
84 85 86 87 88
{
	struct ccid2_seq *seqp;
	int i;

	/* check if we have space to preserve the pointer to the buffer */
89 90
	if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
			       sizeof(struct ccid2_seq *)))
91 92 93
		return -ENOMEM;

	/* allocate buffer and initialize linked list */
G
Gerrit Renker 已提交
94
	seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
95 96 97
	if (seqp == NULL)
		return -ENOMEM;

G
Gerrit Renker 已提交
98
	for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
99 100 101
		seqp[i].ccid2s_next = &seqp[i + 1];
		seqp[i + 1].ccid2s_prev = &seqp[i];
	}
G
Gerrit Renker 已提交
102 103
	seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
	seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
104 105

	/* This is the first allocation.  Initiate the head and tail.  */
106 107
	if (hc->tx_seqbufc == 0)
		hc->tx_seqh = hc->tx_seqt = seqp;
108 109
	else {
		/* link the existing list with the one we just created */
110 111
		hc->tx_seqh->ccid2s_next = seqp;
		seqp->ccid2s_prev = hc->tx_seqh;
112

113 114
		hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
		seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
115 116 117
	}

	/* store the original pointer to the buffer so we can free it */
118 119
	hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
	hc->tx_seqbufc++;
120 121 122 123

	return 0;
}

124
static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
125
{
126
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
127

128
	if (hc->tx_pipe < hc->tx_cwnd)
129 130 131
		return 0;

	return 1; /* XXX CCID should dequeue when ready instead of polling */
132 133
}

134
static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
135 136
{
	struct dccp_sock *dp = dccp_sk(sk);
137
	u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
138

139
	/*
140 141 142 143
	 * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
	 * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
	 * acceptable since this causes starvation/deadlock whenever cwnd < 2.
	 * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
144
	 */
145 146 147
	if (val == 0 || val > max_ratio) {
		DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
		val = max_ratio;
148
	}
149 150
	if (val > DCCPF_ACK_RATIO_MAX)
		val = DCCPF_ACK_RATIO_MAX;
151

152 153 154
	if (val == dp->dccps_l_ack_ratio)
		return;

155
	ccid2_pr_debug("changing local ack ratio to %u\n", val);
156 157 158
	dp->dccps_l_ack_ratio = val;
}

159
static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hc, long val)
160 161
{
	ccid2_pr_debug("change SRTT to %ld\n", val);
162
	hc->tx_srtt = val;
163 164 165 166
}

static void ccid2_start_rto_timer(struct sock *sk);

167 168 169
static void ccid2_hc_tx_rto_expire(unsigned long data)
{
	struct sock *sk = (struct sock *)data;
170
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
171
	long s;
172 173 174

	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
175
		sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
176 177 178 179 180
		goto out;
	}

	ccid2_pr_debug("RTO_EXPIRE\n");

181
	ccid2_hc_tx_check_sanity(hc);
182

183
	/* back-off timer */
184
	hc->tx_rto <<= 1;
185

186
	s = hc->tx_rto / HZ;
187
	if (s > 60)
188
		hc->tx_rto = 60 * HZ;
189 190

	ccid2_start_rto_timer(sk);
191 192

	/* adjust pipe, cwnd etc */
193 194 195 196 197
	hc->tx_ssthresh = hc->tx_cwnd / 2;
	if (hc->tx_ssthresh < 2)
		hc->tx_ssthresh = 2;
	hc->tx_cwnd	 = 1;
	hc->tx_pipe	 = 0;
198 199

	/* clear state about stuff we sent */
200 201
	hc->tx_seqt = hc->tx_seqh;
	hc->tx_packets_acked = 0;
202 203

	/* clear ack ratio state. */
204 205
	hc->tx_rpseq    = 0;
	hc->tx_rpdupack = -1;
206
	ccid2_change_l_ack_ratio(sk, 1);
207
	ccid2_hc_tx_check_sanity(hc);
208 209
out:
	bh_unlock_sock(sk);
210
	sock_put(sk);
211 212
}

213 214
static void ccid2_start_rto_timer(struct sock *sk)
{
215
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
216

217
	ccid2_pr_debug("setting RTO timeout=%ld\n", hc->tx_rto);
218

219 220
	BUG_ON(timer_pending(&hc->tx_rtotimer));
	sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
221 222 223
}

static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
224 225
{
	struct dccp_sock *dp = dccp_sk(sk);
226
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
227
	struct ccid2_seq *next;
228

229
	hc->tx_pipe++;
230

231 232 233
	hc->tx_seqh->ccid2s_seq   = dp->dccps_gss;
	hc->tx_seqh->ccid2s_acked = 0;
	hc->tx_seqh->ccid2s_sent  = jiffies;
234

235
	next = hc->tx_seqh->ccid2s_next;
236
	/* check if we need to alloc more space */
237 238
	if (next == hc->tx_seqt) {
		if (ccid2_hc_tx_alloc_seq(hc)) {
G
Gerrit Renker 已提交
239 240 241 242
			DCCP_CRIT("packet history - out of memory!");
			/* FIXME: find a more graceful way to bail out */
			return;
		}
243 244
		next = hc->tx_seqh->ccid2s_next;
		BUG_ON(next == hc->tx_seqt);
245
	}
246
	hc->tx_seqh = next;
247

248
	ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	/*
	 * FIXME: The code below is broken and the variables have been removed
	 * from the socket struct. The `ackloss' variable was always set to 0,
	 * and with arsent there are several problems:
	 *  (i) it doesn't just count the number of Acks, but all sent packets;
	 *  (ii) it is expressed in # of packets, not # of windows, so the
	 *  comparison below uses the wrong formula: Appendix A of RFC 4341
	 *  comes up with the number K = cwnd / (R^2 - R) of consecutive windows
	 *  of data with no lost or marked Ack packets. If arsent were the # of
	 *  consecutive Acks received without loss, then Ack Ratio needs to be
	 *  decreased by 1 when
	 *	      arsent >=  K * cwnd / R  =  cwnd^2 / (R^3 - R^2)
	 *  where cwnd / R is the number of Acks received per window of data
	 *  (cf. RFC 4341, App. A). The problems are that
	 *  - arsent counts other packets as well;
	 *  - the comparison uses a formula different from RFC 4341;
	 *  - computing a cubic/quadratic equation each time is too complicated.
	 *  Hence a different algorithm is needed.
	 */
#if 0
270
	/* Ack Ratio.  Need to maintain a concept of how many windows we sent */
271
	hc->tx_arsent++;
272
	/* We had an ack loss in this window... */
273 274 275 276
	if (hc->tx_ackloss) {
		if (hc->tx_arsent >= hc->tx_cwnd) {
			hc->tx_arsent  = 0;
			hc->tx_ackloss = 0;
277
		}
278 279
	} else {
		/* No acks lost up to now... */
280 281 282
		/* decrease ack ratio if enough packets were sent */
		if (dp->dccps_l_ack_ratio > 1) {
			/* XXX don't calculate denominator each time */
283 284
			int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
				    dp->dccps_l_ack_ratio;
285

286
			denom = hc->tx_cwnd * hc->tx_cwnd / denom;
287

288
			if (hc->tx_arsent >= denom) {
289
				ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
290
				hc->tx_arsent = 0;
291
			}
292 293
		} else {
			/* we can't increase ack ratio further [1] */
294
			hc->tx_arsent = 0; /* or maybe set it to cwnd*/
295 296
		}
	}
297
#endif
298 299

	/* setup RTO timer */
300
	if (!timer_pending(&hc->tx_rtotimer))
301
		ccid2_start_rto_timer(sk);
302

303
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
304
	do {
305
		struct ccid2_seq *seqp = hc->tx_seqt;
306

307
		while (seqp != hc->tx_seqh) {
308
			ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
309
				       (unsigned long long)seqp->ccid2s_seq,
R
Randy Dunlap 已提交
310
				       seqp->ccid2s_acked, seqp->ccid2s_sent);
311 312
			seqp = seqp->ccid2s_next;
		}
313
	} while (0);
314
	ccid2_pr_debug("=========\n");
315
	ccid2_hc_tx_check_sanity(hc);
316 317 318
#endif
}

319 320 321
/* XXX Lame code duplication!
 * returns -1 if none was found.
 * else returns the next offset to use in the function call.
322
 */
323 324
static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
			   unsigned char **vec, unsigned char *veclen)
325
{
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	const struct dccp_hdr *dh = dccp_hdr(skb);
	unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
	unsigned char *opt_ptr;
	const unsigned char *opt_end = (unsigned char *)dh +
					(dh->dccph_doff * 4);
	unsigned char opt, len;
	unsigned char *value;

	BUG_ON(offset < 0);
	options += offset;
	opt_ptr = options;
	if (opt_ptr >= opt_end)
		return -1;

	while (opt_ptr != opt_end) {
		opt   = *opt_ptr++;
		len   = 0;
		value = NULL;

		/* Check if this isn't a single byte option */
		if (opt > DCCPO_MAX_RESERVED) {
			if (opt_ptr == opt_end)
				goto out_invalid_option;

			len = *opt_ptr++;
			if (len < 3)
				goto out_invalid_option;
353
			/*
354 355
			 * Remove the type and len fields, leaving
			 * just the value size
356
			 */
357 358 359
			len     -= 2;
			value   = opt_ptr;
			opt_ptr += len;
360

361 362
			if (opt_ptr > opt_end)
				goto out_invalid_option;
363 364
		}

365 366 367 368 369 370
		switch (opt) {
		case DCCPO_ACK_VECTOR_0:
		case DCCPO_ACK_VECTOR_1:
			*vec	= value;
			*veclen = len;
			return offset + (opt_ptr - options);
371 372 373
		}
	}

374
	return -1;
375

376 377 378
out_invalid_option:
	DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
	return -1;
379 380
}

381
static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
382
{
383
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
384

385
	sk_stop_timer(sk, &hc->tx_rtotimer);
386
	ccid2_pr_debug("deleted RTO timer\n");
387 388
}

389 390 391
static inline void ccid2_new_ack(struct sock *sk,
				 struct ccid2_seq *seqp,
				 unsigned int *maxincr)
392
{
393
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
394

395 396 397 398 399
	if (hc->tx_cwnd < hc->tx_ssthresh) {
		if (*maxincr > 0 && ++hc->tx_packets_acked == 2) {
			hc->tx_cwnd += 1;
			*maxincr    -= 1;
			hc->tx_packets_acked = 0;
400
		}
401 402 403
	} else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
			hc->tx_cwnd += 1;
			hc->tx_packets_acked = 0;
404 405
	}

406
	/* update RTO */
407 408
	if (hc->tx_srtt == -1 ||
	    time_after(jiffies, hc->tx_lastrtt + hc->tx_srtt)) {
409 410 411 412
		unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
		int s;

		/* first measurement */
413
		if (hc->tx_srtt == -1) {
414 415 416
			ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
				       r, jiffies,
				       (unsigned long long)seqp->ccid2s_seq);
417 418
			ccid2_change_srtt(hc, r);
			hc->tx_rttvar = r >> 1;
419 420
		} else {
			/* RTTVAR */
421
			long tmp = hc->tx_srtt - r;
422 423 424 425 426 427
			long srtt;

			if (tmp < 0)
				tmp *= -1;

			tmp >>= 2;
428 429 430
			hc->tx_rttvar *= 3;
			hc->tx_rttvar >>= 2;
			hc->tx_rttvar += tmp;
431 432

			/* SRTT */
433
			srtt = hc->tx_srtt;
434 435 436 437
			srtt *= 7;
			srtt >>= 3;
			tmp = r >> 3;
			srtt += tmp;
438
			ccid2_change_srtt(hc, srtt);
439
		}
440
		s = hc->tx_rttvar << 2;
441 442 443
		/* clock granularity is 1 when based on jiffies */
		if (!s)
			s = 1;
444
		hc->tx_rto = hc->tx_srtt + s;
445 446

		/* must be at least a second */
447
		s = hc->tx_rto / HZ;
448 449 450
		/* DCCP doesn't require this [but I like it cuz my code sux] */
#if 1
		if (s < 1)
451
			hc->tx_rto = HZ;
452 453 454
#endif
		/* max 60 seconds */
		if (s > 60)
455
			hc->tx_rto = HZ * 60;
456

457
		hc->tx_lastrtt = jiffies;
458

459
		ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
460 461
			       hc->tx_srtt, hc->tx_rttvar,
			       hc->tx_rto, HZ, r);
462 463 464 465 466
	}

	/* we got a new ack, so re-start RTO timer */
	ccid2_hc_tx_kill_rto_timer(sk);
	ccid2_start_rto_timer(sk);
467 468
}

469
static void ccid2_hc_tx_dec_pipe(struct sock *sk)
470
{
471
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
472

473
	if (hc->tx_pipe == 0)
474 475
		DCCP_BUG("pipe == 0");
	else
476
		hc->tx_pipe--;
477

478
	if (hc->tx_pipe == 0)
479 480 481 482 483
		ccid2_hc_tx_kill_rto_timer(sk);
}

static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
{
484
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
485

486
	if (time_before(seqp->ccid2s_sent, hc->tx_last_cong)) {
487 488
		ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
		return;
489
	}
490

491
	hc->tx_last_cong = jiffies;
492

493 494
	hc->tx_cwnd      = hc->tx_cwnd / 2 ? : 1U;
	hc->tx_ssthresh  = max(hc->tx_cwnd, 2U);
495 496

	/* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
497 498
	if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd)
		ccid2_change_l_ack_ratio(sk, hc->tx_cwnd);
499 500
}

501 502 503
static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
{
	struct dccp_sock *dp = dccp_sk(sk);
504
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
505 506
	u64 ackno, seqno;
	struct ccid2_seq *seqp;
507 508 509
	unsigned char *vector;
	unsigned char veclen;
	int offset = 0;
510 511 512
	int done = 0;
	unsigned int maxincr = 0;

513
	ccid2_hc_tx_check_sanity(hc);
514 515 516 517 518 519 520 521
	/* check reverse path congestion */
	seqno = DCCP_SKB_CB(skb)->dccpd_seq;

	/* XXX this whole "algorithm" is broken.  Need to fix it to keep track
	 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
	 * -sorbo.
	 */
	/* need to bootstrap */
522 523 524
	if (hc->tx_rpdupack == -1) {
		hc->tx_rpdupack = 0;
		hc->tx_rpseq    = seqno;
525
	} else {
526
		/* check if packet is consecutive */
527 528
		if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
			hc->tx_rpseq = seqno;
529
		/* it's a later packet */
530 531
		else if (after48(seqno, hc->tx_rpseq)) {
			hc->tx_rpdupack++;
532 533

			/* check if we got enough dupacks */
534 535 536
			if (hc->tx_rpdupack >= NUMDUPACK) {
				hc->tx_rpdupack = -1; /* XXX lame */
				hc->tx_rpseq    = 0;
537

538
				ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
539 540 541 542 543
			}
		}
	}

	/* check forward path congestion */
544
	/* still didn't send out new data packets */
545
	if (hc->tx_seqh == hc->tx_seqt)
546 547
		return;

548 549 550 551 552 553 554
	switch (DCCP_SKB_CB(skb)->dccpd_type) {
	case DCCP_PKT_ACK:
	case DCCP_PKT_DATAACK:
		break;
	default:
		return;
	}
555 556

	ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
557 558
	if (after48(ackno, hc->tx_high_ack))
		hc->tx_high_ack = ackno;
A
Andrea Bittau 已提交
559

560
	seqp = hc->tx_seqt;
A
Andrea Bittau 已提交
561 562
	while (before48(seqp->ccid2s_seq, ackno)) {
		seqp = seqp->ccid2s_next;
563 564
		if (seqp == hc->tx_seqh) {
			seqp = hc->tx_seqh->ccid2s_prev;
A
Andrea Bittau 已提交
565 566 567
			break;
		}
	}
568

569 570 571 572
	/*
	 * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
	 * packets per acknowledgement. Rounding up avoids that cwnd is not
	 * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
573
	 */
574
	if (hc->tx_cwnd < hc->tx_ssthresh)
575
		maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
576 577

	/* go through all ack vectors */
578 579
	while ((offset = ccid2_ackvector(sk, skb, offset,
					 &vector, &veclen)) != -1) {
580
		/* go through this ack vector */
581 582 583
		while (veclen--) {
			const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
			u64 ackno_end_rl = SUB48(ackno, rl);
584

585
			ccid2_pr_debug("ackvec start:%llu end:%llu\n",
R
Randy Dunlap 已提交
586
				       (unsigned long long)ackno,
587
				       (unsigned long long)ackno_end_rl);
588 589 590 591 592
			/* if the seqno we are analyzing is larger than the
			 * current ackno, then move towards the tail of our
			 * seqnos.
			 */
			while (after48(seqp->ccid2s_seq, ackno)) {
593
				if (seqp == hc->tx_seqt) {
594 595 596 597 598 599 600 601 602 603 604 605
					done = 1;
					break;
				}
				seqp = seqp->ccid2s_prev;
			}
			if (done)
				break;

			/* check all seqnos in the range of the vector
			 * run length
			 */
			while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
606 607
				const u8 state = *vector &
						 DCCP_ACKVEC_STATE_MASK;
608 609

				/* new packet received or marked */
610
				if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
611
				    !seqp->ccid2s_acked) {
612 613
					if (state ==
					    DCCP_ACKVEC_STATE_ECN_MARKED) {
614
						ccid2_congestion_event(sk,
615
								       seqp);
616
					} else
617 618 619 620 621
						ccid2_new_ack(sk, seqp,
							      &maxincr);

					seqp->ccid2s_acked = 1;
					ccid2_pr_debug("Got ack for %llu\n",
R
Randy Dunlap 已提交
622
						       (unsigned long long)seqp->ccid2s_seq);
623
					ccid2_hc_tx_dec_pipe(sk);
624
				}
625
				if (seqp == hc->tx_seqt) {
626 627 628
					done = 1;
					break;
				}
629
				seqp = seqp->ccid2s_prev;
630 631 632 633
			}
			if (done)
				break;

634
			ackno = SUB48(ackno_end_rl, 1);
635
			vector++;
636 637 638 639 640 641 642 643
		}
		if (done)
			break;
	}

	/* The state about what is acked should be correct now
	 * Check for NUMDUPACK
	 */
644 645
	seqp = hc->tx_seqt;
	while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
A
Andrea Bittau 已提交
646
		seqp = seqp->ccid2s_next;
647 648
		if (seqp == hc->tx_seqh) {
			seqp = hc->tx_seqh->ccid2s_prev;
A
Andrea Bittau 已提交
649 650 651
			break;
		}
	}
652 653 654 655
	done = 0;
	while (1) {
		if (seqp->ccid2s_acked) {
			done++;
656
			if (done == NUMDUPACK)
657 658
				break;
		}
659
		if (seqp == hc->tx_seqt)
660 661 662 663 664 665 666
			break;
		seqp = seqp->ccid2s_prev;
	}

	/* If there are at least 3 acknowledgements, anything unacknowledged
	 * below the last sequence number is considered lost
	 */
667
	if (done == NUMDUPACK) {
668 669 670 671 672
		struct ccid2_seq *last_acked = seqp;

		/* check for lost packets */
		while (1) {
			if (!seqp->ccid2s_acked) {
673
				ccid2_pr_debug("Packet lost: %llu\n",
R
Randy Dunlap 已提交
674
					       (unsigned long long)seqp->ccid2s_seq);
675 676 677 678
				/* XXX need to traverse from tail -> head in
				 * order to detect multiple congestion events in
				 * one ack vector.
				 */
679
				ccid2_congestion_event(sk, seqp);
680
				ccid2_hc_tx_dec_pipe(sk);
681
			}
682
			if (seqp == hc->tx_seqt)
683 684 685 686
				break;
			seqp = seqp->ccid2s_prev;
		}

687
		hc->tx_seqt = last_acked;
688 689 690
	}

	/* trim acked packets in tail */
691 692
	while (hc->tx_seqt != hc->tx_seqh) {
		if (!hc->tx_seqt->ccid2s_acked)
693 694
			break;

695
		hc->tx_seqt = hc->tx_seqt->ccid2s_next;
696 697
	}

698
	ccid2_hc_tx_check_sanity(hc);
699 700
}

701
static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
702
{
703
	struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
704 705
	struct dccp_sock *dp = dccp_sk(sk);
	u32 max_ratio;
706

707
	/* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
708
	hc->tx_ssthresh = ~0U;
709

710 711 712 713 714
	/*
	 * RFC 4341, 5: "The cwnd parameter is initialized to at most four
	 * packets for new connections, following the rules from [RFC3390]".
	 * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
	 */
715
	hc->tx_cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
716 717

	/* Make sure that Ack Ratio is enabled and within bounds. */
718
	max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
719 720 721
	if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
		dp->dccps_l_ack_ratio = max_ratio;

722
	/* XXX init ~ to window size... */
723
	if (ccid2_hc_tx_alloc_seq(hc))
724
		return -ENOMEM;
725

726 727 728 729 730 731
	hc->tx_rto	 = 3 * HZ;
	ccid2_change_srtt(hc, -1);
	hc->tx_rttvar    = -1;
	hc->tx_rpdupack  = -1;
	hc->tx_last_cong = jiffies;
	setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire,
732 733
			(unsigned long)sk);

734
	ccid2_hc_tx_check_sanity(hc);
735 736 737 738 739
	return 0;
}

static void ccid2_hc_tx_exit(struct sock *sk)
{
740
	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
741
	int i;
742

743
	ccid2_hc_tx_kill_rto_timer(sk);
744

745 746 747
	for (i = 0; i < hc->tx_seqbufc; i++)
		kfree(hc->tx_seqbuf[i]);
	hc->tx_seqbufc = 0;
748 749 750 751 752
}

static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
{
	const struct dccp_sock *dp = dccp_sk(sk);
753
	struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
754 755 756 757

	switch (DCCP_SKB_CB(skb)->dccpd_type) {
	case DCCP_PKT_DATA:
	case DCCP_PKT_DATAACK:
758 759
		hc->rx_data++;
		if (hc->rx_data >= dp->dccps_r_ack_ratio) {
760
			dccp_send_ack(sk);
761
			hc->rx_data = 0;
762 763 764 765 766
		}
		break;
	}
}

767
struct ccid_operations ccid2_ops = {
768 769 770 771 772 773 774 775 776 777
	.ccid_id		= DCCPC_CCID2,
	.ccid_name		= "TCP-like",
	.ccid_hc_tx_obj_size	= sizeof(struct ccid2_hc_tx_sock),
	.ccid_hc_tx_init	= ccid2_hc_tx_init,
	.ccid_hc_tx_exit	= ccid2_hc_tx_exit,
	.ccid_hc_tx_send_packet	= ccid2_hc_tx_send_packet,
	.ccid_hc_tx_packet_sent	= ccid2_hc_tx_packet_sent,
	.ccid_hc_tx_packet_recv	= ccid2_hc_tx_packet_recv,
	.ccid_hc_rx_obj_size	= sizeof(struct ccid2_hc_rx_sock),
	.ccid_hc_rx_packet_recv	= ccid2_hc_rx_packet_recv,
778 779
};

780
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
781
module_param(ccid2_debug, bool, 0644);
782
MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
783
#endif