De-duplicate `panic_handler` and `main`-stub with macro

reviewable/pr1229/r4
Thomas Eizinger 3 weeks ago
parent 4dbf333605
commit 4742e03482
No known key found for this signature in database
GPG Key ID: 05633CD77196CAF3

@ -133,6 +133,22 @@ pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool {
} }
} }
#[macro_export]
macro_rules! prelude {
() => {
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}
};
}
#[inline] #[inline]
fn insert<K, V>( fn insert<K, V>(
def: *mut bindings::bpf_map_def, def: *mut bindings::bpf_map_def,

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes}, helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes},
@ -71,14 +72,3 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) {
ctx.arg::<usize>(0), ctx.arg::<usize>(0),
); );
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
@ -81,14 +82,3 @@ pub fn test_log(ctx: ProbeContext) {
debug!(&ctx, "{:x}", no_copy.consume()); debug!(&ctx, "{:x}", no_copy.consume());
} }
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -3,6 +3,7 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
macros::{map, socket_filter}, macros::{map, socket_filter},
@ -35,14 +36,3 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
0 0
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use core::mem; use core::mem;
@ -53,14 +54,3 @@ fn try_do_dnat(ctx: XdpContext) -> Result<u32, ()> {
} }
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
@ -14,14 +15,3 @@ pub fn ihaveaverylongname(ctx: XdpContext) -> u32 {
unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> { unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
@ -16,14 +17,3 @@ pub fn pass(ctx: XdpContext) -> u32 {
unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> { unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
macros::{map, raw_tracepoint}, macros::{map, raw_tracepoint},
@ -25,14 +26,3 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 {
0 0
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
bindings::xdp_action, bindings::xdp_action,
@ -74,14 +75,3 @@ fn inc_hit(index: u32) {
unsafe { *hit += 1 }; unsafe { *hit += 1 };
} }
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use core::hint; use core::hint;
@ -38,14 +39,3 @@ fn set_result(index: u32, value: u64) {
fn set_result_backward(index: u32, value: u64) { fn set_result_backward(index: u32, value: u64) {
set_result(index, value); set_result(index, value);
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
macros::{map, uprobe}, macros::{map, uprobe},
@ -45,14 +46,3 @@ pub fn ring_buf_test(ctx: ProbeContext) {
entry.discard(0); entry.discard(0);
} }
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -3,6 +3,7 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; use aya_ebpf::{macros::socket_filter, programs::SkBuffContext};
@ -11,14 +12,3 @@ use aya_ebpf::{macros::socket_filter, programs::SkBuffContext};
pub fn simple_prog(_ctx: SkBuffContext) -> i64 { pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
0 0
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
cty::c_long, cty::c_long,
@ -26,14 +27,3 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> {
Ok(()) Ok(())
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext}; use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext};
@ -7,14 +8,3 @@ use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs
pub fn tcx_next(_ctx: TcContext) -> i32 { pub fn tcx_next(_ctx: TcContext) -> i32 {
TCX_NEXT TCX_NEXT
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
bindings::{bpf_ret_code, xdp_action}, bindings::{bpf_ret_code, xdp_action},
@ -52,14 +53,3 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 {
// Linux kernel for inspiration. // Linux kernel for inspiration.
bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -2,6 +2,7 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; use aya_ebpf::{macros::tracepoint, programs::TracePointContext};
@ -13,14 +14,3 @@ pub fn test_tracepoint_one(_ctx: TracePointContext) -> u32 {
pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 { pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 {
0 0
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{ use aya_ebpf::{
EbpfContext as _, helpers, EbpfContext as _, helpers,
@ -17,14 +18,3 @@ pub fn uprobe_cookie(ctx: ProbeContext) {
let cookie_bytes = cookie.to_le_bytes(); let cookie_bytes = cookie.to_le_bytes();
let _res = RING_BUF.output(&cookie_bytes, 0); let _res = RING_BUF.output(&cookie_bytes, 0);
} }
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,6 @@
#![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_std)]
#![cfg_attr(target_arch = "bpf", no_main)] #![cfg_attr(target_arch = "bpf", no_main)]
aya_ebpf::prelude!();
use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext};
@ -18,14 +19,3 @@ probe!(xdp_cpumap, (map = "cpumap"));
probe!(xdp_devmap, (map = "devmap")); probe!(xdp_devmap, (map = "devmap"));
probe!(xdp_frags_cpumap, (frags, map = "cpumap")); probe!(xdp_frags_cpumap, (frags, map = "cpumap"));
probe!(xdp_frags_devmap, (frags, map = "devmap")); probe!(xdp_frags_devmap, (frags, map = "devmap"));
#[cfg(target_arch = "bpf")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

Loading…
Cancel
Save