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 e79fb28 commit e1ce86f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
7 changes: 7 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 @@ -41,6 +41,9 @@ mock_instant = { version = "0.2", optional = true }
socket2 = { version = "0.5", features = ["all"], optional = true }
thiserror = { version = "1", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))'.dependencies]
dispatch = "0.2.0"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28", default-features = false, features = [
"time",
Expand Down
52 changes: 41 additions & 11 deletions boringtun/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use std::os::unix::io::AsRawFd;
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 @@ -118,7 +117,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 @@ -191,14 +193,36 @@ impl DeviceHandle {

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(0, &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 @@ -221,12 +245,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 @@ -303,12 +333,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 e1ce86f

Please sign in to comment.