Skip to content

Commit

Permalink
perf: enhance WireGuard performance (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op authored Sep 22, 2024
2 parents 16e1c7c + 90264cf commit 2ce3d60
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 132 deletions.
125 changes: 23 additions & 102 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ debug = true

[patch.crates-io]
rustls = { git = "https://github.com/XOR-op/rustls.delta.git", branch = "unofficial-rel-0.23" }
smoltcp = { git = "https://github.com/XOR-op/smoltcp.git", branch = "resize-recv-buffer" }
smoltcp = { git = "https://github.com/XOR-op/smoltcp.git", branch = "rcv-buf-pinned" }
# Only used to bump x25519-dalek; will be removed once 0.6.1 is released
boringtun = { git = "https://github.com/XOR-op/boringtun", branch = "master"}
3 changes: 2 additions & 1 deletion boltconn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ network-interface = "2.0.0"
nix = { version = "0.29.0", features = ["user", "fs"] }
rand = { version = "0.8.5", features = ["small_rng"] }
regex = "1.7.0"
sharded-slab = "0.1.7"
socket2 = { version = "0.5.1", features = ["all"] }
thiserror = "1.0.37"
tokio = { version = "1.32.0", features = [
tokio = { version = "1.40.0", features = [
"rt",
"rt-multi-thread",
"net",
Expand Down
10 changes: 0 additions & 10 deletions boltconn/src/common/id_gen.rs

This file was deleted.

3 changes: 2 additions & 1 deletion boltconn/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub mod client_hello;
pub mod duplex_chan;
pub mod evictable_vec;
pub mod host_matcher;
pub mod id_gen;
mod sync;
pub mod utils;

pub use sync::{local_async_run, AbortCanary};

Expand All @@ -45,6 +45,7 @@ impl StreamOutboundTrait for tokio::net::windows::named_pipe::NamedPipeServer {}
impl StreamOutboundTrait for tokio::net::UnixStream {}

pub const MAX_PKT_SIZE: usize = 65576;
pub const MAX_UDP_PKT_SIZE: usize = 1518;

pub async fn read_to_bytes_mut(
buf: &mut BytesMut,
Expand Down
53 changes: 53 additions & 0 deletions boltconn/src/common/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

#[derive(Default, Debug)]
pub struct IdGenerator(AtomicU64);

impl IdGenerator {
pub fn get(&self) -> u64 {
self.0.fetch_add(1, Ordering::Relaxed)
}
}

#[allow(unused)]
pub struct TputMeasurement {
inner: std::sync::Mutex<TputMeasurementInner>,
}

struct TputMeasurementInner {
pub accum_bytes: u64,
pub last_time: Instant,
pub interval: Duration,
}

impl TputMeasurement {
pub fn new(interval: Duration) -> Self {
Self {
inner: std::sync::Mutex::new(TputMeasurementInner {
accum_bytes: 0,
last_time: Instant::now(),
interval,
}),
}
}

pub fn update(&self, bytes: usize) -> Option<f64> {
let mut inner = self.inner.lock().unwrap();
inner.accum_bytes += bytes as u64;
let now = Instant::now();
let elapsed = now - inner.last_time;
if elapsed >= inner.interval {
let tput = inner.accum_bytes as f64 / elapsed.as_secs_f64();
inner.accum_bytes = 0;
inner.last_time = now;
Some(tput)
} else {
None
}
}

pub fn update_to_mbps(&self, bytes: usize) -> Option<f64> {
self.update(bytes).map(|tput| tput * 8.0 / 1_000_000.0)
}
}
Loading

0 comments on commit 2ce3d60

Please sign in to comment.