remove programfd in favor of borrowedfd

Remove the Aya specific Programfd type in favor of the built in
BorrowedFd type.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
pull/694/head
Andrew Stoycos 2 years ago
parent 41fc87c2e6
commit dd988a3556
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, BorrowedFd, 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: BorrowedFd, 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, BorrowedFd, 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: BorrowedFd, 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: BorrowedFd,
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();

@ -69,7 +69,7 @@ use libc::ENOSPC;
use std::{ use std::{
ffi::CString, ffi::CString,
io, io,
os::fd::{AsRawFd, IntoRawFd as _, RawFd}, os::fd::{AsRawFd, BorrowedFd, IntoRawFd as _, RawFd},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use thiserror::Error; use thiserror::Error;
@ -214,16 +214,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 {
@ -373,7 +363,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<BorrowedFd> {
match self { match self {
Program::KProbe(p) => p.fd(), Program::KProbe(p) => p.fd(),
Program::UProbe(p) => p.fd(), Program::UProbe(p) => p.fd(),
@ -733,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<BorrowedFd> {
self.data.fd.map(|fd| ProgramFd(fd)) self.data.fd.map(|fd| unsafe { BorrowedFd::borrow_raw(fd) })
} }
} }
)+ )+

Loading…
Cancel
Save