s2n-quic-dc 0.80.0

Internal crate used by s2n-quic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//! Stream server supporting dcQUIC streams over UDP, TCP, including forwarding over UDS.

use crate::{
    event,
    path::secret,
    stream::{
        application::Builder as StreamBuilder,
        environment::{
            tokio::{self as env, Environment},
            udp as udp_pool, Environment as _,
        },
        runtime::tokio as runtime,
        server::{accept, stats},
        socket,
    },
    sync::mpmc,
};
use core::num::{NonZeroU16, NonZeroUsize};
use s2n_quic_core::ensure;
use std::{io, net::SocketAddr, time::Duration};
use tokio::io::unix::AsyncFd;
use tracing::Instrument as _;

// The kernel contends on fdtable lock when calling accept to locate free file
// descriptors, so even if we don't contend on the underlying socket (due to
// REUSEPORT) it still ends up expensive to have large amounts of threads trying to
// accept() within a single process. Clamp concurrency to at most 4 threads
// executing the TCP acceptor tasks accordingly.
//
// With UDP there's ~no lock contention for receiving packets on separate UDP sockets,
// so we don't clamp concurrency in that case.
pub const MAX_TCP_WORKERS: usize = 4;

pub mod tcp;
pub mod udp;
pub mod uds;

// This trait is a solution to abstract local_addr and map methods
pub trait Handshake: Clone {
    fn local_addr(&self) -> SocketAddr;

    fn map(&self) -> &secret::Map;
}

impl Handshake for crate::psk::server::Provider {
    fn local_addr(&self) -> SocketAddr {
        self.local_addr()
    }

    fn map(&self) -> &secret::Map {
        self.map()
    }
}

#[derive(Clone)]
pub struct Server<H: Handshake + Clone, S: event::Subscriber + Clone> {
    streams: accept::Receiver<S>,
    local_addr: SocketAddr,
    handshake: H,
    stats: stats::Sender,
    /// This field retains a reference to the runtime being used
    #[allow(dead_code)]
    env: Environment<S>,
    #[allow(dead_code)]
    acceptor_rt: runtime::Shared<S>,
    tls: Option<tcp::tls::TlsServer<S>>,
}

impl<H: Handshake + Clone, S: event::Subscriber + Clone> Server<H, S> {
    #[inline]
    pub fn new(acceptor_addr: SocketAddr, handshake: H, subscriber: S) -> io::Result<Self> {
        Builder::default()
            .with_address(acceptor_addr)
            .build(handshake, subscriber)
    }

    pub fn builder() -> Builder {
        Builder::default()
    }

    pub fn drop_state(&self) {
        self.handshake.map().drop_state()
    }

    pub fn handshake_state(&self) -> &H {
        &self.handshake
    }

    /// Should generally only be used for advanced users.
    ///
    /// This should not be used for spawning heavy-weight work (e.g., request processing), and is
    /// generally best used for tiny tasks which intermediate to some other runtime. For example,
    /// it can work well for having some small processing to then send into another channel.
    pub fn acceptor_rt(&self) -> tokio::runtime::Handle {
        (*self.acceptor_rt).clone()
    }

    #[inline]
    pub async fn accept(&self) -> io::Result<(crate::stream::application::Stream<S>, SocketAddr)> {
        let (stream, _info, addr) = accept::accept(&self.streams, &self.stats).await?;
        Ok((stream, addr))
    }

    #[inline]
    pub async fn accept_with_info(
        &self,
    ) -> io::Result<(
        crate::stream::application::Stream<S>,
        crate::stream::application::AcceptInfo,
        SocketAddr,
    )> {
        accept::accept(&self.streams, &self.stats).await
    }

    #[inline]
    pub fn acceptor_addr(&self) -> io::Result<SocketAddr> {
        Ok(self.local_addr)
    }

    #[inline]
    pub fn handshake_addr(&self) -> io::Result<SocketAddr> {
        Ok(self.handshake.local_addr())
    }
}

/// Default to the SOMAXCONN, similar to rust:
/// https://github.com/rust-lang/rust/blob/28a58f2fa7f0c46b8fab8237c02471a915924fe5/library/std/src/os/unix/net/listener.rs#L104
pub const DEFAULT_BACKLOG: u16 = libc::SOMAXCONN as _;

pub struct Builder {
    backlog: Option<NonZeroU16>,
    workers: Option<usize>,
    acceptor_addr: SocketAddr,
    span: Option<tracing::Span>,
    enable_udp: bool,
    enable_tcp: bool,
    accept_flavor: accept::Flavor,
    linger: Option<Duration>,
    send_buffer: Option<usize>,
    recv_buffer: Option<usize>,
    reuse_addr: Option<bool>,
    tls: Option<tcp::tls::Builder>,
}

impl Default for Builder {
    fn default() -> Self {
        Self {
            backlog: None,
            workers: None,
            // FIXME: Don't default to a fixed port?
            acceptor_addr: "[::]:4444".parse().unwrap(),
            span: None,
            enable_udp: true,
            enable_tcp: false,
            linger: None,
            accept_flavor: Default::default(),
            send_buffer: None,
            recv_buffer: None,
            reuse_addr: None,
            tls: None,
        }
    }
}

macro_rules! common_builder_methods {
    () => {
        pub fn with_protocol(mut self, protocol: socket::Protocol) -> Self {
            match protocol {
                socket::Protocol::Udp => {
                    self.enable_udp = true;
                    self.enable_tcp = false
                }
                socket::Protocol::Tcp => {
                    self.enable_udp = false;
                    self.enable_tcp = true;
                }
                _ => {
                    self.enable_udp = false;
                    self.enable_tcp = false;
                }
            }
            self
        }

        pub fn with_udp(mut self, enabled: bool) -> Self {
            self.enable_udp = enabled;
            self
        }

        pub fn with_tcp(mut self, enabled: bool) -> Self {
            self.enable_tcp = enabled;
            self
        }
    };
}
macro_rules! manager_builder_methods {
    () => {
        pub fn with_address(mut self, addr: SocketAddr) -> Self {
            self.acceptor_addr = addr;
            self
        }

        pub fn with_backlog(mut self, backlog: NonZeroU16) -> Self {
            self.backlog = Some(backlog);
            self
        }

        pub fn with_workers(mut self, workers: NonZeroUsize) -> Self {
            self.workers = Some(workers.into());
            self
        }

        pub fn with_linger(mut self, linger: Duration) -> Self {
            self.linger = Some(linger);
            self
        }

        pub fn with_send_buffer(mut self, bytes: usize) -> Self {
            self.send_buffer = Some(bytes);
            self
        }

        pub fn with_recv_buffer(mut self, bytes: usize) -> Self {
            self.recv_buffer = Some(bytes);
            self
        }

        /// Sets the reuse address option for the OS socket handle.
        ///
        /// This allows the application to bind to a previously used local address.
        /// In TCP, this can be useful when a closed socket is in the `TIME_WAIT` state and the application
        /// would like to reuse that address immediately.
        /// On Linux packets are routed to the most recently bound socket.
        ///
        /// See `SO_REUSEADDR` for more information.
        pub fn with_reuse_addr(mut self, enabled: bool) -> Self {
            self.reuse_addr = Some(enabled);
            self
        }
    };
}

impl Builder {
    common_builder_methods!();
    manager_builder_methods!();

    pub fn with_tls(mut self, tls: tcp::tls::Builder) -> Self {
        self.tls = Some(tls);
        self
    }

    pub fn build<H: Handshake + Clone, S: event::Subscriber + Clone>(
        mut self,
        handshake: H,
        subscriber: S,
    ) -> io::Result<Server<H, S>> {
        ensure!(
            self.enable_udp || self.enable_tcp,
            Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "at least one acceptor type needs to be enabled"
            ))
        );

        let concurrency: usize = self.workers.unwrap_or_else(|| {
            std::thread::available_parallelism()
                .unwrap_or_else(|_| 1.try_into().unwrap())
                .into()
        });

        let backlog: usize = self.backlog.map(NonZeroU16::get).unwrap_or(DEFAULT_BACKLOG) as usize;
        let (stream_sender, stream_receiver) = mpmc::new::<StreamBuilder<S>>(backlog);

        let mut env = env::Builder::new(subscriber)
            .with_threads(concurrency)
            .with_acceptor(stream_sender.clone());

        let enable_udp_pool = true;

        if self.enable_udp && enable_udp_pool {
            // configure the socket options with the specified address
            let mut options = socket::Options::new(self.acceptor_addr);

            options.send_buffer = self.send_buffer;
            options.recv_buffer = self.recv_buffer;

            env = env.with_socket_options(options);

            let mut pool = udp_pool::Config::new(handshake.map().clone());

            pool.reuse_port = concurrency > 1;
            pool.accept_flavor = self.accept_flavor;

            env = env.with_pool(pool);
        }

        let env = env.build()?;

        if self.enable_udp && enable_udp_pool {
            // update the address with the selected port
            self.acceptor_addr = env.pool_addr().unwrap();
            // don't use the owned socket acceptor
            self.enable_udp = false;
        }

        // TODO is it better to spawn one current_thread runtime per concurrency?
        let acceptor_rt: runtime::Shared<S> = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .thread_name("acceptor")
            .worker_threads(concurrency)
            .build()?
            .into();

        let mut span = self.span.unwrap_or_else(tracing::span::Span::current);

        if span.is_none() {
            span = tracing::debug_span!("server");
        }

        let (stats_sender, stats_worker, stats) = stats::channel();

        acceptor_rt.spawn(stats_worker.run(env.clock().clone()));

        // Spawn the queue pruner task
        if matches!(self.accept_flavor, accept::Flavor::Lifo) {
            let env = env.clone();
            let channel = stream_receiver.downgrade();
            let stats = stats.clone();

            acceptor_rt.spawn(accept::Pruner::default().run(env, channel, stats));
        }

        let mut server = Server {
            tls: self.tls.map(|t| {
                t.build(
                    stream_sender.clone(),
                    env.clone(),
                    handshake.map().clone(),
                    self.accept_flavor,
                )
            }),
            streams: stream_receiver,
            local_addr: self.acceptor_addr,
            handshake,
            stats: stats_sender,
            env,
            acceptor_rt,
        };

        // split the backlog between all of the workers
        //
        // this is only used in TCP, so clamp division to maximum TCP worker concurrency
        let backlog = backlog
            .div_ceil(concurrency.clamp(0, MAX_TCP_WORKERS))
            .max(1);

        Start {
            enable_tcp: self.enable_tcp,
            enable_udp: self.enable_udp,
            accept_flavor: self.accept_flavor,
            linger: self.linger,
            backlog,
            concurrency,
            server: &mut server,
            stream_sender,
            span,
            next_id: 0,
            send_buffer: self.send_buffer,
            recv_buffer: self.recv_buffer,
            reuse_addr: self.reuse_addr.unwrap_or(false),
        }
        .start()?;

        Ok(server)
    }
}

struct Start<'a, H: Handshake + Clone, S: event::Subscriber + Clone> {
    enable_tcp: bool,
    enable_udp: bool,
    accept_flavor: accept::Flavor,
    backlog: usize,
    concurrency: usize,
    server: &'a mut Server<H, S>,
    stream_sender: accept::Sender<S>,
    span: tracing::Span,
    next_id: usize,
    linger: Option<Duration>,
    send_buffer: Option<usize>,
    recv_buffer: Option<usize>,
    reuse_addr: bool,
}

impl<H: Handshake + Clone, S: event::Subscriber + Clone> Start<'_, H, S> {
    #[inline]
    fn start(&mut self) -> io::Result<()> {
        let _acceptor = self.server.acceptor_rt.enter();

        // check if we need to find a port for which both types are free
        if self.enable_tcp && self.enable_udp && self.server.local_addr.port() == 0 {
            // find a port and spawn the initial listeners
            self.spawn_initial_wildcard_pair()?;
            // spawn the rest of the concurrency
            self.spawn_count(self.concurrency - 1, 1)?;
        } else {
            // otherwise spawn things as normal
            self.spawn_count(self.concurrency, 0)?;
        }

        debug_assert_ne!(
            self.server.local_addr.port(),
            0,
            "a port should be selected"
        );

        Ok(())
    }

    #[inline]
    fn spawn_initial_wildcard_pair(&mut self) -> io::Result<()> {
        debug_assert!(self.enable_tcp);
        debug_assert!(self.enable_udp);

        let (local_addr, udp_socket, tcp_socket) =
            super::spawn_initial_wildcard_pair(self.server.local_addr, |addr| {
                self.socket_opts(addr)
            })?;
        self.server.local_addr = local_addr;
        self.spawn_udp(udp_socket)?;
        self.spawn_tcp(tcp_socket)?;
        Ok(())
    }

    #[inline]
    fn spawn_count(&mut self, count: usize, already_running: usize) -> io::Result<()> {
        for protocol in [socket::Protocol::Udp, socket::Protocol::Tcp] {
            match protocol {
                socket::Protocol::Udp => ensure!(self.enable_udp, continue),
                socket::Protocol::Tcp => ensure!(self.enable_tcp, continue),
                _ => continue,
            }

            for idx in 0..count {
                match protocol {
                    socket::Protocol::Udp => {
                        let socket = self.socket_opts(self.server.local_addr).build_udp()?;
                        self.spawn_udp(socket)?;
                    }
                    socket::Protocol::Tcp => {
                        if idx + already_running >= MAX_TCP_WORKERS {
                            continue;
                        }

                        let socket = self
                            .socket_opts(self.server.local_addr)
                            .build_tcp_listener()?;
                        self.spawn_tcp(socket)?;
                    }
                    _ => continue,
                }
            }
        }

        Ok(())
    }

    #[inline]
    fn socket_opts(&self, local_addr: SocketAddr) -> socket::Options {
        let mut options = socket::Options::new(local_addr);

        // Explicitly do **not** set the socket backlog to self.backlog. While we split the
        // configured backlog amongst our in-process queues as concurrency increases, it doesn't
        // make sense to shrink the kernel backlogs -- that just causes packet drops and generally
        // bad behavior.
        //
        // This is especially true for TCP where we don't have workers matching concurrency.
        options.send_buffer = self.send_buffer;
        options.recv_buffer = self.recv_buffer;
        options.reuse_address = self.reuse_addr;

        // if we have more than one thread then we'll need to use reuse port
        if self.concurrency > 1 {
            // if the application is wanting to bind to a random port then we need to set
            // reuse_port after
            if local_addr.port() == 0 {
                options.reuse_port = socket::ReusePort::AfterBind;
            } else {
                options.reuse_port = socket::ReusePort::BeforeBind;
            }
        }

        options
    }

    #[inline]
    fn spawn_udp(&mut self, socket: std::net::UdpSocket) -> io::Result<()> {
        // if this is the first socket being spawned then update the local address
        if self.server.local_addr.port() == 0 {
            self.server.local_addr = socket.local_addr()?;
        }

        let socket = AsyncFd::new(socket)?;
        let id = self.id();

        let acceptor = udp::Acceptor::new(
            id,
            socket,
            &self.stream_sender,
            &self.server.env,
            self.server.handshake.map(),
            self.accept_flavor,
        )
        .run();

        if self.span.is_disabled() {
            self.server.acceptor_rt.spawn(acceptor);
        } else {
            self.server
                .acceptor_rt
                .spawn(acceptor.instrument(self.span.clone()));
        }

        Ok(())
    }

    #[inline]
    fn spawn_tcp(&mut self, socket: std::net::TcpListener) -> io::Result<()> {
        // if this is the first socket being spawned then update the local address
        if self.server.local_addr.port() == 0 {
            self.server.local_addr = socket.local_addr()?;
        }

        let socket = tokio::io::unix::AsyncFd::new(socket)?;
        let id = self.id();
        let channel_behavior =
            tcp::worker::DefaultBehavior::new(&self.stream_sender, self.server.tls.clone());
        let acceptor = tcp::Acceptor::new(
            id,
            socket,
            &self.server.env,
            self.server.handshake.map(),
            self.backlog,
            self.accept_flavor,
            self.linger,
            channel_behavior,
        )?
        .run();

        if self.span.is_disabled() {
            self.server.acceptor_rt.spawn(acceptor);
        } else {
            self.server
                .acceptor_rt
                .spawn(acceptor.instrument(self.span.clone()));
        }

        Ok(())
    }

    fn id(&mut self) -> usize {
        let id = self.next_id;
        self.next_id += 1;
        id
    }
}

pub(crate) use common_builder_methods;
pub(crate) use manager_builder_methods;