Skip to main content

qualia_core_db/net/peer/host/
path_cc.rs

1//! Couple extra NativePeer paths to [`PathCcTable`]. Shared bottleneck by default.
2
3use super::driver::SealedFrame;
4use super::NativePeer;
5use crate::net::qdnf::errors::QdnfError;
6use crate::net::qdnf::session::paths::{mutate_path, PathHandle, PathState};
7use crate::net::qdnf::types::ObservedLocator;
8
9impl NativePeer {
10    fn path_race_generation(&self) -> u64 {
11        let d = self.identity.digest().0;
12        let mut seed = [0u8; 8];
13        seed.copy_from_slice(&d[..8]);
14        let n = u64::from_le_bytes(seed);
15        if n == 0 || n == u64::MAX {
16            1
17        } else {
18            n
19        }
20    }
21
22    /// Default is shared: extra paths do not multiply the bottleneck window.
23    #[inline]
24    pub fn independent_bottleneck(&self) -> bool {
25        self.sessions.path_cc.independent_bottleneck
26    }
27
28    /// Sets `independent-bottleneck`.
29    pub fn set_independent_bottleneck(&mut self, independent: bool) {
30        self.sessions.path_cc.independent_bottleneck = independent;
31    }
32
33    /// Sets `path_cc_window`.
34    pub fn set_path_cc_window(&mut self, window_bytes: u64) {
35        self.sessions.path_cc.shared.window_bytes = window_bytes;
36    }
37
38    #[inline]
39    /// `NativePeer::path_cc_in_flight`.
40    pub fn path_cc_in_flight(&self) -> u64 {
41        self.sessions.path_cc.total_in_flight()
42    }
43
44    /// Open an extra path and attach it to this peer's congestion table.
45    pub fn open_extra_path(&mut self) -> Result<PathHandle, QdnfError> {
46        let id = self
47            .sessions
48            .paths
49            .start_race(self.path_race_generation())?;
50        let h = self.sessions.paths.handle_of(id)?;
51        self.sessions.paths.attest_reachability(h)?;
52        let h = mutate_path(&mut self.sessions.paths, h, PathState::Active)?;
53        self.sessions.path_cc.attach(h)?;
54        Ok(h)
55    }
56
57    /// Ensures a primary path.
58    pub(super) fn ensure_primary_path(&mut self) -> Result<PathHandle, QdnfError> {
59        if let Some(h) = self.sessions.primary_path {
60            let _ = self.sessions.path_cc.path_rtt(h)?;
61            return Ok(h);
62        }
63        let h = self.open_extra_path()?;
64        self.sessions.primary_path = Some(h);
65        Ok(h)
66    }
67
68    /// Sends protected traffic on `path`.
69    pub fn send_protected_on_path(
70        &mut self,
71        path: PathHandle,
72        dest: &ObservedLocator,
73        payload: &[u8],
74    ) -> Result<SealedFrame, QdnfError> {
75        let h = self.require_primary()?;
76        self.send_protected_with_path(h, path, dest, payload)
77    }
78}
79
80#[cfg(test)]
81mod native_peer {
82    use super::*;
83    use crate::net::peer::host::session_table::SessionHandle;
84    use crate::net::qdnf::authority::{
85        binding_for_controllers, AuthorityOwner, ContactState, InstalledSessionKeys,
86    };
87    use crate::net::qdnf::types::{Generation, ScopeEpoch};
88
89    fn activate(
90        peer: &mut NativePeer,
91        local: &[u8],
92        remote: &[u8],
93        tag: u8,
94        now: u64,
95    ) -> SessionHandle {
96        let mut owner = AuthorityOwner::new();
97        let binding = binding_for_controllers(local, remote, b"q42:QSync/1", &[b'p', tag]).unwrap();
98        let (_cred, _contact, handle) = owner
99            .install_grant(binding, now, now.saturating_add(3600), ContactState::Active)
100            .unwrap();
101        let permit = owner.issue_permit(handle, binding, now, 4096).unwrap();
102        let mut send = [tag.saturating_add(1); 32];
103        let mut recv = [tag.saturating_add(80); 32];
104        send[31] = 1;
105        recv[31] = 2;
106        let keys = InstalledSessionKeys::new(Generation(1), send, recv, true).unwrap();
107        peer.activate_protected(permit, keys, now).unwrap()
108    }
109
110    fn pair_ready() -> (NativePeer, NativePeer) {
111        let scope = ScopeEpoch { scope: 1, epoch: 1 };
112        let (mut a, mut b) = NativePeer::pair_ipc(b"did:q42:a", b"did:q42:b", scope, 1280).unwrap();
113        let now = 1_700_000_000u64;
114        a.announce(&b.locator(), now).unwrap();
115        let _ = b.accept_announce().unwrap();
116        b.announce(&a.locator(), now).unwrap();
117        let _ = a.accept_announce().unwrap();
118        let _ = activate(&mut a, b"did:q42:a", b"did:q42:b", 1, now);
119        (a, b)
120    }
121
122    #[test]
123    fn send_path_cc_shared_bottleneck_two_extra_paths() {
124        let (mut a, b) = pair_ready();
125        assert!(!a.independent_bottleneck());
126        a.set_path_cc_window(500);
127        let p1 = a.open_extra_path().unwrap();
128        let p2 = a.open_extra_path().unwrap();
129        assert_ne!(p1, p2);
130        let dest = b.locator();
131        let body = [0x11u8; 500];
132        a.send_protected_on_path(p1, &dest, &body).unwrap();
133        assert_eq!(a.path_cc_in_flight(), 500);
134        assert_eq!(
135            a.send_protected_on_path(p2, &dest, &[0x22]).unwrap_err(),
136            QdnfError::BudgetExhausted
137        );
138        assert_eq!(a.path_cc_in_flight(), 500);
139        assert_eq!(
140            a.send_protected_on_path(p2, &dest, &[0u8; 4097])
141                .unwrap_err(),
142            QdnfError::Capacity
143        );
144    }
145
146    #[test]
147    fn send_path_cc_shared_bottleneck_independent_windows() {
148        let (mut a, b) = pair_ready();
149        a.set_path_cc_window(500);
150        a.set_independent_bottleneck(true);
151        assert!(a.independent_bottleneck());
152        let p1 = a.open_extra_path().unwrap();
153        let p2 = a.open_extra_path().unwrap();
154        let dest = b.locator();
155        let body = [0x33u8; 500];
156        a.send_protected_on_path(p1, &dest, &body).unwrap();
157        a.send_protected_on_path(p2, &dest, &body).unwrap();
158        assert_eq!(a.path_cc_in_flight(), 1000);
159    }
160
161    #[test]
162    fn send_path_cc_shared_bottleneck_unknown_path_id() {
163        let (mut a, mut b) = pair_ready();
164        a.set_path_cc_window(500);
165        let p1 = a.open_extra_path().unwrap();
166        let dest = b.locator();
167        a.send_protected_on_path(p1, &dest, &[0x44; 40]).unwrap();
168        let before = a.path_cc_in_flight();
169        let id = a
170            .sessions
171            .paths
172            .start_race(a.path_race_generation())
173            .unwrap();
174        let unknown = a.sessions.paths.handle_of(id).unwrap();
175        assert_eq!(
176            a.send_protected_on_path(unknown, &dest, b"x").unwrap_err(),
177            QdnfError::Closed
178        );
179        assert_eq!(a.path_cc_in_flight(), before);
180        let foreign = b.open_extra_path().unwrap();
181        assert_eq!(
182            a.send_protected_on_path(foreign, &dest, b"y").unwrap_err(),
183            QdnfError::StaleGeneration
184        );
185        assert_eq!(a.path_cc_in_flight(), before);
186    }
187}