tokio_quiche/lib.rs
1// Copyright (C) 2025, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#![allow(clippy::collapsible_match)]
28
29//! Bridging the gap between [quiche] and [tokio].
30//!
31//! tokio-quiche connects [quiche::Connection]s and [quiche::h3::Connection]s to
32//! tokio's event loop. Users have the choice between implementing their own,
33//! custom [`ApplicationOverQuic`] or using the ready-made
34//! [H3Driver](crate::http3::driver::H3Driver) for HTTP/3 clients and servers.
35//!
36//! # Starting an HTTP/3 Server
37//!
38//! A server [`listen`]s on a UDP socket for QUIC connections and spawns a new
39//! tokio task to handle each individual connection.
40//!
41//! ```
42//! use futures::stream::StreamExt;
43//! use tokio_quiche::http3::settings::Http3Settings;
44//! use tokio_quiche::listen;
45//! use tokio_quiche::metrics::DefaultMetrics;
46//! use tokio_quiche::quic::SimpleConnectionIdGenerator;
47//! use tokio_quiche::ConnectionParams;
48//! use tokio_quiche::ServerH3Driver;
49//!
50//! # async fn example() -> tokio_quiche::QuicResult<()> {
51//! let socket = tokio::net::UdpSocket::bind("0.0.0.0:443").await?;
52//! let mut listeners =
53//! listen([socket], ConnectionParams::default(), DefaultMetrics)?;
54//! let mut accept_stream = &mut listeners[0];
55//!
56//! while let Some(conn) = accept_stream.next().await {
57//! let (driver, mut controller) =
58//! ServerH3Driver::new(Http3Settings::default());
59//! conn?.start(driver);
60//!
61//! tokio::spawn(async move {
62//! // `controller` is the handle to our established HTTP/3 connection.
63//! // For example, inbound requests are available as H3Events via:
64//! let event = controller.event_receiver_mut().recv().await;
65//! });
66//! }
67//! # Ok(())
68//! # }
69//! ```
70//!
71//! For client-side use cases, check out our [`connect`](crate::quic::connect)
72//! API.
73//!
74//! # Feature Flags
75//!
76//! tokio-quiche supports a number of feature flags to enable experimental
77//! features, performance enhancements, and additional telemetry.
78//!
79//! Enabled by default:
80//!
81//! - `qlog-gzip`: Forwards to the `qlog` crate's `gzip` feature so QLOG output
82//! can be emitted as `.sqlog.gz` via `flate2`.
83//! - `qlog-zstd`: Forwards to the `qlog` crate's `zstd` feature so QLOG output
84//! can be emitted as `.sqlog.zst`. Pulls in the `zstd` crate (C dependency
85//! via `zstd-sys`).
86//!
87//! Both compression features may be enabled together; the algorithm
88//! is selected per connection at runtime via
89//! [`settings::QuicSettings::qlog_compression`]. Disable both with
90//! `default-features = false` to opt out of the extra dependencies;
91//! the default `QlogCompression::None` keeps writing raw `.sqlog`
92//! files in that configuration.
93//!
94//! Off by default:
95//!
96//! - `rpk`: Support for raw public keys (RPK) in QUIC handshakes (via
97//! [boring]).
98//! - `gcongestion`: Replace quiche's original congestion control implementation
99//! with one adapted from google/quiche.
100//! - `zero-copy`: Deprecated. Zero-copy sends are now always enabled. This
101//! feature is kept for backwards compatibility and only enables
102//! `gcongestion`.
103//! - `perf-quic-listener-metrics`: Extra telemetry for QUIC handshake
104//! durations, including protocol overhead and network delays.
105//! - `tokio-task-metrics`: Scheduling & poll duration histograms for tokio
106//! tasks.
107//!
108//! Other parts of the crate are enabled by separate build flags instead, to be
109//! controlled by the final binary:
110//!
111//! - `--cfg capture_keylogs`: Optional `SSLKEYLOGFILE` capturing for QUIC
112//! connections.
113
114pub extern crate quiche;
115
116pub mod buf_factory;
117pub mod http3;
118pub mod metrics;
119pub mod quic;
120mod result;
121pub mod settings;
122pub mod socket;
123
124pub use datagram_socket;
125
126use foundations::telemetry::settings::LogVerbosity;
127use std::io;
128use std::sync::Arc;
129use std::sync::Once;
130use tokio::net::UdpSocket;
131use tokio_stream::wrappers::ReceiverStream;
132
133use crate::metrics::Metrics;
134use crate::socket::QuicListener;
135
136pub use crate::http3::driver::ClientH3Controller;
137pub use crate::http3::driver::ClientH3Driver;
138pub use crate::http3::driver::ServerH3Controller;
139pub use crate::http3::driver::ServerH3Driver;
140pub use crate::http3::ClientH3Connection;
141pub use crate::http3::ServerH3Connection;
142pub use crate::quic::connection::ApplicationOverQuic;
143pub use crate::quic::connection::ConnectionIdGenerator;
144pub use crate::quic::connection::InitialQuicConnection;
145pub use crate::quic::connection::QuicConnection;
146pub use crate::result::BoxError;
147pub use crate::result::QuicResult;
148pub use crate::settings::ConnectionParams;
149
150#[doc(hidden)]
151pub use crate::result::QuicResultExt;
152
153/// A stream of accepted [`InitialQuicConnection`]s from a [`listen`] call.
154///
155/// Errors from processing the client's QUIC initials can also be emitted on
156/// this stream. These do not indicate that the listener itself has failed.
157pub type QuicConnectionStream<M> =
158 ReceiverStream<io::Result<InitialQuicConnection<UdpSocket, M>>>;
159
160/// Starts listening for inbound QUIC connections on the given
161/// [`QuicListener`]s.
162///
163/// Each socket starts a separate tokio task to process and route inbound
164/// packets. This task emits connections on the respective
165/// [`QuicConnectionStream`] after receiving the client's QUIC initial and
166/// (optionally) validating its IP address.
167///
168/// The task shuts down when the returned stream is closed (or dropped) and all
169/// previously-yielded connections are closed.
170pub fn listen_with_capabilities<M>(
171 sockets: impl IntoIterator<Item = QuicListener>, params: ConnectionParams,
172 metrics: M,
173) -> io::Result<Vec<QuicConnectionStream<M>>>
174where
175 M: Metrics,
176{
177 if params.settings.capture_quiche_logs {
178 capture_quiche_logs();
179 }
180
181 sockets
182 .into_iter()
183 .map(|s| crate::quic::start_listener(s, ¶ms, metrics.clone()))
184 .collect()
185}
186
187/// Starts listening for inbound QUIC connections on the given `sockets`.
188///
189/// Each socket is converted into a [`QuicListener`] with defaulted socket
190/// parameters. The listeners are then passed to [`listen_with_capabilities`].
191pub fn listen<S, M>(
192 sockets: impl IntoIterator<Item = S>, params: ConnectionParams, metrics: M,
193) -> io::Result<Vec<QuicConnectionStream<M>>>
194where
195 S: TryInto<QuicListener, Error = io::Error>,
196 M: Metrics,
197{
198 let quic_sockets: Vec<QuicListener> = sockets
199 .into_iter()
200 .map(|s| {
201 #[cfg_attr(not(target_os = "linux"), expect(unused_mut))]
202 let mut socket = s.try_into()?;
203 #[cfg(target_os = "linux")]
204 socket.apply_max_capabilities();
205 Ok(socket)
206 })
207 .collect::<io::Result<_>>()?;
208
209 listen_with_capabilities(quic_sockets, params, metrics)
210}
211
212static GLOBAL_LOGGER_ONCE: Once = Once::new();
213
214/// Forward Quiche logs into the slog::Drain currently used by Foundations
215///
216/// # Warning
217///
218/// This should **only be used for local debugging**. Quiche can potentially
219/// emit lots (and lots, and lots) of logs (the TRACE level emits a log record
220/// on every packet and frame) and you can very easily overwhelm your logging
221/// pipeline.
222///
223/// # Note
224///
225/// Quiche uses the `env_logger` crate, which uses `log` under the hood. `log`
226/// requires that you only set the global logger once. That means that we have
227/// to register the logger at `listen()` time for servers - for clients, we
228/// should register loggers when the `quiche::Connection` is established.
229pub(crate) fn capture_quiche_logs() {
230 GLOBAL_LOGGER_ONCE.call_once(|| {
231 use foundations::telemetry::log as foundations_log;
232 use log::Level as std_level;
233
234 let curr_logger =
235 Arc::clone(&foundations_log::slog_logger()).read().clone();
236 let scope_guard = slog_scope::set_global_logger(curr_logger);
237
238 // Convert slog::Level from Foundations settings to log::Level
239 let normalized_level = match foundations_log::verbosity() {
240 LogVerbosity::Critical | LogVerbosity::Error => std_level::Error,
241 LogVerbosity::Warning => std_level::Warn,
242 LogVerbosity::Info => std_level::Info,
243 LogVerbosity::Debug => std_level::Debug,
244 LogVerbosity::Trace => std_level::Trace,
245 };
246
247 slog_stdlog::init_with_level(normalized_level).unwrap();
248
249 // The slog Drain becomes `slog::Discard` when the scope_guard is dropped,
250 // and you can't set the global logger again because of a mandate
251 // in the `log` crate. We have to manually `forget` the scope
252 // guard so that the logger remains registered for the duration of the
253 // process.
254 std::mem::forget(scope_guard)
255 });
256}