Skip to content

Commit 6ea7be7

Browse files
committed
libbpf-cargo: Make all XXX_data skeleton members optional
As discussed in libbpf#1151, libbpf may not provide direct memory map access to any of the BPF map members if the kernel does not support memory-mapped global variable maps. To that end, make them all optional. Closes: libbpf#1151 Signed-off-by: Daniel Müller <[email protected]>
1 parent bb3e8bd commit 6ea7be7

File tree

9 files changed

+80
-51
lines changed

9 files changed

+80
-51
lines changed

examples/capable/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,16 @@ fn main() -> Result<()> {
174174
}
175175

176176
let mut open_object = MaybeUninit::uninit();
177-
let open_skel = skel_builder.open(&mut open_object)?;
178-
//Pass configuration to BPF
179-
open_skel.maps.rodata_data.tool_config.tgid = opts.pid; //tgid in kernel is pid in userland
180-
open_skel
177+
let mut open_skel = skel_builder.open(&mut open_object)?;
178+
let rodata = open_skel
181179
.maps
182180
.rodata_data
183-
.tool_config
184-
.verbose
185-
.write(opts.verbose);
186-
open_skel.maps.rodata_data.tool_config.unique_type = opts.unique_type;
181+
.as_deref_mut()
182+
.expect("`rodata` is not memory mapped");
183+
//Pass configuration to BPF
184+
rodata.tool_config.tgid = opts.pid; //tgid in kernel is pid in userland
185+
rodata.tool_config.verbose.write(opts.verbose);
186+
rodata.tool_config.unique_type = opts.unique_type;
187187

188188
let mut skel = open_skel.load()?;
189189
skel.attach()?;

examples/ringbuf_multi/src/main.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ fn main() -> Result<()> {
4545
let mut open_object = MaybeUninit::uninit();
4646
let open_skel = skel_builder.open(&mut open_object)?;
4747
let mut skel = open_skel.load()?;
48+
let bss = skel
49+
.maps
50+
.bss_data
51+
.as_deref_mut()
52+
.expect("`bss` is not memory mapped");
4853

4954
// Only trigger BPF program for current process.
5055
let pid = unsafe { libc::getpid() };
51-
skel.maps.bss_data.pid = pid;
56+
bss.pid = pid;
5257

5358
let mut builder = libbpf_rs::RingBufferBuilder::new();
5459
builder
@@ -60,19 +65,24 @@ fn main() -> Result<()> {
6065
let ringbuf = builder.build().unwrap();
6166

6267
let () = skel.attach()?;
68+
let bss = skel
69+
.maps
70+
.bss_data
71+
.as_deref_mut()
72+
.expect("`bss` is not memory mapped");
6373

6474
// trigger few samples, some will be skipped
65-
skel.maps.bss_data.target_ring = 0;
66-
skel.maps.bss_data.value = 333;
75+
bss.target_ring = 0;
76+
bss.value = 333;
6777
let _pgid = unsafe { libc::getpgid(pid) };
6878

6979
// skipped, no ringbuf in slot 1
70-
skel.maps.bss_data.target_ring = 1;
71-
skel.maps.bss_data.value = 555;
80+
bss.target_ring = 1;
81+
bss.value = 555;
7282
let _pgid = unsafe { libc::getpgid(pid) };
7383

74-
skel.maps.bss_data.target_ring = 2;
75-
skel.maps.bss_data.value = 777;
84+
bss.target_ring = 2;
85+
bss.value = 777;
7686
let _pgid = unsafe { libc::getpgid(pid) };
7787

7888
// poll for samples, should get 2 ringbufs back

examples/runqslower/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ fn main() -> Result<()> {
7878
}
7979

8080
let mut open_object = MaybeUninit::uninit();
81-
let open_skel = skel_builder.open(&mut open_object)?;
81+
let mut open_skel = skel_builder.open(&mut open_object)?;
82+
let rodata = open_skel
83+
.maps
84+
.rodata_data
85+
.as_deref_mut()
86+
.expect("`rodata` is not memory mapped");
8287

8388
// Write arguments into prog
84-
open_skel.maps.rodata_data.min_us = opts.latency;
85-
open_skel.maps.rodata_data.targ_pid = opts.pid;
86-
open_skel.maps.rodata_data.targ_tgid = opts.tid;
89+
rodata.min_us = opts.latency;
90+
rodata.targ_pid = opts.pid;
91+
rodata.targ_tgid = opts.tid;
8792

8893
// Begin tracing
8994
let mut skel = open_skel.load()?;

examples/tcp_ca/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,24 @@ fn test(name_to_register: Option<&OsStr>, name_to_use: &OsStr, verbose: bool) ->
163163
// NB: At this point `/proc/sys/net/ipv4/tcp_available_congestion_control`
164164
// would list the registered congestion algorithm.
165165

166-
assert_eq!(skel.maps.bss_data.ca_cnt, 0);
167-
assert!(!skel.maps.bss_data.cong_control);
166+
let bss = skel
167+
.maps
168+
.bss_data
169+
.as_deref_mut()
170+
.expect("`bss` is not memory mapped");
171+
assert_eq!(bss.ca_cnt, 0);
172+
assert!(!bss.cong_control);
168173

169174
// Use our registered TCP congestion algorithm while sending a bunch of data
170175
// over the loopback device.
171176
let () = send_recv(name_to_use)?;
172177
println!("Done.");
173178

174-
let saved_ca_cnt = skel.maps.bss_data.ca_cnt;
179+
let saved_ca_cnt = bss.ca_cnt;
175180
assert_ne!(saved_ca_cnt, 0);
176181
// With `ca_update_cong_control2` active, we should have seen the
177182
// `cong_control` value changed as well.
178-
assert!(skel.maps.bss_data.cong_control);
183+
assert!(bss.cong_control);
179184
Ok(())
180185
}
181186

examples/tcp_option/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ fn main() -> Result<()> {
8686
builder.obj_builder.debug(true);
8787
}
8888
let mut open_object = MaybeUninit::uninit();
89-
let open = builder.open(&mut open_object)?;
89+
let mut open_skel = builder.open(&mut open_object)?;
90+
let rodata = open_skel
91+
.maps
92+
.rodata_data
93+
.as_deref_mut()
94+
.expect("`rodata` is not memory mapped");
9095

91-
open.maps.rodata_data.targ_ip = u32::from_be_bytes(ip.octets()).to_be();
92-
open.maps.rodata_data.data_such_as_trace_id = opts.trace_id;
96+
rodata.targ_ip = u32::from_be_bytes(ip.octets()).to_be();
97+
rodata.data_such_as_trace_id = opts.trace_id;
9398

94-
let skel = open.load()?;
99+
let skel = open_skel.load()?;
95100

96101
let cgroup_fd = OpenOptions::new()
97102
.read(true)

examples/tproxy/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ fn main() -> Result<()> {
6868

6969
// Set constants
7070
let mut open_object = MaybeUninit::uninit();
71-
let open_skel = skel_builder.open(&mut open_object)?;
72-
open_skel.maps.rodata_data.target_port = opts.port.to_be();
73-
open_skel.maps.rodata_data.proxy_addr = proxy_addr.to_be();
74-
open_skel.maps.rodata_data.proxy_port = opts.proxy_port.to_be();
71+
let mut open_skel = skel_builder.open(&mut open_object)?;
72+
let rodata = open_skel
73+
.maps
74+
.rodata_data
75+
.as_deref_mut()
76+
.expect("`rodata` is not memory mapped");
77+
rodata.target_port = opts.port.to_be();
78+
rodata.proxy_addr = proxy_addr.to_be();
79+
rodata.proxy_port = opts.proxy_port.to_be();
7580

7681
// Load into kernel
7782
let skel = open_skel.load()?;

libbpf-cargo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Unreleased
22
----------
3+
- Adjusted all `<xxx>_data` BPF map skeleton members to be `Option`s
34
- Removed `SkeletonBuilder::skip_clang_version_check` and
45
`SkeletonBuilder::debug`
56
- Removed `--skip-clang-version-checks` option of `libbpf build`

libbpf-cargo/src/gen/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn gen_skel_map_defs(
405405
write!(
406406
skel,
407407
"\
408-
pub {name}_data: &'obj{ref_mut} types::{name},
408+
pub {name}_data: Option<&'obj{ref_mut} types::{name}>,
409409
",
410410
name = map.name,
411411
)?;
@@ -499,7 +499,6 @@ fn gen_skel_map_defs(
499499
.expect(\"BPF map `{name}` does not have mmap pointer\")
500500
.cast::<types::{name}>()
501501
.as_{ref_conv}()
502-
.expect(\"BPF map `{name}` mmap pointer is NULL\")
503502
}},
504503
",
505504
name = map.name,

libbpf-cargo/src/test.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -578,30 +578,28 @@ fn test_skeleton_datasec() {
578578
fn main() {
579579
let builder = ProgSkelBuilder::default();
580580
let mut open_object = MaybeUninit::uninit();
581-
let open_skel = builder
581+
let mut open_skel = builder
582582
.open(&mut open_object)
583583
.expect("failed to open skel");
584-
585584
// Check that we set rodata vars before load
586-
open_skel.maps.rodata_data.myconst = std::ptr::null_mut();
585+
open_skel.maps.rodata_data.as_deref_mut().unwrap().myconst = std::ptr::null_mut();
587586
588587
// We can always set bss vars
589-
open_skel.maps.bss_data.myglobal = 42;
588+
open_skel.maps.bss_data.as_deref_mut().unwrap().myglobal = 42;
589+
open_skel.maps.data_custom_data.as_deref_mut().unwrap().mycustomdata = 1337;
590+
open_skel.maps.bss_custom_data.as_deref_mut().unwrap().mycustombss = 12;
591+
assert_eq!(open_skel.maps.rodata_custom_1_data.as_deref_mut().unwrap().mycustomrodata, 43);
590592
591-
open_skel.maps.data_custom_data.mycustomdata = 1337;
592-
open_skel.maps.bss_custom_data.mycustombss = 12;
593-
assert_eq!(open_skel.maps.rodata_custom_1_data.mycustomrodata, 43);
594-
595-
let skel = open_skel.load().expect("failed to load skel");
593+
let mut skel = open_skel.load().expect("failed to load skel");
596594
597595
// We can always set bss vars
598-
skel.maps.bss_data.myglobal = 24;
599-
skel.maps.data_custom_data.mycustomdata += 1;
600-
skel.maps.bss_custom_data.mycustombss += 1;
601-
assert_eq!(skel.maps.rodata_custom_1_data.mycustomrodata, 43);
596+
skel.maps.bss_data.as_deref_mut().unwrap().myglobal = 24;
597+
skel.maps.data_custom_data.as_deref_mut().unwrap().mycustomdata += 1;
598+
skel.maps.bss_custom_data.as_deref_mut().unwrap().mycustombss += 1;
599+
assert_eq!(skel.maps.rodata_custom_1_data.as_deref().unwrap().mycustomrodata, 43);
602600
603601
// Read only for rodata after load
604-
let _rodata: &types::rodata = skel.maps.rodata_data;
602+
let _rodata: &types::rodata = skel.maps.rodata_data.as_deref().unwrap();
605603
}
606604
"#
607605
.to_string();
@@ -819,12 +817,13 @@ fn test_skeleton_builder_arrays_ptrs() {
819817
let open_skel = builder
820818
.open(&mut open_object)
821819
.expect("failed to open skel");
820+
let rodata = open_skel.maps.rodata_data.as_deref().unwrap();
822821
823822
// That everything exists and compiled okay
824-
let _ = open_skel.maps.rodata_data.my_array[0].x;
825-
let _ = open_skel.maps.rodata_data.my_array[0].y[1].b;
826-
let _ = open_skel.maps.rodata_data.my_array[0].z[0].a;
827-
let _ = open_skel.maps.rodata_data.my_ptr;
823+
let _ = rodata.my_array[0].x;
824+
let _ = rodata.my_array[0].y[1].b;
825+
let _ = rodata.my_array[0].z[0].a;
826+
let _ = rodata.my_ptr;
828827
}
829828
"#
830829
.to_string();

0 commit comments

Comments
 (0)