|
| 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 | +} |