From 7ee6f52a7442e97d81ef41bc75673c8560bec5b0 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 1 Aug 2023 17:27:30 -0400 Subject: [PATCH] sys: avoid repeating BPF_BTF_LOAD dance --- aya/src/sys/bpf.rs | 99 ++++------------------------------------------ 1 file changed, 7 insertions(+), 92 deletions(-) diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 9f7e11c6..dd285d3e 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -771,20 +771,7 @@ pub(crate) fn is_btf_supported() -> bool { let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); btf.add_type(int_type); let btf_bytes = btf.to_bytes(); - - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_func_supported() -> bool { @@ -814,19 +801,7 @@ pub(crate) fn is_btf_func_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_func_global_supported() -> bool { @@ -856,19 +831,7 @@ pub(crate) fn is_btf_func_global_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_datasec_supported() -> bool { @@ -892,19 +855,7 @@ pub(crate) fn is_btf_datasec_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_float_supported() -> bool { @@ -915,19 +866,7 @@ pub(crate) fn is_btf_float_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_decl_tag_supported() -> bool { @@ -946,19 +885,7 @@ pub(crate) fn is_btf_decl_tag_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub(crate) fn is_btf_type_tag_supported() -> bool { @@ -975,19 +902,7 @@ pub(crate) fn is_btf_type_tag_supported() -> bool { let btf_bytes = btf.to_bytes(); - let mut attr = unsafe { mem::zeroed::() }; - let u = unsafe { &mut attr.__bindgen_anon_7 }; - u.btf = btf_bytes.as_ptr() as u64; - u.btf_size = btf_bytes.len() as u32; - - match sys_bpf(bpf_cmd::BPF_BTF_LOAD, &attr) { - Ok(v) => { - let fd = v as RawFd; - unsafe { close(fd) }; - true - } - Err(_) => false, - } + bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } pub fn sys_bpf(cmd: bpf_cmd, attr: &bpf_attr) -> SysResult {