lsquic_sfcw.c revision 10c492f0
1/* Copyright (c) 2017 - 2018 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_int_types.h" 10#include "lsquic_conn_flow.h" 11#include "lsquic_types.h" 12#include "lsquic_rtt.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_SFCW 23#define LSQUIC_LOG_CONN_ID fc->sf_conn_pub->lconn->cn_cid 24#define LSQUIC_LOG_STREAM_ID fc->sf_stream_id 25#include "lsquic_logger.h" 26 27void 28lsquic_sfcw_init (struct lsquic_sfcw *fc, unsigned max_recv_window, 29 struct lsquic_cfcw *cfcw, struct lsquic_conn_public *cpub, 30 unsigned stream_id) 31{ 32 memset(fc, 0, sizeof(*fc)); 33 fc->sf_max_recv_win = max_recv_window; 34 fc->sf_cfcw = cfcw; 35 fc->sf_conn_pub = cpub; 36 fc->sf_stream_id = stream_id; 37 (void) lsquic_sfcw_fc_offsets_changed(fc); 38} 39 40 41static void 42sfcw_maybe_increase_max_window (struct lsquic_sfcw *fc) 43{ 44 unsigned new_max_window, max_conn_window; 45 46 new_max_window = fc->sf_max_recv_win * 2; 47 48 /* Do not increase past explicitly specified maximum */ 49 if (new_max_window > fc->sf_conn_pub->enpub->enp_settings.es_max_sfcw) 50 new_max_window = fc->sf_conn_pub->enpub->enp_settings.es_max_sfcw; 51 52 if (fc->sf_cfcw) 53 { 54 /* Do not increase past the connection's maximum window size. The 55 * connection's window will be increased separately, if possible. 56 * 57 * The reference implementation has the logic backwards: Imagine 58 * several concurrent streams that are not being read from fast 59 * enough by the user code. Each of them uses only a fraction 60 * of bandwidth. Does it mean that the connection window must 61 * increase? No. 62 */ 63 max_conn_window = lsquic_cfcw_get_max_recv_window(fc->sf_cfcw); 64 if (new_max_window > max_conn_window) 65 new_max_window = max_conn_window; 66 } 67 else 68 { 69 /* This means that this stream is not affected by connection flow 70 * controller. No need to adjust under connection window. 71 */ 72 } 73 74 if (new_max_window > fc->sf_max_recv_win) 75 { 76 LSQ_DEBUG("max window increase %u -> %u", 77 fc->sf_max_recv_win, new_max_window); 78 EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, 79 "max SFCW increase %u -> %u", fc->sf_max_recv_win, 80 new_max_window); 81 fc->sf_max_recv_win = new_max_window; 82 } 83 else 84 LSQ_DEBUG("max window could use an increase, but we're stuck " 85 "at %u", fc->sf_max_recv_win); 86} 87 88 89int 90lsquic_sfcw_fc_offsets_changed (struct lsquic_sfcw *fc) 91{ 92 lsquic_time_t since_last_update, srtt, now; 93 94 if (fc->sf_recv_off - fc->sf_read_off >= fc->sf_max_recv_win / 2) 95 { 96 LSQ_DEBUG("recv_off has not changed, still at %"PRIu64, 97 fc->sf_recv_off); 98 return 0; 99 } 100 101 now = lsquic_time_now(); 102 since_last_update = now - fc->sf_last_updated; 103 fc->sf_last_updated = now; 104 105 srtt = lsquic_rtt_stats_get_srtt(&fc->sf_conn_pub->rtt_stats); 106 if (since_last_update < srtt * 2) 107 sfcw_maybe_increase_max_window(fc); 108 109 fc->sf_recv_off = fc->sf_read_off + fc->sf_max_recv_win; 110 LSQ_DEBUG("recv_off changed: read_off: %"PRIu64"; " 111 "recv_off: %"PRIu64, fc->sf_read_off, fc->sf_recv_off); 112 return 1; 113} 114 115 116int 117lsquic_sfcw_set_max_recv_off (struct lsquic_sfcw *fc, uint64_t max_recv_off) 118{ 119 if (max_recv_off <= fc->sf_recv_off) 120 { 121 if (!fc->sf_cfcw || lsquic_cfcw_incr_max_recv_off(fc->sf_cfcw, 122 max_recv_off - fc->sf_max_recv_off)) 123 { 124 LSQ_DEBUG("max_recv_off goes from %"PRIu64" to %"PRIu64, 125 fc->sf_max_recv_off, max_recv_off); 126 fc->sf_max_recv_off = max_recv_off; 127 return 1; 128 } 129 else 130 { 131 /* cfcw prints its own warning */ 132 return 0; 133 } 134 } 135 else 136 { 137 LSQ_WARN("flow control violation: received at offset %"PRIu64", " 138 "while flow control receive offset is %"PRIu64, 139 max_recv_off, fc->sf_recv_off); 140 return 0; 141 } 142} 143 144 145void 146lsquic_sfcw_set_read_off (struct lsquic_sfcw *fc, uint64_t off) 147{ 148 if (fc->sf_cfcw) 149 lsquic_cfcw_incr_read_off(fc->sf_cfcw, off - fc->sf_read_off); 150 LSQ_DEBUG("read_off goes from %"PRIu64" to %"PRIu64, 151 fc->sf_read_off, off); 152 fc->sf_read_off = off; 153} 154