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