remove programfd in favor of ownedfd

This commit removes the ProgramFd wrapper of RawFd and deduplicates some
code. Specifically it adds a new constructor for ProgramInfo which
allows it to be generated from an OwnedFd.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
reviewable/pr637/r28
Andrew Stoycos 2 years ago
parent 909394e93d
commit 90d9dbbee1
No known key found for this signature in database
GPG Key ID: 66735B92BB71C096

@ -2,12 +2,11 @@
use std::{ use std::{
borrow::{Borrow, BorrowMut}, borrow::{Borrow, BorrowMut},
os::fd::{AsRawFd, RawFd}, os::fd::{AsRawFd, OwnedFd, RawFd},
}; };
use crate::{ use crate::{
maps::{check_bounds, check_kv_size, MapData, MapError, MapKeys}, maps::{check_bounds, check_kv_size, MapData, MapError, MapKeys},
programs::ProgramFd,
sys::{bpf_map_delete_elem, bpf_map_update_elem}, sys::{bpf_map_delete_elem, bpf_map_update_elem},
}; };
@ -73,7 +72,7 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
/// ///
/// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control
/// flow will jump to `program`. /// flow will jump to `program`.
pub fn set(&mut self, index: u32, program: ProgramFd, flags: u64) -> Result<(), MapError> { pub fn set(&mut self, index: u32, program: OwnedFd, flags: u64) -> Result<(), MapError> {
let data = self.inner.borrow_mut(); let data = self.inner.borrow_mut();
check_bounds(data, index)?; check_bounds(data, index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;

@ -1,5 +1,5 @@
//! Extension programs. //! Extension programs.
use std::os::fd::{AsRawFd, RawFd}; use std::os::fd::{AsRawFd, OwnedFd, RawFd};
use thiserror::Error; use thiserror::Error;
use object::Endianness; use object::Endianness;
@ -7,9 +7,7 @@ use object::Endianness;
use crate::{ use crate::{
generated::{bpf_attach_type::BPF_CGROUP_INET_INGRESS, bpf_prog_type::BPF_PROG_TYPE_EXT}, generated::{bpf_attach_type::BPF_CGROUP_INET_INGRESS, bpf_prog_type::BPF_PROG_TYPE_EXT},
obj::btf::BtfKind, obj::btf::BtfKind,
programs::{ programs::{define_link_wrapper, load_program, FdLink, FdLinkId, ProgramData, ProgramError},
define_link_wrapper, load_program, FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd,
},
sys::{self, bpf_link_create}, sys::{self, bpf_link_create},
Btf, Btf,
}; };
@ -68,7 +66,7 @@ impl Extension {
/// The extension code will be loaded but inactive until it's attached. /// The extension code will be loaded but inactive until it's attached.
/// There are no restrictions on what functions may be replaced, so you could replace /// There are no restrictions on what functions may be replaced, so you could replace
/// the main entry point of your program with an extension. /// the main entry point of your program with an extension.
pub fn load(&mut self, program: ProgramFd, func_name: &str) -> Result<(), ProgramError> { pub fn load(&mut self, program: OwnedFd, func_name: &str) -> Result<(), ProgramError> {
let target_prog_fd = program.as_raw_fd(); let target_prog_fd = program.as_raw_fd();
let (btf_fd, btf_id) = get_btf_info(target_prog_fd, func_name)?; let (btf_fd, btf_id) = get_btf_info(target_prog_fd, func_name)?;
@ -113,7 +111,7 @@ impl Extension {
/// original function, see [Extension::detach]. /// original function, see [Extension::detach].
pub fn attach_to_program( pub fn attach_to_program(
&mut self, &mut self,
program: ProgramFd, program: OwnedFd,
func_name: &str, func_name: &str,
) -> Result<ExtensionLinkId, ProgramError> { ) -> Result<ExtensionLinkId, ProgramError> {
let target_fd = program.as_raw_fd(); let target_fd = program.as_raw_fd();

@ -1,10 +1,10 @@
//! Lirc programs. //! Lirc programs.
use std::os::fd::{AsRawFd, IntoRawFd as _, RawFd}; use std::os::fd::{AsRawFd, FromRawFd as _, IntoRawFd as _, OwnedFd, RawFd};
use crate::{ use crate::{
generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2}, generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2},
programs::{load_program, query, Link, ProgramData, ProgramError, ProgramInfo}, programs::{load_program, query, Link, ProgramData, ProgramError, ProgramInfo},
sys::{bpf_prog_attach, bpf_prog_detach, bpf_prog_get_fd_by_id, bpf_prog_get_info_by_fd}, sys::{bpf_prog_attach, bpf_prog_detach, bpf_prog_get_fd_by_id},
}; };
use libc::{close, dup}; use libc::{close, dup};
@ -132,13 +132,7 @@ impl LircLink {
/// Get ProgramInfo from this link /// Get ProgramInfo from this link
pub fn info(&self) -> Result<ProgramInfo, ProgramError> { pub fn info(&self) -> Result<ProgramInfo, ProgramError> {
match bpf_prog_get_info_by_fd(self.prog_fd, &[]) { ProgramInfo::new_from_fd(unsafe { OwnedFd::from_raw_fd(self.prog_fd) })
Ok(info) => Ok(ProgramInfo(info)),
Err(io_error) => Err(ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
}),
}
} }
} }

@ -69,7 +69,7 @@ use libc::ENOSPC;
use std::{ use std::{
ffi::CString, ffi::CString,
io, io,
os::fd::{AsFd, AsRawFd, OwnedFd, RawFd}, os::fd::{AsFd, AsRawFd, FromRawFd as _, OwnedFd, RawFd},
path::{Path, PathBuf}, path::{Path, PathBuf},
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
@ -216,16 +216,6 @@ pub enum ProgramError {
IOError(#[from] io::Error), IOError(#[from] io::Error),
} }
/// A [`Program`] file descriptor.
#[derive(Copy, Clone)]
pub struct ProgramFd(RawFd);
impl AsRawFd for ProgramFd {
fn as_raw_fd(&self) -> RawFd {
self.0
}
}
/// eBPF program type. /// eBPF program type.
#[derive(Debug)] #[derive(Debug)]
pub enum Program { pub enum Program {
@ -375,7 +365,7 @@ impl Program {
/// ///
/// Can be used to add a program to a [`crate::maps::ProgramArray`] or attach an [`Extension`] program. /// Can be used to add a program to a [`crate::maps::ProgramArray`] or attach an [`Extension`] program.
/// Can be converted to [`RawFd`] using [`AsRawFd`]. /// Can be converted to [`RawFd`] using [`AsRawFd`].
pub fn fd(&self) -> Option<ProgramFd> { pub fn fd(&self) -> Option<OwnedFd> {
match self { match self {
Program::KProbe(p) => p.fd(), Program::KProbe(p) => p.fd(),
Program::UProbe(p) => p.fd(), Program::UProbe(p) => p.fd(),
@ -496,14 +486,11 @@ impl<T: Link> ProgramData<T> {
io_error, io_error,
})? as RawFd; })? as RawFd;
let info = let info = ProgramInfo::new_from_fd(unsafe { OwnedFd::from_raw_fd(fd) })?;
bpf_prog_get_info_by_fd(fd, &[]).map_err(|io_error| ProgramError::SyscallError { let name = info.name_as_str().map(|s| s.to_string());
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
let name = ProgramInfo(info).name_as_str().map(|s| s.to_string()); let ProgramInfo(bpf_prog_info) = info;
ProgramData::from_bpf_prog_info(name, fd, path.as_ref(), info, verifier_log_level) ProgramData::from_bpf_prog_info(name, fd, path.as_ref(), bpf_prog_info, verifier_log_level)
} }
} }
@ -736,8 +723,8 @@ macro_rules! impl_fd {
$( $(
impl $struct_name { impl $struct_name {
/// Returns the file descriptor of this Program. /// Returns the file descriptor of this Program.
pub fn fd(&self) -> Option<ProgramFd> { pub fn fd(&self) -> Option<OwnedFd> {
self.data.fd.map(|fd| ProgramFd(fd)) self.data.fd.map(|fd| unsafe{ OwnedFd::from_raw_fd(fd) })
} }
} }
)+ )+
@ -931,13 +918,7 @@ macro_rules! impl_program_info {
/// Returns the file descriptor of this Program. /// Returns the file descriptor of this Program.
pub fn program_info(&self) -> Result<ProgramInfo, ProgramError> { pub fn program_info(&self) -> Result<ProgramInfo, ProgramError> {
let fd = self.fd().ok_or(ProgramError::NotLoaded)?; let fd = self.fd().ok_or(ProgramError::NotLoaded)?;
ProgramInfo::new_from_fd(fd)
bpf_prog_get_info_by_fd(fd.as_raw_fd(), &[])
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})
.map(ProgramInfo)
} }
} }
)+ )+
@ -976,6 +957,15 @@ impl_program_info!(
pub struct ProgramInfo(bpf_prog_info); pub struct ProgramInfo(bpf_prog_info);
impl ProgramInfo { impl ProgramInfo {
fn new_from_fd(fd: OwnedFd) -> Result<Self, ProgramError> {
bpf_prog_get_info_by_fd(fd.as_raw_fd(), &[])
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})
.map(ProgramInfo)
}
/// The name of the program as was provided when it was load. This is limited to 16 bytes /// The name of the program as was provided when it was load. This is limited to 16 bytes
pub fn name(&self) -> &[u8] { pub fn name(&self) -> &[u8] {
let length = self let length = self
@ -1088,15 +1078,7 @@ impl ProgramInfo {
io_error, io_error,
})? as RawFd; })? as RawFd;
let info = ProgramInfo::new_from_fd(unsafe { OwnedFd::from_raw_fd(fd) })
bpf_prog_get_info_by_fd(fd, &[]).map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
unsafe {
libc::close(fd);
}
Ok(ProgramInfo(info))
} }
} }
@ -1124,14 +1106,7 @@ impl Iterator for ProgramsIter {
call: "bpf_prog_get_fd_by_id", call: "bpf_prog_get_fd_by_id",
io_error, io_error,
}) })
.and_then(|fd| { .and_then(ProgramInfo::new_from_fd),
bpf_prog_get_info_by_fd(fd.as_raw_fd(), &[])
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})
.map(ProgramInfo)
}),
) )
} }
Ok(None) => None, Ok(None) => None,

Loading…
Cancel
Save