lsquic_cfcw.c revision 50aadb33
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <inttypes.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <string.h>
6#include <sys/queue.h>
7
8#include "lsquic.h"
9#include "lsquic_types.h"
10#include "lsquic_int_types.h"
11#include "lsquic_rtt.h"
12#include "lsquic_conn_flow.h"
13#include "lsquic_sfcw.h"
14#include "lsquic_stream.h"
15#include "lsquic_conn_public.h"
16#include "lsquic_mm.h"
17#include "lsquic_engine_public.h"
18#include "lsquic_util.h"
19#include "lsquic_conn.h"
20#include "lsquic_ev_log.h"
21
22#define LSQUIC_LOGGER_MODULE LSQLM_CFCW
23#define LSQUIC_LOG_CONN_ID fc->cf_conn_pub->lconn->cn_cid
24#include "lsquic_logger.h"
25
26
27void
28lsquic_cfcw_init (struct lsquic_cfcw *fc, struct lsquic_conn_public *cpub,
29                                                unsigned max_recv_window)
30{
31    memset(fc, 0, sizeof(*fc));
32    fc->cf_max_recv_win = max_recv_window;
33    fc->cf_conn_pub = cpub;
34    (void) lsquic_cfcw_fc_offsets_changed(fc);
35}
36
37
38static void
39cfcw_maybe_increase_max_window (struct lsquic_cfcw *fc)
40{
41    unsigned new_max_window;
42
43    new_max_window = fc->cf_max_recv_win * 2;
44
45    /* Do not increase past explicitly specified maximum */
46    if (new_max_window > fc->cf_conn_pub->enpub->enp_settings.es_max_cfcw)
47        new_max_window = fc->cf_conn_pub->enpub->enp_settings.es_max_cfcw;
48
49    if (new_max_window > fc->cf_max_recv_win)
50    {
51        LSQ_DEBUG("max window increase %u -> %u", fc->cf_max_recv_win,
52                                                            new_max_window);
53        EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID,
54            "max CFCW increase %u -> %u", fc->cf_max_recv_win,
55                                                            new_max_window);
56        fc->cf_max_recv_win = new_max_window;
57    }
58    else
59        LSQ_DEBUG("max window could use an increase, but we're stuck "
60            "at %u", fc->cf_max_recv_win);
61}
62
63
64int
65lsquic_cfcw_fc_offsets_changed (struct lsquic_cfcw *fc)
66{
67    lsquic_time_t now, since_last_update, srtt;
68
69    if (fc->cf_recv_off - fc->cf_read_off >= fc->cf_max_recv_win / 2)
70        return 0;
71
72    now = lsquic_time_now();
73    since_last_update = now - fc->cf_last_updated;
74    fc->cf_last_updated = now;
75
76    srtt = lsquic_rtt_stats_get_srtt(&fc->cf_conn_pub->rtt_stats);
77    if (since_last_update < srtt * 2)
78        cfcw_maybe_increase_max_window(fc);
79
80    fc->cf_recv_off = fc->cf_read_off + fc->cf_max_recv_win;
81    LSQ_DEBUG("recv_off changed: read_off: %"PRIu64"; recv_off: %"
82        PRIu64"", fc->cf_read_off, fc->cf_recv_off);
83    return 1;
84}
85
86
87int
88lsquic_cfcw_incr_max_recv_off (struct lsquic_cfcw *fc, uint64_t incr)
89{
90    if (fc->cf_max_recv_off + incr <= fc->cf_recv_off)
91    {
92        fc->cf_max_recv_off += incr;
93        LSQ_DEBUG("max_recv_off goes from %"PRIu64" to %"PRIu64"",
94                    fc->cf_max_recv_off - incr, fc->cf_max_recv_off);
95        return 1;
96    }
97    else
98    {
99        LSQ_WARN("flow control violation: received at offset %"PRIu64", while "
100            "flow control receive offset is %"PRIu64,
101            fc->cf_max_recv_off + incr, fc->cf_recv_off);
102        return 0;
103    }
104}
105
106
107void
108lsquic_cfcw_incr_read_off (struct lsquic_cfcw *fc, uint64_t incr)
109{
110    fc->cf_read_off += incr;
111    LSQ_DEBUG("read_off goes from %"PRIu64" to %"PRIu64,
112        fc->cf_read_off - incr, fc->cf_read_off);
113}
114