Merge branch 'dev' into aya-obj/prog-type

pull/979/head
Tyrone Wu 10 months ago committed by GitHub
commit 46685188e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -476,6 +476,11 @@ pub const BPF_F_TOKEN_FD: _bindgen_ty_5 = 65536;
pub const BPF_F_SEGV_ON_FAULT: _bindgen_ty_5 = 131072;
pub const BPF_F_NO_USER_CONV: _bindgen_ty_5 = 262144;
pub type _bindgen_ty_5 = ::core::ffi::c_uint;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum bpf_stats_type {
BPF_STATS_RUN_TIME = 0,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union bpf_attr {

@ -476,6 +476,11 @@ pub const BPF_F_TOKEN_FD: _bindgen_ty_5 = 65536;
pub const BPF_F_SEGV_ON_FAULT: _bindgen_ty_5 = 131072;
pub const BPF_F_NO_USER_CONV: _bindgen_ty_5 = 262144;
pub type _bindgen_ty_5 = ::core::ffi::c_uint;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum bpf_stats_type {
BPF_STATS_RUN_TIME = 0,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union bpf_attr {

@ -476,6 +476,11 @@ pub const BPF_F_TOKEN_FD: _bindgen_ty_5 = 65536;
pub const BPF_F_SEGV_ON_FAULT: _bindgen_ty_5 = 131072;
pub const BPF_F_NO_USER_CONV: _bindgen_ty_5 = 262144;
pub type _bindgen_ty_5 = ::core::ffi::c_uint;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum bpf_stats_type {
BPF_STATS_RUN_TIME = 0,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union bpf_attr {

@ -476,6 +476,11 @@ pub const BPF_F_TOKEN_FD: _bindgen_ty_5 = 65536;
pub const BPF_F_SEGV_ON_FAULT: _bindgen_ty_5 = 131072;
pub const BPF_F_NO_USER_CONV: _bindgen_ty_5 = 262144;
pub type _bindgen_ty_5 = ::core::ffi::c_uint;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum bpf_stats_type {
BPF_STATS_RUN_TIME = 0,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union bpf_attr {

@ -92,10 +92,12 @@ pub mod maps;
pub mod obj;
pub mod programs;
pub mod relocation;
pub mod stats;
mod util;
pub use maps::Map;
pub use obj::*;
pub use stats::BpfStatsType;
/// An error returned from the verifier.
///

@ -0,0 +1,18 @@
//! BPF stats type for `BPF_ENABLE_STATS`
use crate::generated::bpf_stats_type;
/// The type of BPF statistic to enable.
#[derive(Copy, Clone, Debug)]
pub enum BpfStatsType {
/// Metrics for `run_time_ns` and `run_cnt`.
RunTime,
}
impl From<BpfStatsType> for bpf_stats_type {
fn from(value: BpfStatsType) -> Self {
match value {
BpfStatsType::RunTime => bpf_stats_type::BPF_STATS_RUN_TIME,
}
}
}

@ -14,15 +14,15 @@ use aya_obj::{
btf::{BtfFeatures, BtfRelocationError},
generated::{BPF_F_SLEEPABLE, BPF_F_XDP_HAS_FRAGS},
relocation::EbpfRelocationError,
EbpfSectionKind, Features,
BpfStatsType, EbpfSectionKind, Features,
};
use log::{debug, warn};
use thiserror::Error;
use crate::{
generated::{
bpf_map_type, bpf_map_type::*, AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE,
AYA_PERF_EVENT_IOC_SET_BPF,
bpf_map_type::{self, *},
AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE, AYA_PERF_EVENT_IOC_SET_BPF,
},
maps::{Map, MapData, MapError},
obj::{
@ -36,12 +36,12 @@ use crate::{
SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint, UProbe, Xdp,
},
sys::{
bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
bpf_enable_stats, bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
is_btf_datasec_supported, is_btf_decl_tag_supported, is_btf_enum64_supported,
is_btf_float_supported, is_btf_func_global_supported, is_btf_func_supported,
is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported,
is_probe_read_kernel_supported, is_prog_id_supported, is_prog_name_supported,
retry_with_verifier_logs,
retry_with_verifier_logs, SyscallError,
},
util::{bytes_of, bytes_of_slice, page_size, possible_cpus, POSSIBLE_CPUS},
};
@ -1068,6 +1068,29 @@ impl Ebpf {
pub fn programs_mut(&mut self) -> impl Iterator<Item = (&str, &mut Program)> {
self.programs.iter_mut().map(|(s, p)| (s.as_str(), p))
}
/// Enable global statistics tracking for all programs and returns a file descriptor handler.
///
/// Statistics tracking will be disabled once the file descriptor is closed or released.
///
/// Typical usage:
/// 1. Obtain fd from `enable_stats_fd` and bind it to a variable.
/// 2. Record the statistic of interest.
/// 3. Wait for a recorded period of time.
/// 4. Record the statistic of interest again, and calculate the difference.
/// 5. Close/release fd.
///
/// # Examples
///
/// ```no_run
/// # use aya::{Ebpf, EbpfError};
/// # use aya_obj::BpfStatsType;
/// let _fd = Ebpf::enable_stats_fd(BpfStatsType::RunTime)?;
/// # Ok::<(), EbpfError>(())
/// ```
pub fn enable_stats_fd(stats_type: BpfStatsType) -> Result<OwnedFd, SyscallError> {
bpf_enable_stats(stats_type.into())
}
}
/// The error type returned by [`Ebpf::load_file`] and [`Ebpf::load`].
@ -1117,6 +1140,10 @@ pub enum EbpfError {
#[error("program error: {0}")]
/// A program error
ProgramError(#[from] ProgramError),
#[error("syscall error: {0}")]
/// A syscall error
SyscallError(#[from] SyscallError),
}
/// The error type returned by [`Bpf::load_file`] and [`Bpf::load`].

@ -1105,6 +1105,22 @@ impl ProgramInfo {
Ok(ProgramFd(fd))
}
/// The duration the program has been running, in nanoseconds.
///
/// Note this field is only updated for as long as [`BPF_ENABLE_STATS`](crate::Ebpf::enable_stats_fd)
/// is enabled with [`BPF_STATS_RUN_TIME`](aya_obj::BpfStatsType::RunTime) type set.
pub fn run_time_ns(&self) -> u64 {
self.0.run_time_ns
}
/// The number of times the program has ran.
///
/// Note this field is only updated for as long as [`BPF_ENABLE_STATS`](crate::Ebpf::enable_stats_fd)
/// is enabled with [`BPF_STATS_RUN_TIME`](aya_obj::BpfStatsType::RunTime) type set.
pub fn run_cnt(&self) -> u64 {
self.0.run_cnt
}
/// Loads a program from a pinned path in bpffs.
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError> {
use std::os::unix::ffi::OsStrExt as _;

@ -8,6 +8,7 @@ use std::{
};
use assert_matches::assert_matches;
use aya_obj::generated::bpf_stats_type;
use libc::{ENOENT, ENOSPC};
use obj::{
btf::{BtfEnum64, Enum64},
@ -1056,6 +1057,20 @@ pub(crate) fn iter_map_ids() -> impl Iterator<Item = Result<u32, SyscallError>>
iter_obj_ids(bpf_cmd::BPF_MAP_GET_NEXT_ID, "bpf_map_get_next_id")
}
pub(crate) fn bpf_enable_stats(bpf_stats_type: bpf_stats_type) -> Result<OwnedFd, SyscallError> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.enable_stats.type_ = bpf_stats_type as u32;
// SAFETY: BPF_ENABLE_STATS returns a new file descriptor.
unsafe { fd_sys_bpf(bpf_cmd::BPF_ENABLE_STATS, &mut attr) }.map_err(|(code, io_error)| {
assert_eq!(code, -1);
SyscallError {
call: "bpf_enable_stats",
io_error,
}
})
}
pub(crate) fn retry_with_verifier_logs<T>(
max_retries: usize,
f: impl Fn(&mut [u8]) -> SysResult<T>,

@ -98,6 +98,8 @@ fn list_loaded_programs() {
prog.verified_instruction_count();
prog.loaded_at();
prog.fd().unwrap();
prog.run_time_ns();
prog.run_cnt();
}
#[test]

@ -1723,6 +1723,47 @@ impl<T> core::borrow::BorrowMut<T> for aya_obj::generated::bpf_prog_type where T
pub fn aya_obj::generated::bpf_prog_type::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_obj::generated::bpf_prog_type
pub fn aya_obj::generated::bpf_prog_type::from(t: T) -> T
#[repr(u32)] pub enum aya_obj::generated::bpf_stats_type
pub aya_obj::generated::bpf_stats_type::BPF_STATS_RUN_TIME = 0
impl core::clone::Clone for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::clone(&self) -> aya_obj::generated::bpf_stats_type
impl core::cmp::Eq for aya_obj::generated::bpf_stats_type
impl core::cmp::PartialEq for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::eq(&self, other: &aya_obj::generated::bpf_stats_type) -> bool
impl core::convert::From<aya_obj::stats::BpfStatsType> for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::from(value: aya_obj::stats::BpfStatsType) -> Self
impl core::fmt::Debug for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::hash::Hash for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
impl core::marker::Copy for aya_obj::generated::bpf_stats_type
impl core::marker::StructuralPartialEq for aya_obj::generated::bpf_stats_type
impl core::marker::Freeze for aya_obj::generated::bpf_stats_type
impl core::marker::Send for aya_obj::generated::bpf_stats_type
impl core::marker::Sync for aya_obj::generated::bpf_stats_type
impl core::marker::Unpin for aya_obj::generated::bpf_stats_type
impl core::panic::unwind_safe::RefUnwindSafe for aya_obj::generated::bpf_stats_type
impl core::panic::unwind_safe::UnwindSafe for aya_obj::generated::bpf_stats_type
impl<T, U> core::convert::Into<U> for aya_obj::generated::bpf_stats_type where U: core::convert::From<T>
pub fn aya_obj::generated::bpf_stats_type::into(self) -> U
impl<T, U> core::convert::TryFrom<U> for aya_obj::generated::bpf_stats_type where U: core::convert::Into<T>
pub type aya_obj::generated::bpf_stats_type::Error = core::convert::Infallible
pub fn aya_obj::generated::bpf_stats_type::try_from(value: U) -> core::result::Result<T, <T as core::convert::TryFrom<U>>::Error>
impl<T, U> core::convert::TryInto<U> for aya_obj::generated::bpf_stats_type where U: core::convert::TryFrom<T>
pub type aya_obj::generated::bpf_stats_type::Error = <U as core::convert::TryFrom<T>>::Error
pub fn aya_obj::generated::bpf_stats_type::try_into(self) -> core::result::Result<U, <U as core::convert::TryFrom<T>>::Error>
impl<T> alloc::borrow::ToOwned for aya_obj::generated::bpf_stats_type where T: core::clone::Clone
pub type aya_obj::generated::bpf_stats_type::Owned = T
pub fn aya_obj::generated::bpf_stats_type::clone_into(&self, target: &mut T)
pub fn aya_obj::generated::bpf_stats_type::to_owned(&self) -> T
impl<T> core::any::Any for aya_obj::generated::bpf_stats_type where T: 'static + core::marker::Sized
pub fn aya_obj::generated::bpf_stats_type::type_id(&self) -> core::any::TypeId
impl<T> core::borrow::Borrow<T> for aya_obj::generated::bpf_stats_type where T: core::marker::Sized
pub fn aya_obj::generated::bpf_stats_type::borrow(&self) -> &T
impl<T> core::borrow::BorrowMut<T> for aya_obj::generated::bpf_stats_type where T: core::marker::Sized
pub fn aya_obj::generated::bpf_stats_type::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::from(t: T) -> T
#[repr(u32)] pub enum aya_obj::generated::btf_func_linkage
pub aya_obj::generated::btf_func_linkage::BTF_FUNC_EXTERN = 2
pub aya_obj::generated::btf_func_linkage::BTF_FUNC_GLOBAL = 1
@ -7342,6 +7383,77 @@ impl<T> core::borrow::BorrowMut<T> for aya_obj::relocation::EbpfRelocationError
pub fn aya_obj::relocation::EbpfRelocationError::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_obj::relocation::EbpfRelocationError
pub fn aya_obj::relocation::EbpfRelocationError::from(t: T) -> T
pub mod aya_obj::stats
pub enum aya_obj::stats::BpfStatsType
pub aya_obj::stats::BpfStatsType::RunTime
impl core::clone::Clone for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::clone(&self) -> aya_obj::stats::BpfStatsType
impl core::convert::From<aya_obj::stats::BpfStatsType> for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::from(value: aya_obj::stats::BpfStatsType) -> Self
impl core::fmt::Debug for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::marker::Copy for aya_obj::stats::BpfStatsType
impl core::marker::Freeze for aya_obj::stats::BpfStatsType
impl core::marker::Send for aya_obj::stats::BpfStatsType
impl core::marker::Sync for aya_obj::stats::BpfStatsType
impl core::marker::Unpin for aya_obj::stats::BpfStatsType
impl core::panic::unwind_safe::RefUnwindSafe for aya_obj::stats::BpfStatsType
impl core::panic::unwind_safe::UnwindSafe for aya_obj::stats::BpfStatsType
impl<T, U> core::convert::Into<U> for aya_obj::stats::BpfStatsType where U: core::convert::From<T>
pub fn aya_obj::stats::BpfStatsType::into(self) -> U
impl<T, U> core::convert::TryFrom<U> for aya_obj::stats::BpfStatsType where U: core::convert::Into<T>
pub type aya_obj::stats::BpfStatsType::Error = core::convert::Infallible
pub fn aya_obj::stats::BpfStatsType::try_from(value: U) -> core::result::Result<T, <T as core::convert::TryFrom<U>>::Error>
impl<T, U> core::convert::TryInto<U> for aya_obj::stats::BpfStatsType where U: core::convert::TryFrom<T>
pub type aya_obj::stats::BpfStatsType::Error = <U as core::convert::TryFrom<T>>::Error
pub fn aya_obj::stats::BpfStatsType::try_into(self) -> core::result::Result<U, <U as core::convert::TryFrom<T>>::Error>
impl<T> alloc::borrow::ToOwned for aya_obj::stats::BpfStatsType where T: core::clone::Clone
pub type aya_obj::stats::BpfStatsType::Owned = T
pub fn aya_obj::stats::BpfStatsType::clone_into(&self, target: &mut T)
pub fn aya_obj::stats::BpfStatsType::to_owned(&self) -> T
impl<T> core::any::Any for aya_obj::stats::BpfStatsType where T: 'static + core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::type_id(&self) -> core::any::TypeId
impl<T> core::borrow::Borrow<T> for aya_obj::stats::BpfStatsType where T: core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::borrow(&self) -> &T
impl<T> core::borrow::BorrowMut<T> for aya_obj::stats::BpfStatsType where T: core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::from(t: T) -> T
pub enum aya_obj::BpfStatsType
pub aya_obj::BpfStatsType::RunTime
impl core::clone::Clone for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::clone(&self) -> aya_obj::stats::BpfStatsType
impl core::convert::From<aya_obj::stats::BpfStatsType> for aya_obj::generated::bpf_stats_type
pub fn aya_obj::generated::bpf_stats_type::from(value: aya_obj::stats::BpfStatsType) -> Self
impl core::fmt::Debug for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::marker::Copy for aya_obj::stats::BpfStatsType
impl core::marker::Freeze for aya_obj::stats::BpfStatsType
impl core::marker::Send for aya_obj::stats::BpfStatsType
impl core::marker::Sync for aya_obj::stats::BpfStatsType
impl core::marker::Unpin for aya_obj::stats::BpfStatsType
impl core::panic::unwind_safe::RefUnwindSafe for aya_obj::stats::BpfStatsType
impl core::panic::unwind_safe::UnwindSafe for aya_obj::stats::BpfStatsType
impl<T, U> core::convert::Into<U> for aya_obj::stats::BpfStatsType where U: core::convert::From<T>
pub fn aya_obj::stats::BpfStatsType::into(self) -> U
impl<T, U> core::convert::TryFrom<U> for aya_obj::stats::BpfStatsType where U: core::convert::Into<T>
pub type aya_obj::stats::BpfStatsType::Error = core::convert::Infallible
pub fn aya_obj::stats::BpfStatsType::try_from(value: U) -> core::result::Result<T, <T as core::convert::TryFrom<U>>::Error>
impl<T, U> core::convert::TryInto<U> for aya_obj::stats::BpfStatsType where U: core::convert::TryFrom<T>
pub type aya_obj::stats::BpfStatsType::Error = <U as core::convert::TryFrom<T>>::Error
pub fn aya_obj::stats::BpfStatsType::try_into(self) -> core::result::Result<U, <U as core::convert::TryFrom<T>>::Error>
impl<T> alloc::borrow::ToOwned for aya_obj::stats::BpfStatsType where T: core::clone::Clone
pub type aya_obj::stats::BpfStatsType::Owned = T
pub fn aya_obj::stats::BpfStatsType::clone_into(&self, target: &mut T)
pub fn aya_obj::stats::BpfStatsType::to_owned(&self) -> T
impl<T> core::any::Any for aya_obj::stats::BpfStatsType where T: 'static + core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::type_id(&self) -> core::any::TypeId
impl<T> core::borrow::Borrow<T> for aya_obj::stats::BpfStatsType where T: core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::borrow(&self) -> &T
impl<T> core::borrow::BorrowMut<T> for aya_obj::stats::BpfStatsType where T: core::marker::Sized
pub fn aya_obj::stats::BpfStatsType::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_obj::stats::BpfStatsType
pub fn aya_obj::stats::BpfStatsType::from(t: T) -> T
pub enum aya_obj::EbpfSectionKind
pub aya_obj::EbpfSectionKind::Bss
pub aya_obj::EbpfSectionKind::Btf

@ -7884,6 +7884,8 @@ pub fn aya::programs::ProgramInfo::name(&self) -> &[u8]
pub fn aya::programs::ProgramInfo::name_as_str(&self) -> core::option::Option<&str>
pub fn aya::programs::ProgramInfo::program_type(&self) -> u32
pub fn aya::programs::ProgramInfo::program_type_enum(&self) -> aya_obj::generated::linux_bindings_x86_64::bpf_prog_type
pub fn aya::programs::ProgramInfo::run_cnt(&self) -> u64
pub fn aya::programs::ProgramInfo::run_time_ns(&self) -> u64
pub fn aya::programs::ProgramInfo::size_jitted(&self) -> u32
pub fn aya::programs::ProgramInfo::size_translated(&self) -> u32
pub fn aya::programs::ProgramInfo::tag(&self) -> u64
@ -8690,6 +8692,7 @@ pub aya::EbpfError::NoBTF
pub aya::EbpfError::ParseError(aya_obj::obj::ParseError)
pub aya::EbpfError::ProgramError(aya::programs::ProgramError)
pub aya::EbpfError::RelocationError(aya_obj::relocation::EbpfRelocationError)
pub aya::EbpfError::SyscallError(crate::sys::SyscallError)
pub aya::EbpfError::UnexpectedPinningType
pub aya::EbpfError::UnexpectedPinningType::name: u32
impl core::convert::From<aya::maps::MapError> for aya::EbpfError
@ -8736,6 +8739,7 @@ impl<T> core::convert::From<T> for aya::EbpfError
pub fn aya::EbpfError::from(t: T) -> T
pub struct aya::Ebpf
impl aya::Ebpf
pub fn aya::Ebpf::enable_stats_fd(stats_type: aya_obj::stats::BpfStatsType) -> core::result::Result<std::os::fd::owned::OwnedFd, SyscallError>
pub fn aya::Ebpf::load(data: &[u8]) -> core::result::Result<Self, aya::EbpfError>
pub fn aya::Ebpf::load_file<P: core::convert::AsRef<std::path::Path>>(path: P) -> core::result::Result<Self, aya::EbpfError>
pub fn aya::Ebpf::map(&self, name: &str) -> core::option::Option<&aya::maps::Map>

@ -79,6 +79,7 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
"bpf_lpm_trie_key",
"bpf_cpumap_val",
"bpf_devmap_val",
"bpf_stats_type",
// BTF
"btf_header",
"btf_ext_info",

Loading…
Cancel
Save