ccid2.c 20.1 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 24 25
/*
 *  net/dccp/ccids/ccid2.c
 *
 *  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.
 */

/*
26
 * This implementation should follow RFC 4341
27
 */
28
#include "../feat.h"
29 30 31 32 33
#include "../ccid.h"
#include "../dccp.h"
#include "ccid2.h"


34
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
35 36
static int ccid2_debug;
#define ccid2_pr_debug(format, a...)	DCCP_PR_DEBUG(ccid2_debug, format, ##a)
37 38 39 40 41

static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
{
	int len = 0;
	int pipe = 0;
42
	struct ccid2_seq *seqp = hctx->seqh;
43 44

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

51
		while (seqp != hctx->seqt) {
52
			struct ccid2_seq *prev = seqp->ccid2s_prev;
53 54 55 56 57 58

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

			/* packets are sent sequentially */
59 60
			BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
						prev->ccid2s_seq ) >= 0);
61 62
			BUG_ON(time_before(seqp->ccid2s_sent,
					   prev->ccid2s_sent));
63 64 65 66 67

			seqp = prev;
		}
	}

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

	do {
		seqp = seqp->ccid2s_prev;
		len++;
74
	} while (seqp != hctx->seqh);
75 76

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

G
Gerrit Renker 已提交
84
static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx)
85 86 87 88 89
{
	struct ccid2_seq *seqp;
	int i;

	/* check if we have space to preserve the pointer to the buffer */
90
	if (hctx->seqbufc >= sizeof(hctx->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 (hctx->seqbufc == 0)
		hctx->seqh = hctx->seqt = seqp;
108 109
	else {
		/* link the existing list with the one we just created */
110 111
		hctx->seqh->ccid2s_next = seqp;
		seqp->ccid2s_prev = hctx->seqh;
112

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

	/* store the original pointer to the buffer so we can free it */
118 119
	hctx->seqbuf[hctx->seqbufc] = seqp;
	hctx->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 *hctx = ccid2_hc_tx_sk(sk);
127

128
	if (hctx->pipe < hctx->cwnd)
129
		return 0;
130

131
	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)->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 160 161
static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val)
{
	ccid2_pr_debug("change SRTT to %ld\n", val);
162
	hctx->srtt = val;
163 164
}

165 166 167 168 169 170 171 172 173 174
static void ccid2_start_rto_timer(struct sock *sk);

static void ccid2_hc_tx_rto_expire(unsigned long data)
{
	struct sock *sk = (struct sock *)data;
	struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
	long s;

	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
175
		sk_reset_timer(sk, &hctx->rtotimer, jiffies + HZ / 5);
176 177 178 179 180 181 182 183
		goto out;
	}

	ccid2_pr_debug("RTO_EXPIRE\n");

	ccid2_hc_tx_check_sanity(hctx);

	/* back-off timer */
184
	hctx->rto <<= 1;
185

186
	s = hctx->rto / HZ;
187
	if (s > 60)
188
		hctx->rto = 60 * HZ;
189 190 191 192

	ccid2_start_rto_timer(sk);

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

	/* clear state about stuff we sent */
200 201
	hctx->seqt = hctx->seqh;
	hctx->packets_acked = 0;
202 203

	/* clear ack ratio state. */
204 205
	hctx->rpseq    = 0;
	hctx->rpdupack = -1;
206 207 208 209
	ccid2_change_l_ack_ratio(sk, 1);
	ccid2_hc_tx_check_sanity(hctx);
out:
	bh_unlock_sock(sk);
210
	sock_put(sk);
211 212 213 214 215 216
}

static void ccid2_start_rto_timer(struct sock *sk)
{
	struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);

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

219 220 221
	BUG_ON(timer_pending(&hctx->rtotimer));
	sk_reset_timer(sk, &hctx->rtotimer,
		       jiffies + hctx->rto);
222 223
}

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

230
	hctx->pipe++;
231

232 233 234
	hctx->seqh->ccid2s_seq   = dp->dccps_gss;
	hctx->seqh->ccid2s_acked = 0;
	hctx->seqh->ccid2s_sent  = jiffies;
235

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

249
	ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->cwnd, hctx->pipe);
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	/*
	 * 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
271
	/* Ack Ratio.  Need to maintain a concept of how many windows we sent */
272
	hctx->arsent++;
273
	/* We had an ack loss in this window... */
274 275 276 277
	if (hctx->ackloss) {
		if (hctx->arsent >= hctx->cwnd) {
			hctx->arsent  = 0;
			hctx->ackloss = 0;
278
		}
279 280
	} else {
		/* No acks lost up to now... */
281 282 283
		/* decrease ack ratio if enough packets were sent */
		if (dp->dccps_l_ack_ratio > 1) {
			/* XXX don't calculate denominator each time */
284 285
			int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
				    dp->dccps_l_ack_ratio;
286

287
			denom = hctx->cwnd * hctx->cwnd / denom;
288

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

	/* setup RTO timer */
301
	if (!timer_pending(&hctx->rtotimer))
302
		ccid2_start_rto_timer(sk);
303

304
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
305
	do {
306
		struct ccid2_seq *seqp = hctx->seqt;
307

308
		while (seqp != hctx->seqh) {
309
			ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
310
				       (unsigned long long)seqp->ccid2s_seq,
R
Randy Dunlap 已提交
311
				       seqp->ccid2s_acked, seqp->ccid2s_sent);
312 313
			seqp = seqp->ccid2s_next;
		}
314
	} while (0);
315 316 317 318 319 320 321 322 323 324 325 326
	ccid2_pr_debug("=========\n");
	ccid2_hc_tx_check_sanity(hctx);
#endif
}

/* XXX Lame code duplication!
 * returns -1 if none was found.
 * else returns the next offset to use in the function call.
 */
static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
			   unsigned char **vec, unsigned char *veclen)
{
327 328 329 330 331 332 333
	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;
334 335 336 337 338 339 340 341

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

	while (opt_ptr != opt_end) {
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
		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;
			/*
			 * Remove the type and len fields, leaving
			 * just the value size
			 */
			len     -= 2;
			value   = opt_ptr;
			opt_ptr += len;

			if (opt_ptr > opt_end)
				goto out_invalid_option;
		}
365 366 367 368 369 370 371 372 373 374 375 376 377

		switch (opt) {
		case DCCPO_ACK_VECTOR_0:
		case DCCPO_ACK_VECTOR_1:
			*vec	= value;
			*veclen = len;
			return offset + (opt_ptr - options);
		}
	}

	return -1;

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

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

386
	sk_stop_timer(sk, &hctx->rtotimer);
387
	ccid2_pr_debug("deleted RTO timer\n");
388 389 390
}

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

396 397 398 399 400
	if (hctx->cwnd < hctx->ssthresh) {
		if (*maxincr > 0 && ++hctx->packets_acked == 2) {
			hctx->cwnd += 1;
			*maxincr   -= 1;
			hctx->packets_acked = 0;
401
		}
402 403 404
	} else if (++hctx->packets_acked >= hctx->cwnd) {
			hctx->cwnd += 1;
			hctx->packets_acked = 0;
405 406 407
	}

	/* update RTO */
408 409
	if (hctx->srtt == -1 ||
	    time_after(jiffies, hctx->lastrtt + hctx->srtt)) {
410
		unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
411 412 413
		int s;

		/* first measurement */
414
		if (hctx->srtt == -1) {
415
			ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
416
				       r, jiffies,
R
Randy Dunlap 已提交
417
				       (unsigned long long)seqp->ccid2s_seq);
418
			ccid2_change_srtt(hctx, r);
419
			hctx->rttvar = r >> 1;
420
		} else {
421
			/* RTTVAR */
422
			long tmp = hctx->srtt - r;
423 424
			long srtt;

425 426 427 428
			if (tmp < 0)
				tmp *= -1;

			tmp >>= 2;
429 430 431
			hctx->rttvar *= 3;
			hctx->rttvar >>= 2;
			hctx->rttvar += tmp;
432 433

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

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

458
		hctx->lastrtt = jiffies;
459 460

		ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
461 462
			       hctx->srtt, hctx->rttvar,
			       hctx->rto, HZ, r);
463 464 465
	}

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

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

474
	if (hctx->pipe == 0)
475 476
		DCCP_BUG("pipe == 0");
	else
477
		hctx->pipe--;
478

479
	if (hctx->pipe == 0)
480
		ccid2_hc_tx_kill_rto_timer(sk);
481 482
}

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

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

492
	hctx->last_cong = jiffies;
493

494 495
	hctx->cwnd     = hctx->cwnd / 2 ? : 1U;
	hctx->ssthresh = max(hctx->cwnd, 2U);
496 497

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

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
{
	struct dccp_sock *dp = dccp_sk(sk);
	struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
	u64 ackno, seqno;
	struct ccid2_seq *seqp;
	unsigned char *vector;
	unsigned char veclen;
	int offset = 0;
	int done = 0;
	unsigned int maxincr = 0;

	ccid2_hc_tx_check_sanity(hctx);
	/* 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 */
523 524 525
	if (hctx->rpdupack == -1) {
		hctx->rpdupack = 0;
		hctx->rpseq = seqno;
526
	} else {
527
		/* check if packet is consecutive */
528 529
		if (dccp_delta_seqno(hctx->rpseq, seqno) == 1)
			hctx->rpseq = seqno;
530
		/* it's a later packet */
531 532
		else if (after48(seqno, hctx->rpseq)) {
			hctx->rpdupack++;
533 534

			/* check if we got enough dupacks */
535 536 537
			if (hctx->rpdupack >= NUMDUPACK) {
				hctx->rpdupack = -1; /* XXX lame */
				hctx->rpseq = 0;
538

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

	/* check forward path congestion */
	/* still didn't send out new data packets */
546
	if (hctx->seqh == hctx->seqt)
547 548 549 550 551 552 553 554 555 556 557
		return;

	switch (DCCP_SKB_CB(skb)->dccpd_type) {
	case DCCP_PKT_ACK:
	case DCCP_PKT_DATAACK:
		break;
	default:
		return;
	}

	ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
558 559
	if (after48(ackno, hctx->high_ack))
		hctx->high_ack = ackno;
A
Andrea Bittau 已提交
560

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

570 571 572 573
	/*
	 * 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.
574
	 */
575
	if (hctx->cwnd < hctx->ssthresh)
576
		maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
577 578 579 580 581 582 583

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

R
Randy Dunlap 已提交
586 587 588
			ccid2_pr_debug("ackvec start:%llu end:%llu\n",
				       (unsigned long long)ackno,
				       (unsigned long long)ackno_end_rl);
589 590 591 592 593
			/* 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)) {
594
				if (seqp == hctx->seqt) {
595 596 597 598 599 600 601 602 603 604 605 606
					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)) {
607 608
				const u8 state = *vector &
						 DCCP_ACKVEC_STATE_MASK;
609 610 611 612

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

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

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

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

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

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

688
		hctx->seqt = last_acked;
689 690 691
	}

	/* trim acked packets in tail */
692 693
	while (hctx->seqt != hctx->seqh) {
		if (!hctx->seqt->ccid2s_acked)
694 695
			break;

696
		hctx->seqt = hctx->seqt->ccid2s_next;
697 698 699 700 701
	}

	ccid2_hc_tx_check_sanity(hctx);
}

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

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

711 712 713 714 715
	/*
	 * 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.
	 */
716
	hctx->cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
717 718

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

723
	/* XXX init ~ to window size... */
G
Gerrit Renker 已提交
724
	if (ccid2_hc_tx_alloc_seq(hctx))
725
		return -ENOMEM;
726

727
	hctx->rto	 = 3 * HZ;
728
	ccid2_change_srtt(hctx, -1);
729 730 731 732
	hctx->rttvar	= -1;
	hctx->rpdupack  = -1;
	hctx->last_cong = jiffies;
	setup_timer(&hctx->rtotimer, ccid2_hc_tx_rto_expire, (unsigned long)sk);
733 734 735 736 737 738 739

	ccid2_hc_tx_check_sanity(hctx);
	return 0;
}

static void ccid2_hc_tx_exit(struct sock *sk)
{
740
	struct ccid2_hc_tx_sock *hctx = 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 < hctx->seqbufc; i++)
		kfree(hctx->seqbuf[i]);
	hctx->seqbufc = 0;
748 749 750 751 752 753 754 755 756 757
}

static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
{
	const struct dccp_sock *dp = dccp_sk(sk);
	struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);

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

767
static struct ccid_operations ccid2 = {
I
Ian McDonald 已提交
768
	.ccid_id		= DCCPC_CCID2,
769
	.ccid_name		= "TCP-like",
770
	.ccid_owner		= THIS_MODULE,
771
	.ccid_hc_tx_obj_size	= sizeof(struct ccid2_hc_tx_sock),
772 773 774 775 776
	.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,
777
	.ccid_hc_rx_obj_size	= sizeof(struct ccid2_hc_rx_sock),
778 779 780
	.ccid_hc_rx_packet_recv	= ccid2_hc_rx_packet_recv,
};

781
#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
782
module_param(ccid2_debug, bool, 0644);
783
MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
784
#endif
785 786 787 788 789 790 791 792 793 794 795 796 797 798

static __init int ccid2_module_init(void)
{
	return ccid_register(&ccid2);
}
module_init(ccid2_module_init);

static __exit void ccid2_module_exit(void)
{
	ccid_unregister(&ccid2);
}
module_exit(ccid2_module_exit);

MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
799
MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
800 801
MODULE_LICENSE("GPL");
MODULE_ALIAS("net-dccp-ccid-2");