Skip to main content

tokio_quiche/quic/
mod.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//! `async`-ified QUIC connections powered by [quiche].
28//!
29//! Hooking up a [quiche::Connection] to [tokio]'s executor and IO primitives
30//! requires an [`ApplicationOverQuic`] to control the connection. The
31//! application exposes a small number of callbacks which are executed whenever
32//! there is work to do with the connection.
33//!
34//! The primary entrypoints to set up a connection are [`listen`][listen] for
35//! servers and [`connect`] for clients.
36//! [`listen_with_capabilities`](crate::listen_with_capabilities)
37//! and [`connect_with_config`] exist for scenarios that require more in-depth
38//! configuration. Lastly, the [`raw`] submodule allows users to take full
39//! control over connection creation and its ingress path.
40//!
41//! # QUIC Connection Internals
42//!
43//! ![QUIC Worker Setup](https://github.com/cloudflare/quiche/blob/master/tokio-quiche/docs/worker.png?raw=true)
44//!
45//! *Note: Internal details are subject to change between minor versions.*
46//!
47//! tokio-quiche conceptually separates a network socket into a `recv` half and
48//! a `send` half. The `recv` half can only sensibly be used by one async task
49//! at a time, while many tasks can `send` packets on the socket concurrently.
50//! Thus, we spawn a dedicated `InboundPacketRouter` task for each socket which
51//! becomes the sole owner of the socket's `recv` half. It decodes the QUIC
52//! header in each packet, looks up the destination connection ID (DCID), and
53//! forwards the packet to the connection's `IoWorker` task.
54//!
55//! If the packet initiates a new connection, it is passed to an
56//! `InitialPacketHandler` with logic for either the client- or server-side
57//! connection setup. The purple `ConnectionAcceptor` depicted above is the
58//! server-side implementation. It optionally validates the client's IP
59//! address with a `RETRY` packet before packaging the nascent connection into
60//! an [`InitialQuicConnection`][iqc] and sending it to the
61//! [`QuicConnectionStream`] returned by [`listen`][listen].
62//!
63//! At this point the caller of [`listen`][listen] has control of the
64//! [`InitialQuicConnection`][iqc] (`IQC`). Now an `IoWorker` task needs to be
65//! spawned to continue driving the connection. This is possible with
66//! `IQC::handshake` or `IQC::start` (see the [`InitialQuicConnection`][iqc]
67//! docs). Client-side connections use the same infrastructure (except for the
68//! `InitialPacketHandler`), but [`connect`] immediately consumes the
69//! [`QuicConnectionStream`] and calls `IQC::start`.
70//!
71//! `IoWorker` is responsible for feeding inbound packets into the underlying
72//! [`quiche::Connection`], executing the [`ApplicationOverQuic`] callbacks, and
73//! flushing outbound packets to the network via the socket's shared `send`
74//! half. It loops through these operations in the order shown above, yielding
75//! only when sending packets and on `wait_for_data` calls. New inbound packets
76//! or a timeout can also restart the loop while `wait_for_data` is pending.
77//! This continues until the connection is closed or the [`ApplicationOverQuic`]
78//! returns an error.
79//!
80//! [listen]: crate::listen
81//! [iqc]: crate::InitialQuicConnection
82
83use std::sync::Arc;
84use std::time::Duration;
85
86use datagram_socket::DatagramSocketRecv;
87use datagram_socket::DatagramSocketSend;
88use foundations::telemetry::log;
89use qlog::writer::make_qlog_writer_from_path;
90use qlog::writer::qlog_file_name;
91
92use crate::http3::settings::Http3Settings;
93use crate::metrics::DefaultMetrics;
94use crate::metrics::Metrics;
95use crate::settings::Config;
96use crate::socket::QuicListener;
97use crate::socket::Socket;
98use crate::ClientH3Controller;
99use crate::ClientH3Driver;
100use crate::ConnectionParams;
101use crate::QuicConnectionStream;
102use crate::QuicResult;
103use crate::QuicResultExt;
104
105mod addr_validation_token;
106pub(crate) mod connection;
107mod hooks;
108mod io;
109pub mod raw;
110mod router;
111
112use self::connection::ApplicationOverQuic;
113use self::connection::ConnectionIdGenerator as _;
114use self::connection::QuicConnection;
115use self::router::acceptor::ConnectionAcceptor;
116use self::router::acceptor::ConnectionAcceptorConfig;
117use self::router::connector::ClientConnector;
118use self::router::InboundPacketRouter;
119
120pub use self::connection::ConnectionShutdownBehaviour;
121pub use self::connection::HandshakeError;
122pub use self::connection::HandshakeInfo;
123pub use self::connection::Incoming;
124pub use self::connection::QuicCommand;
125pub use self::connection::QuicConnectionStats;
126pub use self::connection::SimpleConnectionIdGenerator;
127pub use self::hooks::ConnectionHook;
128
129/// Alias of [quiche::Connection] used internally by the crate.
130pub type QuicheConnection = quiche::Connection<crate::buf_factory::BufFactory>;
131
132/// Connects to an HTTP/3 server using `socket` and the default client
133/// configuration.
134///
135/// This function always uses the [`ApplicationOverQuic`] provided in
136/// [`http3::driver`](crate::http3::driver) and returns a corresponding
137/// [ClientH3Controller]. To specify a different implementation or customize the
138/// configuration, use [`connect_with_config`].
139///
140/// # Note
141/// tokio-quiche currently only supports one client connection per socket.
142/// Sharing a socket among multiple connections will lead to lost packets as
143/// both connections try to read from the shared socket.
144pub async fn connect<Tx, Rx, S>(
145    socket: S, host: Option<&str>,
146) -> QuicResult<(QuicConnection, ClientH3Controller)>
147where
148    Tx: DatagramSocketSend + Send + 'static,
149    Rx: DatagramSocketRecv + Unpin + 'static,
150    S: TryInto<Socket<Tx, Rx>>,
151    S::Error: std::error::Error + Send + Sync + 'static,
152{
153    // Don't apply_max_capabilities(): some NICs don't support GSO
154    let socket: Socket<Tx, Rx> = socket.try_into()?;
155
156    let (h3_driver, h3_controller) =
157        ClientH3Driver::new(Http3Settings::default());
158    let mut params = ConnectionParams::default();
159    params.settings.max_idle_timeout = Some(Duration::from_secs(30));
160
161    Ok((
162        connect_with_config(socket, host, &params, h3_driver).await?,
163        h3_controller,
164    ))
165}
166
167/// Connects to a QUIC server using `socket` and the provided
168/// [`ApplicationOverQuic`].
169///
170/// When the future resolves, the connection has completed its handshake and
171/// `app` is running in the worker task. In case the handshake failed, we close
172/// the connection automatically and the future will resolve with an error.
173///
174/// # Note
175/// tokio-quiche currently only supports one client connection per socket.
176/// Sharing a socket among multiple connections will lead to lost packets as
177/// both connections try to read from the shared socket.
178pub async fn connect_with_config<Tx, Rx, App>(
179    socket: Socket<Tx, Rx>, host: Option<&str>, params: &ConnectionParams<'_>,
180    app: App,
181) -> QuicResult<QuicConnection>
182where
183    Tx: DatagramSocketSend + Send + 'static,
184    Rx: DatagramSocketRecv + Unpin + 'static,
185    App: ApplicationOverQuic,
186{
187    let mut client_config = Config::new(params, socket.capabilities)?;
188    let scid = SimpleConnectionIdGenerator.new_connection_id();
189
190    #[cfg(feature = "custom-client-dcid")]
191    let mut quiche_conn = if let Some(dcid) = &params.dcid {
192        quiche::connect_with_dcid_and_buffer_factory(
193            host,
194            &scid,
195            dcid,
196            socket.local_addr,
197            socket.peer_addr,
198            client_config.as_mut(),
199        )?
200    } else {
201        quiche::connect_with_buffer_factory(
202            host,
203            &scid,
204            socket.local_addr,
205            socket.peer_addr,
206            client_config.as_mut(),
207        )?
208    };
209
210    #[cfg(not(feature = "custom-client-dcid"))]
211    let mut quiche_conn = quiche::connect_with_buffer_factory(
212        host,
213        &scid,
214        socket.local_addr,
215        socket.peer_addr,
216        client_config.as_mut(),
217    )?;
218
219    #[cfg(feature = "custom-client-dcid")]
220    log::info!("created unestablished quiche::Connection"; "scid" => ?scid, "provided_dcid" => ?params.dcid);
221    #[cfg(not(feature = "custom-client-dcid"))]
222    log::info!("created unestablished quiche::Connection"; "scid" => ?scid);
223
224    if let Some(session) = &params.session {
225        quiche_conn.set_session(session).map_err(|error| {
226            log::error!("application provided an invalid session"; "error"=>?error);
227            quiche::Error::CryptoFail
228        })?;
229    }
230
231    // Set the qlog writer here instead of in the `ClientConnector` to avoid
232    // missing logs from early in the connection
233    if let Some(qlog_dir) = &client_config.qlog_dir {
234        log::info!("setting up qlogs"; "qlog_dir"=>qlog_dir);
235        let id = format!("{:?}", &scid);
236        let path = std::path::Path::new(qlog_dir)
237            .join(qlog_file_name(&id, client_config.qlog_compression));
238        if let Ok(writer) =
239            make_qlog_writer_from_path(&path, client_config.qlog_compression)
240        {
241            quiche_conn.set_qlog(
242                writer,
243                "tokio-quiche qlog".to_string(),
244                format!("tokio-quiche qlog id={id}"),
245            );
246        }
247    }
248
249    // Set the keylog file here for the same reason
250    if let Some(keylog_file) = &client_config.keylog_file {
251        log::info!("setting up keylog file");
252        if let Ok(keylog_clone) = keylog_file.try_clone() {
253            quiche_conn.set_keylog(Box::new(keylog_clone));
254        }
255    }
256
257    let socket_tx = Arc::new(socket.send);
258    let socket_rx = socket.recv;
259
260    let (router, mut quic_connection_stream) = InboundPacketRouter::new(
261        client_config,
262        Arc::clone(&socket_tx),
263        socket_rx,
264        socket.local_addr,
265        ClientConnector::new(socket_tx, quiche_conn),
266        DefaultMetrics,
267    );
268
269    // drive the packet router:
270    tokio::spawn(async move {
271        match router.await {
272            Ok(()) => log::debug!("incoming packet router finished"),
273            Err(error) => {
274                log::error!("incoming packet router failed"; "error"=>error)
275            },
276        }
277    });
278
279    Ok(quic_connection_stream
280        .recv()
281        .await
282        .ok_or("unable to establish connection")??
283        .start(app))
284}
285
286pub(crate) fn start_listener<M>(
287    socket: QuicListener, params: &ConnectionParams, metrics: M,
288) -> std::io::Result<QuicConnectionStream<M>>
289where
290    M: Metrics,
291{
292    #[cfg(unix)]
293    assert!(
294        datagram_socket::is_nonblocking(&socket).unwrap_or_default(),
295        "O_NONBLOCK should be set for the listening socket"
296    );
297
298    let config = Config::new(params, socket.capabilities).into_io()?;
299
300    let local_addr = socket.socket.local_addr()?;
301    let socket_tx = Arc::new(socket.socket);
302    let socket_rx = Arc::clone(&socket_tx);
303
304    let acceptor = ConnectionAcceptor::new(
305        ConnectionAcceptorConfig {
306            disable_client_ip_validation: config.disable_client_ip_validation,
307            qlog_dir: config.qlog_dir.clone(),
308            qlog_compression: config.qlog_compression,
309            keylog_file: config
310                .keylog_file
311                .as_ref()
312                .and_then(|f| f.try_clone().ok()),
313            #[cfg(target_os = "linux")]
314            with_pktinfo: if local_addr.is_ipv4() {
315                config.has_ippktinfo
316            } else {
317                config.has_ipv6pktinfo
318            },
319        },
320        Arc::clone(&socket_tx),
321        Default::default(),
322        socket.cid_generator,
323        metrics.clone(),
324    );
325
326    let (socket_driver, accept_stream) = InboundPacketRouter::new(
327        config,
328        socket_tx,
329        socket_rx,
330        local_addr,
331        acceptor,
332        metrics.clone(),
333    );
334
335    crate::metrics::tokio_task::spawn("quic_udp_listener", metrics, async move {
336        match socket_driver.await {
337            Ok(()) => log::trace!("incoming packet router finished"),
338            Err(error) => {
339                log::error!("incoming packet router failed"; "error"=>error)
340            },
341        }
342    });
343    Ok(QuicConnectionStream::new(accept_stream))
344}