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