quic_statm.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2022 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_ticks2time(333 * OSSL_TIME_MS))
  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. statm->max_ack_delay = ossl_time_infinite();
  53. return 1;
  54. }
  55. void ossl_statm_destroy(OSSL_STATM *statm)
  56. {
  57. /* No-op. */
  58. }
  59. void ossl_statm_set_max_ack_delay(OSSL_STATM *statm, OSSL_TIME max_ack_delay)
  60. {
  61. statm->max_ack_delay = max_ack_delay;
  62. }
  63. void ossl_statm_get_rtt_info(OSSL_STATM *statm, OSSL_RTT_INFO *rtt_info)
  64. {
  65. rtt_info->min_rtt = statm->min_rtt;
  66. rtt_info->latest_rtt = statm->latest_rtt;
  67. rtt_info->smoothed_rtt = statm->smoothed_rtt;
  68. rtt_info->rtt_variance = statm->rtt_variance;
  69. rtt_info->max_ack_delay = statm->max_ack_delay;
  70. }