FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
quic.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __included_quic_h__
17 #define __included_quic_h__
18 
20 
21 #include <vppinfra/lock.h>
23 #include <vppinfra/bihash_16_8.h>
24 
25 #include <quicly.h>
26 
27 #include <vnet/crypto/crypto.h>
28 #include <vppinfra/lock.h>
29 
30 /* QUIC log levels
31  * 1 - errors
32  * 2 - connection/stream events
33  * 3 - packet events
34  * 4 - timer events
35  **/
36 
37 #define QUIC_DEBUG 0
38 #define QUIC_TSTAMP_RESOLUTION 0.001 /* QUIC tick resolution (1ms) */
39 #define QUIC_TIMER_HANDLE_INVALID ((u32) ~0)
40 #define QUIC_SESSION_INVALID ((u32) ~0 - 1)
41 #define QUIC_MAX_PACKET_SIZE 1280
42 
43 #define QUIC_INT_MAX 0x3FFFFFFFFFFFFFFF
44 #define QUIC_DEFAULT_FIFO_SIZE (64 << 10)
45 #define QUIC_SEND_PACKET_VEC_SIZE 16
46 #define QUIC_IV_LEN 17
47 
48 #define QUIC_MAX_COALESCED_PACKET 4
49 
50 #define QUIC_RCV_MAX_PACKETS 16
51 
52 #define QUIC_DEFAULT_CONN_TIMEOUT (30 * 1000) /* 30 seconds */
53 
54 /* Taken from quicly.c */
55 #define QUICLY_QUIC_BIT 0x40
56 
57 #define QUICLY_PACKET_TYPE_INITIAL (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0)
58 #define QUICLY_PACKET_TYPE_0RTT (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x10)
59 #define QUICLY_PACKET_TYPE_HANDSHAKE (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x20)
60 #define QUICLY_PACKET_TYPE_RETRY (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x30)
61 #define QUICLY_PACKET_TYPE_BITMASK 0xf0
62 
63 /* error codes */
64 #define QUIC_ERROR_FULL_FIFO 0xff10
65 #define QUIC_APP_ERROR_CLOSE_NOTIFY QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0)
66 #define QUIC_APP_ALLOCATION_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x1)
67 #define QUIC_APP_ACCEPT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x2)
68 #define QUIC_APP_CONNECT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x3)
69 
70 #define QUIC_DECRYPT_PACKET_OK 0
71 #define QUIC_DECRYPT_PACKET_NOTOFFLOADED 1
72 #define QUIC_DECRYPT_PACKET_ERROR 2
73 
74 #if QUIC_DEBUG
75 #define QUIC_DBG(_lvl, _fmt, _args...) \
76  if (_lvl <= QUIC_DEBUG) \
77  clib_warning (_fmt, ##_args)
78 #else
79 #define QUIC_DBG(_lvl, _fmt, _args...)
80 #endif
81 
82 #if CLIB_ASSERT_ENABLE
83 #define QUIC_ASSERT(truth) ASSERT (truth)
84 #else
85 #define QUIC_ASSERT(truth) \
86  do { \
87  if (PREDICT_FALSE (! (truth))) \
88  QUIC_ERR ("ASSERT(%s) failed", # truth); \
89  } while (0)
90 #endif
91 
92 #define QUIC_ERR(_fmt, _args...) \
93  do { \
94  clib_warning ("QUIC-ERR: " _fmt, ##_args); \
95  } while (0)
96 
97 
98 
100 
101 typedef enum
102 {
103 #define quic_error(n,s) QUIC_ERROR_##n,
105 #undef quic_error
107 } quic_error_t;
108 
110 {
119 
120 typedef enum quic_packet_type_
121 {
129 
130 typedef enum quic_ctx_flags_
131 {
132  QUIC_F_IS_STREAM = (1 << 0),
133  QUIC_F_IS_LISTENER = (1 << 1),
135 
136 typedef enum quic_cc_type
137 {
141 
142 /* This structure is used to implement the concept of VPP connection for QUIC.
143  * We create one per connection and one per stream. */
144 typedef struct quic_ctx_
145 {
146  union
147  {
149  struct
150  { /** QUIC ctx case */
151  quicly_conn_t *conn;
157  u8 _qctx_end_marker; /* Leave this at the end */
158  };
159  struct
160  { /** STREAM ctx case */
161  quicly_stream_t *stream;
164  u8 _sctx_end_marker; /* Leave this at the end */
165  };
166  };
175 
176  struct
177  {
178  ptls_cipher_context_t *hp_ctx;
179  ptls_aead_context_t *aead_ctx;
180  } ingress_keys;
182 
183 } quic_ctx_t;
184 
185 /* Make sure our custom fields don't overlap with the fields we use in
186  .connection
187 */
188 STATIC_ASSERT (offsetof (quic_ctx_t, _qctx_end_marker) <=
190  "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
191 STATIC_ASSERT (offsetof (quic_ctx_t, _sctx_end_marker) <=
193  "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
194 
195 /* single-entry session cache */
196 typedef struct quic_session_cache_
197 {
198  ptls_encrypt_ticket_t super;
199  uint8_t id[32];
200  ptls_iovec_t data;
202 
203 typedef struct quic_stream_data_
204 {
207  u32 app_rx_data_len; /**< bytes received, to be read by external app */
208  u32 app_tx_data_len; /**< bytes sent */
210 
212 {
213  quicly_context_t quicly_ctx;
215  ptls_context_t ptls_ctx;
217 
218 typedef struct quic_worker_ctx_
219 {
220  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
221  int64_t time_now; /**< worker time */
222  tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel; /**< worker timer wheel */
223  quicly_cid_plaintext_t next_cid;
224  crypto_context_t *crypto_ctx_pool; /**< per thread pool of crypto contexes */
225  clib_bihash_24_8_t crypto_context_hash; /**< per thread [params:crypto_ctx_index] hash */
227 
228 typedef struct quic_rx_packet_ctx_
229 {
230  quicly_decoded_packet_t packet;
234  union
235  {
236  struct sockaddr sa;
237  struct sockaddr_in6 sa6;
238  };
239  socklen_t salen;
243 
244 typedef struct quic_main_
245 {
249  clib_bihash_16_8_t connection_hash; /**< quic connection id -> conn handle */
251 
252  ptls_cipher_suite_t ***quic_ciphers; /**< available ciphers by crypto engine */
253  uword *available_crypto_engines; /**< Bitmap for registered engines */
254  u8 default_crypto_engine; /**< Used if you do connect with CRYPTO_ENGINE_NONE (0) */
255  u64 max_packets_per_key; /**< number of packets that can be sent without a key update */
257 
258  ptls_handshake_properties_t hs_properties;
260 
264 
266 
268 } quic_main_t;
269 
270 #endif /* __included_quic_h__ */
271 
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */
quic_rx_packet_ctx_::sa
struct sockaddr sa
Definition: quic.h:236
QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED
@ QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED
Definition: quic.h:115
quic_ctx_::udp_session_handle
session_handle_t udp_session_handle
Definition: quic.h:167
quic_worker_ctx_::timer_wheel
tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel
worker timer wheel
Definition: quic.h:222
quic_stream_data_::thread_index
u32 thread_index
Definition: quic.h:206
session_dgram_header_
Definition: session_types.h:437
quic_ctx_::connection
transport_connection_t connection
Definition: quic.h:148
crypto.h
quic_cc_type_t
enum quic_cc_type quic_cc_type_t
quic_worker_ctx_::crypto_ctx_pool
crypto_context_t * crypto_ctx_pool
per thread pool of crypto contexes
Definition: quic.h:224
quic_error_t
quic_error_t
Definition: quic.h:101
quic_crypto_context_data_::quicly_ctx
quicly_context_t quicly_ctx
Definition: quic.h:213
quic_crypto_context_data_
Definition: quic.h:211
quic_ctx_::bytes_written
u64 bytes_written
Definition: quic.h:162
quic_ctx_
Definition: quic.h:144
quic_worker_ctx_::time_now
int64_t time_now
worker time
Definition: quic.h:221
QUIC_MAX_PACKET_SIZE
#define QUIC_MAX_PACKET_SIZE
Definition: quic.h:41
quic_ctx_::ckpair_index
u32 ckpair_index
Definition: quic.h:171
QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED
@ QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED
Definition: quic.h:116
quic_rx_packet_ctx_::ptype
u8 ptype
Definition: quic.h:240
quic_rx_packet_ctx_::sa6
struct sockaddr_in6 sa6
Definition: quic.h:237
quic_main_::session_cache
quic_session_cache_t session_cache
Definition: quic.h:259
quic_stream_data_::app_rx_data_len
u32 app_rx_data_len
bytes received, to be read by external app
Definition: quic.h:207
quic_ctx_conn_state_t
enum quic_ctx_conn_state_ quic_ctx_conn_state_t
QUIC_CC_CUBIC
@ QUIC_CC_CUBIC
Definition: quic.h:139
quic_ctx_::listener_ctx_id
u32 listener_ctx_id
Definition: quic.h:152
session_handle_t
u64 session_handle_t
Definition: session_types.h:111
quic_crypto_context_data_::ptls_ctx
ptls_context_t ptls_ctx
Definition: quic.h:215
quic_rx_packet_ctx_::thread_index
u32 thread_index
Definition: quic.h:233
quic_ctx_::ingress_keys
struct quic_ctx_::@771 ingress_keys
quic_ctx_::parent_app_id
u32 parent_app_id
Definition: quic.h:170
quic_main_::crypto_keys_quic_rw_lock
clib_rwlock_t crypto_keys_quic_rw_lock
Definition: quic.h:267
transport_connection_t
struct _transport_connection transport_connection_t
quic_ctx_::parent_app_wrk_id
u32 parent_app_wrk_id
Definition: quic.h:169
quic_ctx_::timer_handle
u32 timer_handle
Definition: quic.h:168
quic_ctx_::flags
u8 flags
Definition: quic.h:174
quic_main_::connection_timeout
u32 connection_timeout
Definition: quic.h:263
STATIC_ASSERT
STATIC_ASSERT(offsetof(quic_ctx_t, _qctx_end_marker)<=TRANSPORT_CONN_ID_LEN, "connection data must be less than TRANSPORT_CONN_ID_LEN bytes")
quic_main_
Definition: quic.h:244
QUIC_PACKET_TYPE_ACCEPT
@ QUIC_PACKET_TYPE_ACCEPT
Definition: quic.h:125
quic_ctx_::crypto_context_index
u32 crypto_context_index
Definition: quic.h:173
quic_stream_data_
Definition: quic.h:203
quic_ctx_::srv_hostname
u8 * srv_hostname
Definition: quic.h:154
QUIC_PACKET_TYPE_RECEIVE
@ QUIC_PACKET_TYPE_RECEIVE
Definition: quic.h:123
quic_worker_ctx_t
struct quic_worker_ctx_ quic_worker_ctx_t
quic_main_::wrk_ctx
quic_worker_ctx_t * wrk_ctx
Definition: quic.h:248
quic_ctx_flags_t
enum quic_ctx_flags_ quic_ctx_flags_t
quic_main_::quic_ciphers
ptls_cipher_suite_t *** quic_ciphers
available ciphers by crypto engine
Definition: quic.h:252
QUIC_CC_RENO
@ QUIC_CC_RENO
Definition: quic.h:138
QUIC_PACKET_TYPE_RESET
@ QUIC_PACKET_TYPE_RESET
Definition: quic.h:126
lock.h
QUIC_F_IS_STREAM
@ QUIC_F_IS_STREAM
Definition: quic.h:132
quic_session_cache_
Definition: quic.h:196
quic_main_::hs_properties
ptls_handshake_properties_t hs_properties
Definition: quic.h:258
quic_stream_data_t
struct quic_stream_data_ quic_stream_data_t
QUIC_F_IS_LISTENER
@ QUIC_F_IS_LISTENER
Definition: quic.h:133
quic_main_::vnet_crypto_enabled
u8 vnet_crypto_enabled
Definition: quic.h:265
clib_rw_lock_
Definition: lock.h:139
quic_session_cache_::data
ptls_iovec_t data
Definition: quic.h:200
uword
u64 uword
Definition: types.h:112
quic_packet_type_
quic_packet_type_
Definition: quic.h:120
QUIC_N_ERROR
@ QUIC_N_ERROR
Definition: quic.h:106
quic_main_::ctx_pool
quic_ctx_t ** ctx_pool
Definition: quic.h:247
quic_crypto_context_data_t
struct quic_crypto_context_data_ quic_crypto_context_data_t
quic_packet_type_t
enum quic_packet_type_ quic_packet_type_t
QUIC_PACKET_TYPE_NONE
@ QUIC_PACKET_TYPE_NONE
Definition: quic.h:122
f64
double f64
Definition: types.h:142
quic_main_::udp_fifo_prealloc
u32 udp_fifo_prealloc
Definition: quic.h:262
quic_stream_data_::app_tx_data_len
u32 app_tx_data_len
bytes sent
Definition: quic.h:208
quic_worker_ctx_::next_cid
quicly_cid_plaintext_t next_cid
Definition: quic.h:223
quic_worker_ctx_::crypto_context_hash
clib_bihash_24_8_t crypto_context_hash
per thread [params:crypto_ctx_index] hash
Definition: quic.h:225
quic_main_t
struct quic_main_ quic_main_t
quic_main_::max_packets_per_key
u64 max_packets_per_key
number of packets that can be sent without a key update
Definition: quic.h:255
quic_rx_packet_ctx_::data
u8 data[QUIC_MAX_PACKET_SIZE]
Definition: quic.h:231
quic_ctx_::conn
quicly_conn_t * conn
QUIC ctx case.
Definition: quic.h:151
quic_rx_packet_ctx_::packet
quicly_decoded_packet_t packet
Definition: quic.h:230
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
QUIC_PACKET_TYPE_MIGRATE
@ QUIC_PACKET_TYPE_MIGRATE
Definition: quic.h:124
quic_error.def
quic_main_::default_quic_cc
u8 default_quic_cc
Definition: quic.h:256
QUIC_CONN_STATE_OPENED
@ QUIC_CONN_STATE_OPENED
Definition: quic.h:111
quic_stream_data_::ctx_id
u32 ctx_id
Definition: quic.h:205
quic_ctx_::stream
quicly_stream_t * stream
STREAM ctx case.
Definition: quic.h:161
quic_cc_type
quic_cc_type
Definition: quic.h:136
quic_input_node
vlib_node_registration_t quic_input_node
(constructor) VLIB_REGISTER_NODE (quic_input_node)
Definition: quic.c:2996
u64
unsigned long u64
Definition: types.h:89
quic_session_cache_::super
ptls_encrypt_ticket_t super
Definition: quic.h:198
quic_rx_packet_ctx_::salen
socklen_t salen
Definition: quic.h:239
quic_main_::app_index
u32 app_index
Definition: quic.h:246
crypto_ctx_
Definition: application_interface.h:193
quic_main_::default_crypto_engine
u8 default_crypto_engine
Used if you do connect with CRYPTO_ENGINE_NONE (0)
Definition: quic.h:254
QUIC_IV_LEN
#define QUIC_IV_LEN
Definition: quic.h:46
u32
unsigned int u32
Definition: types.h:88
quic_crypto_context_data_::cid_key
char cid_key[QUIC_IV_LEN]
Definition: quic.h:214
quic_ctx_::udp_is_ip4
u8 udp_is_ip4
Definition: quic.h:156
QUIC_CONN_STATE_HANDSHAKE
@ QUIC_CONN_STATE_HANDSHAKE
Definition: quic.h:112
quic_ctx_::client_opaque
u32 client_opaque
Definition: quic.h:153
bihash_16_8.h
QUIC_CONN_STATE_PASSIVE_CLOSING
@ QUIC_CONN_STATE_PASSIVE_CLOSING
Definition: quic.h:114
quic_ctx_t
struct quic_ctx_ quic_ctx_t
tw_timer_1t_3w_1024sl_ov.h
quic_main_::available_crypto_engines
uword * available_crypto_engines
Bitmap for registered engines.
Definition: quic.h:253
application_interface.h
quic_main_::tstamp_ticks_per_clock
f64 tstamp_ticks_per_clock
Definition: quic.h:250
u8
unsigned char u8
Definition: types.h:56
quic_ctx_::crypto_engine
u32 crypto_engine
Definition: quic.h:172
QUIC_CONN_STATE_READY
@ QUIC_CONN_STATE_READY
Definition: quic.h:113
quic_rx_packet_ctx_::ph
session_dgram_hdr_t ph
Definition: quic.h:241
quic_rx_packet_ctx_::ctx_index
u32 ctx_index
Definition: quic.h:232
QUIC_CONN_STATE_ACTIVE_CLOSING
@ QUIC_CONN_STATE_ACTIVE_CLOSING
Definition: quic.h:117
quic_rx_packet_ctx_t
struct quic_rx_packet_ctx_ quic_rx_packet_ctx_t
quic_worker_ctx_::CLIB_CACHE_LINE_ALIGN_MARK
CLIB_CACHE_LINE_ALIGN_MARK(cacheline0)
QUIC_PACKET_TYPE_DROP
@ QUIC_PACKET_TYPE_DROP
Definition: quic.h:127
quic_ctx_::key_phase_ingress
int key_phase_ingress
Definition: quic.h:181
quic_ctx_::quic_connection_ctx_id
u32 quic_connection_ctx_id
Definition: quic.h:163
quic_ctx_::aead_ctx
ptls_aead_context_t * aead_ctx
Definition: quic.h:179
quic_ctx_flags_
quic_ctx_flags_
Definition: quic.h:130
quic_ctx_::conn_state
u8 conn_state
Definition: quic.h:155
quic_session_cache_t
struct quic_session_cache_ quic_session_cache_t
quic_ctx_::hp_ctx
ptls_cipher_context_t * hp_ctx
Definition: quic.h:178
quic_ctx_conn_state_
quic_ctx_conn_state_
Definition: quic.h:109
quic_main_::udp_fifo_size
u32 udp_fifo_size
Definition: quic.h:261
quic_rx_packet_ctx_
Definition: quic.h:228
quic_worker_ctx_
Definition: quic.h:218
TRANSPORT_CONN_ID_LEN
#define TRANSPORT_CONN_ID_LEN
Definition: transport_types.h:74
quic_main_::connection_hash
clib_bihash_16_8_t connection_hash
quic connection id -> conn handle
Definition: quic.h:249