Line data Source code
1 : #ifndef HEADER_fd_src_waltz_quic_fd_quic_h
2 : #define HEADER_fd_src_waltz_quic_fd_quic_h
3 :
4 : /* fd_quic_t is a partial implementation of QUIC -- an encrypted,
5 : multiplexing transport layer network protocol.
6 :
7 : For now, IPv4 over Ethernet (EN10MB) flows are supported.
8 :
9 : This API is non-blocking and single-threaded. Any requests to the
10 : peer (such as "open a connection") are queued and processed on RX
11 : or service call. The user is notified of events via callbacks.
12 : The user must further ensure that RX (via fd_aio_t) is dispatched
13 : only from the thread with the active join to the target fd_quic_t.
14 :
15 : Scaling is achieved via fd_quic_t instances and steering of RX flows.
16 : For example, incoming RX that exceeds the throughput of one fd_quic_t
17 : may be load balanced based on QUIC dest conn ID, or UDP src flow ID.
18 :
19 : This implementation aims to be compliant to RFC 9000 and RFC 9001:
20 : - https://datatracker.ietf.org/doc/html/rfc9000
21 : - https://datatracker.ietf.org/doc/html/rfc9001
22 :
23 : ### Memory Management
24 :
25 : fd_quic is entirely pre-allocated. Currently, a QUIC object reserves
26 : space for a number of connection slots, with uniform stream,
27 : reassembly, and ACK buffers.
28 :
29 : ### Memory Layout
30 :
31 : fd_quic_t is the publicly exported memory layout of a QUIC object.
32 : The private memory region of a QUIC object extends beyond the end of
33 : this struct. fd_quic_t is not intended to be allocated directly,
34 : refer to the below for details.
35 :
36 : ### Lifecycle
37 :
38 : The below state diagram shows the lifecycle of an fd_quic_t.
39 :
40 : ┌───────────┐ new ┌───────────┐ join ┌──────────┐
41 : │ ├───────►│ ├──────►│ │
42 : │ allocated │ │ formatted │ │ joined │
43 : │ │◄───────┤ │◄──────┤ │
44 : └───────────┘ delete └───────────┘ leave └───▲───┬──┘
45 : │ │ set config
46 : │ │ set callbacks
47 : fini │ │ init
48 : ┌──┴───▼──┐
49 : ┌───│ │
50 : service │ │ ready │
51 : └──►│ │
52 : └─────────┘
53 :
54 : ### Lifecycle: Allocation & Formatting
55 :
56 : A QUIC object resides in a contiguous pre-allocated memory region.
57 : (Usually, in a Firedancer workspace) The footprint and internal
58 : layout depends on the pre-configured fd_quic_limits_t parameters.
59 : These limits are constant throughout the lifetime of an fd_quic_t.
60 :
61 : Use fd_quic_{align,footprint} to determine size and alignment of the
62 : memory region to be used. Use fd_quic_new to format such a memory
63 : region and to obtain an opaque handle. In the formatted state, the
64 : fd_quic_t is position-independent (may be mapped at different virtual
65 : addresses). This is useful for separating allocation and runtime use
66 : into different steps.
67 :
68 : ### Lifecycle: Joining
69 :
70 : Given an opaque handle, fd_quic_join runs basic coherence checks and
71 : returns a typed pointer to the object. The object is not modified
72 : by this operation. Each object may have multiple active joins, but
73 : only one of them may write. (Typically, a single join is used for
74 : service, and secondary joins for read-only monitoring)
75 :
76 : ### Lifecycle: Usage
77 :
78 : fd_quic_init initializes an fd_quic_t for use. On success, the QUIC
79 : becomes ready to serve from the thread that init was called from (it
80 : is invalid to service QUIC from another thread). */
81 :
82 : /* TODO provide fd_quic on non-hosted targets */
83 :
84 : #include "fd_quic_common.h"
85 : #include "fd_quic_enum.h"
86 :
87 : #include "../aio/fd_aio.h"
88 : #include "../tls/fd_tls.h"
89 : #include "../../util/clock/fd_clock.h"
90 : #include "../../util/hist/fd_histf.h"
91 :
92 : /* FD_QUIC_API marks public API declarations. No-op for now. */
93 : #define FD_QUIC_API
94 :
95 : /* Forward declarations */
96 :
97 : struct fd_quic_conn;
98 : typedef struct fd_quic_conn fd_quic_conn_t;
99 :
100 : struct fd_quic_stream;
101 : typedef struct fd_quic_stream fd_quic_stream_t;
102 :
103 : /* fd_quic_limits_t defines the memory layout of an fd_quic_t object.
104 : Limits are immutable and valid for the lifetime of an fd_quic_t
105 : (i.e. outlasts joins, until fd_quic_delete) */
106 :
107 : struct __attribute__((aligned(16UL))) fd_quic_limits {
108 : ulong conn_cnt; /* instance-wide, max concurrent conn count */
109 : ulong handshake_cnt; /* instance-wide, max concurrent handshake count */
110 : ulong log_depth; /* instance-wide, depth of shm log cache */
111 :
112 : ulong conn_id_cnt; /* per-conn, max conn ID count (min 4UL) */
113 : ulong stream_id_cnt; /* per-conn, max concurrent stream ID count */
114 : ulong inflight_frame_cnt; /* instance-wide, total max inflight frame count */
115 : ulong min_inflight_frame_cnt_conn; /* per-conn, min inflight frame count */
116 :
117 : ulong tx_buf_sz; /* per-stream, tx buf sz in bytes */
118 : /* the user consumes rx directly from the network buffer */
119 :
120 : ulong stream_pool_cnt; /* instance-wide, number of streams in stream pool */
121 : };
122 : typedef struct fd_quic_limits fd_quic_limits_t;
123 :
124 : /* fd_quic_layout_t is an offset table describing the memory layout of
125 : an fd_quic_t object. It is deived from fd_quic_limits_t. */
126 :
127 : struct fd_quic_layout {
128 : ulong meta_sz; /* size of this struct */
129 : ulong log_off; /* offset to quic_log */
130 : ulong conns_off; /* offset of connection mem region */
131 : ulong conn_footprint; /* sizeof a conn */
132 : ulong conn_map_off; /* offset of conn map mem region */
133 : int lg_slot_cnt; /* see conn_map_new */
134 : ulong hs_pool_off; /* offset of the handshake pool */
135 : ulong stream_pool_off; /* offset of the stream pool */
136 : ulong svc_timers_off; /* offset of the service timers */
137 : ulong pkt_meta_pool_off; /* offset of the pkt_meta pool */
138 : };
139 :
140 : typedef struct fd_quic_layout fd_quic_layout_t;
141 :
142 : /* fd_quic_config_t defines mutable config of an fd_quic_t. The config is
143 : immutable during an active join. */
144 :
145 : struct __attribute__((aligned(16UL))) fd_quic_config {
146 : /* Used by tracing/logging code */
147 : #define FD_QUIC_CONFIG_ENUM_LIST_role(X,...) \
148 : X( FD_QUIC_ROLE_CLIENT, "ROLE_CLIENT" ) \
149 : X( FD_QUIC_ROLE_SERVER, "ROLE_SERVER" )
150 :
151 : #define FD_QUIC_CONFIG_LIST(X,...) \
152 0 : X( role, "%d", enum, "enum", __VA_ARGS__ ) \
153 0 : X( retry, "%d", bool, "bool", __VA_ARGS__ ) \
154 0 : X( idle_timeout, "%ld", units, "ns", __VA_ARGS__ ) \
155 0 : X( keep_alive, "%d", bool, "bool", __VA_ARGS__ ) \
156 0 : X( ack_delay, "%ld", units, "ns", __VA_ARGS__ ) \
157 0 : X( ack_threshold, "%lu", units, "bytes", __VA_ARGS__ ) \
158 0 : X( retry_ttl, "%ld", units, "ns", __VA_ARGS__ ) \
159 0 : X( tls_hs_ttl, "%ld", units, "ns", __VA_ARGS__ ) \
160 0 : X( identity_public_key, "%x", hex32, "", __VA_ARGS__ ) \
161 0 : X( sign, "%p", ptr, "", __VA_ARGS__ ) \
162 0 : X( sign_ctx, "%p", ptr, "", __VA_ARGS__ ) \
163 0 : X( keylog_file, "%s", value, "", __VA_ARGS__ ) \
164 0 : X( initial_rx_max_stream_data, "%lu", units, "bytes", __VA_ARGS__ ) \
165 0 : X( net.dscp, "0x%02x", value, "", __VA_ARGS__ )
166 :
167 : /* Protocol config ***************************************/
168 :
169 : /* role: one of FD_QUIC_ROLE_{CLIENT,SERVER} */
170 : int role;
171 :
172 : /* retry: whether address validation using retry packets is enabled (RFC 9000, Section 8.1.2) */
173 : int retry;
174 :
175 : /* idle_timeout: Upper bound on conn idle timeout.
176 : Also sent to peer via max_idle_timeout transport param.
177 : If the peer specifies a lower idle timeout, that is used instead. */
178 : long idle_timeout;
179 4272 : # define FD_QUIC_DEFAULT_IDLE_TIMEOUT (ulong)(1e9) /* 1s */
180 :
181 : /* keep_alive
182 : * whether the fd_quic should use QUIC PING frames to keep connections alive
183 : * Set to 1 to keep connections alive
184 : * Set to 0 to allow connections to close on idle
185 : * default is 0 */
186 : int keep_alive;
187 :
188 : /* ack_delay: median delay on outgoing ACKs. Greater delays allow
189 : fd_quic to coalesce packet ACKs. */
190 : long ack_delay;
191 4272 : # define FD_QUIC_DEFAULT_ACK_DELAY (long)(50e6) /* 50ms */
192 :
193 : /* ack_threshold: immediately send an ACK when the number of
194 : unacknowledged stream bytes exceeds this value. */
195 : ulong ack_threshold;
196 2142 : # define FD_QUIC_DEFAULT_ACK_THRESHOLD (65536UL) /* 64 KiB */
197 :
198 : /* retry_ttl: time-to-live for retry tokens */
199 : long retry_ttl;
200 2139 : # define FD_QUIC_DEFAULT_RETRY_TTL (long)(1e9) /* 1s */
201 :
202 : /* hs_ttl: time-to-live for tls_hs */
203 : long tls_hs_ttl;
204 2139 : # define FD_QUIC_DEFAULT_TLS_HS_TTL (long)(3e9) /* 3s */
205 :
206 : /* TLS config ********************************************/
207 :
208 : /* identity_key: Ed25519 public key of node identity */
209 : uchar identity_public_key[ 32 ];
210 :
211 : /* Callback for signing TLS 1.3 certificate verify payload */
212 : fd_tls_sign_fn_t sign;
213 : void * sign_ctx;
214 :
215 0 : # define FD_QUIC_PATH_LEN 1023UL
216 : char keylog_file[ FD_QUIC_PATH_LEN+1UL ];
217 :
218 : ulong initial_rx_max_stream_data; /* per-stream, rx buf sz in bytes, set by the user. */
219 :
220 : /* Network config ****************************************/
221 :
222 : struct { /* Internet config */
223 : /* dscp: Differentiated services code point.
224 : Set on all outgoing IPv4 packets. */
225 : uchar dscp;
226 : } net;
227 : };
228 :
229 : /* Callback API *******************************************************/
230 :
231 : /* Note: QUIC library invokes callbacks during RX or service. Callback
232 : may only invoke fd_quic API methods labelled CB-safe. Callbacks are
233 : not re-entrant. */
234 :
235 : /* fd_quic_cb_conn_new_t: server received a new conn and completed
236 : handshakes. */
237 : typedef void
238 : (* fd_quic_cb_conn_new_t)( fd_quic_conn_t * conn,
239 : void * quic_ctx );
240 :
241 : /* fd_quic_cb_conn_handshake_complete_t: client completed a handshake
242 : of a conn it created. */
243 : typedef void
244 : (* fd_quic_cb_conn_handshake_complete_t)( fd_quic_conn_t * conn,
245 : void * quic_ctx );
246 :
247 : /* fd_quic_cb_conn_final_t: Conn termination notification. The conn
248 : object is freed immediately after returning. User should destroy any
249 : remaining references to conn in this callback. */
250 : typedef void
251 : (* fd_quic_cb_conn_final_t)( fd_quic_conn_t * conn,
252 : void * quic_ctx );
253 :
254 : /* fd_quic_cb_stream_notify_t signals a notable stream event.
255 : stream_ctx object is the user-provided stream context set in the new
256 : callback.
257 :
258 : TODO will only one notify max be served?
259 : TODO will stream be deallocated immediately after callback?
260 :
261 : notify_type is one of FD_QUIC_NOTIFY_{...} */
262 : typedef void
263 : (* fd_quic_cb_stream_notify_t)( fd_quic_stream_t * stream,
264 : void * stream_ctx,
265 : int notify_type );
266 :
267 : typedef int
268 : (* fd_quic_cb_stream_rx_t)( fd_quic_conn_t * conn,
269 : ulong stream_id,
270 : ulong offset,
271 : uchar const * data,
272 : ulong data_sz,
273 : int fin );
274 :
275 : /* fd_quic_cb_tls_keylog_t is called when a new encryption secret
276 : becomes available. line is a cstr containing the secret in NSS key
277 : log format (intended for tests only). */
278 :
279 : typedef void
280 : (* fd_quic_cb_tls_keylog_t)( void * quic_ctx,
281 : char const * line );
282 :
283 : /* fd_quic_callbacks_t defines the set of user-provided callbacks that
284 : are invoked by the QUIC library. Resets on leave. */
285 :
286 : struct fd_quic_callbacks {
287 : /* Function pointers to user callbacks */
288 :
289 : void * quic_ctx; /* user-provided context pointer
290 : for instance-wide callbacks */
291 :
292 : fd_quic_cb_conn_new_t conn_new; /* non-NULL, with quic_ctx */
293 : fd_quic_cb_conn_handshake_complete_t conn_hs_complete; /* non-NULL, with quic_ctx */
294 : fd_quic_cb_conn_final_t conn_final; /* non-NULL, with quic_ctx */
295 : fd_quic_cb_stream_notify_t stream_notify; /* non-NULL, with stream_ctx */
296 : fd_quic_cb_stream_rx_t stream_rx; /* non-NULL, with stream_ctx */
297 : fd_quic_cb_tls_keylog_t tls_keylog; /* nullable, with quic_ctx */
298 :
299 : };
300 : typedef struct fd_quic_callbacks fd_quic_callbacks_t;
301 :
302 : /* fd_quic metrics ****************************************************/
303 :
304 : /* TODO: evaluate performance impact of metrics */
305 :
306 : union fd_quic_metrics {
307 : struct {
308 : /* Network metrics */
309 : ulong net_rx_pkt_cnt; /* number of IP packets received */
310 : ulong net_rx_byte_cnt; /* total bytes received (including IP, UDP, QUIC headers) */
311 : ulong net_tx_pkt_cnt; /* number of IP packets sent */
312 : ulong net_tx_byte_cnt; /* total bytes sent */
313 : ulong retry_tx_cnt; /* number of Retry packets sent */
314 :
315 : /* Conn metrics */
316 : ulong conn_alloc_cnt; /* number of conns currently allocated */
317 : ulong conn_created_cnt; /* number of conns created */
318 : ulong conn_closed_cnt; /* number of conns gracefully closed */
319 : ulong conn_aborted_cnt; /* number of conns aborted */
320 : ulong conn_timeout_cnt; /* number of conns timed out */
321 : ulong conn_retry_cnt; /* number of conns established with retry */
322 : ulong conn_err_no_slots_cnt; /* number of conns that failed to create due to lack of slots */
323 : ulong conn_err_retry_fail_cnt; /* number of conns that failed during retry (e.g. invalid token) */
324 : ulong conn_state_cnt[ 8 ]; /* current number of conns in each state */
325 :
326 : /* Packet metrics */
327 : ulong pkt_net_hdr_err_cnt; /* number of packets dropped due to weird IPv4/UDP headers */
328 : ulong pkt_quic_hdr_err_cnt; /* number of packets dropped due to weird QUIC header */
329 : ulong pkt_undersz_cnt; /* number of QUIC packets dropped due to being too small */
330 : ulong pkt_oversz_cnt; /* number of QUIC packets dropped due to being too large */
331 : ulong pkt_decrypt_fail_cnt[4]; /* number of packets that failed decryption due to auth tag */
332 : ulong pkt_no_key_cnt[4]; /* number of packets that failed decryption due to missing key */
333 : ulong pkt_no_conn_cnt[4]; /* number of packets with unknown conn ID (initial, retry, hs, 1-RTT) */
334 : ulong frame_tx_alloc_cnt[3]; /* number of pkt_meta alloc successes, fails for empty pool, fails at conn max */
335 : ulong pkt_verneg_cnt; /* number of QUIC version negotiation packets or packets with wrong version */
336 : ulong pkt_retransmissions_cnt; /* number of pkt_meta retries */
337 : ulong initial_token_len_cnt[3]; /* number of Initial packets grouped by token length */
338 :
339 : /* Frame metrics */
340 : ulong frame_rx_cnt[ 22 ]; /* number of frames received (indexed by implementation-defined IDs) */
341 : ulong frame_rx_err_cnt; /* number of frames failed */
342 :
343 : /* Handshake metrics */
344 : ulong hs_created_cnt; /* number of handshake flows created */
345 : ulong hs_err_alloc_fail_cnt; /* number of handshakes dropped due to alloc fail */
346 : ulong hs_evicted_cnt; /* number of handshakes evicted */
347 :
348 : /* Stream metrics */
349 : ulong stream_opened_cnt; /* number of streams opened */
350 : ulong stream_closed_cnt[5]; /* indexed by FD_QUIC_STREAM_NOTIFY_{...} */
351 : ulong stream_active_cnt; /* number of active streams */
352 : ulong stream_rx_event_cnt; /* number of stream RX events */
353 : ulong stream_rx_byte_cnt; /* total stream payload bytes received */
354 :
355 : /* ACK metrics */
356 : ulong ack_tx[ 5 ];
357 :
358 : /* Performance metrics */
359 : fd_histf_t service_duration[ 1 ]; /* time spent in service */
360 : fd_histf_t receive_duration[ 1 ]; /* time spent in process_packet calls */
361 : };
362 : };
363 : typedef union fd_quic_metrics fd_quic_metrics_t;
364 :
365 : /* fd_quic_t memory layout ********************************************/
366 :
367 : struct fd_quic {
368 : ulong magic; /* ==FD_QUIC_MAGIC */
369 :
370 : fd_quic_layout_t layout; /* position-independent, persistent, read only */
371 : fd_quic_limits_t limits; /* position-independent, persistent, read only */
372 : fd_quic_config_t config; /* position-independent, persistent, writable pre init */
373 : fd_quic_callbacks_t cb; /* position-dependent, reset on join, writable pre init */
374 : fd_quic_metrics_t metrics; /* position-independent, persistent, read only */
375 :
376 : fd_aio_t aio_rx; /* local AIO */
377 : fd_aio_t aio_tx; /* remote AIO */
378 :
379 : /* ... private variable-length structures follow ... */
380 : };
381 :
382 : FD_PROTOTYPES_BEGIN
383 :
384 : /* Object lifecycle ***************************************************/
385 :
386 : /* fd_quic_{align,footprint} return the required alignment and footprint
387 : of a memory region suitable for use as an fd_quic_t. align returns
388 : FD_QUIC_ALIGN. limits is a temporary reference to the requested
389 :
390 : On failure, footprint will silently return 0 (and thus can be used by
391 : the caller to validate fd_quic_new params) */
392 :
393 : FD_QUIC_API FD_FN_CONST ulong
394 : fd_quic_align( void );
395 :
396 : FD_QUIC_API ulong
397 : fd_quic_footprint( fd_quic_limits_t const * limits );
398 :
399 : /* fd_quic_new formats an unused memory region for use as a QUIC client
400 : or server. mem is a non-NULL pointer to this region in the local
401 : address with the required footprint and alignment. limits is a
402 : temporary reference, identical to the one given to fd_quic_footprint
403 : used to figure out the required footprint. */
404 :
405 : FD_QUIC_API void *
406 : fd_quic_new( void * mem,
407 : fd_quic_limits_t const * limits );
408 :
409 : /* fd_quic_join joins the caller to the fd_quic. shquic points to the
410 : first byte of the memory region backing the QUIC in the caller's
411 : address space.
412 :
413 : Returns a pointer in the local address space to the public fd_quic_t
414 : region on success (do not assume this to be just a cast of shquic)
415 : and NULL on failure (logs details). Reasons for failure are that
416 : shquic is obviously not a pointer to a correctly formatted QUIC
417 : object. Every successful join should have a matching leave. The
418 : lifetime of the join is until the matching leave or the thread group
419 : is terminated. */
420 :
421 : FD_QUIC_API fd_quic_t *
422 : fd_quic_join( void * shquic );
423 :
424 : /* fd_quic_leave leaves a current local join and frees all dynamically
425 : managed resources (heap allocs, OS handles). Returns the given quic
426 : on success and NULL on failure (logs details). Reasons for failure
427 : include quic is NULL or no active join */
428 :
429 : FD_QUIC_API void *
430 : fd_quic_leave( fd_quic_t * quic );
431 :
432 : /* fd_quic_delete unformats a memory region used as an fd_quic_t.
433 : Assumes nobody is joined to the region. Returns the given quic
434 : pointer on success and NULL if used obviously in error (e.g. quic is
435 : obviously not an fd_quic_t ... logs details). The ownership of the
436 : memory region is transferred ot the caller. */
437 :
438 : FD_QUIC_API void *
439 : fd_quic_delete( fd_quic_t * quic );
440 :
441 : /* Configuration ******************************************************/
442 :
443 : /* fd_quic_{limits,config}_from_env populates the given QUIC limits or
444 : config from command-line args and env vars. If parg{c,v} are non-
445 : NULL, they are updated to strip the parsed args. The last element of
446 : the *argv array must be NULL. Returns given config on success and
447 : NULL on failure (logs details). It is up to the caller to properly
448 : initialize the given limits/config. */
449 :
450 : FD_QUIC_API fd_quic_limits_t *
451 : fd_quic_limits_from_env( int * pargc,
452 : char *** pargv,
453 : fd_quic_limits_t * limits );
454 :
455 : FD_QUIC_API fd_quic_config_t *
456 : fd_quic_config_from_env( int * pargc,
457 : char *** pargv,
458 : fd_quic_config_t * config );
459 :
460 : /* fd_quic_get_aio_net_rx returns this QUIC's aio base class. Valid
461 : for lifetime of QUIC. While pointer to aio can be obtained before
462 : init, calls to aio may only be dispatched by the thread with
463 : exclusive access to QUIC that owns it. */
464 :
465 : FD_QUIC_API fd_aio_t const *
466 : fd_quic_get_aio_net_rx( fd_quic_t * quic );
467 :
468 : /* fd_quic_set_aio_net_tx sets the fd_aio_t used by the fd_quic_t to
469 : send tx data to the network driver. Cleared on fini. */
470 :
471 : FD_QUIC_API void
472 : fd_quic_set_aio_net_tx( fd_quic_t * quic,
473 : fd_aio_t const * aio_tx );
474 :
475 : /* Initialization *****************************************************/
476 :
477 : /* fd_quic_init initializes the QUIC such that it is ready to serve.
478 : permits the calling thread exclusive access during which no other
479 : thread may write to the QUIC. Exclusive rights get released when the
480 : thread exits or calls fd_quic_fini.
481 :
482 : Requires valid configuration and external objects (aio, callbacks).
483 : Returns given quic on success and NULL on failure (logs details).
484 : Performs various heap allocations and file system accesses such
485 : reading certs. Reasons for failure include invalid config or
486 : fd_tls error. */
487 :
488 : FD_QUIC_API fd_quic_t *
489 : fd_quic_init( fd_quic_t * quic );
490 :
491 : /* fd_quic_fini releases exclusive access over a QUIC. Zero-initializes
492 : references to external objects (aio, callbacks). Frees any heap
493 : allocs made by fd_quic_init. Returns quic. */
494 :
495 : FD_QUIC_API fd_quic_t *
496 : fd_quic_fini( fd_quic_t * quic );
497 :
498 : /* NOTE: Calling any of the below requires valid initialization from
499 : this thread group. */
500 :
501 : /* Connection API *****************************************************/
502 :
503 : /* fd_quic_connect initiates a new client connection to a remote QUIC
504 : server. On success, returns a pointer to the conn object managed by
505 : QUIC. On failure, returns NULL. Reasons for failure include quic
506 : not a valid join or out of free conns. Lifetime of returned conn is
507 : until conn_final callback.
508 :
509 : args
510 : dst_ip_addr destination ip address, in net order
511 : dst_udp_port destination port number, in host order */
512 :
513 : FD_QUIC_API fd_quic_conn_t *
514 : fd_quic_connect( fd_quic_t * quic, /* requires exclusive access */
515 : uint dst_ip_addr,
516 : ushort dst_udp_port,
517 : uint src_ip_addr,
518 : ushort src_udp_port,
519 : long now );
520 :
521 : /* fd_quic_conn_close asynchronously initiates a shutdown of the conn.
522 : The given reason code is returned to the peer via a CONNECTION_CLOSE
523 : frame, if possible. Causes conn_final callback to be issued
524 : eventually. */
525 :
526 : FD_QUIC_API void
527 : fd_quic_conn_close( fd_quic_conn_t * conn,
528 : uint reason );
529 :
530 : /* fd_quic_conn_let_die stops keeping a conn alive after
531 : 'keep_alive_duration_ns'. No-op if keep-alive is not configured.
532 : Safe to call on a connection in any state.
533 :
534 : If called multiple times on the same connection, only the latest
535 : call will stay in effect. However, it may not take effect if we
536 : already skipped a keep-alive due to a previous call. 'Undoing' a
537 : previous call can be done by passing ULONG_MAX.
538 :
539 : This function does NOT guarantee that the connection will be closed
540 : immediately after the given duration. Rather, it just disables keep-alive
541 : behavior after the given duration. */
542 :
543 : FD_QUIC_API void
544 : fd_quic_conn_let_die( fd_quic_conn_t * conn,
545 : long keep_alive_duration_ns );
546 :
547 : /* Service API ********************************************************/
548 :
549 : /* fd_quic_get_next_wakeup returns the next requested service time.
550 : This is only intended for unit tests. */
551 :
552 : FD_QUIC_API long
553 : fd_quic_get_next_wakeup( fd_quic_t * quic );
554 :
555 : /* fd_quic_service services the next QUIC connection at each service
556 : level, including stream transmit ops, ACK transmit, loss timeout, and
557 : idle timeout. The user should call service at high frequency.
558 : Returns the number of connections serviced, where 0 means the call
559 : did no work. */
560 :
561 : FD_QUIC_API int
562 : fd_quic_service( fd_quic_t * quic,
563 : long now );
564 :
565 : /* fd_quic_svc_validate checks for violations of service queue and free
566 : list invariants, such as cycles in linked lists. Prints to warning/
567 : error log and exits the process if checks fail. Intended for use in
568 : tests. */
569 :
570 : void
571 : fd_quic_state_validate( fd_quic_t * quic );
572 :
573 : /* Stream Send API ****************************************************/
574 :
575 : /* fd_quic_conn_new_stream creates a new unidirectional stream on the
576 : given conn. On success, returns the newly created stream.
577 : On failure, returns NULL. Reasons for failure include invalid conn
578 : state or out of stream quota.
579 :
580 : The user does not own the returned pointer: its lifetime is managed
581 : by the connection. */
582 :
583 : FD_QUIC_API fd_quic_stream_t *
584 : fd_quic_conn_new_stream( fd_quic_conn_t * conn );
585 :
586 : /* fd_quic_stream_send sends a chunk on a stream in order.
587 :
588 : Use fd_quic_conn_new_stream to create a new stream for sending
589 : or use the new stream callback to obtain a stream for replying.
590 :
591 : args
592 : stream the stream to send on
593 : data points to first byte of buffer (ignored if data_sz==0)
594 : data_sz number of bytes to send
595 : fin final: bool
596 : set to indicate the stream is finalized by the last byte
597 : in the batch
598 : If the last buffer in the batch was rejected, the FIN
599 : flag is not set, and may be applied in a future send
600 : or via the fd_quic_stream_fin(...) function
601 :
602 : returns
603 : 0 success
604 : <0 one of FD_QUIC_SEND_ERR_{INVAL_STREAM,INVAL_CONN,AGAIN} */
605 : FD_QUIC_API int
606 : fd_quic_stream_send( fd_quic_stream_t * stream,
607 : void const * data,
608 : ulong data_sz,
609 : int fin );
610 :
611 : /* fd_quic_stream_fin: finish sending on a stream. Called to signal
612 : no more data will be sent to self-to-peer flow of stream. Peer may
613 : continue sending data on their side of the stream. Caller should
614 : only call stream_fin once per stream, except when fin was already
615 : indicated via stream_send. */
616 :
617 : FD_QUIC_API void
618 : fd_quic_stream_fin( fd_quic_stream_t * stream );
619 :
620 : FD_QUIC_API void
621 : fd_quic_process_packet( fd_quic_t * quic,
622 : uchar * data,
623 : ulong data_sz,
624 : long now );
625 :
626 :
627 : uint
628 : fd_quic_tx_buffered_raw( fd_quic_t * quic,
629 : uchar ** tx_ptr_ptr,
630 : uchar * tx_buf,
631 : ushort * ipv4_id,
632 : uint dst_ipv4_addr,
633 : ushort dst_udp_port,
634 : uint src_ipv4_addr,
635 : ushort src_udp_port );
636 :
637 : FD_PROTOTYPES_END
638 :
639 : /* Convenience exports for consumers of API */
640 : #include "fd_quic_conn.h"
641 : #include "fd_quic_stream.h"
642 :
643 : /* FD_DEBUG_MODE: set to enable debug-only code
644 : TODO move to util? */
645 : #ifdef FD_DEBUG_MODE
646 : #define FD_DEBUG(...) __VA_ARGS__
647 : #else
648 : #define FD_DEBUG(...)
649 : #endif
650 :
651 : #endif /* HEADER_fd_src_waltz_quic_fd_quic_h */
|