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, 345 insertions, 274 deletions
diff --git a/net/dccp/ackvec.c b/net/dccp/ackvec.c
index 41819848bdda..1e8be246ad15 100644
--- a/net/dccp/ackvec.c
+++ b/net/dccp/ackvec.c
@@ -1,375 +1,445 @@
1/* 1/*
2 * net/dccp/ackvec.c 2 * net/dccp/ackvec.c
3 * 3 *
4 * An implementation of Ack Vectors for the DCCP protocol 4 * An implementation of the DCCP protocol
5 * Copyright (c) 2007 University of Aberdeen, Scotland, UK
6 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net> 5 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify it 7 * 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 8 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; version 2 of the License; 9 * Free Software Foundation; version 2 of the License;
11 */ 10 */
11
12#include "ackvec.h"
12#include "dccp.h" 13#include "dccp.h"
14
15#include <linux/dccp.h>
16#include <linux/init.h>
17#include <linux/errno.h>
13#include <linux/kernel.h> 18#include <linux/kernel.h>
19#include <linux/skbuff.h>
14#include <linux/slab.h> 20#include <linux/slab.h>
15 21
22#include <net/sock.h>
23
16static struct kmem_cache *dccp_ackvec_slab; 24static struct kmem_cache *dccp_ackvec_slab;
17static struct kmem_cache *dccp_ackvec_record_slab; 25static struct kmem_cache *dccp_ackvec_record_slab;
18 26
19struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority) 27static struct dccp_ackvec_record *dccp_ackvec_record_new(void)
20{ 28{
21 struct dccp_ackvec *av = kmem_cache_zalloc(dccp_ackvec_slab, priority); 29 struct dccp_ackvec_record *avr =
30 kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
22 31
23 if (av != NULL) { 32 if (avr != NULL)
24 av->av_buf_head = av->av_buf_tail = DCCPAV_MAX_ACKVEC_LEN - 1; 33 INIT_LIST_HEAD(&avr->avr_node);
25 INIT_LIST_HEAD(&av->av_records); 34
26 } 35 return avr;
27 return av;
28} 36}
29 37
30static void dccp_ackvec_purge_records(struct dccp_ackvec *av) 38static void dccp_ackvec_record_delete(struct dccp_ackvec_record *avr)
31{ 39{
32 struct dccp_ackvec_record *cur, *next; 40 if (unlikely(avr == NULL))
33 41 return;
34 list_for_each_entry_safe(cur, next, &av->av_records, avr_node) 42 /* Check if deleting a linked record */
35 kmem_cache_free(dccp_ackvec_record_slab, cur); 43 WARN_ON(!list_empty(&avr->avr_node));
36 INIT_LIST_HEAD(&av->av_records); 44 kmem_cache_free(dccp_ackvec_record_slab, avr);
37} 45}
38 46
39void dccp_ackvec_free(struct dccp_ackvec *av) 47static void dccp_ackvec_insert_avr(struct dccp_ackvec *av,
48 struct dccp_ackvec_record *avr)
40{ 49{
41 if (likely(av != NULL)) { 50 /*
42 dccp_ackvec_purge_records(av); 51 * AVRs are sorted by seqno. Since we are sending them in order, we
43 kmem_cache_free(dccp_ackvec_slab, av); 52 * just add the AVR at the head of the list.
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));
44 } 61 }
62
63 list_add(&avr->avr_node, &av->av_records);
45} 64}
46 65
47/** 66int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
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)
54{ 67{
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;
55 struct dccp_ackvec_record *avr; 76 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;
56 84
57 avr = kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC); 85 if (elapsed_time != 0 &&
86 dccp_insert_option_elapsed_time(sk, skb, elapsed_time))
87 return -1;
88
89 avr = dccp_ackvec_record_new();
58 if (avr == NULL) 90 if (avr == NULL)
59 return -ENOBUFS; 91 return -1;
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 }
60 125
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);
66 /*
67 * When the buffer overflows, we keep no more than one record. This is
68 * the simplest way of disambiguating sender-Acks dating from before the
69 * overflow from sender-Acks which refer to after the overflow; a simple
70 * solution is preferable here since we are handling an exception.
71 */
72 if (av->av_overflow)
73 dccp_ackvec_purge_records(av);
74 /* 126 /*
75 * Since GSS is incremented for each packet, the list is automatically 127 * From RFC 4340, A.2:
76 * arranged in descending order of @ack_seqno. 128 *
129 * For each acknowledgement it sends, the HC-Receiver will add an
130 * acknowledgement record. ack_seqno will equal the HC-Receiver
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.
77 */ 134 */
78 list_add(&avr->avr_node, &av->av_records); 135 avr->avr_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
136 avr->avr_ack_ptr = av->av_buf_head;
137 avr->avr_ack_ackno = av->av_buf_ackno;
138 avr->avr_ack_nonce = av->av_buf_nonce;
139 avr->avr_sent_len = av->av_vec_len;
79 140
80 dccp_pr_debug("Added Vector, ack_seqno=%llu, ack_ackno=%llu (rl=%u)\n", 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_sent_len,
81 (unsigned long long)avr->avr_ack_seqno, 146 (unsigned long long)avr->avr_ack_seqno,
82 (unsigned long long)avr->avr_ack_ackno, 147 (unsigned long long)avr->avr_ack_ackno);
83 avr->avr_ack_runlen);
84 return 0; 148 return 0;
85} 149}
86 150
87static struct dccp_ackvec_record *dccp_ackvec_lookup(struct list_head *av_list, 151struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
88 const u64 ackno)
89{ 152{
90 struct dccp_ackvec_record *avr; 153 struct dccp_ackvec *av = kmem_cache_alloc(dccp_ackvec_slab, priority);
91 /* 154
92 * Exploit that records are inserted in descending order of sequence 155 if (av != NULL) {
93 * number, start with the oldest record first. If @ackno is `before' 156 av->av_buf_head = DCCP_MAX_ACKVEC_LEN - 1;
94 * the earliest ack_ackno, the packet is too old to be considered. 157 av->av_buf_ackno = UINT48_MAX + 1;
95 */ 158 av->av_buf_nonce = 0;
96 list_for_each_entry_reverse(avr, av_list, avr_node) { 159 av->av_time = ktime_set(0, 0);
97 if (avr->avr_ack_seqno == ackno) 160 av->av_vec_len = 0;
98 return avr; 161 INIT_LIST_HEAD(&av->av_records);
99 if (before48(ackno, avr->avr_ack_seqno))
100 break;
101 } 162 }
102 return NULL; 163
164 return av;
103} 165}
104 166
105/* 167void dccp_ackvec_free(struct dccp_ackvec *av)
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)
110{ 168{
111 return (a + b) % DCCPAV_MAX_ACKVEC_LEN; 169 if (unlikely(av == NULL))
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);
112} 182}
113 183
114static inline u16 __ackvec_idx_sub(const u16 a, const u16 b) 184static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
185 const u32 index)
115{ 186{
116 return __ackvec_idx_add(a, DCCPAV_MAX_ACKVEC_LEN - b); 187 return av->av_buf[index] & DCCP_ACKVEC_STATE_MASK;
117} 188}
118 189
119u16 dccp_ackvec_buflen(const struct dccp_ackvec *av) 190static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
191 const u32 index)
120{ 192{
121 if (unlikely(av->av_overflow)) 193 return av->av_buf[index] & DCCP_ACKVEC_LEN_MASK;
122 return DCCPAV_MAX_ACKVEC_LEN;
123 return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head);
124} 194}
125 195
126/** 196/*
127 * dccp_ackvec_update_old - Update previous state as per RFC 4340, 11.4.1 197 * If several packets are missing, the HC-Receiver may prefer to enter multiple
128 * @av: non-empty buffer to update 198 * bytes with run length 0, rather than a single byte with a larger run length;
129 * @distance: negative or zero distance of @seqno from buf_ackno downward 199 * this simplifies table updates if one of the missing packets arrives.
130 * @seqno: the (old) sequence number whose record is to be updated
131 * @state: state in which packet carrying @seqno was received
132 */ 200 */
133static void dccp_ackvec_update_old(struct dccp_ackvec *av, s64 distance, 201static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
134 u64 seqno, enum dccp_ackvec_states state) 202 const unsigned int packets,
203 const unsigned char state)
135{ 204{
136 u16 ptr = av->av_buf_head; 205 unsigned int gap;
206 long new_head;
137 207
138 BUG_ON(distance > 0); 208 if (av->av_vec_len + packets > DCCP_MAX_ACKVEC_LEN)
139 if (unlikely(dccp_ackvec_is_empty(av))) 209 return -ENOBUFS;
140 return;
141 210
142 do { 211 gap = packets - 1;
143 u8 runlen = dccp_ackvec_runlen(av->av_buf + ptr); 212 new_head = av->av_buf_head - packets;
144 213
145 if (distance + runlen >= 0) { 214 if (new_head < 0) {
146 /* 215 if (gap > 0) {
147 * Only update the state if packet has not been received 216 memset(av->av_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
148 * yet. This is OK as per the second table in RFC 4340, 217 gap + new_head + 1);
149 * 11.4.1; i.e. here we are using the following table: 218 gap = -new_head;
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;
167 } 219 }
220 new_head += DCCP_MAX_ACKVEC_LEN;
221 }
168 222
169 distance += runlen + 1; 223 av->av_buf_head = new_head;
170 ptr = __ackvec_idx_add(ptr, 1);
171 224
172 } while (ptr != av->av_buf_tail); 225 if (gap > 0)
173} 226 memset(av->av_buf + av->av_buf_head + 1,
227 DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
174 228
175/* Mark @num entries after buf_head as "Not yet received". */ 229 av->av_buf[av->av_buf_head] = state;
176static void dccp_ackvec_reserve_seats(struct dccp_ackvec *av, u16 num) 230 av->av_vec_len += packets;
177{ 231 return 0;
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);
189} 232}
190 233
191/** 234/*
192 * dccp_ackvec_add_new - Record one or more new entries in Ack Vector buffer 235 * Implements the RFC 4340, Appendix A
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
197 */ 236 */
198static void dccp_ackvec_add_new(struct dccp_ackvec *av, u32 num_packets, 237int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
199 u64 seqno, enum dccp_ackvec_states state) 238 const u64 ackno, const u8 state)
200{ 239{
201 u32 num_cells = num_packets; 240 /*
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 */
202 261
203 if (num_packets > DCCPAV_BURST_THRESH) { 262 /* See if this is the first ackno being inserted */
204 u32 lost_packets = num_packets - 1; 263 if (av->av_vec_len == 0) {
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);
205 268
206 DCCP_WARN("Warning: large burst loss (%u)\n", lost_packets);
207 /* 269 /*
208 * We received 1 packet and have a loss of size "num_packets-1" 270 * Look if the state of this packet is the same as the
209 * which we squeeze into num_cells-1 rather than reserving an 271 * previous ackno and if so if we can bump the head len.
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.
218 */ 272 */
219 for (num_packets = num_cells = 1; lost_packets; ++num_cells) { 273 if (delta == 1 &&
220 u8 len = min(lost_packets, (u32)DCCPAV_MAX_RUNLEN); 274 dccp_ackvec_state(av, av->av_buf_head) == state &&
221 275 dccp_ackvec_len(av, av->av_buf_head) < DCCP_ACKVEC_LEN_MASK)
222 av->av_buf_head = __ackvec_idx_sub(av->av_buf_head, 1); 276 av->av_buf[av->av_buf_head]++;
223 av->av_buf[av->av_buf_head] = DCCPAV_NOT_RECEIVED | len; 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;
224 290
225 lost_packets -= len; 291 while (1) {
292 const u8 len = dccp_ackvec_len(av, index);
293 const u8 av_state = dccp_ackvec_state(av, index);
294 /*
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;
226 } 313 }
227 } 314 }
228 315
229 if (num_cells + dccp_ackvec_buflen(av) >= DCCPAV_MAX_ACKVEC_LEN) { 316 av->av_buf_ackno = ackno;
230 DCCP_CRIT("Ack Vector buffer overflow: dropping old entries\n"); 317 av->av_time = ktime_get_real();
231 av->av_overflow = true; 318out:
232 } 319 return 0;
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;
240 320
241 if (num_packets > 1) 321out_duplicate:
242 dccp_ackvec_reserve_seats(av, num_packets - 1); 322 /* Duplicate packet */
323 dccp_pr_debug("Received a dup or already considered lost "
324 "packet: %llu\n", (unsigned long long)ackno);
325 return -EILSEQ;
243} 326}
244 327
245/** 328static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
246 * dccp_ackvec_input - Register incoming packet in the buffer 329 struct dccp_ackvec_record *avr)
247 */
248void dccp_ackvec_input(struct dccp_ackvec *av, struct sk_buff *skb)
249{ 330{
250 u64 seqno = DCCP_SKB_CB(skb)->dccpd_seq; 331 struct dccp_ackvec_record *next;
251 enum dccp_ackvec_states state = DCCPAV_RECEIVED;
252 332
253 if (dccp_ackvec_is_empty(av)) { 333 /* sort out vector length */
254 dccp_ackvec_add_new(av, 1, seqno, state); 334 if (av->av_buf_head <= avr->avr_ack_ptr)
255 av->av_tail_ackno = seqno; 335 av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head;
336 else
337 av->av_vec_len = DCCP_MAX_ACKVEC_LEN - 1 -
338 av->av_buf_head + avr->avr_ack_ptr;
256 339
257 } else { 340 /* free records */
258 s64 num_packets = dccp_delta_seqno(av->av_buf_ackno, seqno); 341 list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
259 u8 *current_head = av->av_buf + av->av_buf_head; 342 list_del_init(&avr->avr_node);
260 343 dccp_ackvec_record_delete(avr);
261 if (num_packets == 1 && 344 }
262 dccp_ackvec_state(current_head) == state && 345}
263 dccp_ackvec_runlen(current_head) < DCCPAV_MAX_RUNLEN) {
264 346
265 *current_head += 1; 347void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
266 av->av_buf_ackno = seqno; 348 const u64 ackno)
349{
350 struct dccp_ackvec_record *avr;
267 351
268 } else if (num_packets > 0) { 352 /*
269 dccp_ackvec_add_new(av, num_packets, seqno, state); 353 * If we traverse backwards, it should be faster when we have large
270 } else { 354 * windows. We will be receiving ACKs for stuff we sent a while back
271 dccp_ackvec_update_old(av, num_packets, seqno, state); 355 * -sorbo.
272 } 356 */
357 list_for_each_entry_reverse(avr, &av->av_records, avr_node) {
358 if (ackno == avr->avr_ack_seqno) {
359 dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
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 */
273 } 368 }
274} 369}
275 370
276/** 371static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
277 * dccp_ackvec_clear_state - Perform house-keeping / garbage-collection 372 struct sock *sk, u64 *ackno,
278 * This routine is called when the peer acknowledges the receipt of Ack Vectors 373 const unsigned char len,
279 * up to and including @ackno. While based on on section A.3 of RFC 4340, here 374 const unsigned char *vector)
280 * are additional precautions to prevent corrupted buffer state. In particular, 375{
281 * we use tail_ackno to identify outdated records; it always marks the earliest 376 unsigned char i;
282 * packet of group (2) in 11.4.2. 377 struct dccp_ackvec_record *avr;
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;
289 378
290 avr = dccp_ackvec_lookup(&av->av_records, ackno); 379 /* Check if we actually sent an ACK vector */
291 if (avr == NULL) 380 if (list_empty(&av->av_records))
292 return; 381 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;
306 382
307 runlen_now = dccp_ackvec_runlen(av->av_buf + avr->avr_ack_ptr); 383 i = len;
308 /* 384 /*
309 * The run length of Ack Vector cells does not decrease over time. If 385 * XXX
310 * the run length is the same as at the time the Ack Vector was sent, we 386 * I think it might be more efficient to work backwards. See comment on
311 * free the ack_ptr cell. That cell can however not be freed if the run 387 * rcv_ackno. -sorbo.
312 * length has increased: in this case we need to move the tail pointer
313 * backwards (towards higher indices), to its next-oldest neighbour.
314 */ 388 */
315 if (runlen_now > eff_runlen) { 389 avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node);
390 while (i--) {
391 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
392 u64 ackno_end_rl;
316 393
317 av->av_buf[avr->avr_ack_ptr] -= eff_runlen + 1; 394 dccp_set_seqno(&ackno_end_rl, *ackno - rl);
318 av->av_buf_tail = __ackvec_idx_add(avr->avr_ack_ptr, 1);
319 395
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;
325 /* 396 /*
326 * We have made sure that avr points to a valid cell within the 397 * If our AVR sequence number is greater than the ack, go
327 * buffer. This cell is either older than head, or equals head 398 * forward in the AVR list until it is not so.
328 * (empty buffer): in both cases we no longer have any overflow.
329 */ 399 */
330 av->av_overflow = 0; 400 list_for_each_entry_from(avr, &av->av_records, avr_node) {
331 } 401 if (!after48(avr->avr_ack_seqno, *ackno))
332 402 goto found;
333 /* 403 }
334 * The peer has acknowledged up to and including ack_ackno. Hence the 404 /* End of the av_records list, not found, exit */
335 * first packet in group (2) of 11.4.2 is the successor of ack_ackno. 405 break;
336 */ 406found:
337 av->av_tail_ackno = ADD48(avr->avr_ack_ackno, 1); 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 }
338 426
339free_records: 427 dccp_set_seqno(ackno, ackno_end_rl - 1);
340 list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) { 428 ++vector;
341 list_del(&avr->avr_node);
342 kmem_cache_free(dccp_ackvec_record_slab, avr);
343 } 429 }
344} 430}
345 431
346/* 432int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
347 * Routines to keep track of Ack Vectors received in an skb 433 u64 *ackno, const u8 opt, const u8 *value, const u8 len)
348 */
349int dccp_ackvec_parsed_add(struct list_head *head, u8 *vec, u8 len, u8 nonce)
350{ 434{
351 struct dccp_ackvec_parsed *new = kmalloc(sizeof(*new), GFP_ATOMIC); 435 if (len > DCCP_MAX_ACKVEC_OPT_LEN)
352 436 return -1;
353 if (new == NULL)
354 return -ENOBUFS;
355 new->vec = vec;
356 new->len = len;
357 new->nonce = nonce;
358 437
359 list_add_tail(&new->node, head); 438 /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
439 dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
440 ackno, len, value);
360 return 0; 441 return 0;
361} 442}
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);
373 443
374int __init dccp_ackvec_init(void) 444int __init dccp_ackvec_init(void)
375{ 445{
@@ -379,9 +449,10 @@ int __init dccp_ackvec_init(void)
379 if (dccp_ackvec_slab == NULL) 449 if (dccp_ackvec_slab == NULL)
380 goto out_err; 450 goto out_err;
381 451
382 dccp_ackvec_record_slab = kmem_cache_create("dccp_ackvec_record", 452 dccp_ackvec_record_slab =
383 sizeof(struct dccp_ackvec_record), 453 kmem_cache_create("dccp_ackvec_record",
384 0, SLAB_HWCACHE_ALIGN, NULL); 454 sizeof(struct dccp_ackvec_record),
455 0, SLAB_HWCACHE_ALIGN, NULL);
385 if (dccp_ackvec_record_slab == NULL) 456 if (dccp_ackvec_record_slab == NULL)
386 goto out_destroy_slab; 457 goto out_destroy_slab;
387 458