lsquic_packet_common.h revision bfc7bfd8
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
53extern const size_t lsquic_frame_types_str_sz;
54
55const char *
56lsquic_frame_types_to_str (char *buf, size_t bufsz, enum quic_ft_bit);
57
58#define QFRAME_REGEN_MASK ((1 << QUIC_FRAME_ACK)                \
59                         | (1 << QUIC_FRAME_STOP_WAITING))
60
61#define QFRAME_REGENERATE(frame_type) ((1 << (frame_type)) & QFRAME_REGEN_MASK)
62
63#define QFRAME_ACKABLE_MASK (                               \
64    (1 << QUIC_FRAME_STREAM)                                \
65  | (1 << QUIC_FRAME_RST_STREAM)                            \
66  | (1 << QUIC_FRAME_GOAWAY)                                \
67  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
68  | (1 << QUIC_FRAME_PING)                                  \
69  | (1 << QUIC_FRAME_BLOCKED)                               \
70)
71
72#define QFRAME_ACKABLE(frame_type) ((1 << (frame_type)) & QFRAME_ACKABLE_MASK)
73
74#define QFRAME_RETRANSMITTABLE_MASK (                       \
75    (1 << QUIC_FRAME_STREAM)                                \
76  | (1 << QUIC_FRAME_RST_STREAM)                            \
77  | (1 << QUIC_FRAME_GOAWAY)                                \
78  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
79  | (1 << QUIC_FRAME_BLOCKED)                               \
80  | (1 << QUIC_FRAME_CONNECTION_CLOSE)                      \
81)
82
83#define QFRAME_RETRANSMITTABLE(frame_type) \
84                        ((1 << (frame_type)) & QFRAME_RETRANSMITTABLE_MASK)
85
86#define QUIC_MAX_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ + 4 /* Version */ \
87                            + 32 /* Nonce */ + 6 /* Packet Number */ )
88
89#define QUIC_MIN_PUBHDR_SZ (1 /* Type */ + 1 /* Packet number */)
90
91/* 12 bytes of FNV hash or encryption IV */
92#define QUIC_PACKET_HASH_SZ 12
93
94/* [draft-hamilton-quic-transport-protocol-01], Section 7 */
95#define QUIC_MAX_IPv4_PACKET_SZ 1370
96#define QUIC_MAX_IPv6_PACKET_SZ 1350
97
98#define QUIC_MAX_PACKET_SZ (QUIC_MAX_IPv4_PACKET_SZ > \
99    QUIC_MAX_IPv6_PACKET_SZ ? QUIC_MAX_IPv4_PACKET_SZ : QUIC_MAX_IPv6_PACKET_SZ)
100
101#define QUIC_MIN_PACKET_OVERHEAD (QUIC_PACKET_HASH_SZ + QUIC_MIN_PUBHDR_SZ)
102
103#define QUIC_MAX_PAYLOAD_SZ (QUIC_MAX_PACKET_SZ - QUIC_MIN_PACKET_OVERHEAD)
104
105#define QUIC_WUF_SZ 13  /* Type (1) + Stream ID (4) + Offset (8) */
106#define QUIC_BLOCKED_FRAME_SZ 5  /* Type (1) + Stream ID (4) */
107#define QUIC_RST_STREAM_SZ 17    /* Type (1) + Stream ID (4) + Offset (8) +
108                                                            Error code (4) */
109#define QUIC_GOAWAY_FRAME_SZ 11  /* Type (1) + Error code (4) + Stream ID (4) +
110                                                Reason phrase length (2) */
111
112/* Bitmask to be used as bits 4 and 5 (0x30) in common header's flag field: */
113enum lsquic_packno_bits
114{
115    PACKNO_LEN_1    = 0,
116    PACKNO_LEN_2    = 1,
117    PACKNO_LEN_4    = 2,
118    PACKNO_LEN_6    = 3,
119};
120
121enum lsquic_packno_bits
122calc_packno_bits (lsquic_packno_t packno, lsquic_packno_t least_unacked,
123                  uint64_t n_in_flight);
124
125#define packno_bits2len(b) (((b) << 1) + !(b))
126
127lsquic_packno_t
128restore_packno (lsquic_packno_t cur_packno,
129                enum lsquic_packno_bits cur_packno_bits,
130                lsquic_packno_t max_packno);
131
132#endif
133