From 68af49bf29414cb53ae1f679c3d6cb0cb4efda2b Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Thu, 4 May 2017 08:22:24 +0200 Subject: [PATCH] Removed a layer of indirection from inner loop --- CHANGELOG.md | 4 ++- src/benches.rs | 8 +++--- src/cloud.rs | 8 +++--- src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 483e490..6f40d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,10 @@ This project follows [semantic versioning](http://semver.org). ### UNRELEASED - [added] Added more tests -- [changed] Updated dependencies and libsodium +- [changed] Updated dependencies +- [changed] Updated libsodium to 1.0.12 - [changed] Small fixes to make clippy happy +- [changed] Removed a layer of indirection from inner loop - [fixed] Fixed two problems with routing table ### v0.8.0 (2016-11-25) diff --git a/src/benches.rs b/src/benches.rs index 988cc39..761a060 100644 --- a/src/benches.rs +++ b/src/benches.rs @@ -150,9 +150,9 @@ fn epoll_wait(b: &mut Bencher) { #[bench] fn handle_interface_data(b: &mut Bencher) { - let mut node = GenericCloud::::new( + let mut node = GenericCloud::::new( MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0, - Box::new(SwitchTable::new(300, 10)), 1800, true, true, vec![], Crypto::None, None + SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None ); let mut data = [0; 1500]; data[105] = 45; @@ -164,9 +164,9 @@ fn handle_interface_data(b: &mut Bencher) { #[bench] fn handle_net_message(b: &mut Bencher) { - let mut node = GenericCloud::::new( + let mut node = GenericCloud::::new( MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0, - Box::new(SwitchTable::new(300, 10)), 1800, true, true, vec![], Crypto::None, None + SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None ); let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1)); let mut data = [0; 1500]; diff --git a/src/cloud.rs b/src/cloud.rs index 07cbdbe..c7c4b5a 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -167,7 +167,7 @@ pub struct ReconnectEntry { } -pub struct GenericCloud { +pub struct GenericCloud { magic: HeaderMagic, node_id: NodeId, peers: PeerList, @@ -176,7 +176,7 @@ pub struct GenericCloud { broadcast: bool, reconnect_peers: Vec, blacklist_peers: Vec, - table: Box, + table: T, socket4: UdpSocket, socket6: UdpSocket, device: Device, @@ -189,10 +189,10 @@ pub struct GenericCloud { _dummy_p: PhantomData

, } -impl GenericCloud

{ +impl GenericCloud { #[allow(unknown_lints)] #[allow(too_many_arguments)] - pub fn new(magic: HeaderMagic, device: Device, listen: u16, table: Box

, + pub fn new(magic: HeaderMagic, device: Device, listen: u16, table: T, peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec, crypto: Crypto, port_forwarding: Option) -> Self { let socket4 = match UdpBuilder::new_v4().expect("Failed to obtain ipv4 socket builder") diff --git a/src/main.rs b/src/main.rs index f1c8c81..d8d3ff3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ use std::io::{self, Write}; use device::{Device, Type}; use ethernet::SwitchTable; use ip::RoutingTable; -use types::{Mode, Range, Table, Protocol, HeaderMagic}; +use types::{Mode, Range, Protocol, HeaderMagic, Error}; use cloud::GenericCloud; use crypto::{Crypto, CryptoMethod}; use port_forwarding::PortForwarding; @@ -138,7 +138,62 @@ fn run_script(script: &str, ifname: &str) { } } -fn run (config: Config) { +enum AnyTable { + Switch(SwitchTable), + Routing(RoutingTable) +} + +enum AnyCloud { + Switch(GenericCloud), + Routing(GenericCloud) +} + +impl AnyCloud

{ + #[allow(unknown_lints,too_many_arguments)] + fn new(magic: HeaderMagic, device: Device, listen: u16, table: AnyTable, + peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec, + crypto: Crypto, port_forwarding: Option) -> Self { + match table { + AnyTable::Switch(t) => AnyCloud::Switch(GenericCloud::::new( + magic, device, listen, t, peer_timeout, learning, broadcast, addresses, crypto, port_forwarding + )), + AnyTable::Routing(t) => AnyCloud::Routing(GenericCloud::::new( + magic, device, listen, t, peer_timeout, learning, broadcast, addresses, crypto, port_forwarding + )) + } + } + + fn ifname(&self) -> &str { + match *self { + AnyCloud::Switch(ref c) => c.ifname(), + AnyCloud::Routing(ref c) => c.ifname() + } + } + + fn run(&mut self) { + match *self { + AnyCloud::Switch(ref mut c) => c.run(), + AnyCloud::Routing(ref mut c) => c.run() + } + } + + fn connect(&mut self, a: &str) -> Result<(), Error> { + match *self { + AnyCloud::Switch(ref mut c) => c.connect(a), + AnyCloud::Routing(ref mut c) => c.connect(a) + } + } + + fn add_reconnect_peer(&mut self, a: String) { + match *self { + AnyCloud::Switch(ref mut c) => c.add_reconnect_peer(a), + AnyCloud::Routing(ref mut c) => c.add_reconnect_peer(a) + } + } +} + + +fn run (config: Config) { let device = try_fail!(Device::new(&config.device_name, config.device_type), "Failed to open virtual {} interface {}: {}", config.device_type, config.device_name); info!("Opened device {}", device.ifname()); @@ -148,14 +203,14 @@ fn run (config: Config) { } let dst_timeout = config.dst_timeout; let peer_timeout = config.peer_timeout; - let (learning, broadcasting, table): (bool, bool, Box

) = match config.mode { + let (learning, broadcasting, table) = match config.mode { Mode::Normal => match config.device_type { - Type::Tap => (true, true, Box::new(SwitchTable::new(dst_timeout, 10))), - Type::Tun => (false, false, Box::new(RoutingTable::new())) + Type::Tap => (true, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10))), + Type::Tun => (false, false, AnyTable::Routing(RoutingTable::new())) }, - Mode::Router => (false, false, Box::new(RoutingTable::new())), - Mode::Switch => (true, true, Box::new(SwitchTable::new(dst_timeout, 10))), - Mode::Hub => (false, true, Box::new(SwitchTable::new(dst_timeout, 10))) + Mode::Router => (false, false, AnyTable::Routing(RoutingTable::new())), + Mode::Switch => (true, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10))), + Mode::Hub => (false, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10))) }; let magic = config.get_magic(); Crypto::init(); @@ -168,7 +223,7 @@ fn run (config: Config) { } else { None }; - let mut cloud = GenericCloud::::new(magic, device, config.port, table, peer_timeout, learning, broadcasting, ranges, crypto, port_forwarding); + let mut cloud = AnyCloud::

::new(magic, device, config.port, table, peer_timeout, learning, broadcasting, ranges, crypto, port_forwarding); if let Some(script) = config.ifup { run_script(&script, cloud.ifname()); }