Skip to content

Commit 855eb84

Browse files
d-e-s-odanielocfb
authored andcommitted
Generate mutable and shared datasec accessors
It's not particularly user friendly to only have exclusive shared datasec accessor variants available, as that can lead to unnecessary borrow conflicts. With this change we revamp the generation logic to generate shared and exclusive accessors when possible. Refs: #606 Signed-off-by: Daniel Müller <[email protected]>
1 parent f22d568 commit 855eb84

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

examples/capable/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ fn main() -> Result<()> {
186186

187187
let mut open_skel = skel_builder.open()?;
188188
//Pass configuration to BPF
189-
open_skel.rodata().tool_config.tgid = opts.pid; //tgid in kernel is pid in userland
190-
open_skel.rodata().tool_config.verbose = opts.verbose;
191-
open_skel.rodata().tool_config.unique_type = opts.unique_type;
189+
open_skel.rodata_mut().tool_config.tgid = opts.pid; //tgid in kernel is pid in userland
190+
open_skel.rodata_mut().tool_config.verbose = opts.verbose;
191+
open_skel.rodata_mut().tool_config.unique_type = opts.unique_type;
192192

193193
let mut skel = open_skel.load()?;
194194
skel.attach()?;

examples/runqslower/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ fn main() -> Result<()> {
9090
let mut open_skel = skel_builder.open()?;
9191

9292
// Write arguments into prog
93-
open_skel.rodata().min_us = opts.latency;
94-
open_skel.rodata().targ_pid = opts.pid;
95-
open_skel.rodata().targ_tgid = opts.tid;
93+
open_skel.rodata_mut().min_us = opts.latency;
94+
open_skel.rodata_mut().targ_pid = opts.pid;
95+
open_skel.rodata_mut().targ_tgid = opts.tid;
9696

9797
// Begin tracing
9898
let mut skel = open_skel.load()?;

examples/tproxy/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ fn main() -> Result<()> {
6464

6565
// Set constants
6666
let mut open_skel = skel_builder.open()?;
67-
open_skel.rodata().target_port = opts.port.to_be();
68-
open_skel.rodata().proxy_addr = proxy_addr.to_be();
69-
open_skel.rodata().proxy_port = opts.proxy_port.to_be();
67+
open_skel.rodata_mut().target_port = opts.port.to_be();
68+
open_skel.rodata_mut().proxy_addr = proxy_addr.to_be();
69+
open_skel.rodata_mut().proxy_port = opts.proxy_port.to_be();
7070

7171
// Load into kernel
7272
let skel = open_skel.load()?;

libbpf-cargo/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Unreleased
22
----------
3+
- Adjusted skeleton creation logic to generate shared and exclusive datasec
4+
accessor functions
35
- Removed `Error` enum in favor of `anyhow::Error`
46
- Bumped minimum Rust version to `1.65`
57

libbpf-cargo/src/gen/mod.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,24 +530,33 @@ fn gen_skel_datasec_getters(
530530
None => continue,
531531
};
532532
let struct_name = format!("{obj_name}_{name}_types::{name}");
533-
let mutability = if loaded && map_is_readonly(map) {
534-
""
533+
let immutable = loaded && map_is_readonly(map);
534+
let mutabilities = if immutable {
535+
[false].as_ref()
535536
} else {
536-
"mut"
537+
[false, true].as_ref()
537538
};
538539

539-
write!(
540-
skel,
541-
r#"
542-
pub fn {name}(&{mutability} self) -> &{mutability} {struct_name} {{
543-
unsafe {{
544-
std::mem::transmute::<*mut std::ffi::c_void, &{mutability} {struct_name}>(
545-
self.skel_config.map_mmap_ptr({idx}).unwrap()
546-
)
540+
for mutable in mutabilities {
541+
let (ref_suffix, ptr_suffix, fn_suffix) = if *mutable {
542+
("mut", "mut", "_mut")
543+
} else {
544+
("", "const", "")
545+
};
546+
547+
write!(
548+
skel,
549+
r#"
550+
pub fn {name}{fn_suffix}(&{ref_suffix} self) -> &{ref_suffix} {struct_name} {{
551+
unsafe {{
552+
std::mem::transmute::<*{ptr_suffix} std::ffi::c_void, &{ref_suffix} {struct_name}>(
553+
self.skel_config.map_mmap_ptr{fn_suffix}({idx}).unwrap()
554+
)
555+
}}
547556
}}
548-
}}
549-
"#
550-
)?;
557+
"#
558+
)?;
559+
}
551560
}
552561

553562
Ok(())

libbpf-cargo/src/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,17 +653,17 @@ fn test_skeleton_datasec() {
653653
.expect("failed to open skel");
654654
655655
// Check that we set rodata vars before load
656-
open_skel.rodata().myconst = std::ptr::null_mut();
656+
open_skel.rodata_mut().myconst = std::ptr::null_mut();
657657
658658
// We can always set bss vars
659-
open_skel.bss().myglobal = 42;
659+
open_skel.bss_mut().myglobal = 42;
660660
661661
let mut skel = open_skel
662662
.load()
663663
.expect("failed to load skel");
664664
665665
// We can always set bss vars
666-
skel.bss().myglobal = 24;
666+
skel.bss_mut().myglobal = 24;
667667
668668
// Read only for rodata after load
669669
let _rodata: &prog_rodata_types::rodata = skel.rodata();
@@ -924,7 +924,7 @@ fn test_skeleton_builder_arrays_ptrs() {
924924
925925
fn main() {{
926926
let builder = ProgSkelBuilder::default();
927-
let mut open_skel = builder
927+
let open_skel = builder
928928
.open()
929929
.expect("failed to open skel");
930930

libbpf-rs/src/skeleton.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use libbpf_sys::bpf_object_skeleton;
1818
use libbpf_sys::bpf_prog_skeleton;
1919
use libbpf_sys::bpf_program;
2020

21+
use crate::error::IntoError as _;
2122
use crate::libbpf_sys;
2223
use crate::util;
2324
use crate::Error;
@@ -263,17 +264,28 @@ impl ObjectSkeletonConfig<'_> {
263264
/// `ObjectSkeletonConfigBuilder::map`. Index starts at 0.
264265
///
265266
/// Warning: the returned pointer is only valid while the `ObjectSkeletonConfig` is alive.
266-
pub fn map_mmap_ptr(&mut self, index: usize) -> Result<*mut c_void> {
267+
pub fn map_mmap_ptr(&self, index: usize) -> Result<*const c_void> {
267268
if index >= self.maps.len() {
268269
return Err(Error::with_invalid_data(format!(
269270
"Invalid map index: {index}"
270271
)));
271272
}
272273

273-
self.maps[index].mmaped.as_ref().map_or_else(
274-
|| Err(Error::with_invalid_data("Map does not have mmaped ptr")),
275-
|p| Ok(**p),
276-
)
274+
let p = self.maps[index]
275+
.mmaped
276+
.as_ref()
277+
.ok_or_invalid_data(|| "Map does not have mmaped ptr")?;
278+
Ok(**p)
279+
}
280+
281+
/// Returns the `mmaped` pointer for a map at the specified `index`.
282+
///
283+
/// The index is determined by the order in which the map was passed to
284+
/// `ObjectSkeletonConfigBuilder::map`. Index starts at 0.
285+
///
286+
/// Warning: the returned pointer is only valid while the `ObjectSkeletonConfig` is alive.
287+
pub fn map_mmap_ptr_mut(&mut self, index: usize) -> Result<*mut c_void> {
288+
self.map_mmap_ptr(index).map(|p| p.cast_mut())
277289
}
278290

279291
/// Returns the link pointer for a prog at the specified `index`.

0 commit comments

Comments
 (0)