/src/openssl/ssl/quic/quic_statm.c
Line  | Count  | Source  | 
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  |  |  | 
10  |  | #include "internal/quic_statm.h"  | 
11  |  |  | 
12  |  | void ossl_statm_update_rtt(OSSL_STATM *statm,  | 
13  |  |                            OSSL_TIME ack_delay,  | 
14  |  |                            OSSL_TIME override_latest_rtt)  | 
15  | 0  | { | 
16  | 0  |     OSSL_TIME adjusted_rtt, latest_rtt = override_latest_rtt;  | 
17  |  |  | 
18  |  |     /* Use provided RTT value, or else last RTT value. */  | 
19  | 0  |     if (ossl_time_is_zero(latest_rtt))  | 
20  | 0  |         latest_rtt = statm->latest_rtt;  | 
21  | 0  |     else  | 
22  | 0  |         statm->latest_rtt = latest_rtt;  | 
23  |  | 
  | 
24  | 0  |     if (!statm->have_first_sample) { | 
25  | 0  |         statm->min_rtt              = latest_rtt;  | 
26  | 0  |         statm->smoothed_rtt         = latest_rtt;  | 
27  | 0  |         statm->rtt_variance         = ossl_time_divide(latest_rtt, 2);  | 
28  | 0  |         statm->have_first_sample    = 1;  | 
29  | 0  |         return;  | 
30  | 0  |     }  | 
31  |  |  | 
32  |  |     /* Update minimum RTT. */  | 
33  | 0  |     if (ossl_time_compare(latest_rtt, statm->min_rtt) < 0)  | 
34  | 0  |         statm->min_rtt = latest_rtt;  | 
35  |  |  | 
36  |  |     /*  | 
37  |  |      * Enforcement of max_ack_delay is the responsibility of  | 
38  |  |      * the caller as it is context-dependent.  | 
39  |  |      */  | 
40  |  | 
  | 
41  | 0  |     adjusted_rtt = latest_rtt;  | 
42  | 0  |     if (ossl_time_compare(latest_rtt, ossl_time_add(statm->min_rtt, ack_delay)) >= 0)  | 
43  | 0  |         adjusted_rtt = ossl_time_subtract(latest_rtt, ack_delay);  | 
44  |  | 
  | 
45  | 0  |     statm->rtt_variance = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->rtt_variance, 3),  | 
46  | 0  |                                                          ossl_time_abs_difference(statm->smoothed_rtt,  | 
47  | 0  |                                                                               adjusted_rtt)), 4);  | 
48  | 0  |     statm->smoothed_rtt = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->smoothed_rtt, 7),  | 
49  | 0  |                                                          adjusted_rtt), 8);  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | /* RFC 9002 kInitialRtt value. RFC recommended value. */  | 
53  | 0  | #define K_INITIAL_RTT               ossl_ms2time(333)  | 
54  |  |  | 
55  |  | int ossl_statm_init(OSSL_STATM *statm)  | 
56  | 0  | { | 
57  | 0  |     statm->smoothed_rtt             = K_INITIAL_RTT;  | 
58  | 0  |     statm->latest_rtt               = ossl_time_zero();  | 
59  | 0  |     statm->min_rtt                  = ossl_time_infinite();  | 
60  | 0  |     statm->rtt_variance             = ossl_time_divide(K_INITIAL_RTT, 2);  | 
61  | 0  |     statm->have_first_sample        = 0;  | 
62  | 0  |     return 1;  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | void ossl_statm_destroy(OSSL_STATM *statm)  | 
66  | 0  | { | 
67  |  |     /* No-op. */  | 
68  | 0  | }  | 
69  |  |  | 
70  |  | void ossl_statm_get_rtt_info(OSSL_STATM *statm, OSSL_RTT_INFO *rtt_info)  | 
71  | 0  | { | 
72  | 0  |     rtt_info->min_rtt           = statm->min_rtt;  | 
73  | 0  |     rtt_info->latest_rtt        = statm->latest_rtt;  | 
74  | 0  |     rtt_info->smoothed_rtt      = statm->smoothed_rtt;  | 
75  | 0  |     rtt_info->rtt_variance      = statm->rtt_variance;  | 
76  | 0  | }  |