Create greened_test.rs

pull/1047/head
Greened 6 months ago committed by GitHub
parent bcbe79bc95
commit c93d2f0ab7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,60 @@
use anyhow::Context;
use aya::{
include_bytes_aligned,
programs::{Xdp, XdpFlags},
Bpf,
};
use aya_log::BpfLogger;
use clap::Parser;
use log::warn;
use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
use tokio::signal::ctrl_c;
#[derive(Debug, Parser)]
struct Opt {
#[clap(short, long, default_value = "eth0")]
//#[clap(short, long, default_value = "enp1s0")]
iface: String,
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
TermLogger::init(
LevelFilter::Debug,
ConfigBuilder::new()
.set_target_level(LevelFilter::Error)
.set_location_level(LevelFilter::Error)
.build(),
TerminalMode::Mixed,
ColorChoice::Auto,
)
.unwrap();
let opt = Opt::parse();
// env_logger::init();
// This will include your eBPF object file as raw bytes at compile-time and load it at
// runtime. This approach is recommended for most real-world use cases. If you would
// like to specify the eBPF program at runtime rather than at compile-time, you can
// reach for `Bpf::load_file` instead.
#[cfg(debug_assertions)]
let mut bpf = Bpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/xdp-syncookie"
))?;
#[cfg(not(debug_assertions))]
let mut bpf = Bpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/xdp-syncookie"
))?;
if let Err(e) = BpfLogger::init(&mut bpf) {
// This can happen if you remove all logger statements from your eBPF program.
warn!("failed to initialize eBPF logger: {}", e);
}
let program: &mut Xdp = bpf.program_mut("xdp_syncookie").unwrap().try_into()?;
program.load()?;
program.attach(&opt.iface, XdpFlags::default())
.context("failed to attach the XDP program with default flags - try changing XdpFlags::default() to XdpFlags::SKB_MODE")?;
ctrl_c().await?;
// (1)
Ok(())
}
Loading…
Cancel
Save