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