GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(locality): add journal-bound kernel incarnation lease gate · arencloud/unf@bbb5a65

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Cargo.lock
  • Cargo.toml
  • crates
    • unf-cni-state/src
      • lib.rs
      • tests
        • retirement.rs
    • unf-locality
      • Cargo.toml
      • examples
        • kernel_incarnation_gate.rs
      • src
        • incarnation.rs
        • incarnation
          • tests.rs
        • lib.rs
  • docs
    • adr
      • 0403-kernel-incarnation-lease-gate.md
    • development
      • phase9-attested-encryption-fabric-plan.md
      • phase9-required-locality-plan.md
    • project-status.md
  • hack
    • verify-locality-incarnation-gate.sh
  • images
    • LocalityGateTestContainerfile
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Cargo.lock‎

Copy file name to clipboardExpand all lines: Cargo.lock
+11Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎Cargo.toml‎

Copy file name to clipboardExpand all lines: Cargo.toml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members = [
1313
"crates/unf-gobgp",
1414
"crates/unf-ipam",
1515
"crates/unf-link",
16+
"crates/unf-locality",
1617
"crates/unf-loadbalancer",
1718
"crates/unf-route",
1819
"crates/unf-runtime",
Collapse file

‎crates/unf-cni-state/src/lib.rs‎

Copy file name to clipboardExpand all lines: crates/unf-cni-state/src/lib.rs
+30-5Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,18 @@ pub struct AttachmentJournal {
367367
attachments: BTreeMap<AttachmentKey, AttachmentRecord>,
368368
instance: Option<Arc<()>>,
369369
revision: u64,
370-
retirement: Option<Box<dyn AttachmentRetirement>>,
370+
retirement: Option<RetirementHook>,
371+
}
372+
373+
struct RetirementHook {
374+
owner: Arc<()>,
375+
callback: Box<dyn AttachmentRetirement>,
376+
}
377+
378+
/// Opaque identity of one hook installation in one open journal. Not a
379+
/// serialized permission; it remains distinct across journal reopen.
380+
pub struct AttachmentRetirementRegistration {
381+
owner: Arc<()>,
371382
}
372383

373384
/// Synchronous, process-local invalidation of a previously published attachment
@@ -550,16 +561,30 @@ impl AttachmentJournal {
550561
pub fn install_retirement(
551562
&mut self,
552563
retirement: Box<dyn AttachmentRetirement>,
553-
) -> Result<(), JournalError> {
564+
) -> Result<AttachmentRetirementRegistration, JournalError> {
554565
if self.retirement.is_some() || self.instance.is_none() {
555566
return Err(JournalError::InvalidTransition(
556567
"retirement installation requires a durable journal without an existing hook"
557568
.into(),
558569
));
559570
}
560-
self.retirement = Some(retirement);
571+
let owner = Arc::new(());
572+
self.retirement = Some(RetirementHook {
573+
owner: Arc::clone(&owner),
574+
callback: retirement,
575+
});
561576
self.instance = Some(Arc::new(()));
562-
Ok(())
577+
Ok(AttachmentRetirementRegistration { owner })
578+
}
579+
580+
/// Checks installation identity, independently of a changing inventory cut.
581+
/// A publisher must additionally check its exact current cut under this
582+
/// journal's transaction lock before issuing a kernel lease.
583+
#[must_use]
584+
pub fn retirement_matches(&self, registration: &AttachmentRetirementRegistration) -> bool {
585+
self.retirement
586+
.as_ref()
587+
.is_some_and(|hook| Arc::ptr_eq(&hook.owner, &registration.owner))
563588
}
564589

565590
/// Applies one validated, durable transaction operation.
@@ -676,7 +701,7 @@ impl AttachmentJournal {
676701
&& record.phase == AttachmentPhase::Ready
677702
&& let Some(token) = &record.creation_token
678703
&& let Some(retirement) = &self.retirement
679-
&& let Err(error) = retirement.retire(token)
704+
&& let Err(error) = retirement.callback.retire(token)
680705
{
681706
previous.restore(self);
682707
// The hook may have partially revoked. Never reuse a pre-failure
Collapse file

‎crates/unf-cni-state/src/tests/retirement.rs‎

Copy file name to clipboardExpand all lines: crates/unf-cni-state/src/tests/retirement.rs
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,26 @@ fn single_record_rollback_restores_failed_creation_commit_abort_and_removal() {
362362
);
363363
}
364364
}
365+
366+
#[test]
367+
fn registration_is_bound_to_one_hook_and_open_journal_not_to_a_changing_cut() {
368+
let directory = tempfile::tempdir().unwrap();
369+
let first_path = directory.path().join("first.json");
370+
let second_path = directory.path().join("second.json");
371+
let mut first = AttachmentJournal::open(&first_path, provider()).unwrap();
372+
let mut second = AttachmentJournal::open(&second_path, provider()).unwrap();
373+
let (hook, _, _) = observer(&first_path);
374+
let registration = first.install_retirement(Box::new(hook)).unwrap();
375+
let (hook, _, _) = observer(&second_path);
376+
let foreign = second.install_retirement(Box::new(hook)).unwrap();
377+
assert!(first.retirement_matches(&registration));
378+
assert!(!first.retirement_matches(&foreign));
379+
assert!(!second.retirement_matches(&registration));
380+
let cut = first.cut().unwrap();
381+
ready(&mut first, "first");
382+
assert!(!first.is_current(&cut));
383+
assert!(first.retirement_matches(&registration));
384+
drop(first);
385+
let reopened = AttachmentJournal::open(&first_path, provider()).unwrap();
386+
assert!(!reopened.retirement_matches(&registration));
387+
}
Collapse file

‎crates/unf-locality/Cargo.toml‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "unf-locality"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
9+
[dependencies]
10+
aya.workspace = true
11+
thiserror.workspace = true
12+
unf-cni-state = { path = "../unf-cni-state" }
13+
14+
[dev-dependencies]
15+
tempfile = "3.27.0"
16+
unf-ipam = { path = "../unf-ipam" }
17+
18+
[lints]
19+
workspace = true
Collapse file

‎crates/unf-locality/examples/kernel_incarnation_gate.rs‎

Copy file name to clipboard
+208Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
//! Disposable kernel-map qualification, not a packet-delivery test. Uses only
2+
//! fresh anonymous BPF maps and a private temporary journal; no host pins/links.
3+
4+
use std::{fs, path::Path};
5+
6+
use unf_cni_state::{
7+
AttachmentJournal, AttachmentKey, AttachmentPhase, AttachmentSpec,
8+
CNI_TRANSACTION_SCHEMA_VERSION, TransactionOperation, TransactionRequest,
9+
};
10+
use unf_ipam::NodeBlockProvider;
11+
use unf_locality::{IncarnationGate, IncarnationLease};
12+
13+
fn request(operation: TransactionOperation) -> TransactionRequest {
14+
TransactionRequest::new(CNI_TRANSACTION_SCHEMA_VERSION, operation)
15+
}
16+
17+
fn spec(name: &str) -> AttachmentSpec {
18+
AttachmentSpec {
19+
key: AttachmentKey {
20+
network: "locality-test".into(),
21+
container_id: name.into(),
22+
ifname: "eth0".into(),
23+
},
24+
netns: format!("/run/unf-locality-disposable/{name}"),
25+
mtu: 1400,
26+
workload_uid: Some(name.into()),
27+
}
28+
}
29+
30+
fn ready(journal: &mut AttachmentJournal, name: &str) {
31+
journal
32+
.apply(request(TransactionOperation::Prepare {
33+
attachment: spec(name),
34+
}))
35+
.unwrap();
36+
journal
37+
.apply(request(TransactionOperation::Commit {
38+
key: spec(name).key,
39+
}))
40+
.unwrap();
41+
}
42+
43+
fn map_reader(gate: &IncarnationGate) -> aya::maps::HashMap<aya::maps::MapData, [u8; 32], u64> {
44+
let data = aya::maps::MapData::from_fd(gate.duplicate_map_fd().unwrap()).unwrap();
45+
let info = data.info().unwrap();
46+
assert_eq!(info.key_size(), 32);
47+
assert_eq!(info.value_size(), 8);
48+
assert_eq!(info.max_entries(), 8);
49+
assert_eq!(info.map_flags(), 1 << 7);
50+
aya::maps::HashMap::try_from(aya::maps::Map::HashMap(data)).unwrap()
51+
}
52+
53+
fn verify_reopen(
54+
path: &Path,
55+
provider: NodeBlockProvider,
56+
gate: &IncarnationGate,
57+
old: &IncarnationLease,
58+
) {
59+
let mut reopened = AttachmentJournal::open(path, provider).unwrap();
60+
assert!(
61+
gate.issue(
62+
&reopened,
63+
&reopened.cut().unwrap(),
64+
reopened.get(&spec("second").key).unwrap()
65+
)
66+
.is_err()
67+
);
68+
let new_gate = IncarnationGate::install(&mut reopened, 8).unwrap();
69+
assert!(!new_gate.is_current(old).unwrap());
70+
let new = new_gate
71+
.issue(
72+
&reopened,
73+
&reopened.cut().unwrap(),
74+
reopened.get(&spec("second").key).unwrap(),
75+
)
76+
.unwrap();
77+
assert!(!gate.is_current(&new).unwrap());
78+
assert!(new_gate.is_current(&new).unwrap());
79+
}
80+
81+
fn verify_capacity(path: &Path, provider: NodeBlockProvider) {
82+
let mut journal = AttachmentJournal::open(path, provider).unwrap();
83+
ready(&mut journal, "first");
84+
ready(&mut journal, "second");
85+
let gate = IncarnationGate::install(&mut journal, 1).unwrap();
86+
let first = journal.get(&spec("first").key).unwrap();
87+
let second = journal.get(&spec("second").key).unwrap();
88+
let cut = journal.cut().unwrap();
89+
let lease = gate.issue(&journal, &cut, first).unwrap();
90+
assert!(gate.issue(&journal, &cut, second).is_err());
91+
assert!(gate.issue(&journal, &cut, first).is_err());
92+
assert!(gate.is_current(&lease).unwrap());
93+
journal
94+
.apply(request(TransactionOperation::BeginDelete {
95+
key: spec("first").key,
96+
}))
97+
.unwrap();
98+
assert!(!gate.is_current(&lease).unwrap());
99+
}
100+
101+
fn main() {
102+
assert_eq!(
103+
std::env::var("UNF_LOCALITY_GATE_ISOLATED_CONTAINER").as_deref(),
104+
Ok("yes"),
105+
"run only in the disposable platform qualification container"
106+
);
107+
let directory = tempfile::tempdir().unwrap();
108+
let path = directory.path().join("journal.json");
109+
let provider = NodeBlockProvider::new(
110+
"192.0.2.0/24".parse().unwrap(),
111+
"2001:db8::/120".parse().unwrap(),
112+
);
113+
let mut journal = AttachmentJournal::open(&path, provider).unwrap();
114+
ready(&mut journal, "first");
115+
ready(&mut journal, "second");
116+
let before_install = journal.cut().unwrap();
117+
let gate = IncarnationGate::install(&mut journal, 8).unwrap();
118+
assert!(!journal.is_current(&before_install));
119+
let first = journal.get(&spec("first").key).unwrap().clone();
120+
let second = journal.get(&spec("second").key).unwrap().clone();
121+
let cut = journal.cut().unwrap();
122+
assert!(gate.issue(&journal, &before_install, &first).is_err());
123+
let a = gate.issue(&journal, &cut, &first).unwrap();
124+
let b = gate.issue(&journal, &cut, &second).unwrap();
125+
assert_ne!(a.serial(), b.serial());
126+
assert!(gate.is_current(&a).unwrap());
127+
assert!(gate.is_current(&b).unwrap());
128+
assert_eq!(
129+
gate.issue(&journal, &cut, &first).unwrap().serial(),
130+
a.serial()
131+
);
132+
let reader = map_reader(&gate);
133+
assert_eq!(reader.get(a.nonce(), 0).unwrap(), a.serial());
134+
135+
// Persist failure AFTER the callback must leave the kernel lease revoked
136+
// despite restoring a Ready record in memory. Reissue requires a fresh cut
137+
// and a new serial; a stale immutable bank's old serial never matches.
138+
let temporary = path.with_extension("json.tmp");
139+
fs::create_dir(&temporary).unwrap();
140+
assert!(
141+
journal
142+
.apply(request(TransactionOperation::BeginDelete {
143+
key: first.spec.key.clone()
144+
}))
145+
.is_err()
146+
);
147+
assert_eq!(
148+
journal.get(&first.spec.key).unwrap().phase,
149+
AttachmentPhase::Ready
150+
);
151+
assert!(!gate.is_current(&a).unwrap());
152+
assert!(gate.is_current(&b).unwrap());
153+
assert!(matches!(
154+
reader.get(a.nonce(), 0),
155+
Err(aya::maps::MapError::KeyNotFound)
156+
));
157+
assert!(gate.issue(&journal, &cut, &first).is_err());
158+
fs::remove_dir(&temporary).unwrap();
159+
ready(&mut journal, "third");
160+
let fresh = journal.cut().unwrap();
161+
let a_new = gate.issue(&journal, &fresh, &first).unwrap();
162+
assert_ne!(a_new.serial(), a.serial());
163+
assert!(!gate.is_current(&a).unwrap());
164+
assert!(gate.is_current(&a_new).unwrap());
165+
assert!(gate.is_current(&b).unwrap());
166+
for _ in 0..2 {
167+
journal
168+
.apply(request(TransactionOperation::BeginDelete {
169+
key: first.spec.key.clone(),
170+
}))
171+
.unwrap();
172+
assert!(!gate.is_current(&a_new).unwrap());
173+
assert!(gate.is_current(&b).unwrap());
174+
}
175+
assert!(
176+
gate.issue(&journal, &journal.cut().unwrap(), &first)
177+
.is_err()
178+
);
179+
journal
180+
.apply(request(TransactionOperation::CompleteDelete {
181+
key: first.spec.key.clone(),
182+
}))
183+
.unwrap();
184+
ready(&mut journal, "first");
185+
let replaced = journal.get(&first.spec.key).unwrap();
186+
assert_ne!(replaced.creation_token, first.creation_token);
187+
assert!(
188+
gate.issue(&journal, &journal.cut().unwrap(), &first)
189+
.is_err()
190+
);
191+
let replacement = gate
192+
.issue(&journal, &journal.cut().unwrap(), replaced)
193+
.unwrap();
194+
assert!(gate.is_current(&replacement).unwrap());
195+
assert!(!gate.is_current(&a).unwrap());
196+
assert!(!gate.is_current(&a_new).unwrap());
197+
198+
verify_reopen(&path, provider, &gate, &b);
199+
verify_capacity(&directory.path().join("capacity.json"), provider);
200+
201+
// All descriptors/journals are local to this process; no shared pin was
202+
// created. Dropping them cannot remove any preexisting cluster resource.
203+
drop((reader, gate, journal, a, a_new, b, replacement));
204+
directory.close().unwrap();
205+
println!(
206+
"kernel-incarnation-gate: PASS schema=1 exact-revocation=true unrelated-preserved=true stale-serial-denied=true foreign-journal-denied=true cleanup=true packet-delivery-tested=false"
207+
);
208+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.