xref: /linux/net/dccp/ccids/ccid2.c (revision d2a4a07190f42e4f82805daf58e708400b703f1c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
4  *
5  *  Changes to meet Linux coding standards, and DCCP infrastructure fixes.
6  *
7  *  Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
8  */
9 
10 /*
11  * This implementation should follow RFC 4341
12  */
13 #include <linux/slab.h>
14 #include "../feat.h"
15 #include "ccid2.h"
16 
17 
18 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
19 static bool ccid2_debug;
20 #define ccid2_pr_debug(format, a...)	DCCP_PR_DEBUG(ccid2_debug, format, ##a)
21 #else
22 #define ccid2_pr_debug(format, a...)
23 #endif
24 
25 static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
26 {
27 	struct ccid2_seq *seqp;
28 	int i;
29 
30 	/* check if we have space to preserve the pointer to the buffer */
31 	if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
32 			       sizeof(struct ccid2_seq *)))
33 		return -ENOMEM;
34 
35 	/* allocate buffer and initialize linked list */
36 	seqp = kmalloc_array(CCID2_SEQBUF_LEN, sizeof(struct ccid2_seq),
37 			     gfp_any());
38 	if (seqp == NULL)
39 		return -ENOMEM;
40 
41 	for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
42 		seqp[i].ccid2s_next = &seqp[i + 1];
43 		seqp[i + 1].ccid2s_prev = &seqp[i];
44 	}
45 	seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
46 	seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
47 
48 	/* This is the first allocation.  Initiate the head and tail.  */
49 	if (hc->tx_seqbufc == 0)
50 		hc->tx_seqh = hc->tx_seqt = seqp;
51 	else {
52 		/* link the existing list with the one we just created */
53 		hc->tx_seqh->ccid2s_next = seqp;
54 		seqp->ccid2s_prev = hc->tx_seqh;
55 
56 		hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
57 		seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
58 	}
59 
60 	/* store the original pointer to the buffer so we can free it */
61 	hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
62 	hc->tx_seqbufc++;
63 
64 	return 0;
65 }
66 
67 static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
68 {
69 	if (ccid2_cwnd_network_limited(ccid2_hc_tx_sk(sk)))
70 		return CCID_PACKET_WILL_DEQUEUE_LATER;
71 	return CCID_PACKET_SEND_AT_ONCE;
72 }
73 
74 static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
75 {
76 	u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
77 
78 	/*
79 	 * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
80 	 * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
81 	 * acceptable since this causes starvation/deadlock whenever cwnd < 2.
82 	 * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
83 	 */
84 	if (val == 0 || val > max_ratio) {
85 		DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
86 		val = max_ratio;
87 	}
88 	dccp_feat_signal_nn_change(sk, DCCPF_ACK_RATIO,
89 				   min_t(u32, val, DCCPF_ACK_RATIO_MAX));
90 }
91 
92 static void ccid2_check_l_ack_ratio(struct sock *sk)
93 {
94 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
95 
96 	/*
97 	 * After a loss, idle period, application limited period, or RTO we
98 	 * need to check that the ack ratio is still less than the congestion
99 	 * window. Otherwise, we will send an entire congestion window of
100 	 * packets and got no response because we haven't sent ack ratio
101 	 * packets yet.
102 	 * If the ack ratio does need to be reduced, we reduce it to half of
103 	 * the congestion window (or 1 if that's zero) instead of to the
104 	 * congestion window. This prevents problems if one ack is lost.
105 	 */
106 	if (dccp_feat_nn_get(sk, DCCPF_ACK_RATIO) > hc->tx_cwnd)
107 		ccid2_change_l_ack_ratio(sk, hc->tx_cwnd/2 ? : 1U);
108 }
109 
110 static void ccid2_change_l_seq_window(struct sock *sk, u64 val)
111 {
112 	dccp_feat_signal_nn_change(sk, DCCPF_SEQUENCE_WINDOW,
113 				   clamp_val(val, DCCPF_SEQ_WMIN,
114 						  DCCPF_SEQ_WMAX));
115 }
116 
117 static void dccp_tasklet_schedule(struct sock *sk)
118 {
119 	struct tasklet_struct *t = &dccp_sk(sk)->dccps_xmitlet;
120 
121 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
122 		sock_hold(sk);
123 		__tasklet_schedule(t);
124 	}
125 }
126 
127 static void ccid2_hc_tx_rto_expire(struct timer_list *t)
128 {
129 	struct ccid2_hc_tx_sock *hc = from_timer(hc, t, tx_rtotimer);
130 	struct sock *sk = hc->sk;
131 	const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
132 
133 	bh_lock_sock(sk);
134 	if (sock_owned_by_user(sk)) {
135 		sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
136 		goto out;
137 	}
138 
139 	ccid2_pr_debug("RTO_EXPIRE\n");
140 
141 	if (sk->sk_state == DCCP_CLOSED)
142 		goto out;
143 
144 	/* back-off timer */
145 	hc->tx_rto <<= 1;
146 	if (hc->tx_rto > DCCP_RTO_MAX)
147 		hc->tx_rto = DCCP_RTO_MAX;
148 
149 	/* adjust pipe, cwnd etc */
150 	hc->tx_ssthresh = hc->tx_cwnd / 2;
151 	if (hc->tx_ssthresh < 2)
152 		hc->tx_ssthresh = 2;
153 	hc->tx_cwnd	= 1;
154 	hc->tx_pipe	= 0;
155 
156 	/* clear state about stuff we sent */
157 	hc->tx_seqt = hc->tx_seqh;
158 	hc->tx_packets_acked = 0;
159 
160 	/* clear ack ratio state. */
161 	hc->tx_rpseq    = 0;
162 	hc->tx_rpdupack = -1;
163 	ccid2_change_l_ack_ratio(sk, 1);
164 
165 	/* if we were blocked before, we may now send cwnd=1 packet */
166 	if (sender_was_blocked)
167 		dccp_tasklet_schedule(sk);
168 	/* restart backed-off timer */
169 	sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
170 out:
171 	bh_unlock_sock(sk);
172 	sock_put(sk);
173 }
174 
175 /*
176  *	Congestion window validation (RFC 2861).
177  */
178 static bool ccid2_do_cwv = true;
179 module_param(ccid2_do_cwv, bool, 0644);
180 MODULE_PARM_DESC(ccid2_do_cwv, "Perform RFC2861 Congestion Window Validation");
181 
182 /**
183  * ccid2_update_used_window  -  Track how much of cwnd is actually used
184  * @hc: socket to update window
185  * @new_wnd: new window values to add into the filter
186  *
187  * This is done in addition to CWV. The sender needs to have an idea of how many
188  * packets may be in flight, to set the local Sequence Window value accordingly
189  * (RFC 4340, 7.5.2). The CWV mechanism is exploited to keep track of the
190  * maximum-used window. We use an EWMA low-pass filter to filter out noise.
191  */
192 static void ccid2_update_used_window(struct ccid2_hc_tx_sock *hc, u32 new_wnd)
193 {
194 	hc->tx_expected_wnd = (3 * hc->tx_expected_wnd + new_wnd) / 4;
195 }
196 
197 /* This borrows the code of tcp_cwnd_application_limited() */
198 static void ccid2_cwnd_application_limited(struct sock *sk, const u32 now)
199 {
200 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
201 	/* don't reduce cwnd below the initial window (IW) */
202 	u32 init_win = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache),
203 	    win_used = max(hc->tx_cwnd_used, init_win);
204 
205 	if (win_used < hc->tx_cwnd) {
206 		hc->tx_ssthresh = max(hc->tx_ssthresh,
207 				     (hc->tx_cwnd >> 1) + (hc->tx_cwnd >> 2));
208 		hc->tx_cwnd = (hc->tx_cwnd + win_used) >> 1;
209 	}
210 	hc->tx_cwnd_used  = 0;
211 	hc->tx_cwnd_stamp = now;
212 
213 	ccid2_check_l_ack_ratio(sk);
214 }
215 
216 /* This borrows the code of tcp_cwnd_restart() */
217 static void ccid2_cwnd_restart(struct sock *sk, const u32 now)
218 {
219 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
220 	u32 cwnd = hc->tx_cwnd, restart_cwnd,
221 	    iwnd = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache);
222 	s32 delta = now - hc->tx_lsndtime;
223 
224 	hc->tx_ssthresh = max(hc->tx_ssthresh, (cwnd >> 1) + (cwnd >> 2));
225 
226 	/* don't reduce cwnd below the initial window (IW) */
227 	restart_cwnd = min(cwnd, iwnd);
228 
229 	while ((delta -= hc->tx_rto) >= 0 && cwnd > restart_cwnd)
230 		cwnd >>= 1;
231 	hc->tx_cwnd = max(cwnd, restart_cwnd);
232 	hc->tx_cwnd_stamp = now;
233 	hc->tx_cwnd_used  = 0;
234 
235 	ccid2_check_l_ack_ratio(sk);
236 }
237 
238 static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
239 {
240 	struct dccp_sock *dp = dccp_sk(sk);
241 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
242 	const u32 now = ccid2_jiffies32;
243 	struct ccid2_seq *next;
244 
245 	/* slow-start after idle periods (RFC 2581, RFC 2861) */
246 	if (ccid2_do_cwv && !hc->tx_pipe &&
247 	    (s32)(now - hc->tx_lsndtime) >= hc->tx_rto)
248 		ccid2_cwnd_restart(sk, now);
249 
250 	hc->tx_lsndtime = now;
251 	hc->tx_pipe    += 1;
252 
253 	/* see whether cwnd was fully used (RFC 2861), update expected window */
254 	if (ccid2_cwnd_network_limited(hc)) {
255 		ccid2_update_used_window(hc, hc->tx_cwnd);
256 		hc->tx_cwnd_used  = 0;
257 		hc->tx_cwnd_stamp = now;
258 	} else {
259 		if (hc->tx_pipe > hc->tx_cwnd_used)
260 			hc->tx_cwnd_used = hc->tx_pipe;
261 
262 		ccid2_update_used_window(hc, hc->tx_cwnd_used);
263 
264 		if (ccid2_do_cwv && (s32)(now - hc->tx_cwnd_stamp) >= hc->tx_rto)
265 			ccid2_cwnd_application_limited(sk, now);
266 	}
267 
268 	hc->tx_seqh->ccid2s_seq   = dp->dccps_gss;
269 	hc->tx_seqh->ccid2s_acked = 0;
270 	hc->tx_seqh->ccid2s_sent  = now;
271 
272 	next = hc->tx_seqh->ccid2s_next;
273 	/* check if we need to alloc more space */
274 	if (next == hc->tx_seqt) {
275 		if (ccid2_hc_tx_alloc_seq(hc)) {
276 			DCCP_CRIT("packet history - out of memory!");
277 			/* FIXME: find a more graceful way to bail out */
278 			return;
279 		}
280 		next = hc->tx_seqh->ccid2s_next;
281 		BUG_ON(next == hc->tx_seqt);
282 	}
283 	hc->tx_seqh = next;
284 
285 	ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
286 
287 	/*
288 	 * FIXME: The code below is broken and the variables have been removed
289 	 * from the socket struct. The `ackloss' variable was always set to 0,
290 	 * and with arsent there are several problems:
291 	 *  (i) it doesn't just count the number of Acks, but all sent packets;
292 	 *  (ii) it is expressed in # of packets, not # of windows, so the
293 	 *  comparison below uses the wrong formula: Appendix A of RFC 4341
294 	 *  comes up with the number K = cwnd / (R^2 - R) of consecutive windows
295 	 *  of data with no lost or marked Ack packets. If arsent were the # of
296 	 *  consecutive Acks received without loss, then Ack Ratio needs to be
297 	 *  decreased by 1 when
298 	 *	      arsent >=  K * cwnd / R  =  cwnd^2 / (R^3 - R^2)
299 	 *  where cwnd / R is the number of Acks received per window of data
300 	 *  (cf. RFC 4341, App. A). The problems are that
301 	 *  - arsent counts other packets as well;
302 	 *  - the comparison uses a formula different from RFC 4341;
303 	 *  - computing a cubic/quadratic equation each time is too complicated.
304 	 *  Hence a different algorithm is needed.
305 	 */
306 #if 0
307 	/* Ack Ratio.  Need to maintain a concept of how many windows we sent */
308 	hc->tx_arsent++;
309 	/* We had an ack loss in this window... */
310 	if (hc->tx_ackloss) {
311 		if (hc->tx_arsent >= hc->tx_cwnd) {
312 			hc->tx_arsent  = 0;
313 			hc->tx_ackloss = 0;
314 		}
315 	} else {
316 		/* No acks lost up to now... */
317 		/* decrease ack ratio if enough packets were sent */
318 		if (dp->dccps_l_ack_ratio > 1) {
319 			/* XXX don't calculate denominator each time */
320 			int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
321 				    dp->dccps_l_ack_ratio;
322 
323 			denom = hc->tx_cwnd * hc->tx_cwnd / denom;
324 
325 			if (hc->tx_arsent >= denom) {
326 				ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
327 				hc->tx_arsent = 0;
328 			}
329 		} else {
330 			/* we can't increase ack ratio further [1] */
331 			hc->tx_arsent = 0; /* or maybe set it to cwnd*/
332 		}
333 	}
334 #endif
335 
336 	sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
337 
338 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
339 	do {
340 		struct ccid2_seq *seqp = hc->tx_seqt;
341 
342 		while (seqp != hc->tx_seqh) {
343 			ccid2_pr_debug("out seq=%llu acked=%d time=%u\n",
344 				       (unsigned long long)seqp->ccid2s_seq,
345 				       seqp->ccid2s_acked, seqp->ccid2s_sent);
346 			seqp = seqp->ccid2s_next;
347 		}
348 	} while (0);
349 	ccid2_pr_debug("=========\n");
350 #endif
351 }
352 
353 /**
354  * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm
355  * @sk: socket to perform estimator on
356  * @mrtt: measured RTT
357  *
358  * This code is almost identical with TCP's tcp_rtt_estimator(), since
359  * - it has a higher sampling frequency (recommended by RFC 1323),
360  * - the RTO does not collapse into RTT due to RTTVAR going towards zero,
361  * - it is simple (cf. more complex proposals such as Eifel timer or research
362  *   which suggests that the gain should be set according to window size),
363  * - in tests it was found to work well with CCID2 [gerrit].
364  */
365 static void ccid2_rtt_estimator(struct sock *sk, const long mrtt)
366 {
367 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
368 	long m = mrtt ? : 1;
369 
370 	if (hc->tx_srtt == 0) {
371 		/* First measurement m */
372 		hc->tx_srtt = m << 3;
373 		hc->tx_mdev = m << 1;
374 
375 		hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk));
376 		hc->tx_rttvar   = hc->tx_mdev_max;
377 
378 		hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
379 	} else {
380 		/* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */
381 		m -= (hc->tx_srtt >> 3);
382 		hc->tx_srtt += m;
383 
384 		/* Similarly, update scaled mdev with regard to |m| */
385 		if (m < 0) {
386 			m = -m;
387 			m -= (hc->tx_mdev >> 2);
388 			/*
389 			 * This neutralises RTO increase when RTT < SRTT - mdev
390 			 * (see P. Sarolahti, A. Kuznetsov,"Congestion Control
391 			 * in Linux TCP", USENIX 2002, pp. 49-62).
392 			 */
393 			if (m > 0)
394 				m >>= 3;
395 		} else {
396 			m -= (hc->tx_mdev >> 2);
397 		}
398 		hc->tx_mdev += m;
399 
400 		if (hc->tx_mdev > hc->tx_mdev_max) {
401 			hc->tx_mdev_max = hc->tx_mdev;
402 			if (hc->tx_mdev_max > hc->tx_rttvar)
403 				hc->tx_rttvar = hc->tx_mdev_max;
404 		}
405 
406 		/*
407 		 * Decay RTTVAR at most once per flight, exploiting that
408 		 *  1) pipe <= cwnd <= Sequence_Window = W  (RFC 4340, 7.5.2)
409 		 *  2) AWL = GSS-W+1 <= GAR <= GSS          (RFC 4340, 7.5.1)
410 		 * GAR is a useful bound for FlightSize = pipe.
411 		 * AWL is probably too low here, as it over-estimates pipe.
412 		 */
413 		if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) {
414 			if (hc->tx_mdev_max < hc->tx_rttvar)
415 				hc->tx_rttvar -= (hc->tx_rttvar -
416 						  hc->tx_mdev_max) >> 2;
417 			hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
418 			hc->tx_mdev_max = tcp_rto_min(sk);
419 		}
420 	}
421 
422 	/*
423 	 * Set RTO from SRTT and RTTVAR
424 	 * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms.
425 	 * This agrees with RFC 4341, 5:
426 	 *	"Because DCCP does not retransmit data, DCCP does not require
427 	 *	 TCP's recommended minimum timeout of one second".
428 	 */
429 	hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar;
430 
431 	if (hc->tx_rto > DCCP_RTO_MAX)
432 		hc->tx_rto = DCCP_RTO_MAX;
433 }
434 
435 static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp,
436 			  unsigned int *maxincr)
437 {
438 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
439 	struct dccp_sock *dp = dccp_sk(sk);
440 	int r_seq_used = hc->tx_cwnd / dp->dccps_l_ack_ratio;
441 
442 	if (hc->tx_cwnd < dp->dccps_l_seq_win &&
443 	    r_seq_used < dp->dccps_r_seq_win) {
444 		if (hc->tx_cwnd < hc->tx_ssthresh) {
445 			if (*maxincr > 0 && ++hc->tx_packets_acked >= 2) {
446 				hc->tx_cwnd += 1;
447 				*maxincr    -= 1;
448 				hc->tx_packets_acked = 0;
449 			}
450 		} else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
451 			hc->tx_cwnd += 1;
452 			hc->tx_packets_acked = 0;
453 		}
454 	}
455 
456 	/*
457 	 * Adjust the local sequence window and the ack ratio to allow about
458 	 * 5 times the number of packets in the network (RFC 4340 7.5.2)
459 	 */
460 	if (r_seq_used * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_r_seq_win)
461 		ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio * 2);
462 	else if (r_seq_used * CCID2_WIN_CHANGE_FACTOR < dp->dccps_r_seq_win/2)
463 		ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio / 2 ? : 1U);
464 
465 	if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_l_seq_win)
466 		ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win * 2);
467 	else if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR < dp->dccps_l_seq_win/2)
468 		ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win / 2);
469 
470 	/*
471 	 * FIXME: RTT is sampled several times per acknowledgment (for each
472 	 * entry in the Ack Vector), instead of once per Ack (as in TCP SACK).
473 	 * This causes the RTT to be over-estimated, since the older entries
474 	 * in the Ack Vector have earlier sending times.
475 	 * The cleanest solution is to not use the ccid2s_sent field at all
476 	 * and instead use DCCP timestamps: requires changes in other places.
477 	 */
478 	ccid2_rtt_estimator(sk, ccid2_jiffies32 - seqp->ccid2s_sent);
479 }
480 
481 static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
482 {
483 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
484 
485 	if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) {
486 		ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
487 		return;
488 	}
489 
490 	hc->tx_last_cong = ccid2_jiffies32;
491 
492 	hc->tx_cwnd      = hc->tx_cwnd / 2 ? : 1U;
493 	hc->tx_ssthresh  = max(hc->tx_cwnd, 2U);
494 
495 	ccid2_check_l_ack_ratio(sk);
496 }
497 
498 static int ccid2_hc_tx_parse_options(struct sock *sk, u8 packet_type,
499 				     u8 option, u8 *optval, u8 optlen)
500 {
501 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
502 
503 	switch (option) {
504 	case DCCPO_ACK_VECTOR_0:
505 	case DCCPO_ACK_VECTOR_1:
506 		return dccp_ackvec_parsed_add(&hc->tx_av_chunks, optval, optlen,
507 					      option - DCCPO_ACK_VECTOR_0);
508 	}
509 	return 0;
510 }
511 
512 static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
513 {
514 	struct dccp_sock *dp = dccp_sk(sk);
515 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
516 	const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
517 	struct dccp_ackvec_parsed *avp;
518 	u64 ackno, seqno;
519 	struct ccid2_seq *seqp;
520 	int done = 0;
521 	unsigned int maxincr = 0;
522 
523 	/* check reverse path congestion */
524 	seqno = DCCP_SKB_CB(skb)->dccpd_seq;
525 
526 	/* XXX this whole "algorithm" is broken.  Need to fix it to keep track
527 	 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
528 	 * -sorbo.
529 	 */
530 	/* need to bootstrap */
531 	if (hc->tx_rpdupack == -1) {
532 		hc->tx_rpdupack = 0;
533 		hc->tx_rpseq    = seqno;
534 	} else {
535 		/* check if packet is consecutive */
536 		if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
537 			hc->tx_rpseq = seqno;
538 		/* it's a later packet */
539 		else if (after48(seqno, hc->tx_rpseq)) {
540 			hc->tx_rpdupack++;
541 
542 			/* check if we got enough dupacks */
543 			if (hc->tx_rpdupack >= NUMDUPACK) {
544 				hc->tx_rpdupack = -1; /* XXX lame */
545 				hc->tx_rpseq    = 0;
546 #ifdef __CCID2_COPES_GRACEFULLY_WITH_ACK_CONGESTION_CONTROL__
547 				/*
548 				 * FIXME: Ack Congestion Control is broken; in
549 				 * the current state instabilities occurred with
550 				 * Ack Ratios greater than 1; causing hang-ups
551 				 * and long RTO timeouts. This needs to be fixed
552 				 * before opening up dynamic changes. -- gerrit
553 				 */
554 				ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
555 #endif
556 			}
557 		}
558 	}
559 
560 	/* check forward path congestion */
561 	if (dccp_packet_without_ack(skb))
562 		return;
563 
564 	/* still didn't send out new data packets */
565 	if (hc->tx_seqh == hc->tx_seqt)
566 		goto done;
567 
568 	ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
569 	if (after48(ackno, hc->tx_high_ack))
570 		hc->tx_high_ack = ackno;
571 
572 	seqp = hc->tx_seqt;
573 	while (before48(seqp->ccid2s_seq, ackno)) {
574 		seqp = seqp->ccid2s_next;
575 		if (seqp == hc->tx_seqh) {
576 			seqp = hc->tx_seqh->ccid2s_prev;
577 			break;
578 		}
579 	}
580 
581 	/*
582 	 * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
583 	 * packets per acknowledgement. Rounding up avoids that cwnd is not
584 	 * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
585 	 */
586 	if (hc->tx_cwnd < hc->tx_ssthresh)
587 		maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
588 
589 	/* go through all ack vectors */
590 	list_for_each_entry(avp, &hc->tx_av_chunks, node) {
591 		/* go through this ack vector */
592 		for (; avp->len--; avp->vec++) {
593 			u64 ackno_end_rl = SUB48(ackno,
594 						 dccp_ackvec_runlen(avp->vec));
595 
596 			ccid2_pr_debug("ackvec %llu |%u,%u|\n",
597 				       (unsigned long long)ackno,
598 				       dccp_ackvec_state(avp->vec) >> 6,
599 				       dccp_ackvec_runlen(avp->vec));
600 			/* if the seqno we are analyzing is larger than the
601 			 * current ackno, then move towards the tail of our
602 			 * seqnos.
603 			 */
604 			while (after48(seqp->ccid2s_seq, ackno)) {
605 				if (seqp == hc->tx_seqt) {
606 					done = 1;
607 					break;
608 				}
609 				seqp = seqp->ccid2s_prev;
610 			}
611 			if (done)
612 				break;
613 
614 			/* check all seqnos in the range of the vector
615 			 * run length
616 			 */
617 			while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
618 				const u8 state = dccp_ackvec_state(avp->vec);
619 
620 				/* new packet received or marked */
621 				if (state != DCCPAV_NOT_RECEIVED &&
622 				    !seqp->ccid2s_acked) {
623 					if (state == DCCPAV_ECN_MARKED)
624 						ccid2_congestion_event(sk,
625 								       seqp);
626 					else
627 						ccid2_new_ack(sk, seqp,
628 							      &maxincr);
629 
630 					seqp->ccid2s_acked = 1;
631 					ccid2_pr_debug("Got ack for %llu\n",
632 						       (unsigned long long)seqp->ccid2s_seq);
633 					hc->tx_pipe--;
634 				}
635 				if (seqp == hc->tx_seqt) {
636 					done = 1;
637 					break;
638 				}
639 				seqp = seqp->ccid2s_prev;
640 			}
641 			if (done)
642 				break;
643 
644 			ackno = SUB48(ackno_end_rl, 1);
645 		}
646 		if (done)
647 			break;
648 	}
649 
650 	/* The state about what is acked should be correct now
651 	 * Check for NUMDUPACK
652 	 */
653 	seqp = hc->tx_seqt;
654 	while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
655 		seqp = seqp->ccid2s_next;
656 		if (seqp == hc->tx_seqh) {
657 			seqp = hc->tx_seqh->ccid2s_prev;
658 			break;
659 		}
660 	}
661 	done = 0;
662 	while (1) {
663 		if (seqp->ccid2s_acked) {
664 			done++;
665 			if (done == NUMDUPACK)
666 				break;
667 		}
668 		if (seqp == hc->tx_seqt)
669 			break;
670 		seqp = seqp->ccid2s_prev;
671 	}
672 
673 	/* If there are at least 3 acknowledgements, anything unacknowledged
674 	 * below the last sequence number is considered lost
675 	 */
676 	if (done == NUMDUPACK) {
677 		struct ccid2_seq *last_acked = seqp;
678 
679 		/* check for lost packets */
680 		while (1) {
681 			if (!seqp->ccid2s_acked) {
682 				ccid2_pr_debug("Packet lost: %llu\n",
683 					       (unsigned long long)seqp->ccid2s_seq);
684 				/* XXX need to traverse from tail -> head in
685 				 * order to detect multiple congestion events in
686 				 * one ack vector.
687 				 */
688 				ccid2_congestion_event(sk, seqp);
689 				hc->tx_pipe--;
690 			}
691 			if (seqp == hc->tx_seqt)
692 				break;
693 			seqp = seqp->ccid2s_prev;
694 		}
695 
696 		hc->tx_seqt = last_acked;
697 	}
698 
699 	/* trim acked packets in tail */
700 	while (hc->tx_seqt != hc->tx_seqh) {
701 		if (!hc->tx_seqt->ccid2s_acked)
702 			break;
703 
704 		hc->tx_seqt = hc->tx_seqt->ccid2s_next;
705 	}
706 
707 	/* restart RTO timer if not all outstanding data has been acked */
708 	if (hc->tx_pipe == 0)
709 		sk_stop_timer(sk, &hc->tx_rtotimer);
710 	else
711 		sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
712 done:
713 	/* check if incoming Acks allow pending packets to be sent */
714 	if (sender_was_blocked && !ccid2_cwnd_network_limited(hc))
715 		dccp_tasklet_schedule(sk);
716 	dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
717 }
718 
719 static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
720 {
721 	struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
722 	struct dccp_sock *dp = dccp_sk(sk);
723 	u32 max_ratio;
724 
725 	/* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
726 	hc->tx_ssthresh = ~0U;
727 
728 	/* Use larger initial windows (RFC 4341, section 5). */
729 	hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache);
730 	hc->tx_expected_wnd = hc->tx_cwnd;
731 
732 	/* Make sure that Ack Ratio is enabled and within bounds. */
733 	max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
734 	if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
735 		dp->dccps_l_ack_ratio = max_ratio;
736 
737 	/* XXX init ~ to window size... */
738 	if (ccid2_hc_tx_alloc_seq(hc))
739 		return -ENOMEM;
740 
741 	hc->tx_rto	 = DCCP_TIMEOUT_INIT;
742 	hc->tx_rpdupack  = -1;
743 	hc->tx_last_cong = hc->tx_lsndtime = hc->tx_cwnd_stamp = ccid2_jiffies32;
744 	hc->tx_cwnd_used = 0;
745 	hc->sk		 = sk;
746 	timer_setup(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire, 0);
747 	INIT_LIST_HEAD(&hc->tx_av_chunks);
748 	return 0;
749 }
750 
751 static void ccid2_hc_tx_exit(struct sock *sk)
752 {
753 	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
754 	int i;
755 
756 	sk_stop_timer(sk, &hc->tx_rtotimer);
757 
758 	for (i = 0; i < hc->tx_seqbufc; i++)
759 		kfree(hc->tx_seqbuf[i]);
760 	hc->tx_seqbufc = 0;
761 	dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
762 }
763 
764 static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
765 {
766 	struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
767 
768 	if (!dccp_data_packet(skb))
769 		return;
770 
771 	if (++hc->rx_num_data_pkts >= dccp_sk(sk)->dccps_r_ack_ratio) {
772 		dccp_send_ack(sk);
773 		hc->rx_num_data_pkts = 0;
774 	}
775 }
776 
777 struct ccid_operations ccid2_ops = {
778 	.ccid_id		  = DCCPC_CCID2,
779 	.ccid_name		  = "TCP-like",
780 	.ccid_hc_tx_obj_size	  = sizeof(struct ccid2_hc_tx_sock),
781 	.ccid_hc_tx_init	  = ccid2_hc_tx_init,
782 	.ccid_hc_tx_exit	  = ccid2_hc_tx_exit,
783 	.ccid_hc_tx_send_packet	  = ccid2_hc_tx_send_packet,
784 	.ccid_hc_tx_packet_sent	  = ccid2_hc_tx_packet_sent,
785 	.ccid_hc_tx_parse_options = ccid2_hc_tx_parse_options,
786 	.ccid_hc_tx_packet_recv	  = ccid2_hc_tx_packet_recv,
787 	.ccid_hc_rx_obj_size	  = sizeof(struct ccid2_hc_rx_sock),
788 	.ccid_hc_rx_packet_recv	  = ccid2_hc_rx_packet_recv,
789 };
790 
791 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
792 module_param(ccid2_debug, bool, 0644);
793 MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
794 #endif
795