lsquic_packet_out.h revision b1a7c3f9
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 frame_rec is associated with one packet_out. packet_out can have 21 * zero or more frame_rec structures. frame_rec keeps a pointer to a stream 22 * that has STREAM, CRYPTO, or RST_STREAM frames inside packet_out. 23 * `fe_frame_type' specifies the type of the frame; if this value is zero 24 * (this happens when a frame is elided), values of the other struct members 25 * are not valid. `fe_off' indicates where inside packet_out->po_data the 26 * frame begins and `fe_len' is its length. 27 * 28 * We need this information for four reasons: 29 * 1. A stream is not destroyed until all of its STREAM and RST_STREAM 30 * frames are acknowledged. This is to make sure that we do not exceed 31 * maximum allowed number of streams. 32 * 2. When a packet is resubmitted, STREAM frames for a stream that has 33 * been reset are not to be resubmitted. 34 * 3. A buffered packet may have to be split before it is scheduled (this 35 * occurs if we guessed incorrectly the number of bytes required to 36 * encode the packet number and the actual number would make packet 37 * larger than the max). 38 * 4. A lost or scheduled packet may need to be resized (down) when path 39 * changes or MTU is reduced due to an RTO. 40 * 41 * In IETF, all frames are recorded. In gQUIC, only STREAM, RST_STREAM, 42 * ACK, and STOP_WAITING are recorded. The latter two are done so that 43 * ACK-deleting code in send controller (see po_regen_sz) is the same for 44 * both QUIC versions. 45 */ 46struct frame_rec { 47 union { 48 struct lsquic_stream *stream; 49 uintptr_t data; 50 } fe_u; 51#define fe_stream fe_u.stream 52 unsigned short fe_off, 53 fe_len; 54 enum quic_frame_type fe_frame_type; 55}; 56 57#define frec_taken(frec) ((frec)->fe_frame_type) 58 59struct frame_rec_arr { 60 TAILQ_ENTRY(frame_rec_arr) next_stream_rec_arr; 61 struct frame_rec frecs[ 62 ( 64 /* Efficient size for malo allocator */ 63 - sizeof(TAILQ_ENTRY(frame_rec)) /* next_stream_rec_arr */ 64 ) / sizeof(struct frame_rec) 65 ]; 66}; 67 68TAILQ_HEAD(frame_rec_arr_tailq, frame_rec_arr); 69 70 71typedef struct lsquic_packet_out 72{ 73 /* `po_next' is used for packets_out, unacked_packets and expired_packets 74 * lists. 75 */ 76 TAILQ_ENTRY(lsquic_packet_out) 77 po_next; 78 lsquic_time_t po_sent; /* Time sent */ 79 lsquic_packno_t po_packno; 80 lsquic_packno_t po_ack2ed; /* If packet has ACK frame, value of 81 * largest acked in it. 82 */ 83 struct lsquic_packet_out 84 *po_loss_chain; /* Circular linked list */ 85 86 enum quic_ft_bit po_frame_types; /* Bitmask of QUIC_FRAME_* */ 87 enum packet_out_flags { 88 /* TODO XXX Phase out PO_MINI in favor of a more specialized flag: 89 * we only need an indicator that a packet contains STREAM frames 90 * but no associated frecs. This type of packets in only created 91 * by GQUIC mini conn. 92 */ 93 PO_MINI = (1 << 0), /* Allocated by mini connection */ 94 PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 95 PO_SENT = (1 << 2), /* Packet has been sent (mini only) */ 96 PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 97 PO_FREC_ARR = (1 << 4), 98#define POBIT_SHIFT 5 99 PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 100 PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 101 PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 102 PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 103 PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 104 PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 105 PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 106 PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 107 PO_STREAM_END 108 = (1 <<13), /* STREAM frame reaches the end of the packet: no 109 * further writes are allowed. 110 */ 111 PO_SCHED = (1 <<14), /* On scheduled queue */ 112 PO_SENT_SZ = (1 <<15), 113 PO_LONGHEAD = (1 <<16), 114#define POIPv6_SHIFT 20 115 PO_IPv6 = (1 <<20), /* Set if pmi_allocate was passed is_ipv6=1, 116 * otherwise unset. 117 */ 118 PO_MTU_PROBE= (1 <<21), /* Special loss and ACK rules apply */ 119#define POPNS_SHIFT 22 120 PO_PNS_HSK = (1 <<22), /* PNS bits contain the value of the */ 121 PO_PNS_APP = (1 <<23), /* packet number space. */ 122 PO_RETRY = (1 <<24), /* Retry packet */ 123 PO_RETX = (1 <<25), /* Retransmitted packet: don't append to it */ 124 PO_POISON = (1 <<26), /* Used to detect opt-ACK attack */ 125 PO_LOSS_REC = (1 <<27), /* This structure is a loss record */ 126 /* Only one of PO_SCHED, PO_UNACKED, or PO_LOST can be set. If pressed 127 * for room in the enum, we can switch to using two bits to represent 128 * this information. 129 */ 130 PO_UNACKED = (1 <<28), /* On unacked queue */ 131 PO_LOST = (1 <<29), /* On lost queue */ 132#define POSPIN_SHIFT 30 133 PO_SPIN_BIT = (1 <<30), /* Value of the spin bit */ 134 } po_flags; 135 unsigned short po_data_sz; /* Number of usable bytes in data */ 136 unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 137 unsigned short po_sent_sz; /* If PO_SENT_SZ is set, real size of sent buffer. */ 138 /* TODO Revisit po_regen_sz once gQUIC is dropped. Now that all frames 139 * are recorded, we have more flexibility where to place ACK frames; they 140 * no longer really have to be at the beginning of the packet, since we 141 * can locate them. 142 */ 143 unsigned short po_regen_sz; /* Number of bytes at the beginning 144 * of data containing bytes that are 145 * not to be retransmitted, e.g. ACK 146 * frames. 147 */ 148 unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 149 unsigned short po_token_len; 150 enum header_type po_header_type:8; 151 unsigned char po_dcid_len; /* If PO_ENCRYPTED is set */ 152 enum { 153 POL_GQUIC = 1 << 0, /* Used for logging */ 154#define POLEV_SHIFT 1 155 POL_ELBIT_0 = 1 << 1, /* EL bits encode the crypto level. */ 156 POL_ELBIT_1 = 1 << 2, 157#define POKP_SHIFT 3 158 POL_KEY_PHASE= 1 << 3, 159#define POECN_SHIFT 4 160 POL_ECNBIT_0 = 1 << 4, 161 POL_ECNBIT_1 = 1 << 5, 162 POL_LOG_QL_BITS = 1 << 6, 163 POL_SQUARE_BIT = 1 << 7, 164 POL_LOSS_BIT = 1 << 8, 165#ifndef NDEBUG 166 POL_HEADER_PROT = 1 << 9, /* Header protection applied */ 167#endif 168 POL_LIMITED = 1 << 10, /* Used to credit sc_next_limit if needed. */ 169 } po_lflags:16; 170 unsigned char *po_data; 171 172 /* A lot of packets contain only one frame. Thus, `one' is used first. 173 * If this is not enough, any number of frame_rec_arr structures can be 174 * allocated to handle more frame records. 175 */ 176 union { 177 struct frame_rec one; 178 struct frame_rec_arr_tailq arr; 179 } po_frecs; 180 181 /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 182 * data. 183 */ 184 unsigned char *po_enc_data; 185 186 lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 187 unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 188 const struct network_path 189 *po_path; 190#define po_token po_nonce 191 struct bwp_state *po_bwp_state; 192} lsquic_packet_out_t; 193 194/* This is to make sure these bit names are not used, they are only for 195 * convenience in gdb output. 196 */ 197#define PO_PNS_HSK 198#define PO_PNS_APP 199 200/* The size of lsquic_packet_out_t could be further reduced: 201 * 202 * po_ver_tag could be encoded as a few bits representing enum lsquic_version 203 * in po_flags. The cost is a bit of complexity. This will save us four bytes. 204 */ 205 206#define lsquic_packet_out_avail(p) ((unsigned short) \ 207 ((p)->po_n_alloc - (p)->po_data_sz)) 208 209#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 210 211#define lsquic_packet_out_set_packno_bits(p, b) do { \ 212 (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 213 (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 214} while (0) 215 216#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1)) 217 218#define lsquic_packet_out_set_ipv6(p, b) do { \ 219 (p)->po_flags &= ~(1 << POIPv6_SHIFT); \ 220 (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT; \ 221} while (0) 222 223#define lsquic_packet_out_spin_bit(p) (((p)->po_flags & PO_SPIN_BIT) > 0) 224#define lsquic_packet_out_square_bit(p) (((p)->po_lflags & POL_SQUARE_BIT) > 0) 225#define lsquic_packet_out_loss_bit(p) (((p)->po_lflags & POL_LOSS_BIT) > 0) 226 227#define lsquic_packet_out_set_spin_bit(p, b) do { \ 228 (p)->po_flags &= ~PO_SPIN_BIT; \ 229 (p)->po_flags |= ((b) & 1) << POSPIN_SHIFT; \ 230} while (0) 231 232#define lsquic_po_header_length(lconn, po_flags, dcid_len) ( \ 233 lconn->cn_pf->pf_packout_max_header_size(lconn, po_flags, dcid_len)) 234 235#define lsquic_packet_out_total_sz(lconn, p) (\ 236 (lconn)->cn_pf->pf_packout_size(lconn, p)) 237 238#if __GNUC__ 239#if LSQUIC_EXTRA_CHECKS 240#define lsquic_packet_out_sent_sz(lconn, p) ( \ 241 __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 242 (assert(((p)->po_flags & PO_HELLO /* Avoid client DCID change */) \ 243 || (p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)), \ 244 (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p)) 245# else 246#define lsquic_packet_out_sent_sz(lconn, p) ( \ 247 __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 248 (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 249#endif 250#else 251# define lsquic_packet_out_sent_sz(lconn, p) ( \ 252 (p)->po_flags & PO_SENT_SZ ? \ 253 (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 254#endif 255 256#define lsquic_packet_out_verneg(p) \ 257 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_VERNEG)) 258 259#define lsquic_packet_out_pubres(p) \ 260 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == PO_NOENCRYPT ) 261 262#define lsquic_packet_out_retry(p) \ 263 (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG|PO_RETRY)) == (PO_NOENCRYPT|PO_RETRY) ) 264 265#define lsquic_packet_out_set_enc_level(p, level) do { \ 266 (p)->po_lflags &= ~(3 << POLEV_SHIFT); \ 267 (p)->po_lflags |= level << POLEV_SHIFT; \ 268} while (0) 269 270#define lsquic_packet_out_enc_level(p) (((p)->po_lflags >> POLEV_SHIFT) & 3) 271 272#define lsquic_packet_out_set_kp(p, kp) do { \ 273 (p)->po_lflags &= ~(1 << POKP_SHIFT); \ 274 (p)->po_lflags |= kp << POKP_SHIFT; \ 275} while (0) 276 277#define lsquic_packet_out_kp(p) (((p)->po_lflags >> POKP_SHIFT) & 1) 278 279#define lsquic_packet_out_set_pns(p, pns) do { \ 280 (p)->po_flags &= ~(3 << POPNS_SHIFT); \ 281 (p)->po_flags |= pns << POPNS_SHIFT; \ 282} while (0) 283 284#define lsquic_packet_out_pns(p) (((p)->po_flags >> POPNS_SHIFT) & 3) 285 286#define lsquic_packet_out_set_ecn(p, ecn) do { \ 287 (p)->po_lflags &= ~(3 << POECN_SHIFT); \ 288 (p)->po_lflags |= ecn << POECN_SHIFT; \ 289} while (0) 290 291#define lsquic_packet_out_ecn(p) (((p)->po_lflags >> POECN_SHIFT) & 3) 292 293struct packet_out_frec_iter { 294 lsquic_packet_out_t *packet_out; 295 struct frame_rec_arr *cur_frec_arr; 296 unsigned frec_idx; 297 int impl_idx; 298}; 299 300 301struct frame_rec * 302lsquic_pofi_first (struct packet_out_frec_iter *pofi, lsquic_packet_out_t *); 303 304struct frame_rec * 305lsquic_pofi_next (struct packet_out_frec_iter *pofi); 306 307lsquic_packet_out_t * 308lsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 309 const struct lsquic_conn *, enum packno_bits, 310 const lsquic_ver_tag_t *, const unsigned char *nonce, 311 const struct network_path *); 312 313void 314lsquic_packet_out_destroy (lsquic_packet_out_t *, 315 struct lsquic_engine_public *, void *peer_ctx); 316 317int 318lsquic_packet_out_add_frame (struct lsquic_packet_out *, 319 struct lsquic_mm *, uintptr_t data, enum quic_frame_type, 320 unsigned short off, unsigned short len); 321 322int 323lsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 324 struct lsquic_mm *mm, 325 struct lsquic_stream *new_stream, 326 enum quic_frame_type, 327 unsigned short off, unsigned short len); 328 329unsigned 330lsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, 331 lsquic_stream_id_t); 332 333void 334lsquic_packet_out_chop_regen (lsquic_packet_out_t *); 335 336void 337lsquic_packet_out_ack_streams (struct lsquic_packet_out *); 338 339void 340lsquic_packet_out_zero_pad (struct lsquic_packet_out *); 341 342size_t 343lsquic_packet_out_mem_used (const struct lsquic_packet_out *); 344 345int 346lsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 347 const struct parse_funcs *, const struct lsquic_stream *); 348 349int 350lsquic_packet_out_equal_dcids (const struct lsquic_packet_out *, 351 const struct lsquic_packet_out *); 352#endif 353