Browse Source

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

aya-log: Move the `Pod` implementations from aya-log-common to aya-log
Alessandro Decina 1 year ago
parent
commit
3d3ce8b
4 changed files with 33 additions and 33 deletions
  1. 0 5
      aya-log-common/Cargo.toml
  2. 0 9
      aya-log-common/src/lib.rs
  3. 1 1
      aya-log/Cargo.toml
  4. 32 18
      aya-log/src/lib.rs

+ 0 - 5
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]

+ 0 - 9
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],

+ 1 - 1
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"

+ 32 - 18
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<DisplayHint>) -> Result<String, ()>;
+    fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()>;
 }
 
 impl Format for u32 {
-    fn format(&self, last_hint: Option<DisplayHint>) -> Result<String, ()> {
-        match last_hint {
+    fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+        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<DisplayHint>) -> Result<String, ()> {
-        match last_hint {
+    fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+        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<DisplayHint>) -> Result<String, ()> {
-        match last_hint {
+    fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+        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<DisplayHint>) -> Result<String, ()> {
-        match last_hint {
+    fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+        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<DisplayHint>) -> Result<String, ()> {
-                match last_hint {
+            fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+                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<DisplayHint>) -> Result<String, ()> {
-                match last_hint {
+            fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
+                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<DisplayHint> = None;
+    let mut last_hint: Option<DisplayHintWrapper> = 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 _) });
             }