Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions crates/minimald/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,19 +894,29 @@ mod tests {

#[test]
fn allocator_exhausts_a_tiny_subnet() {
// /29 => 8 addresses; network, gateway, daemon, host-alias, broadcast
// reserved, leaving exactly three allocatable hosts (.2 through .4).
// The run splits at its midpoint, so this daemon's reserve is .2
// alone and the host's hand-out run is .3 and .4 — exhaustion is
// explicit on each side, with no wrap and no reuse.
// /29 => 8 addresses; network, gateway, proxy, daemon, host-alias,
// broadcast reserved, leaving exactly two allocatable hosts (.2 and
// .3). The run splits at its midpoint, so this daemon's reserve is .2
// alone and the host's hand-out run is .3 alone — exhaustion is
// explicit on each side, with no wrap and no reuse. The proxy's
// address (.4, `broadcast - 3`) is infrastructure above the run, so
// it is refused as a handed address, exactly as one past the run
// would be.
let subnet = SwitchSubnet::new(Ipv4Addr::new(10, 0, 0, 0), 29).unwrap();
assert_eq!(
subnet.box_egress_proxy_address(),
Ipv4Addr::new(10, 0, 0, 4)
);
let mut a = IpAllocator::new(subnet);
assert_eq!(a.allocate().unwrap().ip, Ipv4Addr::new(10, 0, 0, 2));
assert!(matches!(a.allocate(), Err(NetError::SubnetExhausted(_))));
// The reserve bounds only the daemon's draws: the run's remainder is
// still there for the host's hand-outs, none of them ever drawn.
a.hand(Ipv4Addr::new(10, 0, 0, 3)).unwrap();
a.hand(Ipv4Addr::new(10, 0, 0, 4)).unwrap();
assert!(matches!(
a.hand(Ipv4Addr::new(10, 0, 0, 4)),
Err(NetError::HandedAddressOutsidePlan { .. })
));
assert!(matches!(
a.hand(Ipv4Addr::new(10, 0, 0, 3)),
Err(NetError::HandedAddressCollision { .. })
Expand Down Expand Up @@ -940,8 +950,8 @@ mod tests {
"both sub-runs are inside the plan's PTask run, and neither is empty"
);
// A tiny subnet splits in half, so both sides keep at least one
// address: a /29's three PTask addresses give the reserve .2 and the
// hand-out run .3 and .4.
// address: a /29's two PTask addresses give the reserve .2 and the
// hand-out run .3 alone (the proxy at .4 sits above the run).
let tiny = SwitchSubnet::new(Ipv4Addr::new(10, 0, 0, 0), 29).unwrap();
assert_eq!(
self_allocation_run(tiny),
Expand Down Expand Up @@ -1104,6 +1114,9 @@ mod tests {
assert!(cfg.contains("subnet: \"100.64.0.0/16\""));
assert!(cfg.contains("gatewayIP: \"100.64.0.1\""));
assert!(cfg.contains(&format!("\"{}\": \"{}\"", lease.ip, lease.mac)));
// NET-132: the box egress proxy address is infrastructure and must not be
// handled by gvproxy's NAT or virtual-IP machinery.
assert!(!cfg.contains("100.64.255.252"));
// Host alias is NAT'd to loopback and never allocated.
assert!(cfg.contains("\"100.64.255.254\": \"127.0.0.1\""));
// NET-003: the static zone entry the bundle's switch-configuration copy
Expand Down
8 changes: 7 additions & 1 deletion crates/minvmd/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,13 @@ fn run_foreground() -> Result<()> {
&boxes,
) {
Ok(gvproxy) => {
tracing::info!(pid = gvproxy.pid(), "host gvproxy switch up");
let subnet = boxes.subnet();
tracing::info!(
pid = gvproxy.pid(),
proxy_ip = %subnet.box_egress_proxy_address(),
proxy_mac = %subnet.bep_mac(),
"host gvproxy switch up; box egress proxy peer started",
);
Comment on lines +511 to +516

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT · style · confidence 0.75 · in run_foreground

The peer-start event is announced twice for the same occurrence. net.rs:807-811 already logs box egress proxy peer started from inside HostGvproxy's supervising runtime at the moment the peer is up, and run_foreground logs host gvproxy switch up; box egress proxy peer started immediately when HostGvproxy::spawn returns — the same event, two info lines per boot. The run.rs line is redundant with the net.rs one and can drift if the peer-start timing changes (e.g. if the peer is later moved to a path that doesn't run in HostGvproxy). Drop the proxy_ip/proxy_mac/peer phrasing from the run.rs line and keep the net.rs one as the single authoritative announcement.

What the reviewer checked

run.rs:515 "host gvproxy switch up; box egress proxy peer started", / net.rs:810 "box egress proxy peer started",

Hand this to an agent
Act on a code-review finding.

File:       crates/minvmd/src/cmd/run.rs
Lines:      511-516
Category:   style
Severity:   nit (reviewer confidence 0.75)

Finding:
The peer-start event is announced twice for the same occurrence. net.rs:807-811 already logs `box egress proxy peer started` from inside `HostGvproxy`'s supervising runtime at the moment the peer is up, and `run_foreground` logs `host gvproxy switch up; box egress proxy peer started` immediately when `HostGvproxy::spawn` returns — the same event, two info lines per boot. The run.rs line is redundant with the net.rs one and can drift if the peer-start timing changes (e.g. if the peer is later moved to a path that doesn't run in `HostGvproxy`). Drop the `proxy_ip`/`proxy_mac`/peer phrasing from the run.rs line and keep the net.rs one as the single authoritative announcement.

First verify this against the code yourself. It is model output and
may be wrong. If it is wrong, or the tradeoff was deliberate, say so
and change nothing.

👍 useful · 👎 noise — react to tell the reviewer. Replying works too, and says more.

Some(gvproxy)
}
// An own-IP VM cannot work without the switch: fail loudly. A
Expand Down
137 changes: 134 additions & 3 deletions crates/minvmd/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub use shuttle::{VSOCK_GVPROXY_SHUTTLE_PORT, resolve_gate_sock, resolve_switch_
mod baseline;
pub use baseline::{NodeBaselinePhase, NodePlaneBaseline};
pub(crate) mod bep_host;
pub use bep_host::{BepDevice, BepDeviceEnds, BepHost};
pub use bep_host::{BepDevice, BepDeviceEnds, BepHost, BepPeer};

/// Default time to wait for gvproxy to exit on SIGTERM before escalating to
/// SIGKILL.
Expand Down Expand Up @@ -718,6 +718,7 @@ impl HostGvproxy {
// its rows were compiled against — so a lease the gate checks a
// frame's source by is one the switch actually routes.
.with_subnet(registry.subnet());
let subnet = registry.subnet();
let table = registry.table();
// The node-plane baseline set: the helper's built-in enumeration of
// the categories the in-VM daemon's own traffic may reach (NET-130),
Expand All @@ -726,7 +727,7 @@ impl HostGvproxy {
// by it, beside the boxes' rows; announced — the shipped posture —
// the allow-all interim node row still does, and the gate's
// start-up line names the posture deciding.
let baseline = NodePlaneBaseline::built_in(registry.subnet());
let baseline = NodePlaneBaseline::built_in(subnet);
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel::<()>();
let (ready_tx, ready_rx) = std::sync::mpsc::channel::<io::Result<u32>>();

Expand Down Expand Up @@ -789,6 +790,26 @@ impl HostGvproxy {
drop(ready_tx.send(Err(e)));
return;
}
// Start the Box Egress Proxy host stack peer after the switch
// socket is known to accept connections: the peer owns the
// proxy's infrastructure address on the subnet, speaks the
// same length-framed Ethernet the switch socket carries, and
// stops when this runtime drops at switch teardown.
let _bep_peer = match BepPeer::spawn(&sock, subnet).await {
Ok(peer) => peer,
Err(e) => {
tracing::error!(error = %e, "failed to start box egress proxy peer");
switch.stop().await;
drop(ready_tx.send(Err(e)));
return;
}
};
tracing::info!(
proxy_ip = %subnet.box_egress_proxy_address(),
proxy_mac = %subnet.bep_mac(),
"box egress proxy peer started",
);

if ready_tx.send(Ok(pid)).is_err() {
// Caller went away before learning the PID; tear down.
switch.stop().await;
Expand All @@ -799,14 +820,18 @@ impl HostGvproxy {
tokio::select! {
_ = stop_rx => {
switch.stop().await;
drop(_bep_peer);
drop(_gate);
}
status = exit.recv() => {
tracing::error!(
pid,
code = status.and_then(|s| s.code()),
"host gvproxy switch exited unexpectedly",
);
// gvproxy is already gone; drop the handle (no signal).
// gvproxy is already gone; drop the handles (no signal).
drop(_bep_peer);
drop(_gate);
drop(switch);
}
}
Expand Down Expand Up @@ -1055,6 +1080,112 @@ mod tests {
assert!(yaml.contains("{}"));
}

/// NET-132: one source for the run's end. The last PTask address and the
/// proxy's own address both come from the `switch` crate's plan, and every
/// consumer here reads them there — the lease run the registry hands box
/// addresses out of, the gate's answer to which sources the plan could
/// ever have handed out, and the static-lease pool the rendered switch
/// config serves. The three are pinned against each other here, so moving
/// the run's end or the proxy address fails this test instead of leaving a
/// consumer behind. `minimald`'s mirror of the same run — its
/// self-allocation reserve, the run's lower half — is pinned by its own
/// test against the same `switch` crate, and the switch crate's own test
/// pins the proxy address out of gvproxy's NAT and virtual-IP tables.
#[test]
fn lease_run_gate_run_and_rendered_pool_agree() {
let subnet = SwitchSubnet::default();
let first = Ipv4Addr::from(subnet.first_ptask());
let last = Ipv4Addr::from(subnet.last_ptask());

// The reserved run above the lease pool, in order: the proxy address
// the host-side peer owns (NET-132), the daemon's own tap, and the
// host alias gvproxy NATs — all above the run's end, all from the one
// plan.
assert!(
u32::from(last) < u32::from(subnet.box_egress_proxy_address()),
"the proxy address sits above the lease pool"
);
assert!(u32::from(subnet.box_egress_proxy_address()) < u32::from(subnet.daemon_ip()));
assert!(u32::from(subnet.daemon_ip()) < u32::from(subnet.host_alias()));

// The gate's run: the one set of sources the plan could ever hand a
// box — both ends of the lease run inside it, every reserved address
// and everything off the subnet outside it.
let registry = crate::box_registry::BoxRegistry::new(subnet);
let table = registry.table();
assert!(
table.is_allocatable(first.octets()),
"the run's first address is a source the gate's run admits"
);
assert!(
table.is_allocatable(last.octets()),
"the run's last address is a source the gate's run admits"
);
for outside in [
subnet.network(),
subnet.gateway(),
subnet.box_egress_proxy_address(),
subnet.daemon_ip(),
subnet.host_alias(),
subnet.broadcast(),
Ipv4Addr::new(203, 0, 113, 7),
] {
assert!(
!table.is_allocatable(outside.octets()),
"the gate's run never admits the reserved {outside}"
);
}

// The lease book's range: the registry hands a registered box an
// address from the same run — inside its ends, never a reserved one.
for name in ["web", "db"] {
let row = registry
.register_client_box(crate::box_registry::ClientBoxSpec {
name: name.to_string(),
ingress_ports: Vec::new(),
egress: None,
})
.expect("the default plan has hand-out addresses");
let handed = row.switch_addr();
assert!(
first <= handed && handed <= last,
"the lease book hands {handed} from inside the plan's run"
);
}

// The rendered pool: the config's static-lease table carries exactly
// the addresses it is handed — the run's own ends here — on the same
// subnet, and none of the reserved run above it. The lease map is the
// tail of the rendered YAML (`dhcpStaticLeases:` is its last key), so
// the alias's own NAT and DNS lines do not read as leases.
let pool = [
(first, MacAddr::for_switch_ip(first)),
(last, MacAddr::for_switch_ip(last)),
];
let yaml = render_gvproxy_config(subnet, &pool);
assert!(yaml.contains(&format!("subnet: \"{subnet}\"")));
let lease_map = yaml
.split("dhcpStaticLeases:\n")
.nth(1)
.expect("the rendered config carries a static-lease map");
for (ip, mac) in pool {
assert!(lease_map.contains(&format!(" \"{ip}\": \"{mac}\"")));
}
for reserved in [subnet.daemon_ip(), subnet.host_alias()] {
assert!(
!lease_map.contains(&format!("\"{reserved}\"")),
"{reserved} is reserved, never a lease in the rendered pool"
);
}
// The proxy address is nowhere in the config at all: not a lease, not
// a virtual IP, not translated — the peer owns it, and the switch
// crate's own test pins the same from the other side.
assert!(
!yaml.contains(&format!("\"{}\"", subnet.box_egress_proxy_address())),
"the proxy address is the peer's, never gvproxy's"
);
}

#[test]
fn write_config_creates_parent_and_file() {
let dir = tempfile::TempDir::new().expect("tempdir");
Expand Down
Loading
Loading