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