lsquic_send_ctl.h revision b55a5117
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef LSQUIC_SEND_CTL_H 3#define LSQUIC_SEND_CTL_H 1 4 5#include <sys/queue.h> 6 7#include "lsquic_types.h" 8 9#ifndef LSQUIC_SEND_STATS 10# define LSQUIC_SEND_STATS 1 11#endif 12 13TAILQ_HEAD(lsquic_packets_tailq, lsquic_packet_out); 14 15struct lsquic_packet_out; 16struct ack_info; 17struct lsquic_alarmset; 18struct lsquic_engine_public; 19struct lsquic_conn_public; 20struct network_path; 21struct ver_neg; 22enum pns; 23 24enum buf_packet_type { BPT_HIGHEST_PRIO, BPT_OTHER_PRIO, }; 25 26struct buf_packet_q 27{ 28 struct lsquic_packets_tailq bpq_packets; 29 unsigned bpq_count; 30}; 31 32enum send_ctl_flags { 33 SC_TCID0 = (1 << 0), 34 SC_NSTP = (1 << 2), 35 SC_PACE = (1 << 3), 36 SC_SCHED_TICK = (1 << 4), 37 SC_BUFFER_STREAM= (1 << 5), 38 SC_WAS_QUIET = (1 << 6), 39 SC_IETF = (1 << 7), 40#define SCBIT_LOST_ACK_SHIFT 8 41 SC_LOST_ACK_INIT= 1 << 8, 42 SC_LOST_ACK_HSK = SC_LOST_ACK_INIT << PNS_HSK, 43 SC_LOST_ACK_APP = SC_LOST_ACK_INIT << PNS_APP, 44 SC_1RTT_ACKED = 1 << 11, 45 SC_APP_LIMITED = 1 << 12, 46 SC_ECN = 1 << 13, 47 SC_QL_BITS = 1 << 14, 48 SC_SANITY_CHECK = 1 << 15, 49 SC_CIDLEN = 1 << 16, /* sc_cidlen is set */ 50 SC_POISON = 1 << 17, /* poisoned packet exists */ 51}; 52 53typedef struct lsquic_send_ctl { 54 /* The first section consists of struct members which are used in the 55 * time-critical lsquic_send_ctl_got_ack() in the approximate order 56 * of usage. 57 */ 58 lsquic_senhist_t sc_senhist; 59 enum send_ctl_flags sc_flags; 60 enum ecn sc_ecn; 61 unsigned sc_n_stop_waiting; 62 struct lsquic_packets_tailq sc_unacked_packets[N_PNS]; 63 lsquic_packno_t sc_largest_acked_packno; 64 lsquic_time_t sc_largest_acked_sent_time; 65 lsquic_time_t sc_last_sent_time; 66 lsquic_time_t sc_last_rto_time; 67 int (*sc_can_send)(struct lsquic_send_ctl *); 68 unsigned sc_bytes_unacked_retx; 69 unsigned sc_bytes_scheduled; 70 union { 71 struct lsquic_cubic cubic; 72 struct lsquic_bbr bbr; 73 } sc_cong_u; 74 const struct cong_ctl_if *sc_ci; 75 struct lsquic_engine_public *sc_enpub; 76 unsigned sc_bytes_unacked_all; 77 unsigned sc_n_in_flight_all; 78 unsigned sc_n_in_flight_retx; 79 unsigned sc_n_consec_rtos; 80 unsigned sc_n_hsk; 81 unsigned sc_n_tlp; 82 enum quic_ft_bit sc_retx_frames; 83 struct lsquic_alarmset *sc_alset; 84 85 /* Second section: everything else. */ 86 struct lsquic_packets_tailq sc_scheduled_packets, 87 sc_lost_packets; 88 struct buf_packet_q sc_buffered_packets[BPT_OTHER_PRIO + 1]; 89 const struct ver_neg *sc_ver_neg; 90 struct lsquic_conn_public *sc_conn_pub; 91 struct pacer sc_pacer; 92 lsquic_packno_t sc_cur_packno; 93 lsquic_packno_t sc_largest_sent_at_cutback; 94 lsquic_packno_t sc_max_rtt_packno; 95 /* sc_largest_ack2ed is the packet number sent by peer that we acked and 96 * we know that our ACK was received by peer. This is used to determine 97 * the receive history cutoff point for the purposes of generating ACK 98 * frames in the absense of STOP_WAITING frames. Used when NSTP option 99 * is set. (The "ack2ed" is odd enough to not be confused with anything 100 * else and it is not insanely long.) 101 */ 102 lsquic_packno_t sc_largest_ack2ed[N_PNS]; 103 /* sc_largest_acked is the largest packet number in PNS_APP packet number 104 * space sent by peer for which we generated (not necessarily sent) an ACK. 105 * This information is used to drop stale ACK frames from packets in 106 * buffered queues. 107 */ 108 lsquic_packno_t sc_largest_acked; 109 lsquic_time_t sc_loss_to; 110 uint64_t sc_ecn_total_acked[N_PNS]; 111 uint64_t sc_ecn_ce_cnt[N_PNS]; 112 struct 113 { 114 lsquic_stream_id_t stream_id; 115 enum buf_packet_type packet_type; 116 } sc_cached_bpt; 117 unsigned sc_next_limit; 118 unsigned sc_n_scheduled; 119 enum packno_bits sc_max_packno_bits; 120#if LSQUIC_SEND_STATS 121 struct { 122 unsigned n_total_sent, 123 n_resent, 124 n_delayed; 125 } sc_stats; 126#endif 127 unsigned char *sc_token; 128 size_t sc_token_sz; 129 unsigned sc_retry_count; 130 unsigned sc_rt_count; /* Count round trips */ 131 lsquic_packno_t sc_cur_rt_end; 132 lsquic_packno_t sc_gap; 133 unsigned sc_loss_count; /* Used to set loss bit */ 134 unsigned sc_square_count;/* Used to set square bit */ 135 signed char sc_cidlen; /* For debug purposes */ 136} lsquic_send_ctl_t; 137 138void 139lsquic_send_ctl_init (lsquic_send_ctl_t *, struct lsquic_alarmset *, 140 struct lsquic_engine_public *, const struct ver_neg *, 141 struct lsquic_conn_public *, enum send_ctl_flags); 142 143int 144lsquic_send_ctl_sent_packet (lsquic_send_ctl_t *, struct lsquic_packet_out *); 145 146int 147lsquic_send_ctl_got_ack (lsquic_send_ctl_t *, const struct ack_info *, 148 lsquic_time_t, lsquic_time_t); 149 150lsquic_packno_t 151lsquic_send_ctl_smallest_unacked (lsquic_send_ctl_t *ctl); 152 153int 154lsquic_send_ctl_have_unacked_stream_frames (const lsquic_send_ctl_t *); 155 156void 157lsquic_send_ctl_cleanup (lsquic_send_ctl_t *); 158 159int 160lsquic_send_ctl_can_send (lsquic_send_ctl_t *ctl); 161 162void 163lsquic_send_ctl_scheduled_one (lsquic_send_ctl_t *, struct lsquic_packet_out *); 164 165void 166lsquic_send_ctl_delayed_one (lsquic_send_ctl_t *, struct lsquic_packet_out *); 167 168struct lsquic_packet_out * 169lsquic_send_ctl_next_packet_to_send (struct lsquic_send_ctl *, size_t); 170 171void 172lsquic_send_ctl_expire_all (lsquic_send_ctl_t *ctl); 173 174#define lsquic_send_ctl_n_in_flight(ctl) (+(ctl)->sc_n_in_flight) 175 176#define lsquic_send_ctl_n_scheduled(ctl) (+(ctl)->sc_n_scheduled) 177 178#define lsquic_send_ctl_largest_ack2ed(ctl, pns) \ 179 (+(ctl)->sc_largest_ack2ed[pns]) 180 181void 182lsquic_send_ctl_do_sanity_check (const struct lsquic_send_ctl *ctl); 183 184#ifndef NDEBUG 185#define lsquic_send_ctl_sanity_check(ctl) do { \ 186 if ((ctl)->sc_flags & SC_SANITY_CHECK) \ 187 lsquic_send_ctl_do_sanity_check(ctl); \ 188} while (0) 189#else 190#define lsquic_send_ctl_sanity_check(ctl) 191#endif 192 193int 194lsquic_send_ctl_have_outgoing_stream_frames (const lsquic_send_ctl_t *); 195 196int 197lsquic_send_ctl_have_outgoing_retx_frames (const lsquic_send_ctl_t *); 198 199struct lsquic_packet_out * 200lsquic_send_ctl_last_scheduled (struct lsquic_send_ctl *, enum packnum_space, 201 const struct network_path *, int); 202 203struct lsquic_packet_out * 204lsquic_send_ctl_new_packet_out (lsquic_send_ctl_t *, unsigned, 205 enum packnum_space, const struct network_path *); 206 207struct lsquic_packet_out * 208lsquic_send_ctl_get_writeable_packet (lsquic_send_ctl_t *, enum packnum_space, 209 unsigned need_at_least, const struct network_path *, int, int *is_err); 210 211struct lsquic_packet_out * 212lsquic_send_ctl_get_packet_for_stream (lsquic_send_ctl_t *, 213 unsigned need_at_least, const struct network_path *, 214 const struct lsquic_stream *); 215 216struct lsquic_packet_out * 217lsquic_send_ctl_get_packet_for_crypto (struct lsquic_send_ctl *ctl, 218 unsigned need_at_least, enum packnum_space, const struct network_path *); 219 220unsigned 221lsquic_send_ctl_reschedule_packets (lsquic_send_ctl_t *); 222 223#define lsquic_send_ctl_lost_ack(ctl) \ 224 (((ctl)->sc_flags & (SC_LOST_ACK_INIT|SC_LOST_ACK_HSK|SC_LOST_ACK_APP)) \ 225 >> SCBIT_LOST_ACK_SHIFT) 226 227#define lsquic_send_ctl_scheduled_ack(ctl, pns, acked) do { \ 228 (ctl)->sc_flags &= ~(SC_LOST_ACK_INIT << pns); \ 229 if (PNS_APP == pns) \ 230 (ctl)->sc_largest_acked = acked; \ 231} while (0) 232 233void 234lsquic_send_ctl_set_tcid0 (lsquic_send_ctl_t *, int); 235 236#define lsquic_send_ctl_turn_nstp_on(ctl) ((ctl)->sc_flags |= SC_NSTP) 237 238void 239lsquic_send_ctl_elide_stream_frames (lsquic_send_ctl_t *, lsquic_stream_id_t); 240 241int 242lsquic_send_ctl_squeeze_sched (lsquic_send_ctl_t *); 243 244#define lsquic_send_ctl_maybe_squeeze_sched(ctl) ( \ 245 (ctl)->sc_n_scheduled && lsquic_send_ctl_squeeze_sched(ctl) \ 246) 247 248/* Same return value as for squeezing, but without actual squeezing. */ 249int 250lsquic_send_ctl_have_delayed_packets (const lsquic_send_ctl_t *ctl); 251 252void 253lsquic_send_ctl_reset_packnos (lsquic_send_ctl_t *); 254 255void 256lsquic_send_ctl_ack_to_front (struct lsquic_send_ctl *, unsigned n_acks); 257 258#define lsquic_send_ctl_n_stop_waiting(ctl) \ 259 (+(ctl)->sc_n_stop_waiting) 260 261#define lsquic_send_ctl_n_stop_waiting_reset(ctl) do { \ 262 (ctl)->sc_n_stop_waiting = 0; \ 263} while (0) 264 265void 266lsquic_send_ctl_drop_scheduled (lsquic_send_ctl_t *); 267 268#define lsquic_send_ctl_tick_in(ctl, now) do { \ 269 if ((ctl)->sc_flags & SC_PACE) \ 270 { \ 271 (ctl)->sc_flags |= SC_SCHED_TICK; \ 272 lsquic_pacer_tick_in(&(ctl)->sc_pacer, now); \ 273 } \ 274 (ctl)->sc_flags &= ~SC_APP_LIMITED; \ 275} while (0) 276 277#define lsquic_send_ctl_tick_out(ctl) do { \ 278 if ((ctl)->sc_flags & SC_PACE) \ 279 lsquic_pacer_tick_out(&(ctl)->sc_pacer); \ 280} while (0) 281 282#define lsquic_send_ctl_next_pacer_time(ctl) ( \ 283 ((ctl)->sc_flags & SC_PACE) \ 284 && lsquic_pacer_delayed(&(ctl)->sc_pacer) \ 285 ? lsquic_pacer_next_sched(&(ctl)->sc_pacer) \ 286 : 0 ) 287 288enum packno_bits 289lsquic_send_ctl_packno_bits (lsquic_send_ctl_t *); 290 291int 292lsquic_send_ctl_schedule_buffered (lsquic_send_ctl_t *, enum buf_packet_type); 293 294#define lsquic_send_ctl_has_buffered(ctl) ( \ 295 TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets) \ 296 || TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets )) 297 298#define lsquic_send_ctl_has_buffered_high(ctl) ( \ 299 !TAILQ_EMPTY(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets)) 300 301#define lsquic_send_ctl_invalidate_bpt_cache(ctl) do { \ 302 (ctl)->sc_cached_bpt.stream_id = UINT64_MAX; \ 303} while (0) 304 305#ifndef NDEBUG 306enum packno_bits 307lsquic_send_ctl_guess_packno_bits (struct lsquic_send_ctl *); 308 309int 310lsquic_send_ctl_schedule_stream_packets_immediately (struct lsquic_send_ctl *); 311 312enum buf_packet_type 313lsquic_send_ctl_determine_bpt (struct lsquic_send_ctl *, 314 const struct lsquic_stream *); 315 316enum packno_bits 317lsquic_send_ctl_calc_packno_bits (struct lsquic_send_ctl *); 318 319void 320lsquic_send_ctl_set_max_bpq_count (unsigned); 321#endif 322 323size_t 324lsquic_send_ctl_mem_used (const struct lsquic_send_ctl *); 325 326#define lsquic_send_ctl_set_buffer_stream_packets(ctl, b) do { \ 327 (ctl)->sc_flags &= ~SC_BUFFER_STREAM; \ 328 (ctl)->sc_flags |= -!!(b) & SC_BUFFER_STREAM; \ 329} while (0) 330 331int 332lsquic_send_ctl_turn_on_fin (struct lsquic_send_ctl *, 333 const struct lsquic_stream *); 334 335int 336lsquic_send_ctl_pacer_blocked (struct lsquic_send_ctl *); 337 338#define lsquic_send_ctl_incr_pack_sz(ctl, packet, delta) do { \ 339 (packet)->po_data_sz += (delta); \ 340 if ((packet)->po_flags & PO_SCHED) \ 341 (ctl)->sc_bytes_scheduled += (delta); \ 342 lsquic_send_ctl_sanity_check(ctl); \ 343} while (0) 344 345int 346lsquic_send_ctl_sched_is_blocked (struct lsquic_send_ctl *); 347 348void 349lsquic_send_ctl_verneg_done (struct lsquic_send_ctl *); 350 351int 352lsquic_send_ctl_retry (struct lsquic_send_ctl *, const unsigned char *, size_t); 353 354int 355lsquic_send_ctl_set_token (struct lsquic_send_ctl *, 356 const unsigned char *token, size_t token_sz); 357 358void 359lsquic_send_ctl_empty_pns (struct lsquic_send_ctl *, enum packnum_space); 360 361void 362lsquic_send_ctl_repath (struct lsquic_send_ctl *, struct network_path *old, 363 struct network_path *new); 364 365void 366lsquic_send_ctl_return_enc_data (struct lsquic_send_ctl *); 367 368#define lsquic_send_ctl_1rtt_acked(ctl) ((ctl)->sc_flags & SC_1RTT_ACKED) 369 370void 371lsquic_send_ctl_maybe_app_limited (struct lsquic_send_ctl *, 372 const struct network_path *); 373 374#define lsquic_send_ctl_do_ql_bits(ctl) do { \ 375 (ctl)->sc_flags |= SC_QL_BITS; \ 376} while (0) 377 378void 379lsquic_send_ctl_cidlen_change (struct lsquic_send_ctl *, 380 unsigned orig_cid_len, unsigned new_cid_len); 381 382void 383lsquic_send_ctl_begin_optack_detection (struct lsquic_send_ctl *); 384 385#define lsquic_send_ctl_n_unacked(ctl_) ((ctl_)->sc_n_in_flight_retx) 386 387void 388lsquic_send_ctl_path_validated (struct lsquic_send_ctl *); 389 390#endif 391