From 54e23141bd1c564bf0c461220d39f6e5bc73bc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Sat, 2 Oct 2021 11:15:10 +0000 Subject: [PATCH] Use aya_bpf::maps::PerfEventByteArray to output logs --- aya-log-ebpf/src/lib.rs | 53 ++------------------------------------ aya-log-ebpf/src/macros.rs | 2 +- 2 files changed, 3 insertions(+), 52 deletions(-) diff --git a/aya-log-ebpf/src/lib.rs b/aya-log-ebpf/src/lib.rs index ad0ab8b5..448f0871 100644 --- a/aya-log-ebpf/src/lib.rs +++ b/aya-log-ebpf/src/lib.rs @@ -7,12 +7,8 @@ mod macros; use core::{cmp, mem, ptr}; use aya_bpf::{ - bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_F_CURRENT_CPU}, - cty::c_long, - helpers::bpf_perf_event_output, macros::map, - maps::PerCpuArray, - BpfContext, + maps::{PerCpuArray, PerfEventByteArray}, }; pub use aya_log_common::Level; use aya_log_common::{RecordField, LOG_BUF_CAPACITY}; @@ -29,7 +25,7 @@ pub static mut AYA_LOG_BUF: PerCpuArray = PerCpuArray::with_max_entries( #[doc(hidden)] #[map] -pub static mut AYA_LOGS: LogEventArray = LogEventArray::with_max_entries(1024, 0); +pub static mut AYA_LOGS: PerfEventByteArray = PerfEventByteArray::with_max_entries(1024, 0); #[doc(hidden)] pub struct LogBufWriter<'a> { @@ -125,48 +121,3 @@ pub fn write_record_header( Ok(size) } - -#[doc(hidden)] -#[repr(transparent)] -pub struct LogEventArray { - def: bpf_map_def, -} - -impl LogEventArray { - const fn with_max_entries(max_entries: u32, flags: u32) -> LogEventArray { - LogEventArray { - def: bpf_map_def { - type_: BPF_MAP_TYPE_PERF_EVENT_ARRAY, - key_size: mem::size_of::() as u32, - value_size: mem::size_of::() as u32, - max_entries, - map_flags: flags, - id: 0, - pinning: 0, - }, - } - } - - #[inline(always)] - pub fn output( - &mut self, - ctx: &C, - buf: &mut LogBuf, - len: usize, - ) -> Result<(), c_long> { - let ret = unsafe { - bpf_perf_event_output( - ctx.as_ptr(), - &mut self.def as *mut _ as *mut _, - BPF_F_CURRENT_CPU, - buf.buf.as_mut_ptr() as *mut _, - len as u64, - ) - }; - if ret < 0 { - Err(ret) - } else { - Ok(()) - } - } -} diff --git a/aya-log-ebpf/src/macros.rs b/aya-log-ebpf/src/macros.rs index fd56c534..f4c420f1 100644 --- a/aya-log-ebpf/src/macros.rs +++ b/aya-log-ebpf/src/macros.rs @@ -183,7 +183,7 @@ macro_rules! log { if let Some(buf) = unsafe { $crate::AYA_LOG_BUF.get_mut(0) } { if let Ok(header_len) = $crate::write_record_header(&mut buf.buf, module_path!(), $lvl, module_path!(), file!(), line!()) { if let Ok(message_len) = $crate::write_record_message!(&mut buf.buf[header_len..], $($arg)+) { - let _ = unsafe { $crate::AYA_LOGS.output($ctx, buf, header_len + message_len) }; + let _ = unsafe { $crate::AYA_LOGS.output($ctx, &buf.buf[..header_len + message_len], 0) }; }; } }