lsquic_packet_out.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_packet_out.h -- Structure and routines dealing with packet_out
4 */
5
6#ifndef LSQUIC_PACKET_OUT_H
7#define LSQUIC_PACKET_OUT_H 1
8
9#include <sys/queue.h>
10
11struct malo;
12struct lsquic_conn;
13struct lsquic_engine_public;
14struct lsquic_mm;
15struct lsquic_stream;
16struct network_path;
17struct parse_funcs;
18struct bwp_state;
19
20/* Each frame_rec is associated with one packet_out.  packet_out can have
21 * zero or more frame_rec structures.  frame_rec keeps a pointer to a stream
22 * that has STREAM, CRYPTO, or RST_STREAM frames inside packet_out.
23 * `fe_frame_type' specifies the type of the frame; if this value is zero
24 * (this happens when a frame is elided), values of the other struct members
25 * are not valid.  `fe_off' indicates where inside packet_out->po_data the
26 * frame begins and `fe_len' is its length.
27 *
28 * We need this information for four reasons:
29 *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
30 *      frames are acknowledged.  This is to make sure that we do not exceed
31 *      maximum allowed number of streams.
32 *   2. When a packet is resubmitted, STREAM frames for a stream that has
33 *      been reset are not to be resubmitted.
34 *   3. A buffered packet may have to be split before it is scheduled (this
35 *      occurs if we guessed incorrectly the number of bytes required to
36 *      encode the packet number and the actual number would make packet
37 *      larger than the max).
38 *   4. A lost or scheduled packet may need to be resized (down) when path
39 *      changes or MTU is reduced due to an RTO.
40 *
41 * In IETF, all frames are recorded.  In gQUIC, only STREAM, RST_STREAM,
42 * ACK, and STOP_WAITING are recorded.  The latter two are done so that
43 * ACK-deleting code in send controller (see po_regen_sz) is the same for
44 * both QUIC versions.
45 */
46struct frame_rec {
47    union {
48        struct lsquic_stream   *stream;
49        uintptr_t               data;
50    }                        fe_u;
51#define fe_stream fe_u.stream
52    unsigned short           fe_off,
53                             fe_len;
54    enum quic_frame_type     fe_frame_type;
55};
56
57#define frec_taken(frec) ((frec)->fe_frame_type)
58
59struct frame_rec_arr {
60    TAILQ_ENTRY(frame_rec_arr)     next_stream_rec_arr;
61    struct frame_rec               frecs[
62      ( 64                              /* Efficient size for malo allocator */
63      - sizeof(TAILQ_ENTRY(frame_rec))  /* next_stream_rec_arr */
64      ) / sizeof(struct frame_rec)
65    ];
66};
67
68TAILQ_HEAD(frame_rec_arr_tailq, frame_rec_arr);
69
70
71typedef struct lsquic_packet_out
72{
73    /* `po_next' is used for packets_out, unacked_packets and expired_packets
74     * lists.
75     */
76    TAILQ_ENTRY(lsquic_packet_out)
77                       po_next;
78    lsquic_time_t      po_sent;       /* Time sent */
79    lsquic_packno_t    po_packno;
80    lsquic_packno_t    po_ack2ed;       /* If packet has ACK frame, value of
81                                         * largest acked in it.
82                                         */
83    struct lsquic_packet_out
84                      *po_loss_chain;   /* Circular linked list */
85
86    enum quic_ft_bit   po_frame_types;  /* Bitmask of QUIC_FRAME_* */
87    enum packet_out_flags {
88            /* TODO XXX Phase out PO_MINI in favor of a more specialized flag:
89             * we only need an indicator that a packet contains STREAM frames
90             * but no associated frecs.  This type of packets in only created
91             * by GQUIC mini conn.
92             */
93        PO_MINI     = (1 << 0),         /* Allocated by mini connection */
94        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
95        PO_SENT     = (1 << 2),         /* Packet has been sent (mini only) */
96        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
97        PO_FREC_ARR = (1 << 4),
98#define POBIT_SHIFT 5
99        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
100        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
101        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
102        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
103        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
104        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
105        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
106        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
107        PO_STREAM_END
108                    = (1 <<13),         /* STREAM frame reaches the end of the packet: no
109                                         * further writes are allowed.
110                                         */
111        PO_SCHED    = (1 <<14),         /* On scheduled queue */
112        PO_SENT_SZ  = (1 <<15),
113        PO_LONGHEAD = (1 <<16),
114#define POIPv6_SHIFT 20
115        PO_IPv6     = (1 <<20),         /* Set if pmi_allocate was passed is_ipv6=1,
116                                         *   otherwise unset.
117                                         */
118        PO_MTU_PROBE= (1 <<21),         /* Special loss and ACK rules apply */
119#define POPNS_SHIFT 22
120        PO_PNS_HSK  = (1 <<22),         /* PNS bits contain the value of the */
121        PO_PNS_APP  = (1 <<23),         /*   packet number space. */
122        PO_RETRY    = (1 <<24),         /* Retry packet */
123        PO_RETX     = (1 <<25),         /* Retransmitted packet: don't append to it */
124        PO_POISON   = (1 <<26),         /* Used to detect opt-ACK attack */
125        PO_LOSS_REC = (1 <<27),         /* This structure is a loss record */
126        /* Only one of PO_SCHED, PO_UNACKED, or PO_LOST can be set.  If pressed
127         * for room in the enum, we can switch to using two bits to represent
128         * this information.
129         */
130        PO_UNACKED  = (1 <<28),         /* On unacked queue */
131        PO_LOST     = (1 <<29),         /* On lost queue */
132#define POSPIN_SHIFT 30
133        PO_SPIN_BIT = (1 <<30),         /* Value of the spin bit */
134    }                  po_flags;
135    unsigned short     po_data_sz;      /* Number of usable bytes in data */
136    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
137    unsigned short     po_sent_sz;      /* If PO_SENT_SZ is set, real size of sent buffer. */
138    /* TODO Revisit po_regen_sz once gQUIC is dropped.  Now that all frames
139     * are recorded, we have more flexibility where to place ACK frames; they
140     * no longer really have to be at the beginning of the packet, since we
141     * can locate them.
142     */
143    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
144                                         * of data containing bytes that are
145                                         * not to be retransmitted, e.g. ACK
146                                         * frames.
147                                         */
148    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
149    unsigned short     po_token_len;
150    enum header_type   po_header_type:8;
151    unsigned char      po_dcid_len;     /* If PO_ENCRYPTED is set */
152    enum {
153        POL_GQUIC    = 1 << 0,         /* Used for logging */
154#define POLEV_SHIFT 1
155        POL_ELBIT_0  = 1 << 1,         /* EL bits encode the crypto level. */
156        POL_ELBIT_1  = 1 << 2,
157#define POKP_SHIFT 3
158        POL_KEY_PHASE= 1 << 3,
159#define POECN_SHIFT 4
160        POL_ECNBIT_0 = 1 << 4,
161        POL_ECNBIT_1 = 1 << 5,
162        POL_LOG_QL_BITS = 1 << 6,
163        POL_SQUARE_BIT = 1 << 7,
164        POL_LOSS_BIT = 1 << 8,
165#ifndef NDEBUG
166        POL_HEADER_PROT = 1 << 9,       /* Header protection applied */
167#endif
168        POL_LIMITED     = 1 << 10,      /* Used to credit sc_next_limit if needed. */
169        POL_FACKED   = 1 << 11,         /* Lost due to FACK check */
170    }                  po_lflags:16;
171    unsigned char     *po_data;
172
173    /* A lot of packets contain only one frame.  Thus, `one' is used first.
174     * If this is not enough, any number of frame_rec_arr structures can be
175     * allocated to handle more frame records.
176     */
177    union {
178        struct frame_rec               one;
179        struct frame_rec_arr_tailq     arr;
180    }                  po_frecs;
181
182    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
183     * data.
184     */
185    unsigned char     *po_enc_data;
186
187    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
188    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
189    const struct network_path
190                      *po_path;
191#define po_token po_nonce
192    struct bwp_state  *po_bwp_state;
193} lsquic_packet_out_t;
194
195/* This is to make sure these bit names are not used, they are only for
196 * convenience in gdb output.
197 */
198#define PO_PNS_HSK
199#define PO_PNS_APP
200
201/* The size of lsquic_packet_out_t could be further reduced:
202 *
203 * po_ver_tag could be encoded as a few bits representing enum lsquic_version
204 * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
205 */
206
207#define lsquic_packet_out_avail(p) ((unsigned short) \
208                                        ((p)->po_n_alloc - (p)->po_data_sz))
209
210#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
211
212#define lsquic_packet_out_set_packno_bits(p, b) do {                    \
213    (p)->po_flags &= ~(0x3 << POBIT_SHIFT);                             \
214    (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT;                        \
215} while (0)
216
217#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1))
218
219#define lsquic_packet_out_set_ipv6(p, b) do {                           \
220    (p)->po_flags &= ~(1 << POIPv6_SHIFT);                              \
221    (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT;                         \
222} while (0)
223
224#define lsquic_packet_out_spin_bit(p) (((p)->po_flags & PO_SPIN_BIT) > 0)
225#define lsquic_packet_out_square_bit(p) (((p)->po_lflags & POL_SQUARE_BIT) > 0)
226#define lsquic_packet_out_loss_bit(p) (((p)->po_lflags & POL_LOSS_BIT) > 0)
227
228#define lsquic_packet_out_set_spin_bit(p, b) do {                       \
229    (p)->po_flags &= ~PO_SPIN_BIT;                                      \
230    (p)->po_flags |= ((b) & 1) << POSPIN_SHIFT;                         \
231} while (0)
232
233#define lsquic_po_header_length(lconn, po_flags, dcid_len, header_type) (   \
234    lconn->cn_pf->pf_packout_max_header_size(lconn, po_flags, dcid_len,     \
235                                             header_type))                  \
236
237#define lsquic_packet_out_total_sz(lconn, p) (\
238    (lconn)->cn_pf->pf_packout_size(lconn, p))
239
240#if __GNUC__
241#if LSQUIC_EXTRA_CHECKS
242#define lsquic_packet_out_sent_sz(lconn, p) (                               \
243        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
244        (assert(((p)->po_flags & PO_HELLO /* Avoid client DCID change */)   \
245            || (p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)),    \
246            (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p))
247#   else
248#define lsquic_packet_out_sent_sz(lconn, p) (                               \
249        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
250        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
251#endif
252#else
253#   define lsquic_packet_out_sent_sz(lconn, p) (                            \
254        (p)->po_flags & PO_SENT_SZ ?                                        \
255        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
256#endif
257
258#define lsquic_packet_out_verneg(p) \
259    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_VERNEG))
260
261#define lsquic_packet_out_pubres(p) \
262    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) ==  PO_NOENCRYPT           )
263
264#define lsquic_packet_out_retry(p) \
265    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_RETRY) )
266
267#define lsquic_packet_out_set_enc_level(p, level) do {                      \
268    (p)->po_lflags &= ~(3 << POLEV_SHIFT);                                  \
269    (p)->po_lflags |= level << POLEV_SHIFT;                                 \
270} while (0)
271
272#define lsquic_packet_out_enc_level(p)  (((p)->po_lflags >> POLEV_SHIFT) & 3)
273
274#define lsquic_packet_out_set_kp(p, kp) do {                                \
275    (p)->po_lflags &= ~(1 << POKP_SHIFT);                                   \
276    (p)->po_lflags |= kp << POKP_SHIFT;                                     \
277} while (0)
278
279#define lsquic_packet_out_kp(p)  (((p)->po_lflags >> POKP_SHIFT) & 1)
280
281#define lsquic_packet_out_set_pns(p, pns) do {                              \
282    (p)->po_flags &= ~(3 << POPNS_SHIFT);                                   \
283    (p)->po_flags |= pns << POPNS_SHIFT;                                    \
284} while (0)
285
286#define lsquic_packet_out_pns(p)  (((p)->po_flags >> POPNS_SHIFT) & 3)
287
288#define lsquic_packet_out_set_ecn(p, ecn) do {                              \
289    (p)->po_lflags &= ~(3 << POECN_SHIFT);                                  \
290    (p)->po_lflags |= ecn << POECN_SHIFT;                                   \
291} while (0)
292
293#define lsquic_packet_out_ecn(p)  (((p)->po_lflags >> POECN_SHIFT) & 3)
294
295struct packet_out_frec_iter {
296    lsquic_packet_out_t         *packet_out;
297    struct frame_rec_arr        *cur_frec_arr;
298    unsigned                     frec_idx;
299    int                          impl_idx;
300};
301
302
303struct frame_rec *
304lsquic_pofi_first (struct packet_out_frec_iter *pofi, lsquic_packet_out_t *);
305
306struct frame_rec *
307lsquic_pofi_next (struct packet_out_frec_iter *pofi);
308
309lsquic_packet_out_t *
310lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
311                       const struct lsquic_conn *, enum packno_bits,
312                       const lsquic_ver_tag_t *, const unsigned char *nonce,
313                       const struct network_path *, enum header_type);
314
315void
316lsquic_packet_out_destroy (lsquic_packet_out_t *,
317                        struct lsquic_engine_public *, void *peer_ctx);
318
319int
320lsquic_packet_out_add_frame (struct lsquic_packet_out *,
321                  struct lsquic_mm *, uintptr_t data, enum quic_frame_type,
322                  unsigned short off, unsigned short len);
323
324int
325lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
326                              struct lsquic_mm *mm,
327                              struct lsquic_stream *new_stream,
328                              enum quic_frame_type,
329                              unsigned short off, unsigned short len);
330
331unsigned
332lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *,
333                                                    lsquic_stream_id_t);
334
335void
336lsquic_packet_out_chop_regen (lsquic_packet_out_t *);
337
338void
339lsquic_packet_out_ack_streams (struct lsquic_packet_out *);
340
341void
342lsquic_packet_out_zero_pad (struct lsquic_packet_out *);
343
344size_t
345lsquic_packet_out_mem_used (const struct lsquic_packet_out *);
346
347int
348lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
349                   const struct parse_funcs *, const struct lsquic_stream *);
350
351int
352lsquic_packet_out_equal_dcids (const struct lsquic_packet_out *,
353                               const struct lsquic_packet_out *);
354
355void
356lsquic_packet_out_pad_over (struct lsquic_packet_out *packet_out,
357                                                enum quic_ft_bit frame_types);
358
359#endif
360