From 2f3ceab0364497a4a6cf866b505f74443ed6e3ae Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 16 Oct 2019 13:40:40 +0200 Subject: Work on porting timer semantics and linux platform --- src/wireguard/router/mod.rs | 8 ++++++-- src/wireguard/router/tests.rs | 22 +++++++++++----------- src/wireguard/router/types.rs | 8 ++++---- src/wireguard/router/workers.rs | 24 ++++++++++-------------- 4 files changed, 31 insertions(+), 31 deletions(-) (limited to 'src/wireguard/router') diff --git a/src/wireguard/router/mod.rs b/src/wireguard/router/mod.rs index 7a29cd9..4e748cb 100644 --- a/src/wireguard/router/mod.rs +++ b/src/wireguard/router/mod.rs @@ -14,9 +14,13 @@ use messages::TransportHeader; use std::mem; pub const SIZE_MESSAGE_PREFIX: usize = mem::size_of::(); -pub const CAPACITY_MESSAGE_POSTFIX: usize = 16; +pub const CAPACITY_MESSAGE_POSTFIX: usize = workers::SIZE_TAG; + +pub const fn message_data_len(payload: usize) -> usize { + payload + mem::size_of::() + workers::SIZE_TAG +} -pub use messages::TYPE_TRANSPORT; pub use device::Device; +pub use messages::TYPE_TRANSPORT; pub use peer::Peer; pub use types::Callbacks; diff --git a/src/wireguard/router/tests.rs b/src/wireguard/router/tests.rs index fbee39e..93c0773 100644 --- a/src/wireguard/router/tests.rs +++ b/src/wireguard/router/tests.rs @@ -28,8 +28,8 @@ mod tests { // type for tracking events inside the router module struct Flags { - send: Mutex>, - recv: Mutex>, + send: Mutex>, + recv: Mutex>, need_key: Mutex>, key_confirmed: Mutex>, } @@ -56,11 +56,11 @@ mod tests { self.0.key_confirmed.lock().unwrap().clear(); } - fn send(&self) -> Option<(usize, bool, bool)> { + fn send(&self) -> Option<(usize, bool)> { self.0.send.lock().unwrap().pop() } - fn recv(&self) -> Option<(usize, bool, bool)> { + fn recv(&self) -> Option<(usize, bool)> { self.0.recv.lock().unwrap().pop() } @@ -85,12 +85,12 @@ mod tests { impl Callbacks for TestCallbacks { type Opaque = Opaque; - fn send(t: &Self::Opaque, size: usize, data: bool, sent: bool) { - t.0.send.lock().unwrap().push((size, data, sent)) + fn send(t: &Self::Opaque, size: usize, sent: bool) { + t.0.send.lock().unwrap().push((size, sent)) } - fn recv(t: &Self::Opaque, size: usize, data: bool, sent: bool) { - t.0.recv.lock().unwrap().push((size, data, sent)) + fn recv(t: &Self::Opaque, size: usize, sent: bool) { + t.0.recv.lock().unwrap().push((size, sent)) } fn need_key(t: &Self::Opaque) { @@ -135,10 +135,10 @@ mod tests { struct BencherCallbacks {} impl Callbacks for BencherCallbacks { type Opaque = Arc; - fn send(t: &Self::Opaque, size: usize, _data: bool, _sent: bool) { + fn send(t: &Self::Opaque, size: usize, _sent: bool) { t.fetch_add(size, Ordering::SeqCst); } - fn recv(_: &Self::Opaque, _size: usize, _data: bool, _sent: bool) {} + fn recv(_: &Self::Opaque, _size: usize, _sent: bool) {} fn need_key(_: &Self::Opaque) {} fn key_confirmed(_: &Self::Opaque) {} } @@ -253,7 +253,7 @@ mod tests { assert_eq!( opaque.send(), if set_key { - Some((SIZE_KEEPALIVE, false, false)) + Some((SIZE_KEEPALIVE, false)) } else { None }, diff --git a/src/wireguard/router/types.rs b/src/wireguard/router/types.rs index b7c3ae0..52ee4f1 100644 --- a/src/wireguard/router/types.rs +++ b/src/wireguard/router/types.rs @@ -10,9 +10,9 @@ impl Opaque for T where T: Send + Sync + 'static {} /// * `0`, a reference to the opaque value assigned to the peer /// * `1`, a bool indicating whether the message contained data (not just keepalive) /// * `2`, a bool indicating whether the message was transmitted (i.e. did the peer have an associated endpoint?) -pub trait Callback: Fn(&T, usize, bool, bool) -> () + Sync + Send + 'static {} +pub trait Callback: Fn(&T, usize, bool) -> () + Sync + Send + 'static {} -impl Callback for F where F: Fn(&T, usize, bool, bool) -> () + Sync + Send + 'static {} +impl Callback for F where F: Fn(&T, usize, bool) -> () + Sync + Send + 'static {} /// A key callback takes 1 argument /// @@ -23,8 +23,8 @@ impl KeyCallback for F where F: Fn(&T) -> () + Sync + Send + 'static {} pub trait Callbacks: Send + Sync + 'static { type Opaque: Opaque; - fn send(opaque: &Self::Opaque, size: usize, data: bool, sent: bool); - fn recv(opaque: &Self::Opaque, size: usize, data: bool, sent: bool); + fn send(opaque: &Self::Opaque, size: usize, sent: bool); + fn recv(opaque: &Self::Opaque, size: usize, sent: bool); fn need_key(opaque: &Self::Opaque); fn key_confirmed(opaque: &Self::Opaque); } diff --git a/src/wireguard/router/workers.rs b/src/wireguard/router/workers.rs index 2e89bb0..61a7620 100644 --- a/src/wireguard/router/workers.rs +++ b/src/wireguard/router/workers.rs @@ -17,10 +17,10 @@ use super::messages::{TransportHeader, TYPE_TRANSPORT}; use super::peer::PeerInner; use super::types::Callbacks; -use super::super::types::{Endpoint, tun, bind}; +use super::super::types::{bind, tun, Endpoint}; use super::ip::*; -const SIZE_TAG: usize = 16; +pub const SIZE_TAG: usize = 16; #[derive(PartialEq, Debug)] pub enum Operation { @@ -47,7 +47,7 @@ pub type JobInbound> = ( pub type JobOutbound = oneshot::Receiver; #[inline(always)] -fn check_route>( +fn check_route>( device: &Arc>, peer: &Arc>, packet: &[u8], @@ -93,7 +93,7 @@ fn check_route>( } } -pub fn worker_inbound>( +pub fn worker_inbound>( device: Arc>, // related device peer: Arc>, // related peer receiver: Receiver>, @@ -151,7 +151,8 @@ pub fn worker_inbound 0 { if let Some(inner_len) = check_route(&device, &peer, &packet[..length]) { - debug_assert!(inner_len <= length, "should be validated"); + // TODO: Consider moving the cryptkey route check to parallel decryption worker + debug_assert!(inner_len <= length, "should be validated earlier"); if inner_len <= length { sent = match device.inbound.write(&packet[..inner_len]) { Err(e) => { @@ -167,7 +168,7 @@ pub fn worker_inbound>( +pub fn worker_outbound>( device: Arc>, // related device peer: Arc>, // related peer receiver: Receiver, @@ -198,7 +199,7 @@ pub fn worker_outbound = &*device.outbound.read(); + let send: &Option = &*device.outbound.read(); if let Some(writer) = send.as_ref() { match writer.write(&buf.msg[..], dst) { Err(e) => { @@ -215,12 +216,7 @@ pub fn worker_outbound SIZE_TAG + mem::size_of::(), - xmit, - ); + C::send(&peer.opaque, buf.msg.len(), xmit); } }) .wait(); -- cgit v1.2.3-59-g8ed1b