4
0

cgroup_sock.rs 4.5 KB

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