Skip to content

Commit 0efde5c

Browse files
workspace: include eBPF crates in default-members
Being able to compile the eBPF crates for the host architecture is useful because it allows `cargo build` and `cargo test` to "just work". To achieve that, we feature-gate `no_std` and `no_main` on the bpf target architecture.
1 parent 6fc34be commit 0efde5c

19 files changed

+65
-37
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ default-members = [
4242
"aya-ebpf-macros",
4343
"aya-log-ebpf-macros",
4444

45-
# ebpf crates are omitted; they must be built with:
46-
# --target bpfe{b,l}-unknown-none
47-
# CARGO_CFG_BPF_TARGET_ARCH={x86_64,aarch64,arm,riscv64,powerpc64,s390x,mips}
45+
"ebpf/aya-ebpf",
46+
"ebpf/aya-ebpf-bindings",
47+
"ebpf/aya-log-ebpf",
48+
"test/integration-ebpf",
4849
]
4950

5051
[workspace.package]

ebpf/aya-ebpf/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool {
133133
}
134134
}
135135

136+
#[macro_export]
137+
macro_rules! main_stub {
138+
() => {
139+
#[cfg(not(target_arch = "bpf"))]
140+
fn main() {
141+
panic!(r#"eBPF kernels are not designed to be executed in user-space. This main function is only a placeholder to allow the code to compile on the host system (i.e. on any system that is not `target_arch = "bpf"`). This works in tandem with the `no_main` attribute which is only applied when compiling for `target_arch = "bpf"`."#)
142+
}
143+
};
144+
}
145+
136146
#[macro_export]
137147
macro_rules! panic_handler {
138148
() => {

test/integration-ebpf/src/bpf_probe_read.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes},

test/integration-ebpf/src/log.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
56

test/integration-ebpf/src/map_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Socket Filter program for testing with an arbitrary program with maps.
22
// This is mainly used in tests with consideration for old kernels.
33

4-
#![no_std]
5-
#![no_main]
4+
#![cfg_attr(target_arch = "bpf", no_std)]
5+
#![cfg_attr(target_arch = "bpf", no_main)]
6+
aya_ebpf::main_stub!();
67

78
use aya_ebpf::{
89
macros::{map, socket_filter},

test/integration-ebpf/src/memmove_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use core::mem;
56

test/integration-ebpf/src/name_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
56

test/integration-ebpf/src/pass.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
56

test/integration-ebpf/src/raw_tracepoint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
macros::{map, raw_tracepoint},

test/integration-ebpf/src/redirect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
bindings::xdp_action,

test/integration-ebpf/src/relocations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use core::hint;
56

test/integration-ebpf/src/ring_buf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
macros::{map, uprobe},

test/integration-ebpf/src/simple_prog.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Socket Filter program for testing with an arbitrary program.
22
// This is mainly used in tests with consideration for old kernels.
33

4-
#![no_std]
5-
#![no_main]
4+
#![cfg_attr(target_arch = "bpf", no_std)]
5+
#![cfg_attr(target_arch = "bpf", no_main)]
6+
aya_ebpf::main_stub!();
67

78
use aya_ebpf::{macros::socket_filter, programs::SkBuffContext};
89

test/integration-ebpf/src/strncmp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
cty::c_long,

test/integration-ebpf/src/tcx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext};
56

test/integration-ebpf/src/test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
bindings::{bpf_ret_code, xdp_action},

test/integration-ebpf/src/two_progs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Two programs in the same ELF section
22

3-
#![no_std]
4-
#![no_main]
3+
#![cfg_attr(target_arch = "bpf", no_std)]
4+
#![cfg_attr(target_arch = "bpf", no_main)]
5+
aya_ebpf::main_stub!();
56

67
use aya_ebpf::{macros::tracepoint, programs::TracePointContext};
78

test/integration-ebpf/src/uprobe_cookie.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{
56
EbpfContext as _, helpers,

test/integration-ebpf/src/xdp_sec.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
2-
#![no_main]
1+
#![cfg_attr(target_arch = "bpf", no_std)]
2+
#![cfg_attr(target_arch = "bpf", no_main)]
3+
aya_ebpf::main_stub!();
34

45
use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext};
56

0 commit comments

Comments
 (0)