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.c415
1 files changed, 215 insertions, 200 deletions
diff --git a/net/dccp/ackvec.c b/net/dccp/ackvec.c
index abaf241c7353..25b7a8d1ad58 100644
--- a/net/dccp/ackvec.c
+++ b/net/dccp/ackvec.c
@@ -9,18 +9,10 @@
9 * 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
10 * Free Software Foundation; version 2 of the License; 10 * Free Software Foundation; version 2 of the License;
11 */ 11 */
12
13#include "ackvec.h"
14#include "dccp.h" 12#include "dccp.h"
15
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
@@ -92,6 +84,24 @@ int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seqno, u8 nonce_sum)
92 return 0; 84 return 0;
93} 85}
94 86
87static struct dccp_ackvec_record *dccp_ackvec_lookup(struct list_head *av_list,
88 const u64 ackno)
89{
90 struct dccp_ackvec_record *avr;
91 /*
92 * Exploit that records are inserted in descending order of sequence
93 * number, start with the oldest record first. If @ackno is `before'
94 * the earliest ack_ackno, the packet is too old to be considered.
95 */
96 list_for_each_entry_reverse(avr, av_list, avr_node) {
97 if (avr->avr_ack_seqno == ackno)
98 return avr;
99 if (before48(ackno, avr->avr_ack_seqno))
100 break;
101 }
102 return NULL;
103}
104
95/* 105/*
96 * Buffer index and length computation using modulo-buffersize arithmetic. 106 * Buffer index and length computation using modulo-buffersize arithmetic.
97 * Note that, as pointers move from right to left, head is `before' tail. 107 * Note that, as pointers move from right to left, head is `before' tail.
@@ -113,248 +123,253 @@ u16 dccp_ackvec_buflen(const struct dccp_ackvec *av)
113 return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head); 123 return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head);
114} 124}
115 125
116/* 126/**
117 * 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
118 * bytes with run length 0, rather than a single byte with a larger run length; 128 * @av: non-empty buffer to update
119 * 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
120 */ 132 */
121static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av, 133static void dccp_ackvec_update_old(struct dccp_ackvec *av, s64 distance,
122 const unsigned int packets, 134 u64 seqno, enum dccp_ackvec_states state)
123 const unsigned char state)
124{ 135{
125 long gap; 136 u16 ptr = av->av_buf_head;
126 long new_head;
127 137
128 if (av->av_vec_len + packets > DCCPAV_MAX_ACKVEC_LEN) 138 BUG_ON(distance > 0);
129 return -ENOBUFS; 139 if (unlikely(dccp_ackvec_is_empty(av)))
140 return;
130 141
131 gap = packets - 1; 142 do {
132 new_head = av->av_buf_head - packets; 143 u8 runlen = dccp_ackvec_runlen(av->av_buf + ptr);
133 144
134 if (new_head < 0) { 145 if (distance + runlen >= 0) {
135 if (gap > 0) { 146 /*
136 memset(av->av_buf, DCCPAV_NOT_RECEIVED, 147 * Only update the state if packet has not been received
137 gap + new_head + 1); 148 * yet. This is OK as per the second table in RFC 4340,
138 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;
139 } 167 }
140 new_head += DCCPAV_MAX_ACKVEC_LEN;
141 }
142 168
143 av->av_buf_head = new_head; 169 distance += runlen + 1;
170 ptr = __ackvec_idx_add(ptr, 1);
144 171
145 if (gap > 0) 172 } while (ptr != av->av_buf_tail);
146 memset(av->av_buf + av->av_buf_head + 1, 173}
147 DCCPAV_NOT_RECEIVED, gap);
148 174
149 av->av_buf[av->av_buf_head] = state; 175/* Mark @num entries after buf_head as "Not yet received". */
150 av->av_vec_len += packets; 176static void dccp_ackvec_reserve_seats(struct dccp_ackvec *av, u16 num)
151 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);
152} 189}
153 190
154/* 191/**
155 * 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
156 */ 197 */
157int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk, 198static void dccp_ackvec_add_new(struct dccp_ackvec *av, u32 num_packets,
158 const u64 ackno, const u8 state) 199 u64 seqno, enum dccp_ackvec_states state)
159{ 200{
160 u8 *cur_head = av->av_buf + av->av_buf_head, 201 u32 num_cells = num_packets;
161 *buf_end = av->av_buf + DCCPAV_MAX_ACKVEC_LEN;
162 /*
163 * Check at the right places if the buffer is full, if it is, tell the
164 * 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 * We may well decide to do buffer compression, etc, but for now lets
168 * just drop.
169 *
170 * From Appendix A.1.1 (`New Packets'):
171 *
172 * Of course, the circular buffer may overflow, either when the
173 * HC-Sender is sending data at a very high rate, when the
174 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
175 * or when the HC-Sender is forgetting to acknowledge those acks
176 * (so the HC-Receiver is unable to clean up old state). In this
177 * case, the HC-Receiver should either compress the buffer (by
178 * increasing run lengths when possible), transfer its state to
179 * a larger buffer, or, as a last resort, drop all received
180 * packets, without processing them whatsoever, until its buffer
181 * shrinks again.
182 */
183 202
184 /* See if this is the first ackno being inserted */ 203 if (num_packets > DCCPAV_BURST_THRESH) {
185 if (av->av_vec_len == 0) { 204 u32 lost_packets = num_packets - 1;
186 *cur_head = state;
187 av->av_vec_len = 1;
188 } else if (after48(ackno, av->av_buf_ackno)) {
189 const u64 delta = dccp_delta_seqno(av->av_buf_ackno, ackno);
190 205
206 DCCP_WARN("Warning: large burst loss (%u)\n", lost_packets);
191 /* 207 /*
192 * 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"
193 * 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.
194 */ 218 */
195 if (delta == 1 && dccp_ackvec_state(cur_head) == state && 219 for (num_packets = num_cells = 1; lost_packets; ++num_cells) {
196 dccp_ackvec_runlen(cur_head) < DCCPAV_MAX_RUNLEN) 220 u8 len = min(lost_packets, (u32)DCCPAV_MAX_RUNLEN);
197 *cur_head += 1;
198 else if (dccp_ackvec_set_buf_head_state(av, delta, state))
199 return -ENOBUFS;
200 } else {
201 /*
202 * A.1.2. Old Packets
203 *
204 * When a packet with Sequence Number S <= buf_ackno
205 * arrives, the HC-Receiver will scan the table for
206 * the byte corresponding to S. (Indexing structures
207 * could reduce the complexity of this scan.)
208 */
209 u64 delta = dccp_delta_seqno(ackno, av->av_buf_ackno);
210 221
211 while (1) { 222 av->av_buf_head = __ackvec_idx_sub(av->av_buf_head, 1);
212 const u8 len = dccp_ackvec_runlen(cur_head); 223 av->av_buf[av->av_buf_head] = DCCPAV_NOT_RECEIVED | len;
213 /* 224
214 * valid packets not yet in av_buf have a reserved 225 lost_packets -= len;
215 * entry, with a len equal to 0.
216 */
217 if (*cur_head == DCCPAV_NOT_RECEIVED && delta == 0) {
218 dccp_pr_debug("Found %llu reserved seat!\n",
219 (unsigned long long)ackno);
220 *cur_head = state;
221 goto out;
222 }
223 /* len == 0 means one packet */
224 if (delta < len + 1)
225 goto out_duplicate;
226
227 delta -= len + 1;
228 if (++cur_head == buf_end)
229 cur_head = av->av_buf;
230 } 226 }
231 } 227 }
232 228
233 av->av_buf_ackno = ackno; 229 if (num_cells + dccp_ackvec_buflen(av) >= DCCPAV_MAX_ACKVEC_LEN) {
234out: 230 DCCP_CRIT("Ack Vector buffer overflow: dropping old entries\n");
235 return 0; 231 av->av_overflow = true;
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;
237
238 av->av_buf[av->av_buf_head] = state;
239 av->av_buf_ackno = seqno;
236 240
237out_duplicate: 241 if (num_packets > 1)
238 /* Duplicate packet */ 242 dccp_ackvec_reserve_seats(av, num_packets - 1);
239 dccp_pr_debug("Received a dup or already considered lost "
240 "packet: %llu\n", (unsigned long long)ackno);
241 return -EILSEQ;
242} 243}
243 244
244static void dccp_ackvec_throw_record(struct dccp_ackvec *av, 245/**
245 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)
246{ 249{
247 struct dccp_ackvec_record *next; 250 u64 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
251 enum dccp_ackvec_states state = DCCPAV_RECEIVED;
248 252
249 /* sort out vector length */ 253 if (dccp_ackvec_is_empty(av)) {
250 if (av->av_buf_head <= avr->avr_ack_ptr) 254 dccp_ackvec_add_new(av, 1, seqno, state);
251 av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head; 255 av->av_tail_ackno = seqno;
252 else
253 av->av_vec_len = DCCPAV_MAX_ACKVEC_LEN - 1 -
254 av->av_buf_head + avr->avr_ack_ptr;
255 256
256 /* free records */ 257 } else {
257 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);
258 list_del(&avr->avr_node); 259 u8 *current_head = av->av_buf + av->av_buf_head;
259 kmem_cache_free(dccp_ackvec_record_slab, avr);
260 }
261}
262 260
263void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk, 261 if (num_packets == 1 &&
264 const u64 ackno) 262 dccp_ackvec_state(current_head) == state &&
265{ 263 dccp_ackvec_runlen(current_head) < DCCPAV_MAX_RUNLEN) {
266 struct dccp_ackvec_record *avr;
267 264
268 /* 265 *current_head += 1;
269 * If we traverse backwards, it should be faster when we have large 266 av->av_buf_ackno = seqno;
270 * windows. We will be receiving ACKs for stuff we sent a while back 267
271 * -sorbo. 268 } else if (num_packets > 0) {
272 */ 269 dccp_ackvec_add_new(av, num_packets, seqno, state);
273 list_for_each_entry_reverse(avr, &av->av_records, avr_node) { 270 } else {
274 if (ackno == avr->avr_ack_seqno) { 271 dccp_ackvec_update_old(av, num_packets, seqno, state);
275 dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, " 272 }
276 "ack_ackno=%llu, ACKED!\n",
277 dccp_role(sk), avr->avr_ack_runlen,
278 (unsigned long long)avr->avr_ack_seqno,
279 (unsigned long long)avr->avr_ack_ackno);
280 dccp_ackvec_throw_record(av, avr);
281 break;
282 } else if (avr->avr_ack_seqno > ackno)
283 break; /* old news */
284 } 273 }
285} 274}
286 275
287static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av, 276/**
288 struct sock *sk, u64 *ackno, 277 * dccp_ackvec_clear_state - Perform house-keeping / garbage-collection
289 const unsigned char len, 278 * This routine is called when the peer acknowledges the receipt of Ack Vectors
290 const unsigned char *vector) 279 * up to and including @ackno. While based on on section A.3 of RFC 4340, here
280 * are additional precautions to prevent corrupted buffer state. In particular,
281 * we use tail_ackno to identify outdated records; it always marks the earliest
282 * packet of group (2) in 11.4.2.
283 */
284void dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno)
291{ 285{
292 unsigned char i; 286 struct dccp_ackvec_record *avr, *next;
293 struct dccp_ackvec_record *avr; 287 u8 runlen_now, eff_runlen;
288 s64 delta;
294 289
295 /* Check if we actually sent an ACK vector */ 290 avr = dccp_ackvec_lookup(&av->av_records, ackno);
296 if (list_empty(&av->av_records)) 291 if (avr == NULL)
297 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;
298 306
299 i = len; 307 runlen_now = dccp_ackvec_runlen(av->av_buf + avr->avr_ack_ptr);
300 /* 308 /*
301 * XXX 309 * The run length of Ack Vector cells does not decrease over time. If
302 * 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
303 * 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.
304 */ 314 */
305 avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node); 315 if (runlen_now > eff_runlen) {
306 while (i--) {
307 const u8 rl = dccp_ackvec_runlen(vector);
308 u64 ackno_end_rl;
309 316
310 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);
311 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;
312 /* 325 /*
313 * 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
314 * 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.
315 */ 329 */
316 list_for_each_entry_from(avr, &av->av_records, avr_node) { 330 av->av_overflow = 0;
317 if (!after48(avr->avr_ack_seqno, *ackno)) 331 }
318 goto found;
319 }
320 /* End of the av_records list, not found, exit */
321 break;
322found:
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 "ack_seqno=%llu, ack_ackno=%llu, "
327 "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 * If it wasn't received, continue scanning... we might
338 * find another one.
339 */
340 }
341 332
342 dccp_set_seqno(ackno, ackno_end_rl - 1); 333 /*
343 ++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);
344 } 343 }
345} 344}
346 345
347int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb, 346/*
348 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)
349{ 350{
350 if (len > DCCP_SINGLE_OPT_MAXLEN) 351 struct dccp_ackvec_parsed *new = kmalloc(sizeof(*new), GFP_ATOMIC);
351 return -1; 352
353 if (new == NULL)
354 return -ENOBUFS;
355 new->vec = vec;
356 new->len = len;
357 new->nonce = nonce;
352 358
353 /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */ 359 list_add_tail(&new->node, head);
354 dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
355 ackno, len, value);
356 return 0; 360 return 0;
357} 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);
358 373
359int __init dccp_ackvec_init(void) 374int __init dccp_ackvec_init(void)
360{ 375{