2026-09-22 18:08:26 +03:00
|
|
|
//! Userspace forwarding. Topology updates publish immutable tables; the packet
|
|
|
|
|
//! path takes no routing mutex, walks no graph and never inspects plugin bytes.
|
|
|
|
|
//! Each transport reader forwards transit immediately, independently of the
|
|
|
|
|
//! destination protocol's receive task. Only local delivery enters its inbox.
|
2026-09-22 01:32:43 +01:00
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
2026-09-22 01:32:43 +01:00
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
use arc_swap::ArcSwap;
|
|
|
|
|
use bytes::Bytes;
|
2026-09-22 01:32:43 +01:00
|
|
|
use iroh::EndpointId;
|
2026-09-22 18:08:26 +03:00
|
|
|
use tokio::sync::{Notify, mpsc, watch};
|
|
|
|
|
use tokio::task::JoinHandle;
|
2026-09-22 01:32:43 +01:00
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
use super::routing::{FlowId, PeerId, RoutingTable, envelope, mix64};
|
|
|
|
|
use super::transport::{PacketLink, SharedLink, TransportError};
|
2026-09-22 01:32:43 +01:00
|
|
|
use crate::BoxFuture;
|
2026-09-22 18:08:26 +03:00
|
|
|
use crate::config::{MAX_DATA_DATAGRAM, ROUTING_HOP_LIMIT};
|
2026-09-22 01:32:43 +01:00
|
|
|
use crate::identity::NetworkId;
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Fixed routing header, subtracted from the logical transport MTU.
|
|
|
|
|
pub const RELAY_OVERHEAD: usize = envelope::HEADER;
|
2026-09-22 01:32:43 +01:00
|
|
|
const INBOX: usize = 256;
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Forwarding counters, shared by all protocols of one network.
|
2026-09-22 01:32:43 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
|
|
|
|
pub struct RelayCounters {
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Datagrams originated here and sent through another member.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub sent_via: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Transit datagrams handed to the next transport link.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub forwarded: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Local datagrams received through another member.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub received_via: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Missing or failed next hop.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub dropped_no_link: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Malformed envelope or unknown member.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub dropped_unknown: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Exhausted hop limit, including loops during topology convergence.
|
|
|
|
|
pub dropped_hop_limit: u64,
|
|
|
|
|
/// Full local protocol inbox. Transit does not use this queue.
|
|
|
|
|
pub dropped_congested: u64,
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
struct Tally {
|
|
|
|
|
sent_via: AtomicU64,
|
|
|
|
|
forwarded: AtomicU64,
|
|
|
|
|
received_via: AtomicU64,
|
|
|
|
|
dropped_no_link: AtomicU64,
|
|
|
|
|
dropped_unknown: AtomicU64,
|
2026-09-22 18:08:26 +03:00
|
|
|
dropped_hop_limit: AtomicU64,
|
|
|
|
|
dropped_congested: AtomicU64,
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tally {
|
|
|
|
|
fn snapshot(&self) -> RelayCounters {
|
|
|
|
|
RelayCounters {
|
|
|
|
|
sent_via: self.sent_via.load(Ordering::Relaxed),
|
|
|
|
|
forwarded: self.forwarded.load(Ordering::Relaxed),
|
|
|
|
|
received_via: self.received_via.load(Ordering::Relaxed),
|
|
|
|
|
dropped_no_link: self.dropped_no_link.load(Ordering::Relaxed),
|
|
|
|
|
dropped_unknown: self.dropped_unknown.load(Ordering::Relaxed),
|
2026-09-22 18:08:26 +03:00
|
|
|
dropped_hop_limit: self.dropped_hop_limit.load(Ordering::Relaxed),
|
|
|
|
|
dropped_congested: self.dropped_congested.load(Ordering::Relaxed),
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// The links to one neighbour, best first. Which transport carries a frame
|
|
|
|
|
/// is decided here and nowhere else: the end-to-end protocol is in the
|
|
|
|
|
/// envelope and never depends on it.
|
2026-09-22 01:32:43 +01:00
|
|
|
#[derive(Debug)]
|
2026-09-22 18:08:26 +03:00
|
|
|
struct NextHop {
|
|
|
|
|
peer: PeerId,
|
2026-10-05 11:39:33 +01:00
|
|
|
links: Box<[SharedLink]>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NextHop {
|
|
|
|
|
fn usable(&self) -> bool {
|
|
|
|
|
self.links.iter().any(|link| !link.is_closed())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn first_open(&self) -> Option<&SharedLink> {
|
|
|
|
|
self.links.iter().find(|link| !link.is_closed())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Hands a frame to the best open link that accepts it. A link that
|
|
|
|
|
/// cannot carry the frame (too large for its path, closed a moment ago)
|
|
|
|
|
/// leaves it to the next one; the frame is only copied when there is
|
|
|
|
|
/// another to try.
|
|
|
|
|
fn send(&self, frame: Bytes) -> Result<(), TransportError> {
|
|
|
|
|
let mut open = self.links.iter().filter(|link| !link.is_closed());
|
|
|
|
|
let Some(mut current) = open.next() else {
|
|
|
|
|
return Err(TransportError::Closed);
|
|
|
|
|
};
|
|
|
|
|
for next in open {
|
|
|
|
|
if current.send(frame.clone()).is_ok() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
current = next;
|
|
|
|
|
}
|
|
|
|
|
current.send(frame)
|
|
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ForwardRoute {
|
|
|
|
|
hops: u8,
|
|
|
|
|
next: Box<[NextHop]>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ForwardRoute {
|
|
|
|
|
fn select(&self, source: &PeerId, flow: FlowId) -> Option<&NextHop> {
|
|
|
|
|
if self.next.len() == 1 {
|
2026-10-05 11:39:33 +01:00
|
|
|
return self.next.first().filter(|hop| hop.usable());
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
let seed = u64::from_le_bytes(source[..8].try_into().ok()?);
|
|
|
|
|
let start = mix64(flow ^ seed) as usize % self.next.len();
|
|
|
|
|
// Stable fallback if a link closed just before its table was replaced.
|
|
|
|
|
(0..self.next.len())
|
|
|
|
|
.map(|i| &self.next[(start + i) % self.next.len()])
|
2026-10-05 11:39:33 +01:00
|
|
|
.find(|hop| hop.usable())
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
struct ForwardingTable {
|
|
|
|
|
routes: HashMap<PeerId, ForwardRoute>,
|
2026-10-05 11:39:33 +01:00
|
|
|
/// Local delivery, by who sent it and which end-to-end protocol it is.
|
|
|
|
|
inboxes: HashMap<(PeerId, u64), mpsc::Sender<Bytes>>,
|
2026-09-22 18:08:26 +03:00
|
|
|
members: HashSet<PeerId>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Plane {
|
|
|
|
|
local: PeerId,
|
|
|
|
|
table: ArcSwap<ForwardingTable>,
|
2026-09-22 01:32:43 +01:00
|
|
|
tally: Arc<Tally>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
impl Plane {
|
|
|
|
|
/// Synchronous hot path: one snapshot, bounded header decode, table lookup,
|
|
|
|
|
/// and transport send. An exclusively owned buffer is modified in place.
|
|
|
|
|
fn receive(&self, incoming: PeerId, frame: Bytes) {
|
|
|
|
|
let Some(header) = envelope::decode(&frame) else {
|
|
|
|
|
self.tally.dropped_unknown.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let table = self.table.load();
|
|
|
|
|
if !table.members.contains(&header.source) || header.source == self.local {
|
|
|
|
|
self.tally.dropped_unknown.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if header.destination == self.local {
|
2026-10-05 11:39:33 +01:00
|
|
|
let Some(inbox) = table.inboxes.get(&(header.source, header.protocol)) else {
|
2026-09-22 18:08:26 +03:00
|
|
|
self.tally.dropped_unknown.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
if inbox.try_send(frame.slice(RELAY_OVERHEAD..)).is_err() {
|
|
|
|
|
self.tally.dropped_congested.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
} else if incoming != header.source {
|
|
|
|
|
self.tally.received_via.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if header.remaining <= 1 {
|
|
|
|
|
self.tally.dropped_hop_limit.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let hop = table
|
|
|
|
|
.routes
|
|
|
|
|
.get(&header.destination)
|
|
|
|
|
.and_then(|route| route.select(&header.source, header.flow));
|
|
|
|
|
let Some(hop) = hop else {
|
|
|
|
|
self.tally.dropped_no_link.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
};
|
2026-10-05 11:39:33 +01:00
|
|
|
if hop.send(envelope::decrement(frame)).is_ok() {
|
2026-09-22 18:08:26 +03:00
|
|
|
self.tally.forwarded.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
} else {
|
|
|
|
|
self.tally.dropped_no_link.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// One physical link to a neighbour, of whatever kind of transport.
|
2026-09-22 18:08:26 +03:00
|
|
|
#[derive(Debug)]
|
2026-10-05 11:39:33 +01:00
|
|
|
struct RawLink {
|
|
|
|
|
kind: String,
|
|
|
|
|
link: SharedLink,
|
|
|
|
|
reader: JoinHandle<()>,
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// How a destination is reached right now.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub struct RouteSummary {
|
|
|
|
|
/// Number of transport links to it. One is a direct link.
|
|
|
|
|
pub hops: u8,
|
|
|
|
|
/// The neighbour the first link goes to. For a direct link that is the
|
|
|
|
|
/// destination itself.
|
|
|
|
|
pub via: EndpointId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct State {
|
|
|
|
|
plane: Arc<Plane>,
|
|
|
|
|
/// Transport kinds, best first. A neighbour with several links is
|
|
|
|
|
/// reached over the first of these it has.
|
|
|
|
|
priority: Vec<String>,
|
|
|
|
|
graph: HashMap<PeerId, Vec<PeerId>>,
|
|
|
|
|
members: HashSet<PeerId>,
|
|
|
|
|
raw: HashMap<PeerId, Vec<RawLink>>,
|
|
|
|
|
links: HashMap<(PeerId, String), Arc<PeerLink>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
fn rank(&self, kind: &str) -> usize {
|
|
|
|
|
self.priority
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|known| known == kind)
|
|
|
|
|
.unwrap_or(usize::MAX)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A neighbour's links, best kind first.
|
|
|
|
|
fn ordered(&self, peer: &PeerId) -> Vec<&RawLink> {
|
|
|
|
|
let mut links: Vec<&RawLink> = self
|
|
|
|
|
.raw
|
|
|
|
|
.get(peer)
|
|
|
|
|
.map(|links| links.iter().collect())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
links.sort_by_key(|raw| (self.rank(&raw.kind), raw.kind.clone()));
|
|
|
|
|
links
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
fn publish(&self) {
|
|
|
|
|
let mut graph = self.graph.clone();
|
2026-10-05 11:39:33 +01:00
|
|
|
let mut neighbours: Vec<PeerId> = self
|
|
|
|
|
.raw
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|(_, links)| links.iter().any(|raw| !raw.link.is_closed()))
|
|
|
|
|
.map(|(peer, _)| *peer)
|
|
|
|
|
.collect();
|
|
|
|
|
neighbours.sort_unstable();
|
|
|
|
|
graph.insert(self.plane.local, neighbours);
|
2026-09-22 18:08:26 +03:00
|
|
|
let routing = RoutingTable::build(self.plane.local, &graph, ROUTING_HOP_LIMIT);
|
|
|
|
|
let routes = routing
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(peer, route)| {
|
|
|
|
|
let next: Box<[_]> = route
|
|
|
|
|
.next_hops
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|id| {
|
2026-10-05 11:39:33 +01:00
|
|
|
let links: Box<[_]> = self
|
|
|
|
|
.ordered(id)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|raw| raw.link.clone())
|
|
|
|
|
.collect();
|
|
|
|
|
(!links.is_empty()).then_some(NextHop { peer: *id, links })
|
2026-09-22 18:08:26 +03:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
(!next.is_empty()).then_some((
|
|
|
|
|
*peer,
|
|
|
|
|
ForwardRoute {
|
|
|
|
|
hops: route.hops,
|
|
|
|
|
next,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let mut members = self.members.clone();
|
|
|
|
|
members.extend(self.raw.keys().copied());
|
|
|
|
|
self.plane.table.store(Arc::new(ForwardingTable {
|
|
|
|
|
routes,
|
|
|
|
|
members,
|
|
|
|
|
inboxes: self
|
|
|
|
|
.links
|
|
|
|
|
.iter()
|
2026-10-05 11:39:33 +01:00
|
|
|
.map(|((peer, _), link)| ((*peer, link.tag), link.inbox_tx.clone()))
|
2026-09-22 18:08:26 +03:00
|
|
|
.collect(),
|
|
|
|
|
}));
|
|
|
|
|
}
|
2026-10-05 11:39:33 +01:00
|
|
|
|
|
|
|
|
fn stop_readers(&mut self) {
|
|
|
|
|
for raw in self.raw.values().flatten() {
|
|
|
|
|
raw.reader.abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
impl Drop for State {
|
2026-09-22 18:08:26 +03:00
|
|
|
fn drop(&mut self) {
|
2026-10-05 11:39:33 +01:00
|
|
|
self.stop_readers();
|
2026-09-22 18:08:26 +03:00
|
|
|
for link in self.links.values() {
|
|
|
|
|
link.close();
|
|
|
|
|
}
|
|
|
|
|
self.plane.table.store(Arc::default());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// One network's transport links and its published forwarding table.
|
|
|
|
|
///
|
|
|
|
|
/// There is one table for the whole network, whatever protocols run over it.
|
|
|
|
|
/// A link is a way to reach a neighbour; a protocol is an end-to-end channel
|
|
|
|
|
/// to a peer. The two meet only in the envelope, which names the protocol, so
|
|
|
|
|
/// a tunnel keeps working over any mix of transports and a middle member
|
|
|
|
|
/// needs no protocol in common with either end.
|
2026-09-22 18:08:26 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct RelayHub {
|
|
|
|
|
network: NetworkId,
|
2026-10-05 11:39:33 +01:00
|
|
|
state: Mutex<State>,
|
2026-09-22 18:08:26 +03:00
|
|
|
tally: Arc<Tally>,
|
|
|
|
|
changed: Arc<Notify>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 01:32:43 +01:00
|
|
|
impl RelayHub {
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Creates the router at this network's local endpoint.
|
|
|
|
|
pub fn new(network: NetworkId, local: EndpointId) -> Arc<Self> {
|
2026-10-05 11:39:33 +01:00
|
|
|
let tally = Arc::new(Tally::default());
|
2026-09-22 01:32:43 +01:00
|
|
|
Arc::new(Self {
|
|
|
|
|
network,
|
2026-10-05 11:39:33 +01:00
|
|
|
state: Mutex::new(State {
|
|
|
|
|
plane: Arc::new(Plane {
|
|
|
|
|
local: *local.as_bytes(),
|
|
|
|
|
table: ArcSwap::from_pointee(ForwardingTable::default()),
|
|
|
|
|
tally: tally.clone(),
|
|
|
|
|
}),
|
|
|
|
|
priority: Vec::new(),
|
|
|
|
|
graph: HashMap::new(),
|
|
|
|
|
members: HashSet::new(),
|
|
|
|
|
raw: HashMap::new(),
|
|
|
|
|
links: HashMap::new(),
|
|
|
|
|
}),
|
|
|
|
|
tally,
|
2026-09-22 18:08:26 +03:00
|
|
|
changed: Arc::default(),
|
2026-09-22 01:32:43 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
fn state(&self) -> std::sync::MutexGuard<'_, State> {
|
|
|
|
|
self.state.lock().unwrap_or_else(|error| error.into_inner())
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// Sets which kinds of transport are preferred, best first, when a
|
|
|
|
|
/// neighbour can be reached over more than one.
|
|
|
|
|
pub fn set_priority(&self, kinds: Vec<String>) {
|
|
|
|
|
let mut state = self.state();
|
|
|
|
|
if state.priority != kinds {
|
|
|
|
|
state.priority = kinds;
|
|
|
|
|
state.publish();
|
|
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wakes the control loop promptly when a transport reader ends.
|
|
|
|
|
pub async fn changed(&self) {
|
|
|
|
|
self.changed.notified().await;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Current counters.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub fn counters(&self) -> RelayCounters {
|
|
|
|
|
self.tally.snapshot()
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Publishes authenticated topology. Refreshing identical state is cheap.
|
2026-10-05 11:39:33 +01:00
|
|
|
///
|
|
|
|
|
/// Every row of `graph` is one member's own account of who it has a link
|
|
|
|
|
/// to; what kind of link it is does not matter here.
|
|
|
|
|
pub fn set_topology(&self, graph: HashMap<PeerId, Vec<PeerId>>, members: HashSet<PeerId>) {
|
|
|
|
|
let mut state = self.state();
|
2026-09-22 18:08:26 +03:00
|
|
|
if state.graph != graph || state.members != members {
|
|
|
|
|
state.graph = graph;
|
|
|
|
|
state.members = members;
|
|
|
|
|
state.publish();
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Whether the current table has a path to this destination.
|
2026-10-05 11:39:33 +01:00
|
|
|
pub fn reachable(&self, peer: EndpointId) -> bool {
|
|
|
|
|
self.state()
|
|
|
|
|
.plane
|
|
|
|
|
.table
|
|
|
|
|
.load()
|
|
|
|
|
.routes
|
|
|
|
|
.contains_key(peer.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// How a destination is reached right now, if it is.
|
|
|
|
|
pub fn route(&self, peer: EndpointId) -> Option<RouteSummary> {
|
|
|
|
|
let table = self.state().plane.table.load_full();
|
|
|
|
|
let route = table.routes.get(peer.as_bytes())?;
|
|
|
|
|
let via = EndpointId::from_bytes(&route.next.first()?.peer).ok()?;
|
|
|
|
|
Some(RouteSummary {
|
|
|
|
|
hops: route.hops,
|
|
|
|
|
via,
|
2026-09-22 18:08:26 +03:00
|
|
|
})
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// The kinds of transport with an open direct link to a peer, best
|
|
|
|
|
/// first.
|
|
|
|
|
pub fn direct_kinds(&self, peer: EndpointId) -> Vec<String> {
|
|
|
|
|
let state = self.state();
|
|
|
|
|
state
|
|
|
|
|
.ordered(peer.as_bytes())
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|raw| !raw.link.is_closed())
|
|
|
|
|
.map(|raw| raw.kind.clone())
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stable end-to-end channel for a protocol, preserved across topology
|
|
|
|
|
/// and transport changes.
|
2026-09-22 18:08:26 +03:00
|
|
|
pub fn link(&self, peer: EndpointId, protocol: &str) -> Arc<PeerLink> {
|
2026-10-05 11:39:33 +01:00
|
|
|
let mut state = self.state();
|
|
|
|
|
let key = (*peer.as_bytes(), protocol.to_owned());
|
|
|
|
|
if let Some(link) = state.links.get(&key) {
|
2026-09-22 18:08:26 +03:00
|
|
|
return link.clone();
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
let (inbox_tx, inbox_rx) = mpsc::channel(INBOX);
|
2026-09-22 18:08:26 +03:00
|
|
|
let (closed, _) = watch::channel(false);
|
2026-09-22 01:32:43 +01:00
|
|
|
let link = Arc::new(PeerLink {
|
|
|
|
|
network: self.network,
|
|
|
|
|
peer,
|
2026-10-05 11:39:33 +01:00
|
|
|
tag: envelope::protocol_tag(protocol),
|
2026-09-22 18:08:26 +03:00
|
|
|
plane: state.plane.clone(),
|
2026-09-22 01:32:43 +01:00
|
|
|
inbox_tx,
|
|
|
|
|
inbox_rx: tokio::sync::Mutex::new(inbox_rx),
|
2026-09-22 18:08:26 +03:00
|
|
|
closed,
|
2026-09-22 01:32:43 +01:00
|
|
|
});
|
2026-10-05 11:39:33 +01:00
|
|
|
state.links.insert(key, link.clone());
|
2026-09-22 18:08:26 +03:00
|
|
|
state.publish();
|
2026-09-22 01:32:43 +01:00
|
|
|
link
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// Whether the protocol already owns its end-to-end channel to a peer.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub fn has_link(&self, peer: EndpointId, protocol: &str) -> bool {
|
2026-10-05 11:39:33 +01:00
|
|
|
self.state()
|
|
|
|
|
.links
|
|
|
|
|
.contains_key(&(*peer.as_bytes(), protocol.to_owned()))
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Installs a raw transport link and starts its independent ingress reader.
|
2026-10-05 11:39:33 +01:00
|
|
|
///
|
|
|
|
|
/// A neighbour may have one link per kind of transport; installing a
|
|
|
|
|
/// second of the same kind replaces the first.
|
|
|
|
|
pub fn set_direct(&self, peer: EndpointId, kind: &str, raw: SharedLink) {
|
2026-09-22 18:08:26 +03:00
|
|
|
if raw.network() != self.network || raw.peer() != peer {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-05 11:39:33 +01:00
|
|
|
let mut state = self.state();
|
2026-09-22 18:08:26 +03:00
|
|
|
let id = *peer.as_bytes();
|
|
|
|
|
let plane = state.plane.clone();
|
|
|
|
|
let changed = self.changed.clone();
|
2026-10-05 11:39:33 +01:00
|
|
|
let reader_link = raw.clone();
|
|
|
|
|
let reader = tokio::spawn(async move {
|
|
|
|
|
let mut batch = 0;
|
|
|
|
|
while let Some(frame) = reader_link.recv().await {
|
|
|
|
|
plane.receive(id, frame);
|
|
|
|
|
batch += 1;
|
|
|
|
|
if batch == 64 {
|
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
|
batch = 0;
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
2026-10-05 11:39:33 +01:00
|
|
|
}
|
|
|
|
|
changed.notify_one();
|
|
|
|
|
});
|
|
|
|
|
let links = state.raw.entry(id).or_default();
|
|
|
|
|
if let Some(position) = links.iter().position(|known| known.kind == kind) {
|
|
|
|
|
links.remove(position).reader.abort();
|
|
|
|
|
}
|
|
|
|
|
links.push(RawLink {
|
|
|
|
|
kind: kind.to_owned(),
|
|
|
|
|
link: raw,
|
|
|
|
|
reader,
|
|
|
|
|
});
|
|
|
|
|
state.publish();
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 11:39:33 +01:00
|
|
|
/// 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();
|
|
|
|
|
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) {
|
|
|
|
|
links.remove(position).reader.abort();
|
|
|
|
|
changed = true;
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
2026-10-05 11:39:33 +01:00
|
|
|
if links.is_empty() {
|
|
|
|
|
state.raw.remove(peer.as_bytes());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if changed {
|
2026-09-22 18:08:26 +03:00
|
|
|
state.publish();
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-12 16:19:18 +01:00
|
|
|
/// Retires data channels without removing an authenticated peer's topology.
|
|
|
|
|
pub fn clear_peer_links(&self, peer: EndpointId) {
|
2026-10-05 11:39:33 +01:00
|
|
|
let mut state = self.state();
|
|
|
|
|
if let Some(links) = state.raw.remove(peer.as_bytes()) {
|
|
|
|
|
for raw in links {
|
2026-10-12 16:19:18 +01:00
|
|
|
raw.link.close();
|
2026-10-05 11:39:33 +01:00
|
|
|
raw.reader.abort();
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
2026-10-05 11:39:33 +01:00
|
|
|
}
|
|
|
|
|
let gone: Vec<_> = state
|
|
|
|
|
.links
|
|
|
|
|
.keys()
|
|
|
|
|
.filter(|(id, _)| id == peer.as_bytes())
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
for key in gone {
|
|
|
|
|
if let Some(link) = state.links.remove(&key) {
|
2026-09-22 01:32:43 +01:00
|
|
|
link.close();
|
|
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
}
|
2026-10-12 16:19:18 +01:00
|
|
|
state.publish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Revokes all links and topology involving a departed authenticated peer.
|
|
|
|
|
pub fn remove_peer(&self, peer: EndpointId) {
|
|
|
|
|
self.clear_peer_links(peer);
|
|
|
|
|
let mut state = self.state();
|
2026-10-05 11:39:33 +01:00
|
|
|
state.members.remove(peer.as_bytes());
|
|
|
|
|
state.graph.remove(peer.as_bytes());
|
|
|
|
|
for neighbors in state.graph.values_mut() {
|
|
|
|
|
neighbors.retain(|id| id != peer.as_bytes());
|
|
|
|
|
}
|
|
|
|
|
state.publish();
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// Stops every reader and releases all transport handles.
|
2026-09-22 01:32:43 +01:00
|
|
|
pub fn close(&self) {
|
2026-10-05 11:39:33 +01:00
|
|
|
let mut state = self.state();
|
|
|
|
|
state.stop_readers();
|
|
|
|
|
state.raw.clear();
|
|
|
|
|
for link in state.links.values() {
|
|
|
|
|
link.close();
|
|
|
|
|
}
|
|
|
|
|
state.links.clear();
|
|
|
|
|
state.graph.clear();
|
|
|
|
|
state.members.clear();
|
|
|
|
|
state.plane.table.store(Arc::default());
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 18:08:26 +03:00
|
|
|
/// A protocol's end-to-end channel, independent of its current next hop.
|
2026-09-22 01:32:43 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct PeerLink {
|
|
|
|
|
network: NetworkId,
|
|
|
|
|
peer: EndpointId,
|
2026-10-05 11:39:33 +01:00
|
|
|
tag: u64,
|
2026-09-22 18:08:26 +03:00
|
|
|
plane: Arc<Plane>,
|
2026-09-22 01:32:43 +01:00
|
|
|
inbox_tx: mpsc::Sender<Bytes>,
|
|
|
|
|
inbox_rx: tokio::sync::Mutex<mpsc::Receiver<Bytes>>,
|
2026-09-22 18:08:26 +03:00
|
|
|
closed: watch::Sender<bool>,
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PeerLink {
|
|
|
|
|
fn close(&self) {
|
2026-09-22 18:08:26 +03:00
|
|
|
self.closed.send_replace(true);
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PacketLink for PeerLink {
|
|
|
|
|
fn network(&self) -> NetworkId {
|
|
|
|
|
self.network
|
|
|
|
|
}
|
|
|
|
|
fn peer(&self) -> EndpointId {
|
|
|
|
|
self.peer
|
|
|
|
|
}
|
|
|
|
|
fn max_datagram_size(&self) -> usize {
|
2026-09-22 18:08:26 +03:00
|
|
|
MAX_DATA_DATAGRAM - RELAY_OVERHEAD
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
fn send(&self, payload: Bytes) -> Result<(), TransportError> {
|
2026-09-22 18:08:26 +03:00
|
|
|
self.send_flow(payload, 0)
|
|
|
|
|
}
|
|
|
|
|
fn send_flow(&self, payload: Bytes, flow: FlowId) -> Result<(), TransportError> {
|
|
|
|
|
if self.is_closed() {
|
2026-09-22 01:32:43 +01:00
|
|
|
return Err(TransportError::Closed);
|
|
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
if payload.len() > self.max_datagram_size() {
|
|
|
|
|
return Err(TransportError::TooLarge {
|
|
|
|
|
size: payload.len(),
|
|
|
|
|
limit: self.max_datagram_size(),
|
|
|
|
|
});
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
let table = self.plane.table.load();
|
|
|
|
|
let route = table
|
|
|
|
|
.routes
|
|
|
|
|
.get(self.peer.as_bytes())
|
|
|
|
|
.ok_or(TransportError::Closed)?;
|
|
|
|
|
let hop = route
|
|
|
|
|
.select(&self.plane.local, flow)
|
|
|
|
|
.ok_or(TransportError::Closed)?;
|
2026-10-05 11:39:33 +01:00
|
|
|
hop.send(envelope::encode(
|
2026-09-22 18:08:26 +03:00
|
|
|
self.plane.local,
|
|
|
|
|
*self.peer.as_bytes(),
|
|
|
|
|
flow,
|
2026-10-05 11:39:33 +01:00
|
|
|
self.tag,
|
2026-09-22 18:08:26 +03:00
|
|
|
&payload,
|
|
|
|
|
))?;
|
|
|
|
|
if route.hops > 1 {
|
|
|
|
|
self.plane.tally.sent_via.fetch_add(1, Ordering::Relaxed);
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
Ok(())
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
fn recv(&self) -> BoxFuture<'_, Option<Bytes>> {
|
|
|
|
|
Box::pin(async move {
|
2026-09-22 18:08:26 +03:00
|
|
|
let mut closed = self.closed.subscribe();
|
|
|
|
|
if *closed.borrow_and_update() {
|
|
|
|
|
return None;
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
2026-09-22 18:08:26 +03:00
|
|
|
let mut inbox = self.inbox_rx.lock().await;
|
|
|
|
|
tokio::select! { biased; _ = closed.changed() => None, frame = inbox.recv() => frame }
|
2026-09-22 01:32:43 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
fn closed(&self) -> BoxFuture<'_, ()> {
|
|
|
|
|
Box::pin(async move {
|
2026-09-22 18:08:26 +03:00
|
|
|
let mut closed = self.closed.subscribe();
|
|
|
|
|
if !*closed.borrow_and_update() {
|
|
|
|
|
let _ = closed.changed().await;
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
fn is_closed(&self) -> bool {
|
2026-09-22 18:08:26 +03:00
|
|
|
*self.closed.borrow()
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
fn path_description(&self) -> String {
|
2026-09-22 18:08:26 +03:00
|
|
|
let table = self.plane.table.load();
|
|
|
|
|
match table.routes.get(self.peer.as_bytes()) {
|
2026-10-05 11:39:33 +01:00
|
|
|
Some(route) if route.hops == 1 => route.next[0]
|
|
|
|
|
.first_open()
|
|
|
|
|
.map_or_else(|| "unreachable".into(), |link| link.path_description()),
|
2026-09-22 18:08:26 +03:00
|
|
|
Some(route) => format!(
|
|
|
|
|
"relay {} hops via {} ({} equal paths)",
|
|
|
|
|
route.hops,
|
|
|
|
|
hex::encode(route.next[0].peer),
|
|
|
|
|
route.next.len()
|
|
|
|
|
),
|
|
|
|
|
None => "unreachable".into(),
|
2026-09-22 01:32:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2026-09-22 18:08:26 +03:00
|
|
|
#[path = "relay_tests.rs"]
|
|
|
|
|
mod tests;
|