18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Changes to meet Linux coding standards, and DCCP infrastructure fixes.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * This implementation should follow RFC 4341
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include "../feat.h"
158c2ecf20Sopenharmony_ci#include "ccid2.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
198c2ecf20Sopenharmony_cistatic bool ccid2_debug;
208c2ecf20Sopenharmony_ci#define ccid2_pr_debug(format, a...)	DCCP_PR_DEBUG(ccid2_debug, format, ##a)
218c2ecf20Sopenharmony_ci#else
228c2ecf20Sopenharmony_ci#define ccid2_pr_debug(format, a...)
238c2ecf20Sopenharmony_ci#endif
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct ccid2_seq *seqp;
288c2ecf20Sopenharmony_ci	int i;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	/* check if we have space to preserve the pointer to the buffer */
318c2ecf20Sopenharmony_ci	if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
328c2ecf20Sopenharmony_ci			       sizeof(struct ccid2_seq *)))
338c2ecf20Sopenharmony_ci		return -ENOMEM;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/* allocate buffer and initialize linked list */
368c2ecf20Sopenharmony_ci	seqp = kmalloc_array(CCID2_SEQBUF_LEN, sizeof(struct ccid2_seq),
378c2ecf20Sopenharmony_ci			     gfp_any());
388c2ecf20Sopenharmony_ci	if (seqp == NULL)
398c2ecf20Sopenharmony_ci		return -ENOMEM;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
428c2ecf20Sopenharmony_ci		seqp[i].ccid2s_next = &seqp[i + 1];
438c2ecf20Sopenharmony_ci		seqp[i + 1].ccid2s_prev = &seqp[i];
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci	seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
468c2ecf20Sopenharmony_ci	seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* This is the first allocation.  Initiate the head and tail.  */
498c2ecf20Sopenharmony_ci	if (hc->tx_seqbufc == 0)
508c2ecf20Sopenharmony_ci		hc->tx_seqh = hc->tx_seqt = seqp;
518c2ecf20Sopenharmony_ci	else {
528c2ecf20Sopenharmony_ci		/* link the existing list with the one we just created */
538c2ecf20Sopenharmony_ci		hc->tx_seqh->ccid2s_next = seqp;
548c2ecf20Sopenharmony_ci		seqp->ccid2s_prev = hc->tx_seqh;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
578c2ecf20Sopenharmony_ci		seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* store the original pointer to the buffer so we can free it */
618c2ecf20Sopenharmony_ci	hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
628c2ecf20Sopenharmony_ci	hc->tx_seqbufc++;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	if (ccid2_cwnd_network_limited(ccid2_hc_tx_sk(sk)))
708c2ecf20Sopenharmony_ci		return CCID_PACKET_WILL_DEQUEUE_LATER;
718c2ecf20Sopenharmony_ci	return CCID_PACKET_SEND_AT_ONCE;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
808c2ecf20Sopenharmony_ci	 * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
818c2ecf20Sopenharmony_ci	 * acceptable since this causes starvation/deadlock whenever cwnd < 2.
828c2ecf20Sopenharmony_ci	 * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (val == 0 || val > max_ratio) {
858c2ecf20Sopenharmony_ci		DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
868c2ecf20Sopenharmony_ci		val = max_ratio;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci	dccp_feat_signal_nn_change(sk, DCCPF_ACK_RATIO,
898c2ecf20Sopenharmony_ci				   min_t(u32, val, DCCPF_ACK_RATIO_MAX));
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic void ccid2_check_l_ack_ratio(struct sock *sk)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * After a loss, idle period, application limited period, or RTO we
988c2ecf20Sopenharmony_ci	 * need to check that the ack ratio is still less than the congestion
998c2ecf20Sopenharmony_ci	 * window. Otherwise, we will send an entire congestion window of
1008c2ecf20Sopenharmony_ci	 * packets and got no response because we haven't sent ack ratio
1018c2ecf20Sopenharmony_ci	 * packets yet.
1028c2ecf20Sopenharmony_ci	 * If the ack ratio does need to be reduced, we reduce it to half of
1038c2ecf20Sopenharmony_ci	 * the congestion window (or 1 if that's zero) instead of to the
1048c2ecf20Sopenharmony_ci	 * congestion window. This prevents problems if one ack is lost.
1058c2ecf20Sopenharmony_ci	 */
1068c2ecf20Sopenharmony_ci	if (dccp_feat_nn_get(sk, DCCPF_ACK_RATIO) > hc->tx_cwnd)
1078c2ecf20Sopenharmony_ci		ccid2_change_l_ack_ratio(sk, hc->tx_cwnd/2 ? : 1U);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic void ccid2_change_l_seq_window(struct sock *sk, u64 val)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	dccp_feat_signal_nn_change(sk, DCCPF_SEQUENCE_WINDOW,
1138c2ecf20Sopenharmony_ci				   clamp_val(val, DCCPF_SEQ_WMIN,
1148c2ecf20Sopenharmony_ci						  DCCPF_SEQ_WMAX));
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void dccp_tasklet_schedule(struct sock *sk)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct tasklet_struct *t = &dccp_sk(sk)->dccps_xmitlet;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
1228c2ecf20Sopenharmony_ci		sock_hold(sk);
1238c2ecf20Sopenharmony_ci		__tasklet_schedule(t);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void ccid2_hc_tx_rto_expire(struct timer_list *t)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = from_timer(hc, t, tx_rtotimer);
1308c2ecf20Sopenharmony_ci	struct sock *sk = hc->sk;
1318c2ecf20Sopenharmony_ci	const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	bh_lock_sock(sk);
1348c2ecf20Sopenharmony_ci	if (sock_owned_by_user(sk)) {
1358c2ecf20Sopenharmony_ci		sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
1368c2ecf20Sopenharmony_ci		goto out;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ccid2_pr_debug("RTO_EXPIRE\n");
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (sk->sk_state == DCCP_CLOSED)
1428c2ecf20Sopenharmony_ci		goto out;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* back-off timer */
1458c2ecf20Sopenharmony_ci	hc->tx_rto <<= 1;
1468c2ecf20Sopenharmony_ci	if (hc->tx_rto > DCCP_RTO_MAX)
1478c2ecf20Sopenharmony_ci		hc->tx_rto = DCCP_RTO_MAX;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* adjust pipe, cwnd etc */
1508c2ecf20Sopenharmony_ci	hc->tx_ssthresh = hc->tx_cwnd / 2;
1518c2ecf20Sopenharmony_ci	if (hc->tx_ssthresh < 2)
1528c2ecf20Sopenharmony_ci		hc->tx_ssthresh = 2;
1538c2ecf20Sopenharmony_ci	hc->tx_cwnd	= 1;
1548c2ecf20Sopenharmony_ci	hc->tx_pipe	= 0;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* clear state about stuff we sent */
1578c2ecf20Sopenharmony_ci	hc->tx_seqt = hc->tx_seqh;
1588c2ecf20Sopenharmony_ci	hc->tx_packets_acked = 0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* clear ack ratio state. */
1618c2ecf20Sopenharmony_ci	hc->tx_rpseq    = 0;
1628c2ecf20Sopenharmony_ci	hc->tx_rpdupack = -1;
1638c2ecf20Sopenharmony_ci	ccid2_change_l_ack_ratio(sk, 1);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* if we were blocked before, we may now send cwnd=1 packet */
1668c2ecf20Sopenharmony_ci	if (sender_was_blocked)
1678c2ecf20Sopenharmony_ci		dccp_tasklet_schedule(sk);
1688c2ecf20Sopenharmony_ci	/* restart backed-off timer */
1698c2ecf20Sopenharmony_ci	sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
1708c2ecf20Sopenharmony_ciout:
1718c2ecf20Sopenharmony_ci	bh_unlock_sock(sk);
1728c2ecf20Sopenharmony_ci	sock_put(sk);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/*
1768c2ecf20Sopenharmony_ci *	Congestion window validation (RFC 2861).
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_cistatic bool ccid2_do_cwv = true;
1798c2ecf20Sopenharmony_cimodule_param(ccid2_do_cwv, bool, 0644);
1808c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ccid2_do_cwv, "Perform RFC2861 Congestion Window Validation");
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * ccid2_update_used_window  -  Track how much of cwnd is actually used
1848c2ecf20Sopenharmony_ci * This is done in addition to CWV. The sender needs to have an idea of how many
1858c2ecf20Sopenharmony_ci * packets may be in flight, to set the local Sequence Window value accordingly
1868c2ecf20Sopenharmony_ci * (RFC 4340, 7.5.2). The CWV mechanism is exploited to keep track of the
1878c2ecf20Sopenharmony_ci * maximum-used window. We use an EWMA low-pass filter to filter out noise.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_cistatic void ccid2_update_used_window(struct ccid2_hc_tx_sock *hc, u32 new_wnd)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	hc->tx_expected_wnd = (3 * hc->tx_expected_wnd + new_wnd) / 4;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/* This borrows the code of tcp_cwnd_application_limited() */
1958c2ecf20Sopenharmony_cistatic void ccid2_cwnd_application_limited(struct sock *sk, const u32 now)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
1988c2ecf20Sopenharmony_ci	/* don't reduce cwnd below the initial window (IW) */
1998c2ecf20Sopenharmony_ci	u32 init_win = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache),
2008c2ecf20Sopenharmony_ci	    win_used = max(hc->tx_cwnd_used, init_win);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (win_used < hc->tx_cwnd) {
2038c2ecf20Sopenharmony_ci		hc->tx_ssthresh = max(hc->tx_ssthresh,
2048c2ecf20Sopenharmony_ci				     (hc->tx_cwnd >> 1) + (hc->tx_cwnd >> 2));
2058c2ecf20Sopenharmony_ci		hc->tx_cwnd = (hc->tx_cwnd + win_used) >> 1;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci	hc->tx_cwnd_used  = 0;
2088c2ecf20Sopenharmony_ci	hc->tx_cwnd_stamp = now;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	ccid2_check_l_ack_ratio(sk);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* This borrows the code of tcp_cwnd_restart() */
2148c2ecf20Sopenharmony_cistatic void ccid2_cwnd_restart(struct sock *sk, const u32 now)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
2178c2ecf20Sopenharmony_ci	u32 cwnd = hc->tx_cwnd, restart_cwnd,
2188c2ecf20Sopenharmony_ci	    iwnd = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache);
2198c2ecf20Sopenharmony_ci	s32 delta = now - hc->tx_lsndtime;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	hc->tx_ssthresh = max(hc->tx_ssthresh, (cwnd >> 1) + (cwnd >> 2));
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* don't reduce cwnd below the initial window (IW) */
2248c2ecf20Sopenharmony_ci	restart_cwnd = min(cwnd, iwnd);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	while ((delta -= hc->tx_rto) >= 0 && cwnd > restart_cwnd)
2278c2ecf20Sopenharmony_ci		cwnd >>= 1;
2288c2ecf20Sopenharmony_ci	hc->tx_cwnd = max(cwnd, restart_cwnd);
2298c2ecf20Sopenharmony_ci	hc->tx_cwnd_stamp = now;
2308c2ecf20Sopenharmony_ci	hc->tx_cwnd_used  = 0;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	ccid2_check_l_ack_ratio(sk);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct dccp_sock *dp = dccp_sk(sk);
2388c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
2398c2ecf20Sopenharmony_ci	const u32 now = ccid2_jiffies32;
2408c2ecf20Sopenharmony_ci	struct ccid2_seq *next;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* slow-start after idle periods (RFC 2581, RFC 2861) */
2438c2ecf20Sopenharmony_ci	if (ccid2_do_cwv && !hc->tx_pipe &&
2448c2ecf20Sopenharmony_ci	    (s32)(now - hc->tx_lsndtime) >= hc->tx_rto)
2458c2ecf20Sopenharmony_ci		ccid2_cwnd_restart(sk, now);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	hc->tx_lsndtime = now;
2488c2ecf20Sopenharmony_ci	hc->tx_pipe    += 1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* see whether cwnd was fully used (RFC 2861), update expected window */
2518c2ecf20Sopenharmony_ci	if (ccid2_cwnd_network_limited(hc)) {
2528c2ecf20Sopenharmony_ci		ccid2_update_used_window(hc, hc->tx_cwnd);
2538c2ecf20Sopenharmony_ci		hc->tx_cwnd_used  = 0;
2548c2ecf20Sopenharmony_ci		hc->tx_cwnd_stamp = now;
2558c2ecf20Sopenharmony_ci	} else {
2568c2ecf20Sopenharmony_ci		if (hc->tx_pipe > hc->tx_cwnd_used)
2578c2ecf20Sopenharmony_ci			hc->tx_cwnd_used = hc->tx_pipe;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		ccid2_update_used_window(hc, hc->tx_cwnd_used);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		if (ccid2_do_cwv && (s32)(now - hc->tx_cwnd_stamp) >= hc->tx_rto)
2628c2ecf20Sopenharmony_ci			ccid2_cwnd_application_limited(sk, now);
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	hc->tx_seqh->ccid2s_seq   = dp->dccps_gss;
2668c2ecf20Sopenharmony_ci	hc->tx_seqh->ccid2s_acked = 0;
2678c2ecf20Sopenharmony_ci	hc->tx_seqh->ccid2s_sent  = now;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	next = hc->tx_seqh->ccid2s_next;
2708c2ecf20Sopenharmony_ci	/* check if we need to alloc more space */
2718c2ecf20Sopenharmony_ci	if (next == hc->tx_seqt) {
2728c2ecf20Sopenharmony_ci		if (ccid2_hc_tx_alloc_seq(hc)) {
2738c2ecf20Sopenharmony_ci			DCCP_CRIT("packet history - out of memory!");
2748c2ecf20Sopenharmony_ci			/* FIXME: find a more graceful way to bail out */
2758c2ecf20Sopenharmony_ci			return;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci		next = hc->tx_seqh->ccid2s_next;
2788c2ecf20Sopenharmony_ci		BUG_ON(next == hc->tx_seqt);
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci	hc->tx_seqh = next;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/*
2858c2ecf20Sopenharmony_ci	 * FIXME: The code below is broken and the variables have been removed
2868c2ecf20Sopenharmony_ci	 * from the socket struct. The `ackloss' variable was always set to 0,
2878c2ecf20Sopenharmony_ci	 * and with arsent there are several problems:
2888c2ecf20Sopenharmony_ci	 *  (i) it doesn't just count the number of Acks, but all sent packets;
2898c2ecf20Sopenharmony_ci	 *  (ii) it is expressed in # of packets, not # of windows, so the
2908c2ecf20Sopenharmony_ci	 *  comparison below uses the wrong formula: Appendix A of RFC 4341
2918c2ecf20Sopenharmony_ci	 *  comes up with the number K = cwnd / (R^2 - R) of consecutive windows
2928c2ecf20Sopenharmony_ci	 *  of data with no lost or marked Ack packets. If arsent were the # of
2938c2ecf20Sopenharmony_ci	 *  consecutive Acks received without loss, then Ack Ratio needs to be
2948c2ecf20Sopenharmony_ci	 *  decreased by 1 when
2958c2ecf20Sopenharmony_ci	 *	      arsent >=  K * cwnd / R  =  cwnd^2 / (R^3 - R^2)
2968c2ecf20Sopenharmony_ci	 *  where cwnd / R is the number of Acks received per window of data
2978c2ecf20Sopenharmony_ci	 *  (cf. RFC 4341, App. A). The problems are that
2988c2ecf20Sopenharmony_ci	 *  - arsent counts other packets as well;
2998c2ecf20Sopenharmony_ci	 *  - the comparison uses a formula different from RFC 4341;
3008c2ecf20Sopenharmony_ci	 *  - computing a cubic/quadratic equation each time is too complicated.
3018c2ecf20Sopenharmony_ci	 *  Hence a different algorithm is needed.
3028c2ecf20Sopenharmony_ci	 */
3038c2ecf20Sopenharmony_ci#if 0
3048c2ecf20Sopenharmony_ci	/* Ack Ratio.  Need to maintain a concept of how many windows we sent */
3058c2ecf20Sopenharmony_ci	hc->tx_arsent++;
3068c2ecf20Sopenharmony_ci	/* We had an ack loss in this window... */
3078c2ecf20Sopenharmony_ci	if (hc->tx_ackloss) {
3088c2ecf20Sopenharmony_ci		if (hc->tx_arsent >= hc->tx_cwnd) {
3098c2ecf20Sopenharmony_ci			hc->tx_arsent  = 0;
3108c2ecf20Sopenharmony_ci			hc->tx_ackloss = 0;
3118c2ecf20Sopenharmony_ci		}
3128c2ecf20Sopenharmony_ci	} else {
3138c2ecf20Sopenharmony_ci		/* No acks lost up to now... */
3148c2ecf20Sopenharmony_ci		/* decrease ack ratio if enough packets were sent */
3158c2ecf20Sopenharmony_ci		if (dp->dccps_l_ack_ratio > 1) {
3168c2ecf20Sopenharmony_ci			/* XXX don't calculate denominator each time */
3178c2ecf20Sopenharmony_ci			int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
3188c2ecf20Sopenharmony_ci				    dp->dccps_l_ack_ratio;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci			denom = hc->tx_cwnd * hc->tx_cwnd / denom;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci			if (hc->tx_arsent >= denom) {
3238c2ecf20Sopenharmony_ci				ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
3248c2ecf20Sopenharmony_ci				hc->tx_arsent = 0;
3258c2ecf20Sopenharmony_ci			}
3268c2ecf20Sopenharmony_ci		} else {
3278c2ecf20Sopenharmony_ci			/* we can't increase ack ratio further [1] */
3288c2ecf20Sopenharmony_ci			hc->tx_arsent = 0; /* or maybe set it to cwnd*/
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci#endif
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
3368c2ecf20Sopenharmony_ci	do {
3378c2ecf20Sopenharmony_ci		struct ccid2_seq *seqp = hc->tx_seqt;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci		while (seqp != hc->tx_seqh) {
3408c2ecf20Sopenharmony_ci			ccid2_pr_debug("out seq=%llu acked=%d time=%u\n",
3418c2ecf20Sopenharmony_ci				       (unsigned long long)seqp->ccid2s_seq,
3428c2ecf20Sopenharmony_ci				       seqp->ccid2s_acked, seqp->ccid2s_sent);
3438c2ecf20Sopenharmony_ci			seqp = seqp->ccid2s_next;
3448c2ecf20Sopenharmony_ci		}
3458c2ecf20Sopenharmony_ci	} while (0);
3468c2ecf20Sopenharmony_ci	ccid2_pr_debug("=========\n");
3478c2ecf20Sopenharmony_ci#endif
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci/**
3518c2ecf20Sopenharmony_ci * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm
3528c2ecf20Sopenharmony_ci * This code is almost identical with TCP's tcp_rtt_estimator(), since
3538c2ecf20Sopenharmony_ci * - it has a higher sampling frequency (recommended by RFC 1323),
3548c2ecf20Sopenharmony_ci * - the RTO does not collapse into RTT due to RTTVAR going towards zero,
3558c2ecf20Sopenharmony_ci * - it is simple (cf. more complex proposals such as Eifel timer or research
3568c2ecf20Sopenharmony_ci *   which suggests that the gain should be set according to window size),
3578c2ecf20Sopenharmony_ci * - in tests it was found to work well with CCID2 [gerrit].
3588c2ecf20Sopenharmony_ci */
3598c2ecf20Sopenharmony_cistatic void ccid2_rtt_estimator(struct sock *sk, const long mrtt)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
3628c2ecf20Sopenharmony_ci	long m = mrtt ? : 1;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	if (hc->tx_srtt == 0) {
3658c2ecf20Sopenharmony_ci		/* First measurement m */
3668c2ecf20Sopenharmony_ci		hc->tx_srtt = m << 3;
3678c2ecf20Sopenharmony_ci		hc->tx_mdev = m << 1;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci		hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk));
3708c2ecf20Sopenharmony_ci		hc->tx_rttvar   = hc->tx_mdev_max;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
3738c2ecf20Sopenharmony_ci	} else {
3748c2ecf20Sopenharmony_ci		/* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */
3758c2ecf20Sopenharmony_ci		m -= (hc->tx_srtt >> 3);
3768c2ecf20Sopenharmony_ci		hc->tx_srtt += m;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci		/* Similarly, update scaled mdev with regard to |m| */
3798c2ecf20Sopenharmony_ci		if (m < 0) {
3808c2ecf20Sopenharmony_ci			m = -m;
3818c2ecf20Sopenharmony_ci			m -= (hc->tx_mdev >> 2);
3828c2ecf20Sopenharmony_ci			/*
3838c2ecf20Sopenharmony_ci			 * This neutralises RTO increase when RTT < SRTT - mdev
3848c2ecf20Sopenharmony_ci			 * (see P. Sarolahti, A. Kuznetsov,"Congestion Control
3858c2ecf20Sopenharmony_ci			 * in Linux TCP", USENIX 2002, pp. 49-62).
3868c2ecf20Sopenharmony_ci			 */
3878c2ecf20Sopenharmony_ci			if (m > 0)
3888c2ecf20Sopenharmony_ci				m >>= 3;
3898c2ecf20Sopenharmony_ci		} else {
3908c2ecf20Sopenharmony_ci			m -= (hc->tx_mdev >> 2);
3918c2ecf20Sopenharmony_ci		}
3928c2ecf20Sopenharmony_ci		hc->tx_mdev += m;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci		if (hc->tx_mdev > hc->tx_mdev_max) {
3958c2ecf20Sopenharmony_ci			hc->tx_mdev_max = hc->tx_mdev;
3968c2ecf20Sopenharmony_ci			if (hc->tx_mdev_max > hc->tx_rttvar)
3978c2ecf20Sopenharmony_ci				hc->tx_rttvar = hc->tx_mdev_max;
3988c2ecf20Sopenharmony_ci		}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		/*
4018c2ecf20Sopenharmony_ci		 * Decay RTTVAR at most once per flight, exploiting that
4028c2ecf20Sopenharmony_ci		 *  1) pipe <= cwnd <= Sequence_Window = W  (RFC 4340, 7.5.2)
4038c2ecf20Sopenharmony_ci		 *  2) AWL = GSS-W+1 <= GAR <= GSS          (RFC 4340, 7.5.1)
4048c2ecf20Sopenharmony_ci		 * GAR is a useful bound for FlightSize = pipe.
4058c2ecf20Sopenharmony_ci		 * AWL is probably too low here, as it over-estimates pipe.
4068c2ecf20Sopenharmony_ci		 */
4078c2ecf20Sopenharmony_ci		if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) {
4088c2ecf20Sopenharmony_ci			if (hc->tx_mdev_max < hc->tx_rttvar)
4098c2ecf20Sopenharmony_ci				hc->tx_rttvar -= (hc->tx_rttvar -
4108c2ecf20Sopenharmony_ci						  hc->tx_mdev_max) >> 2;
4118c2ecf20Sopenharmony_ci			hc->tx_rtt_seq  = dccp_sk(sk)->dccps_gss;
4128c2ecf20Sopenharmony_ci			hc->tx_mdev_max = tcp_rto_min(sk);
4138c2ecf20Sopenharmony_ci		}
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	/*
4178c2ecf20Sopenharmony_ci	 * Set RTO from SRTT and RTTVAR
4188c2ecf20Sopenharmony_ci	 * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms.
4198c2ecf20Sopenharmony_ci	 * This agrees with RFC 4341, 5:
4208c2ecf20Sopenharmony_ci	 *	"Because DCCP does not retransmit data, DCCP does not require
4218c2ecf20Sopenharmony_ci	 *	 TCP's recommended minimum timeout of one second".
4228c2ecf20Sopenharmony_ci	 */
4238c2ecf20Sopenharmony_ci	hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (hc->tx_rto > DCCP_RTO_MAX)
4268c2ecf20Sopenharmony_ci		hc->tx_rto = DCCP_RTO_MAX;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp,
4308c2ecf20Sopenharmony_ci			  unsigned int *maxincr)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
4338c2ecf20Sopenharmony_ci	struct dccp_sock *dp = dccp_sk(sk);
4348c2ecf20Sopenharmony_ci	int r_seq_used = hc->tx_cwnd / dp->dccps_l_ack_ratio;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (hc->tx_cwnd < dp->dccps_l_seq_win &&
4378c2ecf20Sopenharmony_ci	    r_seq_used < dp->dccps_r_seq_win) {
4388c2ecf20Sopenharmony_ci		if (hc->tx_cwnd < hc->tx_ssthresh) {
4398c2ecf20Sopenharmony_ci			if (*maxincr > 0 && ++hc->tx_packets_acked >= 2) {
4408c2ecf20Sopenharmony_ci				hc->tx_cwnd += 1;
4418c2ecf20Sopenharmony_ci				*maxincr    -= 1;
4428c2ecf20Sopenharmony_ci				hc->tx_packets_acked = 0;
4438c2ecf20Sopenharmony_ci			}
4448c2ecf20Sopenharmony_ci		} else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
4458c2ecf20Sopenharmony_ci			hc->tx_cwnd += 1;
4468c2ecf20Sopenharmony_ci			hc->tx_packets_acked = 0;
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/*
4518c2ecf20Sopenharmony_ci	 * Adjust the local sequence window and the ack ratio to allow about
4528c2ecf20Sopenharmony_ci	 * 5 times the number of packets in the network (RFC 4340 7.5.2)
4538c2ecf20Sopenharmony_ci	 */
4548c2ecf20Sopenharmony_ci	if (r_seq_used * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_r_seq_win)
4558c2ecf20Sopenharmony_ci		ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio * 2);
4568c2ecf20Sopenharmony_ci	else if (r_seq_used * CCID2_WIN_CHANGE_FACTOR < dp->dccps_r_seq_win/2)
4578c2ecf20Sopenharmony_ci		ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio / 2 ? : 1U);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_l_seq_win)
4608c2ecf20Sopenharmony_ci		ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win * 2);
4618c2ecf20Sopenharmony_ci	else if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR < dp->dccps_l_seq_win/2)
4628c2ecf20Sopenharmony_ci		ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win / 2);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/*
4658c2ecf20Sopenharmony_ci	 * FIXME: RTT is sampled several times per acknowledgment (for each
4668c2ecf20Sopenharmony_ci	 * entry in the Ack Vector), instead of once per Ack (as in TCP SACK).
4678c2ecf20Sopenharmony_ci	 * This causes the RTT to be over-estimated, since the older entries
4688c2ecf20Sopenharmony_ci	 * in the Ack Vector have earlier sending times.
4698c2ecf20Sopenharmony_ci	 * The cleanest solution is to not use the ccid2s_sent field at all
4708c2ecf20Sopenharmony_ci	 * and instead use DCCP timestamps: requires changes in other places.
4718c2ecf20Sopenharmony_ci	 */
4728c2ecf20Sopenharmony_ci	ccid2_rtt_estimator(sk, ccid2_jiffies32 - seqp->ccid2s_sent);
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) {
4808c2ecf20Sopenharmony_ci		ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
4818c2ecf20Sopenharmony_ci		return;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	hc->tx_last_cong = ccid2_jiffies32;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	hc->tx_cwnd      = hc->tx_cwnd / 2 ? : 1U;
4878c2ecf20Sopenharmony_ci	hc->tx_ssthresh  = max(hc->tx_cwnd, 2U);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	ccid2_check_l_ack_ratio(sk);
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int ccid2_hc_tx_parse_options(struct sock *sk, u8 packet_type,
4938c2ecf20Sopenharmony_ci				     u8 option, u8 *optval, u8 optlen)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	switch (option) {
4988c2ecf20Sopenharmony_ci	case DCCPO_ACK_VECTOR_0:
4998c2ecf20Sopenharmony_ci	case DCCPO_ACK_VECTOR_1:
5008c2ecf20Sopenharmony_ci		return dccp_ackvec_parsed_add(&hc->tx_av_chunks, optval, optlen,
5018c2ecf20Sopenharmony_ci					      option - DCCPO_ACK_VECTOR_0);
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci	return 0;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct dccp_sock *dp = dccp_sk(sk);
5098c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
5108c2ecf20Sopenharmony_ci	const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
5118c2ecf20Sopenharmony_ci	struct dccp_ackvec_parsed *avp;
5128c2ecf20Sopenharmony_ci	u64 ackno, seqno;
5138c2ecf20Sopenharmony_ci	struct ccid2_seq *seqp;
5148c2ecf20Sopenharmony_ci	int done = 0;
5158c2ecf20Sopenharmony_ci	unsigned int maxincr = 0;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	/* check reverse path congestion */
5188c2ecf20Sopenharmony_ci	seqno = DCCP_SKB_CB(skb)->dccpd_seq;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/* XXX this whole "algorithm" is broken.  Need to fix it to keep track
5218c2ecf20Sopenharmony_ci	 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
5228c2ecf20Sopenharmony_ci	 * -sorbo.
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	/* need to bootstrap */
5258c2ecf20Sopenharmony_ci	if (hc->tx_rpdupack == -1) {
5268c2ecf20Sopenharmony_ci		hc->tx_rpdupack = 0;
5278c2ecf20Sopenharmony_ci		hc->tx_rpseq    = seqno;
5288c2ecf20Sopenharmony_ci	} else {
5298c2ecf20Sopenharmony_ci		/* check if packet is consecutive */
5308c2ecf20Sopenharmony_ci		if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
5318c2ecf20Sopenharmony_ci			hc->tx_rpseq = seqno;
5328c2ecf20Sopenharmony_ci		/* it's a later packet */
5338c2ecf20Sopenharmony_ci		else if (after48(seqno, hc->tx_rpseq)) {
5348c2ecf20Sopenharmony_ci			hc->tx_rpdupack++;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci			/* check if we got enough dupacks */
5378c2ecf20Sopenharmony_ci			if (hc->tx_rpdupack >= NUMDUPACK) {
5388c2ecf20Sopenharmony_ci				hc->tx_rpdupack = -1; /* XXX lame */
5398c2ecf20Sopenharmony_ci				hc->tx_rpseq    = 0;
5408c2ecf20Sopenharmony_ci#ifdef __CCID2_COPES_GRACEFULLY_WITH_ACK_CONGESTION_CONTROL__
5418c2ecf20Sopenharmony_ci				/*
5428c2ecf20Sopenharmony_ci				 * FIXME: Ack Congestion Control is broken; in
5438c2ecf20Sopenharmony_ci				 * the current state instabilities occurred with
5448c2ecf20Sopenharmony_ci				 * Ack Ratios greater than 1; causing hang-ups
5458c2ecf20Sopenharmony_ci				 * and long RTO timeouts. This needs to be fixed
5468c2ecf20Sopenharmony_ci				 * before opening up dynamic changes. -- gerrit
5478c2ecf20Sopenharmony_ci				 */
5488c2ecf20Sopenharmony_ci				ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
5498c2ecf20Sopenharmony_ci#endif
5508c2ecf20Sopenharmony_ci			}
5518c2ecf20Sopenharmony_ci		}
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	/* check forward path congestion */
5558c2ecf20Sopenharmony_ci	if (dccp_packet_without_ack(skb))
5568c2ecf20Sopenharmony_ci		return;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/* still didn't send out new data packets */
5598c2ecf20Sopenharmony_ci	if (hc->tx_seqh == hc->tx_seqt)
5608c2ecf20Sopenharmony_ci		goto done;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
5638c2ecf20Sopenharmony_ci	if (after48(ackno, hc->tx_high_ack))
5648c2ecf20Sopenharmony_ci		hc->tx_high_ack = ackno;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	seqp = hc->tx_seqt;
5678c2ecf20Sopenharmony_ci	while (before48(seqp->ccid2s_seq, ackno)) {
5688c2ecf20Sopenharmony_ci		seqp = seqp->ccid2s_next;
5698c2ecf20Sopenharmony_ci		if (seqp == hc->tx_seqh) {
5708c2ecf20Sopenharmony_ci			seqp = hc->tx_seqh->ccid2s_prev;
5718c2ecf20Sopenharmony_ci			break;
5728c2ecf20Sopenharmony_ci		}
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/*
5768c2ecf20Sopenharmony_ci	 * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
5778c2ecf20Sopenharmony_ci	 * packets per acknowledgement. Rounding up avoids that cwnd is not
5788c2ecf20Sopenharmony_ci	 * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
5798c2ecf20Sopenharmony_ci	 */
5808c2ecf20Sopenharmony_ci	if (hc->tx_cwnd < hc->tx_ssthresh)
5818c2ecf20Sopenharmony_ci		maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	/* go through all ack vectors */
5848c2ecf20Sopenharmony_ci	list_for_each_entry(avp, &hc->tx_av_chunks, node) {
5858c2ecf20Sopenharmony_ci		/* go through this ack vector */
5868c2ecf20Sopenharmony_ci		for (; avp->len--; avp->vec++) {
5878c2ecf20Sopenharmony_ci			u64 ackno_end_rl = SUB48(ackno,
5888c2ecf20Sopenharmony_ci						 dccp_ackvec_runlen(avp->vec));
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci			ccid2_pr_debug("ackvec %llu |%u,%u|\n",
5918c2ecf20Sopenharmony_ci				       (unsigned long long)ackno,
5928c2ecf20Sopenharmony_ci				       dccp_ackvec_state(avp->vec) >> 6,
5938c2ecf20Sopenharmony_ci				       dccp_ackvec_runlen(avp->vec));
5948c2ecf20Sopenharmony_ci			/* if the seqno we are analyzing is larger than the
5958c2ecf20Sopenharmony_ci			 * current ackno, then move towards the tail of our
5968c2ecf20Sopenharmony_ci			 * seqnos.
5978c2ecf20Sopenharmony_ci			 */
5988c2ecf20Sopenharmony_ci			while (after48(seqp->ccid2s_seq, ackno)) {
5998c2ecf20Sopenharmony_ci				if (seqp == hc->tx_seqt) {
6008c2ecf20Sopenharmony_ci					done = 1;
6018c2ecf20Sopenharmony_ci					break;
6028c2ecf20Sopenharmony_ci				}
6038c2ecf20Sopenharmony_ci				seqp = seqp->ccid2s_prev;
6048c2ecf20Sopenharmony_ci			}
6058c2ecf20Sopenharmony_ci			if (done)
6068c2ecf20Sopenharmony_ci				break;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci			/* check all seqnos in the range of the vector
6098c2ecf20Sopenharmony_ci			 * run length
6108c2ecf20Sopenharmony_ci			 */
6118c2ecf20Sopenharmony_ci			while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
6128c2ecf20Sopenharmony_ci				const u8 state = dccp_ackvec_state(avp->vec);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci				/* new packet received or marked */
6158c2ecf20Sopenharmony_ci				if (state != DCCPAV_NOT_RECEIVED &&
6168c2ecf20Sopenharmony_ci				    !seqp->ccid2s_acked) {
6178c2ecf20Sopenharmony_ci					if (state == DCCPAV_ECN_MARKED)
6188c2ecf20Sopenharmony_ci						ccid2_congestion_event(sk,
6198c2ecf20Sopenharmony_ci								       seqp);
6208c2ecf20Sopenharmony_ci					else
6218c2ecf20Sopenharmony_ci						ccid2_new_ack(sk, seqp,
6228c2ecf20Sopenharmony_ci							      &maxincr);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci					seqp->ccid2s_acked = 1;
6258c2ecf20Sopenharmony_ci					ccid2_pr_debug("Got ack for %llu\n",
6268c2ecf20Sopenharmony_ci						       (unsigned long long)seqp->ccid2s_seq);
6278c2ecf20Sopenharmony_ci					hc->tx_pipe--;
6288c2ecf20Sopenharmony_ci				}
6298c2ecf20Sopenharmony_ci				if (seqp == hc->tx_seqt) {
6308c2ecf20Sopenharmony_ci					done = 1;
6318c2ecf20Sopenharmony_ci					break;
6328c2ecf20Sopenharmony_ci				}
6338c2ecf20Sopenharmony_ci				seqp = seqp->ccid2s_prev;
6348c2ecf20Sopenharmony_ci			}
6358c2ecf20Sopenharmony_ci			if (done)
6368c2ecf20Sopenharmony_ci				break;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci			ackno = SUB48(ackno_end_rl, 1);
6398c2ecf20Sopenharmony_ci		}
6408c2ecf20Sopenharmony_ci		if (done)
6418c2ecf20Sopenharmony_ci			break;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	/* The state about what is acked should be correct now
6458c2ecf20Sopenharmony_ci	 * Check for NUMDUPACK
6468c2ecf20Sopenharmony_ci	 */
6478c2ecf20Sopenharmony_ci	seqp = hc->tx_seqt;
6488c2ecf20Sopenharmony_ci	while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
6498c2ecf20Sopenharmony_ci		seqp = seqp->ccid2s_next;
6508c2ecf20Sopenharmony_ci		if (seqp == hc->tx_seqh) {
6518c2ecf20Sopenharmony_ci			seqp = hc->tx_seqh->ccid2s_prev;
6528c2ecf20Sopenharmony_ci			break;
6538c2ecf20Sopenharmony_ci		}
6548c2ecf20Sopenharmony_ci	}
6558c2ecf20Sopenharmony_ci	done = 0;
6568c2ecf20Sopenharmony_ci	while (1) {
6578c2ecf20Sopenharmony_ci		if (seqp->ccid2s_acked) {
6588c2ecf20Sopenharmony_ci			done++;
6598c2ecf20Sopenharmony_ci			if (done == NUMDUPACK)
6608c2ecf20Sopenharmony_ci				break;
6618c2ecf20Sopenharmony_ci		}
6628c2ecf20Sopenharmony_ci		if (seqp == hc->tx_seqt)
6638c2ecf20Sopenharmony_ci			break;
6648c2ecf20Sopenharmony_ci		seqp = seqp->ccid2s_prev;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	/* If there are at least 3 acknowledgements, anything unacknowledged
6688c2ecf20Sopenharmony_ci	 * below the last sequence number is considered lost
6698c2ecf20Sopenharmony_ci	 */
6708c2ecf20Sopenharmony_ci	if (done == NUMDUPACK) {
6718c2ecf20Sopenharmony_ci		struct ccid2_seq *last_acked = seqp;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci		/* check for lost packets */
6748c2ecf20Sopenharmony_ci		while (1) {
6758c2ecf20Sopenharmony_ci			if (!seqp->ccid2s_acked) {
6768c2ecf20Sopenharmony_ci				ccid2_pr_debug("Packet lost: %llu\n",
6778c2ecf20Sopenharmony_ci					       (unsigned long long)seqp->ccid2s_seq);
6788c2ecf20Sopenharmony_ci				/* XXX need to traverse from tail -> head in
6798c2ecf20Sopenharmony_ci				 * order to detect multiple congestion events in
6808c2ecf20Sopenharmony_ci				 * one ack vector.
6818c2ecf20Sopenharmony_ci				 */
6828c2ecf20Sopenharmony_ci				ccid2_congestion_event(sk, seqp);
6838c2ecf20Sopenharmony_ci				hc->tx_pipe--;
6848c2ecf20Sopenharmony_ci			}
6858c2ecf20Sopenharmony_ci			if (seqp == hc->tx_seqt)
6868c2ecf20Sopenharmony_ci				break;
6878c2ecf20Sopenharmony_ci			seqp = seqp->ccid2s_prev;
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci		hc->tx_seqt = last_acked;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	/* trim acked packets in tail */
6948c2ecf20Sopenharmony_ci	while (hc->tx_seqt != hc->tx_seqh) {
6958c2ecf20Sopenharmony_ci		if (!hc->tx_seqt->ccid2s_acked)
6968c2ecf20Sopenharmony_ci			break;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci		hc->tx_seqt = hc->tx_seqt->ccid2s_next;
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	/* restart RTO timer if not all outstanding data has been acked */
7028c2ecf20Sopenharmony_ci	if (hc->tx_pipe == 0)
7038c2ecf20Sopenharmony_ci		sk_stop_timer(sk, &hc->tx_rtotimer);
7048c2ecf20Sopenharmony_ci	else
7058c2ecf20Sopenharmony_ci		sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
7068c2ecf20Sopenharmony_cidone:
7078c2ecf20Sopenharmony_ci	/* check if incoming Acks allow pending packets to be sent */
7088c2ecf20Sopenharmony_ci	if (sender_was_blocked && !ccid2_cwnd_network_limited(hc))
7098c2ecf20Sopenharmony_ci		dccp_tasklet_schedule(sk);
7108c2ecf20Sopenharmony_ci	dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_cistatic int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
7168c2ecf20Sopenharmony_ci	struct dccp_sock *dp = dccp_sk(sk);
7178c2ecf20Sopenharmony_ci	u32 max_ratio;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	/* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
7208c2ecf20Sopenharmony_ci	hc->tx_ssthresh = ~0U;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	/* Use larger initial windows (RFC 4341, section 5). */
7238c2ecf20Sopenharmony_ci	hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache);
7248c2ecf20Sopenharmony_ci	hc->tx_expected_wnd = hc->tx_cwnd;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	/* Make sure that Ack Ratio is enabled and within bounds. */
7278c2ecf20Sopenharmony_ci	max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
7288c2ecf20Sopenharmony_ci	if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
7298c2ecf20Sopenharmony_ci		dp->dccps_l_ack_ratio = max_ratio;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/* XXX init ~ to window size... */
7328c2ecf20Sopenharmony_ci	if (ccid2_hc_tx_alloc_seq(hc))
7338c2ecf20Sopenharmony_ci		return -ENOMEM;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	hc->tx_rto	 = DCCP_TIMEOUT_INIT;
7368c2ecf20Sopenharmony_ci	hc->tx_rpdupack  = -1;
7378c2ecf20Sopenharmony_ci	hc->tx_last_cong = hc->tx_lsndtime = hc->tx_cwnd_stamp = ccid2_jiffies32;
7388c2ecf20Sopenharmony_ci	hc->tx_cwnd_used = 0;
7398c2ecf20Sopenharmony_ci	hc->sk		 = sk;
7408c2ecf20Sopenharmony_ci	timer_setup(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire, 0);
7418c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hc->tx_av_chunks);
7428c2ecf20Sopenharmony_ci	return 0;
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cistatic void ccid2_hc_tx_exit(struct sock *sk)
7468c2ecf20Sopenharmony_ci{
7478c2ecf20Sopenharmony_ci	struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
7488c2ecf20Sopenharmony_ci	int i;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	sk_stop_timer(sk, &hc->tx_rtotimer);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	for (i = 0; i < hc->tx_seqbufc; i++)
7538c2ecf20Sopenharmony_ci		kfree(hc->tx_seqbuf[i]);
7548c2ecf20Sopenharmony_ci	hc->tx_seqbufc = 0;
7558c2ecf20Sopenharmony_ci	dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_cistatic void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	if (!dccp_data_packet(skb))
7638c2ecf20Sopenharmony_ci		return;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	if (++hc->rx_num_data_pkts >= dccp_sk(sk)->dccps_r_ack_ratio) {
7668c2ecf20Sopenharmony_ci		dccp_send_ack(sk);
7678c2ecf20Sopenharmony_ci		hc->rx_num_data_pkts = 0;
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistruct ccid_operations ccid2_ops = {
7728c2ecf20Sopenharmony_ci	.ccid_id		  = DCCPC_CCID2,
7738c2ecf20Sopenharmony_ci	.ccid_name		  = "TCP-like",
7748c2ecf20Sopenharmony_ci	.ccid_hc_tx_obj_size	  = sizeof(struct ccid2_hc_tx_sock),
7758c2ecf20Sopenharmony_ci	.ccid_hc_tx_init	  = ccid2_hc_tx_init,
7768c2ecf20Sopenharmony_ci	.ccid_hc_tx_exit	  = ccid2_hc_tx_exit,
7778c2ecf20Sopenharmony_ci	.ccid_hc_tx_send_packet	  = ccid2_hc_tx_send_packet,
7788c2ecf20Sopenharmony_ci	.ccid_hc_tx_packet_sent	  = ccid2_hc_tx_packet_sent,
7798c2ecf20Sopenharmony_ci	.ccid_hc_tx_parse_options = ccid2_hc_tx_parse_options,
7808c2ecf20Sopenharmony_ci	.ccid_hc_tx_packet_recv	  = ccid2_hc_tx_packet_recv,
7818c2ecf20Sopenharmony_ci	.ccid_hc_rx_obj_size	  = sizeof(struct ccid2_hc_rx_sock),
7828c2ecf20Sopenharmony_ci	.ccid_hc_rx_packet_recv	  = ccid2_hc_rx_packet_recv,
7838c2ecf20Sopenharmony_ci};
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
7868c2ecf20Sopenharmony_cimodule_param(ccid2_debug, bool, 0644);
7878c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
7888c2ecf20Sopenharmony_ci#endif
789