mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-03-22 06:41:51 +00:00
Currently, `connlib` is entirely single-threaded. This allows us to reuse a single buffer for processing IP packets and makes reasoning of the packet processing code very simple. Being single-threaded also means we can only make use of a single CPU core and all operations have to be sequential. Analyzing `connlib` using `perf` shows that we spend 26% of our CPU time writing packets to the TUN interface [0]. Because we are single-threaded, `connlib` cannot do anything else during this time. If we could offload the writing of these packets to a different thread, `connlib` could already process the next packet while the current one is writing. Packets that we send to the TUN interface arrived as an encrypted WG packet over UDP and get decrypted into a - currently - shared buffer. Moving the writing to a different thread implies that we have to have more of these buffer that the next packet(s) can be decrypted into. To avoid IP fragmentation, we set the maximum IP MTU to 1280 bytes on the TUN interface. That actually isn't very big and easily fits into a stackframe. The default stack size for threads is 2MB [1]. Instead of creating more buffers and cycling through them, we can also simply stack-allocate our IP packets. This incurs some overhead from copying packets but it is only ~3.5% [2] (This was measured without a separate thread). With stack-allocated packets, almost all lifetime-annotations go away which in itself is already a welcome ergonomics boost. Stack-allocated packets also means we can simply spawn a new thread for the packet processing. This thread is connected with two channel to connlib's main thread. The capacity of 1000 packets will at most consume an additional 3.5 MB of memory which is fine even on our most-constrained devices such as iOS. [0]: https://share.firefox.dev/3z78CzD [1]: https://doc.rust-lang.org/std/thread/#stack-size [2]: https://share.firefox.dev/3Bf4zla Resolves: #6653. Resolves: #5541.
92 lines
2.7 KiB
Rust
92 lines
2.7 KiB
Rust
//! This crates contains shared types and behavior between all the other libraries.
|
|
//!
|
|
//! This includes types provided by external crates, i.e. [boringtun] to make sure that
|
|
//! we are using the same version across our own crates.
|
|
|
|
pub mod callbacks;
|
|
pub mod messages;
|
|
|
|
pub use boringtun::x25519::PublicKey;
|
|
pub use boringtun::x25519::StaticSecret;
|
|
pub use phoenix_channel::{LoginUrl, LoginUrlError};
|
|
|
|
pub type DomainName = domain::base::Name<Vec<u8>>;
|
|
|
|
const LIB_NAME: &str = "connlib";
|
|
|
|
pub fn get_user_agent(os_version_override: Option<String>, app_version: &str) -> String {
|
|
// Note: we could switch to sys-info and get the hostname
|
|
// but we lose the arch
|
|
// and neither of the libraries provide the kernel version.
|
|
// so I rather keep os_info which seems like the most popular
|
|
// and keep implementing things that we are missing on top
|
|
let info = os_info::get();
|
|
|
|
// iOS returns "Unknown", but we already know we're on iOS here
|
|
#[cfg(target_os = "ios")]
|
|
let os_type = "iOS";
|
|
#[cfg(not(target_os = "ios"))]
|
|
let os_type = info.os_type();
|
|
|
|
let os_version = os_version_override.unwrap_or(info.version().to_string());
|
|
let additional_info = additional_info();
|
|
let lib_name = LIB_NAME;
|
|
format!("{os_type}/{os_version} {lib_name}/{app_version}{additional_info}")
|
|
}
|
|
|
|
fn additional_info() -> String {
|
|
let info = os_info::get();
|
|
match (info.architecture(), kernel_version()) {
|
|
(None, None) => "".to_string(),
|
|
(None, Some(k)) => format!(" ({k})"),
|
|
(Some(a), None) => format!(" ({a})"),
|
|
(Some(a), Some(k)) => format!(" ({a}; {k})"),
|
|
}
|
|
}
|
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
fn kernel_version() -> Option<String> {
|
|
None
|
|
}
|
|
|
|
#[cfg(target_family = "unix")]
|
|
fn kernel_version() -> Option<String> {
|
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
|
let mut utsname = libc::utsname {
|
|
sysname: [0; 65],
|
|
nodename: [0; 65],
|
|
release: [0; 65],
|
|
version: [0; 65],
|
|
machine: [0; 65],
|
|
domainname: [0; 65],
|
|
};
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
|
let mut utsname = libc::utsname {
|
|
sysname: [0; 256],
|
|
nodename: [0; 256],
|
|
release: [0; 256],
|
|
version: [0; 256],
|
|
machine: [0; 256],
|
|
};
|
|
|
|
// SAFETY: we just allocated the pointer
|
|
if unsafe { libc::uname(&mut utsname as *mut _) } != 0 {
|
|
return None;
|
|
}
|
|
|
|
#[cfg_attr(
|
|
all(target_os = "linux", target_arch = "aarch64"),
|
|
allow(clippy::unnecessary_cast)
|
|
)]
|
|
let version: Vec<u8> = utsname
|
|
.release
|
|
.split(|c| *c == 0)
|
|
.next()?
|
|
.iter()
|
|
.map(|x| *x as u8)
|
|
.collect();
|
|
|
|
String::from_utf8(version).ok()
|
|
}
|