Skip to content

Commit 0edc1b5

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 0edc1b5

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ 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::CgroupIterOrder;
133134
pub use crate::program::Input as ProgramInput;
135+
pub use crate::program::IterOpts;
134136
pub use crate::program::KprobeMultiOpts;
135137
pub use crate::program::KprobeOpts;
136138
pub use crate::program::OpenProgram;

libbpf-rs/src/program.rs

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

162+
163+
/// Iteration order for cgroups.
164+
#[non_exhaustive]
165+
#[repr(u32)]
166+
#[derive(Clone, Debug, Default)]
167+
pub enum CgroupIterOrder {
168+
/// Use the default iteration order.
169+
#[default]
170+
Default = libbpf_sys::BPF_CGROUP_ITER_ORDER_UNSPEC,
171+
/// Process only a single object.
172+
SelfOnly = libbpf_sys::BPF_CGROUP_ITER_SELF_ONLY,
173+
/// Walk descendants in pre-order.
174+
DescendantsPre = libbpf_sys::BPF_CGROUP_ITER_DESCENDANTS_PRE,
175+
/// Walk descendants in post-order.
176+
DescendantsPost = libbpf_sys::BPF_CGROUP_ITER_DESCENDANTS_POST,
177+
/// Walk ancestors upward.
178+
AncestorsUp = libbpf_sys::BPF_CGROUP_ITER_ANCESTORS_UP,
179+
}
180+
181+
182+
/// Options to optionally be provided when attaching to an iterator.
183+
#[derive(Clone, Debug, Default)]
184+
pub struct IterOpts<'fd> {
185+
/// The file descriptor of the cgroup.
186+
pub cgroup_fd: Option<BorrowedFd<'fd>>,
187+
/// The iteration to use on the cgroup.
188+
pub cgroup_order: CgroupIterOrder,
189+
#[doc(hidden)]
190+
pub _non_exhaustive: (),
191+
}
192+
193+
162194
/// An immutable parsed but not yet loaded BPF program.
163195
pub type OpenProgram<'obj> = OpenProgramImpl<'obj>;
164196
/// A mutable parsed but not yet loaded BPF program.
@@ -1319,14 +1351,9 @@ impl<'obj> ProgramMut<'obj> {
13191351
)
13201352
}
13211353

1322-
/// Attach this program to a
1323-
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html).
1324-
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
1325-
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 _;
1354+
fn attach_iter_impl(&self, linkinfo: &mut libbpf_sys::bpf_iter_link_info) -> Result<Link> {
13281355
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
1329-
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
1356+
link_info: linkinfo as *mut libbpf_sys::bpf_iter_link_info,
13301357
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
13311358
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
13321359
..Default::default()
@@ -1344,6 +1371,43 @@ impl<'obj> ProgramMut<'obj> {
13441371
Ok(link)
13451372
}
13461373

1374+
1375+
/// Attach this program to a
1376+
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html).
1377+
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
1378+
pub fn attach_iter(&self, map_fd: BorrowedFd<'_>) -> Result<Link> {
1379+
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
1380+
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
1381+
self.attach_iter_impl(&mut linkinfo)
1382+
}
1383+
1384+
/// Attach this program to a
1385+
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html),
1386+
/// providing additional options.
1387+
///
1388+
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
1389+
pub fn attach_iter_with_opts(
1390+
&self,
1391+
map_fd: BorrowedFd<'_>,
1392+
opts: IterOpts<'_>,
1393+
) -> Result<Link> {
1394+
let IterOpts {
1395+
cgroup_fd,
1396+
cgroup_order,
1397+
_non_exhaustive: (),
1398+
} = opts;
1399+
1400+
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
1401+
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
1402+
linkinfo.cgroup.order = cgroup_order as libbpf_sys::bpf_cgroup_iter_order;
1403+
1404+
if let Some(cgroup_fd) = cgroup_fd {
1405+
linkinfo.cgroup.cgroup_fd = cgroup_fd.as_raw_fd() as _;
1406+
}
1407+
1408+
self.attach_iter_impl(&mut linkinfo)
1409+
}
1410+
13471411
/// Test run the program with the given input data.
13481412
///
13491413
/// This function uses the

0 commit comments

Comments
 (0)