lsquic_packet_out.h revision 9fc12041
1/* Copyright (c) 2017 - 2020 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 network_path; 17struct parse_funcs; 18struct bwp_state; 19 20/* Each stream_rec is associated with one packet_out. packet_out can have 21 * zero or more stream_rec structures. stream_rec keeps a pointer to a stream 22 * that has STREAM or RST_STREAM frames inside packet_out. `sr_frame_type' 23 * specifies the type of the frame; if this value is zero, values of the 24 * other struct members are not valid. `sr_off' indicates where inside 25 * packet_out->po_data the frame begins and `sr_len' is its length. 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_frame_type sr_frame_type:16; 44}; 45 46#define srec_taken(srec) ((srec)->sr_frame_type) 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 59 60typedef struct lsquic_packet_out 61{ 62 /* `po_next' is used for packets_out, unacked_packets and expired_packets 63 * lists. 64 */ 65 TAILQ_ENTRY(lsquic_packet_out) 66 po_next; 67 lsquic_time_t po_sent; /* Time sent */ 68 lsquic_packno_t po_packno; 69 lsquic_packno_t po_ack2ed; /* If packet has ACK frame, value of 70 * largest acked in it. 71 */ 72 struct lsquic_packet_out 73 *po_loss_chain; /* Circular linked list */ 74 75 enum quic_ft_bit po_frame_types; /* Bitmask of QUIC_FRAME_* */ 76 enum packet_out_flags { 77 /* TODO XXX Phase out PO_MINI in favor of a more specialized flag: 78 * we only need an indicator that a packet contains STREAM frames 79 * but no associated srecs. This type of packets in only created 80 * by GQUIC mini conn. 81 */ 82 PO_MINI = (1 << 0), /* Allocated by mini connection */ 83 PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 84 PO_SENT = (1 << 2), /* Packet has been sent (mini only) */ 85 PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 86 PO_SREC_ARR = (1 << 4), 87#define POBIT_SHIFT 5 88 PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 89 PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 90 PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 91 PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 92 PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 93 PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 94 PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 95 PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 96 PO_STREAM_END 97 = (1 <<13), /* STREAM frame reaches the end of the packet: no 98 * further writes are allowed. 99 */ 100 PO_SCHED = (1 <<14), /* On scheduled queue */ 101 PO_SENT_SZ = (1 <<15), 102 PO_LONGHEAD = (1 <<16), 103#define POIPv6_SHIFT 20 104 PO_IPv6 = (1 <<20), /* Set if pmi_allocate was passed is_ipv6=1, 105 * otherwise unset. 106 */ 107 PO_LIMITED = (1 <<21), /* Used to credit sc_next_limit if needed. */ 108#define POPNS_SHIFT 22 109 PO_PNS_HSK = (1 <<22), /* PNS bits contain the value of the */ 110 PO_PNS_APP = (1 <<23), /* packet number space. */ 111 PO_RETRY = (1 <<24), /* Retry packet */ 112 PO_RETX = (1 <<25), /* Retransmitted packet: don't append to it */ 113 PO_POISON = (1 <<26), /* Used to detect opt-ACK attack */ 114 PO_LOSS_REC = (1 <<27), /* This structure is a loss record */ 115 /* Only one of PO_SCHED, PO_UNACKED, or PO_LOST can be set. If pressed 116 * for room in the enum, we can switch to using two bits to represent 117 * this information. 118 */ 119 PO_UNACKED = (1 <<28), /* On unacked queue */ 120 PO_LOST = (1 <<29), /* On lost queue */ 121#define POSPIN_SHIFT 30 122 PO_SPIN_BIT = (1 <<30), /* Value of the spin bit */ 123 } po_flags; 124 unsigned short po_data_sz; /* Number of usable bytes in data */ 125 unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 126 unsigned short po_sent_sz; /* If PO_SENT_SZ is set, real size of sent buffer. */ 127 unsigned short po_regen_sz; /* Number of bytes at the beginning 128 * of data containing bytes that are 129 * not to be retransmitted, e.g. ACK 130 * frames. 131 */ 132 unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 133 unsigned short po_token_len; 134 enum header_type po_header_type:8; 135 unsigned char po_path_id; 136 enum { 137 POL_GQUIC = 1 << 0, /* Used for logging */ 138#define POLEV_SHIFT 1 139 POL_ELBIT_0 = 1 << 1, /* EL bits encode the crypto level. */ 140 POL_ELBIT_1 = 1 << 2, 141#define POKP_SHIFT 3 142 POL_KEY_PHASE= 1 << 3, 143#define POECN_SHIFT 4 144 POL_ECNBIT_0 = 1 << 4, 145 POL_ECNBIT_1 = 1 << 5, 146 POL_LOG_QL_BITS = 1 << 6, 147 POL_SQUARE_BIT = 1 << 7, 148 POL_LOSS_BIT = 1 << 8, 149 } po_lflags:16; 150 unsigned char *po_data; 151 152 /* A lot of packets contain data belonging to only one stream. Thus, 153 * `one' is used first. If this is not enough, any number of 154 * stream_rec_arr structures can be allocated to handle more stream 155 * records. 156 */ 157 union { 158 struct stream_rec one; 159 struct stream_rec_arr_tailq arr; 160 } po_srecs; 161 162 /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 163 * data. 164 */ 165 unsigned char *po_enc_data; 166 167 lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 168 unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 169 const struct network_path 170 *po_path; 171#define po_token po_nonce 172 struct bwp_state *po_bwp_state; 173} lsquic_packet_out_t; 174 175/* This is to make sure these bit names are not used, they are only for 176 * convenience in gdb output. 177 */ 178#define PO_PNS_HSK 179#define PO_PNS_APP 180 181/* The size of lsquic_packet_out_t could be further reduced: 182 * 183 * po_ver_tag could be encoded as a few bits representing enum lsquic_version 184 * in po_flags. The cost is a bit of complexity. This will save us four bytes. 185 */ 186 187#define lsquic_packet_out_avail(p) ((unsigned short) \ 188 ((p)->po_n_alloc - (p)->po_data_sz)) 189 190#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 191 192#define lsquic_packet_out_set_packno_bits(p, b) do { \ 193 (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 194 (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 195} while (0) 196 197#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1)) 198 199#define lsquic_packet_out_set_ipv6(p, b) do { \ 200 (p)->po_flags &= ~(1 << POIPv6_SHIFT); \ 201 (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT; \ 202} while (0) 203 204#define lsquic_packet_out_spin_bit(p) (((p)->po_flags & PO_SPIN_BIT) > 0) 205#define lsquic_packet_out_square_bit(p) (((p)->po_lflags & POL_SQUARE_BIT) > 0) 206#define lsquic_packet_out_loss_bit(p) (((p)->po_lflags & POL_LOSS_BIT) > 0) 207 208#define lsquic_packet_out_set_spin_bit(p, b) do { \ 209 (p)->po_flags &= ~PO_SPIN_BIT; \ 210 (p)->po_flags |= ((b) & 1) << POSPIN_SHIFT; \ 211} while (0) 212 213#define lsquic_po_header_length(lconn, po_flags, dcid_len) ( \ 214 lconn->cn_pf->pf_packout_max_header_size(lconn, po_flags, dcid_len)) 215 216#define lsquic_packet_out_total_sz(lconn, p) (\ 217 lconn->cn_pf->pf_packout_size(lconn, p)) 218 219#if __GNUC__ 220#if LSQUIC_EXTRA_CHECKS 221#define lsquic_packet_out_sent_sz(lconn, p) ( \ 222 __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 223 (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)), \ 224 (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p)) 225# else 226#define lsquic_packet_out_sent_sz(lconn, p) ( \ 227 __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 228 (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 229#endif 230#else 231# define lsquic_packet_out_sent_sz(lconn, p) ( \ 232 (p)->po_flags & PO_SENT_SZ ? \ 233 (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 234#endif 235 236#define lsquic_packet_out_verneg(p) \ 237 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_VERNEG)) 238 239#define lsquic_packet_out_pubres(p) \ 240 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == PO_NOENCRYPT ) 241 242#define lsquic_packet_out_retry(p) \ 243 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_RETRY) ) 244 245#define lsquic_packet_out_set_enc_level(p, level) do { \ 246 (p)->po_lflags &= ~(3 << POLEV_SHIFT); \ 247 (p)->po_lflags |= level << POLEV_SHIFT; \ 248} while (0) 249 250#define lsquic_packet_out_enc_level(p) (((p)->po_lflags >> POLEV_SHIFT) & 3) 251 252#define lsquic_packet_out_set_kp(p, kp) do { \ 253 (p)->po_lflags &= ~(1 << POKP_SHIFT); \ 254 (p)->po_lflags |= kp << POKP_SHIFT; \ 255} while (0) 256 257#define lsquic_packet_out_kp(p) (((p)->po_lflags >> POKP_SHIFT) & 1) 258 259#define lsquic_packet_out_set_pns(p, pns) do { \ 260 (p)->po_flags &= ~(3 << POPNS_SHIFT); \ 261 (p)->po_flags |= pns << POPNS_SHIFT; \ 262} while (0) 263 264#define lsquic_packet_out_pns(p) (((p)->po_flags >> POPNS_SHIFT) & 3) 265 266#define lsquic_packet_out_set_ecn(p, ecn) do { \ 267 (p)->po_lflags &= ~(3 << POECN_SHIFT); \ 268 (p)->po_lflags |= ecn << POECN_SHIFT; \ 269} while (0) 270 271#define lsquic_packet_out_ecn(p) (((p)->po_lflags >> POECN_SHIFT) & 3) 272 273struct packet_out_srec_iter { 274 lsquic_packet_out_t *packet_out; 275 struct stream_rec_arr *cur_srec_arr; 276 unsigned srec_idx; 277 int impl_idx; 278}; 279 280 281struct stream_rec * 282posi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *); 283 284struct stream_rec * 285posi_next (struct packet_out_srec_iter *posi); 286 287lsquic_packet_out_t * 288lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 289 const struct lsquic_conn *, enum packno_bits, 290 const lsquic_ver_tag_t *, const unsigned char *nonce, 291 const struct network_path *); 292 293void 294lsquic_packet_out_destroy (lsquic_packet_out_t *, 295 struct lsquic_engine_public *, void *peer_ctx); 296 297int 298lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 299 struct lsquic_mm *mm, 300 struct lsquic_stream *new_stream, 301 enum quic_frame_type, 302 unsigned short off, unsigned short len); 303 304unsigned 305lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, 306 lsquic_stream_id_t); 307 308int 309lsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *, 310 lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes); 311 312void 313lsquic_packet_out_chop_regen (lsquic_packet_out_t *); 314 315void 316lsquic_packet_out_ack_streams (struct lsquic_packet_out *); 317 318void 319lsquic_packet_out_zero_pad (struct lsquic_packet_out *); 320 321size_t 322lsquic_packet_out_mem_used (const struct lsquic_packet_out *); 323 324int 325lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 326 const struct parse_funcs *, const struct lsquic_stream *); 327 328#endif 329