Decouple sysctl runtime validation and modprobe dependencies
This commit is contained in:
@@ -287,6 +287,25 @@ impl Config {
|
||||
self.check_host_tools()
|
||||
}
|
||||
|
||||
pub fn validate_sysctl_tuning_runtime(&self) -> anyhow::Result<()> {
|
||||
self.validate_config()?;
|
||||
|
||||
if !self.sysctl_tuning.enabled {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !tool_exists("sysctl") {
|
||||
anyhow::bail!("required host tool not found in PATH: sysctl");
|
||||
}
|
||||
|
||||
let tcp_bbr_mode = self.sysctl_tuning.tcp_bbr.trim().to_ascii_lowercase();
|
||||
if tcp_bbr_mode == "require" && !tool_exists("modprobe") {
|
||||
anyhow::bail!("required host tool not found in PATH: modprobe");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_all(&self) -> anyhow::Result<()> {
|
||||
self.validate_config()?;
|
||||
self.validate_runtime()?;
|
||||
@@ -299,12 +318,15 @@ impl Config {
|
||||
anyhow::bail!("required host tool not found in PATH: nsenter");
|
||||
}
|
||||
required.push("nsenter");
|
||||
let tcp_bbr_mode = self.sysctl_tuning.tcp_bbr.trim().to_ascii_lowercase();
|
||||
if self.sysctl_tuning.enabled {
|
||||
required.push("sysctl");
|
||||
}
|
||||
if self.sysctl_tuning.enabled
|
||||
&& self.sysctl_tuning.tcp_bbr.trim().to_ascii_lowercase() == "require"
|
||||
{
|
||||
if requires_modprobe(
|
||||
self.traffic.enabled,
|
||||
self.sysctl_tuning.enabled,
|
||||
&tcp_bbr_mode,
|
||||
) {
|
||||
required.push("modprobe");
|
||||
}
|
||||
if self.anti_torrent.enabled {
|
||||
@@ -328,6 +350,9 @@ impl Config {
|
||||
]);
|
||||
}
|
||||
|
||||
required.sort_unstable();
|
||||
required.dedup();
|
||||
|
||||
for tool in required {
|
||||
if !tool_exists(tool) {
|
||||
anyhow::bail!("required host tool not found in PATH: {tool}");
|
||||
@@ -383,6 +408,10 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
fn requires_modprobe(traffic_enabled: bool, sysctl_enabled: bool, tcp_bbr_mode: &str) -> bool {
|
||||
traffic_enabled || (sysctl_enabled && tcp_bbr_mode == "require")
|
||||
}
|
||||
|
||||
fn parse_hhmm(v: &str) -> anyhow::Result<()> {
|
||||
let mut s = v.split(':');
|
||||
let h = s
|
||||
@@ -707,4 +736,25 @@ swappiness = 1
|
||||
assert!(cfg.sysctl_tuning.apply_on_daemon_start);
|
||||
assert_eq!(cfg.sysctl_tuning.tcp_bbr, "auto");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_sysctl_tuning_runtime_skips_modprobe_for_auto_when_disabled() {
|
||||
let mut cfg = base_config();
|
||||
cfg.sysctl_tuning.enabled = false;
|
||||
cfg.sysctl_tuning.tcp_bbr = "auto".into();
|
||||
assert!(cfg.validate_sysctl_tuning_runtime().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_modprobe_when_traffic_enabled() {
|
||||
assert!(requires_modprobe(true, true, "auto"));
|
||||
assert!(requires_modprobe(true, false, "off"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_modprobe_for_sysctl_require_mode() {
|
||||
assert!(requires_modprobe(false, true, "require"));
|
||||
assert!(!requires_modprobe(false, true, "auto"));
|
||||
assert!(!requires_modprobe(false, true, "off"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user