Browse Source

bpf: add support for tracepoint program (#29)

This patch add initial support for tracepoint program type.
Hope you enjoy.

Signed-off-by: Tw <wei.tan@intel.com>
Tw 3 năm trước cách đây
mục cha
commit
55ba0538f2

+ 29 - 0
bpf/aya-bpf-macros/src/expand.rs

@@ -293,3 +293,32 @@ impl std::fmt::Display for ProbeKind {
         }
     }
 }
+
+pub struct TracePoint {
+    item: ItemFn,
+    name: String,
+}
+
+impl TracePoint {
+    pub fn from_syn(mut args: Args, item: ItemFn) -> Result<TracePoint> {
+        let name = name_arg(&mut args)?.unwrap_or_else(|| item.sig.ident.to_string());
+
+        Ok(TracePoint { item, name })
+    }
+
+    pub fn expand(&self) -> Result<TokenStream> {
+        let section_name = format!("tp/{}", self.name);
+        let fn_name = &self.item.sig.ident;
+        let item = &self.item;
+        Ok(quote! {
+            #[no_mangle]
+            #[link_section = #section_name]
+            fn #fn_name(ctx: *mut ::core::ffi::c_void) -> u32 {
+               let _ = #fn_name(::aya_bpf::programs::TracePointContext::new(ctx));
+               return 0;
+
+               #item
+            }
+        })
+    }
+}

+ 12 - 1
bpf/aya-bpf-macros/src/lib.rs

@@ -1,6 +1,6 @@
 mod expand;
 
-use expand::{Args, Map, Probe, ProbeKind, SchedClassifier, SkMsg, SockOps, Xdp};
+use expand::{Args, Map, Probe, ProbeKind, SchedClassifier, SkMsg, SockOps, TracePoint, Xdp};
 use proc_macro::TokenStream;
 use syn::{parse_macro_input, ItemFn, ItemStatic};
 
@@ -101,3 +101,14 @@ fn probe(kind: ProbeKind, attrs: TokenStream, item: TokenStream) -> TokenStream
         .unwrap_or_else(|err| err.to_compile_error())
         .into()
 }
+
+#[proc_macro_attribute]
+pub fn tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
+    let args = parse_macro_input!(attrs as Args);
+    let item = parse_macro_input!(item as ItemFn);
+
+    TracePoint::from_syn(args, item)
+        .and_then(|u| u.expand())
+        .unwrap_or_else(|err| err.to_compile_error())
+        .into()
+}

+ 1 - 1
bpf/aya-bpf/src/maps/hash_map.rs

@@ -4,7 +4,7 @@ use aya_bpf_cty::{c_long, c_void};
 
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH},
-    helpers::{bpf_map_lookup_elem, bpf_map_update_elem, bpf_map_delete_elem},
+    helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
 };
 
 #[repr(transparent)]

+ 2 - 0
bpf/aya-bpf/src/programs/mod.rs

@@ -2,10 +2,12 @@ pub mod probe;
 pub mod sk_msg;
 pub mod sk_skb;
 pub mod sock_ops;
+pub mod tracepoint;
 pub mod xdp;
 
 pub use probe::ProbeContext;
 pub use sk_msg::SkMsgContext;
 pub use sk_skb::SkSkbContext;
 pub use sock_ops::SockOpsContext;
+pub use tracepoint::TracePointContext;
 pub use xdp::XdpContext;

+ 22 - 0
bpf/aya-bpf/src/programs/tracepoint.rs

@@ -0,0 +1,22 @@
+use crate::{helpers::bpf_probe_read, BpfContext};
+use core::ffi::c_void;
+
+pub struct TracePointContext {
+    ctx: *mut c_void,
+}
+
+impl TracePointContext {
+    pub fn new(ctx: *mut c_void) -> TracePointContext {
+        TracePointContext { ctx }
+    }
+
+    pub unsafe fn read_at<T>(&self, offset: usize) -> Result<T, i64> {
+        bpf_probe_read(self.ctx.add(offset) as *const T)
+    }
+}
+
+impl BpfContext for TracePointContext {
+    fn as_ptr(&self) -> *mut c_void {
+        self.ctx
+    }
+}