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