Skip to content

Commit

Permalink
Merge pull request #371 from conectado/verifier-log-level
Browse files Browse the repository at this point in the history
aya: expose BPF verifier log level configuration
  • Loading branch information
alessandrod authored Aug 29, 2022
2 parents 83ec27f + a602525 commit b95adc3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 31 deletions.
97 changes: 68 additions & 29 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ pub struct BpfLoader<'a> {
globals: HashMap<&'a str, &'a [u8]>,
features: Features,
extensions: HashSet<&'a str>,
verifier_log_level: VerifierLogLevel,
}

/// Used to set the verifier log level in [BpfLoader](BpfLoader::verifier_log_level()).
#[repr(u32)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum VerifierLogLevel {
/// Disable all logging.
Disable = 0,

/// Default level of logging, shows verifier stats.
Default = 4,

/// Prints verbose logs showing tracing.
Verbose = 1,

/// Prints full debug details.
Debug = 7,
}

impl<'a> BpfLoader<'a> {
Expand All @@ -206,6 +225,7 @@ impl<'a> BpfLoader<'a> {
globals: HashMap::new(),
features,
extensions: HashSet::new(),
verifier_log_level: VerifierLogLevel::Default,
}
}

Expand Down Expand Up @@ -313,6 +333,24 @@ impl<'a> BpfLoader<'a> {
self
}

/// Sets BPF verifier log level.
///
/// # Example
///
/// ```no_run
/// use aya::{BpfLoader, VerifierLogLevel};
///
/// let bpf = BpfLoader::new()
/// .verifier_log_level(VerifierLogLevel::Verbose)
/// .load_file("file.o")?;
/// # Ok::<(), aya::BpfError>(())
/// ```
///
pub fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut BpfLoader<'a> {
self.verifier_log_level = level;
self
}

/// Loads eBPF bytecode from a file.
///
/// # Examples
Expand Down Expand Up @@ -344,6 +382,7 @@ impl<'a> BpfLoader<'a> {
/// # Ok::<(), aya::BpfError>(())
/// ```
pub fn load(&mut self, data: &[u8]) -> Result<Bpf, BpfError> {
let verifier_log_level = self.verifier_log_level as u32;
let mut obj = Object::parse(data)?;
obj.patch_map_data(self.globals.clone())?;

Expand Down Expand Up @@ -443,123 +482,123 @@ impl<'a> BpfLoader<'a> {

let program = if self.extensions.contains(name.as_str()) {
Program::Extension(Extension {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
})
} else {
match &section {
ProgramSection::KProbe { .. } => Program::KProbe(KProbe {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: ProbeKind::KProbe,
}),
ProgramSection::KRetProbe { .. } => Program::KProbe(KProbe {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: ProbeKind::KRetProbe,
}),
ProgramSection::UProbe { .. } => Program::UProbe(UProbe {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: ProbeKind::UProbe,
}),
ProgramSection::URetProbe { .. } => Program::UProbe(UProbe {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: ProbeKind::URetProbe,
}),
ProgramSection::TracePoint { .. } => Program::TracePoint(TracePoint {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::SocketFilter { .. } => {
Program::SocketFilter(SocketFilter {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
})
}
ProgramSection::Xdp { .. } => Program::Xdp(Xdp {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::SkMsg { .. } => Program::SkMsg(SkMsg {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::CgroupSysctl { .. } => {
Program::CgroupSysctl(CgroupSysctl {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
})
}
ProgramSection::CgroupSockopt { attach_type, .. } => {
Program::CgroupSockopt(CgroupSockopt {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
attach_type: *attach_type,
})
}
ProgramSection::SkSkbStreamParser { .. } => Program::SkSkb(SkSkb {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: SkSkbKind::StreamParser,
}),
ProgramSection::SkSkbStreamVerdict { .. } => Program::SkSkb(SkSkb {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
kind: SkSkbKind::StreamVerdict,
}),
ProgramSection::SockOps { .. } => Program::SockOps(SockOps {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::SchedClassifier { .. } => {
Program::SchedClassifier(SchedClassifier {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
name: unsafe {
CString::from_vec_unchecked(Vec::from(name.clone()))
.into_boxed_c_str()
},
})
}
ProgramSection::CgroupSkb { .. } => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
expected_attach_type: None,
}),
ProgramSection::CgroupSkbIngress { .. } => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
expected_attach_type: Some(CgroupSkbAttachType::Ingress),
}),
ProgramSection::CgroupSkbEgress { .. } => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
expected_attach_type: Some(CgroupSkbAttachType::Egress),
}),
ProgramSection::CgroupSockAddr { attach_type, .. } => {
Program::CgroupSockAddr(CgroupSockAddr {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
attach_type: *attach_type,
})
}
ProgramSection::LircMode2 { .. } => Program::LircMode2(LircMode2 {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::PerfEvent { .. } => Program::PerfEvent(PerfEvent {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::RawTracePoint { .. } => {
Program::RawTracePoint(RawTracePoint {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
})
}
ProgramSection::Lsm { .. } => Program::Lsm(Lsm {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::BtfTracePoint { .. } => {
Program::BtfTracePoint(BtfTracePoint {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
})
}
ProgramSection::FEntry { .. } => Program::FEntry(FEntry {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::FExit { .. } => Program::FExit(FExit {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::Extension { .. } => Program::Extension(Extension {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::SkLookup { .. } => Program::SkLookup(SkLookup {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
}),
ProgramSection::CgroupSock { attach_type, .. } => {
Program::CgroupSock(CgroupSock {
data: ProgramData::new(prog_name, obj, btf_fd),
data: ProgramData::new(prog_name, obj, btf_fd, verifier_log_level),
attach_type: *attach_type,
})
}
Expand Down
9 changes: 8 additions & 1 deletion aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,15 @@ pub(crate) struct ProgramData<T: Link> {
pub(crate) attach_btf_id: Option<u32>,
pub(crate) attach_prog_fd: Option<RawFd>,
pub(crate) btf_fd: Option<RawFd>,
pub(crate) verifier_log_level: u32,
}

impl<T: Link> ProgramData<T> {
pub(crate) fn new(
name: Option<String>,
obj: obj::Program,
btf_fd: Option<RawFd>,
verifier_log_level: u32,
) -> ProgramData<T> {
ProgramData {
name,
Expand All @@ -424,6 +426,7 @@ impl<T: Link> ProgramData<T> {
attach_btf_id: None,
attach_prog_fd: None,
btf_fd,
verifier_log_level,
}
}
}
Expand Down Expand Up @@ -531,7 +534,11 @@ fn load_program<T: Link>(
line_info_rec_size: *line_info_rec_size,
line_info: line_info.clone(),
};
let ret = retry_with_verifier_logs(10, &mut logger, |logger| bpf_load_program(&attr, logger));

let verifier_log_level = data.verifier_log_level;
let ret = retry_with_verifier_logs(10, &mut logger, |logger| {
bpf_load_program(&attr, logger, verifier_log_level)
});

match ret {
Ok(prog_fd) => {
Expand Down
3 changes: 2 additions & 1 deletion aya/src/sys/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub(crate) struct BpfLoadProgramAttrs<'a> {
pub(crate) fn bpf_load_program(
aya_attr: &BpfLoadProgramAttrs,
logger: &mut VerifierLog,
verifier_log_level: u32,
) -> SysResult {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };

Expand Down Expand Up @@ -139,7 +140,7 @@ pub(crate) fn bpf_load_program(
}
let log_buf = logger.buf();
if log_buf.capacity() > 0 {
u.log_level = 7;
u.log_level = verifier_log_level;
u.log_buf = log_buf.as_mut_ptr() as u64;
u.log_size = log_buf.capacity() as u32;
}
Expand Down

0 comments on commit b95adc3

Please sign in to comment.