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