xref: /DragonOS/kernel/src/bpf/mod.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 pub mod helper;
2 pub mod map;
3 pub mod prog;
4 use crate::include::bindings::linux_bpf::{bpf_attr, bpf_cmd};
5 use crate::syscall::user_access::UserBufferReader;
6 use crate::syscall::Syscall;
7 use log::error;
8 use num_traits::FromPrimitive;
9 use system_error::SystemError;
10 
11 type Result<T> = core::result::Result<T, SystemError>;
12 
13 impl Syscall {
sys_bpf(cmd: u32, attr: *mut u8, size: u32) -> Result<usize>14     pub fn sys_bpf(cmd: u32, attr: *mut u8, size: u32) -> Result<usize> {
15         let buf = UserBufferReader::new(attr, size as usize, true)?;
16         let attr = buf.read_one_from_user::<bpf_attr>(0)?;
17         let cmd = bpf_cmd::from_u32(cmd).ok_or(SystemError::EINVAL)?;
18         bpf(cmd, attr)
19     }
20 }
21 
bpf(cmd: bpf_cmd, attr: &bpf_attr) -> Result<usize>22 pub fn bpf(cmd: bpf_cmd, attr: &bpf_attr) -> Result<usize> {
23     let res = match cmd {
24         // Map related commands
25         bpf_cmd::BPF_MAP_CREATE => map::bpf_map_create(attr),
26         bpf_cmd::BPF_MAP_UPDATE_ELEM => map::bpf_map_update_elem(attr),
27         bpf_cmd::BPF_MAP_LOOKUP_ELEM => map::bpf_lookup_elem(attr),
28         bpf_cmd::BPF_MAP_GET_NEXT_KEY => map::bpf_map_get_next_key(attr),
29         bpf_cmd::BPF_MAP_DELETE_ELEM => map::bpf_map_delete_elem(attr),
30         bpf_cmd::BPF_MAP_LOOKUP_AND_DELETE_ELEM => map::bpf_map_lookup_and_delete_elem(attr),
31         bpf_cmd::BPF_MAP_LOOKUP_BATCH => map::bpf_map_lookup_batch(attr),
32         bpf_cmd::BPF_MAP_FREEZE => map::bpf_map_freeze(attr),
33         // Program related commands
34         bpf_cmd::BPF_PROG_LOAD => prog::bpf_prog_load(attr),
35         // Object creation commands
36         bpf_cmd::BPF_BTF_LOAD => {
37             error!("bpf cmd {:?} not implemented", cmd);
38             return Err(SystemError::ENOSYS);
39         }
40         ty => {
41             unimplemented!("bpf cmd {:?} not implemented", ty)
42         }
43     };
44     res
45 }
46 
47 /// Initialize the BPF system
init_bpf_system()48 pub fn init_bpf_system() {
49     helper::init_helper_functions();
50 }
51