diff --git a/Cargo.toml b/Cargo.toml index 74ec3b42..5a29a653 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,9 +44,10 @@ default-members = [ "aya-ebpf-macros", "aya-log-ebpf-macros", - # ebpf crates are omitted; they must be built with: - # --target bpfe{b,l}-unknown-none - # CARGO_CFG_BPF_TARGET_ARCH={x86_64,aarch64,arm,riscv64,powerpc64,s390x,mips} + "ebpf/aya-ebpf", + "ebpf/aya-ebpf-bindings", + "ebpf/aya-log-ebpf", + "test/integration-ebpf", ] [workspace.package] diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs index 4ff3c40b..936dc311 100644 --- a/ebpf/aya-ebpf/src/lib.rs +++ b/ebpf/aya-ebpf/src/lib.rs @@ -133,6 +133,16 @@ pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool { } } +#[macro_export] +macro_rules! main_stub { + () => { + #[cfg(not(target_arch = "bpf"))] + fn main() { + 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"`."#) + } + }; +} + #[inline] fn insert( def: *mut bindings::bpf_map_def, diff --git a/test/integration-ebpf/src/bpf_probe_read.rs b/test/integration-ebpf/src/bpf_probe_read.rs index 3cdc7443..be7165ba 100644 --- a/test/integration-ebpf/src/bpf_probe_read.rs +++ b/test/integration-ebpf/src/bpf_probe_read.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes}, diff --git a/test/integration-ebpf/src/log.rs b/test/integration-ebpf/src/log.rs index 79f3dee3..7fe25e16 100644 --- a/test/integration-ebpf/src/log.rs +++ b/test/integration-ebpf/src/log.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; diff --git a/test/integration-ebpf/src/map_test.rs b/test/integration-ebpf/src/map_test.rs index 909dc5d4..810105d7 100644 --- a/test/integration-ebpf/src/map_test.rs +++ b/test/integration-ebpf/src/map_test.rs @@ -1,8 +1,9 @@ // Socket Filter program for testing with an arbitrary program with maps. // This is mainly used in tests with consideration for old kernels. -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ macros::{map, socket_filter}, diff --git a/test/integration-ebpf/src/memmove_test.rs b/test/integration-ebpf/src/memmove_test.rs index cbe853b5..67ed7f5d 100644 --- a/test/integration-ebpf/src/memmove_test.rs +++ b/test/integration-ebpf/src/memmove_test.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use core::mem; diff --git a/test/integration-ebpf/src/name_test.rs b/test/integration-ebpf/src/name_test.rs index bcc22389..32074d13 100644 --- a/test/integration-ebpf/src/name_test.rs +++ b/test/integration-ebpf/src/name_test.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; #[cfg(target_arch = "bpf")] diff --git a/test/integration-ebpf/src/pass.rs b/test/integration-ebpf/src/pass.rs index 19eb59ab..950dfadf 100644 --- a/test/integration-ebpf/src/pass.rs +++ b/test/integration-ebpf/src/pass.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; #[cfg(target_arch = "bpf")] diff --git a/test/integration-ebpf/src/raw_tracepoint.rs b/test/integration-ebpf/src/raw_tracepoint.rs index 27a84716..9a9fae96 100644 --- a/test/integration-ebpf/src/raw_tracepoint.rs +++ b/test/integration-ebpf/src/raw_tracepoint.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ macros::{map, raw_tracepoint}, diff --git a/test/integration-ebpf/src/redirect.rs b/test/integration-ebpf/src/redirect.rs index cc553735..7349c4d0 100644 --- a/test/integration-ebpf/src/redirect.rs +++ b/test/integration-ebpf/src/redirect.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ bindings::xdp_action, diff --git a/test/integration-ebpf/src/relocations.rs b/test/integration-ebpf/src/relocations.rs index d5dd03f6..bbaf8af3 100644 --- a/test/integration-ebpf/src/relocations.rs +++ b/test/integration-ebpf/src/relocations.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use core::hint; diff --git a/test/integration-ebpf/src/ring_buf.rs b/test/integration-ebpf/src/ring_buf.rs index 5c9422d1..80913ee3 100644 --- a/test/integration-ebpf/src/ring_buf.rs +++ b/test/integration-ebpf/src/ring_buf.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ macros::{map, uprobe}, diff --git a/test/integration-ebpf/src/simple_prog.rs b/test/integration-ebpf/src/simple_prog.rs index 755b3949..e39cfef0 100644 --- a/test/integration-ebpf/src/simple_prog.rs +++ b/test/integration-ebpf/src/simple_prog.rs @@ -1,8 +1,9 @@ // Socket Filter program for testing with an arbitrary program. // This is mainly used in tests with consideration for old kernels. -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; #[cfg(target_arch = "bpf")] diff --git a/test/integration-ebpf/src/strncmp.rs b/test/integration-ebpf/src/strncmp.rs index 55c5eff0..e6e541a8 100644 --- a/test/integration-ebpf/src/strncmp.rs +++ b/test/integration-ebpf/src/strncmp.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ cty::c_long, diff --git a/test/integration-ebpf/src/tcx.rs b/test/integration-ebpf/src/tcx.rs index b3101b22..876ea393 100644 --- a/test/integration-ebpf/src/tcx.rs +++ b/test/integration-ebpf/src/tcx.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext}; #[cfg(target_arch = "bpf")] diff --git a/test/integration-ebpf/src/test.rs b/test/integration-ebpf/src/test.rs index d1a51022..55a10894 100644 --- a/test/integration-ebpf/src/test.rs +++ b/test/integration-ebpf/src/test.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ bindings::{bpf_ret_code, xdp_action}, diff --git a/test/integration-ebpf/src/two_progs.rs b/test/integration-ebpf/src/two_progs.rs index 49231369..1675f1ae 100644 --- a/test/integration-ebpf/src/two_progs.rs +++ b/test/integration-ebpf/src/two_progs.rs @@ -1,7 +1,8 @@ // Two programs in the same ELF section -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; #[cfg(target_arch = "bpf")] diff --git a/test/integration-ebpf/src/uprobe_cookie.rs b/test/integration-ebpf/src/uprobe_cookie.rs index 676a834b..5a6fe8c1 100644 --- a/test/integration-ebpf/src/uprobe_cookie.rs +++ b/test/integration-ebpf/src/uprobe_cookie.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{ EbpfContext as _, helpers, diff --git a/test/integration-ebpf/src/xdp_sec.rs b/test/integration-ebpf/src/xdp_sec.rs index 7583d27b..65dd0297 100644 --- a/test/integration-ebpf/src/xdp_sec.rs +++ b/test/integration-ebpf/src/xdp_sec.rs @@ -1,5 +1,6 @@ -#![no_std] -#![no_main] +#![cfg_attr(target_arch = "bpf", no_std)] +#![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::main_stub!(); use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext}; #[cfg(target_arch = "bpf")] diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index b6e2e5df..06e6b193 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -2728,6 +2728,7 @@ pub fn aya_ebpf::programs::xdp::XdpContext::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_ebpf::programs::xdp::XdpContext pub fn aya_ebpf::programs::xdp::XdpContext::from(t: T) -> T pub macro aya_ebpf::bpf_printk! +pub macro aya_ebpf::main_stub! pub struct aya_ebpf::PtRegs impl aya_ebpf::PtRegs pub fn aya_ebpf::PtRegs::arg(&self, n: usize) -> core::option::Option