interfaces: per-interface hop_limiter, copied to accepted connections as upstream

This commit is contained in:
DeFiDude committed 2026-11-18 13:05:37 -07:00
1 parent b0125cea65
commit 403c8084ba
7 files changed
+214 -4

No files matched your search

@@ -2034,6 +2034,12 @@ pub struct InterfacePostInit {
pub announces_to_internal: Option<bool>,
/// Configured `gravity`; `None` takes `[reticulum] default_gravity`.
pub gravity: Option<i64>,
/// Configured `hop_limiter`, clamped to `0..=PATHFINDER_M`; 0 is `None`, off.
pub hop_limiter: Option<u8>,
/// Whether connections this interface accepts copy `hop_limiter`: TCP
/// server, Backbone and I2P listeners do, AutoInterface peers do not
/// (TCPInterface.py, BackboneInterface.py, I2PInterface.py).
pub spawned_hop_limiter: bool,
/// Required announce stamp value on this interface, clamped to the stamp
/// cost cap. `None` follows the global `[stamps] destination_cost`.
pub destination_cost: Option<f64>,
@@ -2139,6 +2145,13 @@ impl InterfacePostInit {
announces_from_internal,
announces_to_internal: config_bool(section, "announces_to_internal")?,
gravity: config_int(section, "gravity")?,
hop_limiter: config_int(section, "hop_limiter")?
.map(rns_transport::destination_filter::FilterPolicy::clamp_hop_limiter)
.filter(|limit| *limit > 0),
spawned_hop_limiter: matches!(
section.get("type"),
Some("TCPServerInterface" | "BackboneInterface" | "I2PInterface")
),
destination_cost: section
.get_float("destination_cost")
.map(rns_transport::stamps::StampPolicy::clamp_cost),
+24 -2
View File
@@ -104,6 +104,7 @@ struct SpawnInheritance {
announces_from_internal: bool,
announces_to_internal: Option<bool>,
gravity: i64,
hop_limiter: Option<u8>,
}
impl SpawnInheritance {
@@ -5252,6 +5253,8 @@ struct OwnedInterfaceHandle {
/// The address a discoverable interface learns at runtime (I2P b32).
discovery_address: Option<tokio::sync::watch::Receiver<Option<String>>>,
hardware_failures: Option<InterfaceFailureWatch>,
/// A peer Python spawns for its configured interface (I2PInterface.py).
configured_peer: bool,
}
/// A driver's failures Python answers with `RNS.panic()` when
@@ -5429,6 +5432,7 @@ impl From<rns_interface::traits::InterfaceHandle> for OwnedInterfaceHandle {
driver: None,
discovery_address: None,
hardware_failures: None,
configured_peer: false,
}
}
}
@@ -5440,6 +5444,7 @@ impl From<rns_interface::rnode::SpawnedRNodeInterface> for OwnedInterfaceHandle
driver: Some(spawned.driver),
discovery_address: None,
hardware_failures: None,
configured_peer: false,
}
}
}
@@ -6058,6 +6063,7 @@ async fn prepare_interface_with_role_and_overrides(
entry.announces_from_internal = parent.announces_from_internal;
entry.announces_to_internal = parent.announces_to_internal;
entry.gravity = parent.gravity;
entry.hop_limiter = parent.hop_limiter;
}
entry.destination_cost = child_stamp_costs.0;
entry.pr_cost = child_stamp_costs.1;
@@ -6198,6 +6204,8 @@ fn clone_interface_post_init(
announces_from_internal: post_init.announces_from_internal,
announces_to_internal: post_init.announces_to_internal,
gravity: post_init.gravity,
hop_limiter: post_init.hop_limiter,
spawned_hop_limiter: post_init.spawned_hop_limiter,
destination_cost: post_init.destination_cost,
pr_cost: post_init.pr_cost,
autoconnect_source: post_init.autoconnect_source.clone(),
@@ -6272,7 +6280,10 @@ async fn prepare_interface_with_post_init(
));
}
let OwnedInterfaceHandle {
interface, driver, ..
interface,
driver,
configured_peer,
..
} = owned;
let handle = interface;
// Outbound = physical capability AND `outgoing` config flag. An
@@ -6363,6 +6374,9 @@ async fn prepare_interface_with_post_init(
announces_from_internal: post_init.announces_from_internal,
announces_to_internal: post_init.announces_to_internal,
gravity: post_init.gravity.unwrap_or(0),
hop_limiter: post_init
.hop_limiter
.filter(|_| post_init.spawned_hop_limiter),
}),
},
);
@@ -6395,6 +6409,11 @@ async fn prepare_interface_with_post_init(
entry.announces_to_internal = post_init.announces_to_internal;
entry.gravity = post_init.gravity.unwrap_or(0);
entry.shared_medium = post_init.shared_medium;
// Reticulum.py sets it on the configured interface, not on an RNodeMulti
// subinterface or an I2P peer.
entry.hop_limiter = post_init
.hop_limiter
.filter(|_| handle.parent_id.is_none() && !configured_peer);
entry.destination_cost = post_init.destination_cost;
entry.pr_cost = post_init.pr_cost;
Ok(PreparedInterfaceRegistration {
@@ -10826,7 +10845,10 @@ async fn spawn_configured_interface_driver(
)
.await
{
Ok(client) => handles.push(client.into()),
Ok(client) => handles.push(OwnedInterfaceHandle {
configured_peer: true,
..client.into()
}),
Err(e) => {
for spawned in handles {
rollback_unreserved_interface(spawned, InterfaceKind::Standard).await;
@@ -21,3 +21,134 @@ fn config_dir_type_lists_fill_the_filter_policy() {
load_config_dir_lists(empty.path(), &mut rc);
assert!(rc.filter.known_types.is_empty() && rc.filter.flagged_types.is_empty());
}
fn post_init(kind: &str, hop_limiter: &str) -> interface_factory::InterfacePostInit {
let mut section = ConfigSection::new();
section.set("type", kind);
section.set("hop_limiter", hop_limiter);
interface_factory::InterfacePostInit::try_from_section(&section).unwrap()
}
#[test]
fn hop_limiter_parses_like_upstream() {
for (value, expected) in [
("3", Some(3)),
("300", Some(128)),
("0", None),
("-2", None),
] {
assert_eq!(
post_init("TCPClientInterface", value).hop_limiter,
expected,
"{value}"
);
}
let mut section = ConfigSection::new();
section.set("hop_limiter", "many");
assert!(interface_factory::InterfacePostInit::try_from_section(&section).is_err());
for (kind, spawned) in [
("TCPServerInterface", true),
("BackboneInterface", true),
("I2PInterface", true),
("AutoInterface", false),
("RNodeMultiInterface", false),
] {
assert_eq!(post_init(kind, "3").spawned_hop_limiter, spawned, "{kind}");
}
}
async fn registered_hop_limiter(transport_rx: &mut mpsc::Receiver<TransportMessage>) -> Option<u8> {
let msg = transport_rx
.recv()
.await
.map(unwrap_owned_event)
.expect("registration");
let TransportMessage::RegisterInterface { entry, .. } = msg else {
panic!("expected RegisterInterface");
};
entry.hop_limiter
}
/// Reticulum.py sets `hop_limiter` on the configured interface; TCP server,
/// Backbone and I2P listeners copy it to accepted connections, while
/// AutoInterface peers, RNodeMulti subinterfaces and configured I2P peers
/// go without (bb63dd44).
#[tokio::test]
async fn hop_limiter_reaches_the_interfaces_upstream_gives_it() {
let (transport_tx, mut transport_rx) = mpsc::channel::<TransportMessage>(4);
let interface_controls: InterfaceControlMap = Arc::new(std::sync::Mutex::new(HashMap::new()));
let interface_registry = InterfaceRegistry::default();
for (parent_id, kind, child_limit) in [
(940_001, "TCPServerInterface", Some(3)),
(940_011, "AutoInterface", None),
] {
register_interface_with_post_init(
&transport_tx,
test_interface_handle(parent_id, None, kind),
&post_init(kind, "3"),
None,
&interface_controls,
&interface_registry,
InterfaceKind::Standard,
)
.await
.unwrap();
assert_eq!(
registered_hop_limiter(&mut transport_rx).await,
Some(3),
"{kind}"
);
let (role, inherited, ifac_key, ifac_size) =
child_registration_from_parent(&interface_controls, Some(parent_id));
register_interface_handle_with_role_and_overrides(
&transport_tx,
test_interface_handle(parent_id + 1, Some(parent_id), "child"),
role,
inherited,
ifac_key,
ifac_size,
&interface_controls,
&interface_registry,
InterfaceKind::Standard,
false,
)
.await
.unwrap();
assert_eq!(
registered_hop_limiter(&mut transport_rx).await,
child_limit,
"{kind}"
);
}
register_interface_with_post_init(
&transport_tx,
test_interface_handle(940_021, Some(940_020), "RNodeMulti subinterface"),
&post_init("RNodeMultiInterface", "3"),
None,
&interface_controls,
&interface_registry,
InterfaceKind::Standard,
)
.await
.unwrap();
assert_eq!(registered_hop_limiter(&mut transport_rx).await, None);
register_owned_interface_with_post_init(
&transport_tx,
OwnedInterfaceHandle {
configured_peer: true,
..test_interface_handle(940_031, None, "I2P to peer").into()
},
&post_init("I2PInterface", "3"),
None,
&interface_controls,
&interface_registry,
InterfaceKind::Standard,
None,
)
.await
.unwrap();
assert_eq!(registered_hop_limiter(&mut transport_rx).await, None);
}
+9 -2
View File
@@ -300,8 +300,15 @@ impl TransportActor {
return;
}
// Transport.HOP_LIMITER, on wire hops (Transport.py).
if parsed.hops >= self.hop_limiter {
// Interface.hop_limiter, then Transport.HOP_LIMITER, on wire hops (Transport.py).
let interface_limit = self
.interfaces
.get(&packet.interface_id)
.and_then(|entry| entry.hop_limiter)
.filter(|limit| *limit > 0);
if interface_limit.is_some_and(|limit| parsed.hops >= limit)
|| parsed.hops >= self.hop_limiter
{
trace!(
hops = parsed.hops,
interface_id = packet.interface_id,
+1
View File
@@ -2624,6 +2624,7 @@ mod tests {
mod discovery_api;
mod discovery_public_api;
mod held_announces;
mod hop_limiter;
mod inbound_queues;
mod interface_stats;
mod link_endpoint_dispatch;
@@ -0,0 +1,32 @@
//! Per-interface hop limits on inbound packets (Transport.py 1.6.1, bb63dd44).
use super::*;
/// The interface's limit applies before the instance-wide one, on wire hops;
/// 0 is off, as `None` is.
#[test]
fn interface_hop_limiter_drops_at_its_limit() {
let (mut actor, _tx) = TransportActor::new();
let mut keep = Vec::new();
for (id, limit) in [(1, Some(1)), (2, Some(0)), (3, None)] {
let (mut entry, rx) = make_test_interface(&format!("iface{id}"));
entry.hop_limiter = limit;
actor.interfaces.insert(id, entry);
keep.push(rx);
}
let mut learned = Vec::new();
for id in 1..=3 {
for hops in [0, 1] {
let (raw, dest) = make_valid_announce(&format!("hoplimit.i{id}.h{hops}"), hops);
actor.on_inbound(InboundPacket {
raw,
interface_id: id,
rssi: None,
snr: None,
q: None,
});
learned.push(actor.path_table.has_path(&dest));
}
}
assert_eq!(learned, [true, false, true, true, true, true]);
}
+4
View File
@@ -428,6 +428,9 @@ pub struct InterfaceEntry {
/// as on a radio, a serial line or UDP. A queued announce heard again on
/// an interface that is not shared is not rebroadcast there.
pub shared_medium: bool,
/// Packets arriving with this many wire hops or more are dropped before
/// the instance-wide limit; `None` or 0 is off (Transport.py).
pub hop_limiter: Option<u8>,
/// Configured announce stamp requirement; `None` follows
/// `StampPolicy::destination_cost`. Resolve with `StampPolicy::interface_destination_cost`.
pub destination_cost: Option<f64>,
@@ -520,6 +523,7 @@ impl InterfaceEntry {
announces_to_internal: None,
gravity: 0,
shared_medium: false,
hop_limiter: None,
destination_cost: None,
pr_cost: None,
received: ReceivedBytes::default(),