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