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