aya: More pinning fixes

This commit fixes a bug and adds some missing lifecycle APIs.

1. Adds PinnedLink::from_path to create a pinned link from bpffs
2. Adds From<PinnedLink> for FdLink to allow for ^ to be converted
3. Adds From<FdLink> for XdpLink

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/378/head
Dave Tucker 2 years ago
parent 7b99a57330
commit 4b5b9ab3d9

@ -14,7 +14,7 @@ use crate::{
generated::bpf_attach_type, generated::bpf_attach_type,
pin::PinError, pin::PinError,
programs::ProgramError, programs::ProgramError,
sys::{bpf_pin_object, bpf_prog_detach}, sys::{bpf_get_object, bpf_pin_object, bpf_prog_detach},
}; };
/// A Link. /// A Link.
@ -162,6 +162,12 @@ impl Drop for FdLink {
} }
} }
impl From<PinnedLink> for FdLink {
fn from(p: PinnedLink) -> Self {
p.inner
}
}
/// A pinned file descriptor link. /// A pinned file descriptor link.
/// ///
/// This link has been pinned to the BPF filesystem. On drop, the file descriptor that backs /// This link has been pinned to the BPF filesystem. On drop, the file descriptor that backs
@ -181,6 +187,18 @@ impl PinnedLink {
} }
} }
/// Creates a [`PinnedLink`] from a valid path on bpffs
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, LinkError> {
let path_string = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
let fd =
bpf_get_object(&path_string).map_err(|(code, io_error)| LinkError::SyscallError {
call: "BPF_OBJ_GET".to_string(),
code,
io_error,
})? as RawFd;
Ok(PinnedLink::new(path.as_ref().to_path_buf(), fd))
}
/// Removes the pinned link from the filesystem and returns an [`FdLink`]. /// Removes the pinned link from the filesystem and returns an [`FdLink`].
pub fn unpin(self) -> Result<FdLink, io::Error> { pub fn unpin(self) -> Result<FdLink, io::Error> {
std::fs::remove_file(self.path)?; std::fs::remove_file(self.path)?;
@ -272,6 +290,17 @@ pub enum LinkError {
/// Invalid link. /// Invalid link.
#[error("Invalid link")] #[error("Invalid link")]
InvalidLink, InvalidLink,
/// Syscall failed.
#[error("the `{call}` syscall failed with code {code}")]
SyscallError {
/// Syscall Name.
call: String,
/// Error code.
code: libc::c_long,
#[source]
/// Original io::Error.
io_error: io::Error,
},
} }
#[cfg(test)] #[cfg(test)]

@ -250,6 +250,12 @@ impl TryFrom<XdpLink> for FdLink {
} }
} }
impl From<FdLink> for XdpLink {
fn from(fd_link: FdLink) -> Self {
XdpLink(XdpLinkInner::FdLink(fd_link))
}
}
define_link_wrapper!( define_link_wrapper!(
/// The link used by [Xdp] programs. /// The link used by [Xdp] programs.
XdpLink, XdpLink,

Loading…
Cancel
Save