]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/dccp/ackvec.c
dccp ccid-2: Algorithm to update buffer state
[net-next-2.6.git] / net / dccp / ackvec.c
CommitLineData
ae31c339
ACM
1/*
2 * net/dccp/ackvec.c
3 *
f17a37c9
GR
4 * An implementation of Ack Vectors for the DCCP protocol
5 * Copyright (c) 2007 University of Aberdeen, Scotland, UK
ae31c339
ACM
6 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; version 2 of the License;
11 */
12
13#include "ackvec.h"
14#include "dccp.h"
15
9b07ef5d
ACM
16#include <linux/init.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
ae31c339 19#include <linux/skbuff.h>
9b07ef5d 20#include <linux/slab.h>
ae31c339
ACM
21
22#include <net/sock.h>
23
e18b890b
CL
24static struct kmem_cache *dccp_ackvec_slab;
25static struct kmem_cache *dccp_ackvec_record_slab;
02bcf28c 26
f17a37c9 27struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
02bcf28c 28{
f17a37c9
GR
29 struct dccp_ackvec *av = kmem_cache_zalloc(dccp_ackvec_slab, priority);
30
31 if (av != NULL) {
b3d14bff 32 av->av_buf_head = av->av_buf_tail = DCCPAV_MAX_ACKVEC_LEN - 1;
f17a37c9
GR
33 INIT_LIST_HEAD(&av->av_records);
34 }
35 return av;
36}
02bcf28c 37
f17a37c9
GR
38static void dccp_ackvec_purge_records(struct dccp_ackvec *av)
39{
40 struct dccp_ackvec_record *cur, *next;
02bcf28c 41
f17a37c9
GR
42 list_for_each_entry_safe(cur, next, &av->av_records, avr_node)
43 kmem_cache_free(dccp_ackvec_record_slab, cur);
44 INIT_LIST_HEAD(&av->av_records);
02bcf28c
AB
45}
46
f17a37c9 47void dccp_ackvec_free(struct dccp_ackvec *av)
02bcf28c 48{
f17a37c9
GR
49 if (likely(av != NULL)) {
50 dccp_ackvec_purge_records(av);
51 kmem_cache_free(dccp_ackvec_slab, av);
52 }
02bcf28c
AB
53}
54
7d870936
GR
55/**
56 * dccp_ackvec_update_records - Record information about sent Ack Vectors
57 * @av: Ack Vector records to update
58 * @seqno: Sequence number of the packet carrying the Ack Vector just sent
59 * @nonce_sum: The sum of all buffer nonces contained in the Ack Vector
60 */
61int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seqno, u8 nonce_sum)
ae31c339 62{
02bcf28c
AB
63 struct dccp_ackvec_record *avr;
64
f17a37c9 65 avr = kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
2d0817d1 66 if (avr == NULL)
7d870936 67 return -ENOBUFS;
ae31c339 68
7d870936 69 avr->avr_ack_seqno = seqno;
f17a37c9
GR
70 avr->avr_ack_ptr = av->av_buf_head;
71 avr->avr_ack_ackno = av->av_buf_ackno;
7d870936 72 avr->avr_ack_nonce = nonce_sum;
f17a37c9 73 avr->avr_ack_runlen = dccp_ackvec_runlen(av->av_buf + av->av_buf_head);
b3d14bff
GR
74 /*
75 * When the buffer overflows, we keep no more than one record. This is
76 * the simplest way of disambiguating sender-Acks dating from before the
77 * overflow from sender-Acks which refer to after the overflow; a simple
78 * solution is preferable here since we are handling an exception.
79 */
80 if (av->av_overflow)
81 dccp_ackvec_purge_records(av);
7d870936
GR
82 /*
83 * Since GSS is incremented for each packet, the list is automatically
84 * arranged in descending order of @ack_seqno.
85 */
86 list_add(&avr->avr_node, &av->av_records);
02bcf28c 87
7d870936 88 dccp_pr_debug("Added Vector, ack_seqno=%llu, ack_ackno=%llu (rl=%u)\n",
a47c5104 89 (unsigned long long)avr->avr_ack_seqno,
7d870936
GR
90 (unsigned long long)avr->avr_ack_ackno,
91 avr->avr_ack_runlen);
02bcf28c 92 return 0;
ae31c339
ACM
93}
94
5753fdfe
GR
95static struct dccp_ackvec_record *dccp_ackvec_lookup(struct list_head *av_list,
96 const u64 ackno)
97{
98 struct dccp_ackvec_record *avr;
99 /*
100 * Exploit that records are inserted in descending order of sequence
101 * number, start with the oldest record first. If @ackno is `before'
102 * the earliest ack_ackno, the packet is too old to be considered.
103 */
104 list_for_each_entry_reverse(avr, av_list, avr_node) {
105 if (avr->avr_ack_seqno == ackno)
106 return avr;
107 if (before48(ackno, avr->avr_ack_seqno))
108 break;
109 }
110 return NULL;
111}
112
b3d14bff
GR
113/*
114 * Buffer index and length computation using modulo-buffersize arithmetic.
115 * Note that, as pointers move from right to left, head is `before' tail.
116 */
117static inline u16 __ackvec_idx_add(const u16 a, const u16 b)
118{
119 return (a + b) % DCCPAV_MAX_ACKVEC_LEN;
120}
121
122static inline u16 __ackvec_idx_sub(const u16 a, const u16 b)
123{
124 return __ackvec_idx_add(a, DCCPAV_MAX_ACKVEC_LEN - b);
125}
126
127u16 dccp_ackvec_buflen(const struct dccp_ackvec *av)
128{
129 if (unlikely(av->av_overflow))
130 return DCCPAV_MAX_ACKVEC_LEN;
131 return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head);
132}
133
ae31c339
ACM
134/*
135 * If several packets are missing, the HC-Receiver may prefer to enter multiple
136 * bytes with run length 0, rather than a single byte with a larger run length;
137 * this simplifies table updates if one of the missing packets arrives.
138 */
139static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
140 const unsigned int packets,
e4dfd449 141 const unsigned char state)
ae31c339 142{
8e64159d 143 long gap;
a8fc3d8d 144 long new_head;
ae31c339 145
f17a37c9 146 if (av->av_vec_len + packets > DCCPAV_MAX_ACKVEC_LEN)
ae31c339
ACM
147 return -ENOBUFS;
148
149 gap = packets - 1;
a47c5104 150 new_head = av->av_buf_head - packets;
ae31c339
ACM
151
152 if (new_head < 0) {
153 if (gap > 0) {
f17a37c9 154 memset(av->av_buf, DCCPAV_NOT_RECEIVED,
ae31c339
ACM
155 gap + new_head + 1);
156 gap = -new_head;
157 }
f17a37c9 158 new_head += DCCPAV_MAX_ACKVEC_LEN;
8109b02b 159 }
ae31c339 160
a47c5104 161 av->av_buf_head = new_head;
ae31c339
ACM
162
163 if (gap > 0)
a47c5104 164 memset(av->av_buf + av->av_buf_head + 1,
f17a37c9 165 DCCPAV_NOT_RECEIVED, gap);
ae31c339 166
a47c5104
GR
167 av->av_buf[av->av_buf_head] = state;
168 av->av_vec_len += packets;
ae31c339
ACM
169 return 0;
170}
171
172/*
0e64e94e 173 * Implements the RFC 4340, Appendix A
ae31c339
ACM
174 */
175int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
176 const u64 ackno, const u8 state)
177{
f17a37c9
GR
178 u8 *cur_head = av->av_buf + av->av_buf_head,
179 *buf_end = av->av_buf + DCCPAV_MAX_ACKVEC_LEN;
ae31c339
ACM
180 /*
181 * Check at the right places if the buffer is full, if it is, tell the
182 * caller to start dropping packets till the HC-Sender acks our ACK
a47c5104 183 * vectors, when we will free up space in av_buf.
ae31c339
ACM
184 *
185 * We may well decide to do buffer compression, etc, but for now lets
186 * just drop.
187 *
0e64e94e 188 * From Appendix A.1.1 (`New Packets'):
ae31c339
ACM
189 *
190 * Of course, the circular buffer may overflow, either when the
191 * HC-Sender is sending data at a very high rate, when the
192 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
193 * or when the HC-Sender is forgetting to acknowledge those acks
194 * (so the HC-Receiver is unable to clean up old state). In this
195 * case, the HC-Receiver should either compress the buffer (by
196 * increasing run lengths when possible), transfer its state to
197 * a larger buffer, or, as a last resort, drop all received
198 * packets, without processing them whatsoever, until its buffer
199 * shrinks again.
200 */
201
202 /* See if this is the first ackno being inserted */
a47c5104 203 if (av->av_vec_len == 0) {
f17a37c9 204 *cur_head = state;
a47c5104
GR
205 av->av_vec_len = 1;
206 } else if (after48(ackno, av->av_buf_ackno)) {
207 const u64 delta = dccp_delta_seqno(av->av_buf_ackno, ackno);
ae31c339
ACM
208
209 /*
210 * Look if the state of this packet is the same as the
211 * previous ackno and if so if we can bump the head len.
212 */
f17a37c9
GR
213 if (delta == 1 && dccp_ackvec_state(cur_head) == state &&
214 dccp_ackvec_runlen(cur_head) < DCCPAV_MAX_RUNLEN)
215 *cur_head += 1;
ae31c339
ACM
216 else if (dccp_ackvec_set_buf_head_state(av, delta, state))
217 return -ENOBUFS;
218 } else {
219 /*
220 * A.1.2. Old Packets
221 *
0e64e94e
GR
222 * When a packet with Sequence Number S <= buf_ackno
223 * arrives, the HC-Receiver will scan the table for
224 * the byte corresponding to S. (Indexing structures
ae31c339
ACM
225 * could reduce the complexity of this scan.)
226 */
a47c5104 227 u64 delta = dccp_delta_seqno(ackno, av->av_buf_ackno);
ae31c339
ACM
228
229 while (1) {
f17a37c9 230 const u8 len = dccp_ackvec_runlen(cur_head);
ae31c339 231 /*
a47c5104 232 * valid packets not yet in av_buf have a reserved
ae31c339
ACM
233 * entry, with a len equal to 0.
234 */
f17a37c9 235 if (*cur_head == DCCPAV_NOT_RECEIVED && delta == 0) {
ae31c339
ACM
236 dccp_pr_debug("Found %llu reserved seat!\n",
237 (unsigned long long)ackno);
f17a37c9 238 *cur_head = state;
ae31c339
ACM
239 goto out;
240 }
241 /* len == 0 means one packet */
242 if (delta < len + 1)
243 goto out_duplicate;
244
245 delta -= len + 1;
f17a37c9
GR
246 if (++cur_head == buf_end)
247 cur_head = av->av_buf;
ae31c339
ACM
248 }
249 }
250
a47c5104 251 av->av_buf_ackno = ackno;
ae31c339 252out:
ae31c339
ACM
253 return 0;
254
255out_duplicate:
256 /* Duplicate packet */
257 dccp_pr_debug("Received a dup or already considered lost "
258 "packet: %llu\n", (unsigned long long)ackno);
259 return -EILSEQ;
260}
261
02bcf28c
AB
262static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
263 struct dccp_ackvec_record *avr)
ae31c339 264{
02bcf28c
AB
265 struct dccp_ackvec_record *next;
266
23d06e3b 267 /* sort out vector length */
a47c5104
GR
268 if (av->av_buf_head <= avr->avr_ack_ptr)
269 av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head;
23d06e3b 270 else
f17a37c9 271 av->av_vec_len = DCCPAV_MAX_ACKVEC_LEN - 1 -
a47c5104 272 av->av_buf_head + avr->avr_ack_ptr;
02bcf28c
AB
273
274 /* free records */
a47c5104 275 list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
f17a37c9
GR
276 list_del(&avr->avr_node);
277 kmem_cache_free(dccp_ackvec_record_slab, avr);
02bcf28c 278 }
ae31c339
ACM
279}
280
281void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
282 const u64 ackno)
283{
02bcf28c 284 struct dccp_ackvec_record *avr;
ae31c339 285
02bcf28c
AB
286 /*
287 * If we traverse backwards, it should be faster when we have large
288 * windows. We will be receiving ACKs for stuff we sent a while back
289 * -sorbo.
290 */
a47c5104
GR
291 list_for_each_entry_reverse(avr, &av->av_records, avr_node) {
292 if (ackno == avr->avr_ack_seqno) {
09dbc389 293 dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
02bcf28c 294 "ack_ackno=%llu, ACKED!\n",
f17a37c9 295 dccp_role(sk), avr->avr_ack_runlen,
a47c5104
GR
296 (unsigned long long)avr->avr_ack_seqno,
297 (unsigned long long)avr->avr_ack_ackno);
02bcf28c
AB
298 dccp_ackvec_throw_record(av, avr);
299 break;
a47c5104 300 } else if (avr->avr_ack_seqno > ackno)
d23ca15a 301 break; /* old news */
ae31c339
ACM
302 }
303}
304
305static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
bdf13d20 306 struct sock *sk, u64 *ackno,
ae31c339
ACM
307 const unsigned char len,
308 const unsigned char *vector)
309{
310 unsigned char i;
02bcf28c 311 struct dccp_ackvec_record *avr;
ae31c339
ACM
312
313 /* Check if we actually sent an ACK vector */
a47c5104 314 if (list_empty(&av->av_records))
ae31c339 315 return;
ae31c339
ACM
316
317 i = len;
02bcf28c
AB
318 /*
319 * XXX
320 * I think it might be more efficient to work backwards. See comment on
321 * rcv_ackno. -sorbo.
322 */
a47c5104 323 avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node);
ae31c339 324 while (i--) {
f17a37c9 325 const u8 rl = dccp_ackvec_runlen(vector);
ae31c339
ACM
326 u64 ackno_end_rl;
327
bdf13d20 328 dccp_set_seqno(&ackno_end_rl, *ackno - rl);
ae31c339
ACM
329
330 /*
02bcf28c
AB
331 * If our AVR sequence number is greater than the ack, go
332 * forward in the AVR list until it is not so.
ae31c339 333 */
a47c5104
GR
334 list_for_each_entry_from(avr, &av->av_records, avr_node) {
335 if (!after48(avr->avr_ack_seqno, *ackno))
02bcf28c
AB
336 goto found;
337 }
a47c5104 338 /* End of the av_records list, not found, exit */
02bcf28c
AB
339 break;
340found:
a47c5104 341 if (between48(avr->avr_ack_seqno, ackno_end_rl, *ackno)) {
f17a37c9 342 if (dccp_ackvec_state(vector) != DCCPAV_NOT_RECEIVED) {
09dbc389 343 dccp_pr_debug("%s ACK vector 0, len=%d, "
ae31c339
ACM
344 "ack_seqno=%llu, ack_ackno=%llu, "
345 "ACKED!\n",
09dbc389 346 dccp_role(sk), len,
ae31c339 347 (unsigned long long)
a47c5104 348 avr->avr_ack_seqno,
ae31c339 349 (unsigned long long)
a47c5104 350 avr->avr_ack_ackno);
02bcf28c 351 dccp_ackvec_throw_record(av, avr);
afec35e3 352 break;
ae31c339
ACM
353 }
354 /*
02bcf28c
AB
355 * If it wasn't received, continue scanning... we might
356 * find another one.
ae31c339 357 */
ae31c339 358 }
ae31c339 359
bdf13d20 360 dccp_set_seqno(ackno, ackno_end_rl - 1);
ae31c339
ACM
361 ++vector;
362 }
363}
364
365int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
bdf13d20 366 u64 *ackno, const u8 opt, const u8 *value, const u8 len)
ae31c339 367{
b20a9c24 368 if (len > DCCP_SINGLE_OPT_MAXLEN)
ae31c339
ACM
369 return -1;
370
371 /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
372 dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
bdf13d20 373 ackno, len, value);
ae31c339
ACM
374 return 0;
375}
9b07ef5d 376
5753fdfe
GR
377/**
378 * dccp_ackvec_clear_state - Perform house-keeping / garbage-collection
379 * This routine is called when the peer acknowledges the receipt of Ack Vectors
380 * up to and including @ackno. While based on on section A.3 of RFC 4340, here
381 * are additional precautions to prevent corrupted buffer state. In particular,
382 * we use tail_ackno to identify outdated records; it always marks the earliest
383 * packet of group (2) in 11.4.2.
384 */
385void dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno)
386 {
387 struct dccp_ackvec_record *avr, *next;
388 u8 runlen_now, eff_runlen;
389 s64 delta;
390
391 avr = dccp_ackvec_lookup(&av->av_records, ackno);
392 if (avr == NULL)
393 return;
394 /*
395 * Deal with outdated acknowledgments: this arises when e.g. there are
396 * several old records and the acks from the peer come in slowly. In
397 * that case we may still have records that pre-date tail_ackno.
398 */
399 delta = dccp_delta_seqno(av->av_tail_ackno, avr->avr_ack_ackno);
400 if (delta < 0)
401 goto free_records;
402 /*
403 * Deal with overlapping Ack Vectors: don't subtract more than the
404 * number of packets between tail_ackno and ack_ackno.
405 */
406 eff_runlen = delta < avr->avr_ack_runlen ? delta : avr->avr_ack_runlen;
407
408 runlen_now = dccp_ackvec_runlen(av->av_buf + avr->avr_ack_ptr);
409 /*
410 * The run length of Ack Vector cells does not decrease over time. If
411 * the run length is the same as at the time the Ack Vector was sent, we
412 * free the ack_ptr cell. That cell can however not be freed if the run
413 * length has increased: in this case we need to move the tail pointer
414 * backwards (towards higher indices), to its next-oldest neighbour.
415 */
416 if (runlen_now > eff_runlen) {
417
418 av->av_buf[avr->avr_ack_ptr] -= eff_runlen + 1;
419 av->av_buf_tail = __ackvec_idx_add(avr->avr_ack_ptr, 1);
420
421 /* This move may not have cleared the overflow flag. */
422 if (av->av_overflow)
423 av->av_overflow = (av->av_buf_head == av->av_buf_tail);
424 } else {
425 av->av_buf_tail = avr->avr_ack_ptr;
426 /*
427 * We have made sure that avr points to a valid cell within the
428 * buffer. This cell is either older than head, or equals head
429 * (empty buffer): in both cases we no longer have any overflow.
430 */
431 av->av_overflow = 0;
432 }
433
434 /*
435 * The peer has acknowledged up to and including ack_ackno. Hence the
436 * first packet in group (2) of 11.4.2 is the successor of ack_ackno.
437 */
438 av->av_tail_ackno = ADD48(avr->avr_ack_ackno, 1);
439
440free_records:
441 list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
442 list_del(&avr->avr_node);
443 kmem_cache_free(dccp_ackvec_record_slab, avr);
444 }
445}
446
9b07ef5d
ACM
447int __init dccp_ackvec_init(void)
448{
449 dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
450 sizeof(struct dccp_ackvec), 0,
20c2df83 451 SLAB_HWCACHE_ALIGN, NULL);
02bcf28c
AB
452 if (dccp_ackvec_slab == NULL)
453 goto out_err;
454
f17a37c9
GR
455 dccp_ackvec_record_slab = kmem_cache_create("dccp_ackvec_record",
456 sizeof(struct dccp_ackvec_record),
457 0, SLAB_HWCACHE_ALIGN, NULL);
02bcf28c
AB
458 if (dccp_ackvec_record_slab == NULL)
459 goto out_destroy_slab;
9b07ef5d
ACM
460
461 return 0;
02bcf28c
AB
462
463out_destroy_slab:
464 kmem_cache_destroy(dccp_ackvec_slab);
465 dccp_ackvec_slab = NULL;
466out_err:
59348b19 467 DCCP_CRIT("Unable to create Ack Vector slab cache");
02bcf28c 468 return -ENOBUFS;
9b07ef5d
ACM
469}
470
471void dccp_ackvec_exit(void)
472{
473 if (dccp_ackvec_slab != NULL) {
474 kmem_cache_destroy(dccp_ackvec_slab);
475 dccp_ackvec_slab = NULL;
476 }
02bcf28c
AB
477 if (dccp_ackvec_record_slab != NULL) {
478 kmem_cache_destroy(dccp_ackvec_record_slab);
479 dccp_ackvec_record_slab = NULL;
480 }
9b07ef5d 481}