ackvec.c 11.6 KB
Newer Older
1 2 3
/*
 *  net/dccp/ackvec.c
 *
4 5
 *  An implementation of Ack Vectors for the DCCP protocol
 *  Copyright (c) 2007 University of Aberdeen, Scotland, UK
6 7 8 9 10 11 12 13 14 15
 *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
 *
 *      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; version 2 of the License;
 */

#include "ackvec.h"
#include "dccp.h"

16 17 18
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
19
#include <linux/skbuff.h>
20
#include <linux/slab.h>
21 22 23

#include <net/sock.h>

24 25
static struct kmem_cache *dccp_ackvec_slab;
static struct kmem_cache *dccp_ackvec_record_slab;
26

27
struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
28
{
29 30 31 32 33 34 35 36
	struct dccp_ackvec *av = kmem_cache_zalloc(dccp_ackvec_slab, priority);

	if (av != NULL) {
		av->av_buf_head	= DCCPAV_MAX_ACKVEC_LEN - 1;
		INIT_LIST_HEAD(&av->av_records);
	}
	return av;
}
37

38 39 40
static void dccp_ackvec_purge_records(struct dccp_ackvec *av)
{
	struct dccp_ackvec_record *cur, *next;
41

42 43 44
	list_for_each_entry_safe(cur, next, &av->av_records, avr_node)
		kmem_cache_free(dccp_ackvec_record_slab, cur);
	INIT_LIST_HEAD(&av->av_records);
45 46
}

47
void dccp_ackvec_free(struct dccp_ackvec *av)
48
{
49 50 51 52
	if (likely(av != NULL)) {
		dccp_ackvec_purge_records(av);
		kmem_cache_free(dccp_ackvec_slab, av);
	}
53 54 55 56 57 58 59 60 61 62
}

static void dccp_ackvec_insert_avr(struct dccp_ackvec *av,
				   struct dccp_ackvec_record *avr)
{
	/*
	 * AVRs are sorted by seqno. Since we are sending them in order, we
	 * just add the AVR at the head of the list.
	 * -sorbo.
	 */
63
	if (!list_empty(&av->av_records)) {
64
		const struct dccp_ackvec_record *head =
65
					list_entry(av->av_records.next,
66
						   struct dccp_ackvec_record,
67 68
						   avr_node);
		BUG_ON(before48(avr->avr_ack_seqno, head->avr_ack_seqno));
69 70
	}

71
	list_add(&avr->avr_node, &av->av_records);
72
}
73

74 75 76 77
int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
{
	struct dccp_sock *dp = dccp_sk(sk);
	struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
78
	/* Figure out how many options do we need to represent the ackvec */
79
	const u8 nr_opts = DIV_ROUND_UP(av->av_vec_len, DCCP_SINGLE_OPT_MAXLEN);
80 81
	u16 len = av->av_vec_len + 2 * nr_opts;
	u8 i, nonce = 0;
82 83
	const unsigned char *tail, *from;
	unsigned char *to;
84 85 86 87 88
	struct dccp_ackvec_record *avr;

	if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
		return -1;

89
	avr = kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
90 91
	if (avr == NULL)
		return -1;
92 93 94

	DCCP_SKB_CB(skb)->dccpd_opt_len += len;

95
	to   = skb_push(skb, len);
96 97
	len  = av->av_vec_len;
	from = av->av_buf + av->av_buf_head;
98
	tail = av->av_buf + DCCPAV_MAX_ACKVEC_LEN;
99 100 101 102

	for (i = 0; i < nr_opts; ++i) {
		int copylen = len;

103 104
		if (len > DCCP_SINGLE_OPT_MAXLEN)
			copylen = DCCP_SINGLE_OPT_MAXLEN;
105

106 107 108 109 110 111 112
		/*
		 * RFC 4340, 12.2: Encode the Nonce Echo for this Ack Vector via
		 * its type; ack_nonce is the sum of all individual buf_nonce's.
		 */
		nonce ^= av->av_buf_nonce[i];

		*to++ = DCCPO_ACK_VECTOR_0 + av->av_buf_nonce[i];
113 114 115 116 117
		*to++ = copylen + 2;

		/* Check if buf_head wraps */
		if (from + copylen > tail) {
			const u16 tailsize = tail - from;
118

119 120 121 122
			memcpy(to, from, tailsize);
			to	+= tailsize;
			len	-= tailsize;
			copylen	-= tailsize;
123
			from	= av->av_buf;
124
		}
125

126 127 128 129
		memcpy(to, from, copylen);
		from += copylen;
		to   += copylen;
		len  -= copylen;
130 131 132
	}

	/*
133
	 * Each sent Ack Vector is recorded in the list, as per A.2 of RFC 4340.
134
	 */
135 136 137 138 139
	avr->avr_ack_seqno  = DCCP_SKB_CB(skb)->dccpd_seq;
	avr->avr_ack_ptr    = av->av_buf_head;
	avr->avr_ack_ackno  = av->av_buf_ackno;
	avr->avr_ack_nonce  = nonce;
	avr->avr_ack_runlen = dccp_ackvec_runlen(av->av_buf + av->av_buf_head);
140 141

	dccp_ackvec_insert_avr(av, avr);
142

143
	dccp_pr_debug("%s ACK Vector 0, len=%d, ack_seqno=%llu, "
144
		      "ack_ackno=%llu\n",
145
		      dccp_role(sk), avr->avr_ack_runlen,
146 147
		      (unsigned long long)avr->avr_ack_seqno,
		      (unsigned long long)avr->avr_ack_ackno);
148
	return 0;
149 150 151 152 153 154 155 156 157
}

/*
 * If several packets are missing, the HC-Receiver may prefer to enter multiple
 * bytes with run length 0, rather than a single byte with a larger run length;
 * this simplifies table updates if one of the missing packets arrives.
 */
static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
						 const unsigned int packets,
158
						 const unsigned char state)
159 160
{
	unsigned int gap;
K
Kris Katterjohn 已提交
161
	long new_head;
162

163
	if (av->av_vec_len + packets > DCCPAV_MAX_ACKVEC_LEN)
164 165 166
		return -ENOBUFS;

	gap	 = packets - 1;
167
	new_head = av->av_buf_head - packets;
168 169 170

	if (new_head < 0) {
		if (gap > 0) {
171
			memset(av->av_buf, DCCPAV_NOT_RECEIVED,
172 173 174
			       gap + new_head + 1);
			gap = -new_head;
		}
175
		new_head += DCCPAV_MAX_ACKVEC_LEN;
176
	}
177

178
	av->av_buf_head = new_head;
179 180

	if (gap > 0)
181
		memset(av->av_buf + av->av_buf_head + 1,
182
		       DCCPAV_NOT_RECEIVED, gap);
183

184 185
	av->av_buf[av->av_buf_head] = state;
	av->av_vec_len += packets;
186 187 188 189
	return 0;
}

/*
190
 * Implements the RFC 4340, Appendix A
191 192 193 194
 */
int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
		    const u64 ackno, const u8 state)
{
195 196
	u8 *cur_head = av->av_buf + av->av_buf_head,
	   *buf_end  = av->av_buf + DCCPAV_MAX_ACKVEC_LEN;
197 198 199
	/*
	 * Check at the right places if the buffer is full, if it is, tell the
	 * caller to start dropping packets till the HC-Sender acks our ACK
200
	 * vectors, when we will free up space in av_buf.
201 202 203 204
	 *
	 * We may well decide to do buffer compression, etc, but for now lets
	 * just drop.
	 *
205
	 * From Appendix A.1.1 (`New Packets'):
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	 *
	 *	Of course, the circular buffer may overflow, either when the
	 *	HC-Sender is sending data at a very high rate, when the
	 *	HC-Receiver's acknowledgements are not reaching the HC-Sender,
	 *	or when the HC-Sender is forgetting to acknowledge those acks
	 *	(so the HC-Receiver is unable to clean up old state). In this
	 *	case, the HC-Receiver should either compress the buffer (by
	 *	increasing run lengths when possible), transfer its state to
	 *	a larger buffer, or, as a last resort, drop all received
	 *	packets, without processing them whatsoever, until its buffer
	 *	shrinks again.
	 */

	/* See if this is the first ackno being inserted */
220
	if (av->av_vec_len == 0) {
221
		*cur_head = state;
222 223 224
		av->av_vec_len = 1;
	} else if (after48(ackno, av->av_buf_ackno)) {
		const u64 delta = dccp_delta_seqno(av->av_buf_ackno, ackno);
225 226 227 228 229

		/*
		 * Look if the state of this packet is the same as the
		 * previous ackno and if so if we can bump the head len.
		 */
230 231 232
		if (delta == 1 && dccp_ackvec_state(cur_head) == state &&
		    dccp_ackvec_runlen(cur_head) < DCCPAV_MAX_RUNLEN)
			*cur_head += 1;
233 234 235 236 237 238
		else if (dccp_ackvec_set_buf_head_state(av, delta, state))
			return -ENOBUFS;
	} else {
		/*
		 * A.1.2.  Old Packets
		 *
239 240 241
		 *	When a packet with Sequence Number S <= buf_ackno
		 *	arrives, the HC-Receiver will scan the table for
		 *	the byte corresponding to S. (Indexing structures
242 243
		 *	could reduce the complexity of this scan.)
		 */
244
		u64 delta = dccp_delta_seqno(ackno, av->av_buf_ackno);
245 246

		while (1) {
247
			const u8 len = dccp_ackvec_runlen(cur_head);
248
			/*
249
			 * valid packets not yet in av_buf have a reserved
250 251
			 * entry, with a len equal to 0.
			 */
252
			if (*cur_head == DCCPAV_NOT_RECEIVED && delta == 0) {
253 254
				dccp_pr_debug("Found %llu reserved seat!\n",
					      (unsigned long long)ackno);
255
				*cur_head = state;
256 257 258 259 260 261 262
				goto out;
			}
			/* len == 0 means one packet */
			if (delta < len + 1)
				goto out_duplicate;

			delta -= len + 1;
263 264
			if (++cur_head == buf_end)
				cur_head = av->av_buf;
265 266 267
		}
	}

268
	av->av_buf_ackno = ackno;
269 270 271 272 273 274 275 276 277 278
out:
	return 0;

out_duplicate:
	/* Duplicate packet */
	dccp_pr_debug("Received a dup or already considered lost "
		      "packet: %llu\n", (unsigned long long)ackno);
	return -EILSEQ;
}

279 280
static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
				     struct dccp_ackvec_record *avr)
281
{
282 283
	struct dccp_ackvec_record *next;

284
	/* sort out vector length */
285 286
	if (av->av_buf_head <= avr->avr_ack_ptr)
		av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head;
287
	else
288
		av->av_vec_len = DCCPAV_MAX_ACKVEC_LEN - 1 -
289
				 av->av_buf_head + avr->avr_ack_ptr;
290 291

	/* free records */
292
	list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
293 294
		list_del(&avr->avr_node);
		kmem_cache_free(dccp_ackvec_record_slab, avr);
295
	}
296 297 298 299 300
}

void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
				 const u64 ackno)
{
301
	struct dccp_ackvec_record *avr;
302

303 304 305 306 307
	/*
	 * If we traverse backwards, it should be faster when we have large
	 * windows. We will be receiving ACKs for stuff we sent a while back
	 * -sorbo.
	 */
308 309
	list_for_each_entry_reverse(avr, &av->av_records, avr_node) {
		if (ackno == avr->avr_ack_seqno) {
310
			dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
311
				      "ack_ackno=%llu, ACKED!\n",
312
				      dccp_role(sk), avr->avr_ack_runlen,
313 314
				      (unsigned long long)avr->avr_ack_seqno,
				      (unsigned long long)avr->avr_ack_ackno);
315 316
			dccp_ackvec_throw_record(av, avr);
			break;
317
		} else if (avr->avr_ack_seqno > ackno)
318
			break; /* old news */
319 320 321 322
	}
}

static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
323
					    struct sock *sk, u64 *ackno,
324 325 326 327
					    const unsigned char len,
					    const unsigned char *vector)
{
	unsigned char i;
328
	struct dccp_ackvec_record *avr;
329 330

	/* Check if we actually sent an ACK vector */
331
	if (list_empty(&av->av_records))
332 333 334
		return;

	i = len;
335 336 337 338 339
	/*
	 * XXX
	 * I think it might be more efficient to work backwards. See comment on
	 * rcv_ackno. -sorbo.
	 */
340
	avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node);
341
	while (i--) {
342
		const u8 rl = dccp_ackvec_runlen(vector);
343 344
		u64 ackno_end_rl;

345
		dccp_set_seqno(&ackno_end_rl, *ackno - rl);
346 347

		/*
348 349
		 * If our AVR sequence number is greater than the ack, go
		 * forward in the AVR list until it is not so.
350
		 */
351 352
		list_for_each_entry_from(avr, &av->av_records, avr_node) {
			if (!after48(avr->avr_ack_seqno, *ackno))
353 354
				goto found;
		}
355
		/* End of the av_records list, not found, exit */
356 357
		break;
found:
358
		if (between48(avr->avr_ack_seqno, ackno_end_rl, *ackno)) {
359
			if (dccp_ackvec_state(vector) != DCCPAV_NOT_RECEIVED) {
360
				dccp_pr_debug("%s ACK vector 0, len=%d, "
361 362
					      "ack_seqno=%llu, ack_ackno=%llu, "
					      "ACKED!\n",
363
					      dccp_role(sk), len,
364
					      (unsigned long long)
365
					      avr->avr_ack_seqno,
366
					      (unsigned long long)
367
					      avr->avr_ack_ackno);
368
				dccp_ackvec_throw_record(av, avr);
369
				break;
370 371
			}
			/*
372 373
			 * If it wasn't received, continue scanning... we might
			 * find another one.
374 375 376
			 */
		}

377
		dccp_set_seqno(ackno, ackno_end_rl - 1);
378 379 380 381 382
		++vector;
	}
}

int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
383
		      u64 *ackno, const u8 opt, const u8 *value, const u8 len)
384
{
385
	if (len > DCCP_SINGLE_OPT_MAXLEN)
386 387 388 389
		return -1;

	/* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
	dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
390
					ackno, len, value);
391 392
	return 0;
}
393 394 395 396 397

int __init dccp_ackvec_init(void)
{
	dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
					     sizeof(struct dccp_ackvec), 0,
398
					     SLAB_HWCACHE_ALIGN, NULL);
399 400 401
	if (dccp_ackvec_slab == NULL)
		goto out_err;

402 403 404
	dccp_ackvec_record_slab = kmem_cache_create("dccp_ackvec_record",
					     sizeof(struct dccp_ackvec_record),
					     0, SLAB_HWCACHE_ALIGN, NULL);
405 406
	if (dccp_ackvec_record_slab == NULL)
		goto out_destroy_slab;
407 408

	return 0;
409 410 411 412 413

out_destroy_slab:
	kmem_cache_destroy(dccp_ackvec_slab);
	dccp_ackvec_slab = NULL;
out_err:
414
	DCCP_CRIT("Unable to create Ack Vector slab cache");
415
	return -ENOBUFS;
416 417 418 419 420 421 422 423
}

void dccp_ackvec_exit(void)
{
	if (dccp_ackvec_slab != NULL) {
		kmem_cache_destroy(dccp_ackvec_slab);
		dccp_ackvec_slab = NULL;
	}
424 425 426 427
	if (dccp_ackvec_record_slab != NULL) {
		kmem_cache_destroy(dccp_ackvec_record_slab);
		dccp_ackvec_record_slab = NULL;
	}
428
}