diff --git a/crates/alkane-gui/src/bench_sequence.rs b/crates/alkane-gui/src/bench_sequence.rs index ceb51388..fb089cb3 100644 --- a/crates/alkane-gui/src/bench_sequence.rs +++ b/crates/alkane-gui/src/bench_sequence.rs @@ -124,18 +124,87 @@ fn classify(host: &str) -> (String, Option) { } } -/// Poll `classify` until it reaches the wanted stock-ness, or `timeout` elapses. -fn wait_for(host: &str, want_stock: bool, timeout: Duration) -> (String, bool) { +/// The MAC (lladdr) the kernel has cached for `host` (pings first to populate). +fn mac_of(host: &str) -> Option { + let _ = Command::new("ping") + .args(["-c", "1", "-W", "1", host]) + .output(); + let out = Command::new("ip").args(["neigh", "show", host]).output().ok()?; + let s = String::from_utf8_lossy(&out.stdout); + let mut it = s.split_whitespace(); + while let Some(t) = it.next() { + if t == "lladdr" { + return it.next().map(|m| m.to_lowercase()); + } + } + None +} + +/// Populate the neighbour table by parallel-pinging the /24 of `sample` (a bench +/// board can take a different DHCP lease across a reboot, so we must re-find it). +fn sweep_subnet(sample: &str) { + let base = match sample.rsplit_once('.') { + Some((net, _)) => net.to_string(), + None => return, + }; + let mut kids = Vec::new(); + for h in 2..=254 { + if let Ok(c) = Command::new("ping") + .args(["-c", "1", "-W", "1", &format!("{base}.{h}")]) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + kids.push(c); + } + } + for mut c in kids { + let _ = c.wait(); + } +} + +/// Current IPv4 for `mac` from the neighbour table, if present. +fn ip_for_mac(mac: &str) -> Option { + let out = Command::new("ip").args(["neigh"]).output().ok()?; + let s = String::from_utf8_lossy(&out.stdout); + let mac = mac.to_lowercase(); + for line in s.lines() { + if line.to_lowercase().contains(&mac) { + if let Some(ip) = line.split_whitespace().next() { + if ip.contains('.') { + return Some(ip.to_string()); + } + } + } + } + None +} + +/// Poll until the board (located by MAC, so a changed DHCP lease is followed) +/// reaches the wanted stock-ness, or `timeout` elapses. Returns the board's +/// current IP, a description, and whether the target state was reached. +fn wait_for(mac: Option<&str>, fallback: &str, want_stock: bool, timeout: Duration) -> (String, String, bool) { let deadline = Instant::now() + timeout; - let mut last; + let mut ip = fallback.to_string(); + let mut swept = Instant::now() - Duration::from_secs(60); loop { - let (desc, is_stock) = classify(host); - last = desc; + // Re-locate by MAC; sweep the subnet at most every ~25s to catch a new lease. + if let Some(m) = mac { + if ip_for_mac(m).is_none() && swept.elapsed() >= Duration::from_secs(25) { + sweep_subnet(fallback); + swept = Instant::now(); + } + if let Some(found) = ip_for_mac(m) { + ip = found; + } + } + let (desc, is_stock) = classify(&ip); + let line = format!("[{ip}] {desc}"); if is_stock == Some(want_stock) { - return (last, true); + return (ip, line, true); } if Instant::now() >= deadline { - return (last, false); + return (ip, line, false); } std::thread::sleep(Duration::from_secs(5)); } @@ -176,6 +245,14 @@ pub fn run(spec: &str) -> i32 { return 2; } + // Resolve the board's MAC now (on stock) so we can re-find it after each + // reboot even if its DHCP lease changes under alkane. + let mac = mac_of(&host); + println!( + "board MAC: {}", + mac.as_deref().unwrap_or("unknown (will use the fixed host)") + ); + let boot_timeout = Duration::from_secs(300); let mut all_ok = true; @@ -203,13 +280,13 @@ pub fn run(spec: &str) -> i32 { "[2/4] verify alkane (awaiting reboot, up to {}s)...", boot_timeout.as_secs() ); - let (desc, ok) = wait_for(&host, false, boot_timeout); + let (alkane_ip, desc, ok) = wait_for(mac.as_deref(), &host, false, boot_timeout); println!(" {desc} => {}", if ok { "ALKANE" } else { "NOT ALKANE (fail)" }); all_ok = all_ok && ok; - // 3. restore (alkane -> stock) flag-flip through the GUI operation layer - println!("[3/4] restore: alkane -> stock (flag flip)"); - let h = stock_ops::start_restore_flag(vec![host.clone()], password.clone()); + // 3. restore (alkane -> stock) flag-flip at the board's CURRENT address + println!("[3/4] restore: alkane -> stock (flag flip) at {alkane_ip}"); + let h = stock_ops::start_restore_flag(vec![alkane_ip.clone()], password.clone()); if !drive("restore", h) { all_ok = false; println!("[3/4] restore FAILED"); @@ -220,7 +297,7 @@ pub fn run(spec: &str) -> i32 { "[4/4] verify stock (awaiting reboot, up to {}s)...", boot_timeout.as_secs() ); - let (desc, ok) = wait_for(&host, true, boot_timeout); + let (_stock_ip, desc, ok) = wait_for(mac.as_deref(), &host, true, boot_timeout); println!(" {desc} => {}", if ok { "STOCK" } else { "NOT STOCK (fail)" }); all_ok = all_ok && ok; }