xdp.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. use proc_macro2::TokenStream;
  2. use quote::quote;
  3. use syn::{Error, ItemFn, Result};
  4. use crate::args::{err_on_unknown_args, pop_bool_arg, pop_string_arg, Args};
  5. pub(crate) struct Xdp {
  6. item: ItemFn,
  7. frags: bool,
  8. map: Option<XdpMap>,
  9. }
  10. #[derive(Clone, Copy)]
  11. pub(crate) enum XdpMap {
  12. CpuMap,
  13. DevMap,
  14. }
  15. impl Xdp {
  16. pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Xdp> {
  17. let item = syn::parse2(item)?;
  18. let mut args: Args = syn::parse2(attrs)?;
  19. let frags = pop_bool_arg(&mut args, "frags");
  20. let map = match pop_string_arg(&mut args, "map").as_deref() {
  21. Some("cpumap") => Some(XdpMap::CpuMap),
  22. Some("devmap") => Some(XdpMap::DevMap),
  23. Some(name) => {
  24. return Err(Error::new_spanned(
  25. "map",
  26. format!("Invalid value. Expected 'cpumap' or 'devmap', found '{name}'"),
  27. ))
  28. }
  29. None => None,
  30. };
  31. err_on_unknown_args(&args)?;
  32. Ok(Xdp { item, frags, map })
  33. }
  34. pub(crate) fn expand(&self) -> Result<TokenStream> {
  35. let mut section_name = vec![if self.frags { "xdp.frags" } else { "xdp" }];
  36. match self.map {
  37. Some(XdpMap::CpuMap) => section_name.push("cpumap"),
  38. Some(XdpMap::DevMap) => section_name.push("devmap"),
  39. None => (),
  40. };
  41. let section_name = section_name.join("/");
  42. let fn_vis = &self.item.vis;
  43. let fn_name = self.item.sig.ident.clone();
  44. let item = &self.item;
  45. Ok(quote! {
  46. #[no_mangle]
  47. #[link_section = #section_name]
  48. #fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  49. return #fn_name(::aya_ebpf::programs::XdpContext::new(ctx));
  50. #item
  51. }
  52. })
  53. }
  54. }
  55. #[cfg(test)]
  56. mod tests {
  57. use syn::parse_quote;
  58. use super::*;
  59. #[test]
  60. fn test_xdp() {
  61. let prog = Xdp::parse(
  62. parse_quote! {},
  63. parse_quote! {
  64. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  65. 0
  66. }
  67. },
  68. )
  69. .unwrap();
  70. let expanded = prog.expand().unwrap();
  71. let expected = quote! {
  72. #[no_mangle]
  73. #[link_section = "xdp"]
  74. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  75. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  76. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  77. 0
  78. }
  79. }
  80. };
  81. assert_eq!(expected.to_string(), expanded.to_string());
  82. }
  83. #[test]
  84. fn test_xdp_frags() {
  85. let prog = Xdp::parse(
  86. parse_quote! { frags },
  87. parse_quote! {
  88. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  89. 0
  90. }
  91. },
  92. )
  93. .unwrap();
  94. let expanded = prog.expand().unwrap();
  95. let expected = quote! {
  96. #[no_mangle]
  97. #[link_section = "xdp.frags"]
  98. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  99. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  100. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  101. 0
  102. }
  103. }
  104. };
  105. assert_eq!(expected.to_string(), expanded.to_string());
  106. }
  107. #[test]
  108. fn test_xdp_cpumap() {
  109. let prog = Xdp::parse(
  110. parse_quote! { map = "cpumap" },
  111. parse_quote! {
  112. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  113. 0
  114. }
  115. },
  116. )
  117. .unwrap();
  118. let expanded = prog.expand().unwrap();
  119. let expected = quote! {
  120. #[no_mangle]
  121. #[link_section = "xdp/cpumap"]
  122. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  123. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  124. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  125. 0
  126. }
  127. }
  128. };
  129. assert_eq!(expected.to_string(), expanded.to_string());
  130. }
  131. #[test]
  132. fn test_xdp_devmap() {
  133. let prog = Xdp::parse(
  134. parse_quote! { map = "devmap" },
  135. parse_quote! {
  136. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  137. 0
  138. }
  139. },
  140. )
  141. .unwrap();
  142. let expanded = prog.expand().unwrap();
  143. let expected = quote! {
  144. #[no_mangle]
  145. #[link_section = "xdp/devmap"]
  146. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  147. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  148. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  149. 0
  150. }
  151. }
  152. };
  153. assert_eq!(expected.to_string(), expanded.to_string());
  154. }
  155. #[test]
  156. #[should_panic(expected = "Invalid value. Expected 'cpumap' or 'devmap', found 'badmap'")]
  157. fn test_xdp_bad_map() {
  158. Xdp::parse(
  159. parse_quote! { map = "badmap" },
  160. parse_quote! {
  161. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  162. 0
  163. }
  164. },
  165. )
  166. .unwrap();
  167. }
  168. #[test]
  169. fn test_xdp_frags_cpumap() {
  170. let prog = Xdp::parse(
  171. parse_quote! { frags, map = "cpumap" },
  172. parse_quote! {
  173. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  174. 0
  175. }
  176. },
  177. )
  178. .unwrap();
  179. let expanded = prog.expand().unwrap();
  180. let expected = quote! {
  181. #[no_mangle]
  182. #[link_section = "xdp.frags/cpumap"]
  183. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  184. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  185. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  186. 0
  187. }
  188. }
  189. };
  190. assert_eq!(expected.to_string(), expanded.to_string());
  191. }
  192. #[test]
  193. fn test_xdp_frags_devmap() {
  194. let prog = Xdp::parse(
  195. parse_quote! { frags, map = "devmap" },
  196. parse_quote! {
  197. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  198. 0
  199. }
  200. },
  201. )
  202. .unwrap();
  203. let expanded = prog.expand().unwrap();
  204. let expected = quote! {
  205. #[no_mangle]
  206. #[link_section = "xdp.frags/devmap"]
  207. fn prog(ctx: *mut ::aya_ebpf::bindings::xdp_md) -> u32 {
  208. return prog(::aya_ebpf::programs::XdpContext::new(ctx));
  209. fn prog(ctx: &mut ::aya_ebpf::programs::XdpContext) -> i32 {
  210. 0
  211. }
  212. }
  213. };
  214. assert_eq!(expected.to_string(), expanded.to_string());
  215. }
  216. }