ackvec.c 11.0 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
	struct dccp_ackvec *av = kmem_cache_zalloc(dccp_ackvec_slab, priority);

	if (av != NULL) {
32
		av->av_buf_head	= av->av_buf_tail = DCCPAV_MAX_ACKVEC_LEN - 1;
33 34 35 36
		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
/**
 * dccp_ackvec_update_records  -  Record information about sent Ack Vectors
 * @av:		Ack Vector records to update
 * @seqno:	Sequence number of the packet carrying the Ack Vector just sent
 * @nonce_sum:	The sum of all buffer nonces contained in the Ack Vector
 */
int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seqno, u8 nonce_sum)
62
{
63 64
	struct dccp_ackvec_record *avr;

65
	avr = kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
66
	if (avr == NULL)
67
		return -ENOBUFS;
68

69
	avr->avr_ack_seqno  = seqno;
70 71
	avr->avr_ack_ptr    = av->av_buf_head;
	avr->avr_ack_ackno  = av->av_buf_ackno;
72
	avr->avr_ack_nonce  = nonce_sum;
73
	avr->avr_ack_runlen = dccp_ackvec_runlen(av->av_buf + av->av_buf_head);
74 75 76 77 78 79 80 81
	/*
	 * When the buffer overflows, we keep no more than one record. This is
	 * the simplest way of disambiguating sender-Acks dating from before the
	 * overflow from sender-Acks which refer to after the overflow; a simple
	 * solution is preferable here since we are handling an exception.
	 */
	if (av->av_overflow)
		dccp_ackvec_purge_records(av);
82 83 84 85 86
	/*
	 * Since GSS is incremented for each packet, the list is automatically
	 * arranged in descending order of @ack_seqno.
	 */
	list_add(&avr->avr_node, &av->av_records);
87

88
	dccp_pr_debug("Added Vector, ack_seqno=%llu, ack_ackno=%llu (rl=%u)\n",
89
		      (unsigned long long)avr->avr_ack_seqno,
90 91
		      (unsigned long long)avr->avr_ack_ackno,
		      avr->avr_ack_runlen);
92
	return 0;
93 94
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 * Buffer index and length computation using modulo-buffersize arithmetic.
 * Note that, as pointers move from right to left, head is `before' tail.
 */
static inline u16 __ackvec_idx_add(const u16 a, const u16 b)
{
	return (a + b) % DCCPAV_MAX_ACKVEC_LEN;
}

static inline u16 __ackvec_idx_sub(const u16 a, const u16 b)
{
	return __ackvec_idx_add(a, DCCPAV_MAX_ACKVEC_LEN - b);
}

u16 dccp_ackvec_buflen(const struct dccp_ackvec *av)
{
	if (unlikely(av->av_overflow))
		return DCCPAV_MAX_ACKVEC_LEN;
	return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head);
}

116 117 118 119 120 121 122
/*
 * 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,
123
						 const unsigned char state)
124
{
K
Kulikov Vasiliy 已提交
125
	long gap;
K
Kris Katterjohn 已提交
126
	long new_head;
127

128
	if (av->av_vec_len + packets > DCCPAV_MAX_ACKVEC_LEN)
129 130 131
		return -ENOBUFS;

	gap	 = packets - 1;
132
	new_head = av->av_buf_head - packets;
133 134 135

	if (new_head < 0) {
		if (gap > 0) {
136
			memset(av->av_buf, DCCPAV_NOT_RECEIVED,
137 138 139
			       gap + new_head + 1);
			gap = -new_head;
		}
140
		new_head += DCCPAV_MAX_ACKVEC_LEN;
141
	}
142

143
	av->av_buf_head = new_head;
144 145

	if (gap > 0)
146
		memset(av->av_buf + av->av_buf_head + 1,
147
		       DCCPAV_NOT_RECEIVED, gap);
148

149 150
	av->av_buf[av->av_buf_head] = state;
	av->av_vec_len += packets;
151 152 153 154
	return 0;
}

/*
155
 * Implements the RFC 4340, Appendix A
156 157 158 159
 */
int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
		    const u64 ackno, const u8 state)
{
160 161
	u8 *cur_head = av->av_buf + av->av_buf_head,
	   *buf_end  = av->av_buf + DCCPAV_MAX_ACKVEC_LEN;
162 163 164
	/*
	 * 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
165
	 * vectors, when we will free up space in av_buf.
166 167 168 169
	 *
	 * We may well decide to do buffer compression, etc, but for now lets
	 * just drop.
	 *
170
	 * From Appendix A.1.1 (`New Packets'):
171 172 173 174 175 176 177 178 179 180 181 182 183 184
	 *
	 *	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 */
185
	if (av->av_vec_len == 0) {
186
		*cur_head = state;
187 188 189
		av->av_vec_len = 1;
	} else if (after48(ackno, av->av_buf_ackno)) {
		const u64 delta = dccp_delta_seqno(av->av_buf_ackno, ackno);
190 191 192 193 194

		/*
		 * Look if the state of this packet is the same as the
		 * previous ackno and if so if we can bump the head len.
		 */
195 196 197
		if (delta == 1 && dccp_ackvec_state(cur_head) == state &&
		    dccp_ackvec_runlen(cur_head) < DCCPAV_MAX_RUNLEN)
			*cur_head += 1;
198 199 200 201 202 203
		else if (dccp_ackvec_set_buf_head_state(av, delta, state))
			return -ENOBUFS;
	} else {
		/*
		 * A.1.2.  Old Packets
		 *
204 205 206
		 *	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
207 208
		 *	could reduce the complexity of this scan.)
		 */
209
		u64 delta = dccp_delta_seqno(ackno, av->av_buf_ackno);
210 211

		while (1) {
212
			const u8 len = dccp_ackvec_runlen(cur_head);
213
			/*
214
			 * valid packets not yet in av_buf have a reserved
215 216
			 * entry, with a len equal to 0.
			 */
217
			if (*cur_head == DCCPAV_NOT_RECEIVED && delta == 0) {
218 219
				dccp_pr_debug("Found %llu reserved seat!\n",
					      (unsigned long long)ackno);
220
				*cur_head = state;
221 222 223 224 225 226 227
				goto out;
			}
			/* len == 0 means one packet */
			if (delta < len + 1)
				goto out_duplicate;

			delta -= len + 1;
228 229
			if (++cur_head == buf_end)
				cur_head = av->av_buf;
230 231 232
		}
	}

233
	av->av_buf_ackno = ackno;
234 235 236 237 238 239 240 241 242 243
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;
}

244 245
static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
				     struct dccp_ackvec_record *avr)
246
{
247 248
	struct dccp_ackvec_record *next;

249
	/* sort out vector length */
250 251
	if (av->av_buf_head <= avr->avr_ack_ptr)
		av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head;
252
	else
253
		av->av_vec_len = DCCPAV_MAX_ACKVEC_LEN - 1 -
254
				 av->av_buf_head + avr->avr_ack_ptr;
255 256

	/* free records */
257
	list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
258 259
		list_del(&avr->avr_node);
		kmem_cache_free(dccp_ackvec_record_slab, avr);
260
	}
261 262 263 264 265
}

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

268 269 270 271 272
	/*
	 * 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.
	 */
273 274
	list_for_each_entry_reverse(avr, &av->av_records, avr_node) {
		if (ackno == avr->avr_ack_seqno) {
275
			dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
276
				      "ack_ackno=%llu, ACKED!\n",
277
				      dccp_role(sk), avr->avr_ack_runlen,
278 279
				      (unsigned long long)avr->avr_ack_seqno,
				      (unsigned long long)avr->avr_ack_ackno);
280 281
			dccp_ackvec_throw_record(av, avr);
			break;
282
		} else if (avr->avr_ack_seqno > ackno)
283
			break; /* old news */
284 285 286 287
	}
}

static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
288
					    struct sock *sk, u64 *ackno,
289 290 291 292
					    const unsigned char len,
					    const unsigned char *vector)
{
	unsigned char i;
293
	struct dccp_ackvec_record *avr;
294 295

	/* Check if we actually sent an ACK vector */
296
	if (list_empty(&av->av_records))
297 298 299
		return;

	i = len;
300 301 302 303 304
	/*
	 * XXX
	 * I think it might be more efficient to work backwards. See comment on
	 * rcv_ackno. -sorbo.
	 */
305
	avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node);
306
	while (i--) {
307
		const u8 rl = dccp_ackvec_runlen(vector);
308 309
		u64 ackno_end_rl;

310
		dccp_set_seqno(&ackno_end_rl, *ackno - rl);
311 312

		/*
313 314
		 * If our AVR sequence number is greater than the ack, go
		 * forward in the AVR list until it is not so.
315
		 */
316 317
		list_for_each_entry_from(avr, &av->av_records, avr_node) {
			if (!after48(avr->avr_ack_seqno, *ackno))
318 319
				goto found;
		}
320
		/* End of the av_records list, not found, exit */
321 322
		break;
found:
323
		if (between48(avr->avr_ack_seqno, ackno_end_rl, *ackno)) {
324
			if (dccp_ackvec_state(vector) != DCCPAV_NOT_RECEIVED) {
325
				dccp_pr_debug("%s ACK vector 0, len=%d, "
326 327
					      "ack_seqno=%llu, ack_ackno=%llu, "
					      "ACKED!\n",
328
					      dccp_role(sk), len,
329
					      (unsigned long long)
330
					      avr->avr_ack_seqno,
331
					      (unsigned long long)
332
					      avr->avr_ack_ackno);
333
				dccp_ackvec_throw_record(av, avr);
334
				break;
335 336
			}
			/*
337 338
			 * If it wasn't received, continue scanning... we might
			 * find another one.
339 340 341
			 */
		}

342
		dccp_set_seqno(ackno, ackno_end_rl - 1);
343 344 345 346 347
		++vector;
	}
}

int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
348
		      u64 *ackno, const u8 opt, const u8 *value, const u8 len)
349
{
350
	if (len > DCCP_SINGLE_OPT_MAXLEN)
351 352 353 354
		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,
355
					ackno, len, value);
356 357
	return 0;
}
358 359 360 361 362

int __init dccp_ackvec_init(void)
{
	dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
					     sizeof(struct dccp_ackvec), 0,
363
					     SLAB_HWCACHE_ALIGN, NULL);
364 365 366
	if (dccp_ackvec_slab == NULL)
		goto out_err;

367 368 369
	dccp_ackvec_record_slab = kmem_cache_create("dccp_ackvec_record",
					     sizeof(struct dccp_ackvec_record),
					     0, SLAB_HWCACHE_ALIGN, NULL);
370 371
	if (dccp_ackvec_record_slab == NULL)
		goto out_destroy_slab;
372 373

	return 0;
374 375 376 377 378

out_destroy_slab:
	kmem_cache_destroy(dccp_ackvec_slab);
	dccp_ackvec_slab = NULL;
out_err:
379
	DCCP_CRIT("Unable to create Ack Vector slab cache");
380
	return -ENOBUFS;
381 382 383 384 385 386 387 388
}

void dccp_ackvec_exit(void)
{
	if (dccp_ackvec_slab != NULL) {
		kmem_cache_destroy(dccp_ackvec_slab);
		dccp_ackvec_slab = NULL;
	}
389 390 391 392
	if (dccp_ackvec_record_slab != NULL) {
		kmem_cache_destroy(dccp_ackvec_record_slab);
		dccp_ackvec_record_slab = NULL;
	}
393
}