maps/xdp: drop the possibly-unsafe {Cpu,Dev}MapValue conversions

Making a `From` impl that hides an unsafe with some safety guarantee is
actually unsafe, because while we know how we use this impl, we don't
know how other will. And since it's on two public types, anything can
be done.
reviewable/pr527/r2
Tuetuopay 2 years ago
parent 545199a3da
commit df51dd7ae4

@ -77,7 +77,13 @@ impl<T: Borrow<MapData>> CpuMap<T> {
io_error, io_error,
})?; })?;
let value: bpf_cpumap_val = value.ok_or(MapError::KeyNotFound)?; let value: bpf_cpumap_val = value.ok_or(MapError::KeyNotFound)?;
Ok(value.into())
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6149
Ok(CpuMapValue {
qsize: value.qsize,
prog_id: unsafe { value.bpf_prog.id },
})
} }
/// An iterator over the elements of the map. The iterator item type is `Result<u32, /// An iterator over the elements of the map. The iterator item type is `Result<u32,
@ -149,14 +155,3 @@ pub struct CpuMapValue {
pub qsize: u32, pub qsize: u32,
pub prog_id: u32, pub prog_id: u32,
} }
impl From<bpf_cpumap_val> for CpuMapValue {
fn from(value: bpf_cpumap_val) -> Self {
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6149
CpuMapValue {
qsize: value.qsize,
prog_id: unsafe { value.bpf_prog.id },
}
}
}

@ -71,7 +71,13 @@ impl<T: Borrow<MapData>> DevMap<T> {
io_error, io_error,
})?; })?;
let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?; let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?;
Ok(value.into())
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136
Ok(DevMapValue {
ifindex: value.ifindex,
prog_id: unsafe { value.bpf_prog.id },
})
} }
/// An iterator over the elements of the array. The iterator item type is `Result<u32, /// An iterator over the elements of the array. The iterator item type is `Result<u32,
@ -142,14 +148,3 @@ pub struct DevMapValue {
pub ifindex: u32, pub ifindex: u32,
pub prog_id: u32, pub prog_id: u32,
} }
impl From<bpf_devmap_val> for DevMapValue {
fn from(value: bpf_devmap_val) -> Self {
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136
DevMapValue {
ifindex: value.ifindex,
prog_id: unsafe { value.bpf_prog.id },
}
}
}

@ -60,7 +60,13 @@ impl<T: Borrow<MapData>> DevMapHash<T> {
io_error, io_error,
})?; })?;
let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?; let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?;
Ok(value.into())
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136
Ok(DevMapValue {
ifindex: value.ifindex,
prog_id: unsafe { value.bpf_prog.id },
})
} }
/// An iterator over the elements of the devmap in arbitrary order. The iterator item type is /// An iterator over the elements of the devmap in arbitrary order. The iterator item type is

Loading…
Cancel
Save