lsquic_send_ctl.h revision b1a7c3f9
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};
54
55typedef struct lsquic_send_ctl {
56    /* The first section consists of struct members which are used in the
57     * time-critical lsquic_send_ctl_got_ack() in the approximate order
58     * of usage.
59     */
60    lsquic_senhist_t                sc_senhist;
61    enum send_ctl_flags             sc_flags;
62    enum ecn                        sc_ecn;
63    unsigned                        sc_n_stop_waiting;
64    struct lsquic_packets_tailq     sc_unacked_packets[N_PNS];
65    lsquic_packno_t                 sc_largest_acked_packno;
66    lsquic_time_t                   sc_largest_acked_sent_time;
67    lsquic_time_t                   sc_last_sent_time;
68    lsquic_time_t                   sc_last_rto_time;
69    int                           (*sc_can_send)(struct lsquic_send_ctl *);
70    unsigned                        sc_bytes_unacked_retx;
71    unsigned                        sc_bytes_scheduled;
72    struct adaptive_cc              sc_adaptive_cc;
73    const struct cong_ctl_if       *sc_ci;
74    void                           *sc_cong_ctl;
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 *,
170                                                    const struct to_coal *);
171
172int
173lsquic_send_ctl_next_packet_to_send_predict (struct lsquic_send_ctl *);
174
175void
176lsquic_send_ctl_expire_all (lsquic_send_ctl_t *ctl);
177
178#define lsquic_send_ctl_n_in_flight(ctl) (+(ctl)->sc_n_in_flight)
179
180#define lsquic_send_ctl_n_scheduled(ctl) (+(ctl)->sc_n_scheduled)
181
182#define lsquic_send_ctl_largest_ack2ed(ctl, pns) \
183                                            (+(ctl)->sc_largest_ack2ed[pns])
184
185void
186lsquic_send_ctl_do_sanity_check (const struct lsquic_send_ctl *ctl);
187
188#ifndef NDEBUG
189#define lsquic_send_ctl_sanity_check(ctl) do {                      \
190    if ((ctl)->sc_flags & SC_SANITY_CHECK)                          \
191        lsquic_send_ctl_do_sanity_check(ctl);                       \
192} while (0)
193#else
194#define lsquic_send_ctl_sanity_check(ctl)
195#endif
196
197int
198lsquic_send_ctl_have_outgoing_stream_frames (const lsquic_send_ctl_t *);
199
200int
201lsquic_send_ctl_have_outgoing_retx_frames (const lsquic_send_ctl_t *);
202
203struct lsquic_packet_out *
204lsquic_send_ctl_last_scheduled (struct lsquic_send_ctl *, enum packnum_space,
205                                            const struct network_path *, int);
206
207struct lsquic_packet_out *
208lsquic_send_ctl_new_packet_out (lsquic_send_ctl_t *, unsigned,
209                            enum packnum_space, const struct network_path *);
210
211struct lsquic_packet_out *
212lsquic_send_ctl_get_writeable_packet (lsquic_send_ctl_t *, enum packnum_space,
213      unsigned need_at_least, const struct network_path *, int, int *is_err);
214
215struct lsquic_packet_out *
216lsquic_send_ctl_get_packet_for_stream (lsquic_send_ctl_t *,
217                    unsigned need_at_least, const struct network_path *,
218                    const struct lsquic_stream *);
219
220struct lsquic_packet_out *
221lsquic_send_ctl_get_packet_for_crypto (struct lsquic_send_ctl *ctl,
222    unsigned need_at_least, enum packnum_space, const struct network_path *);
223
224unsigned
225lsquic_send_ctl_reschedule_packets (lsquic_send_ctl_t *);
226
227#define lsquic_send_ctl_lost_ack(ctl) \
228    (((ctl)->sc_flags & (SC_LOST_ACK_INIT|SC_LOST_ACK_HSK|SC_LOST_ACK_APP)) \
229                                                        >> SCBIT_LOST_ACK_SHIFT)
230
231#define lsquic_send_ctl_scheduled_ack(ctl, pns, acked) do {         \
232    (ctl)->sc_flags &= ~(SC_LOST_ACK_INIT << pns);                  \
233    if (PNS_APP == pns)                                             \
234        (ctl)->sc_largest_acked = acked;                            \
235} while (0)
236
237void
238lsquic_send_ctl_set_tcid0 (lsquic_send_ctl_t *, int);
239
240#define lsquic_send_ctl_turn_nstp_on(ctl) ((ctl)->sc_flags |= SC_NSTP)
241
242void
243lsquic_send_ctl_elide_stream_frames (lsquic_send_ctl_t *, lsquic_stream_id_t);
244
245int
246lsquic_send_ctl_squeeze_sched (lsquic_send_ctl_t *);
247
248#define lsquic_send_ctl_maybe_squeeze_sched(ctl) (                  \
249    (ctl)->sc_n_scheduled && lsquic_send_ctl_squeeze_sched(ctl)     \
250)
251
252/* Same return value as for squeezing, but without actual squeezing. */
253int
254lsquic_send_ctl_have_delayed_packets (const lsquic_send_ctl_t *ctl);
255
256void
257lsquic_send_ctl_reset_packnos (lsquic_send_ctl_t *);
258
259void
260lsquic_send_ctl_ack_to_front (struct lsquic_send_ctl *, unsigned n_acks);
261
262#define lsquic_send_ctl_n_stop_waiting(ctl) \
263                                    (+(ctl)->sc_n_stop_waiting)
264
265#define lsquic_send_ctl_n_stop_waiting_reset(ctl) do {      \
266    (ctl)->sc_n_stop_waiting = 0;                           \
267} while (0)
268
269void
270lsquic_send_ctl_drop_scheduled (lsquic_send_ctl_t *);
271
272#define lsquic_send_ctl_tick_in(ctl, now) do {              \
273    if ((ctl)->sc_flags & SC_PACE)                          \
274    {                                                       \
275        (ctl)->sc_flags |= SC_SCHED_TICK;                   \
276        lsquic_pacer_tick_in(&(ctl)->sc_pacer, now);        \
277    }                                                       \
278    (ctl)->sc_flags &= ~SC_APP_LIMITED;                     \
279} while (0)
280
281#define lsquic_send_ctl_tick_out(ctl) do {                  \
282    if ((ctl)->sc_flags & SC_PACE)                          \
283        lsquic_pacer_tick_out(&(ctl)->sc_pacer);            \
284} while (0)
285
286#define lsquic_send_ctl_next_pacer_time(ctl) (              \
287    ((ctl)->sc_flags & SC_PACE)                             \
288        && lsquic_pacer_delayed(&(ctl)->sc_pacer)           \
289        ? lsquic_pacer_next_sched(&(ctl)->sc_pacer)         \
290        : 0 )
291
292enum packno_bits
293lsquic_send_ctl_packno_bits (lsquic_send_ctl_t *);
294
295int
296lsquic_send_ctl_schedule_buffered (lsquic_send_ctl_t *, enum buf_packet_type);
297
298#define lsquic_send_ctl_has_buffered(ctl) (                                 \
299    TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets)  \
300 || TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets  ))
301
302#define lsquic_send_ctl_has_buffered_high(ctl) (                            \
303    !TAILQ_EMPTY(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets))
304
305#define lsquic_send_ctl_invalidate_bpt_cache(ctl) do {      \
306    (ctl)->sc_cached_bpt.stream_id = UINT64_MAX;            \
307} while (0)
308
309#ifndef NDEBUG
310enum packno_bits
311lsquic_send_ctl_guess_packno_bits (struct lsquic_send_ctl *);
312
313int
314lsquic_send_ctl_schedule_stream_packets_immediately (struct lsquic_send_ctl *);
315
316enum buf_packet_type
317lsquic_send_ctl_determine_bpt (struct lsquic_send_ctl *,
318                                            const struct lsquic_stream *);
319
320enum packno_bits
321lsquic_send_ctl_calc_packno_bits (struct lsquic_send_ctl *);
322
323void
324lsquic_send_ctl_set_max_bpq_count (unsigned);
325#endif
326
327size_t
328lsquic_send_ctl_mem_used (const struct lsquic_send_ctl *);
329
330#define lsquic_send_ctl_set_buffer_stream_packets(ctl, b) do {  \
331    (ctl)->sc_flags &= ~SC_BUFFER_STREAM;                       \
332    (ctl)->sc_flags |= -!!(b) & SC_BUFFER_STREAM;               \
333} while (0)
334
335int
336lsquic_send_ctl_turn_on_fin (struct lsquic_send_ctl *,
337                             const struct lsquic_stream *);
338
339int
340lsquic_send_ctl_pacer_blocked (struct lsquic_send_ctl *);
341
342#define lsquic_send_ctl_incr_pack_sz(ctl, packet, delta) do {   \
343    (packet)->po_data_sz += (delta);                            \
344    if ((packet)->po_flags & PO_SCHED)                          \
345        (ctl)->sc_bytes_scheduled += (delta);                   \
346    lsquic_send_ctl_sanity_check(ctl);                          \
347} while (0)
348
349int
350lsquic_send_ctl_sched_is_blocked (struct lsquic_send_ctl *);
351
352void
353lsquic_send_ctl_verneg_done (struct lsquic_send_ctl *);
354
355int
356lsquic_send_ctl_retry (struct lsquic_send_ctl *, const unsigned char *, size_t);
357
358int
359lsquic_send_ctl_set_token (struct lsquic_send_ctl *,
360                const unsigned char *token, size_t token_sz);
361
362void
363lsquic_send_ctl_empty_pns (struct lsquic_send_ctl *, enum packnum_space);
364
365void
366lsquic_send_ctl_repath (struct lsquic_send_ctl *ctl,
367    const struct network_path *old, const struct network_path *new,
368    int keep_path_properties);
369
370void
371lsquic_send_ctl_resize (struct lsquic_send_ctl *);
372
373void
374lsquic_send_ctl_return_enc_data (struct lsquic_send_ctl *);
375
376#define lsquic_send_ctl_1rtt_acked(ctl) ((ctl)->sc_flags & SC_1RTT_ACKED)
377
378void
379lsquic_send_ctl_maybe_app_limited (struct lsquic_send_ctl *,
380                                            const struct network_path *);
381
382#define lsquic_send_ctl_do_ql_bits(ctl) do {                       \
383    (ctl)->sc_flags |= SC_QL_BITS;                                 \
384} while (0)
385
386void
387lsquic_send_ctl_cidlen_change (struct lsquic_send_ctl *,
388                                unsigned orig_cid_len, unsigned new_cid_len);
389
390void
391lsquic_send_ctl_begin_optack_detection (struct lsquic_send_ctl *);
392
393#define lsquic_send_ctl_n_unacked(ctl_) ((ctl_)->sc_n_in_flight_retx)
394
395void
396lsquic_send_ctl_path_validated (struct lsquic_send_ctl *);
397
398/* Has immediately sendable packets */
399#define lsquic_send_ctl_has_sendable(ctl_) \
400    (lsquic_send_ctl_n_scheduled(ctl_) > 0 \
401                && lsquic_send_ctl_next_packet_to_send_predict(ctl_))
402
403#define lsquic_send_ctl_in_recovery(ctl_) ((ctl_)->sc_largest_acked_packno \
404    && (ctl_)->sc_largest_acked_packno <= (ctl_)->sc_largest_sent_at_cutback)
405
406#define send_ctl_in_recovery lsquic_send_ctl_in_recovery
407
408int
409lsquic_send_ctl_can_send_probe (const struct lsquic_send_ctl *,
410                                            const struct network_path *);
411
412#define lsquic_send_ctl_ecn_turned_on(ctl_) ((ctl_)->sc_ecn != ECN_NOT_ECT)
413
414void
415lsquic_send_ctl_disable_ecn (struct lsquic_send_ctl *);
416
417struct send_ctl_state
418{
419    struct pacer        pacer;
420    struct ack_state    ack_state;
421    unsigned            buf_counts[BPT_OTHER_PRIO + 1];
422};
423
424void
425lsquic_send_ctl_snapshot (struct lsquic_send_ctl *, struct send_ctl_state *);
426
427void
428lsquic_send_ctl_rollback (struct lsquic_send_ctl *, struct send_ctl_state *,
429                                                const struct iovec *, size_t);
430
431#endif
432