lsquic_packet_out.h revision bfc7bfd8
1/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_packet_out.h -- Structure and routines dealing with packet_out 4 */ 5 6#ifndef LSQUIC_PACKET_OUT_H 7#define LSQUIC_PACKET_OUT_H 1 8 9#include <sys/queue.h> 10 11struct malo; 12struct lsquic_engine_public; 13struct lsquic_mm; 14struct lsquic_stream; 15struct parse_funcs; 16 17/* Each stream_rec is associated with one packet_out. packet_out can have 18 * zero or more stream_rec structures. stream_rec keeps a pointer to a stream 19 * that has STREAM or RST_STREAM frames inside packet_out. `sr_frame_types' 20 * is a bitmask that records which of these two frames are in the packet. 21 * If this value is zero, values of the other struct members are not valid. 22 * `sr_off' indicates where inside packet_out->po_data STREAM frame begins 23 * and `sr_len' is its length. These values are not kept for RST_STREAM 24 * frames. 25 * 26 * We need this information for three reasons: 27 * 1. A stream is not destroyed until all of its STREAM and RST_STREAM 28 * frames are acknowledged. This is to make sure that we do not exceed 29 * maximum allowed number of streams. 30 * 2. When a packet is resubmitted, STREAM frames for a stream that has 31 * been reset are not to be resubmitted. 32 * 3. A buffered packet may have to be split before it is scheduled (this 33 * occurs if we guessed incorrectly the number of bytes required to 34 * encode the packet number and the actual number would make packet 35 * larger than the max). 36 * 37 */ 38struct stream_rec { 39 struct lsquic_stream *sr_stream; 40 unsigned short sr_off, 41 sr_len; 42 enum quic_ft_bit sr_frame_types:16; 43}; 44 45#define srec_taken(srec) ((srec)->sr_frame_types) 46 47struct stream_rec_arr { 48 TAILQ_ENTRY(stream_rec_arr) next_stream_rec_arr; 49 struct stream_rec srecs[ 50 ( 64 /* Efficient size for malo allocator */ 51 - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */ 52 ) / sizeof(struct stream_rec) 53 ]; 54}; 55 56TAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr); 57 58typedef struct lsquic_packet_out 59{ 60 /* `po_next' is used for packets_out, unacked_packets and expired_packets 61 * lists. 62 */ 63 TAILQ_ENTRY(lsquic_packet_out) 64 po_next; 65 lsquic_time_t po_sent; /* Time sent */ 66 lsquic_packno_t po_packno; 67 68 enum packet_out_flags { 69 PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 70 PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 71 PO_SREC_ARR = (1 << 4), 72#define POBIT_SHIFT 5 73 PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 74 PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 75 PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 76 PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 77 PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 78 PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 79 PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 80 PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 81 PO_STREAM_END 82 = (1 <<13), /* STREAM frame reaches the end of the packet: no 83 * further writes are allowed. 84 */ 85 PO_SCHED = (1 <<14), /* On scheduled queue */ 86 } po_flags:16; 87 enum quic_ft_bit po_frame_types:16; /* Bitmask of QUIC_FRAME_* */ 88 unsigned short po_data_sz; /* Number of usable bytes in data */ 89 unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 90 unsigned short po_regen_sz; /* Number of bytes at the beginning 91 * of data containing bytes that are 92 * not to be retransmitted, e.g. ACK 93 * frames. 94 */ 95 unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 96 unsigned char *po_data; 97 98 /* A lot of packets contain data belonging to only one stream. Thus, 99 * `one' is used first. If this is not enough, any number of 100 * stream_rec_arr structures can be allocated to handle more stream 101 * records. 102 */ 103 union { 104 struct stream_rec one; 105 struct stream_rec_arr_tailq arr; 106 } po_srecs; 107 108 /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 109 * data. 110 */ 111 unsigned char *po_enc_data; 112 113 lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 114 unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 115} lsquic_packet_out_t; 116 117/* The size of lsquic_packet_out_t could be further reduced: 118 * 119 * po_ver_tag could be encoded as a few bits representing enum lsquic_version 120 * in po_flags. The cost is a bit of complexity. This will save us four bytes. 121 */ 122 123#define lsquic_packet_out_avail(p) ((unsigned short) \ 124 ((p)->po_n_alloc - (p)->po_data_sz)) 125 126#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 127 128#define lsquic_packet_out_set_packno_bits(p, b) do { \ 129 (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 130 (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 131} while (0) 132 133#define lsquic_po_header_length(po_flags) ( \ 134 1 /* Type */ \ 135 + (!!((po_flags) & PO_CONN_ID) << 3) /* Connection ID */ \ 136 + (!!((po_flags) & PO_VERSION) << 2) /* Version */ \ 137 + (!!((po_flags) & PO_NONCE) << 5) /* Nonce */ \ 138 + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3) /* Packet number */ \ 139) 140 141#define lsquic_packet_out_total_sz(p) \ 142 ((p)->po_data_sz + lsquic_po_header_length((p)->po_flags) \ 143 + QUIC_PACKET_HASH_SZ) 144 145#define lsquic_packet_out_verneg(p) \ 146 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG)) 147 148#define lsquic_packet_out_pubres(p) \ 149 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == PO_NOENCRYPT ) 150 151struct packet_out_srec_iter { 152 lsquic_packet_out_t *packet_out; 153 struct stream_rec_arr *cur_srec_arr; 154 unsigned srec_idx; 155 int impl_idx; 156}; 157 158struct stream_rec * 159posi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *); 160 161struct stream_rec * 162posi_next (struct packet_out_srec_iter *posi); 163 164lsquic_packet_out_t * 165lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 166 unsigned short size, enum lsquic_packno_bits, 167 const lsquic_ver_tag_t *, const unsigned char *nonce); 168 169void 170lsquic_packet_out_destroy (lsquic_packet_out_t *, 171 struct lsquic_engine_public *); 172 173int 174lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 175 struct lsquic_mm *mm, 176 struct lsquic_stream *new_stream, 177 enum QUIC_FRAME_TYPE, 178 unsigned short off, unsigned short len); 179 180unsigned 181lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t); 182 183int 184lsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *, 185 lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes); 186 187void 188lsquic_packet_out_chop_regen (lsquic_packet_out_t *); 189 190int 191lsquic_packet_out_has_frame (struct lsquic_packet_out *, 192 const struct lsquic_stream *, enum QUIC_FRAME_TYPE); 193 194int 195lsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *); 196 197void 198lsquic_packet_out_ack_streams (struct lsquic_packet_out *); 199 200void 201lsquic_packet_out_zero_pad (struct lsquic_packet_out *); 202 203size_t 204lsquic_packet_out_mem_used (const struct lsquic_packet_out *); 205 206int 207lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 208 const struct parse_funcs *, const struct lsquic_stream *); 209 210#endif 211