Skip to content

Commit

Permalink
send error notification when failed to load or attach ebpf programs
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 6, 2024
1 parent 34ab15d commit 2289c92
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 20 deletions.
117 changes: 101 additions & 16 deletions oryx-tui/src/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions oryx-tui/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ impl Notification {
frame.render_widget(block, area);
}

pub fn send(
message: String,
pub fn send<T: ToString>(
message: T,
level: NotificationLevel,
sender: kanal::Sender<Event>,
) -> AppResult<()> {
let notif = Notification {
message,
message: message.to_string(),
level,
ttl: 8,
ttl: 16,
};

sender.send(Event::Notification(notif))?;
Expand Down

0 comments on commit 2289c92

Please sign in to comment.