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