quic_statm.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/quic_statm.h"
  10. void ossl_statm_update_rtt(OSSL_STATM *statm,
  11. OSSL_TIME ack_delay,
  12. OSSL_TIME override_latest_rtt)
  13. {
  14. OSSL_TIME adjusted_rtt, latest_rtt = override_latest_rtt;
  15. /* Use provided RTT value, or else last RTT value. */
  16. if (ossl_time_is_zero(latest_rtt))
  17. latest_rtt = statm->latest_rtt;
  18. else
  19. statm->latest_rtt = latest_rtt;
  20. if (!statm->have_first_sample) {
  21. statm->min_rtt = latest_rtt;
  22. statm->smoothed_rtt = latest_rtt;
  23. statm->rtt_variance = ossl_time_divide(latest_rtt, 2);
  24. statm->have_first_sample = 1;
  25. return;
  26. }
  27. /* Update minimum RTT. */
  28. if (ossl_time_compare(latest_rtt, statm->min_rtt) < 0)
  29. statm->min_rtt = latest_rtt;
  30. /*
  31. * Enforcement of max_ack_delay is the responsibility of
  32. * the caller as it is context-dependent.
  33. */
  34. adjusted_rtt = latest_rtt;
  35. if (ossl_time_compare(latest_rtt, ossl_time_add(statm->min_rtt, ack_delay)) >= 0)
  36. adjusted_rtt = ossl_time_subtract(latest_rtt, ack_delay);
  37. statm->rtt_variance = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->rtt_variance, 3),
  38. ossl_time_abs_difference(statm->smoothed_rtt,
  39. adjusted_rtt)), 4);
  40. statm->smoothed_rtt = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->smoothed_rtt, 7),
  41. adjusted_rtt), 8);
  42. }
  43. /* RFC 9002 kInitialRtt value. RFC recommended value. */
  44. #define K_INITIAL_RTT ossl_ms2time(333)
  45. int ossl_statm_init(OSSL_STATM *statm)
  46. {
  47. statm->smoothed_rtt = K_INITIAL_RTT;
  48. statm->latest_rtt = ossl_time_zero();
  49. statm->min_rtt = ossl_time_infinite();
  50. statm->rtt_variance = ossl_time_divide(K_INITIAL_RTT, 2);
  51. statm->have_first_sample = 0;
  52. return 1;
  53. }
  54. void ossl_statm_destroy(OSSL_STATM *statm)
  55. {
  56. /* No-op. */
  57. }
  58. void ossl_statm_get_rtt_info(OSSL_STATM *statm, OSSL_RTT_INFO *rtt_info)
  59. {
  60. rtt_info->min_rtt = statm->min_rtt;
  61. rtt_info->latest_rtt = statm->latest_rtt;
  62. rtt_info->smoothed_rtt = statm->smoothed_rtt;
  63. rtt_info->rtt_variance = statm->rtt_variance;
  64. }