Skip to content

Commit 8eade52

Browse files
committed
Use Aya in the userspace
This change replaces libbpf-rs with Aya as a loader of eBPF programs in the userspace part in lockc. eBPF programs still remain written in C and are going to be rewritten in Rust in separate changes. Signed-off-by: Michal Rostecki <[email protected]>
1 parent e23b92f commit 8eade52

File tree

13 files changed

+651
-733
lines changed

13 files changed

+651
-733
lines changed

lockc/Cargo.toml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,32 @@ license = "Apache-2.0 AND GPL-2.0-or-later"
1212
[badges]
1313
maintenance = { status = "actively-developed" }
1414

15-
[lib]
16-
name = "lockc"
17-
1815
[dependencies]
1916
anyhow = "1.0"
17+
# TODO(vadorovsky): Switch to main branch as soon as the followinng PRs
18+
# are merged:
19+
# * https://github.com/aya-rs/aya/pull/177
20+
# * https://github.com/aya-rs/aya/pull/179
21+
aya = { git = "https://github.com/dave-tucker/aya", branch = "lockc", features=["async_tokio"] }
2022
bindgen = "0.59"
2123
byteorder = "1.4"
22-
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2324
config = { version = "0.11", default-features = false, features = ["toml"] }
24-
ctrlc = "3.2"
2525
fanotify-rs = { git = "https://github.com/vadorovsky/fanotify-rs", branch = "fix-pid-type" }
2626
futures = "0.3"
27-
goblin = "0.4"
2827
kube = "0.66"
2928
k8s-openapi = { version = "0.13", default-features = false, features = ["v1_21"] }
3029
lazy_static = "1.4"
3130
libc = { version = "0.2", features = [ "extra_traits" ] }
32-
libbpf-rs = "0.14"
33-
lockc-uprobes = { path = "../lockc-uprobes" }
3431
log = "0.4"
3532
nix = "0.23"
36-
plain = "0.2"
3733
procfs = "0.12"
3834
regex = { version = "1.5", default-features = false, features = ["perf"] }
3935
scopeguard = "1.1"
4036
serde = "1.0"
4137
serde_json = "1.0"
4238
simplelog = "0.11"
43-
sysctl = "0.4"
4439
thiserror = "1.0"
4540
tokio = { version = "1.7", features = ["macros", "process", "rt-multi-thread"] }
46-
which = "4.2"
4741

4842
[build-dependencies]
4943
anyhow = "1.0"

lockc/build.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ fn extract_libbpf_headers<P: AsRef<Path>>(include_path: P) -> Result<()> {
6363
}
6464

6565
/// Build eBPF programs with clang and libbpf headers.
66-
fn build_ebpf<P: AsRef<Path>>(out_path: P, include_path: P) -> Result<()> {
67-
extract_libbpf_headers(include_path.as_ref().clone())?;
66+
fn build_ebpf<P: Clone + AsRef<Path>>(out_path: P, include_path: P) -> Result<()> {
67+
// extract_libbpf_headers(include_path.as_ref().clone())?;
68+
extract_libbpf_headers(include_path.clone())?;
6869

6970
let bpf_dir = Path::new("src").join("bpf");
7071
let src = bpf_dir.join("lockc.bpf.c");
@@ -90,7 +91,7 @@ fn build_ebpf<P: AsRef<Path>>(out_path: P, include_path: P) -> Result<()> {
9091
.arg(format!("-D__TARGET_ARCH_{}", arch))
9192
.arg(src.as_os_str())
9293
.arg("-o")
93-
.arg(out.clone());
94+
.arg(out);
9495

9596
let output = cmd.output().context("Failed to execute clang")?;
9697
if !output.status.success() {

lockc/src/bin/lockcd.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

lockc/src/bpfstructs.rs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,12 @@
55
#![allow(non_snake_case)]
66
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
77

8-
use byteorder::{NativeEndian, WriteBytesExt};
9-
108
#[derive(thiserror::Error, Debug)]
119
pub enum NewBpfstructError {
1210
#[error("FFI nul error")]
1311
NulError(#[from] std::ffi::NulError),
1412
}
1513

16-
#[derive(thiserror::Error, Debug)]
17-
pub enum MapOperationError {
18-
#[error("could not convert the key to a byte array")]
19-
ByteWriteError(#[from] std::io::Error),
20-
21-
#[error("libbpf error")]
22-
LibbpfError(#[from] libbpf_rs::Error),
23-
}
24-
25-
/// Deletes an entry from the given map under the given key.
26-
pub fn map_delete(map: &mut libbpf_rs::Map, key: u32) -> Result<(), MapOperationError> {
27-
let mut key_b = vec![];
28-
key_b.write_u32::<NativeEndian>(key)?;
29-
30-
map.delete(&key_b)?;
31-
32-
Ok(())
33-
}
34-
35-
pub trait BpfStruct {
36-
/// Updates the given map with an entry under the given key and a value
37-
/// with a binary representation of the struct.
38-
fn map_update(&self, map: &mut libbpf_rs::Map, key: u32) -> Result<(), MapOperationError> {
39-
let mut key_b = vec![];
40-
key_b.write_u32::<NativeEndian>(key)?;
41-
42-
let val_b = unsafe { plain::as_bytes(self) };
43-
44-
map.update(&key_b, val_b, libbpf_rs::MapFlags::empty())?;
45-
46-
Ok(())
47-
}
48-
}
49-
50-
impl BpfStruct for container {}
51-
impl BpfStruct for process {}
52-
impl BpfStruct for accessed_path {}
53-
5414
impl accessed_path {
5515
/// Creates a new accessed_path instance and converts the given Rust string
5616
/// into C fixed-size char array.
@@ -63,6 +23,10 @@ impl accessed_path {
6323
}
6424
}
6525

26+
unsafe impl aya::Pod for accessed_path {}
27+
unsafe impl aya::Pod for container {}
28+
unsafe impl aya::Pod for process {}
29+
6630
#[cfg(test)]
6731
mod tests {
6832
use super::*;

lockc/src/communication.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use tokio::sync::oneshot;
2+
3+
use crate::{bpfstructs::container_policy_level, maps::MapOperationError};
4+
5+
/// Set of commands that the fanotify thread can send to the eBPF thread
6+
/// to request eBPF map operations.
7+
#[derive(Debug)]
8+
pub enum EbpfCommand {
9+
AddContainer {
10+
container_id: String,
11+
pid: i32,
12+
policy_level: container_policy_level,
13+
responder_tx: oneshot::Sender<Result<(), MapOperationError>>,
14+
},
15+
DeleteContainer {
16+
container_id: String,
17+
responder_tx: oneshot::Sender<Result<(), MapOperationError>>,
18+
},
19+
AddProcess {
20+
container_id: String,
21+
pid: i32,
22+
responder_tx: oneshot::Sender<Result<(), MapOperationError>>,
23+
},
24+
}

0 commit comments

Comments
 (0)