|
| 1 | +//! Owned disposable startup pins. No production path, map or attachment. |
| 2 | +use std::os::fd::AsFd as _; |
| 3 | +use std::{fs, path::Path}; |
| 4 | + |
| 5 | +use anyhow::{Context as _, Result, ensure}; |
| 6 | +use aya::{ |
| 7 | + Ebpf, EbpfLoader, |
| 8 | + maps::{Array, Map, ProgramArray}, |
| 9 | + programs::SchedClassifier, |
| 10 | +}; |
| 11 | +use unf_encryption::EncryptionLocalityContext; |
| 12 | +use unf_locality::LocalityRuntimeMaps; |
| 13 | + |
| 14 | +const NAMES: [&str; 4] = [ |
| 15 | + "UL_INPUT_V1", |
| 16 | + "UL_FENCE_V1", |
| 17 | + "UL_RESUME_V1", |
| 18 | + "UL_DISPATCH_V1", |
| 19 | +]; |
| 20 | + |
| 21 | +fn program(object: &Ebpf) -> Result<&SchedClassifier> { |
| 22 | + object |
| 23 | + .program("unf_locality_fixture") |
| 24 | + .context("fixture program")? |
| 25 | + .try_into() |
| 26 | + .map_err(Into::into) |
| 27 | +} |
| 28 | + |
| 29 | +fn load(runtime: &LocalityRuntimeMaps) -> Result<Ebpf> { |
| 30 | + let mut loader = EbpfLoader::new(); |
| 31 | + runtime.configure_loader(&mut loader)?; |
| 32 | + let mut object = loader.load_file("/usr/local/lib/unf/locality-fixture")?; |
| 33 | + runtime.verify_loaded(&object)?; |
| 34 | + let classifier: &mut SchedClassifier = object |
| 35 | + .program_mut("unf_locality_fixture") |
| 36 | + .context("fixture program")? |
| 37 | + .try_into()?; |
| 38 | + classifier.load()?; |
| 39 | + Ok(object) |
| 40 | +} |
| 41 | + |
| 42 | +fn data<'a>(object: &'a Ebpf, name: &str) -> Result<&'a aya::maps::MapData> { |
| 43 | + match object.map(name).context("fixture map")? { |
| 44 | + Map::Array(data) | Map::PerCpuArray(data) | Map::ProgramArray(data) => Ok(data), |
| 45 | + _ => anyhow::bail!("unexpected map type"), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn assert_withdrawn(object: &Ebpf) -> Result<()> { |
| 50 | + let fence: Array<_, [u64; 4]> = Array::try_from(object.map("UL_FENCE_V1").context("fence")?)?; |
| 51 | + ensure!(fence.get(&0, 0)? == [0; 4], "reopen left an armed fence"); |
| 52 | + // Program-array UAPI lookup returns a fixed-width program ID, or ENOENT. |
| 53 | + let dispatch: Array<_, u32> = Array::try_from(Map::Array(aya::maps::MapData::from_fd( |
| 54 | + data(object, "UL_DISPATCH_V1")? |
| 55 | + .fd() |
| 56 | + .as_fd() |
| 57 | + .try_clone_to_owned()?, |
| 58 | + )?))?; |
| 59 | + ensure!( |
| 60 | + matches!(dispatch.get(&0, 0), Err(aya::maps::MapError::KeyNotFound)), |
| 61 | + "reopen left a selected program" |
| 62 | + ); |
| 63 | + let resume: Array<_, u32> = Array::try_from(Map::Array(aya::maps::MapData::from_fd( |
| 64 | + data(object, "UL_RESUME_V1")? |
| 65 | + .fd() |
| 66 | + .as_fd() |
| 67 | + .try_clone_to_owned()?, |
| 68 | + )?))?; |
| 69 | + for index in 0..4 { |
| 70 | + ensure!( |
| 71 | + matches!(resume.get(&index, 0), Err(aya::maps::MapError::KeyNotFound)), |
| 72 | + "reopen retained an old continuation" |
| 73 | + ); |
| 74 | + } |
| 75 | + Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +pub fn verify(bpffs: &Path, context: &EncryptionLocalityContext) -> Result<()> { |
| 79 | + ensure!( |
| 80 | + bpffs.starts_with("/tmp") && bpffs.file_name().is_some_and(|name| name == "bpffs"), |
| 81 | + "fixture bpffs scope" |
| 82 | + ); |
| 83 | + let parent = tempfile::Builder::new() |
| 84 | + .prefix("unf-locality-owned-") |
| 85 | + .tempdir_in(bpffs)?; |
| 86 | + let regular = tempfile::tempdir()?; |
| 87 | + let owner = regular.path().join("owner.lock"); |
| 88 | + let pins = parent.path().join("v1"); |
| 89 | + ensure!( |
| 90 | + LocalityRuntimeMaps::open_owned(&pins, &owner).is_err() && !pins.exists(), |
| 91 | + "missing pins silently became fresh authority" |
| 92 | + ); |
| 93 | + let mut runtime = LocalityRuntimeMaps::create_owned(&pins, &owner)?; |
| 94 | + let object = load(&runtime)?; |
| 95 | + assert_withdrawn(&object)?; |
| 96 | + ensure!( |
| 97 | + LocalityRuntimeMaps::open_owned(&pins, &owner).is_err(), |
| 98 | + "competing runtime owner accepted" |
| 99 | + ); |
| 100 | + runtime.set_applied(context)?; |
| 101 | + // A deliberately armed private table, not a production consumer. No TC |
| 102 | + // attachment and no packet invocation; reopen must clear this real table. |
| 103 | + let mut dispatch = |
| 104 | + ProgramArray::try_from(aya::maps::Map::ProgramArray(aya::maps::MapData::from_fd( |
| 105 | + data(&object, "UL_DISPATCH_V1")? |
| 106 | + .fd() |
| 107 | + .as_fd() |
| 108 | + .try_clone_to_owned()?, |
| 109 | + )?))?; |
| 110 | + dispatch.set(0, program(&object)?.fd()?, 0)?; |
| 111 | + drop(dispatch); |
| 112 | + let mut resume = ProgramArray::try_from(Map::ProgramArray(aya::maps::MapData::from_fd( |
| 113 | + data(&object, "UL_RESUME_V1")? |
| 114 | + .fd() |
| 115 | + .as_fd() |
| 116 | + .try_clone_to_owned()?, |
| 117 | + )?))?; |
| 118 | + for index in 0..4 { |
| 119 | + resume.set(index, program(&object)?.fd()?, 0)?; |
| 120 | + } |
| 121 | + drop(resume); |
| 122 | + drop(runtime); |
| 123 | + let reopened = LocalityRuntimeMaps::open_owned(&pins, &owner)?; |
| 124 | + reopened.verify_loaded(&object)?; |
| 125 | + assert_withdrawn(&object)?; |
| 126 | + let foreign = Ebpf::load_file("/usr/local/lib/unf/locality-fixture")?; |
| 127 | + ensure!( |
| 128 | + reopened.verify_loaded(&foreign).is_err(), |
| 129 | + "foreign runtime accepted by shape" |
| 130 | + ); |
| 131 | + drop(foreign); |
| 132 | + drop(reopened); |
| 133 | + |
| 134 | + let missing = pins.join("UL_FENCE_V1"); |
| 135 | + // This exact fixture-created pin is recoverable from the retained FD. |
| 136 | + fs::remove_file(&missing)?; |
| 137 | + ensure!( |
| 138 | + LocalityRuntimeMaps::open_owned(&pins, &owner).is_err(), |
| 139 | + "partial inventory accepted" |
| 140 | + ); |
| 141 | + ensure!( |
| 142 | + !missing.exists(), |
| 143 | + "partial inventory silently recreated a map" |
| 144 | + ); |
| 145 | + data(&object, "UL_FENCE_V1")?.pin(&missing)?; |
| 146 | + let extra = pins.join("unexpected"); |
| 147 | + data(&object, "UL_INPUT_V1")?.pin(&extra)?; |
| 148 | + ensure!( |
| 149 | + LocalityRuntimeMaps::open_owned(&pins, &owner).is_err(), |
| 150 | + "foreign inventory accepted" |
| 151 | + ); |
| 152 | + ensure!(extra.exists(), "foreign inventory silently removed"); |
| 153 | + fs::remove_file(&extra)?; |
| 154 | + let final_owner = LocalityRuntimeMaps::open_owned(&pins, &owner)?; |
| 155 | + final_owner.verify_loaded(&object)?; |
| 156 | + assert_withdrawn(&object)?; |
| 157 | + drop(final_owner); |
| 158 | + for name in NAMES { |
| 159 | + let path = pins.join(name); |
| 160 | + let pinned = aya::maps::MapData::from_pin(&path)?; |
| 161 | + ensure!( |
| 162 | + pinned.info()?.id() == data(&object, name)?.info()?.id(), |
| 163 | + "cleanup pin was substituted" |
| 164 | + ); |
| 165 | + fs::remove_file(path)?; |
| 166 | + } |
| 167 | + fs::remove_dir(&pins)?; |
| 168 | + drop(object); |
| 169 | + parent.close()?; |
| 170 | + regular.close()?; |
| 171 | + println!( |
| 172 | + "kernel-locality-runtime-owner: PASS atomic-create=true exclusive-owner=true reopen-withdrawn=true exact-bindings=true partial-rejected=true foreign-preserved=true cleanup=true" |
| 173 | + ); |
| 174 | + Ok(()) |
| 175 | +} |