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/skbuff.h>
20 int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
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: ";
27 struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
28 int len = av->dccpav_vec_len + 2;
31 unsigned char *to, *from;
33 dccp_timestamp(sk, &now);
34 elapsed_time = timeval_delta(&now, &av->dccpav_time) / 10;
36 if (elapsed_time != 0)
37 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
39 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
43 * XXX: now we have just one ack vector sent record, so
44 * we have to wait for it to be cleared.
46 * Of course this is not acceptable, but this is just for
49 if (av->dccpav_ack_seqno != DCCP_MAX_SEQNO + 1)
52 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
54 to = skb_push(skb, len);
55 *to++ = DCCPO_ACK_VECTOR_0;
58 len = av->dccpav_vec_len;
59 from = av->dccpav_buf + av->dccpav_buf_head;
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;
65 memcpy(to, from, tailsize);
68 from = av->dccpav_buf;
71 memcpy(to, from, len);
73 * From draft-ietf-dccp-spec-11.txt:
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
81 * This implemention uses just one ack record for now.
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;
89 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
91 debug_prefix, av->dccpav_sent_len,
92 (unsigned long long)av->dccpav_ack_seqno,
93 (unsigned long long)av->dccpav_ack_ackno);
97 struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
99 struct dccp_ackvec *av = kmalloc(sizeof(*av), priority);
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;
116 void dccp_ackvec_free(struct dccp_ackvec *av)
121 static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
124 return av->dccpav_buf[index] & DCCP_ACKVEC_STATE_MASK;
127 static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
130 return av->dccpav_buf[index] & DCCP_ACKVEC_LEN_MASK;
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.
138 static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
139 const unsigned int packets,
140 const unsigned char state)
145 if (av->dccpav_vec_len + packets > DCCP_MAX_ACKVEC_LEN)
149 new_head = av->dccpav_buf_head - packets;
153 memset(av->dccpav_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
157 new_head += DCCP_MAX_ACKVEC_LEN;
160 av->dccpav_buf_head = new_head;
163 memset(av->dccpav_buf + av->dccpav_buf_head + 1,
164 DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
166 av->dccpav_buf[av->dccpav_buf_head] = state;
167 av->dccpav_vec_len += packets;
172 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
174 int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
175 const u64 ackno, const u8 state)
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.
182 * We may well decide to do buffer compression, etc, but for now lets
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
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,
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.
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))
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.)
227 u64 delta = dccp_delta_seqno(ackno, av->dccpav_buf_ackno);
228 u8 index = av->dccpav_buf_head;
231 const u8 len = dccp_ackvec_len(av, index);
232 const u8 state = dccp_ackvec_state(av, index);
234 * valid packets not yet in dccpav_buf have a reserved
235 * entry, with a len equal to 0.
237 if (state == DCCP_ACKVEC_STATE_NOT_RECEIVED &&
238 len == 0 && delta == 0) { /* Found our
240 dccp_pr_debug("Found %llu reserved seat!\n",
241 (unsigned long long)ackno);
242 av->dccpav_buf[index] = state;
245 /* len == 0 means one packet */
250 if (++index == DCCP_MAX_ACKVEC_LEN)
255 av->dccpav_buf_ackno = ackno;
256 dccp_timestamp(sk, &av->dccpav_time);
262 /* Duplicate packet */
263 dccp_pr_debug("Received a dup or already considered lost "
264 "packet: %llu\n", (unsigned long long)ackno);
268 #ifdef CONFIG_IP_DCCP_DEBUG
269 void dccp_ackvector_print(const u64 ackno, const unsigned char *vector, int len)
274 printk("ACK vector len=%d, ackno=%llu |", len,
275 (unsigned long long)ackno);
278 const u8 state = (*vector & DCCP_ACKVEC_STATE_MASK) >> 6;
279 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
281 printk("%d,%d|", state, rl);
288 void dccp_ackvec_print(const struct dccp_ackvec *av)
290 dccp_ackvector_print(av->dccpav_buf_ackno,
291 av->dccpav_buf + av->dccpav_buf_head,
296 static void dccp_ackvec_throw_away_ack_record(struct dccp_ackvec *av)
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
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;
311 av->dccpav_vec_len -= av->dccpav_sent_len;
314 void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
317 /* Check if we actually sent an ACK vector */
318 if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
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: ";
327 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
328 "ack_ackno=%llu, ACKED!\n",
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;
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)
344 /* Check if we actually sent an ACK vector */
345 if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
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
352 * Extra explanation with example:
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).
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"); */
362 /* dccp_pr_debug_cat("no\n"); */
366 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
369 dccp_set_seqno(&ackno_end_rl, ackno - rl);
372 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
373 * av->dccpav_ack_seqno, ackno);
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"); */
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: ";
387 dccp_pr_debug("%sACK vector 0, len=%d, "
388 "ack_seqno=%llu, ack_ackno=%llu, "
392 av->dccpav_ack_seqno,
394 av->dccpav_ack_ackno);
395 dccp_ackvec_throw_away_ack_record(av);
398 * If dccpav_ack_seqno was not received, no problem
399 * we'll send another ACK vector.
401 av->dccpav_ack_seqno = DCCP_MAX_SEQNO + 1;
404 /* dccp_pr_debug_cat("no\n"); */
406 dccp_set_seqno(&ackno, ackno_end_rl - 1);
411 int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
412 const u8 opt, const u8 *value, const u8 len)
414 if (len > DCCP_MAX_ACKVEC_LEN)
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,