lsquic_packet_common.h revision 461e84d8
1/* Copyright (c) 2017 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 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    SLEN(QUIC_FRAME_STREAM)           + 1 + \
76    SLEN(QUIC_FRAME_ACK)              + 1 + \
77    SLEN(QUIC_FRAME_PADDING)          + 1 + \
78    SLEN(QUIC_FRAME_RST_STREAM)       + 1 + \
79    SLEN(QUIC_FRAME_CONNECTION_CLOSE) + 1 + \
80    SLEN(QUIC_FRAME_GOAWAY)           + 1 + \
81    SLEN(QUIC_FRAME_WINDOW_UPDATE)    + 1 + \
82    SLEN(QUIC_FRAME_BLOCKED)          + 1 + \
83    SLEN(QUIC_FRAME_STOP_WAITING)     + 1 + \
84    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 QUIC_MAX_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ + 4 /* Version */ \
120                            + 32 /* Nonce */ + 6 /* Packet Number */ )
121
122#define QUIC_MIN_PUBHDR_SZ (1 /* Type */ + 1 /* Packet number */)
123
124/* 12 bytes of FNV hash or encryption IV */
125#define QUIC_PACKET_HASH_SZ 12
126
127/* [draft-hamilton-quic-transport-protocol-01], Section 7 */
128#define QUIC_MAX_IPv4_PACKET_SZ 1370
129#define QUIC_MAX_IPv6_PACKET_SZ 1350
130
131#define QUIC_MAX_PACKET_SZ (QUIC_MAX_IPv4_PACKET_SZ > \
132    QUIC_MAX_IPv6_PACKET_SZ ? QUIC_MAX_IPv4_PACKET_SZ : QUIC_MAX_IPv6_PACKET_SZ)
133
134#define QUIC_MIN_PACKET_OVERHEAD (QUIC_PACKET_HASH_SZ + QUIC_MIN_PUBHDR_SZ)
135
136#define QUIC_MAX_PAYLOAD_SZ (QUIC_MAX_PACKET_SZ - QUIC_MIN_PACKET_OVERHEAD)
137
138#define QUIC_WUF_SZ 13  /* Type (1) + Stream ID (4) + Offset (8) */
139#define QUIC_BLOCKED_FRAME_SZ 5  /* Type (1) + Stream ID (4) */
140#define QUIC_RST_STREAM_SZ 17    /* Type (1) + Stream ID (4) + Offset (8) +
141                                                            Error code (4) */
142#define QUIC_GOAWAY_FRAME_SZ 11  /* Type (1) + Error code (4) + Stream ID (4) +
143                                                Reason phrase length (2) */
144
145/* Bitmask to be used as bits 4 and 5 (0x30) in common header's flag field: */
146enum lsquic_packno_bits
147{
148    PACKNO_LEN_1    = 0,
149    PACKNO_LEN_2    = 1,
150    PACKNO_LEN_4    = 2,
151    PACKNO_LEN_6    = 3,
152};
153
154enum lsquic_packno_bits
155calc_packno_bits (lsquic_packno_t packno, lsquic_packno_t least_unacked,
156                  uint64_t n_in_flight);
157
158#define packno_bits2len(b) (((b) << 1) + !(b))
159
160lsquic_packno_t
161restore_packno (lsquic_packno_t cur_packno,
162                enum lsquic_packno_bits cur_packno_bits,
163                lsquic_packno_t max_packno);
164
165#endif
166