lsquic_rtt.c revision 50aadb33
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_rtt.c -- RTT calculation
4 */
5
6#include <stdlib.h>
7#include <string.h>
8#include <sys/time.h>
9
10#include "lsquic_int_types.h"
11#include "lsquic_rtt.h"
12
13/* See RFC 2988 */
14
15#define ALPHA_SHIFT 3   /* Alpha is 1/8 */
16#define BETA_SHIFT  2   /* Beta is 1/4 */
17
18
19void
20lsquic_rtt_stats_update (struct lsquic_rtt_stats *stats,
21                         lsquic_time_t send_delta, lsquic_time_t lack_delta)
22{
23    if (send_delta > lack_delta)
24        send_delta -= lack_delta;
25    if (stats->srtt) {
26        stats->rttvar -= stats->rttvar >> BETA_SHIFT;
27        // FIXED: subtracting unsigned (the (int) cast gets repromoted to uint64_t
28        // made abs() irrelevant and allowed overflow. instead cast the difference
29        // to a signed int64 and use labs() to get abs val.
30        stats->rttvar += (llabs((int64_t) (send_delta - stats->srtt)))
31                                                            >> BETA_SHIFT;
32        stats->srtt -= stats->srtt >> ALPHA_SHIFT;
33        stats->srtt += send_delta >> ALPHA_SHIFT;
34    } else {
35        /* First measurement */
36        stats->srtt   = send_delta;
37        stats->rttvar = send_delta >> 1;
38    }
39}
40