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