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