1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 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> 6 * 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 https://www.wand.net.nz/ 11 * 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> 21 */ 22 #include "../dccp.h" 23 #include "ccid3.h" 24 25 #include <asm/unaligned.h> 26 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) 30 #else 31 #define ccid3_pr_debug(format, a...) 32 #endif 33 34 /* 35 * Transmitter Half-Connection Routines 36 */ 37 #ifdef CONFIG_IP_DCCP_CCID3_DEBUG 38 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state) 39 { 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", 44 }; 45 46 return ccid3_state_names[state]; 47 } 48 #endif 49 50 static void ccid3_hc_tx_set_state(struct sock *sk, 51 enum ccid3_hc_tx_states state) 52 { 53 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 54 enum ccid3_hc_tx_states oldstate = hc->tx_state; 55 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); 60 hc->tx_state = state; 61 } 62 63 /* 64 * Compute the initial sending rate X_init in the manner of RFC 3390: 65 * 66 * X_init = min(4 * s, max(2 * s, 4380 bytes)) / RTT 67 * 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. 71 */ 72 static inline u64 rfc3390_initial_rate(struct sock *sk) 73 { 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); 76 77 return scaled_div(w_init << 6, hc->tx_rtt); 78 } 79 80 /** 81 * ccid3_update_send_interval - Calculate new t_ipi = s / X_inst 82 * This respects the granularity of X_inst (64 * bytes/second). 83 */ 84 static void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hc) 85 { 86 hc->tx_t_ipi = scaled_div32(((u64)hc->tx_s) << 6, hc->tx_x); 87 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)); 91 } 92 93 static u32 ccid3_hc_tx_idle_rtt(struct ccid3_hc_tx_sock *hc, ktime_t now) 94 { 95 u32 delta = ktime_us_delta(now, hc->tx_t_last_win_count); 96 97 return delta / hc->tx_rtt; 98 } 99 100 /** 101 * ccid3_hc_tx_update_x - Update allowed sending rate X 102 * @stamp: most recent time if available - can be left NULL. 103 * 104 * This function tracks draft rfc3448bis, check there for latest details. 105 * 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). 109 * 110 */ 111 static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp) 112 { 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(); 117 118 /* 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. 123 */ 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); 127 } 128 129 if (hc->tx_p > 0) { 130 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); 133 134 } else if (ktime_us_delta(now, hc->tx_t_ld) - (s64)hc->tx_rtt >= 0) { 135 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)); 139 hc->tx_t_ld = now; 140 } 141 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)); 147 148 ccid3_update_send_interval(hc); 149 } 150 } 151 152 /** 153 * ccid3_hc_tx_update_s - Track the mean packet size `s' 154 * @len: DCCP packet payload size in bytes 155 * 156 * cf. RFC 4342, 5.3 and RFC 3448, 4.1 157 */ 158 static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hc, int len) 159 { 160 const u16 old_s = hc->tx_s; 161 162 hc->tx_s = tfrc_ewma(hc->tx_s, len, 9); 163 164 if (hc->tx_s != old_s) 165 ccid3_update_send_interval(hc); 166 } 167 168 /* 169 * Update Window Counter using the algorithm from [RFC 4342, 8.1]. 170 * As elsewhere, RTT > 0 is assumed by using dccp_sample_rtt(). 171 */ 172 static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hc, 173 ktime_t now) 174 { 175 u32 delta = ktime_us_delta(now, hc->tx_t_last_win_count), 176 quarter_rtts = (4 * delta) / hc->tx_rtt; 177 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 */ 182 } 183 } 184 185 static void ccid3_hc_tx_no_feedback_timer(struct timer_list *t) 186 { 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; 190 191 bh_lock_sock(sk); 192 if (sock_owned_by_user(sk)) { 193 /* Try again later. */ 194 /* XXX: set some sensible MIB */ 195 goto restart_timer; 196 } 197 198 ccid3_pr_debug("%s(%p, state=%s) - entry\n", dccp_role(sk), sk, 199 ccid3_tx_state_name(hc->tx_state)); 200 201 /* 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" */ 206 if (hc->tx_state == TFRC_SSTATE_FBACK) 207 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK); 208 209 /* 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. 212 */ 213 if (hc->tx_t_rto == 0 || hc->tx_p == 0) { 214 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); 219 } else { 220 /* 221 * Modify the cached value of X_recv 222 * 223 * If (X_calc > 2 * X_recv) 224 * 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 229 */ 230 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)); 234 else { 235 hc->tx_x_recv = hc->tx_x_calc; 236 hc->tx_x_recv <<= 4; 237 } 238 ccid3_hc_tx_update_x(sk, NULL); 239 } 240 ccid3_pr_debug("Reduced X to %llu/64 bytes/sec\n", 241 (unsigned long long)hc->tx_x); 242 243 /* 244 * Set new timeout for the nofeedback timer. 245 * See comments in packet_recv() regarding the value of t_RTO. 246 */ 247 if (unlikely(hc->tx_t_rto == 0)) /* no feedback received yet */ 248 t_nfb = TFRC_INITIAL_TIMEOUT; 249 else 250 t_nfb = max(hc->tx_t_rto, 2 * hc->tx_t_ipi); 251 252 restart_timer: 253 sk_reset_timer(sk, &hc->tx_no_feedback_timer, 254 jiffies + usecs_to_jiffies(t_nfb)); 255 out: 256 bh_unlock_sock(sk); 257 sock_put(sk); 258 } 259 260 /** 261 * ccid3_hc_tx_send_packet - Delay-based dequeueing of TX packets 262 * @skb: next packet candidate to send on @sk 263 * 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. 266 */ 267 static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb) 268 { 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(); 272 s64 delay; 273 274 /* 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. 278 */ 279 if (unlikely(skb->len == 0)) 280 return -EBADMSG; 281 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; 287 288 /* Set t_0 for initial packet */ 289 hc->tx_t_nom = now; 290 291 hc->tx_s = skb->len; 292 293 /* 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); 300 hc->tx_rtt = dp->dccps_syn_rtt; 301 hc->tx_x = rfc3390_initial_rate(sk); 302 hc->tx_t_ld = now; 303 } else { 304 /* 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 */ 310 hc->tx_rtt = DCCP_FALLBACK_RTT; 311 hc->tx_x = hc->tx_s; 312 hc->tx_x <<= 6; 313 } 314 ccid3_update_send_interval(hc); 315 316 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK); 317 318 } else { 319 delay = ktime_us_delta(hc->tx_t_nom, now); 320 ccid3_pr_debug("delay=%ld\n", (long)delay); 321 /* 322 * Scheduling of packet transmissions (RFC 5348, 8.3) 323 * 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 */ 329 if (delay >= TFRC_T_DELTA) 330 return (u32)delay / USEC_PER_MSEC; 331 332 ccid3_hc_tx_update_win_count(hc, now); 333 } 334 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; 338 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; 342 } 343 344 static void ccid3_hc_tx_packet_sent(struct sock *sk, unsigned int len) 345 { 346 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 347 348 ccid3_hc_tx_update_s(hc, len); 349 350 if (tfrc_tx_hist_add(&hc->tx_hist, dccp_sk(sk)->dccps_gss)) 351 DCCP_CRIT("packet history - out of memory!"); 352 } 353 354 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb) 355 { 356 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 357 struct tfrc_tx_hist_entry *acked; 358 ktime_t now; 359 unsigned long t_nfb; 360 u32 r_sample; 361 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)) 365 return; 366 /* 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) 376 return; 377 /* 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); 384 385 /* 386 * Update allowed sending rate X as per draft rfc3448bis-00, 4.2/3 387 */ 388 if (hc->tx_state == TFRC_SSTATE_NO_FBACK) { 389 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK); 390 391 if (hc->tx_t_rto == 0) { 392 /* 393 * Initial feedback packet: Larger Initial Windows (4.2) 394 */ 395 hc->tx_x = rfc3390_initial_rate(sk); 396 hc->tx_t_ld = now; 397 398 ccid3_update_send_interval(hc); 399 400 goto done_computing_x; 401 } else if (hc->tx_p == 0) { 402 /* 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]) */ 410 if (hc->tx_p > 0) 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); 413 414 done_computing_x: 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)); 421 422 /* unschedule no feedback timer */ 423 sk_stop_timer(sk, &hc->tx_no_feedback_timer); 424 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 /* 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. 435 */ 436 hc->tx_t_rto = max_t(u32, 4 * hc->tx_rtt, 437 USEC_PER_SEC/HZ * tcp_rto_min(sk)); 438 /* 439 * Schedule no feedback timer to expire in 440 * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi) 441 */ 442 t_nfb = max(hc->tx_t_rto, 2 * hc->tx_t_ipi); 443 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); 447 448 sk_reset_timer(sk, &hc->tx_no_feedback_timer, 449 jiffies + usecs_to_jiffies(t_nfb)); 450 } 451 452 static int ccid3_hc_tx_parse_options(struct sock *sk, u8 packet_type, 453 u8 option, u8 *optval, u8 optlen) 454 { 455 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 456 __be32 opt_val; 457 458 switch (option) { 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) 463 break; 464 if (unlikely(optlen != 4)) { 465 DCCP_WARN("%s(%p), invalid len %d for %u\n", 466 dccp_role(sk), sk, optlen, option); 467 return -EINVAL; 468 } 469 opt_val = ntohl(get_unaligned((__be32 *)optval)); 470 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; 474 hc->tx_x_recv <<= 6; 475 476 ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n", 477 dccp_role(sk), sk, opt_val); 478 } else { 479 /* Update the fixpoint Loss Event Rate fraction */ 480 hc->tx_p = tfrc_invert_loss_event_rate(opt_val); 481 482 ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n", 483 dccp_role(sk), sk, opt_val); 484 } 485 } 486 return 0; 487 } 488 489 static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk) 490 { 491 struct ccid3_hc_tx_sock *hc = ccid_priv(ccid); 492 493 hc->tx_state = TFRC_SSTATE_NO_SENT; 494 hc->tx_hist = NULL; 495 hc->sk = sk; 496 timer_setup(&hc->tx_no_feedback_timer, 497 ccid3_hc_tx_no_feedback_timer, 0); 498 return 0; 499 } 500 501 static void ccid3_hc_tx_exit(struct sock *sk) 502 { 503 struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 504 505 sk_stop_timer(sk, &hc->tx_no_feedback_timer); 506 tfrc_tx_hist_purge(&hc->tx_hist); 507 } 508 509 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info) 510 { 511 info->tcpi_rto = ccid3_hc_tx_sk(sk)->tx_t_rto; 512 info->tcpi_rtt = ccid3_hc_tx_sk(sk)->tx_rtt; 513 } 514 515 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len, 516 u32 __user *optval, int __user *optlen) 517 { 518 const struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk); 519 struct tfrc_tx_info tfrc; 520 const void *val; 521 522 switch (optname) { 523 case DCCP_SOCKOPT_CCID_TX_INFO: 524 if (len < sizeof(tfrc)) 525 return -EINVAL; 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; 534 len = sizeof(tfrc); 535 val = &tfrc; 536 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 */ 550 551 /* CCID3 feedback types */ 552 enum 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 560 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state) 561 { 562 static const char *const ccid3_rx_state_names[] = { 563 [TFRC_RSTATE_NO_DATA] = "NO_DATA", 564 [TFRC_RSTATE_DATA] = "DATA", 565 }; 566 567 return ccid3_rx_state_names[state]; 568 } 569 #endif 570 571 static void ccid3_hc_rx_set_state(struct sock *sk, 572 enum ccid3_hc_rx_states state) 573 { 574 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk); 575 enum ccid3_hc_rx_states oldstate = hc->rx_state; 576 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; 582 } 583 584 static void ccid3_hc_rx_send_feedback(struct sock *sk, 585 const struct sk_buff *skb, 586 enum ccid3_fback_type fbtype) 587 { 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(); 591 s64 delta = 0; 592 593 switch (fbtype) { 594 case CCID3_FBACK_INITIAL: 595 hc->rx_x_recv = 0; 596 hc->rx_pinv = ~0U; /* see RFC 4342, 8.5 */ 597 break; 598 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 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. 608 */ 609 if (hc->rx_x_recv > 0) 610 break; 611 /* fall through */ 612 case CCID3_FBACK_PERIODIC: 613 delta = ktime_us_delta(now, hc->rx_tstamp_last_feedback); 614 if (delta <= 0) 615 delta = 1; 616 hc->rx_x_recv = scaled_div32(hc->rx_bytes_recv, delta); 617 break; 618 default: 619 return; 620 } 621 622 ccid3_pr_debug("Interval %lldusec, X_recv=%u, 1/p=%u\n", delta, 623 hc->rx_x_recv, hc->rx_pinv); 624 625 hc->rx_tstamp_last_feedback = now; 626 hc->rx_last_counter = dccp_hdr(skb)->dccph_ccval; 627 hc->rx_bytes_recv = 0; 628 629 dp->dccps_hc_rx_insert_options = 1; 630 dccp_send_ack(sk); 631 } 632 633 static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb) 634 { 635 const struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk); 636 __be32 x_recv, pinv; 637 638 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN)) 639 return 0; 640 641 if (dccp_packet_without_ack(skb)) 642 return 0; 643 644 x_recv = htonl(hc->rx_x_recv); 645 pinv = htonl(hc->rx_pinv); 646 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))) 651 return -1; 652 653 return 0; 654 } 655 656 /** 657 * ccid3_first_li - Implements [RFC 5348, 6.3.1] 658 * 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 */ 666 static u32 ccid3_first_li(struct sock *sk) 667 { 668 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk); 669 u32 x_recv, p; 670 s64 delta; 671 u64 fval; 672 673 if (hc->rx_rtt == 0) { 674 DCCP_WARN("No RTT estimate available, using fallback RTT\n"); 675 hc->rx_rtt = DCCP_FALLBACK_RTT; 676 } 677 678 delta = ktime_us_delta(ktime_get(), hc->rx_tstamp_last_feedback); 679 if (delta <= 0) 680 delta = 1; 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"); 686 return ~0U; 687 } 688 x_recv = hc->rx_x_recv; 689 } 690 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); 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 698 return p == 0 ? ~0U : scaled_div(1, p); 699 } 700 701 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb) 702 { 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); 707 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); 713 hc->rx_s = payload; 714 /* 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 718 */ 719 } 720 goto update_records; 721 } 722 723 if (tfrc_rx_hist_duplicate(&hc->rx_hist, skb)) 724 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 */ 731 hc->rx_s = tfrc_ewma(hc->rx_s, payload, 9); 732 hc->rx_bytes_recv += payload; 733 } 734 735 /* 736 * Perform loss detection and handle pending losses 737 */ 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; 741 goto done_receiving; 742 } 743 744 if (tfrc_rx_hist_loss_pending(&hc->rx_hist)) 745 return; /* done receiving */ 746 747 /* 748 * Handle data packets: RTT sampling and monitoring p 749 */ 750 if (unlikely(!is_data_packet)) 751 goto update_records; 752 753 if (!tfrc_lh_is_initialised(&hc->rx_li_hist)) { 754 const u32 sample = tfrc_rx_hist_sample_rtt(&hc->rx_hist, skb); 755 /* 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) 761 hc->rx_rtt = tfrc_ewma(hc->rx_rtt, sample, 9); 762 763 } else if (tfrc_lh_update_i_mean(&hc->rx_li_hist, skb)) { 764 /* 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 771 /* 772 * Check if the periodic once-per-RTT feedback is due; RFC 4342, 10.3 773 */ 774 if (SUB16(dccp_hdr(skb)->dccph_ccval, hc->rx_last_counter) > 3) 775 do_feedback = CCID3_FBACK_PERIODIC; 776 777 update_records: 778 tfrc_rx_hist_add_packet(&hc->rx_hist, skb, ndp); 779 780 done_receiving: 781 if (do_feedback) 782 ccid3_hc_rx_send_feedback(sk, skb, do_feedback); 783 } 784 785 static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk) 786 { 787 struct ccid3_hc_rx_sock *hc = ccid_priv(ccid); 788 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); 792 } 793 794 static void ccid3_hc_rx_exit(struct sock *sk) 795 { 796 struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk); 797 798 tfrc_rx_hist_purge(&hc->rx_hist); 799 tfrc_lh_cleanup(&hc->rx_li_hist); 800 } 801 802 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info) 803 { 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; 807 } 808 809 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len, 810 u32 __user *optval, int __user *optlen) 811 { 812 const struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk); 813 struct tfrc_rx_info rx_info; 814 const void *val; 815 816 switch (optname) { 817 case DCCP_SOCKOPT_CCID_RX_INFO: 818 if (len < sizeof(rx_info)) 819 return -EINVAL; 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); 824 val = &rx_info; 825 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 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, 855 }; 856 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"); 860 #endif 861