Keep concurrent overlays working and select faster data carriers
A second subnet on macOS could send with the first utun alias. Reconcile and verify each owned subnet route with its actual source, retry partially applied plans and expose host errors to clients. On Windows verify the alias and prefix on the intended adapter. Do not restart underlay sockets when the only network change is a local overlay address. Compare mutually enabled carriers using authenticated data probes while the incumbent carries traffic. The dialler coordinates a generation-scoped decision after a sustained median RTT improvement. Old peers retain ordered fallback. Local IPC moves to v24 to report data RTT and the physical path independently of control RTT. The mesh wire versions, keys and saved network settings stay compatible. Validated formatting, workspace Clippy and the workspace test suite on macOS, including bidirectional dual-network traffic and a live TLS-to-QUIC switch on loopback. Added focused native CI tests for macOS and Windows. The elevated utun test requires administrative access unavailable on the development host.
This commit is contained in:
1 parent
8d107cf0e9
commit
27e29cf8a7
34 files changed
+1569
-253
No files matched your search
@@ -208,6 +208,7 @@ struct State {
|
||||
/// Transport kinds, best first. A neighbour with several links is
|
||||
/// reached over the first of these it has.
|
||||
priority: Vec<String>,
|
||||
preferred: HashMap<PeerId, String>,
|
||||
graph: HashMap<PeerId, Vec<PeerId>>,
|
||||
members: HashSet<PeerId>,
|
||||
raw: HashMap<PeerId, Vec<RawLink>>,
|
||||
@@ -229,7 +230,14 @@ impl State {
|
||||
.get(peer)
|
||||
.map(|links| links.iter().collect())
|
||||
.unwrap_or_default();
|
||||
links.sort_by_key(|raw| (self.rank(&raw.kind), raw.kind.clone()));
|
||||
let preferred = self.preferred.get(peer);
|
||||
links.sort_by_key(|raw| {
|
||||
(
|
||||
preferred != Some(&raw.kind),
|
||||
self.rank(&raw.kind),
|
||||
&raw.kind,
|
||||
)
|
||||
});
|
||||
links
|
||||
}
|
||||
|
||||
@@ -326,6 +334,7 @@ impl RelayHub {
|
||||
tally: tally.clone(),
|
||||
}),
|
||||
priority: Vec::new(),
|
||||
preferred: HashMap::new(),
|
||||
graph: HashMap::new(),
|
||||
members: HashSet::new(),
|
||||
raw: HashMap::new(),
|
||||
@@ -350,6 +359,23 @@ impl RelayHub {
|
||||
}
|
||||
}
|
||||
|
||||
/// Selects an already open carrier for this neighbour. The existing
|
||||
/// immutable forwarding snapshot handles packet delivery and fallback.
|
||||
pub(crate) fn prefer_direct(&self, peer: EndpointId, kind: &str) {
|
||||
let mut state = self.state();
|
||||
let id = peer.as_bytes();
|
||||
if state.preferred.get(id).map(String::as_str) != Some(kind)
|
||||
&& state.raw.get(id).is_some_and(|links| {
|
||||
links
|
||||
.iter()
|
||||
.any(|raw| raw.kind == kind && !raw.link.is_closed())
|
||||
})
|
||||
{
|
||||
state.preferred.insert(*id, kind.to_owned());
|
||||
state.publish();
|
||||
}
|
||||
}
|
||||
|
||||
/// Wakes the control loop promptly when a transport reader ends.
|
||||
pub async fn changed(&self) {
|
||||
self.changed.notified().await;
|
||||
@@ -477,6 +503,9 @@ impl RelayHub {
|
||||
/// Removes one physical link without tearing down end-to-end protocol state.
|
||||
pub fn clear_direct(&self, peer: EndpointId, kind: &str) {
|
||||
let mut state = self.state();
|
||||
if state.preferred.get(peer.as_bytes()).map(String::as_str) == Some(kind) {
|
||||
state.preferred.remove(peer.as_bytes());
|
||||
}
|
||||
let mut changed = false;
|
||||
if let Some(links) = state.raw.get_mut(peer.as_bytes()) {
|
||||
if let Some(position) = links.iter().position(|known| known.kind == kind) {
|
||||
@@ -495,6 +524,7 @@ impl RelayHub {
|
||||
/// Retires data channels without removing an authenticated peer's topology.
|
||||
pub fn clear_peer_links(&self, peer: EndpointId) {
|
||||
let mut state = self.state();
|
||||
state.preferred.remove(peer.as_bytes());
|
||||
if let Some(links) = state.raw.remove(peer.as_bytes()) {
|
||||
for raw in links {
|
||||
raw.link.close();
|
||||
|
||||
@@ -20,6 +20,32 @@ fn tag() -> u64 {
|
||||
envelope::protocol_tag("ip")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn measuring_an_alternative_preserves_the_active_link_until_promotion() {
|
||||
let hub = RelayHub::new(network(), peer(1));
|
||||
hub.set_priority(vec!["first".into(), "second".into()]);
|
||||
let first = Wire::new(2, true);
|
||||
let second = Wire::new(2, true);
|
||||
hub.set_direct(peer(2), "second", second.clone());
|
||||
hub.prefer_direct(peer(2), "second");
|
||||
hub.set_direct(peer(2), "first", first.clone());
|
||||
let logical = hub.link(peer(2), "ip");
|
||||
logical.send(Bytes::from_static(b"while testing")).unwrap();
|
||||
assert_eq!(second.take().len(), 1);
|
||||
assert!(first.take().is_empty());
|
||||
hub.prefer_direct(peer(2), "first");
|
||||
logical
|
||||
.send(Bytes::from_static(b"after switching"))
|
||||
.unwrap();
|
||||
assert_eq!(first.take().len(), 1);
|
||||
first.dead.store(true, Ordering::Relaxed);
|
||||
logical.send(Bytes::from_static(b"fallback")).unwrap();
|
||||
assert_eq!(second.take().len(), 1);
|
||||
hub.clear_direct(peer(2), "first");
|
||||
hub.set_direct(peer(2), "first", Wire::new(2, true));
|
||||
assert!(Arc::ptr_eq(&logical, &hub.link(peer(2), "ip")));
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Wire {
|
||||
peer: EndpointId,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Authenticated probes of the actual data link, independent of control RTT.
|
||||
//! Keys are negotiated privately for each physical link. No fixed wire magic.
|
||||
use super::{PacketLink, SharedLink, TransportError};
|
||||
use super::{LinkQuality, PacketLink, SharedLink, TransportError};
|
||||
use crate::{BoxFuture, NetworkId};
|
||||
use bytes::Bytes;
|
||||
use hmac::{Hmac, KeyInit, Mac};
|
||||
@@ -32,40 +32,74 @@ impl std::fmt::Debug for Settings {
|
||||
}
|
||||
struct Samples {
|
||||
pending: VecDeque<(u64, Instant)>,
|
||||
recent: VecDeque<bool>,
|
||||
recent: VecDeque<Option<Duration>>,
|
||||
last_reply: Option<Instant>,
|
||||
}
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
const WINDOW: usize = 20;
|
||||
impl Samples {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
pending: VecDeque::new(),
|
||||
recent: VecDeque::new(),
|
||||
last_reply: None,
|
||||
}
|
||||
}
|
||||
fn sent(&mut self, seq: u64, now: Instant) {
|
||||
self.pending.push_back((seq, now));
|
||||
}
|
||||
fn acknowledge(&mut self, seq: u64) {
|
||||
if let Some(i) = self.pending.iter().position(|(n, _)| *n == seq) {
|
||||
self.pending.remove(i);
|
||||
self.recent.push_back(true);
|
||||
fn record(&mut self, rtt: Option<Duration>) {
|
||||
if self.recent.len() == WINDOW {
|
||||
self.recent.pop_front();
|
||||
}
|
||||
self.recent.push_back(rtt);
|
||||
}
|
||||
fn acknowledge(&mut self, seq: u64, now: Instant) {
|
||||
if let Some(i) = self.pending.iter().position(|(n, _)| *n == seq)
|
||||
&& let Some((_, sent)) = self.pending.remove(i)
|
||||
{
|
||||
let rtt = now.saturating_duration_since(sent);
|
||||
let success = rtt < PROBE_TIMEOUT;
|
||||
self.record(success.then_some(rtt));
|
||||
if success {
|
||||
self.last_reply = Some(now);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn expire(&mut self, now: Instant) {
|
||||
while self
|
||||
.pending
|
||||
.front()
|
||||
.is_some_and(|(_, at)| now.duration_since(*at) >= Duration::from_secs(3))
|
||||
.is_some_and(|(_, at)| now.duration_since(*at) >= PROBE_TIMEOUT)
|
||||
{
|
||||
self.pending.pop_front();
|
||||
self.recent.push_back(false);
|
||||
}
|
||||
while self.recent.len() > 20 {
|
||||
self.recent.pop_front();
|
||||
self.record(None);
|
||||
}
|
||||
}
|
||||
fn degraded(&self) -> bool {
|
||||
self.recent.len() >= 15
|
||||
&& self.recent.iter().filter(|ok| !**ok).count() * 100 >= self.recent.len() * 60
|
||||
&& self.recent.iter().filter(|rtt| rtt.is_none()).count() * 100
|
||||
>= self.recent.len() * 60
|
||||
}
|
||||
fn quality(&self) -> Option<LinkQuality> {
|
||||
let observed_at = self.last_reply?;
|
||||
let mut delays = [Duration::ZERO; WINDOW];
|
||||
let mut samples = 0;
|
||||
for rtt in self.recent.iter().flatten() {
|
||||
delays[samples] = *rtt;
|
||||
samples += 1;
|
||||
}
|
||||
if samples == 0 {
|
||||
return None;
|
||||
}
|
||||
delays[..samples].sort_unstable();
|
||||
let rtt = (delays[(samples - 1) / 2] + delays[samples / 2]) / 2;
|
||||
Some(LinkQuality {
|
||||
rtt,
|
||||
samples,
|
||||
losses: self.recent.len() - samples,
|
||||
observed_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// A monitored physical link. It forwards application bytes unchanged.
|
||||
@@ -75,6 +109,7 @@ pub(crate) struct HealthLink {
|
||||
rx: tokio::sync::Mutex<mpsc::Receiver<Bytes>>,
|
||||
stop: watch::Sender<bool>,
|
||||
reason: Mutex<Option<String>>,
|
||||
quality: Mutex<Option<([u8; 16], LinkQuality)>>,
|
||||
}
|
||||
impl std::fmt::Debug for HealthLink {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -99,6 +134,7 @@ impl HealthLink {
|
||||
rx: tokio::sync::Mutex::new(rx),
|
||||
stop,
|
||||
reason: Mutex::new(None),
|
||||
quality: Mutex::new(None),
|
||||
});
|
||||
let weak = Arc::downgrade(&link);
|
||||
let mut stopped = link.stop.subscribe();
|
||||
@@ -117,6 +153,7 @@ impl HealthLink {
|
||||
let Some(link)=weak.upgrade()else{break;};let Some(settings)=lock(&link.settings).clone()else{continue;};
|
||||
if marker != Some(settings.marker) { marker = Some(settings.marker); samples = Samples::new(); since = None; }
|
||||
let start=*since.get_or_insert_with(Instant::now);let now=Instant::now();samples.expire(now);
|
||||
*lock(&link.quality) = samples.quality().map(|q| (settings.marker, q));
|
||||
if now.duration_since(start)>=Duration::from_secs(20) && samples.degraded() {
|
||||
*lock(&link.reason)=Some("data delivery checks: at least 60% loss over the last 20 probes".into());break;
|
||||
}
|
||||
@@ -132,7 +169,10 @@ impl HealthLink {
|
||||
if let Some(settings)=settings && packet.starts_with(&settings.marker) {
|
||||
if let Some((kind,seq))=verify(&settings,raw.peer(),&packet) {
|
||||
if kind==1 {let _=raw.send(probe(&settings,local,2,seq,packet.len()));}
|
||||
else if kind==2 {samples.acknowledge(seq);}
|
||||
else if kind==2 && marker == Some(settings.marker) {
|
||||
samples.acknowledge(seq, Instant::now());
|
||||
*lock(&link.quality) = samples.quality().map(|q| (settings.marker, q));
|
||||
}
|
||||
}
|
||||
} else {let _=tx.try_send(packet);}
|
||||
}
|
||||
@@ -149,6 +189,11 @@ impl HealthLink {
|
||||
pub(crate) fn configure(&self, settings: Settings) {
|
||||
*lock(&self.settings) = Some(settings);
|
||||
}
|
||||
pub(crate) fn profile_id(&self) -> Option<[u8; 16]> {
|
||||
lock(&self.settings)
|
||||
.as_ref()
|
||||
.map(|settings| settings.marker)
|
||||
}
|
||||
}
|
||||
fn probe(settings: &Settings, sender: EndpointId, kind: u8, seq: u64, size: usize) -> Bytes {
|
||||
let mut bytes = vec![0; size.max(57)];
|
||||
@@ -236,6 +281,15 @@ impl PacketLink for HealthLink {
|
||||
.clone()
|
||||
.or_else(|| self.raw.failure_reason())
|
||||
}
|
||||
fn quality(&self) -> Option<LinkQuality> {
|
||||
if self.is_closed() {
|
||||
return None;
|
||||
}
|
||||
let current = self.profile_id()?;
|
||||
lock(&self.quality)
|
||||
.filter(|(profile, quality)| *profile == current && quality.is_fresh(Instant::now()))
|
||||
.map(|(_, quality)| quality)
|
||||
}
|
||||
fn path_description(&self) -> String {
|
||||
self.raw.path_description()
|
||||
}
|
||||
@@ -251,6 +305,35 @@ impl Drop for HealthLink {
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
use super::*;
|
||||
#[test]
|
||||
fn quality_uses_matched_fresh_replies_and_keeps_losses_in_the_window() {
|
||||
let mut samples = Samples::new();
|
||||
let start = Instant::now();
|
||||
assert!(samples.quality().is_none());
|
||||
for (seq, delay) in [(1, 10), (2, 30), (3, 250)] {
|
||||
samples.sent(seq, start);
|
||||
samples.acknowledge(seq, start + Duration::from_millis(delay));
|
||||
}
|
||||
samples.acknowledge(1, start + Duration::from_secs(1));
|
||||
samples.sent(4, start);
|
||||
samples.expire(start + Duration::from_secs(3));
|
||||
samples.acknowledge(4, start + Duration::from_secs(4));
|
||||
let measured = samples.quality().unwrap();
|
||||
assert_eq!(measured.rtt, Duration::from_millis(30));
|
||||
assert_eq!((measured.samples, measured.losses), (3, 1));
|
||||
assert!(measured.is_fresh(start + Duration::from_secs(4)));
|
||||
assert!(!measured.is_fresh(start + Duration::from_secs(6)));
|
||||
|
||||
for seq in 5..25 {
|
||||
let sent = start + Duration::from_secs(seq);
|
||||
samples.sent(seq, sent);
|
||||
samples.acknowledge(seq, sent + Duration::from_millis(80));
|
||||
}
|
||||
let recovered = samples.quality().unwrap();
|
||||
assert_eq!(recovered.rtt, Duration::from_millis(80));
|
||||
assert_eq!((recovered.samples, recovered.losses), (20, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_false_failure_before_window_but_persistent_loss_is_detected() {
|
||||
let mut samples = Samples::new();
|
||||
@@ -258,7 +341,7 @@ mod tests {
|
||||
for seq in 0..20 {
|
||||
samples.sent(seq, now);
|
||||
if seq % 5 == 0 {
|
||||
samples.acknowledge(seq);
|
||||
samples.acknowledge(seq, now);
|
||||
}
|
||||
}
|
||||
assert!(!samples.degraded());
|
||||
@@ -266,7 +349,7 @@ mod tests {
|
||||
assert!(samples.degraded());
|
||||
for seq in 20..40 {
|
||||
samples.sent(seq, now);
|
||||
samples.acknowledge(seq);
|
||||
samples.acknowledge(seq, now);
|
||||
}
|
||||
samples.expire(now + Duration::from_secs(4));
|
||||
assert!(!samples.degraded());
|
||||
|
||||
@@ -37,6 +37,7 @@ pub mod carrier;
|
||||
pub use carrier::{DataCarrier, SharedCarrier};
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use bytes::Bytes;
|
||||
use iroh::EndpointId;
|
||||
@@ -73,6 +74,25 @@ pub enum TransportError {
|
||||
Other(String),
|
||||
}
|
||||
|
||||
/// Recent observations from authenticated round trips over a physical data link.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct LinkQuality {
|
||||
/// Median round-trip time of successful probes in the window.
|
||||
pub rtt: Duration,
|
||||
/// Successful probes in the window.
|
||||
pub samples: usize,
|
||||
/// Timed-out probes in the same window.
|
||||
pub losses: usize,
|
||||
/// Receipt time of the latest successful probe.
|
||||
pub observed_at: Instant,
|
||||
}
|
||||
|
||||
impl LinkQuality {
|
||||
pub(crate) fn is_fresh(&self, now: Instant) -> bool {
|
||||
now.saturating_duration_since(self.observed_at) <= Duration::from_secs(5)
|
||||
}
|
||||
}
|
||||
|
||||
/// An authenticated datagram channel to one peer, for one plugin protocol.
|
||||
///
|
||||
/// Dropping the link closes it.
|
||||
@@ -125,6 +145,11 @@ pub trait PacketLink: Send + Sync + std::fmt::Debug + 'static {
|
||||
None
|
||||
}
|
||||
|
||||
/// Fresh measurements from this physical link's own authenticated probes.
|
||||
fn quality(&self) -> Option<LinkQuality> {
|
||||
None
|
||||
}
|
||||
|
||||
/// A short description of the path in use, for diagnostics.
|
||||
///
|
||||
/// Reports what the transport actually knows. It must not invent a value.
|
||||
|
||||
Reference in new issue
Block a user