Skip to main content

kbpf_basic/
lib.rs

1//! Basic eBPF library providing essential functionalities for eBPF programs.
2//!! This library includes support for BPF maps, helper functions, and program
3//! loading mechanisms, making it easier to develop and run eBPF programs in a
4//! kernel-like environment.
5//!
6
7#![deny(missing_docs)]
8#![no_std]
9#![feature(c_variadic)]
10#![allow(unused)]
11extern crate alloc;
12use alloc::string::String;
13
14use map::UnifiedMap;
15pub mod helper;
16pub mod linux_bpf;
17pub mod map;
18pub mod perf;
19mod preprocessor;
20pub mod prog;
21pub mod raw_tracepoint;
22
23pub use preprocessor::EBPFPreProcessor;
24
25type Result<T> = axerrno::LinuxResult<T>;
26type BpfError = axerrno::LinuxError;
27
28/// PollWaiter trait for maps that support polling.
29pub trait PollWaker: Send + Sync {
30    /// Wake up any waiters on the map.
31    fn wake_up(&self);
32}
33
34/// The KernelAuxiliaryOps trait provides auxiliary operations which should
35/// be implemented by the kernel or a kernel-like environment.
36pub trait KernelAuxiliaryOps: Send + Sync + 'static {
37    /// Get a unified map from a pointer.
38    fn get_unified_map_from_ptr<F, R>(ptr: *const u8, func: F) -> Result<R>
39    where
40        F: FnOnce(&mut UnifiedMap) -> Result<R>;
41    /// Get a unified map from a file descriptor.
42    fn get_unified_map_from_fd<F, R>(map_fd: u32, func: F) -> Result<R>
43    where
44        F: FnOnce(&mut UnifiedMap) -> Result<R>;
45    /// Get a unified map pointer from a file descriptor.
46    fn get_unified_map_ptr_from_fd(map_fd: u32) -> Result<*const u8>;
47    /// Copy data from a user space pointer to a kernel space buffer.
48    fn copy_from_user(src: *const u8, size: usize, dst: &mut [u8]) -> Result<()>;
49    /// Copy data from a kernel space buffer to a user space pointer.
50    fn copy_to_user(dest: *mut u8, size: usize, src: &[u8]) -> Result<()>;
51    /// Get the current CPU ID.
52    fn current_cpu_id() -> u32;
53    /// Output some data to a perf buf
54    fn perf_event_output(
55        ctx: *mut core::ffi::c_void,
56        fd: u32,
57        flags: u32,
58        data: &[u8],
59    ) -> Result<()>;
60    /// Read a string from a user space pointer.
61    fn string_from_user_cstr(ptr: *const u8) -> Result<String>;
62    /// For ebpf print helper functions
63    fn ebpf_write_str(str: &str) -> Result<()>;
64    /// For ebpf ktime helper functions
65    fn ebpf_time_ns() -> Result<u64>;
66
67    /// Allocate pages in kernel space. Return the physical address of the allocated page.
68    fn alloc_page() -> Result<usize>;
69    /// Free the allocated page in kernel space.
70    fn free_page(phys_addr: usize);
71    /// Create a virtual mapping for the given physical addresses. Return the virtual address.
72    fn vmap(phys_addrs: &[usize]) -> Result<usize>;
73    /// Unmap the given virtual address.
74    fn unmap(vaddr: usize);
75}
76
77struct DummyAuxImpl;
78impl KernelAuxiliaryOps for DummyAuxImpl {
79    fn get_unified_map_from_ptr<F, R>(_ptr: *const u8, _func: F) -> Result<R>
80    where
81        F: FnOnce(&mut UnifiedMap) -> Result<R>,
82    {
83        Err(BpfError::EPERM)
84    }
85
86    fn get_unified_map_from_fd<F, R>(_map_fd: u32, _func: F) -> Result<R>
87    where
88        F: FnOnce(&mut UnifiedMap) -> Result<R>,
89    {
90        Err(BpfError::EPERM)
91    }
92
93    fn get_unified_map_ptr_from_fd(_map_fd: u32) -> Result<*const u8> {
94        Err(BpfError::EPERM)
95    }
96
97    fn copy_from_user(_src: *const u8, _size: usize, _dst: &mut [u8]) -> Result<()> {
98        Err(BpfError::EPERM)
99    }
100
101    fn copy_to_user(_dest: *mut u8, _size: usize, _src: &[u8]) -> Result<()> {
102        Err(BpfError::EPERM)
103    }
104
105    fn current_cpu_id() -> u32 {
106        0
107    }
108
109    fn perf_event_output(
110        _ctx: *mut core::ffi::c_void,
111        _fd: u32,
112        _flags: u32,
113        _data: &[u8],
114    ) -> Result<()> {
115        Err(BpfError::EPERM)
116    }
117
118    fn string_from_user_cstr(_ptr: *const u8) -> Result<String> {
119        Err(BpfError::EPERM)
120    }
121
122    fn ebpf_write_str(_str: &str) -> Result<()> {
123        Err(BpfError::EPERM)
124    }
125
126    fn ebpf_time_ns() -> Result<u64> {
127        Err(BpfError::EPERM)
128    }
129
130    fn alloc_page() -> Result<usize> {
131        Err(BpfError::EPERM)
132    }
133
134    fn free_page(_phys_addr: usize) {}
135
136    fn vmap(_phys_addrs: &[usize]) -> Result<usize> {
137        Err(BpfError::EPERM)
138    }
139
140    fn unmap(_vaddr: usize) {}
141}