lsquic_packet_out.h revision 16a9b66a
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * lsquic_packet_out.h -- Structure and routines dealing with packet_out
450aadb33SDmitri Tikhonov */
550aadb33SDmitri Tikhonov
650aadb33SDmitri Tikhonov#ifndef LSQUIC_PACKET_OUT_H
750aadb33SDmitri Tikhonov#define LSQUIC_PACKET_OUT_H 1
850aadb33SDmitri Tikhonov
950aadb33SDmitri Tikhonov#include <sys/queue.h>
1050aadb33SDmitri Tikhonov
1150aadb33SDmitri Tikhonovstruct malo;
1250aadb33SDmitri Tikhonovstruct lsquic_engine_public;
1350aadb33SDmitri Tikhonovstruct lsquic_mm;
1450aadb33SDmitri Tikhonovstruct lsquic_stream;
1550aadb33SDmitri Tikhonovstruct parse_funcs;
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonov/* Each stream_rec is associated with one packet_out.  packet_out can have
1850aadb33SDmitri Tikhonov * zero or more stream_rec structures.  stream_rec keeps a pointer to a stream
1950aadb33SDmitri Tikhonov * that has STREAM or RST_STREAM frames inside packet_out.  `sr_frame_types'
2050aadb33SDmitri Tikhonov * is a bitmask that records which of these two frames are in the packet.
21c51ce338SDmitri Tikhonov * If this value is zero, values of the other struct members are not valid.
22c51ce338SDmitri Tikhonov * `sr_off' indicates where inside packet_out->po_data STREAM frame begins
23c51ce338SDmitri Tikhonov * and `sr_len' is its length.  These values are not kept for RST_STREAM
24c51ce338SDmitri Tikhonov * frames.
2550aadb33SDmitri Tikhonov *
26c51ce338SDmitri Tikhonov * We need this information for three reasons:
2750aadb33SDmitri Tikhonov *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
2850aadb33SDmitri Tikhonov *      frames are acknowledged.  This is to make sure that we do not exceed
2950aadb33SDmitri Tikhonov *      maximum allowed number of streams.
3050aadb33SDmitri Tikhonov *   2. When a packet is resubmitted, STREAM frames for a stream that has
3150aadb33SDmitri Tikhonov *      been reset are not to be resubmitted.
32c51ce338SDmitri Tikhonov *   3. A buffered packet may have to be split before it is scheduled (this
33c51ce338SDmitri Tikhonov *      occurs if we guessed incorrectly the number of bytes required to
34c51ce338SDmitri Tikhonov *      encode the packet number and the actual number would make packet
35c51ce338SDmitri Tikhonov *      larger than the max).
36c51ce338SDmitri Tikhonov *
3750aadb33SDmitri Tikhonov */
3850aadb33SDmitri Tikhonovstruct stream_rec {
3950aadb33SDmitri Tikhonov    struct lsquic_stream    *sr_stream;
40c51ce338SDmitri Tikhonov    unsigned short           sr_off,
41c51ce338SDmitri Tikhonov                             sr_len;
42bfc7bfd8SDmitri Tikhonov    enum quic_ft_bit         sr_frame_types:16;
4350aadb33SDmitri Tikhonov};
4450aadb33SDmitri Tikhonov
4550aadb33SDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_types)
4650aadb33SDmitri Tikhonov
4750aadb33SDmitri Tikhonovstruct stream_rec_arr {
48c51ce338SDmitri Tikhonov    TAILQ_ENTRY(stream_rec_arr)     next_stream_rec_arr;
4950aadb33SDmitri Tikhonov    struct stream_rec               srecs[
5050aadb33SDmitri Tikhonov      ( 64                              /* Efficient size for malo allocator */
51c51ce338SDmitri Tikhonov      - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */
5250aadb33SDmitri Tikhonov      ) / sizeof(struct stream_rec)
5350aadb33SDmitri Tikhonov    ];
5450aadb33SDmitri Tikhonov};
5550aadb33SDmitri Tikhonov
56c51ce338SDmitri TikhonovTAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr);
57c51ce338SDmitri Tikhonov
5850aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out
5950aadb33SDmitri Tikhonov{
6050aadb33SDmitri Tikhonov    /* `po_next' is used for packets_out, unacked_packets and expired_packets
6150aadb33SDmitri Tikhonov     * lists.
6250aadb33SDmitri Tikhonov     */
6350aadb33SDmitri Tikhonov    TAILQ_ENTRY(lsquic_packet_out)
6450aadb33SDmitri Tikhonov                       po_next;
6550aadb33SDmitri Tikhonov    lsquic_time_t      po_sent;       /* Time sent */
6650aadb33SDmitri Tikhonov    lsquic_packno_t    po_packno;
6750aadb33SDmitri Tikhonov
68bfc7bfd8SDmitri Tikhonov    enum packet_out_flags {
69bfc7bfd8SDmitri Tikhonov        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
70bfc7bfd8SDmitri Tikhonov        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
71bfc7bfd8SDmitri Tikhonov        PO_SREC_ARR = (1 << 4),
72bfc7bfd8SDmitri Tikhonov#define POBIT_SHIFT 5
73bfc7bfd8SDmitri Tikhonov        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
74bfc7bfd8SDmitri Tikhonov        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
75bfc7bfd8SDmitri Tikhonov        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
76bfc7bfd8SDmitri Tikhonov        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
77bfc7bfd8SDmitri Tikhonov        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
78bfc7bfd8SDmitri Tikhonov        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
79bfc7bfd8SDmitri Tikhonov        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
80bfc7bfd8SDmitri Tikhonov        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
81bfc7bfd8SDmitri Tikhonov        PO_STREAM_END
82bfc7bfd8SDmitri Tikhonov                    = (1 <<13),         /* STREAM frame reaches the end of the packet: no
83bfc7bfd8SDmitri Tikhonov                                         * further writes are allowed.
84bfc7bfd8SDmitri Tikhonov                                         */
85bfc7bfd8SDmitri Tikhonov        PO_SCHED    = (1 <<14),         /* On scheduled queue */
8616a9b66aSDmitri Tikhonov        PO_SENT_SZ  = (1 <<15),
87bfc7bfd8SDmitri Tikhonov    }                  po_flags:16;
88bfc7bfd8SDmitri Tikhonov    enum quic_ft_bit   po_frame_types:16; /* Bitmask of QUIC_FRAME_* */
89bfc7bfd8SDmitri Tikhonov    unsigned short     po_data_sz;      /* Number of usable bytes in data */
90bfc7bfd8SDmitri Tikhonov    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
9116a9b66aSDmitri Tikhonov    unsigned short     po_sent_sz;      /* If PO_SENT_SZ is set, real size of sent buffer. */
92bfc7bfd8SDmitri Tikhonov    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
93bfc7bfd8SDmitri Tikhonov                                         * of data containing bytes that are
94bfc7bfd8SDmitri Tikhonov                                         * not to be retransmitted, e.g. ACK
95bfc7bfd8SDmitri Tikhonov                                         * frames.
96bfc7bfd8SDmitri Tikhonov                                         */
97bfc7bfd8SDmitri Tikhonov    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
98bfc7bfd8SDmitri Tikhonov    unsigned char     *po_data;
9916a9b66aSDmitri Tikhonov    lsquic_packno_t    po_ack2ed;       /* If packet has ACK frame, value of
10016a9b66aSDmitri Tikhonov                                         * largest acked in it.
10116a9b66aSDmitri Tikhonov                                         */
102bfc7bfd8SDmitri Tikhonov
10350aadb33SDmitri Tikhonov    /* A lot of packets contain data belonging to only one stream.  Thus,
104c51ce338SDmitri Tikhonov     * `one' is used first.  If this is not enough, any number of
10550aadb33SDmitri Tikhonov     * stream_rec_arr structures can be allocated to handle more stream
10650aadb33SDmitri Tikhonov     * records.
10750aadb33SDmitri Tikhonov     */
108c51ce338SDmitri Tikhonov    union {
109c51ce338SDmitri Tikhonov        struct stream_rec               one;
110c51ce338SDmitri Tikhonov        struct stream_rec_arr_tailq     arr;
111c51ce338SDmitri Tikhonov    }                  po_srecs;
11250aadb33SDmitri Tikhonov
11350aadb33SDmitri Tikhonov    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
11450aadb33SDmitri Tikhonov     * data.
11550aadb33SDmitri Tikhonov     */
11650aadb33SDmitri Tikhonov    unsigned char     *po_enc_data;
11750aadb33SDmitri Tikhonov
11850aadb33SDmitri Tikhonov    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
11950aadb33SDmitri Tikhonov    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
12050aadb33SDmitri Tikhonov} lsquic_packet_out_t;
12150aadb33SDmitri Tikhonov
12250aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced:
12350aadb33SDmitri Tikhonov *
12450aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version
12550aadb33SDmitri Tikhonov * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
12650aadb33SDmitri Tikhonov */
12750aadb33SDmitri Tikhonov
12850aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \
12950aadb33SDmitri Tikhonov                                        ((p)->po_n_alloc - (p)->po_data_sz))
13050aadb33SDmitri Tikhonov
13150aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
13250aadb33SDmitri Tikhonov
133bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_set_packno_bits(p, b) do {                    \
134bfc7bfd8SDmitri Tikhonov    (p)->po_flags &= ~(0x3 << POBIT_SHIFT);                             \
135bfc7bfd8SDmitri Tikhonov    (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT;                        \
136bfc7bfd8SDmitri Tikhonov} while (0)
137bfc7bfd8SDmitri Tikhonov
13850aadb33SDmitri Tikhonov#define lsquic_po_header_length(po_flags) (                                 \
13950aadb33SDmitri Tikhonov    1                                                   /* Type */          \
14050aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_CONN_ID) << 3)                  /* Connection ID */ \
14150aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_VERSION) << 2)                  /* Version */       \
14250aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_NONCE)   << 5)                  /* Nonce */         \
14350aadb33SDmitri Tikhonov  + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3)  /* Packet number */ \
14450aadb33SDmitri Tikhonov)
14550aadb33SDmitri Tikhonov
146bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_total_sz(p) \
147bfc7bfd8SDmitri Tikhonov    ((p)->po_data_sz + lsquic_po_header_length((p)->po_flags) \
148bfc7bfd8SDmitri Tikhonov        + QUIC_PACKET_HASH_SZ)
149bfc7bfd8SDmitri Tikhonov
15016a9b66aSDmitri Tikhonov#if __GNUC__
15116a9b66aSDmitri Tikhonov#if LSQUIC_EXTRA_CHECKS
15216a9b66aSDmitri Tikhonov#define lsquic_packet_out_sent_sz(p) (                                      \
15316a9b66aSDmitri Tikhonov        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
15416a9b66aSDmitri Tikhonov        (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(p)),          \
15516a9b66aSDmitri Tikhonov            (p)->po_sent_sz) : lsquic_packet_out_total_sz(p))
15616a9b66aSDmitri Tikhonov#   else
15716a9b66aSDmitri Tikhonov#define lsquic_packet_out_sent_sz(p) (                                      \
15816a9b66aSDmitri Tikhonov        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
15916a9b66aSDmitri Tikhonov        (p)->po_sent_sz : lsquic_packet_out_total_sz(p))
16016a9b66aSDmitri Tikhonov#endif
16116a9b66aSDmitri Tikhonov#else
16216a9b66aSDmitri Tikhonov#   define lsquic_packet_out_sent_sz(p) (                                   \
16316a9b66aSDmitri Tikhonov        (p)->po_flags & PO_SENT_SZ ?                                        \
16416a9b66aSDmitri Tikhonov        (p)->po_sent_sz : lsquic_packet_out_total_sz(p))
16516a9b66aSDmitri Tikhonov#endif
16616a9b66aSDmitri Tikhonov
16750aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \
16850aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
16950aadb33SDmitri Tikhonov
17050aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \
17150aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
17250aadb33SDmitri Tikhonov
17350aadb33SDmitri Tikhonovstruct packet_out_srec_iter {
17450aadb33SDmitri Tikhonov    lsquic_packet_out_t         *packet_out;
17550aadb33SDmitri Tikhonov    struct stream_rec_arr       *cur_srec_arr;
17650aadb33SDmitri Tikhonov    unsigned                     srec_idx;
177c51ce338SDmitri Tikhonov    int                          impl_idx;
17850aadb33SDmitri Tikhonov};
17950aadb33SDmitri Tikhonov
18050aadb33SDmitri Tikhonovstruct stream_rec *
18150aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
18250aadb33SDmitri Tikhonov
18350aadb33SDmitri Tikhonovstruct stream_rec *
18450aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi);
18550aadb33SDmitri Tikhonov
18650aadb33SDmitri Tikhonovlsquic_packet_out_t *
18750aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
18850aadb33SDmitri Tikhonov                       unsigned short size, enum lsquic_packno_bits,
18950aadb33SDmitri Tikhonov                       const lsquic_ver_tag_t *, const unsigned char *nonce);
19050aadb33SDmitri Tikhonov
19150aadb33SDmitri Tikhonovvoid
19250aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *,
19350aadb33SDmitri Tikhonov                                        struct lsquic_engine_public *);
19450aadb33SDmitri Tikhonov
19550aadb33SDmitri Tikhonovint
19650aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
19750aadb33SDmitri Tikhonov                              struct lsquic_mm *mm,
19850aadb33SDmitri Tikhonov                              struct lsquic_stream *new_stream,
19950aadb33SDmitri Tikhonov                              enum QUIC_FRAME_TYPE,
200c51ce338SDmitri Tikhonov                              unsigned short off, unsigned short len);
20150aadb33SDmitri Tikhonov
202bfc7bfd8SDmitri Tikhonovunsigned
203c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t);
204c51ce338SDmitri Tikhonov
205c51ce338SDmitri Tikhonovint
206c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *,
207c51ce338SDmitri Tikhonov    lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes);
20850aadb33SDmitri Tikhonov
20950aadb33SDmitri Tikhonovvoid
21050aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *);
21150aadb33SDmitri Tikhonov
212c51ce338SDmitri Tikhonovint
213c51ce338SDmitri Tikhonovlsquic_packet_out_has_frame (struct lsquic_packet_out *,
214c51ce338SDmitri Tikhonov                        const struct lsquic_stream *, enum QUIC_FRAME_TYPE);
215c51ce338SDmitri Tikhonov
216c51ce338SDmitri Tikhonovint
217c51ce338SDmitri Tikhonovlsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *);
218c51ce338SDmitri Tikhonov
219c51ce338SDmitri Tikhonovvoid
220c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *);
221c51ce338SDmitri Tikhonov
222c51ce338SDmitri Tikhonovvoid
223c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *);
224c51ce338SDmitri Tikhonov
225c51ce338SDmitri Tikhonovsize_t
226c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *);
227c51ce338SDmitri Tikhonov
228c51ce338SDmitri Tikhonovint
229c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
230c51ce338SDmitri Tikhonov                   const struct parse_funcs *, const struct lsquic_stream *);
231c51ce338SDmitri Tikhonov
23250aadb33SDmitri Tikhonov#endif
233