lib.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. mod expand;
  2. use expand::{
  3. Args, BtfTracePoint, CgroupSkb, FEntry, FExit, Lsm, Map, PerfEvent, Probe, ProbeKind,
  4. RawTracePoint, SchedClassifier, SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint,
  5. Xdp,
  6. };
  7. use proc_macro::TokenStream;
  8. use syn::{parse_macro_input, ItemFn, ItemStatic};
  9. #[proc_macro_attribute]
  10. pub fn map(attrs: TokenStream, item: TokenStream) -> TokenStream {
  11. let args = parse_macro_input!(attrs as Args);
  12. let item = parse_macro_input!(item as ItemStatic);
  13. Map::from_syn(args, item)
  14. .and_then(|u| u.expand())
  15. .unwrap_or_else(|err| err.to_compile_error())
  16. .into()
  17. }
  18. #[proc_macro_attribute]
  19. pub fn kprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  20. probe(ProbeKind::KProbe, attrs, item)
  21. }
  22. #[proc_macro_attribute]
  23. pub fn kretprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  24. probe(ProbeKind::KRetProbe, attrs, item)
  25. }
  26. #[proc_macro_attribute]
  27. pub fn uprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  28. probe(ProbeKind::UProbe, attrs, item)
  29. }
  30. #[proc_macro_attribute]
  31. pub fn uretprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  32. probe(ProbeKind::URetProbe, attrs, item)
  33. }
  34. #[proc_macro_attribute]
  35. pub fn sock_ops(attrs: TokenStream, item: TokenStream) -> TokenStream {
  36. let args = parse_macro_input!(attrs as Args);
  37. let item = parse_macro_input!(item as ItemFn);
  38. SockOps::from_syn(args, item)
  39. .and_then(|u| u.expand())
  40. .unwrap_or_else(|err| err.to_compile_error())
  41. .into()
  42. }
  43. #[proc_macro_attribute]
  44. pub fn sk_msg(attrs: TokenStream, item: TokenStream) -> TokenStream {
  45. let args = parse_macro_input!(attrs as Args);
  46. let item = parse_macro_input!(item as ItemFn);
  47. SkMsg::from_syn(args, item)
  48. .and_then(|u| u.expand())
  49. .unwrap_or_else(|err| err.to_compile_error())
  50. .into()
  51. }
  52. #[proc_macro_attribute]
  53. pub fn xdp(attrs: TokenStream, item: TokenStream) -> TokenStream {
  54. let args = parse_macro_input!(attrs as Args);
  55. let item = parse_macro_input!(item as ItemFn);
  56. Xdp::from_syn(args, item)
  57. .and_then(|u| u.expand())
  58. .unwrap_or_else(|err| err.to_compile_error())
  59. .into()
  60. }
  61. #[proc_macro_attribute]
  62. pub fn classifier(attrs: TokenStream, item: TokenStream) -> TokenStream {
  63. let args = parse_macro_input!(attrs as Args);
  64. let item = parse_macro_input!(item as ItemFn);
  65. SchedClassifier::from_syn(args, item)
  66. .and_then(|u| u.expand())
  67. .unwrap_or_else(|err| err.to_compile_error())
  68. .into()
  69. }
  70. #[proc_macro_attribute]
  71. pub fn cgroup_skb(attrs: TokenStream, item: TokenStream) -> TokenStream {
  72. let args = parse_macro_input!(attrs as Args);
  73. let item = parse_macro_input!(item as ItemFn);
  74. CgroupSkb::from_syn(args, item)
  75. .and_then(|u| u.expand())
  76. .unwrap_or_else(|err| err.to_compile_error())
  77. .into()
  78. }
  79. fn probe(kind: ProbeKind, attrs: TokenStream, item: TokenStream) -> TokenStream {
  80. let args = parse_macro_input!(attrs as Args);
  81. let item = parse_macro_input!(item as ItemFn);
  82. Probe::from_syn(kind, args, item)
  83. .and_then(|u| u.expand())
  84. .unwrap_or_else(|err| err.to_compile_error())
  85. .into()
  86. }
  87. #[proc_macro_attribute]
  88. pub fn tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  89. let args = parse_macro_input!(attrs as Args);
  90. let item = parse_macro_input!(item as ItemFn);
  91. TracePoint::from_syn(args, item)
  92. .and_then(|u| u.expand())
  93. .unwrap_or_else(|err| err.to_compile_error())
  94. .into()
  95. }
  96. #[proc_macro_attribute]
  97. pub fn perf_event(attrs: TokenStream, item: TokenStream) -> TokenStream {
  98. let args = parse_macro_input!(attrs as Args);
  99. let item = parse_macro_input!(item as ItemFn);
  100. PerfEvent::from_syn(args, item)
  101. .and_then(|u| u.expand())
  102. .unwrap_or_else(|err| err.to_compile_error())
  103. .into()
  104. }
  105. /// Marks a function as a raw tracepoint eBPF program that can be attached at a
  106. /// pre-defined kernel trace point.
  107. ///
  108. /// The kernel provides a set of pre-defined trace points that eBPF programs can
  109. /// be attached to. See `/sys/kernel/debug/tracing/events` for a list of which
  110. /// events can be traced.
  111. ///
  112. /// # Minimum kernel version
  113. ///
  114. /// The minimum kernel version required to use this feature is 4.7.
  115. ///
  116. /// # Examples
  117. ///
  118. /// ```no_run
  119. /// use aya_bpf::{macros::raw_tracepoint, programs::RawTracePointContext};
  120. ///
  121. /// #[raw_tracepoint(name = "sys_enter")]
  122. /// pub fn sys_enter(ctx: RawTracePointContext) -> i32 {
  123. /// match unsafe { try_sys_enter(ctx) } {
  124. /// Ok(ret) => ret,
  125. /// Err(ret) => ret,
  126. /// }
  127. /// }
  128. ///
  129. /// unsafe fn try_sys_enter(_ctx: RawTracePointContext) -> Result<i32, i32> {
  130. /// Ok(0)
  131. /// }
  132. /// ```
  133. #[proc_macro_attribute]
  134. pub fn raw_tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  135. let args = parse_macro_input!(attrs as Args);
  136. let item = parse_macro_input!(item as ItemFn);
  137. RawTracePoint::from_syn(args, item)
  138. .and_then(|u| u.expand())
  139. .unwrap_or_else(|err| err.to_compile_error())
  140. .into()
  141. }
  142. /// Marks a function as an LSM program that can be attached to Linux LSM hooks.
  143. /// Used to implement security policy and audit logging.
  144. ///
  145. /// LSM probes can be attached to the kernel's [security hooks][1] to implement mandatory
  146. /// access control policy and security auditing.
  147. ///
  148. /// LSM probes require a kernel compiled with `CONFIG_BPF_LSM=y` and `CONFIG_DEBUG_INFO_BTF=y`.
  149. /// In order for the probes to fire, you also need the BPF LSM to be enabled through your
  150. /// kernel's boot paramters (like `lsm=lockdown,yama,bpf`).
  151. ///
  152. /// # Minimum kernel version
  153. ///
  154. /// The minimum kernel version required to use this feature is 5.7.
  155. ///
  156. /// # Examples
  157. ///
  158. /// ```no_run
  159. /// use aya_bpf::{macros::lsm, programs::LsmContext};
  160. ///
  161. /// #[lsm(name = "file_open")]
  162. /// pub fn file_open(ctx: LsmContext) -> i32 {
  163. /// match unsafe { try_file_open(ctx) } {
  164. /// Ok(ret) => ret,
  165. /// Err(ret) => ret,
  166. /// }
  167. /// }
  168. ///
  169. /// unsafe fn try_file_open(_ctx: LsmContext) -> Result<i32, i32> {
  170. /// Ok(0)
  171. /// }
  172. /// ```
  173. #[proc_macro_attribute]
  174. pub fn lsm(attrs: TokenStream, item: TokenStream) -> TokenStream {
  175. let args = parse_macro_input!(attrs as Args);
  176. let item = parse_macro_input!(item as ItemFn);
  177. Lsm::from_syn(args, item)
  178. .and_then(|u| u.expand())
  179. .unwrap_or_else(|err| err.to_compile_error())
  180. .into()
  181. }
  182. /// Marks a function as a [BTF-enabled raw tracepoint][1] eBPF program that can be attached at
  183. /// a pre-defined kernel trace point.
  184. ///
  185. /// The kernel provides a set of pre-defined trace points that eBPF programs can
  186. /// be attached to. See `/sys/kernel/debug/tracing/events` for a list of which
  187. /// events can be traced.
  188. ///
  189. /// # Minimum kernel version
  190. ///
  191. /// The minimum kernel version required to use this feature is 5.5.
  192. ///
  193. /// # Examples
  194. ///
  195. /// ```no_run
  196. /// use aya_bpf::{macros::btf_tracepoint, programs::BtfTracePointContext};
  197. ///
  198. /// #[btf_tracepoint(name = "sched_process_fork")]
  199. /// pub fn sched_process_fork(ctx: BtfTracePointContext) -> u32 {
  200. /// match unsafe { try_sched_process_fork(ctx) } {
  201. /// Ok(ret) => ret,
  202. /// Err(ret) => ret,
  203. /// }
  204. /// }
  205. ///
  206. /// unsafe fn try_sched_process_fork(_ctx: BtfTracePointContext) -> Result<u32, u32> {
  207. /// Ok(0)
  208. /// }
  209. /// ```
  210. ///
  211. /// [1]: https://github.com/torvalds/linux/commit/9e15db66136a14cde3f35691f1d839d950118826
  212. #[proc_macro_attribute]
  213. pub fn btf_tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  214. let args = parse_macro_input!(attrs as Args);
  215. let item = parse_macro_input!(item as ItemFn);
  216. BtfTracePoint::from_syn(args, item)
  217. .and_then(|u| u.expand())
  218. .unwrap_or_else(|err| err.to_compile_error())
  219. .into()
  220. }
  221. /// Marks a function as a SK_SKB Stream Parser eBPF program that can be attached
  222. /// to a SockMap
  223. ///
  224. /// # Minimum kernel version
  225. ///
  226. /// The minimum kernel version required to use this feature is 4.14
  227. ///
  228. /// # Examples
  229. ///
  230. /// ```no_run
  231. /// use aya_bpf::{macros::stream_parser, programs::SkBuffContext};
  232. ///
  233. ///
  234. ///#[stream_parser]
  235. ///fn stream_parser(ctx: SkBuffContext) -> u32 {
  236. /// match { try_stream_parser(ctx) } {
  237. /// Ok(ret) => ret,
  238. /// Err(ret) => ret,
  239. /// }
  240. ///}
  241. ///
  242. ///fn try_stream_parser(ctx: SkBuffContext) -> Result<u32, u32> {
  243. /// Ok(ctx.len())
  244. ///}
  245. /// ```
  246. #[proc_macro_attribute]
  247. pub fn stream_parser(attrs: TokenStream, item: TokenStream) -> TokenStream {
  248. sk_skb(SkSkbKind::StreamParser, attrs, item)
  249. }
  250. /// Marks a function as a SK_SKB Stream Verdict eBPF program that can be attached
  251. /// to a SockMap
  252. ///
  253. /// # Minimum kernel version
  254. ///
  255. /// The minimum kernel version required to use this feature is 4.14
  256. ///
  257. /// # Examples
  258. ///
  259. /// ```no_run
  260. /// use aya_bpf::{macros::stream_verdict, programs::SkBuffContext, bindings::sk_action};
  261. ///
  262. ///
  263. ///#[stream_verdict]
  264. ///fn stream_verdict(ctx: SkBuffContext) -> u32 {
  265. /// match { try_stream_verdict(ctx) } {
  266. /// Ok(ret) => ret,
  267. /// Err(ret) => ret,
  268. /// }
  269. ///}
  270. ///
  271. ///fn try_stream_verdict(_ctx: SkBuffContext) -> Result<u32, u32> {
  272. /// Ok(sk_action::SK_PASS)
  273. ///}
  274. /// ```
  275. #[proc_macro_attribute]
  276. pub fn stream_verdict(attrs: TokenStream, item: TokenStream) -> TokenStream {
  277. sk_skb(SkSkbKind::StreamVerdict, attrs, item)
  278. }
  279. fn sk_skb(kind: SkSkbKind, attrs: TokenStream, item: TokenStream) -> TokenStream {
  280. let args = parse_macro_input!(attrs as Args);
  281. let item = parse_macro_input!(item as ItemFn);
  282. SkSkb::from_syn(kind, args, item)
  283. .and_then(|u| u.expand())
  284. .unwrap_or_else(|err| err.to_compile_error())
  285. .into()
  286. }
  287. /// Marks a function as a eBPF Socket Filter program that can be attached to
  288. /// a socket.
  289. ///
  290. /// # Minimum kernel version
  291. ///
  292. /// The minimum kernel version required to use this feature is 3.19
  293. ///
  294. /// # Examples
  295. ///
  296. /// ```no_run
  297. /// use aya_bpf::{macros::socket_filter, programs::SkBuffContext};
  298. ///
  299. /// #[socket_filter(name = "accept_all")]
  300. /// pub fn accept_all(_ctx: SkBuffContext) -> i64 {
  301. /// return 0
  302. /// }
  303. /// ```
  304. #[proc_macro_attribute]
  305. pub fn socket_filter(attrs: TokenStream, item: TokenStream) -> TokenStream {
  306. let args = parse_macro_input!(attrs as Args);
  307. let item = parse_macro_input!(item as ItemFn);
  308. SocketFilter::from_syn(args, item)
  309. .and_then(|u| u.expand())
  310. .unwrap_or_else(|err| err.to_compile_error())
  311. .into()
  312. }
  313. /// Marks a function as a fentry eBPF program that can be attached to almost
  314. /// any function inside the kernel. The difference between fentry and kprobe
  315. /// is that fexit has practically zero overhead to call before kernel function.
  316. /// fentry programs can be also attached to other eBPF programs.
  317. ///
  318. /// # Minimumm kernel version
  319. ///
  320. /// The minimum kernel version required to use this feature is 5.5.
  321. ///
  322. /// # Examples
  323. ///
  324. /// ```no_run
  325. /// # #![allow(non_camel_case_types)]
  326. /// use aya_bpf::{macros::fentry, programs::FEntryContext};
  327. /// # type filename = u32;
  328. /// # type path = u32;
  329. ///
  330. /// #[fentry(name = "filename_lookup")]
  331. /// fn filename_lookup(ctx: FEntryContext) -> i32 {
  332. /// match unsafe { try_filename_lookup(ctx) } {
  333. /// Ok(ret) => ret,
  334. /// Err(ret) => ret,
  335. /// }
  336. /// }
  337. ///
  338. /// unsafe fn try_filename_lookup(ctx: FEntryContext) -> Result<i32, i32> {
  339. /// let _f: *const filename = ctx.arg(1);
  340. /// let _p: *const path = ctx.arg(3);
  341. ///
  342. /// Ok(0)
  343. /// }
  344. /// ```
  345. #[proc_macro_attribute]
  346. pub fn fentry(attrs: TokenStream, item: TokenStream) -> TokenStream {
  347. let args = parse_macro_input!(attrs as Args);
  348. let item = parse_macro_input!(item as ItemFn);
  349. FEntry::from_syn(args, item)
  350. .and_then(|u| u.expand())
  351. .unwrap_or_else(|err| err.to_compile_error())
  352. .into()
  353. }
  354. /// Marks a function as a fexit eBPF program that can be attached to almost
  355. /// any function inside the kernel. The difference between fexit and kretprobe
  356. /// is that fexit has practically zero overhead to call after kernel function
  357. /// and it focuses on access to arguments rather than the return value. fexit
  358. /// programs can be also attached to other eBPF programs
  359. ///
  360. /// # Minimumm kernel version
  361. ///
  362. /// The minimum kernel version required to use this feature is 5.5.
  363. ///
  364. /// # Examples
  365. ///
  366. /// ```no_run
  367. /// # #![allow(non_camel_case_types)]
  368. /// use aya_bpf::{macros::fexit, programs::FExitContext};
  369. /// # type filename = u32;
  370. /// # type path = u32;
  371. ///
  372. /// #[fexit(name = "filename_lookup")]
  373. /// fn filename_lookup(ctx: FExitContext) -> i32 {
  374. /// match unsafe { try_filename_lookup(ctx) } {
  375. /// Ok(ret) => ret,
  376. /// Err(ret) => ret,
  377. /// }
  378. /// }
  379. ///
  380. /// unsafe fn try_filename_lookup(ctx: FExitContext) -> Result<i32, i32> {
  381. /// let _f: *const filename = ctx.arg(1);
  382. /// let _p: *const path = ctx.arg(3);
  383. ///
  384. /// Ok(0)
  385. /// }
  386. /// ```
  387. #[proc_macro_attribute]
  388. pub fn fexit(attrs: TokenStream, item: TokenStream) -> TokenStream {
  389. let args = parse_macro_input!(attrs as Args);
  390. let item = parse_macro_input!(item as ItemFn);
  391. FExit::from_syn(args, item)
  392. .and_then(|u| u.expand())
  393. .unwrap_or_else(|err| err.to_compile_error())
  394. .into()
  395. }