lsquic_packet_out.h revision 8252b0b9
110c492f0SDmitri Tikhonov/* Copyright (c) 2017 - 2018 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; 129626cfc2SDmitri Tikhonovstruct lsquic_conn; 1350aadb33SDmitri Tikhonovstruct lsquic_engine_public; 1450aadb33SDmitri Tikhonovstruct lsquic_mm; 1550aadb33SDmitri Tikhonovstruct lsquic_stream; 1650aadb33SDmitri Tikhonovstruct parse_funcs; 1750aadb33SDmitri Tikhonov 1850aadb33SDmitri Tikhonov/* Each stream_rec is associated with one packet_out. packet_out can have 1950aadb33SDmitri Tikhonov * zero or more stream_rec structures. stream_rec keeps a pointer to a stream 2050aadb33SDmitri Tikhonov * that has STREAM or RST_STREAM frames inside packet_out. `sr_frame_types' 2150aadb33SDmitri Tikhonov * is a bitmask that records which of these two frames are in the packet. 22c51ce338SDmitri Tikhonov * If this value is zero, values of the other struct members are not valid. 23c51ce338SDmitri Tikhonov * `sr_off' indicates where inside packet_out->po_data STREAM frame begins 24c51ce338SDmitri Tikhonov * and `sr_len' is its length. These values are not kept for RST_STREAM 25c51ce338SDmitri Tikhonov * frames. 2650aadb33SDmitri Tikhonov * 27c51ce338SDmitri Tikhonov * We need this information for three reasons: 2850aadb33SDmitri Tikhonov * 1. A stream is not destroyed until all of its STREAM and RST_STREAM 2950aadb33SDmitri Tikhonov * frames are acknowledged. This is to make sure that we do not exceed 3050aadb33SDmitri Tikhonov * maximum allowed number of streams. 3150aadb33SDmitri Tikhonov * 2. When a packet is resubmitted, STREAM frames for a stream that has 3250aadb33SDmitri Tikhonov * been reset are not to be resubmitted. 33c51ce338SDmitri Tikhonov * 3. A buffered packet may have to be split before it is scheduled (this 34c51ce338SDmitri Tikhonov * occurs if we guessed incorrectly the number of bytes required to 35c51ce338SDmitri Tikhonov * encode the packet number and the actual number would make packet 36c51ce338SDmitri Tikhonov * larger than the max). 37c51ce338SDmitri Tikhonov * 3850aadb33SDmitri Tikhonov */ 3950aadb33SDmitri Tikhonovstruct stream_rec { 4050aadb33SDmitri Tikhonov struct lsquic_stream *sr_stream; 41c51ce338SDmitri Tikhonov unsigned short sr_off, 42c51ce338SDmitri Tikhonov sr_len; 43bfc7bfd8SDmitri Tikhonov enum quic_ft_bit sr_frame_types:16; 4450aadb33SDmitri Tikhonov}; 4550aadb33SDmitri Tikhonov 4650aadb33SDmitri Tikhonov#define srec_taken(srec) ((srec)->sr_frame_types) 4750aadb33SDmitri Tikhonov 4850aadb33SDmitri Tikhonovstruct stream_rec_arr { 49c51ce338SDmitri Tikhonov TAILQ_ENTRY(stream_rec_arr) next_stream_rec_arr; 5050aadb33SDmitri Tikhonov struct stream_rec srecs[ 5150aadb33SDmitri Tikhonov ( 64 /* Efficient size for malo allocator */ 52c51ce338SDmitri Tikhonov - sizeof(TAILQ_ENTRY(stream_rec)) /* next_stream_rec_arr */ 5350aadb33SDmitri Tikhonov ) / sizeof(struct stream_rec) 5450aadb33SDmitri Tikhonov ]; 5550aadb33SDmitri Tikhonov}; 5650aadb33SDmitri Tikhonov 57c51ce338SDmitri TikhonovTAILQ_HEAD(stream_rec_arr_tailq, stream_rec_arr); 58c51ce338SDmitri Tikhonov 5950aadb33SDmitri Tikhonovtypedef struct lsquic_packet_out 6050aadb33SDmitri Tikhonov{ 6150aadb33SDmitri Tikhonov /* `po_next' is used for packets_out, unacked_packets and expired_packets 6250aadb33SDmitri Tikhonov * lists. 6350aadb33SDmitri Tikhonov */ 6450aadb33SDmitri Tikhonov TAILQ_ENTRY(lsquic_packet_out) 6550aadb33SDmitri Tikhonov po_next; 6650aadb33SDmitri Tikhonov lsquic_time_t po_sent; /* Time sent */ 6750aadb33SDmitri Tikhonov lsquic_packno_t po_packno; 6850aadb33SDmitri Tikhonov 69bfc7bfd8SDmitri Tikhonov enum packet_out_flags { 70bfc7bfd8SDmitri Tikhonov PO_HELLO = (1 << 1), /* Packet contains SHLO or CHLO data */ 71bfc7bfd8SDmitri Tikhonov PO_ENCRYPTED= (1 << 3), /* po_enc_data has encrypted data */ 72bfc7bfd8SDmitri Tikhonov PO_SREC_ARR = (1 << 4), 73bfc7bfd8SDmitri Tikhonov#define POBIT_SHIFT 5 74bfc7bfd8SDmitri Tikhonov PO_BITS_0 = (1 << 5), /* PO_BITS_0 and PO_BITS_1 encode the */ 75bfc7bfd8SDmitri Tikhonov PO_BITS_1 = (1 << 6), /* packet number length. See macros below. */ 76bfc7bfd8SDmitri Tikhonov PO_NONCE = (1 << 7), /* Use value in `po_nonce' to generate header */ 77bfc7bfd8SDmitri Tikhonov PO_VERSION = (1 << 8), /* Use value in `po_ver_tag' to generate header */ 78bfc7bfd8SDmitri Tikhonov PO_CONN_ID = (1 << 9), /* Include connection ID in public header */ 79bfc7bfd8SDmitri Tikhonov PO_REPACKNO = (1 <<10), /* Regenerate packet number */ 80bfc7bfd8SDmitri Tikhonov PO_NOENCRYPT= (1 <<11), /* Do not encrypt data in po_data */ 81bfc7bfd8SDmitri Tikhonov PO_VERNEG = (1 <<12), /* Version negotiation packet. */ 82bfc7bfd8SDmitri Tikhonov PO_STREAM_END 83bfc7bfd8SDmitri Tikhonov = (1 <<13), /* STREAM frame reaches the end of the packet: no 84bfc7bfd8SDmitri Tikhonov * further writes are allowed. 85bfc7bfd8SDmitri Tikhonov */ 86bfc7bfd8SDmitri Tikhonov PO_SCHED = (1 <<14), /* On scheduled queue */ 8716a9b66aSDmitri Tikhonov PO_SENT_SZ = (1 <<15), 889626cfc2SDmitri Tikhonov PO_LONGHEAD = (1 <<16), 899626cfc2SDmitri Tikhonov PO_GQUIC = (1 <<17), /* Used for logging */ 909626cfc2SDmitri Tikhonov#define POLEV_SHIFT 18 919626cfc2SDmitri Tikhonov PO_BITS_2 = (1 <<18), /* PO_BITS_2 and PO_BITS_3 encode the */ 929626cfc2SDmitri Tikhonov PO_BITS_3 = (1 <<19), /* crypto level. Used for logging. */ 931e75f938SDmitri Tikhonov#define POIPv6_SHIFT 20 941e75f938SDmitri Tikhonov PO_IPv6 = (1 <<20), /* Set if pmi_allocate was passed is_ipv6=1, 951e75f938SDmitri Tikhonov * otherwise unset. 961e75f938SDmitri Tikhonov */ 978252b0b9SDmitri Tikhonov PO_LIMITED = (1 <<21), /* Used to credit sc_next_limit if needed. */ 989626cfc2SDmitri Tikhonov } po_flags; 99bfc7bfd8SDmitri Tikhonov enum quic_ft_bit po_frame_types:16; /* Bitmask of QUIC_FRAME_* */ 100bfc7bfd8SDmitri Tikhonov unsigned short po_data_sz; /* Number of usable bytes in data */ 101bfc7bfd8SDmitri Tikhonov unsigned short po_enc_data_sz; /* Number of usable bytes in data */ 10216a9b66aSDmitri Tikhonov unsigned short po_sent_sz; /* If PO_SENT_SZ is set, real size of sent buffer. */ 103bfc7bfd8SDmitri Tikhonov unsigned short po_regen_sz; /* Number of bytes at the beginning 104bfc7bfd8SDmitri Tikhonov * of data containing bytes that are 105bfc7bfd8SDmitri Tikhonov * not to be retransmitted, e.g. ACK 106bfc7bfd8SDmitri Tikhonov * frames. 107bfc7bfd8SDmitri Tikhonov */ 108bfc7bfd8SDmitri Tikhonov unsigned short po_n_alloc; /* Total number of bytes allocated in po_data */ 1099626cfc2SDmitri Tikhonov enum header_type po_header_type:8; 110bfc7bfd8SDmitri Tikhonov unsigned char *po_data; 11116a9b66aSDmitri Tikhonov lsquic_packno_t po_ack2ed; /* If packet has ACK frame, value of 11216a9b66aSDmitri Tikhonov * largest acked in it. 11316a9b66aSDmitri Tikhonov */ 114bfc7bfd8SDmitri Tikhonov 11550aadb33SDmitri Tikhonov /* A lot of packets contain data belonging to only one stream. Thus, 116c51ce338SDmitri Tikhonov * `one' is used first. If this is not enough, any number of 11750aadb33SDmitri Tikhonov * stream_rec_arr structures can be allocated to handle more stream 11850aadb33SDmitri Tikhonov * records. 11950aadb33SDmitri Tikhonov */ 120c51ce338SDmitri Tikhonov union { 121c51ce338SDmitri Tikhonov struct stream_rec one; 122c51ce338SDmitri Tikhonov struct stream_rec_arr_tailq arr; 123c51ce338SDmitri Tikhonov } po_srecs; 12450aadb33SDmitri Tikhonov 12550aadb33SDmitri Tikhonov /* If PO_ENCRYPTED is set, this points to the buffer that holds encrypted 12650aadb33SDmitri Tikhonov * data. 12750aadb33SDmitri Tikhonov */ 12850aadb33SDmitri Tikhonov unsigned char *po_enc_data; 12950aadb33SDmitri Tikhonov 13050aadb33SDmitri Tikhonov lsquic_ver_tag_t po_ver_tag; /* Set if PO_VERSION is set */ 13150aadb33SDmitri Tikhonov unsigned char *po_nonce; /* Use to generate header if PO_NONCE is set */ 13250aadb33SDmitri Tikhonov} lsquic_packet_out_t; 13350aadb33SDmitri Tikhonov 13450aadb33SDmitri Tikhonov/* The size of lsquic_packet_out_t could be further reduced: 13550aadb33SDmitri Tikhonov * 13650aadb33SDmitri Tikhonov * po_ver_tag could be encoded as a few bits representing enum lsquic_version 13750aadb33SDmitri Tikhonov * in po_flags. The cost is a bit of complexity. This will save us four bytes. 13850aadb33SDmitri Tikhonov */ 13950aadb33SDmitri Tikhonov 14050aadb33SDmitri Tikhonov#define lsquic_packet_out_avail(p) ((unsigned short) \ 14150aadb33SDmitri Tikhonov ((p)->po_n_alloc - (p)->po_data_sz)) 14250aadb33SDmitri Tikhonov 14350aadb33SDmitri Tikhonov#define lsquic_packet_out_packno_bits(p) (((p)->po_flags >> POBIT_SHIFT) & 0x3) 14450aadb33SDmitri Tikhonov 145bfc7bfd8SDmitri Tikhonov#define lsquic_packet_out_set_packno_bits(p, b) do { \ 146bfc7bfd8SDmitri Tikhonov (p)->po_flags &= ~(0x3 << POBIT_SHIFT); \ 147bfc7bfd8SDmitri Tikhonov (p)->po_flags |= ((b) & 0x3) << POBIT_SHIFT; \ 148bfc7bfd8SDmitri Tikhonov} while (0) 149bfc7bfd8SDmitri Tikhonov 1501e75f938SDmitri Tikhonov#define lsquic_packet_out_ipv6(p) ((int)(((p)->po_flags >> POIPv6_SHIFT) & 1)) 1511e75f938SDmitri Tikhonov 1521e75f938SDmitri Tikhonov#define lsquic_packet_out_set_ipv6(p, b) do { \ 1531e75f938SDmitri Tikhonov (p)->po_flags &= ~(1 << POIPv6_SHIFT); \ 1541e75f938SDmitri Tikhonov (p)->po_flags |= ((b) & 1) << POIPv6_SHIFT; \ 1551e75f938SDmitri Tikhonov} while (0) 1561e75f938SDmitri Tikhonov 1579626cfc2SDmitri Tikhonov#define lsquic_po_header_length(lconn, po_flags) ( \ 1589626cfc2SDmitri Tikhonov lconn->cn_pf->pf_packout_header_size(lconn, po_flags)) 15950aadb33SDmitri Tikhonov 1609626cfc2SDmitri Tikhonov#define lsquic_packet_out_total_sz(lconn, p) (\ 1619626cfc2SDmitri Tikhonov lconn->cn_pf->pf_packout_size(lconn, p)) 162bfc7bfd8SDmitri Tikhonov 16316a9b66aSDmitri Tikhonov#if __GNUC__ 16416a9b66aSDmitri Tikhonov#if LSQUIC_EXTRA_CHECKS 1659626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) ( \ 16616a9b66aSDmitri Tikhonov __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 1679626cfc2SDmitri Tikhonov (assert((p)->po_sent_sz == lsquic_packet_out_total_sz(lconn, p)), \ 1689626cfc2SDmitri Tikhonov (p)->po_sent_sz) : lsquic_packet_out_total_sz(lconn, p)) 16916a9b66aSDmitri Tikhonov# else 1709626cfc2SDmitri Tikhonov#define lsquic_packet_out_sent_sz(lconn, p) ( \ 17116a9b66aSDmitri Tikhonov __builtin_expect(((p)->po_flags & PO_SENT_SZ), 1) ? \ 1729626cfc2SDmitri Tikhonov (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 17316a9b66aSDmitri Tikhonov#endif 17416a9b66aSDmitri Tikhonov#else 1759626cfc2SDmitri Tikhonov# define lsquic_packet_out_sent_sz(lconn, p) ( \ 17616a9b66aSDmitri Tikhonov (p)->po_flags & PO_SENT_SZ ? \ 1779626cfc2SDmitri Tikhonov (p)->po_sent_sz : lsquic_packet_out_total_sz(lconn, p)) 17816a9b66aSDmitri Tikhonov#endif 17916a9b66aSDmitri Tikhonov 18050aadb33SDmitri Tikhonov#define lsquic_packet_out_verneg(p) \ 18150aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == (PO_NOENCRYPT|PO_VERNEG)) 18250aadb33SDmitri Tikhonov 18350aadb33SDmitri Tikhonov#define lsquic_packet_out_pubres(p) \ 18450aadb33SDmitri Tikhonov (((p)->po_flags & (PO_NOENCRYPT|PO_VERNEG)) == PO_NOENCRYPT ) 18550aadb33SDmitri Tikhonov 1869626cfc2SDmitri Tikhonov#define lsquic_packet_out_set_enc_level(p, level) do { \ 1879626cfc2SDmitri Tikhonov (p)->po_flags &= ~(3 << POLEV_SHIFT); \ 1889626cfc2SDmitri Tikhonov (p)->po_flags |= level << POLEV_SHIFT; \ 1899626cfc2SDmitri Tikhonov} while (0) 1909626cfc2SDmitri Tikhonov 1919626cfc2SDmitri Tikhonov#define lsquic_packet_out_enc_level(p) (((p)->po_flags >> POLEV_SHIFT) & 3) 1929626cfc2SDmitri Tikhonov 19350aadb33SDmitri Tikhonovstruct packet_out_srec_iter { 19450aadb33SDmitri Tikhonov lsquic_packet_out_t *packet_out; 19550aadb33SDmitri Tikhonov struct stream_rec_arr *cur_srec_arr; 19650aadb33SDmitri Tikhonov unsigned srec_idx; 197c51ce338SDmitri Tikhonov int impl_idx; 19850aadb33SDmitri Tikhonov}; 19950aadb33SDmitri Tikhonov 20050aadb33SDmitri Tikhonovstruct stream_rec * 20150aadb33SDmitri Tikhonovposi_first (struct packet_out_srec_iter *posi, lsquic_packet_out_t *); 20250aadb33SDmitri Tikhonov 20350aadb33SDmitri Tikhonovstruct stream_rec * 20450aadb33SDmitri Tikhonovposi_next (struct packet_out_srec_iter *posi); 20550aadb33SDmitri Tikhonov 20650aadb33SDmitri Tikhonovlsquic_packet_out_t * 20750aadb33SDmitri Tikhonovlsquic_packet_out_new (struct lsquic_mm *, struct malo *, int use_cid, 2089626cfc2SDmitri Tikhonov const struct lsquic_conn *, enum lsquic_packno_bits, 20950aadb33SDmitri Tikhonov const lsquic_ver_tag_t *, const unsigned char *nonce); 21050aadb33SDmitri Tikhonov 21150aadb33SDmitri Tikhonovvoid 21250aadb33SDmitri Tikhonovlsquic_packet_out_destroy (lsquic_packet_out_t *, 2131e75f938SDmitri Tikhonov struct lsquic_engine_public *, void *peer_ctx); 21450aadb33SDmitri Tikhonov 21550aadb33SDmitri Tikhonovint 21650aadb33SDmitri Tikhonovlsquic_packet_out_add_stream (lsquic_packet_out_t *packet_out, 21750aadb33SDmitri Tikhonov struct lsquic_mm *mm, 21850aadb33SDmitri Tikhonov struct lsquic_stream *new_stream, 21950aadb33SDmitri Tikhonov enum QUIC_FRAME_TYPE, 220c51ce338SDmitri Tikhonov unsigned short off, unsigned short len); 22150aadb33SDmitri Tikhonov 222bfc7bfd8SDmitri Tikhonovunsigned 223c51ce338SDmitri Tikhonovlsquic_packet_out_elide_reset_stream_frames (lsquic_packet_out_t *, uint32_t); 224c51ce338SDmitri Tikhonov 225c51ce338SDmitri Tikhonovint 226c51ce338SDmitri Tikhonovlsquic_packet_out_split_in_two (struct lsquic_mm *, lsquic_packet_out_t *, 227c51ce338SDmitri Tikhonov lsquic_packet_out_t *, const struct parse_funcs *, unsigned excess_bytes); 22850aadb33SDmitri Tikhonov 22950aadb33SDmitri Tikhonovvoid 23050aadb33SDmitri Tikhonovlsquic_packet_out_chop_regen (lsquic_packet_out_t *); 23150aadb33SDmitri Tikhonov 232c51ce338SDmitri Tikhonovint 233c51ce338SDmitri Tikhonovlsquic_packet_out_has_frame (struct lsquic_packet_out *, 234c51ce338SDmitri Tikhonov const struct lsquic_stream *, enum QUIC_FRAME_TYPE); 235c51ce338SDmitri Tikhonov 236c51ce338SDmitri Tikhonovint 237c51ce338SDmitri Tikhonovlsquic_packet_out_has_hsk_frames (struct lsquic_packet_out *); 238c51ce338SDmitri Tikhonov 239c51ce338SDmitri Tikhonovvoid 240c51ce338SDmitri Tikhonovlsquic_packet_out_ack_streams (struct lsquic_packet_out *); 241c51ce338SDmitri Tikhonov 242c51ce338SDmitri Tikhonovvoid 243c51ce338SDmitri Tikhonovlsquic_packet_out_zero_pad (struct lsquic_packet_out *); 244c51ce338SDmitri Tikhonov 245c51ce338SDmitri Tikhonovsize_t 246c51ce338SDmitri Tikhonovlsquic_packet_out_mem_used (const struct lsquic_packet_out *); 247c51ce338SDmitri Tikhonov 248c51ce338SDmitri Tikhonovint 249c51ce338SDmitri Tikhonovlsquic_packet_out_turn_on_fin (struct lsquic_packet_out *, 250c51ce338SDmitri Tikhonov const struct parse_funcs *, const struct lsquic_stream *); 251c51ce338SDmitri Tikhonov 25250aadb33SDmitri Tikhonov#endif 253