lsquic_packet_out.h revision c7d81ce1
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 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;
129626cfc2SDmitri Tikhonovstruct lsquic_conn;
1350aadb33SDmitri Tikhonovstruct lsquic_engine_public;
1450aadb33SDmitri Tikhonovstruct lsquic_mm;
1550aadb33SDmitri Tikhonovstruct lsquic_stream;
1650aadb33SDmitri Tikhonovstruct parse_funcs;
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonov/* Each stream_rec is associated with one packet_out.  packet_out can have
1950aadb33SDmitri Tikhonov * zero or more stream_rec structures.  stream_rec keeps a pointer to a stream
204d83f5bdSDmitri Tikhonov * that has STREAM or RST_STREAM frames inside packet_out.  `sr_frame_type'
214d83f5bdSDmitri Tikhonov * specifies the type of the frame; if this value is zero, values of the
224d83f5bdSDmitri Tikhonov * other struct members are not valid.  `sr_off' indicates where inside
234d83f5bdSDmitri Tikhonov * packet_out->po_data the frame begins and `sr_len' is its length.
2450aadb33SDmitri Tikhonov *
25c51ce338SDmitri Tikhonov * We need this information for three reasons:
2650aadb33SDmitri Tikhonov *   1. A stream is not destroyed until all of its STREAM and RST_STREAM
2750aadb33SDmitri Tikhonov *      frames are acknowledged.  This is to make sure that we do not exceed
2850aadb33SDmitri Tikhonov *      maximum allowed number of streams.
2950aadb33SDmitri Tikhonov *   2. When a packet is resubmitted, STREAM frames for a stream that has
3050aadb33SDmitri Tikhonov *      been reset are not to be resubmitted.
31c51ce338SDmitri Tikhonov *   3. A buffered packet may have to be split before it is scheduled (this
32c51ce338SDmitri Tikhonov *      occurs if we guessed incorrectly the number of bytes required to
33c51ce338SDmitri Tikhonov *      encode the packet number and the actual number would make packet
34c51ce338SDmitri Tikhonov *      larger than the max).
35c51ce338SDmitri Tikhonov *
3650aadb33SDmitri Tikhonov */
3750aadb33SDmitri Tikhonovstruct stream_rec {
3850aadb33SDmitri Tikhonov    struct lsquic_stream    *sr_stream;
39c51ce338SDmitri Tikhonov    unsigned short           sr_off,
40c51ce338SDmitri Tikhonov                             sr_len;
416aba801dSDmitri Tikhonov    enum quic_frame_type     sr_frame_type:16;
4250aadb33SDmitri Tikhonov};
4350aadb33SDmitri Tikhonov
444d83f5bdSDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_type)
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonovstruct stream_rec_arr {
47c51ce338SDmitri Tikhonov    TAILQ_ENTRY(stream_rec_arr)     next_stream_rec_arr;
4850aadb33SDmitri Tikhonov    struct stream_rec               srecs[
4950aadb33SDmitri Tikhonov      ( 64                              /* Efficient size for malo allocator */
50c51ce338SDmitri Tikhonov      - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */
5150aadb33SDmitri Tikhonov      ) / sizeof(struct stream_rec)
5250aadb33SDmitri Tikhonov    ];
5350aadb33SDmitri Tikhonov};
5450aadb33SDmitri Tikhonov
55c51ce338SDmitri TikhonovTAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr);
56c51ce338SDmitri Tikhonov
5750aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out
5850aadb33SDmitri Tikhonov{
5950aadb33SDmitri Tikhonov    /* `po_next' is used for packets_out, unacked_packets and expired_packets
6050aadb33SDmitri Tikhonov     * lists.
6150aadb33SDmitri Tikhonov     */
6250aadb33SDmitri Tikhonov    TAILQ_ENTRY(lsquic_packet_out)
6350aadb33SDmitri Tikhonov                       po_next;
6450aadb33SDmitri Tikhonov    lsquic_time_t      po_sent;       /* Time sent */
6550aadb33SDmitri Tikhonov    lsquic_packno_t    po_packno;
6650aadb33SDmitri Tikhonov
67bfc7bfd8SDmitri Tikhonov    enum packet_out_flags {
68bfc7bfd8SDmitri Tikhonov        PO_HELLO    = (1 << 1),         /* Packet contains SHLO or CHLO data */
69bfc7bfd8SDmitri Tikhonov        PO_ENCRYPTED= (1 << 3),         /* po_enc_data has encrypted data */
70bfc7bfd8SDmitri Tikhonov        PO_SREC_ARR = (1 << 4),
71bfc7bfd8SDmitri Tikhonov#define POBIT_SHIFT 5
72bfc7bfd8SDmitri Tikhonov        PO_BITS_0   = (1 << 5),         /* PO_BITS_0 and PO_BITS_1 encode the */
73bfc7bfd8SDmitri Tikhonov        PO_BITS_1   = (1 << 6),         /*   packet number length.  See macros below. */
74bfc7bfd8SDmitri Tikhonov        PO_NONCE    = (1 << 7),         /* Use value in `po_nonce' to generate header */
75bfc7bfd8SDmitri Tikhonov        PO_VERSION  = (1 << 8),         /* Use value in `po_ver_tag' to generate header */
76bfc7bfd8SDmitri Tikhonov        PO_CONN_ID  = (1 << 9),         /* Include connection ID in public header */
77bfc7bfd8SDmitri Tikhonov        PO_REPACKNO = (1 <<10),         /* Regenerate packet number */
78bfc7bfd8SDmitri Tikhonov        PO_NOENCRYPT= (1 <<11),         /* Do not encrypt data in po_data */
79bfc7bfd8SDmitri Tikhonov        PO_VERNEG   = (1 <<12),         /* Version negotiation packet. */
80bfc7bfd8SDmitri Tikhonov        PO_STREAM_END
81bfc7bfd8SDmitri Tikhonov                    = (1 <<13),         /* STREAM frame reaches the end of the packet: no
82bfc7bfd8SDmitri Tikhonov                                         * further writes are allowed.
83bfc7bfd8SDmitri Tikhonov                                         */
84bfc7bfd8SDmitri Tikhonov        PO_SCHED    = (1 <<14),         /* On scheduled queue */
8516a9b66aSDmitri Tikhonov        PO_SENT_SZ  = (1 <<15),
869626cfc2SDmitri Tikhonov        PO_LONGHEAD = (1 <<16),
879626cfc2SDmitri Tikhonov        PO_GQUIC    = (1 <<17),         /* Used for logging */
889626cfc2SDmitri Tikhonov#define POLEV_SHIFT 18
899626cfc2SDmitri Tikhonov        PO_BITS_2   = (1 <<18),         /* PO_BITS_2 and PO_BITS_3 encode the */
909626cfc2SDmitri Tikhonov        PO_BITS_3   = (1 <<19),         /*   crypto level.  Used for logging. */
911e75f938SDmitri Tikhonov#define POIPv6_SHIFT 20
921e75f938SDmitri Tikhonov        PO_IPv6     = (1 <<20),         /* Set if pmi_allocate was passed is_ipv6=1,
931e75f938SDmitri Tikhonov                                         *   otherwise unset.
941e75f938SDmitri Tikhonov                                         */
958252b0b9SDmitri Tikhonov        PO_LIMITED  = (1 <<21),         /* Used to credit sc_next_limit if needed. */
969626cfc2SDmitri Tikhonov    }                  po_flags;
97bfc7bfd8SDmitri Tikhonov    enum quic_ft_bit   po_frame_types:16; /* Bitmask of QUIC_FRAME_* */
98bfc7bfd8SDmitri Tikhonov    unsigned short     po_data_sz;      /* Number of usable bytes in data */
99bfc7bfd8SDmitri Tikhonov    unsigned short     po_enc_data_sz;  /* Number of usable bytes in data */
10016a9b66aSDmitri Tikhonov    unsigned short     po_sent_sz;      /* If PO_SENT_SZ is set, real size of sent buffer. */
101bfc7bfd8SDmitri Tikhonov    unsigned short     po_regen_sz;     /* Number of bytes at the beginning
102bfc7bfd8SDmitri Tikhonov                                         * of data containing bytes that are
103bfc7bfd8SDmitri Tikhonov                                         * not to be retransmitted, e.g. ACK
104bfc7bfd8SDmitri Tikhonov                                         * frames.
105bfc7bfd8SDmitri Tikhonov                                         */
106bfc7bfd8SDmitri Tikhonov    unsigned short     po_n_alloc;      /* Total number of bytes allocated in po_data */
1079626cfc2SDmitri Tikhonov    enum header_type   po_header_type:8;
108bfc7bfd8SDmitri Tikhonov    unsigned char     *po_data;
10916a9b66aSDmitri Tikhonov    lsquic_packno_t    po_ack2ed;       /* If packet has ACK frame, value of
11016a9b66aSDmitri Tikhonov                                         * largest acked in it.
11116a9b66aSDmitri Tikhonov                                         */
112bfc7bfd8SDmitri Tikhonov
11350aadb33SDmitri Tikhonov    /* A lot of packets contain data belonging to only one stream.  Thus,
114c51ce338SDmitri Tikhonov     * `one' is used first.  If this is not enough, any number of
11550aadb33SDmitri Tikhonov     * stream_rec_arr structures can be allocated to handle more stream
11650aadb33SDmitri Tikhonov     * records.
11750aadb33SDmitri Tikhonov     */
118c51ce338SDmitri Tikhonov    union {
119c51ce338SDmitri Tikhonov        struct stream_rec               one;
120c51ce338SDmitri Tikhonov        struct stream_rec_arr_tailq     arr;
121c51ce338SDmitri Tikhonov    }                  po_srecs;
12250aadb33SDmitri Tikhonov
12350aadb33SDmitri Tikhonov    /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted
12450aadb33SDmitri Tikhonov     * data.
12550aadb33SDmitri Tikhonov     */
12650aadb33SDmitri Tikhonov    unsigned char     *po_enc_data;
12750aadb33SDmitri Tikhonov
12850aadb33SDmitri Tikhonov    lsquic_ver_tag_t   po_ver_tag;      /* Set if PO_VERSION is set */
12950aadb33SDmitri Tikhonov    unsigned char     *po_nonce;        /* Use to generate header if PO_NONCE is set */
13050aadb33SDmitri Tikhonov} lsquic_packet_out_t;
13150aadb33SDmitri Tikhonov
13250aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced:
13350aadb33SDmitri Tikhonov *
13450aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version
13550aadb33SDmitri Tikhonov * in po_flags.  The cost is a bit of complexity.  This will save us four bytes.
13650aadb33SDmitri Tikhonov */
13750aadb33SDmitri Tikhonov
13850aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \
13950aadb33SDmitri Tikhonov                                        ((p)->po_n_alloc - (p)->po_data_sz))
14050aadb33SDmitri Tikhonov
14150aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3)
14250aadb33SDmitri Tikhonov
143bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_set_packno_bits(p, b) do {                    \
144bfc7bfd8SDmitri Tikhonov    (p)->po_flags &= ~(0x3 << POBIT_SHIFT);                             \
145bfc7bfd8SDmitri Tikhonov    (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT;                        \
146bfc7bfd8SDmitri Tikhonov} while (0)
147bfc7bfd8SDmitri Tikhonov
1481e75f938SDmitri Tikhonov#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1))
1491e75f938SDmitri Tikhonov
1501e75f938SDmitri Tikhonov#define lsquic_packet_out_set_ipv6(p, b) do {                           \
1511e75f938SDmitri Tikhonov    (p)->po_flags &= ~(1 << POIPv6_SHIFT);                              \
1521e75f938SDmitri Tikhonov    (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT;                         \
1531e75f938SDmitri Tikhonov} while (0)
1541e75f938SDmitri Tikhonov
1559626cfc2SDmitri Tikhonov#define lsquic_po_header_length(lconn, po_flags) ( \
1569626cfc2SDmitri Tikhonov    lconn->cn_pf->pf_packout_header_size(lconn, po_flags))
15750aadb33SDmitri Tikhonov
1589626cfc2SDmitri Tikhonov#define lsquic_packet_out_total_sz(lconn, p) (\
1599626cfc2SDmitri Tikhonov    lconn->cn_pf->pf_packout_size(lconn, p))
160bfc7bfd8SDmitri Tikhonov
16116a9b66aSDmitri Tikhonov#if __GNUC__
16216a9b66aSDmitri Tikhonov#if LSQUIC_EXTRA_CHECKS
1639626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) (                               \
16416a9b66aSDmitri Tikhonov        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
1659626cfc2SDmitri Tikhonov        (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)),   \
1669626cfc2SDmitri Tikhonov            (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p))
16716a9b66aSDmitri Tikhonov#   else
1689626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) (                               \
16916a9b66aSDmitri Tikhonov        __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ?                 \
1709626cfc2SDmitri Tikhonov        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
17116a9b66aSDmitri Tikhonov#endif
17216a9b66aSDmitri Tikhonov#else
1739626cfc2SDmitri Tikhonov#   define lsquic_packet_out_sent_sz(lconn, p) (                            \
17416a9b66aSDmitri Tikhonov        (p)->po_flags & PO_SENT_SZ ?                                        \
1759626cfc2SDmitri Tikhonov        (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p))
17616a9b66aSDmitri Tikhonov#endif
17716a9b66aSDmitri Tikhonov
17850aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \
17950aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG))
18050aadb33SDmitri Tikhonov
18150aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \
18250aadb33SDmitri Tikhonov    (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) ==  PO_NOENCRYPT           )
18350aadb33SDmitri Tikhonov
1849626cfc2SDmitri Tikhonov#define lsquic_packet_out_set_enc_level(p, level) do {                      \
1859626cfc2SDmitri Tikhonov    (p)->po_flags &= ~(3 << POLEV_SHIFT);                                   \
1869626cfc2SDmitri Tikhonov    (p)->po_flags |= level << POLEV_SHIFT;                                  \
1879626cfc2SDmitri Tikhonov} while (0)
1889626cfc2SDmitri Tikhonov
1899626cfc2SDmitri Tikhonov#define lsquic_packet_out_enc_level(p)  (((p)->po_flags >> POLEV_SHIFT) & 3)
1909626cfc2SDmitri Tikhonov
19150aadb33SDmitri Tikhonovstruct packet_out_srec_iter {
19250aadb33SDmitri Tikhonov    lsquic_packet_out_t         *packet_out;
19350aadb33SDmitri Tikhonov    struct stream_rec_arr       *cur_srec_arr;
19450aadb33SDmitri Tikhonov    unsigned                     srec_idx;
195c51ce338SDmitri Tikhonov    int                          impl_idx;
19650aadb33SDmitri Tikhonov};
19750aadb33SDmitri Tikhonov
19850aadb33SDmitri Tikhonovstruct stream_rec *
19950aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *);
20050aadb33SDmitri Tikhonov
20150aadb33SDmitri Tikhonovstruct stream_rec *
20250aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi);
20350aadb33SDmitri Tikhonov
20450aadb33SDmitri Tikhonovlsquic_packet_out_t *
20550aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid,
206c7d81ce1SDmitri Tikhonov                       const struct lsquic_conn *, enum packno_bits,
20750aadb33SDmitri Tikhonov                       const lsquic_ver_tag_t *, const unsigned char *nonce);
20850aadb33SDmitri Tikhonov
20950aadb33SDmitri Tikhonovvoid
21050aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *,
2111e75f938SDmitri Tikhonov                        struct lsquic_engine_public *, void *peer_ctx);
21250aadb33SDmitri Tikhonov
21350aadb33SDmitri Tikhonovint
21450aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out,
21550aadb33SDmitri Tikhonov                              struct lsquic_mm *mm,
21650aadb33SDmitri Tikhonov                              struct lsquic_stream *new_stream,
2176aba801dSDmitri Tikhonov                              enum quic_frame_type,
218c51ce338SDmitri Tikhonov                              unsigned short off, unsigned short len);
21950aadb33SDmitri Tikhonov
220bfc7bfd8SDmitri Tikhonovunsigned
221c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t);
222c51ce338SDmitri Tikhonov
223c51ce338SDmitri Tikhonovint
224c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *,
225c51ce338SDmitri Tikhonov    lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes);
22650aadb33SDmitri Tikhonov
22750aadb33SDmitri Tikhonovvoid
22850aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *);
22950aadb33SDmitri Tikhonov
230c51ce338SDmitri Tikhonovvoid
231c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *);
232c51ce338SDmitri Tikhonov
233c51ce338SDmitri Tikhonovvoid
234c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *);
235c51ce338SDmitri Tikhonov
236c51ce338SDmitri Tikhonovsize_t
237c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *);
238c51ce338SDmitri Tikhonov
239c51ce338SDmitri Tikhonovint
240c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *,
241c51ce338SDmitri Tikhonov                   const struct parse_funcs *, const struct lsquic_stream *);
242c51ce338SDmitri Tikhonov
24350aadb33SDmitri Tikhonov#endif
244