Skip to content

Commit 87651ae

Browse files
authored
Merge pull request deepfence#50 from deepfence/tomasz/ebpf-build
eBPF: add compiled release mode build
2 parents a1e6100 + 33fa0ce commit 87651ae

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

docs/gh/development.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ All commands should be executed from repository/workspace root folder unless not
66

77
## Compilation
88

9-
First compile ebpf bytecode with the following command. It will be embedded
10-
in userspace binary using aya.
9+
Just utilize cargo infrastructure.
1110

1211
```
13-
$ cargo xtask build-ebpf
12+
$ cargo build
1413
```
1514

16-
Then userspace code.
15+
If you make changes to any code under `ebpfguard-ebpf` and/or `ebpfguard-common` make sure to rebuild eBPF objects.
1716

1817
```
19-
$ cargo build
18+
$ cargo xtask build-ebpf
2019
```
2120

2221
## Tests
@@ -41,8 +40,18 @@ Clippy lints.
4140
$ cargo clippy --workspace -- --deny warnings
4241
```
4342

44-
Miri verification. Requires optional dependencies from [miri section](prerequisites.md#miri)
43+
Miri verification.
4544

4645
```
4746
$ cargo +nightly miri test --all-targets
4847
```
48+
49+
## Contributing
50+
51+
Before setting up a PR make sure to run
52+
53+
```
54+
cargo clippy --fix && cargo fmt
55+
```
56+
57+
And verify/commit any resulting changes.

ebpfguard-ebpf/ebpfguard.debug.obj

-176 Bytes
Binary file not shown.

ebpfguard-ebpf/ebpfguard.release.obj

8.47 MB
Binary file not shown.

xtask/src/build_ebpf.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ impl std::fmt::Display for Architecture {
2929
}
3030
}
3131

32+
#[derive(Debug, Copy, Clone)]
33+
pub enum BuildType {
34+
Debug,
35+
Release,
36+
}
37+
38+
impl From<bool> for BuildType {
39+
fn from(value: bool) -> Self {
40+
match value {
41+
true => Self::Release,
42+
false => Self::Debug,
43+
}
44+
}
45+
}
46+
47+
impl std::fmt::Display for BuildType {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+
f.write_str(match self {
50+
Self::Release => "release",
51+
Self::Debug => "debug",
52+
})
53+
}
54+
}
55+
3256
#[derive(Debug, Parser)]
3357
pub struct Options {
3458
/// Set the endianness of the BPF target
@@ -49,7 +73,9 @@ pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
4973
"-Z",
5074
"build-std=core",
5175
];
52-
if opts.release {
76+
let build_type = BuildType::from(opts.release);
77+
78+
if matches!(build_type, BuildType::Release) {
5379
args.push("--release")
5480
}
5581

@@ -64,5 +90,12 @@ pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
6490
.status()
6591
.expect("failed to build bpf program");
6692
assert!(status.success());
93+
94+
let source = format!("target/{}/{}/ebpfguard", opts.target, build_type);
95+
let destination = format!("ebpfguard-ebpf/ebpfguard.{}.obj", build_type);
96+
97+
std::fs::copy(source, destination)
98+
.expect("Couldn't copy compiled eBPFObject to destination path");
99+
67100
Ok(())
68101
}

0 commit comments

Comments
 (0)