Skip to content

Commit 94416da

Browse files
committed
libbpf-rs: BTF custom path setter in ObjectBuilder
Allowing to set a custom BTF path in the object builder will reduce the boilerplate that's currently necessary. Making libbpf use a separate BTF file is incredibly important in systems that don't ship it. Looking at you, Nvidia and Raspberry Pi official kernels. It also can be very helpeful when testing for ABI changes in kernels and it would have been quite helpful for a few investigations where kernel changes have prevented the loading of BPF programs as they either exposed bugs in our code. Several users libbpf-rs that set this option currently leak memory (which might not be a big deal since it's a one-off and not a lot of memory) but having this as a blessed, leak-free way to do it would be great. Test Plan ========= Updated javierhonduco/lightswitch#367 with these changes and verified test pass (which exercise setting an alternative BTF path) and that ASAN reports no issues. Signed-off-by: Francisco Javier Honduvilla Coto <javierhonduco@gmail.com>
1 parent 0f877dd commit 94416da

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

libbpf-rs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Unreleased
1414
- Added `OpenProgram::autoload` getter
1515
- Added `MapCore::lookup_into` for looking up values into preallocated
1616
buffers
17+
- Added `ObjectBuilder::btf_custom_path` setter
1718

1819

1920
0.26.0-beta.0

libbpf-rs/src/object.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub trait AsRawLibbpf {
106106
pub struct ObjectBuilder {
107107
name: Option<CString>,
108108
pin_root_path: Option<CString>,
109+
btf_custom_path: Option<CString>,
109110

110111
opts: OnceCell<libbpf_sys::bpf_object_open_opts>,
111112
}
@@ -115,6 +116,7 @@ impl Default for ObjectBuilder {
115116
Self {
116117
name: None,
117118
pin_root_path: None,
119+
btf_custom_path: None,
118120
opts: OnceCell::new(),
119121
}
120122
}
@@ -125,6 +127,7 @@ impl PartialEq for ObjectBuilder {
125127
let Self {
126128
name,
127129
pin_root_path,
130+
btf_custom_path,
128131
opts,
129132
} = self;
130133

@@ -135,6 +138,7 @@ impl PartialEq for ObjectBuilder {
135138
&& other.opts.get().is_none()
136139
&& name == &other.name
137140
&& pin_root_path == &other.pin_root_path
141+
&& btf_custom_path == &other.btf_custom_path
138142
}
139143
}
140144

@@ -171,6 +175,19 @@ impl ObjectBuilder {
171175
Ok(self)
172176
}
173177

178+
/// Set the `btf_custom_path`.
179+
///
180+
/// By default, this is NULL and libbpf will probe to find the BTF file in a set
181+
/// of well-known paths.
182+
pub fn btf_custom_path<T: AsRef<Path>>(&mut self, path: T) -> Result<&mut Self> {
183+
self.btf_custom_path = Some(util::path_to_cstring(path)?);
184+
self.opts_mut().btf_custom_path = self
185+
.btf_custom_path
186+
.as_ref()
187+
.map_or(ptr::null(), |p| p.as_ptr());
188+
Ok(self)
189+
}
190+
174191
/// Option to parse map definitions non-strictly, allowing extra attributes/data
175192
pub fn relaxed_maps(&mut self, relaxed_maps: bool) -> &mut Self {
176193
self.opts_mut().relaxed_maps = relaxed_maps;

0 commit comments

Comments
 (0)