From 5603d7248a51a16233c249b645e30ea3f6804744 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Thu, 27 Apr 2023 12:38:36 +0200 Subject: [PATCH] aya-log: Move the `Pod` implementations from aya-log-common to aya-log Keeping the `Pod` implementations and optional dependency on aya in aya-log-common breaks the clippy checks (which are made on the entire workspace). The reason is that when different crates inside the workspace have the same dependency with different features, that dependency is built only once with the sum of features needed by all crates. It's **not** being built separately with different feature sets. That's why, before this change, aya-log-common was built once for the entire workspace with `userspace` feature enabled. That made importing aya-log-ebpf inside integration-ebpf impossible. The aya-log-common build, with `userspace` feature enabled, was pulling std as a dependency. Therefore, importing aya-log-ebpf inside integration-ebpf resulted in including std and errors like: ``` error[E0152]: found duplicate lang item `panic_impl` --> test/integration-ebpf/src/log.rs:23:1 | 23 | fn panic(_info: &core::panic::PanicInfo) -> ! { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: the lang item is first defined in crate `std` (which `aya` depends on) ``` This change fixes the problem by removing the `userspace` feature from aya-log-common and moving the `Pod` implementations to aya-log. --- aya-log-common/Cargo.toml | 5 ---- aya-log-common/src/lib.rs | 9 ------- aya-log/Cargo.toml | 2 +- aya-log/src/lib.rs | 50 +++++++++++++++++++++++++-------------- 4 files changed, 33 insertions(+), 33 deletions(-) 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 _) }); }