|
|
|
@ -101,11 +101,12 @@ pub(crate) fn bpf_pin_object(fd: RawFd, path: &CStr) -> SysResult<c_long> {
|
|
|
|
|
sys_bpf(bpf_cmd::BPF_OBJ_PIN, &attr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn bpf_get_object(path: &CStr) -> SysResult<c_long> {
|
|
|
|
|
pub(crate) fn bpf_get_object(path: &CStr) -> SysResult<OwnedFd> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
let u = unsafe { &mut attr.__bindgen_anon_4 };
|
|
|
|
|
u.pathname = path.as_ptr() as u64;
|
|
|
|
|
sys_bpf(bpf_cmd::BPF_OBJ_GET, &attr)
|
|
|
|
|
// SAFETY: BPF_OBJ_GET returns a new file descriptor.
|
|
|
|
|
unsafe { fd_sys_bpf(bpf_cmd::BPF_OBJ_GET, &attr) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct BpfLoadProgramAttrs<'a> {
|
|
|
|
@ -370,7 +371,7 @@ pub(crate) fn bpf_link_create(
|
|
|
|
|
attach_type: bpf_attach_type,
|
|
|
|
|
btf_id: Option<u32>,
|
|
|
|
|
flags: u32,
|
|
|
|
|
) -> SysResult<c_long> {
|
|
|
|
|
) -> SysResult<OwnedFd> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
|
|
|
|
|
attr.link_create.__bindgen_anon_1.prog_fd = prog_fd as u32;
|
|
|
|
@ -381,19 +382,20 @@ pub(crate) fn bpf_link_create(
|
|
|
|
|
attr.link_create.__bindgen_anon_3.target_btf_id = btf_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sys_bpf(bpf_cmd::BPF_LINK_CREATE, &attr)
|
|
|
|
|
// SAFETY: BPF_LINK_CREATE returns a new file descriptor.
|
|
|
|
|
unsafe { fd_sys_bpf(bpf_cmd::BPF_LINK_CREATE, &attr) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// since kernel 5.7
|
|
|
|
|
pub(crate) fn bpf_link_update(
|
|
|
|
|
link_fd: RawFd,
|
|
|
|
|
link_fd: BorrowedFd<'_>,
|
|
|
|
|
new_prog_fd: RawFd,
|
|
|
|
|
old_prog_fd: Option<RawFd>,
|
|
|
|
|
flags: u32,
|
|
|
|
|
) -> SysResult<c_long> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
|
|
|
|
|
attr.link_update.link_fd = link_fd as u32;
|
|
|
|
|
attr.link_update.link_fd = link_fd.as_raw_fd() as u32;
|
|
|
|
|
attr.link_update.__bindgen_anon_1.new_prog_fd = new_prog_fd as u32;
|
|
|
|
|
if let Some(fd) = old_prog_fd {
|
|
|
|
|
attr.link_update.__bindgen_anon_2.old_prog_fd = fd as u32;
|
|
|
|
@ -483,12 +485,12 @@ pub(crate) fn bpf_prog_get_info_by_fd(prog_fd: RawFd) -> Result<bpf_prog_info, i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn bpf_map_get_info_by_fd(prog_fd: RawFd) -> Result<bpf_map_info, io::Error> {
|
|
|
|
|
pub(crate) fn bpf_map_get_info_by_fd(prog_fd: BorrowedFd<'_>) -> Result<bpf_map_info, io::Error> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
// info gets entirely populated by the kernel
|
|
|
|
|
let info = MaybeUninit::zeroed();
|
|
|
|
|
|
|
|
|
|
attr.info.bpf_fd = prog_fd as u32;
|
|
|
|
|
attr.info.bpf_fd = prog_fd.as_raw_fd() as u32;
|
|
|
|
|
attr.info.info = info.as_ptr() as *const _ as u64;
|
|
|
|
|
attr.info.info_len = mem::size_of::<bpf_map_info>() as u32;
|
|
|
|
|
|
|
|
|
@ -498,12 +500,12 @@ pub(crate) fn bpf_map_get_info_by_fd(prog_fd: RawFd) -> Result<bpf_map_info, io:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn bpf_link_get_info_by_fd(link_fd: RawFd) -> Result<bpf_link_info, io::Error> {
|
|
|
|
|
pub(crate) fn bpf_link_get_info_by_fd(link_fd: BorrowedFd<'_>) -> Result<bpf_link_info, io::Error> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
// info gets entirely populated by the kernel
|
|
|
|
|
let info = unsafe { MaybeUninit::zeroed().assume_init() };
|
|
|
|
|
|
|
|
|
|
attr.info.bpf_fd = link_fd as u32;
|
|
|
|
|
attr.info.bpf_fd = link_fd.as_raw_fd() as u32;
|
|
|
|
|
attr.info.info = &info as *const _ as u64;
|
|
|
|
|
attr.info.info_len = mem::size_of::<bpf_link_info>() as u32;
|
|
|
|
|
|
|
|
|
@ -532,7 +534,7 @@ pub(crate) fn btf_obj_get_info_by_fd(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> SysResult<c_long> {
|
|
|
|
|
pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> SysResult<OwnedFd> {
|
|
|
|
|
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
|
|
|
|
|
|
|
|
|
|
attr.raw_tracepoint.name = match name {
|
|
|
|
@ -541,7 +543,8 @@ pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> Sy
|
|
|
|
|
};
|
|
|
|
|
attr.raw_tracepoint.prog_fd = prog_fd as u32;
|
|
|
|
|
|
|
|
|
|
sys_bpf(bpf_cmd::BPF_RAW_TRACEPOINT_OPEN, &attr)
|
|
|
|
|
// SAFETY: BPF_RAW_TRACEPOINT_OPEN returns a new file descriptor.
|
|
|
|
|
unsafe { fd_sys_bpf(bpf_cmd::BPF_RAW_TRACEPOINT_OPEN, &attr) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn bpf_load_btf(
|
|
|
|
|