Skip to content

Commit 4c46033

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 6f57881 commit 4c46033

File tree

15 files changed

+780
-989
lines changed

15 files changed

+780
-989
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[workspace]
22
members = [
33
"lockc",
4-
"lockc-uprobes",
54
"xtask",
65
]

lockc-uprobes/Cargo.toml

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

lockc-uprobes/src/lib.rs

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

lockc/Cargo.toml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,36 @@ 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"] }
24+
clap = { version = "3.0", features = ["derive"] }
2325
config = { version = "0.11", default-features = false, features = ["toml"] }
24-
ctrlc = "3.2"
2526
fanotify-rs = { git = "https://github.com/vadorovsky/fanotify-rs", branch = "fix-pid-type" }
2627
futures = "0.3"
27-
goblin = "0.4"
2828
kube = "0.66"
2929
k8s-openapi = { version = "0.13", default-features = false, features = ["v1_21"] }
3030
lazy_static = "1.4"
3131
libc = { version = "0.2", features = [ "extra_traits" ] }
32-
libbpf-rs = "0.14"
33-
lockc-uprobes = { path = "../lockc-uprobes" }
3432
log = "0.4"
3533
nix = "0.23"
36-
plain = "0.2"
3734
procfs = "0.12"
3835
regex = { version = "1.5", default-features = false, features = ["perf"] }
3936
scopeguard = "1.1"
4037
serde = "1.0"
4138
serde_json = "1.0"
4239
simplelog = "0.11"
43-
sysctl = "0.4"
4440
thiserror = "1.0"
4541
tokio = { version = "1.7", features = ["macros", "process", "rt-multi-thread"] }
46-
which = "4.2"
42+
tracing = "0.1"
43+
tracing-core = "0.1"
44+
tracing-subscriber = "0.3"
4745

4846
[build-dependencies]
4947
anyhow = "1.0"

lockc/src/bin/lockcd.rs

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

lockc/src/bpfstructs.rs

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

8-
use byteorder::{NativeEndian, WriteBytesExt};
8+
use std::ffi::CString;
99

10-
#[derive(thiserror::Error, Debug)]
10+
use thiserror::Error;
11+
12+
#[derive(Error, Debug)]
1113
pub enum NewBpfstructError {
12-
#[error("FFI nul error")]
14+
#[error(transparent)]
1315
NulError(#[from] std::ffi::NulError),
14-
}
15-
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-
}
2416

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(())
17+
#[error("could not convert Vec<u8> to CString")]
18+
VecU8CStringConv,
3319
}
3420

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-
5421
impl accessed_path {
5522
/// Creates a new accessed_path instance and converts the given Rust string
5623
/// into C fixed-size char array.
5724
pub fn new(path: &str) -> Result<Self, NewBpfstructError> {
58-
let mut path_b = std::ffi::CString::new(path)?.into_bytes_with_nul();
25+
let mut path_b = CString::new(path)?.into_bytes_with_nul();
5926
path_b.resize(PATH_LEN as usize, 0);
6027
Ok(accessed_path {
61-
path: path_b.try_into().unwrap(),
28+
path: path_b
29+
.try_into()
30+
.map_err(|_| NewBpfstructError::VecU8CStringConv)?,
6231
})
6332
}
6433
}
6534

35+
impl container_id {
36+
/// Creates a new container_id instance and converts the given Rust string
37+
/// into C fixed size char array.
38+
pub fn new(id: &str) -> Result<Self, NewBpfstructError> {
39+
let mut id_b = CString::new(id)?.into_bytes_with_nul();
40+
id_b.resize(CONTAINER_ID_LIMIT as usize, 0);
41+
Ok(container_id {
42+
id: id_b.try_into().unwrap(),
43+
})
44+
}
45+
}
46+
47+
unsafe impl aya::Pod for accessed_path {}
48+
unsafe impl aya::Pod for container {}
49+
unsafe impl aya::Pod for container_id {}
50+
unsafe impl aya::Pod for process {}
51+
6652
#[cfg(test)]
6753
mod tests {
6854
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)