cgroup_sock.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 CgroupSock {
  7. item: ItemFn,
  8. attach_type: String,
  9. }
  10. impl CgroupSock {
  11. pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<CgroupSock> {
  12. let item: ItemFn = syn::parse2(item)?;
  13. let attach_type: Ident = syn::parse2(attrs)?;
  14. match attach_type.to_string().as_str() {
  15. "post_bind4" | "post_bind6" | "sock_create" | "sock_release" => (),
  16. _ => abort!(attach_type, "invalid attach type"),
  17. }
  18. Ok(CgroupSock {
  19. item,
  20. attach_type: attach_type.to_string(),
  21. })
  22. }
  23. pub(crate) fn expand(&self) -> Result<TokenStream> {
  24. let section_name: Cow<'_, _> = format!("cgroup/{}", self.attach_type).into();
  25. let fn_vis = &self.item.vis;
  26. let fn_name = self.item.sig.ident.clone();
  27. let item = &self.item;
  28. Ok(quote! {
  29. #[no_mangle]
  30. #[link_section = #section_name]
  31. #fn_vis fn #fn_name(ctx: *mut ::aya_bpf::bindings::bpf_sock) -> i32 {
  32. return #fn_name(::aya_bpf::programs::SockContext::new(ctx));
  33. #item
  34. }
  35. })
  36. }
  37. }
  38. #[cfg(test)]
  39. mod tests {
  40. use super::*;
  41. use syn::parse_quote;
  42. #[test]
  43. fn cgroup_sock_post_bind4() {
  44. let prog = CgroupSock::parse(
  45. parse_quote!(post_bind4),
  46. parse_quote!(
  47. fn foo(ctx: SockContext) -> i32 {
  48. 0
  49. }
  50. ),
  51. )
  52. .unwrap();
  53. let expanded = prog.expand().unwrap();
  54. let expected = quote! {
  55. #[no_mangle]
  56. #[link_section = "cgroup/post_bind4"]
  57. fn foo(ctx: *mut ::aya_bpf::bindings::bpf_sock) -> i32 {
  58. return foo(::aya_bpf::programs::SockContext::new(ctx));
  59. fn foo(ctx: SockContext) -> i32 {
  60. 0
  61. }
  62. }
  63. };
  64. assert_eq!(expected.to_string(), expanded.to_string());
  65. }
  66. #[test]
  67. fn cgroup_sock_post_bind6() {
  68. let prog = CgroupSock::parse(
  69. parse_quote!(post_bind6),
  70. parse_quote!(
  71. fn foo(ctx: SockContext) -> i32 {
  72. 0
  73. }
  74. ),
  75. )
  76. .unwrap();
  77. let expanded = prog.expand().unwrap();
  78. let expected = quote! {
  79. #[no_mangle]
  80. #[link_section = "cgroup/post_bind6"]
  81. fn foo(ctx: *mut ::aya_bpf::bindings::bpf_sock) -> i32 {
  82. return foo(::aya_bpf::programs::SockContext::new(ctx));
  83. fn foo(ctx: SockContext) -> i32 {
  84. 0
  85. }
  86. }
  87. };
  88. assert_eq!(expected.to_string(), expanded.to_string());
  89. }
  90. #[test]
  91. fn cgroup_sock_sock_create() {
  92. let prog = CgroupSock::parse(
  93. parse_quote!(sock_create),
  94. parse_quote!(
  95. fn foo(ctx: SockContext) -> i32 {
  96. 0
  97. }
  98. ),
  99. )
  100. .unwrap();
  101. let expanded = prog.expand().unwrap();
  102. let expected = quote! {
  103. #[no_mangle]
  104. #[link_section = "cgroup/sock_create"]
  105. fn foo(ctx: *mut ::aya_bpf::bindings::bpf_sock) -> i32 {
  106. return foo(::aya_bpf::programs::SockContext::new(ctx));
  107. fn foo(ctx: SockContext) -> i32 {
  108. 0
  109. }
  110. }
  111. };
  112. assert_eq!(expected.to_string(), expanded.to_string());
  113. }
  114. #[test]
  115. fn cgroup_sock_sock_release() {
  116. let prog = CgroupSock::parse(
  117. parse_quote!(sock_release),
  118. parse_quote!(
  119. fn foo(ctx: SockContext) -> i32 {
  120. 0
  121. }
  122. ),
  123. )
  124. .unwrap();
  125. let expanded = prog.expand().unwrap();
  126. let expected = quote! {
  127. #[no_mangle]
  128. #[link_section = "cgroup/sock_release"]
  129. fn foo(ctx: *mut ::aya_bpf::bindings::bpf_sock) -> i32 {
  130. return foo(::aya_bpf::programs::SockContext::new(ctx));
  131. fn foo(ctx: SockContext) -> i32 {
  132. 0
  133. }
  134. }
  135. };
  136. assert_eq!(expected.to_string(), expanded.to_string());
  137. }
  138. }