lsquic_packet_common.h revision c7d81ce1
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_PACKET_COMMON_H
3#define LSQUIC_PACKET_COMMON_H 1
4
5#include <stdint.h>
6
7#include "lsquic_int_types.h"
8
9enum PACKET_PUBLIC_FLAGS
10{
11  PACKET_PUBLIC_FLAGS_VERSION = 1,
12  PACKET_PUBLIC_FLAGS_RST = 2,
13  PACKET_PUBLIC_FLAGS_NONCE = 4,
14  PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 8,
15  PACKET_PUBLIC_FLAGS_MULTIPATH = 1 << 6,
16  PACKET_PUBLIC_FLAGS_TWO_OR_MORE_BYTES = 1 << 7,
17};
18
19enum quic_frame_type
20{
21    QUIC_FRAME_INVALID,
22
23    /*Special*/
24    QUIC_FRAME_STREAM,
25    QUIC_FRAME_ACK,
26
27    /*Regular*/
28    QUIC_FRAME_PADDING,
29    QUIC_FRAME_RST_STREAM,
30    QUIC_FRAME_CONNECTION_CLOSE,
31    QUIC_FRAME_GOAWAY,
32    QUIC_FRAME_WINDOW_UPDATE,
33    QUIC_FRAME_BLOCKED,
34    QUIC_FRAME_STOP_WAITING,
35    QUIC_FRAME_PING,
36    N_QUIC_FRAMES
37};
38
39enum quic_ft_bit {
40    QUIC_FTBIT_INVALID          = 1 << QUIC_FRAME_INVALID,
41    QUIC_FTBIT_STREAM           = 1 << QUIC_FRAME_STREAM,
42    QUIC_FTBIT_ACK              = 1 << QUIC_FRAME_ACK,
43    QUIC_FTBIT_PADDING          = 1 << QUIC_FRAME_PADDING,
44    QUIC_FTBIT_RST_STREAM       = 1 << QUIC_FRAME_RST_STREAM,
45    QUIC_FTBIT_CONNECTION_CLOSE = 1 << QUIC_FRAME_CONNECTION_CLOSE,
46    QUIC_FTBIT_GOAWAY           = 1 << QUIC_FRAME_GOAWAY,
47    QUIC_FTBIT_WINDOW_UPDATE    = 1 << QUIC_FRAME_WINDOW_UPDATE,
48    QUIC_FTBIT_BLOCKED          = 1 << QUIC_FRAME_BLOCKED,
49    QUIC_FTBIT_STOP_WAITING     = 1 << QUIC_FRAME_STOP_WAITING,
50    QUIC_FTBIT_PING             = 1 << QUIC_FRAME_PING,
51};
52
53static const char * const frame_type_2_str[N_QUIC_FRAMES] = {
54    [QUIC_FRAME_INVALID]           =  "QUIC_FRAME_INVALID",
55    [QUIC_FRAME_STREAM]            =  "QUIC_FRAME_STREAM",
56    [QUIC_FRAME_ACK]               =  "QUIC_FRAME_ACK",
57    [QUIC_FRAME_PADDING]           =  "QUIC_FRAME_PADDING",
58    [QUIC_FRAME_RST_STREAM]        =  "QUIC_FRAME_RST_STREAM",
59    [QUIC_FRAME_CONNECTION_CLOSE]  =  "QUIC_FRAME_CONNECTION_CLOSE",
60    [QUIC_FRAME_GOAWAY]            =  "QUIC_FRAME_GOAWAY",
61    [QUIC_FRAME_WINDOW_UPDATE]     =  "QUIC_FRAME_WINDOW_UPDATE",
62    [QUIC_FRAME_BLOCKED]           =  "QUIC_FRAME_BLOCKED",
63    [QUIC_FRAME_STOP_WAITING]      =  "QUIC_FRAME_STOP_WAITING",
64    [QUIC_FRAME_PING]              =  "QUIC_FRAME_PING",
65};
66
67
68#define QUIC_FRAME_SLEN(x) (sizeof(#x) - sizeof("QUIC_FRAME_"))
69
70
71    /* We don't need to include INVALID frame in this list because it is
72     * never a part of any frame list bitmask (e.g. po_frame_types).
73     */
74#define lsquic_frame_types_str_sz  \
75    QUIC_FRAME_SLEN(QUIC_FRAME_STREAM)           + 1 + \
76    QUIC_FRAME_SLEN(QUIC_FRAME_ACK)              + 1 + \
77    QUIC_FRAME_SLEN(QUIC_FRAME_PADDING)          + 1 + \
78    QUIC_FRAME_SLEN(QUIC_FRAME_RST_STREAM)       + 1 + \
79    QUIC_FRAME_SLEN(QUIC_FRAME_CONNECTION_CLOSE) + 1 + \
80    QUIC_FRAME_SLEN(QUIC_FRAME_GOAWAY)           + 1 + \
81    QUIC_FRAME_SLEN(QUIC_FRAME_WINDOW_UPDATE)    + 1 + \
82    QUIC_FRAME_SLEN(QUIC_FRAME_BLOCKED)          + 1 + \
83    QUIC_FRAME_SLEN(QUIC_FRAME_STOP_WAITING)     + 1 + \
84    QUIC_FRAME_SLEN(QUIC_FRAME_PING)             + 1
85
86
87
88const char *
89lsquic_frame_types_to_str (char *buf, size_t bufsz, enum quic_ft_bit);
90
91#define QFRAME_REGEN_MASK ((1 << QUIC_FRAME_ACK)                \
92                         | (1 << QUIC_FRAME_STOP_WAITING))
93
94#define QFRAME_REGENERATE(frame_type) ((1 << (frame_type)) & QFRAME_REGEN_MASK)
95
96#define QFRAME_ACKABLE_MASK (                               \
97    (1 << QUIC_FRAME_STREAM)                                \
98  | (1 << QUIC_FRAME_RST_STREAM)                            \
99  | (1 << QUIC_FRAME_GOAWAY)                                \
100  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
101  | (1 << QUIC_FRAME_PING)                                  \
102  | (1 << QUIC_FRAME_BLOCKED)                               \
103)
104
105#define QFRAME_ACKABLE(frame_type) ((1 << (frame_type)) & QFRAME_ACKABLE_MASK)
106
107#define QFRAME_RETRANSMITTABLE_MASK (                       \
108    (1 << QUIC_FRAME_STREAM)                                \
109  | (1 << QUIC_FRAME_RST_STREAM)                            \
110  | (1 << QUIC_FRAME_GOAWAY)                                \
111  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
112  | (1 << QUIC_FRAME_BLOCKED)                               \
113  | (1 << QUIC_FRAME_CONNECTION_CLOSE)                      \
114)
115
116#define QFRAME_RETRANSMITTABLE(frame_type) \
117                        ((1 << (frame_type)) & QFRAME_RETRANSMITTABLE_MASK)
118
119#define GQUIC_MAX_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ + 4 /* Version */ \
120                            + 32 /* Nonce */ + 6 /* Packet Number */ )
121
122#define GQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 1 /* Packet number */)
123
124#define GQUIC_IETF_LONG_HEADER_SIZE (1 /* Type */ + 4 /* Version */ \
125                + 1 /* DCIL/SCIL */ + 8 /* CID */ + 4 /* Packet number */)
126
127/* XXX Nonce? */
128#define IQUIC_MAX_PUBHDR_SZ GQUIC_IETF_LONG_HEADER_SIZE
129
130#define IQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ \
131                                                + 1 /* Packet number */)
132
133#define QUIC_MAX_PUBHDR_SZ (GQUIC_MAX_PUBHDR_SZ > IQUIC_MAX_PUBHDR_SZ \
134                                ? GQUIC_MAX_PUBHDR_SZ : IQUIC_MAX_PUBHDR_SZ)
135
136#define QUIC_MIN_PUBHDR_SZ (GQUIC_MIN_PUBHDR_SZ < IQUIC_MIN_PUBHDR_SZ \
137                                ? GQUIC_MIN_PUBHDR_SZ : IQUIC_MIN_PUBHDR_SZ)
138
139/* 12 bytes of FNV hash or encryption IV */
140#define QUIC_PACKET_HASH_SZ 12
141
142/* [draft-hamilton-quic-transport-protocol-01], Section 7 */
143#define QUIC_MAX_IPv4_PACKET_SZ 1370
144#define QUIC_MAX_IPv6_PACKET_SZ 1350
145
146#define QUIC_MAX_PACKET_SZ (QUIC_MAX_IPv4_PACKET_SZ > \
147    QUIC_MAX_IPv6_PACKET_SZ ? QUIC_MAX_IPv4_PACKET_SZ : QUIC_MAX_IPv6_PACKET_SZ)
148
149#define QUIC_MIN_PACKET_OVERHEAD (QUIC_PACKET_HASH_SZ + QUIC_MIN_PUBHDR_SZ)
150
151#define QUIC_MAX_PAYLOAD_SZ (QUIC_MAX_PACKET_SZ - QUIC_MIN_PACKET_OVERHEAD)
152
153#define QUIC_WUF_SZ 13  /* Type (1) + Stream ID (4) + Offset (8) */
154#define QUIC_BLOCKED_FRAME_SZ 5  /* Type (1) + Stream ID (4) */
155#define QUIC_RST_STREAM_SZ 17    /* Type (1) + Stream ID (4) + Offset (8) +
156                                                            Error code (4) */
157#define QUIC_GOAWAY_FRAME_SZ 11  /* Type (1) + Error code (4) + Stream ID (4) +
158                                                Reason phrase length (2) */
159
160
161/* This value represents a different number of bytes used to encode the packet
162 * length based on whether GQUIC or IQUIC is used.
163 */
164enum packno_bits
165{
166    PACKNO_BITS_0 = 0,
167    PACKNO_BITS_1 = 1,
168    PACKNO_BITS_2 = 2,
169    PACKNO_BITS_3 = 3,
170};
171
172
173/* GQUIC maps 0, 1, 2, 3 -> 1, 2, 4, 6 */
174enum
175{
176    GQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
177    GQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
178    GQUIC_PACKNO_LEN_4 = PACKNO_BITS_2,
179    GQUIC_PACKNO_LEN_6 = PACKNO_BITS_3,
180};
181
182
183/* IQUIC maps 0, 1, 2, 3 -> 1, 2, 3, 4 (as of ID-17) */
184enum
185{
186    IQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
187    IQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
188    IQUIC_PACKNO_LEN_3 = PACKNO_BITS_2,
189    IQUIC_PACKNO_LEN_4 = PACKNO_BITS_3,
190};
191
192
193enum header_type
194{
195    HETY_NOT_SET,       /* This value must be zero */
196    HETY_VERNEG,
197    HETY_INITIAL,
198    HETY_RETRY,
199    HETY_HANDSHAKE,
200    HETY_0RTT,
201};
202
203extern const char *const lsquic_hety2str[];
204
205enum packno_bits
206calc_packno_bits (lsquic_packno_t packno, lsquic_packno_t least_unacked,
207                  uint64_t n_in_flight);
208
209#define gquic_packno_bits2len(b) (((b) << 1) + !(b))
210
211lsquic_packno_t
212restore_packno (lsquic_packno_t cur_packno,
213                unsigned packet_len,
214                lsquic_packno_t max_packno);
215
216#endif
217