Skip to content

Commit 1d5f764

Browse files
authored
Merge pull request #758 from aya-rs/map-fd-not-option
aya: MapData::fd is non-optional
2 parents c7b5cd5 + a31544b commit 1d5f764

File tree

21 files changed

+295
-543
lines changed

21 files changed

+295
-543
lines changed

aya-obj/src/relocation.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ pub enum RelocationError {
7373
address: u64,
7474
},
7575

76-
/// Referenced map not created yet
77-
#[error("the map `{name}` at section `{section_index}` has not been created")]
78-
MapNotCreated {
79-
/// The section index
80-
section_index: usize,
81-
/// The map name
82-
name: String,
83-
},
84-
8576
/// Invalid relocation offset
8677
#[error("invalid offset `{offset}` applying relocation #{relocation_number}")]
8778
InvalidRelocationOffset {
@@ -114,7 +105,7 @@ pub(crate) struct Symbol {
114105

115106
impl Object {
116107
/// Relocates the map references
117-
pub fn relocate_maps<'a, I: Iterator<Item = (&'a str, Option<i32>, &'a Map)>>(
108+
pub fn relocate_maps<'a, I: Iterator<Item = (&'a str, i32, &'a Map)>>(
118109
&mut self,
119110
maps: I,
120111
text_sections: &HashSet<usize>,
@@ -187,8 +178,8 @@ impl Object {
187178
fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
188179
fun: &mut Function,
189180
relocations: I,
190-
maps_by_section: &HashMap<usize, (&str, Option<i32>, &Map)>,
191-
maps_by_symbol: &HashMap<usize, (&str, Option<i32>, &Map)>,
181+
maps_by_section: &HashMap<usize, (&str, i32, &Map)>,
182+
maps_by_symbol: &HashMap<usize, (&str, i32, &Map)>,
192183
symbol_table: &HashMap<usize, Symbol>,
193184
text_sections: &HashSet<usize>,
194185
) -> Result<(), RelocationError> {
@@ -230,7 +221,7 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
230221
continue;
231222
}
232223

233-
let (name, fd, map) = if let Some(m) = maps_by_symbol.get(&rel.symbol_index) {
224+
let (_name, fd, map) = if let Some(m) = maps_by_symbol.get(&rel.symbol_index) {
234225
let map = &m.2;
235226
debug!(
236227
"relocating map by symbol index {:?}, kind {:?} at insn {ins_index} in section {}",
@@ -266,18 +257,13 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
266257
};
267258
debug_assert_eq!(map.section_index(), section_index);
268259

269-
let map_fd = fd.ok_or_else(|| RelocationError::MapNotCreated {
270-
name: (*name).into(),
271-
section_index,
272-
})?;
273-
274260
if !map.data().is_empty() {
275261
instructions[ins_index].set_src_reg(BPF_PSEUDO_MAP_VALUE as u8);
276262
instructions[ins_index + 1].imm = instructions[ins_index].imm + sym.address as i32;
277263
} else {
278264
instructions[ins_index].set_src_reg(BPF_PSEUDO_MAP_FD as u8);
279265
}
280-
instructions[ins_index].imm = map_fd;
266+
instructions[ins_index].imm = *fd;
281267
}
282268

283269
Ok(())
@@ -588,7 +574,7 @@ mod test {
588574
let maps_by_section = HashMap::new();
589575

590576
let map = fake_legacy_map(1);
591-
let maps_by_symbol = HashMap::from([(1, ("test_map", Some(1), &map))]);
577+
let maps_by_symbol = HashMap::from([(1, ("test_map", 1, &map))]);
592578

593579
relocate_maps(
594580
&mut fun,
@@ -642,8 +628,8 @@ mod test {
642628
let map_1 = fake_legacy_map(1);
643629
let map_2 = fake_legacy_map(2);
644630
let maps_by_symbol = HashMap::from([
645-
(1, ("test_map_1", Some(1), &map_1)),
646-
(2, ("test_map_2", Some(2), &map_2)),
631+
(1, ("test_map_1", 1, &map_1)),
632+
(2, ("test_map_2", 2, &map_2)),
647633
]);
648634

649635
relocate_maps(
@@ -683,7 +669,7 @@ mod test {
683669
let maps_by_section = HashMap::new();
684670

685671
let map = fake_btf_map(1);
686-
let maps_by_symbol = HashMap::from([(1, ("test_map", Some(1), &map))]);
672+
let maps_by_symbol = HashMap::from([(1, ("test_map", 1, &map))]);
687673

688674
relocate_maps(
689675
&mut fun,
@@ -737,8 +723,8 @@ mod test {
737723
let map_1 = fake_btf_map(1);
738724
let map_2 = fake_btf_map(2);
739725
let maps_by_symbol = HashMap::from([
740-
(1, ("test_map_1", Some(1), &map_1)),
741-
(2, ("test_map_2", Some(2), &map_2)),
726+
(1, ("test_map_1", 1, &map_1)),
727+
(2, ("test_map_2", 2, &map_2)),
742728
]);
743729

744730
relocate_maps(

aya/src/bpf.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
ffi::CString,
55
fs, io,
66
os::{
7-
fd::{AsFd as _, OwnedFd, RawFd},
7+
fd::{AsFd as _, OwnedFd},
88
raw::c_int,
99
},
1010
path::{Path, PathBuf},
@@ -475,35 +475,15 @@ impl<'a> BpfLoader<'a> {
475475
}
476476
}
477477
}
478-
let mut map = MapData {
479-
obj,
480-
fd: None,
481-
pinned: false,
482-
};
483-
let fd = match map.obj.pinning() {
478+
let btf_fd = btf_fd.as_deref().map(|fd| fd.as_fd());
479+
let mut map = match obj.pinning() {
480+
PinningType::None => MapData::create(obj, &name, btf_fd)?,
484481
PinningType::ByName => {
485-
let path = match &map_pin_path {
486-
Some(p) => p,
487-
None => return Err(BpfError::NoPinPath),
488-
};
489-
// try to open map in case it's already pinned
490-
match map.open_pinned(&name, path) {
491-
Ok(fd) => {
492-
map.pinned = true;
493-
fd as RawFd
494-
}
495-
Err(_) => {
496-
let fd = map.create(&name, btf_fd.as_deref().map(|f| f.as_fd()))?;
497-
map.pin(&name, path).map_err(|error| MapError::PinError {
498-
name: Some(name.to_string()),
499-
error,
500-
})?;
501-
fd
502-
}
503-
}
482+
let path = map_pin_path.as_ref().ok_or(BpfError::NoPinPath)?;
483+
MapData::create_pinned(path, obj, &name, btf_fd)?
504484
}
505-
PinningType::None => map.create(&name, btf_fd.as_deref().map(|f| f.as_fd()))?,
506485
};
486+
let fd = map.fd;
507487
if !map.obj.data().is_empty() && map.obj.section_kind() != BpfSectionKind::Bss {
508488
bpf_map_update_elem_ptr(fd, &0 as *const _, map.obj.data_mut().as_mut_ptr(), 0)
509489
.map_err(|(_, io_error)| SyscallError {

aya/src/maps/array/array.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ impl<T: Borrow<MapData>, V: Pod> Array<T, V> {
3939
let data = map.borrow();
4040
check_kv_size::<u32, V>(data)?;
4141

42-
let _fd = data.fd_or_err()?;
43-
4442
Ok(Array {
4543
inner: map,
4644
_v: PhantomData,
@@ -63,7 +61,7 @@ impl<T: Borrow<MapData>, V: Pod> Array<T, V> {
6361
pub fn get(&self, index: &u32, flags: u64) -> Result<V, MapError> {
6462
let data = self.inner.borrow();
6563
check_bounds(data, *index)?;
66-
let fd = data.fd_or_err()?;
64+
let fd = data.fd;
6765

6866
let value =
6967
bpf_map_lookup_elem(fd, index, flags).map_err(|(_, io_error)| SyscallError {
@@ -90,7 +88,7 @@ impl<T: BorrowMut<MapData>, V: Pod> Array<T, V> {
9088
pub fn set(&mut self, index: u32, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
9189
let data = self.inner.borrow_mut();
9290
check_bounds(data, index)?;
93-
let fd = data.fd_or_err()?;
91+
let fd = data.fd;
9492
bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| {
9593
SyscallError {
9694
call: "bpf_map_update_elem",

aya/src/maps/array/per_cpu_array.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V> {
5858
let data = map.borrow();
5959
check_kv_size::<u32, V>(data)?;
6060

61-
let _fd = data.fd_or_err()?;
62-
6361
Ok(PerCpuArray {
6462
inner: map,
6563
_v: PhantomData,
@@ -82,7 +80,7 @@ impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V> {
8280
pub fn get(&self, index: &u32, flags: u64) -> Result<PerCpuValues<V>, MapError> {
8381
let data = self.inner.borrow();
8482
check_bounds(data, *index)?;
85-
let fd = data.fd_or_err()?;
83+
let fd = data.fd;
8684

8785
let value = bpf_map_lookup_elem_per_cpu(fd, index, flags).map_err(|(_, io_error)| {
8886
SyscallError {
@@ -110,7 +108,7 @@ impl<T: BorrowMut<MapData>, V: Pod> PerCpuArray<T, V> {
110108
pub fn set(&mut self, index: u32, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> {
111109
let data = self.inner.borrow_mut();
112110
check_bounds(data, index)?;
113-
let fd = data.fd_or_err()?;
111+
let fd = data.fd;
114112

115113
bpf_map_update_elem_per_cpu(fd, &index, &values, flags).map_err(|(_, io_error)| {
116114
SyscallError {

aya/src/maps/array/program_array.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ impl<T: Borrow<MapData>> ProgramArray<T> {
5656
let data = map.borrow();
5757
check_kv_size::<u32, RawFd>(data)?;
5858

59-
let _fd = data.fd_or_err()?;
60-
6159
Ok(ProgramArray { inner: map })
6260
}
6361

@@ -76,7 +74,7 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
7674
pub fn set(&mut self, index: u32, program: &ProgramFd, flags: u64) -> Result<(), MapError> {
7775
let data = self.inner.borrow_mut();
7876
check_bounds(data, index)?;
79-
let fd = data.fd_or_err()?;
77+
let fd = data.fd;
8078
let prog_fd = program.as_fd();
8179
let prog_fd = prog_fd.as_raw_fd();
8280

@@ -96,7 +94,7 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
9694
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
9795
let data = self.inner.borrow_mut();
9896
check_bounds(data, *index)?;
99-
let fd = self.inner.borrow_mut().fd_or_err()?;
97+
let fd = self.inner.borrow_mut().fd;
10098

10199
bpf_map_delete_elem(fd, index)
102100
.map(|_| ())

0 commit comments

Comments
 (0)