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