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.
241 lines
7.0 KiB
Rust
241 lines
7.0 KiB
Rust
1 year ago
|
use proc_macro2::TokenStream;
|
||
|
use quote::quote;
|
||
1 year ago
|
use syn::{Error, ItemFn, Result};
|
||
1 year ago
|
|
||
1 year ago
|
use crate::args::{err_on_unknown_args, pop_bool_arg, pop_string_arg, Args};
|
||
1 year ago
|
|
||
|
pub(crate) struct Xdp {
|
||
|
item: ItemFn,
|
||
|
frags: bool,
|
||
1 year ago
|
map: Option<XdpMap>,
|
||
|
}
|
||
|
|
||
|
#[derive(Clone, Copy)]
|
||
|
pub(crate) enum XdpMap {
|
||
|
CpuMap,
|
||
|
DevMap,
|
||
1 year ago
|
}
|
||
|
|
||
|
impl Xdp {
|
||
|
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Xdp> {
|
||
|
let item = syn::parse2(item)?;
|
||
1 year ago
|
let mut args: Args = syn::parse2(attrs)?;
|
||
1 year ago
|
|
||
1 year ago
|
let frags = pop_bool_arg(&mut args, "frags");
|
||
1 year ago
|
let map = match pop_string_arg(&mut args, "map").as_deref() {
|
||
|
Some("cpumap") => Some(XdpMap::CpuMap),
|
||
|
Some("devmap") => Some(XdpMap::DevMap),
|
||
|
Some(name) => {
|
||
|
return Err(Error::new_spanned(
|
||
|
"map",
|
||
1 year ago
|
format!("Invalid value. Expected 'cpumap' or 'devmap', found '{name}'"),
|
||
1 year ago
|
))
|
||
|
}
|
||
|
None => None,
|
||
|
};
|
||
|
|
||
1 year ago
|
err_on_unknown_args(&args)?;
|
||
1 year ago
|
Ok(Xdp { item, frags, map })
|
||
1 year ago
|
}
|
||
|
|
||
|
pub(crate) fn expand(&self) -> Result<TokenStream> {
|
||
1 year ago
|
let mut section_name = vec![if self.frags { "xdp.frags" } else { "xdp" }];
|
||
|
match self.map {
|
||
|
Some(XdpMap::CpuMap) => section_name.push("cpumap"),
|
||
|
Some(XdpMap::DevMap) => section_name.push("devmap"),
|
||
|
None => (),
|
||
1 year ago
|
};
|
||
1 year ago
|
let section_name = section_name.join("/");
|
||
|
|
||
1 year ago
|
let fn_vis = &self.item.vis;
|
||
|
let fn_name = self.item.sig.ident.clone();
|
||
|
let item = &self.item;
|
||
|
Ok(quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = #section_name]
|
||
8 months ago
|
#fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return #fn_name(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
|
#item
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use syn::parse_quote;
|
||
|
|
||
1 year ago
|
use super::*;
|
||
|
|
||
1 year ago
|
#[test]
|
||
|
fn test_xdp() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! {},
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_xdp_frags() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! { frags },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp.frags"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
1 year ago
|
|
||
|
#[test]
|
||
|
fn test_xdp_cpumap() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! { map = "cpumap" },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp/cpumap"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_xdp_devmap() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! { map = "devmap" },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp/devmap"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
#[should_panic(expected = "Invalid value. Expected 'cpumap' or 'devmap', found 'badmap'")]
|
||
|
fn test_xdp_bad_map() {
|
||
|
Xdp::parse(
|
||
|
parse_quote! { map = "badmap" },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_xdp_frags_cpumap() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! { frags, map = "cpumap" },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp.frags/cpumap"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_xdp_frags_devmap() {
|
||
|
let prog = Xdp::parse(
|
||
|
parse_quote! { frags, map = "devmap" },
|
||
|
parse_quote! {
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "xdp.frags/devmap"]
|
||
8 months ago
|
fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
|
||
|
return prog(::aya_ebpf::programs::XdpContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
1 year ago
|
}
|