lsquic_packet_out.h revision 9626cfc2
110c492f0SDmitri Tikhonov/* Copyright (c) 2017 - 2018 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 2050aadb33SDmitri Tikhonov * that has STREAM or RST_STREAM frames inside packet_out. `sr_frame_types' 2150aadb33SDmitri Tikhonov * is a bitmask that records which of these two frames are in the packet. 22c51ce338SDmitri Tikhonov * If this value is zero, values of the other struct members are not valid. 23c51ce338SDmitri Tikhonov * `sr_off' indicates where inside packet_out->po_data STREAM frame begins 24c51ce338SDmitri Tikhonov * and `sr_len' is its length. These values are not kept for RST_STREAM 25c51ce338SDmitri Tikhonov * frames. 2650aadb33SDmitri Tikhonov * 27c51ce338SDmitri Tikhonov * We need this information for three reasons: 2850aadb33SDmitri Tikhonov * 1. A stream is not destroyed until all of its STREAM and RST_STREAM 2950aadb33SDmitri Tikhonov * frames are acknowledged. This is to make sure that we do not exceed 3050aadb33SDmitri Tikhonov * maximum allowed number of streams. 3150aadb33SDmitri Tikhonov * 2. When a packet is resubmitted, STREAM frames for a stream that has 3250aadb33SDmitri Tikhonov * been reset are not to be resubmitted. 33c51ce338SDmitri Tikhonov * 3. A buffered packet may have to be split before it is scheduled (this 34c51ce338SDmitri Tikhonov * occurs if we guessed incorrectly the number of bytes required to 35c51ce338SDmitri Tikhonov * encode the packet number and the actual number would make packet 36c51ce338SDmitri Tikhonov * larger than the max). 37c51ce338SDmitri Tikhonov * 3850aadb33SDmitri Tikhonov */ 3950aadb33SDmitri Tikhonovstruct stream_rec { 4050aadb33SDmitri Tikhonov struct lsquic_stream *sr_stream; 41c51ce338SDmitri Tikhonov unsigned short sr_off, 42c51ce338SDmitri Tikhonov sr_len; 43bfc7bfd8SDmitri Tikhonov enum quic_ft_bit sr_frame_types:16; 4450aadb33SDmitri Tikhonov}; 4550aadb33SDmitri Tikhonov 4650aadb33SDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_types) 4750aadb33SDmitri Tikhonov 4850aadb33SDmitri Tikhonovstruct stream_rec_arr { 49c51ce338SDmitri Tikhonov TAILQ_ENTRY(stream_rec_arr) next_stream_rec_arr; 5050aadb33SDmitri Tikhonov struct stream_rec srecs[ 5150aadb33SDmitri Tikhonov ( 64 /* Efficient size for malo allocator */ 52c51ce338SDmitri Tikhonov - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */ 5350aadb33SDmitri Tikhonov ) / sizeof(struct stream_rec) 5450aadb33SDmitri Tikhonov ]; 5550aadb33SDmitri Tikhonov}; 5650aadb33SDmitri Tikhonov 57c51ce338SDmitri TikhonovTAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr); 58c51ce338SDmitri Tikhonov 5950aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out 6050aadb33SDmitri Tikhonov{ 6150aadb33SDmitri Tikhonov /* `po_next' is used for packets_out, unacked_packets and expired_packets 6250aadb33SDmitri Tikhonov * lists. 6350aadb33SDmitri Tikhonov */ 6450aadb33SDmitri Tikhonov TAILQ_ENTRY(lsquic_packet_out) 6550aadb33SDmitri Tikhonov po_next; 6650aadb33SDmitri Tikhonov lsquic_time_t po_sent; /* Time sent */ 6750aadb33SDmitri Tikhonov lsquic_packno_t po_packno; 6850aadb33SDmitri Tikhonov 69bfc7bfd8SDmitri Tikhonov enum packet_out_flags { 70bfc7bfd8SDmitri Tikhonov PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 71bfc7bfd8SDmitri Tikhonov PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 72bfc7bfd8SDmitri Tikhonov PO_SREC_ARR = (1 << 4), 73bfc7bfd8SDmitri Tikhonov#define POBIT_SHIFT 5 74bfc7bfd8SDmitri Tikhonov PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 75bfc7bfd8SDmitri Tikhonov PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 76bfc7bfd8SDmitri Tikhonov PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 77bfc7bfd8SDmitri Tikhonov PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 78bfc7bfd8SDmitri Tikhonov PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 79bfc7bfd8SDmitri Tikhonov PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 80bfc7bfd8SDmitri Tikhonov PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 81bfc7bfd8SDmitri Tikhonov PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 82bfc7bfd8SDmitri Tikhonov PO_STREAM_END 83bfc7bfd8SDmitri Tikhonov = (1 <<13), /* STREAM frame reaches the end of the packet: no 84bfc7bfd8SDmitri Tikhonov * further writes are allowed. 85bfc7bfd8SDmitri Tikhonov */ 86bfc7bfd8SDmitri Tikhonov PO_SCHED = (1 <<14), /* On scheduled queue */ 8716a9b66aSDmitri Tikhonov PO_SENT_SZ = (1 <<15), 889626cfc2SDmitri Tikhonov PO_LONGHEAD = (1 <<16), 899626cfc2SDmitri Tikhonov PO_GQUIC = (1 <<17), /* Used for logging */ 909626cfc2SDmitri Tikhonov#define POLEV_SHIFT 18 919626cfc2SDmitri Tikhonov PO_BITS_2 = (1 <<18), /* PO_BITS_2 and PO_BITS_3 encode the */ 929626cfc2SDmitri Tikhonov PO_BITS_3 = (1 <<19), /* crypto level. Used for logging. */ 939626cfc2SDmitri Tikhonov } po_flags; 94bfc7bfd8SDmitri Tikhonov enum quic_ft_bit po_frame_types:16; /* Bitmask of QUIC_FRAME_* */ 95bfc7bfd8SDmitri Tikhonov unsigned short po_data_sz; /* Number of usable bytes in data */ 96bfc7bfd8SDmitri Tikhonov unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 9716a9b66aSDmitri Tikhonov unsigned short po_sent_sz; /* If PO_SENT_SZ is set, real size of sent buffer. */ 98bfc7bfd8SDmitri Tikhonov unsigned short po_regen_sz; /* Number of bytes at the beginning 99bfc7bfd8SDmitri Tikhonov * of data containing bytes that are 100bfc7bfd8SDmitri Tikhonov * not to be retransmitted, e.g. ACK 101bfc7bfd8SDmitri Tikhonov * frames. 102bfc7bfd8SDmitri Tikhonov */ 103bfc7bfd8SDmitri Tikhonov unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 1049626cfc2SDmitri Tikhonov enum header_type po_header_type:8; 105bfc7bfd8SDmitri Tikhonov unsigned char *po_data; 10616a9b66aSDmitri Tikhonov lsquic_packno_t po_ack2ed; /* If packet has ACK frame, value of 10716a9b66aSDmitri Tikhonov * largest acked in it. 10816a9b66aSDmitri Tikhonov */ 109bfc7bfd8SDmitri Tikhonov 11050aadb33SDmitri Tikhonov /* A lot of packets contain data belonging to only one stream. Thus, 111c51ce338SDmitri Tikhonov * `one' is used first. If this is not enough, any number of 11250aadb33SDmitri Tikhonov * stream_rec_arr structures can be allocated to handle more stream 11350aadb33SDmitri Tikhonov * records. 11450aadb33SDmitri Tikhonov */ 115c51ce338SDmitri Tikhonov union { 116c51ce338SDmitri Tikhonov struct stream_rec one; 117c51ce338SDmitri Tikhonov struct stream_rec_arr_tailq arr; 118c51ce338SDmitri Tikhonov } po_srecs; 11950aadb33SDmitri Tikhonov 12050aadb33SDmitri Tikhonov /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 12150aadb33SDmitri Tikhonov * data. 12250aadb33SDmitri Tikhonov */ 12350aadb33SDmitri Tikhonov unsigned char *po_enc_data; 12450aadb33SDmitri Tikhonov 12550aadb33SDmitri Tikhonov lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 12650aadb33SDmitri Tikhonov unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 12750aadb33SDmitri Tikhonov} lsquic_packet_out_t; 12850aadb33SDmitri Tikhonov 12950aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced: 13050aadb33SDmitri Tikhonov * 13150aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version 13250aadb33SDmitri Tikhonov * in po_flags. The cost is a bit of complexity. This will save us four bytes. 13350aadb33SDmitri Tikhonov */ 13450aadb33SDmitri Tikhonov 13550aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \ 13650aadb33SDmitri Tikhonov ((p)->po_n_alloc - (p)->po_data_sz)) 13750aadb33SDmitri Tikhonov 13850aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 13950aadb33SDmitri Tikhonov 140bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_set_packno_bits(p, b) do { \ 141bfc7bfd8SDmitri Tikhonov (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 142bfc7bfd8SDmitri Tikhonov (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 143bfc7bfd8SDmitri Tikhonov} while (0) 144bfc7bfd8SDmitri Tikhonov 1459626cfc2SDmitri Tikhonov#define lsquic_po_header_length(lconn, po_flags) ( \ 1469626cfc2SDmitri Tikhonov lconn->cn_pf->pf_packout_header_size(lconn, po_flags)) 14750aadb33SDmitri Tikhonov 1489626cfc2SDmitri Tikhonov#define lsquic_packet_out_total_sz(lconn, p) (\ 1499626cfc2SDmitri Tikhonov lconn->cn_pf->pf_packout_size(lconn, p)) 150bfc7bfd8SDmitri Tikhonov 15116a9b66aSDmitri Tikhonov#if __GNUC__ 15216a9b66aSDmitri Tikhonov#if LSQUIC_EXTRA_CHECKS 1539626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) ( \ 15416a9b66aSDmitri Tikhonov __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 1559626cfc2SDmitri Tikhonov (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)), \ 1569626cfc2SDmitri Tikhonov (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p)) 15716a9b66aSDmitri Tikhonov# else 1589626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) ( \ 15916a9b66aSDmitri Tikhonov __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 1609626cfc2SDmitri Tikhonov (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 16116a9b66aSDmitri Tikhonov#endif 16216a9b66aSDmitri Tikhonov#else 1639626cfc2SDmitri Tikhonov# define lsquic_packet_out_sent_sz(lconn, p) ( \ 16416a9b66aSDmitri Tikhonov (p)->po_flags & PO_SENT_SZ ? \ 1659626cfc2SDmitri Tikhonov (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 16616a9b66aSDmitri Tikhonov#endif 16716a9b66aSDmitri Tikhonov 16850aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \ 16950aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG)) 17050aadb33SDmitri Tikhonov 17150aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \ 17250aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == PO_NOENCRYPT ) 17350aadb33SDmitri Tikhonov 1749626cfc2SDmitri Tikhonov#define lsquic_packet_out_set_enc_level(p, level) do { \ 1759626cfc2SDmitri Tikhonov (p)->po_flags &= ~(3 << POLEV_SHIFT); \ 1769626cfc2SDmitri Tikhonov (p)->po_flags |= level << POLEV_SHIFT; \ 1779626cfc2SDmitri Tikhonov} while (0) 1789626cfc2SDmitri Tikhonov 1799626cfc2SDmitri Tikhonov#define lsquic_packet_out_enc_level(p) (((p)->po_flags >> POLEV_SHIFT) & 3) 1809626cfc2SDmitri Tikhonov 18150aadb33SDmitri Tikhonovstruct packet_out_srec_iter { 18250aadb33SDmitri Tikhonov lsquic_packet_out_t *packet_out; 18350aadb33SDmitri Tikhonov struct stream_rec_arr *cur_srec_arr; 18450aadb33SDmitri Tikhonov unsigned srec_idx; 185c51ce338SDmitri Tikhonov int impl_idx; 18650aadb33SDmitri Tikhonov}; 18750aadb33SDmitri Tikhonov 18850aadb33SDmitri Tikhonovstruct stream_rec * 18950aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *); 19050aadb33SDmitri Tikhonov 19150aadb33SDmitri Tikhonovstruct stream_rec * 19250aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi); 19350aadb33SDmitri Tikhonov 19450aadb33SDmitri Tikhonovlsquic_packet_out_t * 19550aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 1969626cfc2SDmitri Tikhonov const struct lsquic_conn *, enum lsquic_packno_bits, 19750aadb33SDmitri Tikhonov const lsquic_ver_tag_t *, const unsigned char *nonce); 19850aadb33SDmitri Tikhonov 19950aadb33SDmitri Tikhonovvoid 20050aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *, 20150aadb33SDmitri Tikhonov struct lsquic_engine_public *); 20250aadb33SDmitri Tikhonov 20350aadb33SDmitri Tikhonovint 20450aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 20550aadb33SDmitri Tikhonov struct lsquic_mm *mm, 20650aadb33SDmitri Tikhonov struct lsquic_stream *new_stream, 20750aadb33SDmitri Tikhonov enum QUIC_FRAME_TYPE, 208c51ce338SDmitri Tikhonov unsigned short off, unsigned short len); 20950aadb33SDmitri Tikhonov 210bfc7bfd8SDmitri Tikhonovunsigned 211c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t); 212c51ce338SDmitri Tikhonov 213c51ce338SDmitri Tikhonovint 214c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *, 215c51ce338SDmitri Tikhonov lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes); 21650aadb33SDmitri Tikhonov 21750aadb33SDmitri Tikhonovvoid 21850aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *); 21950aadb33SDmitri Tikhonov 220c51ce338SDmitri Tikhonovint 221c51ce338SDmitri Tikhonovlsquic_packet_out_has_frame (struct lsquic_packet_out *, 222c51ce338SDmitri Tikhonov const struct lsquic_stream *, enum QUIC_FRAME_TYPE); 223c51ce338SDmitri Tikhonov 224c51ce338SDmitri Tikhonovint 225c51ce338SDmitri Tikhonovlsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *); 226c51ce338SDmitri Tikhonov 227c51ce338SDmitri Tikhonovvoid 228c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *); 229c51ce338SDmitri Tikhonov 230c51ce338SDmitri Tikhonovvoid 231c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *); 232c51ce338SDmitri Tikhonov 233c51ce338SDmitri Tikhonovsize_t 234c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *); 235c51ce338SDmitri Tikhonov 236c51ce338SDmitri Tikhonovint 237c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 238c51ce338SDmitri Tikhonov const struct parse_funcs *, const struct lsquic_stream *); 239c51ce338SDmitri Tikhonov 24050aadb33SDmitri Tikhonov#endif 241