lib.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //! [![](https://aya-rs.dev/assets/images/aya_logo_docs.svg)](https://aya-rs.dev)
  2. //!
  3. //! A library to write eBPF programs.
  4. //!
  5. //! Aya-bpf is an eBPF library built with a focus on operability and developer experience.
  6. //! It is the kernel-space counterpart of [Aya](https://docs.rs/aya)
  7. #![doc(
  8. html_logo_url = "https://aya-rs.dev/assets/images/crabby.svg",
  9. html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg"
  10. )]
  11. #![cfg_attr(unstable, feature(never_type))]
  12. #![allow(clippy::missing_safety_doc)]
  13. #![no_std]
  14. pub use aya_bpf_bindings::bindings;
  15. mod args;
  16. pub use args::PtRegs;
  17. pub mod helpers;
  18. pub mod maps;
  19. pub mod programs;
  20. pub use aya_bpf_cty as cty;
  21. use core::ffi::c_void;
  22. use cty::{c_int, c_long};
  23. use helpers::{bpf_get_current_comm, bpf_get_current_pid_tgid};
  24. pub use aya_bpf_macros as macros;
  25. pub const TASK_COMM_LEN: usize = 16;
  26. pub trait BpfContext {
  27. fn as_ptr(&self) -> *mut c_void;
  28. #[inline]
  29. fn command(&self) -> Result<[u8; TASK_COMM_LEN], c_long> {
  30. bpf_get_current_comm()
  31. }
  32. fn pid(&self) -> u32 {
  33. bpf_get_current_pid_tgid() as u32
  34. }
  35. fn tgid(&self) -> u32 {
  36. (bpf_get_current_pid_tgid() >> 32) as u32
  37. }
  38. }
  39. #[no_mangle]
  40. pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
  41. let base = s as usize;
  42. for i in 0..n {
  43. *((base + i) as *mut u8) = c as u8;
  44. }
  45. }
  46. #[no_mangle]
  47. pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
  48. let dest_base = dest as usize;
  49. let src_base = src as usize;
  50. for i in 0..n {
  51. *((dest_base + i) as *mut u8) = *((src_base + i) as *mut u8);
  52. }
  53. }