lsquic_packet_out.h revision c51ce338
1/* Copyright (c) 2017 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    short                    sr_frame_types;
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    /* A lot of packets contain data belonging to only one stream.  Thus,
69     * `one' is used first.  If this is not enough, any number of
70     * stream_rec_arr structures can be allocated to handle more stream
71     * records.
72     */
73    union {
74        struct stream_rec               one;
75        struct stream_rec_arr_tailq     arr;
76    }                  po_srecs;
77
78    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
79     * data.
80     */
81    unsigned char     *po_enc_data;
82
83    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
84    short              po_frame_types;  /* Bitmask of QUIC_FRAME_* */
85    unsigned short     po_data_sz;      /* Number of usable bytes in data */
86    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
87    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
88                                         * of data containing bytes that are
89                                         * not to be retransmitted, e.g. ACK
90                                         * frames.
91                                         */
92    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
93    enum packet_out_flags {
94        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
95        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
96        PO_SREC_ARR = (1 << 4),
97#define POBIT_SHIFT 5
98        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
99        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
100        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
101        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
102        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
103        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
104        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
105        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
106    }                  po_flags:16;
107    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
108    unsigned char     *po_data;
109} lsquic_packet_out_t;
110
111/* The size of lsquic_packet_out_t could be further reduced:
112 *
113 * po_ver_tag could be encoded as a few bits representing enum lsquic_version
114 * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
115 */
116
117#define lsquic_packet_out_avail(p) ((unsigned short) \
118                                        ((p)->po_n_alloc - (p)->po_data_sz))
119
120#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
121
122/* XXX This will need to be made into a method for Q041 */
123#define lsquic_po_header_length(po_flags) (                                 \
124    1                                                   /* Type */          \
125  + (!!((po_flags) & PO_CONN_ID) << 3)                  /* Connection ID */ \
126  + (!!((po_flags) & PO_VERSION) << 2)                  /* Version */       \
127  + (!!((po_flags) & PO_NONCE)   << 5)                  /* Nonce */         \
128  + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3)  /* Packet number */ \
129)
130
131#define lsquic_packet_out_verneg(p) \
132    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
133
134#define lsquic_packet_out_pubres(p) \
135    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
136
137struct packet_out_srec_iter {
138    lsquic_packet_out_t         *packet_out;
139    struct stream_rec_arr       *cur_srec_arr;
140    unsigned                     srec_idx;
141    int                          impl_idx;
142};
143
144struct stream_rec *
145posi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
146
147struct stream_rec *
148posi_next (struct packet_out_srec_iter *posi);
149
150lsquic_packet_out_t *
151lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
152                       unsigned short size, enum lsquic_packno_bits,
153                       const lsquic_ver_tag_t *, const unsigned char *nonce);
154
155void
156lsquic_packet_out_destroy (lsquic_packet_out_t *,
157                                        struct lsquic_engine_public *);
158
159int
160lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
161                              struct lsquic_mm *mm,
162                              struct lsquic_stream *new_stream,
163                              enum QUIC_FRAME_TYPE,
164                              unsigned short off, unsigned short len);
165
166void
167lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t);
168
169int
170lsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *,
171    lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes);
172
173void
174lsquic_packet_out_chop_regen (lsquic_packet_out_t *);
175
176int
177lsquic_packet_out_has_frame (struct lsquic_packet_out *,
178                        const struct lsquic_stream *, enum QUIC_FRAME_TYPE);
179
180int
181lsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *);
182
183void
184lsquic_packet_out_ack_streams (struct lsquic_packet_out *);
185
186void
187lsquic_packet_out_zero_pad (struct lsquic_packet_out *);
188
189size_t
190lsquic_packet_out_mem_used (const struct lsquic_packet_out *);
191
192int
193lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
194                   const struct parse_funcs *, const struct lsquic_stream *);
195
196#endif
197