Skip to content

Commit

Permalink
Enable performance cores on Apple
Browse files Browse the repository at this point in the history
  • Loading branch information
matislovas committed Jul 10, 2024
1 parent a933d89 commit 8dd7786
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions boringtun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ thiserror = "1.0"
[target.'cfg(target_os="macos")'.dependencies]
nix = "0.24.1"

[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))'.dependencies]
dispatch = { git = "https://github.com/NordSecurity/rust-dispatch.git", rev = "13447cd7221a74ebcce1277ae0cfc9a421a28ec5" }

[dev-dependencies]
tracing-subscriber = "0.3"
criterion = { version = "0.3.5", features = ["html_reports"] }
Expand Down
53 changes: 42 additions & 11 deletions boringtun/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
use std::thread::JoinHandle;

use crate::noise::errors::WireGuardError;
use crate::noise::handshake::parse_handshake_anon;
Expand Down Expand Up @@ -115,7 +114,10 @@ impl MakeExternalBoringtun for MakeExternalBoringtunNoop {

pub struct DeviceHandle {
pub device: Arc<Lock<Device>>, // The interface this handle owns
threads: Vec<JoinHandle<()>>,
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
threads: Vec<thread::JoinHandle<()>>,
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
threads: (dispatch::Group, Vec<dispatch::Queue>),
}

#[derive(Clone)]
Expand Down Expand Up @@ -183,19 +185,42 @@ impl DeviceHandle {

pub fn new_with_tun(tun: TunSocket, config: DeviceConfig) -> Result<DeviceHandle, Error> {
let n_threads = config.n_threads;

let mut wg_interface = Device::new_with_tun(tun, config)?;
wg_interface.open_listen_socket(0)?; // Start listening on a random port

let interface_lock = Arc::new(Lock::new(wg_interface));

let mut threads = vec![];
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
let threads = {
let group = dispatch::Group::create();
let mut queues = vec![];
for i in 0..n_threads {
queues.push({
let dev = Arc::clone(&interface_lock);
let group_clone = group.clone();
let queue = dispatch::Queue::global(dispatch::QueuePriority::High);
queue.exec_async(move || {
group_clone.enter();
DeviceHandle::event_loop(i, &dev)
});
queue
});
}
(group, queues)
};

for i in 0..n_threads {
threads.push({
let dev = Arc::clone(&interface_lock);
thread::spawn(move || DeviceHandle::event_loop(i, &dev))
});
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
let threads = {
let mut threads = vec![];
for i in 0..n_threads {
threads.push({
let dev = Arc::clone(&interface_lock);
thread::spawn(move || DeviceHandle::event_loop(i, &dev))
});
}
threads
};

Ok(DeviceHandle {
device: interface_lock,
Expand All @@ -218,12 +243,18 @@ impl DeviceHandle {
self.device.read().drop_connected_sockets();
}

#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
pub fn wait(&mut self) {
while let Some(thread) = self.threads.pop() {
thread.join().unwrap();
}
}

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
pub fn wait(&mut self) {
self.threads.0.wait();
}

pub fn clean(&mut self) {
for path in &self.device.read().cleanup_paths {
// attempt to remove any file we created in the work dir
Expand Down Expand Up @@ -300,12 +331,12 @@ impl DeviceHandle {
}
}

fn new_thread_local(thread_id: usize, device_lock: &LockReadGuard<Device>) -> ThreadData {
fn new_thread_local(_thread_id: usize, device_lock: &LockReadGuard<Device>) -> ThreadData {
#[cfg(target_os = "linux")]
let t_local = ThreadData {
src_buf: [0u8; MAX_UDP_SIZE],
dst_buf: [0u8; MAX_UDP_SIZE],
iface: if thread_id == 0 || !device_lock.config.use_multi_queue {
iface: if _thread_id == 0 || !device_lock.config.use_multi_queue {
// For the first thread use the original iface
Arc::clone(&device_lock.iface)
} else {
Expand Down

0 comments on commit 8dd7786

Please sign in to comment.