diff --git a/aya-log-common/Cargo.toml b/aya-log-common/Cargo.toml index 7413d8b7..5eb0d060 100644 --- a/aya-log-common/Cargo.toml +++ b/aya-log-common/Cargo.toml @@ -9,12 +9,7 @@ repository = "https://github.com/aya-rs/aya-log" documentation = "https://docs.rs/aya-log" edition = "2021" -[features] -default = [] -userspace = [ "aya" ] - [dependencies] -aya = { path = "../aya", version = "0.11.0", optional=true } num_enum = { version = "0.6", default-features = false } [lib] diff --git a/aya-log-common/src/lib.rs b/aya-log-common/src/lib.rs index 2e162a0f..976462dd 100644 --- a/aya-log-common/src/lib.rs +++ b/aya-log-common/src/lib.rs @@ -96,15 +96,6 @@ pub enum DisplayHint { UpperMac, } -#[cfg(feature = "userspace")] -mod userspace { - use super::*; - - unsafe impl aya::Pod for RecordField {} - unsafe impl aya::Pod for Argument {} - unsafe impl aya::Pod for DisplayHint {} -} - struct TagLenValue<'a, T> { tag: T, value: &'a [u8], diff --git a/aya-log/Cargo.toml b/aya-log/Cargo.toml index adcdc7a5..5fb83a32 100644 --- a/aya-log/Cargo.toml +++ b/aya-log/Cargo.toml @@ -12,7 +12,7 @@ edition = "2021" [dependencies] aya = { path = "../aya", version = "0.11.0", features=["async_tokio"] } -aya-log-common = { path = "../aya-log-common", version = "0.1.13", features=["userspace"] } +aya-log-common = { path = "../aya-log-common", version = "0.1.13" } thiserror = "1" log = "0.4" bytes = "1.1" diff --git a/aya-log/src/lib.rs b/aya-log/src/lib.rs index 4ba1d784..83521cce 100644 --- a/aya-log/src/lib.rs +++ b/aya-log/src/lib.rs @@ -73,6 +73,20 @@ use aya::{ Bpf, Pod, }; +#[derive(Copy, Clone)] +#[repr(transparent)] +struct RecordFieldWrapper(RecordField); +#[derive(Copy, Clone)] +#[repr(transparent)] +struct ArgumentWrapper(Argument); +#[derive(Copy, Clone)] +#[repr(transparent)] +struct DisplayHintWrapper(DisplayHint); + +unsafe impl aya::Pod for RecordFieldWrapper {} +unsafe impl aya::Pod for ArgumentWrapper {} +unsafe impl aya::Pod for DisplayHintWrapper {} + /// Log messages generated by `aya_log_ebpf` using the [log] crate. /// /// For more details see the [module level documentation](crate). @@ -197,12 +211,12 @@ impl Formatter<[u8; 6]> for UpperMacFormatter { } trait Format { - fn format(&self, last_hint: Option) -> Result; + fn format(&self, last_hint: Option) -> Result; } impl Format for u32 { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), @@ -216,8 +230,8 @@ impl Format for u32 { } impl Format for [u8; 6] { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Err(()), Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()), @@ -231,8 +245,8 @@ impl Format for [u8; 6] { } impl Format for [u8; 16] { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Err(()), Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()), @@ -246,8 +260,8 @@ impl Format for [u8; 16] { } impl Format for [u16; 8] { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Err(()), Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()), @@ -263,8 +277,8 @@ impl Format for [u16; 8] { macro_rules! impl_format { ($type:ident) => { impl Format for $type { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), @@ -293,8 +307,8 @@ impl_format!(usize); macro_rules! impl_format_float { ($type:ident) => { impl Format for $type { - fn format(&self, last_hint: Option) -> Result { - match last_hint { + fn format(&self, last_hint: Option) -> Result { + match last_hint.map(|h| h.0) { Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()), @@ -353,9 +367,9 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> { let mut num_args = None; for _ in 0..LOG_FIELDS { - let (attr, rest) = unsafe { TagLenValue::<'_, RecordField>::try_read(buf)? }; + let (attr, rest) = unsafe { TagLenValue::<'_, RecordFieldWrapper>::try_read(buf)? }; - match attr.tag { + match attr.tag.0 { RecordField::Target => { target = Some(std::str::from_utf8(attr.value).map_err(|_| ())?); } @@ -380,11 +394,11 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> { } let mut full_log_msg = String::new(); - let mut last_hint: Option = None; + let mut last_hint: Option = None; for _ in 0..num_args.ok_or(())? { - let (attr, rest) = unsafe { TagLenValue::<'_, Argument>::try_read(buf)? }; + let (attr, rest) = unsafe { TagLenValue::<'_, ArgumentWrapper>::try_read(buf)? }; - match attr.tag { + match attr.tag.0 { Argument::DisplayHint => { last_hint = Some(unsafe { ptr::read_unaligned(attr.value.as_ptr() as *const _) }); }