vpncloud/src/traffic.rs

174 lines
5.4 KiB
Rust
Raw Normal View History

2019-02-19 21:04:21 +00:00
// VpnCloud - Peer-to-Peer VPN
2020-05-28 07:03:48 +00:00
// Copyright (C) 2018-2020 Dennis Schwerdel
2019-02-19 21:04:21 +00:00
// This software is licensed under GPL-3 or newer (see LICENSE.md)
2019-12-04 08:32:35 +00:00
use std::{
collections::HashMap,
io::{self, Write},
net::SocketAddr
};
2019-01-09 16:45:12 +00:00
2020-05-29 08:04:14 +00:00
use super::{
cloud::Hash,
types::Address,
util::{addr_nice, Bytes}
};
2019-01-09 16:45:12 +00:00
2019-02-14 22:39:16 +00:00
#[derive(Default)]
2019-01-09 16:45:12 +00:00
pub struct TrafficEntry {
pub out_bytes_total: u64,
pub out_packets_total: usize,
pub out_bytes: u64,
pub out_packets: usize,
pub in_bytes_total: u64,
pub in_packets_total: usize,
pub in_bytes: u64,
pub in_packets: usize,
pub idle_periods: usize
}
impl TrafficEntry {
#[inline]
fn count_out(&mut self, bytes: usize) {
self.out_packets += 1;
self.out_bytes += bytes as u64;
}
#[inline]
fn count_in(&mut self, bytes: usize) {
self.in_packets += 1;
self.in_bytes += bytes as u64;
}
fn period(&mut self) {
self.out_bytes_total += self.out_bytes;
self.out_packets_total += self.out_packets;
self.in_bytes_total += self.in_bytes;
self.in_packets_total += self.in_packets;
if self.in_packets == 0 && self.out_packets == 0 {
self.idle_periods += 1;
} else {
self.idle_periods = 0;
}
self.out_packets = 0;
self.in_packets = 0;
self.out_bytes = 0;
self.in_bytes = 0;
}
}
2019-02-14 22:39:16 +00:00
#[derive(Default)]
2019-01-09 16:45:12 +00:00
pub struct TrafficStats {
peers: HashMap<SocketAddr, TrafficEntry, Hash>,
2020-05-29 08:04:14 +00:00
payload: HashMap<(Address, Address), TrafficEntry, Hash>,
dropped: TrafficEntry
2019-01-09 16:45:12 +00:00
}
impl TrafficStats {
#[inline]
pub fn count_out_traffic(&mut self, peer: SocketAddr, bytes: usize) {
2019-02-14 22:39:16 +00:00
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_out(bytes);
2019-01-09 16:45:12 +00:00
}
#[inline]
pub fn count_in_traffic(&mut self, peer: SocketAddr, bytes: usize) {
2019-02-14 22:39:16 +00:00
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_in(bytes);
2019-01-09 16:45:12 +00:00
}
#[inline]
pub fn count_out_payload(&mut self, remote: Address, local: Address, bytes: usize) {
2019-02-14 22:39:16 +00:00
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_out(bytes);
2019-01-09 16:45:12 +00:00
}
#[inline]
pub fn count_in_payload(&mut self, remote: Address, local: Address, bytes: usize) {
2019-02-14 22:39:16 +00:00
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_in(bytes);
2019-01-09 16:45:12 +00:00
}
2020-05-29 08:04:14 +00:00
pub fn count_invalid_protocol(&mut self, bytes: usize) {
self.dropped.count_in(bytes)
}
pub fn count_dropped_payload(&mut self, bytes: usize) {
self.dropped.count_out(bytes)
}
2019-01-09 16:45:12 +00:00
pub fn period(&mut self, cleanup_idle: Option<usize>) {
for entry in self.peers.values_mut() {
entry.period();
}
for entry in self.payload.values_mut() {
entry.period();
}
2020-05-29 08:04:14 +00:00
self.dropped.period();
2019-01-09 16:45:12 +00:00
if let Some(periods) = cleanup_idle {
self.peers.retain(|_, entry| entry.idle_periods < periods);
self.payload.retain(|_, entry| entry.idle_periods < periods);
}
}
2019-12-04 08:32:35 +00:00
pub fn get_peer_traffic(&self) -> impl Iterator<Item = (&SocketAddr, &TrafficEntry)> {
2019-01-09 16:45:12 +00:00
self.peers.iter()
}
2019-12-04 08:32:35 +00:00
pub fn get_payload_traffic(&self) -> impl Iterator<Item = (&(Address, Address), &TrafficEntry)> {
2019-01-09 16:45:12 +00:00
self.payload.iter()
}
#[inline]
pub fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
2020-05-29 08:04:14 +00:00
writeln!(out, "peer_traffic:")?;
2019-01-09 16:45:12 +00:00
let mut peers: Vec<_> = self.get_peer_traffic().collect();
peers.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
for (addr, data) in peers.iter().rev() {
2020-05-29 08:04:14 +00:00
writeln!(
out,
" - peer: \"{}\"\n in: {{ display: \"{}/s\", bytes: {}, packets: {} }}\n out: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
addr_nice(**addr),
Bytes(data.in_bytes / 60),
data.in_bytes,
data.in_packets,
Bytes(data.out_bytes / 60),
data.out_bytes,
data.out_packets
)?;
2019-01-09 16:45:12 +00:00
}
2019-03-01 22:12:19 +00:00
writeln!(out)?;
2020-05-29 08:04:14 +00:00
writeln!(out, "payload_traffic:")?;
2019-01-09 16:45:12 +00:00
let mut payload: Vec<_> = self.get_payload_traffic().collect();
payload.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
for ((remote, local), data) in payload.iter().rev() {
2019-12-04 08:32:35 +00:00
writeln!(
out,
2020-05-29 08:04:14 +00:00
" - addrs: [\"{}\", \"{}\"]\n in: {{ display: \"{}/s\", bytes: {}, packets: {} }}\n out: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
2019-12-04 08:32:35 +00:00
remote,
local,
Bytes(data.in_bytes / 60),
2020-05-29 08:04:14 +00:00
data.in_bytes,
data.in_packets,
Bytes(data.out_bytes / 60),
data.out_bytes,
data.out_packets
2019-12-04 08:32:35 +00:00
)?;
2019-01-09 16:45:12 +00:00
}
2020-05-29 08:04:14 +00:00
writeln!(out)?;
writeln!(
out,
"invalid_protocol_traffic: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
Bytes(self.dropped.in_bytes / 60),
self.dropped.in_bytes,
self.dropped.in_packets
)?;
writeln!(
out,
"dropped_payload_traffic: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
Bytes(self.dropped.out_bytes / 60),
self.dropped.out_bytes,
self.dropped.out_packets
)?;
2019-01-09 16:45:12 +00:00
Ok(())
}
2019-02-14 22:39:16 +00:00
}