]>
Commit | Line | Data |
---|---|---|
2a91aa39 AB |
1 | /* |
2 | * net/dccp/ccids/ccid2.c | |
3 | * | |
4 | * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk> | |
5 | * | |
6 | * Changes to meet Linux coding standards, and DCCP infrastructure fixes. | |
7 | * | |
8 | * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
25 | /* | |
0e64e94e | 26 | * This implementation should follow RFC 4341 |
2a91aa39 | 27 | */ |
86349c8d | 28 | #include "../feat.h" |
2a91aa39 AB |
29 | #include "../ccid.h" |
30 | #include "../dccp.h" | |
31 | #include "ccid2.h" | |
32 | ||
2a91aa39 | 33 | |
8d424f6c | 34 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
84116716 GR |
35 | static int ccid2_debug; |
36 | #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a) | |
2a91aa39 | 37 | |
2a91aa39 AB |
38 | static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx) |
39 | { | |
40 | int len = 0; | |
2a91aa39 | 41 | int pipe = 0; |
1fb87509 | 42 | struct ccid2_seq *seqp = hctx->seqh; |
2a91aa39 AB |
43 | |
44 | /* there is data in the chain */ | |
1fb87509 | 45 | if (seqp != hctx->seqt) { |
2a91aa39 AB |
46 | seqp = seqp->ccid2s_prev; |
47 | len++; | |
48 | if (!seqp->ccid2s_acked) | |
49 | pipe++; | |
50 | ||
1fb87509 | 51 | while (seqp != hctx->seqt) { |
c0c736db | 52 | struct ccid2_seq *prev = seqp->ccid2s_prev; |
2a91aa39 | 53 | |
2a91aa39 AB |
54 | len++; |
55 | if (!prev->ccid2s_acked) | |
56 | pipe++; | |
57 | ||
58 | /* packets are sent sequentially */ | |
5e28599a GR |
59 | BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq, |
60 | prev->ccid2s_seq ) >= 0); | |
29651cda AB |
61 | BUG_ON(time_before(seqp->ccid2s_sent, |
62 | prev->ccid2s_sent)); | |
2a91aa39 AB |
63 | |
64 | seqp = prev; | |
65 | } | |
66 | } | |
67 | ||
1fb87509 | 68 | BUG_ON(pipe != hctx->pipe); |
2a91aa39 AB |
69 | ccid2_pr_debug("len of chain=%d\n", len); |
70 | ||
71 | do { | |
72 | seqp = seqp->ccid2s_prev; | |
73 | len++; | |
1fb87509 | 74 | } while (seqp != hctx->seqh); |
2a91aa39 | 75 | |
2a91aa39 | 76 | ccid2_pr_debug("total len=%d\n", len); |
1fb87509 | 77 | BUG_ON(len != hctx->seqbufc * CCID2_SEQBUF_LEN); |
2a91aa39 AB |
78 | } |
79 | #else | |
84116716 GR |
80 | #define ccid2_pr_debug(format, a...) |
81 | #define ccid2_hc_tx_check_sanity(hctx) | |
2a91aa39 AB |
82 | #endif |
83 | ||
cd1f7d34 | 84 | static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx) |
07978aab AB |
85 | { |
86 | struct ccid2_seq *seqp; | |
87 | int i; | |
88 | ||
89 | /* check if we have space to preserve the pointer to the buffer */ | |
1fb87509 | 90 | if (hctx->seqbufc >= sizeof(hctx->seqbuf) / sizeof(struct ccid2_seq *)) |
07978aab AB |
91 | return -ENOMEM; |
92 | ||
93 | /* allocate buffer and initialize linked list */ | |
cd1f7d34 | 94 | seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any()); |
07978aab AB |
95 | if (seqp == NULL) |
96 | return -ENOMEM; | |
97 | ||
cd1f7d34 | 98 | for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) { |
07978aab AB |
99 | seqp[i].ccid2s_next = &seqp[i + 1]; |
100 | seqp[i + 1].ccid2s_prev = &seqp[i]; | |
101 | } | |
cd1f7d34 GR |
102 | seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp; |
103 | seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1]; | |
07978aab AB |
104 | |
105 | /* This is the first allocation. Initiate the head and tail. */ | |
1fb87509 GR |
106 | if (hctx->seqbufc == 0) |
107 | hctx->seqh = hctx->seqt = seqp; | |
07978aab AB |
108 | else { |
109 | /* link the existing list with the one we just created */ | |
1fb87509 GR |
110 | hctx->seqh->ccid2s_next = seqp; |
111 | seqp->ccid2s_prev = hctx->seqh; | |
07978aab | 112 | |
1fb87509 GR |
113 | hctx->seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1]; |
114 | seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hctx->seqt; | |
07978aab AB |
115 | } |
116 | ||
117 | /* store the original pointer to the buffer so we can free it */ | |
1fb87509 GR |
118 | hctx->seqbuf[hctx->seqbufc] = seqp; |
119 | hctx->seqbufc++; | |
07978aab AB |
120 | |
121 | return 0; | |
122 | } | |
123 | ||
6b57c93d | 124 | static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb) |
2a91aa39 | 125 | { |
6c583248 | 126 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); |
2a91aa39 | 127 | |
1fb87509 | 128 | if (hctx->pipe < hctx->cwnd) |
83399361 | 129 | return 0; |
2a91aa39 | 130 | |
446dec30 | 131 | return 1; /* XXX CCID should dequeue when ready instead of polling */ |
2a91aa39 AB |
132 | } |
133 | ||
df054e1d | 134 | static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val) |
2a91aa39 AB |
135 | { |
136 | struct dccp_sock *dp = dccp_sk(sk); | |
1fb87509 | 137 | u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->cwnd, 2); |
d50ad163 | 138 | |
2a91aa39 | 139 | /* |
d50ad163 GR |
140 | * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from |
141 | * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always | |
142 | * acceptable since this causes starvation/deadlock whenever cwnd < 2. | |
143 | * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled). | |
2a91aa39 | 144 | */ |
d50ad163 GR |
145 | if (val == 0 || val > max_ratio) { |
146 | DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio); | |
147 | val = max_ratio; | |
2a91aa39 | 148 | } |
86349c8d GR |
149 | if (val > DCCPF_ACK_RATIO_MAX) |
150 | val = DCCPF_ACK_RATIO_MAX; | |
2a91aa39 | 151 | |
d50ad163 GR |
152 | if (val == dp->dccps_l_ack_ratio) |
153 | return; | |
154 | ||
df054e1d | 155 | ccid2_pr_debug("changing local ack ratio to %u\n", val); |
2a91aa39 AB |
156 | dp->dccps_l_ack_ratio = val; |
157 | } | |
158 | ||
593f16aa AB |
159 | static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val) |
160 | { | |
161 | ccid2_pr_debug("change SRTT to %ld\n", val); | |
1fb87509 | 162 | hctx->srtt = val; |
593f16aa AB |
163 | } |
164 | ||
2a91aa39 AB |
165 | static void ccid2_start_rto_timer(struct sock *sk); |
166 | ||
167 | static void ccid2_hc_tx_rto_expire(unsigned long data) | |
168 | { | |
169 | struct sock *sk = (struct sock *)data; | |
170 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); | |
171 | long s; | |
172 | ||
2a91aa39 AB |
173 | bh_lock_sock(sk); |
174 | if (sock_owned_by_user(sk)) { | |
1fb87509 | 175 | sk_reset_timer(sk, &hctx->rtotimer, jiffies + HZ / 5); |
2a91aa39 AB |
176 | goto out; |
177 | } | |
178 | ||
179 | ccid2_pr_debug("RTO_EXPIRE\n"); | |
180 | ||
181 | ccid2_hc_tx_check_sanity(hctx); | |
182 | ||
183 | /* back-off timer */ | |
1fb87509 | 184 | hctx->rto <<= 1; |
2a91aa39 | 185 | |
1fb87509 | 186 | s = hctx->rto / HZ; |
2a91aa39 | 187 | if (s > 60) |
1fb87509 | 188 | hctx->rto = 60 * HZ; |
2a91aa39 AB |
189 | |
190 | ccid2_start_rto_timer(sk); | |
191 | ||
192 | /* adjust pipe, cwnd etc */ | |
1fb87509 GR |
193 | hctx->ssthresh = hctx->cwnd / 2; |
194 | if (hctx->ssthresh < 2) | |
195 | hctx->ssthresh = 2; | |
196 | hctx->cwnd = 1; | |
197 | hctx->pipe = 0; | |
2a91aa39 AB |
198 | |
199 | /* clear state about stuff we sent */ | |
1fb87509 GR |
200 | hctx->seqt = hctx->seqh; |
201 | hctx->packets_acked = 0; | |
2a91aa39 AB |
202 | |
203 | /* clear ack ratio state. */ | |
1fb87509 GR |
204 | hctx->rpseq = 0; |
205 | hctx->rpdupack = -1; | |
2a91aa39 AB |
206 | ccid2_change_l_ack_ratio(sk, 1); |
207 | ccid2_hc_tx_check_sanity(hctx); | |
208 | out: | |
209 | bh_unlock_sock(sk); | |
77ff72d5 | 210 | sock_put(sk); |
2a91aa39 AB |
211 | } |
212 | ||
213 | static void ccid2_start_rto_timer(struct sock *sk) | |
214 | { | |
215 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); | |
216 | ||
1fb87509 | 217 | ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->rto); |
2a91aa39 | 218 | |
1fb87509 GR |
219 | BUG_ON(timer_pending(&hctx->rtotimer)); |
220 | sk_reset_timer(sk, &hctx->rtotimer, | |
221 | jiffies + hctx->rto); | |
2a91aa39 AB |
222 | } |
223 | ||
c506d91d | 224 | static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len) |
2a91aa39 AB |
225 | { |
226 | struct dccp_sock *dp = dccp_sk(sk); | |
227 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); | |
07978aab | 228 | struct ccid2_seq *next; |
2a91aa39 | 229 | |
1fb87509 | 230 | hctx->pipe++; |
2a91aa39 | 231 | |
1fb87509 GR |
232 | hctx->seqh->ccid2s_seq = dp->dccps_gss; |
233 | hctx->seqh->ccid2s_acked = 0; | |
234 | hctx->seqh->ccid2s_sent = jiffies; | |
2a91aa39 | 235 | |
1fb87509 | 236 | next = hctx->seqh->ccid2s_next; |
07978aab | 237 | /* check if we need to alloc more space */ |
1fb87509 | 238 | if (next == hctx->seqt) { |
7d9e8931 GR |
239 | if (ccid2_hc_tx_alloc_seq(hctx)) { |
240 | DCCP_CRIT("packet history - out of memory!"); | |
241 | /* FIXME: find a more graceful way to bail out */ | |
242 | return; | |
243 | } | |
1fb87509 GR |
244 | next = hctx->seqh->ccid2s_next; |
245 | BUG_ON(next == hctx->seqt); | |
2a91aa39 | 246 | } |
1fb87509 | 247 | hctx->seqh = next; |
07978aab | 248 | |
1fb87509 | 249 | ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->cwnd, hctx->pipe); |
2a91aa39 | 250 | |
900bfed4 GR |
251 | /* |
252 | * FIXME: The code below is broken and the variables have been removed | |
253 | * from the socket struct. The `ackloss' variable was always set to 0, | |
254 | * and with arsent there are several problems: | |
255 | * (i) it doesn't just count the number of Acks, but all sent packets; | |
256 | * (ii) it is expressed in # of packets, not # of windows, so the | |
257 | * comparison below uses the wrong formula: Appendix A of RFC 4341 | |
258 | * comes up with the number K = cwnd / (R^2 - R) of consecutive windows | |
259 | * of data with no lost or marked Ack packets. If arsent were the # of | |
260 | * consecutive Acks received without loss, then Ack Ratio needs to be | |
261 | * decreased by 1 when | |
262 | * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2) | |
263 | * where cwnd / R is the number of Acks received per window of data | |
264 | * (cf. RFC 4341, App. A). The problems are that | |
265 | * - arsent counts other packets as well; | |
266 | * - the comparison uses a formula different from RFC 4341; | |
267 | * - computing a cubic/quadratic equation each time is too complicated. | |
268 | * Hence a different algorithm is needed. | |
269 | */ | |
270 | #if 0 | |
2a91aa39 | 271 | /* Ack Ratio. Need to maintain a concept of how many windows we sent */ |
1fb87509 | 272 | hctx->arsent++; |
2a91aa39 | 273 | /* We had an ack loss in this window... */ |
1fb87509 GR |
274 | if (hctx->ackloss) { |
275 | if (hctx->arsent >= hctx->cwnd) { | |
276 | hctx->arsent = 0; | |
277 | hctx->ackloss = 0; | |
2a91aa39 | 278 | } |
c0c736db ACM |
279 | } else { |
280 | /* No acks lost up to now... */ | |
2a91aa39 AB |
281 | /* decrease ack ratio if enough packets were sent */ |
282 | if (dp->dccps_l_ack_ratio > 1) { | |
283 | /* XXX don't calculate denominator each time */ | |
c0c736db ACM |
284 | int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio - |
285 | dp->dccps_l_ack_ratio; | |
2a91aa39 | 286 | |
1fb87509 | 287 | denom = hctx->cwnd * hctx->cwnd / denom; |
2a91aa39 | 288 | |
1fb87509 | 289 | if (hctx->arsent >= denom) { |
2a91aa39 | 290 | ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1); |
1fb87509 | 291 | hctx->arsent = 0; |
2a91aa39 | 292 | } |
c0c736db ACM |
293 | } else { |
294 | /* we can't increase ack ratio further [1] */ | |
1fb87509 | 295 | hctx->arsent = 0; /* or maybe set it to cwnd*/ |
2a91aa39 AB |
296 | } |
297 | } | |
900bfed4 | 298 | #endif |
2a91aa39 AB |
299 | |
300 | /* setup RTO timer */ | |
1fb87509 | 301 | if (!timer_pending(&hctx->rtotimer)) |
2a91aa39 | 302 | ccid2_start_rto_timer(sk); |
c0c736db | 303 | |
8d424f6c | 304 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
2a91aa39 | 305 | do { |
1fb87509 | 306 | struct ccid2_seq *seqp = hctx->seqt; |
2a91aa39 | 307 | |
1fb87509 | 308 | while (seqp != hctx->seqh) { |
2a91aa39 | 309 | ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n", |
8109b02b | 310 | (unsigned long long)seqp->ccid2s_seq, |
234af484 | 311 | seqp->ccid2s_acked, seqp->ccid2s_sent); |
2a91aa39 AB |
312 | seqp = seqp->ccid2s_next; |
313 | } | |
c0c736db | 314 | } while (0); |
2a91aa39 AB |
315 | ccid2_pr_debug("=========\n"); |
316 | ccid2_hc_tx_check_sanity(hctx); | |
317 | #endif | |
318 | } | |
319 | ||
320 | /* XXX Lame code duplication! | |
321 | * returns -1 if none was found. | |
322 | * else returns the next offset to use in the function call. | |
323 | */ | |
324 | static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset, | |
325 | unsigned char **vec, unsigned char *veclen) | |
326 | { | |
c9eaf173 YH |
327 | const struct dccp_hdr *dh = dccp_hdr(skb); |
328 | unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); | |
329 | unsigned char *opt_ptr; | |
330 | const unsigned char *opt_end = (unsigned char *)dh + | |
331 | (dh->dccph_doff * 4); | |
332 | unsigned char opt, len; | |
333 | unsigned char *value; | |
2a91aa39 AB |
334 | |
335 | BUG_ON(offset < 0); | |
336 | options += offset; | |
337 | opt_ptr = options; | |
338 | if (opt_ptr >= opt_end) | |
339 | return -1; | |
340 | ||
341 | while (opt_ptr != opt_end) { | |
c9eaf173 YH |
342 | opt = *opt_ptr++; |
343 | len = 0; | |
344 | value = NULL; | |
345 | ||
346 | /* Check if this isn't a single byte option */ | |
347 | if (opt > DCCPO_MAX_RESERVED) { | |
348 | if (opt_ptr == opt_end) | |
349 | goto out_invalid_option; | |
350 | ||
351 | len = *opt_ptr++; | |
352 | if (len < 3) | |
353 | goto out_invalid_option; | |
354 | /* | |
355 | * Remove the type and len fields, leaving | |
356 | * just the value size | |
357 | */ | |
358 | len -= 2; | |
359 | value = opt_ptr; | |
360 | opt_ptr += len; | |
361 | ||
362 | if (opt_ptr > opt_end) | |
363 | goto out_invalid_option; | |
364 | } | |
2a91aa39 AB |
365 | |
366 | switch (opt) { | |
367 | case DCCPO_ACK_VECTOR_0: | |
368 | case DCCPO_ACK_VECTOR_1: | |
369 | *vec = value; | |
370 | *veclen = len; | |
371 | return offset + (opt_ptr - options); | |
2a91aa39 AB |
372 | } |
373 | } | |
374 | ||
375 | return -1; | |
376 | ||
377 | out_invalid_option: | |
59348b19 | 378 | DCCP_BUG("Invalid option - this should not happen (previous parsing)!"); |
2a91aa39 AB |
379 | return -1; |
380 | } | |
381 | ||
77ff72d5 | 382 | static void ccid2_hc_tx_kill_rto_timer(struct sock *sk) |
2a91aa39 | 383 | { |
77ff72d5 AB |
384 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); |
385 | ||
1fb87509 | 386 | sk_stop_timer(sk, &hctx->rtotimer); |
77ff72d5 | 387 | ccid2_pr_debug("deleted RTO timer\n"); |
2a91aa39 AB |
388 | } |
389 | ||
390 | static inline void ccid2_new_ack(struct sock *sk, | |
c9eaf173 | 391 | struct ccid2_seq *seqp, |
2a91aa39 AB |
392 | unsigned int *maxincr) |
393 | { | |
394 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); | |
395 | ||
1fb87509 GR |
396 | if (hctx->cwnd < hctx->ssthresh) { |
397 | if (*maxincr > 0 && ++hctx->packets_acked == 2) { | |
398 | hctx->cwnd += 1; | |
399 | *maxincr -= 1; | |
400 | hctx->packets_acked = 0; | |
2a91aa39 | 401 | } |
1fb87509 GR |
402 | } else if (++hctx->packets_acked >= hctx->cwnd) { |
403 | hctx->cwnd += 1; | |
404 | hctx->packets_acked = 0; | |
2a91aa39 AB |
405 | } |
406 | ||
407 | /* update RTO */ | |
1fb87509 GR |
408 | if (hctx->srtt == -1 || |
409 | time_after(jiffies, hctx->lastrtt + hctx->srtt)) { | |
29651cda | 410 | unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent; |
2a91aa39 AB |
411 | int s; |
412 | ||
413 | /* first measurement */ | |
1fb87509 | 414 | if (hctx->srtt == -1) { |
2a91aa39 | 415 | ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n", |
8109b02b | 416 | r, jiffies, |
234af484 | 417 | (unsigned long long)seqp->ccid2s_seq); |
593f16aa | 418 | ccid2_change_srtt(hctx, r); |
1fb87509 | 419 | hctx->rttvar = r >> 1; |
c0c736db | 420 | } else { |
2a91aa39 | 421 | /* RTTVAR */ |
1fb87509 | 422 | long tmp = hctx->srtt - r; |
593f16aa AB |
423 | long srtt; |
424 | ||
2a91aa39 AB |
425 | if (tmp < 0) |
426 | tmp *= -1; | |
427 | ||
428 | tmp >>= 2; | |
1fb87509 GR |
429 | hctx->rttvar *= 3; |
430 | hctx->rttvar >>= 2; | |
431 | hctx->rttvar += tmp; | |
2a91aa39 AB |
432 | |
433 | /* SRTT */ | |
1fb87509 | 434 | srtt = hctx->srtt; |
593f16aa AB |
435 | srtt *= 7; |
436 | srtt >>= 3; | |
2a91aa39 | 437 | tmp = r >> 3; |
593f16aa AB |
438 | srtt += tmp; |
439 | ccid2_change_srtt(hctx, srtt); | |
2a91aa39 | 440 | } |
1fb87509 | 441 | s = hctx->rttvar << 2; |
2a91aa39 AB |
442 | /* clock granularity is 1 when based on jiffies */ |
443 | if (!s) | |
444 | s = 1; | |
1fb87509 | 445 | hctx->rto = hctx->srtt + s; |
2a91aa39 AB |
446 | |
447 | /* must be at least a second */ | |
1fb87509 | 448 | s = hctx->rto / HZ; |
2a91aa39 AB |
449 | /* DCCP doesn't require this [but I like it cuz my code sux] */ |
450 | #if 1 | |
451 | if (s < 1) | |
1fb87509 | 452 | hctx->rto = HZ; |
2a91aa39 AB |
453 | #endif |
454 | /* max 60 seconds */ | |
455 | if (s > 60) | |
1fb87509 | 456 | hctx->rto = HZ * 60; |
2a91aa39 | 457 | |
1fb87509 | 458 | hctx->lastrtt = jiffies; |
2a91aa39 AB |
459 | |
460 | ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n", | |
1fb87509 GR |
461 | hctx->srtt, hctx->rttvar, |
462 | hctx->rto, HZ, r); | |
2a91aa39 AB |
463 | } |
464 | ||
465 | /* we got a new ack, so re-start RTO timer */ | |
77ff72d5 | 466 | ccid2_hc_tx_kill_rto_timer(sk); |
2a91aa39 AB |
467 | ccid2_start_rto_timer(sk); |
468 | } | |
469 | ||
77ff72d5 | 470 | static void ccid2_hc_tx_dec_pipe(struct sock *sk) |
2a91aa39 | 471 | { |
77ff72d5 AB |
472 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); |
473 | ||
1fb87509 | 474 | if (hctx->pipe == 0) |
95b21d7e GR |
475 | DCCP_BUG("pipe == 0"); |
476 | else | |
1fb87509 | 477 | hctx->pipe--; |
2a91aa39 | 478 | |
1fb87509 | 479 | if (hctx->pipe == 0) |
77ff72d5 | 480 | ccid2_hc_tx_kill_rto_timer(sk); |
2a91aa39 AB |
481 | } |
482 | ||
d50ad163 | 483 | static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp) |
374bcf32 | 484 | { |
d50ad163 GR |
485 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); |
486 | ||
1fb87509 | 487 | if (time_before(seqp->ccid2s_sent, hctx->last_cong)) { |
374bcf32 AB |
488 | ccid2_pr_debug("Multiple losses in an RTT---treating as one\n"); |
489 | return; | |
490 | } | |
491 | ||
1fb87509 | 492 | hctx->last_cong = jiffies; |
374bcf32 | 493 | |
1fb87509 GR |
494 | hctx->cwnd = hctx->cwnd / 2 ? : 1U; |
495 | hctx->ssthresh = max(hctx->cwnd, 2U); | |
d50ad163 GR |
496 | |
497 | /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */ | |
1fb87509 GR |
498 | if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->cwnd) |
499 | ccid2_change_l_ack_ratio(sk, hctx->cwnd); | |
374bcf32 AB |
500 | } |
501 | ||
2a91aa39 AB |
502 | static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb) |
503 | { | |
504 | struct dccp_sock *dp = dccp_sk(sk); | |
505 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); | |
506 | u64 ackno, seqno; | |
507 | struct ccid2_seq *seqp; | |
508 | unsigned char *vector; | |
509 | unsigned char veclen; | |
510 | int offset = 0; | |
511 | int done = 0; | |
2a91aa39 AB |
512 | unsigned int maxincr = 0; |
513 | ||
514 | ccid2_hc_tx_check_sanity(hctx); | |
515 | /* check reverse path congestion */ | |
516 | seqno = DCCP_SKB_CB(skb)->dccpd_seq; | |
517 | ||
518 | /* XXX this whole "algorithm" is broken. Need to fix it to keep track | |
519 | * of the seqnos of the dupacks so that rpseq and rpdupack are correct | |
520 | * -sorbo. | |
521 | */ | |
522 | /* need to bootstrap */ | |
1fb87509 GR |
523 | if (hctx->rpdupack == -1) { |
524 | hctx->rpdupack = 0; | |
525 | hctx->rpseq = seqno; | |
c0c736db | 526 | } else { |
2a91aa39 | 527 | /* check if packet is consecutive */ |
1fb87509 GR |
528 | if (dccp_delta_seqno(hctx->rpseq, seqno) == 1) |
529 | hctx->rpseq = seqno; | |
2a91aa39 | 530 | /* it's a later packet */ |
1fb87509 GR |
531 | else if (after48(seqno, hctx->rpseq)) { |
532 | hctx->rpdupack++; | |
2a91aa39 AB |
533 | |
534 | /* check if we got enough dupacks */ | |
1fb87509 GR |
535 | if (hctx->rpdupack >= NUMDUPACK) { |
536 | hctx->rpdupack = -1; /* XXX lame */ | |
537 | hctx->rpseq = 0; | |
2a91aa39 | 538 | |
df054e1d | 539 | ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio); |
2a91aa39 AB |
540 | } |
541 | } | |
542 | } | |
543 | ||
544 | /* check forward path congestion */ | |
545 | /* still didn't send out new data packets */ | |
1fb87509 | 546 | if (hctx->seqh == hctx->seqt) |
2a91aa39 AB |
547 | return; |
548 | ||
549 | switch (DCCP_SKB_CB(skb)->dccpd_type) { | |
550 | case DCCP_PKT_ACK: | |
551 | case DCCP_PKT_DATAACK: | |
552 | break; | |
2a91aa39 AB |
553 | default: |
554 | return; | |
555 | } | |
556 | ||
557 | ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq; | |
1fb87509 GR |
558 | if (after48(ackno, hctx->high_ack)) |
559 | hctx->high_ack = ackno; | |
32aac18d | 560 | |
1fb87509 | 561 | seqp = hctx->seqt; |
32aac18d AB |
562 | while (before48(seqp->ccid2s_seq, ackno)) { |
563 | seqp = seqp->ccid2s_next; | |
1fb87509 GR |
564 | if (seqp == hctx->seqh) { |
565 | seqp = hctx->seqh->ccid2s_prev; | |
32aac18d AB |
566 | break; |
567 | } | |
568 | } | |
2a91aa39 | 569 | |
a3020025 GR |
570 | /* |
571 | * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2 | |
572 | * packets per acknowledgement. Rounding up avoids that cwnd is not | |
573 | * advanced when Ack Ratio is 1 and gives a slight edge otherwise. | |
2a91aa39 | 574 | */ |
1fb87509 | 575 | if (hctx->cwnd < hctx->ssthresh) |
a3020025 | 576 | maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2); |
2a91aa39 AB |
577 | |
578 | /* go through all ack vectors */ | |
579 | while ((offset = ccid2_ackvector(sk, skb, offset, | |
580 | &vector, &veclen)) != -1) { | |
581 | /* go through this ack vector */ | |
582 | while (veclen--) { | |
ff49e270 | 583 | u64 ackno_end_rl = SUB48(ackno, dccp_ackvec_runlen(vector)); |
2a91aa39 | 584 | |
234af484 RD |
585 | ccid2_pr_debug("ackvec start:%llu end:%llu\n", |
586 | (unsigned long long)ackno, | |
587 | (unsigned long long)ackno_end_rl); | |
2a91aa39 AB |
588 | /* if the seqno we are analyzing is larger than the |
589 | * current ackno, then move towards the tail of our | |
590 | * seqnos. | |
591 | */ | |
592 | while (after48(seqp->ccid2s_seq, ackno)) { | |
1fb87509 | 593 | if (seqp == hctx->seqt) { |
2a91aa39 AB |
594 | done = 1; |
595 | break; | |
596 | } | |
597 | seqp = seqp->ccid2s_prev; | |
598 | } | |
599 | if (done) | |
600 | break; | |
601 | ||
602 | /* check all seqnos in the range of the vector | |
603 | * run length | |
604 | */ | |
605 | while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) { | |
ff49e270 | 606 | const u8 state = dccp_ackvec_state(vector); |
2a91aa39 AB |
607 | |
608 | /* new packet received or marked */ | |
ff49e270 | 609 | if (state != DCCPAV_NOT_RECEIVED && |
2a91aa39 | 610 | !seqp->ccid2s_acked) { |
ff49e270 | 611 | if (state == DCCPAV_ECN_MARKED) |
d50ad163 | 612 | ccid2_congestion_event(sk, |
374bcf32 | 613 | seqp); |
ff49e270 | 614 | else |
2a91aa39 AB |
615 | ccid2_new_ack(sk, seqp, |
616 | &maxincr); | |
2a91aa39 AB |
617 | |
618 | seqp->ccid2s_acked = 1; | |
619 | ccid2_pr_debug("Got ack for %llu\n", | |
234af484 | 620 | (unsigned long long)seqp->ccid2s_seq); |
77ff72d5 | 621 | ccid2_hc_tx_dec_pipe(sk); |
2a91aa39 | 622 | } |
1fb87509 | 623 | if (seqp == hctx->seqt) { |
2a91aa39 AB |
624 | done = 1; |
625 | break; | |
626 | } | |
3de5489f | 627 | seqp = seqp->ccid2s_prev; |
2a91aa39 AB |
628 | } |
629 | if (done) | |
630 | break; | |
631 | ||
cfbbeabc | 632 | ackno = SUB48(ackno_end_rl, 1); |
2a91aa39 AB |
633 | vector++; |
634 | } | |
635 | if (done) | |
636 | break; | |
637 | } | |
638 | ||
639 | /* The state about what is acked should be correct now | |
640 | * Check for NUMDUPACK | |
641 | */ | |
1fb87509 GR |
642 | seqp = hctx->seqt; |
643 | while (before48(seqp->ccid2s_seq, hctx->high_ack)) { | |
32aac18d | 644 | seqp = seqp->ccid2s_next; |
1fb87509 GR |
645 | if (seqp == hctx->seqh) { |
646 | seqp = hctx->seqh->ccid2s_prev; | |
32aac18d AB |
647 | break; |
648 | } | |
649 | } | |
2a91aa39 AB |
650 | done = 0; |
651 | while (1) { | |
652 | if (seqp->ccid2s_acked) { | |
653 | done++; | |
63df18ad | 654 | if (done == NUMDUPACK) |
2a91aa39 | 655 | break; |
2a91aa39 | 656 | } |
1fb87509 | 657 | if (seqp == hctx->seqt) |
2a91aa39 | 658 | break; |
2a91aa39 AB |
659 | seqp = seqp->ccid2s_prev; |
660 | } | |
661 | ||
662 | /* If there are at least 3 acknowledgements, anything unacknowledged | |
663 | * below the last sequence number is considered lost | |
664 | */ | |
63df18ad | 665 | if (done == NUMDUPACK) { |
2a91aa39 AB |
666 | struct ccid2_seq *last_acked = seqp; |
667 | ||
668 | /* check for lost packets */ | |
669 | while (1) { | |
670 | if (!seqp->ccid2s_acked) { | |
374bcf32 | 671 | ccid2_pr_debug("Packet lost: %llu\n", |
234af484 | 672 | (unsigned long long)seqp->ccid2s_seq); |
374bcf32 AB |
673 | /* XXX need to traverse from tail -> head in |
674 | * order to detect multiple congestion events in | |
675 | * one ack vector. | |
676 | */ | |
d50ad163 | 677 | ccid2_congestion_event(sk, seqp); |
77ff72d5 | 678 | ccid2_hc_tx_dec_pipe(sk); |
2a91aa39 | 679 | } |
1fb87509 | 680 | if (seqp == hctx->seqt) |
2a91aa39 AB |
681 | break; |
682 | seqp = seqp->ccid2s_prev; | |
683 | } | |
684 | ||
1fb87509 | 685 | hctx->seqt = last_acked; |
2a91aa39 AB |
686 | } |
687 | ||
688 | /* trim acked packets in tail */ | |
1fb87509 GR |
689 | while (hctx->seqt != hctx->seqh) { |
690 | if (!hctx->seqt->ccid2s_acked) | |
2a91aa39 AB |
691 | break; |
692 | ||
1fb87509 | 693 | hctx->seqt = hctx->seqt->ccid2s_next; |
2a91aa39 AB |
694 | } |
695 | ||
2a91aa39 AB |
696 | ccid2_hc_tx_check_sanity(hctx); |
697 | } | |
698 | ||
91f0ebf7 | 699 | static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk) |
2a91aa39 | 700 | { |
c9eaf173 | 701 | struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid); |
b00d2bbc GR |
702 | struct dccp_sock *dp = dccp_sk(sk); |
703 | u32 max_ratio; | |
2a91aa39 | 704 | |
b00d2bbc | 705 | /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */ |
1fb87509 | 706 | hctx->ssthresh = ~0U; |
2a91aa39 | 707 | |
b00d2bbc GR |
708 | /* |
709 | * RFC 4341, 5: "The cwnd parameter is initialized to at most four | |
710 | * packets for new connections, following the rules from [RFC3390]". | |
711 | * We need to convert the bytes of RFC3390 into the packets of RFC 4341. | |
712 | */ | |
1fb87509 | 713 | hctx->cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U); |
b00d2bbc GR |
714 | |
715 | /* Make sure that Ack Ratio is enabled and within bounds. */ | |
1fb87509 | 716 | max_ratio = DIV_ROUND_UP(hctx->cwnd, 2); |
b00d2bbc GR |
717 | if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio) |
718 | dp->dccps_l_ack_ratio = max_ratio; | |
719 | ||
2a91aa39 | 720 | /* XXX init ~ to window size... */ |
cd1f7d34 | 721 | if (ccid2_hc_tx_alloc_seq(hctx)) |
2a91aa39 | 722 | return -ENOMEM; |
91f0ebf7 | 723 | |
1fb87509 | 724 | hctx->rto = 3 * HZ; |
593f16aa | 725 | ccid2_change_srtt(hctx, -1); |
1fb87509 GR |
726 | hctx->rttvar = -1; |
727 | hctx->rpdupack = -1; | |
728 | hctx->last_cong = jiffies; | |
729 | setup_timer(&hctx->rtotimer, ccid2_hc_tx_rto_expire, (unsigned long)sk); | |
2a91aa39 AB |
730 | |
731 | ccid2_hc_tx_check_sanity(hctx); | |
732 | return 0; | |
733 | } | |
734 | ||
735 | static void ccid2_hc_tx_exit(struct sock *sk) | |
736 | { | |
c9eaf173 | 737 | struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk); |
07978aab | 738 | int i; |
2a91aa39 | 739 | |
77ff72d5 | 740 | ccid2_hc_tx_kill_rto_timer(sk); |
07978aab | 741 | |
1fb87509 GR |
742 | for (i = 0; i < hctx->seqbufc; i++) |
743 | kfree(hctx->seqbuf[i]); | |
744 | hctx->seqbufc = 0; | |
2a91aa39 AB |
745 | } |
746 | ||
747 | static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb) | |
748 | { | |
749 | const struct dccp_sock *dp = dccp_sk(sk); | |
750 | struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk); | |
751 | ||
752 | switch (DCCP_SKB_CB(skb)->dccpd_type) { | |
753 | case DCCP_PKT_DATA: | |
754 | case DCCP_PKT_DATAACK: | |
1fb87509 GR |
755 | hcrx->data++; |
756 | if (hcrx->data >= dp->dccps_r_ack_ratio) { | |
2a91aa39 | 757 | dccp_send_ack(sk); |
1fb87509 | 758 | hcrx->data = 0; |
2a91aa39 AB |
759 | } |
760 | break; | |
761 | } | |
762 | } | |
763 | ||
91f0ebf7 | 764 | static struct ccid_operations ccid2 = { |
3dd9a7c3 | 765 | .ccid_id = DCCPC_CCID2, |
84a97b0a | 766 | .ccid_name = "TCP-like", |
2a91aa39 | 767 | .ccid_owner = THIS_MODULE, |
91f0ebf7 | 768 | .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock), |
2a91aa39 AB |
769 | .ccid_hc_tx_init = ccid2_hc_tx_init, |
770 | .ccid_hc_tx_exit = ccid2_hc_tx_exit, | |
771 | .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet, | |
772 | .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent, | |
773 | .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv, | |
91f0ebf7 | 774 | .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock), |
2a91aa39 AB |
775 | .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv, |
776 | }; | |
777 | ||
84116716 | 778 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
43264991 | 779 | module_param(ccid2_debug, bool, 0644); |
2a91aa39 | 780 | MODULE_PARM_DESC(ccid2_debug, "Enable debug messages"); |
84116716 | 781 | #endif |
2a91aa39 AB |
782 | |
783 | static __init int ccid2_module_init(void) | |
784 | { | |
785 | return ccid_register(&ccid2); | |
786 | } | |
787 | module_init(ccid2_module_init); | |
788 | ||
789 | static __exit void ccid2_module_exit(void) | |
790 | { | |
791 | ccid_unregister(&ccid2); | |
792 | } | |
793 | module_exit(ccid2_module_exit); | |
794 | ||
795 | MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>"); | |
c0c736db | 796 | MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID"); |
2a91aa39 AB |
797 | MODULE_LICENSE("GPL"); |
798 | MODULE_ALIAS("net-dccp-ccid-2"); |