lib.rs 2.7 KB

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