1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
4 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
7 * An implementation of the DCCP protocol
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/
12 * This code also uses code from Lulea University, rereleased as GPL by its
14 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
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>.
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
25 #include <asm/unaligned.h>
27 #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
28 static bool ccid3_debug
;
29 #define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
31 #define ccid3_pr_debug(format, a...)
35 * Transmitter Half-Connection Routines
37 #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
38 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state
)
40 static const char *const ccid3_state_names
[] = {
41 [TFRC_SSTATE_NO_SENT
] = "NO_SENT",
42 [TFRC_SSTATE_NO_FBACK
] = "NO_FBACK",
43 [TFRC_SSTATE_FBACK
] = "FBACK",
46 return ccid3_state_names
[state
];
50 static void ccid3_hc_tx_set_state(struct sock
*sk
,
51 enum ccid3_hc_tx_states state
)
53 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
54 enum ccid3_hc_tx_states oldstate
= hc
->tx_state
;
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
);
64 * Compute the initial sending rate X_init in the manner of RFC 3390:
66 * X_init = min(4 * s, max(2 * s, 4380 bytes)) / RTT
68 * 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.
70 * For consistency with other parts of the code, X_init is scaled by 2^6.
72 static inline u64
rfc3390_initial_rate(struct sock
*sk
)
74 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
);
77 return scaled_div(w_init
<< 6, hc
->tx_rtt
);
81 * ccid3_update_send_interval - Calculate new t_ipi = s / X_inst
82 * This respects the granularity of X_inst (64 * bytes/second).
84 static void ccid3_update_send_interval(struct ccid3_hc_tx_sock
*hc
)
86 hc
->tx_t_ipi
= scaled_div32(((u64
)hc
->tx_s
) << 6, hc
->tx_x
);
88 DCCP_BUG_ON(hc
->tx_t_ipi
== 0);
89 ccid3_pr_debug("t_ipi=%u, s=%u, X=%u\n", hc
->tx_t_ipi
,
90 hc
->tx_s
, (unsigned int)(hc
->tx_x
>> 6));
93 static u32
ccid3_hc_tx_idle_rtt(struct ccid3_hc_tx_sock
*hc
, ktime_t now
)
95 u32 delta
= ktime_us_delta(now
, hc
->tx_t_last_win_count
);
97 return delta
/ hc
->tx_rtt
;
101 * ccid3_hc_tx_update_x - Update allowed sending rate X
102 * @stamp: most recent time if available - can be left NULL.
104 * This function tracks draft rfc3448bis, check there for latest details.
106 * 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).
111 static void ccid3_hc_tx_update_x(struct sock
*sk
, ktime_t
*stamp
)
113 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
;
116 ktime_t now
= stamp
? *stamp
: ktime_get_real();
119 * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
120 * 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.
122 * For consistency with X and X_recv, min_rate is also scaled by 2^6.
124 if (ccid3_hc_tx_idle_rtt(hc
, now
) >= 2) {
125 min_rate
= rfc3390_initial_rate(sk
);
126 min_rate
= max(min_rate
, 2 * hc
->tx_x_recv
);
131 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
);
134 } else if (ktime_us_delta(now
, hc
->tx_t_ld
) - (s64
)hc
->tx_rtt
>= 0) {
136 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
));
142 if (hc
->tx_x
!= old_x
) {
143 ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "
144 "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));
148 ccid3_update_send_interval(hc
);
153 * ccid3_hc_tx_update_s - Track the mean packet size `s'
154 * @len: DCCP packet payload size in bytes
156 * cf. RFC 4342, 5.3 and RFC 3448, 4.1
158 static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock
*hc
, int len
)
160 const u16 old_s
= hc
->tx_s
;
162 hc
->tx_s
= tfrc_ewma(hc
->tx_s
, len
, 9);
164 if (hc
->tx_s
!= old_s
)
165 ccid3_update_send_interval(hc
);
169 * Update Window Counter using the algorithm from [RFC 4342, 8.1].
170 * As elsewhere, RTT > 0 is assumed by using dccp_sample_rtt().
172 static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock
*hc
,
175 u32 delta
= ktime_us_delta(now
, hc
->tx_t_last_win_count
),
176 quarter_rtts
= (4 * delta
) / hc
->tx_rtt
;
178 if (quarter_rtts
> 0) {
179 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 */
185 static void ccid3_hc_tx_no_feedback_timer(struct timer_list
*t
)
187 struct ccid3_hc_tx_sock
*hc
= from_timer(hc
, t
, tx_no_feedback_timer
);
188 struct sock
*sk
= hc
->sk
;
189 unsigned long t_nfb
= USEC_PER_SEC
/ 5;
192 if (sock_owned_by_user(sk
)) {
193 /* Try again later. */
194 /* XXX: set some sensible MIB */
198 ccid3_pr_debug("%s(%p, state=%s) - entry\n", dccp_role(sk
), sk
,
199 ccid3_tx_state_name(hc
->tx_state
));
201 /* Ignore and do not restart after leaving the established state */
202 if ((1 << sk
->sk_state
) & ~(DCCPF_OPEN
| DCCPF_PARTOPEN
))
205 /* Reset feedback state to "no feedback received" */
206 if (hc
->tx_state
== TFRC_SSTATE_FBACK
)
207 ccid3_hc_tx_set_state(sk
, TFRC_SSTATE_NO_FBACK
);
210 * Determine new allowed sending rate X as per draft rfc3448bis-00, 4.4
211 * RTO is 0 if and only if no feedback has been received yet.
213 if (hc
->tx_t_rto
== 0 || hc
->tx_p
== 0) {
215 /* halve send rate directly */
216 hc
->tx_x
= max(hc
->tx_x
/ 2,
217 (((__u64
)hc
->tx_s
) << 6) / TFRC_T_MBI
);
218 ccid3_update_send_interval(hc
);
221 * Modify the cached value of X_recv
223 * If (X_calc > 2 * X_recv)
224 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
226 * X_recv = X_calc / 4;
228 * Note that X_recv is scaled by 2^6 while X_calc is not
230 if (hc
->tx_x_calc
> (hc
->tx_x_recv
>> 5))
232 max(hc
->tx_x_recv
/ 2,
233 (((__u64
)hc
->tx_s
) << 6) / (2*TFRC_T_MBI
));
235 hc
->tx_x_recv
= hc
->tx_x_calc
;
238 ccid3_hc_tx_update_x(sk
, NULL
);
240 ccid3_pr_debug("Reduced X to %llu/64 bytes/sec\n",
241 (unsigned long long)hc
->tx_x
);
244 * Set new timeout for the nofeedback timer.
245 * See comments in packet_recv() regarding the value of t_RTO.
247 if (unlikely(hc
->tx_t_rto
== 0)) /* no feedback received yet */
248 t_nfb
= TFRC_INITIAL_TIMEOUT
;
250 t_nfb
= max(hc
->tx_t_rto
, 2 * hc
->tx_t_ipi
);
253 sk_reset_timer(sk
, &hc
->tx_no_feedback_timer
,
254 jiffies
+ usecs_to_jiffies(t_nfb
));
261 * ccid3_hc_tx_send_packet - Delay-based dequeueing of TX packets
262 * @skb: next packet candidate to send on @sk
264 * This function uses the convention of ccid_packet_dequeue_eval() and
265 * returns a millisecond-delay value between 0 and t_mbi = 64000 msec.
267 static int ccid3_hc_tx_send_packet(struct sock
*sk
, struct sk_buff
*skb
)
269 struct dccp_sock
*dp
= dccp_sk(sk
);
270 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
271 ktime_t now
= ktime_get_real();
275 * 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.
279 if (unlikely(skb
->len
== 0))
282 if (hc
->tx_state
== TFRC_SSTATE_NO_SENT
) {
283 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
;
288 /* Set t_0 for initial packet */
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.
298 if (dp
->dccps_syn_rtt
) {
299 ccid3_pr_debug("SYN RTT = %uus\n", dp
->dccps_syn_rtt
);
300 hc
->tx_rtt
= dp
->dccps_syn_rtt
;
301 hc
->tx_x
= rfc3390_initial_rate(sk
);
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.
310 hc
->tx_rtt
= DCCP_FALLBACK_RTT
;
314 ccid3_update_send_interval(hc
);
316 ccid3_hc_tx_set_state(sk
, TFRC_SSTATE_NO_FBACK
);
319 delay
= ktime_us_delta(hc
->tx_t_nom
, now
);
320 ccid3_pr_debug("delay=%ld\n", (long)delay
);
322 * Scheduling of packet transmissions (RFC 5348, 8.3)
324 * if (t_now > t_nom - delta)
325 * // send the packet now
327 * // send the packet in (t_nom - t_now) milliseconds.
329 if (delay
>= TFRC_T_DELTA
)
330 return (u32
)delay
/ USEC_PER_MSEC
;
332 ccid3_hc_tx_update_win_count(hc
, now
);
335 /* prepare to send now (add options etc.) */
336 dp
->dccps_hc_tx_insert_options
= 1;
337 DCCP_SKB_CB(skb
)->dccpd_ccval
= hc
->tx_last_win_count
;
339 /* set the nominal send time for the next following packet */
340 hc
->tx_t_nom
= ktime_add_us(hc
->tx_t_nom
, hc
->tx_t_ipi
);
341 return CCID_PACKET_SEND_AT_ONCE
;
344 static void ccid3_hc_tx_packet_sent(struct sock
*sk
, unsigned int len
)
346 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
348 ccid3_hc_tx_update_s(hc
, len
);
350 if (tfrc_tx_hist_add(&hc
->tx_hist
, dccp_sk(sk
)->dccps_gss
))
351 DCCP_CRIT("packet history - out of memory!");
354 static void ccid3_hc_tx_packet_recv(struct sock
*sk
, struct sk_buff
*skb
)
356 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
357 struct tfrc_tx_hist_entry
*acked
;
362 /* 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
))
367 * Locate the acknowledged packet in the TX history.
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).
374 acked
= tfrc_tx_hist_find_entry(hc
->tx_hist
, dccp_hdr_ack_seq(skb
));
377 /* For the sake of RTT sampling, ignore/remove all older entries */
378 tfrc_tx_hist_purge(&acked
->next
);
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);
386 * Update allowed sending rate X as per draft rfc3448bis-00, 4.2/3
388 if (hc
->tx_state
== TFRC_SSTATE_NO_FBACK
) {
389 ccid3_hc_tx_set_state(sk
, TFRC_SSTATE_FBACK
);
391 if (hc
->tx_t_rto
== 0) {
393 * Initial feedback packet: Larger Initial Windows (4.2)
395 hc
->tx_x
= rfc3390_initial_rate(sk
);
398 ccid3_update_send_interval(hc
);
400 goto done_computing_x
;
401 } else if (hc
->tx_p
== 0) {
403 * First feedback after nofeedback timer expiry (4.3)
405 goto done_computing_x
;
409 /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
411 hc
->tx_x_calc
= tfrc_calc_x(hc
->tx_s
, hc
->tx_rtt
, hc
->tx_p
);
412 ccid3_hc_tx_update_x(sk
, &now
);
415 ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
416 "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
417 dccp_role(sk
), sk
, hc
->tx_rtt
, r_sample
,
418 hc
->tx_s
, hc
->tx_p
, hc
->tx_x_calc
,
419 (unsigned int)(hc
->tx_x_recv
>> 6),
420 (unsigned int)(hc
->tx_x
>> 6));
422 /* unschedule no feedback timer */
423 sk_stop_timer(sk
, &hc
->tx_no_feedback_timer
);
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
429 sk
->sk_write_space(sk
);
432 * 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.
436 hc
->tx_t_rto
= max_t(u32
, 4 * hc
->tx_rtt
,
437 USEC_PER_SEC
/HZ
* tcp_rto_min(sk
));
439 * Schedule no feedback timer to expire in
440 * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
442 t_nfb
= max(hc
->tx_t_rto
, 2 * hc
->tx_t_ipi
);
444 ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
445 "expire in %lu jiffies (%luus)\n",
446 dccp_role(sk
), sk
, usecs_to_jiffies(t_nfb
), t_nfb
);
448 sk_reset_timer(sk
, &hc
->tx_no_feedback_timer
,
449 jiffies
+ usecs_to_jiffies(t_nfb
));
452 static int ccid3_hc_tx_parse_options(struct sock
*sk
, u8 packet_type
,
453 u8 option
, u8
*optval
, u8 optlen
)
455 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
459 case TFRC_OPT_RECEIVE_RATE
:
460 case TFRC_OPT_LOSS_EVENT_RATE
:
461 /* Must be ignored on Data packets, cf. RFC 4342 8.3 and 8.5 */
462 if (packet_type
== DCCP_PKT_DATA
)
464 if (unlikely(optlen
!= 4)) {
465 DCCP_WARN("%s(%p), invalid len %d for %u\n",
466 dccp_role(sk
), sk
, optlen
, option
);
469 opt_val
= ntohl(get_unaligned((__be32
*)optval
));
471 if (option
== TFRC_OPT_RECEIVE_RATE
) {
472 /* Receive Rate is kept in units of 64 bytes/second */
473 hc
->tx_x_recv
= opt_val
;
476 ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
477 dccp_role(sk
), sk
, opt_val
);
479 /* Update the fixpoint Loss Event Rate fraction */
480 hc
->tx_p
= tfrc_invert_loss_event_rate(opt_val
);
482 ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
483 dccp_role(sk
), sk
, opt_val
);
489 static int ccid3_hc_tx_init(struct ccid
*ccid
, struct sock
*sk
)
491 struct ccid3_hc_tx_sock
*hc
= ccid_priv(ccid
);
493 hc
->tx_state
= TFRC_SSTATE_NO_SENT
;
496 timer_setup(&hc
->tx_no_feedback_timer
,
497 ccid3_hc_tx_no_feedback_timer
, 0);
501 static void ccid3_hc_tx_exit(struct sock
*sk
)
503 struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
505 sk_stop_timer(sk
, &hc
->tx_no_feedback_timer
);
506 tfrc_tx_hist_purge(&hc
->tx_hist
);
509 static void ccid3_hc_tx_get_info(struct sock
*sk
, struct tcp_info
*info
)
511 info
->tcpi_rto
= ccid3_hc_tx_sk(sk
)->tx_t_rto
;
512 info
->tcpi_rtt
= ccid3_hc_tx_sk(sk
)->tx_rtt
;
515 static int ccid3_hc_tx_getsockopt(struct sock
*sk
, const int optname
, int len
,
516 u32 __user
*optval
, int __user
*optlen
)
518 const struct ccid3_hc_tx_sock
*hc
= ccid3_hc_tx_sk(sk
);
519 struct tfrc_tx_info tfrc
;
523 case DCCP_SOCKOPT_CCID_TX_INFO
:
524 if (len
< sizeof(tfrc
))
526 memset(&tfrc
, 0, sizeof(tfrc
));
527 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
;
541 if (put_user(len
, optlen
) || copy_to_user(optval
, val
, len
))
548 * Receiver Half-Connection Routines
551 /* CCID3 feedback types */
552 enum ccid3_fback_type
{
553 CCID3_FBACK_NONE
= 0,
555 CCID3_FBACK_PERIODIC
,
556 CCID3_FBACK_PARAM_CHANGE
559 #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
560 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state
)
562 static const char *const ccid3_rx_state_names
[] = {
563 [TFRC_RSTATE_NO_DATA
] = "NO_DATA",
564 [TFRC_RSTATE_DATA
] = "DATA",
567 return ccid3_rx_state_names
[state
];
571 static void ccid3_hc_rx_set_state(struct sock
*sk
,
572 enum ccid3_hc_rx_states state
)
574 struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
575 enum ccid3_hc_rx_states oldstate
= hc
->rx_state
;
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
);
581 hc
->rx_state
= state
;
584 static void ccid3_hc_rx_send_feedback(struct sock
*sk
,
585 const struct sk_buff
*skb
,
586 enum ccid3_fback_type fbtype
)
588 struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
589 struct dccp_sock
*dp
= dccp_sk(sk
);
590 ktime_t now
= ktime_get();
594 case CCID3_FBACK_INITIAL
:
596 hc
->rx_pinv
= ~0U; /* see RFC 4342, 8.5 */
598 case CCID3_FBACK_PARAM_CHANGE
:
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
602 * 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.
609 if (hc
->rx_x_recv
> 0)
612 case CCID3_FBACK_PERIODIC
:
613 delta
= ktime_us_delta(now
, hc
->rx_tstamp_last_feedback
);
616 hc
->rx_x_recv
= scaled_div32(hc
->rx_bytes_recv
, delta
);
622 ccid3_pr_debug("Interval %lldusec, X_recv=%u, 1/p=%u\n", delta
,
623 hc
->rx_x_recv
, hc
->rx_pinv
);
625 hc
->rx_tstamp_last_feedback
= now
;
626 hc
->rx_last_counter
= dccp_hdr(skb
)->dccph_ccval
;
627 hc
->rx_bytes_recv
= 0;
629 dp
->dccps_hc_rx_insert_options
= 1;
633 static int ccid3_hc_rx_insert_options(struct sock
*sk
, struct sk_buff
*skb
)
635 const struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
638 if (!(sk
->sk_state
== DCCP_OPEN
|| sk
->sk_state
== DCCP_PARTOPEN
))
641 if (dccp_packet_without_ack(skb
))
644 x_recv
= htonl(hc
->rx_x_recv
);
645 pinv
= htonl(hc
->rx_pinv
);
647 if (dccp_insert_option(skb
, TFRC_OPT_LOSS_EVENT_RATE
,
648 &pinv
, sizeof(pinv
)) ||
649 dccp_insert_option(skb
, TFRC_OPT_RECEIVE_RATE
,
650 &x_recv
, sizeof(x_recv
)))
657 * ccid3_first_li - Implements [RFC 5348, 6.3.1]
659 * Determine the length of the first loss interval via inverse lookup.
660 * Assume that X_recv can be computed by the throughput equation
664 * Find some p such that f(p) = fval; return 1/p (scaled).
666 static u32
ccid3_first_li(struct sock
*sk
)
668 struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
673 if (hc
->rx_rtt
== 0) {
674 DCCP_WARN("No RTT estimate available, using fallback RTT\n");
675 hc
->rx_rtt
= DCCP_FALLBACK_RTT
;
678 delta
= ktime_us_delta(ktime_get(), hc
->rx_tstamp_last_feedback
);
681 x_recv
= scaled_div32(hc
->rx_bytes_recv
, delta
);
682 if (x_recv
== 0) { /* would also trigger divide-by-zero */
683 DCCP_WARN("X_recv==0\n");
684 if (hc
->rx_x_recv
== 0) {
685 DCCP_BUG("stored value of X_recv is zero");
688 x_recv
= hc
->rx_x_recv
;
691 fval
= scaled_div(hc
->rx_s
, hc
->rx_rtt
);
692 fval
= scaled_div32(fval
, x_recv
);
693 p
= tfrc_calc_x_reverse_lookup(fval
);
695 ccid3_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
696 "loss rate=%u\n", dccp_role(sk
), sk
, x_recv
, p
);
698 return p
== 0 ? ~0U : scaled_div(1, p
);
701 static void ccid3_hc_rx_packet_recv(struct sock
*sk
, struct sk_buff
*skb
)
703 struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
704 enum ccid3_fback_type do_feedback
= CCID3_FBACK_NONE
;
705 const u64 ndp
= dccp_sk(sk
)->dccps_options_received
.dccpor_ndp
;
706 const bool is_data_packet
= dccp_data_packet(skb
);
708 if (unlikely(hc
->rx_state
== TFRC_RSTATE_NO_DATA
)) {
709 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
);
715 * Not necessary to update rx_bytes_recv here,
716 * since X_recv = 0 for the first feedback packet (cf.
717 * RFC 3448, 6.3) -- gerrit
723 if (tfrc_rx_hist_duplicate(&hc
->rx_hist
, skb
))
724 return; /* done receiving */
726 if (is_data_packet
) {
727 const u32 payload
= skb
->len
- dccp_hdr(skb
)->dccph_doff
* 4;
729 * Update moving-average of s and the sum of received payload bytes
731 hc
->rx_s
= tfrc_ewma(hc
->rx_s
, payload
, 9);
732 hc
->rx_bytes_recv
+= payload
;
736 * Perform loss detection and handle pending losses
738 if (tfrc_rx_handle_loss(&hc
->rx_hist
, &hc
->rx_li_hist
,
739 skb
, ndp
, ccid3_first_li
, sk
)) {
740 do_feedback
= CCID3_FBACK_PARAM_CHANGE
;
744 if (tfrc_rx_hist_loss_pending(&hc
->rx_hist
))
745 return; /* done receiving */
748 * Handle data packets: RTT sampling and monitoring p
750 if (unlikely(!is_data_packet
))
753 if (!tfrc_lh_is_initialised(&hc
->rx_li_hist
)) {
754 const u32 sample
= tfrc_rx_hist_sample_rtt(&hc
->rx_hist
, skb
);
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.
761 hc
->rx_rtt
= tfrc_ewma(hc
->rx_rtt
, sample
, 9);
763 } else if (tfrc_lh_update_i_mean(&hc
->rx_li_hist
, skb
)) {
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.
768 do_feedback
= CCID3_FBACK_PARAM_CHANGE
;
772 * Check if the periodic once-per-RTT feedback is due; RFC 4342, 10.3
774 if (SUB16(dccp_hdr(skb
)->dccph_ccval
, hc
->rx_last_counter
) > 3)
775 do_feedback
= CCID3_FBACK_PERIODIC
;
778 tfrc_rx_hist_add_packet(&hc
->rx_hist
, skb
, ndp
);
782 ccid3_hc_rx_send_feedback(sk
, skb
, do_feedback
);
785 static int ccid3_hc_rx_init(struct ccid
*ccid
, struct sock
*sk
)
787 struct ccid3_hc_rx_sock
*hc
= ccid_priv(ccid
);
789 hc
->rx_state
= TFRC_RSTATE_NO_DATA
;
790 tfrc_lh_init(&hc
->rx_li_hist
);
791 return tfrc_rx_hist_alloc(&hc
->rx_hist
);
794 static void ccid3_hc_rx_exit(struct sock
*sk
)
796 struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
798 tfrc_rx_hist_purge(&hc
->rx_hist
);
799 tfrc_lh_cleanup(&hc
->rx_li_hist
);
802 static void ccid3_hc_rx_get_info(struct sock
*sk
, struct tcp_info
*info
)
804 info
->tcpi_ca_state
= ccid3_hc_rx_sk(sk
)->rx_state
;
805 info
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
806 info
->tcpi_rcv_rtt
= ccid3_hc_rx_sk(sk
)->rx_rtt
;
809 static int ccid3_hc_rx_getsockopt(struct sock
*sk
, const int optname
, int len
,
810 u32 __user
*optval
, int __user
*optlen
)
812 const struct ccid3_hc_rx_sock
*hc
= ccid3_hc_rx_sk(sk
);
813 struct tfrc_rx_info rx_info
;
817 case DCCP_SOCKOPT_CCID_RX_INFO
:
818 if (len
< sizeof(rx_info
))
820 rx_info
.tfrcrx_x_recv
= hc
->rx_x_recv
;
821 rx_info
.tfrcrx_rtt
= hc
->rx_rtt
;
822 rx_info
.tfrcrx_p
= tfrc_invert_loss_event_rate(hc
->rx_pinv
);
823 len
= sizeof(rx_info
);
830 if (put_user(len
, optlen
) || copy_to_user(optval
, val
, len
))
836 struct ccid_operations ccid3_ops
= {
837 .ccid_id
= DCCPC_CCID3
,
838 .ccid_name
= "TCP-Friendly Rate Control",
839 .ccid_hc_tx_obj_size
= sizeof(struct ccid3_hc_tx_sock
),
840 .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
,
845 .ccid_hc_tx_parse_options
= ccid3_hc_tx_parse_options
,
846 .ccid_hc_rx_obj_size
= sizeof(struct ccid3_hc_rx_sock
),
847 .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
,
851 .ccid_hc_rx_get_info
= ccid3_hc_rx_get_info
,
852 .ccid_hc_tx_get_info
= ccid3_hc_tx_get_info
,
853 .ccid_hc_rx_getsockopt
= ccid3_hc_rx_getsockopt
,
854 .ccid_hc_tx_getsockopt
= ccid3_hc_tx_getsockopt
,
857 #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
858 module_param(ccid3_debug
, bool, 0644);
859 MODULE_PARM_DESC(ccid3_debug
, "Enable CCID-3 debug messages");