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