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