]>
Commit | Line | Data |
---|---|---|
2a91aa39 | 1 | /* |
2a91aa39 AB |
2 | * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk> |
3 | * | |
4 | * Changes to meet Linux coding standards, and DCCP infrastructure fixes. | |
5 | * | |
6 | * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | /* | |
0e64e94e | 24 | * This implementation should follow RFC 4341 |
2a91aa39 | 25 | */ |
5a0e3ad6 | 26 | #include <linux/slab.h> |
e8ef967a | 27 | #include "../feat.h" |
2a91aa39 AB |
28 | #include "ccid2.h" |
29 | ||
2a91aa39 | 30 | |
8d424f6c | 31 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
84116716 GR |
32 | static int ccid2_debug; |
33 | #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a) | |
2a91aa39 | 34 | #else |
84116716 | 35 | #define ccid2_pr_debug(format, a...) |
2a91aa39 AB |
36 | #endif |
37 | ||
77d2dd93 | 38 | static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc) |
07978aab AB |
39 | { |
40 | struct ccid2_seq *seqp; | |
41 | int i; | |
42 | ||
43 | /* check if we have space to preserve the pointer to the buffer */ | |
77d2dd93 GR |
44 | if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) / |
45 | sizeof(struct ccid2_seq *))) | |
07978aab AB |
46 | return -ENOMEM; |
47 | ||
48 | /* allocate buffer and initialize linked list */ | |
cd1f7d34 | 49 | seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any()); |
07978aab AB |
50 | if (seqp == NULL) |
51 | return -ENOMEM; | |
52 | ||
cd1f7d34 | 53 | for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) { |
07978aab AB |
54 | seqp[i].ccid2s_next = &seqp[i + 1]; |
55 | seqp[i + 1].ccid2s_prev = &seqp[i]; | |
56 | } | |
cd1f7d34 GR |
57 | seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp; |
58 | seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1]; | |
07978aab AB |
59 | |
60 | /* This is the first allocation. Initiate the head and tail. */ | |
77d2dd93 GR |
61 | if (hc->tx_seqbufc == 0) |
62 | hc->tx_seqh = hc->tx_seqt = seqp; | |
07978aab AB |
63 | else { |
64 | /* link the existing list with the one we just created */ | |
77d2dd93 GR |
65 | hc->tx_seqh->ccid2s_next = seqp; |
66 | seqp->ccid2s_prev = hc->tx_seqh; | |
07978aab | 67 | |
77d2dd93 GR |
68 | hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1]; |
69 | seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt; | |
07978aab AB |
70 | } |
71 | ||
72 | /* store the original pointer to the buffer so we can free it */ | |
77d2dd93 GR |
73 | hc->tx_seqbuf[hc->tx_seqbufc] = seqp; |
74 | hc->tx_seqbufc++; | |
07978aab AB |
75 | |
76 | return 0; | |
77 | } | |
78 | ||
6b57c93d | 79 | static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb) |
2a91aa39 | 80 | { |
77d2dd93 | 81 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
410e27a4 | 82 | |
77d2dd93 | 83 | if (hc->tx_pipe < hc->tx_cwnd) |
410e27a4 GR |
84 | return 0; |
85 | ||
86 | return 1; /* XXX CCID should dequeue when ready instead of polling */ | |
2a91aa39 AB |
87 | } |
88 | ||
df054e1d | 89 | static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val) |
2a91aa39 AB |
90 | { |
91 | struct dccp_sock *dp = dccp_sk(sk); | |
b1c00fe3 | 92 | u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2); |
d50ad163 | 93 | |
2a91aa39 | 94 | /* |
d50ad163 GR |
95 | * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from |
96 | * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always | |
97 | * acceptable since this causes starvation/deadlock whenever cwnd < 2. | |
98 | * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled). | |
2a91aa39 | 99 | */ |
d50ad163 GR |
100 | if (val == 0 || val > max_ratio) { |
101 | DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio); | |
102 | val = max_ratio; | |
2a91aa39 | 103 | } |
e8ef967a GR |
104 | if (val > DCCPF_ACK_RATIO_MAX) |
105 | val = DCCPF_ACK_RATIO_MAX; | |
2a91aa39 | 106 | |
d50ad163 GR |
107 | if (val == dp->dccps_l_ack_ratio) |
108 | return; | |
109 | ||
df054e1d | 110 | ccid2_pr_debug("changing local ack ratio to %u\n", val); |
2a91aa39 AB |
111 | dp->dccps_l_ack_ratio = val; |
112 | } | |
113 | ||
2a91aa39 AB |
114 | static void ccid2_hc_tx_rto_expire(unsigned long data) |
115 | { | |
116 | struct sock *sk = (struct sock *)data; | |
77d2dd93 | 117 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
2a91aa39 | 118 | |
2a91aa39 AB |
119 | bh_lock_sock(sk); |
120 | if (sock_owned_by_user(sk)) { | |
77d2dd93 | 121 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5); |
2a91aa39 AB |
122 | goto out; |
123 | } | |
124 | ||
125 | ccid2_pr_debug("RTO_EXPIRE\n"); | |
126 | ||
2a91aa39 | 127 | /* back-off timer */ |
77d2dd93 | 128 | hc->tx_rto <<= 1; |
231cc2aa GR |
129 | if (hc->tx_rto > DCCP_RTO_MAX) |
130 | hc->tx_rto = DCCP_RTO_MAX; | |
410e27a4 | 131 | |
d26eeb07 | 132 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto); |
2a91aa39 | 133 | |
2a91aa39 | 134 | /* adjust pipe, cwnd etc */ |
77d2dd93 GR |
135 | hc->tx_ssthresh = hc->tx_cwnd / 2; |
136 | if (hc->tx_ssthresh < 2) | |
137 | hc->tx_ssthresh = 2; | |
67b67e36 GR |
138 | hc->tx_cwnd = 1; |
139 | hc->tx_pipe = 0; | |
2a91aa39 AB |
140 | |
141 | /* clear state about stuff we sent */ | |
77d2dd93 GR |
142 | hc->tx_seqt = hc->tx_seqh; |
143 | hc->tx_packets_acked = 0; | |
2a91aa39 AB |
144 | |
145 | /* clear ack ratio state. */ | |
77d2dd93 GR |
146 | hc->tx_rpseq = 0; |
147 | hc->tx_rpdupack = -1; | |
2a91aa39 | 148 | ccid2_change_l_ack_ratio(sk, 1); |
2a91aa39 AB |
149 | out: |
150 | bh_unlock_sock(sk); | |
77ff72d5 | 151 | sock_put(sk); |
2a91aa39 AB |
152 | } |
153 | ||
baf9e782 | 154 | static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len) |
2a91aa39 AB |
155 | { |
156 | struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 157 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
07978aab | 158 | struct ccid2_seq *next; |
2a91aa39 | 159 | |
77d2dd93 | 160 | hc->tx_pipe++; |
2a91aa39 | 161 | |
77d2dd93 GR |
162 | hc->tx_seqh->ccid2s_seq = dp->dccps_gss; |
163 | hc->tx_seqh->ccid2s_acked = 0; | |
d82b6f85 | 164 | hc->tx_seqh->ccid2s_sent = ccid2_time_stamp; |
2a91aa39 | 165 | |
77d2dd93 | 166 | next = hc->tx_seqh->ccid2s_next; |
07978aab | 167 | /* check if we need to alloc more space */ |
77d2dd93 GR |
168 | if (next == hc->tx_seqt) { |
169 | if (ccid2_hc_tx_alloc_seq(hc)) { | |
7d9e8931 GR |
170 | DCCP_CRIT("packet history - out of memory!"); |
171 | /* FIXME: find a more graceful way to bail out */ | |
172 | return; | |
173 | } | |
77d2dd93 GR |
174 | next = hc->tx_seqh->ccid2s_next; |
175 | BUG_ON(next == hc->tx_seqt); | |
2a91aa39 | 176 | } |
77d2dd93 | 177 | hc->tx_seqh = next; |
07978aab | 178 | |
77d2dd93 | 179 | ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe); |
2a91aa39 | 180 | |
900bfed4 GR |
181 | /* |
182 | * FIXME: The code below is broken and the variables have been removed | |
183 | * from the socket struct. The `ackloss' variable was always set to 0, | |
184 | * and with arsent there are several problems: | |
185 | * (i) it doesn't just count the number of Acks, but all sent packets; | |
186 | * (ii) it is expressed in # of packets, not # of windows, so the | |
187 | * comparison below uses the wrong formula: Appendix A of RFC 4341 | |
188 | * comes up with the number K = cwnd / (R^2 - R) of consecutive windows | |
189 | * of data with no lost or marked Ack packets. If arsent were the # of | |
190 | * consecutive Acks received without loss, then Ack Ratio needs to be | |
191 | * decreased by 1 when | |
192 | * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2) | |
193 | * where cwnd / R is the number of Acks received per window of data | |
194 | * (cf. RFC 4341, App. A). The problems are that | |
195 | * - arsent counts other packets as well; | |
196 | * - the comparison uses a formula different from RFC 4341; | |
197 | * - computing a cubic/quadratic equation each time is too complicated. | |
198 | * Hence a different algorithm is needed. | |
199 | */ | |
200 | #if 0 | |
2a91aa39 | 201 | /* Ack Ratio. Need to maintain a concept of how many windows we sent */ |
77d2dd93 | 202 | hc->tx_arsent++; |
2a91aa39 | 203 | /* We had an ack loss in this window... */ |
77d2dd93 GR |
204 | if (hc->tx_ackloss) { |
205 | if (hc->tx_arsent >= hc->tx_cwnd) { | |
206 | hc->tx_arsent = 0; | |
207 | hc->tx_ackloss = 0; | |
2a91aa39 | 208 | } |
c0c736db ACM |
209 | } else { |
210 | /* No acks lost up to now... */ | |
2a91aa39 AB |
211 | /* decrease ack ratio if enough packets were sent */ |
212 | if (dp->dccps_l_ack_ratio > 1) { | |
213 | /* XXX don't calculate denominator each time */ | |
c0c736db ACM |
214 | int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio - |
215 | dp->dccps_l_ack_ratio; | |
2a91aa39 | 216 | |
77d2dd93 | 217 | denom = hc->tx_cwnd * hc->tx_cwnd / denom; |
2a91aa39 | 218 | |
77d2dd93 | 219 | if (hc->tx_arsent >= denom) { |
2a91aa39 | 220 | ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1); |
77d2dd93 | 221 | hc->tx_arsent = 0; |
2a91aa39 | 222 | } |
c0c736db ACM |
223 | } else { |
224 | /* we can't increase ack ratio further [1] */ | |
77d2dd93 | 225 | hc->tx_arsent = 0; /* or maybe set it to cwnd*/ |
2a91aa39 AB |
226 | } |
227 | } | |
900bfed4 | 228 | #endif |
2a91aa39 | 229 | |
d26eeb07 | 230 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto); |
c0c736db | 231 | |
8d424f6c | 232 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
2a91aa39 | 233 | do { |
77d2dd93 | 234 | struct ccid2_seq *seqp = hc->tx_seqt; |
2a91aa39 | 235 | |
77d2dd93 | 236 | while (seqp != hc->tx_seqh) { |
d82b6f85 | 237 | ccid2_pr_debug("out seq=%llu acked=%d time=%u\n", |
8109b02b | 238 | (unsigned long long)seqp->ccid2s_seq, |
234af484 | 239 | seqp->ccid2s_acked, seqp->ccid2s_sent); |
2a91aa39 AB |
240 | seqp = seqp->ccid2s_next; |
241 | } | |
c0c736db | 242 | } while (0); |
2a91aa39 | 243 | ccid2_pr_debug("=========\n"); |
2a91aa39 AB |
244 | #endif |
245 | } | |
246 | ||
410e27a4 GR |
247 | /* XXX Lame code duplication! |
248 | * returns -1 if none was found. | |
249 | * else returns the next offset to use in the function call. | |
1435562d | 250 | */ |
410e27a4 GR |
251 | static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset, |
252 | unsigned char **vec, unsigned char *veclen) | |
1435562d | 253 | { |
410e27a4 GR |
254 | const struct dccp_hdr *dh = dccp_hdr(skb); |
255 | unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); | |
256 | unsigned char *opt_ptr; | |
257 | const unsigned char *opt_end = (unsigned char *)dh + | |
258 | (dh->dccph_doff * 4); | |
259 | unsigned char opt, len; | |
260 | unsigned char *value; | |
261 | ||
262 | BUG_ON(offset < 0); | |
263 | options += offset; | |
264 | opt_ptr = options; | |
265 | if (opt_ptr >= opt_end) | |
266 | return -1; | |
267 | ||
268 | while (opt_ptr != opt_end) { | |
269 | opt = *opt_ptr++; | |
270 | len = 0; | |
271 | value = NULL; | |
272 | ||
273 | /* Check if this isn't a single byte option */ | |
274 | if (opt > DCCPO_MAX_RESERVED) { | |
275 | if (opt_ptr == opt_end) | |
276 | goto out_invalid_option; | |
277 | ||
278 | len = *opt_ptr++; | |
279 | if (len < 3) | |
280 | goto out_invalid_option; | |
1435562d | 281 | /* |
410e27a4 GR |
282 | * Remove the type and len fields, leaving |
283 | * just the value size | |
1435562d | 284 | */ |
410e27a4 GR |
285 | len -= 2; |
286 | value = opt_ptr; | |
287 | opt_ptr += len; | |
1435562d | 288 | |
410e27a4 GR |
289 | if (opt_ptr > opt_end) |
290 | goto out_invalid_option; | |
1435562d GR |
291 | } |
292 | ||
410e27a4 GR |
293 | switch (opt) { |
294 | case DCCPO_ACK_VECTOR_0: | |
295 | case DCCPO_ACK_VECTOR_1: | |
296 | *vec = value; | |
297 | *veclen = len; | |
298 | return offset + (opt_ptr - options); | |
1435562d GR |
299 | } |
300 | } | |
301 | ||
410e27a4 | 302 | return -1; |
1435562d | 303 | |
410e27a4 GR |
304 | out_invalid_option: |
305 | DCCP_BUG("Invalid option - this should not happen (previous parsing)!"); | |
306 | return -1; | |
1435562d GR |
307 | } |
308 | ||
231cc2aa GR |
309 | /** |
310 | * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm | |
311 | * This code is almost identical with TCP's tcp_rtt_estimator(), since | |
312 | * - it has a higher sampling frequency (recommended by RFC 1323), | |
313 | * - the RTO does not collapse into RTT due to RTTVAR going towards zero, | |
314 | * - it is simple (cf. more complex proposals such as Eifel timer or research | |
315 | * which suggests that the gain should be set according to window size), | |
316 | * - in tests it was found to work well with CCID2 [gerrit]. | |
317 | */ | |
318 | static void ccid2_rtt_estimator(struct sock *sk, const long mrtt) | |
319 | { | |
320 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); | |
321 | long m = mrtt ? : 1; | |
322 | ||
323 | if (hc->tx_srtt == 0) { | |
324 | /* First measurement m */ | |
325 | hc->tx_srtt = m << 3; | |
326 | hc->tx_mdev = m << 1; | |
327 | ||
4886fcad | 328 | hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk)); |
231cc2aa | 329 | hc->tx_rttvar = hc->tx_mdev_max; |
4886fcad | 330 | |
231cc2aa GR |
331 | hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss; |
332 | } else { | |
333 | /* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */ | |
334 | m -= (hc->tx_srtt >> 3); | |
335 | hc->tx_srtt += m; | |
336 | ||
337 | /* Similarly, update scaled mdev with regard to |m| */ | |
338 | if (m < 0) { | |
339 | m = -m; | |
340 | m -= (hc->tx_mdev >> 2); | |
341 | /* | |
342 | * This neutralises RTO increase when RTT < SRTT - mdev | |
343 | * (see P. Sarolahti, A. Kuznetsov,"Congestion Control | |
344 | * in Linux TCP", USENIX 2002, pp. 49-62). | |
345 | */ | |
346 | if (m > 0) | |
347 | m >>= 3; | |
348 | } else { | |
349 | m -= (hc->tx_mdev >> 2); | |
350 | } | |
351 | hc->tx_mdev += m; | |
352 | ||
353 | if (hc->tx_mdev > hc->tx_mdev_max) { | |
354 | hc->tx_mdev_max = hc->tx_mdev; | |
355 | if (hc->tx_mdev_max > hc->tx_rttvar) | |
356 | hc->tx_rttvar = hc->tx_mdev_max; | |
357 | } | |
358 | ||
359 | /* | |
360 | * Decay RTTVAR at most once per flight, exploiting that | |
361 | * 1) pipe <= cwnd <= Sequence_Window = W (RFC 4340, 7.5.2) | |
362 | * 2) AWL = GSS-W+1 <= GAR <= GSS (RFC 4340, 7.5.1) | |
363 | * GAR is a useful bound for FlightSize = pipe. | |
364 | * AWL is probably too low here, as it over-estimates pipe. | |
365 | */ | |
366 | if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) { | |
367 | if (hc->tx_mdev_max < hc->tx_rttvar) | |
368 | hc->tx_rttvar -= (hc->tx_rttvar - | |
369 | hc->tx_mdev_max) >> 2; | |
370 | hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss; | |
4886fcad | 371 | hc->tx_mdev_max = tcp_rto_min(sk); |
231cc2aa GR |
372 | } |
373 | } | |
374 | ||
375 | /* | |
376 | * Set RTO from SRTT and RTTVAR | |
377 | * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms. | |
378 | * This agrees with RFC 4341, 5: | |
379 | * "Because DCCP does not retransmit data, DCCP does not require | |
380 | * TCP's recommended minimum timeout of one second". | |
381 | */ | |
382 | hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar; | |
383 | ||
384 | if (hc->tx_rto > DCCP_RTO_MAX) | |
385 | hc->tx_rto = DCCP_RTO_MAX; | |
386 | } | |
387 | ||
388 | static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp, | |
389 | unsigned int *maxincr) | |
374bcf32 | 390 | { |
77d2dd93 | 391 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
d50ad163 | 392 | |
77d2dd93 GR |
393 | if (hc->tx_cwnd < hc->tx_ssthresh) { |
394 | if (*maxincr > 0 && ++hc->tx_packets_acked == 2) { | |
395 | hc->tx_cwnd += 1; | |
396 | *maxincr -= 1; | |
397 | hc->tx_packets_acked = 0; | |
410e27a4 | 398 | } |
77d2dd93 GR |
399 | } else if (++hc->tx_packets_acked >= hc->tx_cwnd) { |
400 | hc->tx_cwnd += 1; | |
401 | hc->tx_packets_acked = 0; | |
374bcf32 | 402 | } |
231cc2aa GR |
403 | /* |
404 | * FIXME: RTT is sampled several times per acknowledgment (for each | |
405 | * entry in the Ack Vector), instead of once per Ack (as in TCP SACK). | |
406 | * This causes the RTT to be over-estimated, since the older entries | |
407 | * in the Ack Vector have earlier sending times. | |
408 | * The cleanest solution is to not use the ccid2s_sent field at all | |
409 | * and instead use DCCP timestamps: requires changes in other places. | |
410 | */ | |
d82b6f85 | 411 | ccid2_rtt_estimator(sk, ccid2_time_stamp - seqp->ccid2s_sent); |
410e27a4 GR |
412 | } |
413 | ||
414 | static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp) | |
415 | { | |
77d2dd93 | 416 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
410e27a4 | 417 | |
d82b6f85 | 418 | if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) { |
410e27a4 GR |
419 | ccid2_pr_debug("Multiple losses in an RTT---treating as one\n"); |
420 | return; | |
c8bf462b | 421 | } |
410e27a4 | 422 | |
d82b6f85 | 423 | hc->tx_last_cong = ccid2_time_stamp; |
410e27a4 | 424 | |
77d2dd93 GR |
425 | hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U; |
426 | hc->tx_ssthresh = max(hc->tx_cwnd, 2U); | |
410e27a4 GR |
427 | |
428 | /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */ | |
77d2dd93 GR |
429 | if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd) |
430 | ccid2_change_l_ack_ratio(sk, hc->tx_cwnd); | |
c8bf462b GR |
431 | } |
432 | ||
2a91aa39 AB |
433 | static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb) |
434 | { | |
435 | struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 436 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
2a91aa39 AB |
437 | u64 ackno, seqno; |
438 | struct ccid2_seq *seqp; | |
410e27a4 GR |
439 | unsigned char *vector; |
440 | unsigned char veclen; | |
441 | int offset = 0; | |
2a91aa39 | 442 | int done = 0; |
2a91aa39 AB |
443 | unsigned int maxincr = 0; |
444 | ||
2a91aa39 AB |
445 | /* check reverse path congestion */ |
446 | seqno = DCCP_SKB_CB(skb)->dccpd_seq; | |
447 | ||
448 | /* XXX this whole "algorithm" is broken. Need to fix it to keep track | |
449 | * of the seqnos of the dupacks so that rpseq and rpdupack are correct | |
450 | * -sorbo. | |
451 | */ | |
452 | /* need to bootstrap */ | |
77d2dd93 GR |
453 | if (hc->tx_rpdupack == -1) { |
454 | hc->tx_rpdupack = 0; | |
455 | hc->tx_rpseq = seqno; | |
c0c736db | 456 | } else { |
2a91aa39 | 457 | /* check if packet is consecutive */ |
77d2dd93 GR |
458 | if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1) |
459 | hc->tx_rpseq = seqno; | |
2a91aa39 | 460 | /* it's a later packet */ |
77d2dd93 GR |
461 | else if (after48(seqno, hc->tx_rpseq)) { |
462 | hc->tx_rpdupack++; | |
2a91aa39 AB |
463 | |
464 | /* check if we got enough dupacks */ | |
77d2dd93 GR |
465 | if (hc->tx_rpdupack >= NUMDUPACK) { |
466 | hc->tx_rpdupack = -1; /* XXX lame */ | |
467 | hc->tx_rpseq = 0; | |
2a91aa39 | 468 | |
df054e1d | 469 | ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio); |
2a91aa39 AB |
470 | } |
471 | } | |
472 | } | |
473 | ||
474 | /* check forward path congestion */ | |
410e27a4 | 475 | /* still didn't send out new data packets */ |
77d2dd93 | 476 | if (hc->tx_seqh == hc->tx_seqt) |
2a91aa39 AB |
477 | return; |
478 | ||
410e27a4 GR |
479 | switch (DCCP_SKB_CB(skb)->dccpd_type) { |
480 | case DCCP_PKT_ACK: | |
481 | case DCCP_PKT_DATAACK: | |
482 | break; | |
483 | default: | |
484 | return; | |
485 | } | |
2a91aa39 AB |
486 | |
487 | ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq; | |
77d2dd93 GR |
488 | if (after48(ackno, hc->tx_high_ack)) |
489 | hc->tx_high_ack = ackno; | |
32aac18d | 490 | |
77d2dd93 | 491 | seqp = hc->tx_seqt; |
32aac18d AB |
492 | while (before48(seqp->ccid2s_seq, ackno)) { |
493 | seqp = seqp->ccid2s_next; | |
77d2dd93 GR |
494 | if (seqp == hc->tx_seqh) { |
495 | seqp = hc->tx_seqh->ccid2s_prev; | |
32aac18d AB |
496 | break; |
497 | } | |
498 | } | |
2a91aa39 | 499 | |
a3020025 GR |
500 | /* |
501 | * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2 | |
502 | * packets per acknowledgement. Rounding up avoids that cwnd is not | |
503 | * advanced when Ack Ratio is 1 and gives a slight edge otherwise. | |
2a91aa39 | 504 | */ |
77d2dd93 | 505 | if (hc->tx_cwnd < hc->tx_ssthresh) |
a3020025 | 506 | maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2); |
2a91aa39 AB |
507 | |
508 | /* go through all ack vectors */ | |
410e27a4 GR |
509 | while ((offset = ccid2_ackvector(sk, skb, offset, |
510 | &vector, &veclen)) != -1) { | |
2a91aa39 | 511 | /* go through this ack vector */ |
410e27a4 GR |
512 | while (veclen--) { |
513 | const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK; | |
514 | u64 ackno_end_rl = SUB48(ackno, rl); | |
2a91aa39 | 515 | |
410e27a4 | 516 | ccid2_pr_debug("ackvec start:%llu end:%llu\n", |
234af484 | 517 | (unsigned long long)ackno, |
410e27a4 | 518 | (unsigned long long)ackno_end_rl); |
2a91aa39 AB |
519 | /* if the seqno we are analyzing is larger than the |
520 | * current ackno, then move towards the tail of our | |
521 | * seqnos. | |
522 | */ | |
523 | while (after48(seqp->ccid2s_seq, ackno)) { | |
77d2dd93 | 524 | if (seqp == hc->tx_seqt) { |
2a91aa39 AB |
525 | done = 1; |
526 | break; | |
527 | } | |
528 | seqp = seqp->ccid2s_prev; | |
529 | } | |
530 | if (done) | |
531 | break; | |
532 | ||
533 | /* check all seqnos in the range of the vector | |
534 | * run length | |
535 | */ | |
536 | while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) { | |
410e27a4 GR |
537 | const u8 state = *vector & |
538 | DCCP_ACKVEC_STATE_MASK; | |
2a91aa39 AB |
539 | |
540 | /* new packet received or marked */ | |
410e27a4 | 541 | if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED && |
2a91aa39 | 542 | !seqp->ccid2s_acked) { |
410e27a4 GR |
543 | if (state == |
544 | DCCP_ACKVEC_STATE_ECN_MARKED) { | |
d50ad163 | 545 | ccid2_congestion_event(sk, |
374bcf32 | 546 | seqp); |
410e27a4 | 547 | } else |
2a91aa39 AB |
548 | ccid2_new_ack(sk, seqp, |
549 | &maxincr); | |
2a91aa39 AB |
550 | |
551 | seqp->ccid2s_acked = 1; | |
552 | ccid2_pr_debug("Got ack for %llu\n", | |
234af484 | 553 | (unsigned long long)seqp->ccid2s_seq); |
c38c92a8 | 554 | hc->tx_pipe--; |
2a91aa39 | 555 | } |
77d2dd93 | 556 | if (seqp == hc->tx_seqt) { |
2a91aa39 AB |
557 | done = 1; |
558 | break; | |
559 | } | |
3de5489f | 560 | seqp = seqp->ccid2s_prev; |
2a91aa39 AB |
561 | } |
562 | if (done) | |
563 | break; | |
564 | ||
cfbbeabc | 565 | ackno = SUB48(ackno_end_rl, 1); |
410e27a4 | 566 | vector++; |
2a91aa39 AB |
567 | } |
568 | if (done) | |
569 | break; | |
570 | } | |
571 | ||
572 | /* The state about what is acked should be correct now | |
573 | * Check for NUMDUPACK | |
574 | */ | |
77d2dd93 GR |
575 | seqp = hc->tx_seqt; |
576 | while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) { | |
32aac18d | 577 | seqp = seqp->ccid2s_next; |
77d2dd93 GR |
578 | if (seqp == hc->tx_seqh) { |
579 | seqp = hc->tx_seqh->ccid2s_prev; | |
32aac18d AB |
580 | break; |
581 | } | |
582 | } | |
2a91aa39 AB |
583 | done = 0; |
584 | while (1) { | |
585 | if (seqp->ccid2s_acked) { | |
586 | done++; | |
63df18ad | 587 | if (done == NUMDUPACK) |
2a91aa39 | 588 | break; |
2a91aa39 | 589 | } |
77d2dd93 | 590 | if (seqp == hc->tx_seqt) |
2a91aa39 | 591 | break; |
2a91aa39 AB |
592 | seqp = seqp->ccid2s_prev; |
593 | } | |
594 | ||
595 | /* If there are at least 3 acknowledgements, anything unacknowledged | |
596 | * below the last sequence number is considered lost | |
597 | */ | |
63df18ad | 598 | if (done == NUMDUPACK) { |
2a91aa39 AB |
599 | struct ccid2_seq *last_acked = seqp; |
600 | ||
601 | /* check for lost packets */ | |
602 | while (1) { | |
603 | if (!seqp->ccid2s_acked) { | |
374bcf32 | 604 | ccid2_pr_debug("Packet lost: %llu\n", |
234af484 | 605 | (unsigned long long)seqp->ccid2s_seq); |
374bcf32 AB |
606 | /* XXX need to traverse from tail -> head in |
607 | * order to detect multiple congestion events in | |
608 | * one ack vector. | |
609 | */ | |
d50ad163 | 610 | ccid2_congestion_event(sk, seqp); |
c38c92a8 | 611 | hc->tx_pipe--; |
2a91aa39 | 612 | } |
77d2dd93 | 613 | if (seqp == hc->tx_seqt) |
2a91aa39 AB |
614 | break; |
615 | seqp = seqp->ccid2s_prev; | |
616 | } | |
617 | ||
77d2dd93 | 618 | hc->tx_seqt = last_acked; |
2a91aa39 AB |
619 | } |
620 | ||
621 | /* trim acked packets in tail */ | |
77d2dd93 GR |
622 | while (hc->tx_seqt != hc->tx_seqh) { |
623 | if (!hc->tx_seqt->ccid2s_acked) | |
2a91aa39 AB |
624 | break; |
625 | ||
77d2dd93 | 626 | hc->tx_seqt = hc->tx_seqt->ccid2s_next; |
2a91aa39 | 627 | } |
c38c92a8 GR |
628 | |
629 | /* restart RTO timer if not all outstanding data has been acked */ | |
630 | if (hc->tx_pipe == 0) | |
631 | sk_stop_timer(sk, &hc->tx_rtotimer); | |
632 | else | |
633 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto); | |
2a91aa39 AB |
634 | } |
635 | ||
91f0ebf7 | 636 | static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk) |
2a91aa39 | 637 | { |
77d2dd93 | 638 | struct ccid2_hc_tx_sock *hc = ccid_priv(ccid); |
b00d2bbc GR |
639 | struct dccp_sock *dp = dccp_sk(sk); |
640 | u32 max_ratio; | |
2a91aa39 | 641 | |
b00d2bbc | 642 | /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */ |
77d2dd93 | 643 | hc->tx_ssthresh = ~0U; |
2a91aa39 | 644 | |
22b71c8f GR |
645 | /* Use larger initial windows (RFC 4341, section 5). */ |
646 | hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache); | |
b00d2bbc GR |
647 | |
648 | /* Make sure that Ack Ratio is enabled and within bounds. */ | |
77d2dd93 | 649 | max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2); |
b00d2bbc GR |
650 | if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio) |
651 | dp->dccps_l_ack_ratio = max_ratio; | |
652 | ||
2a91aa39 | 653 | /* XXX init ~ to window size... */ |
77d2dd93 | 654 | if (ccid2_hc_tx_alloc_seq(hc)) |
2a91aa39 | 655 | return -ENOMEM; |
91f0ebf7 | 656 | |
231cc2aa | 657 | hc->tx_rto = DCCP_TIMEOUT_INIT; |
77d2dd93 | 658 | hc->tx_rpdupack = -1; |
d82b6f85 | 659 | hc->tx_last_cong = ccid2_time_stamp; |
77d2dd93 | 660 | setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire, |
410e27a4 | 661 | (unsigned long)sk); |
2a91aa39 AB |
662 | return 0; |
663 | } | |
664 | ||
665 | static void ccid2_hc_tx_exit(struct sock *sk) | |
666 | { | |
77d2dd93 | 667 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
07978aab | 668 | int i; |
2a91aa39 | 669 | |
d26eeb07 | 670 | sk_stop_timer(sk, &hc->tx_rtotimer); |
07978aab | 671 | |
77d2dd93 GR |
672 | for (i = 0; i < hc->tx_seqbufc; i++) |
673 | kfree(hc->tx_seqbuf[i]); | |
674 | hc->tx_seqbufc = 0; | |
2a91aa39 AB |
675 | } |
676 | ||
677 | static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb) | |
678 | { | |
679 | const struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 680 | struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk); |
2a91aa39 AB |
681 | |
682 | switch (DCCP_SKB_CB(skb)->dccpd_type) { | |
683 | case DCCP_PKT_DATA: | |
684 | case DCCP_PKT_DATAACK: | |
77d2dd93 GR |
685 | hc->rx_data++; |
686 | if (hc->rx_data >= dp->dccps_r_ack_ratio) { | |
2a91aa39 | 687 | dccp_send_ack(sk); |
77d2dd93 | 688 | hc->rx_data = 0; |
2a91aa39 AB |
689 | } |
690 | break; | |
691 | } | |
692 | } | |
693 | ||
ddebc973 | 694 | struct ccid_operations ccid2_ops = { |
410e27a4 GR |
695 | .ccid_id = DCCPC_CCID2, |
696 | .ccid_name = "TCP-like", | |
410e27a4 GR |
697 | .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock), |
698 | .ccid_hc_tx_init = ccid2_hc_tx_init, | |
699 | .ccid_hc_tx_exit = ccid2_hc_tx_exit, | |
700 | .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet, | |
701 | .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent, | |
702 | .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv, | |
703 | .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock), | |
704 | .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv, | |
2a91aa39 AB |
705 | }; |
706 | ||
84116716 | 707 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
43264991 | 708 | module_param(ccid2_debug, bool, 0644); |
ddebc973 | 709 | MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages"); |
84116716 | 710 | #endif |