From 2289c92408b14ad2840823e3d0c5a0caf9d5a803 Mon Sep 17 00:00:00 2001 From: pythops Date: Fri, 6 Sep 2024 23:46:40 +0200 Subject: [PATCH] send error notification when failed to load or attach ebpf programs --- oryx-tui/src/ebpf.rs | 117 ++++++++++++++++++++++++++++++----- oryx-tui/src/notification.rs | 8 +-- 2 files changed, 105 insertions(+), 20 deletions(-) diff --git a/oryx-tui/src/ebpf.rs b/oryx-tui/src/ebpf.rs index 9022469..23887cb 100644 --- a/oryx-tui/src/ebpf.rs +++ b/oryx-tui/src/ebpf.rs @@ -13,6 +13,7 @@ use mio::{Events, Interest, Poll, Registry, Token}; use oryx_common::IpPacket; use crate::event::Event; +use crate::notification::{Notification, NotificationLevel}; use mio::event::Source; use mio::unix::SourceFd; @@ -72,23 +73,67 @@ impl Ebpf { unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; #[cfg(debug_assertions)] - let mut bpf = Bpf::load(include_bytes_aligned!( + let mut bpf = match Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/debug/oryx" - )) - .unwrap(); + )) { + Ok(v) => v, + Err(e) => { + Notification::send( + format!("Failed to load the ingress eBPF bytecode\n {}", e), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + } + }; #[cfg(not(debug_assertions))] - let mut bpf = Bpf::load(include_bytes_aligned!( + let mut bpf = match Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/release/oryx" - )) - .unwrap(); + )) { + Ok(v) => v, + Err(e) => { + Notification::send( + format!("Failed to load the ingress eBPF bytecode\n {}", e), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + } + }; let _ = tc::qdisc_add_clsact(&iface); + let program: &mut SchedClassifier = bpf.program_mut("oryx").unwrap().try_into().unwrap(); - program.load().unwrap(); - program.attach(&iface, TcAttachType::Ingress).unwrap(); + if let Err(e) = program.load() { + Notification::send( + format!( + "Failed to load the ingress eBPF program to the kernel\n{}", + e + ), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + }; + + if let Err(e) = program.attach(&iface, TcAttachType::Ingress) { + Notification::send( + format!( + "Failed to attach the ingress eBPF program to the interface\n{}", + e + ), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + }; let mut poll = Poll::new().unwrap(); let mut events = Events::with_capacity(128); @@ -149,23 +194,63 @@ impl Ebpf { unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; #[cfg(debug_assertions)] - let mut bpf = Bpf::load(include_bytes_aligned!( + let mut bpf = match Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/debug/oryx" - )) - .unwrap(); + )) { + Ok(v) => v, + Err(e) => { + Notification::send( + format!("Fail to load the egress eBPF bytecode\n {}", e), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + } + }; #[cfg(not(debug_assertions))] - let mut bpf = Bpf::load(include_bytes_aligned!( + let mut bpf = match Bpf::load(include_bytes_aligned!( "../../target/bpfel-unknown-none/release/oryx" - )) - .unwrap(); + )) { + Ok(v) => v, + Err(e) => { + Notification::send( + format!("Failed to load the egress eBPF bytecode\n {}", e), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + } + }; let _ = tc::qdisc_add_clsact(&iface); let program: &mut SchedClassifier = bpf.program_mut("oryx").unwrap().try_into().unwrap(); - program.load().unwrap(); - program.attach(&iface, TcAttachType::Egress).unwrap(); + if let Err(e) = program.load() { + Notification::send( + format!("Fail to load the egress eBPF program to the kernel\n{}", e), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + }; + + if let Err(e) = program.attach(&iface, TcAttachType::Egress) { + Notification::send( + format!( + "Failed to attach the egress eBPF program to the interface\n{}", + e + ), + NotificationLevel::Error, + sender, + ) + .unwrap(); + return; + }; let mut poll = Poll::new().unwrap(); let mut events = Events::with_capacity(128); diff --git a/oryx-tui/src/notification.rs b/oryx-tui/src/notification.rs index bed0775..0a00254 100644 --- a/oryx-tui/src/notification.rs +++ b/oryx-tui/src/notification.rs @@ -61,15 +61,15 @@ impl Notification { frame.render_widget(block, area); } - pub fn send( - message: String, + pub fn send( + message: T, level: NotificationLevel, sender: kanal::Sender, ) -> AppResult<()> { let notif = Notification { - message, + message: message.to_string(), level, - ttl: 8, + ttl: 16, }; sender.send(Event::Notification(notif))?;