lsquic_packet_out.h revision b8fa6195
1/* Copyright (c) 2017 - 2020 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    enum {
152        POL_GQUIC    = 1 << 0,         /* Used for logging */
153#define POLEV_SHIFT 1
154        POL_ELBIT_0  = 1 << 1,         /* EL bits encode the crypto level. */
155        POL_ELBIT_1  = 1 << 2,
156#define POKP_SHIFT 3
157        POL_KEY_PHASE= 1 << 3,
158#define POECN_SHIFT 4
159        POL_ECNBIT_0 = 1 << 4,
160        POL_ECNBIT_1 = 1 << 5,
161        POL_LOG_QL_BITS = 1 << 6,
162        POL_SQUARE_BIT = 1 << 7,
163        POL_LOSS_BIT = 1 << 8,
164#ifndef NDEBUG
165        POL_HEADER_PROT = 1 << 9,       /* Header protection applied */
166#endif
167        POL_LIMITED     = 1 << 10,      /* Used to credit sc_next_limit if needed. */
168    }                  po_lflags:16;
169    unsigned char     *po_data;
170
171    /* A lot of packets contain only one frame.  Thus, `one' is used first.
172     * If this is not enough, any number of frame_rec_arr structures can be
173     * allocated to handle more frame records.
174     */
175    union {
176        struct frame_rec               one;
177        struct frame_rec_arr_tailq     arr;
178    }                  po_frecs;
179
180    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
181     * data.
182     */
183    unsigned char     *po_enc_data;
184
185    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
186    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
187    const struct network_path
188                      *po_path;
189#define po_token po_nonce
190    struct bwp_state  *po_bwp_state;
191} lsquic_packet_out_t;
192
193/* This is to make sure these bit names are not used, they are only for
194 * convenience in gdb output.
195 */
196#define PO_PNS_HSK
197#define PO_PNS_APP
198
199/* The size of lsquic_packet_out_t could be further reduced:
200 *
201 * po_ver_tag could be encoded as a few bits representing enum lsquic_version
202 * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
203 */
204
205#define lsquic_packet_out_avail(p) ((unsigned short) \
206                                        ((p)->po_n_alloc - (p)->po_data_sz))
207
208#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
209
210#define lsquic_packet_out_set_packno_bits(p, b) do {                    \
211    (p)->po_flags &= ~(0x3 << POBIT_SHIFT);                             \
212    (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT;                        \
213} while (0)
214
215#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1))
216
217#define lsquic_packet_out_set_ipv6(p, b) do {                           \
218    (p)->po_flags &= ~(1 << POIPv6_SHIFT);                              \
219    (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT;                         \
220} while (0)
221
222#define lsquic_packet_out_spin_bit(p) (((p)->po_flags & PO_SPIN_BIT) > 0)
223#define lsquic_packet_out_square_bit(p) (((p)->po_lflags & POL_SQUARE_BIT) > 0)
224#define lsquic_packet_out_loss_bit(p) (((p)->po_lflags & POL_LOSS_BIT) > 0)
225
226#define lsquic_packet_out_set_spin_bit(p, b) do {                       \
227    (p)->po_flags &= ~PO_SPIN_BIT;                                      \
228    (p)->po_flags |= ((b) & 1) << POSPIN_SHIFT;                         \
229} while (0)
230
231#define lsquic_po_header_length(lconn, po_flags, dcid_len) ( \
232    lconn->cn_pf->pf_packout_max_header_size(lconn, po_flags, dcid_len))
233
234#define lsquic_packet_out_total_sz(lconn, p) (\
235    (lconn)->cn_pf->pf_packout_size(lconn, p))
236
237#if __GNUC__
238#if LSQUIC_EXTRA_CHECKS
239#define lsquic_packet_out_sent_sz(lconn, p) (                               \
240        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
241        (assert(((p)->po_flags & PO_HELLO /* Avoid client DCID change */)   \
242            || (p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)),    \
243            (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p))
244#   else
245#define lsquic_packet_out_sent_sz(lconn, p) (                               \
246        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
247        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
248#endif
249#else
250#   define lsquic_packet_out_sent_sz(lconn, p) (                            \
251        (p)->po_flags & PO_SENT_SZ ?                                        \
252        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
253#endif
254
255#define lsquic_packet_out_verneg(p) \
256    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_VERNEG))
257
258#define lsquic_packet_out_pubres(p) \
259    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) ==  PO_NOENCRYPT           )
260
261#define lsquic_packet_out_retry(p) \
262    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_RETRY) )
263
264#define lsquic_packet_out_set_enc_level(p, level) do {                      \
265    (p)->po_lflags &= ~(3 << POLEV_SHIFT);                                  \
266    (p)->po_lflags |= level << POLEV_SHIFT;                                 \
267} while (0)
268
269#define lsquic_packet_out_enc_level(p)  (((p)->po_lflags >> POLEV_SHIFT) & 3)
270
271#define lsquic_packet_out_set_kp(p, kp) do {                                \
272    (p)->po_lflags &= ~(1 << POKP_SHIFT);                                   \
273    (p)->po_lflags |= kp << POKP_SHIFT;                                     \
274} while (0)
275
276#define lsquic_packet_out_kp(p)  (((p)->po_lflags >> POKP_SHIFT) & 1)
277
278#define lsquic_packet_out_set_pns(p, pns) do {                              \
279    (p)->po_flags &= ~(3 << POPNS_SHIFT);                                   \
280    (p)->po_flags |= pns << POPNS_SHIFT;                                    \
281} while (0)
282
283#define lsquic_packet_out_pns(p)  (((p)->po_flags >> POPNS_SHIFT) & 3)
284
285#define lsquic_packet_out_set_ecn(p, ecn) do {                              \
286    (p)->po_lflags &= ~(3 << POECN_SHIFT);                                  \
287    (p)->po_lflags |= ecn << POECN_SHIFT;                                   \
288} while (0)
289
290#define lsquic_packet_out_ecn(p)  (((p)->po_lflags >> POECN_SHIFT) & 3)
291
292struct packet_out_frec_iter {
293    lsquic_packet_out_t         *packet_out;
294    struct frame_rec_arr        *cur_frec_arr;
295    unsigned                     frec_idx;
296    int                          impl_idx;
297};
298
299
300struct frame_rec *
301lsquic_pofi_first (struct packet_out_frec_iter *pofi, lsquic_packet_out_t *);
302
303struct frame_rec *
304lsquic_pofi_next (struct packet_out_frec_iter *pofi);
305
306lsquic_packet_out_t *
307lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
308                       const struct lsquic_conn *, enum packno_bits,
309                       const lsquic_ver_tag_t *, const unsigned char *nonce,
310                       const struct network_path *);
311
312void
313lsquic_packet_out_destroy (lsquic_packet_out_t *,
314                        struct lsquic_engine_public *, void *peer_ctx);
315
316int
317lsquic_packet_out_add_frame (struct lsquic_packet_out *,
318                  struct lsquic_mm *, uintptr_t data, enum quic_frame_type,
319                  unsigned short off, unsigned short len);
320
321int
322lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
323                              struct lsquic_mm *mm,
324                              struct lsquic_stream *new_stream,
325                              enum quic_frame_type,
326                              unsigned short off, unsigned short len);
327
328unsigned
329lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *,
330                                                    lsquic_stream_id_t);
331
332void
333lsquic_packet_out_chop_regen (lsquic_packet_out_t *);
334
335void
336lsquic_packet_out_ack_streams (struct lsquic_packet_out *);
337
338void
339lsquic_packet_out_zero_pad (struct lsquic_packet_out *);
340
341size_t
342lsquic_packet_out_mem_used (const struct lsquic_packet_out *);
343
344int
345lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
346                   const struct parse_funcs *, const struct lsquic_stream *);
347
348#endif
349