egress-pin: a sweep says why a port stays unbound, which veths are off the bridge and which machines it could not read
A port nobody owns, a vb- veth that never joined the bridge and a machine whose state file or namespace can't be read all left the bridge sweep silent, so a container stayed unbound for 11 hours with nothing in the journal. A timer run now prints one line for each, after the finding has stood for 60 s and once until it clears, kept in <state file>.diag. Nothing it binds changes.
This commit is contained in:
parent
62551e004e
commit
94dd24fa99
1 changed files with 658 additions and 38 deletions
|
|
@ -11,7 +11,7 @@
|
|||
//! container uses. A port nobody owns stays unbound, and the table drops
|
||||
//! everything it sends. With `<port>` the command first waits for that port
|
||||
//! to be bound or to vanish. udev runs it per new port, a timer for the ports
|
||||
//! still unbound.
|
||||
//! still unbound; that run also says why one stays ([`crate::egress_diag`]).
|
||||
//! - `egress-lease <action> [<mac> <ip> [<host>]]` is dnsmasq's `dhcp-script`:
|
||||
//! it keeps the table's set of leased MAC and IPv4 pairs in step.
|
||||
//! - `egress-whois` is squid's external ACL helper: it answers a source address
|
||||
|
|
@ -40,6 +40,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
|||
|
||||
use anyhow::{Context as _, Result, bail};
|
||||
|
||||
use crate::egress_diag;
|
||||
use crate::egress_ech::same_host;
|
||||
|
||||
/// The sub-command squid runs to learn which agent holds a source address.
|
||||
|
|
@ -197,6 +198,20 @@ pub(crate) struct Resolution {
|
|||
pub(crate) unbound: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// The machine that owns `port`, and the MAC of its link: the link whose index
|
||||
/// is the port's peer index and whose own peer index is the port's index.
|
||||
pub(crate) fn owner_of<'a>(
|
||||
port: &HostPort,
|
||||
machines: &'a [(String, Vec<NsLink>)],
|
||||
) -> Option<(&'a str, Mac)> {
|
||||
machines.iter().find_map(|(machine, links)| {
|
||||
links
|
||||
.iter()
|
||||
.find(|l| l.ifindex == port.peer_index && l.peer_index == Some(port.ifindex))
|
||||
.map(|l| (machine.as_str(), l.mac))
|
||||
})
|
||||
}
|
||||
|
||||
/// Bind each port to the MAC of the link at the other end of its veth pair.
|
||||
///
|
||||
/// `machines` holds each registered container's links. `earlier` holds the
|
||||
|
|
@ -221,13 +236,7 @@ pub(crate) fn resolve(
|
|||
kept.push(pin.clone());
|
||||
continue;
|
||||
}
|
||||
let owner = machines.iter().find_map(|(machine, links)| {
|
||||
links
|
||||
.iter()
|
||||
.find(|l| l.ifindex == port.peer_index && l.peer_index == Some(port.ifindex))
|
||||
.map(|l| (machine.as_str(), l.mac))
|
||||
});
|
||||
match owner {
|
||||
match owner_of(port, machines) {
|
||||
None => out
|
||||
.unbound
|
||||
.push((port.name.clone(), "no registered container owns it".into())),
|
||||
|
|
@ -295,7 +304,7 @@ pub(crate) fn parse_state(text: &str) -> Vec<Pin> {
|
|||
}
|
||||
|
||||
/// Whether a name is safe to put into an nft script as a quoted string.
|
||||
fn is_plain_name(name: &str) -> bool {
|
||||
pub(crate) fn is_plain_name(name: &str) -> bool {
|
||||
!name.is_empty()
|
||||
&& name.len() < 16
|
||||
&& name
|
||||
|
|
@ -705,18 +714,37 @@ pub(crate) fn parse_ports(json: &str) -> Result<Vec<HostPort>> {
|
|||
.collect())
|
||||
}
|
||||
|
||||
/// The leader of a machine's state file, or `None` when it is not a container.
|
||||
pub(crate) fn container_leader(state: &str) -> Option<u32> {
|
||||
let mut leader = None;
|
||||
let mut container = false;
|
||||
for line in state.lines() {
|
||||
match line.split_once('=') {
|
||||
Some(("CLASS", "container")) => container = true,
|
||||
Some(("LEADER", pid)) => leader = pid.parse().ok(),
|
||||
_ => {}
|
||||
}
|
||||
/// The leader a machine's state file names, whatever the machine is.
|
||||
fn state_leader(state: &str) -> Option<u32> {
|
||||
state
|
||||
.lines()
|
||||
.find_map(|line| line.strip_prefix("LEADER=")?.parse().ok())
|
||||
}
|
||||
|
||||
/// The leader of a machine's state file, or why the machine is not read.
|
||||
pub(crate) fn container_leader(state: &str) -> Result<u32, &'static str> {
|
||||
if !state.lines().any(|line| line == "CLASS=container") {
|
||||
return Err("it is not a container");
|
||||
}
|
||||
leader.filter(|_| container)
|
||||
state_leader(state).ok_or("its state file has no usable LEADER")
|
||||
}
|
||||
|
||||
/// A registered machine a pass did not read, and why.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Skip {
|
||||
pub(crate) machine: String,
|
||||
/// The leader its state file names: a machine started again has another,
|
||||
/// and counts as new.
|
||||
pub(crate) leader: Option<u32>,
|
||||
pub(crate) why: String,
|
||||
}
|
||||
|
||||
/// What reading the registered machines found.
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct Machines {
|
||||
/// Each container whose namespace could be read, with its links.
|
||||
pub(crate) links: Vec<(String, Vec<NsLink>)>,
|
||||
pub(crate) skipped: Vec<Skip>,
|
||||
}
|
||||
|
||||
/// Run `program` with `args`, `input` on its stdin, and collect what it
|
||||
|
|
@ -815,22 +843,38 @@ fn nft(script: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// The links of every registered container, by machine name. A machine whose
|
||||
/// namespace cannot be read is reported and skipped.
|
||||
fn machine_links(dir: &Path) -> Vec<(String, Vec<NsLink>)> {
|
||||
/// The links of every registered container, by machine name. A machine that
|
||||
/// cannot be read is listed with the reason, not reported: a sweep says it
|
||||
/// once ([`crate::egress_diag`]).
|
||||
fn machine_links(dir: &Path) -> Machines {
|
||||
let mut machines = Machines::default();
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return Vec::new();
|
||||
return machines;
|
||||
};
|
||||
let mut machines = Vec::new();
|
||||
for entry in entries.flatten() {
|
||||
let Some(name) = entry.file_name().to_str().map(str::to_string) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(state) = std::fs::read_to_string(entry.path()) else {
|
||||
continue;
|
||||
let mut skip = |leader: Option<u32>, why: String| {
|
||||
machines.skipped.push(Skip {
|
||||
machine: name.clone(),
|
||||
leader,
|
||||
why,
|
||||
});
|
||||
};
|
||||
let Some(leader) = container_leader(&state) else {
|
||||
continue;
|
||||
let state = match std::fs::read_to_string(entry.path()) {
|
||||
Ok(state) => state,
|
||||
Err(e) => {
|
||||
skip(None, format!("its state file cannot be read: {e}"));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let leader = match container_leader(&state) {
|
||||
Ok(leader) => leader,
|
||||
Err(why) => {
|
||||
skip(state_leader(&state), why.into());
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let links = output_of(
|
||||
"nsenter",
|
||||
|
|
@ -846,8 +890,8 @@ fn machine_links(dir: &Path) -> Vec<(String, Vec<NsLink>)> {
|
|||
)
|
||||
.and_then(|json| parse_links(&json));
|
||||
match links {
|
||||
Ok(links) => machines.push((name, links)),
|
||||
Err(e) => eprintln!("{PIN}: machine {name}: {e:#}"),
|
||||
Ok(links) => machines.links.push((name.clone(), links)),
|
||||
Err(e) => skip(Some(leader), format!("its namespace cannot be read: {e:#}")),
|
||||
}
|
||||
}
|
||||
machines
|
||||
|
|
@ -868,6 +912,10 @@ pub(crate) fn ports_to_resolve<'a>(ports: &'a [HostPort], earlier: &[Pin]) -> Ve
|
|||
/// What a pass found out.
|
||||
struct Survey {
|
||||
earlier: Vec<Pin>,
|
||||
/// The bridge's ports.
|
||||
ports: Vec<HostPort>,
|
||||
/// The machines, read only when a port has no binding.
|
||||
machines: Machines,
|
||||
resolution: Resolution,
|
||||
}
|
||||
|
||||
|
|
@ -881,13 +929,15 @@ fn survey(bridge: &str) -> Result<Survey> {
|
|||
let ports = parse_ports(&output_of("ip", &["-j", "link", "show", "master", bridge])?)?;
|
||||
let earlier = parse_state(&std::fs::read_to_string(state_path()).unwrap_or_default());
|
||||
let machines = if ports_to_resolve(&ports, &earlier).is_empty() {
|
||||
Vec::new()
|
||||
Machines::default()
|
||||
} else {
|
||||
machine_links(Path::new(MACHINES_DIR))
|
||||
};
|
||||
let resolution = resolve(&ports, &machines, &earlier);
|
||||
let resolution = resolve(&ports, &machines.links, &earlier);
|
||||
Ok(Survey {
|
||||
earlier,
|
||||
ports,
|
||||
machines,
|
||||
resolution,
|
||||
})
|
||||
}
|
||||
|
|
@ -970,6 +1020,28 @@ fn report(resolution: &Resolution, which: Report<'_>) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Say what a sweep leaves as it is: a port that stays unbound, a `vb-` veth
|
||||
/// that is not on the bridge, a machine it could not read. A line is printed
|
||||
/// once, after it has stood for a while ([`egress_diag::advance`]). Changes
|
||||
/// nothing, and a failure here is a warning: the pass has already bound what it
|
||||
/// could.
|
||||
fn diagnose(bridge: &str, survey: &Survey) {
|
||||
let unenslaved = output_of("ip", &["-j", "link", "show", "type", "veth"])
|
||||
.and_then(|json| egress_diag::parse_unenslaved(&json, bridge))
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{PIN}: list the veths: {e:#}");
|
||||
Vec::new()
|
||||
});
|
||||
let findings = egress_diag::findings(&egress_diag::Pass {
|
||||
bridge,
|
||||
ports: &survey.ports,
|
||||
unbound: &survey.resolution.unbound,
|
||||
machines: &survey.machines,
|
||||
unenslaved: &unenslaved,
|
||||
});
|
||||
egress_diag::say(&findings, &format!("{}.diag", state_path()), now_secs());
|
||||
}
|
||||
|
||||
/// `egress-pin [<port>]`: one pass, or with `<port>` passes until that port is
|
||||
/// bound or gone. Without `<port>` it prints only what it binds: the timer
|
||||
/// runs it again and again for the ports still unbound.
|
||||
|
|
@ -991,6 +1063,9 @@ fn pin(args: &[String]) -> Result<()> {
|
|||
connection_list().as_deref(),
|
||||
))?;
|
||||
commit(&survey)?;
|
||||
if wait_for.is_none() {
|
||||
diagnose(&bridge, &survey);
|
||||
}
|
||||
survey
|
||||
};
|
||||
let Some(port) = wait_for else {
|
||||
|
|
@ -1002,6 +1077,9 @@ fn pin(args: &[String]) -> Result<()> {
|
|||
if bound || !present || Instant::now() >= deadline {
|
||||
report(&survey.resolution, Report::Port(port));
|
||||
if present && !bound {
|
||||
for skip in &survey.machines.skipped {
|
||||
eprintln!("{PIN}: {}", egress_diag::skip_line(skip));
|
||||
}
|
||||
bail!("port {port} stays unbound");
|
||||
}
|
||||
return Ok(());
|
||||
|
|
@ -1837,12 +1915,59 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn only_a_container_with_a_leader_is_read() {
|
||||
assert_eq!(container_leader("CLASS=container\nLEADER=1234\n"), Ok(1234));
|
||||
assert_eq!(
|
||||
container_leader("CLASS=container\nLEADER=1234\n"),
|
||||
Some(1234)
|
||||
container_leader("CLASS=vm\nLEADER=1234\n"),
|
||||
Err("it is not a container")
|
||||
);
|
||||
assert_eq!(
|
||||
container_leader("CLASS=container\n"),
|
||||
Err("its state file has no usable LEADER")
|
||||
);
|
||||
assert_eq!(
|
||||
container_leader("CLASS=container\nLEADER=x\n"),
|
||||
Err("its state file has no usable LEADER")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_machine_that_cannot_be_read_is_listed_with_the_reason() {
|
||||
let dir = std::env::temp_dir().join(format!("hive-priv-machines-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(dir.join("unreadable")).unwrap();
|
||||
std::fs::write(dir.join("vm"), "CLASS=vm\nLEADER=7\n").unwrap();
|
||||
std::fs::write(dir.join("no-leader"), "CLASS=container\n").unwrap();
|
||||
let machines = machine_links(&dir);
|
||||
let mut skipped = machines.skipped.clone();
|
||||
skipped.sort_by(|a, b| a.machine.cmp(&b.machine));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
assert!(machines.links.is_empty());
|
||||
assert_eq!(skipped.len(), 3);
|
||||
assert_eq!(
|
||||
skipped[0],
|
||||
Skip {
|
||||
machine: "no-leader".into(),
|
||||
leader: None,
|
||||
why: "its state file has no usable LEADER".into(),
|
||||
}
|
||||
);
|
||||
assert_eq!(skipped[1].machine, "unreadable");
|
||||
assert_eq!(skipped[1].leader, None);
|
||||
assert!(
|
||||
skipped[1]
|
||||
.why
|
||||
.starts_with("its state file cannot be read: "),
|
||||
"{}",
|
||||
skipped[1].why
|
||||
);
|
||||
assert_eq!(
|
||||
skipped[2],
|
||||
Skip {
|
||||
machine: "vm".into(),
|
||||
leader: Some(7),
|
||||
why: "it is not a container".into(),
|
||||
}
|
||||
);
|
||||
assert_eq!(container_leader("CLASS=vm\nLEADER=1234\n"), None);
|
||||
assert_eq!(container_leader("CLASS=container\n"), None);
|
||||
}
|
||||
|
||||
fn lock_path(name: &str) -> String {
|
||||
|
|
|
|||
Loading…
Reference in a new issue