mirror of
https://github.com/h3xduck/TripleCross.git
synced 2025-12-21 09:13:07 +08:00
Setup development environment with libbpf
This commit is contained in:
1
external/libbpf-bootstrap/examples/rust/xdp/src/bpf/vmlinux.h
vendored
Symbolic link
1
external/libbpf-bootstrap/examples/rust/xdp/src/bpf/vmlinux.h
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../../../vmlinux/vmlinux.h
|
||||
15
external/libbpf-bootstrap/examples/rust/xdp/src/bpf/xdppass.bpf.c
vendored
Normal file
15
external/libbpf-bootstrap/examples/rust/xdp/src/bpf/xdppass.bpf.c
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
SEC("xdp")
|
||||
int xdp_pass(struct xdp_md *ctx)
|
||||
{
|
||||
void *data = (void *)(long)ctx->data;
|
||||
void *data_end = (void *)(long)ctx->data_end;
|
||||
int pkt_sz = data_end - data;
|
||||
|
||||
bpf_printk("packet size: %d", pkt_sz);
|
||||
return XDP_PASS;
|
||||
}
|
||||
|
||||
char __license[] SEC("license") = "GPL";
|
||||
57
external/libbpf-bootstrap/examples/rust/xdp/src/main.rs
vendored
Normal file
57
external/libbpf-bootstrap/examples/rust/xdp/src/main.rs
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::{thread, time};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[path = "bpf/.output/xdppass.skel.rs"]
|
||||
mod xdppass;
|
||||
use xdppass::*;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
struct Command {
|
||||
/// Interface index to attach XDP program
|
||||
#[structopt(default_value = "0")]
|
||||
ifindex: i32,
|
||||
}
|
||||
|
||||
fn bump_memlock_rlimit() -> Result<()> {
|
||||
let rlimit = libc::rlimit {
|
||||
rlim_cur: 128 << 20,
|
||||
rlim_max: 128 << 20,
|
||||
};
|
||||
|
||||
if unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlimit) } != 0 {
|
||||
bail!("Failed to increase rlimit");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let opts = Command::from_args();
|
||||
|
||||
bump_memlock_rlimit()?;
|
||||
|
||||
let skel_builder = XdppassSkelBuilder::default();
|
||||
let open_skel = skel_builder.open()?;
|
||||
let mut skel = open_skel.load()?;
|
||||
let link = skel.progs_mut().xdp_pass().attach_xdp(opts.ifindex)?;
|
||||
skel.links = XdppassLinks {
|
||||
xdp_pass: Some(link),
|
||||
};
|
||||
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
let r = running.clone();
|
||||
ctrlc::set_handler(move || {
|
||||
r.store(false, Ordering::SeqCst);
|
||||
})?;
|
||||
|
||||
while running.load(Ordering::SeqCst) {
|
||||
eprint!(".");
|
||||
thread::sleep(time::Duration::from_secs(1));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user