Merge pull request #591 from vadorovsky/aya-log-impl-pod

aya-log: Move the `Pod` implementations from aya-log-common to aya-log
pull/574/head
Alessandro Decina 1 year ago committed by GitHub
commit 3d3ce8bfa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,12 +9,7 @@ repository = "https://github.com/aya-rs/aya-log"
documentation = "https://docs.rs/aya-log" documentation = "https://docs.rs/aya-log"
edition = "2021" edition = "2021"
[features]
default = []
userspace = [ "aya" ]
[dependencies] [dependencies]
aya = { path = "../aya", version = "0.11.0", optional=true }
num_enum = { version = "0.6", default-features = false } num_enum = { version = "0.6", default-features = false }
[lib] [lib]

@ -96,15 +96,6 @@ pub enum DisplayHint {
UpperMac, 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> { struct TagLenValue<'a, T> {
tag: T, tag: T,
value: &'a [u8], value: &'a [u8],

@ -12,7 +12,7 @@ edition = "2021"
[dependencies] [dependencies]
aya = { path = "../aya", version = "0.11.0", features=["async_tokio"] } 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" thiserror = "1"
log = "0.4" log = "0.4"
bytes = "1.1" bytes = "1.1"

@ -73,6 +73,20 @@ use aya::{
Bpf, Pod, 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. /// Log messages generated by `aya_log_ebpf` using the [log] crate.
/// ///
/// For more details see the [module level documentation](crate). /// For more details see the [module level documentation](crate).
@ -197,12 +211,12 @@ impl Formatter<[u8; 6]> for UpperMacFormatter {
} }
trait Format { trait Format {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()>; fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()>;
} }
impl Format for u32 { impl Format for u32 {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
@ -216,8 +230,8 @@ impl Format for u32 {
} }
impl Format for [u8; 6] { impl Format for [u8; 6] {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
@ -231,8 +245,8 @@ impl Format for [u8; 6] {
} }
impl Format for [u8; 16] { impl Format for [u8; 16] {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
@ -246,8 +260,8 @@ impl Format for [u8; 16] {
} }
impl Format for [u16; 8] { impl Format for [u16; 8] {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
@ -263,8 +277,8 @@ impl Format for [u16; 8] {
macro_rules! impl_format { macro_rules! impl_format {
($type:ident) => { ($type:ident) => {
impl Format for $type { impl Format for $type {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
@ -293,8 +307,8 @@ impl_format!(usize);
macro_rules! impl_format_float { macro_rules! impl_format_float {
($type:ident) => { ($type:ident) => {
impl Format for $type { impl Format for $type {
fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> { fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
match last_hint { match last_hint.map(|h| h.0) {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
@ -353,9 +367,9 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
let mut num_args = None; let mut num_args = None;
for _ in 0..LOG_FIELDS { 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 => { RecordField::Target => {
target = Some(std::str::from_utf8(attr.value).map_err(|_| ())?); 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 full_log_msg = String::new();
let mut last_hint: Option<DisplayHint> = None; let mut last_hint: Option<DisplayHintWrapper> = None;
for _ in 0..num_args.ok_or(())? { 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 => { Argument::DisplayHint => {
last_hint = Some(unsafe { ptr::read_unaligned(attr.value.as_ptr() as *const _) }); last_hint = Some(unsafe { ptr::read_unaligned(attr.value.as_ptr() as *const _) });
} }

Loading…
Cancel
Save