quic: fix stream tx_fifo race condition
[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 #define QUIC_SEND_MAX_BATCH_PACKETS 16
45 #define QUIC_RCV_MAX_BATCH_PACKETS 16
46 #define QUIC_DEFAULT_CONN_TIMEOUT (30 * 1000)   /* 30 seconds */
47
48 /* Taken from quicly.c */
49 #define QUICLY_QUIC_BIT 0x40
50
51 #define QUICLY_PACKET_TYPE_INITIAL (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0)
52 #define QUICLY_PACKET_TYPE_0RTT (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x10)
53 #define QUICLY_PACKET_TYPE_HANDSHAKE (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x20)
54 #define QUICLY_PACKET_TYPE_RETRY (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x30)
55 #define QUICLY_PACKET_TYPE_BITMASK 0xf0
56
57 /* error codes */
58 #define QUIC_ERROR_FULL_FIFO 0xff10
59 #define QUIC_APP_ERROR_CLOSE_NOTIFY QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0)
60 #define QUIC_APP_ALLOCATION_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x1)
61 #define QUIC_APP_ACCEPT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x2)
62 #define QUIC_APP_CONNECT_NOTIFY_ERROR QUICLY_ERROR_FROM_APPLICATION_ERROR_CODE(0x3)
63
64 #if QUIC_DEBUG
65 #define QUIC_DBG(_lvl, _fmt, _args...)   \
66   if (_lvl <= QUIC_DEBUG)                \
67     clib_warning (_fmt, ##_args)
68 #else
69 #define QUIC_DBG(_lvl, _fmt, _args...)
70 #endif
71
72 #if CLIB_ASSERT_ENABLE
73 #define QUIC_ASSERT(truth) ASSERT (truth)
74 #else
75 #define QUIC_ASSERT(truth)                        \
76   do {                                            \
77     if (PREDICT_FALSE (! (truth)))                \
78       QUIC_ERR ("ASSERT(%s) failed", # truth);    \
79   } while (0)
80 #endif
81
82 #define QUIC_ERR(_fmt, _args...)                \
83   do {                                          \
84     clib_warning ("QUIC-ERR: " _fmt, ##_args);  \
85   } while (0)
86
87
88
89 extern vlib_node_registration_t quic_input_node;
90
91 typedef enum
92 {
93 #define quic_error(n,s) QUIC_ERROR_##n,
94 #include <plugins/quic/quic_error.def>
95 #undef quic_error
96   QUIC_N_ERROR,
97 } quic_error_t;
98
99 typedef enum quic_ctx_conn_state_
100 {
101   QUIC_CONN_STATE_OPENED,
102   QUIC_CONN_STATE_HANDSHAKE,
103   QUIC_CONN_STATE_READY,
104   QUIC_CONN_STATE_PASSIVE_CLOSING,
105   QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED,
106   QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED,
107   QUIC_CONN_STATE_ACTIVE_CLOSING,
108 } quic_ctx_conn_state_t;
109
110 typedef enum quic_packet_type_
111 {
112   QUIC_PACKET_TYPE_NONE,
113   QUIC_PACKET_TYPE_RECEIVE,
114   QUIC_PACKET_TYPE_MIGRATE,
115   QUIC_PACKET_TYPE_ACCEPT,
116   QUIC_PACKET_TYPE_RESET,
117   QUIC_PACKET_TYPE_DROP,
118 } quic_packet_type_t;
119
120 typedef enum quic_ctx_flags_
121 {
122   QUIC_F_IS_STREAM = (1 << 0),
123   QUIC_F_IS_LISTENER = (1 << 1),
124 } quic_ctx_flags_t;
125
126 /* This structure is used to implement the concept of VPP connection for QUIC.
127  * We create one per connection and one per stream. */
128 typedef struct quic_ctx_
129 {
130   union
131   {
132     transport_connection_t connection;
133     struct
134     {         /** QUIC ctx case */
135       quicly_conn_t *conn;
136       u32 listener_ctx_id;
137       u32 client_opaque;
138       u8 *srv_hostname;
139       u8 conn_state;
140       u8 udp_is_ip4;
141       u8 _qctx_end_marker;      /* Leave this at the end */
142     };
143     struct
144     {         /** STREAM ctx case */
145       quicly_stream_t *stream;
146       u32 quic_connection_ctx_id;
147       u8 _sctx_end_marker;      /* Leave this at the end */
148     };
149   };
150   session_handle_t udp_session_handle;
151   u32 timer_handle;
152   u32 parent_app_wrk_id;
153   u32 parent_app_id;
154   u32 ckpair_index;
155   quicly_context_t *quicly_ctx;
156   u8 flags;
157 } quic_ctx_t;
158
159 /* Make sure our custom fields don't overlap with the fields we use in
160    .connection
161 */
162 STATIC_ASSERT (offsetof (quic_ctx_t, _qctx_end_marker) <=
163                TRANSPORT_CONN_ID_LEN,
164                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
165 STATIC_ASSERT (offsetof (quic_ctx_t, _sctx_end_marker) <=
166                TRANSPORT_CONN_ID_LEN,
167                "connection data must be less than TRANSPORT_CONN_ID_LEN bytes");
168
169 /* single-entry session cache */
170 typedef struct quic_session_cache_
171 {
172   ptls_encrypt_ticket_t super;
173   uint8_t id[32];
174   ptls_iovec_t data;
175 } quic_session_cache_t;
176
177 typedef struct quic_stream_data_
178 {
179   u32 ctx_id;
180   u32 thread_index;
181   u32 app_rx_data_len;          /**< bytes received, to be read by external app */
182   u32 app_tx_data_len;          /**< bytes sent */
183 } quic_stream_data_t;
184
185 typedef struct quic_worker_ctx_
186 {
187   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
188   int64_t time_now;                                /**< worker time */
189   tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel;    /**< worker timer wheel */
190 } quic_worker_ctx_t;
191
192 typedef struct quic_rx_packet_ctx_
193 {
194   quicly_decoded_packet_t packet;
195   u8 data[QUIC_MAX_PACKET_SIZE];
196   u32 ctx_index;
197   u32 thread_index;
198   union
199   {
200     struct sockaddr sa;
201     struct sockaddr_in6 sa6;
202   };
203   socklen_t salen;
204   u8 ptype;
205   session_dgram_hdr_t ph;
206 } quic_rx_packet_ctx_t;
207
208 typedef struct quicly_ctx_data_
209 {
210   quicly_context_t quicly_ctx;
211   char cid_key[17];
212   ptls_context_t ptls_ctx;
213 } quicly_ctx_data_t;
214
215 typedef struct quic_main_
216 {
217   u32 app_index;
218   quic_ctx_t **ctx_pool;
219   quic_worker_ctx_t *wrk_ctx;
220   clib_bihash_16_8_t connection_hash;   /**< quic connection id -> conn handle */
221   f64 tstamp_ticks_per_clock;
222
223   ptls_cipher_suite_t ***quic_ciphers;  /**< available ciphers by crypto engine */
224   uword *available_crypto_engines;      /**< Bitmap for registered engines */
225   u8 default_crypto_engine;             /**< Used if you do connect with CRYPTO_ENGINE_NONE (0) */
226
227   ptls_handshake_properties_t hs_properties;
228   quicly_cid_plaintext_t next_cid;
229   quic_session_cache_t session_cache;
230
231   u32 udp_fifo_size;
232   u32 udp_fifo_prealloc;
233   u32 connection_timeout;
234 } quic_main_t;
235
236 #endif /* __included_quic_h__ */
237
238 /*
239  * fd.io coding-style-patch-verification: ON
240  *
241  * Local Variables:
242  * eval: (c-set-style "gnu")
243  * End:
244  */