mirror of https://github.com/aya-rs/aya
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.8 KiB
Rust
67 lines
1.8 KiB
Rust
use proc_macro2::TokenStream;
|
|
use proc_macro_error::abort;
|
|
use quote::quote;
|
|
use syn::{ItemFn, Result};
|
|
|
|
pub(crate) struct SkLookup {
|
|
item: ItemFn,
|
|
}
|
|
|
|
impl SkLookup {
|
|
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Self> {
|
|
if !attrs.is_empty() {
|
|
abort!(attrs, "unexpected attribute")
|
|
}
|
|
let item = syn::parse2(item)?;
|
|
Ok(SkLookup { item })
|
|
}
|
|
|
|
pub(crate) fn expand(&self) -> Result<TokenStream> {
|
|
let fn_name = self.item.sig.ident.clone();
|
|
let fn_vis = &self.item.vis;
|
|
let item = &self.item;
|
|
Ok(quote! {
|
|
#[no_mangle]
|
|
#[link_section = "sk_lookup"]
|
|
#fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::bpf_sk_lookup) -> u32 {
|
|
return #fn_name(::aya_ebpf::programs::SkLookupContext::new(ctx));
|
|
|
|
#item
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use syn::parse_quote;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_sk_lookup() {
|
|
let prog = SkLookup::parse(
|
|
parse_quote! {},
|
|
parse_quote! {
|
|
fn prog(ctx: &mut ::aya_ebpf::programs::SkLookupContext) -> u32 {
|
|
0
|
|
}
|
|
},
|
|
)
|
|
.unwrap();
|
|
let expanded = prog.expand().unwrap();
|
|
let expected = quote! {
|
|
#[no_mangle]
|
|
#[link_section = "sk_lookup"]
|
|
fn prog(ctx: *mut ::aya_ebpf::bindings::bpf_sk_lookup) -> u32 {
|
|
return prog(::aya_ebpf::programs::SkLookupContext::new(ctx));
|
|
|
|
fn prog(ctx: &mut ::aya_ebpf::programs::SkLookupContext) -> u32 {
|
|
0
|
|
}
|
|
}
|
|
};
|
|
assert_eq!(expected.to_string(), expanded.to_string());
|
|
}
|
|
}
|