lsquic_sfcw.c revision 50aadb33
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <inttypes.h>
350aadb33SDmitri Tikhonov#include <stdint.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
650aadb33SDmitri Tikhonov#include <sys/queue.h>
750aadb33SDmitri Tikhonov
850aadb33SDmitri Tikhonov#include "lsquic.h"
950aadb33SDmitri Tikhonov#include "lsquic_int_types.h"
1050aadb33SDmitri Tikhonov#include "lsquic_conn_flow.h"
1150aadb33SDmitri Tikhonov#include "lsquic_types.h"
1250aadb33SDmitri Tikhonov#include "lsquic_rtt.h"
1350aadb33SDmitri Tikhonov#include "lsquic_sfcw.h"
1450aadb33SDmitri Tikhonov#include "lsquic_stream.h"
1550aadb33SDmitri Tikhonov#include "lsquic_conn_public.h"
1650aadb33SDmitri Tikhonov#include "lsquic_mm.h"
1750aadb33SDmitri Tikhonov#include "lsquic_engine_public.h"
1850aadb33SDmitri Tikhonov#include "lsquic_util.h"
1950aadb33SDmitri Tikhonov#include "lsquic_conn.h"
2050aadb33SDmitri Tikhonov#include "lsquic_ev_log.h"
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_SFCW
2350aadb33SDmitri Tikhonov#define LSQUIC_LOG_CONN_ID fc->sf_conn_pub->lconn->cn_cid
2450aadb33SDmitri Tikhonov#define LSQUIC_LOG_STREAM_ID fc->sf_stream_id
2550aadb33SDmitri Tikhonov#include "lsquic_logger.h"
2650aadb33SDmitri Tikhonov
2750aadb33SDmitri Tikhonovvoid
2850aadb33SDmitri Tikhonovlsquic_sfcw_init (struct lsquic_sfcw *fc, unsigned max_recv_window,
2950aadb33SDmitri Tikhonov                  struct lsquic_cfcw *cfcw, struct lsquic_conn_public *cpub,
3050aadb33SDmitri Tikhonov                  unsigned stream_id)
3150aadb33SDmitri Tikhonov{
3250aadb33SDmitri Tikhonov    memset(fc, 0, sizeof(*fc));
3350aadb33SDmitri Tikhonov    fc->sf_max_recv_win = max_recv_window;
3450aadb33SDmitri Tikhonov    fc->sf_cfcw = cfcw;
3550aadb33SDmitri Tikhonov    fc->sf_conn_pub = cpub;
3650aadb33SDmitri Tikhonov    fc->sf_stream_id = stream_id;
3750aadb33SDmitri Tikhonov    (void) lsquic_sfcw_fc_offsets_changed(fc);
3850aadb33SDmitri Tikhonov}
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov
4150aadb33SDmitri Tikhonovstatic void
4250aadb33SDmitri Tikhonovsfcw_maybe_increase_max_window (struct lsquic_sfcw *fc)
4350aadb33SDmitri Tikhonov{
4450aadb33SDmitri Tikhonov    unsigned new_max_window, max_conn_window;
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonov    new_max_window = fc->sf_max_recv_win * 2;
4750aadb33SDmitri Tikhonov
4850aadb33SDmitri Tikhonov    /* Do not increase past explicitly specified maximum */
4950aadb33SDmitri Tikhonov    if (new_max_window > fc->sf_conn_pub->enpub->enp_settings.es_max_sfcw)
5050aadb33SDmitri Tikhonov        new_max_window = fc->sf_conn_pub->enpub->enp_settings.es_max_sfcw;
5150aadb33SDmitri Tikhonov
5250aadb33SDmitri Tikhonov    /* Do not increase past the connection's maximum window size.  The
5350aadb33SDmitri Tikhonov     * connection's window will be increased separately, if possible.
5450aadb33SDmitri Tikhonov     *
5550aadb33SDmitri Tikhonov     * The reference implementation has the logic backwards:  Imagine
5650aadb33SDmitri Tikhonov     * several concurrent streams that are not being read from fast
5750aadb33SDmitri Tikhonov     * enough by the user code.  Each of them uses only a fraction
5850aadb33SDmitri Tikhonov     * of bandwidth.  Does it mean that the connection window must
5950aadb33SDmitri Tikhonov     * increase?  No.
6050aadb33SDmitri Tikhonov     */
6150aadb33SDmitri Tikhonov    max_conn_window = lsquic_cfcw_get_max_recv_window(fc->sf_cfcw);
6250aadb33SDmitri Tikhonov    if (new_max_window > max_conn_window)
6350aadb33SDmitri Tikhonov        new_max_window = max_conn_window;
6450aadb33SDmitri Tikhonov
6550aadb33SDmitri Tikhonov    if (new_max_window > fc->sf_max_recv_win)
6650aadb33SDmitri Tikhonov    {
6750aadb33SDmitri Tikhonov        LSQ_DEBUG("max window increase %u -> %u",
6850aadb33SDmitri Tikhonov            fc->sf_max_recv_win, new_max_window);
6950aadb33SDmitri Tikhonov        EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID,
7050aadb33SDmitri Tikhonov            "max SFCW increase %u -> %u", fc->sf_max_recv_win,
7150aadb33SDmitri Tikhonov                                                            new_max_window);
7250aadb33SDmitri Tikhonov        fc->sf_max_recv_win = new_max_window;
7350aadb33SDmitri Tikhonov    }
7450aadb33SDmitri Tikhonov    else
7550aadb33SDmitri Tikhonov        LSQ_DEBUG("max window could use an increase, but we're stuck "
7650aadb33SDmitri Tikhonov            "at %u", fc->sf_max_recv_win);
7750aadb33SDmitri Tikhonov}
7850aadb33SDmitri Tikhonov
7950aadb33SDmitri Tikhonov
8050aadb33SDmitri Tikhonovint
8150aadb33SDmitri Tikhonovlsquic_sfcw_fc_offsets_changed (struct lsquic_sfcw *fc)
8250aadb33SDmitri Tikhonov{
8350aadb33SDmitri Tikhonov    lsquic_time_t since_last_update, srtt, now;
8450aadb33SDmitri Tikhonov
8550aadb33SDmitri Tikhonov    if (fc->sf_recv_off - fc->sf_read_off >= fc->sf_max_recv_win / 2)
8650aadb33SDmitri Tikhonov    {
8750aadb33SDmitri Tikhonov        LSQ_DEBUG("recv_off has not changed, still at %"PRIu64,
8850aadb33SDmitri Tikhonov                                                            fc->sf_recv_off);
8950aadb33SDmitri Tikhonov        return 0;
9050aadb33SDmitri Tikhonov    }
9150aadb33SDmitri Tikhonov
9250aadb33SDmitri Tikhonov    now = lsquic_time_now();
9350aadb33SDmitri Tikhonov    since_last_update = now - fc->sf_last_updated;
9450aadb33SDmitri Tikhonov    fc->sf_last_updated = now;
9550aadb33SDmitri Tikhonov
9650aadb33SDmitri Tikhonov    srtt = lsquic_rtt_stats_get_srtt(&fc->sf_conn_pub->rtt_stats);
9750aadb33SDmitri Tikhonov    if (since_last_update < srtt * 2)
9850aadb33SDmitri Tikhonov        sfcw_maybe_increase_max_window(fc);
9950aadb33SDmitri Tikhonov
10050aadb33SDmitri Tikhonov    fc->sf_recv_off = fc->sf_read_off + fc->sf_max_recv_win;
10150aadb33SDmitri Tikhonov    LSQ_DEBUG("recv_off changed: read_off: %"PRIu64"; "
10250aadb33SDmitri Tikhonov        "recv_off: %"PRIu64, fc->sf_read_off, fc->sf_recv_off);
10350aadb33SDmitri Tikhonov    return 1;
10450aadb33SDmitri Tikhonov}
10550aadb33SDmitri Tikhonov
10650aadb33SDmitri Tikhonov
10750aadb33SDmitri Tikhonovint
10850aadb33SDmitri Tikhonovlsquic_sfcw_set_max_recv_off (struct lsquic_sfcw *fc, uint64_t max_recv_off)
10950aadb33SDmitri Tikhonov{
11050aadb33SDmitri Tikhonov    if (max_recv_off <= fc->sf_recv_off)
11150aadb33SDmitri Tikhonov    {
11250aadb33SDmitri Tikhonov        if (!fc->sf_cfcw || lsquic_cfcw_incr_max_recv_off(fc->sf_cfcw,
11350aadb33SDmitri Tikhonov                                        max_recv_off - fc->sf_max_recv_off))
11450aadb33SDmitri Tikhonov        {
11550aadb33SDmitri Tikhonov            LSQ_DEBUG("max_recv_off goes from %"PRIu64" to %"PRIu64,
11650aadb33SDmitri Tikhonov                                            fc->sf_max_recv_off, max_recv_off);
11750aadb33SDmitri Tikhonov            fc->sf_max_recv_off = max_recv_off;
11850aadb33SDmitri Tikhonov            return 1;
11950aadb33SDmitri Tikhonov        }
12050aadb33SDmitri Tikhonov        else
12150aadb33SDmitri Tikhonov        {
12250aadb33SDmitri Tikhonov            /* cfcw prints its own warning */
12350aadb33SDmitri Tikhonov            return 0;
12450aadb33SDmitri Tikhonov        }
12550aadb33SDmitri Tikhonov    }
12650aadb33SDmitri Tikhonov    else
12750aadb33SDmitri Tikhonov    {
12850aadb33SDmitri Tikhonov        LSQ_WARN("flow control violation: received at offset %"PRIu64", "
12950aadb33SDmitri Tikhonov            "while flow control receive offset is %"PRIu64,
13050aadb33SDmitri Tikhonov            max_recv_off, fc->sf_recv_off);
13150aadb33SDmitri Tikhonov        return 0;
13250aadb33SDmitri Tikhonov    }
13350aadb33SDmitri Tikhonov}
13450aadb33SDmitri Tikhonov
13550aadb33SDmitri Tikhonov
13650aadb33SDmitri Tikhonovvoid
13750aadb33SDmitri Tikhonovlsquic_sfcw_set_read_off (struct lsquic_sfcw *fc, uint64_t off)
13850aadb33SDmitri Tikhonov{
13950aadb33SDmitri Tikhonov    if (fc->sf_cfcw)
14050aadb33SDmitri Tikhonov        lsquic_cfcw_incr_read_off(fc->sf_cfcw, off - fc->sf_read_off);
14150aadb33SDmitri Tikhonov    LSQ_DEBUG("read_off goes from %"PRIu64" to %"PRIu64,
14250aadb33SDmitri Tikhonov                                                fc->sf_read_off, off);
14350aadb33SDmitri Tikhonov    fc->sf_read_off = off;
14450aadb33SDmitri Tikhonov}
145