program_array.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use core::{cell::UnsafeCell, hint::unreachable_unchecked, mem};
  2. use aya_ebpf_cty::c_long;
  3. use crate::{
  4. bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PROG_ARRAY},
  5. helpers::bpf_tail_call,
  6. maps::PinningType,
  7. BpfContext,
  8. };
  9. /// A BPF map that stores an array of program indices for tail calling.
  10. ///
  11. /// # Examples
  12. ///
  13. /// ```no_run
  14. /// # #![allow(dead_code)]
  15. /// use aya_ebpf::{macros::map, maps::ProgramArray, cty::c_long};
  16. /// # use aya_ebpf::{programs::LsmContext};
  17. ///
  18. /// #[map]
  19. /// static JUMP_TABLE: ProgramArray = ProgramArray::with_max_entries(16, 0);
  20. ///
  21. /// # unsafe fn try_test(ctx: &LsmContext) -> Result<(), c_long> {
  22. /// let index: u32 = 13;
  23. ///
  24. /// if let Err(e) = JUMP_TABLE.tail_call(ctx, index) {
  25. /// return Err(e);
  26. /// }
  27. ///
  28. /// # Err(-1)
  29. /// }
  30. /// ```
  31. #[repr(transparent)]
  32. pub struct ProgramArray {
  33. def: UnsafeCell<bpf_map_def>,
  34. }
  35. unsafe impl Sync for ProgramArray {}
  36. impl ProgramArray {
  37. pub const fn with_max_entries(max_entries: u32, flags: u32) -> ProgramArray {
  38. ProgramArray {
  39. def: UnsafeCell::new(bpf_map_def {
  40. type_: BPF_MAP_TYPE_PROG_ARRAY,
  41. key_size: mem::size_of::<u32>() as u32,
  42. value_size: mem::size_of::<u32>() as u32,
  43. max_entries,
  44. map_flags: flags,
  45. id: 0,
  46. pinning: PinningType::None as u32,
  47. }),
  48. }
  49. }
  50. pub const fn pinned(max_entries: u32, flags: u32) -> ProgramArray {
  51. ProgramArray {
  52. def: UnsafeCell::new(bpf_map_def {
  53. type_: BPF_MAP_TYPE_PROG_ARRAY,
  54. key_size: mem::size_of::<u32>() as u32,
  55. value_size: mem::size_of::<u32>() as u32,
  56. max_entries,
  57. map_flags: flags,
  58. id: 0,
  59. pinning: PinningType::ByName as u32,
  60. }),
  61. }
  62. }
  63. /// Perform a tail call into a program indexed by this map.
  64. ///
  65. /// # Safety
  66. ///
  67. /// This function is inherently unsafe, since it causes control flow to jump into
  68. /// another eBPF program. This can have side effects, such as drop methods not being
  69. /// called. Note that tail calling into an eBPF program is not the same thing as
  70. /// a function call -- control flow never returns to the caller.
  71. ///
  72. /// # Return Value
  73. ///
  74. /// On success, this function **does not return** into the original program.
  75. /// On failure, a negative error is returned, wrapped in `Err()`.
  76. #[cfg(not(unstable))]
  77. pub unsafe fn tail_call<C: BpfContext>(&self, ctx: &C, index: u32) -> Result<(), c_long> {
  78. let res = bpf_tail_call(ctx.as_ptr(), self.def.get() as *mut _, index);
  79. if res != 0 {
  80. Err(res)
  81. } else {
  82. unreachable_unchecked()
  83. }
  84. }
  85. /// Perform a tail call into a program indexed by this map.
  86. ///
  87. /// # Safety
  88. ///
  89. /// This function is inherently unsafe, since it causes control flow to jump into
  90. /// another eBPF program. This can have side effects, such as drop methods not being
  91. /// called. Note that tail calling into an eBPF program is not the same thing as
  92. /// a function call -- control flow never returns to the caller.
  93. ///
  94. /// # Return Value
  95. ///
  96. /// On success, this function **does not return** into the original program.
  97. /// On failure, a negative error is returned, wrapped in `Err()`.
  98. #[cfg(unstable)]
  99. pub unsafe fn tail_call<C: BpfContext>(&self, ctx: &C, index: u32) -> Result<!, c_long> {
  100. let res = bpf_tail_call(ctx.as_ptr(), self.def.get() as *mut _, index);
  101. if res != 0 {
  102. Err(res)
  103. } else {
  104. unreachable_unchecked()
  105. }
  106. }
  107. }