lsquic_packet_out.h revision 1e75f938
1/* Copyright (c) 2017 - 2018 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 parse_funcs;
17
18/* Each stream_rec is associated with one packet_out.  packet_out can have
19 * zero or more stream_rec structures.  stream_rec keeps a pointer to a stream
20 * that has STREAM or RST_STREAM frames inside packet_out.  `sr_frame_types'
21 * is a bitmask that records which of these two frames are in the packet.
22 * If this value is zero, values of the other struct members are not valid.
23 * `sr_off' indicates where inside packet_out->po_data STREAM frame begins
24 * and `sr_len' is its length.  These values are not kept for RST_STREAM
25 * frames.
26 *
27 * We need this information for three reasons:
28 *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
29 *      frames are acknowledged.  This is to make sure that we do not exceed
30 *      maximum allowed number of streams.
31 *   2. When a packet is resubmitted, STREAM frames for a stream that has
32 *      been reset are not to be resubmitted.
33 *   3. A buffered packet may have to be split before it is scheduled (this
34 *      occurs if we guessed incorrectly the number of bytes required to
35 *      encode the packet number and the actual number would make packet
36 *      larger than the max).
37 *
38 */
39struct stream_rec {
40    struct lsquic_stream    *sr_stream;
41    unsigned short           sr_off,
42                             sr_len;
43    enum quic_ft_bit         sr_frame_types:16;
44};
45
46#define srec_taken(srec) ((srec)->sr_frame_types)
47
48struct stream_rec_arr {
49    TAILQ_ENTRY(stream_rec_arr)     next_stream_rec_arr;
50    struct stream_rec               srecs[
51      ( 64                              /* Efficient size for malo allocator */
52      - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */
53      ) / sizeof(struct stream_rec)
54    ];
55};
56
57TAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr);
58
59typedef struct lsquic_packet_out
60{
61    /* `po_next' is used for packets_out, unacked_packets and expired_packets
62     * lists.
63     */
64    TAILQ_ENTRY(lsquic_packet_out)
65                       po_next;
66    lsquic_time_t      po_sent;       /* Time sent */
67    lsquic_packno_t    po_packno;
68
69    enum packet_out_flags {
70        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
71        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
72        PO_SREC_ARR = (1 << 4),
73#define POBIT_SHIFT 5
74        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
75        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
76        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
77        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
78        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
79        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
80        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
81        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
82        PO_STREAM_END
83                    = (1 <<13),         /* STREAM frame reaches the end of the packet: no
84                                         * further writes are allowed.
85                                         */
86        PO_SCHED    = (1 <<14),         /* On scheduled queue */
87        PO_SENT_SZ  = (1 <<15),
88        PO_LONGHEAD = (1 <<16),
89        PO_GQUIC    = (1 <<17),         /* Used for logging */
90#define POLEV_SHIFT 18
91        PO_BITS_2   = (1 <<18),         /* PO_BITS_2 and PO_BITS_3 encode the */
92        PO_BITS_3   = (1 <<19),         /*   crypto level.  Used for logging. */
93#define POIPv6_SHIFT 20
94        PO_IPv6     = (1 <<20),         /* Set if pmi_allocate was passed is_ipv6=1,
95                                         *   otherwise unset.
96                                         */
97    }                  po_flags;
98    enum quic_ft_bit   po_frame_types:16; /* Bitmask of QUIC_FRAME_* */
99    unsigned short     po_data_sz;      /* Number of usable bytes in data */
100    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
101    unsigned short     po_sent_sz;      /* If PO_SENT_SZ is set, real size of sent buffer. */
102    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
103                                         * of data containing bytes that are
104                                         * not to be retransmitted, e.g. ACK
105                                         * frames.
106                                         */
107    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
108    enum header_type   po_header_type:8;
109    unsigned char     *po_data;
110    lsquic_packno_t    po_ack2ed;       /* If packet has ACK frame, value of
111                                         * largest acked in it.
112                                         */
113
114    /* A lot of packets contain data belonging to only one stream.  Thus,
115     * `one' is used first.  If this is not enough, any number of
116     * stream_rec_arr structures can be allocated to handle more stream
117     * records.
118     */
119    union {
120        struct stream_rec               one;
121        struct stream_rec_arr_tailq     arr;
122    }                  po_srecs;
123
124    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
125     * data.
126     */
127    unsigned char     *po_enc_data;
128
129    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
130    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
131} lsquic_packet_out_t;
132
133/* The size of lsquic_packet_out_t could be further reduced:
134 *
135 * po_ver_tag could be encoded as a few bits representing enum lsquic_version
136 * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
137 */
138
139#define lsquic_packet_out_avail(p) ((unsigned short) \
140                                        ((p)->po_n_alloc - (p)->po_data_sz))
141
142#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
143
144#define lsquic_packet_out_set_packno_bits(p, b) do {                    \
145    (p)->po_flags &= ~(0x3 << POBIT_SHIFT);                             \
146    (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT;                        \
147} while (0)
148
149#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1))
150
151#define lsquic_packet_out_set_ipv6(p, b) do {                           \
152    (p)->po_flags &= ~(1 << POIPv6_SHIFT);                              \
153    (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT;                         \
154} while (0)
155
156#define lsquic_po_header_length(lconn, po_flags) ( \
157    lconn->cn_pf->pf_packout_header_size(lconn, po_flags))
158
159#define lsquic_packet_out_total_sz(lconn, p) (\
160    lconn->cn_pf->pf_packout_size(lconn, p))
161
162#if __GNUC__
163#if LSQUIC_EXTRA_CHECKS
164#define lsquic_packet_out_sent_sz(lconn, p) (                               \
165        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
166        (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)),   \
167            (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p))
168#   else
169#define lsquic_packet_out_sent_sz(lconn, p) (                               \
170        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
171        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
172#endif
173#else
174#   define lsquic_packet_out_sent_sz(lconn, p) (                            \
175        (p)->po_flags & PO_SENT_SZ ?                                        \
176        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
177#endif
178
179#define lsquic_packet_out_verneg(p) \
180    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
181
182#define lsquic_packet_out_pubres(p) \
183    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
184
185#define lsquic_packet_out_set_enc_level(p, level) do {                      \
186    (p)->po_flags &= ~(3 << POLEV_SHIFT);                                   \
187    (p)->po_flags |= level << POLEV_SHIFT;                                  \
188} while (0)
189
190#define lsquic_packet_out_enc_level(p)  (((p)->po_flags >> POLEV_SHIFT) & 3)
191
192struct packet_out_srec_iter {
193    lsquic_packet_out_t         *packet_out;
194    struct stream_rec_arr       *cur_srec_arr;
195    unsigned                     srec_idx;
196    int                          impl_idx;
197};
198
199struct stream_rec *
200posi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
201
202struct stream_rec *
203posi_next (struct packet_out_srec_iter *posi);
204
205lsquic_packet_out_t *
206lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
207                       const struct lsquic_conn *, enum lsquic_packno_bits,
208                       const lsquic_ver_tag_t *, const unsigned char *nonce);
209
210void
211lsquic_packet_out_destroy (lsquic_packet_out_t *,
212                        struct lsquic_engine_public *, void *peer_ctx);
213
214int
215lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
216                              struct lsquic_mm *mm,
217                              struct lsquic_stream *new_stream,
218                              enum QUIC_FRAME_TYPE,
219                              unsigned short off, unsigned short len);
220
221unsigned
222lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t);
223
224int
225lsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *,
226    lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes);
227
228void
229lsquic_packet_out_chop_regen (lsquic_packet_out_t *);
230
231int
232lsquic_packet_out_has_frame (struct lsquic_packet_out *,
233                        const struct lsquic_stream *, enum QUIC_FRAME_TYPE);
234
235int
236lsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *);
237
238void
239lsquic_packet_out_ack_streams (struct lsquic_packet_out *);
240
241void
242lsquic_packet_out_zero_pad (struct lsquic_packet_out *);
243
244size_t
245lsquic_packet_out_mem_used (const struct lsquic_packet_out *);
246
247int
248lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
249                   const struct parse_funcs *, const struct lsquic_stream *);
250
251#endif
252