lsquic_packet_out.h revision bfc7bfd8
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 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; 1250aadb33SDmitri Tikhonovstruct lsquic_engine_public; 1350aadb33SDmitri Tikhonovstruct lsquic_mm; 1450aadb33SDmitri Tikhonovstruct lsquic_stream; 1550aadb33SDmitri Tikhonovstruct parse_funcs; 1650aadb33SDmitri Tikhonov 1750aadb33SDmitri Tikhonov/* Each stream_rec is associated with one packet_out. packet_out can have 1850aadb33SDmitri Tikhonov * zero or more stream_rec structures. stream_rec keeps a pointer to a stream 1950aadb33SDmitri Tikhonov * that has STREAM or RST_STREAM frames inside packet_out. `sr_frame_types' 2050aadb33SDmitri Tikhonov * is a bitmask that records which of these two frames are in the packet. 21c51ce338SDmitri Tikhonov * If this value is zero, values of the other struct members are not valid. 22c51ce338SDmitri Tikhonov * `sr_off' indicates where inside packet_out->po_data STREAM frame begins 23c51ce338SDmitri Tikhonov * and `sr_len' is its length. These values are not kept for RST_STREAM 24c51ce338SDmitri Tikhonov * frames. 2550aadb33SDmitri Tikhonov * 26c51ce338SDmitri Tikhonov * We need this information for three reasons: 2750aadb33SDmitri Tikhonov * 1. A stream is not destroyed until all of its STREAM and RST_STREAM 2850aadb33SDmitri Tikhonov * frames are acknowledged. This is to make sure that we do not exceed 2950aadb33SDmitri Tikhonov * maximum allowed number of streams. 3050aadb33SDmitri Tikhonov * 2. When a packet is resubmitted, STREAM frames for a stream that has 3150aadb33SDmitri Tikhonov * been reset are not to be resubmitted. 32c51ce338SDmitri Tikhonov * 3. A buffered packet may have to be split before it is scheduled (this 33c51ce338SDmitri Tikhonov * occurs if we guessed incorrectly the number of bytes required to 34c51ce338SDmitri Tikhonov * encode the packet number and the actual number would make packet 35c51ce338SDmitri Tikhonov * larger than the max). 36c51ce338SDmitri Tikhonov * 3750aadb33SDmitri Tikhonov */ 3850aadb33SDmitri Tikhonovstruct stream_rec { 3950aadb33SDmitri Tikhonov struct lsquic_stream *sr_stream; 40c51ce338SDmitri Tikhonov unsigned short sr_off, 41c51ce338SDmitri Tikhonov sr_len; 42bfc7bfd8SDmitri Tikhonov enum quic_ft_bit sr_frame_types:16; 4350aadb33SDmitri Tikhonov}; 4450aadb33SDmitri Tikhonov 4550aadb33SDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_types) 4650aadb33SDmitri Tikhonov 4750aadb33SDmitri Tikhonovstruct stream_rec_arr { 48c51ce338SDmitri Tikhonov TAILQ_ENTRY(stream_rec_arr) next_stream_rec_arr; 4950aadb33SDmitri Tikhonov struct stream_rec srecs[ 5050aadb33SDmitri Tikhonov ( 64 /* Efficient size for malo allocator */ 51c51ce338SDmitri Tikhonov - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */ 5250aadb33SDmitri Tikhonov ) / sizeof(struct stream_rec) 5350aadb33SDmitri Tikhonov ]; 5450aadb33SDmitri Tikhonov}; 5550aadb33SDmitri Tikhonov 56c51ce338SDmitri TikhonovTAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr); 57c51ce338SDmitri Tikhonov 5850aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out 5950aadb33SDmitri Tikhonov{ 6050aadb33SDmitri Tikhonov /* `po_next' is used for packets_out, unacked_packets and expired_packets 6150aadb33SDmitri Tikhonov * lists. 6250aadb33SDmitri Tikhonov */ 6350aadb33SDmitri Tikhonov TAILQ_ENTRY(lsquic_packet_out) 6450aadb33SDmitri Tikhonov po_next; 6550aadb33SDmitri Tikhonov lsquic_time_t po_sent; /* Time sent */ 6650aadb33SDmitri Tikhonov lsquic_packno_t po_packno; 6750aadb33SDmitri Tikhonov 68bfc7bfd8SDmitri Tikhonov enum packet_out_flags { 69bfc7bfd8SDmitri Tikhonov PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 70bfc7bfd8SDmitri Tikhonov PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 71bfc7bfd8SDmitri Tikhonov PO_SREC_ARR = (1 << 4), 72bfc7bfd8SDmitri Tikhonov#define POBIT_SHIFT 5 73bfc7bfd8SDmitri Tikhonov PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 74bfc7bfd8SDmitri Tikhonov PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 75bfc7bfd8SDmitri Tikhonov PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 76bfc7bfd8SDmitri Tikhonov PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 77bfc7bfd8SDmitri Tikhonov PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 78bfc7bfd8SDmitri Tikhonov PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 79bfc7bfd8SDmitri Tikhonov PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 80bfc7bfd8SDmitri Tikhonov PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 81bfc7bfd8SDmitri Tikhonov PO_STREAM_END 82bfc7bfd8SDmitri Tikhonov = (1 <<13), /* STREAM frame reaches the end of the packet: no 83bfc7bfd8SDmitri Tikhonov * further writes are allowed. 84bfc7bfd8SDmitri Tikhonov */ 85bfc7bfd8SDmitri Tikhonov PO_SCHED = (1 <<14), /* On scheduled queue */ 86bfc7bfd8SDmitri Tikhonov } po_flags:16; 87bfc7bfd8SDmitri Tikhonov enum quic_ft_bit po_frame_types:16; /* Bitmask of QUIC_FRAME_* */ 88bfc7bfd8SDmitri Tikhonov unsigned short po_data_sz; /* Number of usable bytes in data */ 89bfc7bfd8SDmitri Tikhonov unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 90bfc7bfd8SDmitri Tikhonov unsigned short po_regen_sz; /* Number of bytes at the beginning 91bfc7bfd8SDmitri Tikhonov * of data containing bytes that are 92bfc7bfd8SDmitri Tikhonov * not to be retransmitted, e.g. ACK 93bfc7bfd8SDmitri Tikhonov * frames. 94bfc7bfd8SDmitri Tikhonov */ 95bfc7bfd8SDmitri Tikhonov unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 96bfc7bfd8SDmitri Tikhonov unsigned char *po_data; 97bfc7bfd8SDmitri Tikhonov 9850aadb33SDmitri Tikhonov /* A lot of packets contain data belonging to only one stream. Thus, 99c51ce338SDmitri Tikhonov * `one' is used first. If this is not enough, any number of 10050aadb33SDmitri Tikhonov * stream_rec_arr structures can be allocated to handle more stream 10150aadb33SDmitri Tikhonov * records. 10250aadb33SDmitri Tikhonov */ 103c51ce338SDmitri Tikhonov union { 104c51ce338SDmitri Tikhonov struct stream_rec one; 105c51ce338SDmitri Tikhonov struct stream_rec_arr_tailq arr; 106c51ce338SDmitri Tikhonov } po_srecs; 10750aadb33SDmitri Tikhonov 10850aadb33SDmitri Tikhonov /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 10950aadb33SDmitri Tikhonov * data. 11050aadb33SDmitri Tikhonov */ 11150aadb33SDmitri Tikhonov unsigned char *po_enc_data; 11250aadb33SDmitri Tikhonov 11350aadb33SDmitri Tikhonov lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 11450aadb33SDmitri Tikhonov unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 11550aadb33SDmitri Tikhonov} lsquic_packet_out_t; 11650aadb33SDmitri Tikhonov 11750aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced: 11850aadb33SDmitri Tikhonov * 11950aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version 12050aadb33SDmitri Tikhonov * in po_flags. The cost is a bit of complexity. This will save us four bytes. 12150aadb33SDmitri Tikhonov */ 12250aadb33SDmitri Tikhonov 12350aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \ 12450aadb33SDmitri Tikhonov ((p)->po_n_alloc - (p)->po_data_sz)) 12550aadb33SDmitri Tikhonov 12650aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 12750aadb33SDmitri Tikhonov 128bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_set_packno_bits(p, b) do { \ 129bfc7bfd8SDmitri Tikhonov (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 130bfc7bfd8SDmitri Tikhonov (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 131bfc7bfd8SDmitri Tikhonov} while (0) 132bfc7bfd8SDmitri Tikhonov 13350aadb33SDmitri Tikhonov#define lsquic_po_header_length(po_flags) ( \ 13450aadb33SDmitri Tikhonov 1 /* Type */ \ 13550aadb33SDmitri Tikhonov + (!!((po_flags) & PO_CONN_ID) << 3) /* Connection ID */ \ 13650aadb33SDmitri Tikhonov + (!!((po_flags) & PO_VERSION) << 2) /* Version */ \ 13750aadb33SDmitri Tikhonov + (!!((po_flags) & PO_NONCE) << 5) /* Nonce */ \ 13850aadb33SDmitri Tikhonov + packno_bits2len(((po_flags) >> POBIT_SHIFT) & 0x3) /* Packet number */ \ 13950aadb33SDmitri Tikhonov) 14050aadb33SDmitri Tikhonov 141bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_total_sz(p) \ 142bfc7bfd8SDmitri Tikhonov ((p)->po_data_sz + lsquic_po_header_length((p)->po_flags) \ 143bfc7bfd8SDmitri Tikhonov + QUIC_PACKET_HASH_SZ) 144bfc7bfd8SDmitri Tikhonov 14550aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \ 14650aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG)) 14750aadb33SDmitri Tikhonov 14850aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \ 14950aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == PO_NOENCRYPT ) 15050aadb33SDmitri Tikhonov 15150aadb33SDmitri Tikhonovstruct packet_out_srec_iter { 15250aadb33SDmitri Tikhonov lsquic_packet_out_t *packet_out; 15350aadb33SDmitri Tikhonov struct stream_rec_arr *cur_srec_arr; 15450aadb33SDmitri Tikhonov unsigned srec_idx; 155c51ce338SDmitri Tikhonov int impl_idx; 15650aadb33SDmitri Tikhonov}; 15750aadb33SDmitri Tikhonov 15850aadb33SDmitri Tikhonovstruct stream_rec * 15950aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *); 16050aadb33SDmitri Tikhonov 16150aadb33SDmitri Tikhonovstruct stream_rec * 16250aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi); 16350aadb33SDmitri Tikhonov 16450aadb33SDmitri Tikhonovlsquic_packet_out_t * 16550aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 16650aadb33SDmitri Tikhonov unsigned short size, enum lsquic_packno_bits, 16750aadb33SDmitri Tikhonov const lsquic_ver_tag_t *, const unsigned char *nonce); 16850aadb33SDmitri Tikhonov 16950aadb33SDmitri Tikhonovvoid 17050aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *, 17150aadb33SDmitri Tikhonov struct lsquic_engine_public *); 17250aadb33SDmitri Tikhonov 17350aadb33SDmitri Tikhonovint 17450aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 17550aadb33SDmitri Tikhonov struct lsquic_mm *mm, 17650aadb33SDmitri Tikhonov struct lsquic_stream *new_stream, 17750aadb33SDmitri Tikhonov enum QUIC_FRAME_TYPE, 178c51ce338SDmitri Tikhonov unsigned short off, unsigned short len); 17950aadb33SDmitri Tikhonov 180bfc7bfd8SDmitri Tikhonovunsigned 181c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t); 182c51ce338SDmitri Tikhonov 183c51ce338SDmitri Tikhonovint 184c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *, 185c51ce338SDmitri Tikhonov lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes); 18650aadb33SDmitri Tikhonov 18750aadb33SDmitri Tikhonovvoid 18850aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *); 18950aadb33SDmitri Tikhonov 190c51ce338SDmitri Tikhonovint 191c51ce338SDmitri Tikhonovlsquic_packet_out_has_frame (struct lsquic_packet_out *, 192c51ce338SDmitri Tikhonov const struct lsquic_stream *, enum QUIC_FRAME_TYPE); 193c51ce338SDmitri Tikhonov 194c51ce338SDmitri Tikhonovint 195c51ce338SDmitri Tikhonovlsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *); 196c51ce338SDmitri Tikhonov 197c51ce338SDmitri Tikhonovvoid 198c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *); 199c51ce338SDmitri Tikhonov 200c51ce338SDmitri Tikhonovvoid 201c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *); 202c51ce338SDmitri Tikhonov 203c51ce338SDmitri Tikhonovsize_t 204c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *); 205c51ce338SDmitri Tikhonov 206c51ce338SDmitri Tikhonovint 207c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 208c51ce338SDmitri Tikhonov const struct parse_funcs *, const struct lsquic_stream *); 209c51ce338SDmitri Tikhonov 21050aadb33SDmitri Tikhonov#endif 211