Procházet zdrojové kódy

aya: consolidate errors into ProgramError::SyscallError

Alessandro Decina před 4 roky
rodič
revize
683a58ea6d

+ 3 - 14
aya/src/programs/mod.rs

@@ -93,14 +93,9 @@ pub enum ProgramError {
         verifier_log: String,
     },
 
-    #[error("the perf_event_open syscall failed")]
-    PerfEventOpenError {
-        #[source]
-        io_error: io::Error,
-    },
-
-    #[error("PERF_EVENT_IOC_SET_BPF/PERF_EVENT_IOC_ENABLE failed")]
-    PerfEventAttachError {
+    #[error("`{call}` failed")]
+    SyscallError {
+        call: String,
         #[source]
         io_error: io::Error,
     },
@@ -108,12 +103,6 @@ pub enum ProgramError {
     #[error("unknown network interface {name}")]
     UnknownInterface { name: String },
 
-    #[error("BPF_LINK_CREATE failed")]
-    BpfLinkCreateError {
-        #[source]
-        io_error: io::Error,
-    },
-
     #[error("unexpected program type")]
     UnexpectedProgramType,
 

+ 12 - 4
aya/src/programs/perf_attach.rs

@@ -32,10 +32,18 @@ impl Drop for PerfLink {
 
 pub(crate) fn perf_attach(data: &mut ProgramData, fd: RawFd) -> Result<LinkRef, ProgramError> {
     let prog_fd = data.fd_or_err()?;
-    perf_event_ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd)
-        .map_err(|(_, io_error)| ProgramError::PerfEventAttachError { io_error })?;
-    perf_event_ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)
-        .map_err(|(_, io_error)| ProgramError::PerfEventAttachError { io_error })?;
+    perf_event_ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd).map_err(|(_, io_error)| {
+        ProgramError::SyscallError {
+            call: "PERF_EVENT_IOC_SET_BPF".to_owned(),
+            io_error,
+        }
+    })?;
+    perf_event_ioctl(fd, PERF_EVENT_IOC_ENABLE, 0).map_err(|(_, io_error)| {
+        ProgramError::SyscallError {
+            call: "PERF_EVENT_IOC_ENABLE".to_owned(),
+            io_error,
+        }
+    })?;
 
     Ok(data.link(PerfLink { perf_fd: Some(fd) }))
 }

+ 6 - 3
aya/src/programs/probe.rs

@@ -44,9 +44,12 @@ pub(crate) fn attach(
         _ => None,
     };
 
-    let fd = perf_event_open_probe(perf_ty, ret_bit, name, offset, pid)
-        .map_err(|(_code, io_error)| ProgramError::PerfEventOpenError { io_error })?
-        as i32;
+    let fd = perf_event_open_probe(perf_ty, ret_bit, name, offset, pid).map_err(
+        |(_code, io_error)| ProgramError::SyscallError {
+            call: "perf_event_open".to_owned(),
+            io_error,
+        },
+    )? as i32;
 
     perf_attach(program_data, fd)
 }

+ 6 - 3
aya/src/programs/trace_point.rs

@@ -27,9 +27,12 @@ impl TracePoint {
 
     pub fn attach(&mut self, category: &str, name: &str) -> Result<LinkRef, ProgramError> {
         let id = read_sys_fs_trace_point_id(category, name)?;
-        let fd = perf_event_open_trace_point(id)
-            .map_err(|(_code, io_error)| ProgramError::PerfEventOpenError { io_error })?
-            as i32;
+        let fd = perf_event_open_trace_point(id).map_err(|(_code, io_error)| {
+            ProgramError::SyscallError {
+                call: "perf_event_open".to_owned(),
+                io_error,
+            }
+        })? as i32;
 
         perf_attach(&mut self.data, fd)
     }

+ 6 - 3
aya/src/programs/xdp.rs

@@ -59,9 +59,12 @@ impl Xdp {
 
         let k_ver = kernel_version().unwrap();
         if k_ver >= (5, 9, 0) {
-            let link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, flags.bits)
-                .map_err(|(_, io_error)| ProgramError::BpfLinkCreateError { io_error })?
-                as RawFd;
+            let link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, flags.bits).map_err(
+                |(_, io_error)| ProgramError::SyscallError {
+                    call: "bpf_link_create".to_owned(),
+                    io_error,
+                },
+            )? as RawFd;
             Ok(self
                 .data
                 .link(XdpLink::FdLink(FdLink { fd: Some(link_fd) })))