2026-04-24 12:32:24 -06:00
|
|
|
//! Hook state for pending and active protocol flows.
|
|
|
|
|
|
|
|
|
|
use alloc::{collections::BTreeMap, string::String, vec::Vec};
|
|
|
|
|
|
|
|
|
|
/// Hook table key scoped to the hook host path.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
|
pub struct HookKey {
|
|
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
pub hook_id: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HookKey {
|
2026-04-25 11:11:19 -06:00
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn new(return_path: Vec<String>, hook_id: u64) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
return_path,
|
|
|
|
|
hook_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:37:54 -06:00
|
|
|
/// Pending hook context used only for fault attribution before activation.
|
2026-04-24 12:32:24 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2026-04-25 12:37:54 -06:00
|
|
|
pub struct PendingHook {
|
2026-04-24 12:32:24 -06:00
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
pub hook_id: u64,
|
2026-04-25 12:37:54 -06:00
|
|
|
pub caller_src_path: Vec<String>,
|
2026-04-24 12:32:24 -06:00
|
|
|
pub procedure_id: String,
|
|
|
|
|
pub dst_leaf: Option<String>,
|
2026-04-25 11:57:37 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:37:54 -06:00
|
|
|
/// Active hook context used for ordinary data traffic.
|
2026-04-25 12:15:38 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2026-04-25 12:37:54 -06:00
|
|
|
pub struct ActiveHook {
|
2026-04-25 12:15:38 -06:00
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
pub hook_id: u64,
|
2026-04-25 12:37:54 -06:00
|
|
|
pub peer_path: Vec<String>,
|
2026-04-25 12:15:38 -06:00
|
|
|
pub procedure_id: String,
|
|
|
|
|
pub dst_leaf: Option<String>,
|
2026-04-25 12:37:54 -06:00
|
|
|
pub local_ended: bool,
|
|
|
|
|
pub peer_ended: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
|
struct PeerHookKey {
|
|
|
|
|
hook_id: u64,
|
|
|
|
|
peer_path: Vec<String>,
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 14:25:35 -06:00
|
|
|
/// Duplicate hook insertion error.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub struct HookConflict;
|
|
|
|
|
|
2026-04-24 14:10:03 -06:00
|
|
|
/// Durable hook state tables.
|
2026-04-25 12:37:54 -06:00
|
|
|
#[derive(Debug, Default)]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub struct HookTable {
|
2026-04-25 12:37:54 -06:00
|
|
|
pending: BTreeMap<HookKey, PendingHook>,
|
|
|
|
|
active: BTreeMap<HookKey, ActiveHook>,
|
|
|
|
|
active_by_peer: BTreeMap<PeerHookKey, HookKey>,
|
2026-04-24 14:10:03 -06:00
|
|
|
next_id: u64,
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 14:10:03 -06:00
|
|
|
impl HookTable {
|
2026-04-25 11:11:19 -06:00
|
|
|
#[must_use]
|
2026-04-24 14:10:03 -06:00
|
|
|
pub fn allocate_hook_id(&mut self, _return_path: &[String]) -> u64 {
|
2026-04-25 12:37:54 -06:00
|
|
|
let id = self.next_id.max(1);
|
|
|
|
|
self.next_id = id.wrapping_add(1);
|
2026-04-24 14:10:03 -06:00
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:15:38 -06:00
|
|
|
pub fn insert_pending(&mut self, pending: PendingHook) -> Result<(), HookConflict> {
|
2026-04-25 12:37:54 -06:00
|
|
|
let key = HookKey::new(pending.return_path.clone(), pending.hook_id);
|
|
|
|
|
if self.pending.contains_key(&key) || self.active.contains_key(&key) {
|
2026-04-25 12:15:38 -06:00
|
|
|
return Err(HookConflict);
|
|
|
|
|
}
|
2026-04-25 12:37:54 -06:00
|
|
|
self.pending.insert(key, pending);
|
2026-04-25 12:15:38 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn activate_pending(&mut self, key: &HookKey) -> Option<()> {
|
2026-04-25 12:37:54 -06:00
|
|
|
let pending = self.pending.remove(key)?;
|
2026-04-25 12:15:38 -06:00
|
|
|
self.insert_active(ActiveHook {
|
|
|
|
|
return_path: pending.return_path,
|
|
|
|
|
hook_id: pending.hook_id,
|
|
|
|
|
peer_path: pending.caller_src_path,
|
|
|
|
|
procedure_id: pending.procedure_id,
|
|
|
|
|
dst_leaf: pending.dst_leaf,
|
|
|
|
|
local_ended: false,
|
|
|
|
|
peer_ended: false,
|
|
|
|
|
})
|
|
|
|
|
.ok()?;
|
|
|
|
|
Some(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:37:54 -06:00
|
|
|
pub fn insert_active(&mut self, active: ActiveHook) -> Result<(), HookConflict> {
|
|
|
|
|
let key = HookKey::new(active.return_path.clone(), active.hook_id);
|
|
|
|
|
let peer_key = PeerHookKey {
|
|
|
|
|
hook_id: active.hook_id,
|
|
|
|
|
peer_path: active.peer_path.clone(),
|
|
|
|
|
};
|
|
|
|
|
if self.pending.contains_key(&key)
|
|
|
|
|
|| self.active.contains_key(&key)
|
|
|
|
|
|| self.active_by_peer.contains_key(&peer_key)
|
|
|
|
|
{
|
|
|
|
|
return Err(HookConflict);
|
2026-04-25 12:15:38 -06:00
|
|
|
}
|
2026-04-25 12:37:54 -06:00
|
|
|
self.active_by_peer.insert(peer_key, key.clone());
|
|
|
|
|
self.active.insert(key, active);
|
|
|
|
|
Ok(())
|
2026-04-25 12:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:37:54 -06:00
|
|
|
pub fn remove_pending(&mut self, key: &HookKey) -> Option<PendingHook> {
|
|
|
|
|
self.pending.remove(key)
|
|
|
|
|
}
|
2026-04-25 12:15:38 -06:00
|
|
|
|
2026-04-25 12:37:54 -06:00
|
|
|
pub fn remove_active(&mut self, key: &HookKey) -> Option<ActiveHook> {
|
|
|
|
|
let active = self.active.remove(key)?;
|
|
|
|
|
self.active_by_peer.remove(&PeerHookKey {
|
|
|
|
|
hook_id: active.hook_id,
|
|
|
|
|
peer_path: active.peer_path.clone(),
|
|
|
|
|
});
|
2026-04-25 11:57:37 -06:00
|
|
|
Some(active)
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:15:38 -06:00
|
|
|
#[must_use]
|
|
|
|
|
pub fn pending(&self, key: &HookKey) -> Option<&PendingHook> {
|
2026-04-25 12:37:54 -06:00
|
|
|
self.pending.get(key)
|
2026-04-25 12:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn active(&self, key: &HookKey) -> Option<&ActiveHook> {
|
2026-04-25 12:37:54 -06:00
|
|
|
self.active.get(key)
|
2026-04-25 12:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn active_mut(&mut self, key: &HookKey) -> Option<&mut ActiveHook> {
|
2026-04-25 12:37:54 -06:00
|
|
|
self.active.get_mut(key)
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:57:37 -06:00
|
|
|
#[must_use]
|
|
|
|
|
pub fn resolve_active_key(
|
|
|
|
|
&self,
|
|
|
|
|
return_path: &[String],
|
|
|
|
|
hook_id: u64,
|
|
|
|
|
peer_path: &[String],
|
|
|
|
|
) -> Option<HookKey> {
|
|
|
|
|
let host_key = HookKey::new(return_path.to_vec(), hook_id);
|
2026-04-25 12:37:54 -06:00
|
|
|
if self.active.contains_key(&host_key) {
|
2026-04-25 11:57:37 -06:00
|
|
|
return Some(host_key);
|
2026-04-24 16:19:42 -06:00
|
|
|
}
|
2026-04-25 11:57:37 -06:00
|
|
|
self.active_by_peer
|
2026-04-25 12:37:54 -06:00
|
|
|
.get(&PeerHookKey {
|
|
|
|
|
hook_id,
|
|
|
|
|
peer_path: peer_path.to_vec(),
|
|
|
|
|
})
|
2026-04-25 11:57:37 -06:00
|
|
|
.cloned()
|
2026-04-25 12:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn mark_local_end(&mut self, key: &HookKey) -> bool {
|
|
|
|
|
let Some(active) = self.active_mut(key) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
active.local_ended = true;
|
|
|
|
|
active.peer_ended
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn mark_peer_end(&mut self, key: &HookKey) -> bool {
|
|
|
|
|
let Some(active) = self.active_mut(key) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
active.peer_ended = true;
|
|
|
|
|
active.local_ended
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2026-04-25 12:37:54 -06:00
|
|
|
pub fn active_len(&self) -> usize {
|
|
|
|
|
self.active.len()
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
#[must_use]
|
2026-04-25 12:37:54 -06:00
|
|
|
pub fn pending_len(&self) -> usize {
|
|
|
|
|
self.pending.len()
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
}
|