2021-11-06 17:46:58 +00:00
|
|
|
//! Traffic Control Settings (Network rate limits)
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use anyhow::Error;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
use proxmox_schema::{ApiType, Schema};
|
|
|
|
|
|
|
|
use pbs_api_types::{TrafficControlRule, TRAFFIC_CONTROL_ID_SCHEMA};
|
|
|
|
|
|
|
|
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
use crate::ConfigVersionCache;
|
2021-11-06 17:46:58 +00:00
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
/// Static [`SectionConfig`] to access parser/writer functions.
|
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
let mut config = SectionConfig::new(&TRAFFIC_CONTROL_ID_SCHEMA);
|
|
|
|
|
|
|
|
let obj_schema = match TrafficControlRule::API_SCHEMA {
|
2021-11-21 09:20:41 +00:00
|
|
|
Schema::AllOf(ref allof_schema) => allof_schema,
|
2021-11-06 17:46:58 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let plugin = SectionConfigPlugin::new("rule".to_string(), Some("name".to_string()), obj_schema);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration file name
|
|
|
|
pub const TRAFFIC_CONTROL_CFG_FILENAME: &str = "/etc/proxmox-backup/traffic-control.cfg";
|
|
|
|
/// Lock file name (used to prevent concurrent access)
|
|
|
|
pub const TRAFFIC_CONTROL_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.traffic-control.lck";
|
|
|
|
|
|
|
|
/// Get exclusive lock
|
|
|
|
pub fn lock_config() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(TRAFFIC_CONTROL_CFG_LOCKFILE, None, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read and parse the configuration file
|
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
|
|
|
|
|
|
|
let content = proxmox::tools::fs::file_read_optional_string(TRAFFIC_CONTROL_CFG_FILENAME)?
|
|
|
|
.unwrap_or_else(|| "".to_string());
|
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(TRAFFIC_CONTROL_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save the configuration file
|
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
let raw = CONFIG.write(TRAFFIC_CONTROL_CFG_FILENAME, &config)?;
|
2021-11-08 11:37:15 +00:00
|
|
|
replace_backup_config(TRAFFIC_CONTROL_CFG_FILENAME, raw.as_bytes())?;
|
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
// increase traffic control version
|
2021-11-08 11:37:15 +00:00
|
|
|
// We use this in TrafficControlCache
|
2021-11-12 17:44:28 +00:00
|
|
|
let version_cache = ConfigVersionCache::new()?;
|
|
|
|
version_cache.increase_traffic_control_generation();
|
2021-11-08 11:37:15 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-11-06 17:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_traffic_control_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test1() -> Result<(), Error> {
|
|
|
|
let content = "rule: rule1
|
|
|
|
comment localnet at working hours
|
|
|
|
network 192.168.2.0/24
|
|
|
|
network 192.168.3.0/24
|
|
|
|
rate-in 500000
|
|
|
|
timeframe mon..wed 8:00-16:30
|
|
|
|
timeframe fri 9:00-12:00
|
|
|
|
";
|
|
|
|
let data = CONFIG.parse(TRAFFIC_CONTROL_CFG_FILENAME, &content)?;
|
|
|
|
eprintln!("GOT {:?}", data);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|