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