lsquic_packet_out.h revision 50aadb33
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, `sr_stream' and `sr_off' values are not valid.
22 * `sr_off' indicates where inside packet_out->po_data STREAM frame begins.
23 *
24 * We need this information for two reasons:
25 *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
26 *      frames are acknowledged.  This is to make sure that we do not exceed
27 *      maximum allowed number of streams.
28 *   2. When a packet is resubmitted, STREAM frames for a stream that has
29 *      been reset are not to be resubmitted.
30 */
31struct stream_rec {
32    struct lsquic_stream    *sr_stream;
33    unsigned short           sr_off;
34    short                    sr_frame_types;
35};
36
37#define srec_taken(srec) ((srec)->sr_frame_types)
38
39struct stream_rec_arr {
40    STAILQ_ENTRY(stream_rec_arr)    next_stream_rec_arr;
41    struct stream_rec               srecs[
42      ( 64                              /* Efficient size for malo allocator */
43      - sizeof(SLIST_ENTRY(stream_rec)) /* next_stream_rec */
44      ) / sizeof(struct stream_rec)
45    ];
46};
47
48typedef struct lsquic_packet_out
49{
50    /* `po_next' is used for packets_out, unacked_packets and expired_packets
51     * lists.
52     */
53    TAILQ_ENTRY(lsquic_packet_out)
54                       po_next;
55    lsquic_time_t      po_sent;       /* Time sent */
56    lsquic_packno_t    po_packno;
57
58    /* A lot of packets contain data belonging to only one stream.  Thus,
59     * `srec' is used first.  If this is not enough, any number of
60     * stream_rec_arr structures can be allocated to handle more stream
61     * records.
62     */
63    struct stream_rec  po_srec;
64    STAILQ_HEAD(, stream_rec_arr)
65                       po_srec_arrs;
66
67    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
68     * data.
69     */
70    unsigned char     *po_enc_data;
71
72    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
73    short              po_frame_types;  /* Bitmask of QUIC_FRAME_* */
74    unsigned short     po_data_sz;      /* Number of usable bytes in data */
75    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
76    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
77                                         * of data containing bytes that are
78                                         * not to be retransmitted, e.g. ACK
79                                         * frames.
80                                         */
81    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
82    enum packet_out_flags {
83        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
84        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
85        PO_WRITEABLE= (1 << 4),         /* Packet is writeable */
86#define POBIT_SHIFT 5
87        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
88        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
89        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
90        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
91        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
92        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
93        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
94        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
95    }                  po_flags:16;
96    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
97    unsigned char     *po_data;
98} lsquic_packet_out_t;
99
100/* The size of lsquic_packet_out_t could be further reduced:
101 *
102 * po_ver_tag could be encoded as a few bits representing enum lsquic_version
103 * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
104 */
105
106#define lsquic_packet_out_avail(p) ((unsigned short) \
107                                        ((p)->po_n_alloc - (p)->po_data_sz))
108
109#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
110
111/* XXX This will need to be made into a method for Q041 */
112#define lsquic_po_header_length(po_flags) (                                 \
113    1                                                   /* Type */          \
114  + (!!((po_flags) & PO_CONN_ID) << 3)                  /* Connection ID */ \
115  + (!!((po_flags) & PO_VERSION) << 2)                  /* Version */       \
116  + (!!((po_flags) & PO_NONCE)   << 5)                  /* Nonce */         \
117  + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3)  /* Packet number */ \
118)
119
120#define lsquic_packet_out_verneg(p) \
121    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
122
123#define lsquic_packet_out_pubres(p) \
124    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
125
126struct packet_out_srec_iter {
127    lsquic_packet_out_t         *packet_out;
128    struct stream_rec_arr       *cur_srec_arr;
129    unsigned                     srec_idx;
130    int                          past_srec;
131};
132
133struct stream_rec *
134posi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
135
136struct stream_rec *
137posi_next (struct packet_out_srec_iter *posi);
138
139lsquic_packet_out_t *
140lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
141                       unsigned short size, enum lsquic_packno_bits,
142                       const lsquic_ver_tag_t *, const unsigned char *nonce);
143
144void
145lsquic_packet_out_destroy (lsquic_packet_out_t *,
146                                        struct lsquic_engine_public *);
147
148int
149lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
150                              struct lsquic_mm *mm,
151                              struct lsquic_stream *new_stream,
152                              enum QUIC_FRAME_TYPE,
153                              unsigned short off);
154
155void
156lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *,
157                                        const struct parse_funcs *, uint32_t);
158
159void
160lsquic_packet_out_chop_regen (lsquic_packet_out_t *);
161
162#endif
163