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/r4
Tuetuopay 2 years ago
parent 1450b9be63
commit 5af04c4582

@ -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. /// An iterator over the elements of the map.
@ -148,14 +154,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. /// An iterator over the elements of the array.
@ -141,14 +147,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. /// An iterator over the elements of the devmap in arbitrary order.

Loading…
Cancel
Save