2022-02-14 12:44:27 +00:00
|
|
|
//! Traffic control implementation
|
|
|
|
|
2021-11-08 11:40:09 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2021-11-14 16:20:55 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
use std::convert::TryInto;
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
use anyhow::Error;
|
|
|
|
use cidr::IpInet;
|
|
|
|
|
2021-11-13 14:43:56 +00:00
|
|
|
use proxmox_http::client::{ShareableRateLimit, RateLimiter};
|
2021-11-08 11:40:09 +00:00
|
|
|
use proxmox_section_config::SectionConfigData;
|
|
|
|
|
2021-11-17 11:29:31 +00:00
|
|
|
use proxmox_time::{parse_daily_duration, DailyDuration, TmEditor};
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
use pbs_api_types::TrafficControlRule;
|
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
use pbs_config::ConfigVersionCache;
|
2021-11-08 11:40:09 +00:00
|
|
|
|
2021-11-13 16:19:07 +00:00
|
|
|
use super::SharedRateLimiter;
|
|
|
|
|
2021-11-14 16:20:55 +00:00
|
|
|
lazy_static::lazy_static!{
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Shared traffic control cache singleton.
|
2021-11-14 16:20:55 +00:00
|
|
|
pub static ref TRAFFIC_CONTROL_CACHE: Arc<Mutex<TrafficControlCache>> =
|
|
|
|
Arc::new(Mutex::new(TrafficControlCache::new()));
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:40:09 +00:00
|
|
|
struct ParsedTcRule {
|
|
|
|
config: TrafficControlRule, // original rule config
|
|
|
|
networks: Vec<IpInet>, // parsed networks
|
|
|
|
timeframe: Vec<DailyDuration>, // parsed timeframe
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Traffic control statistics
|
2021-11-14 16:20:55 +00:00
|
|
|
pub struct TrafficStat {
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Total incomming traffic (bytes)
|
2021-11-14 16:20:55 +00:00
|
|
|
pub traffic_in: u64,
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Incoming data rate (bytes/second)
|
2021-11-14 16:20:55 +00:00
|
|
|
pub rate_in: u64,
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Total outgoing traffic (bytes)
|
2021-11-14 16:20:55 +00:00
|
|
|
pub traffic_out: u64,
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Outgoing data rate (bytes/second)
|
2021-11-14 16:20:55 +00:00
|
|
|
pub rate_out: u64,
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Cache rules from `/etc/proxmox-backup/traffic-control.cfg`
|
|
|
|
/// together with corresponding rate limiter implementation.
|
2021-11-08 11:40:09 +00:00
|
|
|
pub struct TrafficControlCache {
|
2022-02-14 12:44:27 +00:00
|
|
|
// use shared memory to make it work with daemon restarts
|
2021-11-13 16:19:07 +00:00
|
|
|
use_shared_memory: bool,
|
2021-11-14 16:20:55 +00:00
|
|
|
last_rate_compute: Instant,
|
|
|
|
current_rate_map: HashMap<String, TrafficStat>,
|
2021-11-08 11:40:09 +00:00
|
|
|
last_update: i64,
|
|
|
|
last_traffic_control_generation: usize,
|
|
|
|
rules: Vec<ParsedTcRule>,
|
2021-11-13 14:43:56 +00:00
|
|
|
limiter_map: HashMap<String, (Option<Arc<dyn ShareableRateLimit>>, Option<Arc<dyn ShareableRateLimit>>)>,
|
2021-11-08 11:40:09 +00:00
|
|
|
use_utc: bool, // currently only used for testing
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeframe_match(
|
|
|
|
duration_list: &[DailyDuration],
|
|
|
|
now: &TmEditor,
|
|
|
|
) -> bool {
|
|
|
|
|
|
|
|
if duration_list.is_empty() { return true; }
|
|
|
|
|
|
|
|
for duration in duration_list.iter() {
|
|
|
|
if duration.time_match_with_tm_editor(now) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn network_match_len(
|
|
|
|
networks: &[IpInet],
|
|
|
|
ip: &IpAddr,
|
|
|
|
) -> Option<u8> {
|
|
|
|
|
|
|
|
let mut match_len = None;
|
|
|
|
|
|
|
|
for cidr in networks.iter() {
|
|
|
|
if cidr.contains(ip) {
|
|
|
|
let network_length = cidr.network_length();
|
|
|
|
match match_len {
|
|
|
|
Some(len) => {
|
|
|
|
if network_length > len {
|
|
|
|
match_len = Some(network_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => match_len = Some(network_length),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match_len
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cannonical_ip(ip: IpAddr) -> IpAddr {
|
|
|
|
// TODO: use std::net::IpAddr::to_cananical once stable
|
|
|
|
match ip {
|
|
|
|
IpAddr::V4(addr) => IpAddr::V4(addr),
|
|
|
|
IpAddr::V6(addr) => {
|
|
|
|
match addr.octets() {
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
|
|
|
|
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
|
|
|
|
}
|
|
|
|
_ => IpAddr::V6(addr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 14:43:56 +00:00
|
|
|
fn create_limiter(
|
2021-11-13 16:19:07 +00:00
|
|
|
use_shared_memory: bool,
|
|
|
|
name: &str,
|
2021-11-13 14:43:56 +00:00
|
|
|
rate: u64,
|
|
|
|
burst: u64,
|
|
|
|
) -> Result<Arc<dyn ShareableRateLimit>, Error> {
|
2021-11-13 16:19:07 +00:00
|
|
|
if use_shared_memory {
|
|
|
|
let limiter = SharedRateLimiter::mmap_shmem(name, rate, burst)?;
|
|
|
|
Ok(Arc::new(limiter))
|
|
|
|
} else {
|
|
|
|
Ok(Arc::new(Mutex::new(RateLimiter::new(rate, burst))))
|
|
|
|
}
|
2021-11-13 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:40:09 +00:00
|
|
|
impl TrafficControlCache {
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
fn new() -> Self {
|
2021-11-08 11:40:09 +00:00
|
|
|
Self {
|
2021-11-13 16:19:07 +00:00
|
|
|
use_shared_memory: true,
|
2021-11-08 11:40:09 +00:00
|
|
|
rules: Vec::new(),
|
|
|
|
limiter_map: HashMap::new(),
|
|
|
|
last_traffic_control_generation: 0,
|
|
|
|
last_update: 0,
|
|
|
|
use_utc: false,
|
2021-11-14 16:20:55 +00:00
|
|
|
last_rate_compute: Instant::now(),
|
|
|
|
current_rate_map: HashMap::new(),
|
2021-11-08 11:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Reload rules from configuration file
|
|
|
|
///
|
|
|
|
/// Only reload if configuration file was updated
|
|
|
|
/// ([ConfigVersionCache]) or last update is older that 60
|
|
|
|
/// seconds.
|
2021-11-08 11:40:09 +00:00
|
|
|
pub fn reload(&mut self, now: i64) {
|
2021-11-12 17:44:28 +00:00
|
|
|
let version_cache = match ConfigVersionCache::new() {
|
|
|
|
Ok(cache) => cache,
|
2021-11-08 11:40:09 +00:00
|
|
|
Err(err) => {
|
2021-11-12 17:44:28 +00:00
|
|
|
log::error!("TrafficControlCache::reload failed in ConfigVersionCache::new: {}", err);
|
2021-11-08 11:40:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
let traffic_control_generation = version_cache.traffic_control_generation();
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
if (self.last_update != 0) &&
|
|
|
|
(traffic_control_generation == self.last_traffic_control_generation) &&
|
|
|
|
((now - self.last_update) < 60) { return; }
|
|
|
|
|
|
|
|
log::debug!("reload traffic control rules");
|
|
|
|
|
|
|
|
self.last_traffic_control_generation = traffic_control_generation;
|
|
|
|
self.last_update = now;
|
|
|
|
|
|
|
|
match self.reload_impl() {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("TrafficControlCache::reload failed -> {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reload_impl(&mut self) -> Result<(), Error> {
|
|
|
|
let (config, _) = pbs_config::traffic_control::config()?;
|
|
|
|
|
|
|
|
self.update_config(&config)
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Compute current data rates.
|
|
|
|
///
|
|
|
|
/// This should be called every second (from `proxmox-backup-proxy`).
|
2021-11-14 16:20:55 +00:00
|
|
|
pub fn compute_current_rates(&mut self) {
|
|
|
|
|
|
|
|
let elapsed = self.last_rate_compute.elapsed().as_micros();
|
|
|
|
if elapsed < 200_000 { return } // not enough data
|
|
|
|
|
|
|
|
let mut new_rate_map = HashMap::new();
|
|
|
|
|
|
|
|
for (rule, (read_limit, write_limit)) in self.limiter_map.iter() {
|
|
|
|
let traffic_in = read_limit.as_ref().map(|l| l.traffic()).unwrap_or(0);
|
|
|
|
let traffic_out = write_limit.as_ref().map(|l| l.traffic()).unwrap_or(0);
|
|
|
|
|
|
|
|
let traffic_diff_in;
|
|
|
|
let traffic_diff_out;
|
|
|
|
|
|
|
|
if let Some(stat) = self.current_rate_map.get(rule) {
|
|
|
|
traffic_diff_in = traffic_in.saturating_sub(stat.traffic_in);
|
|
|
|
traffic_diff_out = traffic_out.saturating_sub(stat.traffic_out);
|
|
|
|
} else {
|
|
|
|
traffic_diff_in = 0;
|
|
|
|
traffic_diff_out = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rate_in = ((traffic_diff_in as u128) * 1_000_000) / elapsed;
|
|
|
|
let rate_out = ((traffic_diff_out as u128) * 1_000_000) / elapsed;
|
|
|
|
|
|
|
|
let stat = TrafficStat {
|
|
|
|
traffic_in,
|
|
|
|
traffic_out,
|
|
|
|
rate_in: rate_in.try_into().unwrap_or(u64::MAX),
|
|
|
|
rate_out: rate_out.try_into().unwrap_or(u64::MAX),
|
|
|
|
};
|
|
|
|
new_rate_map.insert(rule.clone(), stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.current_rate_map = new_rate_map;
|
|
|
|
|
|
|
|
self.last_rate_compute = Instant::now()
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Returns current [TrafficStat] for each configured rule.
|
2021-11-14 16:20:55 +00:00
|
|
|
pub fn current_rate_map(&self) -> &HashMap<String, TrafficStat> {
|
|
|
|
&self.current_rate_map
|
|
|
|
}
|
2021-11-13 14:43:56 +00:00
|
|
|
|
2021-11-08 11:40:09 +00:00
|
|
|
fn update_config(&mut self, config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
self.limiter_map.retain(|key, _value| config.sections.contains_key(key));
|
|
|
|
|
|
|
|
let rules: Vec<TrafficControlRule> =
|
|
|
|
config.convert_to_typed_array("rule")?;
|
|
|
|
|
|
|
|
let mut active_rules = Vec::new();
|
|
|
|
|
|
|
|
for rule in rules {
|
|
|
|
|
|
|
|
let entry = self.limiter_map.entry(rule.name.clone()).or_insert((None, None));
|
2021-11-21 09:20:41 +00:00
|
|
|
let limit = &rule.limit;
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
match entry.0 {
|
|
|
|
Some(ref read_limiter) => {
|
2021-11-21 09:20:41 +00:00
|
|
|
match limit.rate_in {
|
2021-11-08 11:40:09 +00:00
|
|
|
Some(rate_in) => {
|
2021-11-18 07:29:22 +00:00
|
|
|
read_limiter.update_rate(
|
|
|
|
rate_in.as_u64(),
|
2021-11-21 09:20:41 +00:00
|
|
|
limit.burst_in.unwrap_or(rate_in).as_u64(),
|
2021-11-18 07:29:22 +00:00
|
|
|
);
|
2021-11-08 11:40:09 +00:00
|
|
|
}
|
|
|
|
None => entry.0 = None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2021-11-21 09:20:41 +00:00
|
|
|
if let Some(rate_in) = limit.rate_in {
|
2021-11-13 16:19:07 +00:00
|
|
|
let name = format!("{}.in", rule.name);
|
|
|
|
let limiter = create_limiter(
|
|
|
|
self.use_shared_memory,
|
|
|
|
&name,
|
2021-11-18 07:29:22 +00:00
|
|
|
rate_in.as_u64(),
|
2021-11-21 09:20:41 +00:00
|
|
|
limit.burst_in.unwrap_or(rate_in).as_u64(),
|
2021-11-13 16:19:07 +00:00
|
|
|
)?;
|
2021-11-13 14:43:56 +00:00
|
|
|
entry.0 = Some(limiter);
|
2021-11-08 11:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match entry.1 {
|
|
|
|
Some(ref write_limiter) => {
|
2021-11-21 09:20:41 +00:00
|
|
|
match limit.rate_out {
|
2021-11-08 11:40:09 +00:00
|
|
|
Some(rate_out) => {
|
2021-11-18 07:29:22 +00:00
|
|
|
write_limiter.update_rate(
|
|
|
|
rate_out.as_u64(),
|
2021-11-21 09:20:41 +00:00
|
|
|
limit.burst_out.unwrap_or(rate_out).as_u64(),
|
2021-11-18 07:29:22 +00:00
|
|
|
);
|
2021-11-08 11:40:09 +00:00
|
|
|
}
|
|
|
|
None => entry.1 = None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2021-11-21 09:20:41 +00:00
|
|
|
if let Some(rate_out) = limit.rate_out {
|
2021-11-13 16:19:07 +00:00
|
|
|
let name = format!("{}.out", rule.name);
|
|
|
|
let limiter = create_limiter(
|
|
|
|
self.use_shared_memory,
|
|
|
|
&name,
|
2021-11-18 07:29:22 +00:00
|
|
|
rate_out.as_u64(),
|
2021-11-21 09:20:41 +00:00
|
|
|
limit.burst_out.unwrap_or(rate_out).as_u64(),
|
2021-11-13 16:19:07 +00:00
|
|
|
)?;
|
2021-11-13 14:43:56 +00:00
|
|
|
entry.1 = Some(limiter);
|
2021-11-08 11:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut timeframe = Vec::new();
|
|
|
|
|
|
|
|
if let Some(ref timefram_list) = rule.timeframe {
|
|
|
|
for duration_str in timefram_list {
|
|
|
|
let duration = parse_daily_duration(duration_str)?;
|
|
|
|
timeframe.push(duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut networks = Vec::new();
|
|
|
|
|
|
|
|
for network in rule.network.iter() {
|
|
|
|
let cidr = match network.parse() {
|
|
|
|
Ok(cidr) => cidr,
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("unable to parse network '{}' - {}", network, err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
networks.push(cidr);
|
|
|
|
}
|
|
|
|
|
|
|
|
active_rules.push(ParsedTcRule { config: rule, networks, timeframe });
|
|
|
|
}
|
|
|
|
|
|
|
|
self.rules = active_rules;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:44:27 +00:00
|
|
|
/// Returns the rate limiter (if any) for the specified peer address.
|
|
|
|
///
|
|
|
|
/// - Rules where timeframe does not match are skipped.
|
|
|
|
/// - Rules with smaller network size have higher priority.
|
|
|
|
///
|
|
|
|
/// Behavior is undefined if more than one rule matches after
|
|
|
|
/// above selection.
|
2021-11-08 11:40:09 +00:00
|
|
|
pub fn lookup_rate_limiter(
|
|
|
|
&self,
|
2022-01-28 14:10:30 +00:00
|
|
|
peer: SocketAddr,
|
2021-11-08 11:40:09 +00:00
|
|
|
now: i64,
|
2021-11-13 14:43:56 +00:00
|
|
|
) -> (&str, Option<Arc<dyn ShareableRateLimit>>, Option<Arc<dyn ShareableRateLimit>>) {
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
let peer_ip = cannonical_ip(peer.ip());
|
|
|
|
|
|
|
|
log::debug!("lookup_rate_limiter: {:?}", peer_ip);
|
|
|
|
|
|
|
|
let now = match TmEditor::with_epoch(now, self.use_utc) {
|
|
|
|
Ok(now) => now,
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("lookup_rate_limiter: TmEditor::with_epoch failed - {}", err);
|
|
|
|
return ("", None, None);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut last_rule_match = None;
|
|
|
|
|
|
|
|
for rule in self.rules.iter() {
|
|
|
|
if !timeframe_match(&rule.timeframe, &now) { continue; }
|
|
|
|
|
|
|
|
if let Some(match_len) = network_match_len(&rule.networks, &peer_ip) {
|
|
|
|
match last_rule_match {
|
|
|
|
None => last_rule_match = Some((rule, match_len)),
|
|
|
|
Some((_, last_len)) => {
|
|
|
|
if match_len > last_len {
|
|
|
|
last_rule_match = Some((rule, match_len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match last_rule_match {
|
|
|
|
Some((rule, _)) => {
|
|
|
|
match self.limiter_map.get(&rule.config.name) {
|
|
|
|
Some((read_limiter, write_limiter)) => {
|
|
|
|
(&rule.config.name, read_limiter.clone(), write_limiter.clone())
|
|
|
|
}
|
|
|
|
None => ("", None, None), // should never happen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => ("", None, None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const fn make_test_time(mday: i32, hour: i32, min: i32) -> i64 {
|
|
|
|
(mday*3600*24 + hour*3600 + min*60) as i64
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn testnetwork_match() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let networks = ["192.168.2.1/24", "127.0.0.0/8"];
|
|
|
|
let networks: Vec<IpInet> = networks.iter().map(|n| n.parse().unwrap()).collect();
|
|
|
|
|
|
|
|
assert_eq!(network_match_len(&networks, &"192.168.2.1".parse()?), Some(24));
|
|
|
|
assert_eq!(network_match_len(&networks, &"192.168.2.254".parse()?), Some(24));
|
|
|
|
assert_eq!(network_match_len(&networks, &"192.168.3.1".parse()?), None);
|
|
|
|
assert_eq!(network_match_len(&networks, &"127.1.1.0".parse()?), Some(8));
|
|
|
|
assert_eq!(network_match_len(&networks, &"128.1.1.0".parse()?), None);
|
|
|
|
|
|
|
|
let networks = ["0.0.0.0/0"];
|
|
|
|
let networks: Vec<IpInet> = networks.iter().map(|n| n.parse().unwrap()).collect();
|
|
|
|
assert_eq!(network_match_len(&networks, &"127.1.1.0".parse()?), Some(0));
|
|
|
|
assert_eq!(network_match_len(&networks, &"192.168.2.1".parse()?), Some(0));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rule_match() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let config_data = "
|
|
|
|
rule: rule1
|
|
|
|
comment my test rule
|
|
|
|
network 192.168.2.0/24
|
|
|
|
rate-in 50000000
|
|
|
|
rate-out 50000000
|
|
|
|
timeframe 8-12
|
|
|
|
timeframe 14-16
|
|
|
|
|
|
|
|
rule: rule2
|
|
|
|
network 192.168.2.35/32
|
|
|
|
network 127.0.0.1/8
|
|
|
|
rate-in 150000000
|
|
|
|
rate-out 150000000
|
|
|
|
timeframe 18-20
|
|
|
|
|
|
|
|
rule: somewhere
|
|
|
|
network 0.0.0.0/0
|
|
|
|
rate-in 100000000
|
|
|
|
rate-out 100000000
|
|
|
|
";
|
|
|
|
let config = pbs_config::traffic_control::CONFIG.parse("testconfig", config_data)?;
|
|
|
|
|
|
|
|
let mut cache = TrafficControlCache::new();
|
|
|
|
cache.use_utc = true;
|
2021-11-14 07:05:40 +00:00
|
|
|
cache.use_shared_memory = false; // avoid permission problems in test environment
|
2021-11-08 11:40:09 +00:00
|
|
|
|
|
|
|
cache.update_config(&config)?;
|
|
|
|
|
|
|
|
const THURSDAY_80_00: i64 = make_test_time(0, 8, 0);
|
|
|
|
const THURSDAY_15_00: i64 = make_test_time(0, 15, 0);
|
|
|
|
const THURSDAY_19_00: i64 = make_test_time(0, 19, 0);
|
|
|
|
|
|
|
|
let local = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1234);
|
|
|
|
let gateway = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 2, 1)), 1234);
|
|
|
|
let private = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 2, 35)), 1234);
|
|
|
|
let somewhere = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)), 1234);
|
|
|
|
|
2022-01-28 14:10:30 +00:00
|
|
|
let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(somewhere, THURSDAY_80_00);
|
2021-11-08 11:40:09 +00:00
|
|
|
assert_eq!(rule, "somewhere");
|
|
|
|
assert!(read_limiter.is_some());
|
|
|
|
assert!(write_limiter.is_some());
|
|
|
|
|
2022-01-28 14:10:30 +00:00
|
|
|
let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(local, THURSDAY_19_00);
|
2021-11-08 11:40:09 +00:00
|
|
|
assert_eq!(rule, "rule2");
|
|
|
|
assert!(read_limiter.is_some());
|
|
|
|
assert!(write_limiter.is_some());
|
|
|
|
|
2022-01-28 14:10:30 +00:00
|
|
|
let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(gateway, THURSDAY_15_00);
|
2021-11-08 11:40:09 +00:00
|
|
|
assert_eq!(rule, "rule1");
|
|
|
|
assert!(read_limiter.is_some());
|
|
|
|
assert!(write_limiter.is_some());
|
|
|
|
|
2022-01-28 14:10:30 +00:00
|
|
|
let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(gateway, THURSDAY_19_00);
|
2021-11-08 11:40:09 +00:00
|
|
|
assert_eq!(rule, "somewhere");
|
|
|
|
assert!(read_limiter.is_some());
|
|
|
|
assert!(write_limiter.is_some());
|
|
|
|
|
2022-01-28 14:10:30 +00:00
|
|
|
let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(private, THURSDAY_19_00);
|
2021-11-08 11:40:09 +00:00
|
|
|
assert_eq!(rule, "rule2");
|
|
|
|
assert!(read_limiter.is_some());
|
|
|
|
assert!(write_limiter.is_some());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|