mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-22 23:10:33 +00:00
transport: gate announce sizes and count filtered announces in upstream's order
This commit is contained in:
1 parent
874f742c27
commit
3d04714a3b
7 files changed
+426
-48
No files matched your search
@@ -69,8 +69,8 @@ impl TransportActor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Upstream drops stamp and blackhole rejections silently; anything else
|
||||
/// is a protocol violation (Transport.py).
|
||||
/// Upstream counts stamp and blackhole rejections as filtered; anything
|
||||
/// else is a protocol violation (Transport.py).
|
||||
fn reject_announce(
|
||||
&mut self,
|
||||
header: &rns_wire::header::PacketHeader,
|
||||
@@ -89,6 +89,8 @@ impl TransportActor {
|
||||
);
|
||||
if reason == InvalidAnnounce::Invalid {
|
||||
self.protocol_violation(interface_id);
|
||||
} else {
|
||||
self.filtered_announce(interface_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,20 +384,13 @@ impl TransportActor {
|
||||
self.count_rx_packet();
|
||||
}
|
||||
|
||||
// Destination-type filtering runs after the packet filter and before
|
||||
// the hash is remembered (Transport.py).
|
||||
// Announce gates run after the packet filter and before the hash is
|
||||
// remembered (Transport.py).
|
||||
if parsed.flags.packet_type == rns_wire::flags::PacketType::Announce
|
||||
&& !self.admit_announce_type(&raw[data_offset..], parsed.hops, packet.interface_id)
|
||||
&& !self.admit_announce_ingress(&raw, &parsed, data_offset, packet.interface_id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Transport.py: announce frames above the base MTU.
|
||||
if parsed.flags.packet_type == rns_wire::flags::PacketType::Announce
|
||||
&& raw.len() > rns_wire::constants::MTU
|
||||
{
|
||||
self.protocol_violation(packet.interface_id);
|
||||
return;
|
||||
}
|
||||
|
||||
if check_hashlist {
|
||||
self.packet_hashlist.insert(pkt_hash);
|
||||
@@ -430,28 +425,46 @@ impl TransportActor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Python's announce ingress filter: a short name hash is a protocol
|
||||
/// violation, blocked and unrequired types are dropped silently.
|
||||
fn admit_announce_type(
|
||||
/// Python's announce ingress in `preprocess_inbound`, in its order: the
|
||||
/// frame and payload sizes (protocol violations), a blocked type (silent),
|
||||
/// then an unrequired type (filtered) (Transport.py).
|
||||
fn admit_announce_ingress(
|
||||
&mut self,
|
||||
payload: &[u8],
|
||||
wire_hops: u8,
|
||||
raw: &[u8],
|
||||
header: &rns_wire::header::PacketHeader,
|
||||
data_offset: usize,
|
||||
interface_id: InterfaceId,
|
||||
) -> bool {
|
||||
use crate::destination_filter::{AnnounceSource, NAME_HASH_LEN, NameHash};
|
||||
const KEYSIZE: usize = 64;
|
||||
let Some(name_hash) = payload
|
||||
.get(KEYSIZE..KEYSIZE + NAME_HASH_LEN)
|
||||
.and_then(|bytes| NameHash::try_from(bytes).ok())
|
||||
else {
|
||||
use rns_wire::constants::{KEYSIZE, MTU, TRUNCATED_HASHLENGTH};
|
||||
const KEY_LEN: usize = KEYSIZE / 8;
|
||||
const RANDOM_BLOB_LEN: usize = 10;
|
||||
let payload = &raw[data_offset..];
|
||||
// A HEADER_1 announce must leave room for a relay's transport id.
|
||||
let oversize = raw.len() > MTU
|
||||
|| (header.flags.header_type == rns_wire::flags::HeaderType::Header1
|
||||
&& raw.len() > MTU - TRUNCATED_HASHLENGTH / 8);
|
||||
if oversize || payload.len() < KEY_LEN + NAME_HASH_LEN + RANDOM_BLOB_LEN {
|
||||
debug!(
|
||||
interface_id,
|
||||
size = payload.len(),
|
||||
"malformed announce packet"
|
||||
frame = raw.len(),
|
||||
payload = payload.len(),
|
||||
"announce size violation"
|
||||
);
|
||||
self.protocol_violation(interface_id);
|
||||
return false;
|
||||
};
|
||||
}
|
||||
let name_hash: NameHash = payload[KEY_LEN..KEY_LEN + NAME_HASH_LEN]
|
||||
.try_into()
|
||||
.expect("payload length checked");
|
||||
if self.destination_filter.is_blocked(&name_hash) {
|
||||
trace!(
|
||||
interface_id,
|
||||
destination_type = self.destination_filter.type_name(&name_hash),
|
||||
"announce dropped by destination type filter"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
let source = if self.is_local_client_interface(interface_id) {
|
||||
AnnounceSource::LocalClient
|
||||
} else if self.is_shared_instance_peer_interface(interface_id) {
|
||||
@@ -459,17 +472,19 @@ impl TransportActor {
|
||||
} else {
|
||||
AnnounceSource::Other
|
||||
};
|
||||
let admitted = self
|
||||
if !self
|
||||
.destination_filter
|
||||
.admits(&name_hash, source, wire_hops);
|
||||
if !admitted {
|
||||
.admits(&name_hash, source, header.hops)
|
||||
{
|
||||
trace!(
|
||||
interface_id,
|
||||
destination_type = self.destination_filter.type_name(&name_hash),
|
||||
"announce dropped by destination type filter"
|
||||
"announce filtered as not required"
|
||||
);
|
||||
self.filtered_announce(interface_id);
|
||||
return false;
|
||||
}
|
||||
admitted
|
||||
true
|
||||
}
|
||||
|
||||
fn process_announce(
|
||||
@@ -612,27 +627,34 @@ impl TransportActor {
|
||||
.discovery_path_requests
|
||||
.get(&header.destination_hash)
|
||||
.is_some_and(|request| now_f64() < request.timeout);
|
||||
self.record_interface_event(interface_id, InterfaceEvent::ReceivedAnnounce, raw.len());
|
||||
self.count_transport_event(InterfaceEvent::ReceivedAnnounce);
|
||||
// A held announce samples the frequency after the limiter decision,
|
||||
// with no bytes and no transport count (Transport.py).
|
||||
if let Some(entry) = self.interfaces.get_mut(&interface_id)
|
||||
&& !is_from_local_client
|
||||
&& !announced_destination_known
|
||||
&& !answers_path_request
|
||||
&& entry.ingress.should_ingress_limit()
|
||||
{
|
||||
entry.ingress.hold_announce(crate::ingress::HeldAnnounce {
|
||||
let outcome = entry.ingress.hold_announce(crate::ingress::HeldAnnounce {
|
||||
raw: bytes::Bytes::copy_from_slice(raw),
|
||||
destination_hash: header.destination_hash,
|
||||
hops: header.hops,
|
||||
receiving_interface_id: interface_id,
|
||||
});
|
||||
if outcome.is_filtered() {
|
||||
self.filtered_announce(interface_id);
|
||||
}
|
||||
self.record_interface_event(interface_id, InterfaceEvent::ReceivedAnnounce, 0);
|
||||
debug!(
|
||||
dest = hex::encode(header.destination_hash),
|
||||
interface = interface_id,
|
||||
?outcome,
|
||||
"announce held by ingress controller (burst active)"
|
||||
);
|
||||
return;
|
||||
}
|
||||
self.record_interface_event(interface_id, InterfaceEvent::ReceivedAnnounce, raw.len());
|
||||
self.count_transport_event(InterfaceEvent::ReceivedAnnounce);
|
||||
|
||||
if let Some(validated_identity) = validated_identity.as_ref() {
|
||||
let known_public_key = self
|
||||
|
||||
@@ -2589,6 +2589,7 @@ fn mode_discovers_unknown_paths(mode: InterfaceMode) -> bool {
|
||||
mod tests {
|
||||
mod announce_admission;
|
||||
mod announce_identity;
|
||||
mod announce_ingress;
|
||||
mod bitrate_timing;
|
||||
mod discovery_api;
|
||||
mod discovery_public_api;
|
||||
|
||||
@@ -145,6 +145,7 @@ impl TransportActor {
|
||||
announces_sent: entry.ingress.announces_sent(),
|
||||
path_requests_received: entry.ingress.path_requests_received(),
|
||||
path_requests_sent: entry.ingress.path_requests_sent(),
|
||||
announces_filtered: entry.ingress.announces_filtered(),
|
||||
protocol_violations: entry.protocol_violations,
|
||||
ifac_violations: entry.ifac_violations,
|
||||
packet_filter_hits: entry.packet_filter_hits,
|
||||
@@ -881,6 +882,20 @@ impl TransportActor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Python `Interface.filtered_announce`: the interface, its parent and the
|
||||
/// transport total (Interface.py).
|
||||
pub(super) fn filtered_announce(&mut self, id: InterfaceId) {
|
||||
let Some(entry) = self.interfaces.get_mut(&id) else {
|
||||
return;
|
||||
};
|
||||
entry.ingress.filtered_announce();
|
||||
let parent_id = entry.parent_id;
|
||||
self.traffic_count.totals.announce_filtered_packets += 1;
|
||||
if let Some(parent) = parent_id.and_then(|parent| self.interfaces.get_mut(&parent)) {
|
||||
parent.ingress.filtered_announce();
|
||||
}
|
||||
}
|
||||
|
||||
/// Python `Transport.rx_packets += 1` once a packet passes the packet
|
||||
/// filter (Transport.py).
|
||||
pub(super) fn count_rx_packet(&mut self) {
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
//! Python 1.6.1 announce ingress (Transport.py `preprocess_inbound`): the size
|
||||
//! gates, the type filters, the filtered count `afrxc`, and the accounting
|
||||
//! of held announces (Interface.py).
|
||||
use super::*;
|
||||
use crate::ingress::{IngressController, IngressOverrides};
|
||||
use rns_wire::context::PacketContext;
|
||||
|
||||
const NOW: f64 = 1_800_000_000.0;
|
||||
const APP: &str = "prefilter.test";
|
||||
|
||||
struct Node {
|
||||
actor: TransportActor,
|
||||
_rx: Vec<mpsc::Receiver<Bytes>>,
|
||||
}
|
||||
|
||||
fn node(transport: bool) -> Node {
|
||||
let (mut actor, _) = TransportActor::new();
|
||||
actor.is_transport_enabled = transport;
|
||||
actor.transport_identity_hash = Some([0x42; 16]);
|
||||
let mut receivers = Vec::new();
|
||||
for id in [1, 2] {
|
||||
let (mut entry, rx) = make_test_interface(&format!("prefilter_{id}"));
|
||||
entry.ingress = IngressController::disabled();
|
||||
actor.interfaces.insert(id, entry);
|
||||
receivers.push(rx);
|
||||
}
|
||||
Node {
|
||||
actor,
|
||||
_rx: receivers,
|
||||
}
|
||||
}
|
||||
|
||||
struct Announce {
|
||||
raw: Bytes,
|
||||
destination: [u8; 16],
|
||||
}
|
||||
|
||||
/// A signed announce emitted at `emitted` (Unix seconds).
|
||||
fn signed(
|
||||
identity: &rns_identity::identity::Identity,
|
||||
emitted: u64,
|
||||
hops: u8,
|
||||
context: PacketContext,
|
||||
app_data: &[u8],
|
||||
transport_id: Option<[u8; 16]>,
|
||||
) -> Announce {
|
||||
let app_data = (!app_data.is_empty()).then_some(app_data);
|
||||
let data =
|
||||
rns_identity::announce::AnnounceData::create_at(identity, APP, app_data, None, emitted)
|
||||
.unwrap();
|
||||
let destination = rns_identity::destination::Destination::hash_from_name_and_identity(
|
||||
APP,
|
||||
Some(&identity.hash),
|
||||
);
|
||||
let header = rns_wire::header::PacketHeader {
|
||||
flags: rns_wire::flags::PacketFlags {
|
||||
header_type: if transport_id.is_some() {
|
||||
rns_wire::flags::HeaderType::Header2
|
||||
} else {
|
||||
rns_wire::flags::HeaderType::Header1
|
||||
},
|
||||
context_flag: false,
|
||||
transport_type: if transport_id.is_some() {
|
||||
rns_wire::flags::TransportType::Transport
|
||||
} else {
|
||||
rns_wire::flags::TransportType::Broadcast
|
||||
},
|
||||
destination_type: rns_wire::flags::DestinationType::Single,
|
||||
packet_type: rns_wire::flags::PacketType::Announce,
|
||||
},
|
||||
hops,
|
||||
transport_id,
|
||||
destination_hash: destination,
|
||||
context,
|
||||
};
|
||||
let mut raw = header.pack();
|
||||
raw.extend_from_slice(&data.pack());
|
||||
Announce {
|
||||
raw: Bytes::from(raw),
|
||||
destination,
|
||||
}
|
||||
}
|
||||
|
||||
fn plain(identity: &rns_identity::identity::Identity, emitted: u64) -> Announce {
|
||||
signed(identity, emitted, 1, PacketContext::None, &[], None)
|
||||
}
|
||||
|
||||
fn inbound(actor: &mut TransportActor, raw: &Bytes, interface_id: InterfaceId) {
|
||||
actor.on_inbound(InboundPacket {
|
||||
raw: raw.clone(),
|
||||
interface_id,
|
||||
rssi: None,
|
||||
snr: None,
|
||||
q: None,
|
||||
});
|
||||
}
|
||||
|
||||
fn stats(actor: &mut TransportActor, id: InterfaceId) -> crate::messages::InterfaceStatRpcEntry {
|
||||
let TransportQueryResponse::InterfaceStats(entries) =
|
||||
actor.handle_query(TransportQuery::GetInterfaceStats)
|
||||
else {
|
||||
panic!("unexpected interface stats response");
|
||||
};
|
||||
entries
|
||||
.into_iter()
|
||||
.find(|entry| entry.id == id)
|
||||
.expect("interface is listed")
|
||||
}
|
||||
|
||||
fn totals(actor: &mut TransportActor) -> crate::messages::TrafficTotalsRpcEntry {
|
||||
let TransportQueryResponse::TrafficTotals(totals) =
|
||||
actor.handle_query(TransportQuery::GetTrafficTotals)
|
||||
else {
|
||||
panic!("unexpected traffic totals response");
|
||||
};
|
||||
totals
|
||||
}
|
||||
|
||||
/// `(arxc, arxb, afrxc)` on an interface.
|
||||
fn counts(actor: &mut TransportActor, id: InterfaceId) -> (u64, u64, u64) {
|
||||
let entry = stats(actor, id);
|
||||
(
|
||||
entry.announces_received,
|
||||
entry.announce_rx_bytes,
|
||||
entry.announces_filtered,
|
||||
)
|
||||
}
|
||||
|
||||
/// Python 1.6.1: a frame over the MTU, a HEADER_1 announce that leaves no
|
||||
/// room for a transport id, and a payload under 84 bytes are protocol
|
||||
/// violations, checked before the type filters (Transport.py). 317 bytes of
|
||||
/// app data is upstream's largest HEADER_1 announce: `Packet.pack` built a
|
||||
/// 484-byte frame and refused 318 bytes (`RNS.Packet(...).pack()`, 1.6.1).
|
||||
#[test]
|
||||
fn announce_size_gates_come_before_the_type_filters() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
let violations = |node: &Node| node.actor.interfaces[&1].protocol_violations;
|
||||
|
||||
let fits = signed(
|
||||
&rns_identity::identity::Identity::new(),
|
||||
600,
|
||||
1,
|
||||
PacketContext::None,
|
||||
&[0xA5; 317],
|
||||
None,
|
||||
);
|
||||
assert_eq!(fits.raw.len(), 484);
|
||||
inbound(&mut node.actor, &fits.raw, 1);
|
||||
assert!(node.actor.path_table.get(&fits.destination).is_some());
|
||||
|
||||
let too_long = signed(
|
||||
&rns_identity::identity::Identity::new(),
|
||||
600,
|
||||
1,
|
||||
PacketContext::None,
|
||||
&[0xA5; 318],
|
||||
None,
|
||||
);
|
||||
inbound(&mut node.actor, &too_long.raw, 1);
|
||||
assert!(node.actor.path_table.get(&too_long.destination).is_none());
|
||||
assert_eq!(violations(&node), 1);
|
||||
|
||||
let relayed = signed(
|
||||
&rns_identity::identity::Identity::new(),
|
||||
600,
|
||||
1,
|
||||
PacketContext::None,
|
||||
&[0xA5; 317],
|
||||
Some([0x77; 16]),
|
||||
);
|
||||
assert_eq!(relayed.raw.len(), 500);
|
||||
inbound(&mut node.actor, &relayed.raw, 1);
|
||||
assert!(node.actor.path_table.get(&relayed.destination).is_some());
|
||||
assert_eq!(violations(&node), 1);
|
||||
|
||||
node.actor.destination_filter.block("blocked.type");
|
||||
let mut short = rns_wire::header::PacketHeader::unpack(&fits.raw).unwrap().0;
|
||||
short.destination_hash = [0x31; 16];
|
||||
let mut short = short.pack();
|
||||
short.extend_from_slice(&[0x11; 64]);
|
||||
short.extend_from_slice(&rns_identity::name_hash::name_hash("blocked.type"));
|
||||
short.extend_from_slice(&[0x22; 9]);
|
||||
inbound(&mut node.actor, &Bytes::from(short), 1);
|
||||
assert_eq!(violations(&node), 2, "83 bytes of payload");
|
||||
assert_eq!(counts(&mut node.actor, 1).2, 0);
|
||||
}
|
||||
|
||||
/// A blocked type is dropped silently; an unrequired one counts as filtered
|
||||
/// (Transport.py).
|
||||
#[test]
|
||||
fn only_an_unrequired_type_counts_as_filtered() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
let identity = rns_identity::identity::Identity::new();
|
||||
node.actor.destination_filter.block(APP);
|
||||
inbound(&mut node.actor, &plain(&identity, 700).raw, 1);
|
||||
assert_eq!(counts(&mut node.actor, 1), (0, 0, 0));
|
||||
|
||||
let mut node = self::node(false);
|
||||
node.actor.destination_filter.set_required_only(true);
|
||||
inbound(&mut node.actor, &plain(&identity, 700).raw, 1);
|
||||
assert_eq!(counts(&mut node.actor, 1), (0, 0, 1));
|
||||
assert_eq!(node.actor.interfaces[&1].protocol_violations, 0);
|
||||
}
|
||||
|
||||
fn bursting(actor: &mut TransportActor, overrides: IngressOverrides) {
|
||||
let ingress = &mut actor.interfaces.get_mut(&1).unwrap().ingress;
|
||||
*ingress = IngressController::try_with_overrides(&overrides).unwrap();
|
||||
for _ in 0..50 {
|
||||
ingress.received_announce();
|
||||
}
|
||||
assert!(ingress.should_ingress_limit(), "precondition: a burst");
|
||||
}
|
||||
|
||||
/// A held announce counts with no bytes and no transport count; its release
|
||||
/// counts its bytes once (Transport.py, Interface.py).
|
||||
#[test]
|
||||
fn a_held_announce_counts_its_bytes_only_when_released() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
bursting(&mut node.actor, IngressOverrides::default());
|
||||
let (arxc, arxb, _) = counts(&mut node.actor, 1);
|
||||
let rx_packets = totals(&mut node.actor).announce_rx_packets;
|
||||
let announce = plain(&rns_identity::identity::Identity::new(), 800);
|
||||
|
||||
inbound(&mut node.actor, &announce.raw, 1);
|
||||
|
||||
assert_eq!(node.actor.interfaces[&1].ingress.held_count(), 1);
|
||||
assert_eq!(counts(&mut node.actor, 1), (arxc + 1, arxb, 0));
|
||||
assert_eq!(totals(&mut node.actor).announce_rx_packets, rx_packets);
|
||||
|
||||
let held = std::mem::take(&mut node.actor.interfaces.get_mut(&1).unwrap().ingress)
|
||||
.cull_and_release(&[])
|
||||
.unwrap();
|
||||
node.actor.on_released_announce(held);
|
||||
assert!(node.actor.path_table.get(&announce.destination).is_some());
|
||||
assert_eq!(
|
||||
counts(&mut node.actor, 1),
|
||||
(1, announce.raw.len() as u64, 0),
|
||||
"a calm interface releases and counts the bytes"
|
||||
);
|
||||
assert_eq!(totals(&mut node.actor).announce_rx_packets, rx_packets + 1);
|
||||
}
|
||||
|
||||
/// Interface.py `hold_announce`: a replaced held entry and a refused one are
|
||||
/// both filtered.
|
||||
#[test]
|
||||
fn refused_and_replaced_holds_count_as_filtered() {
|
||||
let _clock = crate::test_clock::Clock::at(NOW);
|
||||
let mut node = node(false);
|
||||
bursting(
|
||||
&mut node.actor,
|
||||
IngressOverrides {
|
||||
max_held: Some(1),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let first = rns_identity::identity::Identity::new();
|
||||
inbound(&mut node.actor, &plain(&first, 900).raw, 1);
|
||||
assert_eq!(counts(&mut node.actor, 1).2, 0);
|
||||
inbound(
|
||||
&mut node.actor,
|
||||
&plain(&rns_identity::identity::Identity::new(), 900).raw,
|
||||
1,
|
||||
);
|
||||
assert_eq!(
|
||||
counts(&mut node.actor, 1).2,
|
||||
1,
|
||||
"refused: the buffer is full"
|
||||
);
|
||||
inbound(&mut node.actor, &plain(&first, 901).raw, 1);
|
||||
assert_eq!(counts(&mut node.actor, 1).2, 2, "replaced");
|
||||
assert_eq!(node.actor.interfaces[&1].ingress.held_count(), 1);
|
||||
}
|
||||
@@ -137,6 +137,11 @@ impl DestinationTypeFilter {
|
||||
self.names.get(hash).map_or("unknown", String::as_str)
|
||||
}
|
||||
|
||||
/// Python drops a blocked type silently, ahead of the required-only check.
|
||||
pub fn is_blocked(&self, hash: &NameHash) -> bool {
|
||||
self.blocked.contains(hash)
|
||||
}
|
||||
|
||||
/// The decision inside Python `Transport.inbound` (Transport.py),
|
||||
/// given the wire hop count before the inbound increment.
|
||||
pub fn admits(&self, hash: &NameHash, source: AnnounceSource, wire_hops: u8) -> bool {
|
||||
|
||||
@@ -117,6 +117,25 @@ pub struct HeldAnnounce {
|
||||
pub receiving_interface_id: u64,
|
||||
}
|
||||
|
||||
/// What [`IngressController::hold_announce`] did with an announce. Python
|
||||
/// counts a replacement and a refusal as filtered (Interface.py).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum HoldOutcome {
|
||||
/// Stored in a free slot.
|
||||
Held,
|
||||
/// Replaced the announce held for the same destination.
|
||||
Replaced,
|
||||
/// Not stored: at the hop limit, or the buffer is full.
|
||||
Refused,
|
||||
}
|
||||
|
||||
impl HoldOutcome {
|
||||
/// Whether Python's `hold_announce` calls `filtered_announce` (Interface.py).
|
||||
pub fn is_filtered(self) -> bool {
|
||||
self != Self::Held
|
||||
}
|
||||
}
|
||||
|
||||
/// Monotonic-clock samples and limiter state for one interface. Record events
|
||||
/// through the `received_*`/`sent_*` methods before consulting limiters. Every
|
||||
/// frequency read, status getters included, drops at most one stale sample.
|
||||
@@ -150,6 +169,8 @@ pub struct IngressController {
|
||||
announces_sent: u64,
|
||||
path_requests_received: u64,
|
||||
path_requests_sent: u64,
|
||||
/// Python `afrxc`, including spawned children's.
|
||||
announces_filtered: u64,
|
||||
/// Python `arxb`/`atxb`/`prxb`/`ptxb`, including spawned children's.
|
||||
announce_rx_bytes: u64,
|
||||
announce_tx_bytes: u64,
|
||||
@@ -193,6 +214,7 @@ impl IngressController {
|
||||
announces_sent: 0,
|
||||
path_requests_received: 0,
|
||||
path_requests_sent: 0,
|
||||
announces_filtered: 0,
|
||||
announce_rx_bytes: 0,
|
||||
announce_tx_bytes: 0,
|
||||
path_request_rx_bytes: 0,
|
||||
@@ -428,6 +450,17 @@ impl IngressController {
|
||||
self.path_requests_sent
|
||||
}
|
||||
|
||||
/// Python `filtered_announce` on this interface, or a spawned child's on
|
||||
/// its parent: the count only (Interface.py).
|
||||
pub fn filtered_announce(&mut self) {
|
||||
self.announces_filtered = self.announces_filtered.saturating_add(1);
|
||||
}
|
||||
|
||||
/// Python `afrxc`.
|
||||
pub fn announces_filtered(&self) -> u64 {
|
||||
self.announces_filtered
|
||||
}
|
||||
|
||||
pub fn incoming_announce_frequency(&mut self) -> f64 {
|
||||
frequency_from_deque_mut(
|
||||
&mut self.ia_freq_deque,
|
||||
@@ -575,11 +608,11 @@ impl IngressController {
|
||||
/// its packet. Discard hop counts of 127 or more and new destinations when
|
||||
/// `max_held` is reached; existing destinations can still be replaced.
|
||||
/// This does not consult the enabled flag or a limiter decision.
|
||||
pub fn hold_announce(&mut self, announce: HeldAnnounce) {
|
||||
pub fn hold_announce(&mut self, announce: HeldAnnounce) -> HoldOutcome {
|
||||
// Python 1.3.8 Interface.py: announces already at the hop cap
|
||||
// would be dropped by every receiver on release — don't hold them.
|
||||
if announce.hops >= PATHFINDER_M - 1 {
|
||||
return;
|
||||
return HoldOutcome::Refused;
|
||||
}
|
||||
if let Some(held) = self
|
||||
.held_announces
|
||||
@@ -587,8 +620,12 @@ impl IngressController {
|
||||
.find(|held| held.destination_hash == announce.destination_hash)
|
||||
{
|
||||
*held = announce;
|
||||
HoldOutcome::Replaced
|
||||
} else if self.held_announces.len() < self.max_held {
|
||||
self.held_announces.push(announce);
|
||||
HoldOutcome::Held
|
||||
} else {
|
||||
HoldOutcome::Refused
|
||||
}
|
||||
}
|
||||
|
||||
@@ -599,25 +636,37 @@ impl IngressController {
|
||||
/// while burst state remains set. The returned entry is removed; the caller
|
||||
/// must process it, since this helper does not send packets.
|
||||
pub fn try_release_held(&mut self) -> Option<HeldAnnounce> {
|
||||
if self.held_announces.is_empty() {
|
||||
if !self.release_due() {
|
||||
return None;
|
||||
}
|
||||
self.cull_and_release(&[])
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
if now < self.held_release {
|
||||
return None;
|
||||
/// The release gate of [`Self::try_release_held`]: something is held, the
|
||||
/// release time has come and the frequency is below the threshold.
|
||||
pub fn release_due(&mut self) -> bool {
|
||||
if self.held_announces.is_empty() || Instant::now() < self.held_release {
|
||||
return false;
|
||||
}
|
||||
|
||||
let freq_threshold = if self.age() < self.new_time {
|
||||
self.burst_freq_new
|
||||
} else {
|
||||
self.burst_freq
|
||||
};
|
||||
self.incoming_announce_frequency() < freq_threshold
|
||||
}
|
||||
|
||||
if self.incoming_announce_frequency() >= freq_threshold {
|
||||
return None;
|
||||
}
|
||||
/// Held announces in the order they were first held.
|
||||
pub fn held_announces(&self) -> impl Iterator<Item = &HeldAnnounce> {
|
||||
self.held_announces.iter()
|
||||
}
|
||||
|
||||
/// Drop the held announces for `culled` destinations, then release the
|
||||
/// rest as [`Self::try_release_held`] does, without its gate. Python culls
|
||||
/// only when a release is due (Interface.py).
|
||||
pub fn cull_and_release(&mut self, culled: &[[u8; 16]]) -> Option<HeldAnnounce> {
|
||||
self.held_announces
|
||||
.retain(|held| !culled.contains(&held.destination_hash));
|
||||
let mut best: Option<usize> = None;
|
||||
for (idx, held) in self.held_announces.iter().enumerate() {
|
||||
if best.is_none_or(|b| held.hops < self.held_announces[b].hops) {
|
||||
@@ -625,6 +674,7 @@ impl IngressController {
|
||||
}
|
||||
}
|
||||
let announce = self.held_announces.remove(best?);
|
||||
let now = Instant::now();
|
||||
self.held_release = now
|
||||
.checked_add(Duration::from_secs_f64(self.held_release_interval))
|
||||
.unwrap_or(now);
|
||||
@@ -979,7 +1029,7 @@ mod tests {
|
||||
hops: 1,
|
||||
receiving_interface_id: 1,
|
||||
};
|
||||
ctrl.hold_announce(a);
|
||||
assert_eq!(ctrl.hold_announce(a), HoldOutcome::Held);
|
||||
assert_eq!(ctrl.held_count(), 1);
|
||||
|
||||
let a2 = HeldAnnounce {
|
||||
@@ -988,7 +1038,7 @@ mod tests {
|
||||
hops: 2,
|
||||
receiving_interface_id: 1,
|
||||
};
|
||||
ctrl.hold_announce(a2);
|
||||
assert_eq!(ctrl.hold_announce(a2), HoldOutcome::Replaced);
|
||||
assert_eq!(ctrl.held_count(), 1);
|
||||
|
||||
let a3 = HeldAnnounce {
|
||||
@@ -997,7 +1047,7 @@ mod tests {
|
||||
hops: 0,
|
||||
receiving_interface_id: 1,
|
||||
};
|
||||
ctrl.hold_announce(a3);
|
||||
assert_eq!(ctrl.hold_announce(a3), HoldOutcome::Held);
|
||||
assert_eq!(ctrl.held_count(), 2);
|
||||
}
|
||||
|
||||
@@ -1005,12 +1055,13 @@ mod tests {
|
||||
fn hold_announce_ignores_max_hop_announces() {
|
||||
// Python 1.3.8 Interface.py: hops >= PATHFINDER_M-1 not held.
|
||||
let mut ctrl = IngressController::new();
|
||||
ctrl.hold_announce(HeldAnnounce {
|
||||
let outcome = ctrl.hold_announce(HeldAnnounce {
|
||||
raw: Bytes::from_static(&[1, 2, 3]),
|
||||
destination_hash: [0xA1; 16],
|
||||
hops: PATHFINDER_M - 1,
|
||||
receiving_interface_id: 1,
|
||||
});
|
||||
assert_eq!(outcome, HoldOutcome::Refused);
|
||||
assert_eq!(ctrl.held_count(), 0);
|
||||
|
||||
ctrl.hold_announce(HeldAnnounce {
|
||||
@@ -1038,12 +1089,15 @@ mod tests {
|
||||
}
|
||||
assert_eq!(ctrl.held_count(), MAX_HELD_ANNOUNCES);
|
||||
|
||||
ctrl.hold_announce(HeldAnnounce {
|
||||
let outcome = ctrl.hold_announce(HeldAnnounce {
|
||||
raw: Bytes::new(),
|
||||
destination_hash: [0xFF; 16],
|
||||
hops: 0,
|
||||
receiving_interface_id: 1,
|
||||
});
|
||||
assert_eq!(outcome, HoldOutcome::Refused);
|
||||
assert!(outcome.is_filtered() && HoldOutcome::Replaced.is_filtered());
|
||||
assert!(!HoldOutcome::Held.is_filtered());
|
||||
assert_eq!(ctrl.held_count(), MAX_HELD_ANNOUNCES);
|
||||
}
|
||||
}
|
||||
@@ -1331,6 +1331,10 @@ pub struct InterfaceStatRpcEntry {
|
||||
pub announces_sent: u64,
|
||||
pub path_requests_received: u64,
|
||||
pub path_requests_sent: u64,
|
||||
/// Python `afrxc`: announces dropped by the type filter, the prefilter,
|
||||
/// a stamp or blackhole check, or a refused hold; a parent counts its
|
||||
/// children's.
|
||||
pub announces_filtered: u64,
|
||||
pub protocol_violations: u64,
|
||||
pub ifac_violations: u64,
|
||||
pub packet_filter_hits: u64,
|
||||
@@ -1409,6 +1413,8 @@ pub struct TrafficTotalsRpcEntry {
|
||||
pub tx_packets: u64,
|
||||
pub announce_rx_packets: u64,
|
||||
pub announce_tx_packets: u64,
|
||||
/// Python `Transport.filtered_arxc`, which upstream does not export.
|
||||
pub announce_filtered_packets: u64,
|
||||
pub pr_rx_packets: u64,
|
||||
pub pr_tx_packets: u64,
|
||||
pub link_count: u64,
|
||||
|
||||
Reference in new issue
Block a user