[DCCP] ackvec: Ditch dccpav_buf_len
[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/skbuff.h>
17
18 #include <net/sock.h>
19
20 int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
21 {
22         struct dccp_sock *dp = dccp_sk(sk);
23 #ifdef CONFIG_IP_DCCP_DEBUG
24         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
25                                 "CLIENT tx: " : "server tx: ";
26 #endif
27         struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
28         int len = av->dccpav_vec_len + 2;
29         struct timeval now;
30         u32 elapsed_time;
31         unsigned char *to, *from;
32
33         dccp_timestamp(sk, &now);
34         elapsed_time = timeval_delta(&now, &av->dccpav_time) / 10;
35
36         if (elapsed_time != 0)
37                 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
38
39         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
40                 return -1;
41
42         /*
43          * XXX: now we have just one ack vector sent record, so
44          * we have to wait for it to be cleared.
45          *
46          * Of course this is not acceptable, but this is just for
47          * basic testing now.
48          */
49         if (av->dccpav_ack_seqno != DCCP_MAX_SEQNO + 1)
50                 return -1;
51
52         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
53
54         to    = skb_push(skb, len);
55         *to++ = DCCPO_ACK_VECTOR_0;
56         *to++ = len;
57
58         len  = av->dccpav_vec_len;
59         from = av->dccpav_buf + av->dccpav_buf_head;
60
61         /* Check if buf_head wraps */
62         if ((int)av->dccpav_buf_head + len > av->dccpav_vec_len) {
63                 const u32 tailsize = av->dccpav_vec_len - av->dccpav_buf_head;
64
65                 memcpy(to, from, tailsize);
66                 to   += tailsize;
67                 len  -= tailsize;
68                 from = av->dccpav_buf;
69         }
70
71         memcpy(to, from, len);
72         /*
73          *      From draft-ietf-dccp-spec-11.txt:
74          *
75          *      For each acknowledgement it sends, the HC-Receiver will add an
76          *      acknowledgement record.  ack_seqno will equal the HC-Receiver
77          *      sequence number it used for the ack packet; ack_ptr will equal
78          *      buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
79          *      equal buf_nonce.
80          *
81          * This implemention uses just one ack record for now.
82          */
83         av->dccpav_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
84         av->dccpav_ack_ptr   = av->dccpav_buf_head;
85         av->dccpav_ack_ackno = av->dccpav_buf_ackno;
86         av->dccpav_ack_nonce = av->dccpav_buf_nonce;
87         av->dccpav_sent_len  = av->dccpav_vec_len;
88
89         dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
90                       "ack_ackno=%llu\n",
91                       debug_prefix, av->dccpav_sent_len,
92                       (unsigned long long)av->dccpav_ack_seqno,
93                       (unsigned long long)av->dccpav_ack_ackno);
94         return -1;
95 }
96
97 struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
98 {
99         struct dccp_ackvec *av = kmalloc(sizeof(*av), priority);
100
101         if (av != NULL) {
102                 av->dccpav_buf_head     =
103                         av->dccpav_buf_tail = DCCP_MAX_ACKVEC_LEN - 1;
104                 av->dccpav_buf_ackno    =
105                         av->dccpav_ack_ackno = av->dccpav_ack_seqno = ~0LLU;
106                 av->dccpav_buf_nonce = av->dccpav_buf_nonce = 0;
107                 av->dccpav_ack_ptr      = 0;
108                 av->dccpav_time.tv_sec  = 0;
109                 av->dccpav_time.tv_usec = 0;
110                 av->dccpav_sent_len     = av->dccpav_vec_len = 0;
111         }
112
113         return av;
114 }
115
116 void dccp_ackvec_free(struct dccp_ackvec *av)
117 {
118         kfree(av);
119 }
120
121 static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
122                                    const u8 index)
123 {
124         return av->dccpav_buf[index] & DCCP_ACKVEC_STATE_MASK;
125 }
126
127 static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
128                                  const u8 index)
129 {
130         return av->dccpav_buf[index] & DCCP_ACKVEC_LEN_MASK;
131 }
132
133 /*
134  * If several packets are missing, the HC-Receiver may prefer to enter multiple
135  * bytes with run length 0, rather than a single byte with a larger run length;
136  * this simplifies table updates if one of the missing packets arrives.
137  */
138 static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
139                                                  const unsigned int packets,
140                                                  const unsigned char state)
141 {
142         unsigned int gap;
143         long new_head;
144
145         if (av->dccpav_vec_len + packets > DCCP_MAX_ACKVEC_LEN)
146                 return -ENOBUFS;
147
148         gap      = packets - 1;
149         new_head = av->dccpav_buf_head - packets;
150
151         if (new_head < 0) {
152                 if (gap > 0) {
153                         memset(av->dccpav_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
154                                gap + new_head + 1);
155                         gap = -new_head;
156                 }
157                 new_head += DCCP_MAX_ACKVEC_LEN;
158         } 
159
160         av->dccpav_buf_head = new_head;
161
162         if (gap > 0)
163                 memset(av->dccpav_buf + av->dccpav_buf_head + 1,
164                        DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
165
166         av->dccpav_buf[av->dccpav_buf_head] = state;
167         av->dccpav_vec_len += packets;
168         return 0;
169 }
170
171 /*
172  * Implements the draft-ietf-dccp-spec-11.txt Appendix A
173  */
174 int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
175                     const u64 ackno, const u8 state)
176 {
177         /*
178          * Check at the right places if the buffer is full, if it is, tell the
179          * caller to start dropping packets till the HC-Sender acks our ACK
180          * vectors, when we will free up space in dccpav_buf.
181          *
182          * We may well decide to do buffer compression, etc, but for now lets
183          * just drop.
184          *
185          * From Appendix A:
186          *
187          *      Of course, the circular buffer may overflow, either when the
188          *      HC-Sender is sending data at a very high rate, when the
189          *      HC-Receiver's acknowledgements are not reaching the HC-Sender,
190          *      or when the HC-Sender is forgetting to acknowledge those acks
191          *      (so the HC-Receiver is unable to clean up old state). In this
192          *      case, the HC-Receiver should either compress the buffer (by
193          *      increasing run lengths when possible), transfer its state to
194          *      a larger buffer, or, as a last resort, drop all received
195          *      packets, without processing them whatsoever, until its buffer
196          *      shrinks again.
197          */
198
199         /* See if this is the first ackno being inserted */
200         if (av->dccpav_vec_len == 0) {
201                 av->dccpav_buf[av->dccpav_buf_head] = state;
202                 av->dccpav_vec_len = 1;
203         } else if (after48(ackno, av->dccpav_buf_ackno)) {
204                 const u64 delta = dccp_delta_seqno(av->dccpav_buf_ackno,
205                                                    ackno);
206
207                 /*
208                  * Look if the state of this packet is the same as the
209                  * previous ackno and if so if we can bump the head len.
210                  */
211                 if (delta == 1 &&
212                     dccp_ackvec_state(av, av->dccpav_buf_head) == state &&
213                     (dccp_ackvec_len(av, av->dccpav_buf_head) <
214                      DCCP_ACKVEC_LEN_MASK))
215                         av->dccpav_buf[av->dccpav_buf_head]++;
216                 else if (dccp_ackvec_set_buf_head_state(av, delta, state))
217                         return -ENOBUFS;
218         } else {
219                 /*
220                  * A.1.2.  Old Packets
221                  *
222                  *      When a packet with Sequence Number S arrives, and
223                  *      S <= buf_ackno, the HC-Receiver will scan the table
224                  *      for the byte corresponding to S. (Indexing structures
225                  *      could reduce the complexity of this scan.)
226                  */
227                 u64 delta = dccp_delta_seqno(ackno, av->dccpav_buf_ackno);
228                 u8 index = av->dccpav_buf_head;
229
230                 while (1) {
231                         const u8 len = dccp_ackvec_len(av, index);
232                         const u8 state = dccp_ackvec_state(av, index);
233                         /*
234                          * valid packets not yet in dccpav_buf have a reserved
235                          * entry, with a len equal to 0.
236                          */
237                         if (state == DCCP_ACKVEC_STATE_NOT_RECEIVED &&
238                             len == 0 && delta == 0) { /* Found our
239                                                          reserved seat! */
240                                 dccp_pr_debug("Found %llu reserved seat!\n",
241                                               (unsigned long long)ackno);
242                                 av->dccpav_buf[index] = state;
243                                 goto out;
244                         }
245                         /* len == 0 means one packet */
246                         if (delta < len + 1)
247                                 goto out_duplicate;
248
249                         delta -= len + 1;
250                         if (++index == DCCP_MAX_ACKVEC_LEN)
251                                 index = 0;
252                 }
253         }
254
255         av->dccpav_buf_ackno = ackno;
256         dccp_timestamp(sk, &av->dccpav_time);
257 out:
258         dccp_pr_debug("");
259         return 0;
260
261 out_duplicate:
262         /* Duplicate packet */
263         dccp_pr_debug("Received a dup or already considered lost "
264                       "packet: %llu\n", (unsigned long long)ackno);
265         return -EILSEQ;
266 }
267
268 #ifdef CONFIG_IP_DCCP_DEBUG
269 void dccp_ackvector_print(const u64 ackno, const unsigned char *vector, int len)
270 {
271         if (!dccp_debug)
272                 return;
273
274         printk("ACK vector len=%d, ackno=%llu |", len,
275                (unsigned long long)ackno);
276
277         while (len--) {
278                 const u8 state = (*vector & DCCP_ACKVEC_STATE_MASK) >> 6;
279                 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
280
281                 printk("%d,%d|", state, rl);
282                 ++vector;
283         }
284
285         printk("\n");
286 }
287
288 void dccp_ackvec_print(const struct dccp_ackvec *av)
289 {
290         dccp_ackvector_print(av->dccpav_buf_ackno,
291                              av->dccpav_buf + av->dccpav_buf_head,
292                              av->dccpav_vec_len);
293 }
294 #endif
295
296 static void dccp_ackvec_throw_away_ack_record(struct dccp_ackvec *av)
297 {
298         /*
299          * As we're keeping track of the ack vector size (dccpav_vec_len) and
300          * the sent ack vector size (dccpav_sent_len) we don't need
301          * dccpav_buf_tail at all, but keep this code here as in the future
302          * we'll implement a vector of ack records, as suggested in
303          * draft-ietf-dccp-spec-11.txt Appendix A. -acme
304          */
305 #if 0
306         u32 new_buf_tail = av->dccpav_ack_ptr + 1;
307         if (new_buf_tail >= av->dccpav_vec_len)
308                 new_buf_tail -= av->dccpav_vec_len;
309         av->dccpav_buf_tail = new_buf_tail;
310 #endif
311         av->dccpav_vec_len -= av->dccpav_sent_len;
312 }
313
314 void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
315                                  const u64 ackno)
316 {
317         /* Check if we actually sent an ACK vector */
318         if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
319                 return;
320
321         if (ackno == av->dccpav_ack_seqno) {
322 #ifdef CONFIG_IP_DCCP_DEBUG
323                 struct dccp_sock *dp = dccp_sk(sk);
324                 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
325                                         "CLIENT rx ack: " : "server rx ack: ";
326 #endif
327                 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
328                               "ack_ackno=%llu, ACKED!\n",
329                               debug_prefix, 1,
330                               (unsigned long long)av->dccpav_ack_seqno,
331                               (unsigned long long)av->dccpav_ack_ackno);
332                 dccp_ackvec_throw_away_ack_record(av);
333                 av->dccpav_ack_seqno = DCCP_MAX_SEQNO + 1;
334         }
335 }
336
337 static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
338                                             struct sock *sk, u64 ackno,
339                                             const unsigned char len,
340                                             const unsigned char *vector)
341 {
342         unsigned char i;
343
344         /* Check if we actually sent an ACK vector */
345         if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
346                 return;
347         /*
348          * We're in the receiver half connection, so if the received an ACK
349          * vector ackno (e.g. 50) before dccpav_ack_seqno (e.g. 52), we're
350          * not interested.
351          *
352          * Extra explanation with example:
353          * 
354          * if we received an ACK vector with ackno 50, it can only be acking
355          * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
356          */
357         /* dccp_pr_debug("is %llu < %llu? ", ackno, av->dccpav_ack_seqno); */
358         if (before48(ackno, av->dccpav_ack_seqno)) {
359                 /* dccp_pr_debug_cat("yes\n"); */
360                 return;
361         }
362         /* dccp_pr_debug_cat("no\n"); */
363
364         i = len;
365         while (i--) {
366                 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
367                 u64 ackno_end_rl;
368
369                 dccp_set_seqno(&ackno_end_rl, ackno - rl);
370
371                 /*
372                  * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
373                  * av->dccpav_ack_seqno, ackno);
374                  */
375                 if (between48(av->dccpav_ack_seqno, ackno_end_rl, ackno)) {
376                         const u8 state = (*vector &
377                                           DCCP_ACKVEC_STATE_MASK) >> 6;
378                         /* dccp_pr_debug_cat("yes\n"); */
379
380                         if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED) {
381 #ifdef CONFIG_IP_DCCP_DEBUG
382                                 struct dccp_sock *dp = dccp_sk(sk);
383                                 const char *debug_prefix =
384                                         dp->dccps_role == DCCP_ROLE_CLIENT ?
385                                         "CLIENT rx ack: " : "server rx ack: ";
386 #endif
387                                 dccp_pr_debug("%sACK vector 0, len=%d, "
388                                               "ack_seqno=%llu, ack_ackno=%llu, "
389                                               "ACKED!\n",
390                                               debug_prefix, len,
391                                               (unsigned long long)
392                                               av->dccpav_ack_seqno,
393                                               (unsigned long long)
394                                               av->dccpav_ack_ackno);
395                                 dccp_ackvec_throw_away_ack_record(av);
396                         }
397                         /*
398                          * If dccpav_ack_seqno was not received, no problem
399                          * we'll send another ACK vector.
400                          */
401                         av->dccpav_ack_seqno = DCCP_MAX_SEQNO + 1;
402                         break;
403                 }
404                 /* dccp_pr_debug_cat("no\n"); */
405
406                 dccp_set_seqno(&ackno, ackno_end_rl - 1);
407                 ++vector;
408         }
409 }
410
411 int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
412                       const u8 opt, const u8 *value, const u8 len)
413 {
414         if (len > DCCP_MAX_ACKVEC_LEN)
415                 return -1;
416
417         /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
418         dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
419                                         DCCP_SKB_CB(skb)->dccpd_ack_seq,
420                                         len, value);
421         return 0;
422 }