Skip to content

Commit 5074b6c

Browse files
committed
Introduce Program::attach_iter_with_opts()
Add the Program::attach_iter_with_opts() method to allow for iterator attachment with support for providing additional options. Signed-off-by: Daniel Müller <[email protected]>
1 parent 4bdf81b commit 5074b6c

File tree

3 files changed

+125
-3
lines changed

3 files changed

+125
-3
lines changed

libbpf-rs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Unreleased
2+
----------
3+
- Added `Program::attach_iter_with_opts` for attaching to iterators with
4+
additional options
5+
6+
17
0.26.0-beta.0
28
-------------
39
- Added `target_obj_id` and `target_btf_id` fields to `TracingLinkInfo`

libbpf-rs/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,13 @@ pub use crate::print::get_print;
130130
pub use crate::print::set_print;
131131
pub use crate::print::PrintCallback;
132132
pub use crate::print::PrintLevel;
133+
pub use crate::program::CgroupIterOpts;
134+
pub use crate::program::CgroupIterOrder;
133135
pub use crate::program::Input as ProgramInput;
136+
pub use crate::program::IterOpts;
134137
pub use crate::program::KprobeMultiOpts;
135138
pub use crate::program::KprobeOpts;
139+
pub use crate::program::MapIterOpts;
136140
pub use crate::program::OpenProgram;
137141
pub use crate::program::OpenProgramImpl;
138142
pub use crate::program::OpenProgramMut;

libbpf-rs/src/program.rs

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,106 @@ impl From<PerfEventOpts> for libbpf_sys::bpf_perf_event_opts {
159159
}
160160
}
161161

162+
163+
/// Options used when iterating over a map.
164+
#[derive(Clone, Debug)]
165+
pub struct MapIterOpts<'fd> {
166+
/// The file descriptor of the map.
167+
pub fd: BorrowedFd<'fd>,
168+
#[doc(hidden)]
169+
pub _non_exhaustive: (),
170+
}
171+
172+
impl<'fd> MapIterOpts<'fd> {
173+
/// Create a `MapIterOpts` using the given file descriptor.
174+
pub fn from_fd(fd: BorrowedFd<'fd>) -> Self {
175+
Self {
176+
fd,
177+
_non_exhaustive: (),
178+
}
179+
}
180+
}
181+
182+
183+
/// Iteration order for cgroups.
184+
#[non_exhaustive]
185+
#[repr(u32)]
186+
#[derive(Clone, Debug, Default)]
187+
pub enum CgroupIterOrder {
188+
/// Use the default iteration order.
189+
#[default]
190+
Default = libbpf_sys::BPF_CGROUP_ITER_ORDER_UNSPEC,
191+
/// Process only a single object.
192+
SelfOnly = libbpf_sys::BPF_CGROUP_ITER_SELF_ONLY,
193+
/// Walk descendants in pre-order.
194+
DescendantsPre = libbpf_sys::BPF_CGROUP_ITER_DESCENDANTS_PRE,
195+
/// Walk descendants in post-order.
196+
DescendantsPost = libbpf_sys::BPF_CGROUP_ITER_DESCENDANTS_POST,
197+
/// Walk ancestors upward.
198+
AncestorsUp = libbpf_sys::BPF_CGROUP_ITER_ANCESTORS_UP,
199+
}
200+
201+
/// Options used when iterating over a cgroup.
202+
#[derive(Clone, Debug)]
203+
pub struct CgroupIterOpts<'fd> {
204+
/// The file descriptor of the cgroup.
205+
pub fd: BorrowedFd<'fd>,
206+
/// The iteration to use on the cgroup.
207+
pub order: CgroupIterOrder,
208+
#[doc(hidden)]
209+
pub _non_exhaustive: (),
210+
}
211+
212+
impl<'fd> CgroupIterOpts<'fd> {
213+
/// Create a `CgroupIterOpts` using the given file descriptor.
214+
pub fn from_fd(fd: BorrowedFd<'fd>) -> Self {
215+
Self {
216+
fd,
217+
order: CgroupIterOrder::default(),
218+
_non_exhaustive: (),
219+
}
220+
}
221+
}
222+
223+
224+
/// Options to optionally be provided when attaching to an iterator.
225+
#[non_exhaustive]
226+
#[derive(Clone, Debug)]
227+
pub enum IterOpts<'fd> {
228+
/// Iterate over a map.
229+
Map(MapIterOpts<'fd>),
230+
/// Iterate over a group.
231+
Cgroup(CgroupIterOpts<'fd>),
232+
}
233+
234+
impl From<IterOpts<'_>> for libbpf_sys::bpf_iter_link_info {
235+
fn from(opts: IterOpts) -> Self {
236+
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
237+
match opts {
238+
IterOpts::Map(map_opts) => {
239+
let MapIterOpts {
240+
fd,
241+
_non_exhaustive: (),
242+
} = map_opts;
243+
244+
linkinfo.map.map_fd = fd.as_raw_fd() as _;
245+
}
246+
IterOpts::Cgroup(cgroup_opts) => {
247+
let CgroupIterOpts {
248+
fd,
249+
order,
250+
_non_exhaustive: (),
251+
} = cgroup_opts;
252+
253+
linkinfo.cgroup.cgroup_fd = fd.as_raw_fd() as _;
254+
linkinfo.cgroup.order = order as libbpf_sys::bpf_cgroup_iter_order;
255+
}
256+
};
257+
linkinfo
258+
}
259+
}
260+
261+
162262
/// An immutable parsed but not yet loaded BPF program.
163263
pub type OpenProgram<'obj> = OpenProgramImpl<'obj>;
164264
/// A mutable parsed but not yet loaded BPF program.
@@ -1323,10 +1423,22 @@ impl<'obj> ProgramMut<'obj> {
13231423
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html).
13241424
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
13251425
pub fn attach_iter(&self, map_fd: BorrowedFd<'_>) -> Result<Link> {
1326-
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
1327-
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
1426+
let map_opts = MapIterOpts {
1427+
fd: map_fd,
1428+
_non_exhaustive: (),
1429+
};
1430+
self.attach_iter_with_opts(IterOpts::Map(map_opts))
1431+
}
1432+
1433+
/// Attach this program to a
1434+
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html),
1435+
/// providing additional options.
1436+
///
1437+
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
1438+
pub fn attach_iter_with_opts(&self, opts: IterOpts<'_>) -> Result<Link> {
1439+
let mut linkinfo = libbpf_sys::bpf_iter_link_info::from(opts);
13281440
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
1329-
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
1441+
link_info: &raw mut linkinfo,
13301442
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
13311443
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
13321444
..Default::default()

0 commit comments

Comments
 (0)