This commit is contained in:
Dennis Schwerdel 2019-02-14 23:39:16 +01:00
parent 19ad3ed7f7
commit 54e0119451
4 changed files with 42 additions and 70 deletions

View File

@ -1,22 +0,0 @@
.PHONY: default
default: test build
.PHONY: build
build:
cargo build --release
.PHONY: test
test:
cargo test
.PHONY: bench
bench:
cargo bench --features bench
.PHONY: deb
deb:
make -C deb
.PHONY: clean
clean:
rm -rf target

View File

@ -9,6 +9,7 @@ use std::net::{UdpSocket, ToSocketAddrs, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::os::unix::io::AsRawFd;
use super::MAGIC;
use super::config::Config;
use super::cloud::GenericCloud;
use super::device::{Device, Type};
use super::udpmessage::{Message, encode, decode};
@ -150,9 +151,10 @@ fn epoll_wait(b: &mut Bencher) {
#[bench]
fn handle_interface_data(b: &mut Bencher) {
let config = Config::default();
let mut node = GenericCloud::<Frame, SwitchTable>::new(
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None
&config, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(),
SwitchTable::new(300, 10), true, true, vec![], Crypto::None, None
);
let mut data = [0; 1500];
data[105] = 45;
@ -164,9 +166,10 @@ fn handle_interface_data(b: &mut Bencher) {
#[bench]
fn handle_net_message(b: &mut Bencher) {
let config = Config::default();
let mut node = GenericCloud::<Frame, SwitchTable>::new(
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None
&config, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(),
SwitchTable::new(300, 10), true, true, vec![], Crypto::None, None
);
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
let mut data = [0; 1500];

View File

@ -3,7 +3,7 @@
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::net::UdpSocket;
use std::io::{self, Write};
use std::fmt;
@ -12,7 +12,8 @@ use std::marker::PhantomData;
use std::hash::BuildHasherDefault;
use std::time::Instant;
use std::cmp::min;
use std::fs::File;
use std::fs::{self, File, Permissions};
use std::os::unix::fs::PermissionsExt;
use fnv::FnvHasher;
use signal::{trap::Trap, Signal};
@ -46,7 +47,7 @@ struct PeerList {
timeout: Duration,
peers: HashMap<SocketAddr, PeerData, Hash>,
nodes: HashMap<NodeId, SocketAddr, Hash>,
addresses: HashSet<SocketAddr, Hash>
addresses: HashMap<SocketAddr, NodeId, Hash>
}
impl PeerList {
@ -55,7 +56,7 @@ impl PeerList {
peers: HashMap::default(),
timeout,
nodes: HashMap::default(),
addresses: HashSet::default()
addresses: HashMap::default()
}
}
@ -82,7 +83,7 @@ impl PeerList {
#[inline]
fn contains_addr(&self, addr: &SocketAddr) -> bool {
self.addresses.contains(addr)
self.addresses.contains_key(addr)
}
#[inline]
@ -110,7 +111,7 @@ impl PeerList {
node_id,
alt_addrs: vec![]
});
self.addresses.insert(addr);
self.addresses.insert(addr, node_id);
}
}
@ -122,22 +123,27 @@ impl PeerList {
}
#[inline]
fn add_alt_addr(&mut self, node_id: NodeId, addr: SocketAddr) {
if let Some(main_addr) = self.nodes.get(&node_id) {
if let Some(ref mut data) = self.peers.get_mut(main_addr) {
data.alt_addrs.push(addr);
self.addresses.insert(addr);
} else {
error!("Main address for node is not connected");
}
} else {
error!("Node not connected");
fn make_primary(&mut self, node_id: NodeId, addr: SocketAddr) {
if self.peers.contains_key(&addr) {
return
}
let old_addr = match self.nodes.remove(&node_id) {
Some(old_addr) => old_addr,
None => return error!("Node not connected")
};
self.nodes.insert(node_id, addr);
let mut peer = match self.peers.remove(&old_addr) {
Some(peer) => peer,
None => return error!("Main address for node is not connected")
};
peer.alt_addrs.retain(|i| i != &addr);
peer.alt_addrs.push(old_addr);
self.peers.insert(addr, peer);
}
#[inline]
fn as_vec(&self) -> Vec<SocketAddr> {
self.addresses.iter().cloned().collect()
self.addresses.keys().cloned().collect()
}
#[inline]
@ -153,7 +159,7 @@ impl PeerList {
#[inline]
fn subset(&self, size: usize) -> Vec<SocketAddr> {
self.addresses.iter().choose_multiple(&mut thread_rng(), size).into_iter().cloned().collect()
self.peers.keys().choose_multiple(&mut thread_rng(), size).into_iter().cloned().collect()
}
#[inline]
@ -250,7 +256,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
next_housekeep: now(),
next_stats_out: now() + STATS_INTERVAL,
port_forwarding,
traffic: TrafficStats::new(),
traffic: TrafficStats::default(),
stats_file: config.stats_file.clone(),
_dummy_p: PhantomData,
}
@ -474,6 +480,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
try!(writeln!(&mut f));
try!(self.traffic.write_out(&mut f));
try!(writeln!(&mut f));
try!(fs::set_permissions(self.stats_file.as_ref().unwrap(), Permissions::from_mode(0o644)));
Ok(())
}
@ -595,8 +602,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
}
// Add sender as peer or as alternative address to existing peer
if self.peers.contains_node(&node_id) {
//TODO: make this address primary
self.peers.add_alt_addr(node_id, peer);
self.peers.make_primary(node_id, peer);
} else {
self.peers.add(node_id, peer);
for range in ranges {

View File

@ -7,6 +7,7 @@ use super::cloud::Hash;
use super::util::Bytes;
#[derive(Default)]
pub struct TrafficEntry {
pub out_bytes_total: u64,
pub out_packets_total: usize,
@ -20,20 +21,6 @@ pub struct TrafficEntry {
}
impl TrafficEntry {
pub fn new() -> Self {
TrafficEntry {
out_bytes_total: 0,
out_packets_total: 0,
out_bytes: 0,
out_packets: 0,
in_bytes_total: 0,
in_packets_total: 0,
in_bytes: 0,
in_packets: 0,
idle_periods: 0
}
}
#[inline]
fn count_out(&mut self, bytes: usize) {
self.out_packets += 1;
@ -63,34 +50,32 @@ impl TrafficEntry {
}
}
#[derive(Default)]
pub struct TrafficStats {
peers: HashMap<SocketAddr, TrafficEntry, Hash>,
payload: HashMap<(Address, Address), TrafficEntry, Hash>
}
impl TrafficStats {
pub fn new() -> Self {
Self { peers: Default::default(), payload: Default::default() }
}
#[inline]
pub fn count_out_traffic(&mut self, peer: SocketAddr, bytes: usize) {
self.peers.entry(peer).or_insert_with(TrafficEntry::new).count_out(bytes);
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_out(bytes);
}
#[inline]
pub fn count_in_traffic(&mut self, peer: SocketAddr, bytes: usize) {
self.peers.entry(peer).or_insert_with(TrafficEntry::new).count_in(bytes);
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_in(bytes);
}
#[inline]
pub fn count_out_payload(&mut self, remote: Address, local: Address, bytes: usize) {
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::new).count_out(bytes);
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_out(bytes);
}
#[inline]
pub fn count_in_payload(&mut self, remote: Address, local: Address, bytes: usize) {
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::new).count_in(bytes);
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_in(bytes);
}
pub fn period(&mut self, cleanup_idle: Option<usize>) {
@ -131,4 +116,4 @@ impl TrafficStats {
}
Ok(())
}
}
}