aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuanhao Yin <sopium@mysterious.site>2017-03-25 17:39:39 +0800
committerGuanhao Yin <sopium@mysterious.site>2017-03-25 17:39:39 +0800
commitcc1ffa4424107079f8faa928fe8bbc7fec52d309 (patch)
tree0f9234cd21c68b17ddac5eeb79eb05a998076721
parentAdd functions to convert between uapi/libc types and Rust std types (diff)
downloadwireguard-rs-cc1ffa4424107079f8faa928fe8bbc7fec52d309.tar.xz
wireguard-rs-cc1ffa4424107079f8faa928fe8bbc7fec52d309.zip
Use a global timer
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs4
-rw-r--r--src/protocol/controller.rs7
-rw-r--r--src/protocol/timer.rs26
4 files changed, 20 insertions, 18 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 29b6abe..e4106f5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,6 +15,7 @@ byteorder = "1.0.0"
clap = { version = "2", features = ["yaml"] }
daemonize = "0.2"
error-chain = "0.10"
+lazy_static = "0.2"
libc = "0.2"
log = "0.3"
mowl = "1.0"
diff --git a/src/lib.rs b/src/lib.rs
index d18d770..021128c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -26,6 +26,8 @@
extern crate daemonize;
#[macro_use]
+extern crate lazy_static;
+#[macro_use]
extern crate log;
extern crate libc;
#[macro_use]
@@ -38,7 +40,7 @@ mod crypto;
pub mod protocol;
pub mod error;
-mod uapi;
+pub mod uapi;
use error::*;
use uapi::{WgDevice, WgIpMask, WgPeer};
diff --git a/src/protocol/controller.rs b/src/protocol/controller.rs
index d4a0680..8f71519 100644
--- a/src/protocol/controller.rs
+++ b/src/protocol/controller.rs
@@ -76,8 +76,6 @@ pub struct WgState {
// The secret used to calc cookie.
cookie_secret: Mutex<([u8; 32], Instant)>,
-
- timer_controller: TimerController,
}
/// Removes `Id` from `id_map` when dropped.
@@ -553,7 +551,7 @@ fn do_handshake(wg: Arc<WgState>, peer0: SharedPeerState, sock: Arc<UdpSocket>)
})
};
- let resend = wg.timer_controller.register_delay(Duration::from_secs(REKEY_TIMEOUT), resend);
+ let resend = CONTROLLER.register_delay(Duration::from_secs(REKEY_TIMEOUT), resend);
resend.activate();
peer.handshake = Some(Handshake {
@@ -745,7 +743,7 @@ pub fn wg_change_peer<F>(wg: Arc<WgState>, peer_pubkey: &X25519Pubkey, f: F) ->
/// Add a peer to a WG interface.
/// The peer should not already exist.
pub fn wg_add_peer(wg: Arc<WgState>, peer: &PeerInfo, sock: Arc<UdpSocket>) {
- let register = |a| wg.timer_controller.register_delay(Duration::from_secs(0), a);
+ let register = |a| CONTROLLER.register_delay(Duration::from_secs(0), a);
let dummy_action = || Box::new(|| {});
// Lock pubkey_map.
@@ -842,7 +840,6 @@ impl WgState {
rt4: RwLock::new(IpLookupTable::new()),
rt6: RwLock::new(IpLookupTable::new()),
cookie_secret: Mutex::new((cookie, Instant::now())),
- timer_controller: TimerController::new(),
}
}
diff --git a/src/protocol/timer.rs b/src/protocol/timer.rs
index 9ebcdfc..96490d1 100644
--- a/src/protocol/timer.rs
+++ b/src/protocol/timer.rs
@@ -25,6 +25,11 @@ use std::time::{Duration, Instant};
type Action = Box<Fn() + Send + Sync>;
+lazy_static! {
+ /// Global timer controller.
+ pub static ref CONTROLLER: TimerController = TimerController::new();
+}
+
struct Timer {
activated: AtomicBool,
// Actually, this is not used outside the big whole wheel mutex.
@@ -34,7 +39,6 @@ struct Timer {
}
pub struct TimerHandle {
- controller: Arc<Mutex<Wheel>>,
pos: AtomicUsize,
timer: ArcTimer,
}
@@ -45,7 +49,7 @@ const WHEEL_SLOTS: usize = 128;
const TICK_MS: usize = 128;
// This is hashed timing wheel.
-pub struct TimerController(Arc<Mutex<Wheel>>);
+pub struct TimerController(Mutex<Wheel>);
struct Wheel {
// Usually a linked list is used in each slot.
@@ -56,19 +60,18 @@ struct Wheel {
}
impl TimerController {
- pub fn new() -> Self {
- let con = Arc::new(Mutex::new(Wheel {
+ fn new() -> Self {
+ let con = Mutex::new(Wheel {
wheel: ::std::iter::repeat(HashSet::new()).take(WHEEL_SLOTS).collect(),
cur: 0,
- }));
- let con1 = con.clone();
+ });
- thread::Builder::new().name("timer".to_string()).spawn(move || {
+ thread::Builder::new().name("timer".to_string()).spawn(|| {
loop {
let tick_start = Instant::now();
let mut to_run = Vec::new();
- let mut wheel = con.lock().unwrap();
+ let mut wheel = CONTROLLER.0.lock().unwrap();
let cur = wheel.cur;
wheel.cur = (wheel.cur + 1) % WHEEL_SLOTS;
{
@@ -105,7 +108,7 @@ impl TimerController {
}
}).unwrap();
- TimerController(con1)
+ TimerController(con)
}
pub fn register_delay(&self, delay: Duration, action: Action) -> TimerHandle {
@@ -123,7 +126,6 @@ impl TimerController {
wheel.wheel[pos].insert(ArcTimer(timer.clone()));
TimerHandle {
- controller: self.0.clone(),
pos: AtomicUsize::new(pos),
timer: ArcTimer(timer),
}
@@ -142,7 +144,7 @@ impl TimerHandle {
pub fn adjust_and_activate(&self, secs: u64) {
let (offset, rounds) = calc_offset_and_rounds(Duration::from_secs(secs));
- let mut wheel = self.controller.lock().unwrap();
+ let mut wheel = CONTROLLER.0.lock().unwrap();
let old_pos = self.pos.load(Ordering::Relaxed);
let new_pos = (wheel.cur + offset) % WHEEL_SLOTS;
self.pos.store(new_pos, Ordering::Relaxed);
@@ -151,7 +153,7 @@ impl TimerHandle {
let t = wheel.wheel[old_pos].take(&self.timer).unwrap();
wheel.wheel[new_pos].insert(t);
- self.timer.activated.store(true, Ordering::Release);
+ self.timer.activated.store(true, Ordering::Relaxed);
}
pub fn adjust_and_activate_if_not_activated(&self, secs: u64) {