lib.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #![cfg_attr(target_arch = "bpf", feature(asm_experimental_arch))]
  13. #![allow(clippy::missing_safety_doc)]
  14. #![warn(clippy::cast_lossless, clippy::cast_sign_loss)]
  15. #![no_std]
  16. pub use aya_bpf_bindings::bindings;
  17. mod args;
  18. pub use args::PtRegs;
  19. pub mod helpers;
  20. pub mod maps;
  21. pub mod programs;
  22. pub use aya_bpf_cty as cty;
  23. use core::ffi::c_void;
  24. use cty::{c_int, c_long};
  25. use helpers::{bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid};
  26. pub use aya_bpf_macros as macros;
  27. pub const TASK_COMM_LEN: usize = 16;
  28. pub trait BpfContext {
  29. fn as_ptr(&self) -> *mut c_void;
  30. #[inline]
  31. fn command(&self) -> Result<[u8; TASK_COMM_LEN], c_long> {
  32. bpf_get_current_comm()
  33. }
  34. fn pid(&self) -> u32 {
  35. bpf_get_current_pid_tgid() as u32
  36. }
  37. fn tgid(&self) -> u32 {
  38. (bpf_get_current_pid_tgid() >> 32) as u32
  39. }
  40. fn uid(&self) -> u32 {
  41. bpf_get_current_uid_gid() as u32
  42. }
  43. fn gid(&self) -> u32 {
  44. (bpf_get_current_uid_gid() >> 32) as u32
  45. }
  46. }
  47. #[no_mangle]
  48. pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
  49. #[allow(clippy::cast_sign_loss)]
  50. let b = c as u8;
  51. for i in 0..n {
  52. *s.add(i) = b;
  53. }
  54. }
  55. #[no_mangle]
  56. pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
  57. for i in 0..n {
  58. *dest.add(i) = *src.add(i);
  59. }
  60. }
  61. /// Check if a value is within a range, using conditional forms compatible with
  62. /// the verifier.
  63. #[inline(always)]
  64. pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool {
  65. #[cfg(target_arch = "bpf")]
  66. unsafe {
  67. let mut in_bounds = 0u64;
  68. core::arch::asm!(
  69. "if {value} s< {lower} goto +2",
  70. "if {value} s> {upper} goto +1",
  71. "{i} = 1",
  72. i = inout(reg) in_bounds,
  73. lower = in(reg) lower,
  74. upper = in(reg) upper,
  75. value = in(reg) value,
  76. );
  77. in_bounds == 1
  78. }
  79. // We only need this for doc tests which are compiled for the host target
  80. #[cfg(not(target_arch = "bpf"))]
  81. {
  82. let _ = value;
  83. let _ = lower;
  84. let _ = upper;
  85. unimplemented!()
  86. }
  87. }