blob: 9ef9bee9610ffabbd041a7cdd82a1621d15f1129 [file] [log] [blame]
Thomas Gleixner79312872019-05-24 12:03:52 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07002/*
Gerrit Renker954c2db2007-12-12 14:06:14 -02003 * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
Ian McDonaldb2f41ff2007-05-28 12:23:29 -03004 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07006 *
7 * An implementation of the DCCP protocol
8 *
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070011 *
12 * This code also uses code from Lulea University, rereleased as GPL by its
13 * authors:
14 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15 *
16 * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17 * and to make it work as a loadable module in the DCCP stack written by
18 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
19 *
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070021 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070022#include "../dccp.h"
23#include "ccid3.h"
24
Gerrit Renker76fd1e82007-10-24 10:46:58 -020025#include <asm/unaligned.h>
26
Gerrit Renker56724aa2006-11-20 18:28:09 -020027#ifdef CONFIG_IP_DCCP_CCID3_DEBUG
Rusty Russelleb939922011-12-19 14:08:01 +000028static bool ccid3_debug;
Gerrit Renker56724aa2006-11-20 18:28:09 -020029#define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070030#else
31#define ccid3_pr_debug(format, a...)
32#endif
33
Gerrit Renker9bf17472007-03-20 13:11:24 -030034/*
35 * Transmitter Half-Connection Routines
36 */
Gerrit Renker410e27a2008-09-09 13:27:22 +020037#ifdef CONFIG_IP_DCCP_CCID3_DEBUG
38static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
39{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070040 static const char *const ccid3_state_names[] = {
Gerrit Renker410e27a2008-09-09 13:27:22 +020041 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
42 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
43 [TFRC_SSTATE_FBACK] = "FBACK",
Gerrit Renker410e27a2008-09-09 13:27:22 +020044 };
45
46 return ccid3_state_names[state];
47}
48#endif
49
50static void ccid3_hc_tx_set_state(struct sock *sk,
51 enum ccid3_hc_tx_states state)
52{
Gerrit Renker996ccf42009-10-05 00:53:13 +000053 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
54 enum ccid3_hc_tx_states oldstate = hc->tx_state;
Gerrit Renker410e27a2008-09-09 13:27:22 +020055
56 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
57 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
58 ccid3_tx_state_name(state));
59 WARN_ON(state == oldstate);
Gerrit Renker996ccf42009-10-05 00:53:13 +000060 hc->tx_state = state;
Gerrit Renker410e27a2008-09-09 13:27:22 +020061}
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070062
Gerrit Renker17893bc2006-11-27 20:31:33 -020063/*
Gerrit Renker6c08b2c2007-11-20 17:33:17 -020064 * Compute the initial sending rate X_init in the manner of RFC 3390:
65 *
Gerrit Renker410e27a2008-09-09 13:27:22 +020066 * X_init = min(4 * s, max(2 * s, 4380 bytes)) / RTT
Gerrit Renker6c08b2c2007-11-20 17:33:17 -020067 *
Gerrit Renker410e27a2008-09-09 13:27:22 +020068 * Note that RFC 3390 uses MSS, RFC 4342 refers to RFC 3390, and rfc3448bis
69 * (rev-02) clarifies the use of RFC 3390 with regard to the above formula.
Gerrit Renkera21f9f92007-03-20 15:12:10 -030070 * For consistency with other parts of the code, X_init is scaled by 2^6.
71 */
72static inline u64 rfc3390_initial_rate(struct sock *sk)
73{
Gerrit Renker996ccf42009-10-05 00:53:13 +000074 const struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
75 const __u32 w_init = clamp_t(__u32, 4380U, 2 * hc->tx_s, 4 * hc->tx_s);
Gerrit Renkera21f9f92007-03-20 15:12:10 -030076
Gerrit Renker996ccf42009-10-05 00:53:13 +000077 return scaled_div(w_init << 6, hc->tx_rtt);
Gerrit Renkera21f9f92007-03-20 15:12:10 -030078}
79
Gerrit Renker20cbd3e2010-09-14 20:16:59 +020080/**
81 * ccid3_update_send_interval - Calculate new t_ipi = s / X_inst
82 * This respects the granularity of X_inst (64 * bytes/second).
Gerrit Renker17893bc2006-11-27 20:31:33 -020083 */
Gerrit Renker996ccf42009-10-05 00:53:13 +000084static void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070085{
Gerrit Renker996ccf42009-10-05 00:53:13 +000086 hc->tx_t_ipi = scaled_div32(((u64)hc->tx_s) << 6, hc->tx_x);
Gerrit Renker410e27a2008-09-09 13:27:22 +020087
Gerrit Renker793734b2012-02-27 12:29:44 -070088 DCCP_BUG_ON(hc->tx_t_ipi == 0);
Gerrit Renker20cbd3e2010-09-14 20:16:59 +020089 ccid3_pr_debug("t_ipi=%u, s=%u, X=%u\n", hc->tx_t_ipi,
Eric Dumazet95c96172012-04-15 05:58:06 +000090 hc->tx_s, (unsigned int)(hc->tx_x >> 6));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070091}
Gerrit Renkeraa97efd2007-09-25 22:39:16 -070092
Gerrit Renker996ccf42009-10-05 00:53:13 +000093static u32 ccid3_hc_tx_idle_rtt(struct ccid3_hc_tx_sock *hc, ktime_t now)
Gerrit Renkera5358fd2007-11-20 18:01:59 -020094{
Gerrit Renker996ccf42009-10-05 00:53:13 +000095 u32 delta = ktime_us_delta(now, hc->tx_t_last_win_count);
Gerrit Renkera5358fd2007-11-20 18:01:59 -020096
Gerrit Renker996ccf42009-10-05 00:53:13 +000097 return delta / hc->tx_rtt;
Gerrit Renkera5358fd2007-11-20 18:01:59 -020098}
99
Gerrit Renkeraa97efd2007-09-25 22:39:16 -0700100/**
101 * ccid3_hc_tx_update_x - Update allowed sending rate X
102 * @stamp: most recent time if available - can be left NULL.
Ben Hutchings2c530402012-07-10 10:55:09 +0000103 *
Gerrit Renkeraa97efd2007-09-25 22:39:16 -0700104 * This function tracks draft rfc3448bis, check there for latest details.
Gerrit Renker5c3fbb62006-12-03 14:50:56 -0200105 *
Gerrit Renker1a21e492006-12-10 00:02:12 -0200106 * Note: X and X_recv are both stored in units of 64 * bytes/second, to support
107 * fine-grained resolution of sending rates. This requires scaling by 2^6
108 * throughout the code. Only X_calc is unscaled (in bytes/second).
109 *
Gerrit Renker1a21e492006-12-10 00:02:12 -0200110 */
Gerrit Renkeraa97efd2007-09-25 22:39:16 -0700111static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700112{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000113 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
114 __u64 min_rate = 2 * hc->tx_x_recv;
115 const __u64 old_x = hc->tx_x;
Gerrit Renker52515e72007-12-17 12:57:43 -0200116 ktime_t now = stamp ? *stamp : ktime_get_real();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700117
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300118 /*
119 * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
Gerrit Renkera5358fd2007-11-20 18:01:59 -0200120 * when idling [RFC 4342, 5.1]. Definition of idling is from rfc3448bis:
121 * a sender is idle if it has not sent anything over a 2-RTT-period.
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300122 * For consistency with X and X_recv, min_rate is also scaled by 2^6.
123 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000124 if (ccid3_hc_tx_idle_rtt(hc, now) >= 2) {
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300125 min_rate = rfc3390_initial_rate(sk);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000126 min_rate = max(min_rate, 2 * hc->tx_x_recv);
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300127 }
128
Gerrit Renker996ccf42009-10-05 00:53:13 +0000129 if (hc->tx_p > 0) {
Gerrit Renker1a21e492006-12-10 00:02:12 -0200130
Gerrit Renker996ccf42009-10-05 00:53:13 +0000131 hc->tx_x = min(((__u64)hc->tx_x_calc) << 6, min_rate);
132 hc->tx_x = max(hc->tx_x, (((__u64)hc->tx_s) << 6) / TFRC_T_MBI);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300133
Gerrit Renker996ccf42009-10-05 00:53:13 +0000134 } else if (ktime_us_delta(now, hc->tx_t_ld) - (s64)hc->tx_rtt >= 0) {
Gerrit Renker1a21e492006-12-10 00:02:12 -0200135
Gerrit Renker996ccf42009-10-05 00:53:13 +0000136 hc->tx_x = min(2 * hc->tx_x, min_rate);
137 hc->tx_x = max(hc->tx_x,
138 scaled_div(((__u64)hc->tx_s) << 6, hc->tx_rtt));
139 hc->tx_t_ld = now;
Gerrit Renkerff586292006-12-10 00:00:14 -0200140 }
Gerrit Renkera79ef762006-11-28 19:51:42 -0200141
Gerrit Renker996ccf42009-10-05 00:53:13 +0000142 if (hc->tx_x != old_x) {
Gerrit Renker1761f7d2007-03-20 15:04:30 -0300143 ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "
Eric Dumazet95c96172012-04-15 05:58:06 +0000144 "X_recv=%u\n", (unsigned int)(old_x >> 6),
145 (unsigned int)(hc->tx_x >> 6), hc->tx_x_calc,
146 (unsigned int)(hc->tx_x_recv >> 6));
Ian McDonald8699be72007-03-20 14:49:20 -0300147
Gerrit Renker996ccf42009-10-05 00:53:13 +0000148 ccid3_update_send_interval(hc);
Ian McDonald8699be72007-03-20 14:49:20 -0300149 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700150}
151
Ben Hutchings2c530402012-07-10 10:55:09 +0000152/**
153 * ccid3_hc_tx_update_s - Track the mean packet size `s'
Gerrit Renker410e27a2008-09-09 13:27:22 +0200154 * @len: DCCP packet payload size in bytes
Ben Hutchings2c530402012-07-10 10:55:09 +0000155 *
156 * cf. RFC 4342, 5.3 and RFC 3448, 4.1
Gerrit Renker78ad7132006-11-28 19:22:33 -0200157 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000158static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hc, int len)
Gerrit Renker78ad7132006-11-28 19:22:33 -0200159{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000160 const u16 old_s = hc->tx_s;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200161
Gerrit Renker996ccf42009-10-05 00:53:13 +0000162 hc->tx_s = tfrc_ewma(hc->tx_s, len, 9);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200163
Gerrit Renker996ccf42009-10-05 00:53:13 +0000164 if (hc->tx_s != old_s)
165 ccid3_update_send_interval(hc);
Gerrit Renker78ad7132006-11-28 19:22:33 -0200166}
167
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200168/*
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200169 * Update Window Counter using the algorithm from [RFC 4342, 8.1].
Gerrit Renker825de272008-05-27 06:33:54 -0700170 * As elsewhere, RTT > 0 is assumed by using dccp_sample_rtt().
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200171 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000172static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hc,
Gerrit Renker8132da42007-06-16 13:34:02 -0300173 ktime_t now)
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200174{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000175 u32 delta = ktime_us_delta(now, hc->tx_t_last_win_count),
176 quarter_rtts = (4 * delta) / hc->tx_rtt;
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200177
178 if (quarter_rtts > 0) {
Gerrit Renker996ccf42009-10-05 00:53:13 +0000179 hc->tx_t_last_win_count = now;
180 hc->tx_last_win_count += min(quarter_rtts, 5U);
181 hc->tx_last_win_count &= 0xF; /* mod 16 */
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200182 }
183}
184
Kees Cook839a6092017-10-24 01:46:09 -0700185static void ccid3_hc_tx_no_feedback_timer(struct timer_list *t)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700186{
Kees Cook839a6092017-10-24 01:46:09 -0700187 struct ccid3_hc_tx_sock *hc = from_timer(hc, t, tx_no_feedback_timer);
188 struct sock *sk = hc->sk;
Gerrit Renker2a1fda62006-11-28 18:34:34 -0200189 unsigned long t_nfb = USEC_PER_SEC / 5;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700190
191 bh_lock_sock(sk);
192 if (sock_owned_by_user(sk)) {
193 /* Try again later. */
194 /* XXX: set some sensible MIB */
Gerrit Renker48e03ee2006-11-27 20:29:27 -0200195 goto restart_timer;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700196 }
197
Frans Popb1383382010-03-24 07:57:28 +0000198 ccid3_pr_debug("%s(%p, state=%s) - entry\n", dccp_role(sk), sk,
Gerrit Renker996ccf42009-10-05 00:53:13 +0000199 ccid3_tx_state_name(hc->tx_state));
Gerrit Renkera9672412006-12-10 00:14:12 -0200200
Gerrit Renker80763dfb2010-09-19 20:08:24 +0200201 /* Ignore and do not restart after leaving the established state */
202 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
203 goto out;
204
205 /* Reset feedback state to "no feedback received" */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000206 if (hc->tx_state == TFRC_SSTATE_FBACK)
Gerrit Renker410e27a2008-09-09 13:27:22 +0200207 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
Gerrit Renkerd0995e62008-09-04 07:30:19 +0200208
Gerrit Renker52515e72007-12-17 12:57:43 -0200209 /*
210 * Determine new allowed sending rate X as per draft rfc3448bis-00, 4.4
Gerrit Renker67b67e32010-08-22 19:41:36 +0000211 * RTO is 0 if and only if no feedback has been received yet.
Gerrit Renker52515e72007-12-17 12:57:43 -0200212 */
Gerrit Renker67b67e32010-08-22 19:41:36 +0000213 if (hc->tx_t_rto == 0 || hc->tx_p == 0) {
Gerrit Renker52515e72007-12-17 12:57:43 -0200214
215 /* halve send rate directly */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000216 hc->tx_x = max(hc->tx_x / 2,
217 (((__u64)hc->tx_s) << 6) / TFRC_T_MBI);
218 ccid3_update_send_interval(hc);
Gerrit Renker52515e72007-12-17 12:57:43 -0200219 } else {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300220 /*
Gerrit Renker52515e72007-12-17 12:57:43 -0200221 * Modify the cached value of X_recv
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300222 *
Gerrit Renker52515e72007-12-17 12:57:43 -0200223 * If (X_calc > 2 * X_recv)
Gerrit Renker0c150ef2007-03-20 15:19:07 -0300224 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
225 * Else
226 * X_recv = X_calc / 4;
227 *
228 * Note that X_recv is scaled by 2^6 while X_calc is not
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300229 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000230 if (hc->tx_x_calc > (hc->tx_x_recv >> 5))
231 hc->tx_x_recv =
232 max(hc->tx_x_recv / 2,
233 (((__u64)hc->tx_s) << 6) / (2*TFRC_T_MBI));
Gerrit Renker52515e72007-12-17 12:57:43 -0200234 else {
Gerrit Renker996ccf42009-10-05 00:53:13 +0000235 hc->tx_x_recv = hc->tx_x_calc;
236 hc->tx_x_recv <<= 4;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700237 }
Gerrit Renkeraa97efd2007-09-25 22:39:16 -0700238 ccid3_hc_tx_update_x(sk, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700239 }
Gerrit Renker52515e72007-12-17 12:57:43 -0200240 ccid3_pr_debug("Reduced X to %llu/64 bytes/sec\n",
Gerrit Renker996ccf42009-10-05 00:53:13 +0000241 (unsigned long long)hc->tx_x);
Gerrit Renker52515e72007-12-17 12:57:43 -0200242
243 /*
244 * Set new timeout for the nofeedback timer.
245 * See comments in packet_recv() regarding the value of t_RTO.
246 */
Gerrit Renker67b67e32010-08-22 19:41:36 +0000247 if (unlikely(hc->tx_t_rto == 0)) /* no feedback received yet */
Gerrit Renker52515e72007-12-17 12:57:43 -0200248 t_nfb = TFRC_INITIAL_TIMEOUT;
249 else
Gerrit Renker996ccf42009-10-05 00:53:13 +0000250 t_nfb = max(hc->tx_t_rto, 2 * hc->tx_t_ipi);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700251
Gerrit Renker48e03ee2006-11-27 20:29:27 -0200252restart_timer:
Gerrit Renker996ccf42009-10-05 00:53:13 +0000253 sk_reset_timer(sk, &hc->tx_no_feedback_timer,
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900254 jiffies + usecs_to_jiffies(t_nfb));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700255out:
256 bh_unlock_sock(sk);
257 sock_put(sk);
258}
259
Gerrit Renkerfe84f412010-10-27 19:16:25 +0000260/**
261 * ccid3_hc_tx_send_packet - Delay-based dequeueing of TX packets
262 * @skb: next packet candidate to send on @sk
Ben Hutchings2c530402012-07-10 10:55:09 +0000263 *
Gerrit Renkerfe84f412010-10-27 19:16:25 +0000264 * This function uses the convention of ccid_packet_dequeue_eval() and
265 * returns a millisecond-delay value between 0 and t_mbi = 64000 msec.
Gerrit Renker7da7f452006-11-27 12:26:03 -0200266 */
Gerrit Renker6b57c932006-11-28 19:55:06 -0200267static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700268{
269 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000270 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Gerrit Renker8132da42007-06-16 13:34:02 -0300271 ktime_t now = ktime_get_real();
272 s64 delay;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700273
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700274 /*
Gerrit Renkerda335ba2006-11-27 12:26:57 -0200275 * This function is called only for Data and DataAck packets. Sending
276 * zero-sized Data(Ack)s is theoretically possible, but for congestion
277 * control this case is pathological - ignore it.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700278 */
Gerrit Renker6b57c932006-11-28 19:55:06 -0200279 if (unlikely(skb->len == 0))
Gerrit Renkerda335ba2006-11-27 12:26:57 -0200280 return -EBADMSG;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700281
Gerrit Renker80763dfb2010-09-19 20:08:24 +0200282 if (hc->tx_state == TFRC_SSTATE_NO_SENT) {
Gerrit Renker996ccf42009-10-05 00:53:13 +0000283 sk_reset_timer(sk, &hc->tx_no_feedback_timer, (jiffies +
284 usecs_to_jiffies(TFRC_INITIAL_TIMEOUT)));
285 hc->tx_last_win_count = 0;
286 hc->tx_t_last_win_count = now;
Gerrit Renker90feeb92006-11-27 12:13:38 -0200287
288 /* Set t_0 for initial packet */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000289 hc->tx_t_nom = now;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200290
Gerrit Renker996ccf42009-10-05 00:53:13 +0000291 hc->tx_s = skb->len;
Gerrit Renker30833ff2007-03-20 15:31:56 -0300292
Gerrit Renker30833ff2007-03-20 15:31:56 -0300293 /*
294 * Use initial RTT sample when available: recommended by erratum
295 * to RFC 4342. This implements the initialisation procedure of
296 * draft rfc3448bis, section 4.2. Remember, X is scaled by 2^6.
297 */
298 if (dp->dccps_syn_rtt) {
299 ccid3_pr_debug("SYN RTT = %uus\n", dp->dccps_syn_rtt);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000300 hc->tx_rtt = dp->dccps_syn_rtt;
301 hc->tx_x = rfc3390_initial_rate(sk);
302 hc->tx_t_ld = now;
Gerrit Renker30833ff2007-03-20 15:31:56 -0300303 } else {
Gerrit Renker3294f202008-06-11 11:19:09 +0100304 /*
305 * Sender does not have RTT sample:
306 * - set fallback RTT (RFC 4340, 3.4) since a RTT value
307 * is needed in several parts (e.g. window counter);
308 * - set sending rate X_pps = 1pps as per RFC 3448, 4.2.
309 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000310 hc->tx_rtt = DCCP_FALLBACK_RTT;
311 hc->tx_x = hc->tx_s;
312 hc->tx_x <<= 6;
Gerrit Renker30833ff2007-03-20 15:31:56 -0300313 }
Gerrit Renker996ccf42009-10-05 00:53:13 +0000314 ccid3_update_send_interval(hc);
Gerrit Renker30833ff2007-03-20 15:31:56 -0300315
Gerrit Renker410e27a2008-09-09 13:27:22 +0200316 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
Gerrit Renker80763dfb2010-09-19 20:08:24 +0200317
318 } else {
Gerrit Renker996ccf42009-10-05 00:53:13 +0000319 delay = ktime_us_delta(hc->tx_t_nom, now);
Ian McDonald8699be72007-03-20 14:49:20 -0300320 ccid3_pr_debug("delay=%ld\n", (long)delay);
Gerrit Renker91cf5a12006-11-27 12:25:10 -0200321 /*
Gerrit Renker20cbd3e2010-09-14 20:16:59 +0200322 * Scheduling of packet transmissions (RFC 5348, 8.3)
Gerrit Renker91cf5a12006-11-27 12:25:10 -0200323 *
324 * if (t_now > t_nom - delta)
325 * // send the packet now
326 * else
327 * // send the packet in (t_nom - t_now) milliseconds.
328 */
Gerrit Renker20cbd3e2010-09-14 20:16:59 +0200329 if (delay >= TFRC_T_DELTA)
330 return (u32)delay / USEC_PER_MSEC;
Gerrit Renker9f8681d2006-12-10 00:07:37 -0200331
Gerrit Renker996ccf42009-10-05 00:53:13 +0000332 ccid3_hc_tx_update_win_count(hc, now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700333 }
334
Gerrit Renker7da7f452006-11-27 12:26:03 -0200335 /* prepare to send now (add options etc.) */
336 dp->dccps_hc_tx_insert_options = 1;
Gerrit Renker996ccf42009-10-05 00:53:13 +0000337 DCCP_SKB_CB(skb)->dccpd_ccval = hc->tx_last_win_count;
Gerrit Renkere312d102006-12-10 00:08:09 -0200338
339 /* set the nominal send time for the next following packet */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000340 hc->tx_t_nom = ktime_add_us(hc->tx_t_nom, hc->tx_t_ipi);
Gerrit Renkerfe84f412010-10-27 19:16:25 +0000341 return CCID_PACKET_SEND_AT_ONCE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700342}
343
Gerrit Renkerbaf9e782010-10-11 20:37:38 +0200344static void ccid3_hc_tx_packet_sent(struct sock *sk, unsigned int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700345{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000346 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700347
Gerrit Renker996ccf42009-10-05 00:53:13 +0000348 ccid3_hc_tx_update_s(hc, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700349
Gerrit Renker996ccf42009-10-05 00:53:13 +0000350 if (tfrc_tx_hist_add(&hc->tx_hist, dccp_sk(sk)->dccps_gss))
Gerrit Renkerc5a1ae92006-12-10 00:09:21 -0200351 DCCP_CRIT("packet history - out of memory!");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700352}
353
354static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
355{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000356 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Gerrit Renkerd2c72632010-09-14 20:18:00 +0200357 struct tfrc_tx_hist_entry *acked;
Arnaldo Carvalho de Melo0740d492007-08-19 17:18:13 -0700358 ktime_t now;
Gerrit Renker2a1fda62006-11-28 18:34:34 -0200359 unsigned long t_nfb;
Gerrit Renker536bb202010-09-19 20:14:23 +0200360 u32 r_sample;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300361
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700362 /* we are only interested in ACKs */
363 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
364 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
365 return;
Gerrit Renkerd2c72632010-09-14 20:18:00 +0200366 /*
367 * Locate the acknowledged packet in the TX history.
368 *
369 * Returning "entry not found" here can for instance happen when
370 * - the host has not sent out anything (e.g. a passive server),
371 * - the Ack is outdated (packet with higher Ack number was received),
372 * - it is a bogus Ack (for a packet not sent on this connection).
373 */
374 acked = tfrc_tx_hist_find_entry(hc->tx_hist, dccp_hdr_ack_seq(skb));
375 if (acked == NULL)
Gerrit Renker410e27a2008-09-09 13:27:22 +0200376 return;
Gerrit Renkerd2c72632010-09-14 20:18:00 +0200377 /* For the sake of RTT sampling, ignore/remove all older entries */
378 tfrc_tx_hist_purge(&acked->next);
379
380 /* Update the moving average for the RTT estimate (RFC 3448, 4.3) */
381 now = ktime_get_real();
382 r_sample = dccp_sample_rtt(sk, ktime_us_delta(now, acked->stamp));
383 hc->tx_rtt = tfrc_ewma(hc->tx_rtt, r_sample, 9);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200384
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200385 /*
386 * Update allowed sending rate X as per draft rfc3448bis-00, 4.2/3
387 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000388 if (hc->tx_state == TFRC_SSTATE_NO_FBACK) {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200389 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200390
Gerrit Renker996ccf42009-10-05 00:53:13 +0000391 if (hc->tx_t_rto == 0) {
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200392 /*
393 * Initial feedback packet: Larger Initial Windows (4.2)
394 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000395 hc->tx_x = rfc3390_initial_rate(sk);
396 hc->tx_t_ld = now;
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200397
Gerrit Renker996ccf42009-10-05 00:53:13 +0000398 ccid3_update_send_interval(hc);
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200399
400 goto done_computing_x;
Gerrit Renker996ccf42009-10-05 00:53:13 +0000401 } else if (hc->tx_p == 0) {
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200402 /*
403 * First feedback after nofeedback timer expiry (4.3)
404 */
405 goto done_computing_x;
406 }
407 }
408
409 /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000410 if (hc->tx_p > 0)
411 hc->tx_x_calc = tfrc_calc_x(hc->tx_s, hc->tx_rtt, hc->tx_p);
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200412 ccid3_hc_tx_update_x(sk, &now);
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200413
Gerrit Renkerd8d12522007-12-17 12:48:47 -0200414done_computing_x:
415 ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200416 "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
Gerrit Renker996ccf42009-10-05 00:53:13 +0000417 dccp_role(sk), sk, hc->tx_rtt, r_sample,
418 hc->tx_s, hc->tx_p, hc->tx_x_calc,
Eric Dumazet95c96172012-04-15 05:58:06 +0000419 (unsigned int)(hc->tx_x_recv >> 6),
420 (unsigned int)(hc->tx_x >> 6));
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200421
422 /* unschedule no feedback timer */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000423 sk_stop_timer(sk, &hc->tx_no_feedback_timer);
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200424
425 /*
426 * As we have calculated new ipi, delta, t_nom it is possible
427 * that we now can send a packet, so wake up dccp_wait_for_ccid
428 */
429 sk->sk_write_space(sk);
430
431 /*
Gerrit Renker89858ad2010-08-29 19:23:14 +0000432 * Update timeout interval for the nofeedback timer. In order to control
433 * rate halving on networks with very low RTTs (<= 1 ms), use per-route
434 * tunable RTAX_RTO_MIN value as the lower bound.
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200435 */
Gerrit Renker89858ad2010-08-29 19:23:14 +0000436 hc->tx_t_rto = max_t(u32, 4 * hc->tx_rtt,
437 USEC_PER_SEC/HZ * tcp_rto_min(sk));
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200438 /*
439 * Schedule no feedback timer to expire in
440 * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
441 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000442 t_nfb = max(hc->tx_t_rto, 2 * hc->tx_t_ipi);
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200443
444 ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
445 "expire in %lu jiffies (%luus)\n",
Gerrit Renker388d5e92009-10-05 00:53:11 +0000446 dccp_role(sk), sk, usecs_to_jiffies(t_nfb), t_nfb);
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200447
Gerrit Renker996ccf42009-10-05 00:53:13 +0000448 sk_reset_timer(sk, &hc->tx_no_feedback_timer,
Gerrit Renker5bd370a2007-12-17 10:25:06 -0200449 jiffies + usecs_to_jiffies(t_nfb));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700450}
451
Gerrit Renker4874c132010-09-19 20:06:50 +0200452static int ccid3_hc_tx_parse_options(struct sock *sk, u8 packet_type,
453 u8 option, u8 *optval, u8 optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700454{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000455 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Gerrit Renker76fd1e82007-10-24 10:46:58 -0200456 __be32 opt_val;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700457
Gerrit Renker410e27a2008-09-09 13:27:22 +0200458 switch (option) {
Gerrit Renker37efb032010-09-14 20:21:29 +0200459 case TFRC_OPT_RECEIVE_RATE:
Gerrit Renker410e27a2008-09-09 13:27:22 +0200460 case TFRC_OPT_LOSS_EVENT_RATE:
Gerrit Renker4874c132010-09-19 20:06:50 +0200461 /* Must be ignored on Data packets, cf. RFC 4342 8.3 and 8.5 */
462 if (packet_type == DCCP_PKT_DATA)
463 break;
464 if (unlikely(optlen != 4)) {
Gerrit Renker37efb032010-09-14 20:21:29 +0200465 DCCP_WARN("%s(%p), invalid len %d for %u\n",
Gerrit Renker4874c132010-09-19 20:06:50 +0200466 dccp_role(sk), sk, optlen, option);
Gerrit Renker37efb032010-09-14 20:21:29 +0200467 return -EINVAL;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200468 }
Gerrit Renker4874c132010-09-19 20:06:50 +0200469 opt_val = ntohl(get_unaligned((__be32 *)optval));
Gerrit Renker410e27a2008-09-09 13:27:22 +0200470
Gerrit Renker37efb032010-09-14 20:21:29 +0200471 if (option == TFRC_OPT_RECEIVE_RATE) {
Gerrit Renker536bb202010-09-19 20:14:23 +0200472 /* Receive Rate is kept in units of 64 bytes/second */
473 hc->tx_x_recv = opt_val;
474 hc->tx_x_recv <<= 6;
475
Gerrit Renker37efb032010-09-14 20:21:29 +0200476 ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
477 dccp_role(sk), sk, opt_val);
478 } else {
Gerrit Renker536bb202010-09-19 20:14:23 +0200479 /* Update the fixpoint Loss Event Rate fraction */
480 hc->tx_p = tfrc_invert_loss_event_rate(opt_val);
481
Gerrit Renker37efb032010-09-14 20:21:29 +0200482 ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
483 dccp_role(sk), sk, opt_val);
484 }
485 }
486 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700487}
488
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800489static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700490{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000491 struct ccid3_hc_tx_sock *hc = ccid_priv(ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700492
Gerrit Renker996ccf42009-10-05 00:53:13 +0000493 hc->tx_state = TFRC_SSTATE_NO_SENT;
494 hc->tx_hist = NULL;
Kees Cook839a6092017-10-24 01:46:09 -0700495 hc->sk = sk;
496 timer_setup(&hc->tx_no_feedback_timer,
497 ccid3_hc_tx_no_feedback_timer, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700498 return 0;
499}
500
501static void ccid3_hc_tx_exit(struct sock *sk)
502{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000503 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504
Gerrit Renker996ccf42009-10-05 00:53:13 +0000505 sk_stop_timer(sk, &hc->tx_no_feedback_timer);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000506 tfrc_tx_hist_purge(&hc->tx_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700507}
508
Gerrit Renker9bf17472007-03-20 13:11:24 -0300509static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
510{
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000511 info->tcpi_rto = ccid3_hc_tx_sk(sk)->tx_t_rto;
512 info->tcpi_rtt = ccid3_hc_tx_sk(sk)->tx_rtt;
Gerrit Renker9bf17472007-03-20 13:11:24 -0300513}
514
515static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
516 u32 __user *optval, int __user *optlen)
517{
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000518 const struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
Gerrit Renker67b67e32010-08-22 19:41:36 +0000519 struct tfrc_tx_info tfrc;
Gerrit Renker9bf17472007-03-20 13:11:24 -0300520 const void *val;
521
Gerrit Renker9bf17472007-03-20 13:11:24 -0300522 switch (optname) {
523 case DCCP_SOCKOPT_CCID_TX_INFO:
Gerrit Renker67b67e32010-08-22 19:41:36 +0000524 if (len < sizeof(tfrc))
Gerrit Renker9bf17472007-03-20 13:11:24 -0300525 return -EINVAL;
Mathias Krause7b07f8e2012-08-15 11:31:55 +0000526 memset(&tfrc, 0, sizeof(tfrc));
Gerrit Renker67b67e32010-08-22 19:41:36 +0000527 tfrc.tfrctx_x = hc->tx_x;
528 tfrc.tfrctx_x_recv = hc->tx_x_recv;
529 tfrc.tfrctx_x_calc = hc->tx_x_calc;
530 tfrc.tfrctx_rtt = hc->tx_rtt;
531 tfrc.tfrctx_p = hc->tx_p;
532 tfrc.tfrctx_rto = hc->tx_t_rto;
533 tfrc.tfrctx_ipi = hc->tx_t_ipi;
534 len = sizeof(tfrc);
535 val = &tfrc;
Gerrit Renker9bf17472007-03-20 13:11:24 -0300536 break;
537 default:
538 return -ENOPROTOOPT;
539 }
540
541 if (put_user(len, optlen) || copy_to_user(optval, val, len))
542 return -EFAULT;
543
544 return 0;
545}
546
547/*
548 * Receiver Half-Connection Routines
549 */
Gerrit Renker410e27a2008-09-09 13:27:22 +0200550
551/* CCID3 feedback types */
552enum ccid3_fback_type {
553 CCID3_FBACK_NONE = 0,
554 CCID3_FBACK_INITIAL,
555 CCID3_FBACK_PERIODIC,
556 CCID3_FBACK_PARAM_CHANGE
557};
558
559#ifdef CONFIG_IP_DCCP_CCID3_DEBUG
560static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
561{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700562 static const char *const ccid3_rx_state_names[] = {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200563 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
564 [TFRC_RSTATE_DATA] = "DATA",
Gerrit Renker410e27a2008-09-09 13:27:22 +0200565 };
566
567 return ccid3_rx_state_names[state];
568}
569#endif
570
571static void ccid3_hc_rx_set_state(struct sock *sk,
572 enum ccid3_hc_rx_states state)
573{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000574 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
575 enum ccid3_hc_rx_states oldstate = hc->rx_state;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200576
577 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
578 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
579 ccid3_rx_state_name(state));
580 WARN_ON(state == oldstate);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000581 hc->rx_state = state;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200582}
583
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200584static void ccid3_hc_rx_send_feedback(struct sock *sk,
585 const struct sk_buff *skb,
586 enum ccid3_fback_type fbtype)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700587{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000588 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200589 struct dccp_sock *dp = dccp_sk(sk);
Eric Dumazet0ce4e702018-06-22 06:44:15 -0700590 ktime_t now = ktime_get();
Gerrit Renker410e27a2008-09-09 13:27:22 +0200591 s64 delta = 0;
592
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200593 switch (fbtype) {
594 case CCID3_FBACK_INITIAL:
Gerrit Renker996ccf42009-10-05 00:53:13 +0000595 hc->rx_x_recv = 0;
596 hc->rx_pinv = ~0U; /* see RFC 4342, 8.5 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700597 break;
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200598 case CCID3_FBACK_PARAM_CHANGE:
599 /*
600 * When parameters change (new loss or p > p_prev), we do not
601 * have a reliable estimate for R_m of [RFC 3448, 6.2] and so
Gerrit Renker410e27a2008-09-09 13:27:22 +0200602 * need to reuse the previous value of X_recv. However, when
603 * X_recv was 0 (due to early loss), this would kill X down to
604 * s/t_mbi (i.e. one packet in 64 seconds).
605 * To avoid such drastic reduction, we approximate X_recv as
606 * the number of bytes since last feedback.
607 * This is a safe fallback, since X is bounded above by X_calc.
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200608 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000609 if (hc->rx_x_recv > 0)
Gerrit Renker410e27a2008-09-09 13:27:22 +0200610 break;
611 /* fall through */
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200612 case CCID3_FBACK_PERIODIC:
Gerrit Renker996ccf42009-10-05 00:53:13 +0000613 delta = ktime_us_delta(now, hc->rx_tstamp_last_feedback);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200614 if (delta <= 0)
Eric Dumazet74174fe2018-06-22 06:44:14 -0700615 delta = 1;
616 hc->rx_x_recv = scaled_div32(hc->rx_bytes_recv, delta);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700617 break;
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200618 default:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700619 return;
620 }
621
Eric Dumazet0ce4e702018-06-22 06:44:15 -0700622 ccid3_pr_debug("Interval %lldusec, X_recv=%u, 1/p=%u\n", delta,
Gerrit Renker996ccf42009-10-05 00:53:13 +0000623 hc->rx_x_recv, hc->rx_pinv);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700624
Gerrit Renker996ccf42009-10-05 00:53:13 +0000625 hc->rx_tstamp_last_feedback = now;
626 hc->rx_last_counter = dccp_hdr(skb)->dccph_ccval;
627 hc->rx_bytes_recv = 0;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200628
629 dp->dccps_hc_rx_insert_options = 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700630 dccp_send_ack(sk);
631}
632
Arnaldo Carvalho de Melo2d0817d2006-03-20 22:32:06 -0800633static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700634{
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000635 const struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800636 __be32 x_recv, pinv;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700637
Arnaldo Carvalho de Melo59d203f2005-09-09 20:01:25 -0300638 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
Arnaldo Carvalho de Melo2d0817d2006-03-20 22:32:06 -0800639 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700640
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700641 if (dccp_packet_without_ack(skb))
Arnaldo Carvalho de Melo2d0817d2006-03-20 22:32:06 -0800642 return 0;
643
Gerrit Renker996ccf42009-10-05 00:53:13 +0000644 x_recv = htonl(hc->rx_x_recv);
645 pinv = htonl(hc->rx_pinv);
Arnaldo Carvalho de Melo2d0817d2006-03-20 22:32:06 -0800646
Gerrit Renkera7d13fb2010-06-22 01:14:34 +0000647 if (dccp_insert_option(skb, TFRC_OPT_LOSS_EVENT_RATE,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200648 &pinv, sizeof(pinv)) ||
Gerrit Renkera7d13fb2010-06-22 01:14:34 +0000649 dccp_insert_option(skb, TFRC_OPT_RECEIVE_RATE,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200650 &x_recv, sizeof(x_recv)))
Arnaldo Carvalho de Melo2d0817d2006-03-20 22:32:06 -0800651 return -1;
652
653 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700654}
655
Gerrit Renkeraa1b1ff2009-09-12 07:47:01 +0000656/**
657 * ccid3_first_li - Implements [RFC 5348, 6.3.1]
Gerrit Renker954c2db2007-12-12 14:06:14 -0200658 *
659 * Determine the length of the first loss interval via inverse lookup.
660 * Assume that X_recv can be computed by the throughput equation
661 * s
662 * X_recv = --------
663 * R * fval
664 * Find some p such that f(p) = fval; return 1/p (scaled).
665 */
666static u32 ccid3_first_li(struct sock *sk)
667{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000668 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Eric Dumazet0ce4e702018-06-22 06:44:15 -0700669 u32 x_recv, p;
670 s64 delta;
Gerrit Renker954c2db2007-12-12 14:06:14 -0200671 u64 fval;
672
Gerrit Renker996ccf42009-10-05 00:53:13 +0000673 if (hc->rx_rtt == 0) {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200674 DCCP_WARN("No RTT estimate available, using fallback RTT\n");
Gerrit Renker996ccf42009-10-05 00:53:13 +0000675 hc->rx_rtt = DCCP_FALLBACK_RTT;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200676 }
Gerrit Renkerd20ed952008-09-04 07:30:19 +0200677
Eric Dumazet0ce4e702018-06-22 06:44:15 -0700678 delta = ktime_us_delta(ktime_get(), hc->rx_tstamp_last_feedback);
679 if (delta <= 0)
680 delta = 1;
Gerrit Renker996ccf42009-10-05 00:53:13 +0000681 x_recv = scaled_div32(hc->rx_bytes_recv, delta);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200682 if (x_recv == 0) { /* would also trigger divide-by-zero */
683 DCCP_WARN("X_recv==0\n");
Gerrit Renker67b67e32010-08-22 19:41:36 +0000684 if (hc->rx_x_recv == 0) {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200685 DCCP_BUG("stored value of X_recv is zero");
686 return ~0U;
687 }
Gerrit Renker67b67e32010-08-22 19:41:36 +0000688 x_recv = hc->rx_x_recv;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200689 }
Gerrit Renker954c2db2007-12-12 14:06:14 -0200690
Gerrit Renker996ccf42009-10-05 00:53:13 +0000691 fval = scaled_div(hc->rx_s, hc->rx_rtt);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200692 fval = scaled_div32(fval, x_recv);
Gerrit Renker954c2db2007-12-12 14:06:14 -0200693 p = tfrc_calc_x_reverse_lookup(fval);
694
695 ccid3_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
696 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
697
Gerrit Renker410e27a2008-09-09 13:27:22 +0200698 return p == 0 ? ~0U : scaled_div(1, p);
Gerrit Renker954c2db2007-12-12 14:06:14 -0200699}
700
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700701static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
702{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000703 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200704 enum ccid3_fback_type do_feedback = CCID3_FBACK_NONE;
Gerrit Renker5b5d0e72008-07-13 11:51:40 +0100705 const u64 ndp = dccp_sk(sk)->dccps_options_received.dccpor_ndp;
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200706 const bool is_data_packet = dccp_data_packet(skb);
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300707
Gerrit Renker996ccf42009-10-05 00:53:13 +0000708 if (unlikely(hc->rx_state == TFRC_RSTATE_NO_DATA)) {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200709 if (is_data_packet) {
710 const u32 payload = skb->len - dccp_hdr(skb)->dccph_doff * 4;
711 do_feedback = CCID3_FBACK_INITIAL;
712 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
Gerrit Renker996ccf42009-10-05 00:53:13 +0000713 hc->rx_s = payload;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200714 /*
Gerrit Renker388d5e92009-10-05 00:53:11 +0000715 * Not necessary to update rx_bytes_recv here,
Gerrit Renker410e27a2008-09-09 13:27:22 +0200716 * since X_recv = 0 for the first feedback packet (cf.
717 * RFC 3448, 6.3) -- gerrit
718 */
719 }
720 goto update_records;
721 }
722
Gerrit Renker996ccf42009-10-05 00:53:13 +0000723 if (tfrc_rx_hist_duplicate(&hc->rx_hist, skb))
Gerrit Renker410e27a2008-09-09 13:27:22 +0200724 return; /* done receiving */
725
726 if (is_data_packet) {
727 const u32 payload = skb->len - dccp_hdr(skb)->dccph_doff * 4;
728 /*
729 * Update moving-average of s and the sum of received payload bytes
730 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000731 hc->rx_s = tfrc_ewma(hc->rx_s, payload, 9);
732 hc->rx_bytes_recv += payload;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200733 }
734
Gerrit Renkerd20ed952008-09-04 07:30:19 +0200735 /*
736 * Perform loss detection and handle pending losses
737 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000738 if (tfrc_rx_handle_loss(&hc->rx_hist, &hc->rx_li_hist,
Gerrit Renker410e27a2008-09-09 13:27:22 +0200739 skb, ndp, ccid3_first_li, sk)) {
740 do_feedback = CCID3_FBACK_PARAM_CHANGE;
741 goto done_receiving;
742 }
743
Gerrit Renker996ccf42009-10-05 00:53:13 +0000744 if (tfrc_rx_hist_loss_pending(&hc->rx_hist))
Gerrit Renker410e27a2008-09-09 13:27:22 +0200745 return; /* done receiving */
746
Gerrit Renker88e97a92008-09-04 07:30:19 +0200747 /*
Gerrit Renker410e27a2008-09-09 13:27:22 +0200748 * Handle data packets: RTT sampling and monitoring p
Gerrit Renker88e97a92008-09-04 07:30:19 +0200749 */
Gerrit Renker410e27a2008-09-09 13:27:22 +0200750 if (unlikely(!is_data_packet))
751 goto update_records;
752
Gerrit Renker996ccf42009-10-05 00:53:13 +0000753 if (!tfrc_lh_is_initialised(&hc->rx_li_hist)) {
754 const u32 sample = tfrc_rx_hist_sample_rtt(&hc->rx_hist, skb);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200755 /*
756 * Empty loss history: no loss so far, hence p stays 0.
757 * Sample RTT values, since an RTT estimate is required for the
758 * computation of p when the first loss occurs; RFC 3448, 6.3.1.
759 */
760 if (sample != 0)
Gerrit Renker996ccf42009-10-05 00:53:13 +0000761 hc->rx_rtt = tfrc_ewma(hc->rx_rtt, sample, 9);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200762
Gerrit Renker996ccf42009-10-05 00:53:13 +0000763 } else if (tfrc_lh_update_i_mean(&hc->rx_li_hist, skb)) {
Gerrit Renker410e27a2008-09-09 13:27:22 +0200764 /*
765 * Step (3) of [RFC 3448, 6.1]: Recompute I_mean and, if I_mean
766 * has decreased (resp. p has increased), send feedback now.
767 */
768 do_feedback = CCID3_FBACK_PARAM_CHANGE;
769 }
770
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200771 /*
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200772 * Check if the periodic once-per-RTT feedback is due; RFC 4342, 10.3
773 */
Gerrit Renker996ccf42009-10-05 00:53:13 +0000774 if (SUB16(dccp_hdr(skb)->dccph_ccval, hc->rx_last_counter) > 3)
Gerrit Renker410e27a2008-09-09 13:27:22 +0200775 do_feedback = CCID3_FBACK_PERIODIC;
776
777update_records:
Gerrit Renker996ccf42009-10-05 00:53:13 +0000778 tfrc_rx_hist_add_packet(&hc->rx_hist, skb, ndp);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200779
780done_receiving:
781 if (do_feedback)
782 ccid3_hc_rx_send_feedback(sk, skb, do_feedback);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700783}
784
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800785static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700786{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000787 struct ccid3_hc_rx_sock *hc = ccid_priv(ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700788
Gerrit Renker996ccf42009-10-05 00:53:13 +0000789 hc->rx_state = TFRC_RSTATE_NO_DATA;
790 tfrc_lh_init(&hc->rx_li_hist);
791 return tfrc_rx_hist_alloc(&hc->rx_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700792}
793
794static void ccid3_hc_rx_exit(struct sock *sk)
795{
Gerrit Renker996ccf42009-10-05 00:53:13 +0000796 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700797
Gerrit Renker996ccf42009-10-05 00:53:13 +0000798 tfrc_rx_hist_purge(&hc->rx_hist);
799 tfrc_lh_cleanup(&hc->rx_li_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700800}
801
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -0700802static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
803{
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000804 info->tcpi_ca_state = ccid3_hc_rx_sk(sk)->rx_state;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200805 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000806 info->tcpi_rcv_rtt = ccid3_hc_rx_sk(sk)->rx_rtt;
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -0700807}
808
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700809static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
810 u32 __user *optval, int __user *optlen)
811{
Gerrit Renker51c22bb2010-08-22 19:41:37 +0000812 const struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
Gerrit Renker8e138e72007-12-17 10:07:44 -0200813 struct tfrc_rx_info rx_info;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700814 const void *val;
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900815
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700816 switch (optname) {
817 case DCCP_SOCKOPT_CCID_RX_INFO:
Gerrit Renker8e138e72007-12-17 10:07:44 -0200818 if (len < sizeof(rx_info))
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700819 return -EINVAL;
Gerrit Renker996ccf42009-10-05 00:53:13 +0000820 rx_info.tfrcrx_x_recv = hc->rx_x_recv;
821 rx_info.tfrcrx_rtt = hc->rx_rtt;
Gerrit Renker792e6d32010-09-19 20:10:52 +0200822 rx_info.tfrcrx_p = tfrc_invert_loss_event_rate(hc->rx_pinv);
Gerrit Renker8e138e72007-12-17 10:07:44 -0200823 len = sizeof(rx_info);
824 val = &rx_info;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700825 break;
826 default:
827 return -ENOPROTOOPT;
828 }
829
830 if (put_user(len, optlen) || copy_to_user(optval, val, len))
831 return -EFAULT;
832
833 return 0;
834}
835
Gerrit Renkerddebc972009-01-04 21:42:53 -0800836struct ccid_operations ccid3_ops = {
Ian McDonald3dd9a7c2006-09-22 14:26:44 +1200837 .ccid_id = DCCPC_CCID3,
Gerrit Renker84a97b02007-12-13 23:33:25 -0200838 .ccid_name = "TCP-Friendly Rate Control",
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800839 .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840 .ccid_hc_tx_init = ccid3_hc_tx_init,
841 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
842 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
843 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
844 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700845 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800846 .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847 .ccid_hc_rx_init = ccid3_hc_rx_init,
848 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
849 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
850 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -0700851 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
852 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700853 .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
854 .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700855};
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200856
Gerrit Renker56724aa2006-11-20 18:28:09 -0200857#ifdef CONFIG_IP_DCCP_CCID3_DEBUG
Gerrit Renker43264992008-08-23 13:28:27 +0200858module_param(ccid3_debug, bool, 0644);
Gerrit Renkerddebc972009-01-04 21:42:53 -0800859MODULE_PARM_DESC(ccid3_debug, "Enable CCID-3 debug messages");
Gerrit Renker56724aa2006-11-20 18:28:09 -0200860#endif