lsquic_packet_out.h revision c51ce338
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;
4250aadb33SDmitri Tikhonov    short                    sr_frame_types;
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
6850aadb33SDmitri Tikhonov    /* A lot of packets contain data belonging to only one stream.  Thus,
69c51ce338SDmitri Tikhonov     * `one' is used first.  If this is not enough, any number of
7050aadb33SDmitri Tikhonov     * stream_rec_arr structures can be allocated to handle more stream
7150aadb33SDmitri Tikhonov     * records.
7250aadb33SDmitri Tikhonov     */
73c51ce338SDmitri Tikhonov    union {
74c51ce338SDmitri Tikhonov        struct stream_rec               one;
75c51ce338SDmitri Tikhonov        struct stream_rec_arr_tailq     arr;
76c51ce338SDmitri Tikhonov    }                  po_srecs;
7750aadb33SDmitri Tikhonov
7850aadb33SDmitri Tikhonov    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
7950aadb33SDmitri Tikhonov     * data.
8050aadb33SDmitri Tikhonov     */
8150aadb33SDmitri Tikhonov    unsigned char     *po_enc_data;
8250aadb33SDmitri Tikhonov
8350aadb33SDmitri Tikhonov    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
8450aadb33SDmitri Tikhonov    short              po_frame_types;  /* Bitmask of QUIC_FRAME_* */
8550aadb33SDmitri Tikhonov    unsigned short     po_data_sz;      /* Number of usable bytes in data */
8650aadb33SDmitri Tikhonov    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
8750aadb33SDmitri Tikhonov    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
8850aadb33SDmitri Tikhonov                                         * of data containing bytes that are
8950aadb33SDmitri Tikhonov                                         * not to be retransmitted, e.g. ACK
9050aadb33SDmitri Tikhonov                                         * frames.
9150aadb33SDmitri Tikhonov                                         */
9250aadb33SDmitri Tikhonov    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
9350aadb33SDmitri Tikhonov    enum packet_out_flags {
9450aadb33SDmitri Tikhonov        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
9550aadb33SDmitri Tikhonov        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
96c51ce338SDmitri Tikhonov        PO_SREC_ARR = (1 << 4),
9750aadb33SDmitri Tikhonov#define POBIT_SHIFT 5
9850aadb33SDmitri Tikhonov        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
9950aadb33SDmitri Tikhonov        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
10050aadb33SDmitri Tikhonov        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
10150aadb33SDmitri Tikhonov        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
10250aadb33SDmitri Tikhonov        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
10350aadb33SDmitri Tikhonov        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
10450aadb33SDmitri Tikhonov        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
10550aadb33SDmitri Tikhonov        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
10650aadb33SDmitri Tikhonov    }                  po_flags:16;
10750aadb33SDmitri Tikhonov    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
10850aadb33SDmitri Tikhonov    unsigned char     *po_data;
10950aadb33SDmitri Tikhonov} lsquic_packet_out_t;
11050aadb33SDmitri Tikhonov
11150aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced:
11250aadb33SDmitri Tikhonov *
11350aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version
11450aadb33SDmitri Tikhonov * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
11550aadb33SDmitri Tikhonov */
11650aadb33SDmitri Tikhonov
11750aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \
11850aadb33SDmitri Tikhonov                                        ((p)->po_n_alloc - (p)->po_data_sz))
11950aadb33SDmitri Tikhonov
12050aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
12150aadb33SDmitri Tikhonov
12250aadb33SDmitri Tikhonov/* XXX This will need to be made into a method for Q041 */
12350aadb33SDmitri Tikhonov#define lsquic_po_header_length(po_flags) (                                 \
12450aadb33SDmitri Tikhonov    1                                                   /* Type */          \
12550aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_CONN_ID) << 3)                  /* Connection ID */ \
12650aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_VERSION) << 2)                  /* Version */       \
12750aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_NONCE)   << 5)                  /* Nonce */         \
12850aadb33SDmitri Tikhonov  + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3)  /* Packet number */ \
12950aadb33SDmitri Tikhonov)
13050aadb33SDmitri Tikhonov
13150aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \
13250aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
13350aadb33SDmitri Tikhonov
13450aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \
13550aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
13650aadb33SDmitri Tikhonov
13750aadb33SDmitri Tikhonovstruct packet_out_srec_iter {
13850aadb33SDmitri Tikhonov    lsquic_packet_out_t         *packet_out;
13950aadb33SDmitri Tikhonov    struct stream_rec_arr       *cur_srec_arr;
14050aadb33SDmitri Tikhonov    unsigned                     srec_idx;
141c51ce338SDmitri Tikhonov    int                          impl_idx;
14250aadb33SDmitri Tikhonov};
14350aadb33SDmitri Tikhonov
14450aadb33SDmitri Tikhonovstruct stream_rec *
14550aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
14650aadb33SDmitri Tikhonov
14750aadb33SDmitri Tikhonovstruct stream_rec *
14850aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi);
14950aadb33SDmitri Tikhonov
15050aadb33SDmitri Tikhonovlsquic_packet_out_t *
15150aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
15250aadb33SDmitri Tikhonov                       unsigned short size, enum lsquic_packno_bits,
15350aadb33SDmitri Tikhonov                       const lsquic_ver_tag_t *, const unsigned char *nonce);
15450aadb33SDmitri Tikhonov
15550aadb33SDmitri Tikhonovvoid
15650aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *,
15750aadb33SDmitri Tikhonov                                        struct lsquic_engine_public *);
15850aadb33SDmitri Tikhonov
15950aadb33SDmitri Tikhonovint
16050aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
16150aadb33SDmitri Tikhonov                              struct lsquic_mm *mm,
16250aadb33SDmitri Tikhonov                              struct lsquic_stream *new_stream,
16350aadb33SDmitri Tikhonov                              enum QUIC_FRAME_TYPE,
164c51ce338SDmitri Tikhonov                              unsigned short off, unsigned short len);
16550aadb33SDmitri Tikhonov
16650aadb33SDmitri Tikhonovvoid
167c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t);
168c51ce338SDmitri Tikhonov
169c51ce338SDmitri Tikhonovint
170c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *,
171c51ce338SDmitri Tikhonov    lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes);
17250aadb33SDmitri Tikhonov
17350aadb33SDmitri Tikhonovvoid
17450aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *);
17550aadb33SDmitri Tikhonov
176c51ce338SDmitri Tikhonovint
177c51ce338SDmitri Tikhonovlsquic_packet_out_has_frame (struct lsquic_packet_out *,
178c51ce338SDmitri Tikhonov                        const struct lsquic_stream *, enum QUIC_FRAME_TYPE);
179c51ce338SDmitri Tikhonov
180c51ce338SDmitri Tikhonovint
181c51ce338SDmitri Tikhonovlsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *);
182c51ce338SDmitri Tikhonov
183c51ce338SDmitri Tikhonovvoid
184c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *);
185c51ce338SDmitri Tikhonov
186c51ce338SDmitri Tikhonovvoid
187c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *);
188c51ce338SDmitri Tikhonov
189c51ce338SDmitri Tikhonovsize_t
190c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *);
191c51ce338SDmitri Tikhonov
192c51ce338SDmitri Tikhonovint
193c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
194c51ce338SDmitri Tikhonov                   const struct parse_funcs *, const struct lsquic_stream *);
195c51ce338SDmitri Tikhonov
19650aadb33SDmitri Tikhonov#endif
197