maps: MapData::{obj, fd} are private

pull/770/head
Tamir Duberstein 1 year ago
parent f41592663c
commit b4d5a1e8db
No known key found for this signature in database

@ -36,12 +36,11 @@ use crate::{
SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint, UProbe, Xdp, SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint, UProbe, Xdp,
}, },
sys::{ sys::{
bpf_load_btf, bpf_map_freeze, bpf_map_update_elem_ptr, is_bpf_cookie_supported, bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
is_bpf_global_data_supported, is_btf_datasec_supported, is_btf_decl_tag_supported, is_btf_datasec_supported, is_btf_decl_tag_supported, is_btf_enum64_supported,
is_btf_enum64_supported, is_btf_float_supported, is_btf_func_global_supported, is_btf_float_supported, is_btf_func_global_supported, is_btf_func_supported,
is_btf_func_supported, is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported, is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported,
is_probe_read_kernel_supported, is_prog_name_supported, retry_with_verifier_logs, is_probe_read_kernel_supported, is_prog_name_supported, retry_with_verifier_logs,
SyscallError,
}, },
util::{bytes_of, bytes_of_slice, possible_cpus, POSSIBLE_CPUS}, util::{bytes_of, bytes_of_slice, possible_cpus, POSSIBLE_CPUS},
}; };
@ -482,23 +481,7 @@ impl<'a> BpfLoader<'a> {
MapData::create_pinned(path, obj, &name, btf_fd)? MapData::create_pinned(path, obj, &name, btf_fd)?
} }
}; };
let MapData { obj, fd, pinned: _ } = &mut map; map.finalize()?;
if !obj.data().is_empty() && obj.section_kind() != BpfSectionKind::Bss {
bpf_map_update_elem_ptr(fd.as_fd(), &0 as *const _, obj.data_mut().as_mut_ptr(), 0)
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(MapError::from)?;
}
if obj.section_kind() == BpfSectionKind::Rodata {
bpf_map_freeze(fd.as_fd())
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_freeze",
io_error,
})
.map_err(MapError::from)?;
}
maps.insert(name, map); maps.insert(name, map);
} }
@ -510,7 +493,7 @@ impl<'a> BpfLoader<'a> {
obj.relocate_maps( obj.relocate_maps(
maps.iter() maps.iter()
.map(|(s, data)| (s.as_str(), data.fd().as_fd().as_raw_fd(), &data.obj)), .map(|(s, data)| (s.as_str(), data.fd().as_fd().as_raw_fd(), data.obj())),
&text_sections, &text_sections,
)?; )?;
obj.relocate_calls(&text_sections)?; obj.relocate_calls(&text_sections)?;
@ -691,7 +674,7 @@ impl<'a> BpfLoader<'a> {
if !*allow_unsupported_maps { if !*allow_unsupported_maps {
maps.iter().try_for_each(|(_, x)| match x { maps.iter().try_for_each(|(_, x)| match x {
Map::Unsupported(map) => Err(BpfError::MapError(MapError::Unsupported { Map::Unsupported(map) => Err(BpfError::MapError(MapError::Unsupported {
map_type: map.obj.map_type(), map_type: map.obj().map_type(),
})), })),
_ => Ok(()), _ => Ok(()),
})?; })?;
@ -705,7 +688,7 @@ fn parse_map(data: (String, MapData)) -> Result<(String, Map), BpfError> {
let name = data.0; let name = data.0;
let map = data.1; let map = data.1;
let map_type = let map_type =
bpf_map_type::try_from(map.obj.map_type()).map_err(|e| MapError::InvalidMapType { bpf_map_type::try_from(map.obj().map_type()).map_err(|e| MapError::InvalidMapType {
map_type: e.map_type, map_type: e.map_type,
})?; })?;
let map = match map_type { let map = match map_type {

@ -64,11 +64,11 @@ use log::warn;
use thiserror::Error; use thiserror::Error;
use crate::{ use crate::{
obj::{self, parse_map_info}, obj::{self, parse_map_info, BpfSectionKind},
pin::PinError, pin::PinError,
sys::{ sys::{
bpf_create_map, bpf_get_object, bpf_map_get_info_by_fd, bpf_map_get_next_key, bpf_create_map, bpf_get_object, bpf_map_freeze, bpf_map_get_info_by_fd,
bpf_pin_object, SyscallError, bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object, SyscallError,
}, },
util::nr_cpus, util::nr_cpus,
PinningType, Pod, PinningType, Pod,
@ -408,8 +408,8 @@ pub(crate) fn check_v_size<V>(map: &MapData) -> Result<(), MapError> {
/// You should never need to use this unless you're implementing a new map type. /// You should never need to use this unless you're implementing a new map type.
#[derive(Debug)] #[derive(Debug)]
pub struct MapData { pub struct MapData {
pub(crate) obj: obj::Map, obj: obj::Map,
pub(crate) fd: MapFd, fd: MapFd,
/// Indicates if this map has been pinned to bpffs /// Indicates if this map has been pinned to bpffs
pub pinned: bool, pub pinned: bool,
} }
@ -489,6 +489,27 @@ impl MapData {
} }
} }
pub(crate) fn finalize(&mut self) -> Result<(), MapError> {
let Self { obj, fd, pinned: _ } = self;
if !obj.data().is_empty() && obj.section_kind() != BpfSectionKind::Bss {
bpf_map_update_elem_ptr(fd.as_fd(), &0 as *const _, obj.data_mut().as_mut_ptr(), 0)
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(MapError::from)?;
}
if obj.section_kind() == BpfSectionKind::Rodata {
bpf_map_freeze(fd.as_fd())
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_freeze",
io_error,
})
.map_err(MapError::from)?;
}
Ok(())
}
/// Loads a map from a pinned path in bpffs. /// Loads a map from a pinned path in bpffs.
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, MapError> { pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, MapError> {
use std::os::unix::ffi::OsStrExt as _; use std::os::unix::ffi::OsStrExt as _;
@ -561,6 +582,15 @@ impl MapData {
} = self; } = self;
fd fd
} }
pub(crate) fn obj(&self) -> &obj::Map {
let Self {
obj,
fd: _,
pinned: _,
} = self;
obj
}
} }
/// An iterable map /// An iterable map

Loading…
Cancel
Save