quic: disable vnet_crypto and batching if no crypto engines are loaded
[vpp.git] / src / plugins / quic / quic.h
1 /*
2  * Copyright (c) 2019 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
19 #include <vnet/session/application_interface.h>
20
21 #include <vppinfra/lock.h>
22 #include <vppinfra/tw_timer_1t_3w_1024sl_ov.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_SEND_MAX_BATCH_PACKETS 16
51 #define QUIC_RCV_MAX_BATCH_PACKETS 16
52
53 #define QUIC_DEFAULT_CONN_TIMEOUT (30 * 1000)   /* 30 seconds */
54
55 /* Taken from quicly.c */
56 #define QUICLY_QUIC_BIT 0x40
57
58 #define QUICLY_PACKET_TYPE_INITIAL (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0)
59 #define QUICLY_PACKET_TYPE_0RTT (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x10)
60 #define QUICLY_PACKET_TYPE_HANDSHAKE (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x20)
61 #define QUICLY_PACKET_TYPE_RETRY (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x30)
62 #define QUICLY_PACKET_TYPE_BITMASK 0xf0
63
64 /* error codes */
65 #define QUIC_ERROR_FULL_FIFO 0xff10
66 #define QUIC_APP_ERROR_CLOSE_NOTIFY QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0)
67 #define QUIC_APP_ALLOCATION_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x1)
68 #define QUIC_APP_ACCEPT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x2)
69 #define QUIC_APP_CONNECT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x3)
70
71 #define QUIC_DECRYPT_PACKET_OK 0
72 #define QUIC_DECRYPT_PACKET_NOTOFFLOADED 1
73 #define QUIC_DECRYPT_PACKET_ERROR 2
74
75 #if QUIC_DEBUG
76 #define QUIC_DBG(_lvl, _fmt, _args...)   \
77   if (_lvl <= QUIC_DEBUG)                \
78     clib_warning (_fmt, ##_args)
79 #else
80 #define QUIC_DBG(_lvl, _fmt, _args...)
81 #endif
82
83 #if CLIB_ASSERT_ENABLE
84 #define QUIC_ASSERT(truth) ASSERT (truth)
85 #else
86 #define QUIC_ASSERT(truth)                        \
87   do {                                            \
88     if (PREDICT_FALSE (! (truth)))                \
89       QUIC_ERR ("ASSERT(%s) failed", # truth);    \
90   } while (0)
91 #endif
92
93 #define QUIC_ERR(_fmt, _args...)                \
94   do {                                          \
95     clib_warning ("QUIC-ERR: " _fmt, ##_args);  \
96   } while (0)
97
98
99
100 extern vlib_node_registration_t quic_input_node;
101
102 typedef enum
103 {
104 #define quic_error(n,s) QUIC_ERROR_##n,
105 #include <plugins/quic/quic_error.def>
106 #undef quic_error
107   QUIC_N_ERROR,
108 } quic_error_t;
109
110 typedef enum quic_ctx_conn_state_
111 {
112   QUIC_CONN_STATE_OPENED,
113   QUIC_CONN_STATE_HANDSHAKE,
114   QUIC_CONN_STATE_READY,
115   QUIC_CONN_STATE_PASSIVE_CLOSING,
116   QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED,
117   QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED,
118   QUIC_CONN_STATE_ACTIVE_CLOSING,
119 } quic_ctx_conn_state_t;
120
121 typedef enum quic_packet_type_
122 {
123   QUIC_PACKET_TYPE_NONE,
124   QUIC_PACKET_TYPE_RECEIVE,
125   QUIC_PACKET_TYPE_MIGRATE,
126   QUIC_PACKET_TYPE_ACCEPT,
127   QUIC_PACKET_TYPE_RESET,
128   QUIC_PACKET_TYPE_DROP,
129 } quic_packet_type_t;
130
131 typedef enum quic_ctx_flags_
132 {
133   QUIC_F_IS_STREAM = (1 << 0),
134   QUIC_F_IS_LISTENER = (1 << 1),
135 } quic_ctx_flags_t;
136
137 /* This structure is used to implement the concept of VPP connection for QUIC.
138  * We create one per connection and one per stream. */
139 typedef struct quic_ctx_
140 {
141   union
142   {
143     transport_connection_t connection;
144     struct
145     {         /** QUIC ctx case */
146       quicly_conn_t *conn;
147       u32 listener_ctx_id;
148       u32 client_opaque;
149       u8 *srv_hostname;
150       u8 conn_state;
151       u8 udp_is_ip4;
152       u8 _qctx_end_marker;      /* Leave this at the end */
153     };
154     struct
155     {         /** STREAM ctx case */
156       quicly_stream_t *stream;
157       u32 quic_connection_ctx_id;
158       u8 _sctx_end_marker;      /* Leave this at the end */
159     };
160   };
161   session_handle_t udp_session_handle;
162   u32 timer_handle;
163   u32 parent_app_wrk_id;
164   u32 parent_app_id;
165   u32 ckpair_index;
166   u32 crypto_engine;
167   u32 crypto_context_index;
168   u8 flags;
169
170   struct
171   {
172     ptls_cipher_context_t *hp_ctx;
173     ptls_aead_context_t *aead_ctx;
174   } ingress_keys;
175   int key_phase_ingress;
176
177 } quic_ctx_t;
178
179 /* Make sure our custom fields don't overlap with the fields we use in
180    .connection
181 */
182 STATIC_ASSERT (offsetof (quic_ctx_t, _qctx_end_marker) <=
183                TRANSPORT_CONN_ID_LEN,
184                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
185 STATIC_ASSERT (offsetof (quic_ctx_t, _sctx_end_marker) <=
186                TRANSPORT_CONN_ID_LEN,
187                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
188
189 /* single-entry session cache */
190 typedef struct quic_session_cache_
191 {
192   ptls_encrypt_ticket_t super;
193   uint8_t id[32];
194   ptls_iovec_t data;
195 } quic_session_cache_t;
196
197 typedef struct quic_stream_data_
198 {
199   u32 ctx_id;
200   u32 thread_index;
201   u32 app_rx_data_len;          /**< bytes received, to be read by external app */
202   u32 app_tx_data_len;          /**< bytes sent */
203 } quic_stream_data_t;
204
205 typedef struct quic_crypto_context_data_
206 {
207   quicly_context_t quicly_ctx;
208   char cid_key[QUIC_IV_LEN];
209   ptls_context_t ptls_ctx;
210 } quic_crypto_context_data_t;
211
212 typedef struct quic_encrypt_cb_ctx_
213 {
214   quicly_datagram_t *packet;
215   struct quic_finalize_send_packet_cb_ctx_
216   {
217     size_t payload_from;
218     size_t first_byte_at;
219     ptls_cipher_context_t *hp;
220   } snd_ctx[QUIC_MAX_COALESCED_PACKET];
221   size_t snd_ctx_count;
222 } quic_encrypt_cb_ctx;
223
224 typedef struct quic_crypto_batch_ctx_
225 {
226   vnet_crypto_op_t aead_crypto_tx_packets_ops[QUIC_SEND_MAX_BATCH_PACKETS],
227     aead_crypto_rx_packets_ops[QUIC_RCV_MAX_BATCH_PACKETS];
228   size_t nb_tx_packets, nb_rx_packets;
229 } quic_crypto_batch_ctx_t;
230
231 typedef struct quic_worker_ctx_
232 {
233   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
234   int64_t time_now;                                /**< worker time */
235   tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel;    /**< worker timer wheel */
236   quicly_cid_plaintext_t next_cid;
237   crypto_context_t *crypto_ctx_pool;            /**< per thread pool of crypto contexes */
238   clib_bihash_24_8_t crypto_context_hash;       /**< per thread [params:crypto_ctx_index] hash */
239   quic_crypto_batch_ctx_t crypto_context_batch;
240 } quic_worker_ctx_t;
241
242 typedef struct quic_rx_packet_ctx_
243 {
244   quicly_decoded_packet_t packet;
245   u8 data[QUIC_MAX_PACKET_SIZE];
246   u32 ctx_index;
247   u32 thread_index;
248   union
249   {
250     struct sockaddr sa;
251     struct sockaddr_in6 sa6;
252   };
253   socklen_t salen;
254   u8 ptype;
255   session_dgram_hdr_t ph;
256 } quic_rx_packet_ctx_t;
257
258 typedef struct quic_main_
259 {
260   u32 app_index;
261   quic_ctx_t **ctx_pool;
262   quic_worker_ctx_t *wrk_ctx;
263   clib_bihash_16_8_t connection_hash;   /**< quic connection id -> conn handle */
264   f64 tstamp_ticks_per_clock;
265
266   ptls_cipher_suite_t ***quic_ciphers;  /**< available ciphers by crypto engine */
267   uword *available_crypto_engines;      /**< Bitmap for registered engines */
268   u8 default_crypto_engine;             /**< Used if you do connect with CRYPTO_ENGINE_NONE (0) */
269   u64 max_packets_per_key;              /**< number of packets that can be sent without a key update */
270
271   ptls_handshake_properties_t hs_properties;
272   quic_session_cache_t session_cache;
273
274   u32 udp_fifo_size;
275   u32 udp_fifo_prealloc;
276   u32 connection_timeout;
277
278   u8 vnet_crypto_enabled;
279
280   clib_rwlock_t crypto_keys_quic_rw_lock;
281 } quic_main_t;
282
283 #endif /* __included_quic_h__ */
284
285 /*
286  * fd.io coding-style-patch-verification: ON
287  *
288  * Local Variables:
289  * eval: (c-set-style "gnu")
290  * End:
291  */