lsquic_packet_out.h revision 50aadb33
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.
2150aadb33SDmitri Tikhonov * If this value is zero, `sr_stream' and `sr_off' values are not valid.
2250aadb33SDmitri Tikhonov * `sr_off' indicates where inside packet_out->po_data STREAM frame begins.
2350aadb33SDmitri Tikhonov *
2450aadb33SDmitri Tikhonov * We need this information for two reasons:
2550aadb33SDmitri Tikhonov *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
2650aadb33SDmitri Tikhonov *      frames are acknowledged.  This is to make sure that we do not exceed
2750aadb33SDmitri Tikhonov *      maximum allowed number of streams.
2850aadb33SDmitri Tikhonov *   2. When a packet is resubmitted, STREAM frames for a stream that has
2950aadb33SDmitri Tikhonov *      been reset are not to be resubmitted.
3050aadb33SDmitri Tikhonov */
3150aadb33SDmitri Tikhonovstruct stream_rec {
3250aadb33SDmitri Tikhonov    struct lsquic_stream    *sr_stream;
3350aadb33SDmitri Tikhonov    unsigned short           sr_off;
3450aadb33SDmitri Tikhonov    short                    sr_frame_types;
3550aadb33SDmitri Tikhonov};
3650aadb33SDmitri Tikhonov
3750aadb33SDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_types)
3850aadb33SDmitri Tikhonov
3950aadb33SDmitri Tikhonovstruct stream_rec_arr {
4050aadb33SDmitri Tikhonov    STAILQ_ENTRY(stream_rec_arr)    next_stream_rec_arr;
4150aadb33SDmitri Tikhonov    struct stream_rec               srecs[
4250aadb33SDmitri Tikhonov      ( 64                              /* Efficient size for malo allocator */
4350aadb33SDmitri Tikhonov      - sizeof(SLIST_ENTRY(stream_rec)) /* next_stream_rec */
4450aadb33SDmitri Tikhonov      ) / sizeof(struct stream_rec)
4550aadb33SDmitri Tikhonov    ];
4650aadb33SDmitri Tikhonov};
4750aadb33SDmitri Tikhonov
4850aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out
4950aadb33SDmitri Tikhonov{
5050aadb33SDmitri Tikhonov    /* `po_next' is used for packets_out, unacked_packets and expired_packets
5150aadb33SDmitri Tikhonov     * lists.
5250aadb33SDmitri Tikhonov     */
5350aadb33SDmitri Tikhonov    TAILQ_ENTRY(lsquic_packet_out)
5450aadb33SDmitri Tikhonov                       po_next;
5550aadb33SDmitri Tikhonov    lsquic_time_t      po_sent;       /* Time sent */
5650aadb33SDmitri Tikhonov    lsquic_packno_t    po_packno;
5750aadb33SDmitri Tikhonov
5850aadb33SDmitri Tikhonov    /* A lot of packets contain data belonging to only one stream.  Thus,
5950aadb33SDmitri Tikhonov     * `srec' is used first.  If this is not enough, any number of
6050aadb33SDmitri Tikhonov     * stream_rec_arr structures can be allocated to handle more stream
6150aadb33SDmitri Tikhonov     * records.
6250aadb33SDmitri Tikhonov     */
6350aadb33SDmitri Tikhonov    struct stream_rec  po_srec;
6450aadb33SDmitri Tikhonov    STAILQ_HEAD(, stream_rec_arr)
6550aadb33SDmitri Tikhonov                       po_srec_arrs;
6650aadb33SDmitri Tikhonov
6750aadb33SDmitri Tikhonov    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
6850aadb33SDmitri Tikhonov     * data.
6950aadb33SDmitri Tikhonov     */
7050aadb33SDmitri Tikhonov    unsigned char     *po_enc_data;
7150aadb33SDmitri Tikhonov
7250aadb33SDmitri Tikhonov    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
7350aadb33SDmitri Tikhonov    short              po_frame_types;  /* Bitmask of QUIC_FRAME_* */
7450aadb33SDmitri Tikhonov    unsigned short     po_data_sz;      /* Number of usable bytes in data */
7550aadb33SDmitri Tikhonov    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
7650aadb33SDmitri Tikhonov    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
7750aadb33SDmitri Tikhonov                                         * of data containing bytes that are
7850aadb33SDmitri Tikhonov                                         * not to be retransmitted, e.g. ACK
7950aadb33SDmitri Tikhonov                                         * frames.
8050aadb33SDmitri Tikhonov                                         */
8150aadb33SDmitri Tikhonov    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
8250aadb33SDmitri Tikhonov    enum packet_out_flags {
8350aadb33SDmitri Tikhonov        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
8450aadb33SDmitri Tikhonov        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
8550aadb33SDmitri Tikhonov        PO_WRITEABLE= (1 << 4),         /* Packet is writeable */
8650aadb33SDmitri Tikhonov#define POBIT_SHIFT 5
8750aadb33SDmitri Tikhonov        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
8850aadb33SDmitri Tikhonov        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
8950aadb33SDmitri Tikhonov        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
9050aadb33SDmitri Tikhonov        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
9150aadb33SDmitri Tikhonov        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
9250aadb33SDmitri Tikhonov        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
9350aadb33SDmitri Tikhonov        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
9450aadb33SDmitri Tikhonov        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
9550aadb33SDmitri Tikhonov    }                  po_flags:16;
9650aadb33SDmitri Tikhonov    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
9750aadb33SDmitri Tikhonov    unsigned char     *po_data;
9850aadb33SDmitri Tikhonov} lsquic_packet_out_t;
9950aadb33SDmitri Tikhonov
10050aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced:
10150aadb33SDmitri Tikhonov *
10250aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version
10350aadb33SDmitri Tikhonov * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
10450aadb33SDmitri Tikhonov */
10550aadb33SDmitri Tikhonov
10650aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \
10750aadb33SDmitri Tikhonov                                        ((p)->po_n_alloc - (p)->po_data_sz))
10850aadb33SDmitri Tikhonov
10950aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
11050aadb33SDmitri Tikhonov
11150aadb33SDmitri Tikhonov/* XXX This will need to be made into a method for Q041 */
11250aadb33SDmitri Tikhonov#define lsquic_po_header_length(po_flags) (                                 \
11350aadb33SDmitri Tikhonov    1                                                   /* Type */          \
11450aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_CONN_ID) << 3)                  /* Connection ID */ \
11550aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_VERSION) << 2)                  /* Version */       \
11650aadb33SDmitri Tikhonov  + (!!((po_flags) & PO_NONCE)   << 5)                  /* Nonce */         \
11750aadb33SDmitri Tikhonov  + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3)  /* Packet number */ \
11850aadb33SDmitri Tikhonov)
11950aadb33SDmitri Tikhonov
12050aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \
12150aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
12250aadb33SDmitri Tikhonov
12350aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \
12450aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
12550aadb33SDmitri Tikhonov
12650aadb33SDmitri Tikhonovstruct packet_out_srec_iter {
12750aadb33SDmitri Tikhonov    lsquic_packet_out_t         *packet_out;
12850aadb33SDmitri Tikhonov    struct stream_rec_arr       *cur_srec_arr;
12950aadb33SDmitri Tikhonov    unsigned                     srec_idx;
13050aadb33SDmitri Tikhonov    int                          past_srec;
13150aadb33SDmitri Tikhonov};
13250aadb33SDmitri Tikhonov
13350aadb33SDmitri Tikhonovstruct stream_rec *
13450aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
13550aadb33SDmitri Tikhonov
13650aadb33SDmitri Tikhonovstruct stream_rec *
13750aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi);
13850aadb33SDmitri Tikhonov
13950aadb33SDmitri Tikhonovlsquic_packet_out_t *
14050aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
14150aadb33SDmitri Tikhonov                       unsigned short size, enum lsquic_packno_bits,
14250aadb33SDmitri Tikhonov                       const lsquic_ver_tag_t *, const unsigned char *nonce);
14350aadb33SDmitri Tikhonov
14450aadb33SDmitri Tikhonovvoid
14550aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *,
14650aadb33SDmitri Tikhonov                                        struct lsquic_engine_public *);
14750aadb33SDmitri Tikhonov
14850aadb33SDmitri Tikhonovint
14950aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
15050aadb33SDmitri Tikhonov                              struct lsquic_mm *mm,
15150aadb33SDmitri Tikhonov                              struct lsquic_stream *new_stream,
15250aadb33SDmitri Tikhonov                              enum QUIC_FRAME_TYPE,
15350aadb33SDmitri Tikhonov                              unsigned short off);
15450aadb33SDmitri Tikhonov
15550aadb33SDmitri Tikhonovvoid
15650aadb33SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *,
15750aadb33SDmitri Tikhonov                                        const struct parse_funcs *, uint32_t);
15850aadb33SDmitri Tikhonov
15950aadb33SDmitri Tikhonovvoid
16050aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *);
16150aadb33SDmitri Tikhonov
16250aadb33SDmitri Tikhonov#endif
163