cgroup_skb.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. use std::borrow::Cow;
  2. use proc_macro2::TokenStream;
  3. use proc_macro_error::abort;
  4. use quote::quote;
  5. use syn::{Ident, ItemFn, Result};
  6. pub(crate) struct CgroupSkb {
  7. item: ItemFn,
  8. attach_type: Option<String>,
  9. }
  10. impl CgroupSkb {
  11. pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<CgroupSkb> {
  12. let item: ItemFn = syn::parse2(item)?;
  13. let mut attach_type = None;
  14. if !attrs.is_empty() {
  15. let ident: Ident = syn::parse2(attrs)?;
  16. match ident.to_string().as_str() {
  17. "ingress" | "egress" => (),
  18. _ => abort!(attach_type, "invalid attach type"),
  19. }
  20. attach_type = Some(ident.to_string());
  21. }
  22. Ok(CgroupSkb { item, attach_type })
  23. }
  24. pub(crate) fn expand(&self) -> Result<TokenStream> {
  25. let section_name: Cow<'_, _> = if self.attach_type.is_some() {
  26. format!("cgroup_skb/{}", self.attach_type.as_ref().unwrap()).into()
  27. } else {
  28. "cgroup_skb".into()
  29. };
  30. let fn_vis = &self.item.vis;
  31. let fn_name = self.item.sig.ident.clone();
  32. let item = &self.item;
  33. Ok(quote! {
  34. #[no_mangle]
  35. #[link_section = #section_name]
  36. #fn_vis fn #fn_name(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  37. return #fn_name(::aya_bpf::programs::SkBuffContext::new(ctx));
  38. #item
  39. }
  40. })
  41. }
  42. }
  43. #[cfg(test)]
  44. mod tests {
  45. use super::*;
  46. use syn::parse_quote;
  47. #[test]
  48. fn cgroup_skb() {
  49. let prog = CgroupSkb::parse(
  50. parse_quote!(),
  51. parse_quote!(
  52. fn foo(ctx: SkBuffContext) -> i32 {
  53. 0
  54. }
  55. ),
  56. )
  57. .unwrap();
  58. let expanded = prog.expand().unwrap();
  59. let expected = quote! {
  60. #[no_mangle]
  61. #[link_section = "cgroup_skb"]
  62. fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  63. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  64. fn foo(ctx: SkBuffContext) -> i32 {
  65. 0
  66. }
  67. }
  68. };
  69. assert_eq!(expected.to_string(), expanded.to_string());
  70. }
  71. #[test]
  72. fn cgroup_skb_egress() {
  73. let prog = CgroupSkb::parse(
  74. parse_quote!(egress),
  75. parse_quote!(
  76. fn foo(ctx: SkBuffContext) -> i32 {
  77. 0
  78. }
  79. ),
  80. )
  81. .unwrap();
  82. let expanded = prog.expand().unwrap();
  83. let expected = quote! {
  84. #[no_mangle]
  85. #[link_section = "cgroup_skb/egress"]
  86. fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  87. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  88. fn foo(ctx: SkBuffContext) -> i32 {
  89. 0
  90. }
  91. }
  92. };
  93. assert_eq!(expected.to_string(), expanded.to_string());
  94. }
  95. #[test]
  96. fn cgroup_skb_ingress() {
  97. let prog = CgroupSkb::parse(
  98. parse_quote!(ingress),
  99. parse_quote!(
  100. fn foo(ctx: SkBuffContext) -> i32 {
  101. 0
  102. }
  103. ),
  104. )
  105. .unwrap();
  106. let expanded = prog.expand().unwrap();
  107. let expected = quote! {
  108. #[no_mangle]
  109. #[link_section = "cgroup_skb/ingress"]
  110. fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  111. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  112. fn foo(ctx: SkBuffContext) -> i32 {
  113. 0
  114. }
  115. }
  116. };
  117. assert_eq!(expected.to_string(), expanded.to_string());
  118. }
  119. #[test]
  120. fn priv_function() {
  121. let prog = CgroupSkb::parse(
  122. parse_quote!(egress),
  123. parse_quote!(
  124. fn foo(ctx: SkBuffContext) -> i32 {
  125. 0
  126. }
  127. ),
  128. )
  129. .unwrap();
  130. let expanded = prog.expand().unwrap();
  131. let expected = quote! {
  132. #[no_mangle]
  133. #[link_section = "cgroup_skb/egress"]
  134. fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  135. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  136. fn foo(ctx: SkBuffContext) -> i32 {
  137. 0
  138. }
  139. }
  140. };
  141. assert_eq!(expected.to_string(), expanded.to_string());
  142. }
  143. #[test]
  144. fn pub_function() {
  145. let prog = CgroupSkb::parse(
  146. parse_quote!(egress),
  147. parse_quote!(
  148. pub fn foo(ctx: SkBuffContext) -> i32 {
  149. 0
  150. }
  151. ),
  152. )
  153. .unwrap();
  154. let expanded = prog.expand().unwrap();
  155. let expected = quote! {
  156. #[no_mangle]
  157. #[link_section = "cgroup_skb/egress"]
  158. pub fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  159. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  160. pub fn foo(ctx: SkBuffContext) -> i32 {
  161. 0
  162. }
  163. }
  164. };
  165. assert_eq!(expected.to_string(), expanded.to_string());
  166. }
  167. #[test]
  168. fn pub_crate_function() {
  169. let prog = CgroupSkb::parse(
  170. parse_quote!(egress),
  171. parse_quote!(
  172. pub(crate) fn foo(ctx: SkBuffContext) -> i32 {
  173. 0
  174. }
  175. ),
  176. )
  177. .unwrap();
  178. let expanded = prog.expand().unwrap();
  179. let expected = quote! {
  180. #[no_mangle]
  181. #[link_section = "cgroup_skb/egress"]
  182. pub(crate) fn foo(ctx: *mut ::aya_bpf::bindings::__sk_buff) -> i32 {
  183. return foo(::aya_bpf::programs::SkBuffContext::new(ctx));
  184. pub(crate) fn foo(ctx: SkBuffContext) -> i32 {
  185. 0
  186. }
  187. }
  188. };
  189. assert_eq!(expected.to_string(), expanded.to_string());
  190. }
  191. }