lsquic_send_ctl.h revision 7d09751d
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#define lsquic_send_ctl_sanity_check(ctl) do {                      \
184    if ((ctl)->sc_flags & SC_SANITY_CHECK)                          \
185        lsquic_send_ctl_do_sanity_check(ctl);                       \
186} while (0)
187
188int
189lsquic_send_ctl_have_outgoing_stream_frames (const lsquic_send_ctl_t *);
190
191int
192lsquic_send_ctl_have_outgoing_retx_frames (const lsquic_send_ctl_t *);
193
194struct lsquic_packet_out *
195lsquic_send_ctl_last_scheduled (struct lsquic_send_ctl *, enum packnum_space,
196                                            const struct network_path *, int);
197
198struct lsquic_packet_out *
199lsquic_send_ctl_new_packet_out (lsquic_send_ctl_t *, unsigned,
200                            enum packnum_space, const struct network_path *);
201
202struct lsquic_packet_out *
203lsquic_send_ctl_get_writeable_packet (lsquic_send_ctl_t *, enum packnum_space,
204      unsigned need_at_least, const struct network_path *, int, int *is_err);
205
206struct lsquic_packet_out *
207lsquic_send_ctl_get_packet_for_stream (lsquic_send_ctl_t *,
208                    unsigned need_at_least, const struct network_path *,
209                    const struct lsquic_stream *);
210
211struct lsquic_packet_out *
212lsquic_send_ctl_get_packet_for_crypto (struct lsquic_send_ctl *ctl,
213    unsigned need_at_least, enum packnum_space, const struct network_path *);
214
215unsigned
216lsquic_send_ctl_reschedule_packets (lsquic_send_ctl_t *);
217
218#define lsquic_send_ctl_lost_ack(ctl) \
219    (((ctl)->sc_flags & (SC_LOST_ACK_INIT|SC_LOST_ACK_HSK|SC_LOST_ACK_APP)) \
220                                                        >> SCBIT_LOST_ACK_SHIFT)
221
222#define lsquic_send_ctl_scheduled_ack(ctl, pns, acked) do {         \
223    (ctl)->sc_flags &= ~(SC_LOST_ACK_INIT << pns);                  \
224    if (PNS_APP == pns)                                             \
225        (ctl)->sc_largest_acked = acked;                            \
226} while (0)
227
228void
229lsquic_send_ctl_set_tcid0 (lsquic_send_ctl_t *, int);
230
231#define lsquic_send_ctl_turn_nstp_on(ctl) ((ctl)->sc_flags |= SC_NSTP)
232
233void
234lsquic_send_ctl_elide_stream_frames (lsquic_send_ctl_t *, lsquic_stream_id_t);
235
236int
237lsquic_send_ctl_squeeze_sched (lsquic_send_ctl_t *);
238
239#define lsquic_send_ctl_maybe_squeeze_sched(ctl) (                  \
240    (ctl)->sc_n_scheduled && lsquic_send_ctl_squeeze_sched(ctl)     \
241)
242
243/* Same return value as for squeezing, but without actual squeezing. */
244int
245lsquic_send_ctl_have_delayed_packets (const lsquic_send_ctl_t *ctl);
246
247void
248lsquic_send_ctl_reset_packnos (lsquic_send_ctl_t *);
249
250void
251lsquic_send_ctl_ack_to_front (struct lsquic_send_ctl *, unsigned n_acks);
252
253#define lsquic_send_ctl_n_stop_waiting(ctl) \
254                                    (+(ctl)->sc_n_stop_waiting)
255
256#define lsquic_send_ctl_n_stop_waiting_reset(ctl) do {      \
257    (ctl)->sc_n_stop_waiting = 0;                           \
258} while (0)
259
260void
261lsquic_send_ctl_drop_scheduled (lsquic_send_ctl_t *);
262
263#define lsquic_send_ctl_tick_in(ctl, now) do {              \
264    if ((ctl)->sc_flags & SC_PACE)                          \
265    {                                                       \
266        (ctl)->sc_flags |= SC_SCHED_TICK;                   \
267        pacer_tick_in(&(ctl)->sc_pacer, now);               \
268    }                                                       \
269    (ctl)->sc_flags &= ~SC_APP_LIMITED;                     \
270} while (0)
271
272#define lsquic_send_ctl_tick_out(ctl) do {                  \
273    if ((ctl)->sc_flags & SC_PACE)                          \
274        pacer_tick_out(&(ctl)->sc_pacer);                   \
275} while (0)
276
277#define lsquic_send_ctl_next_pacer_time(ctl) (              \
278    ((ctl)->sc_flags & SC_PACE)                             \
279        && pacer_delayed(&(ctl)->sc_pacer)                  \
280        ? pacer_next_sched(&(ctl)->sc_pacer)                \
281        : 0 )
282
283enum packno_bits
284lsquic_send_ctl_packno_bits (lsquic_send_ctl_t *);
285
286int
287lsquic_send_ctl_schedule_buffered (lsquic_send_ctl_t *, enum buf_packet_type);
288
289#define lsquic_send_ctl_has_buffered(ctl) (                                 \
290    TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets)  \
291 || TAILQ_FIRST(&(ctl)->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets  ))
292
293#define lsquic_send_ctl_has_buffered_high(ctl) (                            \
294    !TAILQ_EMPTY(&(ctl)->sc_buffered_packets[BPT_HIGHEST_PRIO].bpq_packets))
295
296#define lsquic_send_ctl_invalidate_bpt_cache(ctl) do {      \
297    (ctl)->sc_cached_bpt.stream_id = UINT64_MAX;            \
298} while (0)
299
300#ifndef NDEBUG
301enum packno_bits
302lsquic_send_ctl_guess_packno_bits (struct lsquic_send_ctl *);
303
304int
305lsquic_send_ctl_schedule_stream_packets_immediately (struct lsquic_send_ctl *);
306
307enum buf_packet_type
308lsquic_send_ctl_determine_bpt (struct lsquic_send_ctl *,
309                                            const struct lsquic_stream *);
310
311enum packno_bits
312lsquic_send_ctl_calc_packno_bits (struct lsquic_send_ctl *);
313
314void
315lsquic_send_ctl_set_max_bpq_count (unsigned);
316#endif
317
318size_t
319lsquic_send_ctl_mem_used (const struct lsquic_send_ctl *);
320
321#define lsquic_send_ctl_set_buffer_stream_packets(ctl, b) do {  \
322    (ctl)->sc_flags &= ~SC_BUFFER_STREAM;                       \
323    (ctl)->sc_flags |= -!!(b) & SC_BUFFER_STREAM;               \
324} while (0)
325
326int
327lsquic_send_ctl_turn_on_fin (struct lsquic_send_ctl *,
328                             const struct lsquic_stream *);
329
330int
331lsquic_send_ctl_pacer_blocked (struct lsquic_send_ctl *);
332
333#define lsquic_send_ctl_incr_pack_sz(ctl, packet, delta) do {   \
334    (packet)->po_data_sz += (delta);                            \
335    if ((packet)->po_flags & PO_SCHED)                          \
336        (ctl)->sc_bytes_scheduled += (delta);                   \
337    lsquic_send_ctl_sanity_check(ctl);                          \
338} while (0)
339
340int
341lsquic_send_ctl_sched_is_blocked (struct lsquic_send_ctl *);
342
343void
344lsquic_send_ctl_verneg_done (struct lsquic_send_ctl *);
345
346int
347lsquic_send_ctl_retry (struct lsquic_send_ctl *, const unsigned char *, size_t);
348
349int
350lsquic_send_ctl_set_token (struct lsquic_send_ctl *,
351                const unsigned char *token, size_t token_sz);
352
353void
354lsquic_send_ctl_empty_pns (struct lsquic_send_ctl *, enum packnum_space);
355
356void
357lsquic_send_ctl_repath (struct lsquic_send_ctl *, struct network_path *old,
358                                struct network_path *new);
359
360void
361lsquic_send_ctl_return_enc_data (struct lsquic_send_ctl *);
362
363#define lsquic_send_ctl_1rtt_acked(ctl) ((ctl)->sc_flags & SC_1RTT_ACKED)
364
365void
366lsquic_send_ctl_maybe_app_limited (struct lsquic_send_ctl *,
367                                            const struct network_path *);
368
369#define lsquic_send_ctl_do_ql_bits(ctl) do {                       \
370    (ctl)->sc_flags |= SC_QL_BITS;                                 \
371} while (0)
372
373void
374lsquic_send_ctl_cidlen_change (struct lsquic_send_ctl *,
375                                unsigned orig_cid_len, unsigned new_cid_len);
376
377void
378lsquic_send_ctl_begin_optack_detection (struct lsquic_send_ctl *);
379
380#endif
381