[DCCP] ackvec: Introduce ack vector records
[safe/jmp/linux-2.6] / net / dccp / ackvec.c
1 /*
2  *  net/dccp/ackvec.c
3  *
4  *  An implementation of the DCCP protocol
5  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
6  *
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;
10  */
11
12 #include "ackvec.h"
13 #include "dccp.h"
14
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>
21
22 #include <net/sock.h>
23
24 static kmem_cache_t *dccp_ackvec_slab;
25 static kmem_cache_t *dccp_ackvec_record_slab;
26
27 static struct dccp_ackvec_record *dccp_ackvec_record_new(void)
28 {
29         struct dccp_ackvec_record *avr =
30                         kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
31
32         if (avr != NULL)
33                 INIT_LIST_HEAD(&avr->dccpavr_node);
34
35         return avr;
36 }
37
38 static void dccp_ackvec_record_delete(struct dccp_ackvec_record *avr)
39 {
40         if (unlikely(avr == NULL))
41                 return;
42         /* Check if deleting a linked record */
43         WARN_ON(!list_empty(&avr->dccpavr_node));
44         kmem_cache_free(dccp_ackvec_record_slab, avr);
45 }
46
47 static void dccp_ackvec_insert_avr(struct dccp_ackvec *av,
48                                    struct dccp_ackvec_record *avr)
49 {
50         /*
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.
53          * -sorbo.
54          */
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,
59                                                    dccpavr_node);
60                 BUG_ON(before48(avr->dccpavr_ack_seqno,
61                                 head->dccpavr_ack_seqno));
62         }
63
64         list_add(&avr->dccpavr_node, &av->dccpav_records);
65 }
66
67 int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
68 {
69         struct dccp_sock *dp = dccp_sk(sk);
70 #ifdef CONFIG_IP_DCCP_DEBUG
71         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
72                                 "CLIENT tx: " : "server tx: ";
73 #endif
74         struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
75         int len = av->dccpav_vec_len + 2;
76         struct timeval now;
77         u32 elapsed_time;
78         unsigned char *to, *from;
79         struct dccp_ackvec_record *avr;
80
81         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
82                 return -1;
83
84         avr = dccp_ackvec_record_new();
85         if (avr == NULL)
86                 return -1;
87
88         dccp_timestamp(sk, &now);
89         elapsed_time = timeval_delta(&now, &av->dccpav_time) / 10;
90
91         if (elapsed_time != 0)
92                 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
93
94         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
95
96         to    = skb_push(skb, len);
97         *to++ = DCCPO_ACK_VECTOR_0;
98         *to++ = len;
99
100         len  = av->dccpav_vec_len;
101         from = av->dccpav_buf + av->dccpav_buf_head;
102
103         /* Check if buf_head wraps */
104         if ((int)av->dccpav_buf_head + len > DCCP_MAX_ACKVEC_LEN) {
105                 const u32 tailsize = DCCP_MAX_ACKVEC_LEN - av->dccpav_buf_head;
106
107                 memcpy(to, from, tailsize);
108                 to   += tailsize;
109                 len  -= tailsize;
110                 from = av->dccpav_buf;
111         }
112
113         memcpy(to, from, len);
114         /*
115          *      From draft-ietf-dccp-spec-11.txt:
116          *
117          *      For each acknowledgement it sends, the HC-Receiver will add an
118          *      acknowledgement record.  ack_seqno will equal the HC-Receiver
119          *      sequence number it used for the ack packet; ack_ptr will equal
120          *      buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
121          *      equal buf_nonce.
122          */
123         avr->dccpavr_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
124         avr->dccpavr_ack_ptr   = av->dccpav_buf_head;
125         avr->dccpavr_ack_ackno = av->dccpav_buf_ackno;
126         avr->dccpavr_ack_nonce = av->dccpav_buf_nonce;
127         avr->dccpavr_sent_len  = av->dccpav_vec_len;
128
129         dccp_ackvec_insert_avr(av, avr);
130
131         dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
132                       "ack_ackno=%llu\n",
133                       debug_prefix, avr->dccpavr_sent_len,
134                       (unsigned long long)avr->dccpavr_ack_seqno,
135                       (unsigned long long)avr->dccpavr_ack_ackno);
136         return 0;
137 }
138
139 struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
140 {
141         struct dccp_ackvec *av = kmem_cache_alloc(dccp_ackvec_slab, priority);
142
143         if (av != NULL) {
144                 av->dccpav_buf_head     =
145                         av->dccpav_buf_tail = DCCP_MAX_ACKVEC_LEN - 1;
146                 av->dccpav_buf_ackno    = DCCP_MAX_SEQNO + 1;
147                 av->dccpav_buf_nonce = av->dccpav_buf_nonce = 0;
148                 av->dccpav_ack_ptr      = 0;
149                 av->dccpav_time.tv_sec  = 0;
150                 av->dccpav_time.tv_usec = 0;
151                 av->dccpav_sent_len     = av->dccpav_vec_len = 0;
152                 INIT_LIST_HEAD(&av->dccpav_records);
153         }
154
155         return av;
156 }
157
158 void dccp_ackvec_free(struct dccp_ackvec *av)
159 {
160         if (unlikely(av == NULL))
161                 return;
162         WARN_ON(!list_empty(&av->dccpav_records));
163         kmem_cache_free(dccp_ackvec_slab, av);
164 }
165
166 static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
167                                    const u8 index)
168 {
169         return av->dccpav_buf[index] & DCCP_ACKVEC_STATE_MASK;
170 }
171
172 static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
173                                  const u8 index)
174 {
175         return av->dccpav_buf[index] & DCCP_ACKVEC_LEN_MASK;
176 }
177
178 /*
179  * If several packets are missing, the HC-Receiver may prefer to enter multiple
180  * bytes with run length 0, rather than a single byte with a larger run length;
181  * this simplifies table updates if one of the missing packets arrives.
182  */
183 static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
184                                                  const unsigned int packets,
185                                                  const unsigned char state)
186 {
187         unsigned int gap;
188         long new_head;
189
190         if (av->dccpav_vec_len + packets > DCCP_MAX_ACKVEC_LEN)
191                 return -ENOBUFS;
192
193         gap      = packets - 1;
194         new_head = av->dccpav_buf_head - packets;
195
196         if (new_head < 0) {
197                 if (gap > 0) {
198                         memset(av->dccpav_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
199                                gap + new_head + 1);
200                         gap = -new_head;
201                 }
202                 new_head += DCCP_MAX_ACKVEC_LEN;
203         } 
204
205         av->dccpav_buf_head = new_head;
206
207         if (gap > 0)
208                 memset(av->dccpav_buf + av->dccpav_buf_head + 1,
209                        DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
210
211         av->dccpav_buf[av->dccpav_buf_head] = state;
212         av->dccpav_vec_len += packets;
213         return 0;
214 }
215
216 /*
217  * Implements the draft-ietf-dccp-spec-11.txt Appendix A
218  */
219 int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
220                     const u64 ackno, const u8 state)
221 {
222         /*
223          * Check at the right places if the buffer is full, if it is, tell the
224          * caller to start dropping packets till the HC-Sender acks our ACK
225          * vectors, when we will free up space in dccpav_buf.
226          *
227          * We may well decide to do buffer compression, etc, but for now lets
228          * just drop.
229          *
230          * From Appendix A:
231          *
232          *      Of course, the circular buffer may overflow, either when the
233          *      HC-Sender is sending data at a very high rate, when the
234          *      HC-Receiver's acknowledgements are not reaching the HC-Sender,
235          *      or when the HC-Sender is forgetting to acknowledge those acks
236          *      (so the HC-Receiver is unable to clean up old state). In this
237          *      case, the HC-Receiver should either compress the buffer (by
238          *      increasing run lengths when possible), transfer its state to
239          *      a larger buffer, or, as a last resort, drop all received
240          *      packets, without processing them whatsoever, until its buffer
241          *      shrinks again.
242          */
243
244         /* See if this is the first ackno being inserted */
245         if (av->dccpav_vec_len == 0) {
246                 av->dccpav_buf[av->dccpav_buf_head] = state;
247                 av->dccpav_vec_len = 1;
248         } else if (after48(ackno, av->dccpav_buf_ackno)) {
249                 const u64 delta = dccp_delta_seqno(av->dccpav_buf_ackno,
250                                                    ackno);
251
252                 /*
253                  * Look if the state of this packet is the same as the
254                  * previous ackno and if so if we can bump the head len.
255                  */
256                 if (delta == 1 &&
257                     dccp_ackvec_state(av, av->dccpav_buf_head) == state &&
258                     (dccp_ackvec_len(av, av->dccpav_buf_head) <
259                      DCCP_ACKVEC_LEN_MASK))
260                         av->dccpav_buf[av->dccpav_buf_head]++;
261                 else if (dccp_ackvec_set_buf_head_state(av, delta, state))
262                         return -ENOBUFS;
263         } else {
264                 /*
265                  * A.1.2.  Old Packets
266                  *
267                  *      When a packet with Sequence Number S arrives, and
268                  *      S <= buf_ackno, the HC-Receiver will scan the table
269                  *      for the byte corresponding to S. (Indexing structures
270                  *      could reduce the complexity of this scan.)
271                  */
272                 u64 delta = dccp_delta_seqno(ackno, av->dccpav_buf_ackno);
273                 u8 index = av->dccpav_buf_head;
274
275                 while (1) {
276                         const u8 len = dccp_ackvec_len(av, index);
277                         const u8 state = dccp_ackvec_state(av, index);
278                         /*
279                          * valid packets not yet in dccpav_buf have a reserved
280                          * entry, with a len equal to 0.
281                          */
282                         if (state == DCCP_ACKVEC_STATE_NOT_RECEIVED &&
283                             len == 0 && delta == 0) { /* Found our
284                                                          reserved seat! */
285                                 dccp_pr_debug("Found %llu reserved seat!\n",
286                                               (unsigned long long)ackno);
287                                 av->dccpav_buf[index] = state;
288                                 goto out;
289                         }
290                         /* len == 0 means one packet */
291                         if (delta < len + 1)
292                                 goto out_duplicate;
293
294                         delta -= len + 1;
295                         if (++index == DCCP_MAX_ACKVEC_LEN)
296                                 index = 0;
297                 }
298         }
299
300         av->dccpav_buf_ackno = ackno;
301         dccp_timestamp(sk, &av->dccpav_time);
302 out:
303         dccp_pr_debug("");
304         return 0;
305
306 out_duplicate:
307         /* Duplicate packet */
308         dccp_pr_debug("Received a dup or already considered lost "
309                       "packet: %llu\n", (unsigned long long)ackno);
310         return -EILSEQ;
311 }
312
313 #ifdef CONFIG_IP_DCCP_DEBUG
314 void dccp_ackvector_print(const u64 ackno, const unsigned char *vector, int len)
315 {
316         if (!dccp_debug)
317                 return;
318
319         printk("ACK vector len=%d, ackno=%llu |", len,
320                (unsigned long long)ackno);
321
322         while (len--) {
323                 const u8 state = (*vector & DCCP_ACKVEC_STATE_MASK) >> 6;
324                 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
325
326                 printk("%d,%d|", state, rl);
327                 ++vector;
328         }
329
330         printk("\n");
331 }
332
333 void dccp_ackvec_print(const struct dccp_ackvec *av)
334 {
335         dccp_ackvector_print(av->dccpav_buf_ackno,
336                              av->dccpav_buf + av->dccpav_buf_head,
337                              av->dccpav_vec_len);
338 }
339 #endif
340
341 static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
342                                      struct dccp_ackvec_record *avr)
343 {
344         struct dccp_ackvec_record *next;
345
346         av->dccpav_buf_tail = avr->dccpavr_ack_ptr - 1;
347         if (av->dccpav_buf_tail == 0)
348                 av->dccpav_buf_tail = DCCP_MAX_ACKVEC_LEN - 1;
349
350         av->dccpav_vec_len -= avr->dccpavr_sent_len;
351
352         /* free records */
353         list_for_each_entry_safe_from(avr, next, &av->dccpav_records,
354                                       dccpavr_node) {
355                 list_del_init(&avr->dccpavr_node);
356                 dccp_ackvec_record_delete(avr);
357         }
358 }
359
360 void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
361                                  const u64 ackno)
362 {
363         struct dccp_ackvec_record *avr;
364
365         /*
366          * If we traverse backwards, it should be faster when we have large
367          * windows. We will be receiving ACKs for stuff we sent a while back
368          * -sorbo.
369          */
370         list_for_each_entry_reverse(avr, &av->dccpav_records, dccpavr_node) {
371                 if (ackno == avr->dccpavr_ack_seqno) {
372 #ifdef CONFIG_IP_DCCP_DEBUG
373                         struct dccp_sock *dp = dccp_sk(sk);
374                         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
375                                                 "CLIENT rx ack: " : "server rx ack: ";
376 #endif
377                         dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
378                                       "ack_ackno=%llu, ACKED!\n",
379                                       debug_prefix, 1,
380                                       (unsigned long long)avr->dccpavr_ack_seqno,
381                                       (unsigned long long)avr->dccpavr_ack_ackno);
382                         dccp_ackvec_throw_record(av, avr);
383                         break;
384                 }
385         }
386 }
387
388 static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
389                                             struct sock *sk, u64 ackno,
390                                             const unsigned char len,
391                                             const unsigned char *vector)
392 {
393         unsigned char i;
394         struct dccp_ackvec_record *avr;
395
396         /* Check if we actually sent an ACK vector */
397         if (list_empty(&av->dccpav_records))
398                 return;
399
400         i = len;
401         /*
402          * XXX
403          * I think it might be more efficient to work backwards. See comment on
404          * rcv_ackno. -sorbo.
405          */
406         avr = list_entry(av->dccpav_records.next, struct dccp_ackvec_record,
407                          dccpavr_node);
408         while (i--) {
409                 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
410                 u64 ackno_end_rl;
411
412                 dccp_set_seqno(&ackno_end_rl, ackno - rl);
413
414                 /*
415                  * If our AVR sequence number is greater than the ack, go
416                  * forward in the AVR list until it is not so.
417                  */
418                 list_for_each_entry_from(avr, &av->dccpav_records,
419                                          dccpavr_node) {
420                         if (!after48(avr->dccpavr_ack_seqno, ackno))
421                                 goto found;
422                 }
423                 /* End of the dccpav_records list, not found, exit */
424                 break;
425 found:
426                 if (between48(avr->dccpavr_ack_seqno, ackno_end_rl, ackno)) {
427                         const u8 state = (*vector &
428                                           DCCP_ACKVEC_STATE_MASK) >> 6;
429                         if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED) {
430 #ifdef CONFIG_IP_DCCP_DEBUG
431                                 struct dccp_sock *dp = dccp_sk(sk);
432                                 const char *debug_prefix =
433                                         dp->dccps_role == DCCP_ROLE_CLIENT ?
434                                         "CLIENT rx ack: " : "server rx ack: ";
435 #endif
436                                 dccp_pr_debug("%sACK vector 0, len=%d, "
437                                               "ack_seqno=%llu, ack_ackno=%llu, "
438                                               "ACKED!\n",
439                                               debug_prefix, len,
440                                               (unsigned long long)
441                                               avr->dccpavr_ack_seqno,
442                                               (unsigned long long)
443                                               avr->dccpavr_ack_ackno);
444                                 dccp_ackvec_throw_record(av, avr);
445                         }
446                         /*
447                          * If it wasn't received, continue scanning... we might
448                          * find another one.
449                          */
450                 }
451
452                 dccp_set_seqno(&ackno, ackno_end_rl - 1);
453                 ++vector;
454         }
455 }
456
457 int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
458                       const u8 opt, const u8 *value, const u8 len)
459 {
460         if (len > DCCP_MAX_ACKVEC_LEN)
461                 return -1;
462
463         /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
464         dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
465                                         DCCP_SKB_CB(skb)->dccpd_ack_seq,
466                                         len, value);
467         return 0;
468 }
469
470 static char dccp_ackvec_slab_msg[] __initdata =
471         KERN_CRIT "DCCP: Unable to create ack vectors slab caches\n";
472
473 int __init dccp_ackvec_init(void)
474 {
475         dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
476                                              sizeof(struct dccp_ackvec), 0,
477                                              SLAB_HWCACHE_ALIGN, NULL, NULL);
478         if (dccp_ackvec_slab == NULL)
479                 goto out_err;
480
481         dccp_ackvec_record_slab =
482                         kmem_cache_create("dccp_ackvec_record",
483                                           sizeof(struct dccp_ackvec_record),
484                                           0, SLAB_HWCACHE_ALIGN, NULL, NULL);
485         if (dccp_ackvec_record_slab == NULL)
486                 goto out_destroy_slab;
487
488         return 0;
489
490 out_destroy_slab:
491         kmem_cache_destroy(dccp_ackvec_slab);
492         dccp_ackvec_slab = NULL;
493 out_err:
494         printk(dccp_ackvec_slab_msg);
495         return -ENOBUFS;
496 }
497
498 void dccp_ackvec_exit(void)
499 {
500         if (dccp_ackvec_slab != NULL) {
501                 kmem_cache_destroy(dccp_ackvec_slab);
502                 dccp_ackvec_slab = NULL;
503         }
504         if (dccp_ackvec_record_slab != NULL) {
505                 kmem_cache_destroy(dccp_ackvec_record_slab);
506                 dccp_ackvec_record_slab = NULL;
507         }
508 }