]>
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 | { |
1c0e0a05 GR |
81 | if (ccid2_cwnd_network_limited(ccid2_hc_tx_sk(sk))) |
82 | return CCID_PACKET_WILL_DEQUEUE_LATER; | |
83 | return CCID_PACKET_SEND_AT_ONCE; | |
2a91aa39 AB |
84 | } |
85 | ||
df054e1d | 86 | static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val) |
2a91aa39 AB |
87 | { |
88 | struct dccp_sock *dp = dccp_sk(sk); | |
b1c00fe3 | 89 | u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2); |
d50ad163 | 90 | |
2a91aa39 | 91 | /* |
d50ad163 GR |
92 | * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from |
93 | * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always | |
94 | * acceptable since this causes starvation/deadlock whenever cwnd < 2. | |
95 | * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled). | |
2a91aa39 | 96 | */ |
d50ad163 GR |
97 | if (val == 0 || val > max_ratio) { |
98 | DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio); | |
99 | val = max_ratio; | |
2a91aa39 | 100 | } |
e8ef967a GR |
101 | if (val > DCCPF_ACK_RATIO_MAX) |
102 | val = DCCPF_ACK_RATIO_MAX; | |
2a91aa39 | 103 | |
d50ad163 GR |
104 | if (val == dp->dccps_l_ack_ratio) |
105 | return; | |
106 | ||
df054e1d | 107 | ccid2_pr_debug("changing local ack ratio to %u\n", val); |
2a91aa39 AB |
108 | dp->dccps_l_ack_ratio = val; |
109 | } | |
110 | ||
2a91aa39 AB |
111 | static void ccid2_hc_tx_rto_expire(unsigned long data) |
112 | { | |
113 | struct sock *sk = (struct sock *)data; | |
77d2dd93 | 114 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
1c0e0a05 | 115 | const bool sender_was_blocked = ccid2_cwnd_network_limited(hc); |
2a91aa39 | 116 | |
2a91aa39 AB |
117 | bh_lock_sock(sk); |
118 | if (sock_owned_by_user(sk)) { | |
77d2dd93 | 119 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5); |
2a91aa39 AB |
120 | goto out; |
121 | } | |
122 | ||
123 | ccid2_pr_debug("RTO_EXPIRE\n"); | |
124 | ||
2a91aa39 | 125 | /* back-off timer */ |
77d2dd93 | 126 | hc->tx_rto <<= 1; |
231cc2aa GR |
127 | if (hc->tx_rto > DCCP_RTO_MAX) |
128 | hc->tx_rto = DCCP_RTO_MAX; | |
410e27a4 | 129 | |
2a91aa39 | 130 | /* adjust pipe, cwnd etc */ |
77d2dd93 GR |
131 | hc->tx_ssthresh = hc->tx_cwnd / 2; |
132 | if (hc->tx_ssthresh < 2) | |
133 | hc->tx_ssthresh = 2; | |
67b67e36 GR |
134 | hc->tx_cwnd = 1; |
135 | hc->tx_pipe = 0; | |
2a91aa39 AB |
136 | |
137 | /* clear state about stuff we sent */ | |
77d2dd93 GR |
138 | hc->tx_seqt = hc->tx_seqh; |
139 | hc->tx_packets_acked = 0; | |
2a91aa39 AB |
140 | |
141 | /* clear ack ratio state. */ | |
77d2dd93 GR |
142 | hc->tx_rpseq = 0; |
143 | hc->tx_rpdupack = -1; | |
2a91aa39 | 144 | ccid2_change_l_ack_ratio(sk, 1); |
1c0e0a05 GR |
145 | |
146 | /* if we were blocked before, we may now send cwnd=1 packet */ | |
147 | if (sender_was_blocked) | |
148 | tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet); | |
149 | /* restart backed-off timer */ | |
150 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto); | |
2a91aa39 AB |
151 | out: |
152 | bh_unlock_sock(sk); | |
77ff72d5 | 153 | sock_put(sk); |
2a91aa39 AB |
154 | } |
155 | ||
baf9e782 | 156 | static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len) |
2a91aa39 AB |
157 | { |
158 | struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 159 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
07978aab | 160 | struct ccid2_seq *next; |
2a91aa39 | 161 | |
77d2dd93 | 162 | hc->tx_pipe++; |
2a91aa39 | 163 | |
77d2dd93 GR |
164 | hc->tx_seqh->ccid2s_seq = dp->dccps_gss; |
165 | hc->tx_seqh->ccid2s_acked = 0; | |
d82b6f85 | 166 | hc->tx_seqh->ccid2s_sent = ccid2_time_stamp; |
2a91aa39 | 167 | |
77d2dd93 | 168 | next = hc->tx_seqh->ccid2s_next; |
07978aab | 169 | /* check if we need to alloc more space */ |
77d2dd93 GR |
170 | if (next == hc->tx_seqt) { |
171 | if (ccid2_hc_tx_alloc_seq(hc)) { | |
7d9e8931 GR |
172 | DCCP_CRIT("packet history - out of memory!"); |
173 | /* FIXME: find a more graceful way to bail out */ | |
174 | return; | |
175 | } | |
77d2dd93 GR |
176 | next = hc->tx_seqh->ccid2s_next; |
177 | BUG_ON(next == hc->tx_seqt); | |
2a91aa39 | 178 | } |
77d2dd93 | 179 | hc->tx_seqh = next; |
07978aab | 180 | |
77d2dd93 | 181 | ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe); |
2a91aa39 | 182 | |
900bfed4 GR |
183 | /* |
184 | * FIXME: The code below is broken and the variables have been removed | |
185 | * from the socket struct. The `ackloss' variable was always set to 0, | |
186 | * and with arsent there are several problems: | |
187 | * (i) it doesn't just count the number of Acks, but all sent packets; | |
188 | * (ii) it is expressed in # of packets, not # of windows, so the | |
189 | * comparison below uses the wrong formula: Appendix A of RFC 4341 | |
190 | * comes up with the number K = cwnd / (R^2 - R) of consecutive windows | |
191 | * of data with no lost or marked Ack packets. If arsent were the # of | |
192 | * consecutive Acks received without loss, then Ack Ratio needs to be | |
193 | * decreased by 1 when | |
194 | * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2) | |
195 | * where cwnd / R is the number of Acks received per window of data | |
196 | * (cf. RFC 4341, App. A). The problems are that | |
197 | * - arsent counts other packets as well; | |
198 | * - the comparison uses a formula different from RFC 4341; | |
199 | * - computing a cubic/quadratic equation each time is too complicated. | |
200 | * Hence a different algorithm is needed. | |
201 | */ | |
202 | #if 0 | |
2a91aa39 | 203 | /* Ack Ratio. Need to maintain a concept of how many windows we sent */ |
77d2dd93 | 204 | hc->tx_arsent++; |
2a91aa39 | 205 | /* We had an ack loss in this window... */ |
77d2dd93 GR |
206 | if (hc->tx_ackloss) { |
207 | if (hc->tx_arsent >= hc->tx_cwnd) { | |
208 | hc->tx_arsent = 0; | |
209 | hc->tx_ackloss = 0; | |
2a91aa39 | 210 | } |
c0c736db ACM |
211 | } else { |
212 | /* No acks lost up to now... */ | |
2a91aa39 AB |
213 | /* decrease ack ratio if enough packets were sent */ |
214 | if (dp->dccps_l_ack_ratio > 1) { | |
215 | /* XXX don't calculate denominator each time */ | |
c0c736db ACM |
216 | int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio - |
217 | dp->dccps_l_ack_ratio; | |
2a91aa39 | 218 | |
77d2dd93 | 219 | denom = hc->tx_cwnd * hc->tx_cwnd / denom; |
2a91aa39 | 220 | |
77d2dd93 | 221 | if (hc->tx_arsent >= denom) { |
2a91aa39 | 222 | ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1); |
77d2dd93 | 223 | hc->tx_arsent = 0; |
2a91aa39 | 224 | } |
c0c736db ACM |
225 | } else { |
226 | /* we can't increase ack ratio further [1] */ | |
77d2dd93 | 227 | hc->tx_arsent = 0; /* or maybe set it to cwnd*/ |
2a91aa39 AB |
228 | } |
229 | } | |
900bfed4 | 230 | #endif |
2a91aa39 | 231 | |
d26eeb07 | 232 | sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto); |
c0c736db | 233 | |
8d424f6c | 234 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
2a91aa39 | 235 | do { |
77d2dd93 | 236 | struct ccid2_seq *seqp = hc->tx_seqt; |
2a91aa39 | 237 | |
77d2dd93 | 238 | while (seqp != hc->tx_seqh) { |
d82b6f85 | 239 | ccid2_pr_debug("out seq=%llu acked=%d time=%u\n", |
8109b02b | 240 | (unsigned long long)seqp->ccid2s_seq, |
234af484 | 241 | seqp->ccid2s_acked, seqp->ccid2s_sent); |
2a91aa39 AB |
242 | seqp = seqp->ccid2s_next; |
243 | } | |
c0c736db | 244 | } while (0); |
2a91aa39 | 245 | ccid2_pr_debug("=========\n"); |
2a91aa39 AB |
246 | #endif |
247 | } | |
248 | ||
410e27a4 GR |
249 | /* XXX Lame code duplication! |
250 | * returns -1 if none was found. | |
251 | * else returns the next offset to use in the function call. | |
1435562d | 252 | */ |
410e27a4 GR |
253 | static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset, |
254 | unsigned char **vec, unsigned char *veclen) | |
1435562d | 255 | { |
410e27a4 GR |
256 | const struct dccp_hdr *dh = dccp_hdr(skb); |
257 | unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); | |
258 | unsigned char *opt_ptr; | |
259 | const unsigned char *opt_end = (unsigned char *)dh + | |
260 | (dh->dccph_doff * 4); | |
261 | unsigned char opt, len; | |
262 | unsigned char *value; | |
263 | ||
264 | BUG_ON(offset < 0); | |
265 | options += offset; | |
266 | opt_ptr = options; | |
267 | if (opt_ptr >= opt_end) | |
268 | return -1; | |
269 | ||
270 | while (opt_ptr != opt_end) { | |
271 | opt = *opt_ptr++; | |
272 | len = 0; | |
273 | value = NULL; | |
274 | ||
275 | /* Check if this isn't a single byte option */ | |
276 | if (opt > DCCPO_MAX_RESERVED) { | |
277 | if (opt_ptr == opt_end) | |
278 | goto out_invalid_option; | |
279 | ||
280 | len = *opt_ptr++; | |
281 | if (len < 3) | |
282 | goto out_invalid_option; | |
1435562d | 283 | /* |
410e27a4 GR |
284 | * Remove the type and len fields, leaving |
285 | * just the value size | |
1435562d | 286 | */ |
410e27a4 GR |
287 | len -= 2; |
288 | value = opt_ptr; | |
289 | opt_ptr += len; | |
1435562d | 290 | |
410e27a4 GR |
291 | if (opt_ptr > opt_end) |
292 | goto out_invalid_option; | |
1435562d GR |
293 | } |
294 | ||
410e27a4 GR |
295 | switch (opt) { |
296 | case DCCPO_ACK_VECTOR_0: | |
297 | case DCCPO_ACK_VECTOR_1: | |
298 | *vec = value; | |
299 | *veclen = len; | |
300 | return offset + (opt_ptr - options); | |
1435562d GR |
301 | } |
302 | } | |
303 | ||
410e27a4 | 304 | return -1; |
1435562d | 305 | |
410e27a4 GR |
306 | out_invalid_option: |
307 | DCCP_BUG("Invalid option - this should not happen (previous parsing)!"); | |
308 | return -1; | |
1435562d GR |
309 | } |
310 | ||
231cc2aa GR |
311 | /** |
312 | * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm | |
313 | * This code is almost identical with TCP's tcp_rtt_estimator(), since | |
314 | * - it has a higher sampling frequency (recommended by RFC 1323), | |
315 | * - the RTO does not collapse into RTT due to RTTVAR going towards zero, | |
316 | * - it is simple (cf. more complex proposals such as Eifel timer or research | |
317 | * which suggests that the gain should be set according to window size), | |
318 | * - in tests it was found to work well with CCID2 [gerrit]. | |
319 | */ | |
320 | static void ccid2_rtt_estimator(struct sock *sk, const long mrtt) | |
321 | { | |
322 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); | |
323 | long m = mrtt ? : 1; | |
324 | ||
325 | if (hc->tx_srtt == 0) { | |
326 | /* First measurement m */ | |
327 | hc->tx_srtt = m << 3; | |
328 | hc->tx_mdev = m << 1; | |
329 | ||
4886fcad | 330 | hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk)); |
231cc2aa | 331 | hc->tx_rttvar = hc->tx_mdev_max; |
4886fcad | 332 | |
231cc2aa GR |
333 | hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss; |
334 | } else { | |
335 | /* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */ | |
336 | m -= (hc->tx_srtt >> 3); | |
337 | hc->tx_srtt += m; | |
338 | ||
339 | /* Similarly, update scaled mdev with regard to |m| */ | |
340 | if (m < 0) { | |
341 | m = -m; | |
342 | m -= (hc->tx_mdev >> 2); | |
343 | /* | |
344 | * This neutralises RTO increase when RTT < SRTT - mdev | |
345 | * (see P. Sarolahti, A. Kuznetsov,"Congestion Control | |
346 | * in Linux TCP", USENIX 2002, pp. 49-62). | |
347 | */ | |
348 | if (m > 0) | |
349 | m >>= 3; | |
350 | } else { | |
351 | m -= (hc->tx_mdev >> 2); | |
352 | } | |
353 | hc->tx_mdev += m; | |
354 | ||
355 | if (hc->tx_mdev > hc->tx_mdev_max) { | |
356 | hc->tx_mdev_max = hc->tx_mdev; | |
357 | if (hc->tx_mdev_max > hc->tx_rttvar) | |
358 | hc->tx_rttvar = hc->tx_mdev_max; | |
359 | } | |
360 | ||
361 | /* | |
362 | * Decay RTTVAR at most once per flight, exploiting that | |
363 | * 1) pipe <= cwnd <= Sequence_Window = W (RFC 4340, 7.5.2) | |
364 | * 2) AWL = GSS-W+1 <= GAR <= GSS (RFC 4340, 7.5.1) | |
365 | * GAR is a useful bound for FlightSize = pipe. | |
366 | * AWL is probably too low here, as it over-estimates pipe. | |
367 | */ | |
368 | if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) { | |
369 | if (hc->tx_mdev_max < hc->tx_rttvar) | |
370 | hc->tx_rttvar -= (hc->tx_rttvar - | |
371 | hc->tx_mdev_max) >> 2; | |
372 | hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss; | |
4886fcad | 373 | hc->tx_mdev_max = tcp_rto_min(sk); |
231cc2aa GR |
374 | } |
375 | } | |
376 | ||
377 | /* | |
378 | * Set RTO from SRTT and RTTVAR | |
379 | * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms. | |
380 | * This agrees with RFC 4341, 5: | |
381 | * "Because DCCP does not retransmit data, DCCP does not require | |
382 | * TCP's recommended minimum timeout of one second". | |
383 | */ | |
384 | hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar; | |
385 | ||
386 | if (hc->tx_rto > DCCP_RTO_MAX) | |
387 | hc->tx_rto = DCCP_RTO_MAX; | |
388 | } | |
389 | ||
390 | static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp, | |
391 | unsigned int *maxincr) | |
374bcf32 | 392 | { |
77d2dd93 | 393 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
d50ad163 | 394 | |
77d2dd93 GR |
395 | if (hc->tx_cwnd < hc->tx_ssthresh) { |
396 | if (*maxincr > 0 && ++hc->tx_packets_acked == 2) { | |
397 | hc->tx_cwnd += 1; | |
398 | *maxincr -= 1; | |
399 | hc->tx_packets_acked = 0; | |
410e27a4 | 400 | } |
77d2dd93 GR |
401 | } else if (++hc->tx_packets_acked >= hc->tx_cwnd) { |
402 | hc->tx_cwnd += 1; | |
403 | hc->tx_packets_acked = 0; | |
374bcf32 | 404 | } |
231cc2aa GR |
405 | /* |
406 | * FIXME: RTT is sampled several times per acknowledgment (for each | |
407 | * entry in the Ack Vector), instead of once per Ack (as in TCP SACK). | |
408 | * This causes the RTT to be over-estimated, since the older entries | |
409 | * in the Ack Vector have earlier sending times. | |
410 | * The cleanest solution is to not use the ccid2s_sent field at all | |
411 | * and instead use DCCP timestamps: requires changes in other places. | |
412 | */ | |
d82b6f85 | 413 | ccid2_rtt_estimator(sk, ccid2_time_stamp - seqp->ccid2s_sent); |
410e27a4 GR |
414 | } |
415 | ||
416 | static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp) | |
417 | { | |
77d2dd93 | 418 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
410e27a4 | 419 | |
d82b6f85 | 420 | if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) { |
410e27a4 GR |
421 | ccid2_pr_debug("Multiple losses in an RTT---treating as one\n"); |
422 | return; | |
c8bf462b | 423 | } |
410e27a4 | 424 | |
d82b6f85 | 425 | hc->tx_last_cong = ccid2_time_stamp; |
410e27a4 | 426 | |
77d2dd93 GR |
427 | hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U; |
428 | hc->tx_ssthresh = max(hc->tx_cwnd, 2U); | |
410e27a4 GR |
429 | |
430 | /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */ | |
77d2dd93 GR |
431 | if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd) |
432 | ccid2_change_l_ack_ratio(sk, hc->tx_cwnd); | |
c8bf462b GR |
433 | } |
434 | ||
2a91aa39 AB |
435 | static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb) |
436 | { | |
437 | struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 438 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
1c0e0a05 | 439 | const bool sender_was_blocked = ccid2_cwnd_network_limited(hc); |
2a91aa39 AB |
440 | u64 ackno, seqno; |
441 | struct ccid2_seq *seqp; | |
410e27a4 GR |
442 | unsigned char *vector; |
443 | unsigned char veclen; | |
444 | int offset = 0; | |
2a91aa39 | 445 | int done = 0; |
2a91aa39 AB |
446 | unsigned int maxincr = 0; |
447 | ||
2a91aa39 AB |
448 | /* check reverse path congestion */ |
449 | seqno = DCCP_SKB_CB(skb)->dccpd_seq; | |
450 | ||
451 | /* XXX this whole "algorithm" is broken. Need to fix it to keep track | |
452 | * of the seqnos of the dupacks so that rpseq and rpdupack are correct | |
453 | * -sorbo. | |
454 | */ | |
455 | /* need to bootstrap */ | |
77d2dd93 GR |
456 | if (hc->tx_rpdupack == -1) { |
457 | hc->tx_rpdupack = 0; | |
458 | hc->tx_rpseq = seqno; | |
c0c736db | 459 | } else { |
2a91aa39 | 460 | /* check if packet is consecutive */ |
77d2dd93 GR |
461 | if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1) |
462 | hc->tx_rpseq = seqno; | |
2a91aa39 | 463 | /* it's a later packet */ |
77d2dd93 GR |
464 | else if (after48(seqno, hc->tx_rpseq)) { |
465 | hc->tx_rpdupack++; | |
2a91aa39 AB |
466 | |
467 | /* check if we got enough dupacks */ | |
77d2dd93 GR |
468 | if (hc->tx_rpdupack >= NUMDUPACK) { |
469 | hc->tx_rpdupack = -1; /* XXX lame */ | |
470 | hc->tx_rpseq = 0; | |
2a91aa39 | 471 | |
df054e1d | 472 | ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio); |
2a91aa39 AB |
473 | } |
474 | } | |
475 | } | |
476 | ||
477 | /* check forward path congestion */ | |
410e27a4 | 478 | /* still didn't send out new data packets */ |
77d2dd93 | 479 | if (hc->tx_seqh == hc->tx_seqt) |
2a91aa39 AB |
480 | return; |
481 | ||
410e27a4 GR |
482 | switch (DCCP_SKB_CB(skb)->dccpd_type) { |
483 | case DCCP_PKT_ACK: | |
484 | case DCCP_PKT_DATAACK: | |
485 | break; | |
486 | default: | |
487 | return; | |
488 | } | |
2a91aa39 AB |
489 | |
490 | ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq; | |
77d2dd93 GR |
491 | if (after48(ackno, hc->tx_high_ack)) |
492 | hc->tx_high_ack = ackno; | |
32aac18d | 493 | |
77d2dd93 | 494 | seqp = hc->tx_seqt; |
32aac18d AB |
495 | while (before48(seqp->ccid2s_seq, ackno)) { |
496 | seqp = seqp->ccid2s_next; | |
77d2dd93 GR |
497 | if (seqp == hc->tx_seqh) { |
498 | seqp = hc->tx_seqh->ccid2s_prev; | |
32aac18d AB |
499 | break; |
500 | } | |
501 | } | |
2a91aa39 | 502 | |
a3020025 GR |
503 | /* |
504 | * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2 | |
505 | * packets per acknowledgement. Rounding up avoids that cwnd is not | |
506 | * advanced when Ack Ratio is 1 and gives a slight edge otherwise. | |
2a91aa39 | 507 | */ |
77d2dd93 | 508 | if (hc->tx_cwnd < hc->tx_ssthresh) |
a3020025 | 509 | maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2); |
2a91aa39 AB |
510 | |
511 | /* go through all ack vectors */ | |
410e27a4 GR |
512 | while ((offset = ccid2_ackvector(sk, skb, offset, |
513 | &vector, &veclen)) != -1) { | |
2a91aa39 | 514 | /* go through this ack vector */ |
410e27a4 | 515 | while (veclen--) { |
f17a37c9 | 516 | u64 ackno_end_rl = SUB48(ackno, dccp_ackvec_runlen(vector)); |
2a91aa39 | 517 | |
410e27a4 | 518 | ccid2_pr_debug("ackvec start:%llu end:%llu\n", |
234af484 | 519 | (unsigned long long)ackno, |
410e27a4 | 520 | (unsigned long long)ackno_end_rl); |
2a91aa39 AB |
521 | /* if the seqno we are analyzing is larger than the |
522 | * current ackno, then move towards the tail of our | |
523 | * seqnos. | |
524 | */ | |
525 | while (after48(seqp->ccid2s_seq, ackno)) { | |
77d2dd93 | 526 | if (seqp == hc->tx_seqt) { |
2a91aa39 AB |
527 | done = 1; |
528 | break; | |
529 | } | |
530 | seqp = seqp->ccid2s_prev; | |
531 | } | |
532 | if (done) | |
533 | break; | |
534 | ||
535 | /* check all seqnos in the range of the vector | |
536 | * run length | |
537 | */ | |
538 | while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) { | |
f17a37c9 | 539 | const u8 state = dccp_ackvec_state(vector); |
2a91aa39 AB |
540 | |
541 | /* new packet received or marked */ | |
f17a37c9 | 542 | if (state != DCCPAV_NOT_RECEIVED && |
2a91aa39 | 543 | !seqp->ccid2s_acked) { |
f17a37c9 | 544 | if (state == DCCPAV_ECN_MARKED) |
d50ad163 | 545 | ccid2_congestion_event(sk, |
374bcf32 | 546 | seqp); |
f17a37c9 | 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); | |
1c0e0a05 GR |
634 | |
635 | /* check if incoming Acks allow pending packets to be sent */ | |
636 | if (sender_was_blocked && !ccid2_cwnd_network_limited(hc)) | |
637 | tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet); | |
2a91aa39 AB |
638 | } |
639 | ||
91f0ebf7 | 640 | static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk) |
2a91aa39 | 641 | { |
77d2dd93 | 642 | struct ccid2_hc_tx_sock *hc = ccid_priv(ccid); |
b00d2bbc GR |
643 | struct dccp_sock *dp = dccp_sk(sk); |
644 | u32 max_ratio; | |
2a91aa39 | 645 | |
b00d2bbc | 646 | /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */ |
77d2dd93 | 647 | hc->tx_ssthresh = ~0U; |
2a91aa39 | 648 | |
22b71c8f GR |
649 | /* Use larger initial windows (RFC 4341, section 5). */ |
650 | hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache); | |
b00d2bbc GR |
651 | |
652 | /* Make sure that Ack Ratio is enabled and within bounds. */ | |
77d2dd93 | 653 | max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2); |
b00d2bbc GR |
654 | if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio) |
655 | dp->dccps_l_ack_ratio = max_ratio; | |
656 | ||
2a91aa39 | 657 | /* XXX init ~ to window size... */ |
77d2dd93 | 658 | if (ccid2_hc_tx_alloc_seq(hc)) |
2a91aa39 | 659 | return -ENOMEM; |
91f0ebf7 | 660 | |
231cc2aa | 661 | hc->tx_rto = DCCP_TIMEOUT_INIT; |
77d2dd93 | 662 | hc->tx_rpdupack = -1; |
d82b6f85 | 663 | hc->tx_last_cong = ccid2_time_stamp; |
77d2dd93 | 664 | setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire, |
410e27a4 | 665 | (unsigned long)sk); |
2a91aa39 AB |
666 | return 0; |
667 | } | |
668 | ||
669 | static void ccid2_hc_tx_exit(struct sock *sk) | |
670 | { | |
77d2dd93 | 671 | struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk); |
07978aab | 672 | int i; |
2a91aa39 | 673 | |
d26eeb07 | 674 | sk_stop_timer(sk, &hc->tx_rtotimer); |
07978aab | 675 | |
77d2dd93 GR |
676 | for (i = 0; i < hc->tx_seqbufc; i++) |
677 | kfree(hc->tx_seqbuf[i]); | |
678 | hc->tx_seqbufc = 0; | |
2a91aa39 AB |
679 | } |
680 | ||
681 | static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb) | |
682 | { | |
683 | const struct dccp_sock *dp = dccp_sk(sk); | |
77d2dd93 | 684 | struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk); |
2a91aa39 AB |
685 | |
686 | switch (DCCP_SKB_CB(skb)->dccpd_type) { | |
687 | case DCCP_PKT_DATA: | |
688 | case DCCP_PKT_DATAACK: | |
77d2dd93 GR |
689 | hc->rx_data++; |
690 | if (hc->rx_data >= dp->dccps_r_ack_ratio) { | |
2a91aa39 | 691 | dccp_send_ack(sk); |
77d2dd93 | 692 | hc->rx_data = 0; |
2a91aa39 AB |
693 | } |
694 | break; | |
695 | } | |
696 | } | |
697 | ||
ddebc973 | 698 | struct ccid_operations ccid2_ops = { |
410e27a4 GR |
699 | .ccid_id = DCCPC_CCID2, |
700 | .ccid_name = "TCP-like", | |
410e27a4 GR |
701 | .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock), |
702 | .ccid_hc_tx_init = ccid2_hc_tx_init, | |
703 | .ccid_hc_tx_exit = ccid2_hc_tx_exit, | |
704 | .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet, | |
705 | .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent, | |
706 | .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv, | |
707 | .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock), | |
708 | .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv, | |
2a91aa39 AB |
709 | }; |
710 | ||
84116716 | 711 | #ifdef CONFIG_IP_DCCP_CCID2_DEBUG |
43264991 | 712 | module_param(ccid2_debug, bool, 0644); |
ddebc973 | 713 | MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages"); |
84116716 | 714 | #endif |