c426e39baf41ccb227484b404531906e4401c7dd
[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 /* QUIC log levels
28  * 1 - errors
29  * 2 - connection/stream events
30  * 3 - packet events
31  * 4 - timer events
32  **/
33
34 #define QUIC_DEBUG               0
35 #define QUIC_TSTAMP_RESOLUTION  0.001   /* QUIC tick resolution (1ms) */
36 #define QUIC_TIMER_HANDLE_INVALID ((u32) ~0)
37 #define QUIC_SESSION_INVALID ((u32) ~0 - 1)
38 #define QUIC_MAX_PACKET_SIZE 1280
39
40 #define QUIC_INT_MAX  0x3FFFFFFFFFFFFFFF
41 #define QUIC_DEFAULT_FIFO_SIZE (64 << 10)
42 #define QUIC_SEND_PACKET_VEC_SIZE 16
43
44 /* Taken from quicly.c */
45 #define QUICLY_QUIC_BIT 0x40
46
47 #define QUICLY_PACKET_TYPE_INITIAL (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0)
48 #define QUICLY_PACKET_TYPE_0RTT (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x10)
49 #define QUICLY_PACKET_TYPE_HANDSHAKE (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x20)
50 #define QUICLY_PACKET_TYPE_RETRY (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x30)
51 #define QUICLY_PACKET_TYPE_BITMASK 0xf0
52
53 /* error codes */
54 #define QUIC_ERROR_FULL_FIFO 0xff10
55 #define QUIC_APP_ERROR_CLOSE_NOTIFY QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0)
56 #define QUIC_APP_ALLOCATION_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x1)
57 #define QUIC_APP_ACCEPT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x2)
58 #define QUIC_APP_CONNECT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x3)
59
60 #if QUIC_DEBUG
61 #define QUIC_DBG(_lvl, _fmt, _args...)   \
62   if (_lvl <= QUIC_DEBUG)                \
63     clib_warning (_fmt, ##_args)
64 #else
65 #define QUIC_DBG(_lvl, _fmt, _args...)
66 #endif
67
68 #define QUIC_ERR(_fmt, _args...)                \
69   do {                                          \
70     clib_warning ("QUIC-ERR: " _fmt, ##_args);  \
71   } while (0)
72
73 extern vlib_node_registration_t quic_input_node;
74
75 typedef enum
76 {
77 #define quic_error(n,s) QUIC_ERROR_##n,
78 #include <plugins/quic/quic_error.def>
79 #undef quic_error
80   QUIC_N_ERROR,
81 } quic_error_t;
82
83 typedef enum quic_ctx_conn_state_
84 {
85   QUIC_CONN_STATE_OPENED,
86   QUIC_CONN_STATE_HANDSHAKE,
87   QUIC_CONN_STATE_READY,
88   QUIC_CONN_STATE_PASSIVE_CLOSING,
89   QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED,
90   QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED,
91   QUIC_CONN_STATE_ACTIVE_CLOSING,
92 } quic_ctx_conn_state_t;
93
94
95 typedef enum quic_ctx_flags_
96 {
97   QUIC_F_IS_STREAM = (1 << 0),
98   QUIC_F_IS_LISTENER = (1 << 1),
99 } quic_ctx_flags_t;
100
101 /* This structure is used to implement the concept of VPP connection for QUIC.
102  * We create one per connection and one per stream. */
103 typedef struct quic_ctx_
104 {
105   union
106   {
107     transport_connection_t connection;
108     struct
109     {         /** QUIC ctx case */
110       quicly_conn_t *conn;
111       u32 listener_ctx_id;
112       u32 client_opaque;
113       u8 *srv_hostname;
114       u8 conn_state;
115       u8 udp_is_ip4;
116       u8 _qctx_end_marker;      /* Leave this at the end */
117     };
118     struct
119     {         /** STREAM ctx case */
120       quicly_stream_t *stream;
121       u32 quic_connection_ctx_id;
122       u8 _sctx_end_marker;      /* Leave this at the end */
123     };
124   };
125   session_handle_t udp_session_handle;
126   u32 timer_handle;
127   u32 parent_app_wrk_id;
128   u32 parent_app_id;
129   u32 ckpair_index;
130   quicly_context_t *quicly_ctx;
131   u8 flags;
132 } quic_ctx_t;
133
134 /* Make sure our custom fields don't overlap with the fields we use in
135    .connection
136 */
137 STATIC_ASSERT (offsetof (quic_ctx_t, _qctx_end_marker) <=
138                TRANSPORT_CONN_ID_LEN,
139                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
140 STATIC_ASSERT (offsetof (quic_ctx_t, _sctx_end_marker) <=
141                TRANSPORT_CONN_ID_LEN,
142                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
143
144 /* single-entry session cache */
145 typedef struct quic_session_cache_
146 {
147   ptls_encrypt_ticket_t super;
148   uint8_t id[32];
149   ptls_iovec_t data;
150 } quic_session_cache_t;
151
152 typedef struct quic_stream_data_
153 {
154   u32 ctx_id;
155   u32 thread_index;
156   u32 app_rx_data_len;          /* bytes received, to be read by external app */
157 } quic_stream_data_t;
158
159 typedef struct quic_worker_ctx_
160 {
161   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
162   int64_t time_now;                                /**< worker time */
163   tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel;    /**< worker timer wheel */
164   u32 *opening_ctx_pool;
165 } quic_worker_ctx_t;
166
167 typedef struct quic_rx_packet_ctx_
168 {
169   quicly_decoded_packet_t packet;
170   u8 data[QUIC_MAX_PACKET_SIZE];
171   u32 ctx_index;
172   u32 thread_index;
173 } quic_rx_packet_ctx_t;
174
175 typedef struct quicly_ctx_data_
176 {
177   quicly_context_t quicly_ctx;
178   char cid_key[17];
179   ptls_context_t ptls_ctx;
180 } quicly_ctx_data_t;
181
182 typedef struct quic_main_
183 {
184   u32 app_index;
185   quic_ctx_t **ctx_pool;
186   quic_worker_ctx_t *wrk_ctx;
187   clib_bihash_16_8_t connection_hash;   /* quic connection id -> conn handle */
188   f64 tstamp_ticks_per_clock;
189
190   ptls_cipher_suite_t ***quic_ciphers;  /* available ciphers by crypto engine */
191   u8 default_cipher;
192   quic_session_cache_t session_cache;
193
194   /*
195    * Config
196    */
197   quicly_context_t quicly_ctx;
198   ptls_handshake_properties_t hs_properties;
199   quicly_cid_plaintext_t next_cid;
200
201   u32 udp_fifo_size;
202   u32 udp_fifo_prealloc;
203 } quic_main_t;
204
205 #endif /* __included_quic_h__ */
206
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "gnu")
212  * End:
213  */