From dd988a3556cc28c9418cad1fcb470b660abc626a Mon Sep 17 00:00:00 2001 From: Andrew Stoycos Date: Thu, 27 Jul 2023 09:36:03 -0400 Subject: [PATCH] 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 --- aya/src/maps/array/program_array.rs | 5 ++--- aya/src/programs/extension.rs | 10 ++++------ aya/src/programs/mod.rs | 18 ++++-------------- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 613d7b17..0dd51207 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -2,12 +2,11 @@ use std::{ borrow::{Borrow, BorrowMut}, - os::fd::{AsRawFd, RawFd}, + os::fd::{AsRawFd, BorrowedFd, RawFd}, }; use crate::{ maps::{check_bounds, check_kv_size, MapData, MapError, MapKeys}, - programs::ProgramFd, sys::{bpf_map_delete_elem, bpf_map_update_elem}, }; @@ -73,7 +72,7 @@ impl> ProgramArray { /// /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control /// 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(); check_bounds(data, index)?; let fd = data.fd_or_err()?; diff --git a/aya/src/programs/extension.rs b/aya/src/programs/extension.rs index 682d14ad..3d7fafd5 100644 --- a/aya/src/programs/extension.rs +++ b/aya/src/programs/extension.rs @@ -1,5 +1,5 @@ //! Extension programs. -use std::os::fd::{AsRawFd, RawFd}; +use std::os::fd::{AsRawFd, BorrowedFd, RawFd}; use thiserror::Error; use object::Endianness; @@ -7,9 +7,7 @@ use object::Endianness; use crate::{ generated::{bpf_attach_type::BPF_CGROUP_INET_INGRESS, bpf_prog_type::BPF_PROG_TYPE_EXT}, obj::btf::BtfKind, - programs::{ - define_link_wrapper, load_program, FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, - }, + programs::{define_link_wrapper, load_program, FdLink, FdLinkId, ProgramData, ProgramError}, sys::{self, bpf_link_create}, Btf, }; @@ -68,7 +66,7 @@ impl Extension { /// 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 /// 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 (btf_fd, btf_id) = get_btf_info(target_prog_fd, func_name)?; @@ -113,7 +111,7 @@ impl Extension { /// original function, see [Extension::detach]. pub fn attach_to_program( &mut self, - program: ProgramFd, + program: BorrowedFd, func_name: &str, ) -> Result { let target_fd = program.as_raw_fd(); diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index f868dfc8..33e30b0e 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -69,7 +69,7 @@ use libc::ENOSPC; use std::{ ffi::CString, io, - os::fd::{AsRawFd, IntoRawFd as _, RawFd}, + os::fd::{AsRawFd, BorrowedFd, IntoRawFd as _, RawFd}, path::{Path, PathBuf}, }; use thiserror::Error; @@ -214,16 +214,6 @@ pub enum ProgramError { 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. #[derive(Debug)] 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 converted to [`RawFd`] using [`AsRawFd`]. - pub fn fd(&self) -> Option { + pub fn fd(&self) -> Option { match self { Program::KProbe(p) => p.fd(), Program::UProbe(p) => p.fd(), @@ -733,8 +723,8 @@ macro_rules! impl_fd { $( impl $struct_name { /// Returns the file descriptor of this Program. - pub fn fd(&self) -> Option { - self.data.fd.map(|fd| ProgramFd(fd)) + pub fn fd(&self) -> Option { + self.data.fd.map(|fd| unsafe { BorrowedFd::borrow_raw(fd) }) } } )+