4 * An implementation of the DCCP protocol
5 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
7 * 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 * Free Software Foundation; version 2 of the License;
15 #include <linux/dccp.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/skbuff.h>
20 #include <linux/slab.h>
24 static struct kmem_cache
*dccp_ackvec_slab
;
25 static struct kmem_cache
*dccp_ackvec_record_slab
;
27 static struct dccp_ackvec_record
*dccp_ackvec_record_new(void)
29 struct dccp_ackvec_record
*avr
=
30 kmem_cache_alloc(dccp_ackvec_record_slab
, GFP_ATOMIC
);
33 INIT_LIST_HEAD(&avr
->dccpavr_node
);
38 static void dccp_ackvec_record_delete(struct dccp_ackvec_record
*avr
)
40 if (unlikely(avr
== NULL
))
42 /* Check if deleting a linked record */
43 WARN_ON(!list_empty(&avr
->dccpavr_node
));
44 kmem_cache_free(dccp_ackvec_record_slab
, avr
);
47 static void dccp_ackvec_insert_avr(struct dccp_ackvec
*av
,
48 struct dccp_ackvec_record
*avr
)
51 * AVRs are sorted by seqno. Since we are sending them in order, we
52 * just add the AVR at the head of the list.
55 if (!list_empty(&av
->dccpav_records
)) {
56 const struct dccp_ackvec_record
*head
=
57 list_entry(av
->dccpav_records
.next
,
58 struct dccp_ackvec_record
,
60 BUG_ON(before48(avr
->dccpavr_ack_seqno
,
61 head
->dccpavr_ack_seqno
));
64 list_add(&avr
->dccpavr_node
, &av
->dccpav_records
);
67 int dccp_insert_option_ackvec(struct sock
*sk
, struct sk_buff
*skb
)
69 struct dccp_sock
*dp
= dccp_sk(sk
);
70 struct dccp_ackvec
*av
= dp
->dccps_hc_rx_ackvec
;
71 /* Figure out how many options do we need to represent the ackvec */
72 const u16 nr_opts
= (av
->dccpav_vec_len
+
73 DCCP_MAX_ACKVEC_OPT_LEN
- 1) /
74 DCCP_MAX_ACKVEC_OPT_LEN
;
75 u16 len
= av
->dccpav_vec_len
+ 2 * nr_opts
, i
;
78 const unsigned char *tail
, *from
;
80 struct dccp_ackvec_record
*avr
;
82 if (DCCP_SKB_CB(skb
)->dccpd_opt_len
+ len
> DCCP_MAX_OPT_LEN
)
85 dccp_timestamp(sk
, &now
);
86 elapsed_time
= timeval_delta(&now
, &av
->dccpav_time
) / 10;
88 if (elapsed_time
!= 0 &&
89 dccp_insert_option_elapsed_time(sk
, skb
, elapsed_time
))
92 avr
= dccp_ackvec_record_new();
96 DCCP_SKB_CB(skb
)->dccpd_opt_len
+= len
;
98 to
= skb_push(skb
, len
);
99 len
= av
->dccpav_vec_len
;
100 from
= av
->dccpav_buf
+ av
->dccpav_buf_head
;
101 tail
= av
->dccpav_buf
+ DCCP_MAX_ACKVEC_LEN
;
103 for (i
= 0; i
< nr_opts
; ++i
) {
106 if (len
> DCCP_MAX_ACKVEC_OPT_LEN
)
107 copylen
= DCCP_MAX_ACKVEC_OPT_LEN
;
109 *to
++ = DCCPO_ACK_VECTOR_0
;
112 /* Check if buf_head wraps */
113 if (from
+ copylen
> tail
) {
114 const u16 tailsize
= tail
- from
;
116 memcpy(to
, from
, tailsize
);
120 from
= av
->dccpav_buf
;
123 memcpy(to
, from
, copylen
);
130 * From RFC 4340, A.2:
132 * For each acknowledgement it sends, the HC-Receiver will add an
133 * acknowledgement record. ack_seqno will equal the HC-Receiver
134 * sequence number it used for the ack packet; ack_ptr will equal
135 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
138 avr
->dccpavr_ack_seqno
= DCCP_SKB_CB(skb
)->dccpd_seq
;
139 avr
->dccpavr_ack_ptr
= av
->dccpav_buf_head
;
140 avr
->dccpavr_ack_ackno
= av
->dccpav_buf_ackno
;
141 avr
->dccpavr_ack_nonce
= av
->dccpav_buf_nonce
;
142 avr
->dccpavr_sent_len
= av
->dccpav_vec_len
;
144 dccp_ackvec_insert_avr(av
, avr
);
146 dccp_pr_debug("%s ACK Vector 0, len=%d, ack_seqno=%llu, "
148 dccp_role(sk
), avr
->dccpavr_sent_len
,
149 (unsigned long long)avr
->dccpavr_ack_seqno
,
150 (unsigned long long)avr
->dccpavr_ack_ackno
);
154 struct dccp_ackvec
*dccp_ackvec_alloc(const gfp_t priority
)
156 struct dccp_ackvec
*av
= kmem_cache_alloc(dccp_ackvec_slab
, priority
);
159 av
->dccpav_buf_head
= DCCP_MAX_ACKVEC_LEN
- 1;
160 av
->dccpav_buf_ackno
= UINT48_MAX
+ 1;
161 av
->dccpav_buf_nonce
= av
->dccpav_buf_nonce
= 0;
162 av
->dccpav_time
.tv_sec
= 0;
163 av
->dccpav_time
.tv_usec
= 0;
164 av
->dccpav_vec_len
= 0;
165 INIT_LIST_HEAD(&av
->dccpav_records
);
171 void dccp_ackvec_free(struct dccp_ackvec
*av
)
173 if (unlikely(av
== NULL
))
176 if (!list_empty(&av
->dccpav_records
)) {
177 struct dccp_ackvec_record
*avr
, *next
;
179 list_for_each_entry_safe(avr
, next
, &av
->dccpav_records
,
181 list_del_init(&avr
->dccpavr_node
);
182 dccp_ackvec_record_delete(avr
);
186 kmem_cache_free(dccp_ackvec_slab
, av
);
189 static inline u8
dccp_ackvec_state(const struct dccp_ackvec
*av
,
192 return av
->dccpav_buf
[index
] & DCCP_ACKVEC_STATE_MASK
;
195 static inline u8
dccp_ackvec_len(const struct dccp_ackvec
*av
,
198 return av
->dccpav_buf
[index
] & DCCP_ACKVEC_LEN_MASK
;
202 * If several packets are missing, the HC-Receiver may prefer to enter multiple
203 * bytes with run length 0, rather than a single byte with a larger run length;
204 * this simplifies table updates if one of the missing packets arrives.
206 static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec
*av
,
207 const unsigned int packets
,
208 const unsigned char state
)
213 if (av
->dccpav_vec_len
+ packets
> DCCP_MAX_ACKVEC_LEN
)
217 new_head
= av
->dccpav_buf_head
- packets
;
221 memset(av
->dccpav_buf
, DCCP_ACKVEC_STATE_NOT_RECEIVED
,
225 new_head
+= DCCP_MAX_ACKVEC_LEN
;
228 av
->dccpav_buf_head
= new_head
;
231 memset(av
->dccpav_buf
+ av
->dccpav_buf_head
+ 1,
232 DCCP_ACKVEC_STATE_NOT_RECEIVED
, gap
);
234 av
->dccpav_buf
[av
->dccpav_buf_head
] = state
;
235 av
->dccpav_vec_len
+= packets
;
240 * Implements the RFC 4340, Appendix A
242 int dccp_ackvec_add(struct dccp_ackvec
*av
, const struct sock
*sk
,
243 const u64 ackno
, const u8 state
)
246 * Check at the right places if the buffer is full, if it is, tell the
247 * caller to start dropping packets till the HC-Sender acks our ACK
248 * vectors, when we will free up space in dccpav_buf.
250 * We may well decide to do buffer compression, etc, but for now lets
253 * From Appendix A.1.1 (`New Packets'):
255 * Of course, the circular buffer may overflow, either when the
256 * HC-Sender is sending data at a very high rate, when the
257 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
258 * or when the HC-Sender is forgetting to acknowledge those acks
259 * (so the HC-Receiver is unable to clean up old state). In this
260 * case, the HC-Receiver should either compress the buffer (by
261 * increasing run lengths when possible), transfer its state to
262 * a larger buffer, or, as a last resort, drop all received
263 * packets, without processing them whatsoever, until its buffer
267 /* See if this is the first ackno being inserted */
268 if (av
->dccpav_vec_len
== 0) {
269 av
->dccpav_buf
[av
->dccpav_buf_head
] = state
;
270 av
->dccpav_vec_len
= 1;
271 } else if (after48(ackno
, av
->dccpav_buf_ackno
)) {
272 const u64 delta
= dccp_delta_seqno(av
->dccpav_buf_ackno
,
276 * Look if the state of this packet is the same as the
277 * previous ackno and if so if we can bump the head len.
280 dccp_ackvec_state(av
, av
->dccpav_buf_head
) == state
&&
281 (dccp_ackvec_len(av
, av
->dccpav_buf_head
) <
282 DCCP_ACKVEC_LEN_MASK
))
283 av
->dccpav_buf
[av
->dccpav_buf_head
]++;
284 else if (dccp_ackvec_set_buf_head_state(av
, delta
, state
))
290 * When a packet with Sequence Number S <= buf_ackno
291 * arrives, the HC-Receiver will scan the table for
292 * the byte corresponding to S. (Indexing structures
293 * could reduce the complexity of this scan.)
295 u64 delta
= dccp_delta_seqno(ackno
, av
->dccpav_buf_ackno
);
296 u32 index
= av
->dccpav_buf_head
;
299 const u8 len
= dccp_ackvec_len(av
, index
);
300 const u8 state
= dccp_ackvec_state(av
, index
);
302 * valid packets not yet in dccpav_buf have a reserved
303 * entry, with a len equal to 0.
305 if (state
== DCCP_ACKVEC_STATE_NOT_RECEIVED
&&
306 len
== 0 && delta
== 0) { /* Found our
308 dccp_pr_debug("Found %llu reserved seat!\n",
309 (unsigned long long)ackno
);
310 av
->dccpav_buf
[index
] = state
;
313 /* len == 0 means one packet */
318 if (++index
== DCCP_MAX_ACKVEC_LEN
)
323 av
->dccpav_buf_ackno
= ackno
;
324 dccp_timestamp(sk
, &av
->dccpav_time
);
329 /* Duplicate packet */
330 dccp_pr_debug("Received a dup or already considered lost "
331 "packet: %llu\n", (unsigned long long)ackno
);
335 #ifdef CONFIG_IP_DCCP_DEBUG
336 void dccp_ackvector_print(const u64 ackno
, const unsigned char *vector
, int len
)
338 dccp_pr_debug_cat("ACK vector len=%d, ackno=%llu |", len
,
339 (unsigned long long)ackno
);
342 const u8 state
= (*vector
& DCCP_ACKVEC_STATE_MASK
) >> 6;
343 const u8 rl
= *vector
& DCCP_ACKVEC_LEN_MASK
;
345 dccp_pr_debug_cat("%d,%d|", state
, rl
);
349 dccp_pr_debug_cat("\n");
352 void dccp_ackvec_print(const struct dccp_ackvec
*av
)
354 dccp_ackvector_print(av
->dccpav_buf_ackno
,
355 av
->dccpav_buf
+ av
->dccpav_buf_head
,
360 static void dccp_ackvec_throw_record(struct dccp_ackvec
*av
,
361 struct dccp_ackvec_record
*avr
)
363 struct dccp_ackvec_record
*next
;
365 /* sort out vector length */
366 if (av
->dccpav_buf_head
<= avr
->dccpavr_ack_ptr
)
367 av
->dccpav_vec_len
= avr
->dccpavr_ack_ptr
- av
->dccpav_buf_head
;
369 av
->dccpav_vec_len
= DCCP_MAX_ACKVEC_LEN
- 1
370 - av
->dccpav_buf_head
371 + avr
->dccpavr_ack_ptr
;
374 list_for_each_entry_safe_from(avr
, next
, &av
->dccpav_records
,
376 list_del_init(&avr
->dccpavr_node
);
377 dccp_ackvec_record_delete(avr
);
381 void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec
*av
, struct sock
*sk
,
384 struct dccp_ackvec_record
*avr
;
387 * If we traverse backwards, it should be faster when we have large
388 * windows. We will be receiving ACKs for stuff we sent a while back
391 list_for_each_entry_reverse(avr
, &av
->dccpav_records
, dccpavr_node
) {
392 if (ackno
== avr
->dccpavr_ack_seqno
) {
393 dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
394 "ack_ackno=%llu, ACKED!\n",
396 (unsigned long long)avr
->dccpavr_ack_seqno
,
397 (unsigned long long)avr
->dccpavr_ack_ackno
);
398 dccp_ackvec_throw_record(av
, avr
);
400 } else if (avr
->dccpavr_ack_seqno
> ackno
)
401 break; /* old news */
405 static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec
*av
,
406 struct sock
*sk
, u64
*ackno
,
407 const unsigned char len
,
408 const unsigned char *vector
)
411 struct dccp_ackvec_record
*avr
;
413 /* Check if we actually sent an ACK vector */
414 if (list_empty(&av
->dccpav_records
))
420 * I think it might be more efficient to work backwards. See comment on
423 avr
= list_entry(av
->dccpav_records
.next
, struct dccp_ackvec_record
,
426 const u8 rl
= *vector
& DCCP_ACKVEC_LEN_MASK
;
429 dccp_set_seqno(&ackno_end_rl
, *ackno
- rl
);
432 * If our AVR sequence number is greater than the ack, go
433 * forward in the AVR list until it is not so.
435 list_for_each_entry_from(avr
, &av
->dccpav_records
,
437 if (!after48(avr
->dccpavr_ack_seqno
, *ackno
))
440 /* End of the dccpav_records list, not found, exit */
443 if (between48(avr
->dccpavr_ack_seqno
, ackno_end_rl
, *ackno
)) {
444 const u8 state
= *vector
& DCCP_ACKVEC_STATE_MASK
;
445 if (state
!= DCCP_ACKVEC_STATE_NOT_RECEIVED
) {
446 dccp_pr_debug("%s ACK vector 0, len=%d, "
447 "ack_seqno=%llu, ack_ackno=%llu, "
451 avr
->dccpavr_ack_seqno
,
453 avr
->dccpavr_ack_ackno
);
454 dccp_ackvec_throw_record(av
, avr
);
458 * If it wasn't received, continue scanning... we might
463 dccp_set_seqno(ackno
, ackno_end_rl
- 1);
468 int dccp_ackvec_parse(struct sock
*sk
, const struct sk_buff
*skb
,
469 u64
*ackno
, const u8 opt
, const u8
*value
, const u8 len
)
471 if (len
> DCCP_MAX_ACKVEC_OPT_LEN
)
474 /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
475 dccp_ackvec_check_rcv_ackvector(dccp_sk(sk
)->dccps_hc_rx_ackvec
, sk
,
480 int __init
dccp_ackvec_init(void)
482 dccp_ackvec_slab
= kmem_cache_create("dccp_ackvec",
483 sizeof(struct dccp_ackvec
), 0,
484 SLAB_HWCACHE_ALIGN
, NULL
);
485 if (dccp_ackvec_slab
== NULL
)
488 dccp_ackvec_record_slab
=
489 kmem_cache_create("dccp_ackvec_record",
490 sizeof(struct dccp_ackvec_record
),
491 0, SLAB_HWCACHE_ALIGN
, NULL
);
492 if (dccp_ackvec_record_slab
== NULL
)
493 goto out_destroy_slab
;
498 kmem_cache_destroy(dccp_ackvec_slab
);
499 dccp_ackvec_slab
= NULL
;
501 DCCP_CRIT("Unable to create Ack Vector slab cache");
505 void dccp_ackvec_exit(void)
507 if (dccp_ackvec_slab
!= NULL
) {
508 kmem_cache_destroy(dccp_ackvec_slab
);
509 dccp_ackvec_slab
= NULL
;
511 if (dccp_ackvec_record_slab
!= NULL
) {
512 kmem_cache_destroy(dccp_ackvec_record_slab
);
513 dccp_ackvec_record_slab
= NULL
;